2 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
7 #include "compiler/translator/OutputHLSL.h"
9 #include "common/angleutils.h"
10 #include "common/utilities.h"
11 #include "common/blocklayout.h"
12 #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"
17 #include "compiler/translator/FlagStd140Structs.h"
18 #include "compiler/translator/NodeSearch.h"
19 #include "compiler/translator/RewriteElseBlocks.h"
20 #include "compiler/translator/UtilsHLSL.h"
21 #include "compiler/translator/util.h"
22 #include "compiler/translator/UniformHLSL.h"
23 #include "compiler/translator/StructureHLSL.h"
24 #include "compiler/translator/TranslatorHLSL.h"
33 TString OutputHLSL::TextureFunction::name() const
35 TString name = "gl_texture";
37 if (IsSampler2D(sampler))
41 else if (IsSampler3D(sampler))
45 else if (IsSamplerCube(sampler))
64 case BIAS: break; // Extra parameter makes the signature unique
65 case LOD: name += "Lod"; break;
66 case LOD0: name += "Lod0"; break;
67 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
68 case SIZE: name += "Size"; break;
69 case FETCH: name += "Fetch"; break;
70 case GRAD: name += "Grad"; break;
71 default: UNREACHABLE();
77 bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
79 if (sampler < rhs.sampler) return true;
80 if (sampler > rhs.sampler) return false;
82 if (coords < rhs.coords) return true;
83 if (coords > rhs.coords) return false;
85 if (!proj && rhs.proj) return true;
86 if (proj && !rhs.proj) return false;
88 if (!offset && rhs.offset) return true;
89 if (offset && !rhs.offset) return false;
91 if (method < rhs.method) return true;
92 if (method > rhs.method) return false;
97 OutputHLSL::OutputHLSL(TParseContext &context, TranslatorHLSL *parentTranslator)
98 : TIntermTraverser(true, true, true),
100 mOutputType(parentTranslator->getOutputType())
102 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
103 mInsideFunction = false;
105 mUsesFragColor = false;
106 mUsesFragData = false;
107 mUsesDepthRange = false;
108 mUsesFragCoord = false;
109 mUsesPointCoord = false;
110 mUsesFrontFacing = false;
111 mUsesPointSize = false;
112 mUsesFragDepth = false;
121 mUsesFaceforward1 = false;
122 mUsesFaceforward2 = false;
123 mUsesFaceforward3 = false;
124 mUsesFaceforward4 = false;
125 mUsesAtan2_1 = false;
126 mUsesAtan2_2 = false;
127 mUsesAtan2_3 = false;
128 mUsesAtan2_4 = false;
129 mUsesDiscardRewriting = false;
130 mUsesNestedBreak = false;
132 const ShBuiltInResources &resources = parentTranslator->getResources();
133 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
137 mContainsLoopDiscontinuity = false;
138 mOutputLod0Function = false;
139 mInsideDiscontinuousLoop = false;
140 mNestedLoopDepth = 0;
142 mExcessiveLoopIndex = NULL;
144 mStructureHLSL = new StructureHLSL;
145 mUniformHLSL = new UniformHLSL(mStructureHLSL, parentTranslator);
147 if (mOutputType == SH_HLSL9_OUTPUT)
149 if (mContext.shaderType == GL_FRAGMENT_SHADER)
151 // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
152 mUniformHLSL->reserveUniformRegisters(3);
156 // Reserve registers for dx_DepthRange and dx_ViewAdjust
157 mUniformHLSL->reserveUniformRegisters(2);
161 // Reserve registers for the default uniform block and driver constants
162 mUniformHLSL->reserveInterfaceBlockRegisters(2);
165 OutputHLSL::~OutputHLSL()
167 SafeDelete(mUnfoldShortCircuit);
168 SafeDelete(mStructureHLSL);
169 SafeDelete(mUniformHLSL);
172 void OutputHLSL::output()
174 mContainsLoopDiscontinuity = mContext.shaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
175 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
176 makeFlaggedStructMaps(flaggedStructs);
178 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
179 // use a vertex attribute as a condition, and some related computation in the else block.
180 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == GL_VERTEX_SHADER)
182 RewriteElseBlocks(mContext.treeRoot);
185 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
188 mContext.infoSink().obj << mHeader.c_str();
189 mContext.infoSink().obj << mBody.c_str();
192 void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
194 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
196 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
198 // This will mark the necessary block elements as referenced
199 flaggedNode->traverse(this);
200 TString structName(mBody.c_str());
203 mFlaggedStructOriginalNames[flaggedNode] = structName;
205 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
207 structName.erase(pos, 1);
210 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
214 TInfoSinkBase &OutputHLSL::getBodyStream()
219 const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
221 return mUniformHLSL->getInterfaceBlockRegisterMap();
224 const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
226 return mUniformHLSL->getUniformRegisterMap();
229 int OutputHLSL::vectorSize(const TType &type) const
231 int elementSize = type.isMatrix() ? type.getCols() : 1;
232 int arraySize = type.isArray() ? type.getArraySize() : 1;
234 return elementSize * arraySize;
237 TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
241 TString preIndentString;
242 TString fullIndentString;
244 for (int spaces = 0; spaces < (indent * 4); spaces++)
246 preIndentString += ' ';
249 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
251 fullIndentString += ' ';
254 init += preIndentString + "{\n";
256 const TFieldList &fields = structure.fields();
257 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
259 const TField &field = *fields[fieldIndex];
260 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
261 const TType &fieldType = *field.type();
263 if (fieldType.getStruct())
265 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
269 init += fullIndentString + fieldName + ",\n";
273 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
278 void OutputHLSL::header()
280 TInfoSinkBase &out = mHeader;
284 TString flaggedStructs;
286 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
288 TIntermTyped *structNode = flaggedStructIt->first;
289 const TString &mappedName = flaggedStructIt->second;
290 const TStructure &structure = *structNode->getType().getStruct();
291 const TString &originalName = mFlaggedStructOriginalNames[structNode];
293 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
294 flaggedStructs += structInitializerString(0, structure, originalName);
295 flaggedStructs += "\n";
298 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
300 const TType &type = varying->second->getType();
301 const TString &name = varying->second->getSymbol();
303 // Program linking depends on this exact format
304 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
305 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
308 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
310 const TType &type = attribute->second->getType();
311 const TString &name = attribute->second->getSymbol();
313 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
316 out << mStructureHLSL->structsHeader();
318 out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms);
319 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
321 if (mUsesDiscardRewriting)
323 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
326 if (mUsesNestedBreak)
328 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
331 if (mContext.shaderType == GL_FRAGMENT_SHADER)
333 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
334 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
336 out << "// Varyings\n";
340 if (mContext.getShaderVersion() >= 300)
342 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
344 const TString &variableName = outputVariableIt->first;
345 const TType &variableType = outputVariableIt->second->getType();
347 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
348 " = " + initializer(variableType) + ";\n";
353 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
355 out << "static float4 gl_Color[" << numColorValues << "] =\n"
357 for (unsigned int i = 0; i < numColorValues; i++)
359 out << " float4(0, 0, 0, 0)";
360 if (i + 1 != numColorValues)
372 out << "static float gl_Depth = 0.0;\n";
377 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
382 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
385 if (mUsesFrontFacing)
387 out << "static bool gl_FrontFacing = false;\n";
394 out << "struct gl_DepthRangeParameters\n"
403 if (mOutputType == SH_HLSL11_OUTPUT)
405 out << "cbuffer DriverConstants : register(b1)\n"
410 out << " float3 dx_DepthRange : packoffset(c0);\n";
415 out << " float4 dx_ViewCoords : packoffset(c1);\n";
418 if (mUsesFragCoord || mUsesFrontFacing)
420 out << " float3 dx_DepthFront : packoffset(c2);\n";
429 out << "uniform float3 dx_DepthRange : register(c0);";
434 out << "uniform float4 dx_ViewCoords : register(c1);\n";
437 if (mUsesFragCoord || mUsesFrontFacing)
439 out << "uniform float3 dx_DepthFront : register(c2);\n";
447 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
451 if (!flaggedStructs.empty())
453 out << "// Std140 Structures accessed by value\n";
455 out << flaggedStructs;
459 if (usingMRTExtension && mNumRenderTargets > 1)
461 out << "#define GL_USES_MRT\n";
466 out << "#define GL_USES_FRAG_COLOR\n";
471 out << "#define GL_USES_FRAG_DATA\n";
474 else // Vertex shader
476 out << "// Attributes\n";
479 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
483 out << "static float gl_PointSize = float(1);\n";
493 out << "struct gl_DepthRangeParameters\n"
502 if (mOutputType == SH_HLSL11_OUTPUT)
506 out << "cbuffer DriverConstants : register(b1)\n"
508 " float3 dx_DepthRange : packoffset(c0);\n"
517 out << "uniform float3 dx_DepthRange : register(c0);\n";
520 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
526 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
530 if (!flaggedStructs.empty())
532 out << "// Std140 Structures accessed by value\n";
534 out << flaggedStructs;
539 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
542 if (textureFunction->method == TextureFunction::SIZE)
544 switch(textureFunction->sampler)
546 case EbtSampler2D: out << "int2 "; break;
547 case EbtSampler3D: out << "int3 "; break;
548 case EbtSamplerCube: out << "int2 "; break;
549 case EbtSampler2DArray: out << "int3 "; break;
550 case EbtISampler2D: out << "int2 "; break;
551 case EbtISampler3D: out << "int3 "; break;
552 case EbtISamplerCube: out << "int2 "; break;
553 case EbtISampler2DArray: out << "int3 "; break;
554 case EbtUSampler2D: out << "int2 "; break;
555 case EbtUSampler3D: out << "int3 "; break;
556 case EbtUSamplerCube: out << "int2 "; break;
557 case EbtUSampler2DArray: out << "int3 "; break;
558 case EbtSampler2DShadow: out << "int2 "; break;
559 case EbtSamplerCubeShadow: out << "int2 "; break;
560 case EbtSampler2DArrayShadow: out << "int3 "; break;
561 default: UNREACHABLE();
564 else // Sampling function
566 switch(textureFunction->sampler)
568 case EbtSampler2D: out << "float4 "; break;
569 case EbtSampler3D: out << "float4 "; break;
570 case EbtSamplerCube: out << "float4 "; break;
571 case EbtSampler2DArray: out << "float4 "; break;
572 case EbtISampler2D: out << "int4 "; break;
573 case EbtISampler3D: out << "int4 "; break;
574 case EbtISamplerCube: out << "int4 "; break;
575 case EbtISampler2DArray: out << "int4 "; break;
576 case EbtUSampler2D: out << "uint4 "; break;
577 case EbtUSampler3D: out << "uint4 "; break;
578 case EbtUSamplerCube: out << "uint4 "; break;
579 case EbtUSampler2DArray: out << "uint4 "; break;
580 case EbtSampler2DShadow: out << "float "; break;
581 case EbtSamplerCubeShadow: out << "float "; break;
582 case EbtSampler2DArrayShadow: out << "float "; break;
583 default: UNREACHABLE();
588 out << textureFunction->name();
593 if (mOutputType == SH_HLSL9_OUTPUT)
595 switch(textureFunction->sampler)
597 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
598 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
599 default: UNREACHABLE();
602 switch(textureFunction->method)
604 case TextureFunction::IMPLICIT: break;
605 case TextureFunction::BIAS: hlslCoords = 4; break;
606 case TextureFunction::LOD: hlslCoords = 4; break;
607 case TextureFunction::LOD0: hlslCoords = 4; break;
608 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
609 default: UNREACHABLE();
612 else if (mOutputType == SH_HLSL11_OUTPUT)
614 switch(textureFunction->sampler)
616 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
617 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
618 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
619 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
620 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
621 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
622 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
623 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
624 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
625 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
626 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
627 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
628 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
629 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
630 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
631 default: UNREACHABLE();
636 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
638 switch(textureFunction->coords)
640 case 2: out << ", int2 t"; break;
641 case 3: out << ", int3 t"; break;
642 default: UNREACHABLE();
645 else // Floating-point coordinates (except textureSize)
647 switch(textureFunction->coords)
649 case 1: out << ", int lod"; break; // textureSize()
650 case 2: out << ", float2 t"; break;
651 case 3: out << ", float3 t"; break;
652 case 4: out << ", float4 t"; break;
653 default: UNREACHABLE();
657 if (textureFunction->method == TextureFunction::GRAD)
659 switch(textureFunction->sampler)
664 case EbtSampler2DArray:
665 case EbtISampler2DArray:
666 case EbtUSampler2DArray:
667 case EbtSampler2DShadow:
668 case EbtSampler2DArrayShadow:
669 out << ", float2 ddx, float2 ddy";
675 case EbtISamplerCube:
676 case EbtUSamplerCube:
677 case EbtSamplerCubeShadow:
678 out << ", float3 ddx, float3 ddy";
680 default: UNREACHABLE();
684 switch(textureFunction->method)
686 case TextureFunction::IMPLICIT: break;
687 case TextureFunction::BIAS: break; // Comes after the offset parameter
688 case TextureFunction::LOD: out << ", float lod"; break;
689 case TextureFunction::LOD0: break;
690 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
691 case TextureFunction::SIZE: break;
692 case TextureFunction::FETCH: out << ", int mip"; break;
693 case TextureFunction::GRAD: break;
694 default: UNREACHABLE();
697 if (textureFunction->offset)
699 switch(textureFunction->sampler)
701 case EbtSampler2D: out << ", int2 offset"; break;
702 case EbtSampler3D: out << ", int3 offset"; break;
703 case EbtSampler2DArray: out << ", int2 offset"; break;
704 case EbtISampler2D: out << ", int2 offset"; break;
705 case EbtISampler3D: out << ", int3 offset"; break;
706 case EbtISampler2DArray: out << ", int2 offset"; break;
707 case EbtUSampler2D: out << ", int2 offset"; break;
708 case EbtUSampler3D: out << ", int3 offset"; break;
709 case EbtUSampler2DArray: out << ", int2 offset"; break;
710 case EbtSampler2DShadow: out << ", int2 offset"; break;
711 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
712 default: UNREACHABLE();
716 if (textureFunction->method == TextureFunction::BIAS ||
717 textureFunction->method == TextureFunction::LOD0BIAS)
719 out << ", float bias";
725 if (textureFunction->method == TextureFunction::SIZE)
727 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
729 if (IsSamplerArray(textureFunction->sampler))
731 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
732 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
736 out << " uint width; uint height; uint numberOfLevels;\n"
737 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
740 else if (IsSampler3D(textureFunction->sampler))
742 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
743 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
747 switch(textureFunction->sampler)
749 case EbtSampler2D: out << " return int2(width, height);"; break;
750 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
751 case EbtSamplerCube: out << " return int2(width, height);"; break;
752 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
753 case EbtISampler2D: out << " return int2(width, height);"; break;
754 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
755 case EbtISamplerCube: out << " return int2(width, height);"; break;
756 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
757 case EbtUSampler2D: out << " return int2(width, height);"; break;
758 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
759 case EbtUSamplerCube: out << " return int2(width, height);"; break;
760 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
761 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
762 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
763 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
764 default: UNREACHABLE();
769 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
771 out << " float width; float height; float layers; float levels;\n";
773 out << " uint mip = 0;\n";
775 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
777 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
778 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
779 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
780 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
782 // FACE_POSITIVE_X = 000b
783 // FACE_NEGATIVE_X = 001b
784 // FACE_POSITIVE_Y = 010b
785 // FACE_NEGATIVE_Y = 011b
786 // FACE_POSITIVE_Z = 100b
787 // FACE_NEGATIVE_Z = 101b
788 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
790 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
791 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
792 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
794 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
795 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
797 else if (IsIntegerSampler(textureFunction->sampler) &&
798 textureFunction->method != TextureFunction::FETCH)
800 if (IsSampler2D(textureFunction->sampler))
802 if (IsSamplerArray(textureFunction->sampler))
804 out << " float width; float height; float layers; float levels;\n";
806 if (textureFunction->method == TextureFunction::LOD0)
808 out << " uint mip = 0;\n";
810 else if (textureFunction->method == TextureFunction::LOD0BIAS)
812 out << " uint mip = bias;\n";
816 if (textureFunction->method == TextureFunction::IMPLICIT ||
817 textureFunction->method == TextureFunction::BIAS)
819 out << " x.GetDimensions(0, width, height, layers, levels);\n"
820 " float2 tSized = float2(t.x * width, t.y * height);\n"
821 " float dx = length(ddx(tSized));\n"
822 " float dy = length(ddy(tSized));\n"
823 " float lod = log2(max(dx, dy));\n";
825 if (textureFunction->method == TextureFunction::BIAS)
827 out << " lod += bias;\n";
830 else if (textureFunction->method == TextureFunction::GRAD)
832 out << " x.GetDimensions(0, width, height, layers, levels);\n"
833 " float lod = log2(max(length(ddx), length(ddy)));\n";
836 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
839 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
843 out << " float width; float height; float levels;\n";
845 if (textureFunction->method == TextureFunction::LOD0)
847 out << " uint mip = 0;\n";
849 else if (textureFunction->method == TextureFunction::LOD0BIAS)
851 out << " uint mip = bias;\n";
855 if (textureFunction->method == TextureFunction::IMPLICIT ||
856 textureFunction->method == TextureFunction::BIAS)
858 out << " x.GetDimensions(0, width, height, levels);\n"
859 " float2 tSized = float2(t.x * width, t.y * height);\n"
860 " float dx = length(ddx(tSized));\n"
861 " float dy = length(ddy(tSized));\n"
862 " float lod = log2(max(dx, dy));\n";
864 if (textureFunction->method == TextureFunction::BIAS)
866 out << " lod += bias;\n";
869 else if (textureFunction->method == TextureFunction::LOD)
871 out << " x.GetDimensions(0, width, height, levels);\n";
873 else if (textureFunction->method == TextureFunction::GRAD)
875 out << " x.GetDimensions(0, width, height, levels);\n"
876 " float lod = log2(max(length(ddx), length(ddy)));\n";
879 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
882 out << " x.GetDimensions(mip, width, height, levels);\n";
885 else if (IsSampler3D(textureFunction->sampler))
887 out << " float width; float height; float depth; float levels;\n";
889 if (textureFunction->method == TextureFunction::LOD0)
891 out << " uint mip = 0;\n";
893 else if (textureFunction->method == TextureFunction::LOD0BIAS)
895 out << " uint mip = bias;\n";
899 if (textureFunction->method == TextureFunction::IMPLICIT ||
900 textureFunction->method == TextureFunction::BIAS)
902 out << " x.GetDimensions(0, width, height, depth, levels);\n"
903 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
904 " float dx = length(ddx(tSized));\n"
905 " float dy = length(ddy(tSized));\n"
906 " float lod = log2(max(dx, dy));\n";
908 if (textureFunction->method == TextureFunction::BIAS)
910 out << " lod += bias;\n";
913 else if (textureFunction->method == TextureFunction::GRAD)
915 out << " x.GetDimensions(0, width, height, depth, levels);\n"
916 " float lod = log2(max(length(ddx), length(ddy)));\n";
919 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
922 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
930 if (mOutputType == SH_HLSL9_OUTPUT)
932 switch(textureFunction->sampler)
934 case EbtSampler2D: out << "tex2D"; break;
935 case EbtSamplerCube: out << "texCUBE"; break;
936 default: UNREACHABLE();
939 switch(textureFunction->method)
941 case TextureFunction::IMPLICIT: out << "(s, "; break;
942 case TextureFunction::BIAS: out << "bias(s, "; break;
943 case TextureFunction::LOD: out << "lod(s, "; break;
944 case TextureFunction::LOD0: out << "lod(s, "; break;
945 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
946 default: UNREACHABLE();
949 else if (mOutputType == SH_HLSL11_OUTPUT)
951 if (textureFunction->method == TextureFunction::GRAD)
953 if (IsIntegerSampler(textureFunction->sampler))
957 else if (IsShadowSampler(textureFunction->sampler))
959 out << "x.SampleCmpLevelZero(s, ";
963 out << "x.SampleGrad(s, ";
966 else if (IsIntegerSampler(textureFunction->sampler) ||
967 textureFunction->method == TextureFunction::FETCH)
971 else if (IsShadowSampler(textureFunction->sampler))
973 out << "x.SampleCmp(s, ";
977 switch(textureFunction->method)
979 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
980 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
981 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
982 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
983 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
984 default: UNREACHABLE();
990 // Integer sampling requires integer addresses
991 TString addressx = "";
992 TString addressy = "";
993 TString addressz = "";
996 if (IsIntegerSampler(textureFunction->sampler) ||
997 textureFunction->method == TextureFunction::FETCH)
1001 case 2: out << "int3("; break;
1002 case 3: out << "int4("; break;
1003 default: UNREACHABLE();
1006 // Convert from normalized floating-point to integer
1007 if (textureFunction->method != TextureFunction::FETCH)
1009 addressx = "int(floor(width * frac((";
1010 addressy = "int(floor(height * frac((";
1012 if (IsSamplerArray(textureFunction->sampler))
1014 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1016 else if (IsSamplerCube(textureFunction->sampler))
1022 addressz = "int(floor(depth * frac((";
1032 case 2: out << "float2("; break;
1033 case 3: out << "float3("; break;
1034 case 4: out << "float4("; break;
1035 default: UNREACHABLE();
1039 TString proj = ""; // Only used for projected textures
1041 if (textureFunction->proj)
1043 switch(textureFunction->coords)
1045 case 3: proj = " / t.z"; break;
1046 case 4: proj = " / t.w"; break;
1047 default: UNREACHABLE();
1051 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
1053 if (mOutputType == SH_HLSL9_OUTPUT)
1055 if (hlslCoords >= 3)
1057 if (textureFunction->coords < 3)
1063 out << ", t.z" + proj;
1067 if (hlslCoords == 4)
1069 switch(textureFunction->method)
1071 case TextureFunction::BIAS: out << ", bias"; break;
1072 case TextureFunction::LOD: out << ", lod"; break;
1073 case TextureFunction::LOD0: out << ", 0"; break;
1074 case TextureFunction::LOD0BIAS: out << ", bias"; break;
1075 default: UNREACHABLE();
1081 else if (mOutputType == SH_HLSL11_OUTPUT)
1083 if (hlslCoords >= 3)
1085 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1091 out << ", " + addressz + ("t.z" + proj) + close;
1095 if (textureFunction->method == TextureFunction::GRAD)
1097 if (IsIntegerSampler(textureFunction->sampler))
1101 else if (IsShadowSampler(textureFunction->sampler))
1104 switch(textureFunction->coords)
1106 case 3: out << "), t.z"; break;
1107 case 4: out << "), t.w"; break;
1108 default: UNREACHABLE();
1113 out << "), ddx, ddy";
1116 else if (IsIntegerSampler(textureFunction->sampler) ||
1117 textureFunction->method == TextureFunction::FETCH)
1121 else if (IsShadowSampler(textureFunction->sampler))
1124 switch(textureFunction->coords)
1126 case 3: out << "), t.z"; break;
1127 case 4: out << "), t.w"; break;
1128 default: UNREACHABLE();
1133 switch(textureFunction->method)
1135 case TextureFunction::IMPLICIT: out << ")"; break;
1136 case TextureFunction::BIAS: out << "), bias"; break;
1137 case TextureFunction::LOD: out << "), lod"; break;
1138 case TextureFunction::LOD0: out << "), 0"; break;
1139 case TextureFunction::LOD0BIAS: out << "), bias"; break;
1140 default: UNREACHABLE();
1144 if (textureFunction->offset)
1161 out << "#define GL_USES_FRAG_COORD\n";
1164 if (mUsesPointCoord)
1166 out << "#define GL_USES_POINT_COORD\n";
1169 if (mUsesFrontFacing)
1171 out << "#define GL_USES_FRONT_FACING\n";
1176 out << "#define GL_USES_POINT_SIZE\n";
1181 out << "#define GL_USES_FRAG_DEPTH\n";
1184 if (mUsesDepthRange)
1186 out << "#define GL_USES_DEPTH_RANGE\n";
1191 out << "bool xor(bool p, bool q)\n"
1193 " return (p || q) && !(p && q);\n"
1200 out << "float mod(float x, float y)\n"
1202 " return x - y * floor(x / y);\n"
1209 out << "float2 mod(float2 x, float2 y)\n"
1211 " return x - y * floor(x / y);\n"
1218 out << "float2 mod(float2 x, float y)\n"
1220 " return x - y * floor(x / y);\n"
1227 out << "float3 mod(float3 x, float3 y)\n"
1229 " return x - y * floor(x / y);\n"
1236 out << "float3 mod(float3 x, float y)\n"
1238 " return x - y * floor(x / y);\n"
1245 out << "float4 mod(float4 x, float4 y)\n"
1247 " return x - y * floor(x / y);\n"
1254 out << "float4 mod(float4 x, float y)\n"
1256 " return x - y * floor(x / y);\n"
1261 if (mUsesFaceforward1)
1263 out << "float faceforward(float N, float I, float Nref)\n"
1265 " if(dot(Nref, I) >= 0)\n"
1277 if (mUsesFaceforward2)
1279 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1281 " if(dot(Nref, I) >= 0)\n"
1293 if (mUsesFaceforward3)
1295 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1297 " if(dot(Nref, I) >= 0)\n"
1309 if (mUsesFaceforward4)
1311 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1313 " if(dot(Nref, I) >= 0)\n"
1327 out << "float atanyx(float y, float x)\n"
1329 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1330 " return atan2(y, x);\n"
1336 out << "float2 atanyx(float2 y, float2 x)\n"
1338 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1339 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1340 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1346 out << "float3 atanyx(float3 y, float3 x)\n"
1348 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1349 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1350 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1351 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1357 out << "float4 atanyx(float4 y, float4 x)\n"
1359 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1360 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1361 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1362 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1363 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1368 void OutputHLSL::visitSymbol(TIntermSymbol *node)
1370 TInfoSinkBase &out = mBody;
1372 // Handle accessing std140 structs by value
1373 if (mFlaggedStructMappedNames.count(node) > 0)
1375 out << mFlaggedStructMappedNames[node];
1379 TString name = node->getSymbol();
1381 if (name == "gl_DepthRange")
1383 mUsesDepthRange = true;
1388 TQualifier qualifier = node->getQualifier();
1390 if (qualifier == EvqUniform)
1392 const TType& nodeType = node->getType();
1393 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1397 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
1401 mReferencedUniforms[name] = node;
1404 out << DecorateUniform(name, nodeType);
1406 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
1408 mReferencedAttributes[name] = node;
1409 out << Decorate(name);
1411 else if (IsVarying(qualifier))
1413 mReferencedVaryings[name] = node;
1414 out << Decorate(name);
1416 else if (qualifier == EvqFragmentOut)
1418 mReferencedOutputVariables[name] = node;
1419 out << "out_" << name;
1421 else if (qualifier == EvqFragColor)
1423 out << "gl_Color[0]";
1424 mUsesFragColor = true;
1426 else if (qualifier == EvqFragData)
1429 mUsesFragData = true;
1431 else if (qualifier == EvqFragCoord)
1433 mUsesFragCoord = true;
1436 else if (qualifier == EvqPointCoord)
1438 mUsesPointCoord = true;
1441 else if (qualifier == EvqFrontFacing)
1443 mUsesFrontFacing = true;
1446 else if (qualifier == EvqPointSize)
1448 mUsesPointSize = true;
1451 else if (name == "gl_FragDepthEXT")
1453 mUsesFragDepth = true;
1456 else if (qualifier == EvqInternal)
1462 out << Decorate(name);
1467 void OutputHLSL::visitRaw(TIntermRaw *node)
1469 mBody << node->getRawText();
1472 bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1474 TInfoSinkBase &out = mBody;
1476 // Handle accessing std140 structs by value
1477 if (mFlaggedStructMappedNames.count(node) > 0)
1479 out << mFlaggedStructMappedNames[node];
1483 switch (node->getOp())
1485 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
1487 if (visit == PreVisit)
1489 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1490 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1491 // new variable is created before the assignment is evaluated), so we need to convert
1492 // this to "float t = x, x = t;".
1494 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1495 TIntermTyped *expression = node->getRight();
1497 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1498 expression->traverse(&searchSymbol);
1499 bool sameSymbol = searchSymbol.foundMatch();
1503 // Type already printed
1504 out << "t" + str(mUniqueIndex) + " = ";
1505 expression->traverse(this);
1507 symbolNode->traverse(this);
1508 out << " = t" + str(mUniqueIndex);
1514 else if (visit == InVisit)
1519 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1520 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1521 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1522 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1523 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1524 case EOpVectorTimesMatrixAssign:
1525 if (visit == PreVisit)
1529 else if (visit == InVisit)
1532 node->getLeft()->traverse(this);
1533 out << ", transpose(";
1540 case EOpMatrixTimesMatrixAssign:
1541 if (visit == PreVisit)
1545 else if (visit == InVisit)
1548 node->getLeft()->traverse(this);
1556 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
1557 case EOpIndexDirect:
1559 const TType& leftType = node->getLeft()->getType();
1560 if (leftType.isInterfaceBlock())
1562 if (visit == PreVisit)
1564 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1565 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1566 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1567 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1573 outputTriplet(visit, "", "[", "]");
1577 case EOpIndexIndirect:
1578 // We do not currently support indirect references to interface blocks
1579 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1580 outputTriplet(visit, "", "[", "]");
1582 case EOpIndexDirectStruct:
1583 if (visit == InVisit)
1585 const TStructure* structure = node->getLeft()->getType().getStruct();
1586 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1587 const TField* field = structure->fields()[index->getIConst(0)];
1588 out << "." + DecorateField(field->name(), *structure);
1593 case EOpIndexDirectInterfaceBlock:
1594 if (visit == InVisit)
1596 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1597 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1598 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1599 out << "." + Decorate(field->name());
1604 case EOpVectorSwizzle:
1605 if (visit == InVisit)
1609 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1613 TIntermSequence *sequence = swizzle->getSequence();
1615 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
1617 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1621 int i = element->getIConst(0);
1625 case 0: out << "x"; break;
1626 case 1: out << "y"; break;
1627 case 2: out << "z"; break;
1628 case 3: out << "w"; break;
1629 default: UNREACHABLE();
1637 return false; // Fully processed
1640 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1641 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1642 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1643 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
1646 if (node->getLeft()->isScalar())
1648 if (node->getOp() == EOpEqual)
1650 outputTriplet(visit, "(", " == ", ")");
1654 outputTriplet(visit, "(", " != ", ")");
1657 else if (node->getLeft()->getBasicType() == EbtStruct)
1659 if (node->getOp() == EOpEqual)
1668 const TStructure &structure = *node->getLeft()->getType().getStruct();
1669 const TFieldList &fields = structure.fields();
1671 for (size_t i = 0; i < fields.size(); i++)
1673 const TField *field = fields[i];
1675 node->getLeft()->traverse(this);
1676 out << "." + DecorateField(field->name(), structure) + " == ";
1677 node->getRight()->traverse(this);
1678 out << "." + DecorateField(field->name(), structure);
1680 if (i < fields.size() - 1)
1692 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
1694 if (node->getOp() == EOpEqual)
1696 outputTriplet(visit, "all(", " == ", ")");
1700 outputTriplet(visit, "!all(", " == ", ")");
1704 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1705 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1706 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1707 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1708 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
1709 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
1710 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1711 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
1712 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
1714 if (node->getRight()->hasSideEffects())
1716 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1721 outputTriplet(visit, "(", " || ", ")");
1726 outputTriplet(visit, "xor(", ", ", ")");
1729 if (node->getRight()->hasSideEffects())
1731 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1736 outputTriplet(visit, "(", " && ", ")");
1739 default: UNREACHABLE();
1745 bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1747 switch (node->getOp())
1749 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1750 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1751 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1752 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1753 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1754 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1755 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
1756 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1757 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1758 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1759 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1760 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1761 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1762 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1763 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1764 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1765 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1766 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1767 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1768 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1769 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1770 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1771 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1772 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1773 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1774 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1775 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1776 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
1778 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1780 outputTriplet(visit, "(", "", ", 0.0)");
1784 outputTriplet(visit, "ddx(", "", ")");
1788 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1790 outputTriplet(visit, "(", "", ", 0.0)");
1794 outputTriplet(visit, "ddy(", "", ")");
1798 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1800 outputTriplet(visit, "(", "", ", 0.0)");
1804 outputTriplet(visit, "fwidth(", "", ")");
1807 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1808 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
1809 default: UNREACHABLE();
1815 bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1817 TInfoSinkBase &out = mBody;
1819 switch (node->getOp())
1823 if (mInsideFunction)
1825 outputLineDirective(node->getLine().first_line);
1829 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
1831 outputLineDirective((*sit)->getLine().first_line);
1833 traverseStatements(*sit);
1838 if (mInsideFunction)
1840 outputLineDirective(node->getLine().last_line);
1846 case EOpDeclaration:
1847 if (visit == PreVisit)
1849 TIntermSequence *sequence = node->getSequence();
1850 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1852 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
1854 TStructure *structure = variable->getType().getStruct();
1858 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
1861 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1863 if (!mInsideFunction)
1868 out << TypeString(variable->getType()) + " ";
1870 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
1872 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1876 symbol->traverse(this);
1877 out << ArrayString(symbol->getType());
1878 out << " = " + initializer(symbol->getType());
1882 (*sit)->traverse(this);
1885 if (*sit != sequence->back())
1891 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1893 // Already added to constructor map
1897 else if (variable && IsVaryingOut(variable->getQualifier()))
1899 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
1901 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1905 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1906 mReferencedVaryings[symbol->getSymbol()] = symbol;
1910 (*sit)->traverse(this);
1917 else if (visit == InVisit)
1922 case EOpInvariantDeclaration:
1923 // Do not do any translation
1926 if (visit == PreVisit)
1928 out << TypeString(node->getType()) << " " << Decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
1930 TIntermSequence *arguments = node->getSequence();
1932 for (unsigned int i = 0; i < arguments->size(); i++)
1934 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1938 out << argumentString(symbol);
1940 if (i < arguments->size() - 1)
1950 // Also prototype the Lod0 variant if needed
1951 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1953 mOutputLod0Function = true;
1954 node->traverse(this);
1955 mOutputLod0Function = false;
1961 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
1964 TString name = TFunction::unmangleName(node->getName());
1966 out << TypeString(node->getType()) << " ";
1974 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
1977 TIntermSequence *sequence = node->getSequence();
1978 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
1980 for (unsigned int i = 0; i < arguments->size(); i++)
1982 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1986 TStructure *structure = symbol->getType().getStruct();
1990 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
1993 out << argumentString(symbol);
1995 if (i < arguments->size() - 1)
2006 if (sequence->size() > 1)
2008 mInsideFunction = true;
2009 (*sequence)[1]->traverse(this);
2010 mInsideFunction = false;
2015 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2019 mOutputLod0Function = true;
2020 node->traverse(this);
2021 mOutputLod0Function = false;
2028 case EOpFunctionCall:
2030 TString name = TFunction::unmangleName(node->getName());
2031 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
2032 TIntermSequence *arguments = node->getSequence();
2034 if (node->isUserDefined())
2036 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
2040 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
2042 TextureFunction textureFunction;
2043 textureFunction.sampler = samplerType;
2044 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
2045 textureFunction.method = TextureFunction::IMPLICIT;
2046 textureFunction.proj = false;
2047 textureFunction.offset = false;
2049 if (name == "texture2D" || name == "textureCube" || name == "texture")
2051 textureFunction.method = TextureFunction::IMPLICIT;
2053 else if (name == "texture2DProj" || name == "textureProj")
2055 textureFunction.method = TextureFunction::IMPLICIT;
2056 textureFunction.proj = true;
2058 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2059 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
2061 textureFunction.method = TextureFunction::LOD;
2063 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
2065 textureFunction.method = TextureFunction::LOD;
2066 textureFunction.proj = true;
2068 else if (name == "textureSize")
2070 textureFunction.method = TextureFunction::SIZE;
2072 else if (name == "textureOffset")
2074 textureFunction.method = TextureFunction::IMPLICIT;
2075 textureFunction.offset = true;
2077 else if (name == "textureProjOffset")
2079 textureFunction.method = TextureFunction::IMPLICIT;
2080 textureFunction.offset = true;
2081 textureFunction.proj = true;
2083 else if (name == "textureLodOffset")
2085 textureFunction.method = TextureFunction::LOD;
2086 textureFunction.offset = true;
2088 else if (name == "textureProjLodOffset")
2090 textureFunction.method = TextureFunction::LOD;
2091 textureFunction.proj = true;
2092 textureFunction.offset = true;
2094 else if (name == "texelFetch")
2096 textureFunction.method = TextureFunction::FETCH;
2098 else if (name == "texelFetchOffset")
2100 textureFunction.method = TextureFunction::FETCH;
2101 textureFunction.offset = true;
2103 else if (name == "textureGrad" || name == "texture2DGradEXT")
2105 textureFunction.method = TextureFunction::GRAD;
2107 else if (name == "textureGradOffset")
2109 textureFunction.method = TextureFunction::GRAD;
2110 textureFunction.offset = true;
2112 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
2114 textureFunction.method = TextureFunction::GRAD;
2115 textureFunction.proj = true;
2117 else if (name == "textureProjGradOffset")
2119 textureFunction.method = TextureFunction::GRAD;
2120 textureFunction.proj = true;
2121 textureFunction.offset = true;
2125 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
2127 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2129 if (textureFunction.offset)
2131 mandatoryArgumentCount++;
2134 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
2136 if (lod0 || mContext.shaderType == GL_VERTEX_SHADER)
2140 textureFunction.method = TextureFunction::LOD0BIAS;
2144 textureFunction.method = TextureFunction::LOD0;
2149 textureFunction.method = TextureFunction::BIAS;
2153 mUsesTexture.insert(textureFunction);
2155 out << textureFunction.name();
2158 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
2160 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2163 (*arg)->traverse(this);
2164 out << ", sampler_";
2167 (*arg)->traverse(this);
2169 if (arg < arguments->end() - 1)
2180 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
2181 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break;
2182 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break;
2183 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break;
2184 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break;
2185 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break;
2186 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break;
2187 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break;
2188 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break;
2189 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break;
2190 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break;
2191 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break;
2192 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break;
2193 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break;
2194 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break;
2195 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break;
2196 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break;
2197 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break;
2198 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break;
2199 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break;
2200 case EOpConstructStruct:
2202 const TString &structName = StructNameString(*node->getType().getStruct());
2203 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
2204 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2207 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2208 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2209 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2210 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2211 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2212 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
2215 // We need to look at the number of components in both arguments
2216 const int modValue = (*node->getSequence())[0]->getAsTyped()->getNominalSize() * 10 +
2217 (*node->getSequence())[1]->getAsTyped()->getNominalSize();
2220 case 11: mUsesMod1 = true; break;
2221 case 22: mUsesMod2v = true; break;
2222 case 21: mUsesMod2f = true; break;
2223 case 33: mUsesMod3v = true; break;
2224 case 31: mUsesMod3f = true; break;
2225 case 44: mUsesMod4v = true; break;
2226 case 41: mUsesMod4f = true; break;
2227 default: UNREACHABLE();
2230 outputTriplet(visit, "mod(", ", ", ")");
2233 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
2235 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2236 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize())
2238 case 1: mUsesAtan2_1 = true; break;
2239 case 2: mUsesAtan2_2 = true; break;
2240 case 3: mUsesAtan2_3 = true; break;
2241 case 4: mUsesAtan2_4 = true; break;
2242 default: UNREACHABLE();
2244 outputTriplet(visit, "atanyx(", ", ", ")");
2246 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2247 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2248 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2249 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2250 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2251 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2252 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2253 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2254 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
2255 case EOpFaceForward:
2257 switch ((*node->getSequence())[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
2259 case 1: mUsesFaceforward1 = true; break;
2260 case 2: mUsesFaceforward2 = true; break;
2261 case 3: mUsesFaceforward3 = true; break;
2262 case 4: mUsesFaceforward4 = true; break;
2263 default: UNREACHABLE();
2266 outputTriplet(visit, "faceforward(", ", ", ")");
2269 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2270 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2271 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
2272 default: UNREACHABLE();
2278 bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2280 TInfoSinkBase &out = mBody;
2282 if (node->usesTernaryOperator())
2284 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2286 else // if/else statement
2288 mUnfoldShortCircuit->traverse(node->getCondition());
2292 node->getCondition()->traverse(this);
2296 outputLineDirective(node->getLine().first_line);
2299 bool discard = false;
2301 if (node->getTrueBlock())
2303 traverseStatements(node->getTrueBlock());
2305 // Detect true discard
2306 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2309 outputLineDirective(node->getLine().first_line);
2312 if (node->getFalseBlock())
2316 outputLineDirective(node->getFalseBlock()->getLine().first_line);
2319 outputLineDirective(node->getFalseBlock()->getLine().first_line);
2320 traverseStatements(node->getFalseBlock());
2322 outputLineDirective(node->getFalseBlock()->getLine().first_line);
2325 // Detect false discard
2326 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2329 // ANGLE issue 486: Detect problematic conditional discard
2330 if (discard && FindSideEffectRewriting::search(node))
2332 mUsesDiscardRewriting = true;
2339 void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2341 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
2344 bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2348 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2350 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
2352 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2355 if (mOutputType == SH_HLSL9_OUTPUT)
2357 if (handleExcessiveLoop(node))
2359 mInsideDiscontinuousLoop = wasDiscontinuous;
2366 TInfoSinkBase &out = mBody;
2368 if (node->getType() == ELoopDoWhile)
2372 outputLineDirective(node->getLine().first_line);
2379 if (node->getInit())
2381 node->getInit()->traverse(this);
2386 if (node->getCondition())
2388 node->getCondition()->traverse(this);
2393 if (node->getExpression())
2395 node->getExpression()->traverse(this);
2400 outputLineDirective(node->getLine().first_line);
2404 if (node->getBody())
2406 traverseStatements(node->getBody());
2409 outputLineDirective(node->getLine().first_line);
2412 if (node->getType() == ELoopDoWhile)
2414 outputLineDirective(node->getCondition()->getLine().first_line);
2417 node->getCondition()->traverse(this);
2424 mInsideDiscontinuousLoop = wasDiscontinuous;
2430 bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2432 TInfoSinkBase &out = mBody;
2434 switch (node->getFlowOp())
2437 outputTriplet(visit, "discard;\n", "", "");
2440 if (visit == PreVisit)
2442 if (mNestedLoopDepth > 1)
2444 mUsesNestedBreak = true;
2447 if (mExcessiveLoopIndex)
2450 mExcessiveLoopIndex->traverse(this);
2451 out << " = true; break;}\n";
2459 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
2461 if (visit == PreVisit)
2463 if (node->getExpression())
2472 else if (visit == PostVisit)
2474 if (node->getExpression())
2480 default: UNREACHABLE();
2486 void OutputHLSL::traverseStatements(TIntermNode *node)
2488 if (isSingleStatement(node))
2490 mUnfoldShortCircuit->traverse(node);
2493 node->traverse(this);
2496 bool OutputHLSL::isSingleStatement(TIntermNode *node)
2498 TIntermAggregate *aggregate = node->getAsAggregate();
2502 if (aggregate->getOp() == EOpSequence)
2508 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
2510 if (!isSingleStatement(*sit))
2523 // Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2524 // (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
2525 bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2527 const int MAX_LOOP_ITERATIONS = 254;
2528 TInfoSinkBase &out = mBody;
2530 // Parse loops of the form:
2531 // for(int index = initial; index [comparator] limit; index += increment)
2532 TIntermSymbol *index = NULL;
2533 TOperator comparator = EOpNull;
2538 // Parse index name and intial value
2539 if (node->getInit())
2541 TIntermAggregate *init = node->getInit()->getAsAggregate();
2545 TIntermSequence *sequence = init->getSequence();
2546 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
2548 if (variable && variable->getQualifier() == EvqTemporary)
2550 TIntermBinary *assign = variable->getAsBinaryNode();
2552 if (assign->getOp() == EOpInitialize)
2554 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2555 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2557 if (symbol && constant)
2559 if (constant->getBasicType() == EbtInt && constant->isScalar())
2562 initial = constant->getIConst(0);
2570 // Parse comparator and limit value
2571 if (index != NULL && node->getCondition())
2573 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
2575 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2577 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2581 if (constant->getBasicType() == EbtInt && constant->isScalar())
2583 comparator = test->getOp();
2584 limit = constant->getIConst(0);
2591 if (index != NULL && comparator != EOpNull && node->getExpression())
2593 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2594 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
2598 TOperator op = binaryTerminal->getOp();
2599 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2603 if (constant->getBasicType() == EbtInt && constant->isScalar())
2605 int value = constant->getIConst(0);
2609 case EOpAddAssign: increment = value; break;
2610 case EOpSubAssign: increment = -value; break;
2611 default: UNIMPLEMENTED();
2616 else if (unaryTerminal)
2618 TOperator op = unaryTerminal->getOp();
2622 case EOpPostIncrement: increment = 1; break;
2623 case EOpPostDecrement: increment = -1; break;
2624 case EOpPreIncrement: increment = 1; break;
2625 case EOpPreDecrement: increment = -1; break;
2626 default: UNIMPLEMENTED();
2631 if (index != NULL && comparator != EOpNull && increment != 0)
2633 if (comparator == EOpLessThanEqual)
2635 comparator = EOpLessThan;
2639 if (comparator == EOpLessThan)
2641 int iterations = (limit - initial) / increment;
2643 if (iterations <= MAX_LOOP_ITERATIONS)
2645 return false; // Not an excessive loop
2648 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2649 mExcessiveLoopIndex = index;
2652 index->traverse(this);
2655 index->traverse(this);
2656 out << " = false;\n";
2658 bool firstLoopFragment = true;
2660 while (iterations > 0)
2662 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
2664 if (!firstLoopFragment)
2666 out << "if (!Break";
2667 index->traverse(this);
2671 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2673 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2676 // for(int index = initial; index < clampedLimit; index += increment)
2679 index->traverse(this);
2684 index->traverse(this);
2686 out << clampedLimit;
2689 index->traverse(this);
2694 outputLineDirective(node->getLine().first_line);
2697 if (node->getBody())
2699 node->getBody()->traverse(this);
2702 outputLineDirective(node->getLine().first_line);
2705 if (!firstLoopFragment)
2710 firstLoopFragment = false;
2712 initial += MAX_LOOP_ITERATIONS * increment;
2713 iterations -= MAX_LOOP_ITERATIONS;
2718 mExcessiveLoopIndex = restoreIndex;
2722 else UNIMPLEMENTED();
2725 return false; // Not handled as an excessive loop
2728 void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
2730 TInfoSinkBase &out = mBody;
2732 if (visit == PreVisit)
2736 else if (visit == InVisit)
2740 else if (visit == PostVisit)
2746 void OutputHLSL::outputLineDirective(int line)
2748 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2751 mBody << "#line " << line;
2753 if (mContext.sourcePath)
2755 mBody << " \"" << mContext.sourcePath << "\"";
2762 TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2764 TQualifier qualifier = symbol->getQualifier();
2765 const TType &type = symbol->getType();
2766 TString name = symbol->getSymbol();
2768 if (name.empty()) // HLSL demands named arguments, also for prototypes
2770 name = "x" + str(mUniqueIndex++);
2774 name = Decorate(name);
2777 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2779 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
2780 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
2783 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
2786 TString OutputHLSL::initializer(const TType &type)
2790 size_t size = type.getObjectSize();
2791 for (size_t component = 0; component < size; component++)
2795 if (component + 1 < size)
2801 return "{" + string + "}";
2804 void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
2806 TInfoSinkBase &out = mBody;
2808 if (visit == PreVisit)
2810 mStructureHLSL->addConstructor(type, name, parameters);
2814 else if (visit == InVisit)
2818 else if (visit == PostVisit)
2824 const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2826 TInfoSinkBase &out = mBody;
2828 const TStructure* structure = type.getStruct();
2831 out << StructNameString(*structure) + "_ctor(";
2833 const TFieldList& fields = structure->fields();
2835 for (size_t i = 0; i < fields.size(); i++)
2837 const TType *fieldType = fields[i]->type();
2838 constUnion = writeConstantUnion(*fieldType, constUnion);
2840 if (i != fields.size() - 1)
2850 size_t size = type.getObjectSize();
2851 bool writeType = size > 1;
2855 out << TypeString(type) << "(";
2858 for (size_t i = 0; i < size; i++, constUnion++)
2860 switch (constUnion->getType())
2862 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
2863 case EbtInt: out << constUnion->getIConst(); break;
2864 case EbtUInt: out << constUnion->getUConst(); break;
2865 case EbtBool: out << constUnion->getBConst(); break;
2866 default: UNREACHABLE();