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