blob: 22545dd60550c888605800dc6d53d152fe132a9d [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"
Jamie Madillbd044ed2017-06-05 12:59:21 -040013#include "libANGLE/Context.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Program.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050015#include "libANGLE/Shader.h"
Jamie Madillbd044ed2017-06-05 12:59:21 -040016#include "libANGLE/VaryingPacking.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/formatutils.h"
Jamie Madill28afae52015-11-09 15:07:57 -050018#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill80a6fc02015-08-21 16:53:16 -040019#include "libANGLE/renderer/d3d/RendererD3D.h"
20#include "libANGLE/renderer/d3d/ShaderD3D.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040021
Brandon Jonesd8d72432014-08-22 15:11:23 -070022using namespace gl;
23
Jamie Madill30d6c252014-11-13 10:03:33 -050024namespace rx
25{
26
Jamie Madill3f2e61d2014-09-05 10:38:05 -040027namespace
Jamie Madill5f562732014-02-14 16:41:24 -050028{
Jamie Madill8664b062014-02-14 16:41:29 -050029
30std::string HLSLComponentTypeString(GLenum componentType)
31{
32 switch (componentType)
33 {
Jamie Madill334d6152015-10-22 14:00:28 -040034 case GL_UNSIGNED_INT:
35 return "uint";
36 case GL_INT:
37 return "int";
38 case GL_UNSIGNED_NORMALIZED:
39 case GL_SIGNED_NORMALIZED:
40 case GL_FLOAT:
41 return "float";
42 default:
43 UNREACHABLE();
44 return "not-component-type";
Jamie Madill8664b062014-02-14 16:41:29 -050045 }
Jamie Madill5f562732014-02-14 16:41:24 -050046}
47
Jamie Madill8664b062014-02-14 16:41:29 -050048std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
49{
50 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
51}
52
53std::string HLSLMatrixTypeString(GLenum type)
54{
55 switch (type)
56 {
Jamie Madill334d6152015-10-22 14:00:28 -040057 case GL_FLOAT_MAT2:
58 return "float2x2";
59 case GL_FLOAT_MAT3:
60 return "float3x3";
61 case GL_FLOAT_MAT4:
62 return "float4x4";
63 case GL_FLOAT_MAT2x3:
64 return "float2x3";
65 case GL_FLOAT_MAT3x2:
66 return "float3x2";
67 case GL_FLOAT_MAT2x4:
68 return "float2x4";
69 case GL_FLOAT_MAT4x2:
70 return "float4x2";
71 case GL_FLOAT_MAT3x4:
72 return "float3x4";
73 case GL_FLOAT_MAT4x3:
74 return "float4x3";
75 default:
76 UNREACHABLE();
77 return "not-matrix-type";
Jamie Madill8664b062014-02-14 16:41:29 -050078 }
79}
80
81std::string HLSLTypeString(GLenum type)
82{
83 if (gl::IsMatrixType(type))
84 {
85 return HLSLMatrixTypeString(type);
86 }
87
Jamie Madill334d6152015-10-22 14:00:28 -040088 return HLSLComponentTypeString(gl::VariableComponentType(type),
89 gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050090}
91
Jamie Madill334d6152015-10-22 14:00:28 -040092const PixelShaderOutputVariable *FindOutputAtLocation(
93 const std::vector<PixelShaderOutputVariable> &outputVariables,
94 unsigned int location)
Jamie Madill3f2e61d2014-09-05 10:38:05 -040095{
96 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
97 {
98 if (outputVariables[variableIndex].outputIndex == location)
99 {
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000100 return &outputVariables[variableIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400101 }
102 }
103
Jamie Madill334d6152015-10-22 14:00:28 -0400104 return nullptr;
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400105}
106
Jamie Madill55c25d02015-11-18 13:08:08 -0500107void WriteArrayString(std::stringstream &strstr, unsigned int i)
108{
109 static_assert(GL_INVALID_INDEX == UINT_MAX,
110 "GL_INVALID_INDEX must be equal to the max unsigned int.");
111 if (i == UINT_MAX)
112 {
113 return;
114 }
115
116 strstr << "[";
117 strstr << i;
118 strstr << "]";
119}
120
Jamie Madill51c47682016-10-25 15:50:14 -0400121constexpr const char *VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
122constexpr const char *PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400123} // anonymous namespace
Jamie Madill4cff2472015-08-21 16:53:18 -0400124
Jamie Madill9fc36822015-11-18 13:08:07 -0500125// DynamicHLSL implementation
126
Jamie Madill65345da2015-11-13 11:25:23 -0500127DynamicHLSL::DynamicHLSL(RendererD3D *const renderer) : mRenderer(renderer)
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000128{
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000129}
130
Jamie Madill334d6152015-10-22 14:00:28 -0400131std::string DynamicHLSL::generateVertexShaderForInputLayout(
132 const std::string &sourceShader,
133 const InputLayout &inputLayout,
134 const std::vector<sh::Attribute> &shaderAttributes) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500135{
Jamie Madill334d6152015-10-22 14:00:28 -0400136 std::stringstream structStream;
137 std::stringstream initStream;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500138
Jamie Madill334d6152015-10-22 14:00:28 -0400139 structStream << "struct VS_INPUT\n"
140 << "{\n";
141
142 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400143 unsigned int inputIndex = 0;
144
Cooper Partine6d14cc2015-02-20 12:32:58 -0800145 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
146 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
147 // must be used.
148 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
Jamie Madill334d6152015-10-22 14:00:28 -0400149 bool useInstancedPointSpriteEmulation =
150 usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Cooper Partine6d14cc2015-02-20 12:32:58 -0800151
152 // Instanced PointSprite emulation requires additional entries in the
153 // VS_INPUT structure to support the vertices that make up the quad vertices.
154 // These values must be in sync with the cooresponding values added during inputlayout creation
155 // in InputLayoutCache::applyVertexBuffers().
156 //
157 // The additional entries must appear first in the VS_INPUT layout because
158 // Windows Phone 8 era devices require per vertex data to physically come
159 // before per instance data in the shader.
160 if (useInstancedPointSpriteEmulation)
161 {
Jamie Madill334d6152015-10-22 14:00:28 -0400162 structStream << " float3 spriteVertexPos : SPRITEPOSITION0;\n"
163 << " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
Cooper Partine6d14cc2015-02-20 12:32:58 -0800164 }
165
Jamie Madill3da79b72015-04-27 11:09:17 -0400166 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex)
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500167 {
Jamie Madillf2575982014-06-25 16:04:54 -0400168 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500169 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500170 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400171 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400172 VertexFormatType vertexFormatType =
173 inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID;
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400174
Jamie Madill3b7e2052014-03-17 09:47:43 -0400175 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500176 if (IsMatrixType(shaderAttribute.type))
177 {
178 // Matrix types are always transposed
Jamie Madill334d6152015-10-22 14:00:28 -0400179 structStream << " "
180 << HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500181 }
182 else
183 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400184 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000185
Corentin Wallezb076add2016-01-11 16:45:46 -0500186 if (shaderAttribute.name == "gl_InstanceID" ||
187 shaderAttribute.name == "gl_VertexID")
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000188 {
Corentin Wallezb076add2016-01-11 16:45:46 -0500189 // The input types of the instance ID and vertex ID in HLSL (uint) differs from
190 // the ones in ESSL (int).
Jamie Madill334d6152015-10-22 14:00:28 -0400191 structStream << " uint";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000192 }
193 else
194 {
Jamie Madill334d6152015-10-22 14:00:28 -0400195 structStream << " " << HLSLComponentTypeString(
196 componentType,
197 VariableComponentCount(shaderAttribute.type));
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000198 }
Jamie Madill8664b062014-02-14 16:41:29 -0500199 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500200
Jamie Madill334d6152015-10-22 14:00:28 -0400201 structStream << " " << decorateVariable(shaderAttribute.name) << " : ";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000202
203 if (shaderAttribute.name == "gl_InstanceID")
204 {
Jamie Madill334d6152015-10-22 14:00:28 -0400205 structStream << "SV_InstanceID";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000206 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500207 else if (shaderAttribute.name == "gl_VertexID")
208 {
209 structStream << "SV_VertexID";
210 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000211 else
212 {
Jamie Madill334d6152015-10-22 14:00:28 -0400213 structStream << "TEXCOORD" << semanticIndex;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000214 semanticIndex += VariableRegisterCount(shaderAttribute.type);
215 }
216
Jamie Madill334d6152015-10-22 14:00:28 -0400217 structStream << ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500218
Jamie Madill3b7e2052014-03-17 09:47:43 -0400219 // HLSL code for initialization
Jamie Madill334d6152015-10-22 14:00:28 -0400220 initStream << " " << decorateVariable(shaderAttribute.name) << " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500221
222 // Mismatched vertex attribute to vertex input may result in an undefined
223 // data reinterpretation (eg for pure integer->float, float->pure integer)
224 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400225 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400226 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500227 {
Jamie Madill334d6152015-10-22 14:00:28 -0400228 initStream << generateAttributeConversionHLSL(vertexFormatType, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500229 }
230 else
231 {
Jamie Madill334d6152015-10-22 14:00:28 -0400232 initStream << "input." << decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500233 }
234
Jamie Madill334d6152015-10-22 14:00:28 -0400235 initStream << ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400236
Jamie Madillac0a2672014-04-11 13:33:56 -0400237 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
238 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500239 }
240
Jamie Madill334d6152015-10-22 14:00:28 -0400241 structStream << "};\n"
242 "\n"
243 "void initAttributes(VS_INPUT input)\n"
244 "{\n"
245 << initStream.str() << "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400246
247 std::string vertexHLSL(sourceShader);
248
249 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
Jamie Madill51c47682016-10-25 15:50:14 -0400250 vertexHLSL.replace(copyInsertionPos, strlen(VERTEX_ATTRIBUTE_STUB_STRING), structStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400251
252 return vertexHLSL;
253}
254
Jamie Madill334d6152015-10-22 14:00:28 -0400255std::string DynamicHLSL::generatePixelShaderForOutputSignature(
256 const std::string &sourceShader,
257 const std::vector<PixelShaderOutputVariable> &outputVariables,
258 bool usesFragDepth,
259 const std::vector<GLenum> &outputLayout) const
Geoff Lang04fb89a2014-06-09 15:05:36 -0400260{
Jamie Madill334d6152015-10-22 14:00:28 -0400261 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang04fb89a2014-06-09 15:05:36 -0400262 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
Jamie Madill334d6152015-10-22 14:00:28 -0400263 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400264
Jamie Madill334d6152015-10-22 14:00:28 -0400265 std::stringstream declarationStream;
266 std::stringstream copyStream;
267
268 declarationStream << "struct PS_OUTPUT\n"
269 "{\n";
Geoff Lang4ace4232014-06-18 19:12:48 -0400270
Jamie Madill66a08192017-02-03 15:24:25 -0500271 // Workaround for HLSL 3.x: We can't do a depth/stencil only render, the runtime will complain.
272 size_t numOutputs = outputLayout.empty() ? 1u : outputLayout.size();
273 const PixelShaderOutputVariable defaultOutput(GL_FLOAT_VEC4, "dummy", "float4(0, 0, 0, 1)", 0);
274
275 for (size_t layoutIndex = 0; layoutIndex < numOutputs; ++layoutIndex)
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400276 {
Jamie Madill66a08192017-02-03 15:24:25 -0500277 GLenum binding = outputLayout.empty() ? GL_COLOR_ATTACHMENT0 : outputLayout[layoutIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400278
279 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400280 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400281 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
282
Jamie Madill334d6152015-10-22 14:00:28 -0400283 const PixelShaderOutputVariable *outputVariable =
Jamie Madill66a08192017-02-03 15:24:25 -0500284 outputLayout.empty() ? &defaultOutput
285 : FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400286
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000287 // OpenGL ES 3.0 spec $4.2.1
Jamie Madill334d6152015-10-22 14:00:28 -0400288 // If [...] not all user-defined output variables are written, the values of fragment
289 // colors
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000290 // corresponding to unwritten variables are similarly undefined.
291 if (outputVariable)
292 {
Jamie Madill334d6152015-10-22 14:00:28 -0400293 declarationStream << " " + HLSLTypeString(outputVariable->type) << " "
294 << outputVariable->name << " : " << targetSemantic
295 << static_cast<int>(layoutIndex) << ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400296
Jamie Madill334d6152015-10-22 14:00:28 -0400297 copyStream << " output." << outputVariable->name << " = "
298 << outputVariable->source << ";\n";
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000299 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400300 }
301 }
302
303 if (usesFragDepth)
304 {
Jamie Madill334d6152015-10-22 14:00:28 -0400305 declarationStream << " float gl_Depth : " << depthSemantic << ";\n";
306 copyStream << " output.gl_Depth = gl_Depth; \n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400307 }
308
Jamie Madill334d6152015-10-22 14:00:28 -0400309 declarationStream << "};\n"
310 "\n"
311 "PS_OUTPUT generateOutput()\n"
312 "{\n"
313 " PS_OUTPUT output;\n"
314 << copyStream.str() << " return output;\n"
315 "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400316
317 std::string pixelHLSL(sourceShader);
318
319 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
Jamie Madill51c47682016-10-25 15:50:14 -0400320 pixelHLSL.replace(outputInsertionPos, strlen(PIXEL_OUTPUT_STUB_STRING),
Jamie Madill334d6152015-10-22 14:00:28 -0400321 declarationStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400322
323 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500324}
325
Jamie Madill192745a2016-12-22 15:58:21 -0500326void DynamicHLSL::generateVaryingLinkHLSL(const VaryingPacking &varyingPacking,
327 const BuiltinInfo &builtins,
328 bool programUsesPointSize,
329 std::stringstream &hlslStream) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400330{
Jamie Madill9fc36822015-11-18 13:08:07 -0500331 ASSERT(builtins.dxPosition.enabled);
Jamie Madill192745a2016-12-22 15:58:21 -0500332 hlslStream << "{\n"
Jamie Madill9fc36822015-11-18 13:08:07 -0500333 << " float4 dx_Position : " << builtins.dxPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700334
Jamie Madill9fc36822015-11-18 13:08:07 -0500335 if (builtins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700336 {
Jamie Madill192745a2016-12-22 15:58:21 -0500337 hlslStream << " float4 gl_Position : " << builtins.glPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700338 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400339
Jamie Madill9fc36822015-11-18 13:08:07 -0500340 if (builtins.glFragCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400341 {
Jamie Madill192745a2016-12-22 15:58:21 -0500342 hlslStream << " float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400343 }
344
Jamie Madill9fc36822015-11-18 13:08:07 -0500345 if (builtins.glPointCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400346 {
Jamie Madill192745a2016-12-22 15:58:21 -0500347 hlslStream << " float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400348 }
349
Jamie Madill9fc36822015-11-18 13:08:07 -0500350 if (builtins.glPointSize.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400351 {
Jamie Madill192745a2016-12-22 15:58:21 -0500352 hlslStream << " float gl_PointSize : " << builtins.glPointSize.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400353 }
354
Jamie Madill192745a2016-12-22 15:58:21 -0500355 std::string varyingSemantic =
356 GetVaryingSemantic(mRenderer->getMajorShaderModel(), programUsesPointSize);
Austin Kinross8b695ee2015-03-12 13:12:20 -0700357
Jamie Madill192745a2016-12-22 15:58:21 -0500358 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
359 {
360 const auto &varying = *registerInfo.packedVarying->varying;
361 ASSERT(!varying.isStruct());
362
363 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many
364 // registers being used.
365 // For example, if there are N registers, and we have N vec3 varyings and 1 float
366 // varying, then D3D will pack them into N registers.
367 // If the float varying has the 'nointerpolation' modifier on it then we would need
368 // N + 1 registers, and D3D compilation will fail.
369
370 switch (registerInfo.packedVarying->interpolation)
371 {
372 case sh::INTERPOLATION_SMOOTH:
373 hlslStream << " ";
374 break;
375 case sh::INTERPOLATION_FLAT:
376 hlslStream << " nointerpolation ";
377 break;
378 case sh::INTERPOLATION_CENTROID:
379 hlslStream << " centroid ";
380 break;
381 default:
382 UNREACHABLE();
383 }
384
385 GLenum transposedType = gl::TransposeMatrixType(varying.type);
386 GLenum componentType = gl::VariableComponentType(transposedType);
387 int columnCount = gl::VariableColumnCount(transposedType);
388 hlslStream << HLSLComponentTypeString(componentType, columnCount);
389 unsigned int semanticIndex = registerInfo.semanticIndex;
390 hlslStream << " v" << semanticIndex << " : " << varyingSemantic << semanticIndex << ";\n";
391 }
392
393 hlslStream << "};\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400394}
395
Jamie Madillbd044ed2017-06-05 12:59:21 -0400396void DynamicHLSL::generateShaderLinkHLSL(const gl::Context *context,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400397 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500398 const ProgramD3DMetadata &programMetadata,
Jamie Madill9fc36822015-11-18 13:08:07 -0500399 const VaryingPacking &varyingPacking,
Jamie Madill192745a2016-12-22 15:58:21 -0500400 const BuiltinVaryingsD3D &builtinsD3D,
Jamie Madill334d6152015-10-22 14:00:28 -0400401 std::string *pixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -0500402 std::string *vertexHLSL) const
Jamie Madill5f562732014-02-14 16:41:24 -0500403{
Jamie Madill334d6152015-10-22 14:00:28 -0400404 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500405
Jamie Madillbd044ed2017-06-05 12:59:21 -0400406 const auto &data = context->getContextState();
407 gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
408 gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
Jamie Madill91445bc2015-09-23 16:47:53 -0400409 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700410 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400411
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800412 // usesViewScale() isn't supported in the D3D9 renderer
413 ASSERT(shaderModel >= 4 || !programMetadata.usesViewScale());
414
Jamie Madill334d6152015-10-22 14:00:28 -0400415 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500416 programMetadata.usesPointSize() &&
417 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400418
Jamie Madill14e95b32015-05-07 10:10:41 -0400419 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400420 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500421
Jamie Madill334d6152015-10-22 14:00:28 -0400422 std::stringstream vertexStream;
Jamie Madillbd044ed2017-06-05 12:59:21 -0400423 vertexStream << vertexShaderGL->getTranslatedSource(context);
Jamie Madill334d6152015-10-22 14:00:28 -0400424
Cooper Partine6664f02015-01-09 16:22:24 -0800425 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400426 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800427 if (useInstancedPointSpriteEmulation)
428 {
Jamie Madill334d6152015-10-22 14:00:28 -0400429 vertexStream << "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700430 << static_cast<int>(data.getCaps().minAliasedPointSize) << ".0f;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400431 << "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700432 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800433 }
434
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500435 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill51c47682016-10-25 15:50:14 -0400436 vertexStream << "\n" << std::string(VERTEX_ATTRIBUTE_STUB_STRING) << "\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500437
Jamie Madill192745a2016-12-22 15:58:21 -0500438 const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX];
439
Jamie Madill9fc36822015-11-18 13:08:07 -0500440 // Write the HLSL input/output declarations
441 vertexStream << "struct VS_OUTPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500442 generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
443 vertexStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400444 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400445 << "VS_OUTPUT main(VS_INPUT input)\n"
446 << "{\n"
447 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500448
Jamie Madill334d6152015-10-22 14:00:28 -0400449 vertexStream << "\n"
450 << " gl_main();\n"
451 << "\n"
452 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700453
Jamie Madill9fc36822015-11-18 13:08:07 -0500454 if (vertexBuiltins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700455 {
Jamie Madill334d6152015-10-22 14:00:28 -0400456 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700457 }
Jamie Madill37997142015-01-28 10:06:34 -0500458
Austin Kinross4fd18b12014-12-22 12:32:05 -0800459 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400460 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500461 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800462 vertexStream << " output.dx_Position.x = gl_Position.x;\n";
463
464 if (programMetadata.usesViewScale())
465 {
466 // This code assumes that dx_ViewScale.y = -1.0f when rendering to texture, and +1.0f
467 // when rendering to the default framebuffer. No other values are valid.
468 vertexStream << " output.dx_Position.y = dx_ViewScale.y * gl_Position.y;\n";
469 }
470 else
471 {
472 vertexStream << " output.dx_Position.y = - gl_Position.y;\n";
473 }
474
475 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400476 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500477 }
478 else
479 {
Jamie Madill334d6152015-10-22 14:00:28 -0400480 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800481 "dx_ViewAdjust.x * gl_Position.w;\n";
482
483 // If usesViewScale() is true and we're using the D3D11 renderer via Feature Level 9_*,
484 // then we need to multiply the gl_Position.y by the viewScale.
485 // usesViewScale() isn't supported when using the D3D9 renderer.
486 if (programMetadata.usesViewScale() &&
487 (shaderModel >= 4 && mRenderer->getShaderModelSuffix() != ""))
488 {
489 vertexStream << " output.dx_Position.y = dx_ViewScale.y * (gl_Position.y * "
490 "dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n";
491 }
492 else
493 {
494 vertexStream << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
495 "dx_ViewAdjust.y * gl_Position.w);\n";
496 }
497
498 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400499 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500500 }
501
Austin Kinross8b695ee2015-03-12 13:12:20 -0700502 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
Jamie Madill9fc36822015-11-18 13:08:07 -0500503 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500504 {
Jamie Madill334d6152015-10-22 14:00:28 -0400505 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500506 }
507
Jamie Madill9fc36822015-11-18 13:08:07 -0500508 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500509 {
Jamie Madill334d6152015-10-22 14:00:28 -0400510 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500511 }
512
Jamie Madill9fc36822015-11-18 13:08:07 -0500513 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500514 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500515 const auto &packedVarying = *registerInfo.packedVarying;
516 const auto &varying = *packedVarying.varying;
517 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400518
Jamie Madill55c25d02015-11-18 13:08:08 -0500519 vertexStream << " output.v" << registerInfo.semanticIndex << " = ";
520
521 if (packedVarying.isStructField())
522 {
523 vertexStream << decorateVariable(packedVarying.parentStructName) << ".";
524 }
525
526 vertexStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400527
528 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400529 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500530 WriteArrayString(vertexStream, registerInfo.varyingArrayIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500531 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400532
Jamie Madill55c25d02015-11-18 13:08:08 -0500533 if (VariableRowCount(varying.type) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400534 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500535 WriteArrayString(vertexStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400536 }
537
538 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500539 }
540
Cooper Partine6664f02015-01-09 16:22:24 -0800541 // Instanced PointSprite emulation requires additional entries to calculate
542 // the final output vertex positions of the quad that represents each sprite.
543 if (useInstancedPointSpriteEmulation)
544 {
Jamie Madill334d6152015-10-22 14:00:28 -0400545 vertexStream << "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800546 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n";
547
548 vertexStream << " output.dx_Position.x += (input.spriteVertexPos.x * gl_PointSize / "
549 "(dx_ViewCoords.x*2)) * output.dx_Position.w;";
550
551 if (programMetadata.usesViewScale())
552 {
553 // Multiply by ViewScale to invert the rendering when appropriate
554 vertexStream << " output.dx_Position.y += (-dx_ViewScale.y * "
555 "input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2)) * "
556 "output.dx_Position.w;";
557 }
558 else
559 {
560 vertexStream << " output.dx_Position.y += (input.spriteVertexPos.y * gl_PointSize / "
561 "(dx_ViewCoords.y*2)) * output.dx_Position.w;";
562 }
563
564 vertexStream
565 << " output.dx_Position.z += input.spriteVertexPos.z * output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800566
Jamie Madille39a3f02015-11-17 20:42:15 -0500567 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800568 {
Jamie Madill334d6152015-10-22 14:00:28 -0400569 vertexStream << "\n"
570 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800571 }
572 }
573
Cooper Partin7c89d242015-10-13 12:45:59 -0700574 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
575 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
576 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500577 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700578 {
579 ASSERT(!useInstancedPointSpriteEmulation);
580 vertexStream << "\n"
581 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
582 }
583
Jamie Madill334d6152015-10-22 14:00:28 -0400584 vertexStream << "\n"
585 << " return output;\n"
586 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500587
Jamie Madill192745a2016-12-22 15:58:21 -0500588 const auto &pixelBuiltins = builtinsD3D[SHADER_PIXEL];
589
Jamie Madill334d6152015-10-22 14:00:28 -0400590 std::stringstream pixelStream;
Jamie Madillbd044ed2017-06-05 12:59:21 -0400591 pixelStream << fragmentShaderGL->getTranslatedSource(context);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400592 pixelStream << "struct PS_INPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500593 generateVaryingLinkHLSL(varyingPacking, pixelBuiltins, builtinsD3D.usesPointSize(),
594 pixelStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400595 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500596
Jamie Madill51c47682016-10-25 15:50:14 -0400597 pixelStream << std::string(PIXEL_OUTPUT_STUB_STRING) << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500598
Jamie Madill334d6152015-10-22 14:00:28 -0400599 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500600 {
601 if (shaderModel >= 4)
602 {
Jamie Madill334d6152015-10-22 14:00:28 -0400603 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
604 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500605 }
606 else
607 {
Jamie Madill334d6152015-10-22 14:00:28 -0400608 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
609 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500610 }
611 }
612 else
613 {
Jamie Madill334d6152015-10-22 14:00:28 -0400614 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
615 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500616 }
617
Jamie Madill9fc36822015-11-18 13:08:07 -0500618 if (pixelBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500619 {
Jamie Madill334d6152015-10-22 14:00:28 -0400620 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500621
Austin Kinross588434c2014-12-10 10:41:45 -0800622 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400623 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
624 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800625 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500626 {
Jamie Madill334d6152015-10-22 14:00:28 -0400627 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
628 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500629 }
Austin Kinross588434c2014-12-10 10:41:45 -0800630 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500631 {
Jamie Madill334d6152015-10-22 14:00:28 -0400632 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
633 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500634 }
635 else
636 {
Jamie Madill334d6152015-10-22 14:00:28 -0400637 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
638 // Renderer::setViewport()
639 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
640 "dx_ViewCoords.z;\n"
641 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
642 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500643 }
644
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800645 if (programMetadata.usesViewScale())
646 {
647 // For Feature Level 9_3 and below, we need to correct gl_FragCoord.y to account
648 // for dx_ViewScale. On Feature Level 10_0+, gl_FragCoord.y is calculated above using
649 // dx_ViewCoords and is always correct irrespective of dx_ViewScale's value.
650 // NOTE: usesViewScale() can only be true on D3D11 (i.e. Shader Model 4.0+).
651 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
652 {
653 // Some assumptions:
654 // - dx_ViewScale.y = -1.0f when rendering to texture
655 // - dx_ViewScale.y = +1.0f when rendering to the default framebuffer
656 // - gl_FragCoord.y has been set correctly above.
657 //
658 // When rendering to the backbuffer, the code inverts gl_FragCoord's y coordinate.
659 // This involves subtracting the y coordinate from the height of the area being
660 // rendered to.
661 //
662 // First we calculate the height of the area being rendered to:
663 // render_area_height = (2.0f / (1.0f - input.gl_FragCoord.y * rhw)) *
664 // gl_FragCoord.y
665 //
666 // Note that when we're rendering to default FB, we want our output to be
667 // equivalent to:
668 // "gl_FragCoord.y = render_area_height - gl_FragCoord.y"
669 //
670 // When we're rendering to a texture, we want our output to be equivalent to:
671 // "gl_FragCoord.y = gl_FragCoord.y;"
672 //
673 // If we set scale_factor = ((1.0f + dx_ViewScale.y) / 2.0f), then notice that
674 // - When rendering to default FB: scale_factor = 1.0f
675 // - When rendering to texture: scale_factor = 0.0f
676 //
677 // Therefore, we can get our desired output by setting:
678 // "gl_FragCoord.y = scale_factor * render_area_height - dx_ViewScale.y *
679 // gl_FragCoord.y"
680 //
681 // Simplifying, this becomes:
682 pixelStream
683 << " gl_FragCoord.y = (1.0f + dx_ViewScale.y) * gl_FragCoord.y /"
684 "(1.0f - input.gl_FragCoord.y * rhw) - dx_ViewScale.y * gl_FragCoord.y;\n";
685 }
686 }
687
Jamie Madill334d6152015-10-22 14:00:28 -0400688 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
689 "dx_DepthFront.y;\n"
690 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500691 }
692
Jamie Madill9fc36822015-11-18 13:08:07 -0500693 if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500694 {
Jamie Madill334d6152015-10-22 14:00:28 -0400695 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
696 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500697 }
698
Jamie Madill334d6152015-10-22 14:00:28 -0400699 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500700 {
701 if (shaderModel <= 3)
702 {
Jamie Madill334d6152015-10-22 14:00:28 -0400703 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500704 }
705 else
706 {
Jamie Madill334d6152015-10-22 14:00:28 -0400707 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500708 }
709 }
710
Jamie Madill9fc36822015-11-18 13:08:07 -0500711 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500712 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500713 const auto &packedVarying = *registerInfo.packedVarying;
714 const auto &varying = *packedVarying.varying;
715 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400716
Jamie Madillca03b352015-09-02 12:38:13 -0400717 // Don't reference VS-only transform feedback varyings in the PS.
Geoff Lang82a468a2016-06-21 17:18:25 -0400718 // TODO: Consider updating the fragment shader's varyings with a parameter signaling that a
719 // varying is only used in the vertex shader in MergeVaryings
720 if (packedVarying.vertexOnly || (!varying.staticUse && !packedVarying.isStructField()))
Jamie Madillca03b352015-09-02 12:38:13 -0400721 continue;
722
Jamie Madill55c25d02015-11-18 13:08:08 -0500723 pixelStream << " ";
724
725 if (packedVarying.isStructField())
726 {
727 pixelStream << decorateVariable(packedVarying.parentStructName) << ".";
728 }
729
730 pixelStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400731
732 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400733 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500734 WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400735 }
736
Jamie Madill55c25d02015-11-18 13:08:08 -0500737 GLenum transposedType = TransposeMatrixType(varying.type);
738 if (VariableRowCount(transposedType) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400739 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500740 WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400741 }
742
Jamie Madill9fc36822015-11-18 13:08:07 -0500743 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400744
Jamie Madill55c25d02015-11-18 13:08:08 -0500745 switch (VariableColumnCount(transposedType))
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400746 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500747 case 1:
748 pixelStream << ".x";
749 break;
750 case 2:
751 pixelStream << ".xy";
752 break;
753 case 3:
754 pixelStream << ".xyz";
755 break;
756 case 4:
757 break;
758 default:
759 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500760 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400761 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500762 }
763
Jamie Madill334d6152015-10-22 14:00:28 -0400764 pixelStream << "\n"
765 << " gl_main();\n"
766 << "\n"
767 << " return generateOutput();\n"
768 << "}\n";
769
770 *vertexHLSL = vertexStream.str();
771 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500772}
773
Jamie Madillbd044ed2017-06-05 12:59:21 -0400774std::string DynamicHLSL::generateComputeShaderLinkHLSL(const gl::Context *context,
775 const gl::ProgramState &programData) const
Xinghua Caob1239382016-12-13 15:07:05 +0800776{
Jamie Madillbd044ed2017-06-05 12:59:21 -0400777 gl::Shader *computeShaderGL = programData.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +0800778 std::stringstream computeStream;
Jamie Madillbd044ed2017-06-05 12:59:21 -0400779 std::string translatedSource = computeShaderGL->getTranslatedSource(context);
Xinghua Caob1239382016-12-13 15:07:05 +0800780 computeStream << translatedSource;
781
782 bool usesWorkGroupID = translatedSource.find("GL_USES_WORK_GROUP_ID") != std::string::npos;
783 bool usesLocalInvocationID =
784 translatedSource.find("GL_USES_LOCAL_INVOCATION_ID") != std::string::npos;
785 bool usesGlobalInvocationID =
786 translatedSource.find("GL_USES_GLOBAL_INVOCATION_ID") != std::string::npos;
787 bool usesLocalInvocationIndex =
788 translatedSource.find("GL_USES_LOCAL_INVOCATION_INDEX") != std::string::npos;
789
790 computeStream << "\nstruct CS_INPUT\n{\n";
791 if (usesWorkGroupID)
792 {
793 computeStream << " uint3 dx_WorkGroupID : "
794 << "SV_GroupID;\n";
795 }
796
797 if (usesLocalInvocationID)
798 {
799 computeStream << " uint3 dx_LocalInvocationID : "
800 << "SV_GroupThreadID;\n";
801 }
802
803 if (usesGlobalInvocationID)
804 {
805 computeStream << " uint3 dx_GlobalInvocationID : "
806 << "SV_DispatchThreadID;\n";
807 }
808
809 if (usesLocalInvocationIndex)
810 {
811 computeStream << " uint dx_LocalInvocationIndex : "
812 << "SV_GroupIndex;\n";
813 }
814
815 computeStream << "};\n\n";
816
Jamie Madillbd044ed2017-06-05 12:59:21 -0400817 const sh::WorkGroupSize &localSize = computeShaderGL->getWorkGroupSize(context);
Xinghua Caob1239382016-12-13 15:07:05 +0800818 computeStream << "[numthreads(" << localSize[0] << ", " << localSize[1] << ", " << localSize[2]
819 << ")]\n";
820
821 computeStream << "void main(CS_INPUT input)\n"
822 << "{\n";
823
824 if (usesWorkGroupID)
825 {
826 computeStream << " gl_WorkGroupID = input.dx_WorkGroupID;\n";
827 }
828 if (usesLocalInvocationID)
829 {
830 computeStream << " gl_LocalInvocationID = input.dx_LocalInvocationID;\n";
831 }
832 if (usesGlobalInvocationID)
833 {
834 computeStream << " gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n";
835 }
836 if (usesLocalInvocationIndex)
837 {
838 computeStream << " gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n";
839 }
840
841 computeStream << "\n"
842 << " gl_main();\n"
843 << "}\n";
844
845 return computeStream.str();
846}
847
Jamie Madill192745a2016-12-22 15:58:21 -0500848std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking,
849 const BuiltinVaryingsD3D &builtinsD3D) const
Jamie Madill5f562732014-02-14 16:41:24 -0500850{
Jamie Madill9fc36822015-11-18 13:08:07 -0500851 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400852
Jamie Madill4e31ad52015-10-29 10:32:57 -0400853 std::stringstream preambleStream;
854
Jamie Madill192745a2016-12-22 15:58:21 -0500855 const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX];
Jamie Madill9fc36822015-11-18 13:08:07 -0500856
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400857 preambleStream << "struct GS_INPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500858 generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
859 preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400860 preambleStream << "\n"
861 << "struct GS_OUTPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500862 generateVaryingLinkHLSL(varyingPacking, builtinsD3D[SHADER_GEOMETRY],
863 builtinsD3D.usesPointSize(), preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400864 preambleStream
865 << "\n"
866 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
867 << "{\n"
868 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400869
Jamie Madill192745a2016-12-22 15:58:21 -0500870 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400871 {
872 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
873 }
874
Jamie Madill9fc36822015-11-18 13:08:07 -0500875 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400876 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500877 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill55c25d02015-11-18 13:08:08 -0500878 if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400879 {
880 preambleStream << "flat";
881 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500882 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400883 }
884
Jamie Madill192745a2016-12-22 15:58:21 -0500885 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400886 {
887 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
888 }
889
890 // Only write the dx_Position if we aren't using point sprites
891 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
892 << " output.dx_Position = input.dx_Position;\n"
893 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
894 << "}\n";
895
896 return preambleStream.str();
897}
898
899std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
Jamie Madill9082b982016-04-27 15:21:51 -0400900 const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400901 const gl::ProgramState &programData,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800902 const bool useViewScale,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400903 const std::string &preambleString) const
904{
905 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500906
Jamie Madill334d6152015-10-22 14:00:28 -0400907 std::stringstream shaderStream;
908
Jamie Madill4e31ad52015-10-29 10:32:57 -0400909 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
910 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500911
Jamie Madill76f8fa62015-10-29 10:32:56 -0400912 const char *inputPT = nullptr;
913 const char *outputPT = nullptr;
914 int inputSize = 0;
915 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500916
Jamie Madill76f8fa62015-10-29 10:32:56 -0400917 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500918 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400919 case PRIMITIVE_POINTS:
920 inputPT = "point";
921 outputPT = "Triangle";
922 inputSize = 1;
923 maxVertexOutput = 4;
924 break;
925
926 case PRIMITIVE_LINES:
927 case PRIMITIVE_LINE_STRIP:
928 case PRIMITIVE_LINE_LOOP:
929 inputPT = "line";
930 outputPT = "Line";
931 inputSize = 2;
932 maxVertexOutput = 2;
933 break;
934
935 case PRIMITIVE_TRIANGLES:
936 case PRIMITIVE_TRIANGLE_STRIP:
937 case PRIMITIVE_TRIANGLE_FAN:
938 inputPT = "triangle";
939 outputPT = "Triangle";
940 inputSize = 3;
941 maxVertexOutput = 3;
942 break;
943
944 default:
945 UNREACHABLE();
946 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500947 }
948
Jamie Madill76f8fa62015-10-29 10:32:56 -0400949 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500950 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400951 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
952 "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800953 "uniform float4 dx_ViewCoords : register(c1);\n";
954
955 if (useViewScale)
956 {
957 shaderStream << "uniform float2 dx_ViewScale : register(c3);\n";
958 }
959
960 shaderStream << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400961 "static float2 pointSpriteCorners[] = \n"
962 "{\n"
963 " float2( 0.5f, -0.5f),\n"
964 " float2( 0.5f, 0.5f),\n"
965 " float2(-0.5f, -0.5f),\n"
966 " float2(-0.5f, 0.5f)\n"
967 "};\n"
968 "\n"
969 "static float2 pointSpriteTexcoords[] = \n"
970 "{\n"
971 " float2(1.0f, 1.0f),\n"
972 " float2(1.0f, 0.0f),\n"
973 " float2(0.0f, 1.0f),\n"
974 " float2(0.0f, 0.0f)\n"
975 "};\n"
976 "\n"
977 "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700978 << static_cast<int>(data.getCaps().minAliasedPointSize)
Jamie Madill76f8fa62015-10-29 10:32:56 -0400979 << ".0f;\n"
980 "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700981 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400982 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500983 }
984
Jamie Madill4e31ad52015-10-29 10:32:57 -0400985 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400986 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400987 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
988
989 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
990 {
991 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
992 }
993
994 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400995 << "{\n"
996 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500997
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400998 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
999 {
1000 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
1001 }
1002 else
1003 {
1004 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
1005 }
1006
Jamie Madill4e31ad52015-10-29 10:32:57 -04001007 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -05001008 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001009 shaderStream << " copyVertex(output, input[" << vertexIndex
1010 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001011
1012 if (!pointSprites)
1013 {
1014 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001015 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001016 }
1017 }
1018
1019 if (pointSprites)
1020 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001021 shaderStream << "\n"
1022 " float4 dx_Position = input[0].dx_Position;\n"
1023 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
1024 "maxPointSize);\n"
1025 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
1026 "dx_ViewCoords.y) * dx_Position.w;\n";
1027
1028 for (int corner = 0; corner < 4; corner++)
1029 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001030 if (useViewScale)
1031 {
1032 shaderStream << " \n"
1033 " output.dx_Position = dx_Position + float4(1.0f, "
1034 "-dx_ViewScale.y, 1.0f, 1.0f)"
1035 " * float4(pointSpriteCorners["
1036 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1037 }
1038 else
1039 {
1040 shaderStream << "\n"
1041 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
1042 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1043 }
Jamie Madill76f8fa62015-10-29 10:32:56 -04001044
1045 if (usesPointCoord)
1046 {
1047 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
1048 << "];\n";
1049 }
1050
1051 shaderStream << " outStream.Append(output);\n";
1052 }
Jamie Madill5f562732014-02-14 16:41:24 -05001053 }
1054
Jamie Madill334d6152015-10-22 14:00:28 -04001055 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001056 " outStream.RestartStrip();\n"
1057 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001058
Jamie Madill334d6152015-10-22 14:00:28 -04001059 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -05001060}
1061
1062// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001063std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001064{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001065 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001066 {
1067 return "_" + name;
1068 }
1069
1070 return name;
1071}
1072
Jamie Madill334d6152015-10-22 14:00:28 -04001073std::string DynamicHLSL::generateAttributeConversionHLSL(
1074 gl::VertexFormatType vertexFormatType,
1075 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001076{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001077 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -04001078 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001079
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001080 // Matrix
1081 if (IsMatrixType(shaderAttrib.type))
1082 {
Jamie Madill8664b062014-02-14 16:41:29 -05001083 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001084 }
1085
Jamie Madillf2575982014-06-25 16:04:54 -04001086 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001087 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001088
Jamie Madill8664b062014-02-14 16:41:29 -05001089 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -04001090 bool requiresTypeConversion =
1091 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001092
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001093 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001094 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001095 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001096 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001097 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001098 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001099
1100 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001101 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001102}
Jamie Madille39a3f02015-11-17 20:42:15 -05001103
Jamie Madill9082b982016-04-27 15:21:51 -04001104void DynamicHLSL::getPixelShaderOutputKey(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001105 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -05001106 const ProgramD3DMetadata &metadata,
1107 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1108{
1109 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1110 // - with a 3.0 context, the output color is copied to channel 0
1111 // - with a 2.0 context, the output color is broadcast to all channels
1112 bool broadcast = metadata.usesBroadcast(data);
1113 const unsigned int numRenderTargets =
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001114 (broadcast || metadata.usesMultipleFragmentOuts() ? data.getCaps().maxDrawBuffers : 1);
Jamie Madille39a3f02015-11-17 20:42:15 -05001115
1116 if (metadata.getMajorShaderVersion() < 300)
1117 {
1118 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1119 renderTargetIndex++)
1120 {
1121 PixelShaderOutputVariable outputKeyVariable;
1122 outputKeyVariable.type = GL_FLOAT_VEC4;
1123 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1124 outputKeyVariable.source =
1125 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1126 outputKeyVariable.outputIndex = renderTargetIndex;
1127
1128 outPixelShaderKey->push_back(outputKeyVariable);
1129 }
1130 }
1131 else
1132 {
1133 const auto &shaderOutputVars =
1134 metadata.getFragmentShader()->getData().getActiveOutputVariables();
1135
jchen1015015f72017-03-16 13:54:21 +08001136 for (auto outputPair : programData.getOutputLocations())
Jamie Madille39a3f02015-11-17 20:42:15 -05001137 {
1138 const VariableLocation &outputLocation = outputPair.second;
1139 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1140 const std::string &variableName = "out_" + outputLocation.name;
1141 const std::string &elementString =
1142 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
1143
1144 ASSERT(outputVariable.staticUse);
1145
1146 PixelShaderOutputVariable outputKeyVariable;
1147 outputKeyVariable.type = outputVariable.type;
1148 outputKeyVariable.name = variableName + elementString;
1149 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
1150 outputKeyVariable.outputIndex = outputPair.first;
1151
1152 outPixelShaderKey->push_back(outputKeyVariable);
1153 }
1154 }
1155}
1156
Jamie Madill192745a2016-12-22 15:58:21 -05001157// BuiltinVarying Implementation.
1158BuiltinVarying::BuiltinVarying() : enabled(false), index(0), systemValue(false)
1159{
1160}
1161
1162std::string BuiltinVarying::str() const
1163{
1164 return (systemValue ? semantic : (semantic + Str(index)));
1165}
1166
1167void BuiltinVarying::enableSystem(const std::string &systemValueSemantic)
1168{
1169 enabled = true;
1170 semantic = systemValueSemantic;
1171 systemValue = true;
1172}
1173
1174void BuiltinVarying::enable(const std::string &semanticVal, unsigned int indexVal)
1175{
1176 enabled = true;
1177 semantic = semanticVal;
1178 index = indexVal;
1179}
1180
1181// BuiltinVaryingsD3D Implementation.
1182BuiltinVaryingsD3D::BuiltinVaryingsD3D(const ProgramD3DMetadata &metadata,
1183 const VaryingPacking &packing)
1184{
1185 updateBuiltins(SHADER_VERTEX, metadata, packing);
1186 updateBuiltins(SHADER_PIXEL, metadata, packing);
1187 if (metadata.getRendererMajorShaderModel() >= 4)
1188 {
1189 updateBuiltins(SHADER_GEOMETRY, metadata, packing);
1190 }
1191}
1192
1193void BuiltinVaryingsD3D::updateBuiltins(ShaderType shaderType,
1194 const ProgramD3DMetadata &metadata,
1195 const VaryingPacking &packing)
1196{
1197 const std::string &userSemantic = GetVaryingSemantic(metadata.getRendererMajorShaderModel(),
1198 metadata.usesSystemValuePointSize());
1199
1200 unsigned int reservedSemanticIndex = packing.getMaxSemanticIndex();
1201
1202 BuiltinInfo *builtins = &mBuiltinInfo[shaderType];
1203
1204 if (metadata.getRendererMajorShaderModel() >= 4)
1205 {
1206 builtins->dxPosition.enableSystem("SV_Position");
1207 }
1208 else if (shaderType == SHADER_PIXEL)
1209 {
1210 builtins->dxPosition.enableSystem("VPOS");
1211 }
1212 else
1213 {
1214 builtins->dxPosition.enableSystem("POSITION");
1215 }
1216
1217 if (metadata.usesTransformFeedbackGLPosition())
1218 {
1219 builtins->glPosition.enable(userSemantic, reservedSemanticIndex++);
1220 }
1221
1222 if (metadata.usesFragCoord())
1223 {
1224 builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++);
1225 }
1226
1227 if (shaderType == SHADER_VERTEX ? metadata.addsPointCoordToVertexShader()
1228 : metadata.usesPointCoord())
1229 {
1230 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
1231 // In D3D11 we manually compute gl_PointCoord in the GS.
1232 if (metadata.getRendererMajorShaderModel() >= 4)
1233 {
1234 builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++);
1235 }
1236 else
1237 {
1238 builtins->glPointCoord.enable("TEXCOORD", 0);
1239 }
1240 }
1241
1242 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
1243 if (metadata.usesSystemValuePointSize() &&
1244 (shaderType != SHADER_PIXEL || metadata.getRendererMajorShaderModel() >= 4))
1245 {
1246 builtins->glPointSize.enableSystem("PSIZE");
1247 }
1248}
1249
Jamie Madill334d6152015-10-22 14:00:28 -04001250} // namespace rx