blob: c194bb16c7def0e2f1738f8cda0c9192cc9abbc9 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com15795192011-05-11 15:36:20 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org91b72322010-06-02 15:50:56 +00009#include "compiler/debug.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "compiler/InfoSink.h"
11#include "compiler/UnfoldSelect.h"
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +000012#include "compiler/SearchSymbol.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000013
apatrick@chromium.orgbad6c2a2010-07-20 20:19:51 +000014#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000015#include <algorithm>
16
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017namespace sh
18{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000019// Integer to TString conversion
20TString str(int i)
21{
22 char buffer[20];
23 sprintf(buffer, "%d", i);
24 return buffer;
25}
26
daniel@transgaming.com950f9932010-04-13 03:26:14 +000027OutputHLSL::OutputHLSL(TParseContext &context) : TIntermTraverser(true, true, true), mContext(context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028{
daniel@transgaming.comb5875982010-04-15 20:44:53 +000029 mUnfoldSelect = new UnfoldSelect(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000030 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000031
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000032 mUsesTexture2D = false;
33 mUsesTexture2D_bias = false;
34 mUsesTexture2DProj = false;
35 mUsesTexture2DProj_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000036 mUsesTexture2DProjLod = false;
37 mUsesTexture2DLod = false;
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000038 mUsesTextureCube = false;
39 mUsesTextureCube_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000040 mUsesTextureCubeLod = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000041 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000042 mUsesFragCoord = false;
43 mUsesPointCoord = false;
44 mUsesFrontFacing = false;
45 mUsesPointSize = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000046 mUsesXor = false;
47 mUsesMod1 = false;
48 mUsesMod2 = false;
49 mUsesMod3 = false;
50 mUsesMod4 = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +000051 mUsesFaceforward1 = false;
52 mUsesFaceforward2 = false;
53 mUsesFaceforward3 = false;
54 mUsesFaceforward4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000055 mUsesEqualMat2 = false;
56 mUsesEqualMat3 = false;
57 mUsesEqualMat4 = false;
58 mUsesEqualVec2 = false;
59 mUsesEqualVec3 = false;
60 mUsesEqualVec4 = false;
61 mUsesEqualIVec2 = false;
62 mUsesEqualIVec3 = false;
63 mUsesEqualIVec4 = false;
64 mUsesEqualBVec2 = false;
65 mUsesEqualBVec3 = false;
66 mUsesEqualBVec4 = false;
daniel@transgaming.com0f189612010-05-07 13:03:36 +000067 mUsesAtan2 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +000068
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000069 mScopeDepth = 0;
70
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +000071 mUniqueIndex = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072}
73
daniel@transgaming.comb5875982010-04-15 20:44:53 +000074OutputHLSL::~OutputHLSL()
75{
76 delete mUnfoldSelect;
77}
78
daniel@transgaming.com950f9932010-04-13 03:26:14 +000079void OutputHLSL::output()
80{
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000081 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
daniel@transgaming.com950f9932010-04-13 03:26:14 +000082 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +000083
84 mContext.infoSink.obj << mHeader.c_str();
85 mContext.infoSink.obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +000086}
87
daniel@transgaming.comb5875982010-04-15 20:44:53 +000088TInfoSinkBase &OutputHLSL::getBodyStream()
89{
90 return mBody;
91}
92
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +000093int OutputHLSL::vectorSize(const TType &type) const
94{
95 int elementSize = type.isMatrix() ? type.getNominalSize() : 1;
96 int arraySize = type.isArray() ? type.getArraySize() : 1;
97
98 return elementSize * arraySize;
99}
100
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101void OutputHLSL::header()
102{
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000103 ShShaderType shaderType = mContext.shaderType;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000104 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000106 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000107 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000108 out << *structDeclaration;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000109 }
110
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000111 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000112 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000113 out << *constructor;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000114 }
115
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000116 if (shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117 {
118 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000119 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000121 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122 int semanticIndex = 0;
123
124 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
125 {
126 const TSymbol *symbol = (*namedSymbol).second;
127 const TString &name = symbol->getName();
128
129 if (symbol->isVariable())
130 {
131 const TVariable *variable = static_cast<const TVariable*>(symbol);
132 const TType &type = variable->getType();
133 TQualifier qualifier = type.getQualifier();
134
135 if (qualifier == EvqUniform)
136 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000137 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
138 {
139 uniforms += "uniform " + typeString(type) + " " + decorate(name) + arrayString(type) + ";\n";
140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141 }
142 else if (qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
143 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000144 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
145 {
146 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000147 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000148
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000149 semanticIndex += type.isArray() ? type.getArraySize() : 1;
150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000152 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000153 {
154 // Globals are declared and intialized as an aggregate node
155 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000156 else if (qualifier == EvqConst)
157 {
158 // Constants are repeated as literals where used
159 }
160 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161 }
162 }
163
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000164 out << "// Varyings\n";
165 out << varyings;
166 out << "\n"
167 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n";
168
169 if (mUsesFragCoord)
170 {
171 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
172 }
173
174 if (mUsesPointCoord)
175 {
176 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
177 }
178
179 if (mUsesFrontFacing)
180 {
181 out << "static bool gl_FrontFacing = false;\n";
182 }
183
184 out << "\n";
185
186 if (mUsesFragCoord)
187 {
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +0000188 out << "uniform float4 dx_Viewport;\n"
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000189 "uniform float2 dx_Depth;\n";
190 }
191
192 if (mUsesFrontFacing)
193 {
194 out << "uniform bool dx_PointsOrLines;\n"
195 "uniform bool dx_FrontCCW;\n";
196 }
197
198 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000200 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000201
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000202 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
203 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
204 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
205 // convention, the Y coordinate passed to these functions is adjusted to compensate.
206 //
207 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
208 //
209 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
210 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
211 //
212 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
213 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
214 // +Y and -Y faces everywhere else throughout the code.
215
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000216 if (mUsesTexture2D)
217 {
218 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
219 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000220 " return tex2D(s, float2(t.x, 1 - t.y));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000221 "}\n"
222 "\n";
223 }
224
225 if (mUsesTexture2D_bias)
226 {
227 out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n"
228 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000229 " return tex2Dbias(s, float4(t.x, 1 - t.y, 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000230 "}\n"
231 "\n";
232 }
233
234 if (mUsesTexture2DProj)
235 {
236 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
237 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000238 " return tex2Dproj(s, float4(t.x, t.z - t.y, 0, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000239 "}\n"
240 "\n"
241 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
242 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000243 " return tex2Dproj(s, float4(t.x, t.w - t.y, t.z, t.w));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000244 "}\n"
245 "\n";
246 }
247
248 if (mUsesTexture2DProj_bias)
249 {
250 out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n"
251 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000252 " return tex2Dbias(s, float4(t.x / t.z, 1 - (t.y / t.z), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000253 "}\n"
254 "\n"
255 "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n"
256 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000257 " return tex2Dbias(s, float4(t.x / t.w, 1 - (t.y / t.w), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000258 "}\n"
259 "\n";
260 }
261
262 if (mUsesTextureCube)
263 {
264 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
265 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000266 " return texCUBE(s, float3(t.x, -t.y, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000267 "}\n"
268 "\n";
269 }
270
271 if (mUsesTextureCube_bias)
272 {
273 out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n"
274 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000275 " return texCUBEbias(s, float4(t.x, -t.y, t.z, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000276 "}\n"
277 "\n";
278 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000280 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281 {
282 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000283 TString attributes;
284 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000286 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287
288 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
289 {
290 const TSymbol *symbol = (*namedSymbol).second;
291 const TString &name = symbol->getName();
292
293 if (symbol->isVariable())
294 {
295 const TVariable *variable = static_cast<const TVariable*>(symbol);
296 const TType &type = variable->getType();
297 TQualifier qualifier = type.getQualifier();
298
299 if (qualifier == EvqUniform)
300 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000301 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
302 {
303 uniforms += "uniform " + typeString(type) + " " + decorate(name) + arrayString(type) + ";\n";
304 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 }
306 else if (qualifier == EvqAttribute)
307 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000308 if (mReferencedAttributes.find(name.c_str()) != mReferencedAttributes.end())
309 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000310 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000311 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312 }
313 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
314 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000315 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
316 {
317 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000318 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000319 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000321 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322 {
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000323 // Globals are declared and intialized as an aggregate node
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000325 else if (qualifier == EvqConst)
326 {
327 // Constants are repeated as literals where used
328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329 else UNREACHABLE();
330 }
331 }
332
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000333 out << "// Attributes\n";
334 out << attributes;
335 out << "\n"
336 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
337
338 if (mUsesPointSize)
339 {
340 out << "static float gl_PointSize = float(1);\n";
341 }
342
343 out << "\n"
344 "// Varyings\n";
345 out << varyings;
346 out << "\n"
347 "uniform float2 dx_HalfPixelSize;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000348 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000351
352 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
353 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
354 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
355 // convention, the Y coordinate passed to these functions is adjusted to compensate.
356 //
357 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
358 //
359 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
360 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
361 //
362 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
363 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
364 // +Y and -Y faces everywhere else throughout the code.
365
366 if (mUsesTexture2D)
367 {
368 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
369 "{\n"
370 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
371 "}\n"
372 "\n";
373 }
374
375 if (mUsesTexture2DLod)
376 {
377 out << "float4 gl_texture2DLod(sampler2D s, float2 t, float lod)\n"
378 "{\n"
379 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, lod));\n"
380 "}\n"
381 "\n";
382 }
383
384 if (mUsesTexture2DProj)
385 {
386 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
387 "{\n"
388 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
389 "}\n"
390 "\n"
391 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
392 "{\n"
393 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
394 "}\n"
395 "\n";
396 }
397
398 if (mUsesTexture2DProjLod)
399 {
400 out << "float4 gl_texture2DProjLod(sampler2D s, float3 t, float lod)\n"
401 "{\n"
402 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, lod));\n"
403 "}\n"
404 "\n"
405 "float4 gl_texture2DProjLod(sampler2D s, float4 t, float lod)\n"
406 "{\n"
407 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, lod));\n"
408 "}\n"
409 "\n";
410 }
411
412 if (mUsesTextureCube)
413 {
414 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
415 "{\n"
416 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
417 "}\n"
418 "\n";
419 }
420
421 if (mUsesTextureCubeLod)
422 {
423 out << "float4 gl_textureCubeLod(samplerCUBE s, float3 t, float lod)\n"
424 "{\n"
425 " return texCUBElod(s, float4(t.x, -t.y, t.z, lod));\n"
426 "}\n"
427 "\n";
428 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429 }
430
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000431 if (mUsesFragCoord)
432 {
433 out << "#define GL_USES_FRAG_COORD\n";
434 }
435
436 if (mUsesPointCoord)
437 {
438 out << "#define GL_USES_POINT_COORD\n";
439 }
440
441 if (mUsesFrontFacing)
442 {
443 out << "#define GL_USES_FRONT_FACING\n";
444 }
445
446 if (mUsesPointSize)
447 {
448 out << "#define GL_USES_POINT_SIZE\n";
449 }
450
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000451 if (mUsesDepthRange)
452 {
453 out << "struct gl_DepthRangeParameters\n"
454 "{\n"
455 " float near;\n"
456 " float far;\n"
457 " float diff;\n"
458 "};\n"
459 "\n"
daniel@transgaming.com31754962010-11-28 02:02:52 +0000460 "uniform float3 dx_DepthRange;"
461 "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000462 "\n";
463 }
464
465 if (mUsesXor)
466 {
467 out << "bool xor(bool p, bool q)\n"
468 "{\n"
469 " return (p || q) && !(p && q);\n"
470 "}\n"
471 "\n";
472 }
473
474 if (mUsesMod1)
475 {
476 out << "float mod(float x, float y)\n"
477 "{\n"
478 " return x - y * floor(x / y);\n"
479 "}\n"
480 "\n";
481 }
482
483 if (mUsesMod2)
484 {
485 out << "float2 mod(float2 x, float y)\n"
486 "{\n"
487 " return x - y * floor(x / y);\n"
488 "}\n"
489 "\n";
490 }
491
492 if (mUsesMod3)
493 {
494 out << "float3 mod(float3 x, float y)\n"
495 "{\n"
496 " return x - y * floor(x / y);\n"
497 "}\n"
498 "\n";
499 }
500
501 if (mUsesMod4)
502 {
503 out << "float4 mod(float4 x, float y)\n"
504 "{\n"
505 " return x - y * floor(x / y);\n"
506 "}\n"
507 "\n";
508 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000509
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000510 if (mUsesFaceforward1)
511 {
512 out << "float faceforward(float N, float I, float Nref)\n"
513 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000514 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000515 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000516 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000517 " }\n"
518 " else\n"
519 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000520 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000521 " }\n"
522 "}\n"
523 "\n";
524 }
525
526 if (mUsesFaceforward2)
527 {
528 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
529 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000530 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000531 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000532 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000533 " }\n"
534 " else\n"
535 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000536 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000537 " }\n"
538 "}\n"
539 "\n";
540 }
541
542 if (mUsesFaceforward3)
543 {
544 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
545 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000546 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000547 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000548 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000549 " }\n"
550 " else\n"
551 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000552 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000553 " }\n"
554 "}\n"
555 "\n";
556 }
557
558 if (mUsesFaceforward4)
559 {
560 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
561 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000562 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000563 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000564 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000565 " }\n"
566 " else\n"
567 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000568 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000569 " }\n"
570 "}\n"
571 "\n";
572 }
573
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000574 if (mUsesEqualMat2)
575 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000576 out << "bool equal(float2x2 m, float2x2 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000577 "{\n"
578 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
579 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
580 "}\n";
581 }
582
583 if (mUsesEqualMat3)
584 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000585 out << "bool equal(float3x3 m, float3x3 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000586 "{\n"
587 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
588 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
589 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
590 "}\n";
591 }
592
593 if (mUsesEqualMat4)
594 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000595 out << "bool equal(float4x4 m, float4x4 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000596 "{\n"
597 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] && m[0][3] == n[0][3] &&\n"
598 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] && m[1][3] == n[1][3] &&\n"
599 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2] && m[2][3] == n[2][3] &&\n"
600 " m[3][0] == n[3][0] && m[3][1] == n[3][1] && m[3][2] == n[3][2] && m[3][3] == n[3][3];\n"
601 "}\n";
602 }
603
604 if (mUsesEqualVec2)
605 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000606 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000607 "{\n"
608 " return v.x == u.x && v.y == u.y;\n"
609 "}\n";
610 }
611
612 if (mUsesEqualVec3)
613 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000614 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000615 "{\n"
616 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
617 "}\n";
618 }
619
620 if (mUsesEqualVec4)
621 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000622 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000623 "{\n"
624 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
625 "}\n";
626 }
627
628 if (mUsesEqualIVec2)
629 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000630 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000631 "{\n"
632 " return v.x == u.x && v.y == u.y;\n"
633 "}\n";
634 }
635
636 if (mUsesEqualIVec3)
637 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000638 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000639 "{\n"
640 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
641 "}\n";
642 }
643
644 if (mUsesEqualIVec4)
645 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000646 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000647 "{\n"
648 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
649 "}\n";
650 }
651
652 if (mUsesEqualBVec2)
653 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000654 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000655 "{\n"
656 " return v.x == u.x && v.y == u.y;\n"
657 "}\n";
658 }
659
660 if (mUsesEqualBVec3)
661 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000662 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000663 "{\n"
664 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
665 "}\n";
666 }
667
668 if (mUsesEqualBVec4)
669 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000670 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000671 "{\n"
672 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
673 "}\n";
674 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +0000675
676 if (mUsesAtan2)
677 {
678 out << "float atanyx(float y, float x)\n"
679 "{\n"
680 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
681 " return atan2(y, x);\n"
682 "}\n";
683 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684}
685
686void OutputHLSL::visitSymbol(TIntermSymbol *node)
687{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000688 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689
690 TString name = node->getSymbol();
691
692 if (name == "gl_FragColor")
693 {
694 out << "gl_Color[0]";
695 }
696 else if (name == "gl_FragData")
697 {
698 out << "gl_Color";
699 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000700 else if (name == "gl_DepthRange")
701 {
702 mUsesDepthRange = true;
703 out << name;
704 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000705 else if (name == "gl_FragCoord")
706 {
707 mUsesFragCoord = true;
708 out << name;
709 }
710 else if (name == "gl_PointCoord")
711 {
712 mUsesPointCoord = true;
713 out << name;
714 }
715 else if (name == "gl_FrontFacing")
716 {
717 mUsesFrontFacing = true;
718 out << name;
719 }
720 else if (name == "gl_PointSize")
721 {
722 mUsesPointSize = true;
723 out << name;
724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725 else
726 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000727 TQualifier qualifier = node->getQualifier();
728
729 if (qualifier == EvqUniform)
730 {
731 mReferencedUniforms.insert(name.c_str());
732 }
733 else if (qualifier == EvqAttribute)
734 {
735 mReferencedAttributes.insert(name.c_str());
736 }
737 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut || qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
738 {
739 mReferencedVaryings.insert(name.c_str());
740 }
741
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000742 out << decorate(name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743 }
744}
745
746bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
747{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000748 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749
750 switch (node->getOp())
751 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000752 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000753 case EOpInitialize:
754 if (visit == PreVisit)
755 {
756 // GLSL allows to write things like "float x = x;" where a new variable x is defined
757 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
758 // new variable is created before the assignment is evaluated), so we need to convert
759 // this to "float t = x, x = t;".
760
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000761 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
762 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000763
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000764 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
765 expression->traverse(&searchSymbol);
766 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000767
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000768 if (sameSymbol)
769 {
770 // Type already printed
771 out << "t" + str(mUniqueIndex) + " = ";
772 expression->traverse(this);
773 out << ", ";
774 symbolNode->traverse(this);
775 out << " = t" + str(mUniqueIndex);
776
777 mUniqueIndex++;
778 return false;
779 }
780 }
781 else if (visit == InVisit)
782 {
783 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000784 }
785 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000786 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
787 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
788 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
789 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
790 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
791 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000792 if (visit == PreVisit)
793 {
794 out << "(";
795 }
796 else if (visit == InVisit)
797 {
798 out << " = mul(";
799 node->getLeft()->traverse(this);
800 out << ", transpose(";
801 }
802 else
803 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000804 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000805 }
806 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000807 case EOpMatrixTimesMatrixAssign:
808 if (visit == PreVisit)
809 {
810 out << "(";
811 }
812 else if (visit == InVisit)
813 {
814 out << " = mul(";
815 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000816 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000817 }
818 else
819 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000820 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000821 }
822 break;
823 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +0000824 case EOpIndexDirect: outputTriplet(visit, "", "[", "]"); break;
825 case EOpIndexIndirect: outputTriplet(visit, "", "[", "]"); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +0000826 case EOpIndexDirectStruct:
827 if (visit == InVisit)
828 {
829 out << "." + node->getType().getFieldName();
830
831 return false;
832 }
833 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834 case EOpVectorSwizzle:
835 if (visit == InVisit)
836 {
837 out << ".";
838
839 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
840
841 if (swizzle)
842 {
843 TIntermSequence &sequence = swizzle->getSequence();
844
845 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
846 {
847 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
848
849 if (element)
850 {
851 int i = element->getUnionArrayPointer()[0].getIConst();
852
853 switch (i)
854 {
855 case 0: out << "x"; break;
856 case 1: out << "y"; break;
857 case 2: out << "z"; break;
858 case 3: out << "w"; break;
859 default: UNREACHABLE();
860 }
861 }
862 else UNREACHABLE();
863 }
864 }
865 else UNREACHABLE();
866
867 return false; // Fully processed
868 }
869 break;
870 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
871 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
872 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
873 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000874 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000875 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000876 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000877 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000878 if (node->getOp() == EOpEqual)
879 {
880 outputTriplet(visit, "(", " == ", ")");
881 }
882 else
883 {
884 outputTriplet(visit, "(", " != ", ")");
885 }
886 }
887 else if (node->getLeft()->getBasicType() == EbtStruct)
888 {
889 if (node->getOp() == EOpEqual)
890 {
891 out << "(";
892 }
893 else
894 {
895 out << "!(";
896 }
897
898 const TTypeList *fields = node->getLeft()->getType().getStruct();
899
900 for (size_t i = 0; i < fields->size(); i++)
901 {
902 const TType *fieldType = (*fields)[i].type;
903
904 node->getLeft()->traverse(this);
905 out << "." + fieldType->getFieldName() + " == ";
906 node->getRight()->traverse(this);
907 out << "." + fieldType->getFieldName();
908
909 if (i < fields->size() - 1)
910 {
911 out << " && ";
912 }
913 }
914
915 out << ")";
916
917 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000918 }
919 else
920 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000921 if (node->getLeft()->isMatrix())
922 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000923 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000924 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000925 case 2: mUsesEqualMat2 = true; break;
926 case 3: mUsesEqualMat3 = true; break;
927 case 4: mUsesEqualMat4 = true; break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000928 default: UNREACHABLE();
929 }
930 }
931 else if (node->getLeft()->isVector())
932 {
933 switch (node->getLeft()->getBasicType())
934 {
935 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +0000936 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000937 {
938 case 2: mUsesEqualVec2 = true; break;
939 case 3: mUsesEqualVec3 = true; break;
940 case 4: mUsesEqualVec4 = true; break;
941 default: UNREACHABLE();
942 }
943 break;
944 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +0000945 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000946 {
947 case 2: mUsesEqualIVec2 = true; break;
948 case 3: mUsesEqualIVec3 = true; break;
949 case 4: mUsesEqualIVec4 = true; break;
950 default: UNREACHABLE();
951 }
952 break;
953 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +0000954 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000955 {
956 case 2: mUsesEqualBVec2 = true; break;
957 case 3: mUsesEqualBVec3 = true; break;
958 case 4: mUsesEqualBVec4 = true; break;
959 default: UNREACHABLE();
960 }
961 break;
962 default: UNREACHABLE();
963 }
964 }
965 else UNREACHABLE();
966
967 if (node->getOp() == EOpEqual)
968 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000969 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000970 }
971 else
972 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000973 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000974 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000975 }
976 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
978 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
979 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
980 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
981 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000982 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000983 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
984 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +0000985 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000987 case EOpLogicalXor:
988 mUsesXor = true;
989 outputTriplet(visit, "xor(", ", ", ")");
990 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
992 default: UNREACHABLE();
993 }
994
995 return true;
996}
997
998bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
999{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001000 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001
1002 switch (node->getOp())
1003 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001004 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1005 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1006 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1007 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1008 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1009 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1010 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011 case EOpConvIntToBool:
1012 case EOpConvFloatToBool:
1013 switch (node->getOperand()->getType().getNominalSize())
1014 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001015 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1016 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1017 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1018 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001019 default: UNREACHABLE();
1020 }
1021 break;
1022 case EOpConvBoolToFloat:
1023 case EOpConvIntToFloat:
1024 switch (node->getOperand()->getType().getNominalSize())
1025 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001026 case 1: outputTriplet(visit, "float(", "", ")"); break;
1027 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1028 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1029 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030 default: UNREACHABLE();
1031 }
1032 break;
1033 case EOpConvFloatToInt:
1034 case EOpConvBoolToInt:
1035 switch (node->getOperand()->getType().getNominalSize())
1036 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001037 case 1: outputTriplet(visit, "int(", "", ")"); break;
1038 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1039 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1040 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041 default: UNREACHABLE();
1042 }
1043 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001044 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1045 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1046 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1047 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1048 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1049 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1050 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1051 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1052 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1053 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1054 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1055 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1056 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1057 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1058 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1059 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1060 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1061 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1062 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1063 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1064 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
alokp@chromium.org06098892010-08-26 19:36:42 +00001065 case EOpDFdx: outputTriplet(visit, "ddx(", "", ")"); break;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001066 case EOpDFdy: outputTriplet(visit, "(-ddy(", "", "))"); break;
alokp@chromium.org06098892010-08-26 19:36:42 +00001067 case EOpFwidth: outputTriplet(visit, "fwidth(", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001068 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1069 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070 default: UNREACHABLE();
1071 }
1072
1073 return true;
1074}
1075
1076bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1077{
alokp@chromium.org4888ceb2010-10-01 21:13:12 +00001078 ShShaderType shaderType = mContext.shaderType;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001079 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081 switch (node->getOp())
1082 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001083 case EOpSequence:
1084 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001085 if (mInsideFunction)
1086 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001087 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001088 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001089
1090 mScopeDepth++;
1091
1092 if (mScopeBracket.size() < mScopeDepth)
1093 {
1094 mScopeBracket.push_back(0); // New scope level
1095 }
1096 else
1097 {
1098 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1099 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001100 }
1101
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001102 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1103 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001104 outputLineDirective((*sit)->getLine());
1105
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001106 if (isSingleStatement(*sit))
1107 {
1108 mUnfoldSelect->traverse(*sit);
1109 }
1110
1111 (*sit)->traverse(this);
1112
1113 out << ";\n";
1114 }
1115
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001116 if (mInsideFunction)
1117 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001118 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001119 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001120
1121 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001122 }
1123
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001124 return false;
1125 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001126 case EOpDeclaration:
1127 if (visit == PreVisit)
1128 {
1129 TIntermSequence &sequence = node->getSequence();
1130 TIntermTyped *variable = sequence[0]->getAsTyped();
1131 bool visit = true;
1132
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001133 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001135 if (variable->getType().getStruct())
1136 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001137 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001138 }
1139
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001140 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001142 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001143 {
1144 out << "static ";
1145 }
1146
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001147 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001148
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001149 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001150 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001151 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001153 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001154 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001155 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001156 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001157 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001158 }
1159 else
1160 {
1161 (*sit)->traverse(this);
1162 }
1163
1164 if (visit && this->inVisit)
1165 {
1166 if (*sit != sequence.back())
1167 {
1168 visit = this->visitAggregate(InVisit, node);
1169 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001170 }
1171 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001172
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001173 if (visit && this->postVisit)
1174 {
1175 this->visitAggregate(PostVisit, node);
1176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001177 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001178 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1179 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001180 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001181 }
1182 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183 }
1184
1185 return false;
1186 }
1187 else if (visit == InVisit)
1188 {
1189 out << ", ";
1190 }
1191 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001192 case EOpPrototype:
1193 if (visit == PreVisit)
1194 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001195 out << typeString(node->getType()) << " " << decorate(node->getName()) << "(";
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001196
1197 TIntermSequence &arguments = node->getSequence();
1198
1199 for (unsigned int i = 0; i < arguments.size(); i++)
1200 {
1201 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1202
1203 if (symbol)
1204 {
1205 out << argumentString(symbol);
1206
1207 if (i < arguments.size() - 1)
1208 {
1209 out << ", ";
1210 }
1211 }
1212 else UNREACHABLE();
1213 }
1214
1215 out << ");\n";
1216
1217 return false;
1218 }
1219 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001220 case EOpComma: outputTriplet(visit, "", ", ", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001221 case EOpFunction:
1222 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001223 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001224
1225 if (visit == PreVisit)
1226 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001227 out << typeString(node->getType()) << " ";
1228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229 if (name == "main")
1230 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001231 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001232 }
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001233 else
1234 {
1235 out << decorate(name) << "(";
1236 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001237
1238 TIntermSequence &sequence = node->getSequence();
1239 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1240
1241 for (unsigned int i = 0; i < arguments.size(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001242 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001243 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001244
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001245 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001246 {
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001247 out << argumentString(symbol);
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001248
1249 if (i < arguments.size() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001250 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001251 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001254 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001255 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +00001256
1257 sequence.erase(sequence.begin());
1258
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001259 out << ")\n";
1260
1261 outputLineDirective(node->getLine());
1262 out << "{\n";
1263
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001264 mInsideFunction = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001265 }
1266 else if (visit == PostVisit)
1267 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001268 outputLineDirective(node->getEndLine());
daniel@transgaming.com63691862010-04-29 03:32:42 +00001269 out << "}\n";
1270
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001271 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001272 }
1273 }
1274 break;
1275 case EOpFunctionCall:
1276 {
1277 if (visit == PreVisit)
1278 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001279 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001280
1281 if (node->isUserDefined())
1282 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001283 out << decorate(name) << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001284 }
1285 else
1286 {
1287 if (name == "texture2D")
1288 {
1289 if (node->getSequence().size() == 2)
1290 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001291 mUsesTexture2D = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292 }
1293 else if (node->getSequence().size() == 3)
1294 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001295 mUsesTexture2D_bias = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001296 }
1297 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001298
1299 out << "gl_texture2D(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300 }
1301 else if (name == "texture2DProj")
1302 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001303 if (node->getSequence().size() == 2)
1304 {
1305 mUsesTexture2DProj = true;
1306 }
1307 else if (node->getSequence().size() == 3)
1308 {
1309 mUsesTexture2DProj_bias = true;
1310 }
1311 else UNREACHABLE();
1312
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313 out << "gl_texture2DProj(";
1314 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001315 else if (name == "textureCube")
1316 {
1317 if (node->getSequence().size() == 2)
1318 {
1319 mUsesTextureCube = true;
1320 }
1321 else if (node->getSequence().size() == 3)
1322 {
1323 mUsesTextureCube_bias = true;
1324 }
1325 else UNREACHABLE();
1326
1327 out << "gl_textureCube(";
1328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001329 else if (name == "texture2DLod")
1330 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001331 if (node->getSequence().size() == 3)
1332 {
1333 mUsesTexture2DLod = true;
1334 }
1335 else UNREACHABLE();
1336
1337 out << "gl_texture2DLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001338 }
1339 else if (name == "texture2DProjLod")
1340 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001341 if (node->getSequence().size() == 3)
1342 {
1343 mUsesTexture2DProjLod = true;
1344 }
1345 else UNREACHABLE();
1346
1347 out << "gl_texture2DProjLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001348 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001349 else if (name == "textureCubeLod")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001350 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001351 if (node->getSequence().size() == 3)
1352 {
1353 mUsesTextureCubeLod = true;
1354 }
1355 else UNREACHABLE();
1356
1357 out << "gl_textureCubeLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001358 }
daniel@transgaming.comec55d292010-04-15 20:44:49 +00001359 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001360 }
1361 }
1362 else if (visit == InVisit)
1363 {
1364 out << ", ";
1365 }
1366 else
1367 {
1368 out << ")";
1369 }
1370 }
1371 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001372 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001373 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001374 addConstructor(node->getType(), "vec1", &node->getSequence());
1375 outputTriplet(visit, "vec1(", "", ")");
1376 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001377 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001378 addConstructor(node->getType(), "vec2", &node->getSequence());
1379 outputTriplet(visit, "vec2(", ", ", ")");
1380 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001381 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001382 addConstructor(node->getType(), "vec3", &node->getSequence());
1383 outputTriplet(visit, "vec3(", ", ", ")");
1384 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001385 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001386 addConstructor(node->getType(), "vec4", &node->getSequence());
1387 outputTriplet(visit, "vec4(", ", ", ")");
1388 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001389 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001390 addConstructor(node->getType(), "bvec1", &node->getSequence());
1391 outputTriplet(visit, "bvec1(", "", ")");
1392 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001393 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001394 addConstructor(node->getType(), "bvec2", &node->getSequence());
1395 outputTriplet(visit, "bvec2(", ", ", ")");
1396 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001397 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001398 addConstructor(node->getType(), "bvec3", &node->getSequence());
1399 outputTriplet(visit, "bvec3(", ", ", ")");
1400 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001401 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001402 addConstructor(node->getType(), "bvec4", &node->getSequence());
1403 outputTriplet(visit, "bvec4(", ", ", ")");
1404 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001405 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001406 addConstructor(node->getType(), "ivec1", &node->getSequence());
1407 outputTriplet(visit, "ivec1(", "", ")");
1408 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001409 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001410 addConstructor(node->getType(), "ivec2", &node->getSequence());
1411 outputTriplet(visit, "ivec2(", ", ", ")");
1412 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001413 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001414 addConstructor(node->getType(), "ivec3", &node->getSequence());
1415 outputTriplet(visit, "ivec3(", ", ", ")");
1416 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001417 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001418 addConstructor(node->getType(), "ivec4", &node->getSequence());
1419 outputTriplet(visit, "ivec4(", ", ", ")");
1420 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001421 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001422 addConstructor(node->getType(), "mat2", &node->getSequence());
1423 outputTriplet(visit, "mat2(", ", ", ")");
1424 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001425 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001426 addConstructor(node->getType(), "mat3", &node->getSequence());
1427 outputTriplet(visit, "mat3(", ", ", ")");
1428 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001429 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001430 addConstructor(node->getType(), "mat4", &node->getSequence());
1431 outputTriplet(visit, "mat4(", ", ", ")");
1432 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001433 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001434 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
1435 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001436 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001437 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1438 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1439 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1440 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1441 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
1442 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001443 case EOpMod:
1444 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001445 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001446 {
1447 case 1: mUsesMod1 = true; break;
1448 case 2: mUsesMod2 = true; break;
1449 case 3: mUsesMod3 = true; break;
1450 case 4: mUsesMod4 = true; break;
1451 default: UNREACHABLE();
1452 }
1453
1454 outputTriplet(visit, "mod(", ", ", ")");
1455 }
1456 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001457 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001458 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001459 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
1460 mUsesAtan2 = true;
1461 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001462 break;
1463 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
1464 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
1465 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
1466 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
1467 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
1468 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
1469 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
1470 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
1471 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001472 case EOpFaceForward:
1473 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001474 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001475 {
1476 case 1: mUsesFaceforward1 = true; break;
1477 case 2: mUsesFaceforward2 = true; break;
1478 case 3: mUsesFaceforward3 = true; break;
1479 case 4: mUsesFaceforward4 = true; break;
1480 default: UNREACHABLE();
1481 }
1482
1483 outputTriplet(visit, "faceforward(", ", ", ")");
1484 }
1485 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001486 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
1487 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
1488 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001489 default: UNREACHABLE();
1490 }
1491
1492 return true;
1493}
1494
1495bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1496{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001497 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001498
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001499 if (node->usesTernaryOperator())
1500 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001501 out << "t" << mUnfoldSelect->getTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001502 }
1503 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001504 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001505 mUnfoldSelect->traverse(node->getCondition());
1506
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001507 out << "if(";
1508
1509 node->getCondition()->traverse(this);
1510
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001511 out << ")\n";
1512
1513 outputLineDirective(node->getLine());
1514 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515
daniel@transgaming.combb885322010-04-15 20:45:24 +00001516 if (node->getTrueBlock())
1517 {
1518 node->getTrueBlock()->traverse(this);
1519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001520
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001521 outputLineDirective(node->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522 out << ";}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001523
1524 if (node->getFalseBlock())
1525 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001526 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001527
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001528 outputLineDirective(node->getFalseBlock()->getLine());
1529 out << "{\n";
1530
1531 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001532 node->getFalseBlock()->traverse(this);
1533
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001534 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001535 out << ";}\n";
1536 }
1537 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001538
1539 return false;
1540}
1541
1542void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
1543{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001544 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001545}
1546
1547bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1548{
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001549 if (handleExcessiveLoop(node))
1550 {
1551 return false;
1552 }
1553
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001554 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001555
alokp@chromium.org52813552010-11-16 18:36:09 +00001556 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001557 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001558 out << "do\n";
1559
1560 outputLineDirective(node->getLine());
1561 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001562 }
1563 else
1564 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001565 if (node->getInit())
1566 {
1567 mUnfoldSelect->traverse(node->getInit());
1568 }
1569
alokp@chromium.org52813552010-11-16 18:36:09 +00001570 if (node->getCondition())
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001571 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001572 mUnfoldSelect->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001573 }
1574
alokp@chromium.org52813552010-11-16 18:36:09 +00001575 if (node->getExpression())
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001576 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001577 mUnfoldSelect->traverse(node->getExpression());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001578 }
1579
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001580 out << "for(";
1581
1582 if (node->getInit())
1583 {
1584 node->getInit()->traverse(this);
1585 }
1586
1587 out << "; ";
1588
alokp@chromium.org52813552010-11-16 18:36:09 +00001589 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001591 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592 }
1593
1594 out << "; ";
1595
alokp@chromium.org52813552010-11-16 18:36:09 +00001596 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001598 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001599 }
1600
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001601 out << ")\n";
1602
1603 outputLineDirective(node->getLine());
1604 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001605 }
1606
1607 if (node->getBody())
1608 {
1609 node->getBody()->traverse(this);
1610 }
1611
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001612 outputLineDirective(node->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001613 out << "}\n";
1614
alokp@chromium.org52813552010-11-16 18:36:09 +00001615 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001616 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001617 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618 out << "while(\n";
1619
alokp@chromium.org52813552010-11-16 18:36:09 +00001620 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001621
1622 out << ")";
1623 }
1624
1625 out << ";\n";
1626
1627 return false;
1628}
1629
1630bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1631{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001632 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001633
1634 switch (node->getFlowOp())
1635 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00001636 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
1637 case EOpBreak: outputTriplet(visit, "break;\n", "", ""); break;
1638 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639 case EOpReturn:
1640 if (visit == PreVisit)
1641 {
1642 if (node->getExpression())
1643 {
1644 out << "return ";
1645 }
1646 else
1647 {
1648 out << "return;\n";
1649 }
1650 }
1651 else if (visit == PostVisit)
1652 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001653 if (node->getExpression())
1654 {
1655 out << ";\n";
1656 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657 }
1658 break;
1659 default: UNREACHABLE();
1660 }
1661
1662 return true;
1663}
1664
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001665bool OutputHLSL::isSingleStatement(TIntermNode *node)
1666{
1667 TIntermAggregate *aggregate = node->getAsAggregate();
1668
1669 if (aggregate)
1670 {
1671 if (aggregate->getOp() == EOpSequence)
1672 {
1673 return false;
1674 }
1675 else
1676 {
1677 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
1678 {
1679 if (!isSingleStatement(*sit))
1680 {
1681 return false;
1682 }
1683 }
1684
1685 return true;
1686 }
1687 }
1688
1689 return true;
1690}
1691
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001692// Handle loops with more than 255 iterations (unsupported by D3D9) by splitting them
1693bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
1694{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001695 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001696
1697 // Parse loops of the form:
1698 // for(int index = initial; index [comparator] limit; index += increment)
1699 TIntermSymbol *index = NULL;
1700 TOperator comparator = EOpNull;
1701 int initial = 0;
1702 int limit = 0;
1703 int increment = 0;
1704
1705 // Parse index name and intial value
1706 if (node->getInit())
1707 {
1708 TIntermAggregate *init = node->getInit()->getAsAggregate();
1709
1710 if (init)
1711 {
1712 TIntermSequence &sequence = init->getSequence();
1713 TIntermTyped *variable = sequence[0]->getAsTyped();
1714
1715 if (variable && variable->getQualifier() == EvqTemporary)
1716 {
1717 TIntermBinary *assign = variable->getAsBinaryNode();
1718
1719 if (assign->getOp() == EOpInitialize)
1720 {
1721 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
1722 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
1723
1724 if (symbol && constant)
1725 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001726 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001727 {
1728 index = symbol;
1729 initial = constant->getUnionArrayPointer()[0].getIConst();
1730 }
1731 }
1732 }
1733 }
1734 }
1735 }
1736
1737 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00001738 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001739 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001740 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001741
1742 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
1743 {
1744 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
1745
1746 if (constant)
1747 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001748 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001749 {
1750 comparator = test->getOp();
1751 limit = constant->getUnionArrayPointer()[0].getIConst();
1752 }
1753 }
1754 }
1755 }
1756
1757 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00001758 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001759 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001760 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
1761 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001762
1763 if (binaryTerminal)
1764 {
1765 TOperator op = binaryTerminal->getOp();
1766 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
1767
1768 if (constant)
1769 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001770 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001771 {
1772 int value = constant->getUnionArrayPointer()[0].getIConst();
1773
1774 switch (op)
1775 {
1776 case EOpAddAssign: increment = value; break;
1777 case EOpSubAssign: increment = -value; break;
1778 default: UNIMPLEMENTED();
1779 }
1780 }
1781 }
1782 }
1783 else if (unaryTerminal)
1784 {
1785 TOperator op = unaryTerminal->getOp();
1786
1787 switch (op)
1788 {
1789 case EOpPostIncrement: increment = 1; break;
1790 case EOpPostDecrement: increment = -1; break;
1791 case EOpPreIncrement: increment = 1; break;
1792 case EOpPreDecrement: increment = -1; break;
1793 default: UNIMPLEMENTED();
1794 }
1795 }
1796 }
1797
1798 if (index != NULL && comparator != EOpNull && increment != 0)
1799 {
1800 if (comparator == EOpLessThanEqual)
1801 {
1802 comparator = EOpLessThan;
1803 limit += 1;
1804 }
1805
1806 if (comparator == EOpLessThan)
1807 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00001808 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001809
1810 if (iterations <= 255)
1811 {
1812 return false; // Not an excessive loop
1813 }
1814
1815 while (iterations > 0)
1816 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00001817 int remainder = (limit - initial) % increment;
1818 int clampedLimit = initial + increment * std::min(255, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001819
1820 // for(int index = initial; index < clampedLimit; index += increment)
1821
1822 out << "for(int ";
1823 index->traverse(this);
1824 out << " = ";
1825 out << initial;
1826
1827 out << "; ";
1828 index->traverse(this);
1829 out << " < ";
1830 out << clampedLimit;
1831
1832 out << "; ";
1833 index->traverse(this);
1834 out << " += ";
1835 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001836 out << ")\n";
1837
1838 outputLineDirective(node->getLine());
1839 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001840
1841 if (node->getBody())
1842 {
1843 node->getBody()->traverse(this);
1844 }
1845
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001846 outputLineDirective(node->getLine());
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001847 out << "}\n";
1848
1849 initial += 255 * increment;
1850 iterations -= 255;
1851 }
1852
1853 return true;
1854 }
1855 else UNIMPLEMENTED();
1856 }
1857
1858 return false; // Not handled as an excessive loop
1859}
1860
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001861void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001863 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001865 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866 {
1867 out << preString;
1868 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001869 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 {
1871 out << inString;
1872 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001873 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 {
1875 out << postString;
1876 }
1877}
1878
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001879void OutputHLSL::outputLineDirective(int line)
1880{
1881 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
1882 {
1883 mBody << "#line " << line;
1884
1885 if (mContext.sourcePath)
1886 {
1887 mBody << " \"" << mContext.sourcePath << "\"";
1888 }
1889
1890 mBody << "\n";
1891 }
1892}
1893
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001894TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
1895{
1896 TQualifier qualifier = symbol->getQualifier();
1897 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00001898 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001899
daniel@transgaming.com005c7392010-04-15 20:45:27 +00001900 if (name.empty()) // HLSL demands named arguments, also for prototypes
1901 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001902 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00001903 }
1904 else
1905 {
1906 name = decorate(name);
1907 }
1908
1909 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001910}
1911
1912TString OutputHLSL::qualifierString(TQualifier qualifier)
1913{
1914 switch(qualifier)
1915 {
1916 case EvqIn: return "in";
1917 case EvqOut: return "out";
1918 case EvqInOut: return "inout";
1919 case EvqConstReadOnly: return "const";
1920 default: UNREACHABLE();
1921 }
1922
1923 return "";
1924}
1925
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926TString OutputHLSL::typeString(const TType &type)
1927{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001928 if (type.getBasicType() == EbtStruct)
1929 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00001930 if (type.getTypeName() != "")
1931 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001932 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00001933 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00001934 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00001935 {
1936 const TTypeList &fields = *type.getStruct();
1937
1938 TString string = "struct\n"
1939 "{\n";
1940
1941 for (unsigned int i = 0; i < fields.size(); i++)
1942 {
1943 const TType &field = *fields[i].type;
1944
1945 string += " " + typeString(field) + " " + field.getFieldName() + arrayString(field) + ";\n";
1946 }
1947
1948 string += "} ";
1949
1950 return string;
1951 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001952 }
1953 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954 {
1955 switch (type.getNominalSize())
1956 {
1957 case 2: return "float2x2";
1958 case 3: return "float3x3";
1959 case 4: return "float4x4";
1960 }
1961 }
1962 else
1963 {
1964 switch (type.getBasicType())
1965 {
1966 case EbtFloat:
1967 switch (type.getNominalSize())
1968 {
1969 case 1: return "float";
1970 case 2: return "float2";
1971 case 3: return "float3";
1972 case 4: return "float4";
1973 }
1974 case EbtInt:
1975 switch (type.getNominalSize())
1976 {
1977 case 1: return "int";
1978 case 2: return "int2";
1979 case 3: return "int3";
1980 case 4: return "int4";
1981 }
1982 case EbtBool:
1983 switch (type.getNominalSize())
1984 {
1985 case 1: return "bool";
1986 case 2: return "bool2";
1987 case 3: return "bool3";
1988 case 4: return "bool4";
1989 }
1990 case EbtVoid:
1991 return "void";
1992 case EbtSampler2D:
1993 return "sampler2D";
1994 case EbtSamplerCube:
1995 return "samplerCUBE";
1996 }
1997 }
1998
1999 UNIMPLEMENTED(); // FIXME
2000 return "<unknown type>";
2001}
2002
2003TString OutputHLSL::arrayString(const TType &type)
2004{
2005 if (!type.isArray())
2006 {
2007 return "";
2008 }
2009
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002010 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002011}
2012
2013TString OutputHLSL::initializer(const TType &type)
2014{
2015 TString string;
2016
daniel@transgaming.comead23042010-04-29 03:35:36 +00002017 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002019 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002020
daniel@transgaming.comead23042010-04-29 03:35:36 +00002021 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 {
2023 string += ", ";
2024 }
2025 }
2026
daniel@transgaming.comead23042010-04-29 03:35:36 +00002027 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002029
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002030void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002031{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002032 if (name == "")
2033 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002034 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002035 }
2036
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002037 TType ctorType = type;
2038 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00002039 ctorType.setPrecision(EbpHigh);
2040 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00002041
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002042 TString ctorName = type.getStruct() ? decorate(name) : name;
2043
2044 typedef std::vector<TType> ParameterArray;
2045 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002046
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002047 if (parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002048 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002049 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
2050 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002051 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002052 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002053 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002054 else if (type.getStruct())
2055 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002056 mStructNames.insert(decorate(name));
2057
2058 TString structure;
2059 structure += "struct " + decorate(name) + "\n"
2060 "{\n";
2061
2062 const TTypeList &fields = *type.getStruct();
2063
2064 for (unsigned int i = 0; i < fields.size(); i++)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002065 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002066 const TType &field = *fields[i].type;
2067
2068 structure += " " + typeString(field) + " " + field.getFieldName() + arrayString(field) + ";\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002069 }
2070
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002071 structure += "};\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002072
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002073 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002074 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002075 mStructDeclarations.push_back(structure);
2076 }
2077
2078 for (unsigned int i = 0; i < fields.size(); i++)
2079 {
2080 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002081 }
2082 }
2083 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00002084
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002085 TString constructor;
2086
2087 if (ctorType.getStruct())
2088 {
2089 constructor += ctorName + " " + ctorName + "_ctor(";
2090 }
2091 else // Built-in type
2092 {
2093 constructor += typeString(ctorType) + " " + ctorName + "(";
2094 }
2095
2096 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
2097 {
2098 const TType &type = ctorParameters[parameter];
2099
2100 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
2101
2102 if (parameter < ctorParameters.size() - 1)
2103 {
2104 constructor += ", ";
2105 }
2106 }
2107
2108 constructor += ")\n"
2109 "{\n";
2110
2111 if (ctorType.getStruct())
2112 {
2113 constructor += " " + ctorName + " structure = {";
2114 }
2115 else
2116 {
2117 constructor += " return " + typeString(ctorType) + "(";
2118 }
2119
2120 if (ctorType.isMatrix() && ctorParameters.size() == 1)
2121 {
2122 int dim = ctorType.getNominalSize();
2123 const TType &parameter = ctorParameters[0];
2124
2125 if (parameter.isScalar())
2126 {
2127 for (int row = 0; row < dim; row++)
2128 {
2129 for (int col = 0; col < dim; col++)
2130 {
2131 constructor += TString((row == col) ? "x0" : "0.0");
2132
2133 if (row < dim - 1 || col < dim - 1)
2134 {
2135 constructor += ", ";
2136 }
2137 }
2138 }
2139 }
2140 else if (parameter.isMatrix())
2141 {
2142 for (int row = 0; row < dim; row++)
2143 {
2144 for (int col = 0; col < dim; col++)
2145 {
2146 if (row < parameter.getNominalSize() && col < parameter.getNominalSize())
2147 {
2148 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
2149 }
2150 else
2151 {
2152 constructor += TString((row == col) ? "1.0" : "0.0");
2153 }
2154
2155 if (row < dim - 1 || col < dim - 1)
2156 {
2157 constructor += ", ";
2158 }
2159 }
2160 }
2161 }
2162 else UNREACHABLE();
2163 }
2164 else
2165 {
2166 int remainingComponents = ctorType.getObjectSize();
2167 int parameterIndex = 0;
2168
2169 while (remainingComponents > 0)
2170 {
2171 const TType &parameter = ctorParameters[parameterIndex];
2172 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
2173
2174 constructor += "x" + str(parameterIndex);
2175
2176 if (parameter.isScalar())
2177 {
2178 remainingComponents -= parameter.getObjectSize();
2179 }
2180 else if (parameter.isVector())
2181 {
2182 if (remainingComponents == parameter.getObjectSize() || moreParameters)
2183 {
2184 remainingComponents -= parameter.getObjectSize();
2185 }
2186 else if (remainingComponents < parameter.getNominalSize())
2187 {
2188 switch (remainingComponents)
2189 {
2190 case 1: constructor += ".x"; break;
2191 case 2: constructor += ".xy"; break;
2192 case 3: constructor += ".xyz"; break;
2193 case 4: constructor += ".xyzw"; break;
2194 default: UNREACHABLE();
2195 }
2196
2197 remainingComponents = 0;
2198 }
2199 else UNREACHABLE();
2200 }
2201 else if (parameter.isMatrix() || parameter.getStruct())
2202 {
2203 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
2204
2205 remainingComponents -= parameter.getObjectSize();
2206 }
2207 else UNREACHABLE();
2208
2209 if (moreParameters)
2210 {
2211 parameterIndex++;
2212 }
2213
2214 if (remainingComponents)
2215 {
2216 constructor += ", ";
2217 }
2218 }
2219 }
2220
2221 if (ctorType.getStruct())
2222 {
2223 constructor += "};\n"
2224 " return structure;\n"
2225 "}\n";
2226 }
2227 else
2228 {
2229 constructor += ");\n"
2230 "}\n";
2231 }
2232
daniel@transgaming.com63691862010-04-29 03:32:42 +00002233 mConstructors.insert(constructor);
2234}
2235
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002236const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2237{
2238 TInfoSinkBase &out = mBody;
2239
2240 if (type.getBasicType() == EbtStruct)
2241 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002242 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002243
2244 const TTypeList *structure = type.getStruct();
2245
2246 for (size_t i = 0; i < structure->size(); i++)
2247 {
2248 const TType *fieldType = (*structure)[i].type;
2249
2250 constUnion = writeConstantUnion(*fieldType, constUnion);
2251
2252 if (i != structure->size() - 1)
2253 {
2254 out << ", ";
2255 }
2256 }
2257
2258 out << ")";
2259 }
2260 else
2261 {
2262 int size = type.getObjectSize();
2263 bool writeType = size > 1;
2264
2265 if (writeType)
2266 {
2267 out << typeString(type) << "(";
2268 }
2269
2270 for (int i = 0; i < size; i++, constUnion++)
2271 {
2272 switch (constUnion->getType())
2273 {
2274 case EbtFloat: out << constUnion->getFConst(); break;
2275 case EbtInt: out << constUnion->getIConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002276 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002277 default: UNREACHABLE();
2278 }
2279
2280 if (i != size - 1)
2281 {
2282 out << ", ";
2283 }
2284 }
2285
2286 if (writeType)
2287 {
2288 out << ")";
2289 }
2290 }
2291
2292 return constUnion;
2293}
2294
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002295TString OutputHLSL::scopeString(unsigned int depthLimit)
2296{
2297 TString string;
2298
2299 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
2300 {
2301 string += "_" + str(i);
2302 }
2303
2304 return string;
2305}
2306
2307TString OutputHLSL::scopedStruct(const TString &typeName)
2308{
2309 if (typeName == "")
2310 {
2311 return typeName;
2312 }
2313
2314 return typeName + scopeString(mScopeDepth);
2315}
2316
2317TString OutputHLSL::structLookup(const TString &typeName)
2318{
2319 for (int depth = mScopeDepth; depth >= 0; depth--)
2320 {
2321 TString scopedName = decorate(typeName + scopeString(depth));
2322
2323 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
2324 {
2325 if (*structName == scopedName)
2326 {
2327 return scopedName;
2328 }
2329 }
2330 }
2331
2332 UNREACHABLE(); // Should have found a matching constructor
2333
2334 return typeName;
2335}
2336
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002337TString OutputHLSL::decorate(const TString &string)
2338{
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +00002339 if (string.substr(0, 3) != "gl_" && string.substr(0, 3) != "dx_")
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002340 {
2341 return "_" + string;
2342 }
2343 else
2344 {
2345 return string;
2346 }
2347}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348}