blob: 6057e6bdce6755ed3273bb886b0f135ed66a9234 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
alokp@chromium.org91b72322010-06-02 15:50:56 +000011#include "compiler/debug.h"
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000012#include "compiler/DetectDiscontinuity.h"
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000013#include "compiler/InfoSink.h"
14#include "compiler/SearchSymbol.h"
15#include "compiler/UnfoldShortCircuit.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000017#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000018#include <cfloat>
19#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000020
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021namespace sh
22{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000023// Integer to TString conversion
24TString str(int i)
25{
26 char buffer[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000027 snprintf(buffer, sizeof(buffer), "%d", i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000028 return buffer;
29}
30
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000031OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000032 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000034 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000035 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000036
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000037 mUsesTexture2D = false;
38 mUsesTexture2D_bias = false;
39 mUsesTexture2DProj = false;
40 mUsesTexture2DProj_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000041 mUsesTexture2DProjLod = false;
42 mUsesTexture2DLod = false;
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000043 mUsesTextureCube = false;
44 mUsesTextureCube_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000045 mUsesTextureCubeLod = false;
daniel@transgaming.coma54f5182012-05-31 01:20:16 +000046 mUsesTexture2DLod0 = false;
47 mUsesTexture2DLod0_bias = false;
48 mUsesTexture2DProjLod0 = false;
49 mUsesTexture2DProjLod0_bias = false;
50 mUsesTextureCubeLod0 = false;
51 mUsesTextureCubeLod0_bias = false;
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000052 mUsesFragColor = false;
53 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000054 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000055 mUsesFragCoord = false;
56 mUsesPointCoord = false;
57 mUsesFrontFacing = false;
58 mUsesPointSize = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000059 mUsesXor = false;
60 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +000061 mUsesMod2v = false;
62 mUsesMod2f = false;
63 mUsesMod3v = false;
64 mUsesMod3f = false;
65 mUsesMod4v = false;
66 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +000067 mUsesFaceforward1 = false;
68 mUsesFaceforward2 = false;
69 mUsesFaceforward3 = false;
70 mUsesFaceforward4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000071 mUsesEqualMat2 = false;
72 mUsesEqualMat3 = false;
73 mUsesEqualMat4 = false;
74 mUsesEqualVec2 = false;
75 mUsesEqualVec3 = false;
76 mUsesEqualVec4 = false;
77 mUsesEqualIVec2 = false;
78 mUsesEqualIVec3 = false;
79 mUsesEqualIVec4 = false;
80 mUsesEqualBVec2 = false;
81 mUsesEqualBVec3 = false;
82 mUsesEqualBVec4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +000083 mUsesAtan2_1 = false;
84 mUsesAtan2_2 = false;
85 mUsesAtan2_3 = false;
86 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +000087
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000088 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
89
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000090 mScopeDepth = 0;
91
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +000092 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000093
94 mContainsLoopDiscontinuity = false;
95 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +000096 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +000097
98 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +000099
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000100 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000101 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000102 if (mContext.shaderType == SH_FRAGMENT_SHADER)
103 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000104 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000105 }
106 else
107 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000108 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000109 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000110 }
111 else
112 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000113 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000114 }
115
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000116 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000117 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118}
119
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000120OutputHLSL::~OutputHLSL()
121{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000122 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000123}
124
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000125void OutputHLSL::output()
126{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000127 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000128
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000129 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 +0000130 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000131
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000132 mContext.infoSink().obj << mHeader.c_str();
133 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000134}
135
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000136TInfoSinkBase &OutputHLSL::getBodyStream()
137{
138 return mBody;
139}
140
daniel@transgaming.com043da132012-12-20 21:12:22 +0000141const ActiveUniforms &OutputHLSL::getUniforms()
142{
143 return mActiveUniforms;
144}
145
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000146const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
147{
148 return mActiveInterfaceBlocks;
149}
150
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000151int OutputHLSL::vectorSize(const TType &type) const
152{
153 int elementSize = type.isMatrix() ? type.getNominalSize() : 1;
154 int arraySize = type.isArray() ? type.getArraySize() : 1;
155
156 return elementSize * arraySize;
157}
158
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000159TString OutputHLSL::interfaceBlockUniformName(const TType &interfaceBlockType, const TType &uniformType)
160{
161 if (interfaceBlockType.hasInstanceName())
162 {
163 return interfaceBlockType.getTypeName() + "." + uniformType.getFieldName();
164 }
165 else
166 {
167 return uniformType.getFieldName();
168 }
169}
170
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171void OutputHLSL::header()
172{
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000173 ShShaderType shaderType = mContext.shaderType;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000174 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000176 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000177 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000178 out << *structDeclaration;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000179 }
180
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000181 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000182 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000183 out << *constructor;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000184 }
185
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000186 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000187 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000188 TString varyings;
189 TString attributes;
190
191 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
192 {
193 const TType &type = uniform->second->getType();
194 const TString &name = uniform->second->getSymbol();
195
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000196 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
197 {
198 int index = samplerRegister(mReferencedUniforms[name]);
199
200 uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) +
201 " : register(s" + str(index) + ");\n";
202
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000203 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000204 " : register(t" + str(index) + ");\n";
205 }
206 else
207 {
208 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
209 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
210 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000211 }
212
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000213 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
214 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000215 const TType &nodeType = interfaceBlockIt->second->getType();
216 const TType &interfaceBlockType = nodeType.isInterfaceBlockMember() ? *nodeType.getInterfaceBlockType() : nodeType;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000217 const TString &blockName = interfaceBlockType.getTypeName();
218 const TTypeList &typeList = *interfaceBlockType.getStruct();
219
220 sh::InterfaceBlock interfaceBlock(blockName.c_str(), 0, mInterfaceBlockRegister++);
221 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
222 {
223 const TType &memberType = *typeList[typeIndex].type;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000224 const TString &fullUniformName = interfaceBlockUniformName(interfaceBlockType, memberType);
225 declareUniformToList(memberType, fullUniformName, typeIndex, interfaceBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000226 }
227
228 // TODO: handle other block layouts
229 interfaceBlock.setPackedBlockLayout();
230 mActiveInterfaceBlocks.push_back(interfaceBlock);
231
232 interfaceBlocks += "cbuffer " + blockName + " : register(b" + str(interfaceBlock.registerIndex) + ")\n"
233 "{\n";
234
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000235 if (interfaceBlockType.hasInstanceName())
236 {
237 interfaceBlocks += " struct {\n";
238 }
239
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000240 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
241 {
242 // TODO: padding for standard layout
243 const TType &memberType = *typeList[typeIndex].type;
244 interfaceBlocks += " " + typeString(memberType) + " " + decorate(memberType.getFieldName()) + arrayString(memberType) + ";\n";
245 }
246
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000247 if (interfaceBlockType.hasInstanceName())
248 {
249 interfaceBlocks += " } " + decorate(interfaceBlockType.getInstanceName()) + ";\n";
250 }
251
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000252 interfaceBlocks += "};\n\n";
253 }
254
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000255 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
256 {
257 const TType &type = varying->second->getType();
258 const TString &name = varying->second->getSymbol();
259
260 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000261 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
262 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000263 }
264
265 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
266 {
267 const TType &type = attribute->second->getType();
268 const TString &name = attribute->second->getSymbol();
269
270 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
271 }
272
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000273 if (shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000275 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000276 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000277
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000278 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000279
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000280 out << "// Varyings\n";
281 out << varyings;
282 out << "\n"
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000283 "static float4 gl_Color[" << numColorValues << "] =\n"
284 "{\n";
285 for (unsigned int i = 0; i < numColorValues; i++)
286 {
287 out << " float4(0, 0, 0, 0)";
288 if (i + 1 != numColorValues)
289 {
290 out << ",";
291 }
292 out << "\n";
293 }
294 out << "};\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000295
296 if (mUsesFragCoord)
297 {
298 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
299 }
300
301 if (mUsesPointCoord)
302 {
303 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
304 }
305
306 if (mUsesFrontFacing)
307 {
308 out << "static bool gl_FrontFacing = false;\n";
309 }
310
311 out << "\n";
312
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000313 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000314 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000315 out << "struct gl_DepthRangeParameters\n"
316 "{\n"
317 " float near;\n"
318 " float far;\n"
319 " float diff;\n"
320 "};\n"
321 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000322 }
323
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000324 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000325 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000326 out << "cbuffer DriverConstants : register(b1)\n"
327 "{\n";
328
329 if (mUsesDepthRange)
330 {
331 out << " float3 dx_DepthRange : packoffset(c0);\n";
332 }
333
334 if (mUsesFragCoord)
335 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000336 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000337 }
338
339 if (mUsesFragCoord || mUsesFrontFacing)
340 {
341 out << " float3 dx_DepthFront : packoffset(c2);\n";
342 }
343
344 out << "};\n";
345 }
346 else
347 {
348 if (mUsesDepthRange)
349 {
350 out << "uniform float3 dx_DepthRange : register(c0);";
351 }
352
353 if (mUsesFragCoord)
354 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000355 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000356 }
357
358 if (mUsesFragCoord || mUsesFrontFacing)
359 {
360 out << "uniform float3 dx_DepthFront : register(c2);\n";
361 }
362 }
363
364 out << "\n";
365
366 if (mUsesDepthRange)
367 {
368 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
369 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000370 }
371
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000373 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000374
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000375 if (!interfaceBlocks.empty())
376 {
377 out << interfaceBlocks;
378 out << "\n";
379 }
380
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000381 if (mUsesTexture2D)
382 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000383 if (mOutputType == SH_HLSL9_OUTPUT)
384 {
385 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
386 "{\n"
387 " return tex2D(s, t);\n"
388 "}\n"
389 "\n";
390 }
391 else if (mOutputType == SH_HLSL11_OUTPUT)
392 {
393 out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv)\n"
394 "{\n"
395 " return t.Sample(s, uv);\n"
396 "}\n"
397 "\n";
398 }
399 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000400 }
401
402 if (mUsesTexture2D_bias)
403 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000404 if (mOutputType == SH_HLSL9_OUTPUT)
405 {
406 out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n"
407 "{\n"
408 " return tex2Dbias(s, float4(t.x, t.y, 0, bias));\n"
409 "}\n"
410 "\n";
411 }
412 else if (mOutputType == SH_HLSL11_OUTPUT)
413 {
414 out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv, float bias)\n"
415 "{\n"
416 " return t.SampleBias(s, uv, bias);\n"
417 "}\n"
418 "\n";
419 }
420 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000421 }
422
423 if (mUsesTexture2DProj)
424 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000425 if (mOutputType == SH_HLSL9_OUTPUT)
426 {
427 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
428 "{\n"
429 " return tex2Dproj(s, float4(t.x, t.y, 0, t.z));\n"
430 "}\n"
431 "\n"
432 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
433 "{\n"
434 " return tex2Dproj(s, t);\n"
435 "}\n"
436 "\n";
437 }
438 else if (mOutputType == SH_HLSL11_OUTPUT)
439 {
440 out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw)\n"
441 "{\n"
442 " return t.Sample(s, float2(uvw.x / uvw.z, uvw.y / uvw.z));\n"
443 "}\n"
444 "\n"
445 "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n"
446 "{\n"
447 " return t.Sample(s, float2(uvw.x / uvw.w, uvw.y / uvw.w));\n"
448 "}\n"
449 "\n";
450 }
451 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000452 }
453
454 if (mUsesTexture2DProj_bias)
455 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000456 if (mOutputType == SH_HLSL9_OUTPUT)
457 {
458 out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n"
459 "{\n"
460 " return tex2Dbias(s, float4(t.x / t.z, t.y / t.z, 0, bias));\n"
461 "}\n"
462 "\n"
463 "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n"
464 "{\n"
465 " return tex2Dbias(s, float4(t.x / t.w, t.y / t.w, 0, bias));\n"
466 "}\n"
467 "\n";
468 }
469 else if (mOutputType == SH_HLSL11_OUTPUT)
470 {
471 out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw, float bias)\n"
472 "{\n"
473 " return t.SampleBias(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), bias);\n"
474 "}\n"
475 "\n"
476 "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw, float bias)\n"
477 "{\n"
478 " return t.SampleBias(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), bias);\n"
479 "}\n"
480 "\n";
481 }
482 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000483 }
484
485 if (mUsesTextureCube)
486 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000487 if (mOutputType == SH_HLSL9_OUTPUT)
488 {
489 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
490 "{\n"
491 " return texCUBE(s, t);\n"
492 "}\n"
493 "\n";
494 }
495 else if (mOutputType == SH_HLSL11_OUTPUT)
496 {
497 out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw)\n"
498 "{\n"
499 " return t.Sample(s, uvw);\n"
500 "}\n"
501 "\n";
502 }
503 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000504 }
505
506 if (mUsesTextureCube_bias)
507 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000508 if (mOutputType == SH_HLSL9_OUTPUT)
509 {
510 out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n"
511 "{\n"
512 " return texCUBEbias(s, float4(t.x, t.y, t.z, bias));\n"
513 "}\n"
514 "\n";
515 }
516 else if (mOutputType == SH_HLSL11_OUTPUT)
517 {
518 out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw, float bias)\n"
519 "{\n"
520 " return t.SampleBias(s, uvw, bias);\n"
521 "}\n"
522 "\n";
523 }
524 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000525 }
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000526
527 // These *Lod0 intrinsics are not available in GL fragment shaders.
528 // They are used to sample using discontinuous texture coordinates.
529 if (mUsesTexture2DLod0)
530 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000531 if (mOutputType == SH_HLSL9_OUTPUT)
532 {
533 out << "float4 gl_texture2DLod0(sampler2D s, float2 t)\n"
534 "{\n"
535 " return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n"
536 "}\n"
537 "\n";
538 }
539 else if (mOutputType == SH_HLSL11_OUTPUT)
540 {
541 out << "float4 gl_texture2DLod0(Texture2D t, SamplerState s, float2 uv)\n"
542 "{\n"
543 " return t.SampleLevel(s, uv, 0);\n"
544 "}\n"
545 "\n";
546 }
547 else UNREACHABLE();
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000548 }
549
550 if (mUsesTexture2DLod0_bias)
551 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000552 if (mOutputType == SH_HLSL9_OUTPUT)
553 {
554 out << "float4 gl_texture2DLod0(sampler2D s, float2 t, float bias)\n"
555 "{\n"
556 " return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n"
557 "}\n"
558 "\n";
559 }
560 else if (mOutputType == SH_HLSL11_OUTPUT)
561 {
562 out << "float4 gl_texture2DLod0(Texture2D t, SamplerState s, float2 uv, float bias)\n"
563 "{\n"
564 " return t.SampleLevel(s, uv, 0);\n"
565 "}\n"
566 "\n";
567 }
568 else UNREACHABLE();
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000569 }
570
571 if (mUsesTexture2DProjLod0)
572 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000573 if (mOutputType == SH_HLSL9_OUTPUT)
574 {
575 out << "float4 gl_texture2DProjLod0(sampler2D s, float3 t)\n"
576 "{\n"
577 " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n"
578 "}\n"
579 "\n"
580 "float4 gl_texture2DProjLod(sampler2D s, float4 t)\n"
581 "{\n"
582 " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n"
583 "}\n"
584 "\n";
585 }
586 else if (mOutputType == SH_HLSL11_OUTPUT)
587 {
588 out << "float4 gl_texture2DProjLod0(Texture2D t, SamplerState s, float3 uvw)\n"
589 "{\n"
590 " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n"
591 "}\n"
592 "\n"
593 "float4 gl_texture2DProjLod0(Texture2D t, SamplerState s, float4 uvw)\n"
594 "{\n"
595 " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n"
596 "}\n"
597 "\n";
598 }
599 else UNREACHABLE();
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000600 }
601
602 if (mUsesTexture2DProjLod0_bias)
603 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000604 if (mOutputType == SH_HLSL9_OUTPUT)
605 {
606 out << "float4 gl_texture2DProjLod0_bias(sampler2D s, float3 t, float bias)\n"
607 "{\n"
608 " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n"
609 "}\n"
610 "\n"
611 "float4 gl_texture2DProjLod_bias(sampler2D s, float4 t, float bias)\n"
612 "{\n"
613 " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n"
614 "}\n"
615 "\n";
616 }
617 else if (mOutputType == SH_HLSL11_OUTPUT)
618 {
619 out << "float4 gl_texture2DProjLod_bias(Texture2D t, SamplerState s, float3 uvw, float bias)\n"
620 "{\n"
621 " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n"
622 "}\n"
623 "\n"
624 "float4 gl_texture2DProjLod_bias(Texture2D t, SamplerState s, float4 uvw, float bias)\n"
625 "{\n"
626 " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n"
627 "}\n"
628 "\n";
629 }
630 else UNREACHABLE();
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000631 }
632
633 if (mUsesTextureCubeLod0)
634 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000635 if (mOutputType == SH_HLSL9_OUTPUT)
636 {
637 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t)\n"
638 "{\n"
639 " return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n"
640 "}\n"
641 "\n";
642 }
643 else if (mOutputType == SH_HLSL11_OUTPUT)
644 {
645 out << "float4 gl_textureCubeLod0(TextureCube t, SamplerState s, float3 uvw)\n"
646 "{\n"
647 " return t.SampleLevel(s, uvw, 0);\n"
648 "}\n"
649 "\n";
650 }
651 else UNREACHABLE();
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000652 }
653
654 if (mUsesTextureCubeLod0_bias)
655 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000656 if (mOutputType == SH_HLSL9_OUTPUT)
657 {
658 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t, float bias)\n"
659 "{\n"
660 " return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n"
661 "}\n"
662 "\n";
663 }
664 else if (mOutputType == SH_HLSL11_OUTPUT)
665 {
666 out << "float4 gl_textureCubeLod0(TextureCube t, SamplerState s, float3 uvw, float bias)\n"
667 "{\n"
668 " return t.SampleLevel(s, uvw, 0);\n"
669 "}\n"
670 "\n";
671 }
672 else UNREACHABLE();
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000673 }
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000674
675 if (usingMRTExtension && mNumRenderTargets > 1)
676 {
677 out << "#define GL_USES_MRT\n";
678 }
679
680 if (mUsesFragColor)
681 {
682 out << "#define GL_USES_FRAG_COLOR\n";
683 }
684
685 if (mUsesFragData)
686 {
687 out << "#define GL_USES_FRAG_DATA\n";
688 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000690 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000692 out << "// Attributes\n";
693 out << attributes;
694 out << "\n"
695 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
696
697 if (mUsesPointSize)
698 {
699 out << "static float gl_PointSize = float(1);\n";
700 }
701
702 out << "\n"
703 "// Varyings\n";
704 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000705 out << "\n";
706
707 if (mUsesDepthRange)
708 {
709 out << "struct gl_DepthRangeParameters\n"
710 "{\n"
711 " float near;\n"
712 " float far;\n"
713 " float diff;\n"
714 "};\n"
715 "\n";
716 }
717
718 if (mOutputType == SH_HLSL11_OUTPUT)
719 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000720 if (mUsesDepthRange)
721 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000722 out << "cbuffer DriverConstants : register(b1)\n"
723 "{\n"
724 " float3 dx_DepthRange : packoffset(c0);\n"
725 "};\n"
726 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000727 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000728 }
729 else
730 {
731 if (mUsesDepthRange)
732 {
733 out << "uniform float3 dx_DepthRange : register(c0);\n";
734 }
735
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000736 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000737 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000738 }
739
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000740 if (mUsesDepthRange)
741 {
742 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
743 "\n";
744 }
745
746 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000747 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000748
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000749 if (!interfaceBlocks.empty())
750 {
751 out << interfaceBlocks;
752 out << "\n";
753 }
754
daniel@transgaming.com15795192011-05-11 15:36:20 +0000755 if (mUsesTexture2D)
756 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000757 if (mOutputType == SH_HLSL9_OUTPUT)
758 {
759 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
760 "{\n"
761 " return tex2Dlod(s, float4(t.x, t.y, 0, 0));\n"
762 "}\n"
763 "\n";
764 }
765 else if (mOutputType == SH_HLSL11_OUTPUT)
766 {
767 out << "float4 gl_texture2D(Texture2D t, SamplerState s, float2 uv)\n"
768 "{\n"
769 " return t.SampleLevel(s, uv, 0);\n"
770 "}\n"
771 "\n";
772 }
773 else UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000774 }
775
776 if (mUsesTexture2DLod)
777 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000778 if (mOutputType == SH_HLSL9_OUTPUT)
779 {
780 out << "float4 gl_texture2DLod(sampler2D s, float2 t, float lod)\n"
781 "{\n"
782 " return tex2Dlod(s, float4(t.x, t.y, 0, lod));\n"
783 "}\n"
784 "\n";
785 }
786 else if (mOutputType == SH_HLSL11_OUTPUT)
787 {
788 out << "float4 gl_texture2DLod(Texture2D t, SamplerState s, float2 uv, float lod)\n"
789 "{\n"
790 " return t.SampleLevel(s, uv, lod);\n"
791 "}\n"
792 "\n";
793 }
794 else UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000795 }
796
797 if (mUsesTexture2DProj)
798 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000799 if (mOutputType == SH_HLSL9_OUTPUT)
800 {
801 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
802 "{\n"
803 " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, 0));\n"
804 "}\n"
805 "\n"
806 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
807 "{\n"
808 " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, 0));\n"
809 "}\n"
810 "\n";
811 }
812 else if (mOutputType == SH_HLSL11_OUTPUT)
813 {
814 out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw)\n"
815 "{\n"
816 " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), 0);\n"
817 "}\n"
818 "\n"
819 "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n"
820 "{\n"
821 " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), 0);\n"
822 "}\n"
823 "\n";
824 }
825 else UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000826 }
827
828 if (mUsesTexture2DProjLod)
829 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000830 if (mOutputType == SH_HLSL9_OUTPUT)
831 {
832 out << "float4 gl_texture2DProjLod(sampler2D s, float3 t, float lod)\n"
833 "{\n"
834 " return tex2Dlod(s, float4(t.x / t.z, t.y / t.z, 0, lod));\n"
835 "}\n"
836 "\n"
837 "float4 gl_texture2DProjLod(sampler2D s, float4 t, float lod)\n"
838 "{\n"
839 " return tex2Dlod(s, float4(t.x / t.w, t.y / t.w, 0, lod));\n"
840 "}\n"
841 "\n";
842 }
843 else if (mOutputType == SH_HLSL11_OUTPUT)
844 {
845 out << "float4 gl_texture2DProj(Texture2D t, SamplerState s, float3 uvw, float lod)\n"
846 "{\n"
847 " return t.SampleLevel(s, float2(uvw.x / uvw.z, uvw.y / uvw.z), lod);\n"
848 "}\n"
849 "\n"
850 "float4 gl_texture2DProj(Texture2D t, SamplerState s, float4 uvw)\n"
851 "{\n"
852 " return t.SampleLevel(s, float2(uvw.x / uvw.w, uvw.y / uvw.w), lod);\n"
853 "}\n"
854 "\n";
855 }
856 else UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000857 }
858
859 if (mUsesTextureCube)
860 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000861 if (mOutputType == SH_HLSL9_OUTPUT)
862 {
863 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
864 "{\n"
865 " return texCUBElod(s, float4(t.x, t.y, t.z, 0));\n"
866 "}\n"
867 "\n";
868 }
869 else if (mOutputType == SH_HLSL11_OUTPUT)
870 {
871 out << "float4 gl_textureCube(TextureCube t, SamplerState s, float3 uvw)\n"
872 "{\n"
873 " return t.SampleLevel(s, uvw, 0);\n"
874 "}\n"
875 "\n";
876 }
877 else UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000878 }
879
880 if (mUsesTextureCubeLod)
881 {
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000882 if (mOutputType == SH_HLSL9_OUTPUT)
883 {
884 out << "float4 gl_textureCubeLod(samplerCUBE s, float3 t, float lod)\n"
885 "{\n"
886 " return texCUBElod(s, float4(t.x, t.y, t.z, lod));\n"
887 "}\n"
888 "\n";
889 }
890 else if (mOutputType == SH_HLSL11_OUTPUT)
891 {
892 out << "float4 gl_textureCubeLod(TextureCube t, SamplerState s, float3 uvw, float lod)\n"
893 "{\n"
894 " return t.SampleLevel(s, uvw, lod);\n"
895 "}\n"
896 "\n";
897 }
898 else UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000899 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900 }
901
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000902 if (mUsesFragCoord)
903 {
904 out << "#define GL_USES_FRAG_COORD\n";
905 }
906
907 if (mUsesPointCoord)
908 {
909 out << "#define GL_USES_POINT_COORD\n";
910 }
911
912 if (mUsesFrontFacing)
913 {
914 out << "#define GL_USES_FRONT_FACING\n";
915 }
916
917 if (mUsesPointSize)
918 {
919 out << "#define GL_USES_POINT_SIZE\n";
920 }
921
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000922 if (mUsesDepthRange)
923 {
924 out << "#define GL_USES_DEPTH_RANGE\n";
925 }
926
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000927 if (mUsesXor)
928 {
929 out << "bool xor(bool p, bool q)\n"
930 "{\n"
931 " return (p || q) && !(p && q);\n"
932 "}\n"
933 "\n";
934 }
935
936 if (mUsesMod1)
937 {
938 out << "float mod(float x, float y)\n"
939 "{\n"
940 " return x - y * floor(x / y);\n"
941 "}\n"
942 "\n";
943 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000944
945 if (mUsesMod2v)
946 {
947 out << "float2 mod(float2 x, float2 y)\n"
948 "{\n"
949 " return x - y * floor(x / y);\n"
950 "}\n"
951 "\n";
952 }
953
954 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000955 {
956 out << "float2 mod(float2 x, float y)\n"
957 "{\n"
958 " return x - y * floor(x / y);\n"
959 "}\n"
960 "\n";
961 }
962
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000963 if (mUsesMod3v)
964 {
965 out << "float3 mod(float3 x, float3 y)\n"
966 "{\n"
967 " return x - y * floor(x / y);\n"
968 "}\n"
969 "\n";
970 }
971
972 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000973 {
974 out << "float3 mod(float3 x, float y)\n"
975 "{\n"
976 " return x - y * floor(x / y);\n"
977 "}\n"
978 "\n";
979 }
980
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000981 if (mUsesMod4v)
982 {
983 out << "float4 mod(float4 x, float4 y)\n"
984 "{\n"
985 " return x - y * floor(x / y);\n"
986 "}\n"
987 "\n";
988 }
989
990 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000991 {
992 out << "float4 mod(float4 x, float y)\n"
993 "{\n"
994 " return x - y * floor(x / y);\n"
995 "}\n"
996 "\n";
997 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000998
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000999 if (mUsesFaceforward1)
1000 {
1001 out << "float faceforward(float N, float I, float Nref)\n"
1002 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001003 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001004 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001005 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001006 " }\n"
1007 " else\n"
1008 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001009 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001010 " }\n"
1011 "}\n"
1012 "\n";
1013 }
1014
1015 if (mUsesFaceforward2)
1016 {
1017 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1018 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001019 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001020 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001021 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001022 " }\n"
1023 " else\n"
1024 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001025 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001026 " }\n"
1027 "}\n"
1028 "\n";
1029 }
1030
1031 if (mUsesFaceforward3)
1032 {
1033 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1034 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001035 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001036 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001037 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001038 " }\n"
1039 " else\n"
1040 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001041 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001042 " }\n"
1043 "}\n"
1044 "\n";
1045 }
1046
1047 if (mUsesFaceforward4)
1048 {
1049 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1050 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001051 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001052 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001053 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001054 " }\n"
1055 " else\n"
1056 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001057 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001058 " }\n"
1059 "}\n"
1060 "\n";
1061 }
1062
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001063 if (mUsesEqualMat2)
1064 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001065 out << "bool equal(float2x2 m, float2x2 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001066 "{\n"
1067 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
1068 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
1069 "}\n";
1070 }
1071
1072 if (mUsesEqualMat3)
1073 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001074 out << "bool equal(float3x3 m, float3x3 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001075 "{\n"
1076 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
1077 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
1078 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
1079 "}\n";
1080 }
1081
1082 if (mUsesEqualMat4)
1083 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001084 out << "bool equal(float4x4 m, float4x4 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001085 "{\n"
1086 " 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"
1087 " 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"
1088 " 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"
1089 " 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"
1090 "}\n";
1091 }
1092
1093 if (mUsesEqualVec2)
1094 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001095 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001096 "{\n"
1097 " return v.x == u.x && v.y == u.y;\n"
1098 "}\n";
1099 }
1100
1101 if (mUsesEqualVec3)
1102 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001103 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001104 "{\n"
1105 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1106 "}\n";
1107 }
1108
1109 if (mUsesEqualVec4)
1110 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001111 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001112 "{\n"
1113 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1114 "}\n";
1115 }
1116
1117 if (mUsesEqualIVec2)
1118 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001119 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001120 "{\n"
1121 " return v.x == u.x && v.y == u.y;\n"
1122 "}\n";
1123 }
1124
1125 if (mUsesEqualIVec3)
1126 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001127 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001128 "{\n"
1129 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1130 "}\n";
1131 }
1132
1133 if (mUsesEqualIVec4)
1134 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001135 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001136 "{\n"
1137 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1138 "}\n";
1139 }
1140
1141 if (mUsesEqualBVec2)
1142 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001143 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001144 "{\n"
1145 " return v.x == u.x && v.y == u.y;\n"
1146 "}\n";
1147 }
1148
1149 if (mUsesEqualBVec3)
1150 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001151 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001152 "{\n"
1153 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1154 "}\n";
1155 }
1156
1157 if (mUsesEqualBVec4)
1158 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001159 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001160 "{\n"
1161 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1162 "}\n";
1163 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001164
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001165 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001166 {
1167 out << "float atanyx(float y, float x)\n"
1168 "{\n"
1169 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1170 " return atan2(y, x);\n"
1171 "}\n";
1172 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001173
1174 if (mUsesAtan2_2)
1175 {
1176 out << "float2 atanyx(float2 y, float2 x)\n"
1177 "{\n"
1178 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1179 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1180 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1181 "}\n";
1182 }
1183
1184 if (mUsesAtan2_3)
1185 {
1186 out << "float3 atanyx(float3 y, float3 x)\n"
1187 "{\n"
1188 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1189 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1190 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1191 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1192 "}\n";
1193 }
1194
1195 if (mUsesAtan2_4)
1196 {
1197 out << "float4 atanyx(float4 y, float4 x)\n"
1198 "{\n"
1199 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1200 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1201 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1202 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1203 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1204 "}\n";
1205 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001206}
1207
1208void OutputHLSL::visitSymbol(TIntermSymbol *node)
1209{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001210 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001211
1212 TString name = node->getSymbol();
1213
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001214 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001215 {
1216 mUsesDepthRange = true;
1217 out << name;
1218 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001219 else
1220 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001221 TQualifier qualifier = node->getQualifier();
1222
1223 if (qualifier == EvqUniform)
1224 {
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001225 if (node->getType().isInterfaceBlockMember())
1226 {
1227 const TString& interfaceBlockTypeName = node->getType().getInterfaceBlockType()->getTypeName();
1228 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1229 out << decorateUniform(name, node->getType());
1230 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001231 else if (node->getBasicType() == EbtInterfaceBlock)
1232 {
1233 const TString& interfaceBlockTypeName = node->getType().getTypeName();
1234 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1235 out << decorateUniform(name, node->getType());
1236 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001237 else
1238 {
1239 mReferencedUniforms[name] = node;
1240 out << decorateUniform(name, node->getType());
1241 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001242 }
1243 else if (qualifier == EvqAttribute)
1244 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001245 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001246 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001247 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001248 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001249 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001250 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001251 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001252 }
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001253 else if (qualifier == EvqFragColor)
1254 {
1255 out << "gl_Color[0]";
1256 mUsesFragColor = true;
1257 }
1258 else if (qualifier == EvqFragData)
1259 {
1260 out << "gl_Color";
1261 mUsesFragData = true;
1262 }
1263 else if (qualifier == EvqFragCoord)
1264 {
1265 mUsesFragCoord = true;
1266 out << name;
1267 }
1268 else if (qualifier == EvqPointCoord)
1269 {
1270 mUsesPointCoord = true;
1271 out << name;
1272 }
1273 else if (qualifier == EvqFrontFacing)
1274 {
1275 mUsesFrontFacing = true;
1276 out << name;
1277 }
1278 else if (qualifier == EvqPointSize)
1279 {
1280 mUsesPointSize = true;
1281 out << name;
1282 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001283 else
1284 {
1285 out << decorate(name);
1286 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287 }
1288}
1289
1290bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1291{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001292 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001293
1294 switch (node->getOp())
1295 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001296 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001297 case EOpInitialize:
1298 if (visit == PreVisit)
1299 {
1300 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1301 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1302 // new variable is created before the assignment is evaluated), so we need to convert
1303 // this to "float t = x, x = t;".
1304
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001305 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1306 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001307
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001308 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1309 expression->traverse(&searchSymbol);
1310 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001311
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001312 if (sameSymbol)
1313 {
1314 // Type already printed
1315 out << "t" + str(mUniqueIndex) + " = ";
1316 expression->traverse(this);
1317 out << ", ";
1318 symbolNode->traverse(this);
1319 out << " = t" + str(mUniqueIndex);
1320
1321 mUniqueIndex++;
1322 return false;
1323 }
1324 }
1325 else if (visit == InVisit)
1326 {
1327 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001328 }
1329 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001330 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1331 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1332 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1333 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1334 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1335 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001336 if (visit == PreVisit)
1337 {
1338 out << "(";
1339 }
1340 else if (visit == InVisit)
1341 {
1342 out << " = mul(";
1343 node->getLeft()->traverse(this);
1344 out << ", transpose(";
1345 }
1346 else
1347 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001348 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001349 }
1350 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001351 case EOpMatrixTimesMatrixAssign:
1352 if (visit == PreVisit)
1353 {
1354 out << "(";
1355 }
1356 else if (visit == InVisit)
1357 {
1358 out << " = mul(";
1359 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001360 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001361 }
1362 else
1363 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001364 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001365 }
1366 break;
1367 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001368 case EOpIndexDirect: outputTriplet(visit, "", "[", "]"); break;
1369 case EOpIndexIndirect: outputTriplet(visit, "", "[", "]"); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001370 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001371 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001372 if (visit == InVisit)
1373 {
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001374 out << "." + decorateField(node->getType().getFieldName(), node->getLeft()->getType());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001375
1376 return false;
1377 }
1378 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379 case EOpVectorSwizzle:
1380 if (visit == InVisit)
1381 {
1382 out << ".";
1383
1384 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1385
1386 if (swizzle)
1387 {
1388 TIntermSequence &sequence = swizzle->getSequence();
1389
1390 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1391 {
1392 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1393
1394 if (element)
1395 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001396 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001397
1398 switch (i)
1399 {
1400 case 0: out << "x"; break;
1401 case 1: out << "y"; break;
1402 case 2: out << "z"; break;
1403 case 3: out << "w"; break;
1404 default: UNREACHABLE();
1405 }
1406 }
1407 else UNREACHABLE();
1408 }
1409 }
1410 else UNREACHABLE();
1411
1412 return false; // Fully processed
1413 }
1414 break;
1415 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1416 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1417 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1418 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001419 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001420 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001421 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001422 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001423 if (node->getOp() == EOpEqual)
1424 {
1425 outputTriplet(visit, "(", " == ", ")");
1426 }
1427 else
1428 {
1429 outputTriplet(visit, "(", " != ", ")");
1430 }
1431 }
1432 else if (node->getLeft()->getBasicType() == EbtStruct)
1433 {
1434 if (node->getOp() == EOpEqual)
1435 {
1436 out << "(";
1437 }
1438 else
1439 {
1440 out << "!(";
1441 }
1442
1443 const TTypeList *fields = node->getLeft()->getType().getStruct();
1444
1445 for (size_t i = 0; i < fields->size(); i++)
1446 {
1447 const TType *fieldType = (*fields)[i].type;
1448
1449 node->getLeft()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001450 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType()) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001451 node->getRight()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001452 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001453
1454 if (i < fields->size() - 1)
1455 {
1456 out << " && ";
1457 }
1458 }
1459
1460 out << ")";
1461
1462 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001463 }
1464 else
1465 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001466 if (node->getLeft()->isMatrix())
1467 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001468 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001469 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001470 case 2: mUsesEqualMat2 = true; break;
1471 case 3: mUsesEqualMat3 = true; break;
1472 case 4: mUsesEqualMat4 = true; break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001473 default: UNREACHABLE();
1474 }
1475 }
1476 else if (node->getLeft()->isVector())
1477 {
1478 switch (node->getLeft()->getBasicType())
1479 {
1480 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001481 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001482 {
1483 case 2: mUsesEqualVec2 = true; break;
1484 case 3: mUsesEqualVec3 = true; break;
1485 case 4: mUsesEqualVec4 = true; break;
1486 default: UNREACHABLE();
1487 }
1488 break;
1489 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001490 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001491 {
1492 case 2: mUsesEqualIVec2 = true; break;
1493 case 3: mUsesEqualIVec3 = true; break;
1494 case 4: mUsesEqualIVec4 = true; break;
1495 default: UNREACHABLE();
1496 }
1497 break;
1498 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001499 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001500 {
1501 case 2: mUsesEqualBVec2 = true; break;
1502 case 3: mUsesEqualBVec3 = true; break;
1503 case 4: mUsesEqualBVec4 = true; break;
1504 default: UNREACHABLE();
1505 }
1506 break;
1507 default: UNREACHABLE();
1508 }
1509 }
1510 else UNREACHABLE();
1511
1512 if (node->getOp() == EOpEqual)
1513 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001514 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001515 }
1516 else
1517 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001518 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001519 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001520 }
1521 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1523 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1524 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1525 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1526 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001527 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001528 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1529 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001530 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001531 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001532 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001533 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001534 case EOpLogicalXor:
1535 mUsesXor = true;
1536 outputTriplet(visit, "xor(", ", ", ")");
1537 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001538 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001539 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001540 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001541 default: UNREACHABLE();
1542 }
1543
1544 return true;
1545}
1546
1547bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1548{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001549 switch (node->getOp())
1550 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001551 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1552 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1553 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1554 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1555 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1556 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1557 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001558 case EOpConvIntToBool:
1559 case EOpConvFloatToBool:
1560 switch (node->getOperand()->getType().getNominalSize())
1561 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001562 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1563 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1564 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1565 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001566 default: UNREACHABLE();
1567 }
1568 break;
1569 case EOpConvBoolToFloat:
1570 case EOpConvIntToFloat:
1571 switch (node->getOperand()->getType().getNominalSize())
1572 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001573 case 1: outputTriplet(visit, "float(", "", ")"); break;
1574 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1575 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1576 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577 default: UNREACHABLE();
1578 }
1579 break;
1580 case EOpConvFloatToInt:
1581 case EOpConvBoolToInt:
1582 switch (node->getOperand()->getType().getNominalSize())
1583 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001584 case 1: outputTriplet(visit, "int(", "", ")"); break;
1585 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1586 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1587 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588 default: UNREACHABLE();
1589 }
1590 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001591 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1592 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1593 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1594 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1595 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1596 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1597 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1598 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1599 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1600 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1601 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1602 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1603 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1604 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1605 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1606 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1607 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1608 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1609 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1610 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1611 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001612 case EOpDFdx:
1613 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1614 {
1615 outputTriplet(visit, "(", "", ", 0.0)");
1616 }
1617 else
1618 {
1619 outputTriplet(visit, "ddx(", "", ")");
1620 }
1621 break;
1622 case EOpDFdy:
1623 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1624 {
1625 outputTriplet(visit, "(", "", ", 0.0)");
1626 }
1627 else
1628 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001629 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001630 }
1631 break;
1632 case EOpFwidth:
1633 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1634 {
1635 outputTriplet(visit, "(", "", ", 0.0)");
1636 }
1637 else
1638 {
1639 outputTriplet(visit, "fwidth(", "", ")");
1640 }
1641 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001642 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1643 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001644 default: UNREACHABLE();
1645 }
1646
1647 return true;
1648}
1649
1650bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1651{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001652 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654 switch (node->getOp())
1655 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001656 case EOpSequence:
1657 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001658 if (mInsideFunction)
1659 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001660 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001661 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001662
1663 mScopeDepth++;
1664
1665 if (mScopeBracket.size() < mScopeDepth)
1666 {
1667 mScopeBracket.push_back(0); // New scope level
1668 }
1669 else
1670 {
1671 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1672 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001673 }
1674
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001675 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1676 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001677 outputLineDirective((*sit)->getLine());
1678
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001679 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001680
1681 out << ";\n";
1682 }
1683
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001684 if (mInsideFunction)
1685 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001686 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001687 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001688
1689 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001690 }
1691
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001692 return false;
1693 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694 case EOpDeclaration:
1695 if (visit == PreVisit)
1696 {
1697 TIntermSequence &sequence = node->getSequence();
1698 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001699
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001700 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001701 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001702 if (variable->getType().getStruct())
1703 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001704 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001705 }
1706
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001707 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001708 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001709 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001710 {
1711 out << "static ";
1712 }
1713
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001714 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001716 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001718 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001720 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001722 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001723 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001724 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001725 }
1726 else
1727 {
1728 (*sit)->traverse(this);
1729 }
1730
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001731 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001732 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001733 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734 }
1735 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001737 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1738 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001739 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001740 }
1741 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001743 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001744 {
1745 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1746 {
1747 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1748
1749 if (symbol)
1750 {
1751 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1752 mReferencedVaryings[symbol->getSymbol()] = symbol;
1753 }
1754 else
1755 {
1756 (*sit)->traverse(this);
1757 }
1758 }
1759 }
1760
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761 return false;
1762 }
1763 else if (visit == InVisit)
1764 {
1765 out << ", ";
1766 }
1767 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001768 case EOpPrototype:
1769 if (visit == PreVisit)
1770 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001771 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001772
1773 TIntermSequence &arguments = node->getSequence();
1774
1775 for (unsigned int i = 0; i < arguments.size(); i++)
1776 {
1777 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1778
1779 if (symbol)
1780 {
1781 out << argumentString(symbol);
1782
1783 if (i < arguments.size() - 1)
1784 {
1785 out << ", ";
1786 }
1787 }
1788 else UNREACHABLE();
1789 }
1790
1791 out << ");\n";
1792
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001793 // Also prototype the Lod0 variant if needed
1794 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1795 {
1796 mOutputLod0Function = true;
1797 node->traverse(this);
1798 mOutputLod0Function = false;
1799 }
1800
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001801 return false;
1802 }
1803 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001804 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805 case EOpFunction:
1806 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001807 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001809 out << typeString(node->getType()) << " ";
1810
1811 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001813 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001815 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001817 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001818 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001819
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001820 TIntermSequence &sequence = node->getSequence();
1821 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1822
1823 for (unsigned int i = 0; i < arguments.size(); i++)
1824 {
1825 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1826
1827 if (symbol)
1828 {
1829 if (symbol->getType().getStruct())
1830 {
1831 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getTypeName()), NULL);
1832 }
1833
1834 out << argumentString(symbol);
1835
1836 if (i < arguments.size() - 1)
1837 {
1838 out << ", ";
1839 }
1840 }
1841 else UNREACHABLE();
1842 }
1843
1844 out << ")\n"
1845 "{\n";
1846
1847 if (sequence.size() > 1)
1848 {
1849 mInsideFunction = true;
1850 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001851 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001853
1854 out << "}\n";
1855
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001856 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1857 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001858 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001859 {
1860 mOutputLod0Function = true;
1861 node->traverse(this);
1862 mOutputLod0Function = false;
1863 }
1864 }
1865
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001866 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001867 }
1868 break;
1869 case EOpFunctionCall:
1870 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001871 TString name = TFunction::unmangleName(node->getName());
1872 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001873 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001874
1875 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001877 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878 }
1879 else
1880 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001881 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
1882
1883 if (name == "texture2D" || (name == "texture" && samplerType == EbtSampler2D))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001884 {
1885 if (!lod0)
1886 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001887 if (arguments.size() == 2)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001888 {
1889 mUsesTexture2D = true;
1890 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001891 else if (arguments.size() == 3)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001892 {
1893 mUsesTexture2D_bias = true;
1894 }
1895 else UNREACHABLE();
1896
1897 out << "gl_texture2D(";
1898 }
1899 else
1900 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001901 if (arguments.size() == 2)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001902 {
1903 mUsesTexture2DLod0 = true;
1904 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001905 else if (arguments.size() == 3)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001906 {
1907 mUsesTexture2DLod0_bias = true;
1908 }
1909 else UNREACHABLE();
1910
1911 out << "gl_texture2DLod0(";
1912 }
1913 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001914 else if (name == "texture2DProj" || (name == "textureProj" && samplerType == EbtSampler2D))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001915 {
1916 if (!lod0)
1917 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001918 if (arguments.size() == 2)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001919 {
1920 mUsesTexture2DProj = true;
1921 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001922 else if (arguments.size() == 3)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001923 {
1924 mUsesTexture2DProj_bias = true;
1925 }
1926 else UNREACHABLE();
1927
1928 out << "gl_texture2DProj(";
1929 }
1930 else
1931 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001932 if (arguments.size() == 2)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001933 {
1934 mUsesTexture2DProjLod0 = true;
1935 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001936 else if (arguments.size() == 3)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001937 {
1938 mUsesTexture2DProjLod0_bias = true;
1939 }
1940 else UNREACHABLE();
1941
1942 out << "gl_texture2DProjLod0(";
1943 }
1944 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001945 else if (name == "textureCube" || (name == "texture" && samplerType == EbtSamplerCube))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001946 {
1947 if (!lod0)
1948 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001949 if (arguments.size() == 2)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001950 {
1951 mUsesTextureCube = true;
1952 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001953 else if (arguments.size() == 3)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001954 {
1955 mUsesTextureCube_bias = true;
1956 }
1957 else UNREACHABLE();
1958
1959 out << "gl_textureCube(";
1960 }
1961 else
1962 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001963 if (arguments.size() == 2)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001964 {
1965 mUsesTextureCubeLod0 = true;
1966 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001967 else if (arguments.size() == 3)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001968 {
1969 mUsesTextureCubeLod0_bias = true;
1970 }
1971 else UNREACHABLE();
1972
1973 out << "gl_textureCubeLod0(";
1974 }
1975 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001976 else if (name == "texture2DLod" || (name == "textureLod" && samplerType == EbtSampler2D))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001977 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001978 if (arguments.size() == 3)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001979 {
1980 mUsesTexture2DLod = true;
1981 }
1982 else UNREACHABLE();
1983
1984 out << "gl_texture2DLod(";
1985 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001986 else if (name == "texture2DProjLod" || (name == "textureProjLod" && samplerType == EbtSampler2D))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001987 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001988 if (arguments.size() == 3)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001989 {
1990 mUsesTexture2DProjLod = true;
1991 }
1992 else UNREACHABLE();
1993
1994 out << "gl_texture2DProjLod(";
1995 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001996 else if (name == "textureCubeLod" || (name == "textureLod" && samplerType == EbtSamplerCube))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001997 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00001998 if (arguments.size() == 3)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001999 {
2000 mUsesTextureCubeLod = true;
2001 }
2002 else UNREACHABLE();
2003
2004 out << "gl_textureCubeLod(";
2005 }
2006 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002008
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002009 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2010 {
2011 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2012 {
2013 out << "texture_";
2014 (*arg)->traverse(this);
2015 out << ", sampler_";
2016 }
2017
2018 (*arg)->traverse(this);
2019
2020 if (arg < arguments.end() - 1)
2021 {
2022 out << ", ";
2023 }
2024 }
2025
2026 out << ")";
2027
2028 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029 }
2030 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002031 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002032 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002033 addConstructor(node->getType(), "vec1", &node->getSequence());
2034 outputTriplet(visit, "vec1(", "", ")");
2035 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002036 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002037 addConstructor(node->getType(), "vec2", &node->getSequence());
2038 outputTriplet(visit, "vec2(", ", ", ")");
2039 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002040 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002041 addConstructor(node->getType(), "vec3", &node->getSequence());
2042 outputTriplet(visit, "vec3(", ", ", ")");
2043 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002044 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002045 addConstructor(node->getType(), "vec4", &node->getSequence());
2046 outputTriplet(visit, "vec4(", ", ", ")");
2047 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002048 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002049 addConstructor(node->getType(), "bvec1", &node->getSequence());
2050 outputTriplet(visit, "bvec1(", "", ")");
2051 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002052 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002053 addConstructor(node->getType(), "bvec2", &node->getSequence());
2054 outputTriplet(visit, "bvec2(", ", ", ")");
2055 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002056 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002057 addConstructor(node->getType(), "bvec3", &node->getSequence());
2058 outputTriplet(visit, "bvec3(", ", ", ")");
2059 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002060 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002061 addConstructor(node->getType(), "bvec4", &node->getSequence());
2062 outputTriplet(visit, "bvec4(", ", ", ")");
2063 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002064 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002065 addConstructor(node->getType(), "ivec1", &node->getSequence());
2066 outputTriplet(visit, "ivec1(", "", ")");
2067 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002068 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002069 addConstructor(node->getType(), "ivec2", &node->getSequence());
2070 outputTriplet(visit, "ivec2(", ", ", ")");
2071 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002072 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002073 addConstructor(node->getType(), "ivec3", &node->getSequence());
2074 outputTriplet(visit, "ivec3(", ", ", ")");
2075 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002076 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002077 addConstructor(node->getType(), "ivec4", &node->getSequence());
2078 outputTriplet(visit, "ivec4(", ", ", ")");
2079 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002080 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002081 addConstructor(node->getType(), "mat2", &node->getSequence());
2082 outputTriplet(visit, "mat2(", ", ", ")");
2083 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002084 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002085 addConstructor(node->getType(), "mat3", &node->getSequence());
2086 outputTriplet(visit, "mat3(", ", ", ")");
2087 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002088 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002089 addConstructor(node->getType(), "mat4", &node->getSequence());
2090 outputTriplet(visit, "mat4(", ", ", ")");
2091 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002092 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002093 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
2094 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002095 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002096 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2097 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2098 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2099 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2100 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2101 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002102 case EOpMod:
2103 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002104 // We need to look at the number of components in both arguments
2105 switch (node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2106 + node->getSequence()[1]->getAsTyped()->getNominalSize())
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002107 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002108 case 11: mUsesMod1 = true; break;
2109 case 22: mUsesMod2v = true; break;
2110 case 21: mUsesMod2f = true; break;
2111 case 33: mUsesMod3v = true; break;
2112 case 31: mUsesMod3f = true; break;
2113 case 44: mUsesMod4v = true; break;
2114 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002115 default: UNREACHABLE();
2116 }
2117
2118 outputTriplet(visit, "mod(", ", ", ")");
2119 }
2120 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002121 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002123 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002124 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2125 {
2126 case 1: mUsesAtan2_1 = true; break;
2127 case 2: mUsesAtan2_2 = true; break;
2128 case 3: mUsesAtan2_3 = true; break;
2129 case 4: mUsesAtan2_4 = true; break;
2130 default: UNREACHABLE();
2131 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002132 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 break;
2134 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2135 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2136 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2137 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2138 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2139 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2140 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2141 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2142 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002143 case EOpFaceForward:
2144 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002145 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002146 {
2147 case 1: mUsesFaceforward1 = true; break;
2148 case 2: mUsesFaceforward2 = true; break;
2149 case 3: mUsesFaceforward3 = true; break;
2150 case 4: mUsesFaceforward4 = true; break;
2151 default: UNREACHABLE();
2152 }
2153
2154 outputTriplet(visit, "faceforward(", ", ", ")");
2155 }
2156 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2158 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2159 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002160 default: UNREACHABLE();
2161 }
2162
2163 return true;
2164}
2165
2166bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2167{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002168 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002170 if (node->usesTernaryOperator())
2171 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002172 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002173 }
2174 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002176 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002177
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002178 out << "if(";
2179
2180 node->getCondition()->traverse(this);
2181
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002182 out << ")\n";
2183
2184 outputLineDirective(node->getLine());
2185 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186
daniel@transgaming.combb885322010-04-15 20:45:24 +00002187 if (node->getTrueBlock())
2188 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002189 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002190 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002191
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002192 outputLineDirective(node->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002193 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002194
2195 if (node->getFalseBlock())
2196 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002197 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002198
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002199 outputLineDirective(node->getFalseBlock()->getLine());
2200 out << "{\n";
2201
2202 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002203 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002204
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002205 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002206 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002207 }
2208 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209
2210 return false;
2211}
2212
2213void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2214{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002215 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002216}
2217
2218bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2219{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002220 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2221
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002222 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002223 {
2224 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2225 }
2226
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002227 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002228 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002229 if (handleExcessiveLoop(node))
2230 {
2231 return false;
2232 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002233 }
2234
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002235 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002236
alokp@chromium.org52813552010-11-16 18:36:09 +00002237 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002239 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002240
2241 outputLineDirective(node->getLine());
2242 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002243 }
2244 else
2245 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002246 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247
2248 if (node->getInit())
2249 {
2250 node->getInit()->traverse(this);
2251 }
2252
2253 out << "; ";
2254
alokp@chromium.org52813552010-11-16 18:36:09 +00002255 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002256 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002257 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258 }
2259
2260 out << "; ";
2261
alokp@chromium.org52813552010-11-16 18:36:09 +00002262 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002264 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265 }
2266
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002267 out << ")\n";
2268
2269 outputLineDirective(node->getLine());
2270 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 }
2272
2273 if (node->getBody())
2274 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002275 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 }
2277
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002278 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002279 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280
alokp@chromium.org52813552010-11-16 18:36:09 +00002281 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002283 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284 out << "while(\n";
2285
alokp@chromium.org52813552010-11-16 18:36:09 +00002286 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287
daniel@transgaming.com73536982012-03-21 20:45:49 +00002288 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 }
2290
daniel@transgaming.com73536982012-03-21 20:45:49 +00002291 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002293 mInsideDiscontinuousLoop = wasDiscontinuous;
2294
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 return false;
2296}
2297
2298bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2299{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002300 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
2302 switch (node->getFlowOp())
2303 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002304 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002305 case EOpBreak:
2306 if (visit == PreVisit)
2307 {
2308 if (mExcessiveLoopIndex)
2309 {
2310 out << "{Break";
2311 mExcessiveLoopIndex->traverse(this);
2312 out << " = true; break;}\n";
2313 }
2314 else
2315 {
2316 out << "break;\n";
2317 }
2318 }
2319 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002320 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321 case EOpReturn:
2322 if (visit == PreVisit)
2323 {
2324 if (node->getExpression())
2325 {
2326 out << "return ";
2327 }
2328 else
2329 {
2330 out << "return;\n";
2331 }
2332 }
2333 else if (visit == PostVisit)
2334 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002335 if (node->getExpression())
2336 {
2337 out << ";\n";
2338 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339 }
2340 break;
2341 default: UNREACHABLE();
2342 }
2343
2344 return true;
2345}
2346
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002347void OutputHLSL::traverseStatements(TIntermNode *node)
2348{
2349 if (isSingleStatement(node))
2350 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002351 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002352 }
2353
2354 node->traverse(this);
2355}
2356
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002357bool OutputHLSL::isSingleStatement(TIntermNode *node)
2358{
2359 TIntermAggregate *aggregate = node->getAsAggregate();
2360
2361 if (aggregate)
2362 {
2363 if (aggregate->getOp() == EOpSequence)
2364 {
2365 return false;
2366 }
2367 else
2368 {
2369 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2370 {
2371 if (!isSingleStatement(*sit))
2372 {
2373 return false;
2374 }
2375 }
2376
2377 return true;
2378 }
2379 }
2380
2381 return true;
2382}
2383
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002384// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2385// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002386bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2387{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002388 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002389 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002390
2391 // Parse loops of the form:
2392 // for(int index = initial; index [comparator] limit; index += increment)
2393 TIntermSymbol *index = NULL;
2394 TOperator comparator = EOpNull;
2395 int initial = 0;
2396 int limit = 0;
2397 int increment = 0;
2398
2399 // Parse index name and intial value
2400 if (node->getInit())
2401 {
2402 TIntermAggregate *init = node->getInit()->getAsAggregate();
2403
2404 if (init)
2405 {
2406 TIntermSequence &sequence = init->getSequence();
2407 TIntermTyped *variable = sequence[0]->getAsTyped();
2408
2409 if (variable && variable->getQualifier() == EvqTemporary)
2410 {
2411 TIntermBinary *assign = variable->getAsBinaryNode();
2412
2413 if (assign->getOp() == EOpInitialize)
2414 {
2415 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2416 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2417
2418 if (symbol && constant)
2419 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002420 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421 {
2422 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002423 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002424 }
2425 }
2426 }
2427 }
2428 }
2429 }
2430
2431 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002432 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002433 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002434 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002435
2436 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2437 {
2438 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2439
2440 if (constant)
2441 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002442 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002443 {
2444 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002445 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002446 }
2447 }
2448 }
2449 }
2450
2451 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002452 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002453 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002454 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2455 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002456
2457 if (binaryTerminal)
2458 {
2459 TOperator op = binaryTerminal->getOp();
2460 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2461
2462 if (constant)
2463 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002464 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002465 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002466 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002467
2468 switch (op)
2469 {
2470 case EOpAddAssign: increment = value; break;
2471 case EOpSubAssign: increment = -value; break;
2472 default: UNIMPLEMENTED();
2473 }
2474 }
2475 }
2476 }
2477 else if (unaryTerminal)
2478 {
2479 TOperator op = unaryTerminal->getOp();
2480
2481 switch (op)
2482 {
2483 case EOpPostIncrement: increment = 1; break;
2484 case EOpPostDecrement: increment = -1; break;
2485 case EOpPreIncrement: increment = 1; break;
2486 case EOpPreDecrement: increment = -1; break;
2487 default: UNIMPLEMENTED();
2488 }
2489 }
2490 }
2491
2492 if (index != NULL && comparator != EOpNull && increment != 0)
2493 {
2494 if (comparator == EOpLessThanEqual)
2495 {
2496 comparator = EOpLessThan;
2497 limit += 1;
2498 }
2499
2500 if (comparator == EOpLessThan)
2501 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002502 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002503
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002504 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002505 {
2506 return false; // Not an excessive loop
2507 }
2508
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002509 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2510 mExcessiveLoopIndex = index;
2511
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002512 out << "{int ";
2513 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002514 out << ";\n"
2515 "bool Break";
2516 index->traverse(this);
2517 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002518
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002519 bool firstLoopFragment = true;
2520
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002521 while (iterations > 0)
2522 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002523 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002524
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002525 if (!firstLoopFragment)
2526 {
2527 out << "if(!Break";
2528 index->traverse(this);
2529 out << ") {\n";
2530 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002531
2532 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2533 {
2534 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2535 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002536
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002537 // for(int index = initial; index < clampedLimit; index += increment)
2538
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002539 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002540 index->traverse(this);
2541 out << " = ";
2542 out << initial;
2543
2544 out << "; ";
2545 index->traverse(this);
2546 out << " < ";
2547 out << clampedLimit;
2548
2549 out << "; ";
2550 index->traverse(this);
2551 out << " += ";
2552 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002553 out << ")\n";
2554
2555 outputLineDirective(node->getLine());
2556 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002557
2558 if (node->getBody())
2559 {
2560 node->getBody()->traverse(this);
2561 }
2562
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002563 outputLineDirective(node->getLine());
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002564 out << ";}\n";
2565
2566 if (!firstLoopFragment)
2567 {
2568 out << "}\n";
2569 }
2570
2571 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002573 initial += MAX_LOOP_ITERATIONS * increment;
2574 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002575 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002576
2577 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002578
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002579 mExcessiveLoopIndex = restoreIndex;
2580
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002581 return true;
2582 }
2583 else UNIMPLEMENTED();
2584 }
2585
2586 return false; // Not handled as an excessive loop
2587}
2588
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002589void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002591 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002593 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594 {
2595 out << preString;
2596 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002597 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598 {
2599 out << inString;
2600 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002601 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602 {
2603 out << postString;
2604 }
2605}
2606
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002607void OutputHLSL::outputLineDirective(int line)
2608{
2609 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2610 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002611 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002612 mBody << "#line " << line;
2613
2614 if (mContext.sourcePath)
2615 {
2616 mBody << " \"" << mContext.sourcePath << "\"";
2617 }
2618
2619 mBody << "\n";
2620 }
2621}
2622
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002623TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2624{
2625 TQualifier qualifier = symbol->getQualifier();
2626 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002627 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002628
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002629 if (name.empty()) // HLSL demands named arguments, also for prototypes
2630 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002631 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002632 }
2633 else
2634 {
2635 name = decorate(name);
2636 }
2637
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002638 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2639 {
2640 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2641 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
2642 }
2643
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002644 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002645}
2646
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002647TString OutputHLSL::interpolationString(TQualifier qualifier)
2648{
2649 switch(qualifier)
2650 {
2651 case EvqVaryingIn: return "";
2652 case EvqInvariantVaryingIn: return "";
2653 case EvqSmoothIn: return "linear";
2654 case EvqFlatIn: return "nointerpolation";
2655 case EvqCentroidIn: return "centroid";
2656 case EvqVaryingOut: return "";
2657 case EvqInvariantVaryingOut: return "";
2658 case EvqSmoothOut: return "linear";
2659 case EvqFlatOut: return "nointerpolation";
2660 case EvqCentroidOut: return "centroid";
2661 default: UNREACHABLE();
2662 }
2663
2664 return "";
2665}
2666
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002667TString OutputHLSL::qualifierString(TQualifier qualifier)
2668{
2669 switch(qualifier)
2670 {
2671 case EvqIn: return "in";
2672 case EvqOut: return "out";
2673 case EvqInOut: return "inout";
2674 case EvqConstReadOnly: return "const";
2675 default: UNREACHABLE();
2676 }
2677
2678 return "";
2679}
2680
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681TString OutputHLSL::typeString(const TType &type)
2682{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002683 if (type.getBasicType() == EbtStruct)
2684 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002685 if (type.getTypeName() != "")
2686 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002687 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002688 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002689 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002690 {
2691 const TTypeList &fields = *type.getStruct();
2692
2693 TString string = "struct\n"
2694 "{\n";
2695
2696 for (unsigned int i = 0; i < fields.size(); i++)
2697 {
2698 const TType &field = *fields[i].type;
2699
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002700 string += " " + typeString(field) + " " + decorate(field.getFieldName()) + arrayString(field) + ";\n";
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002701 }
2702
2703 string += "} ";
2704
2705 return string;
2706 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002707 }
2708 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709 {
2710 switch (type.getNominalSize())
2711 {
2712 case 2: return "float2x2";
2713 case 3: return "float3x3";
2714 case 4: return "float4x4";
2715 }
2716 }
2717 else
2718 {
2719 switch (type.getBasicType())
2720 {
2721 case EbtFloat:
2722 switch (type.getNominalSize())
2723 {
2724 case 1: return "float";
2725 case 2: return "float2";
2726 case 3: return "float3";
2727 case 4: return "float4";
2728 }
2729 case EbtInt:
2730 switch (type.getNominalSize())
2731 {
2732 case 1: return "int";
2733 case 2: return "int2";
2734 case 3: return "int3";
2735 case 4: return "int4";
2736 }
2737 case EbtBool:
2738 switch (type.getNominalSize())
2739 {
2740 case 1: return "bool";
2741 case 2: return "bool2";
2742 case 3: return "bool3";
2743 case 4: return "bool4";
2744 }
2745 case EbtVoid:
2746 return "void";
2747 case EbtSampler2D:
2748 return "sampler2D";
2749 case EbtSamplerCube:
2750 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002751 case EbtSamplerExternalOES:
2752 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002753 default:
2754 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002755 }
2756 }
2757
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002758 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002759 return "<unknown type>";
2760}
2761
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002762TString OutputHLSL::textureString(const TType &type)
2763{
2764 switch (type.getBasicType())
2765 {
2766 case EbtSampler2D:
2767 return "Texture2D";
2768 case EbtSamplerCube:
2769 return "TextureCube";
2770 case EbtSamplerExternalOES:
2771 return "Texture2D";
2772 default:
2773 break;
2774 }
2775
2776 UNREACHABLE();
2777 return "<unknown texture type>";
2778}
2779
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780TString OutputHLSL::arrayString(const TType &type)
2781{
2782 if (!type.isArray())
2783 {
2784 return "";
2785 }
2786
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002787 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788}
2789
2790TString OutputHLSL::initializer(const TType &type)
2791{
2792 TString string;
2793
daniel@transgaming.comead23042010-04-29 03:35:36 +00002794 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002796 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002797
daniel@transgaming.comead23042010-04-29 03:35:36 +00002798 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002799 {
2800 string += ", ";
2801 }
2802 }
2803
daniel@transgaming.comead23042010-04-29 03:35:36 +00002804 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002806
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002807void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002808{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002809 if (name == "")
2810 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002811 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002812 }
2813
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00002814 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
2815 {
2816 return; // Already added
2817 }
2818
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002819 TType ctorType = type;
2820 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00002821 ctorType.setPrecision(EbpHigh);
2822 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00002823
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002824 TString ctorName = type.getStruct() ? decorate(name) : name;
2825
2826 typedef std::vector<TType> ParameterArray;
2827 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002828
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002829 if (type.getStruct())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002830 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002831 mStructNames.insert(decorate(name));
2832
2833 TString structure;
2834 structure += "struct " + decorate(name) + "\n"
2835 "{\n";
2836
2837 const TTypeList &fields = *type.getStruct();
2838
2839 for (unsigned int i = 0; i < fields.size(); i++)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002840 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002841 const TType &field = *fields[i].type;
2842
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002843 structure += " " + typeString(field) + " " + decorateField(field.getFieldName(), type) + arrayString(field) + ";\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002844 }
2845
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002846 structure += "};\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002847
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002848 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002849 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002850 mStructDeclarations.push_back(structure);
2851 }
2852
2853 for (unsigned int i = 0; i < fields.size(); i++)
2854 {
2855 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002856 }
2857 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002858 else if (parameters)
2859 {
2860 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
2861 {
2862 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
2863 }
2864 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002865 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00002866
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002867 TString constructor;
2868
2869 if (ctorType.getStruct())
2870 {
2871 constructor += ctorName + " " + ctorName + "_ctor(";
2872 }
2873 else // Built-in type
2874 {
2875 constructor += typeString(ctorType) + " " + ctorName + "(";
2876 }
2877
2878 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
2879 {
2880 const TType &type = ctorParameters[parameter];
2881
2882 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
2883
2884 if (parameter < ctorParameters.size() - 1)
2885 {
2886 constructor += ", ";
2887 }
2888 }
2889
2890 constructor += ")\n"
2891 "{\n";
2892
2893 if (ctorType.getStruct())
2894 {
2895 constructor += " " + ctorName + " structure = {";
2896 }
2897 else
2898 {
2899 constructor += " return " + typeString(ctorType) + "(";
2900 }
2901
2902 if (ctorType.isMatrix() && ctorParameters.size() == 1)
2903 {
2904 int dim = ctorType.getNominalSize();
2905 const TType &parameter = ctorParameters[0];
2906
2907 if (parameter.isScalar())
2908 {
2909 for (int row = 0; row < dim; row++)
2910 {
2911 for (int col = 0; col < dim; col++)
2912 {
2913 constructor += TString((row == col) ? "x0" : "0.0");
2914
2915 if (row < dim - 1 || col < dim - 1)
2916 {
2917 constructor += ", ";
2918 }
2919 }
2920 }
2921 }
2922 else if (parameter.isMatrix())
2923 {
2924 for (int row = 0; row < dim; row++)
2925 {
2926 for (int col = 0; col < dim; col++)
2927 {
2928 if (row < parameter.getNominalSize() && col < parameter.getNominalSize())
2929 {
2930 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
2931 }
2932 else
2933 {
2934 constructor += TString((row == col) ? "1.0" : "0.0");
2935 }
2936
2937 if (row < dim - 1 || col < dim - 1)
2938 {
2939 constructor += ", ";
2940 }
2941 }
2942 }
2943 }
2944 else UNREACHABLE();
2945 }
2946 else
2947 {
2948 int remainingComponents = ctorType.getObjectSize();
2949 int parameterIndex = 0;
2950
2951 while (remainingComponents > 0)
2952 {
2953 const TType &parameter = ctorParameters[parameterIndex];
2954 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
2955
2956 constructor += "x" + str(parameterIndex);
2957
2958 if (parameter.isScalar())
2959 {
2960 remainingComponents -= parameter.getObjectSize();
2961 }
2962 else if (parameter.isVector())
2963 {
2964 if (remainingComponents == parameter.getObjectSize() || moreParameters)
2965 {
2966 remainingComponents -= parameter.getObjectSize();
2967 }
2968 else if (remainingComponents < parameter.getNominalSize())
2969 {
2970 switch (remainingComponents)
2971 {
2972 case 1: constructor += ".x"; break;
2973 case 2: constructor += ".xy"; break;
2974 case 3: constructor += ".xyz"; break;
2975 case 4: constructor += ".xyzw"; break;
2976 default: UNREACHABLE();
2977 }
2978
2979 remainingComponents = 0;
2980 }
2981 else UNREACHABLE();
2982 }
2983 else if (parameter.isMatrix() || parameter.getStruct())
2984 {
2985 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
2986
2987 remainingComponents -= parameter.getObjectSize();
2988 }
2989 else UNREACHABLE();
2990
2991 if (moreParameters)
2992 {
2993 parameterIndex++;
2994 }
2995
2996 if (remainingComponents)
2997 {
2998 constructor += ", ";
2999 }
3000 }
3001 }
3002
3003 if (ctorType.getStruct())
3004 {
3005 constructor += "};\n"
3006 " return structure;\n"
3007 "}\n";
3008 }
3009 else
3010 {
3011 constructor += ");\n"
3012 "}\n";
3013 }
3014
daniel@transgaming.com63691862010-04-29 03:32:42 +00003015 mConstructors.insert(constructor);
3016}
3017
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003018const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3019{
3020 TInfoSinkBase &out = mBody;
3021
3022 if (type.getBasicType() == EbtStruct)
3023 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003024 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003025
3026 const TTypeList *structure = type.getStruct();
3027
3028 for (size_t i = 0; i < structure->size(); i++)
3029 {
3030 const TType *fieldType = (*structure)[i].type;
3031
3032 constUnion = writeConstantUnion(*fieldType, constUnion);
3033
3034 if (i != structure->size() - 1)
3035 {
3036 out << ", ";
3037 }
3038 }
3039
3040 out << ")";
3041 }
3042 else
3043 {
3044 int size = type.getObjectSize();
3045 bool writeType = size > 1;
3046
3047 if (writeType)
3048 {
3049 out << typeString(type) << "(";
3050 }
3051
3052 for (int i = 0; i < size; i++, constUnion++)
3053 {
3054 switch (constUnion->getType())
3055 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003056 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003057 case EbtInt: out << constUnion->getIConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003058 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003059 default: UNREACHABLE();
3060 }
3061
3062 if (i != size - 1)
3063 {
3064 out << ", ";
3065 }
3066 }
3067
3068 if (writeType)
3069 {
3070 out << ")";
3071 }
3072 }
3073
3074 return constUnion;
3075}
3076
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003077TString OutputHLSL::scopeString(unsigned int depthLimit)
3078{
3079 TString string;
3080
3081 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3082 {
3083 string += "_" + str(i);
3084 }
3085
3086 return string;
3087}
3088
3089TString OutputHLSL::scopedStruct(const TString &typeName)
3090{
3091 if (typeName == "")
3092 {
3093 return typeName;
3094 }
3095
3096 return typeName + scopeString(mScopeDepth);
3097}
3098
3099TString OutputHLSL::structLookup(const TString &typeName)
3100{
3101 for (int depth = mScopeDepth; depth >= 0; depth--)
3102 {
3103 TString scopedName = decorate(typeName + scopeString(depth));
3104
3105 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3106 {
3107 if (*structName == scopedName)
3108 {
3109 return scopedName;
3110 }
3111 }
3112 }
3113
3114 UNREACHABLE(); // Should have found a matching constructor
3115
3116 return typeName;
3117}
3118
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003119TString OutputHLSL::decorate(const TString &string)
3120{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003121 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003122 {
3123 return "_" + string;
3124 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003125
3126 return string;
3127}
3128
apatrick@chromium.org65756022012-01-17 21:45:38 +00003129TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003130{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003131 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003132 {
3133 return "ex_" + string;
3134 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003135
3136 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003137}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003138
3139TString OutputHLSL::decorateField(const TString &string, const TType &structure)
3140{
3141 if (structure.getTypeName().compare(0, 3, "gl_") != 0)
3142 {
3143 return decorate(string);
3144 }
3145
3146 return string;
3147}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003148
3149TString OutputHLSL::registerString(TIntermSymbol *operand)
3150{
3151 ASSERT(operand->getQualifier() == EvqUniform);
3152
3153 if (IsSampler(operand->getBasicType()))
3154 {
3155 return "s" + str(samplerRegister(operand));
3156 }
3157
3158 return "c" + str(uniformRegister(operand));
3159}
3160
3161int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3162{
3163 const TType &type = sampler->getType();
3164 ASSERT(IsSampler(type.getBasicType()));
3165
3166 int index = mSamplerRegister;
3167 mSamplerRegister += sampler->totalRegisterCount();
3168
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003169 declareUniform(type, sampler->getSymbol(), index);
3170
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003171 return index;
3172}
3173
3174int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3175{
3176 const TType &type = uniform->getType();
3177 ASSERT(!IsSampler(type.getBasicType()));
3178
3179 int index = mUniformRegister;
3180 mUniformRegister += uniform->totalRegisterCount();
3181
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003182 declareUniform(type, uniform->getSymbol(), index);
3183
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003184 return index;
3185}
3186
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003187void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int index, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003188{
3189 const TTypeList *structure = type.getStruct();
3190
3191 if (!structure)
3192 {
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003193 output.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)index));
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003194 }
3195 else
3196 {
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003197 Uniform structUniform(GL_NONE, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)index);
3198
3199 int fieldIndex = index;
3200
3201 for (size_t i = 0; i < structure->size(); i++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003202 {
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003203 const TType &fieldType = *(*structure)[i].type;
3204 const TString &fieldName = fieldType.getFieldName();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003205
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003206 declareUniformToList(fieldType, fieldName, fieldIndex, structUniform.fields);
3207 fieldIndex += fieldType.totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003208 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003209
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003210 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003211 }
3212}
3213
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003214void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3215{
3216 declareUniformToList(type, name, index, mActiveUniforms);
3217}
3218
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003219GLenum OutputHLSL::glVariableType(const TType &type)
3220{
3221 if (type.getBasicType() == EbtFloat)
3222 {
3223 if (type.isScalar())
3224 {
3225 return GL_FLOAT;
3226 }
3227 else if (type.isVector())
3228 {
3229 switch(type.getNominalSize())
3230 {
3231 case 2: return GL_FLOAT_VEC2;
3232 case 3: return GL_FLOAT_VEC3;
3233 case 4: return GL_FLOAT_VEC4;
3234 default: UNREACHABLE();
3235 }
3236 }
3237 else if (type.isMatrix())
3238 {
3239 switch(type.getNominalSize())
3240 {
3241 case 2: return GL_FLOAT_MAT2;
3242 case 3: return GL_FLOAT_MAT3;
3243 case 4: return GL_FLOAT_MAT4;
3244 default: UNREACHABLE();
3245 }
3246 }
3247 else UNREACHABLE();
3248 }
3249 else if (type.getBasicType() == EbtInt)
3250 {
3251 if (type.isScalar())
3252 {
3253 return GL_INT;
3254 }
3255 else if (type.isVector())
3256 {
3257 switch(type.getNominalSize())
3258 {
3259 case 2: return GL_INT_VEC2;
3260 case 3: return GL_INT_VEC3;
3261 case 4: return GL_INT_VEC4;
3262 default: UNREACHABLE();
3263 }
3264 }
3265 else UNREACHABLE();
3266 }
3267 else if (type.getBasicType() == EbtBool)
3268 {
3269 if (type.isScalar())
3270 {
3271 return GL_BOOL;
3272 }
3273 else if (type.isVector())
3274 {
3275 switch(type.getNominalSize())
3276 {
3277 case 2: return GL_BOOL_VEC2;
3278 case 3: return GL_BOOL_VEC3;
3279 case 4: return GL_BOOL_VEC4;
3280 default: UNREACHABLE();
3281 }
3282 }
3283 else UNREACHABLE();
3284 }
3285 else if (type.getBasicType() == EbtSampler2D)
3286 {
3287 return GL_SAMPLER_2D;
3288 }
3289 else if (type.getBasicType() == EbtSamplerCube)
3290 {
3291 return GL_SAMPLER_CUBE;
3292 }
3293 else UNREACHABLE();
3294
3295 return GL_NONE;
3296}
3297
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003298GLenum OutputHLSL::glVariablePrecision(const TType &type)
3299{
3300 if (type.getBasicType() == EbtFloat)
3301 {
3302 switch (type.getPrecision())
3303 {
3304 case EbpHigh: return GL_HIGH_FLOAT;
3305 case EbpMedium: return GL_MEDIUM_FLOAT;
3306 case EbpLow: return GL_LOW_FLOAT;
3307 case EbpUndefined:
3308 // Should be defined as the default precision by the parser
3309 default: UNREACHABLE();
3310 }
3311 }
3312 else if (type.getBasicType() == EbtInt)
3313 {
3314 switch (type.getPrecision())
3315 {
3316 case EbpHigh: return GL_HIGH_INT;
3317 case EbpMedium: return GL_MEDIUM_INT;
3318 case EbpLow: return GL_LOW_INT;
3319 case EbpUndefined:
3320 // Should be defined as the default precision by the parser
3321 default: UNREACHABLE();
3322 }
3323 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003324
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003325 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003326 return GL_NONE;
3327}
3328
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003329bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3330{
3331 switch(qualifier)
3332 {
3333 case EvqVaryingOut:
3334 case EvqInvariantVaryingOut:
3335 case EvqSmoothOut:
3336 case EvqFlatOut:
3337 case EvqCentroidOut:
3338 return true;
3339 }
3340
3341 return false;
3342}
3343
3344bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3345{
3346 switch(qualifier)
3347 {
3348 case EvqVaryingIn:
3349 case EvqInvariantVaryingIn:
3350 case EvqSmoothIn:
3351 case EvqFlatIn:
3352 case EvqCentroidIn:
3353 return true;
3354 }
3355
3356 return false;
3357}
3358
3359bool OutputHLSL::isVarying(TQualifier qualifier)
3360{
3361 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3362}
3363
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003364}