blob: 629a4ea2f5f16a655b2a134fbf903bda3e75f95e [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com15795192011-05-11 15:36:20 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org91b72322010-06-02 15:50:56 +00009#include "compiler/debug.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "compiler/InfoSink.h"
11#include "compiler/UnfoldSelect.h"
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +000012#include "compiler/SearchSymbol.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000013
apatrick@chromium.orgbad6c2a2010-07-20 20:19:51 +000014#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000015#include <algorithm>
16
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017namespace sh
18{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000019// Integer to TString conversion
20TString str(int i)
21{
22 char buffer[20];
23 sprintf(buffer, "%d", i);
24 return buffer;
25}
26
daniel@transgaming.com950f9932010-04-13 03:26:14 +000027OutputHLSL::OutputHLSL(TParseContext &context) : TIntermTraverser(true, true, true), mContext(context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028{
daniel@transgaming.comb5875982010-04-15 20:44:53 +000029 mUnfoldSelect = new UnfoldSelect(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000030 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000031
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000032 mUsesTexture2D = false;
33 mUsesTexture2D_bias = false;
34 mUsesTexture2DProj = false;
35 mUsesTexture2DProj_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000036 mUsesTexture2DProjLod = false;
37 mUsesTexture2DLod = false;
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000038 mUsesTextureCube = false;
39 mUsesTextureCube_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000040 mUsesTextureCubeLod = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000041 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000042 mUsesFragCoord = false;
43 mUsesPointCoord = false;
44 mUsesFrontFacing = false;
45 mUsesPointSize = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000046 mUsesXor = false;
47 mUsesMod1 = false;
48 mUsesMod2 = false;
49 mUsesMod3 = false;
50 mUsesMod4 = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +000051 mUsesFaceforward1 = false;
52 mUsesFaceforward2 = false;
53 mUsesFaceforward3 = false;
54 mUsesFaceforward4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000055 mUsesEqualMat2 = false;
56 mUsesEqualMat3 = false;
57 mUsesEqualMat4 = false;
58 mUsesEqualVec2 = false;
59 mUsesEqualVec3 = false;
60 mUsesEqualVec4 = false;
61 mUsesEqualIVec2 = false;
62 mUsesEqualIVec3 = false;
63 mUsesEqualIVec4 = false;
64 mUsesEqualBVec2 = false;
65 mUsesEqualBVec3 = false;
66 mUsesEqualBVec4 = false;
daniel@transgaming.com0f189612010-05-07 13:03:36 +000067 mUsesAtan2 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +000068
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000069 mScopeDepth = 0;
70
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +000071 mUniqueIndex = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072}
73
daniel@transgaming.comb5875982010-04-15 20:44:53 +000074OutputHLSL::~OutputHLSL()
75{
76 delete mUnfoldSelect;
77}
78
daniel@transgaming.com950f9932010-04-13 03:26:14 +000079void OutputHLSL::output()
80{
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000081 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
daniel@transgaming.com950f9932010-04-13 03:26:14 +000082 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +000083
84 mContext.infoSink.obj << mHeader.c_str();
85 mContext.infoSink.obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +000086}
87
daniel@transgaming.comb5875982010-04-15 20:44:53 +000088TInfoSinkBase &OutputHLSL::getBodyStream()
89{
90 return mBody;
91}
92
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000093int OutputHLSL::vectorSize(const TType &type) const
94{
95 int elementSize = type.isMatrix() ? type.getNominalSize() : 1;
96 int arraySize = type.isArray() ? type.getArraySize() : 1;
97
98 return elementSize * arraySize;
99}
100
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101void OutputHLSL::header()
102{
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000103 ShShaderType shaderType = mContext.shaderType;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000104 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000106 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000107 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000108 out << *structDeclaration;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000109 }
110
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000111 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000112 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000113 out << *constructor;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000114 }
115
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000116 if (shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117 {
118 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000119 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000121 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122 int semanticIndex = 0;
123
124 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
125 {
126 const TSymbol *symbol = (*namedSymbol).second;
127 const TString &name = symbol->getName();
128
129 if (symbol->isVariable())
130 {
131 const TVariable *variable = static_cast<const TVariable*>(symbol);
132 const TType &type = variable->getType();
133 TQualifier qualifier = type.getQualifier();
134
135 if (qualifier == EvqUniform)
136 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000137 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
138 {
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000139 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type.isArray()) + arrayString(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141 }
142 else if (qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
143 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000144 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
145 {
146 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000147 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000148
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000149 semanticIndex += type.isArray() ? type.getArraySize() : 1;
150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000152 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000153 {
154 // Globals are declared and intialized as an aggregate node
155 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000156 else if (qualifier == EvqConst)
157 {
158 // Constants are repeated as literals where used
159 }
160 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161 }
162 }
163
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000164 out << "// Varyings\n";
165 out << varyings;
166 out << "\n"
167 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n";
168
169 if (mUsesFragCoord)
170 {
171 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
172 }
173
174 if (mUsesPointCoord)
175 {
176 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
177 }
178
179 if (mUsesFrontFacing)
180 {
181 out << "static bool gl_FrontFacing = false;\n";
182 }
183
184 out << "\n";
185
186 if (mUsesFragCoord)
187 {
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +0000188 out << "uniform float4 dx_Viewport;\n"
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000189 "uniform float2 dx_Depth;\n";
190 }
191
192 if (mUsesFrontFacing)
193 {
194 out << "uniform bool dx_PointsOrLines;\n"
195 "uniform bool dx_FrontCCW;\n";
196 }
197
198 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000200 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000201
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000202 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
203 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
204 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
205 // convention, the Y coordinate passed to these functions is adjusted to compensate.
206 //
207 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
208 //
209 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
210 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
211 //
212 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
213 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
214 // +Y and -Y faces everywhere else throughout the code.
215
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000216 if (mUsesTexture2D)
217 {
218 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
219 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000220 " return tex2D(s, float2(t.x, 1 - t.y));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000221 "}\n"
222 "\n";
223 }
224
225 if (mUsesTexture2D_bias)
226 {
227 out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n"
228 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000229 " return tex2Dbias(s, float4(t.x, 1 - t.y, 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000230 "}\n"
231 "\n";
232 }
233
234 if (mUsesTexture2DProj)
235 {
236 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
237 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000238 " return tex2Dproj(s, float4(t.x, t.z - t.y, 0, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000239 "}\n"
240 "\n"
241 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
242 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000243 " return tex2Dproj(s, float4(t.x, t.w - t.y, t.z, t.w));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000244 "}\n"
245 "\n";
246 }
247
248 if (mUsesTexture2DProj_bias)
249 {
250 out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n"
251 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000252 " return tex2Dbias(s, float4(t.x / t.z, 1 - (t.y / t.z), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000253 "}\n"
254 "\n"
255 "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n"
256 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000257 " return tex2Dbias(s, float4(t.x / t.w, 1 - (t.y / t.w), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000258 "}\n"
259 "\n";
260 }
261
262 if (mUsesTextureCube)
263 {
264 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
265 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000266 " return texCUBE(s, float3(t.x, -t.y, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000267 "}\n"
268 "\n";
269 }
270
271 if (mUsesTextureCube_bias)
272 {
273 out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n"
274 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000275 " return texCUBEbias(s, float4(t.x, -t.y, t.z, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000276 "}\n"
277 "\n";
278 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000280 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281 {
282 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000283 TString attributes;
284 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000286 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287
288 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
289 {
290 const TSymbol *symbol = (*namedSymbol).second;
291 const TString &name = symbol->getName();
292
293 if (symbol->isVariable())
294 {
295 const TVariable *variable = static_cast<const TVariable*>(symbol);
296 const TType &type = variable->getType();
297 TQualifier qualifier = type.getQualifier();
298
299 if (qualifier == EvqUniform)
300 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000301 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
302 {
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000303 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type.isArray()) + arrayString(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000304 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 }
306 else if (qualifier == EvqAttribute)
307 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000308 if (mReferencedAttributes.find(name.c_str()) != mReferencedAttributes.end())
309 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000310 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000311 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312 }
313 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
314 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000315 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
316 {
317 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000318 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000319 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000321 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322 {
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000323 // Globals are declared and intialized as an aggregate node
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000325 else if (qualifier == EvqConst)
326 {
327 // Constants are repeated as literals where used
328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329 else UNREACHABLE();
330 }
331 }
332
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000333 out << "// Attributes\n";
334 out << attributes;
335 out << "\n"
336 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
337
338 if (mUsesPointSize)
339 {
340 out << "static float gl_PointSize = float(1);\n";
341 }
342
343 out << "\n"
344 "// Varyings\n";
345 out << varyings;
346 out << "\n"
347 "uniform float2 dx_HalfPixelSize;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000348 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000351
352 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
353 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
354 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
355 // convention, the Y coordinate passed to these functions is adjusted to compensate.
356 //
357 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
358 //
359 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
360 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
361 //
362 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
363 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
364 // +Y and -Y faces everywhere else throughout the code.
365
366 if (mUsesTexture2D)
367 {
368 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
369 "{\n"
370 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
371 "}\n"
372 "\n";
373 }
374
375 if (mUsesTexture2DLod)
376 {
377 out << "float4 gl_texture2DLod(sampler2D s, float2 t, float lod)\n"
378 "{\n"
379 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, lod));\n"
380 "}\n"
381 "\n";
382 }
383
384 if (mUsesTexture2DProj)
385 {
386 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
387 "{\n"
388 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
389 "}\n"
390 "\n"
391 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
392 "{\n"
393 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
394 "}\n"
395 "\n";
396 }
397
398 if (mUsesTexture2DProjLod)
399 {
400 out << "float4 gl_texture2DProjLod(sampler2D s, float3 t, float lod)\n"
401 "{\n"
402 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, lod));\n"
403 "}\n"
404 "\n"
405 "float4 gl_texture2DProjLod(sampler2D s, float4 t, float lod)\n"
406 "{\n"
407 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, lod));\n"
408 "}\n"
409 "\n";
410 }
411
412 if (mUsesTextureCube)
413 {
414 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
415 "{\n"
416 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
417 "}\n"
418 "\n";
419 }
420
421 if (mUsesTextureCubeLod)
422 {
423 out << "float4 gl_textureCubeLod(samplerCUBE s, float3 t, float lod)\n"
424 "{\n"
425 " return texCUBElod(s, float4(t.x, -t.y, t.z, lod));\n"
426 "}\n"
427 "\n";
428 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429 }
430
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000431 if (mUsesFragCoord)
432 {
433 out << "#define GL_USES_FRAG_COORD\n";
434 }
435
436 if (mUsesPointCoord)
437 {
438 out << "#define GL_USES_POINT_COORD\n";
439 }
440
441 if (mUsesFrontFacing)
442 {
443 out << "#define GL_USES_FRONT_FACING\n";
444 }
445
446 if (mUsesPointSize)
447 {
448 out << "#define GL_USES_POINT_SIZE\n";
449 }
450
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000451 if (mUsesDepthRange)
452 {
453 out << "struct gl_DepthRangeParameters\n"
454 "{\n"
455 " float near;\n"
456 " float far;\n"
457 " float diff;\n"
458 "};\n"
459 "\n"
daniel@transgaming.com31754962010-11-28 02:02:52 +0000460 "uniform float3 dx_DepthRange;"
461 "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000462 "\n";
463 }
464
465 if (mUsesXor)
466 {
467 out << "bool xor(bool p, bool q)\n"
468 "{\n"
469 " return (p || q) && !(p && q);\n"
470 "}\n"
471 "\n";
472 }
473
474 if (mUsesMod1)
475 {
476 out << "float mod(float x, float y)\n"
477 "{\n"
478 " return x - y * floor(x / y);\n"
479 "}\n"
480 "\n";
481 }
482
483 if (mUsesMod2)
484 {
485 out << "float2 mod(float2 x, float y)\n"
486 "{\n"
487 " return x - y * floor(x / y);\n"
488 "}\n"
489 "\n";
490 }
491
492 if (mUsesMod3)
493 {
494 out << "float3 mod(float3 x, float y)\n"
495 "{\n"
496 " return x - y * floor(x / y);\n"
497 "}\n"
498 "\n";
499 }
500
501 if (mUsesMod4)
502 {
503 out << "float4 mod(float4 x, float y)\n"
504 "{\n"
505 " return x - y * floor(x / y);\n"
506 "}\n"
507 "\n";
508 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000509
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000510 if (mUsesFaceforward1)
511 {
512 out << "float faceforward(float N, float I, float Nref)\n"
513 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000514 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000515 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000516 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000517 " }\n"
518 " else\n"
519 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000520 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000521 " }\n"
522 "}\n"
523 "\n";
524 }
525
526 if (mUsesFaceforward2)
527 {
528 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
529 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000530 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000531 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000532 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000533 " }\n"
534 " else\n"
535 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000536 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000537 " }\n"
538 "}\n"
539 "\n";
540 }
541
542 if (mUsesFaceforward3)
543 {
544 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
545 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000546 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000547 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000548 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000549 " }\n"
550 " else\n"
551 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000552 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000553 " }\n"
554 "}\n"
555 "\n";
556 }
557
558 if (mUsesFaceforward4)
559 {
560 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
561 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000562 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000563 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000564 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000565 " }\n"
566 " else\n"
567 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000568 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000569 " }\n"
570 "}\n"
571 "\n";
572 }
573
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000574 if (mUsesEqualMat2)
575 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000576 out << "bool equal(float2x2 m, float2x2 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000577 "{\n"
578 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
579 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
580 "}\n";
581 }
582
583 if (mUsesEqualMat3)
584 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000585 out << "bool equal(float3x3 m, float3x3 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000586 "{\n"
587 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
588 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
589 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
590 "}\n";
591 }
592
593 if (mUsesEqualMat4)
594 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000595 out << "bool equal(float4x4 m, float4x4 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000596 "{\n"
597 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] && m[0][3] == n[0][3] &&\n"
598 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] && m[1][3] == n[1][3] &&\n"
599 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2] && m[2][3] == n[2][3] &&\n"
600 " m[3][0] == n[3][0] && m[3][1] == n[3][1] && m[3][2] == n[3][2] && m[3][3] == n[3][3];\n"
601 "}\n";
602 }
603
604 if (mUsesEqualVec2)
605 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000606 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000607 "{\n"
608 " return v.x == u.x && v.y == u.y;\n"
609 "}\n";
610 }
611
612 if (mUsesEqualVec3)
613 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000614 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000615 "{\n"
616 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
617 "}\n";
618 }
619
620 if (mUsesEqualVec4)
621 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000622 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000623 "{\n"
624 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
625 "}\n";
626 }
627
628 if (mUsesEqualIVec2)
629 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000630 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000631 "{\n"
632 " return v.x == u.x && v.y == u.y;\n"
633 "}\n";
634 }
635
636 if (mUsesEqualIVec3)
637 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000638 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000639 "{\n"
640 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
641 "}\n";
642 }
643
644 if (mUsesEqualIVec4)
645 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000646 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000647 "{\n"
648 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
649 "}\n";
650 }
651
652 if (mUsesEqualBVec2)
653 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000654 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000655 "{\n"
656 " return v.x == u.x && v.y == u.y;\n"
657 "}\n";
658 }
659
660 if (mUsesEqualBVec3)
661 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000662 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000663 "{\n"
664 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
665 "}\n";
666 }
667
668 if (mUsesEqualBVec4)
669 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000670 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000671 "{\n"
672 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
673 "}\n";
674 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +0000675
676 if (mUsesAtan2)
677 {
678 out << "float atanyx(float y, float x)\n"
679 "{\n"
680 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
681 " return atan2(y, x);\n"
682 "}\n";
683 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684}
685
686void OutputHLSL::visitSymbol(TIntermSymbol *node)
687{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000688 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689
690 TString name = node->getSymbol();
691
692 if (name == "gl_FragColor")
693 {
694 out << "gl_Color[0]";
695 }
696 else if (name == "gl_FragData")
697 {
698 out << "gl_Color";
699 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000700 else if (name == "gl_DepthRange")
701 {
702 mUsesDepthRange = true;
703 out << name;
704 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000705 else if (name == "gl_FragCoord")
706 {
707 mUsesFragCoord = true;
708 out << name;
709 }
710 else if (name == "gl_PointCoord")
711 {
712 mUsesPointCoord = true;
713 out << name;
714 }
715 else if (name == "gl_FrontFacing")
716 {
717 mUsesFrontFacing = true;
718 out << name;
719 }
720 else if (name == "gl_PointSize")
721 {
722 mUsesPointSize = true;
723 out << name;
724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725 else
726 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000727 TQualifier qualifier = node->getQualifier();
728
729 if (qualifier == EvqUniform)
730 {
731 mReferencedUniforms.insert(name.c_str());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000732 out << decorateUniform(name, node->isArray());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000733 }
734 else if (qualifier == EvqAttribute)
735 {
736 mReferencedAttributes.insert(name.c_str());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000737 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000738 }
739 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut || qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
740 {
741 mReferencedVaryings.insert(name.c_str());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000742 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000743 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000744 else
745 {
746 out << decorate(name);
747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748 }
749}
750
751bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
752{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000753 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754
755 switch (node->getOp())
756 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000757 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000758 case EOpInitialize:
759 if (visit == PreVisit)
760 {
761 // GLSL allows to write things like "float x = x;" where a new variable x is defined
762 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
763 // new variable is created before the assignment is evaluated), so we need to convert
764 // this to "float t = x, x = t;".
765
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000766 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
767 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000768
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000769 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
770 expression->traverse(&searchSymbol);
771 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000772
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000773 if (sameSymbol)
774 {
775 // Type already printed
776 out << "t" + str(mUniqueIndex) + " = ";
777 expression->traverse(this);
778 out << ", ";
779 symbolNode->traverse(this);
780 out << " = t" + str(mUniqueIndex);
781
782 mUniqueIndex++;
783 return false;
784 }
785 }
786 else if (visit == InVisit)
787 {
788 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000789 }
790 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000791 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
792 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
793 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
794 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
795 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
796 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000797 if (visit == PreVisit)
798 {
799 out << "(";
800 }
801 else if (visit == InVisit)
802 {
803 out << " = mul(";
804 node->getLeft()->traverse(this);
805 out << ", transpose(";
806 }
807 else
808 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000809 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000810 }
811 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000812 case EOpMatrixTimesMatrixAssign:
813 if (visit == PreVisit)
814 {
815 out << "(";
816 }
817 else if (visit == InVisit)
818 {
819 out << " = mul(";
820 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000821 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000822 }
823 else
824 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000825 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000826 }
827 break;
828 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +0000829 case EOpIndexDirect: outputTriplet(visit, "", "[", "]"); break;
830 case EOpIndexIndirect: outputTriplet(visit, "", "[", "]"); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +0000831 case EOpIndexDirectStruct:
832 if (visit == InVisit)
833 {
834 out << "." + node->getType().getFieldName();
835
836 return false;
837 }
838 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839 case EOpVectorSwizzle:
840 if (visit == InVisit)
841 {
842 out << ".";
843
844 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
845
846 if (swizzle)
847 {
848 TIntermSequence &sequence = swizzle->getSequence();
849
850 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
851 {
852 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
853
854 if (element)
855 {
856 int i = element->getUnionArrayPointer()[0].getIConst();
857
858 switch (i)
859 {
860 case 0: out << "x"; break;
861 case 1: out << "y"; break;
862 case 2: out << "z"; break;
863 case 3: out << "w"; break;
864 default: UNREACHABLE();
865 }
866 }
867 else UNREACHABLE();
868 }
869 }
870 else UNREACHABLE();
871
872 return false; // Fully processed
873 }
874 break;
875 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
876 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
877 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
878 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000879 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000880 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000881 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000882 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000883 if (node->getOp() == EOpEqual)
884 {
885 outputTriplet(visit, "(", " == ", ")");
886 }
887 else
888 {
889 outputTriplet(visit, "(", " != ", ")");
890 }
891 }
892 else if (node->getLeft()->getBasicType() == EbtStruct)
893 {
894 if (node->getOp() == EOpEqual)
895 {
896 out << "(";
897 }
898 else
899 {
900 out << "!(";
901 }
902
903 const TTypeList *fields = node->getLeft()->getType().getStruct();
904
905 for (size_t i = 0; i < fields->size(); i++)
906 {
907 const TType *fieldType = (*fields)[i].type;
908
909 node->getLeft()->traverse(this);
910 out << "." + fieldType->getFieldName() + " == ";
911 node->getRight()->traverse(this);
912 out << "." + fieldType->getFieldName();
913
914 if (i < fields->size() - 1)
915 {
916 out << " && ";
917 }
918 }
919
920 out << ")";
921
922 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000923 }
924 else
925 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000926 if (node->getLeft()->isMatrix())
927 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000928 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000929 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000930 case 2: mUsesEqualMat2 = true; break;
931 case 3: mUsesEqualMat3 = true; break;
932 case 4: mUsesEqualMat4 = true; break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000933 default: UNREACHABLE();
934 }
935 }
936 else if (node->getLeft()->isVector())
937 {
938 switch (node->getLeft()->getBasicType())
939 {
940 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +0000941 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000942 {
943 case 2: mUsesEqualVec2 = true; break;
944 case 3: mUsesEqualVec3 = true; break;
945 case 4: mUsesEqualVec4 = true; break;
946 default: UNREACHABLE();
947 }
948 break;
949 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +0000950 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000951 {
952 case 2: mUsesEqualIVec2 = true; break;
953 case 3: mUsesEqualIVec3 = true; break;
954 case 4: mUsesEqualIVec4 = true; break;
955 default: UNREACHABLE();
956 }
957 break;
958 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +0000959 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000960 {
961 case 2: mUsesEqualBVec2 = true; break;
962 case 3: mUsesEqualBVec3 = true; break;
963 case 4: mUsesEqualBVec4 = true; break;
964 default: UNREACHABLE();
965 }
966 break;
967 default: UNREACHABLE();
968 }
969 }
970 else UNREACHABLE();
971
972 if (node->getOp() == EOpEqual)
973 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000974 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000975 }
976 else
977 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000978 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000979 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000980 }
981 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
983 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
984 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
985 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
986 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000987 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000988 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
989 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +0000990 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000992 case EOpLogicalXor:
993 mUsesXor = true;
994 outputTriplet(visit, "xor(", ", ", ")");
995 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
997 default: UNREACHABLE();
998 }
999
1000 return true;
1001}
1002
1003bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1004{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001005 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006
1007 switch (node->getOp())
1008 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001009 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1010 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1011 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1012 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1013 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1014 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1015 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016 case EOpConvIntToBool:
1017 case EOpConvFloatToBool:
1018 switch (node->getOperand()->getType().getNominalSize())
1019 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001020 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1021 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1022 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1023 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024 default: UNREACHABLE();
1025 }
1026 break;
1027 case EOpConvBoolToFloat:
1028 case EOpConvIntToFloat:
1029 switch (node->getOperand()->getType().getNominalSize())
1030 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001031 case 1: outputTriplet(visit, "float(", "", ")"); break;
1032 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1033 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1034 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035 default: UNREACHABLE();
1036 }
1037 break;
1038 case EOpConvFloatToInt:
1039 case EOpConvBoolToInt:
1040 switch (node->getOperand()->getType().getNominalSize())
1041 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001042 case 1: outputTriplet(visit, "int(", "", ")"); break;
1043 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1044 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1045 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046 default: UNREACHABLE();
1047 }
1048 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001049 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1050 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1051 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1052 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1053 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1054 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1055 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1056 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1057 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1058 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1059 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1060 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1061 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1062 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1063 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1064 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1065 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1066 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1067 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1068 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1069 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
alokp@chromium.org06098892010-08-26 19:36:42 +00001070 case EOpDFdx: outputTriplet(visit, "ddx(", "", ")"); break;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001071 case EOpDFdy: outputTriplet(visit, "(-ddy(", "", "))"); break;
alokp@chromium.org06098892010-08-26 19:36:42 +00001072 case EOpFwidth: outputTriplet(visit, "fwidth(", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001073 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1074 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075 default: UNREACHABLE();
1076 }
1077
1078 return true;
1079}
1080
1081bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1082{
alokp@chromium.org4888ceb2010-10-01 21:13:12 +00001083 ShShaderType shaderType = mContext.shaderType;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001084 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086 switch (node->getOp())
1087 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001088 case EOpSequence:
1089 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001090 if (mInsideFunction)
1091 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001092 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001093 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001094
1095 mScopeDepth++;
1096
1097 if (mScopeBracket.size() < mScopeDepth)
1098 {
1099 mScopeBracket.push_back(0); // New scope level
1100 }
1101 else
1102 {
1103 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1104 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001105 }
1106
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001107 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1108 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001109 outputLineDirective((*sit)->getLine());
1110
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001111 if (isSingleStatement(*sit))
1112 {
1113 mUnfoldSelect->traverse(*sit);
1114 }
1115
1116 (*sit)->traverse(this);
1117
1118 out << ";\n";
1119 }
1120
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001121 if (mInsideFunction)
1122 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001123 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001124 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001125
1126 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001127 }
1128
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001129 return false;
1130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131 case EOpDeclaration:
1132 if (visit == PreVisit)
1133 {
1134 TIntermSequence &sequence = node->getSequence();
1135 TIntermTyped *variable = sequence[0]->getAsTyped();
1136 bool visit = true;
1137
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001138 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001140 if (variable->getType().getStruct())
1141 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001142 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001143 }
1144
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001145 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001147 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001148 {
1149 out << "static ";
1150 }
1151
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001152 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001154 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001156 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001158 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001160 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001161 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001162 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001163 }
1164 else
1165 {
1166 (*sit)->traverse(this);
1167 }
1168
1169 if (visit && this->inVisit)
1170 {
1171 if (*sit != sequence.back())
1172 {
1173 visit = this->visitAggregate(InVisit, node);
1174 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001175 }
1176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001177
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001178 if (visit && this->postVisit)
1179 {
1180 this->visitAggregate(PostVisit, node);
1181 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001183 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1184 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001185 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001186 }
1187 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001188 }
1189
1190 return false;
1191 }
1192 else if (visit == InVisit)
1193 {
1194 out << ", ";
1195 }
1196 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001197 case EOpPrototype:
1198 if (visit == PreVisit)
1199 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001200 out << typeString(node->getType()) << " " << decorate(node->getName()) << "(";
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001201
1202 TIntermSequence &arguments = node->getSequence();
1203
1204 for (unsigned int i = 0; i < arguments.size(); i++)
1205 {
1206 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1207
1208 if (symbol)
1209 {
1210 out << argumentString(symbol);
1211
1212 if (i < arguments.size() - 1)
1213 {
1214 out << ", ";
1215 }
1216 }
1217 else UNREACHABLE();
1218 }
1219
1220 out << ");\n";
1221
1222 return false;
1223 }
1224 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001225 case EOpComma: outputTriplet(visit, "", ", ", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001226 case EOpFunction:
1227 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001228 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229
1230 if (visit == PreVisit)
1231 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001232 out << typeString(node->getType()) << " ";
1233
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001234 if (name == "main")
1235 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001236 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001237 }
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001238 else
1239 {
1240 out << decorate(name) << "(";
1241 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001242
1243 TIntermSequence &sequence = node->getSequence();
1244 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1245
1246 for (unsigned int i = 0; i < arguments.size(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001248 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001249
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001250 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001251 {
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001252 out << argumentString(symbol);
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001253
1254 if (i < arguments.size() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001255 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001256 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001257 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001259 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001260 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001261
1262 sequence.erase(sequence.begin());
1263
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001264 out << ")\n";
1265
1266 outputLineDirective(node->getLine());
1267 out << "{\n";
1268
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001269 mInsideFunction = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001270 }
1271 else if (visit == PostVisit)
1272 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001273 outputLineDirective(node->getEndLine());
daniel@transgaming.com63691862010-04-29 03:32:42 +00001274 out << "}\n";
1275
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001276 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001277 }
1278 }
1279 break;
1280 case EOpFunctionCall:
1281 {
1282 if (visit == PreVisit)
1283 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001284 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001285
1286 if (node->isUserDefined())
1287 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001288 out << decorate(name) << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289 }
1290 else
1291 {
1292 if (name == "texture2D")
1293 {
1294 if (node->getSequence().size() == 2)
1295 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001296 mUsesTexture2D = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001297 }
1298 else if (node->getSequence().size() == 3)
1299 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001300 mUsesTexture2D_bias = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001301 }
1302 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001303
1304 out << "gl_texture2D(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001305 }
1306 else if (name == "texture2DProj")
1307 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001308 if (node->getSequence().size() == 2)
1309 {
1310 mUsesTexture2DProj = true;
1311 }
1312 else if (node->getSequence().size() == 3)
1313 {
1314 mUsesTexture2DProj_bias = true;
1315 }
1316 else UNREACHABLE();
1317
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001318 out << "gl_texture2DProj(";
1319 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001320 else if (name == "textureCube")
1321 {
1322 if (node->getSequence().size() == 2)
1323 {
1324 mUsesTextureCube = true;
1325 }
1326 else if (node->getSequence().size() == 3)
1327 {
1328 mUsesTextureCube_bias = true;
1329 }
1330 else UNREACHABLE();
1331
1332 out << "gl_textureCube(";
1333 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334 else if (name == "texture2DLod")
1335 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001336 if (node->getSequence().size() == 3)
1337 {
1338 mUsesTexture2DLod = true;
1339 }
1340 else UNREACHABLE();
1341
1342 out << "gl_texture2DLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001343 }
1344 else if (name == "texture2DProjLod")
1345 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001346 if (node->getSequence().size() == 3)
1347 {
1348 mUsesTexture2DProjLod = true;
1349 }
1350 else UNREACHABLE();
1351
1352 out << "gl_texture2DProjLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001353 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001354 else if (name == "textureCubeLod")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001355 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001356 if (node->getSequence().size() == 3)
1357 {
1358 mUsesTextureCubeLod = true;
1359 }
1360 else UNREACHABLE();
1361
1362 out << "gl_textureCubeLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001363 }
daniel@transgaming.comec55d292010-04-15 20:44:49 +00001364 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001365 }
1366 }
1367 else if (visit == InVisit)
1368 {
1369 out << ", ";
1370 }
1371 else
1372 {
1373 out << ")";
1374 }
1375 }
1376 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001377 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001378 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001379 addConstructor(node->getType(), "vec1", &node->getSequence());
1380 outputTriplet(visit, "vec1(", "", ")");
1381 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001382 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001383 addConstructor(node->getType(), "vec2", &node->getSequence());
1384 outputTriplet(visit, "vec2(", ", ", ")");
1385 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001386 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001387 addConstructor(node->getType(), "vec3", &node->getSequence());
1388 outputTriplet(visit, "vec3(", ", ", ")");
1389 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001390 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001391 addConstructor(node->getType(), "vec4", &node->getSequence());
1392 outputTriplet(visit, "vec4(", ", ", ")");
1393 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001394 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001395 addConstructor(node->getType(), "bvec1", &node->getSequence());
1396 outputTriplet(visit, "bvec1(", "", ")");
1397 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001398 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001399 addConstructor(node->getType(), "bvec2", &node->getSequence());
1400 outputTriplet(visit, "bvec2(", ", ", ")");
1401 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001402 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001403 addConstructor(node->getType(), "bvec3", &node->getSequence());
1404 outputTriplet(visit, "bvec3(", ", ", ")");
1405 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001406 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001407 addConstructor(node->getType(), "bvec4", &node->getSequence());
1408 outputTriplet(visit, "bvec4(", ", ", ")");
1409 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001410 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001411 addConstructor(node->getType(), "ivec1", &node->getSequence());
1412 outputTriplet(visit, "ivec1(", "", ")");
1413 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001414 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001415 addConstructor(node->getType(), "ivec2", &node->getSequence());
1416 outputTriplet(visit, "ivec2(", ", ", ")");
1417 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001418 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001419 addConstructor(node->getType(), "ivec3", &node->getSequence());
1420 outputTriplet(visit, "ivec3(", ", ", ")");
1421 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001422 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001423 addConstructor(node->getType(), "ivec4", &node->getSequence());
1424 outputTriplet(visit, "ivec4(", ", ", ")");
1425 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001426 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001427 addConstructor(node->getType(), "mat2", &node->getSequence());
1428 outputTriplet(visit, "mat2(", ", ", ")");
1429 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001430 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001431 addConstructor(node->getType(), "mat3", &node->getSequence());
1432 outputTriplet(visit, "mat3(", ", ", ")");
1433 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001434 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001435 addConstructor(node->getType(), "mat4", &node->getSequence());
1436 outputTriplet(visit, "mat4(", ", ", ")");
1437 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001438 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001439 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
1440 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001441 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001442 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1443 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1444 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1445 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1446 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
1447 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001448 case EOpMod:
1449 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001450 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001451 {
1452 case 1: mUsesMod1 = true; break;
1453 case 2: mUsesMod2 = true; break;
1454 case 3: mUsesMod3 = true; break;
1455 case 4: mUsesMod4 = true; break;
1456 default: UNREACHABLE();
1457 }
1458
1459 outputTriplet(visit, "mod(", ", ", ")");
1460 }
1461 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001462 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001463 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001464 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
1465 mUsesAtan2 = true;
1466 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001467 break;
1468 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
1469 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
1470 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
1471 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
1472 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
1473 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
1474 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
1475 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
1476 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001477 case EOpFaceForward:
1478 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001479 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001480 {
1481 case 1: mUsesFaceforward1 = true; break;
1482 case 2: mUsesFaceforward2 = true; break;
1483 case 3: mUsesFaceforward3 = true; break;
1484 case 4: mUsesFaceforward4 = true; break;
1485 default: UNREACHABLE();
1486 }
1487
1488 outputTriplet(visit, "faceforward(", ", ", ")");
1489 }
1490 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001491 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
1492 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
1493 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494 default: UNREACHABLE();
1495 }
1496
1497 return true;
1498}
1499
1500bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1501{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001502 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001504 if (node->usesTernaryOperator())
1505 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001506 out << "t" << mUnfoldSelect->getTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001507 }
1508 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001509 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001510 mUnfoldSelect->traverse(node->getCondition());
1511
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001512 out << "if(";
1513
1514 node->getCondition()->traverse(this);
1515
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001516 out << ")\n";
1517
1518 outputLineDirective(node->getLine());
1519 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001520
daniel@transgaming.combb885322010-04-15 20:45:24 +00001521 if (node->getTrueBlock())
1522 {
1523 node->getTrueBlock()->traverse(this);
1524 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001525
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001526 outputLineDirective(node->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001527 out << ";}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001528
1529 if (node->getFalseBlock())
1530 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001531 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001532
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001533 outputLineDirective(node->getFalseBlock()->getLine());
1534 out << "{\n";
1535
1536 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001537 node->getFalseBlock()->traverse(this);
1538
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001539 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001540 out << ";}\n";
1541 }
1542 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001543
1544 return false;
1545}
1546
1547void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
1548{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001549 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001550}
1551
1552bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1553{
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001554 if (handleExcessiveLoop(node))
1555 {
1556 return false;
1557 }
1558
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001559 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001560
alokp@chromium.org52813552010-11-16 18:36:09 +00001561 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001562 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001563 out << "do\n";
1564
1565 outputLineDirective(node->getLine());
1566 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001567 }
1568 else
1569 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001570 if (node->getInit())
1571 {
1572 mUnfoldSelect->traverse(node->getInit());
1573 }
1574
alokp@chromium.org52813552010-11-16 18:36:09 +00001575 if (node->getCondition())
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001576 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001577 mUnfoldSelect->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001578 }
1579
alokp@chromium.org52813552010-11-16 18:36:09 +00001580 if (node->getExpression())
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001581 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001582 mUnfoldSelect->traverse(node->getExpression());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001583 }
1584
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585 out << "for(";
1586
1587 if (node->getInit())
1588 {
1589 node->getInit()->traverse(this);
1590 }
1591
1592 out << "; ";
1593
alokp@chromium.org52813552010-11-16 18:36:09 +00001594 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001596 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597 }
1598
1599 out << "; ";
1600
alokp@chromium.org52813552010-11-16 18:36:09 +00001601 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001603 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001604 }
1605
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001606 out << ")\n";
1607
1608 outputLineDirective(node->getLine());
1609 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001610 }
1611
1612 if (node->getBody())
1613 {
1614 node->getBody()->traverse(this);
1615 }
1616
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001617 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00001618 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619
alokp@chromium.org52813552010-11-16 18:36:09 +00001620 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001621 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001622 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623 out << "while(\n";
1624
alokp@chromium.org52813552010-11-16 18:36:09 +00001625 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626
1627 out << ")";
1628 }
1629
1630 out << ";\n";
1631
1632 return false;
1633}
1634
1635bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1636{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001637 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001638
1639 switch (node->getFlowOp())
1640 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00001641 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
1642 case EOpBreak: outputTriplet(visit, "break;\n", "", ""); break;
1643 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001644 case EOpReturn:
1645 if (visit == PreVisit)
1646 {
1647 if (node->getExpression())
1648 {
1649 out << "return ";
1650 }
1651 else
1652 {
1653 out << "return;\n";
1654 }
1655 }
1656 else if (visit == PostVisit)
1657 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001658 if (node->getExpression())
1659 {
1660 out << ";\n";
1661 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001662 }
1663 break;
1664 default: UNREACHABLE();
1665 }
1666
1667 return true;
1668}
1669
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001670bool OutputHLSL::isSingleStatement(TIntermNode *node)
1671{
1672 TIntermAggregate *aggregate = node->getAsAggregate();
1673
1674 if (aggregate)
1675 {
1676 if (aggregate->getOp() == EOpSequence)
1677 {
1678 return false;
1679 }
1680 else
1681 {
1682 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
1683 {
1684 if (!isSingleStatement(*sit))
1685 {
1686 return false;
1687 }
1688 }
1689
1690 return true;
1691 }
1692 }
1693
1694 return true;
1695}
1696
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001697// Handle loops with more than 255 iterations (unsupported by D3D9) by splitting them
1698bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
1699{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001700 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001701
1702 // Parse loops of the form:
1703 // for(int index = initial; index [comparator] limit; index += increment)
1704 TIntermSymbol *index = NULL;
1705 TOperator comparator = EOpNull;
1706 int initial = 0;
1707 int limit = 0;
1708 int increment = 0;
1709
1710 // Parse index name and intial value
1711 if (node->getInit())
1712 {
1713 TIntermAggregate *init = node->getInit()->getAsAggregate();
1714
1715 if (init)
1716 {
1717 TIntermSequence &sequence = init->getSequence();
1718 TIntermTyped *variable = sequence[0]->getAsTyped();
1719
1720 if (variable && variable->getQualifier() == EvqTemporary)
1721 {
1722 TIntermBinary *assign = variable->getAsBinaryNode();
1723
1724 if (assign->getOp() == EOpInitialize)
1725 {
1726 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
1727 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
1728
1729 if (symbol && constant)
1730 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001731 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001732 {
1733 index = symbol;
1734 initial = constant->getUnionArrayPointer()[0].getIConst();
1735 }
1736 }
1737 }
1738 }
1739 }
1740 }
1741
1742 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00001743 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001744 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001745 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001746
1747 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
1748 {
1749 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
1750
1751 if (constant)
1752 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001753 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001754 {
1755 comparator = test->getOp();
1756 limit = constant->getUnionArrayPointer()[0].getIConst();
1757 }
1758 }
1759 }
1760 }
1761
1762 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00001763 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001764 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001765 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
1766 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001767
1768 if (binaryTerminal)
1769 {
1770 TOperator op = binaryTerminal->getOp();
1771 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
1772
1773 if (constant)
1774 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001775 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001776 {
1777 int value = constant->getUnionArrayPointer()[0].getIConst();
1778
1779 switch (op)
1780 {
1781 case EOpAddAssign: increment = value; break;
1782 case EOpSubAssign: increment = -value; break;
1783 default: UNIMPLEMENTED();
1784 }
1785 }
1786 }
1787 }
1788 else if (unaryTerminal)
1789 {
1790 TOperator op = unaryTerminal->getOp();
1791
1792 switch (op)
1793 {
1794 case EOpPostIncrement: increment = 1; break;
1795 case EOpPostDecrement: increment = -1; break;
1796 case EOpPreIncrement: increment = 1; break;
1797 case EOpPreDecrement: increment = -1; break;
1798 default: UNIMPLEMENTED();
1799 }
1800 }
1801 }
1802
1803 if (index != NULL && comparator != EOpNull && increment != 0)
1804 {
1805 if (comparator == EOpLessThanEqual)
1806 {
1807 comparator = EOpLessThan;
1808 limit += 1;
1809 }
1810
1811 if (comparator == EOpLessThan)
1812 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00001813 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001814
1815 if (iterations <= 255)
1816 {
1817 return false; // Not an excessive loop
1818 }
1819
1820 while (iterations > 0)
1821 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00001822 int remainder = (limit - initial) % increment;
1823 int clampedLimit = initial + increment * std::min(255, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001824
1825 // for(int index = initial; index < clampedLimit; index += increment)
1826
1827 out << "for(int ";
1828 index->traverse(this);
1829 out << " = ";
1830 out << initial;
1831
1832 out << "; ";
1833 index->traverse(this);
1834 out << " < ";
1835 out << clampedLimit;
1836
1837 out << "; ";
1838 index->traverse(this);
1839 out << " += ";
1840 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001841 out << ")\n";
1842
1843 outputLineDirective(node->getLine());
1844 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001845
1846 if (node->getBody())
1847 {
1848 node->getBody()->traverse(this);
1849 }
1850
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001851 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00001852 out << ";}\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001853
1854 initial += 255 * increment;
1855 iterations -= 255;
1856 }
1857
1858 return true;
1859 }
1860 else UNIMPLEMENTED();
1861 }
1862
1863 return false; // Not handled as an excessive loop
1864}
1865
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001866void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001867{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001868 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001870 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871 {
1872 out << preString;
1873 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001874 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875 {
1876 out << inString;
1877 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001878 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 {
1880 out << postString;
1881 }
1882}
1883
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001884void OutputHLSL::outputLineDirective(int line)
1885{
1886 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
1887 {
baustin@google.com8ab69842011-06-02 21:53:45 +00001888 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001889 mBody << "#line " << line;
1890
1891 if (mContext.sourcePath)
1892 {
1893 mBody << " \"" << mContext.sourcePath << "\"";
1894 }
1895
1896 mBody << "\n";
1897 }
1898}
1899
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001900TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
1901{
1902 TQualifier qualifier = symbol->getQualifier();
1903 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00001904 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001905
daniel@transgaming.com005c7392010-04-15 20:45:27 +00001906 if (name.empty()) // HLSL demands named arguments, also for prototypes
1907 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001908 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00001909 }
1910 else
1911 {
1912 name = decorate(name);
1913 }
1914
1915 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001916}
1917
1918TString OutputHLSL::qualifierString(TQualifier qualifier)
1919{
1920 switch(qualifier)
1921 {
1922 case EvqIn: return "in";
1923 case EvqOut: return "out";
1924 case EvqInOut: return "inout";
1925 case EvqConstReadOnly: return "const";
1926 default: UNREACHABLE();
1927 }
1928
1929 return "";
1930}
1931
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932TString OutputHLSL::typeString(const TType &type)
1933{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001934 if (type.getBasicType() == EbtStruct)
1935 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00001936 if (type.getTypeName() != "")
1937 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001938 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00001939 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00001940 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00001941 {
1942 const TTypeList &fields = *type.getStruct();
1943
1944 TString string = "struct\n"
1945 "{\n";
1946
1947 for (unsigned int i = 0; i < fields.size(); i++)
1948 {
1949 const TType &field = *fields[i].type;
1950
1951 string += " " + typeString(field) + " " + field.getFieldName() + arrayString(field) + ";\n";
1952 }
1953
1954 string += "} ";
1955
1956 return string;
1957 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001958 }
1959 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960 {
1961 switch (type.getNominalSize())
1962 {
1963 case 2: return "float2x2";
1964 case 3: return "float3x3";
1965 case 4: return "float4x4";
1966 }
1967 }
1968 else
1969 {
1970 switch (type.getBasicType())
1971 {
1972 case EbtFloat:
1973 switch (type.getNominalSize())
1974 {
1975 case 1: return "float";
1976 case 2: return "float2";
1977 case 3: return "float3";
1978 case 4: return "float4";
1979 }
1980 case EbtInt:
1981 switch (type.getNominalSize())
1982 {
1983 case 1: return "int";
1984 case 2: return "int2";
1985 case 3: return "int3";
1986 case 4: return "int4";
1987 }
1988 case EbtBool:
1989 switch (type.getNominalSize())
1990 {
1991 case 1: return "bool";
1992 case 2: return "bool2";
1993 case 3: return "bool3";
1994 case 4: return "bool4";
1995 }
1996 case EbtVoid:
1997 return "void";
1998 case EbtSampler2D:
1999 return "sampler2D";
2000 case EbtSamplerCube:
2001 return "samplerCUBE";
2002 }
2003 }
2004
2005 UNIMPLEMENTED(); // FIXME
2006 return "<unknown type>";
2007}
2008
2009TString OutputHLSL::arrayString(const TType &type)
2010{
2011 if (!type.isArray())
2012 {
2013 return "";
2014 }
2015
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002016 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017}
2018
2019TString OutputHLSL::initializer(const TType &type)
2020{
2021 TString string;
2022
daniel@transgaming.comead23042010-04-29 03:35:36 +00002023 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002025 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026
daniel@transgaming.comead23042010-04-29 03:35:36 +00002027 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028 {
2029 string += ", ";
2030 }
2031 }
2032
daniel@transgaming.comead23042010-04-29 03:35:36 +00002033 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002035
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002036void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002037{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002038 if (name == "")
2039 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002040 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002041 }
2042
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002043 TType ctorType = type;
2044 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00002045 ctorType.setPrecision(EbpHigh);
2046 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00002047
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002048 TString ctorName = type.getStruct() ? decorate(name) : name;
2049
2050 typedef std::vector<TType> ParameterArray;
2051 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002052
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002053 if (parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002054 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002055 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
2056 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002057 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002058 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002059 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002060 else if (type.getStruct())
2061 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002062 mStructNames.insert(decorate(name));
2063
2064 TString structure;
2065 structure += "struct " + decorate(name) + "\n"
2066 "{\n";
2067
2068 const TTypeList &fields = *type.getStruct();
2069
2070 for (unsigned int i = 0; i < fields.size(); i++)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002071 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002072 const TType &field = *fields[i].type;
2073
2074 structure += " " + typeString(field) + " " + field.getFieldName() + arrayString(field) + ";\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002075 }
2076
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002077 structure += "};\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002078
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002079 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002080 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002081 mStructDeclarations.push_back(structure);
2082 }
2083
2084 for (unsigned int i = 0; i < fields.size(); i++)
2085 {
2086 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002087 }
2088 }
2089 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00002090
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002091 TString constructor;
2092
2093 if (ctorType.getStruct())
2094 {
2095 constructor += ctorName + " " + ctorName + "_ctor(";
2096 }
2097 else // Built-in type
2098 {
2099 constructor += typeString(ctorType) + " " + ctorName + "(";
2100 }
2101
2102 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
2103 {
2104 const TType &type = ctorParameters[parameter];
2105
2106 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
2107
2108 if (parameter < ctorParameters.size() - 1)
2109 {
2110 constructor += ", ";
2111 }
2112 }
2113
2114 constructor += ")\n"
2115 "{\n";
2116
2117 if (ctorType.getStruct())
2118 {
2119 constructor += " " + ctorName + " structure = {";
2120 }
2121 else
2122 {
2123 constructor += " return " + typeString(ctorType) + "(";
2124 }
2125
2126 if (ctorType.isMatrix() && ctorParameters.size() == 1)
2127 {
2128 int dim = ctorType.getNominalSize();
2129 const TType &parameter = ctorParameters[0];
2130
2131 if (parameter.isScalar())
2132 {
2133 for (int row = 0; row < dim; row++)
2134 {
2135 for (int col = 0; col < dim; col++)
2136 {
2137 constructor += TString((row == col) ? "x0" : "0.0");
2138
2139 if (row < dim - 1 || col < dim - 1)
2140 {
2141 constructor += ", ";
2142 }
2143 }
2144 }
2145 }
2146 else if (parameter.isMatrix())
2147 {
2148 for (int row = 0; row < dim; row++)
2149 {
2150 for (int col = 0; col < dim; col++)
2151 {
2152 if (row < parameter.getNominalSize() && col < parameter.getNominalSize())
2153 {
2154 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
2155 }
2156 else
2157 {
2158 constructor += TString((row == col) ? "1.0" : "0.0");
2159 }
2160
2161 if (row < dim - 1 || col < dim - 1)
2162 {
2163 constructor += ", ";
2164 }
2165 }
2166 }
2167 }
2168 else UNREACHABLE();
2169 }
2170 else
2171 {
2172 int remainingComponents = ctorType.getObjectSize();
2173 int parameterIndex = 0;
2174
2175 while (remainingComponents > 0)
2176 {
2177 const TType &parameter = ctorParameters[parameterIndex];
2178 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
2179
2180 constructor += "x" + str(parameterIndex);
2181
2182 if (parameter.isScalar())
2183 {
2184 remainingComponents -= parameter.getObjectSize();
2185 }
2186 else if (parameter.isVector())
2187 {
2188 if (remainingComponents == parameter.getObjectSize() || moreParameters)
2189 {
2190 remainingComponents -= parameter.getObjectSize();
2191 }
2192 else if (remainingComponents < parameter.getNominalSize())
2193 {
2194 switch (remainingComponents)
2195 {
2196 case 1: constructor += ".x"; break;
2197 case 2: constructor += ".xy"; break;
2198 case 3: constructor += ".xyz"; break;
2199 case 4: constructor += ".xyzw"; break;
2200 default: UNREACHABLE();
2201 }
2202
2203 remainingComponents = 0;
2204 }
2205 else UNREACHABLE();
2206 }
2207 else if (parameter.isMatrix() || parameter.getStruct())
2208 {
2209 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
2210
2211 remainingComponents -= parameter.getObjectSize();
2212 }
2213 else UNREACHABLE();
2214
2215 if (moreParameters)
2216 {
2217 parameterIndex++;
2218 }
2219
2220 if (remainingComponents)
2221 {
2222 constructor += ", ";
2223 }
2224 }
2225 }
2226
2227 if (ctorType.getStruct())
2228 {
2229 constructor += "};\n"
2230 " return structure;\n"
2231 "}\n";
2232 }
2233 else
2234 {
2235 constructor += ");\n"
2236 "}\n";
2237 }
2238
daniel@transgaming.com63691862010-04-29 03:32:42 +00002239 mConstructors.insert(constructor);
2240}
2241
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002242const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2243{
2244 TInfoSinkBase &out = mBody;
2245
2246 if (type.getBasicType() == EbtStruct)
2247 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002248 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002249
2250 const TTypeList *structure = type.getStruct();
2251
2252 for (size_t i = 0; i < structure->size(); i++)
2253 {
2254 const TType *fieldType = (*structure)[i].type;
2255
2256 constUnion = writeConstantUnion(*fieldType, constUnion);
2257
2258 if (i != structure->size() - 1)
2259 {
2260 out << ", ";
2261 }
2262 }
2263
2264 out << ")";
2265 }
2266 else
2267 {
2268 int size = type.getObjectSize();
2269 bool writeType = size > 1;
2270
2271 if (writeType)
2272 {
2273 out << typeString(type) << "(";
2274 }
2275
2276 for (int i = 0; i < size; i++, constUnion++)
2277 {
2278 switch (constUnion->getType())
2279 {
2280 case EbtFloat: out << constUnion->getFConst(); break;
2281 case EbtInt: out << constUnion->getIConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002282 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002283 default: UNREACHABLE();
2284 }
2285
2286 if (i != size - 1)
2287 {
2288 out << ", ";
2289 }
2290 }
2291
2292 if (writeType)
2293 {
2294 out << ")";
2295 }
2296 }
2297
2298 return constUnion;
2299}
2300
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002301TString OutputHLSL::scopeString(unsigned int depthLimit)
2302{
2303 TString string;
2304
2305 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
2306 {
2307 string += "_" + str(i);
2308 }
2309
2310 return string;
2311}
2312
2313TString OutputHLSL::scopedStruct(const TString &typeName)
2314{
2315 if (typeName == "")
2316 {
2317 return typeName;
2318 }
2319
2320 return typeName + scopeString(mScopeDepth);
2321}
2322
2323TString OutputHLSL::structLookup(const TString &typeName)
2324{
2325 for (int depth = mScopeDepth; depth >= 0; depth--)
2326 {
2327 TString scopedName = decorate(typeName + scopeString(depth));
2328
2329 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
2330 {
2331 if (*structName == scopedName)
2332 {
2333 return scopedName;
2334 }
2335 }
2336 }
2337
2338 UNREACHABLE(); // Should have found a matching constructor
2339
2340 return typeName;
2341}
2342
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002343TString OutputHLSL::decorate(const TString &string)
2344{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00002345 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002346 {
2347 return "_" + string;
2348 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002349
2350 return string;
2351}
2352
2353TString OutputHLSL::decorateUniform(const TString &string, bool array)
2354{
2355 if (array)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002356 {
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002357 return "ar_" + string; // Allows identifying arrays of size 1
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002358 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002359
2360 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002361}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362}