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