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