blob: 2b8e2ec45d61a338cd8b3a968ead2fddfb200d37 [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 Madill3f2e61d2014-09-05 10:38:05 -0400270 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
271 {
272 GLenum binding = outputLayout[layoutIndex];
273
274 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400275 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400276 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
277
Jamie Madill334d6152015-10-22 14:00:28 -0400278 const PixelShaderOutputVariable *outputVariable =
279 FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400280
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000281 // OpenGL ES 3.0 spec $4.2.1
Jamie Madill334d6152015-10-22 14:00:28 -0400282 // If [...] not all user-defined output variables are written, the values of fragment
283 // colors
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000284 // corresponding to unwritten variables are similarly undefined.
285 if (outputVariable)
286 {
Jamie Madill334d6152015-10-22 14:00:28 -0400287 declarationStream << " " + HLSLTypeString(outputVariable->type) << " "
288 << outputVariable->name << " : " << targetSemantic
289 << static_cast<int>(layoutIndex) << ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400290
Jamie Madill334d6152015-10-22 14:00:28 -0400291 copyStream << " output." << outputVariable->name << " = "
292 << outputVariable->source << ";\n";
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000293 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400294 }
295 }
296
297 if (usesFragDepth)
298 {
Jamie Madill334d6152015-10-22 14:00:28 -0400299 declarationStream << " float gl_Depth : " << depthSemantic << ";\n";
300 copyStream << " output.gl_Depth = gl_Depth; \n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400301 }
302
Jamie Madill334d6152015-10-22 14:00:28 -0400303 declarationStream << "};\n"
304 "\n"
305 "PS_OUTPUT generateOutput()\n"
306 "{\n"
307 " PS_OUTPUT output;\n"
308 << copyStream.str() << " return output;\n"
309 "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400310
311 std::string pixelHLSL(sourceShader);
312
313 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
Jamie Madill51c47682016-10-25 15:50:14 -0400314 pixelHLSL.replace(outputInsertionPos, strlen(PIXEL_OUTPUT_STUB_STRING),
Jamie Madill334d6152015-10-22 14:00:28 -0400315 declarationStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400316
317 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500318}
319
Jamie Madill192745a2016-12-22 15:58:21 -0500320void DynamicHLSL::generateVaryingLinkHLSL(const VaryingPacking &varyingPacking,
321 const BuiltinInfo &builtins,
322 bool programUsesPointSize,
323 std::stringstream &hlslStream) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400324{
Jamie Madill9fc36822015-11-18 13:08:07 -0500325 ASSERT(builtins.dxPosition.enabled);
Jamie Madill192745a2016-12-22 15:58:21 -0500326 hlslStream << "{\n"
Jamie Madill9fc36822015-11-18 13:08:07 -0500327 << " float4 dx_Position : " << builtins.dxPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700328
Jamie Madill9fc36822015-11-18 13:08:07 -0500329 if (builtins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700330 {
Jamie Madill192745a2016-12-22 15:58:21 -0500331 hlslStream << " float4 gl_Position : " << builtins.glPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700332 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400333
Jamie Madill9fc36822015-11-18 13:08:07 -0500334 if (builtins.glFragCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400335 {
Jamie Madill192745a2016-12-22 15:58:21 -0500336 hlslStream << " float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400337 }
338
Jamie Madill9fc36822015-11-18 13:08:07 -0500339 if (builtins.glPointCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400340 {
Jamie Madill192745a2016-12-22 15:58:21 -0500341 hlslStream << " float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400342 }
343
Jamie Madill9fc36822015-11-18 13:08:07 -0500344 if (builtins.glPointSize.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400345 {
Jamie Madill192745a2016-12-22 15:58:21 -0500346 hlslStream << " float gl_PointSize : " << builtins.glPointSize.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400347 }
348
Jamie Madill192745a2016-12-22 15:58:21 -0500349 std::string varyingSemantic =
350 GetVaryingSemantic(mRenderer->getMajorShaderModel(), programUsesPointSize);
Austin Kinross8b695ee2015-03-12 13:12:20 -0700351
Jamie Madill192745a2016-12-22 15:58:21 -0500352 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
353 {
354 const auto &varying = *registerInfo.packedVarying->varying;
355 ASSERT(!varying.isStruct());
356
357 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many
358 // registers being used.
359 // For example, if there are N registers, and we have N vec3 varyings and 1 float
360 // varying, then D3D will pack them into N registers.
361 // If the float varying has the 'nointerpolation' modifier on it then we would need
362 // N + 1 registers, and D3D compilation will fail.
363
364 switch (registerInfo.packedVarying->interpolation)
365 {
366 case sh::INTERPOLATION_SMOOTH:
367 hlslStream << " ";
368 break;
369 case sh::INTERPOLATION_FLAT:
370 hlslStream << " nointerpolation ";
371 break;
372 case sh::INTERPOLATION_CENTROID:
373 hlslStream << " centroid ";
374 break;
375 default:
376 UNREACHABLE();
377 }
378
379 GLenum transposedType = gl::TransposeMatrixType(varying.type);
380 GLenum componentType = gl::VariableComponentType(transposedType);
381 int columnCount = gl::VariableColumnCount(transposedType);
382 hlslStream << HLSLComponentTypeString(componentType, columnCount);
383 unsigned int semanticIndex = registerInfo.semanticIndex;
384 hlslStream << " v" << semanticIndex << " : " << varyingSemantic << semanticIndex << ";\n";
385 }
386
387 hlslStream << "};\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400388}
389
Jamie Madill192745a2016-12-22 15:58:21 -0500390void DynamicHLSL::generateShaderLinkHLSL(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400391 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500392 const ProgramD3DMetadata &programMetadata,
Jamie Madill9fc36822015-11-18 13:08:07 -0500393 const VaryingPacking &varyingPacking,
Jamie Madill192745a2016-12-22 15:58:21 -0500394 const BuiltinVaryingsD3D &builtinsD3D,
Jamie Madill334d6152015-10-22 14:00:28 -0400395 std::string *pixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -0500396 std::string *vertexHLSL) const
Jamie Madill5f562732014-02-14 16:41:24 -0500397{
Jamie Madill334d6152015-10-22 14:00:28 -0400398 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500399
Jamie Madill91445bc2015-09-23 16:47:53 -0400400 const gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
Jamie Madill91445bc2015-09-23 16:47:53 -0400401 const gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
402 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700403 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400404
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800405 // usesViewScale() isn't supported in the D3D9 renderer
406 ASSERT(shaderModel >= 4 || !programMetadata.usesViewScale());
407
Jamie Madill334d6152015-10-22 14:00:28 -0400408 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500409 programMetadata.usesPointSize() &&
410 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400411
Jamie Madill14e95b32015-05-07 10:10:41 -0400412 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400413 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500414
Jamie Madill334d6152015-10-22 14:00:28 -0400415 std::stringstream vertexStream;
416 vertexStream << vertexShaderGL->getTranslatedSource();
417
Cooper Partine6664f02015-01-09 16:22:24 -0800418 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400419 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800420 if (useInstancedPointSpriteEmulation)
421 {
Jamie Madill334d6152015-10-22 14:00:28 -0400422 vertexStream << "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700423 << static_cast<int>(data.getCaps().minAliasedPointSize) << ".0f;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400424 << "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700425 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800426 }
427
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500428 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill51c47682016-10-25 15:50:14 -0400429 vertexStream << "\n" << std::string(VERTEX_ATTRIBUTE_STUB_STRING) << "\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500430
Jamie Madill192745a2016-12-22 15:58:21 -0500431 const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX];
432
Jamie Madill9fc36822015-11-18 13:08:07 -0500433 // Write the HLSL input/output declarations
434 vertexStream << "struct VS_OUTPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500435 generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
436 vertexStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400437 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400438 << "VS_OUTPUT main(VS_INPUT input)\n"
439 << "{\n"
440 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500441
Jamie Madill334d6152015-10-22 14:00:28 -0400442 vertexStream << "\n"
443 << " gl_main();\n"
444 << "\n"
445 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700446
Jamie Madill9fc36822015-11-18 13:08:07 -0500447 if (vertexBuiltins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700448 {
Jamie Madill334d6152015-10-22 14:00:28 -0400449 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700450 }
Jamie Madill37997142015-01-28 10:06:34 -0500451
Austin Kinross4fd18b12014-12-22 12:32:05 -0800452 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400453 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500454 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800455 vertexStream << " output.dx_Position.x = gl_Position.x;\n";
456
457 if (programMetadata.usesViewScale())
458 {
459 // This code assumes that dx_ViewScale.y = -1.0f when rendering to texture, and +1.0f
460 // when rendering to the default framebuffer. No other values are valid.
461 vertexStream << " output.dx_Position.y = dx_ViewScale.y * gl_Position.y;\n";
462 }
463 else
464 {
465 vertexStream << " output.dx_Position.y = - gl_Position.y;\n";
466 }
467
468 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400469 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500470 }
471 else
472 {
Jamie Madill334d6152015-10-22 14:00:28 -0400473 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800474 "dx_ViewAdjust.x * gl_Position.w;\n";
475
476 // If usesViewScale() is true and we're using the D3D11 renderer via Feature Level 9_*,
477 // then we need to multiply the gl_Position.y by the viewScale.
478 // usesViewScale() isn't supported when using the D3D9 renderer.
479 if (programMetadata.usesViewScale() &&
480 (shaderModel >= 4 && mRenderer->getShaderModelSuffix() != ""))
481 {
482 vertexStream << " output.dx_Position.y = dx_ViewScale.y * (gl_Position.y * "
483 "dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n";
484 }
485 else
486 {
487 vertexStream << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
488 "dx_ViewAdjust.y * gl_Position.w);\n";
489 }
490
491 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400492 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500493 }
494
Austin Kinross8b695ee2015-03-12 13:12:20 -0700495 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
Jamie Madill9fc36822015-11-18 13:08:07 -0500496 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500497 {
Jamie Madill334d6152015-10-22 14:00:28 -0400498 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500499 }
500
Jamie Madill9fc36822015-11-18 13:08:07 -0500501 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500502 {
Jamie Madill334d6152015-10-22 14:00:28 -0400503 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500504 }
505
Jamie Madill9fc36822015-11-18 13:08:07 -0500506 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500507 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500508 const auto &packedVarying = *registerInfo.packedVarying;
509 const auto &varying = *packedVarying.varying;
510 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400511
Jamie Madill55c25d02015-11-18 13:08:08 -0500512 vertexStream << " output.v" << registerInfo.semanticIndex << " = ";
513
514 if (packedVarying.isStructField())
515 {
516 vertexStream << decorateVariable(packedVarying.parentStructName) << ".";
517 }
518
519 vertexStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400520
521 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400522 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500523 WriteArrayString(vertexStream, registerInfo.varyingArrayIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500524 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400525
Jamie Madill55c25d02015-11-18 13:08:08 -0500526 if (VariableRowCount(varying.type) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400527 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500528 WriteArrayString(vertexStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400529 }
530
531 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500532 }
533
Cooper Partine6664f02015-01-09 16:22:24 -0800534 // Instanced PointSprite emulation requires additional entries to calculate
535 // the final output vertex positions of the quad that represents each sprite.
536 if (useInstancedPointSpriteEmulation)
537 {
Jamie Madill334d6152015-10-22 14:00:28 -0400538 vertexStream << "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800539 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n";
540
541 vertexStream << " output.dx_Position.x += (input.spriteVertexPos.x * gl_PointSize / "
542 "(dx_ViewCoords.x*2)) * output.dx_Position.w;";
543
544 if (programMetadata.usesViewScale())
545 {
546 // Multiply by ViewScale to invert the rendering when appropriate
547 vertexStream << " output.dx_Position.y += (-dx_ViewScale.y * "
548 "input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2)) * "
549 "output.dx_Position.w;";
550 }
551 else
552 {
553 vertexStream << " output.dx_Position.y += (input.spriteVertexPos.y * gl_PointSize / "
554 "(dx_ViewCoords.y*2)) * output.dx_Position.w;";
555 }
556
557 vertexStream
558 << " output.dx_Position.z += input.spriteVertexPos.z * output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800559
Jamie Madille39a3f02015-11-17 20:42:15 -0500560 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800561 {
Jamie Madill334d6152015-10-22 14:00:28 -0400562 vertexStream << "\n"
563 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800564 }
565 }
566
Cooper Partin7c89d242015-10-13 12:45:59 -0700567 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
568 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
569 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500570 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700571 {
572 ASSERT(!useInstancedPointSpriteEmulation);
573 vertexStream << "\n"
574 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
575 }
576
Jamie Madill334d6152015-10-22 14:00:28 -0400577 vertexStream << "\n"
578 << " return output;\n"
579 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500580
Jamie Madill192745a2016-12-22 15:58:21 -0500581 const auto &pixelBuiltins = builtinsD3D[SHADER_PIXEL];
582
Jamie Madill334d6152015-10-22 14:00:28 -0400583 std::stringstream pixelStream;
584 pixelStream << fragmentShaderGL->getTranslatedSource();
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400585 pixelStream << "struct PS_INPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500586 generateVaryingLinkHLSL(varyingPacking, pixelBuiltins, builtinsD3D.usesPointSize(),
587 pixelStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400588 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500589
Jamie Madill51c47682016-10-25 15:50:14 -0400590 pixelStream << std::string(PIXEL_OUTPUT_STUB_STRING) << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500591
Jamie Madill334d6152015-10-22 14:00:28 -0400592 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500593 {
594 if (shaderModel >= 4)
595 {
Jamie Madill334d6152015-10-22 14:00:28 -0400596 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
597 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500598 }
599 else
600 {
Jamie Madill334d6152015-10-22 14:00:28 -0400601 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
602 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500603 }
604 }
605 else
606 {
Jamie Madill334d6152015-10-22 14:00:28 -0400607 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
608 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500609 }
610
Jamie Madill9fc36822015-11-18 13:08:07 -0500611 if (pixelBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500612 {
Jamie Madill334d6152015-10-22 14:00:28 -0400613 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500614
Austin Kinross588434c2014-12-10 10:41:45 -0800615 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400616 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
617 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800618 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500619 {
Jamie Madill334d6152015-10-22 14:00:28 -0400620 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
621 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500622 }
Austin Kinross588434c2014-12-10 10:41:45 -0800623 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500624 {
Jamie Madill334d6152015-10-22 14:00:28 -0400625 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
626 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500627 }
628 else
629 {
Jamie Madill334d6152015-10-22 14:00:28 -0400630 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
631 // Renderer::setViewport()
632 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
633 "dx_ViewCoords.z;\n"
634 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
635 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500636 }
637
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800638 if (programMetadata.usesViewScale())
639 {
640 // For Feature Level 9_3 and below, we need to correct gl_FragCoord.y to account
641 // for dx_ViewScale. On Feature Level 10_0+, gl_FragCoord.y is calculated above using
642 // dx_ViewCoords and is always correct irrespective of dx_ViewScale's value.
643 // NOTE: usesViewScale() can only be true on D3D11 (i.e. Shader Model 4.0+).
644 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
645 {
646 // Some assumptions:
647 // - dx_ViewScale.y = -1.0f when rendering to texture
648 // - dx_ViewScale.y = +1.0f when rendering to the default framebuffer
649 // - gl_FragCoord.y has been set correctly above.
650 //
651 // When rendering to the backbuffer, the code inverts gl_FragCoord's y coordinate.
652 // This involves subtracting the y coordinate from the height of the area being
653 // rendered to.
654 //
655 // First we calculate the height of the area being rendered to:
656 // render_area_height = (2.0f / (1.0f - input.gl_FragCoord.y * rhw)) *
657 // gl_FragCoord.y
658 //
659 // Note that when we're rendering to default FB, we want our output to be
660 // equivalent to:
661 // "gl_FragCoord.y = render_area_height - gl_FragCoord.y"
662 //
663 // When we're rendering to a texture, we want our output to be equivalent to:
664 // "gl_FragCoord.y = gl_FragCoord.y;"
665 //
666 // If we set scale_factor = ((1.0f + dx_ViewScale.y) / 2.0f), then notice that
667 // - When rendering to default FB: scale_factor = 1.0f
668 // - When rendering to texture: scale_factor = 0.0f
669 //
670 // Therefore, we can get our desired output by setting:
671 // "gl_FragCoord.y = scale_factor * render_area_height - dx_ViewScale.y *
672 // gl_FragCoord.y"
673 //
674 // Simplifying, this becomes:
675 pixelStream
676 << " gl_FragCoord.y = (1.0f + dx_ViewScale.y) * gl_FragCoord.y /"
677 "(1.0f - input.gl_FragCoord.y * rhw) - dx_ViewScale.y * gl_FragCoord.y;\n";
678 }
679 }
680
Jamie Madill334d6152015-10-22 14:00:28 -0400681 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
682 "dx_DepthFront.y;\n"
683 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500684 }
685
Jamie Madill9fc36822015-11-18 13:08:07 -0500686 if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500687 {
Jamie Madill334d6152015-10-22 14:00:28 -0400688 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
689 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500690 }
691
Jamie Madill334d6152015-10-22 14:00:28 -0400692 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500693 {
694 if (shaderModel <= 3)
695 {
Jamie Madill334d6152015-10-22 14:00:28 -0400696 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500697 }
698 else
699 {
Jamie Madill334d6152015-10-22 14:00:28 -0400700 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500701 }
702 }
703
Jamie Madill9fc36822015-11-18 13:08:07 -0500704 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500705 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500706 const auto &packedVarying = *registerInfo.packedVarying;
707 const auto &varying = *packedVarying.varying;
708 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400709
Jamie Madillca03b352015-09-02 12:38:13 -0400710 // Don't reference VS-only transform feedback varyings in the PS.
Geoff Lang82a468a2016-06-21 17:18:25 -0400711 // TODO: Consider updating the fragment shader's varyings with a parameter signaling that a
712 // varying is only used in the vertex shader in MergeVaryings
713 if (packedVarying.vertexOnly || (!varying.staticUse && !packedVarying.isStructField()))
Jamie Madillca03b352015-09-02 12:38:13 -0400714 continue;
715
Jamie Madill55c25d02015-11-18 13:08:08 -0500716 pixelStream << " ";
717
718 if (packedVarying.isStructField())
719 {
720 pixelStream << decorateVariable(packedVarying.parentStructName) << ".";
721 }
722
723 pixelStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400724
725 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400726 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500727 WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400728 }
729
Jamie Madill55c25d02015-11-18 13:08:08 -0500730 GLenum transposedType = TransposeMatrixType(varying.type);
731 if (VariableRowCount(transposedType) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400732 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500733 WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400734 }
735
Jamie Madill9fc36822015-11-18 13:08:07 -0500736 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400737
Jamie Madill55c25d02015-11-18 13:08:08 -0500738 switch (VariableColumnCount(transposedType))
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400739 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500740 case 1:
741 pixelStream << ".x";
742 break;
743 case 2:
744 pixelStream << ".xy";
745 break;
746 case 3:
747 pixelStream << ".xyz";
748 break;
749 case 4:
750 break;
751 default:
752 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500753 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400754 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500755 }
756
Jamie Madill334d6152015-10-22 14:00:28 -0400757 pixelStream << "\n"
758 << " gl_main();\n"
759 << "\n"
760 << " return generateOutput();\n"
761 << "}\n";
762
763 *vertexHLSL = vertexStream.str();
764 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500765}
766
Xinghua Caob1239382016-12-13 15:07:05 +0800767std::string DynamicHLSL::generateComputeShaderLinkHLSL(const gl::ProgramState &programData) const
768{
769 const gl::Shader *computeShaderGL = programData.getAttachedComputeShader();
770 std::stringstream computeStream;
771 std::string translatedSource = computeShaderGL->getTranslatedSource();
772 computeStream << translatedSource;
773
774 bool usesWorkGroupID = translatedSource.find("GL_USES_WORK_GROUP_ID") != std::string::npos;
775 bool usesLocalInvocationID =
776 translatedSource.find("GL_USES_LOCAL_INVOCATION_ID") != std::string::npos;
777 bool usesGlobalInvocationID =
778 translatedSource.find("GL_USES_GLOBAL_INVOCATION_ID") != std::string::npos;
779 bool usesLocalInvocationIndex =
780 translatedSource.find("GL_USES_LOCAL_INVOCATION_INDEX") != std::string::npos;
781
782 computeStream << "\nstruct CS_INPUT\n{\n";
783 if (usesWorkGroupID)
784 {
785 computeStream << " uint3 dx_WorkGroupID : "
786 << "SV_GroupID;\n";
787 }
788
789 if (usesLocalInvocationID)
790 {
791 computeStream << " uint3 dx_LocalInvocationID : "
792 << "SV_GroupThreadID;\n";
793 }
794
795 if (usesGlobalInvocationID)
796 {
797 computeStream << " uint3 dx_GlobalInvocationID : "
798 << "SV_DispatchThreadID;\n";
799 }
800
801 if (usesLocalInvocationIndex)
802 {
803 computeStream << " uint dx_LocalInvocationIndex : "
804 << "SV_GroupIndex;\n";
805 }
806
807 computeStream << "};\n\n";
808
809 const sh::WorkGroupSize &localSize = computeShaderGL->getWorkGroupSize();
810 computeStream << "[numthreads(" << localSize[0] << ", " << localSize[1] << ", " << localSize[2]
811 << ")]\n";
812
813 computeStream << "void main(CS_INPUT input)\n"
814 << "{\n";
815
816 if (usesWorkGroupID)
817 {
818 computeStream << " gl_WorkGroupID = input.dx_WorkGroupID;\n";
819 }
820 if (usesLocalInvocationID)
821 {
822 computeStream << " gl_LocalInvocationID = input.dx_LocalInvocationID;\n";
823 }
824 if (usesGlobalInvocationID)
825 {
826 computeStream << " gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n";
827 }
828 if (usesLocalInvocationIndex)
829 {
830 computeStream << " gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n";
831 }
832
833 computeStream << "\n"
834 << " gl_main();\n"
835 << "}\n";
836
837 return computeStream.str();
838}
839
Jamie Madill192745a2016-12-22 15:58:21 -0500840std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking,
841 const BuiltinVaryingsD3D &builtinsD3D) const
Jamie Madill5f562732014-02-14 16:41:24 -0500842{
Jamie Madill9fc36822015-11-18 13:08:07 -0500843 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400844
Jamie Madill4e31ad52015-10-29 10:32:57 -0400845 std::stringstream preambleStream;
846
Jamie Madill192745a2016-12-22 15:58:21 -0500847 const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX];
Jamie Madill9fc36822015-11-18 13:08:07 -0500848
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400849 preambleStream << "struct GS_INPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500850 generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
851 preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400852 preambleStream << "\n"
853 << "struct GS_OUTPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500854 generateVaryingLinkHLSL(varyingPacking, builtinsD3D[SHADER_GEOMETRY],
855 builtinsD3D.usesPointSize(), preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400856 preambleStream
857 << "\n"
858 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
859 << "{\n"
860 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400861
Jamie Madill192745a2016-12-22 15:58:21 -0500862 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400863 {
864 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
865 }
866
Jamie Madill9fc36822015-11-18 13:08:07 -0500867 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400868 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500869 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill55c25d02015-11-18 13:08:08 -0500870 if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400871 {
872 preambleStream << "flat";
873 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500874 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400875 }
876
Jamie Madill192745a2016-12-22 15:58:21 -0500877 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400878 {
879 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
880 }
881
882 // Only write the dx_Position if we aren't using point sprites
883 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
884 << " output.dx_Position = input.dx_Position;\n"
885 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
886 << "}\n";
887
888 return preambleStream.str();
889}
890
891std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
Jamie Madill9082b982016-04-27 15:21:51 -0400892 const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400893 const gl::ProgramState &programData,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800894 const bool useViewScale,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400895 const std::string &preambleString) const
896{
897 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500898
Jamie Madill334d6152015-10-22 14:00:28 -0400899 std::stringstream shaderStream;
900
Jamie Madill4e31ad52015-10-29 10:32:57 -0400901 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
902 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500903
Jamie Madill76f8fa62015-10-29 10:32:56 -0400904 const char *inputPT = nullptr;
905 const char *outputPT = nullptr;
906 int inputSize = 0;
907 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500908
Jamie Madill76f8fa62015-10-29 10:32:56 -0400909 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500910 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400911 case PRIMITIVE_POINTS:
912 inputPT = "point";
913 outputPT = "Triangle";
914 inputSize = 1;
915 maxVertexOutput = 4;
916 break;
917
918 case PRIMITIVE_LINES:
919 case PRIMITIVE_LINE_STRIP:
920 case PRIMITIVE_LINE_LOOP:
921 inputPT = "line";
922 outputPT = "Line";
923 inputSize = 2;
924 maxVertexOutput = 2;
925 break;
926
927 case PRIMITIVE_TRIANGLES:
928 case PRIMITIVE_TRIANGLE_STRIP:
929 case PRIMITIVE_TRIANGLE_FAN:
930 inputPT = "triangle";
931 outputPT = "Triangle";
932 inputSize = 3;
933 maxVertexOutput = 3;
934 break;
935
936 default:
937 UNREACHABLE();
938 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500939 }
940
Jamie Madill76f8fa62015-10-29 10:32:56 -0400941 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500942 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400943 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
944 "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800945 "uniform float4 dx_ViewCoords : register(c1);\n";
946
947 if (useViewScale)
948 {
949 shaderStream << "uniform float2 dx_ViewScale : register(c3);\n";
950 }
951
952 shaderStream << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400953 "static float2 pointSpriteCorners[] = \n"
954 "{\n"
955 " float2( 0.5f, -0.5f),\n"
956 " float2( 0.5f, 0.5f),\n"
957 " float2(-0.5f, -0.5f),\n"
958 " float2(-0.5f, 0.5f)\n"
959 "};\n"
960 "\n"
961 "static float2 pointSpriteTexcoords[] = \n"
962 "{\n"
963 " float2(1.0f, 1.0f),\n"
964 " float2(1.0f, 0.0f),\n"
965 " float2(0.0f, 1.0f),\n"
966 " float2(0.0f, 0.0f)\n"
967 "};\n"
968 "\n"
969 "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700970 << static_cast<int>(data.getCaps().minAliasedPointSize)
Jamie Madill76f8fa62015-10-29 10:32:56 -0400971 << ".0f;\n"
972 "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700973 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400974 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500975 }
976
Jamie Madill4e31ad52015-10-29 10:32:57 -0400977 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400978 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400979 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
980
981 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
982 {
983 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
984 }
985
986 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400987 << "{\n"
988 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500989
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400990 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
991 {
992 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
993 }
994 else
995 {
996 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
997 }
998
Jamie Madill4e31ad52015-10-29 10:32:57 -0400999 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -05001000 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001001 shaderStream << " copyVertex(output, input[" << vertexIndex
1002 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001003
1004 if (!pointSprites)
1005 {
1006 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001007 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001008 }
1009 }
1010
1011 if (pointSprites)
1012 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001013 shaderStream << "\n"
1014 " float4 dx_Position = input[0].dx_Position;\n"
1015 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
1016 "maxPointSize);\n"
1017 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
1018 "dx_ViewCoords.y) * dx_Position.w;\n";
1019
1020 for (int corner = 0; corner < 4; corner++)
1021 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001022 if (useViewScale)
1023 {
1024 shaderStream << " \n"
1025 " output.dx_Position = dx_Position + float4(1.0f, "
1026 "-dx_ViewScale.y, 1.0f, 1.0f)"
1027 " * float4(pointSpriteCorners["
1028 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1029 }
1030 else
1031 {
1032 shaderStream << "\n"
1033 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
1034 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1035 }
Jamie Madill76f8fa62015-10-29 10:32:56 -04001036
1037 if (usesPointCoord)
1038 {
1039 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
1040 << "];\n";
1041 }
1042
1043 shaderStream << " outStream.Append(output);\n";
1044 }
Jamie Madill5f562732014-02-14 16:41:24 -05001045 }
1046
Jamie Madill334d6152015-10-22 14:00:28 -04001047 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001048 " outStream.RestartStrip();\n"
1049 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001050
Jamie Madill334d6152015-10-22 14:00:28 -04001051 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -05001052}
1053
1054// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001055std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001056{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001057 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001058 {
1059 return "_" + name;
1060 }
1061
1062 return name;
1063}
1064
Jamie Madill334d6152015-10-22 14:00:28 -04001065std::string DynamicHLSL::generateAttributeConversionHLSL(
1066 gl::VertexFormatType vertexFormatType,
1067 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001068{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001069 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -04001070 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001071
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001072 // Matrix
1073 if (IsMatrixType(shaderAttrib.type))
1074 {
Jamie Madill8664b062014-02-14 16:41:29 -05001075 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001076 }
1077
Jamie Madillf2575982014-06-25 16:04:54 -04001078 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001079 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001080
Jamie Madill8664b062014-02-14 16:41:29 -05001081 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -04001082 bool requiresTypeConversion =
1083 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001084
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001085 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001086 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001087 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001088 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001089 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001090 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001091
1092 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001093 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001094}
Jamie Madille39a3f02015-11-17 20:42:15 -05001095
Jamie Madill9082b982016-04-27 15:21:51 -04001096void DynamicHLSL::getPixelShaderOutputKey(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001097 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -05001098 const ProgramD3DMetadata &metadata,
1099 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1100{
1101 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1102 // - with a 3.0 context, the output color is copied to channel 0
1103 // - with a 2.0 context, the output color is broadcast to all channels
1104 bool broadcast = metadata.usesBroadcast(data);
1105 const unsigned int numRenderTargets =
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001106 (broadcast || metadata.usesMultipleFragmentOuts() ? data.getCaps().maxDrawBuffers : 1);
Jamie Madille39a3f02015-11-17 20:42:15 -05001107
1108 if (metadata.getMajorShaderVersion() < 300)
1109 {
1110 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1111 renderTargetIndex++)
1112 {
1113 PixelShaderOutputVariable outputKeyVariable;
1114 outputKeyVariable.type = GL_FLOAT_VEC4;
1115 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1116 outputKeyVariable.source =
1117 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1118 outputKeyVariable.outputIndex = renderTargetIndex;
1119
1120 outPixelShaderKey->push_back(outputKeyVariable);
1121 }
1122 }
1123 else
1124 {
1125 const auto &shaderOutputVars =
1126 metadata.getFragmentShader()->getData().getActiveOutputVariables();
1127
1128 for (auto outputPair : programData.getOutputVariables())
1129 {
1130 const VariableLocation &outputLocation = outputPair.second;
1131 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1132 const std::string &variableName = "out_" + outputLocation.name;
1133 const std::string &elementString =
1134 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
1135
1136 ASSERT(outputVariable.staticUse);
1137
1138 PixelShaderOutputVariable outputKeyVariable;
1139 outputKeyVariable.type = outputVariable.type;
1140 outputKeyVariable.name = variableName + elementString;
1141 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
1142 outputKeyVariable.outputIndex = outputPair.first;
1143
1144 outPixelShaderKey->push_back(outputKeyVariable);
1145 }
1146 }
1147}
1148
Jamie Madill192745a2016-12-22 15:58:21 -05001149// BuiltinVarying Implementation.
1150BuiltinVarying::BuiltinVarying() : enabled(false), index(0), systemValue(false)
1151{
1152}
1153
1154std::string BuiltinVarying::str() const
1155{
1156 return (systemValue ? semantic : (semantic + Str(index)));
1157}
1158
1159void BuiltinVarying::enableSystem(const std::string &systemValueSemantic)
1160{
1161 enabled = true;
1162 semantic = systemValueSemantic;
1163 systemValue = true;
1164}
1165
1166void BuiltinVarying::enable(const std::string &semanticVal, unsigned int indexVal)
1167{
1168 enabled = true;
1169 semantic = semanticVal;
1170 index = indexVal;
1171}
1172
1173// BuiltinVaryingsD3D Implementation.
1174BuiltinVaryingsD3D::BuiltinVaryingsD3D(const ProgramD3DMetadata &metadata,
1175 const VaryingPacking &packing)
1176{
1177 updateBuiltins(SHADER_VERTEX, metadata, packing);
1178 updateBuiltins(SHADER_PIXEL, metadata, packing);
1179 if (metadata.getRendererMajorShaderModel() >= 4)
1180 {
1181 updateBuiltins(SHADER_GEOMETRY, metadata, packing);
1182 }
1183}
1184
1185void BuiltinVaryingsD3D::updateBuiltins(ShaderType shaderType,
1186 const ProgramD3DMetadata &metadata,
1187 const VaryingPacking &packing)
1188{
1189 const std::string &userSemantic = GetVaryingSemantic(metadata.getRendererMajorShaderModel(),
1190 metadata.usesSystemValuePointSize());
1191
1192 unsigned int reservedSemanticIndex = packing.getMaxSemanticIndex();
1193
1194 BuiltinInfo *builtins = &mBuiltinInfo[shaderType];
1195
1196 if (metadata.getRendererMajorShaderModel() >= 4)
1197 {
1198 builtins->dxPosition.enableSystem("SV_Position");
1199 }
1200 else if (shaderType == SHADER_PIXEL)
1201 {
1202 builtins->dxPosition.enableSystem("VPOS");
1203 }
1204 else
1205 {
1206 builtins->dxPosition.enableSystem("POSITION");
1207 }
1208
1209 if (metadata.usesTransformFeedbackGLPosition())
1210 {
1211 builtins->glPosition.enable(userSemantic, reservedSemanticIndex++);
1212 }
1213
1214 if (metadata.usesFragCoord())
1215 {
1216 builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++);
1217 }
1218
1219 if (shaderType == SHADER_VERTEX ? metadata.addsPointCoordToVertexShader()
1220 : metadata.usesPointCoord())
1221 {
1222 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
1223 // In D3D11 we manually compute gl_PointCoord in the GS.
1224 if (metadata.getRendererMajorShaderModel() >= 4)
1225 {
1226 builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++);
1227 }
1228 else
1229 {
1230 builtins->glPointCoord.enable("TEXCOORD", 0);
1231 }
1232 }
1233
1234 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
1235 if (metadata.usesSystemValuePointSize() &&
1236 (shaderType != SHADER_PIXEL || metadata.getRendererMajorShaderModel() >= 4))
1237 {
1238 builtins->glPointSize.enableSystem("PSIZE");
1239 }
1240}
1241
Jamie Madill334d6152015-10-22 14:00:28 -04001242} // namespace rx