blob: ea0045f04eeae833b84c31df6496ceea411c2497 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 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
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/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"
Jamie Madill834e8b72014-04-11 13:33:58 -040011#include "common/blocklayout.h"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/compilerdebug.h"
13#include "compiler/translator/InfoSink.h"
14#include "compiler/translator/DetectDiscontinuity.h"
15#include "compiler/translator/SearchSymbol.h"
16#include "compiler/translator/UnfoldShortCircuit.h"
Geoff Lang17732822013-08-29 13:46:49 -040017#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050018#include "compiler/translator/NodeSearch.h"
Jamie Madille53c98b2014-02-03 11:57:13 -050019#include "compiler/translator/RewriteElseBlocks.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000021#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000022#include <cfloat>
23#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000024
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025namespace sh
26{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000027
Nicolas Capense0ba27a2013-06-24 16:10:52 -040028TString OutputHLSL::TextureFunction::name() const
29{
30 TString name = "gl_texture";
31
Nicolas Capens6d232bb2013-07-08 15:56:38 -040032 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033 {
34 name += "2D";
35 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040036 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040037 {
38 name += "3D";
39 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "Cube";
43 }
44 else UNREACHABLE();
45
46 if (proj)
47 {
48 name += "Proj";
49 }
50
Nicolas Capensb1f45b72013-12-19 17:37:19 -050051 if (offset)
52 {
53 name += "Offset";
54 }
55
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
Nicolas Capensfc014542014-02-18 14:47:13 -050058 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040059 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050060 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040062 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050063 case SIZE: name += "Size"; break;
64 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050065 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040066 default: UNREACHABLE();
67 }
68
69 return name + "(";
70}
71
Jamie Madillc2141fb2013-08-30 13:21:08 -040072const char *RegisterPrefix(const TType &type)
73{
74 if (IsSampler(type.getBasicType()))
75 {
76 return "s";
77 }
78 else
79 {
80 return "c";
81 }
82}
83
Nicolas Capense0ba27a2013-06-24 16:10:52 -040084bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
85{
86 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040087 if (sampler > rhs.sampler) return false;
88
Nicolas Capense0ba27a2013-06-24 16:10:52 -040089 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040090 if (coords > rhs.coords) return false;
91
Nicolas Capense0ba27a2013-06-24 16:10:52 -040092 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (proj && !rhs.proj) return false;
94
95 if (!offset && rhs.offset) return true;
96 if (offset && !rhs.offset) return false;
97
Nicolas Capens75fb4752013-07-10 15:14:47 -040098 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040099 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400100
101 return false;
102}
103
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000104OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000105 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000107 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000108 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000109
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000110 mUsesFragColor = false;
111 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000112 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000113 mUsesFragCoord = false;
114 mUsesPointCoord = false;
115 mUsesFrontFacing = false;
116 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400117 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000118 mUsesXor = false;
119 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000120 mUsesMod2v = false;
121 mUsesMod2f = false;
122 mUsesMod3v = false;
123 mUsesMod3f = false;
124 mUsesMod4v = false;
125 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000126 mUsesFaceforward1 = false;
127 mUsesFaceforward2 = false;
128 mUsesFaceforward3 = false;
129 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000130 mUsesAtan2_1 = false;
131 mUsesAtan2_2 = false;
132 mUsesAtan2_3 = false;
133 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500134 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400135 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000136
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000137 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
138
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000139 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000140
141 mContainsLoopDiscontinuity = false;
142 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000143 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400144 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000145
146 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000147
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000148 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000149 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000150 if (mContext.shaderType == SH_FRAGMENT_SHADER)
151 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000152 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000153 }
154 else
155 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000156 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000157 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000158 }
159 else
160 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000161 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000162 }
163
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000164 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000165 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400166 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167}
168
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000169OutputHLSL::~OutputHLSL()
170{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000171 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000172}
173
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000174void OutputHLSL::output()
175{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000176 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400177 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
178 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000179
Jamie Madille53c98b2014-02-03 11:57:13 -0500180 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
181 // use a vertex attribute as a condition, and some related computation in the else block.
182 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
183 {
184 RewriteElseBlocks(mContext.treeRoot);
185 }
186
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000187 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 +0000188 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000189
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000190 mContext.infoSink().obj << mHeader.c_str();
191 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000192}
193
Jamie Madill570e04d2013-06-21 09:15:33 -0400194void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
195{
196 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
197 {
198 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
199
200 // This will mark the necessary block elements as referenced
201 flaggedNode->traverse(this);
202 TString structName(mBody.c_str());
203 mBody.erase();
204
205 mFlaggedStructOriginalNames[flaggedNode] = structName;
206
207 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
208 {
209 structName.erase(pos, 1);
210 }
211
212 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
213 }
214}
215
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000216TInfoSinkBase &OutputHLSL::getBodyStream()
217{
218 return mBody;
219}
220
Jamie Madill834e8b72014-04-11 13:33:58 -0400221const std::vector<gl::Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000222{
223 return mActiveUniforms;
224}
225
Jamie Madill834e8b72014-04-11 13:33:58 -0400226const std::vector<gl::InterfaceBlock> &OutputHLSL::getInterfaceBlocks() const
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000227{
228 return mActiveInterfaceBlocks;
229}
230
Jamie Madill834e8b72014-04-11 13:33:58 -0400231const std::vector<gl::Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400232{
233 return mActiveOutputVariables;
234}
235
Jamie Madill834e8b72014-04-11 13:33:58 -0400236const std::vector<gl::Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400237{
238 return mActiveAttributes;
239}
240
Jamie Madill834e8b72014-04-11 13:33:58 -0400241const std::vector<gl::Varying> &OutputHLSL::getVaryings() const
Jamie Madill47fdd132013-08-30 13:21:04 -0400242{
243 return mActiveVaryings;
244}
245
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000246int OutputHLSL::vectorSize(const TType &type) const
247{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000248 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000249 int arraySize = type.isArray() ? type.getArraySize() : 1;
250
251 return elementSize * arraySize;
252}
253
Jamie Madill98493dd2013-07-08 14:39:03 -0400254TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000255{
Jamie Madill98493dd2013-07-08 14:39:03 -0400256 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000257 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000259 }
260 else
261 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400262 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000263 }
264}
265
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000266TString OutputHLSL::decoratePrivate(const TString &privateText)
267{
268 return "dx_" + privateText;
269}
270
Jamie Madill98493dd2013-07-08 14:39:03 -0400271TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000272{
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000274}
275
Jamie Madill98493dd2013-07-08 14:39:03 -0400276TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000277{
Jamie Madill98493dd2013-07-08 14:39:03 -0400278 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000279 {
280 return "";
281 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400282 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000283 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000285 }
286 else
287 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400288 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000289 }
290}
291
Jamie Madill98493dd2013-07-08 14:39:03 -0400292TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000293{
Jamie Madill98493dd2013-07-08 14:39:03 -0400294 const TType &fieldType = *field.type();
295 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400296 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000297
Jamie Madill98493dd2013-07-08 14:39:03 -0400298 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000299 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400300 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400301 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400302 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000303 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400304 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000305 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400306 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400307 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000308 }
309 else
310 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400311 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000312 }
313}
314
Jamie Madill98493dd2013-07-08 14:39:03 -0400315TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
316{
317 TString hlsl;
318
319 int elementIndex = 0;
320
321 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
322 {
323 const TField &field = *interfaceBlock.fields()[typeIndex];
324 const TType &fieldType = *field.type();
325
326 if (blockStorage == EbsStd140)
327 {
328 // 2 and 3 component vector types in some cases need pre-padding
329 hlsl += std140PrePaddingString(fieldType, &elementIndex);
330 }
331
332 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
333 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
334
335 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
336 if (blockStorage == EbsStd140)
337 {
338 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
339 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
340 }
341 }
342
343 return hlsl;
344}
345
346TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
347{
348 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
349
350 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
351 "{\n" +
352 interfaceBlockFieldString(interfaceBlock, blockStorage) +
353 "};\n\n";
354}
355
356TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
357{
358 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
359 const TString &blockName = interfaceBlock.name() + arrayIndexString;
360 TString hlsl;
361
362 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
363 "{\n";
364
365 if (interfaceBlock.hasInstanceName())
366 {
367 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
368 }
369 else
370 {
371 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
372 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
373 }
374
375 hlsl += "};\n\n";
376
377 return hlsl;
378}
379
Jamie Madill574d9dd2013-06-20 11:55:56 -0400380TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
381{
382 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
383 {
384 // no padding needed, HLSL will align the field to a new register
385 *elementIndex = 0;
386 return "";
387 }
388
389 const GLenum glType = glVariableType(type);
390 const int numComponents = gl::UniformComponentCount(glType);
391
392 if (numComponents >= 4)
393 {
394 // no padding needed, HLSL will align the field to a new register
395 *elementIndex = 0;
396 return "";
397 }
398
399 if (*elementIndex + numComponents > 4)
400 {
401 // no padding needed, HLSL will align the field to a new register
402 *elementIndex = numComponents;
403 return "";
404 }
405
406 TString padding;
407
408 const int alignment = numComponents == 3 ? 4 : numComponents;
409 const int paddingOffset = (*elementIndex % alignment);
410
411 if (paddingOffset != 0)
412 {
413 // padding is neccessary
414 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
415 {
416 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
417 }
418
419 *elementIndex += (alignment - paddingOffset);
420 }
421
422 *elementIndex += numComponents;
423 *elementIndex %= 4;
424
425 return padding;
426}
427
Jamie Madille4075c92013-06-21 09:15:32 -0400428TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400429{
Jamie Madillc835df62013-06-21 09:15:32 -0400430 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400431 {
432 return "";
433 }
434
Jamie Madill574d9dd2013-06-20 11:55:56 -0400435 int numComponents = 0;
436
437 if (type.isMatrix())
438 {
Jamie Madille4075c92013-06-21 09:15:32 -0400439 // This method can also be called from structureString, which does not use layout qualifiers.
440 // Thus, use the method parameter for determining the matrix packing.
441 //
442 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
443 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
444 //
445 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400446 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400447 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
448 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400449 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400450 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400451 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400452 numComponents = mStd140StructElementIndexes[structName];
453
454 if (numComponents == 0)
455 {
456 return "";
457 }
Jamie Madillc835df62013-06-21 09:15:32 -0400458 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400459 else
460 {
Jamie Madillc835df62013-06-21 09:15:32 -0400461 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400462 numComponents = gl::UniformComponentCount(glType);
463 }
464
465 TString padding;
466 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
467 {
468 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
469 }
470 return padding;
471}
472
Jamie Madill440dc742013-06-20 11:55:55 -0400473// Use the same layout for packed and shared
Jamie Madill834e8b72014-04-11 13:33:58 -0400474void setBlockLayout(gl::InterfaceBlock *interfaceBlock, gl::BlockLayoutType newLayout)
Jamie Madill440dc742013-06-20 11:55:55 -0400475{
476 interfaceBlock->layout = newLayout;
477 interfaceBlock->blockInfo.clear();
478
479 switch (newLayout)
480 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400481 case gl::BLOCKLAYOUT_SHARED:
482 case gl::BLOCKLAYOUT_PACKED:
Jamie Madill440dc742013-06-20 11:55:55 -0400483 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -0400484 gl::HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo, gl::HLSLBlockEncoder::ENCODE_PACKED);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400485 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400486 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
487 }
488 break;
489
Jamie Madill834e8b72014-04-11 13:33:58 -0400490 case gl::BLOCKLAYOUT_STANDARD:
Jamie Madill440dc742013-06-20 11:55:55 -0400491 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400492 gl::Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400493 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400494 interfaceBlock->dataSize = stdEncoder.getBlockSize();
495 }
496 break;
497
498 default:
499 UNREACHABLE();
500 break;
501 }
502}
503
Jamie Madill834e8b72014-04-11 13:33:58 -0400504gl::BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400505{
506 switch (blockStorage)
507 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400508 case EbsPacked: return gl::BLOCKLAYOUT_PACKED;
509 case EbsShared: return gl::BLOCKLAYOUT_SHARED;
510 case EbsStd140: return gl::BLOCKLAYOUT_STANDARD;
511 default: UNREACHABLE(); return gl::BLOCKLAYOUT_SHARED;
Jamie Madill574d9dd2013-06-20 11:55:56 -0400512 }
513}
514
Jamie Madill98493dd2013-07-08 14:39:03 -0400515TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400516{
517 TString init;
518
519 TString preIndentString;
520 TString fullIndentString;
521
522 for (int spaces = 0; spaces < (indent * 4); spaces++)
523 {
524 preIndentString += ' ';
525 }
526
527 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
528 {
529 fullIndentString += ' ';
530 }
531
532 init += preIndentString + "{\n";
533
Jamie Madill98493dd2013-07-08 14:39:03 -0400534 const TFieldList &fields = structure.fields();
535 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400536 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400537 const TField &field = *fields[fieldIndex];
538 const TString &fieldName = rhsStructName + "." + decorate(field.name());
539 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400540
Jamie Madill98493dd2013-07-08 14:39:03 -0400541 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400542 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400543 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400544 }
545 else
546 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400547 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400548 }
549 }
550
551 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
552
553 return init;
554}
555
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000556void OutputHLSL::header()
557{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000558 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000559
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000560 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000561 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000562 TString varyings;
563 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400564 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000565
Jamie Madillc2141fb2013-08-30 13:21:08 -0400566 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000567 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400568 const TIntermSymbol &uniform = *uniformIt->second;
569 const TType &type = uniform.getType();
570 const TString &name = uniform.getSymbol();
571
572 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000573
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000574 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
575 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400576 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400577 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000578
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000579 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400580 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000581 }
582 else
583 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400584 const TStructure *structure = type.getStruct();
585 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
586
587 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
588
589 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000590 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000591 }
592
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000593 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
594 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000595 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400596 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
597 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000598
Jamie Madill98493dd2013-07-08 14:39:03 -0400599 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
Jamie Madill834e8b72014-04-11 13:33:58 -0400600 gl::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
Jamie Madill98493dd2013-07-08 14:39:03 -0400601 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000602 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400603 const TField &field = *fieldList[typeIndex];
604 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400605 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000606 }
607
Jamie Madill98493dd2013-07-08 14:39:03 -0400608 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000609
Jamie Madill834e8b72014-04-11 13:33:58 -0400610 gl::BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
Jamie Madill98493dd2013-07-08 14:39:03 -0400611 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700612
613 if (interfaceBlock.matrixPacking() == EmpRowMajor)
614 {
615 activeBlock.isRowMajorLayout = true;
616 }
617
Jamie Madill98493dd2013-07-08 14:39:03 -0400618 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000619
Jamie Madill98493dd2013-07-08 14:39:03 -0400620 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000621 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400622 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000623 }
624
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000625 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000626 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000627 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
628 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400629 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000630 }
631 }
632 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000633 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400634 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000635 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000636 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000637
Jamie Madill829f59e2013-11-13 19:40:54 -0500638 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400639 {
640 TIntermTyped *structNode = flaggedStructIt->first;
641 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400642 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400643 const TString &originalName = mFlaggedStructOriginalNames[structNode];
644
Jamie Madill98493dd2013-07-08 14:39:03 -0400645 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
646 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400647 flaggedStructs += "\n";
648 }
649
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000650 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
651 {
652 const TType &type = varying->second->getType();
653 const TString &name = varying->second->getSymbol();
654
655 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000656 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
657 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400658
Jamie Madill94599662013-08-30 13:21:10 -0400659 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000660 }
661
662 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
663 {
664 const TType &type = attribute->second->getType();
665 const TString &name = attribute->second->getSymbol();
666
667 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400668
Jamie Madill834e8b72014-04-11 13:33:58 -0400669 gl::Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400670 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
671 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000672 }
673
Jamie Madill529077d2013-06-20 11:55:54 -0400674 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
675 {
676 out << *structDeclaration;
677 }
678
679 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
680 {
681 out << *constructor;
682 }
683
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500684 if (mUsesDiscardRewriting)
685 {
686 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
687 }
688
Nicolas Capens655fe362014-04-11 13:12:34 -0400689 if (mUsesNestedBreak)
690 {
691 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
692 }
693
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400694 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000696 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000697 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000698
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000699 out << "// Varyings\n";
700 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400701 out << "\n";
702
703 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000704 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500705 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000706 {
Jamie Madill46131a32013-06-20 11:55:50 -0400707 const TString &variableName = outputVariableIt->first;
708 const TType &variableType = outputVariableIt->second->getType();
709 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
710
711 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
712 " = " + initializer(variableType) + ";\n";
713
Jamie Madill834e8b72014-04-11 13:33:58 -0400714 gl::Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400715 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400716 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000717 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000718 }
Jamie Madill46131a32013-06-20 11:55:50 -0400719 else
720 {
721 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
722
723 out << "static float4 gl_Color[" << numColorValues << "] =\n"
724 "{\n";
725 for (unsigned int i = 0; i < numColorValues; i++)
726 {
727 out << " float4(0, 0, 0, 0)";
728 if (i + 1 != numColorValues)
729 {
730 out << ",";
731 }
732 out << "\n";
733 }
734
735 out << "};\n";
736 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000737
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400738 if (mUsesFragDepth)
739 {
740 out << "static float gl_Depth = 0.0;\n";
741 }
742
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000743 if (mUsesFragCoord)
744 {
745 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
746 }
747
748 if (mUsesPointCoord)
749 {
750 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
751 }
752
753 if (mUsesFrontFacing)
754 {
755 out << "static bool gl_FrontFacing = false;\n";
756 }
757
758 out << "\n";
759
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000760 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000761 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000762 out << "struct gl_DepthRangeParameters\n"
763 "{\n"
764 " float near;\n"
765 " float far;\n"
766 " float diff;\n"
767 "};\n"
768 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000769 }
770
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000771 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000772 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000773 out << "cbuffer DriverConstants : register(b1)\n"
774 "{\n";
775
776 if (mUsesDepthRange)
777 {
778 out << " float3 dx_DepthRange : packoffset(c0);\n";
779 }
780
781 if (mUsesFragCoord)
782 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000783 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000784 }
785
786 if (mUsesFragCoord || mUsesFrontFacing)
787 {
788 out << " float3 dx_DepthFront : packoffset(c2);\n";
789 }
790
791 out << "};\n";
792 }
793 else
794 {
795 if (mUsesDepthRange)
796 {
797 out << "uniform float3 dx_DepthRange : register(c0);";
798 }
799
800 if (mUsesFragCoord)
801 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000802 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000803 }
804
805 if (mUsesFragCoord || mUsesFrontFacing)
806 {
807 out << "uniform float3 dx_DepthFront : register(c2);\n";
808 }
809 }
810
811 out << "\n";
812
813 if (mUsesDepthRange)
814 {
815 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
816 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000817 }
818
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000820 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000821
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000822 if (!interfaceBlocks.empty())
823 {
824 out << interfaceBlocks;
825 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400826
827 if (!flaggedStructs.empty())
828 {
829 out << "// Std140 Structures accessed by value\n";
830 out << "\n";
831 out << flaggedStructs;
832 out << "\n";
833 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000834 }
835
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000836 if (usingMRTExtension && mNumRenderTargets > 1)
837 {
838 out << "#define GL_USES_MRT\n";
839 }
840
841 if (mUsesFragColor)
842 {
843 out << "#define GL_USES_FRAG_COLOR\n";
844 }
845
846 if (mUsesFragData)
847 {
848 out << "#define GL_USES_FRAG_DATA\n";
849 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000851 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000853 out << "// Attributes\n";
854 out << attributes;
855 out << "\n"
856 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
857
858 if (mUsesPointSize)
859 {
860 out << "static float gl_PointSize = float(1);\n";
861 }
862
863 out << "\n"
864 "// Varyings\n";
865 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000866 out << "\n";
867
868 if (mUsesDepthRange)
869 {
870 out << "struct gl_DepthRangeParameters\n"
871 "{\n"
872 " float near;\n"
873 " float far;\n"
874 " float diff;\n"
875 "};\n"
876 "\n";
877 }
878
879 if (mOutputType == SH_HLSL11_OUTPUT)
880 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000881 if (mUsesDepthRange)
882 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000883 out << "cbuffer DriverConstants : register(b1)\n"
884 "{\n"
885 " float3 dx_DepthRange : packoffset(c0);\n"
886 "};\n"
887 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000888 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000889 }
890 else
891 {
892 if (mUsesDepthRange)
893 {
894 out << "uniform float3 dx_DepthRange : register(c0);\n";
895 }
896
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000897 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000898 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000899 }
900
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000901 if (mUsesDepthRange)
902 {
903 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
904 "\n";
905 }
906
907 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000909
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000910 if (!interfaceBlocks.empty())
911 {
912 out << interfaceBlocks;
913 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400914
915 if (!flaggedStructs.empty())
916 {
917 out << "// Std140 Structures accessed by value\n";
918 out << "\n";
919 out << flaggedStructs;
920 out << "\n";
921 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000922 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400923 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000924
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400925 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
926 {
927 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400928 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000929 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400930 switch(textureFunction->sampler)
931 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400932 case EbtSampler2D: out << "int2 "; break;
933 case EbtSampler3D: out << "int3 "; break;
934 case EbtSamplerCube: out << "int2 "; break;
935 case EbtSampler2DArray: out << "int3 "; break;
936 case EbtISampler2D: out << "int2 "; break;
937 case EbtISampler3D: out << "int3 "; break;
938 case EbtISamplerCube: out << "int2 "; break;
939 case EbtISampler2DArray: out << "int3 "; break;
940 case EbtUSampler2D: out << "int2 "; break;
941 case EbtUSampler3D: out << "int3 "; break;
942 case EbtUSamplerCube: out << "int2 "; break;
943 case EbtUSampler2DArray: out << "int3 "; break;
944 case EbtSampler2DShadow: out << "int2 "; break;
945 case EbtSamplerCubeShadow: out << "int2 "; break;
946 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400947 default: UNREACHABLE();
948 }
949 }
950 else // Sampling function
951 {
952 switch(textureFunction->sampler)
953 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400954 case EbtSampler2D: out << "float4 "; break;
955 case EbtSampler3D: out << "float4 "; break;
956 case EbtSamplerCube: out << "float4 "; break;
957 case EbtSampler2DArray: out << "float4 "; break;
958 case EbtISampler2D: out << "int4 "; break;
959 case EbtISampler3D: out << "int4 "; break;
960 case EbtISamplerCube: out << "int4 "; break;
961 case EbtISampler2DArray: out << "int4 "; break;
962 case EbtUSampler2D: out << "uint4 "; break;
963 case EbtUSampler3D: out << "uint4 "; break;
964 case EbtUSamplerCube: out << "uint4 "; break;
965 case EbtUSampler2DArray: out << "uint4 "; break;
966 case EbtSampler2DShadow: out << "float "; break;
967 case EbtSamplerCubeShadow: out << "float "; break;
968 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400969 default: UNREACHABLE();
970 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000971 }
972
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400973 // Function name
974 out << textureFunction->name();
975
976 // Argument list
977 int hlslCoords = 4;
978
979 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000980 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400981 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000982 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400983 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
984 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
985 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000986 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400987
Nicolas Capens75fb4752013-07-10 15:14:47 -0400988 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000989 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400990 case TextureFunction::IMPLICIT: break;
991 case TextureFunction::BIAS: hlslCoords = 4; break;
992 case TextureFunction::LOD: hlslCoords = 4; break;
993 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400994 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400995 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000996 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400997 }
998 else if (mOutputType == SH_HLSL11_OUTPUT)
999 {
1000 switch(textureFunction->sampler)
1001 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001002 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
1003 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
1004 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
1005 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
1006 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
1007 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001008 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001009 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
1010 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
1011 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001012 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001013 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
1014 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
1015 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
1016 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001017 default: UNREACHABLE();
1018 }
1019 }
1020 else UNREACHABLE();
1021
Nicolas Capensfc014542014-02-18 14:47:13 -05001022 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001023 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001024 switch(textureFunction->coords)
1025 {
1026 case 2: out << ", int2 t"; break;
1027 case 3: out << ", int3 t"; break;
1028 default: UNREACHABLE();
1029 }
1030 }
1031 else // Floating-point coordinates (except textureSize)
1032 {
1033 switch(textureFunction->coords)
1034 {
1035 case 1: out << ", int lod"; break; // textureSize()
1036 case 2: out << ", float2 t"; break;
1037 case 3: out << ", float3 t"; break;
1038 case 4: out << ", float4 t"; break;
1039 default: UNREACHABLE();
1040 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001041 }
1042
Nicolas Capensd11d5492014-02-19 17:06:10 -05001043 if (textureFunction->method == TextureFunction::GRAD)
1044 {
1045 switch(textureFunction->sampler)
1046 {
1047 case EbtSampler2D:
1048 case EbtISampler2D:
1049 case EbtUSampler2D:
1050 case EbtSampler2DArray:
1051 case EbtISampler2DArray:
1052 case EbtUSampler2DArray:
1053 case EbtSampler2DShadow:
1054 case EbtSampler2DArrayShadow:
1055 out << ", float2 ddx, float2 ddy";
1056 break;
1057 case EbtSampler3D:
1058 case EbtISampler3D:
1059 case EbtUSampler3D:
1060 case EbtSamplerCube:
1061 case EbtISamplerCube:
1062 case EbtUSamplerCube:
1063 case EbtSamplerCubeShadow:
1064 out << ", float3 ddx, float3 ddy";
1065 break;
1066 default: UNREACHABLE();
1067 }
1068 }
1069
Nicolas Capens75fb4752013-07-10 15:14:47 -04001070 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001071 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001072 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001073 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074 case TextureFunction::LOD: out << ", float lod"; break;
1075 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001076 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -04001077 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -05001078 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -05001079 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001080 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001081 }
1082
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001083 if (textureFunction->offset)
1084 {
1085 switch(textureFunction->sampler)
1086 {
1087 case EbtSampler2D: out << ", int2 offset"; break;
1088 case EbtSampler3D: out << ", int3 offset"; break;
1089 case EbtSampler2DArray: out << ", int2 offset"; break;
1090 case EbtISampler2D: out << ", int2 offset"; break;
1091 case EbtISampler3D: out << ", int3 offset"; break;
1092 case EbtISampler2DArray: out << ", int2 offset"; break;
1093 case EbtUSampler2D: out << ", int2 offset"; break;
1094 case EbtUSampler3D: out << ", int3 offset"; break;
1095 case EbtUSampler2DArray: out << ", int2 offset"; break;
1096 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -05001097 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001098 default: UNREACHABLE();
1099 }
1100 }
1101
Nicolas Capens84cfa122014-04-14 13:48:45 -04001102 if (textureFunction->method == TextureFunction::BIAS ||
1103 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001104 {
1105 out << ", float bias";
1106 }
1107
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001108 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001109 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001110
Nicolas Capens75fb4752013-07-10 15:14:47 -04001111 if (textureFunction->method == TextureFunction::SIZE)
1112 {
1113 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1114 {
1115 if (IsSamplerArray(textureFunction->sampler))
1116 {
1117 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1118 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1119 }
1120 else
1121 {
1122 out << " uint width; uint height; uint numberOfLevels;\n"
1123 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1124 }
1125 }
1126 else if (IsSampler3D(textureFunction->sampler))
1127 {
1128 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1129 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1130 }
1131 else UNREACHABLE();
1132
1133 switch(textureFunction->sampler)
1134 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001135 case EbtSampler2D: out << " return int2(width, height);"; break;
1136 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1137 case EbtSamplerCube: out << " return int2(width, height);"; break;
1138 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1139 case EbtISampler2D: out << " return int2(width, height);"; break;
1140 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1141 case EbtISamplerCube: out << " return int2(width, height);"; break;
1142 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1143 case EbtUSampler2D: out << " return int2(width, height);"; break;
1144 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1145 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1146 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1147 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1148 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1149 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001150 default: UNREACHABLE();
1151 }
1152 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001153 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001154 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001155 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1156 {
1157 out << " float width; float height; float layers; float levels;\n";
1158
1159 out << " uint mip = 0;\n";
1160
1161 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
1162
1163 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
1164 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
1165 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
1166 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
1167
1168 // FACE_POSITIVE_X = 000b
1169 // FACE_NEGATIVE_X = 001b
1170 // FACE_POSITIVE_Y = 010b
1171 // FACE_NEGATIVE_Y = 011b
1172 // FACE_POSITIVE_Z = 100b
1173 // FACE_NEGATIVE_Z = 101b
1174 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
1175
1176 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
1177 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
1178 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
1179
1180 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
1181 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
1182 }
1183 else if (IsIntegerSampler(textureFunction->sampler) &&
1184 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001185 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001186 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001187 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001188 if (IsSamplerArray(textureFunction->sampler))
1189 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001190 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001191
Nicolas Capens9edebd62013-08-06 10:59:10 -04001192 if (textureFunction->method == TextureFunction::LOD0)
1193 {
1194 out << " uint mip = 0;\n";
1195 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001196 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1197 {
1198 out << " uint mip = bias;\n";
1199 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001200 else
1201 {
1202 if (textureFunction->method == TextureFunction::IMPLICIT ||
1203 textureFunction->method == TextureFunction::BIAS)
1204 {
1205 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1206 " float2 tSized = float2(t.x * width, t.y * height);\n"
1207 " float dx = length(ddx(tSized));\n"
1208 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001209 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001210
1211 if (textureFunction->method == TextureFunction::BIAS)
1212 {
1213 out << " lod += bias;\n";
1214 }
1215 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001216 else if (textureFunction->method == TextureFunction::GRAD)
1217 {
1218 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1219 " float lod = log2(max(length(ddx), length(ddy)));\n";
1220 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001221
1222 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1223 }
1224
1225 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001226 }
1227 else
1228 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001229 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001230
Nicolas Capens9edebd62013-08-06 10:59:10 -04001231 if (textureFunction->method == TextureFunction::LOD0)
1232 {
1233 out << " uint mip = 0;\n";
1234 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001235 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1236 {
1237 out << " uint mip = bias;\n";
1238 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001239 else
1240 {
1241 if (textureFunction->method == TextureFunction::IMPLICIT ||
1242 textureFunction->method == TextureFunction::BIAS)
1243 {
1244 out << " x.GetDimensions(0, width, height, levels);\n"
1245 " float2 tSized = float2(t.x * width, t.y * height);\n"
1246 " float dx = length(ddx(tSized));\n"
1247 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001248 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001249
1250 if (textureFunction->method == TextureFunction::BIAS)
1251 {
1252 out << " lod += bias;\n";
1253 }
1254 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001255 else if (textureFunction->method == TextureFunction::LOD)
1256 {
1257 out << " x.GetDimensions(0, width, height, levels);\n";
1258 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001259 else if (textureFunction->method == TextureFunction::GRAD)
1260 {
1261 out << " x.GetDimensions(0, width, height, levels);\n"
1262 " float lod = log2(max(length(ddx), length(ddy)));\n";
1263 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001264
1265 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1266 }
1267
1268 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001269 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001270 }
1271 else if (IsSampler3D(textureFunction->sampler))
1272 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001273 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001274
Nicolas Capens9edebd62013-08-06 10:59:10 -04001275 if (textureFunction->method == TextureFunction::LOD0)
1276 {
1277 out << " uint mip = 0;\n";
1278 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001279 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1280 {
1281 out << " uint mip = bias;\n";
1282 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001283 else
1284 {
1285 if (textureFunction->method == TextureFunction::IMPLICIT ||
1286 textureFunction->method == TextureFunction::BIAS)
1287 {
1288 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1289 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1290 " float dx = length(ddx(tSized));\n"
1291 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001292 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001293
1294 if (textureFunction->method == TextureFunction::BIAS)
1295 {
1296 out << " lod += bias;\n";
1297 }
1298 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001299 else if (textureFunction->method == TextureFunction::GRAD)
1300 {
1301 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1302 " float lod = log2(max(length(ddx), length(ddy)));\n";
1303 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001304
1305 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1306 }
1307
1308 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001309 }
1310 else UNREACHABLE();
1311 }
1312
1313 out << " return ";
1314
1315 // HLSL intrinsic
1316 if (mOutputType == SH_HLSL9_OUTPUT)
1317 {
1318 switch(textureFunction->sampler)
1319 {
1320 case EbtSampler2D: out << "tex2D"; break;
1321 case EbtSamplerCube: out << "texCUBE"; break;
1322 default: UNREACHABLE();
1323 }
1324
Nicolas Capens75fb4752013-07-10 15:14:47 -04001325 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001326 {
1327 case TextureFunction::IMPLICIT: out << "(s, "; break;
1328 case TextureFunction::BIAS: out << "bias(s, "; break;
1329 case TextureFunction::LOD: out << "lod(s, "; break;
1330 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001331 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001332 default: UNREACHABLE();
1333 }
1334 }
1335 else if (mOutputType == SH_HLSL11_OUTPUT)
1336 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001337 if (textureFunction->method == TextureFunction::GRAD)
1338 {
1339 if (IsIntegerSampler(textureFunction->sampler))
1340 {
1341 out << "x.Load(";
1342 }
1343 else if (IsShadowSampler(textureFunction->sampler))
1344 {
1345 out << "x.SampleCmpLevelZero(s, ";
1346 }
1347 else
1348 {
1349 out << "x.SampleGrad(s, ";
1350 }
1351 }
1352 else if (IsIntegerSampler(textureFunction->sampler) ||
1353 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001354 {
1355 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001356 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001357 else if (IsShadowSampler(textureFunction->sampler))
1358 {
1359 out << "x.SampleCmp(s, ";
1360 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001361 else
1362 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001363 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001364 {
1365 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1366 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1367 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1368 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001369 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001370 default: UNREACHABLE();
1371 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001372 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001373 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001374 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001375
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001376 // Integer sampling requires integer addresses
1377 TString addressx = "";
1378 TString addressy = "";
1379 TString addressz = "";
1380 TString close = "";
1381
Nicolas Capensfc014542014-02-18 14:47:13 -05001382 if (IsIntegerSampler(textureFunction->sampler) ||
1383 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001384 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001385 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001386 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001387 case 2: out << "int3("; break;
1388 case 3: out << "int4("; break;
1389 default: UNREACHABLE();
1390 }
1391
Nicolas Capensfc014542014-02-18 14:47:13 -05001392 // Convert from normalized floating-point to integer
1393 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001394 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001395 addressx = "int(floor(width * frac((";
1396 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001397
Nicolas Capensfc014542014-02-18 14:47:13 -05001398 if (IsSamplerArray(textureFunction->sampler))
1399 {
1400 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1401 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001402 else if (IsSamplerCube(textureFunction->sampler))
1403 {
1404 addressz = "((((";
1405 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001406 else
1407 {
1408 addressz = "int(floor(depth * frac((";
1409 }
1410
1411 close = "))))";
1412 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001413 }
1414 else
1415 {
1416 switch(hlslCoords)
1417 {
1418 case 2: out << "float2("; break;
1419 case 3: out << "float3("; break;
1420 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001421 default: UNREACHABLE();
1422 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001423 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001424
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001425 TString proj = ""; // Only used for projected textures
1426
1427 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001428 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001429 switch(textureFunction->coords)
1430 {
1431 case 3: proj = " / t.z"; break;
1432 case 4: proj = " / t.w"; break;
1433 default: UNREACHABLE();
1434 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001435 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001436
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001437 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001438
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001439 if (mOutputType == SH_HLSL9_OUTPUT)
1440 {
1441 if (hlslCoords >= 3)
1442 {
1443 if (textureFunction->coords < 3)
1444 {
1445 out << ", 0";
1446 }
1447 else
1448 {
1449 out << ", t.z" + proj;
1450 }
1451 }
1452
1453 if (hlslCoords == 4)
1454 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001455 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001456 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001457 case TextureFunction::BIAS: out << ", bias"; break;
1458 case TextureFunction::LOD: out << ", lod"; break;
1459 case TextureFunction::LOD0: out << ", 0"; break;
1460 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001461 default: UNREACHABLE();
1462 }
1463 }
1464
1465 out << "));\n";
1466 }
1467 else if (mOutputType == SH_HLSL11_OUTPUT)
1468 {
1469 if (hlslCoords >= 3)
1470 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001471 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1472 {
1473 out << ", face";
1474 }
1475 else
1476 {
1477 out << ", " + addressz + ("t.z" + proj) + close;
1478 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001479 }
1480
Nicolas Capensd11d5492014-02-19 17:06:10 -05001481 if (textureFunction->method == TextureFunction::GRAD)
1482 {
1483 if (IsIntegerSampler(textureFunction->sampler))
1484 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001485 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001486 }
1487 else if (IsShadowSampler(textureFunction->sampler))
1488 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001489 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001490 switch(textureFunction->coords)
1491 {
1492 case 3: out << "), t.z"; break;
1493 case 4: out << "), t.w"; break;
1494 default: UNREACHABLE();
1495 }
1496 }
1497 else
1498 {
1499 out << "), ddx, ddy";
1500 }
1501 }
1502 else if (IsIntegerSampler(textureFunction->sampler) ||
1503 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001504 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001505 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001506 }
1507 else if (IsShadowSampler(textureFunction->sampler))
1508 {
1509 // Compare value
1510 switch(textureFunction->coords)
1511 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001512 case 3: out << "), t.z"; break;
1513 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001514 default: UNREACHABLE();
1515 }
1516 }
1517 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001518 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001519 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001520 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001521 case TextureFunction::IMPLICIT: out << ")"; break;
1522 case TextureFunction::BIAS: out << "), bias"; break;
1523 case TextureFunction::LOD: out << "), lod"; break;
1524 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001525 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001526 default: UNREACHABLE();
1527 }
1528 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001529
1530 if (textureFunction->offset)
1531 {
1532 out << ", offset";
1533 }
1534
1535 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001536 }
1537 else UNREACHABLE();
1538 }
1539
1540 out << "\n"
1541 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001542 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001543 }
1544
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001545 if (mUsesFragCoord)
1546 {
1547 out << "#define GL_USES_FRAG_COORD\n";
1548 }
1549
1550 if (mUsesPointCoord)
1551 {
1552 out << "#define GL_USES_POINT_COORD\n";
1553 }
1554
1555 if (mUsesFrontFacing)
1556 {
1557 out << "#define GL_USES_FRONT_FACING\n";
1558 }
1559
1560 if (mUsesPointSize)
1561 {
1562 out << "#define GL_USES_POINT_SIZE\n";
1563 }
1564
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001565 if (mUsesFragDepth)
1566 {
1567 out << "#define GL_USES_FRAG_DEPTH\n";
1568 }
1569
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001570 if (mUsesDepthRange)
1571 {
1572 out << "#define GL_USES_DEPTH_RANGE\n";
1573 }
1574
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001575 if (mUsesXor)
1576 {
1577 out << "bool xor(bool p, bool q)\n"
1578 "{\n"
1579 " return (p || q) && !(p && q);\n"
1580 "}\n"
1581 "\n";
1582 }
1583
1584 if (mUsesMod1)
1585 {
1586 out << "float mod(float x, float y)\n"
1587 "{\n"
1588 " return x - y * floor(x / y);\n"
1589 "}\n"
1590 "\n";
1591 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001592
1593 if (mUsesMod2v)
1594 {
1595 out << "float2 mod(float2 x, float2 y)\n"
1596 "{\n"
1597 " return x - y * floor(x / y);\n"
1598 "}\n"
1599 "\n";
1600 }
1601
1602 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001603 {
1604 out << "float2 mod(float2 x, float y)\n"
1605 "{\n"
1606 " return x - y * floor(x / y);\n"
1607 "}\n"
1608 "\n";
1609 }
1610
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001611 if (mUsesMod3v)
1612 {
1613 out << "float3 mod(float3 x, float3 y)\n"
1614 "{\n"
1615 " return x - y * floor(x / y);\n"
1616 "}\n"
1617 "\n";
1618 }
1619
1620 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001621 {
1622 out << "float3 mod(float3 x, float y)\n"
1623 "{\n"
1624 " return x - y * floor(x / y);\n"
1625 "}\n"
1626 "\n";
1627 }
1628
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001629 if (mUsesMod4v)
1630 {
1631 out << "float4 mod(float4 x, float4 y)\n"
1632 "{\n"
1633 " return x - y * floor(x / y);\n"
1634 "}\n"
1635 "\n";
1636 }
1637
1638 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001639 {
1640 out << "float4 mod(float4 x, float y)\n"
1641 "{\n"
1642 " return x - y * floor(x / y);\n"
1643 "}\n"
1644 "\n";
1645 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001646
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001647 if (mUsesFaceforward1)
1648 {
1649 out << "float faceforward(float N, float I, float Nref)\n"
1650 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001651 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001652 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001653 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001654 " }\n"
1655 " else\n"
1656 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001657 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001658 " }\n"
1659 "}\n"
1660 "\n";
1661 }
1662
1663 if (mUsesFaceforward2)
1664 {
1665 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1666 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001667 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001668 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001669 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001670 " }\n"
1671 " else\n"
1672 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001673 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001674 " }\n"
1675 "}\n"
1676 "\n";
1677 }
1678
1679 if (mUsesFaceforward3)
1680 {
1681 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1682 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001683 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001684 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001685 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001686 " }\n"
1687 " else\n"
1688 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001689 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001690 " }\n"
1691 "}\n"
1692 "\n";
1693 }
1694
1695 if (mUsesFaceforward4)
1696 {
1697 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1698 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001699 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001700 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001701 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001702 " }\n"
1703 " else\n"
1704 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001705 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001706 " }\n"
1707 "}\n"
1708 "\n";
1709 }
1710
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001711 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001712 {
1713 out << "float atanyx(float y, float x)\n"
1714 "{\n"
1715 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1716 " return atan2(y, x);\n"
1717 "}\n";
1718 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001719
1720 if (mUsesAtan2_2)
1721 {
1722 out << "float2 atanyx(float2 y, float2 x)\n"
1723 "{\n"
1724 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1725 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1726 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1727 "}\n";
1728 }
1729
1730 if (mUsesAtan2_3)
1731 {
1732 out << "float3 atanyx(float3 y, float3 x)\n"
1733 "{\n"
1734 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1735 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1736 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1737 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1738 "}\n";
1739 }
1740
1741 if (mUsesAtan2_4)
1742 {
1743 out << "float4 atanyx(float4 y, float4 x)\n"
1744 "{\n"
1745 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1746 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1747 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1748 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1749 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1750 "}\n";
1751 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752}
1753
1754void OutputHLSL::visitSymbol(TIntermSymbol *node)
1755{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001756 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757
Jamie Madill570e04d2013-06-21 09:15:33 -04001758 // Handle accessing std140 structs by value
1759 if (mFlaggedStructMappedNames.count(node) > 0)
1760 {
1761 out << mFlaggedStructMappedNames[node];
1762 return;
1763 }
1764
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765 TString name = node->getSymbol();
1766
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001767 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001768 {
1769 mUsesDepthRange = true;
1770 out << name;
1771 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772 else
1773 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001774 TQualifier qualifier = node->getQualifier();
1775
1776 if (qualifier == EvqUniform)
1777 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001778 const TType& nodeType = node->getType();
1779 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1780
1781 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001782 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001783 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001784 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001785 else
1786 {
1787 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001788 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001789
1790 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001791 }
Jamie Madill19571812013-08-12 15:26:34 -07001792 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001793 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001794 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001795 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001796 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001797 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001798 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001799 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001800 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001801 }
Jamie Madill19571812013-08-12 15:26:34 -07001802 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001803 {
1804 mReferencedOutputVariables[name] = node;
1805 out << "out_" << name;
1806 }
1807 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001808 {
1809 out << "gl_Color[0]";
1810 mUsesFragColor = true;
1811 }
1812 else if (qualifier == EvqFragData)
1813 {
1814 out << "gl_Color";
1815 mUsesFragData = true;
1816 }
1817 else if (qualifier == EvqFragCoord)
1818 {
1819 mUsesFragCoord = true;
1820 out << name;
1821 }
1822 else if (qualifier == EvqPointCoord)
1823 {
1824 mUsesPointCoord = true;
1825 out << name;
1826 }
1827 else if (qualifier == EvqFrontFacing)
1828 {
1829 mUsesFrontFacing = true;
1830 out << name;
1831 }
1832 else if (qualifier == EvqPointSize)
1833 {
1834 mUsesPointSize = true;
1835 out << name;
1836 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001837 else if (name == "gl_FragDepthEXT")
1838 {
1839 mUsesFragDepth = true;
1840 out << "gl_Depth";
1841 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001842 else if (qualifier == EvqInternal)
1843 {
1844 out << name;
1845 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001846 else
1847 {
1848 out << decorate(name);
1849 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850 }
1851}
1852
1853bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1854{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001855 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856
Jamie Madill570e04d2013-06-21 09:15:33 -04001857 // Handle accessing std140 structs by value
1858 if (mFlaggedStructMappedNames.count(node) > 0)
1859 {
1860 out << mFlaggedStructMappedNames[node];
1861 return false;
1862 }
1863
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864 switch (node->getOp())
1865 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001866 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001867 case EOpInitialize:
1868 if (visit == PreVisit)
1869 {
1870 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1871 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1872 // new variable is created before the assignment is evaluated), so we need to convert
1873 // this to "float t = x, x = t;".
1874
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001875 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1876 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001877
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001878 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1879 expression->traverse(&searchSymbol);
1880 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001881
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001882 if (sameSymbol)
1883 {
1884 // Type already printed
1885 out << "t" + str(mUniqueIndex) + " = ";
1886 expression->traverse(this);
1887 out << ", ";
1888 symbolNode->traverse(this);
1889 out << " = t" + str(mUniqueIndex);
1890
1891 mUniqueIndex++;
1892 return false;
1893 }
1894 }
1895 else if (visit == InVisit)
1896 {
1897 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001898 }
1899 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001900 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1901 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1902 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1903 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1904 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1905 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001906 if (visit == PreVisit)
1907 {
1908 out << "(";
1909 }
1910 else if (visit == InVisit)
1911 {
1912 out << " = mul(";
1913 node->getLeft()->traverse(this);
1914 out << ", transpose(";
1915 }
1916 else
1917 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001918 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001919 }
1920 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001921 case EOpMatrixTimesMatrixAssign:
1922 if (visit == PreVisit)
1923 {
1924 out << "(";
1925 }
1926 else if (visit == InVisit)
1927 {
1928 out << " = mul(";
1929 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001930 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001931 }
1932 else
1933 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001934 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001935 }
1936 break;
1937 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001938 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001939 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001940 const TType& leftType = node->getLeft()->getType();
1941 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001942 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001943 if (visit == PreVisit)
1944 {
1945 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1946 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1947
1948 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1949 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1950
1951 return false;
1952 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001953 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001954 else
1955 {
1956 outputTriplet(visit, "", "[", "]");
1957 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001958 }
1959 break;
1960 case EOpIndexIndirect:
1961 // We do not currently support indirect references to interface blocks
1962 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1963 outputTriplet(visit, "", "[", "]");
1964 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001965 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001966 if (visit == InVisit)
1967 {
1968 const TStructure* structure = node->getLeft()->getType().getStruct();
1969 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1970 const TField* field = structure->fields()[index->getIConst(0)];
1971 out << "." + decorateField(field->name(), *structure);
1972
1973 return false;
1974 }
1975 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001976 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001977 if (visit == InVisit)
1978 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001979 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1980 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1981 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1982 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001983
1984 return false;
1985 }
1986 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987 case EOpVectorSwizzle:
1988 if (visit == InVisit)
1989 {
1990 out << ".";
1991
1992 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1993
1994 if (swizzle)
1995 {
1996 TIntermSequence &sequence = swizzle->getSequence();
1997
1998 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1999 {
2000 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
2001
2002 if (element)
2003 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002004 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005
2006 switch (i)
2007 {
2008 case 0: out << "x"; break;
2009 case 1: out << "y"; break;
2010 case 2: out << "z"; break;
2011 case 3: out << "w"; break;
2012 default: UNREACHABLE();
2013 }
2014 }
2015 else UNREACHABLE();
2016 }
2017 }
2018 else UNREACHABLE();
2019
2020 return false; // Fully processed
2021 }
2022 break;
2023 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
2024 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
2025 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
2026 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002027 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002028 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002029 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002030 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002031 if (node->getOp() == EOpEqual)
2032 {
2033 outputTriplet(visit, "(", " == ", ")");
2034 }
2035 else
2036 {
2037 outputTriplet(visit, "(", " != ", ")");
2038 }
2039 }
2040 else if (node->getLeft()->getBasicType() == EbtStruct)
2041 {
2042 if (node->getOp() == EOpEqual)
2043 {
2044 out << "(";
2045 }
2046 else
2047 {
2048 out << "!(";
2049 }
2050
Jamie Madill98493dd2013-07-08 14:39:03 -04002051 const TStructure &structure = *node->getLeft()->getType().getStruct();
2052 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002053
Jamie Madill98493dd2013-07-08 14:39:03 -04002054 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002055 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002056 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002057
2058 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002059 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002060 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002061 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002062
Jamie Madill98493dd2013-07-08 14:39:03 -04002063 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002064 {
2065 out << " && ";
2066 }
2067 }
2068
2069 out << ")";
2070
2071 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002072 }
2073 else
2074 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002075 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002076
2077 if (node->getOp() == EOpEqual)
2078 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002079 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002080 }
2081 else
2082 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002083 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002084 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002085 }
2086 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2088 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2089 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2090 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2091 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00002092 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00002093 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
2094 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00002095 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002096 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002097 if (node->getRight()->hasSideEffects())
2098 {
2099 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2100 return false;
2101 }
2102 else
2103 {
2104 outputTriplet(visit, "(", " || ", ")");
2105 return true;
2106 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002107 case EOpLogicalXor:
2108 mUsesXor = true;
2109 outputTriplet(visit, "xor(", ", ", ")");
2110 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002111 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002112 if (node->getRight()->hasSideEffects())
2113 {
2114 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2115 return false;
2116 }
2117 else
2118 {
2119 outputTriplet(visit, "(", " && ", ")");
2120 return true;
2121 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 default: UNREACHABLE();
2123 }
2124
2125 return true;
2126}
2127
2128bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2129{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130 switch (node->getOp())
2131 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002132 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2133 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2134 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2135 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2136 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2137 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2138 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04002140 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 case EOpConvFloatToBool:
2142 switch (node->getOperand()->getType().getNominalSize())
2143 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002144 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2145 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2146 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2147 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 default: UNREACHABLE();
2149 }
2150 break;
2151 case EOpConvBoolToFloat:
2152 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002153 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 switch (node->getOperand()->getType().getNominalSize())
2155 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002156 case 1: outputTriplet(visit, "float(", "", ")"); break;
2157 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2158 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2159 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002160 default: UNREACHABLE();
2161 }
2162 break;
2163 case EOpConvFloatToInt:
2164 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002165 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 switch (node->getOperand()->getType().getNominalSize())
2167 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002168 case 1: outputTriplet(visit, "int(", "", ")"); break;
2169 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2170 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2171 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 default: UNREACHABLE();
2173 }
2174 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002175 case EOpConvFloatToUInt:
2176 case EOpConvBoolToUInt:
2177 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002178 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002179 {
2180 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002181 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2182 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2183 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002184 default: UNREACHABLE();
2185 }
2186 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002187 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2188 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2189 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2190 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2191 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2192 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2193 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2194 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2195 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2196 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2197 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2198 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2199 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2200 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2201 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2202 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2203 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2204 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2205 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2206 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2207 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002208 case EOpDFdx:
2209 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2210 {
2211 outputTriplet(visit, "(", "", ", 0.0)");
2212 }
2213 else
2214 {
2215 outputTriplet(visit, "ddx(", "", ")");
2216 }
2217 break;
2218 case EOpDFdy:
2219 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2220 {
2221 outputTriplet(visit, "(", "", ", 0.0)");
2222 }
2223 else
2224 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002225 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002226 }
2227 break;
2228 case EOpFwidth:
2229 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2230 {
2231 outputTriplet(visit, "(", "", ", 0.0)");
2232 }
2233 else
2234 {
2235 outputTriplet(visit, "fwidth(", "", ")");
2236 }
2237 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002238 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2239 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240 default: UNREACHABLE();
2241 }
2242
2243 return true;
2244}
2245
2246bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2247{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002248 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002249
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250 switch (node->getOp())
2251 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002252 case EOpSequence:
2253 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002254 if (mInsideFunction)
2255 {
Jamie Madill075edd82013-07-08 13:30:19 -04002256 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002257 out << "{\n";
2258 }
2259
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002260 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2261 {
Jamie Madill075edd82013-07-08 13:30:19 -04002262 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002263
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002264 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002265
2266 out << ";\n";
2267 }
2268
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002269 if (mInsideFunction)
2270 {
Jamie Madill075edd82013-07-08 13:30:19 -04002271 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002272 out << "}\n";
2273 }
2274
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002275 return false;
2276 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277 case EOpDeclaration:
2278 if (visit == PreVisit)
2279 {
2280 TIntermSequence &sequence = node->getSequence();
2281 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002283 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002285 if (variable->getType().getStruct())
2286 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04002287 addConstructor(variable->getType(), structNameString(*variable->getType().getStruct()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002288 }
2289
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002290 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002292 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002293 {
2294 out << "static ";
2295 }
2296
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002297 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002299 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002301 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002303 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002305 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002306 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002307 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002308 }
2309 else
2310 {
2311 (*sit)->traverse(this);
2312 }
2313
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002314 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002315 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002316 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 }
2318 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002320 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2321 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002322 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002323 }
2324 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002326 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002327 {
2328 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2329 {
2330 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2331
2332 if (symbol)
2333 {
2334 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2335 mReferencedVaryings[symbol->getSymbol()] = symbol;
2336 }
2337 else
2338 {
2339 (*sit)->traverse(this);
2340 }
2341 }
2342 }
2343
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344 return false;
2345 }
2346 else if (visit == InVisit)
2347 {
2348 out << ", ";
2349 }
2350 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002351 case EOpPrototype:
2352 if (visit == PreVisit)
2353 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002354 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002355
2356 TIntermSequence &arguments = node->getSequence();
2357
2358 for (unsigned int i = 0; i < arguments.size(); i++)
2359 {
2360 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2361
2362 if (symbol)
2363 {
2364 out << argumentString(symbol);
2365
2366 if (i < arguments.size() - 1)
2367 {
2368 out << ", ";
2369 }
2370 }
2371 else UNREACHABLE();
2372 }
2373
2374 out << ");\n";
2375
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002376 // Also prototype the Lod0 variant if needed
2377 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2378 {
2379 mOutputLod0Function = true;
2380 node->traverse(this);
2381 mOutputLod0Function = false;
2382 }
2383
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002384 return false;
2385 }
2386 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002387 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 case EOpFunction:
2389 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002390 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002392 out << typeString(node->getType()) << " ";
2393
2394 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002396 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002398 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002400 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002401 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002402
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002403 TIntermSequence &sequence = node->getSequence();
2404 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2405
2406 for (unsigned int i = 0; i < arguments.size(); i++)
2407 {
2408 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2409
2410 if (symbol)
2411 {
2412 if (symbol->getType().getStruct())
2413 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04002414 addConstructor(symbol->getType(), structNameString(*symbol->getType().getStruct()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002415 }
2416
2417 out << argumentString(symbol);
2418
2419 if (i < arguments.size() - 1)
2420 {
2421 out << ", ";
2422 }
2423 }
2424 else UNREACHABLE();
2425 }
2426
2427 out << ")\n"
2428 "{\n";
2429
2430 if (sequence.size() > 1)
2431 {
2432 mInsideFunction = true;
2433 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002434 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002436
2437 out << "}\n";
2438
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002439 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2440 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002441 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002442 {
2443 mOutputLod0Function = true;
2444 node->traverse(this);
2445 mOutputLod0Function = false;
2446 }
2447 }
2448
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002449 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450 }
2451 break;
2452 case EOpFunctionCall:
2453 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002454 TString name = TFunction::unmangleName(node->getName());
2455 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002456 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002457
2458 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002460 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461 }
2462 else
2463 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002464 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2465
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002466 TextureFunction textureFunction;
2467 textureFunction.sampler = samplerType;
2468 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002469 textureFunction.method = TextureFunction::IMPLICIT;
2470 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002471 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002472
2473 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002474 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002475 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002476 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002477 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002478 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002479 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002480 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002481 }
Nicolas Capens46485082014-04-15 13:12:50 -04002482 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2483 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002484 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002485 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002486 }
Nicolas Capens46485082014-04-15 13:12:50 -04002487 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002488 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002489 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002490 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002491 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002492 else if (name == "textureSize")
2493 {
2494 textureFunction.method = TextureFunction::SIZE;
2495 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002496 else if (name == "textureOffset")
2497 {
2498 textureFunction.method = TextureFunction::IMPLICIT;
2499 textureFunction.offset = true;
2500 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002501 else if (name == "textureProjOffset")
2502 {
2503 textureFunction.method = TextureFunction::IMPLICIT;
2504 textureFunction.offset = true;
2505 textureFunction.proj = true;
2506 }
2507 else if (name == "textureLodOffset")
2508 {
2509 textureFunction.method = TextureFunction::LOD;
2510 textureFunction.offset = true;
2511 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002512 else if (name == "textureProjLodOffset")
2513 {
2514 textureFunction.method = TextureFunction::LOD;
2515 textureFunction.proj = true;
2516 textureFunction.offset = true;
2517 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002518 else if (name == "texelFetch")
2519 {
2520 textureFunction.method = TextureFunction::FETCH;
2521 }
2522 else if (name == "texelFetchOffset")
2523 {
2524 textureFunction.method = TextureFunction::FETCH;
2525 textureFunction.offset = true;
2526 }
Nicolas Capens46485082014-04-15 13:12:50 -04002527 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002528 {
2529 textureFunction.method = TextureFunction::GRAD;
2530 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002531 else if (name == "textureGradOffset")
2532 {
2533 textureFunction.method = TextureFunction::GRAD;
2534 textureFunction.offset = true;
2535 }
Nicolas Capens46485082014-04-15 13:12:50 -04002536 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002537 {
2538 textureFunction.method = TextureFunction::GRAD;
2539 textureFunction.proj = true;
2540 }
2541 else if (name == "textureProjGradOffset")
2542 {
2543 textureFunction.method = TextureFunction::GRAD;
2544 textureFunction.proj = true;
2545 textureFunction.offset = true;
2546 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002547 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002548
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002549 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002550 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002551 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2552
2553 if (textureFunction.offset)
2554 {
2555 mandatoryArgumentCount++;
2556 }
2557
2558 bool bias = (arguments.size() > mandatoryArgumentCount); // Bias argument is optional
2559
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002560 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2561 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002562 if (bias)
2563 {
2564 textureFunction.method = TextureFunction::LOD0BIAS;
2565 }
2566 else
2567 {
2568 textureFunction.method = TextureFunction::LOD0;
2569 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002570 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002571 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002572 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002573 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002574 }
2575 }
2576
2577 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002578
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002579 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002581
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002582 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2583 {
2584 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2585 {
2586 out << "texture_";
2587 (*arg)->traverse(this);
2588 out << ", sampler_";
2589 }
2590
2591 (*arg)->traverse(this);
2592
2593 if (arg < arguments.end() - 1)
2594 {
2595 out << ", ";
2596 }
2597 }
2598
2599 out << ")";
2600
2601 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602 }
2603 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002604 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002605 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002606 addConstructor(node->getType(), "vec1", &node->getSequence());
2607 outputTriplet(visit, "vec1(", "", ")");
2608 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002609 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002610 addConstructor(node->getType(), "vec2", &node->getSequence());
2611 outputTriplet(visit, "vec2(", ", ", ")");
2612 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002613 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002614 addConstructor(node->getType(), "vec3", &node->getSequence());
2615 outputTriplet(visit, "vec3(", ", ", ")");
2616 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002617 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002618 addConstructor(node->getType(), "vec4", &node->getSequence());
2619 outputTriplet(visit, "vec4(", ", ", ")");
2620 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002621 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002622 addConstructor(node->getType(), "bvec1", &node->getSequence());
2623 outputTriplet(visit, "bvec1(", "", ")");
2624 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002625 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002626 addConstructor(node->getType(), "bvec2", &node->getSequence());
2627 outputTriplet(visit, "bvec2(", ", ", ")");
2628 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002629 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002630 addConstructor(node->getType(), "bvec3", &node->getSequence());
2631 outputTriplet(visit, "bvec3(", ", ", ")");
2632 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002633 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002634 addConstructor(node->getType(), "bvec4", &node->getSequence());
2635 outputTriplet(visit, "bvec4(", ", ", ")");
2636 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002637 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002638 addConstructor(node->getType(), "ivec1", &node->getSequence());
2639 outputTriplet(visit, "ivec1(", "", ")");
2640 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002641 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002642 addConstructor(node->getType(), "ivec2", &node->getSequence());
2643 outputTriplet(visit, "ivec2(", ", ", ")");
2644 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002645 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002646 addConstructor(node->getType(), "ivec3", &node->getSequence());
2647 outputTriplet(visit, "ivec3(", ", ", ")");
2648 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002649 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002650 addConstructor(node->getType(), "ivec4", &node->getSequence());
2651 outputTriplet(visit, "ivec4(", ", ", ")");
2652 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002653 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002654 addConstructor(node->getType(), "uvec1", &node->getSequence());
2655 outputTriplet(visit, "uvec1(", "", ")");
2656 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002657 case EOpConstructUVec2:
2658 addConstructor(node->getType(), "uvec2", &node->getSequence());
2659 outputTriplet(visit, "uvec2(", ", ", ")");
2660 break;
2661 case EOpConstructUVec3:
2662 addConstructor(node->getType(), "uvec3", &node->getSequence());
2663 outputTriplet(visit, "uvec3(", ", ", ")");
2664 break;
2665 case EOpConstructUVec4:
2666 addConstructor(node->getType(), "uvec4", &node->getSequence());
2667 outputTriplet(visit, "uvec4(", ", ", ")");
2668 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002669 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002670 addConstructor(node->getType(), "mat2", &node->getSequence());
2671 outputTriplet(visit, "mat2(", ", ", ")");
2672 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002673 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002674 addConstructor(node->getType(), "mat3", &node->getSequence());
2675 outputTriplet(visit, "mat3(", ", ", ")");
2676 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002677 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002678 addConstructor(node->getType(), "mat4", &node->getSequence());
2679 outputTriplet(visit, "mat4(", ", ", ")");
2680 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002681 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002682 {
2683 const TString &structName = structNameString(*node->getType().getStruct());
2684 addConstructor(node->getType(), structName, &node->getSequence());
2685 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2686 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002687 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002688 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2689 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2690 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2691 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2692 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2693 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002694 case EOpMod:
2695 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002696 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002697 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2698 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2699 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002700 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002701 case 11: mUsesMod1 = true; break;
2702 case 22: mUsesMod2v = true; break;
2703 case 21: mUsesMod2f = true; break;
2704 case 33: mUsesMod3v = true; break;
2705 case 31: mUsesMod3f = true; break;
2706 case 44: mUsesMod4v = true; break;
2707 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002708 default: UNREACHABLE();
2709 }
2710
2711 outputTriplet(visit, "mod(", ", ", ")");
2712 }
2713 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002714 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002716 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002717 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2718 {
2719 case 1: mUsesAtan2_1 = true; break;
2720 case 2: mUsesAtan2_2 = true; break;
2721 case 3: mUsesAtan2_3 = true; break;
2722 case 4: mUsesAtan2_4 = true; break;
2723 default: UNREACHABLE();
2724 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002725 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726 break;
2727 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2728 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2729 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2730 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2731 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2732 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2733 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2734 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2735 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002736 case EOpFaceForward:
2737 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002738 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002739 {
2740 case 1: mUsesFaceforward1 = true; break;
2741 case 2: mUsesFaceforward2 = true; break;
2742 case 3: mUsesFaceforward3 = true; break;
2743 case 4: mUsesFaceforward4 = true; break;
2744 default: UNREACHABLE();
2745 }
2746
2747 outputTriplet(visit, "faceforward(", ", ", ")");
2748 }
2749 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002750 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2751 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2752 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753 default: UNREACHABLE();
2754 }
2755
2756 return true;
2757}
2758
2759bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2760{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002761 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002763 if (node->usesTernaryOperator())
2764 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002765 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002766 }
2767 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002769 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002770
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002771 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002772
2773 node->getCondition()->traverse(this);
2774
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002775 out << ")\n";
2776
Jamie Madill075edd82013-07-08 13:30:19 -04002777 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002778 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002779
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002780 bool discard = false;
2781
daniel@transgaming.combb885322010-04-15 20:45:24 +00002782 if (node->getTrueBlock())
2783 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002784 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002785
2786 // Detect true discard
2787 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002788 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789
Jamie Madill075edd82013-07-08 13:30:19 -04002790 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002791 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002792
2793 if (node->getFalseBlock())
2794 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002795 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002796
Jamie Madill075edd82013-07-08 13:30:19 -04002797 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002798 out << "{\n";
2799
Jamie Madill075edd82013-07-08 13:30:19 -04002800 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002801 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002802
Jamie Madill075edd82013-07-08 13:30:19 -04002803 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002804 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002805
2806 // Detect false discard
2807 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2808 }
2809
2810 // ANGLE issue 486: Detect problematic conditional discard
2811 if (discard && FindSideEffectRewriting::search(node))
2812 {
2813 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002814 }
2815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816
2817 return false;
2818}
2819
2820void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2821{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002822 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002823}
2824
2825bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2826{
Nicolas Capens655fe362014-04-11 13:12:34 -04002827 mNestedLoopDepth++;
2828
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002829 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2830
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002831 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002832 {
2833 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2834 }
2835
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002836 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002837 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002838 if (handleExcessiveLoop(node))
2839 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002840 mInsideDiscontinuousLoop = wasDiscontinuous;
2841 mNestedLoopDepth--;
2842
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002843 return false;
2844 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002845 }
2846
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002847 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002848
alokp@chromium.org52813552010-11-16 18:36:09 +00002849 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002851 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002852
Jamie Madill075edd82013-07-08 13:30:19 -04002853 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002854 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002855 }
2856 else
2857 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002858 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002859
2860 if (node->getInit())
2861 {
2862 node->getInit()->traverse(this);
2863 }
2864
2865 out << "; ";
2866
alokp@chromium.org52813552010-11-16 18:36:09 +00002867 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002868 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002869 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870 }
2871
2872 out << "; ";
2873
alokp@chromium.org52813552010-11-16 18:36:09 +00002874 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002875 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002876 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002877 }
2878
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002879 out << ")\n";
2880
Jamie Madill075edd82013-07-08 13:30:19 -04002881 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002882 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883 }
2884
2885 if (node->getBody())
2886 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002887 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002888 }
2889
Jamie Madill075edd82013-07-08 13:30:19 -04002890 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002891 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002892
alokp@chromium.org52813552010-11-16 18:36:09 +00002893 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002894 {
Jamie Madill075edd82013-07-08 13:30:19 -04002895 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002896 out << "while(\n";
2897
alokp@chromium.org52813552010-11-16 18:36:09 +00002898 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002899
daniel@transgaming.com73536982012-03-21 20:45:49 +00002900 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002901 }
2902
daniel@transgaming.com73536982012-03-21 20:45:49 +00002903 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002904
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002905 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002906 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002907
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002908 return false;
2909}
2910
2911bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2912{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002913 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002914
2915 switch (node->getFlowOp())
2916 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002917 case EOpKill:
2918 outputTriplet(visit, "discard;\n", "", "");
2919 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002920 case EOpBreak:
2921 if (visit == PreVisit)
2922 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002923 if (mNestedLoopDepth > 1)
2924 {
2925 mUsesNestedBreak = true;
2926 }
2927
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002928 if (mExcessiveLoopIndex)
2929 {
2930 out << "{Break";
2931 mExcessiveLoopIndex->traverse(this);
2932 out << " = true; break;}\n";
2933 }
2934 else
2935 {
2936 out << "break;\n";
2937 }
2938 }
2939 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002940 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941 case EOpReturn:
2942 if (visit == PreVisit)
2943 {
2944 if (node->getExpression())
2945 {
2946 out << "return ";
2947 }
2948 else
2949 {
2950 out << "return;\n";
2951 }
2952 }
2953 else if (visit == PostVisit)
2954 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002955 if (node->getExpression())
2956 {
2957 out << ";\n";
2958 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002959 }
2960 break;
2961 default: UNREACHABLE();
2962 }
2963
2964 return true;
2965}
2966
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002967void OutputHLSL::traverseStatements(TIntermNode *node)
2968{
2969 if (isSingleStatement(node))
2970 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002971 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002972 }
2973
2974 node->traverse(this);
2975}
2976
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002977bool OutputHLSL::isSingleStatement(TIntermNode *node)
2978{
2979 TIntermAggregate *aggregate = node->getAsAggregate();
2980
2981 if (aggregate)
2982 {
2983 if (aggregate->getOp() == EOpSequence)
2984 {
2985 return false;
2986 }
2987 else
2988 {
2989 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2990 {
2991 if (!isSingleStatement(*sit))
2992 {
2993 return false;
2994 }
2995 }
2996
2997 return true;
2998 }
2999 }
3000
3001 return true;
3002}
3003
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003004// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
3005// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003006bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
3007{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003008 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003009 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003010
3011 // Parse loops of the form:
3012 // for(int index = initial; index [comparator] limit; index += increment)
3013 TIntermSymbol *index = NULL;
3014 TOperator comparator = EOpNull;
3015 int initial = 0;
3016 int limit = 0;
3017 int increment = 0;
3018
3019 // Parse index name and intial value
3020 if (node->getInit())
3021 {
3022 TIntermAggregate *init = node->getInit()->getAsAggregate();
3023
3024 if (init)
3025 {
3026 TIntermSequence &sequence = init->getSequence();
3027 TIntermTyped *variable = sequence[0]->getAsTyped();
3028
3029 if (variable && variable->getQualifier() == EvqTemporary)
3030 {
3031 TIntermBinary *assign = variable->getAsBinaryNode();
3032
3033 if (assign->getOp() == EOpInitialize)
3034 {
3035 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3036 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3037
3038 if (symbol && constant)
3039 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003040 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003041 {
3042 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003043 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003044 }
3045 }
3046 }
3047 }
3048 }
3049 }
3050
3051 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003052 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003053 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003054 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003055
3056 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3057 {
3058 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3059
3060 if (constant)
3061 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003062 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003063 {
3064 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003065 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003066 }
3067 }
3068 }
3069 }
3070
3071 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003072 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003073 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003074 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3075 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003076
3077 if (binaryTerminal)
3078 {
3079 TOperator op = binaryTerminal->getOp();
3080 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3081
3082 if (constant)
3083 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003084 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003085 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003086 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003087
3088 switch (op)
3089 {
3090 case EOpAddAssign: increment = value; break;
3091 case EOpSubAssign: increment = -value; break;
3092 default: UNIMPLEMENTED();
3093 }
3094 }
3095 }
3096 }
3097 else if (unaryTerminal)
3098 {
3099 TOperator op = unaryTerminal->getOp();
3100
3101 switch (op)
3102 {
3103 case EOpPostIncrement: increment = 1; break;
3104 case EOpPostDecrement: increment = -1; break;
3105 case EOpPreIncrement: increment = 1; break;
3106 case EOpPreDecrement: increment = -1; break;
3107 default: UNIMPLEMENTED();
3108 }
3109 }
3110 }
3111
3112 if (index != NULL && comparator != EOpNull && increment != 0)
3113 {
3114 if (comparator == EOpLessThanEqual)
3115 {
3116 comparator = EOpLessThan;
3117 limit += 1;
3118 }
3119
3120 if (comparator == EOpLessThan)
3121 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003122 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003123
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003124 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003125 {
3126 return false; // Not an excessive loop
3127 }
3128
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003129 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3130 mExcessiveLoopIndex = index;
3131
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003132 out << "{int ";
3133 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003134 out << ";\n"
3135 "bool Break";
3136 index->traverse(this);
3137 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003138
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003139 bool firstLoopFragment = true;
3140
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003141 while (iterations > 0)
3142 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003143 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003144
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003145 if (!firstLoopFragment)
3146 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003147 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003148 index->traverse(this);
3149 out << ") {\n";
3150 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003151
3152 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3153 {
3154 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3155 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003156
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003157 // for(int index = initial; index < clampedLimit; index += increment)
3158
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003159 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003160 index->traverse(this);
3161 out << " = ";
3162 out << initial;
3163
3164 out << "; ";
3165 index->traverse(this);
3166 out << " < ";
3167 out << clampedLimit;
3168
3169 out << "; ";
3170 index->traverse(this);
3171 out << " += ";
3172 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003173 out << ")\n";
3174
Jamie Madill075edd82013-07-08 13:30:19 -04003175 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003176 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003177
3178 if (node->getBody())
3179 {
3180 node->getBody()->traverse(this);
3181 }
3182
Jamie Madill075edd82013-07-08 13:30:19 -04003183 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003184 out << ";}\n";
3185
3186 if (!firstLoopFragment)
3187 {
3188 out << "}\n";
3189 }
3190
3191 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003192
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003193 initial += MAX_LOOP_ITERATIONS * increment;
3194 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003195 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003196
3197 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003198
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003199 mExcessiveLoopIndex = restoreIndex;
3200
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003201 return true;
3202 }
3203 else UNIMPLEMENTED();
3204 }
3205
3206 return false; // Not handled as an excessive loop
3207}
3208
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003209void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003210{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003211 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003212
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003213 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003214 {
3215 out << preString;
3216 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003217 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003218 {
3219 out << inString;
3220 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003221 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003222 {
3223 out << postString;
3224 }
3225}
3226
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003227void OutputHLSL::outputLineDirective(int line)
3228{
3229 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3230 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003231 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003232 mBody << "#line " << line;
3233
3234 if (mContext.sourcePath)
3235 {
3236 mBody << " \"" << mContext.sourcePath << "\"";
3237 }
3238
3239 mBody << "\n";
3240 }
3241}
3242
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003243TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3244{
3245 TQualifier qualifier = symbol->getQualifier();
3246 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003247 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003248
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003249 if (name.empty()) // HLSL demands named arguments, also for prototypes
3250 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003251 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003252 }
3253 else
3254 {
3255 name = decorate(name);
3256 }
3257
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003258 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3259 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003260 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3261 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003262 }
3263
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003264 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003265}
3266
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003267TString OutputHLSL::interpolationString(TQualifier qualifier)
3268{
3269 switch(qualifier)
3270 {
3271 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003272 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003273 case EvqInvariantVaryingIn: return "";
3274 case EvqSmoothIn: return "linear";
3275 case EvqFlatIn: return "nointerpolation";
3276 case EvqCentroidIn: return "centroid";
3277 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003278 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003279 case EvqInvariantVaryingOut: return "";
3280 case EvqSmoothOut: return "linear";
3281 case EvqFlatOut: return "nointerpolation";
3282 case EvqCentroidOut: return "centroid";
3283 default: UNREACHABLE();
3284 }
3285
3286 return "";
3287}
3288
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003289TString OutputHLSL::qualifierString(TQualifier qualifier)
3290{
3291 switch(qualifier)
3292 {
3293 case EvqIn: return "in";
Geoff Lang5f5320a2014-05-26 13:56:11 -04003294 case EvqOut: return "inout"; // 'out' results in an HLSL error if not all fields are written, for GLSL it's undefined
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003295 case EvqInOut: return "inout";
3296 case EvqConstReadOnly: return "const";
3297 default: UNREACHABLE();
3298 }
3299
3300 return "";
3301}
3302
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003303TString OutputHLSL::typeString(const TType &type)
3304{
Jamie Madill98493dd2013-07-08 14:39:03 -04003305 const TStructure* structure = type.getStruct();
3306 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003307 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003308 const TString& typeName = structure->name();
3309 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003310 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04003311 return structNameString(*type.getStruct());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003312 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003313 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003314 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003315 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003316 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003317 }
3318 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003319 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003320 int cols = type.getCols();
3321 int rows = type.getRows();
3322 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003323 }
3324 else
3325 {
3326 switch (type.getBasicType())
3327 {
3328 case EbtFloat:
3329 switch (type.getNominalSize())
3330 {
3331 case 1: return "float";
3332 case 2: return "float2";
3333 case 3: return "float3";
3334 case 4: return "float4";
3335 }
3336 case EbtInt:
3337 switch (type.getNominalSize())
3338 {
3339 case 1: return "int";
3340 case 2: return "int2";
3341 case 3: return "int3";
3342 case 4: return "int4";
3343 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003344 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003345 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003346 {
3347 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003348 case 2: return "uint2";
3349 case 3: return "uint3";
3350 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003351 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003352 case EbtBool:
3353 switch (type.getNominalSize())
3354 {
3355 case 1: return "bool";
3356 case 2: return "bool2";
3357 case 3: return "bool3";
3358 case 4: return "bool4";
3359 }
3360 case EbtVoid:
3361 return "void";
3362 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003363 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003364 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003365 case EbtSampler2DArray:
3366 case EbtISampler2DArray:
3367 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003368 return "sampler2D";
3369 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003370 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003371 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003372 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003373 case EbtSamplerExternalOES:
3374 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003375 default:
3376 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003377 }
3378 }
3379
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003380 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003381 return "<unknown type>";
3382}
3383
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003384TString OutputHLSL::textureString(const TType &type)
3385{
3386 switch (type.getBasicType())
3387 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003388 case EbtSampler2D: return "Texture2D";
3389 case EbtSamplerCube: return "TextureCube";
3390 case EbtSamplerExternalOES: return "Texture2D";
3391 case EbtSampler2DArray: return "Texture2DArray";
3392 case EbtSampler3D: return "Texture3D";
3393 case EbtISampler2D: return "Texture2D<int4>";
3394 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003395 case EbtISamplerCube: return "Texture2DArray<int4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003396 case EbtISampler2DArray: return "Texture2DArray<int4>";
3397 case EbtUSampler2D: return "Texture2D<uint4>";
3398 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003399 case EbtUSamplerCube: return "Texture2DArray<uint4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003400 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3401 case EbtSampler2DShadow: return "Texture2D";
3402 case EbtSamplerCubeShadow: return "TextureCube";
3403 case EbtSampler2DArrayShadow: return "Texture2DArray";
3404 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003405 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003406
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003407 return "<unknown texture type>";
3408}
3409
Nicolas Capenscb127d32013-07-15 17:26:18 -04003410TString OutputHLSL::samplerString(const TType &type)
3411{
3412 if (IsShadowSampler(type.getBasicType()))
3413 {
3414 return "SamplerComparisonState";
3415 }
3416 else
3417 {
3418 return "SamplerState";
3419 }
3420}
3421
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003422TString OutputHLSL::arrayString(const TType &type)
3423{
3424 if (!type.isArray())
3425 {
3426 return "";
3427 }
3428
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003429 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003430}
3431
3432TString OutputHLSL::initializer(const TType &type)
3433{
3434 TString string;
3435
Jamie Madill94bf7f22013-07-08 13:31:15 -04003436 size_t size = type.getObjectSize();
3437 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003438 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003439 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003440
Jamie Madill94bf7f22013-07-08 13:31:15 -04003441 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003442 {
3443 string += ", ";
3444 }
3445 }
3446
daniel@transgaming.comead23042010-04-29 03:35:36 +00003447 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003448}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003449
Jamie Madill98493dd2013-07-08 14:39:03 -04003450TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003451{
Jamie Madill98493dd2013-07-08 14:39:03 -04003452 const TFieldList &fields = structure.fields();
3453 const bool isNameless = (structure.name() == "");
3454 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003455 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3456
Jamie Madill98493dd2013-07-08 14:39:03 -04003457 TString string;
3458 string += declareString + "\n"
3459 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003460
Jamie Madillc835df62013-06-21 09:15:32 -04003461 int elementIndex = 0;
3462
Jamie Madill9cf6c072013-06-20 11:55:53 -04003463 for (unsigned int i = 0; i < fields.size(); i++)
3464 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003465 const TField &field = *fields[i];
3466 const TType &fieldType = *field.type();
3467 const TStructure *fieldStruct = fieldType.getStruct();
3468 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003469
Jamie Madillc835df62013-06-21 09:15:32 -04003470 if (useStd140Packing)
3471 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003472 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003473 }
3474
Jamie Madill98493dd2013-07-08 14:39:03 -04003475 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003476
3477 if (useStd140Packing)
3478 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003479 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003480 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003481 }
3482
3483 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003484 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003485
Jamie Madille4075c92013-06-21 09:15:32 -04003486 // Add remaining element index to the global map, for use with nested structs in standard layouts
3487 if (useStd140Packing)
3488 {
3489 mStd140StructElementIndexes[structName] = elementIndex;
3490 }
3491
Jamie Madill98493dd2013-07-08 14:39:03 -04003492 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003493}
3494
Jamie Madill98493dd2013-07-08 14:39:03 -04003495TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003496{
Jamie Madill98493dd2013-07-08 14:39:03 -04003497 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003498 {
3499 return "";
3500 }
3501
3502 TString prefix = "";
3503
3504 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3505 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003506
3507 if (useStd140Packing)
3508 {
3509 prefix += "std";
3510 }
3511
Jamie Madill9cf6c072013-06-20 11:55:53 -04003512 if (useHLSLRowMajorPacking)
3513 {
Jamie Madillc835df62013-06-21 09:15:32 -04003514 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003515 prefix += "rm";
3516 }
3517
Jamie Madillbfa91f42014-06-05 15:45:18 -04003518 return prefix + structNameString(structure);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003519}
3520
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003521void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003522{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003523 if (name == "")
3524 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003525 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003526 }
3527
Jamie Madill96509e42014-05-29 14:33:27 -04003528 if (type.getStruct() && mStructNames.find(name) != mStructNames.end())
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003529 {
3530 return; // Already added
3531 }
3532
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003533 TType ctorType = type;
3534 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003535 ctorType.setPrecision(EbpHigh);
3536 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003537
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003538 typedef std::vector<TType> ParameterArray;
3539 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003540
Jamie Madill98493dd2013-07-08 14:39:03 -04003541 const TStructure* structure = type.getStruct();
3542 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003543 {
Jamie Madill96509e42014-05-29 14:33:27 -04003544 mStructNames.insert(name);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003545
Jamie Madill98493dd2013-07-08 14:39:03 -04003546 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003547
Jamie Madill98493dd2013-07-08 14:39:03 -04003548 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003549 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003550 // Add row-major packed struct for interface blocks
3551 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003552 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003553 "#pragma pack_matrix(column_major)\n";
3554
Jamie Madill98493dd2013-07-08 14:39:03 -04003555 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003556 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003557 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003558 "#pragma pack_matrix(column_major)\n";
3559
Jamie Madill98493dd2013-07-08 14:39:03 -04003560 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003561 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003562 mStructDeclarations.push_back(std140String);
3563 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003564 }
3565
Jamie Madill98493dd2013-07-08 14:39:03 -04003566 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003567 for (unsigned int i = 0; i < fields.size(); i++)
3568 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003569 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003570 }
3571 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003572 else if (parameters)
3573 {
3574 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3575 {
3576 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3577 }
3578 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003579 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003580
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003581 TString constructor;
3582
3583 if (ctorType.getStruct())
3584 {
Jamie Madill96509e42014-05-29 14:33:27 -04003585 constructor += name + " " + name + "_ctor(";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003586 }
3587 else // Built-in type
3588 {
Jamie Madill96509e42014-05-29 14:33:27 -04003589 constructor += typeString(ctorType) + " " + name + "(";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003590 }
3591
3592 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3593 {
3594 const TType &type = ctorParameters[parameter];
3595
3596 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3597
3598 if (parameter < ctorParameters.size() - 1)
3599 {
3600 constructor += ", ";
3601 }
3602 }
3603
3604 constructor += ")\n"
3605 "{\n";
3606
3607 if (ctorType.getStruct())
3608 {
Jamie Madill96509e42014-05-29 14:33:27 -04003609 constructor += " " + name + " structure = {";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003610 }
3611 else
3612 {
3613 constructor += " return " + typeString(ctorType) + "(";
3614 }
3615
3616 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3617 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003618 int rows = ctorType.getRows();
3619 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003620 const TType &parameter = ctorParameters[0];
3621
3622 if (parameter.isScalar())
3623 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003624 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003625 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003626 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003627 {
3628 constructor += TString((row == col) ? "x0" : "0.0");
3629
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003630 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003631 {
3632 constructor += ", ";
3633 }
3634 }
3635 }
3636 }
3637 else if (parameter.isMatrix())
3638 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003639 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003640 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003641 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003642 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003643 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003644 {
3645 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3646 }
3647 else
3648 {
3649 constructor += TString((row == col) ? "1.0" : "0.0");
3650 }
3651
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003652 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003653 {
3654 constructor += ", ";
3655 }
3656 }
3657 }
3658 }
3659 else UNREACHABLE();
3660 }
3661 else
3662 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003663 size_t remainingComponents = ctorType.getObjectSize();
3664 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003665
3666 while (remainingComponents > 0)
3667 {
3668 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003669 const size_t parameterSize = parameter.getObjectSize();
3670 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003671
3672 constructor += "x" + str(parameterIndex);
3673
3674 if (parameter.isScalar())
3675 {
3676 remainingComponents -= parameter.getObjectSize();
3677 }
3678 else if (parameter.isVector())
3679 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003680 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003681 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003682 ASSERT(parameterSize <= remainingComponents);
3683 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003684 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003685 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003686 {
3687 switch (remainingComponents)
3688 {
3689 case 1: constructor += ".x"; break;
3690 case 2: constructor += ".xy"; break;
3691 case 3: constructor += ".xyz"; break;
3692 case 4: constructor += ".xyzw"; break;
3693 default: UNREACHABLE();
3694 }
3695
3696 remainingComponents = 0;
3697 }
3698 else UNREACHABLE();
3699 }
3700 else if (parameter.isMatrix() || parameter.getStruct())
3701 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003702 ASSERT(remainingComponents == parameterSize || moreParameters);
3703 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003704
Jamie Madill94bf7f22013-07-08 13:31:15 -04003705 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003706 }
3707 else UNREACHABLE();
3708
3709 if (moreParameters)
3710 {
3711 parameterIndex++;
3712 }
3713
3714 if (remainingComponents)
3715 {
3716 constructor += ", ";
3717 }
3718 }
3719 }
3720
3721 if (ctorType.getStruct())
3722 {
3723 constructor += "};\n"
3724 " return structure;\n"
3725 "}\n";
3726 }
3727 else
3728 {
3729 constructor += ");\n"
3730 "}\n";
3731 }
3732
daniel@transgaming.com63691862010-04-29 03:32:42 +00003733 mConstructors.insert(constructor);
3734}
3735
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003736const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3737{
3738 TInfoSinkBase &out = mBody;
3739
Jamie Madill98493dd2013-07-08 14:39:03 -04003740 const TStructure* structure = type.getStruct();
3741 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003742 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04003743 out << structNameString(*structure) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003744
Jamie Madill98493dd2013-07-08 14:39:03 -04003745 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003746
Jamie Madill98493dd2013-07-08 14:39:03 -04003747 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003748 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003749 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003750
3751 constUnion = writeConstantUnion(*fieldType, constUnion);
3752
Jamie Madill98493dd2013-07-08 14:39:03 -04003753 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003754 {
3755 out << ", ";
3756 }
3757 }
3758
3759 out << ")";
3760 }
3761 else
3762 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003763 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003764 bool writeType = size > 1;
3765
3766 if (writeType)
3767 {
3768 out << typeString(type) << "(";
3769 }
3770
Jamie Madill94bf7f22013-07-08 13:31:15 -04003771 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003772 {
3773 switch (constUnion->getType())
3774 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003775 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003776 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003777 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003778 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003779 default: UNREACHABLE();
3780 }
3781
3782 if (i != size - 1)
3783 {
3784 out << ", ";
3785 }
3786 }
3787
3788 if (writeType)
3789 {
3790 out << ")";
3791 }
3792 }
3793
3794 return constUnion;
3795}
3796
Jamie Madillbfa91f42014-06-05 15:45:18 -04003797TString OutputHLSL::structNameString(const TStructure &structure)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003798{
Jamie Madillbfa91f42014-06-05 15:45:18 -04003799 if (structure.name().empty())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003800 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04003801 return "";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003802 }
3803
Jamie Madillbfa91f42014-06-05 15:45:18 -04003804 return "ss_" + str(structure.uniqueId()) + structure.name();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003805}
3806
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003807TString OutputHLSL::decorate(const TString &string)
3808{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003809 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003810 {
3811 return "_" + string;
3812 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003813
3814 return string;
3815}
3816
apatrick@chromium.org65756022012-01-17 21:45:38 +00003817TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003818{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003819 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003820 {
3821 return "ex_" + string;
3822 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003823
3824 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003825}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003826
Jamie Madill98493dd2013-07-08 14:39:03 -04003827TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003828{
Jamie Madill98493dd2013-07-08 14:39:03 -04003829 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003830 {
3831 return decorate(string);
3832 }
3833
3834 return string;
3835}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003836
Jamie Madill834e8b72014-04-11 13:33:58 -04003837void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<gl::InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003838{
Jamie Madill98493dd2013-07-08 14:39:03 -04003839 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003840
3841 if (!structure)
3842 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003843 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill834e8b72014-04-11 13:33:58 -04003844 gl::InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3845 (unsigned int)type.getArraySize(), isRowMajorMatrix);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003846 output.push_back(field);
3847 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003848 else
3849 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003850 gl::InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003851
3852 const TFieldList &fields = structure->fields();
3853
3854 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3855 {
3856 TField *field = fields[fieldIndex];
3857 TType *fieldType = field->type();
3858
3859 // make sure to copy matrix packing information
3860 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3861
3862 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3863 }
3864
3865 output.push_back(structField);
3866 }
3867}
3868
Jamie Madill834e8b72014-04-11 13:33:58 -04003869gl::Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<gl::Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003870{
3871 const TStructure *structure = type.getStruct();
3872
3873 if (!structure)
3874 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003875 gl::Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
3876 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003877 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003878
3879 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003880 }
3881 else
3882 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003883 gl::Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3884 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003885
Jamie Madill98493dd2013-07-08 14:39:03 -04003886 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003887
Jamie Madill98493dd2013-07-08 14:39:03 -04003888 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003889 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003890 TField *field = fields[fieldIndex];
3891 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003892
Jamie Madill56093782013-08-30 13:21:11 -04003893 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003894 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003895
Jamie Madill56093782013-08-30 13:21:11 -04003896 // assign register offset information -- this will override the information in any sub-structures.
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003897 HLSLVariableGetRegisterInfo(registerIndex, &structUniform, mOutputType);
Jamie Madill56093782013-08-30 13:21:11 -04003898
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003899 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003900
3901 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003902 }
3903}
3904
Jamie Madill834e8b72014-04-11 13:33:58 -04003905gl::InterpolationType getInterpolationType(TQualifier qualifier)
Jamie Madill139b9092013-08-30 13:21:06 -04003906{
3907 switch (qualifier)
3908 {
3909 case EvqFlatIn:
3910 case EvqFlatOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003911 return gl::INTERPOLATION_FLAT;
Jamie Madill139b9092013-08-30 13:21:06 -04003912
3913 case EvqSmoothIn:
3914 case EvqSmoothOut:
3915 case EvqVertexOut:
3916 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003917 case EvqVaryingIn:
3918 case EvqVaryingOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003919 return gl::INTERPOLATION_SMOOTH;
Jamie Madill139b9092013-08-30 13:21:06 -04003920
3921 case EvqCentroidIn:
3922 case EvqCentroidOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003923 return gl::INTERPOLATION_CENTROID;
Jamie Madill139b9092013-08-30 13:21:06 -04003924
3925 default: UNREACHABLE();
Jamie Madill834e8b72014-04-11 13:33:58 -04003926 return gl::INTERPOLATION_SMOOTH;
Jamie Madill139b9092013-08-30 13:21:06 -04003927 }
3928}
3929
Jamie Madill834e8b72014-04-11 13:33:58 -04003930void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<gl::Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003931{
3932 const TStructure *structure = type.getStruct();
3933
Jamie Madill834e8b72014-04-11 13:33:58 -04003934 gl::InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003935 if (!structure)
3936 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003937 gl::Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003938 fieldsOut.push_back(varying);
3939 }
3940 else
3941 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003942 gl::Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003943 const TFieldList &fields = structure->fields();
3944
Jamie Madill28167c62013-08-30 13:21:10 -04003945 structVarying.structName = structure->name().c_str();
3946
Jamie Madill47fdd132013-08-30 13:21:04 -04003947 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3948 {
3949 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003950 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003951 }
3952
3953 fieldsOut.push_back(structVarying);
3954 }
3955}
3956
Jamie Madillc2141fb2013-08-30 13:21:08 -04003957int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003958{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003959 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3960
Jamie Madill834e8b72014-04-11 13:33:58 -04003961 const gl::Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003962
3963 if (IsSampler(type.getBasicType()))
3964 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003965 mSamplerRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003966 }
3967 else
3968 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003969 mUniformRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003970 }
3971
3972 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003973}
3974
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003975GLenum OutputHLSL::glVariableType(const TType &type)
3976{
3977 if (type.getBasicType() == EbtFloat)
3978 {
3979 if (type.isScalar())
3980 {
3981 return GL_FLOAT;
3982 }
3983 else if (type.isVector())
3984 {
3985 switch(type.getNominalSize())
3986 {
3987 case 2: return GL_FLOAT_VEC2;
3988 case 3: return GL_FLOAT_VEC3;
3989 case 4: return GL_FLOAT_VEC4;
3990 default: UNREACHABLE();
3991 }
3992 }
3993 else if (type.isMatrix())
3994 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003995 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003996 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003997 case 2:
3998 switch(type.getRows())
3999 {
4000 case 2: return GL_FLOAT_MAT2;
4001 case 3: return GL_FLOAT_MAT2x3;
4002 case 4: return GL_FLOAT_MAT2x4;
4003 default: UNREACHABLE();
4004 }
4005
4006 case 3:
4007 switch(type.getRows())
4008 {
4009 case 2: return GL_FLOAT_MAT3x2;
4010 case 3: return GL_FLOAT_MAT3;
4011 case 4: return GL_FLOAT_MAT3x4;
4012 default: UNREACHABLE();
4013 }
4014
4015 case 4:
4016 switch(type.getRows())
4017 {
4018 case 2: return GL_FLOAT_MAT4x2;
4019 case 3: return GL_FLOAT_MAT4x3;
4020 case 4: return GL_FLOAT_MAT4;
4021 default: UNREACHABLE();
4022 }
4023
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004024 default: UNREACHABLE();
4025 }
4026 }
4027 else UNREACHABLE();
4028 }
4029 else if (type.getBasicType() == EbtInt)
4030 {
4031 if (type.isScalar())
4032 {
4033 return GL_INT;
4034 }
4035 else if (type.isVector())
4036 {
4037 switch(type.getNominalSize())
4038 {
4039 case 2: return GL_INT_VEC2;
4040 case 3: return GL_INT_VEC3;
4041 case 4: return GL_INT_VEC4;
4042 default: UNREACHABLE();
4043 }
4044 }
4045 else UNREACHABLE();
4046 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004047 else if (type.getBasicType() == EbtUInt)
4048 {
4049 if (type.isScalar())
4050 {
4051 return GL_UNSIGNED_INT;
4052 }
4053 else if (type.isVector())
4054 {
Jamie Madill22d63da2013-06-07 12:45:12 -04004055 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00004056 {
4057 case 2: return GL_UNSIGNED_INT_VEC2;
4058 case 3: return GL_UNSIGNED_INT_VEC3;
4059 case 4: return GL_UNSIGNED_INT_VEC4;
4060 default: UNREACHABLE();
4061 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004062 }
4063 else UNREACHABLE();
4064 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004065 else if (type.getBasicType() == EbtBool)
4066 {
4067 if (type.isScalar())
4068 {
4069 return GL_BOOL;
4070 }
4071 else if (type.isVector())
4072 {
4073 switch(type.getNominalSize())
4074 {
4075 case 2: return GL_BOOL_VEC2;
4076 case 3: return GL_BOOL_VEC3;
4077 case 4: return GL_BOOL_VEC4;
4078 default: UNREACHABLE();
4079 }
4080 }
4081 else UNREACHABLE();
4082 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004083
4084 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004085 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004086 case EbtSampler2D: return GL_SAMPLER_2D;
4087 case EbtSampler3D: return GL_SAMPLER_3D;
4088 case EbtSamplerCube: return GL_SAMPLER_CUBE;
4089 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
4090 case EbtISampler2D: return GL_INT_SAMPLER_2D;
4091 case EbtISampler3D: return GL_INT_SAMPLER_3D;
4092 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
4093 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
4094 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
4095 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
4096 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
4097 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
4098 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
4099 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
4100 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
4101 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004102 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004103
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004104 return GL_NONE;
4105}
4106
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004107GLenum OutputHLSL::glVariablePrecision(const TType &type)
4108{
4109 if (type.getBasicType() == EbtFloat)
4110 {
4111 switch (type.getPrecision())
4112 {
4113 case EbpHigh: return GL_HIGH_FLOAT;
4114 case EbpMedium: return GL_MEDIUM_FLOAT;
4115 case EbpLow: return GL_LOW_FLOAT;
4116 case EbpUndefined:
4117 // Should be defined as the default precision by the parser
4118 default: UNREACHABLE();
4119 }
4120 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004121 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004122 {
4123 switch (type.getPrecision())
4124 {
4125 case EbpHigh: return GL_HIGH_INT;
4126 case EbpMedium: return GL_MEDIUM_INT;
4127 case EbpLow: return GL_LOW_INT;
4128 case EbpUndefined:
4129 // Should be defined as the default precision by the parser
4130 default: UNREACHABLE();
4131 }
4132 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004133
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004134 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004135 return GL_NONE;
4136}
4137
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004138bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4139{
4140 switch(qualifier)
4141 {
4142 case EvqVaryingOut:
4143 case EvqInvariantVaryingOut:
4144 case EvqSmoothOut:
4145 case EvqFlatOut:
4146 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004147 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004148 return true;
Jamie Madill10567262014-04-17 16:40:00 -04004149
4150 default: break;
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004151 }
4152
4153 return false;
4154}
4155
4156bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4157{
4158 switch(qualifier)
4159 {
4160 case EvqVaryingIn:
4161 case EvqInvariantVaryingIn:
4162 case EvqSmoothIn:
4163 case EvqFlatIn:
4164 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004165 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004166 return true;
Jamie Madill10567262014-04-17 16:40:00 -04004167
4168 default: break;
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004169 }
4170
4171 return false;
4172}
4173
4174bool OutputHLSL::isVarying(TQualifier qualifier)
4175{
4176 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4177}
4178
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004179}