1 //
2 // Copyright (c) 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.
5 //
6 // DynamicHLSL.cpp: Implementation for link and run-time HLSL generation
7 //
8 
9 #include "libANGLE/renderer/d3d/DynamicHLSL.h"
10 
11 #include "common/string_utils.h"
12 #include "common/utilities.h"
13 #include "compiler/translator/blocklayoutHLSL.h"
14 #include "libANGLE/Context.h"
15 #include "libANGLE/Program.h"
16 #include "libANGLE/Shader.h"
17 #include "libANGLE/VaryingPacking.h"
18 #include "libANGLE/formatutils.h"
19 #include "libANGLE/renderer/d3d/ProgramD3D.h"
20 #include "libANGLE/renderer/d3d/RendererD3D.h"
21 #include "libANGLE/renderer/d3d/ShaderD3D.h"
22 
23 using namespace gl;
24 
25 namespace rx
26 {
27 
28 namespace
29 {
30 
31 // This class needs to match OutputHLSL::decorate
32 class DecorateVariable final : angle::NonCopyable
33 {
34   public:
DecorateVariable(const std::string & str)35     explicit DecorateVariable(const std::string &str) : mName(str) {}
getName() const36     const std::string &getName() const { return mName; }
37 
38   private:
39     const std::string &mName;
40 };
41 
operator <<(std::ostream & o,const DecorateVariable & dv)42 std::ostream &operator<<(std::ostream &o, const DecorateVariable &dv)
43 {
44     if (dv.getName().compare(0, 3, "gl_") != 0)
45     {
46         o << "_";
47     }
48     o << dv.getName();
49     return o;
50 }
51 
HLSLComponentTypeString(GLenum componentType)52 const char *HLSLComponentTypeString(GLenum componentType)
53 {
54     switch (componentType)
55     {
56         case GL_UNSIGNED_INT:
57             return "uint";
58         case GL_INT:
59             return "int";
60         case GL_UNSIGNED_NORMALIZED:
61         case GL_SIGNED_NORMALIZED:
62         case GL_FLOAT:
63             return "float";
64         default:
65             UNREACHABLE();
66             return "not-component-type";
67     }
68 }
69 
HLSLComponentTypeString(std::ostringstream & ostream,GLenum componentType,int componentCount)70 void HLSLComponentTypeString(std::ostringstream &ostream, GLenum componentType, int componentCount)
71 {
72     ostream << HLSLComponentTypeString(componentType);
73     if (componentCount > 1)
74     {
75         ostream << componentCount;
76     }
77 }
78 
HLSLMatrixTypeString(GLenum type)79 const char *HLSLMatrixTypeString(GLenum type)
80 {
81     switch (type)
82     {
83         case GL_FLOAT_MAT2:
84             return "float2x2";
85         case GL_FLOAT_MAT3:
86             return "float3x3";
87         case GL_FLOAT_MAT4:
88             return "float4x4";
89         case GL_FLOAT_MAT2x3:
90             return "float2x3";
91         case GL_FLOAT_MAT3x2:
92             return "float3x2";
93         case GL_FLOAT_MAT2x4:
94             return "float2x4";
95         case GL_FLOAT_MAT4x2:
96             return "float4x2";
97         case GL_FLOAT_MAT3x4:
98             return "float3x4";
99         case GL_FLOAT_MAT4x3:
100             return "float4x3";
101         default:
102             UNREACHABLE();
103             return "not-matrix-type";
104     }
105 }
106 
HLSLTypeString(std::ostringstream & ostream,GLenum type)107 void HLSLTypeString(std::ostringstream &ostream, GLenum type)
108 {
109     if (gl::IsMatrixType(type))
110     {
111         ostream << HLSLMatrixTypeString(type);
112         return;
113     }
114 
115     HLSLComponentTypeString(ostream, gl::VariableComponentType(type),
116                             gl::VariableComponentCount(type));
117 }
118 
FindOutputAtLocation(const std::vector<PixelShaderOutputVariable> & outputVariables,unsigned int location)119 const PixelShaderOutputVariable *FindOutputAtLocation(
120     const std::vector<PixelShaderOutputVariable> &outputVariables,
121     unsigned int location)
122 {
123     for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
124     {
125         if (outputVariables[variableIndex].outputIndex == location)
126         {
127             return &outputVariables[variableIndex];
128         }
129     }
130 
131     return nullptr;
132 }
133 
WriteArrayString(std::ostringstream & strstr,unsigned int i)134 void WriteArrayString(std::ostringstream &strstr, unsigned int i)
135 {
136     static_assert(GL_INVALID_INDEX == UINT_MAX,
137                   "GL_INVALID_INDEX must be equal to the max unsigned int.");
138     if (i == UINT_MAX)
139     {
140         return;
141     }
142 
143     strstr << "[";
144     strstr << i;
145     strstr << "]";
146 }
147 
148 constexpr const char *VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
149 constexpr const char *PIXEL_OUTPUT_STUB_STRING     = "@@ PIXEL OUTPUT @@";
150 }  // anonymous namespace
151 
152 // BuiltinInfo implementation
153 
154 BuiltinInfo::BuiltinInfo()  = default;
155 BuiltinInfo::~BuiltinInfo() = default;
156 
157 // DynamicHLSL implementation
158 
DynamicHLSL(RendererD3D * const renderer)159 DynamicHLSL::DynamicHLSL(RendererD3D *const renderer) : mRenderer(renderer)
160 {
161 }
162 
generateVertexShaderForInputLayout(const std::string & sourceShader,const InputLayout & inputLayout,const std::vector<sh::Attribute> & shaderAttributes) const163 std::string DynamicHLSL::generateVertexShaderForInputLayout(
164     const std::string &sourceShader,
165     const InputLayout &inputLayout,
166     const std::vector<sh::Attribute> &shaderAttributes) const
167 {
168     std::ostringstream structStream;
169     std::ostringstream initStream;
170 
171     structStream << "struct VS_INPUT\n"
172                  << "{\n";
173 
174     int semanticIndex       = 0;
175     unsigned int inputIndex = 0;
176 
177     // If gl_PointSize is used in the shader then pointsprites rendering is expected.
178     // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
179     // must be used.
180     bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
181     bool useInstancedPointSpriteEmulation =
182         usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
183 
184     // Instanced PointSprite emulation requires additional entries in the
185     // VS_INPUT structure to support the vertices that make up the quad vertices.
186     // These values must be in sync with the cooresponding values added during inputlayout creation
187     // in InputLayoutCache::applyVertexBuffers().
188     //
189     // The additional entries must appear first in the VS_INPUT layout because
190     // Windows Phone 8 era devices require per vertex data to physically come
191     // before per instance data in the shader.
192     if (useInstancedPointSpriteEmulation)
193     {
194         structStream << "    float3 spriteVertexPos : SPRITEPOSITION0;\n"
195                      << "    float2 spriteTexCoord : SPRITETEXCOORD0;\n";
196     }
197 
198     for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex)
199     {
200         const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
201         if (!shaderAttribute.name.empty())
202         {
203             ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
204             VertexFormatType vertexFormatType =
205                 inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID;
206 
207             // HLSL code for input structure
208             if (IsMatrixType(shaderAttribute.type))
209             {
210                 // Matrix types are always transposed
211                 structStream << "    "
212                              << HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
213             }
214             else
215             {
216                 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
217 
218                 if (shaderAttribute.name == "gl_InstanceID" ||
219                     shaderAttribute.name == "gl_VertexID")
220                 {
221                     // The input types of the instance ID and vertex ID in HLSL (uint) differs from
222                     // the ones in ESSL (int).
223                     structStream << " uint";
224                 }
225                 else
226                 {
227                     structStream << "    ";
228                     HLSLComponentTypeString(structStream, componentType,
229                                             VariableComponentCount(shaderAttribute.type));
230                 }
231             }
232 
233             structStream << " " << DecorateVariable(shaderAttribute.name) << " : ";
234 
235             if (shaderAttribute.name == "gl_InstanceID")
236             {
237                 structStream << "SV_InstanceID";
238             }
239             else if (shaderAttribute.name == "gl_VertexID")
240             {
241                 structStream << "SV_VertexID";
242             }
243             else
244             {
245                 structStream << "TEXCOORD" << semanticIndex;
246                 semanticIndex += VariableRegisterCount(shaderAttribute.type);
247             }
248 
249             structStream << ";\n";
250 
251             // HLSL code for initialization
252             initStream << "    " << DecorateVariable(shaderAttribute.name) << " = ";
253 
254             // Mismatched vertex attribute to vertex input may result in an undefined
255             // data reinterpretation (eg for pure integer->float, float->pure integer)
256             // TODO: issue warning with gl debug info extension, when supported
257             if (IsMatrixType(shaderAttribute.type) ||
258                 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
259             {
260                 GenerateAttributeConversionHLSL(vertexFormatType, shaderAttribute, initStream);
261             }
262             else
263             {
264                 initStream << "input." << DecorateVariable(shaderAttribute.name);
265             }
266 
267             initStream << ";\n";
268 
269             inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
270         }
271     }
272 
273     structStream << "};\n"
274                     "\n"
275                     "void initAttributes(VS_INPUT input)\n"
276                     "{\n"
277                  << initStream.str() << "}\n";
278 
279     std::string vertexHLSL(sourceShader);
280 
281     bool success =
282         angle::ReplaceSubstring(&vertexHLSL, VERTEX_ATTRIBUTE_STUB_STRING, structStream.str());
283     ASSERT(success);
284 
285     return vertexHLSL;
286 }
287 
generatePixelShaderForOutputSignature(const std::string & sourceShader,const std::vector<PixelShaderOutputVariable> & outputVariables,bool usesFragDepth,const std::vector<GLenum> & outputLayout) const288 std::string DynamicHLSL::generatePixelShaderForOutputSignature(
289     const std::string &sourceShader,
290     const std::vector<PixelShaderOutputVariable> &outputVariables,
291     bool usesFragDepth,
292     const std::vector<GLenum> &outputLayout) const
293 {
294     const int shaderModel      = mRenderer->getMajorShaderModel();
295     std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
296     std::string depthSemantic  = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
297 
298     std::ostringstream declarationStream;
299     std::ostringstream copyStream;
300 
301     declarationStream << "struct PS_OUTPUT\n"
302                          "{\n";
303 
304     size_t numOutputs = outputLayout.size();
305 
306     // Workaround for HLSL 3.x: We can't do a depth/stencil only render, the runtime will complain.
307     if (numOutputs == 0 && (shaderModel == 3 || !mRenderer->getShaderModelSuffix().empty()))
308     {
309         numOutputs = 1u;
310     }
311     const PixelShaderOutputVariable defaultOutput(GL_FLOAT_VEC4, "dummy", "float4(0, 0, 0, 1)", 0);
312 
313     for (size_t layoutIndex = 0; layoutIndex < numOutputs; ++layoutIndex)
314     {
315         GLenum binding = outputLayout.empty() ? GL_COLOR_ATTACHMENT0 : outputLayout[layoutIndex];
316 
317         if (binding != GL_NONE)
318         {
319             unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
320 
321             const PixelShaderOutputVariable *outputVariable =
322                 outputLayout.empty() ? &defaultOutput
323                                      : FindOutputAtLocation(outputVariables, location);
324 
325             // OpenGL ES 3.0 spec $4.2.1
326             // If [...] not all user-defined output variables are written, the values of fragment
327             // colors
328             // corresponding to unwritten variables are similarly undefined.
329             if (outputVariable)
330             {
331                 declarationStream << "    ";
332                 HLSLTypeString(declarationStream, outputVariable->type);
333                 declarationStream << " " << outputVariable->name << " : " << targetSemantic
334                                   << static_cast<int>(layoutIndex) << ";\n";
335 
336                 copyStream << "    output." << outputVariable->name << " = "
337                            << outputVariable->source << ";\n";
338             }
339         }
340     }
341 
342     if (usesFragDepth)
343     {
344         declarationStream << "    float gl_Depth : " << depthSemantic << ";\n";
345         copyStream << "    output.gl_Depth = gl_Depth; \n";
346     }
347 
348     declarationStream << "};\n"
349                          "\n"
350                          "PS_OUTPUT generateOutput()\n"
351                          "{\n"
352                          "    PS_OUTPUT output;\n"
353                       << copyStream.str() << "    return output;\n"
354                                              "}\n";
355 
356     std::string pixelHLSL(sourceShader);
357 
358     bool success =
359         angle::ReplaceSubstring(&pixelHLSL, PIXEL_OUTPUT_STUB_STRING, declarationStream.str());
360     ASSERT(success);
361 
362     return pixelHLSL;
363 }
364 
generateVaryingLinkHLSL(const VaryingPacking & varyingPacking,const BuiltinInfo & builtins,bool programUsesPointSize,std::ostringstream & hlslStream) const365 void DynamicHLSL::generateVaryingLinkHLSL(const VaryingPacking &varyingPacking,
366                                           const BuiltinInfo &builtins,
367                                           bool programUsesPointSize,
368                                           std::ostringstream &hlslStream) const
369 {
370     ASSERT(builtins.dxPosition.enabled);
371     hlslStream << "{\n"
372                << "    float4 dx_Position : " << builtins.dxPosition.str() << ";\n";
373 
374     if (builtins.glPosition.enabled)
375     {
376         hlslStream << "    float4 gl_Position : " << builtins.glPosition.str() << ";\n";
377     }
378 
379     if (builtins.glFragCoord.enabled)
380     {
381         hlslStream << "    float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n";
382     }
383 
384     if (builtins.glPointCoord.enabled)
385     {
386         hlslStream << "    float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n";
387     }
388 
389     if (builtins.glPointSize.enabled)
390     {
391         hlslStream << "    float gl_PointSize : " << builtins.glPointSize.str() << ";\n";
392     }
393 
394     if (builtins.glViewIDOVR.enabled)
395     {
396         hlslStream << "    nointerpolation uint gl_ViewID_OVR : " << builtins.glViewIDOVR.str()
397                    << ";\n";
398     }
399 
400     if (builtins.glViewportIndex.enabled)
401     {
402         hlslStream << "    nointerpolation uint gl_ViewportIndex : "
403                    << builtins.glViewportIndex.str() << ";\n";
404     }
405 
406     if (builtins.glLayer.enabled)
407     {
408         hlslStream << "    nointerpolation uint gl_Layer : " << builtins.glLayer.str() << ";\n";
409     }
410 
411     std::string varyingSemantic =
412         GetVaryingSemantic(mRenderer->getMajorShaderModel(), programUsesPointSize);
413 
414     for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
415     {
416         const auto &varying = *registerInfo.packedVarying->varying;
417         ASSERT(!varying.isStruct());
418 
419         // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many
420         // registers being used.
421         // For example, if there are N registers, and we have N vec3 varyings and 1 float
422         // varying, then D3D will pack them into N registers.
423         // If the float varying has the 'nointerpolation' modifier on it then we would need
424         // N + 1 registers, and D3D compilation will fail.
425 
426         switch (registerInfo.packedVarying->interpolation)
427         {
428             case sh::INTERPOLATION_SMOOTH:
429                 hlslStream << "    ";
430                 break;
431             case sh::INTERPOLATION_FLAT:
432                 hlslStream << "    nointerpolation ";
433                 break;
434             case sh::INTERPOLATION_CENTROID:
435                 hlslStream << "    centroid ";
436                 break;
437             default:
438                 UNREACHABLE();
439         }
440 
441         GLenum transposedType = gl::TransposeMatrixType(varying.type);
442         GLenum componentType  = gl::VariableComponentType(transposedType);
443         int columnCount       = gl::VariableColumnCount(transposedType);
444         HLSLComponentTypeString(hlslStream, componentType, columnCount);
445         unsigned int semanticIndex = registerInfo.semanticIndex;
446         hlslStream << " v" << semanticIndex << " : " << varyingSemantic << semanticIndex << ";\n";
447     }
448 
449     hlslStream << "};\n";
450 }
451 
generateShaderLinkHLSL(const gl::Context * context,const gl::ProgramState & programData,const ProgramD3DMetadata & programMetadata,const VaryingPacking & varyingPacking,const BuiltinVaryingsD3D & builtinsD3D,std::string * pixelHLSL,std::string * vertexHLSL) const452 void DynamicHLSL::generateShaderLinkHLSL(const gl::Context *context,
453                                          const gl::ProgramState &programData,
454                                          const ProgramD3DMetadata &programMetadata,
455                                          const VaryingPacking &varyingPacking,
456                                          const BuiltinVaryingsD3D &builtinsD3D,
457                                          std::string *pixelHLSL,
458                                          std::string *vertexHLSL) const
459 {
460     ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
461 
462     const auto &data                   = context->getContextState();
463     gl::Shader *vertexShaderGL         = programData.getAttachedVertexShader();
464     gl::Shader *fragmentShaderGL       = programData.getAttachedFragmentShader();
465     const ShaderD3D *fragmentShader    = GetImplAs<ShaderD3D>(fragmentShaderGL);
466     const int shaderModel              = mRenderer->getMajorShaderModel();
467 
468     // usesViewScale() isn't supported in the D3D9 renderer
469     ASSERT(shaderModel >= 4 || !programMetadata.usesViewScale());
470 
471     bool useInstancedPointSpriteEmulation =
472         programMetadata.usesPointSize() &&
473         mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
474 
475     // Validation done in the compiler
476     ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
477 
478     std::ostringstream vertexStream;
479     vertexStream << vertexShaderGL->getTranslatedSource(context);
480 
481     // Instanced PointSprite emulation requires additional entries originally generated in the
482     // GeometryShader HLSL. These include pointsize clamp values.
483     if (useInstancedPointSpriteEmulation)
484     {
485         vertexStream << "static float minPointSize = "
486                      << static_cast<int>(data.getCaps().minAliasedPointSize) << ".0f;\n"
487                      << "static float maxPointSize = "
488                      << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n";
489     }
490 
491     // Add stub string to be replaced when shader is dynamically defined by its layout
492     vertexStream << "\n" << std::string(VERTEX_ATTRIBUTE_STUB_STRING) << "\n";
493 
494     const auto &vertexBuiltins = builtinsD3D[gl::SHADER_VERTEX];
495 
496     // Write the HLSL input/output declarations
497     vertexStream << "struct VS_OUTPUT\n";
498     generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
499                             vertexStream);
500     vertexStream << "\n"
501                  << "VS_OUTPUT main(VS_INPUT input)\n"
502                  << "{\n"
503                  << "    initAttributes(input);\n";
504 
505     vertexStream << "\n"
506                  << "    gl_main();\n"
507                  << "\n"
508                  << "    VS_OUTPUT output;\n";
509 
510     if (vertexBuiltins.glPosition.enabled)
511     {
512         vertexStream << "    output.gl_Position = gl_Position;\n";
513     }
514 
515     if (vertexBuiltins.glViewIDOVR.enabled)
516     {
517         vertexStream << "    output.gl_ViewID_OVR = _ViewID_OVR;\n";
518     }
519     if (programMetadata.hasANGLEMultiviewEnabled() && programMetadata.canSelectViewInVertexShader())
520     {
521         ASSERT(vertexBuiltins.glViewportIndex.enabled && vertexBuiltins.glLayer.enabled);
522         vertexStream << "    if (multiviewSelectViewportIndex)\n"
523                      << "    {\n"
524                      << "         output.gl_ViewportIndex = _ViewID_OVR;\n"
525                      << "    } else {\n"
526                      << "         output.gl_ViewportIndex = 0;\n"
527                      << "         output.gl_Layer = _ViewID_OVR;\n"
528                      << "    }\n";
529     }
530 
531     // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
532     if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
533     {
534         vertexStream << "    output.dx_Position.x = gl_Position.x;\n";
535 
536         if (programMetadata.usesViewScale())
537         {
538             // This code assumes that dx_ViewScale.y = -1.0f when rendering to texture, and +1.0f
539             // when rendering to the default framebuffer. No other values are valid.
540             vertexStream << "    output.dx_Position.y = dx_ViewScale.y * gl_Position.y;\n";
541         }
542         else
543         {
544             vertexStream << "    output.dx_Position.y = - gl_Position.y;\n";
545         }
546 
547         vertexStream << "    output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
548                      << "    output.dx_Position.w = gl_Position.w;\n";
549     }
550     else
551     {
552         vertexStream << "    output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
553                         "dx_ViewAdjust.x * gl_Position.w;\n";
554 
555         // If usesViewScale() is true and we're using the D3D11 renderer via Feature Level 9_*,
556         // then we need to multiply the gl_Position.y by the viewScale.
557         // usesViewScale() isn't supported when using the D3D9 renderer.
558         if (programMetadata.usesViewScale() &&
559             (shaderModel >= 4 && mRenderer->getShaderModelSuffix() != ""))
560         {
561             vertexStream << "    output.dx_Position.y = dx_ViewScale.y * (gl_Position.y * "
562                             "dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n";
563         }
564         else
565         {
566             vertexStream << "    output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
567                             "dx_ViewAdjust.y * gl_Position.w);\n";
568         }
569 
570         vertexStream << "    output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
571                      << "    output.dx_Position.w = gl_Position.w;\n";
572     }
573 
574     // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
575     if (vertexBuiltins.glPointSize.enabled)
576     {
577         vertexStream << "    output.gl_PointSize = gl_PointSize;\n";
578     }
579 
580     if (vertexBuiltins.glFragCoord.enabled)
581     {
582         vertexStream << "    output.gl_FragCoord = gl_Position;\n";
583     }
584 
585     for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
586     {
587         const auto &packedVarying = *registerInfo.packedVarying;
588         const auto &varying = *packedVarying.varying;
589         ASSERT(!varying.isStruct());
590 
591         vertexStream << "    output.v" << registerInfo.semanticIndex << " = ";
592 
593         if (packedVarying.isStructField())
594         {
595             vertexStream << DecorateVariable(packedVarying.parentStructName) << ".";
596         }
597 
598         vertexStream << DecorateVariable(varying.name);
599 
600         if (varying.isArray())
601         {
602             WriteArrayString(vertexStream, registerInfo.varyingArrayIndex);
603         }
604 
605         if (VariableRowCount(varying.type) > 1)
606         {
607             WriteArrayString(vertexStream, registerInfo.varyingRowIndex);
608         }
609 
610         vertexStream << ";\n";
611     }
612 
613     // Instanced PointSprite emulation requires additional entries to calculate
614     // the final output vertex positions of the quad that represents each sprite.
615     if (useInstancedPointSpriteEmulation)
616     {
617         vertexStream << "\n"
618                      << "    gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n";
619 
620         vertexStream << "    output.dx_Position.x += (input.spriteVertexPos.x * gl_PointSize / "
621                         "(dx_ViewCoords.x*2)) * output.dx_Position.w;";
622 
623         if (programMetadata.usesViewScale())
624         {
625             // Multiply by ViewScale to invert the rendering when appropriate
626             vertexStream << "    output.dx_Position.y += (-dx_ViewScale.y * "
627                             "input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2)) * "
628                             "output.dx_Position.w;";
629         }
630         else
631         {
632             vertexStream << "    output.dx_Position.y += (input.spriteVertexPos.y * gl_PointSize / "
633                             "(dx_ViewCoords.y*2)) * output.dx_Position.w;";
634         }
635 
636         vertexStream
637             << "    output.dx_Position.z += input.spriteVertexPos.z * output.dx_Position.w;\n";
638 
639         if (programMetadata.usesPointCoord())
640         {
641             vertexStream << "\n"
642                          << "    output.gl_PointCoord = input.spriteTexCoord;\n";
643         }
644     }
645 
646     // Renderers that enable instanced pointsprite emulation require the vertex shader output member
647     // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
648     // default value used in the generated pixel shader.
649     if (programMetadata.usesInsertedPointCoordValue())
650     {
651         ASSERT(!useInstancedPointSpriteEmulation);
652         vertexStream << "\n"
653                      << "    output.gl_PointCoord = float2(0.5, 0.5);\n";
654     }
655 
656     vertexStream << "\n"
657                  << "    return output;\n"
658                  << "}\n";
659 
660     const auto &pixelBuiltins = builtinsD3D[gl::SHADER_FRAGMENT];
661 
662     std::ostringstream pixelStream;
663     pixelStream << fragmentShaderGL->getTranslatedSource(context);
664     pixelStream << "struct PS_INPUT\n";
665     generateVaryingLinkHLSL(varyingPacking, pixelBuiltins, builtinsD3D.usesPointSize(),
666                             pixelStream);
667     pixelStream << "\n";
668 
669     pixelStream << std::string(PIXEL_OUTPUT_STUB_STRING) << "\n";
670 
671     if (fragmentShader->usesFrontFacing())
672     {
673         if (shaderModel >= 4)
674         {
675             pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
676                         << "{\n";
677         }
678         else
679         {
680             pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
681                         << "{\n";
682         }
683     }
684     else
685     {
686         pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
687                     << "{\n";
688     }
689 
690     if (fragmentShader->usesViewID())
691     {
692         ASSERT(pixelBuiltins.glViewIDOVR.enabled);
693         pixelStream << "    _ViewID_OVR = input.gl_ViewID_OVR;\n";
694     }
695 
696     if (pixelBuiltins.glFragCoord.enabled)
697     {
698         pixelStream << "    float rhw = 1.0 / input.gl_FragCoord.w;\n";
699 
700         // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
701         // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
702         // dx_ViewCoords.
703         if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
704         {
705             pixelStream << "    gl_FragCoord.x = input.dx_Position.x;\n"
706                         << "    gl_FragCoord.y = input.dx_Position.y;\n";
707         }
708         else if (shaderModel == 3)
709         {
710             pixelStream << "    gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
711                         << "    gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
712         }
713         else
714         {
715             // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
716             // Renderer::setViewport()
717             pixelStream << "    gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
718                            "dx_ViewCoords.z;\n"
719                         << "    gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
720                            "dx_ViewCoords.w;\n";
721         }
722 
723         if (programMetadata.usesViewScale())
724         {
725             // For Feature Level 9_3 and below, we need to correct gl_FragCoord.y to account
726             // for dx_ViewScale. On Feature Level 10_0+, gl_FragCoord.y is calculated above using
727             // dx_ViewCoords and is always correct irrespective of dx_ViewScale's value.
728             // NOTE: usesViewScale() can only be true on D3D11 (i.e. Shader Model 4.0+).
729             if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
730             {
731                 // Some assumptions:
732                 //  - dx_ViewScale.y = -1.0f when rendering to texture
733                 //  - dx_ViewScale.y = +1.0f when rendering to the default framebuffer
734                 //  - gl_FragCoord.y has been set correctly above.
735                 //
736                 // When rendering to the backbuffer, the code inverts gl_FragCoord's y coordinate.
737                 // This involves subtracting the y coordinate from the height of the area being
738                 // rendered to.
739                 //
740                 // First we calculate the height of the area being rendered to:
741                 //    render_area_height = (2.0f / (1.0f - input.gl_FragCoord.y * rhw)) *
742                 //    gl_FragCoord.y
743                 //
744                 // Note that when we're rendering to default FB, we want our output to be
745                 // equivalent to:
746                 //    "gl_FragCoord.y = render_area_height - gl_FragCoord.y"
747                 //
748                 // When we're rendering to a texture, we want our output to be equivalent to:
749                 //    "gl_FragCoord.y = gl_FragCoord.y;"
750                 //
751                 // If we set scale_factor = ((1.0f + dx_ViewScale.y) / 2.0f), then notice that
752                 //  - When rendering to default FB: scale_factor = 1.0f
753                 //  - When rendering to texture:    scale_factor = 0.0f
754                 //
755                 // Therefore, we can get our desired output by setting:
756                 //    "gl_FragCoord.y = scale_factor * render_area_height - dx_ViewScale.y *
757                 //    gl_FragCoord.y"
758                 //
759                 // Simplifying, this becomes:
760                 pixelStream
761                     << "    gl_FragCoord.y = (1.0f + dx_ViewScale.y) * gl_FragCoord.y /"
762                        "(1.0f - input.gl_FragCoord.y * rhw)  - dx_ViewScale.y * gl_FragCoord.y;\n";
763             }
764         }
765 
766         pixelStream << "    gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
767                        "dx_DepthFront.y;\n"
768                     << "    gl_FragCoord.w = rhw;\n";
769     }
770 
771     if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
772     {
773         pixelStream << "    gl_PointCoord.x = input.gl_PointCoord.x;\n"
774                     << "    gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
775     }
776 
777     if (fragmentShader->usesFrontFacing())
778     {
779         if (shaderModel <= 3)
780         {
781             pixelStream << "    gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
782         }
783         else
784         {
785             pixelStream << "    gl_FrontFacing = isFrontFace;\n";
786         }
787     }
788 
789     for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
790     {
791         const auto &packedVarying = *registerInfo.packedVarying;
792         const auto &varying = *packedVarying.varying;
793         ASSERT(!varying.isBuiltIn() && !varying.isStruct());
794 
795         // Don't reference VS-only transform feedback varyings in the PS. Note that we're relying on
796         // that the staticUse flag is set according to usage in the fragment shader.
797         if (packedVarying.vertexOnly || !varying.staticUse)
798             continue;
799 
800         pixelStream << "    ";
801 
802         if (packedVarying.isStructField())
803         {
804             pixelStream << DecorateVariable(packedVarying.parentStructName) << ".";
805         }
806 
807         pixelStream << DecorateVariable(varying.name);
808 
809         if (varying.isArray())
810         {
811             WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
812         }
813 
814         GLenum transposedType = TransposeMatrixType(varying.type);
815         if (VariableRowCount(transposedType) > 1)
816         {
817             WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
818         }
819 
820         pixelStream << " = input.v" << registerInfo.semanticIndex;
821 
822         switch (VariableColumnCount(transposedType))
823         {
824             case 1:
825                 pixelStream << ".x";
826                 break;
827             case 2:
828                 pixelStream << ".xy";
829                 break;
830             case 3:
831                 pixelStream << ".xyz";
832                 break;
833             case 4:
834                 break;
835             default:
836                 UNREACHABLE();
837         }
838         pixelStream << ";\n";
839     }
840 
841     pixelStream << "\n"
842                 << "    gl_main();\n"
843                 << "\n"
844                 << "    return generateOutput();\n"
845                 << "}\n";
846 
847     *vertexHLSL = vertexStream.str();
848     *pixelHLSL  = pixelStream.str();
849 }
850 
generateComputeShaderLinkHLSL(const gl::Context * context,const gl::ProgramState & programData) const851 std::string DynamicHLSL::generateComputeShaderLinkHLSL(const gl::Context *context,
852                                                        const gl::ProgramState &programData) const
853 {
854     gl::Shader *computeShaderGL = programData.getAttachedComputeShader();
855     std::stringstream computeStream;
856     std::string translatedSource = computeShaderGL->getTranslatedSource(context);
857     computeStream << translatedSource;
858 
859     bool usesWorkGroupID = translatedSource.find("GL_USES_WORK_GROUP_ID") != std::string::npos;
860     bool usesLocalInvocationID =
861         translatedSource.find("GL_USES_LOCAL_INVOCATION_ID") != std::string::npos;
862     bool usesGlobalInvocationID =
863         translatedSource.find("GL_USES_GLOBAL_INVOCATION_ID") != std::string::npos;
864     bool usesLocalInvocationIndex =
865         translatedSource.find("GL_USES_LOCAL_INVOCATION_INDEX") != std::string::npos;
866 
867     computeStream << "\nstruct CS_INPUT\n{\n";
868     if (usesWorkGroupID)
869     {
870         computeStream << "    uint3 dx_WorkGroupID : "
871                       << "SV_GroupID;\n";
872     }
873 
874     if (usesLocalInvocationID)
875     {
876         computeStream << "    uint3 dx_LocalInvocationID : "
877                       << "SV_GroupThreadID;\n";
878     }
879 
880     if (usesGlobalInvocationID)
881     {
882         computeStream << "    uint3 dx_GlobalInvocationID : "
883                       << "SV_DispatchThreadID;\n";
884     }
885 
886     if (usesLocalInvocationIndex)
887     {
888         computeStream << "    uint dx_LocalInvocationIndex : "
889                       << "SV_GroupIndex;\n";
890     }
891 
892     computeStream << "};\n\n";
893 
894     const sh::WorkGroupSize &localSize = computeShaderGL->getWorkGroupSize(context);
895     computeStream << "[numthreads(" << localSize[0] << ", " << localSize[1] << ", " << localSize[2]
896                   << ")]\n";
897 
898     computeStream << "void main(CS_INPUT input)\n"
899                   << "{\n";
900 
901     if (usesWorkGroupID)
902     {
903         computeStream << "    gl_WorkGroupID = input.dx_WorkGroupID;\n";
904     }
905     if (usesLocalInvocationID)
906     {
907         computeStream << "    gl_LocalInvocationID = input.dx_LocalInvocationID;\n";
908     }
909     if (usesGlobalInvocationID)
910     {
911         computeStream << "    gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n";
912     }
913     if (usesLocalInvocationIndex)
914     {
915         computeStream << "    gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n";
916     }
917 
918     computeStream << "\n"
919                   << "    gl_main();\n"
920                   << "}\n";
921 
922     return computeStream.str();
923 }
924 
generateGeometryShaderPreamble(const VaryingPacking & varyingPacking,const BuiltinVaryingsD3D & builtinsD3D,const bool hasANGLEMultiviewEnabled,const bool selectViewInVS) const925 std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking,
926                                                         const BuiltinVaryingsD3D &builtinsD3D,
927                                                         const bool hasANGLEMultiviewEnabled,
928                                                         const bool selectViewInVS) const
929 {
930     ASSERT(mRenderer->getMajorShaderModel() >= 4);
931 
932     std::ostringstream preambleStream;
933 
934     const auto &vertexBuiltins = builtinsD3D[gl::SHADER_VERTEX];
935 
936     preambleStream << "struct GS_INPUT\n";
937     generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
938                             preambleStream);
939     preambleStream << "\n"
940                    << "struct GS_OUTPUT\n";
941     generateVaryingLinkHLSL(varyingPacking, builtinsD3D[gl::SHADER_GEOMETRY],
942                             builtinsD3D.usesPointSize(), preambleStream);
943     preambleStream
944         << "\n"
945         << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
946         << "{\n"
947         << "    output.gl_Position = input.gl_Position;\n";
948 
949     if (vertexBuiltins.glPointSize.enabled)
950     {
951         preambleStream << "    output.gl_PointSize = input.gl_PointSize;\n";
952     }
953 
954     if (hasANGLEMultiviewEnabled)
955     {
956         preambleStream << "    output.gl_ViewID_OVR = input.gl_ViewID_OVR;\n";
957         if (selectViewInVS)
958         {
959             ASSERT(builtinsD3D[gl::SHADER_GEOMETRY].glViewportIndex.enabled &&
960                    builtinsD3D[gl::SHADER_GEOMETRY].glLayer.enabled);
961 
962             // If the view is already selected in the VS, then we just pass the gl_ViewportIndex and
963             // gl_Layer to the output.
964             preambleStream << "    output.gl_ViewportIndex = input.gl_ViewportIndex;\n"
965                            << "    output.gl_Layer = input.gl_Layer;\n";
966         }
967     }
968 
969     for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
970     {
971         preambleStream << "    output.v" << varyingRegister.semanticIndex << " = ";
972         if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
973         {
974             preambleStream << "flat";
975         }
976         preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
977     }
978 
979     if (vertexBuiltins.glFragCoord.enabled)
980     {
981         preambleStream << "    output.gl_FragCoord = input.gl_FragCoord;\n";
982     }
983 
984     // Only write the dx_Position if we aren't using point sprites
985     preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
986                    << "    output.dx_Position = input.dx_Position;\n"
987                    << "#endif  // ANGLE_POINT_SPRITE_SHADER\n"
988                    << "}\n";
989 
990     if (hasANGLEMultiviewEnabled && !selectViewInVS)
991     {
992         ASSERT(builtinsD3D[gl::SHADER_GEOMETRY].glViewportIndex.enabled &&
993                builtinsD3D[gl::SHADER_GEOMETRY].glLayer.enabled);
994 
995         // According to the HLSL reference, using SV_RenderTargetArrayIndex is only valid if the
996         // render target is an array resource. Because of this we do not write to gl_Layer if we are
997         // taking the side-by-side code path. We still select the viewport index in the layered code
998         // path as that is always valid. See:
999         // https://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx
1000         preambleStream << "\n"
1001                        << "void selectView(inout GS_OUTPUT output, GS_INPUT input)\n"
1002                        << "{\n"
1003                        << "    if (multiviewSelectViewportIndex)\n"
1004                        << "    {\n"
1005                        << "        output.gl_ViewportIndex = input.gl_ViewID_OVR;\n"
1006                        << "    } else {\n"
1007                        << "        output.gl_ViewportIndex = 0;\n"
1008                        << "        output.gl_Layer = input.gl_ViewID_OVR;\n"
1009                        << "    }\n"
1010                        << "}\n";
1011     }
1012 
1013     return preambleStream.str();
1014 }
1015 
generateGeometryShaderHLSL(const gl::Context * context,gl::PrimitiveType primitiveType,const gl::ProgramState & programData,const bool useViewScale,const bool hasANGLEMultiviewEnabled,const bool selectViewInVS,const bool pointSpriteEmulation,const std::string & preambleString) const1016 std::string DynamicHLSL::generateGeometryShaderHLSL(const gl::Context *context,
1017                                                     gl::PrimitiveType primitiveType,
1018                                                     const gl::ProgramState &programData,
1019                                                     const bool useViewScale,
1020                                                     const bool hasANGLEMultiviewEnabled,
1021                                                     const bool selectViewInVS,
1022                                                     const bool pointSpriteEmulation,
1023                                                     const std::string &preambleString) const
1024 {
1025     ASSERT(mRenderer->getMajorShaderModel() >= 4);
1026 
1027     std::stringstream shaderStream;
1028 
1029     const bool pointSprites   = (primitiveType == PRIMITIVE_POINTS) && pointSpriteEmulation;
1030     const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
1031 
1032     const char *inputPT  = nullptr;
1033     const char *outputPT = nullptr;
1034     int inputSize        = 0;
1035     int maxVertexOutput  = 0;
1036 
1037     switch (primitiveType)
1038     {
1039         case PRIMITIVE_POINTS:
1040             inputPT         = "point";
1041             inputSize       = 1;
1042 
1043             if (pointSprites)
1044             {
1045                 outputPT        = "Triangle";
1046                 maxVertexOutput = 4;
1047             }
1048             else
1049             {
1050                 outputPT        = "Point";
1051                 maxVertexOutput = 1;
1052             }
1053 
1054             break;
1055 
1056         case PRIMITIVE_LINES:
1057         case PRIMITIVE_LINE_STRIP:
1058         case PRIMITIVE_LINE_LOOP:
1059             inputPT         = "line";
1060             outputPT        = "Line";
1061             inputSize       = 2;
1062             maxVertexOutput = 2;
1063             break;
1064 
1065         case PRIMITIVE_TRIANGLES:
1066         case PRIMITIVE_TRIANGLE_STRIP:
1067         case PRIMITIVE_TRIANGLE_FAN:
1068             inputPT         = "triangle";
1069             outputPT        = "Triangle";
1070             inputSize       = 3;
1071             maxVertexOutput = 3;
1072             break;
1073 
1074         default:
1075             UNREACHABLE();
1076             break;
1077     }
1078 
1079     if (pointSprites || hasANGLEMultiviewEnabled)
1080     {
1081         shaderStream << "cbuffer DriverConstants : register(b0)\n"
1082                         "{\n";
1083 
1084         if (pointSprites)
1085         {
1086             shaderStream << "    float4 dx_ViewCoords : packoffset(c1);\n";
1087             if (useViewScale)
1088             {
1089                 shaderStream << "    float2 dx_ViewScale : packoffset(c3);\n";
1090             }
1091         }
1092 
1093         if (hasANGLEMultiviewEnabled)
1094         {
1095             // We have to add a value which we can use to keep track of which multi-view code path
1096             // is to be selected in the GS.
1097             shaderStream << "    float multiviewSelectViewportIndex : packoffset(c3.z);\n";
1098         }
1099 
1100         shaderStream << "};\n\n";
1101     }
1102 
1103     if (pointSprites)
1104     {
1105         shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
1106                         "\n"
1107                         "static float2 pointSpriteCorners[] = \n"
1108                         "{\n"
1109                         "    float2( 0.5f, -0.5f),\n"
1110                         "    float2( 0.5f,  0.5f),\n"
1111                         "    float2(-0.5f, -0.5f),\n"
1112                         "    float2(-0.5f,  0.5f)\n"
1113                         "};\n"
1114                         "\n"
1115                         "static float2 pointSpriteTexcoords[] = \n"
1116                         "{\n"
1117                         "    float2(1.0f, 1.0f),\n"
1118                         "    float2(1.0f, 0.0f),\n"
1119                         "    float2(0.0f, 1.0f),\n"
1120                         "    float2(0.0f, 0.0f)\n"
1121                         "};\n"
1122                         "\n"
1123                         "static float minPointSize = "
1124                      << static_cast<int>(context->getCaps().minAliasedPointSize)
1125                      << ".0f;\n"
1126                         "static float maxPointSize = "
1127                      << static_cast<int>(context->getCaps().maxAliasedPointSize) << ".0f;\n"
1128                      << "\n";
1129     }
1130 
1131     shaderStream << preambleString << "\n"
1132                  << "[maxvertexcount(" << maxVertexOutput << ")]\n"
1133                  << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
1134 
1135     if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
1136     {
1137         shaderStream << "uint primitiveID : SV_PrimitiveID, ";
1138     }
1139 
1140     shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
1141                  << "{\n"
1142                  << "    GS_OUTPUT output = (GS_OUTPUT)0;\n";
1143 
1144     if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
1145     {
1146         shaderStream << "    uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
1147     }
1148     else
1149     {
1150         shaderStream << "    uint lastVertexIndex = " << (inputSize - 1) << ";\n";
1151     }
1152 
1153     for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
1154     {
1155         shaderStream << "    copyVertex(output, input[" << vertexIndex
1156                      << "], input[lastVertexIndex]);\n";
1157         if (hasANGLEMultiviewEnabled && !selectViewInVS)
1158         {
1159             shaderStream << "   selectView(output, input[" << vertexIndex << "]);\n";
1160         }
1161         if (!pointSprites)
1162         {
1163             ASSERT(inputSize == maxVertexOutput);
1164             shaderStream << "    outStream.Append(output);\n";
1165         }
1166     }
1167 
1168     if (pointSprites)
1169     {
1170         shaderStream << "\n"
1171                         "    float4 dx_Position = input[0].dx_Position;\n"
1172                         "    float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
1173                         "maxPointSize);\n"
1174                         "    float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
1175                         "dx_ViewCoords.y) * dx_Position.w;\n";
1176 
1177         for (int corner = 0; corner < 4; corner++)
1178         {
1179             if (useViewScale)
1180             {
1181                 shaderStream << "    \n"
1182                                 "    output.dx_Position = dx_Position + float4(1.0f, "
1183                                 "-dx_ViewScale.y, 1.0f, 1.0f)"
1184                                 "        * float4(pointSpriteCorners["
1185                              << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1186             }
1187             else
1188             {
1189                 shaderStream << "\n"
1190                                 "    output.dx_Position = dx_Position + float4(pointSpriteCorners["
1191                              << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1192             }
1193 
1194             if (usesPointCoord)
1195             {
1196                 shaderStream << "    output.gl_PointCoord = pointSpriteTexcoords[" << corner
1197                              << "];\n";
1198             }
1199 
1200             shaderStream << "    outStream.Append(output);\n";
1201         }
1202     }
1203 
1204     shaderStream << "    \n"
1205                     "    outStream.RestartStrip();\n"
1206                     "}\n";
1207 
1208     return shaderStream.str();
1209 }
1210 
1211 // static
GenerateAttributeConversionHLSL(gl::VertexFormatType vertexFormatType,const sh::ShaderVariable & shaderAttrib,std::ostringstream & outStream)1212 void DynamicHLSL::GenerateAttributeConversionHLSL(gl::VertexFormatType vertexFormatType,
1213                                                   const sh::ShaderVariable &shaderAttrib,
1214                                                   std::ostringstream &outStream)
1215 {
1216     // Matrix
1217     if (IsMatrixType(shaderAttrib.type))
1218     {
1219         outStream << "transpose(input." << DecorateVariable(shaderAttrib.name) << ")";
1220         return;
1221     }
1222 
1223     GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1224     int shaderComponentCount   = VariableComponentCount(shaderAttrib.type);
1225     const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
1226 
1227     // Perform integer to float conversion (if necessary)
1228     if (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT)
1229     {
1230         // TODO: normalization for 32-bit integer formats
1231         ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
1232         outStream << "float" << shaderComponentCount << "(input."
1233                   << DecorateVariable(shaderAttrib.name) << ")";
1234         return;
1235     }
1236 
1237     // No conversion necessary
1238     outStream << "input." << DecorateVariable(shaderAttrib.name);
1239 }
1240 
getPixelShaderOutputKey(const gl::ContextState & data,const gl::ProgramState & programData,const ProgramD3DMetadata & metadata,std::vector<PixelShaderOutputVariable> * outPixelShaderKey)1241 void DynamicHLSL::getPixelShaderOutputKey(const gl::ContextState &data,
1242                                           const gl::ProgramState &programData,
1243                                           const ProgramD3DMetadata &metadata,
1244                                           std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1245 {
1246     // Two cases when writing to gl_FragColor and using ESSL 1.0:
1247     // - with a 3.0 context, the output color is copied to channel 0
1248     // - with a 2.0 context, the output color is broadcast to all channels
1249     bool broadcast = metadata.usesBroadcast(data);
1250     const unsigned int numRenderTargets =
1251         (broadcast || metadata.usesMultipleFragmentOuts() ? data.getCaps().maxDrawBuffers : 1);
1252 
1253     if (metadata.getMajorShaderVersion() < 300)
1254     {
1255         for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1256              renderTargetIndex++)
1257         {
1258             PixelShaderOutputVariable outputKeyVariable;
1259             outputKeyVariable.type = GL_FLOAT_VEC4;
1260             outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1261             outputKeyVariable.source =
1262                 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1263             outputKeyVariable.outputIndex = renderTargetIndex;
1264 
1265             outPixelShaderKey->push_back(outputKeyVariable);
1266         }
1267     }
1268     else
1269     {
1270         const auto &shaderOutputVars =
1271             metadata.getFragmentShader()->getData().getActiveOutputVariables();
1272 
1273         for (size_t outputLocationIndex = 0u;
1274              outputLocationIndex < programData.getOutputLocations().size(); ++outputLocationIndex)
1275         {
1276             const VariableLocation &outputLocation =
1277                 programData.getOutputLocations().at(outputLocationIndex);
1278             if (!outputLocation.used())
1279             {
1280                 continue;
1281             }
1282             const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1283             const std::string &variableName          = "out_" + outputVariable.name;
1284 
1285             // Fragment outputs can't be arrays of arrays. ESSL 3.10 section 4.3.6.
1286             const std::string &elementString =
1287                 (outputVariable.isArray() ? Str(outputLocation.arrayIndex) : "");
1288 
1289             ASSERT(outputVariable.staticUse);
1290 
1291             PixelShaderOutputVariable outputKeyVariable;
1292             outputKeyVariable.type        = outputVariable.type;
1293             outputKeyVariable.name        = variableName + elementString;
1294             outputKeyVariable.source =
1295                 variableName +
1296                 (outputVariable.isArray() ? ArrayString(outputLocation.arrayIndex) : "");
1297             outputKeyVariable.outputIndex = outputLocationIndex;
1298 
1299             outPixelShaderKey->push_back(outputKeyVariable);
1300         }
1301     }
1302 }
1303 
1304 // BuiltinVarying Implementation.
BuiltinVarying()1305 BuiltinVarying::BuiltinVarying() : enabled(false), index(0), systemValue(false)
1306 {
1307 }
1308 
str() const1309 std::string BuiltinVarying::str() const
1310 {
1311     return (systemValue ? semantic : (semantic + Str(index)));
1312 }
1313 
enableSystem(const std::string & systemValueSemantic)1314 void BuiltinVarying::enableSystem(const std::string &systemValueSemantic)
1315 {
1316     enabled     = true;
1317     semantic    = systemValueSemantic;
1318     systemValue = true;
1319 }
1320 
enable(const std::string & semanticVal,unsigned int indexVal)1321 void BuiltinVarying::enable(const std::string &semanticVal, unsigned int indexVal)
1322 {
1323     enabled  = true;
1324     semantic = semanticVal;
1325     index    = indexVal;
1326 }
1327 
1328 // BuiltinVaryingsD3D Implementation.
BuiltinVaryingsD3D(const ProgramD3DMetadata & metadata,const VaryingPacking & packing)1329 BuiltinVaryingsD3D::BuiltinVaryingsD3D(const ProgramD3DMetadata &metadata,
1330                                        const VaryingPacking &packing)
1331 {
1332     updateBuiltins(gl::SHADER_VERTEX, metadata, packing);
1333     updateBuiltins(gl::SHADER_FRAGMENT, metadata, packing);
1334     if (metadata.getRendererMajorShaderModel() >= 4)
1335     {
1336         updateBuiltins(gl::SHADER_GEOMETRY, metadata, packing);
1337     }
1338 }
1339 
1340 BuiltinVaryingsD3D::~BuiltinVaryingsD3D() = default;
1341 
updateBuiltins(gl::ShaderType shaderType,const ProgramD3DMetadata & metadata,const VaryingPacking & packing)1342 void BuiltinVaryingsD3D::updateBuiltins(gl::ShaderType shaderType,
1343                                         const ProgramD3DMetadata &metadata,
1344                                         const VaryingPacking &packing)
1345 {
1346     const std::string &userSemantic = GetVaryingSemantic(metadata.getRendererMajorShaderModel(),
1347                                                          metadata.usesSystemValuePointSize());
1348 
1349     unsigned int reservedSemanticIndex = packing.getMaxSemanticIndex();
1350 
1351     BuiltinInfo *builtins = &mBuiltinInfo[shaderType];
1352 
1353     if (metadata.getRendererMajorShaderModel() >= 4)
1354     {
1355         builtins->dxPosition.enableSystem("SV_Position");
1356     }
1357     else if (shaderType == gl::SHADER_FRAGMENT)
1358     {
1359         builtins->dxPosition.enableSystem("VPOS");
1360     }
1361     else
1362     {
1363         builtins->dxPosition.enableSystem("POSITION");
1364     }
1365 
1366     if (metadata.usesTransformFeedbackGLPosition())
1367     {
1368         builtins->glPosition.enable(userSemantic, reservedSemanticIndex++);
1369     }
1370 
1371     if (metadata.usesFragCoord())
1372     {
1373         builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++);
1374     }
1375 
1376     if (shaderType == gl::SHADER_VERTEX ? metadata.addsPointCoordToVertexShader()
1377                                         : metadata.usesPointCoord())
1378     {
1379         // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
1380         // In D3D11 we manually compute gl_PointCoord in the GS.
1381         if (metadata.getRendererMajorShaderModel() >= 4)
1382         {
1383             builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++);
1384         }
1385         else
1386         {
1387             builtins->glPointCoord.enable("TEXCOORD", 0);
1388         }
1389     }
1390 
1391     if (shaderType == gl::SHADER_VERTEX && metadata.hasANGLEMultiviewEnabled())
1392     {
1393         builtins->glViewIDOVR.enable(userSemantic, reservedSemanticIndex++);
1394         if (metadata.canSelectViewInVertexShader())
1395         {
1396             builtins->glViewportIndex.enableSystem("SV_ViewportArrayIndex");
1397             builtins->glLayer.enableSystem("SV_RenderTargetArrayIndex");
1398         }
1399     }
1400 
1401     if (shaderType == gl::SHADER_FRAGMENT && metadata.hasANGLEMultiviewEnabled())
1402     {
1403         builtins->glViewIDOVR.enable(userSemantic, reservedSemanticIndex++);
1404     }
1405 
1406     if (shaderType == gl::SHADER_GEOMETRY && metadata.hasANGLEMultiviewEnabled())
1407     {
1408         // Although it is possible to retrieve gl_ViewID_OVR from the value of
1409         // SV_ViewportArrayIndex or SV_RenderTargetArrayIndex based on the multi-view state in the
1410         // driver constant buffer, it is easier and cleaner to pass it as a varying.
1411         builtins->glViewIDOVR.enable(userSemantic, reservedSemanticIndex++);
1412 
1413         // gl_Layer and gl_ViewportIndex are necessary so that we can write to either based on the
1414         // multiview state in the driver constant buffer.
1415         builtins->glViewportIndex.enableSystem("SV_ViewportArrayIndex");
1416         builtins->glLayer.enableSystem("SV_RenderTargetArrayIndex");
1417     }
1418 
1419     // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
1420     if (metadata.usesSystemValuePointSize() &&
1421         (shaderType != gl::SHADER_FRAGMENT || metadata.getRendererMajorShaderModel() >= 4))
1422     {
1423         builtins->glPointSize.enableSystem("PSIZE");
1424     }
1425 }
1426 
1427 }  // namespace rx
1428