blob: b4366e7d621072e0f4ea28ee0e1a090228424fae [file] [log] [blame]
Jamie Madill5f562732014-02-14 16:41:24 -05001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// DynamicHLSL.cpp: Implementation for link and run-time HLSL generation
7//
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050010
11#include "common/utilities.h"
Daniel Bratell73941de2015-02-25 14:34:49 +010012#include "compiler/translator/blocklayoutHLSL.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Program.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Shader.h"
15#include "libANGLE/formatutils.h"
Jamie Madill28afae52015-11-09 15:07:57 -050016#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill80a6fc02015-08-21 16:53:16 -040017#include "libANGLE/renderer/d3d/RendererD3D.h"
18#include "libANGLE/renderer/d3d/ShaderD3D.h"
Jamie Madill192745a2016-12-22 15:58:21 -050019#include "libANGLE/VaryingPacking.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040020
Brandon Jonesd8d72432014-08-22 15:11:23 -070021using namespace gl;
22
Jamie Madill30d6c252014-11-13 10:03:33 -050023namespace rx
24{
25
Jamie Madill3f2e61d2014-09-05 10:38:05 -040026namespace
Jamie Madill5f562732014-02-14 16:41:24 -050027{
Jamie Madill8664b062014-02-14 16:41:29 -050028
29std::string HLSLComponentTypeString(GLenum componentType)
30{
31 switch (componentType)
32 {
Jamie Madill334d6152015-10-22 14:00:28 -040033 case GL_UNSIGNED_INT:
34 return "uint";
35 case GL_INT:
36 return "int";
37 case GL_UNSIGNED_NORMALIZED:
38 case GL_SIGNED_NORMALIZED:
39 case GL_FLOAT:
40 return "float";
41 default:
42 UNREACHABLE();
43 return "not-component-type";
Jamie Madill8664b062014-02-14 16:41:29 -050044 }
Jamie Madill5f562732014-02-14 16:41:24 -050045}
46
Jamie Madill8664b062014-02-14 16:41:29 -050047std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
48{
49 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
50}
51
52std::string HLSLMatrixTypeString(GLenum type)
53{
54 switch (type)
55 {
Jamie Madill334d6152015-10-22 14:00:28 -040056 case GL_FLOAT_MAT2:
57 return "float2x2";
58 case GL_FLOAT_MAT3:
59 return "float3x3";
60 case GL_FLOAT_MAT4:
61 return "float4x4";
62 case GL_FLOAT_MAT2x3:
63 return "float2x3";
64 case GL_FLOAT_MAT3x2:
65 return "float3x2";
66 case GL_FLOAT_MAT2x4:
67 return "float2x4";
68 case GL_FLOAT_MAT4x2:
69 return "float4x2";
70 case GL_FLOAT_MAT3x4:
71 return "float3x4";
72 case GL_FLOAT_MAT4x3:
73 return "float4x3";
74 default:
75 UNREACHABLE();
76 return "not-matrix-type";
Jamie Madill8664b062014-02-14 16:41:29 -050077 }
78}
79
80std::string HLSLTypeString(GLenum type)
81{
82 if (gl::IsMatrixType(type))
83 {
84 return HLSLMatrixTypeString(type);
85 }
86
Jamie Madill334d6152015-10-22 14:00:28 -040087 return HLSLComponentTypeString(gl::VariableComponentType(type),
88 gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050089}
90
Jamie Madill334d6152015-10-22 14:00:28 -040091const PixelShaderOutputVariable *FindOutputAtLocation(
92 const std::vector<PixelShaderOutputVariable> &outputVariables,
93 unsigned int location)
Jamie Madill3f2e61d2014-09-05 10:38:05 -040094{
95 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
96 {
97 if (outputVariables[variableIndex].outputIndex == location)
98 {
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +000099 return &outputVariables[variableIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400100 }
101 }
102
Jamie Madill334d6152015-10-22 14:00:28 -0400103 return nullptr;
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400104}
105
Jamie Madill55c25d02015-11-18 13:08:08 -0500106void WriteArrayString(std::stringstream &strstr, unsigned int i)
107{
108 static_assert(GL_INVALID_INDEX == UINT_MAX,
109 "GL_INVALID_INDEX must be equal to the max unsigned int.");
110 if (i == UINT_MAX)
111 {
112 return;
113 }
114
115 strstr << "[";
116 strstr << i;
117 strstr << "]";
118}
119
Jamie Madill51c47682016-10-25 15:50:14 -0400120constexpr const char *VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
121constexpr const char *PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400122} // anonymous namespace
Jamie Madill4cff2472015-08-21 16:53:18 -0400123
Jamie Madill9fc36822015-11-18 13:08:07 -0500124// DynamicHLSL implementation
125
Jamie Madill65345da2015-11-13 11:25:23 -0500126DynamicHLSL::DynamicHLSL(RendererD3D *const renderer) : mRenderer(renderer)
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000127{
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000128}
129
Jamie Madill334d6152015-10-22 14:00:28 -0400130std::string DynamicHLSL::generateVertexShaderForInputLayout(
131 const std::string &sourceShader,
132 const InputLayout &inputLayout,
133 const std::vector<sh::Attribute> &shaderAttributes) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500134{
Jamie Madill334d6152015-10-22 14:00:28 -0400135 std::stringstream structStream;
136 std::stringstream initStream;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500137
Jamie Madill334d6152015-10-22 14:00:28 -0400138 structStream << "struct VS_INPUT\n"
139 << "{\n";
140
141 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400142 unsigned int inputIndex = 0;
143
Cooper Partine6d14cc2015-02-20 12:32:58 -0800144 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
145 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
146 // must be used.
147 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
Jamie Madill334d6152015-10-22 14:00:28 -0400148 bool useInstancedPointSpriteEmulation =
149 usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Cooper Partine6d14cc2015-02-20 12:32:58 -0800150
151 // Instanced PointSprite emulation requires additional entries in the
152 // VS_INPUT structure to support the vertices that make up the quad vertices.
153 // These values must be in sync with the cooresponding values added during inputlayout creation
154 // in InputLayoutCache::applyVertexBuffers().
155 //
156 // The additional entries must appear first in the VS_INPUT layout because
157 // Windows Phone 8 era devices require per vertex data to physically come
158 // before per instance data in the shader.
159 if (useInstancedPointSpriteEmulation)
160 {
Jamie Madill334d6152015-10-22 14:00:28 -0400161 structStream << " float3 spriteVertexPos : SPRITEPOSITION0;\n"
162 << " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
Cooper Partine6d14cc2015-02-20 12:32:58 -0800163 }
164
Jamie Madill3da79b72015-04-27 11:09:17 -0400165 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex)
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500166 {
Jamie Madillf2575982014-06-25 16:04:54 -0400167 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500168 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500169 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400170 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400171 VertexFormatType vertexFormatType =
172 inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID;
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400173
Jamie Madill3b7e2052014-03-17 09:47:43 -0400174 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500175 if (IsMatrixType(shaderAttribute.type))
176 {
177 // Matrix types are always transposed
Jamie Madill334d6152015-10-22 14:00:28 -0400178 structStream << " "
179 << HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500180 }
181 else
182 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400183 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000184
Corentin Wallezb076add2016-01-11 16:45:46 -0500185 if (shaderAttribute.name == "gl_InstanceID" ||
186 shaderAttribute.name == "gl_VertexID")
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000187 {
Corentin Wallezb076add2016-01-11 16:45:46 -0500188 // The input types of the instance ID and vertex ID in HLSL (uint) differs from
189 // the ones in ESSL (int).
Jamie Madill334d6152015-10-22 14:00:28 -0400190 structStream << " uint";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000191 }
192 else
193 {
Jamie Madill334d6152015-10-22 14:00:28 -0400194 structStream << " " << HLSLComponentTypeString(
195 componentType,
196 VariableComponentCount(shaderAttribute.type));
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000197 }
Jamie Madill8664b062014-02-14 16:41:29 -0500198 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500199
Jamie Madill334d6152015-10-22 14:00:28 -0400200 structStream << " " << decorateVariable(shaderAttribute.name) << " : ";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000201
202 if (shaderAttribute.name == "gl_InstanceID")
203 {
Jamie Madill334d6152015-10-22 14:00:28 -0400204 structStream << "SV_InstanceID";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000205 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500206 else if (shaderAttribute.name == "gl_VertexID")
207 {
208 structStream << "SV_VertexID";
209 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000210 else
211 {
Jamie Madill334d6152015-10-22 14:00:28 -0400212 structStream << "TEXCOORD" << semanticIndex;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000213 semanticIndex += VariableRegisterCount(shaderAttribute.type);
214 }
215
Jamie Madill334d6152015-10-22 14:00:28 -0400216 structStream << ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500217
Jamie Madill3b7e2052014-03-17 09:47:43 -0400218 // HLSL code for initialization
Jamie Madill334d6152015-10-22 14:00:28 -0400219 initStream << " " << decorateVariable(shaderAttribute.name) << " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500220
221 // Mismatched vertex attribute to vertex input may result in an undefined
222 // data reinterpretation (eg for pure integer->float, float->pure integer)
223 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400224 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400225 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500226 {
Jamie Madill334d6152015-10-22 14:00:28 -0400227 initStream << generateAttributeConversionHLSL(vertexFormatType, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500228 }
229 else
230 {
Jamie Madill334d6152015-10-22 14:00:28 -0400231 initStream << "input." << decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500232 }
233
Jamie Madill334d6152015-10-22 14:00:28 -0400234 initStream << ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400235
Jamie Madillac0a2672014-04-11 13:33:56 -0400236 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
237 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500238 }
239
Jamie Madill334d6152015-10-22 14:00:28 -0400240 structStream << "};\n"
241 "\n"
242 "void initAttributes(VS_INPUT input)\n"
243 "{\n"
244 << initStream.str() << "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400245
246 std::string vertexHLSL(sourceShader);
247
248 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
Jamie Madill51c47682016-10-25 15:50:14 -0400249 vertexHLSL.replace(copyInsertionPos, strlen(VERTEX_ATTRIBUTE_STUB_STRING), structStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400250
251 return vertexHLSL;
252}
253
Jamie Madill334d6152015-10-22 14:00:28 -0400254std::string DynamicHLSL::generatePixelShaderForOutputSignature(
255 const std::string &sourceShader,
256 const std::vector<PixelShaderOutputVariable> &outputVariables,
257 bool usesFragDepth,
258 const std::vector<GLenum> &outputLayout) const
Geoff Lang04fb89a2014-06-09 15:05:36 -0400259{
Jamie Madill334d6152015-10-22 14:00:28 -0400260 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang04fb89a2014-06-09 15:05:36 -0400261 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
Jamie Madill334d6152015-10-22 14:00:28 -0400262 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400263
Jamie Madill334d6152015-10-22 14:00:28 -0400264 std::stringstream declarationStream;
265 std::stringstream copyStream;
266
267 declarationStream << "struct PS_OUTPUT\n"
268 "{\n";
Geoff Lang4ace4232014-06-18 19:12:48 -0400269
Jamie Madill66a08192017-02-03 15:24:25 -0500270 // Workaround for HLSL 3.x: We can't do a depth/stencil only render, the runtime will complain.
271 size_t numOutputs = outputLayout.empty() ? 1u : outputLayout.size();
272 const PixelShaderOutputVariable defaultOutput(GL_FLOAT_VEC4, "dummy", "float4(0, 0, 0, 1)", 0);
273
274 for (size_t layoutIndex = 0; layoutIndex < numOutputs; ++layoutIndex)
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400275 {
Jamie Madill66a08192017-02-03 15:24:25 -0500276 GLenum binding = outputLayout.empty() ? GL_COLOR_ATTACHMENT0 : outputLayout[layoutIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400277
278 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400279 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400280 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
281
Jamie Madill334d6152015-10-22 14:00:28 -0400282 const PixelShaderOutputVariable *outputVariable =
Jamie Madill66a08192017-02-03 15:24:25 -0500283 outputLayout.empty() ? &defaultOutput
284 : FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400285
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000286 // OpenGL ES 3.0 spec $4.2.1
Jamie Madill334d6152015-10-22 14:00:28 -0400287 // If [...] not all user-defined output variables are written, the values of fragment
288 // colors
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000289 // corresponding to unwritten variables are similarly undefined.
290 if (outputVariable)
291 {
Jamie Madill334d6152015-10-22 14:00:28 -0400292 declarationStream << " " + HLSLTypeString(outputVariable->type) << " "
293 << outputVariable->name << " : " << targetSemantic
294 << static_cast<int>(layoutIndex) << ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400295
Jamie Madill334d6152015-10-22 14:00:28 -0400296 copyStream << " output." << outputVariable->name << " = "
297 << outputVariable->source << ";\n";
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000298 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400299 }
300 }
301
302 if (usesFragDepth)
303 {
Jamie Madill334d6152015-10-22 14:00:28 -0400304 declarationStream << " float gl_Depth : " << depthSemantic << ";\n";
305 copyStream << " output.gl_Depth = gl_Depth; \n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400306 }
307
Jamie Madill334d6152015-10-22 14:00:28 -0400308 declarationStream << "};\n"
309 "\n"
310 "PS_OUTPUT generateOutput()\n"
311 "{\n"
312 " PS_OUTPUT output;\n"
313 << copyStream.str() << " return output;\n"
314 "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400315
316 std::string pixelHLSL(sourceShader);
317
318 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
Jamie Madill51c47682016-10-25 15:50:14 -0400319 pixelHLSL.replace(outputInsertionPos, strlen(PIXEL_OUTPUT_STUB_STRING),
Jamie Madill334d6152015-10-22 14:00:28 -0400320 declarationStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400321
322 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500323}
324
Jamie Madill192745a2016-12-22 15:58:21 -0500325void DynamicHLSL::generateVaryingLinkHLSL(const VaryingPacking &varyingPacking,
326 const BuiltinInfo &builtins,
327 bool programUsesPointSize,
328 std::stringstream &hlslStream) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400329{
Jamie Madill9fc36822015-11-18 13:08:07 -0500330 ASSERT(builtins.dxPosition.enabled);
Jamie Madill192745a2016-12-22 15:58:21 -0500331 hlslStream << "{\n"
Jamie Madill9fc36822015-11-18 13:08:07 -0500332 << " float4 dx_Position : " << builtins.dxPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700333
Jamie Madill9fc36822015-11-18 13:08:07 -0500334 if (builtins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700335 {
Jamie Madill192745a2016-12-22 15:58:21 -0500336 hlslStream << " float4 gl_Position : " << builtins.glPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700337 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400338
Jamie Madill9fc36822015-11-18 13:08:07 -0500339 if (builtins.glFragCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400340 {
Jamie Madill192745a2016-12-22 15:58:21 -0500341 hlslStream << " float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400342 }
343
Jamie Madill9fc36822015-11-18 13:08:07 -0500344 if (builtins.glPointCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400345 {
Jamie Madill192745a2016-12-22 15:58:21 -0500346 hlslStream << " float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400347 }
348
Jamie Madill9fc36822015-11-18 13:08:07 -0500349 if (builtins.glPointSize.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400350 {
Jamie Madill192745a2016-12-22 15:58:21 -0500351 hlslStream << " float gl_PointSize : " << builtins.glPointSize.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400352 }
353
Jamie Madill192745a2016-12-22 15:58:21 -0500354 std::string varyingSemantic =
355 GetVaryingSemantic(mRenderer->getMajorShaderModel(), programUsesPointSize);
Austin Kinross8b695ee2015-03-12 13:12:20 -0700356
Jamie Madill192745a2016-12-22 15:58:21 -0500357 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
358 {
359 const auto &varying = *registerInfo.packedVarying->varying;
360 ASSERT(!varying.isStruct());
361
362 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many
363 // registers being used.
364 // For example, if there are N registers, and we have N vec3 varyings and 1 float
365 // varying, then D3D will pack them into N registers.
366 // If the float varying has the 'nointerpolation' modifier on it then we would need
367 // N + 1 registers, and D3D compilation will fail.
368
369 switch (registerInfo.packedVarying->interpolation)
370 {
371 case sh::INTERPOLATION_SMOOTH:
372 hlslStream << " ";
373 break;
374 case sh::INTERPOLATION_FLAT:
375 hlslStream << " nointerpolation ";
376 break;
377 case sh::INTERPOLATION_CENTROID:
378 hlslStream << " centroid ";
379 break;
380 default:
381 UNREACHABLE();
382 }
383
384 GLenum transposedType = gl::TransposeMatrixType(varying.type);
385 GLenum componentType = gl::VariableComponentType(transposedType);
386 int columnCount = gl::VariableColumnCount(transposedType);
387 hlslStream << HLSLComponentTypeString(componentType, columnCount);
388 unsigned int semanticIndex = registerInfo.semanticIndex;
389 hlslStream << " v" << semanticIndex << " : " << varyingSemantic << semanticIndex << ";\n";
390 }
391
392 hlslStream << "};\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400393}
394
Jamie Madill192745a2016-12-22 15:58:21 -0500395void DynamicHLSL::generateShaderLinkHLSL(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400396 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500397 const ProgramD3DMetadata &programMetadata,
Jamie Madill9fc36822015-11-18 13:08:07 -0500398 const VaryingPacking &varyingPacking,
Jamie Madill192745a2016-12-22 15:58:21 -0500399 const BuiltinVaryingsD3D &builtinsD3D,
Jamie Madill334d6152015-10-22 14:00:28 -0400400 std::string *pixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -0500401 std::string *vertexHLSL) const
Jamie Madill5f562732014-02-14 16:41:24 -0500402{
Jamie Madill334d6152015-10-22 14:00:28 -0400403 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500404
Jamie Madill91445bc2015-09-23 16:47:53 -0400405 const gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
Jamie Madill91445bc2015-09-23 16:47:53 -0400406 const gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
407 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700408 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400409
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800410 // usesViewScale() isn't supported in the D3D9 renderer
411 ASSERT(shaderModel >= 4 || !programMetadata.usesViewScale());
412
Jamie Madill334d6152015-10-22 14:00:28 -0400413 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500414 programMetadata.usesPointSize() &&
415 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400416
Jamie Madill14e95b32015-05-07 10:10:41 -0400417 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400418 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500419
Jamie Madill334d6152015-10-22 14:00:28 -0400420 std::stringstream vertexStream;
421 vertexStream << vertexShaderGL->getTranslatedSource();
422
Cooper Partine6664f02015-01-09 16:22:24 -0800423 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400424 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800425 if (useInstancedPointSpriteEmulation)
426 {
Jamie Madill334d6152015-10-22 14:00:28 -0400427 vertexStream << "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700428 << static_cast<int>(data.getCaps().minAliasedPointSize) << ".0f;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400429 << "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700430 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800431 }
432
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500433 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill51c47682016-10-25 15:50:14 -0400434 vertexStream << "\n" << std::string(VERTEX_ATTRIBUTE_STUB_STRING) << "\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500435
Jamie Madill192745a2016-12-22 15:58:21 -0500436 const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX];
437
Jamie Madill9fc36822015-11-18 13:08:07 -0500438 // Write the HLSL input/output declarations
439 vertexStream << "struct VS_OUTPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500440 generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
441 vertexStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400442 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400443 << "VS_OUTPUT main(VS_INPUT input)\n"
444 << "{\n"
445 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500446
Jamie Madill334d6152015-10-22 14:00:28 -0400447 vertexStream << "\n"
448 << " gl_main();\n"
449 << "\n"
450 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700451
Jamie Madill9fc36822015-11-18 13:08:07 -0500452 if (vertexBuiltins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700453 {
Jamie Madill334d6152015-10-22 14:00:28 -0400454 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700455 }
Jamie Madill37997142015-01-28 10:06:34 -0500456
Austin Kinross4fd18b12014-12-22 12:32:05 -0800457 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400458 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500459 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800460 vertexStream << " output.dx_Position.x = gl_Position.x;\n";
461
462 if (programMetadata.usesViewScale())
463 {
464 // This code assumes that dx_ViewScale.y = -1.0f when rendering to texture, and +1.0f
465 // when rendering to the default framebuffer. No other values are valid.
466 vertexStream << " output.dx_Position.y = dx_ViewScale.y * gl_Position.y;\n";
467 }
468 else
469 {
470 vertexStream << " output.dx_Position.y = - gl_Position.y;\n";
471 }
472
473 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400474 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500475 }
476 else
477 {
Jamie Madill334d6152015-10-22 14:00:28 -0400478 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800479 "dx_ViewAdjust.x * gl_Position.w;\n";
480
481 // If usesViewScale() is true and we're using the D3D11 renderer via Feature Level 9_*,
482 // then we need to multiply the gl_Position.y by the viewScale.
483 // usesViewScale() isn't supported when using the D3D9 renderer.
484 if (programMetadata.usesViewScale() &&
485 (shaderModel >= 4 && mRenderer->getShaderModelSuffix() != ""))
486 {
487 vertexStream << " output.dx_Position.y = dx_ViewScale.y * (gl_Position.y * "
488 "dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n";
489 }
490 else
491 {
492 vertexStream << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
493 "dx_ViewAdjust.y * gl_Position.w);\n";
494 }
495
496 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400497 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500498 }
499
Austin Kinross8b695ee2015-03-12 13:12:20 -0700500 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
Jamie Madill9fc36822015-11-18 13:08:07 -0500501 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500502 {
Jamie Madill334d6152015-10-22 14:00:28 -0400503 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500504 }
505
Jamie Madill9fc36822015-11-18 13:08:07 -0500506 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500507 {
Jamie Madill334d6152015-10-22 14:00:28 -0400508 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500509 }
510
Jamie Madill9fc36822015-11-18 13:08:07 -0500511 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500512 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500513 const auto &packedVarying = *registerInfo.packedVarying;
514 const auto &varying = *packedVarying.varying;
515 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400516
Jamie Madill55c25d02015-11-18 13:08:08 -0500517 vertexStream << " output.v" << registerInfo.semanticIndex << " = ";
518
519 if (packedVarying.isStructField())
520 {
521 vertexStream << decorateVariable(packedVarying.parentStructName) << ".";
522 }
523
524 vertexStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400525
526 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400527 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500528 WriteArrayString(vertexStream, registerInfo.varyingArrayIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500529 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400530
Jamie Madill55c25d02015-11-18 13:08:08 -0500531 if (VariableRowCount(varying.type) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400532 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500533 WriteArrayString(vertexStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400534 }
535
536 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500537 }
538
Cooper Partine6664f02015-01-09 16:22:24 -0800539 // Instanced PointSprite emulation requires additional entries to calculate
540 // the final output vertex positions of the quad that represents each sprite.
541 if (useInstancedPointSpriteEmulation)
542 {
Jamie Madill334d6152015-10-22 14:00:28 -0400543 vertexStream << "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800544 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n";
545
546 vertexStream << " output.dx_Position.x += (input.spriteVertexPos.x * gl_PointSize / "
547 "(dx_ViewCoords.x*2)) * output.dx_Position.w;";
548
549 if (programMetadata.usesViewScale())
550 {
551 // Multiply by ViewScale to invert the rendering when appropriate
552 vertexStream << " output.dx_Position.y += (-dx_ViewScale.y * "
553 "input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2)) * "
554 "output.dx_Position.w;";
555 }
556 else
557 {
558 vertexStream << " output.dx_Position.y += (input.spriteVertexPos.y * gl_PointSize / "
559 "(dx_ViewCoords.y*2)) * output.dx_Position.w;";
560 }
561
562 vertexStream
563 << " output.dx_Position.z += input.spriteVertexPos.z * output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800564
Jamie Madille39a3f02015-11-17 20:42:15 -0500565 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800566 {
Jamie Madill334d6152015-10-22 14:00:28 -0400567 vertexStream << "\n"
568 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800569 }
570 }
571
Cooper Partin7c89d242015-10-13 12:45:59 -0700572 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
573 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
574 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500575 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700576 {
577 ASSERT(!useInstancedPointSpriteEmulation);
578 vertexStream << "\n"
579 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
580 }
581
Jamie Madill334d6152015-10-22 14:00:28 -0400582 vertexStream << "\n"
583 << " return output;\n"
584 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500585
Jamie Madill192745a2016-12-22 15:58:21 -0500586 const auto &pixelBuiltins = builtinsD3D[SHADER_PIXEL];
587
Jamie Madill334d6152015-10-22 14:00:28 -0400588 std::stringstream pixelStream;
589 pixelStream << fragmentShaderGL->getTranslatedSource();
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400590 pixelStream << "struct PS_INPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500591 generateVaryingLinkHLSL(varyingPacking, pixelBuiltins, builtinsD3D.usesPointSize(),
592 pixelStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400593 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500594
Jamie Madill51c47682016-10-25 15:50:14 -0400595 pixelStream << std::string(PIXEL_OUTPUT_STUB_STRING) << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500596
Jamie Madill334d6152015-10-22 14:00:28 -0400597 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500598 {
599 if (shaderModel >= 4)
600 {
Jamie Madill334d6152015-10-22 14:00:28 -0400601 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
602 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500603 }
604 else
605 {
Jamie Madill334d6152015-10-22 14:00:28 -0400606 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
607 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500608 }
609 }
610 else
611 {
Jamie Madill334d6152015-10-22 14:00:28 -0400612 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
613 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500614 }
615
Jamie Madill9fc36822015-11-18 13:08:07 -0500616 if (pixelBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500617 {
Jamie Madill334d6152015-10-22 14:00:28 -0400618 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500619
Austin Kinross588434c2014-12-10 10:41:45 -0800620 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400621 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
622 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800623 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500624 {
Jamie Madill334d6152015-10-22 14:00:28 -0400625 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
626 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500627 }
Austin Kinross588434c2014-12-10 10:41:45 -0800628 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500629 {
Jamie Madill334d6152015-10-22 14:00:28 -0400630 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
631 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500632 }
633 else
634 {
Jamie Madill334d6152015-10-22 14:00:28 -0400635 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
636 // Renderer::setViewport()
637 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
638 "dx_ViewCoords.z;\n"
639 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
640 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500641 }
642
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800643 if (programMetadata.usesViewScale())
644 {
645 // For Feature Level 9_3 and below, we need to correct gl_FragCoord.y to account
646 // for dx_ViewScale. On Feature Level 10_0+, gl_FragCoord.y is calculated above using
647 // dx_ViewCoords and is always correct irrespective of dx_ViewScale's value.
648 // NOTE: usesViewScale() can only be true on D3D11 (i.e. Shader Model 4.0+).
649 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
650 {
651 // Some assumptions:
652 // - dx_ViewScale.y = -1.0f when rendering to texture
653 // - dx_ViewScale.y = +1.0f when rendering to the default framebuffer
654 // - gl_FragCoord.y has been set correctly above.
655 //
656 // When rendering to the backbuffer, the code inverts gl_FragCoord's y coordinate.
657 // This involves subtracting the y coordinate from the height of the area being
658 // rendered to.
659 //
660 // First we calculate the height of the area being rendered to:
661 // render_area_height = (2.0f / (1.0f - input.gl_FragCoord.y * rhw)) *
662 // gl_FragCoord.y
663 //
664 // Note that when we're rendering to default FB, we want our output to be
665 // equivalent to:
666 // "gl_FragCoord.y = render_area_height - gl_FragCoord.y"
667 //
668 // When we're rendering to a texture, we want our output to be equivalent to:
669 // "gl_FragCoord.y = gl_FragCoord.y;"
670 //
671 // If we set scale_factor = ((1.0f + dx_ViewScale.y) / 2.0f), then notice that
672 // - When rendering to default FB: scale_factor = 1.0f
673 // - When rendering to texture: scale_factor = 0.0f
674 //
675 // Therefore, we can get our desired output by setting:
676 // "gl_FragCoord.y = scale_factor * render_area_height - dx_ViewScale.y *
677 // gl_FragCoord.y"
678 //
679 // Simplifying, this becomes:
680 pixelStream
681 << " gl_FragCoord.y = (1.0f + dx_ViewScale.y) * gl_FragCoord.y /"
682 "(1.0f - input.gl_FragCoord.y * rhw) - dx_ViewScale.y * gl_FragCoord.y;\n";
683 }
684 }
685
Jamie Madill334d6152015-10-22 14:00:28 -0400686 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
687 "dx_DepthFront.y;\n"
688 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500689 }
690
Jamie Madill9fc36822015-11-18 13:08:07 -0500691 if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500692 {
Jamie Madill334d6152015-10-22 14:00:28 -0400693 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
694 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500695 }
696
Jamie Madill334d6152015-10-22 14:00:28 -0400697 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500698 {
699 if (shaderModel <= 3)
700 {
Jamie Madill334d6152015-10-22 14:00:28 -0400701 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500702 }
703 else
704 {
Jamie Madill334d6152015-10-22 14:00:28 -0400705 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500706 }
707 }
708
Jamie Madill9fc36822015-11-18 13:08:07 -0500709 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500710 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500711 const auto &packedVarying = *registerInfo.packedVarying;
712 const auto &varying = *packedVarying.varying;
713 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400714
Jamie Madillca03b352015-09-02 12:38:13 -0400715 // Don't reference VS-only transform feedback varyings in the PS.
Geoff Lang82a468a2016-06-21 17:18:25 -0400716 // TODO: Consider updating the fragment shader's varyings with a parameter signaling that a
717 // varying is only used in the vertex shader in MergeVaryings
718 if (packedVarying.vertexOnly || (!varying.staticUse && !packedVarying.isStructField()))
Jamie Madillca03b352015-09-02 12:38:13 -0400719 continue;
720
Jamie Madill55c25d02015-11-18 13:08:08 -0500721 pixelStream << " ";
722
723 if (packedVarying.isStructField())
724 {
725 pixelStream << decorateVariable(packedVarying.parentStructName) << ".";
726 }
727
728 pixelStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400729
730 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400731 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500732 WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400733 }
734
Jamie Madill55c25d02015-11-18 13:08:08 -0500735 GLenum transposedType = TransposeMatrixType(varying.type);
736 if (VariableRowCount(transposedType) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400737 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500738 WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400739 }
740
Jamie Madill9fc36822015-11-18 13:08:07 -0500741 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400742
Jamie Madill55c25d02015-11-18 13:08:08 -0500743 switch (VariableColumnCount(transposedType))
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400744 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500745 case 1:
746 pixelStream << ".x";
747 break;
748 case 2:
749 pixelStream << ".xy";
750 break;
751 case 3:
752 pixelStream << ".xyz";
753 break;
754 case 4:
755 break;
756 default:
757 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500758 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400759 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500760 }
761
Jamie Madill334d6152015-10-22 14:00:28 -0400762 pixelStream << "\n"
763 << " gl_main();\n"
764 << "\n"
765 << " return generateOutput();\n"
766 << "}\n";
767
768 *vertexHLSL = vertexStream.str();
769 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500770}
771
Xinghua Caob1239382016-12-13 15:07:05 +0800772std::string DynamicHLSL::generateComputeShaderLinkHLSL(const gl::ProgramState &programData) const
773{
774 const gl::Shader *computeShaderGL = programData.getAttachedComputeShader();
775 std::stringstream computeStream;
776 std::string translatedSource = computeShaderGL->getTranslatedSource();
777 computeStream << translatedSource;
778
779 bool usesWorkGroupID = translatedSource.find("GL_USES_WORK_GROUP_ID") != std::string::npos;
780 bool usesLocalInvocationID =
781 translatedSource.find("GL_USES_LOCAL_INVOCATION_ID") != std::string::npos;
782 bool usesGlobalInvocationID =
783 translatedSource.find("GL_USES_GLOBAL_INVOCATION_ID") != std::string::npos;
784 bool usesLocalInvocationIndex =
785 translatedSource.find("GL_USES_LOCAL_INVOCATION_INDEX") != std::string::npos;
786
787 computeStream << "\nstruct CS_INPUT\n{\n";
788 if (usesWorkGroupID)
789 {
790 computeStream << " uint3 dx_WorkGroupID : "
791 << "SV_GroupID;\n";
792 }
793
794 if (usesLocalInvocationID)
795 {
796 computeStream << " uint3 dx_LocalInvocationID : "
797 << "SV_GroupThreadID;\n";
798 }
799
800 if (usesGlobalInvocationID)
801 {
802 computeStream << " uint3 dx_GlobalInvocationID : "
803 << "SV_DispatchThreadID;\n";
804 }
805
806 if (usesLocalInvocationIndex)
807 {
808 computeStream << " uint dx_LocalInvocationIndex : "
809 << "SV_GroupIndex;\n";
810 }
811
812 computeStream << "};\n\n";
813
814 const sh::WorkGroupSize &localSize = computeShaderGL->getWorkGroupSize();
815 computeStream << "[numthreads(" << localSize[0] << ", " << localSize[1] << ", " << localSize[2]
816 << ")]\n";
817
818 computeStream << "void main(CS_INPUT input)\n"
819 << "{\n";
820
821 if (usesWorkGroupID)
822 {
823 computeStream << " gl_WorkGroupID = input.dx_WorkGroupID;\n";
824 }
825 if (usesLocalInvocationID)
826 {
827 computeStream << " gl_LocalInvocationID = input.dx_LocalInvocationID;\n";
828 }
829 if (usesGlobalInvocationID)
830 {
831 computeStream << " gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n";
832 }
833 if (usesLocalInvocationIndex)
834 {
835 computeStream << " gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n";
836 }
837
838 computeStream << "\n"
839 << " gl_main();\n"
840 << "}\n";
841
842 return computeStream.str();
843}
844
Jamie Madill192745a2016-12-22 15:58:21 -0500845std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking,
846 const BuiltinVaryingsD3D &builtinsD3D) const
Jamie Madill5f562732014-02-14 16:41:24 -0500847{
Jamie Madill9fc36822015-11-18 13:08:07 -0500848 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400849
Jamie Madill4e31ad52015-10-29 10:32:57 -0400850 std::stringstream preambleStream;
851
Jamie Madill192745a2016-12-22 15:58:21 -0500852 const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX];
Jamie Madill9fc36822015-11-18 13:08:07 -0500853
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400854 preambleStream << "struct GS_INPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500855 generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
856 preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400857 preambleStream << "\n"
858 << "struct GS_OUTPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500859 generateVaryingLinkHLSL(varyingPacking, builtinsD3D[SHADER_GEOMETRY],
860 builtinsD3D.usesPointSize(), preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400861 preambleStream
862 << "\n"
863 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
864 << "{\n"
865 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400866
Jamie Madill192745a2016-12-22 15:58:21 -0500867 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400868 {
869 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
870 }
871
Jamie Madill9fc36822015-11-18 13:08:07 -0500872 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400873 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500874 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill55c25d02015-11-18 13:08:08 -0500875 if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400876 {
877 preambleStream << "flat";
878 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500879 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400880 }
881
Jamie Madill192745a2016-12-22 15:58:21 -0500882 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400883 {
884 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
885 }
886
887 // Only write the dx_Position if we aren't using point sprites
888 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
889 << " output.dx_Position = input.dx_Position;\n"
890 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
891 << "}\n";
892
893 return preambleStream.str();
894}
895
896std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
Jamie Madill9082b982016-04-27 15:21:51 -0400897 const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400898 const gl::ProgramState &programData,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800899 const bool useViewScale,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400900 const std::string &preambleString) const
901{
902 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500903
Jamie Madill334d6152015-10-22 14:00:28 -0400904 std::stringstream shaderStream;
905
Jamie Madill4e31ad52015-10-29 10:32:57 -0400906 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
907 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500908
Jamie Madill76f8fa62015-10-29 10:32:56 -0400909 const char *inputPT = nullptr;
910 const char *outputPT = nullptr;
911 int inputSize = 0;
912 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500913
Jamie Madill76f8fa62015-10-29 10:32:56 -0400914 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500915 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400916 case PRIMITIVE_POINTS:
917 inputPT = "point";
918 outputPT = "Triangle";
919 inputSize = 1;
920 maxVertexOutput = 4;
921 break;
922
923 case PRIMITIVE_LINES:
924 case PRIMITIVE_LINE_STRIP:
925 case PRIMITIVE_LINE_LOOP:
926 inputPT = "line";
927 outputPT = "Line";
928 inputSize = 2;
929 maxVertexOutput = 2;
930 break;
931
932 case PRIMITIVE_TRIANGLES:
933 case PRIMITIVE_TRIANGLE_STRIP:
934 case PRIMITIVE_TRIANGLE_FAN:
935 inputPT = "triangle";
936 outputPT = "Triangle";
937 inputSize = 3;
938 maxVertexOutput = 3;
939 break;
940
941 default:
942 UNREACHABLE();
943 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500944 }
945
Jamie Madill76f8fa62015-10-29 10:32:56 -0400946 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500947 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400948 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
949 "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800950 "uniform float4 dx_ViewCoords : register(c1);\n";
951
952 if (useViewScale)
953 {
954 shaderStream << "uniform float2 dx_ViewScale : register(c3);\n";
955 }
956
957 shaderStream << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400958 "static float2 pointSpriteCorners[] = \n"
959 "{\n"
960 " float2( 0.5f, -0.5f),\n"
961 " float2( 0.5f, 0.5f),\n"
962 " float2(-0.5f, -0.5f),\n"
963 " float2(-0.5f, 0.5f)\n"
964 "};\n"
965 "\n"
966 "static float2 pointSpriteTexcoords[] = \n"
967 "{\n"
968 " float2(1.0f, 1.0f),\n"
969 " float2(1.0f, 0.0f),\n"
970 " float2(0.0f, 1.0f),\n"
971 " float2(0.0f, 0.0f)\n"
972 "};\n"
973 "\n"
974 "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700975 << static_cast<int>(data.getCaps().minAliasedPointSize)
Jamie Madill76f8fa62015-10-29 10:32:56 -0400976 << ".0f;\n"
977 "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700978 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400979 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500980 }
981
Jamie Madill4e31ad52015-10-29 10:32:57 -0400982 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400983 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400984 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
985
986 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
987 {
988 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
989 }
990
991 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400992 << "{\n"
993 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500994
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400995 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
996 {
997 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
998 }
999 else
1000 {
1001 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
1002 }
1003
Jamie Madill4e31ad52015-10-29 10:32:57 -04001004 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -05001005 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001006 shaderStream << " copyVertex(output, input[" << vertexIndex
1007 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001008
1009 if (!pointSprites)
1010 {
1011 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001012 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001013 }
1014 }
1015
1016 if (pointSprites)
1017 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001018 shaderStream << "\n"
1019 " float4 dx_Position = input[0].dx_Position;\n"
1020 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
1021 "maxPointSize);\n"
1022 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
1023 "dx_ViewCoords.y) * dx_Position.w;\n";
1024
1025 for (int corner = 0; corner < 4; corner++)
1026 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001027 if (useViewScale)
1028 {
1029 shaderStream << " \n"
1030 " output.dx_Position = dx_Position + float4(1.0f, "
1031 "-dx_ViewScale.y, 1.0f, 1.0f)"
1032 " * float4(pointSpriteCorners["
1033 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1034 }
1035 else
1036 {
1037 shaderStream << "\n"
1038 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
1039 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1040 }
Jamie Madill76f8fa62015-10-29 10:32:56 -04001041
1042 if (usesPointCoord)
1043 {
1044 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
1045 << "];\n";
1046 }
1047
1048 shaderStream << " outStream.Append(output);\n";
1049 }
Jamie Madill5f562732014-02-14 16:41:24 -05001050 }
1051
Jamie Madill334d6152015-10-22 14:00:28 -04001052 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001053 " outStream.RestartStrip();\n"
1054 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001055
Jamie Madill334d6152015-10-22 14:00:28 -04001056 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -05001057}
1058
1059// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001060std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001061{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001062 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001063 {
1064 return "_" + name;
1065 }
1066
1067 return name;
1068}
1069
Jamie Madill334d6152015-10-22 14:00:28 -04001070std::string DynamicHLSL::generateAttributeConversionHLSL(
1071 gl::VertexFormatType vertexFormatType,
1072 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001073{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001074 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -04001075 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001076
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001077 // Matrix
1078 if (IsMatrixType(shaderAttrib.type))
1079 {
Jamie Madill8664b062014-02-14 16:41:29 -05001080 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001081 }
1082
Jamie Madillf2575982014-06-25 16:04:54 -04001083 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001084 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001085
Jamie Madill8664b062014-02-14 16:41:29 -05001086 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -04001087 bool requiresTypeConversion =
1088 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001089
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001090 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001091 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001092 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001093 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001094 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001095 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001096
1097 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001098 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001099}
Jamie Madille39a3f02015-11-17 20:42:15 -05001100
Jamie Madill9082b982016-04-27 15:21:51 -04001101void DynamicHLSL::getPixelShaderOutputKey(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001102 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -05001103 const ProgramD3DMetadata &metadata,
1104 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1105{
1106 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1107 // - with a 3.0 context, the output color is copied to channel 0
1108 // - with a 2.0 context, the output color is broadcast to all channels
1109 bool broadcast = metadata.usesBroadcast(data);
1110 const unsigned int numRenderTargets =
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001111 (broadcast || metadata.usesMultipleFragmentOuts() ? data.getCaps().maxDrawBuffers : 1);
Jamie Madille39a3f02015-11-17 20:42:15 -05001112
1113 if (metadata.getMajorShaderVersion() < 300)
1114 {
1115 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1116 renderTargetIndex++)
1117 {
1118 PixelShaderOutputVariable outputKeyVariable;
1119 outputKeyVariable.type = GL_FLOAT_VEC4;
1120 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1121 outputKeyVariable.source =
1122 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1123 outputKeyVariable.outputIndex = renderTargetIndex;
1124
1125 outPixelShaderKey->push_back(outputKeyVariable);
1126 }
1127 }
1128 else
1129 {
1130 const auto &shaderOutputVars =
1131 metadata.getFragmentShader()->getData().getActiveOutputVariables();
1132
jchen1015015f72017-03-16 13:54:21 +08001133 for (auto outputPair : programData.getOutputLocations())
Jamie Madille39a3f02015-11-17 20:42:15 -05001134 {
1135 const VariableLocation &outputLocation = outputPair.second;
1136 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1137 const std::string &variableName = "out_" + outputLocation.name;
1138 const std::string &elementString =
1139 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
1140
1141 ASSERT(outputVariable.staticUse);
1142
1143 PixelShaderOutputVariable outputKeyVariable;
1144 outputKeyVariable.type = outputVariable.type;
1145 outputKeyVariable.name = variableName + elementString;
1146 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
1147 outputKeyVariable.outputIndex = outputPair.first;
1148
1149 outPixelShaderKey->push_back(outputKeyVariable);
1150 }
1151 }
1152}
1153
Jamie Madill192745a2016-12-22 15:58:21 -05001154// BuiltinVarying Implementation.
1155BuiltinVarying::BuiltinVarying() : enabled(false), index(0), systemValue(false)
1156{
1157}
1158
1159std::string BuiltinVarying::str() const
1160{
1161 return (systemValue ? semantic : (semantic + Str(index)));
1162}
1163
1164void BuiltinVarying::enableSystem(const std::string &systemValueSemantic)
1165{
1166 enabled = true;
1167 semantic = systemValueSemantic;
1168 systemValue = true;
1169}
1170
1171void BuiltinVarying::enable(const std::string &semanticVal, unsigned int indexVal)
1172{
1173 enabled = true;
1174 semantic = semanticVal;
1175 index = indexVal;
1176}
1177
1178// BuiltinVaryingsD3D Implementation.
1179BuiltinVaryingsD3D::BuiltinVaryingsD3D(const ProgramD3DMetadata &metadata,
1180 const VaryingPacking &packing)
1181{
1182 updateBuiltins(SHADER_VERTEX, metadata, packing);
1183 updateBuiltins(SHADER_PIXEL, metadata, packing);
1184 if (metadata.getRendererMajorShaderModel() >= 4)
1185 {
1186 updateBuiltins(SHADER_GEOMETRY, metadata, packing);
1187 }
1188}
1189
1190void BuiltinVaryingsD3D::updateBuiltins(ShaderType shaderType,
1191 const ProgramD3DMetadata &metadata,
1192 const VaryingPacking &packing)
1193{
1194 const std::string &userSemantic = GetVaryingSemantic(metadata.getRendererMajorShaderModel(),
1195 metadata.usesSystemValuePointSize());
1196
1197 unsigned int reservedSemanticIndex = packing.getMaxSemanticIndex();
1198
1199 BuiltinInfo *builtins = &mBuiltinInfo[shaderType];
1200
1201 if (metadata.getRendererMajorShaderModel() >= 4)
1202 {
1203 builtins->dxPosition.enableSystem("SV_Position");
1204 }
1205 else if (shaderType == SHADER_PIXEL)
1206 {
1207 builtins->dxPosition.enableSystem("VPOS");
1208 }
1209 else
1210 {
1211 builtins->dxPosition.enableSystem("POSITION");
1212 }
1213
1214 if (metadata.usesTransformFeedbackGLPosition())
1215 {
1216 builtins->glPosition.enable(userSemantic, reservedSemanticIndex++);
1217 }
1218
1219 if (metadata.usesFragCoord())
1220 {
1221 builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++);
1222 }
1223
1224 if (shaderType == SHADER_VERTEX ? metadata.addsPointCoordToVertexShader()
1225 : metadata.usesPointCoord())
1226 {
1227 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
1228 // In D3D11 we manually compute gl_PointCoord in the GS.
1229 if (metadata.getRendererMajorShaderModel() >= 4)
1230 {
1231 builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++);
1232 }
1233 else
1234 {
1235 builtins->glPointCoord.enable("TEXCOORD", 0);
1236 }
1237 }
1238
1239 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
1240 if (metadata.usesSystemValuePointSize() &&
1241 (shaderType != SHADER_PIXEL || metadata.getRendererMajorShaderModel() >= 4))
1242 {
1243 builtins->glPointSize.enableSystem("PSIZE");
1244 }
1245}
1246
Jamie Madill334d6152015-10-22 14:00:28 -04001247} // namespace rx