blob: d64a24243da2a446091f6aa530699de8445ad862 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002// Copyright (c) 2002-2012 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.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
alokp@chromium.org91b72322010-06-02 15:50:56 +000010#include "compiler/debug.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011#include "compiler/InfoSink.h"
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000012#include "compiler/UnfoldShortCircuit.h"
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +000013#include "compiler/SearchSymbol.h"
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000014#include "compiler/DetectDiscontinuity.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015
apatrick@chromium.orgbad6c2a2010-07-20 20:19:51 +000016#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000017#include <algorithm>
18
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019namespace sh
20{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000021// Integer to TString conversion
22TString str(int i)
23{
24 char buffer[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000025 snprintf(buffer, sizeof(buffer), "%d", i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000026 return buffer;
27}
28
daniel@transgaming.com950f9932010-04-13 03:26:14 +000029OutputHLSL::OutputHLSL(TParseContext &context) : TIntermTraverser(true, true, true), mContext(context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000031 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000032 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000033
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000034 mUsesTexture2D = false;
35 mUsesTexture2D_bias = false;
36 mUsesTexture2DProj = false;
37 mUsesTexture2DProj_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000038 mUsesTexture2DProjLod = false;
39 mUsesTexture2DLod = false;
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000040 mUsesTextureCube = false;
41 mUsesTextureCube_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000042 mUsesTextureCubeLod = false;
daniel@transgaming.coma54f5182012-05-31 01:20:16 +000043 mUsesTexture2DLod0 = false;
44 mUsesTexture2DLod0_bias = false;
45 mUsesTexture2DProjLod0 = false;
46 mUsesTexture2DProjLod0_bias = false;
47 mUsesTextureCubeLod0 = false;
48 mUsesTextureCubeLod0_bias = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000049 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000050 mUsesFragCoord = false;
51 mUsesPointCoord = false;
52 mUsesFrontFacing = false;
53 mUsesPointSize = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000054 mUsesXor = false;
55 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +000056 mUsesMod2v = false;
57 mUsesMod2f = false;
58 mUsesMod3v = false;
59 mUsesMod3f = false;
60 mUsesMod4v = false;
61 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +000062 mUsesFaceforward1 = false;
63 mUsesFaceforward2 = false;
64 mUsesFaceforward3 = false;
65 mUsesFaceforward4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000066 mUsesEqualMat2 = false;
67 mUsesEqualMat3 = false;
68 mUsesEqualMat4 = false;
69 mUsesEqualVec2 = false;
70 mUsesEqualVec3 = false;
71 mUsesEqualVec4 = false;
72 mUsesEqualIVec2 = false;
73 mUsesEqualIVec3 = false;
74 mUsesEqualIVec4 = false;
75 mUsesEqualBVec2 = false;
76 mUsesEqualBVec3 = false;
77 mUsesEqualBVec4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +000078 mUsesAtan2_1 = false;
79 mUsesAtan2_2 = false;
80 mUsesAtan2_3 = false;
81 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +000082
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000083 mScopeDepth = 0;
84
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +000085 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000086
87 mContainsLoopDiscontinuity = false;
88 mOutputLod0Function = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089}
90
daniel@transgaming.comb5875982010-04-15 20:44:53 +000091OutputHLSL::~OutputHLSL()
92{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000093 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000094}
95
daniel@transgaming.com950f9932010-04-13 03:26:14 +000096void OutputHLSL::output()
97{
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000098 mContainsLoopDiscontinuity = containsLoopDiscontinuity(mContext.treeRoot);
99
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000100 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 +0000101 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000102
103 mContext.infoSink.obj << mHeader.c_str();
104 mContext.infoSink.obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000105}
106
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000107TInfoSinkBase &OutputHLSL::getBodyStream()
108{
109 return mBody;
110}
111
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000112int OutputHLSL::vectorSize(const TType &type) const
113{
114 int elementSize = type.isMatrix() ? type.getNominalSize() : 1;
115 int arraySize = type.isArray() ? type.getArraySize() : 1;
116
117 return elementSize * arraySize;
118}
119
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120void OutputHLSL::header()
121{
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000122 ShShaderType shaderType = mContext.shaderType;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000123 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000125 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000126 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000127 out << *structDeclaration;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000128 }
129
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000130 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000131 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000132 out << *constructor;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000133 }
134
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000135 if (shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 {
137 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000138 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000140 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141 int semanticIndex = 0;
142
143 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
144 {
145 const TSymbol *symbol = (*namedSymbol).second;
146 const TString &name = symbol->getName();
147
148 if (symbol->isVariable())
149 {
150 const TVariable *variable = static_cast<const TVariable*>(symbol);
151 const TType &type = variable->getType();
152 TQualifier qualifier = type.getQualifier();
153
154 if (qualifier == EvqUniform)
155 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000156 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
157 {
apatrick@chromium.org65756022012-01-17 21:45:38 +0000158 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000159 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160 }
161 else if (qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
162 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000163 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
164 {
165 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000166 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000167
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000168 semanticIndex += type.isArray() ? type.getArraySize() : 1;
169 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000171 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000172 {
173 // Globals are declared and intialized as an aggregate node
174 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000175 else if (qualifier == EvqConst)
176 {
177 // Constants are repeated as literals where used
178 }
179 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180 }
181 }
182
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000183 out << "// Varyings\n";
184 out << varyings;
185 out << "\n"
186 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n";
187
188 if (mUsesFragCoord)
189 {
190 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
191 }
192
193 if (mUsesPointCoord)
194 {
195 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
196 }
197
198 if (mUsesFrontFacing)
199 {
200 out << "static bool gl_FrontFacing = false;\n";
201 }
202
203 out << "\n";
204
205 if (mUsesFragCoord)
206 {
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +0000207 out << "uniform float4 dx_Coord;\n"
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000208 "uniform float2 dx_Depth;\n";
209 }
210
211 if (mUsesFrontFacing)
212 {
213 out << "uniform bool dx_PointsOrLines;\n"
214 "uniform bool dx_FrontCCW;\n";
215 }
216
217 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000219 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000220
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000221 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
222 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
223 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
224 // convention, the Y coordinate passed to these functions is adjusted to compensate.
225 //
226 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
227 //
228 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
229 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
230 //
231 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
232 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
233 // +Y and -Y faces everywhere else throughout the code.
234
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000235 if (mUsesTexture2D)
236 {
237 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
238 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000239 " return tex2D(s, float2(t.x, 1 - t.y));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000240 "}\n"
241 "\n";
242 }
243
244 if (mUsesTexture2D_bias)
245 {
246 out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n"
247 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000248 " return tex2Dbias(s, float4(t.x, 1 - t.y, 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000249 "}\n"
250 "\n";
251 }
252
253 if (mUsesTexture2DProj)
254 {
255 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
256 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000257 " return tex2Dproj(s, float4(t.x, t.z - t.y, 0, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000258 "}\n"
259 "\n"
260 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
261 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000262 " return tex2Dproj(s, float4(t.x, t.w - t.y, t.z, t.w));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000263 "}\n"
264 "\n";
265 }
266
267 if (mUsesTexture2DProj_bias)
268 {
269 out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n"
270 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000271 " return tex2Dbias(s, float4(t.x / t.z, 1 - (t.y / t.z), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000272 "}\n"
273 "\n"
274 "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n"
275 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000276 " return tex2Dbias(s, float4(t.x / t.w, 1 - (t.y / t.w), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000277 "}\n"
278 "\n";
279 }
280
281 if (mUsesTextureCube)
282 {
283 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
284 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000285 " return texCUBE(s, float3(t.x, -t.y, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000286 "}\n"
287 "\n";
288 }
289
290 if (mUsesTextureCube_bias)
291 {
292 out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n"
293 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000294 " return texCUBEbias(s, float4(t.x, -t.y, t.z, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000295 "}\n"
296 "\n";
297 }
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000298
299 // These *Lod0 intrinsics are not available in GL fragment shaders.
300 // They are used to sample using discontinuous texture coordinates.
301 if (mUsesTexture2DLod0)
302 {
303 out << "float4 gl_texture2DLod0(sampler2D s, float2 t)\n"
304 "{\n"
305 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
306 "}\n"
307 "\n";
308 }
309
310 if (mUsesTexture2DLod0_bias)
311 {
312 out << "float4 gl_texture2DLod0(sampler2D s, float2 t, float bias)\n"
313 "{\n"
314 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
315 "}\n"
316 "\n";
317 }
318
319 if (mUsesTexture2DProjLod0)
320 {
321 out << "float4 gl_texture2DProjLod0(sampler2D s, float3 t)\n"
322 "{\n"
323 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
324 "}\n"
325 "\n"
326 "float4 gl_texture2DProjLod(sampler2D s, float4 t)\n"
327 "{\n"
328 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
329 "}\n"
330 "\n";
331 }
332
333 if (mUsesTexture2DProjLod0_bias)
334 {
335 out << "float4 gl_texture2DProjLod0_bias(sampler2D s, float3 t, float bias)\n"
336 "{\n"
337 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
338 "}\n"
339 "\n"
340 "float4 gl_texture2DProjLod_bias(sampler2D s, float4 t, float bias)\n"
341 "{\n"
342 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
343 "}\n"
344 "\n";
345 }
346
347 if (mUsesTextureCubeLod0)
348 {
349 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t)\n"
350 "{\n"
351 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
352 "}\n"
353 "\n";
354 }
355
356 if (mUsesTextureCubeLod0_bias)
357 {
358 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t, float bias)\n"
359 "{\n"
360 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
361 "}\n"
362 "\n";
363 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000365 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366 {
367 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000368 TString attributes;
369 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000371 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372
373 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
374 {
375 const TSymbol *symbol = (*namedSymbol).second;
376 const TString &name = symbol->getName();
377
378 if (symbol->isVariable())
379 {
380 const TVariable *variable = static_cast<const TVariable*>(symbol);
381 const TType &type = variable->getType();
382 TQualifier qualifier = type.getQualifier();
383
384 if (qualifier == EvqUniform)
385 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000386 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
387 {
apatrick@chromium.org65756022012-01-17 21:45:38 +0000388 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000389 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390 }
391 else if (qualifier == EvqAttribute)
392 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000393 if (mReferencedAttributes.find(name.c_str()) != mReferencedAttributes.end())
394 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000395 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000396 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397 }
398 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
399 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000400 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
401 {
402 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000403 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000404 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000406 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407 {
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000408 // Globals are declared and intialized as an aggregate node
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000409 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000410 else if (qualifier == EvqConst)
411 {
412 // Constants are repeated as literals where used
413 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414 else UNREACHABLE();
415 }
416 }
417
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000418 out << "// Attributes\n";
419 out << attributes;
420 out << "\n"
421 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
422
423 if (mUsesPointSize)
424 {
425 out << "static float gl_PointSize = float(1);\n";
426 }
427
428 out << "\n"
429 "// Varyings\n";
430 out << varyings;
431 out << "\n"
432 "uniform float2 dx_HalfPixelSize;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000433 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000436
437 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
438 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
439 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
440 // convention, the Y coordinate passed to these functions is adjusted to compensate.
441 //
442 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
443 //
444 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
445 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
446 //
447 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
448 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
449 // +Y and -Y faces everywhere else throughout the code.
450
451 if (mUsesTexture2D)
452 {
453 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
454 "{\n"
455 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
456 "}\n"
457 "\n";
458 }
459
460 if (mUsesTexture2DLod)
461 {
462 out << "float4 gl_texture2DLod(sampler2D s, float2 t, float lod)\n"
463 "{\n"
464 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, lod));\n"
465 "}\n"
466 "\n";
467 }
468
469 if (mUsesTexture2DProj)
470 {
471 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
472 "{\n"
473 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
474 "}\n"
475 "\n"
476 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
477 "{\n"
478 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
479 "}\n"
480 "\n";
481 }
482
483 if (mUsesTexture2DProjLod)
484 {
485 out << "float4 gl_texture2DProjLod(sampler2D s, float3 t, float lod)\n"
486 "{\n"
487 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, lod));\n"
488 "}\n"
489 "\n"
490 "float4 gl_texture2DProjLod(sampler2D s, float4 t, float lod)\n"
491 "{\n"
492 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, lod));\n"
493 "}\n"
494 "\n";
495 }
496
497 if (mUsesTextureCube)
498 {
499 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
500 "{\n"
501 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
502 "}\n"
503 "\n";
504 }
505
506 if (mUsesTextureCubeLod)
507 {
508 out << "float4 gl_textureCubeLod(samplerCUBE s, float3 t, float lod)\n"
509 "{\n"
510 " return texCUBElod(s, float4(t.x, -t.y, t.z, lod));\n"
511 "}\n"
512 "\n";
513 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514 }
515
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000516 if (mUsesFragCoord)
517 {
518 out << "#define GL_USES_FRAG_COORD\n";
519 }
520
521 if (mUsesPointCoord)
522 {
523 out << "#define GL_USES_POINT_COORD\n";
524 }
525
526 if (mUsesFrontFacing)
527 {
528 out << "#define GL_USES_FRONT_FACING\n";
529 }
530
531 if (mUsesPointSize)
532 {
533 out << "#define GL_USES_POINT_SIZE\n";
534 }
535
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000536 if (mUsesDepthRange)
537 {
538 out << "struct gl_DepthRangeParameters\n"
539 "{\n"
540 " float near;\n"
541 " float far;\n"
542 " float diff;\n"
543 "};\n"
544 "\n"
daniel@transgaming.com31754962010-11-28 02:02:52 +0000545 "uniform float3 dx_DepthRange;"
546 "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000547 "\n";
548 }
549
550 if (mUsesXor)
551 {
552 out << "bool xor(bool p, bool q)\n"
553 "{\n"
554 " return (p || q) && !(p && q);\n"
555 "}\n"
556 "\n";
557 }
558
559 if (mUsesMod1)
560 {
561 out << "float mod(float x, float y)\n"
562 "{\n"
563 " return x - y * floor(x / y);\n"
564 "}\n"
565 "\n";
566 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000567
568 if (mUsesMod2v)
569 {
570 out << "float2 mod(float2 x, float2 y)\n"
571 "{\n"
572 " return x - y * floor(x / y);\n"
573 "}\n"
574 "\n";
575 }
576
577 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000578 {
579 out << "float2 mod(float2 x, float y)\n"
580 "{\n"
581 " return x - y * floor(x / y);\n"
582 "}\n"
583 "\n";
584 }
585
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000586 if (mUsesMod3v)
587 {
588 out << "float3 mod(float3 x, float3 y)\n"
589 "{\n"
590 " return x - y * floor(x / y);\n"
591 "}\n"
592 "\n";
593 }
594
595 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000596 {
597 out << "float3 mod(float3 x, float y)\n"
598 "{\n"
599 " return x - y * floor(x / y);\n"
600 "}\n"
601 "\n";
602 }
603
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000604 if (mUsesMod4v)
605 {
606 out << "float4 mod(float4 x, float4 y)\n"
607 "{\n"
608 " return x - y * floor(x / y);\n"
609 "}\n"
610 "\n";
611 }
612
613 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000614 {
615 out << "float4 mod(float4 x, float y)\n"
616 "{\n"
617 " return x - y * floor(x / y);\n"
618 "}\n"
619 "\n";
620 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000621
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000622 if (mUsesFaceforward1)
623 {
624 out << "float faceforward(float N, float I, float Nref)\n"
625 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000626 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000627 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000628 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000629 " }\n"
630 " else\n"
631 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000632 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000633 " }\n"
634 "}\n"
635 "\n";
636 }
637
638 if (mUsesFaceforward2)
639 {
640 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
641 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000642 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000643 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000644 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000645 " }\n"
646 " else\n"
647 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000648 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000649 " }\n"
650 "}\n"
651 "\n";
652 }
653
654 if (mUsesFaceforward3)
655 {
656 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
657 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000658 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000659 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000660 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000661 " }\n"
662 " else\n"
663 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000664 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000665 " }\n"
666 "}\n"
667 "\n";
668 }
669
670 if (mUsesFaceforward4)
671 {
672 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
673 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000674 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000675 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000676 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000677 " }\n"
678 " else\n"
679 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000680 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000681 " }\n"
682 "}\n"
683 "\n";
684 }
685
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000686 if (mUsesEqualMat2)
687 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000688 out << "bool equal(float2x2 m, float2x2 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000689 "{\n"
690 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
691 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
692 "}\n";
693 }
694
695 if (mUsesEqualMat3)
696 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000697 out << "bool equal(float3x3 m, float3x3 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000698 "{\n"
699 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
700 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
701 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
702 "}\n";
703 }
704
705 if (mUsesEqualMat4)
706 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000707 out << "bool equal(float4x4 m, float4x4 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000708 "{\n"
709 " 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"
710 " 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"
711 " 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"
712 " 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"
713 "}\n";
714 }
715
716 if (mUsesEqualVec2)
717 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000718 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000719 "{\n"
720 " return v.x == u.x && v.y == u.y;\n"
721 "}\n";
722 }
723
724 if (mUsesEqualVec3)
725 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000726 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000727 "{\n"
728 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
729 "}\n";
730 }
731
732 if (mUsesEqualVec4)
733 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000734 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000735 "{\n"
736 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
737 "}\n";
738 }
739
740 if (mUsesEqualIVec2)
741 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000742 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000743 "{\n"
744 " return v.x == u.x && v.y == u.y;\n"
745 "}\n";
746 }
747
748 if (mUsesEqualIVec3)
749 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000750 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000751 "{\n"
752 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
753 "}\n";
754 }
755
756 if (mUsesEqualIVec4)
757 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000758 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000759 "{\n"
760 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
761 "}\n";
762 }
763
764 if (mUsesEqualBVec2)
765 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000766 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000767 "{\n"
768 " return v.x == u.x && v.y == u.y;\n"
769 "}\n";
770 }
771
772 if (mUsesEqualBVec3)
773 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000774 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000775 "{\n"
776 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
777 "}\n";
778 }
779
780 if (mUsesEqualBVec4)
781 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000782 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000783 "{\n"
784 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
785 "}\n";
786 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +0000787
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000788 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +0000789 {
790 out << "float atanyx(float y, float x)\n"
791 "{\n"
792 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
793 " return atan2(y, x);\n"
794 "}\n";
795 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000796
797 if (mUsesAtan2_2)
798 {
799 out << "float2 atanyx(float2 y, float2 x)\n"
800 "{\n"
801 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
802 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
803 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
804 "}\n";
805 }
806
807 if (mUsesAtan2_3)
808 {
809 out << "float3 atanyx(float3 y, float3 x)\n"
810 "{\n"
811 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
812 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
813 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
814 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
815 "}\n";
816 }
817
818 if (mUsesAtan2_4)
819 {
820 out << "float4 atanyx(float4 y, float4 x)\n"
821 "{\n"
822 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
823 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
824 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
825 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
826 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
827 "}\n";
828 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829}
830
831void OutputHLSL::visitSymbol(TIntermSymbol *node)
832{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000833 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834
835 TString name = node->getSymbol();
836
837 if (name == "gl_FragColor")
838 {
839 out << "gl_Color[0]";
840 }
841 else if (name == "gl_FragData")
842 {
843 out << "gl_Color";
844 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000845 else if (name == "gl_DepthRange")
846 {
847 mUsesDepthRange = true;
848 out << name;
849 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000850 else if (name == "gl_FragCoord")
851 {
852 mUsesFragCoord = true;
853 out << name;
854 }
855 else if (name == "gl_PointCoord")
856 {
857 mUsesPointCoord = true;
858 out << name;
859 }
860 else if (name == "gl_FrontFacing")
861 {
862 mUsesFrontFacing = true;
863 out << name;
864 }
865 else if (name == "gl_PointSize")
866 {
867 mUsesPointSize = true;
868 out << name;
869 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870 else
871 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000872 TQualifier qualifier = node->getQualifier();
873
874 if (qualifier == EvqUniform)
875 {
876 mReferencedUniforms.insert(name.c_str());
apatrick@chromium.org65756022012-01-17 21:45:38 +0000877 out << decorateUniform(name, node->getType());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000878 }
879 else if (qualifier == EvqAttribute)
880 {
881 mReferencedAttributes.insert(name.c_str());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000882 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000883 }
884 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut || qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
885 {
886 mReferencedVaryings.insert(name.c_str());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000887 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000888 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000889 else
890 {
891 out << decorate(name);
892 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893 }
894}
895
896bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
897{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000898 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899
900 switch (node->getOp())
901 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000902 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000903 case EOpInitialize:
904 if (visit == PreVisit)
905 {
906 // GLSL allows to write things like "float x = x;" where a new variable x is defined
907 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
908 // new variable is created before the assignment is evaluated), so we need to convert
909 // this to "float t = x, x = t;".
910
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000911 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
912 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000913
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000914 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
915 expression->traverse(&searchSymbol);
916 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000917
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000918 if (sameSymbol)
919 {
920 // Type already printed
921 out << "t" + str(mUniqueIndex) + " = ";
922 expression->traverse(this);
923 out << ", ";
924 symbolNode->traverse(this);
925 out << " = t" + str(mUniqueIndex);
926
927 mUniqueIndex++;
928 return false;
929 }
930 }
931 else if (visit == InVisit)
932 {
933 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000934 }
935 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000936 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
937 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
938 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
939 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
940 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
941 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000942 if (visit == PreVisit)
943 {
944 out << "(";
945 }
946 else if (visit == InVisit)
947 {
948 out << " = mul(";
949 node->getLeft()->traverse(this);
950 out << ", transpose(";
951 }
952 else
953 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000954 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000955 }
956 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000957 case EOpMatrixTimesMatrixAssign:
958 if (visit == PreVisit)
959 {
960 out << "(";
961 }
962 else if (visit == InVisit)
963 {
964 out << " = mul(";
965 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000966 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000967 }
968 else
969 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000970 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000971 }
972 break;
973 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +0000974 case EOpIndexDirect: outputTriplet(visit, "", "[", "]"); break;
975 case EOpIndexIndirect: outputTriplet(visit, "", "[", "]"); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +0000976 case EOpIndexDirectStruct:
977 if (visit == InVisit)
978 {
daniel@transgaming.com2e793f02012-04-11 19:41:35 +0000979 out << "." + decorateField(node->getType().getFieldName(), node->getLeft()->getType());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +0000980
981 return false;
982 }
983 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984 case EOpVectorSwizzle:
985 if (visit == InVisit)
986 {
987 out << ".";
988
989 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
990
991 if (swizzle)
992 {
993 TIntermSequence &sequence = swizzle->getSequence();
994
995 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
996 {
997 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
998
999 if (element)
1000 {
1001 int i = element->getUnionArrayPointer()[0].getIConst();
1002
1003 switch (i)
1004 {
1005 case 0: out << "x"; break;
1006 case 1: out << "y"; break;
1007 case 2: out << "z"; break;
1008 case 3: out << "w"; break;
1009 default: UNREACHABLE();
1010 }
1011 }
1012 else UNREACHABLE();
1013 }
1014 }
1015 else UNREACHABLE();
1016
1017 return false; // Fully processed
1018 }
1019 break;
1020 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1021 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1022 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1023 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001024 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001025 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001026 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001027 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001028 if (node->getOp() == EOpEqual)
1029 {
1030 outputTriplet(visit, "(", " == ", ")");
1031 }
1032 else
1033 {
1034 outputTriplet(visit, "(", " != ", ")");
1035 }
1036 }
1037 else if (node->getLeft()->getBasicType() == EbtStruct)
1038 {
1039 if (node->getOp() == EOpEqual)
1040 {
1041 out << "(";
1042 }
1043 else
1044 {
1045 out << "!(";
1046 }
1047
1048 const TTypeList *fields = node->getLeft()->getType().getStruct();
1049
1050 for (size_t i = 0; i < fields->size(); i++)
1051 {
1052 const TType *fieldType = (*fields)[i].type;
1053
1054 node->getLeft()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001055 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType()) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001056 node->getRight()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001057 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001058
1059 if (i < fields->size() - 1)
1060 {
1061 out << " && ";
1062 }
1063 }
1064
1065 out << ")";
1066
1067 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001068 }
1069 else
1070 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001071 if (node->getLeft()->isMatrix())
1072 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001073 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001074 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001075 case 2: mUsesEqualMat2 = true; break;
1076 case 3: mUsesEqualMat3 = true; break;
1077 case 4: mUsesEqualMat4 = true; break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001078 default: UNREACHABLE();
1079 }
1080 }
1081 else if (node->getLeft()->isVector())
1082 {
1083 switch (node->getLeft()->getBasicType())
1084 {
1085 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001086 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001087 {
1088 case 2: mUsesEqualVec2 = true; break;
1089 case 3: mUsesEqualVec3 = true; break;
1090 case 4: mUsesEqualVec4 = true; break;
1091 default: UNREACHABLE();
1092 }
1093 break;
1094 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001095 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001096 {
1097 case 2: mUsesEqualIVec2 = true; break;
1098 case 3: mUsesEqualIVec3 = true; break;
1099 case 4: mUsesEqualIVec4 = true; break;
1100 default: UNREACHABLE();
1101 }
1102 break;
1103 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001104 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001105 {
1106 case 2: mUsesEqualBVec2 = true; break;
1107 case 3: mUsesEqualBVec3 = true; break;
1108 case 4: mUsesEqualBVec4 = true; break;
1109 default: UNREACHABLE();
1110 }
1111 break;
1112 default: UNREACHABLE();
1113 }
1114 }
1115 else UNREACHABLE();
1116
1117 if (node->getOp() == EOpEqual)
1118 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001119 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001120 }
1121 else
1122 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001123 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001124 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001125 }
1126 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001127 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1128 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1129 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1130 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1131 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001132 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001133 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1134 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001135 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001136 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001137 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001138 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001139 case EOpLogicalXor:
1140 mUsesXor = true;
1141 outputTriplet(visit, "xor(", ", ", ")");
1142 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001143 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001144 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001145 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146 default: UNREACHABLE();
1147 }
1148
1149 return true;
1150}
1151
1152bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1153{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001154 switch (node->getOp())
1155 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001156 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1157 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1158 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1159 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1160 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1161 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1162 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163 case EOpConvIntToBool:
1164 case EOpConvFloatToBool:
1165 switch (node->getOperand()->getType().getNominalSize())
1166 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001167 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1168 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1169 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1170 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171 default: UNREACHABLE();
1172 }
1173 break;
1174 case EOpConvBoolToFloat:
1175 case EOpConvIntToFloat:
1176 switch (node->getOperand()->getType().getNominalSize())
1177 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001178 case 1: outputTriplet(visit, "float(", "", ")"); break;
1179 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1180 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1181 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182 default: UNREACHABLE();
1183 }
1184 break;
1185 case EOpConvFloatToInt:
1186 case EOpConvBoolToInt:
1187 switch (node->getOperand()->getType().getNominalSize())
1188 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001189 case 1: outputTriplet(visit, "int(", "", ")"); break;
1190 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1191 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1192 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193 default: UNREACHABLE();
1194 }
1195 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001196 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1197 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1198 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1199 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1200 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1201 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1202 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1203 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1204 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1205 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1206 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1207 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1208 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1209 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1210 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1211 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1212 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1213 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1214 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1215 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1216 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
alokp@chromium.org06098892010-08-26 19:36:42 +00001217 case EOpDFdx: outputTriplet(visit, "ddx(", "", ")"); break;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001218 case EOpDFdy: outputTriplet(visit, "(-ddy(", "", "))"); break;
alokp@chromium.org06098892010-08-26 19:36:42 +00001219 case EOpFwidth: outputTriplet(visit, "fwidth(", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001220 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1221 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001222 default: UNREACHABLE();
1223 }
1224
1225 return true;
1226}
1227
1228bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1229{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001230 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001231
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001232 switch (node->getOp())
1233 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001234 case EOpSequence:
1235 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001236 if (mInsideFunction)
1237 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001238 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001239 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001240
1241 mScopeDepth++;
1242
1243 if (mScopeBracket.size() < mScopeDepth)
1244 {
1245 mScopeBracket.push_back(0); // New scope level
1246 }
1247 else
1248 {
1249 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1250 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001251 }
1252
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001253 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1254 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001255 outputLineDirective((*sit)->getLine());
1256
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001257 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001258
1259 out << ";\n";
1260 }
1261
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001262 if (mInsideFunction)
1263 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001264 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001265 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001266
1267 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001268 }
1269
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001270 return false;
1271 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001272 case EOpDeclaration:
1273 if (visit == PreVisit)
1274 {
1275 TIntermSequence &sequence = node->getSequence();
1276 TIntermTyped *variable = sequence[0]->getAsTyped();
1277 bool visit = true;
1278
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001279 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001280 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001281 if (variable->getType().getStruct())
1282 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001283 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001284 }
1285
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001286 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001288 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001289 {
1290 out << "static ";
1291 }
1292
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001293 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001294
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001295 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001296 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001297 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001298
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001299 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001301 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001302 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001303 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001304 }
1305 else
1306 {
1307 (*sit)->traverse(this);
1308 }
1309
1310 if (visit && this->inVisit)
1311 {
1312 if (*sit != sequence.back())
1313 {
1314 visit = this->visitAggregate(InVisit, node);
1315 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001316 }
1317 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001318
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001319 if (visit && this->postVisit)
1320 {
1321 this->visitAggregate(PostVisit, node);
1322 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001323 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001324 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1325 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001326 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001327 }
1328 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001329 }
1330
1331 return false;
1332 }
1333 else if (visit == InVisit)
1334 {
1335 out << ", ";
1336 }
1337 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001338 case EOpPrototype:
1339 if (visit == PreVisit)
1340 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001341 out << typeString(node->getType()) << " " << decorate(node->getName()) << "(";
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001342
1343 TIntermSequence &arguments = node->getSequence();
1344
1345 for (unsigned int i = 0; i < arguments.size(); i++)
1346 {
1347 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1348
1349 if (symbol)
1350 {
1351 out << argumentString(symbol);
1352
1353 if (i < arguments.size() - 1)
1354 {
1355 out << ", ";
1356 }
1357 }
1358 else UNREACHABLE();
1359 }
1360
1361 out << ");\n";
1362
1363 return false;
1364 }
1365 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001366 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001367 case EOpFunction:
1368 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001369 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001370
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001371 out << typeString(node->getType()) << " ";
1372
1373 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001375 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001377 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001378 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001379 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001380 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001381
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001382 TIntermSequence &sequence = node->getSequence();
1383 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1384
1385 for (unsigned int i = 0; i < arguments.size(); i++)
1386 {
1387 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1388
1389 if (symbol)
1390 {
1391 if (symbol->getType().getStruct())
1392 {
1393 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getTypeName()), NULL);
1394 }
1395
1396 out << argumentString(symbol);
1397
1398 if (i < arguments.size() - 1)
1399 {
1400 out << ", ";
1401 }
1402 }
1403 else UNREACHABLE();
1404 }
1405
1406 out << ")\n"
1407 "{\n";
1408
1409 if (sequence.size() > 1)
1410 {
1411 mInsideFunction = true;
1412 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001413 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001414 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001415
1416 out << "}\n";
1417
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001418 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1419 {
1420 if (name != "main" && containsGradientOperation(node))
1421 {
1422 mOutputLod0Function = true;
1423 node->traverse(this);
1424 mOutputLod0Function = false;
1425 }
1426 }
1427
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001428 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001429 }
1430 break;
1431 case EOpFunctionCall:
1432 {
1433 if (visit == PreVisit)
1434 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001435 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001436
1437 if (node->isUserDefined())
1438 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001439 out << decorate(name) << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001440 }
1441 else
1442 {
1443 if (name == "texture2D")
1444 {
1445 if (node->getSequence().size() == 2)
1446 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001447 mUsesTexture2D = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001448 }
1449 else if (node->getSequence().size() == 3)
1450 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001451 mUsesTexture2D_bias = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001452 }
1453 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001454
1455 out << "gl_texture2D(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001456 }
1457 else if (name == "texture2DProj")
1458 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001459 if (node->getSequence().size() == 2)
1460 {
1461 mUsesTexture2DProj = true;
1462 }
1463 else if (node->getSequence().size() == 3)
1464 {
1465 mUsesTexture2DProj_bias = true;
1466 }
1467 else UNREACHABLE();
1468
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001469 out << "gl_texture2DProj(";
1470 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001471 else if (name == "textureCube")
1472 {
1473 if (node->getSequence().size() == 2)
1474 {
1475 mUsesTextureCube = true;
1476 }
1477 else if (node->getSequence().size() == 3)
1478 {
1479 mUsesTextureCube_bias = true;
1480 }
1481 else UNREACHABLE();
1482
1483 out << "gl_textureCube(";
1484 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001485 else if (name == "texture2DLod")
1486 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001487 if (node->getSequence().size() == 3)
1488 {
1489 mUsesTexture2DLod = true;
1490 }
1491 else UNREACHABLE();
1492
1493 out << "gl_texture2DLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494 }
1495 else if (name == "texture2DProjLod")
1496 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001497 if (node->getSequence().size() == 3)
1498 {
1499 mUsesTexture2DProjLod = true;
1500 }
1501 else UNREACHABLE();
1502
1503 out << "gl_texture2DProjLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001504 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001505 else if (name == "textureCubeLod")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001506 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001507 if (node->getSequence().size() == 3)
1508 {
1509 mUsesTextureCubeLod = true;
1510 }
1511 else UNREACHABLE();
1512
1513 out << "gl_textureCubeLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001514 }
daniel@transgaming.comec55d292010-04-15 20:44:49 +00001515 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001516 }
1517 }
1518 else if (visit == InVisit)
1519 {
1520 out << ", ";
1521 }
1522 else
1523 {
1524 out << ")";
1525 }
1526 }
1527 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001528 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001529 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001530 addConstructor(node->getType(), "vec1", &node->getSequence());
1531 outputTriplet(visit, "vec1(", "", ")");
1532 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001533 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001534 addConstructor(node->getType(), "vec2", &node->getSequence());
1535 outputTriplet(visit, "vec2(", ", ", ")");
1536 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001537 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001538 addConstructor(node->getType(), "vec3", &node->getSequence());
1539 outputTriplet(visit, "vec3(", ", ", ")");
1540 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001541 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001542 addConstructor(node->getType(), "vec4", &node->getSequence());
1543 outputTriplet(visit, "vec4(", ", ", ")");
1544 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001545 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001546 addConstructor(node->getType(), "bvec1", &node->getSequence());
1547 outputTriplet(visit, "bvec1(", "", ")");
1548 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001549 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001550 addConstructor(node->getType(), "bvec2", &node->getSequence());
1551 outputTriplet(visit, "bvec2(", ", ", ")");
1552 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001553 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001554 addConstructor(node->getType(), "bvec3", &node->getSequence());
1555 outputTriplet(visit, "bvec3(", ", ", ")");
1556 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001557 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001558 addConstructor(node->getType(), "bvec4", &node->getSequence());
1559 outputTriplet(visit, "bvec4(", ", ", ")");
1560 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001561 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001562 addConstructor(node->getType(), "ivec1", &node->getSequence());
1563 outputTriplet(visit, "ivec1(", "", ")");
1564 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001565 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001566 addConstructor(node->getType(), "ivec2", &node->getSequence());
1567 outputTriplet(visit, "ivec2(", ", ", ")");
1568 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001569 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001570 addConstructor(node->getType(), "ivec3", &node->getSequence());
1571 outputTriplet(visit, "ivec3(", ", ", ")");
1572 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001573 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001574 addConstructor(node->getType(), "ivec4", &node->getSequence());
1575 outputTriplet(visit, "ivec4(", ", ", ")");
1576 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001577 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001578 addConstructor(node->getType(), "mat2", &node->getSequence());
1579 outputTriplet(visit, "mat2(", ", ", ")");
1580 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001581 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001582 addConstructor(node->getType(), "mat3", &node->getSequence());
1583 outputTriplet(visit, "mat3(", ", ", ")");
1584 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001585 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001586 addConstructor(node->getType(), "mat4", &node->getSequence());
1587 outputTriplet(visit, "mat4(", ", ", ")");
1588 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001589 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001590 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
1591 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001592 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001593 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1594 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1595 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1596 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1597 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
1598 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001599 case EOpMod:
1600 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001601 // We need to look at the number of components in both arguments
1602 switch (node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
1603 + node->getSequence()[1]->getAsTyped()->getNominalSize())
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001604 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001605 case 11: mUsesMod1 = true; break;
1606 case 22: mUsesMod2v = true; break;
1607 case 21: mUsesMod2f = true; break;
1608 case 33: mUsesMod3v = true; break;
1609 case 31: mUsesMod3f = true; break;
1610 case 44: mUsesMod4v = true; break;
1611 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001612 default: UNREACHABLE();
1613 }
1614
1615 outputTriplet(visit, "mod(", ", ", ")");
1616 }
1617 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001618 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001620 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001621 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
1622 {
1623 case 1: mUsesAtan2_1 = true; break;
1624 case 2: mUsesAtan2_2 = true; break;
1625 case 3: mUsesAtan2_3 = true; break;
1626 case 4: mUsesAtan2_4 = true; break;
1627 default: UNREACHABLE();
1628 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001629 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630 break;
1631 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
1632 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
1633 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
1634 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
1635 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
1636 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
1637 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
1638 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
1639 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001640 case EOpFaceForward:
1641 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001642 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001643 {
1644 case 1: mUsesFaceforward1 = true; break;
1645 case 2: mUsesFaceforward2 = true; break;
1646 case 3: mUsesFaceforward3 = true; break;
1647 case 4: mUsesFaceforward4 = true; break;
1648 default: UNREACHABLE();
1649 }
1650
1651 outputTriplet(visit, "faceforward(", ", ", ")");
1652 }
1653 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
1655 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
1656 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657 default: UNREACHABLE();
1658 }
1659
1660 return true;
1661}
1662
1663bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1664{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001665 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001667 if (node->usesTernaryOperator())
1668 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001669 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001670 }
1671 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001673 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001674
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001675 out << "if(";
1676
1677 node->getCondition()->traverse(this);
1678
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001679 out << ")\n";
1680
1681 outputLineDirective(node->getLine());
1682 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683
daniel@transgaming.combb885322010-04-15 20:45:24 +00001684 if (node->getTrueBlock())
1685 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001686 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00001687 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001689 outputLineDirective(node->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001690 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001691
1692 if (node->getFalseBlock())
1693 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001694 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001695
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001696 outputLineDirective(node->getFalseBlock()->getLine());
1697 out << "{\n";
1698
1699 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001700 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001701
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001702 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001703 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001704 }
1705 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706
1707 return false;
1708}
1709
1710void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
1711{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001712 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001713}
1714
1715bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1716{
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001717 if (handleExcessiveLoop(node))
1718 {
1719 return false;
1720 }
1721
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001722 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723
alokp@chromium.org52813552010-11-16 18:36:09 +00001724 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001725 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00001726 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001727
1728 outputLineDirective(node->getLine());
1729 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001730 }
1731 else
1732 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00001733 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734
1735 if (node->getInit())
1736 {
1737 node->getInit()->traverse(this);
1738 }
1739
1740 out << "; ";
1741
alokp@chromium.org52813552010-11-16 18:36:09 +00001742 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001744 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745 }
1746
1747 out << "; ";
1748
alokp@chromium.org52813552010-11-16 18:36:09 +00001749 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001751 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 }
1753
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001754 out << ")\n";
1755
1756 outputLineDirective(node->getLine());
1757 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 }
1759
1760 if (node->getBody())
1761 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001762 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 }
1764
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001765 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00001766 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767
alokp@chromium.org52813552010-11-16 18:36:09 +00001768 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001770 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771 out << "while(\n";
1772
alokp@chromium.org52813552010-11-16 18:36:09 +00001773 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774
daniel@transgaming.com73536982012-03-21 20:45:49 +00001775 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 }
1777
daniel@transgaming.com73536982012-03-21 20:45:49 +00001778 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001779
1780 return false;
1781}
1782
1783bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1784{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001785 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786
1787 switch (node->getFlowOp())
1788 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00001789 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
1790 case EOpBreak: outputTriplet(visit, "break;\n", "", ""); break;
1791 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792 case EOpReturn:
1793 if (visit == PreVisit)
1794 {
1795 if (node->getExpression())
1796 {
1797 out << "return ";
1798 }
1799 else
1800 {
1801 out << "return;\n";
1802 }
1803 }
1804 else if (visit == PostVisit)
1805 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001806 if (node->getExpression())
1807 {
1808 out << ";\n";
1809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810 }
1811 break;
1812 default: UNREACHABLE();
1813 }
1814
1815 return true;
1816}
1817
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001818void OutputHLSL::traverseStatements(TIntermNode *node)
1819{
1820 if (isSingleStatement(node))
1821 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001822 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001823 }
1824
1825 node->traverse(this);
1826}
1827
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001828bool OutputHLSL::isSingleStatement(TIntermNode *node)
1829{
1830 TIntermAggregate *aggregate = node->getAsAggregate();
1831
1832 if (aggregate)
1833 {
1834 if (aggregate->getOp() == EOpSequence)
1835 {
1836 return false;
1837 }
1838 else
1839 {
1840 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
1841 {
1842 if (!isSingleStatement(*sit))
1843 {
1844 return false;
1845 }
1846 }
1847
1848 return true;
1849 }
1850 }
1851
1852 return true;
1853}
1854
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001855// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
1856// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001857bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
1858{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001859 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001860 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001861
1862 // Parse loops of the form:
1863 // for(int index = initial; index [comparator] limit; index += increment)
1864 TIntermSymbol *index = NULL;
1865 TOperator comparator = EOpNull;
1866 int initial = 0;
1867 int limit = 0;
1868 int increment = 0;
1869
1870 // Parse index name and intial value
1871 if (node->getInit())
1872 {
1873 TIntermAggregate *init = node->getInit()->getAsAggregate();
1874
1875 if (init)
1876 {
1877 TIntermSequence &sequence = init->getSequence();
1878 TIntermTyped *variable = sequence[0]->getAsTyped();
1879
1880 if (variable && variable->getQualifier() == EvqTemporary)
1881 {
1882 TIntermBinary *assign = variable->getAsBinaryNode();
1883
1884 if (assign->getOp() == EOpInitialize)
1885 {
1886 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
1887 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
1888
1889 if (symbol && constant)
1890 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001891 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001892 {
1893 index = symbol;
1894 initial = constant->getUnionArrayPointer()[0].getIConst();
1895 }
1896 }
1897 }
1898 }
1899 }
1900 }
1901
1902 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00001903 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001904 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001905 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001906
1907 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
1908 {
1909 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
1910
1911 if (constant)
1912 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001913 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001914 {
1915 comparator = test->getOp();
1916 limit = constant->getUnionArrayPointer()[0].getIConst();
1917 }
1918 }
1919 }
1920 }
1921
1922 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00001923 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001924 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001925 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
1926 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001927
1928 if (binaryTerminal)
1929 {
1930 TOperator op = binaryTerminal->getOp();
1931 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
1932
1933 if (constant)
1934 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001935 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001936 {
1937 int value = constant->getUnionArrayPointer()[0].getIConst();
1938
1939 switch (op)
1940 {
1941 case EOpAddAssign: increment = value; break;
1942 case EOpSubAssign: increment = -value; break;
1943 default: UNIMPLEMENTED();
1944 }
1945 }
1946 }
1947 }
1948 else if (unaryTerminal)
1949 {
1950 TOperator op = unaryTerminal->getOp();
1951
1952 switch (op)
1953 {
1954 case EOpPostIncrement: increment = 1; break;
1955 case EOpPostDecrement: increment = -1; break;
1956 case EOpPreIncrement: increment = 1; break;
1957 case EOpPreDecrement: increment = -1; break;
1958 default: UNIMPLEMENTED();
1959 }
1960 }
1961 }
1962
1963 if (index != NULL && comparator != EOpNull && increment != 0)
1964 {
1965 if (comparator == EOpLessThanEqual)
1966 {
1967 comparator = EOpLessThan;
1968 limit += 1;
1969 }
1970
1971 if (comparator == EOpLessThan)
1972 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00001973 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001974
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001975 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001976 {
1977 return false; // Not an excessive loop
1978 }
1979
1980 while (iterations > 0)
1981 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001982 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001983
1984 // for(int index = initial; index < clampedLimit; index += increment)
1985
1986 out << "for(int ";
1987 index->traverse(this);
1988 out << " = ";
1989 out << initial;
1990
1991 out << "; ";
1992 index->traverse(this);
1993 out << " < ";
1994 out << clampedLimit;
1995
1996 out << "; ";
1997 index->traverse(this);
1998 out << " += ";
1999 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002000 out << ")\n";
2001
2002 outputLineDirective(node->getLine());
2003 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002004
2005 if (node->getBody())
2006 {
2007 node->getBody()->traverse(this);
2008 }
2009
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002010 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002011 out << ";}\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002012
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002013 initial += MAX_LOOP_ITERATIONS * increment;
2014 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002015 }
2016
2017 return true;
2018 }
2019 else UNIMPLEMENTED();
2020 }
2021
2022 return false; // Not handled as an excessive loop
2023}
2024
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002025void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002027 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002029 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030 {
2031 out << preString;
2032 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002033 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034 {
2035 out << inString;
2036 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002037 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038 {
2039 out << postString;
2040 }
2041}
2042
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002043void OutputHLSL::outputLineDirective(int line)
2044{
2045 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2046 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002047 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002048 mBody << "#line " << line;
2049
2050 if (mContext.sourcePath)
2051 {
2052 mBody << " \"" << mContext.sourcePath << "\"";
2053 }
2054
2055 mBody << "\n";
2056 }
2057}
2058
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002059TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2060{
2061 TQualifier qualifier = symbol->getQualifier();
2062 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002063 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002064
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002065 if (name.empty()) // HLSL demands named arguments, also for prototypes
2066 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002067 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002068 }
2069 else
2070 {
2071 name = decorate(name);
2072 }
2073
2074 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002075}
2076
2077TString OutputHLSL::qualifierString(TQualifier qualifier)
2078{
2079 switch(qualifier)
2080 {
2081 case EvqIn: return "in";
2082 case EvqOut: return "out";
2083 case EvqInOut: return "inout";
2084 case EvqConstReadOnly: return "const";
2085 default: UNREACHABLE();
2086 }
2087
2088 return "";
2089}
2090
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091TString OutputHLSL::typeString(const TType &type)
2092{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002093 if (type.getBasicType() == EbtStruct)
2094 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002095 if (type.getTypeName() != "")
2096 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002097 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002098 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002099 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002100 {
2101 const TTypeList &fields = *type.getStruct();
2102
2103 TString string = "struct\n"
2104 "{\n";
2105
2106 for (unsigned int i = 0; i < fields.size(); i++)
2107 {
2108 const TType &field = *fields[i].type;
2109
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002110 string += " " + typeString(field) + " " + decorate(field.getFieldName()) + arrayString(field) + ";\n";
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002111 }
2112
2113 string += "} ";
2114
2115 return string;
2116 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002117 }
2118 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119 {
2120 switch (type.getNominalSize())
2121 {
2122 case 2: return "float2x2";
2123 case 3: return "float3x3";
2124 case 4: return "float4x4";
2125 }
2126 }
2127 else
2128 {
2129 switch (type.getBasicType())
2130 {
2131 case EbtFloat:
2132 switch (type.getNominalSize())
2133 {
2134 case 1: return "float";
2135 case 2: return "float2";
2136 case 3: return "float3";
2137 case 4: return "float4";
2138 }
2139 case EbtInt:
2140 switch (type.getNominalSize())
2141 {
2142 case 1: return "int";
2143 case 2: return "int2";
2144 case 3: return "int3";
2145 case 4: return "int4";
2146 }
2147 case EbtBool:
2148 switch (type.getNominalSize())
2149 {
2150 case 1: return "bool";
2151 case 2: return "bool2";
2152 case 3: return "bool3";
2153 case 4: return "bool4";
2154 }
2155 case EbtVoid:
2156 return "void";
2157 case EbtSampler2D:
2158 return "sampler2D";
2159 case EbtSamplerCube:
2160 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002161 case EbtSamplerExternalOES:
2162 return "sampler2D";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 }
2164 }
2165
2166 UNIMPLEMENTED(); // FIXME
2167 return "<unknown type>";
2168}
2169
2170TString OutputHLSL::arrayString(const TType &type)
2171{
2172 if (!type.isArray())
2173 {
2174 return "";
2175 }
2176
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002177 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178}
2179
2180TString OutputHLSL::initializer(const TType &type)
2181{
2182 TString string;
2183
daniel@transgaming.comead23042010-04-29 03:35:36 +00002184 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002186 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187
daniel@transgaming.comead23042010-04-29 03:35:36 +00002188 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189 {
2190 string += ", ";
2191 }
2192 }
2193
daniel@transgaming.comead23042010-04-29 03:35:36 +00002194 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002195}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002196
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002197void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002198{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002199 if (name == "")
2200 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002201 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002202 }
2203
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00002204 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
2205 {
2206 return; // Already added
2207 }
2208
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002209 TType ctorType = type;
2210 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00002211 ctorType.setPrecision(EbpHigh);
2212 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00002213
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002214 TString ctorName = type.getStruct() ? decorate(name) : name;
2215
2216 typedef std::vector<TType> ParameterArray;
2217 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002218
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002219 if (type.getStruct())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002220 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002221 mStructNames.insert(decorate(name));
2222
2223 TString structure;
2224 structure += "struct " + decorate(name) + "\n"
2225 "{\n";
2226
2227 const TTypeList &fields = *type.getStruct();
2228
2229 for (unsigned int i = 0; i < fields.size(); i++)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002230 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002231 const TType &field = *fields[i].type;
2232
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002233 structure += " " + typeString(field) + " " + decorateField(field.getFieldName(), type) + arrayString(field) + ";\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002234 }
2235
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002236 structure += "};\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002237
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002238 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002239 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002240 mStructDeclarations.push_back(structure);
2241 }
2242
2243 for (unsigned int i = 0; i < fields.size(); i++)
2244 {
2245 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002246 }
2247 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002248 else if (parameters)
2249 {
2250 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
2251 {
2252 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
2253 }
2254 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002255 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00002256
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002257 TString constructor;
2258
2259 if (ctorType.getStruct())
2260 {
2261 constructor += ctorName + " " + ctorName + "_ctor(";
2262 }
2263 else // Built-in type
2264 {
2265 constructor += typeString(ctorType) + " " + ctorName + "(";
2266 }
2267
2268 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
2269 {
2270 const TType &type = ctorParameters[parameter];
2271
2272 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
2273
2274 if (parameter < ctorParameters.size() - 1)
2275 {
2276 constructor += ", ";
2277 }
2278 }
2279
2280 constructor += ")\n"
2281 "{\n";
2282
2283 if (ctorType.getStruct())
2284 {
2285 constructor += " " + ctorName + " structure = {";
2286 }
2287 else
2288 {
2289 constructor += " return " + typeString(ctorType) + "(";
2290 }
2291
2292 if (ctorType.isMatrix() && ctorParameters.size() == 1)
2293 {
2294 int dim = ctorType.getNominalSize();
2295 const TType &parameter = ctorParameters[0];
2296
2297 if (parameter.isScalar())
2298 {
2299 for (int row = 0; row < dim; row++)
2300 {
2301 for (int col = 0; col < dim; col++)
2302 {
2303 constructor += TString((row == col) ? "x0" : "0.0");
2304
2305 if (row < dim - 1 || col < dim - 1)
2306 {
2307 constructor += ", ";
2308 }
2309 }
2310 }
2311 }
2312 else if (parameter.isMatrix())
2313 {
2314 for (int row = 0; row < dim; row++)
2315 {
2316 for (int col = 0; col < dim; col++)
2317 {
2318 if (row < parameter.getNominalSize() && col < parameter.getNominalSize())
2319 {
2320 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
2321 }
2322 else
2323 {
2324 constructor += TString((row == col) ? "1.0" : "0.0");
2325 }
2326
2327 if (row < dim - 1 || col < dim - 1)
2328 {
2329 constructor += ", ";
2330 }
2331 }
2332 }
2333 }
2334 else UNREACHABLE();
2335 }
2336 else
2337 {
2338 int remainingComponents = ctorType.getObjectSize();
2339 int parameterIndex = 0;
2340
2341 while (remainingComponents > 0)
2342 {
2343 const TType &parameter = ctorParameters[parameterIndex];
2344 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
2345
2346 constructor += "x" + str(parameterIndex);
2347
2348 if (parameter.isScalar())
2349 {
2350 remainingComponents -= parameter.getObjectSize();
2351 }
2352 else if (parameter.isVector())
2353 {
2354 if (remainingComponents == parameter.getObjectSize() || moreParameters)
2355 {
2356 remainingComponents -= parameter.getObjectSize();
2357 }
2358 else if (remainingComponents < parameter.getNominalSize())
2359 {
2360 switch (remainingComponents)
2361 {
2362 case 1: constructor += ".x"; break;
2363 case 2: constructor += ".xy"; break;
2364 case 3: constructor += ".xyz"; break;
2365 case 4: constructor += ".xyzw"; break;
2366 default: UNREACHABLE();
2367 }
2368
2369 remainingComponents = 0;
2370 }
2371 else UNREACHABLE();
2372 }
2373 else if (parameter.isMatrix() || parameter.getStruct())
2374 {
2375 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
2376
2377 remainingComponents -= parameter.getObjectSize();
2378 }
2379 else UNREACHABLE();
2380
2381 if (moreParameters)
2382 {
2383 parameterIndex++;
2384 }
2385
2386 if (remainingComponents)
2387 {
2388 constructor += ", ";
2389 }
2390 }
2391 }
2392
2393 if (ctorType.getStruct())
2394 {
2395 constructor += "};\n"
2396 " return structure;\n"
2397 "}\n";
2398 }
2399 else
2400 {
2401 constructor += ");\n"
2402 "}\n";
2403 }
2404
daniel@transgaming.com63691862010-04-29 03:32:42 +00002405 mConstructors.insert(constructor);
2406}
2407
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002408const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2409{
2410 TInfoSinkBase &out = mBody;
2411
2412 if (type.getBasicType() == EbtStruct)
2413 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002414 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002415
2416 const TTypeList *structure = type.getStruct();
2417
2418 for (size_t i = 0; i < structure->size(); i++)
2419 {
2420 const TType *fieldType = (*structure)[i].type;
2421
2422 constUnion = writeConstantUnion(*fieldType, constUnion);
2423
2424 if (i != structure->size() - 1)
2425 {
2426 out << ", ";
2427 }
2428 }
2429
2430 out << ")";
2431 }
2432 else
2433 {
2434 int size = type.getObjectSize();
2435 bool writeType = size > 1;
2436
2437 if (writeType)
2438 {
2439 out << typeString(type) << "(";
2440 }
2441
2442 for (int i = 0; i < size; i++, constUnion++)
2443 {
2444 switch (constUnion->getType())
2445 {
2446 case EbtFloat: out << constUnion->getFConst(); break;
2447 case EbtInt: out << constUnion->getIConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002448 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002449 default: UNREACHABLE();
2450 }
2451
2452 if (i != size - 1)
2453 {
2454 out << ", ";
2455 }
2456 }
2457
2458 if (writeType)
2459 {
2460 out << ")";
2461 }
2462 }
2463
2464 return constUnion;
2465}
2466
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002467TString OutputHLSL::scopeString(unsigned int depthLimit)
2468{
2469 TString string;
2470
2471 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
2472 {
2473 string += "_" + str(i);
2474 }
2475
2476 return string;
2477}
2478
2479TString OutputHLSL::scopedStruct(const TString &typeName)
2480{
2481 if (typeName == "")
2482 {
2483 return typeName;
2484 }
2485
2486 return typeName + scopeString(mScopeDepth);
2487}
2488
2489TString OutputHLSL::structLookup(const TString &typeName)
2490{
2491 for (int depth = mScopeDepth; depth >= 0; depth--)
2492 {
2493 TString scopedName = decorate(typeName + scopeString(depth));
2494
2495 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
2496 {
2497 if (*structName == scopedName)
2498 {
2499 return scopedName;
2500 }
2501 }
2502 }
2503
2504 UNREACHABLE(); // Should have found a matching constructor
2505
2506 return typeName;
2507}
2508
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002509TString OutputHLSL::decorate(const TString &string)
2510{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00002511 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002512 {
2513 return "_" + string;
2514 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002515
2516 return string;
2517}
2518
apatrick@chromium.org65756022012-01-17 21:45:38 +00002519TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002520{
apatrick@chromium.org65756022012-01-17 21:45:38 +00002521 if (type.isArray())
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002522 {
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002523 return "ar_" + string; // Allows identifying arrays of size 1
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002524 }
apatrick@chromium.org65756022012-01-17 21:45:38 +00002525 else if (type.getBasicType() == EbtSamplerExternalOES)
2526 {
2527 return "ex_" + string;
2528 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002529
2530 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002531}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002532
2533TString OutputHLSL::decorateField(const TString &string, const TType &structure)
2534{
2535 if (structure.getTypeName().compare(0, 3, "gl_") != 0)
2536 {
2537 return decorate(string);
2538 }
2539
2540 return string;
2541}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542}