blob: 27d29948b7fd08eb99a688b0030070553d0ad17d [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
Jamie Madill10bed9f2017-06-05 12:59:22 -040030// This class needs to match OutputHLSL::decorate
31class DecorateVariable final : angle::NonCopyable
32{
33 public:
34 explicit DecorateVariable(const std::string &str) : mName(str) {}
35 const std::string &getName() const { return mName; }
36
37 private:
38 const std::string &mName;
39};
40
41std::ostream &operator<<(std::ostream &o, const DecorateVariable &dv)
42{
43 if (dv.getName().compare(0, 3, "gl_") != 0)
44 {
45 o << "_";
46 }
47 o << dv.getName();
48 return o;
49}
50
51const char *HLSLComponentTypeString(GLenum componentType)
Jamie Madill8664b062014-02-14 16:41:29 -050052{
53 switch (componentType)
54 {
Jamie Madill334d6152015-10-22 14:00:28 -040055 case GL_UNSIGNED_INT:
56 return "uint";
57 case GL_INT:
58 return "int";
59 case GL_UNSIGNED_NORMALIZED:
60 case GL_SIGNED_NORMALIZED:
61 case GL_FLOAT:
62 return "float";
63 default:
64 UNREACHABLE();
65 return "not-component-type";
Jamie Madill8664b062014-02-14 16:41:29 -050066 }
Jamie Madill5f562732014-02-14 16:41:24 -050067}
68
Jamie Madill10bed9f2017-06-05 12:59:22 -040069void HLSLComponentTypeString(std::ostringstream &ostream, GLenum componentType, int componentCount)
Jamie Madill8664b062014-02-14 16:41:29 -050070{
Jamie Madill10bed9f2017-06-05 12:59:22 -040071 ostream << HLSLComponentTypeString(componentType);
72 if (componentCount > 1)
73 {
74 ostream << componentCount;
75 }
Jamie Madill8664b062014-02-14 16:41:29 -050076}
77
Jamie Madill10bed9f2017-06-05 12:59:22 -040078const char *HLSLMatrixTypeString(GLenum type)
Jamie Madill8664b062014-02-14 16:41:29 -050079{
80 switch (type)
81 {
Jamie Madill334d6152015-10-22 14:00:28 -040082 case GL_FLOAT_MAT2:
83 return "float2x2";
84 case GL_FLOAT_MAT3:
85 return "float3x3";
86 case GL_FLOAT_MAT4:
87 return "float4x4";
88 case GL_FLOAT_MAT2x3:
89 return "float2x3";
90 case GL_FLOAT_MAT3x2:
91 return "float3x2";
92 case GL_FLOAT_MAT2x4:
93 return "float2x4";
94 case GL_FLOAT_MAT4x2:
95 return "float4x2";
96 case GL_FLOAT_MAT3x4:
97 return "float3x4";
98 case GL_FLOAT_MAT4x3:
99 return "float4x3";
100 default:
101 UNREACHABLE();
102 return "not-matrix-type";
Jamie Madill8664b062014-02-14 16:41:29 -0500103 }
104}
105
Jamie Madill10bed9f2017-06-05 12:59:22 -0400106void HLSLTypeString(std::ostringstream &ostream, GLenum type)
Jamie Madill8664b062014-02-14 16:41:29 -0500107{
108 if (gl::IsMatrixType(type))
109 {
Jamie Madill10bed9f2017-06-05 12:59:22 -0400110 ostream << HLSLMatrixTypeString(type);
111 return;
Jamie Madill8664b062014-02-14 16:41:29 -0500112 }
113
Jamie Madill10bed9f2017-06-05 12:59:22 -0400114 HLSLComponentTypeString(ostream, gl::VariableComponentType(type),
115 gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -0500116}
117
Jamie Madill334d6152015-10-22 14:00:28 -0400118const PixelShaderOutputVariable *FindOutputAtLocation(
119 const std::vector<PixelShaderOutputVariable> &outputVariables,
120 unsigned int location)
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400121{
122 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
123 {
124 if (outputVariables[variableIndex].outputIndex == location)
125 {
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000126 return &outputVariables[variableIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400127 }
128 }
129
Jamie Madill334d6152015-10-22 14:00:28 -0400130 return nullptr;
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400131}
132
Jamie Madill10bed9f2017-06-05 12:59:22 -0400133void WriteArrayString(std::ostringstream &strstr, unsigned int i)
Jamie Madill55c25d02015-11-18 13:08:08 -0500134{
135 static_assert(GL_INVALID_INDEX == UINT_MAX,
136 "GL_INVALID_INDEX must be equal to the max unsigned int.");
137 if (i == UINT_MAX)
138 {
139 return;
140 }
141
142 strstr << "[";
143 strstr << i;
144 strstr << "]";
145}
146
Jamie Madill51c47682016-10-25 15:50:14 -0400147constexpr const char *VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
148constexpr const char *PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400149} // anonymous namespace
Jamie Madill4cff2472015-08-21 16:53:18 -0400150
Jamie Madill9fc36822015-11-18 13:08:07 -0500151// DynamicHLSL implementation
152
Jamie Madill65345da2015-11-13 11:25:23 -0500153DynamicHLSL::DynamicHLSL(RendererD3D *const renderer) : mRenderer(renderer)
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000154{
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000155}
156
Jamie Madill334d6152015-10-22 14:00:28 -0400157std::string DynamicHLSL::generateVertexShaderForInputLayout(
158 const std::string &sourceShader,
159 const InputLayout &inputLayout,
160 const std::vector<sh::Attribute> &shaderAttributes) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500161{
Jamie Madill10bed9f2017-06-05 12:59:22 -0400162 std::ostringstream structStream;
163 std::ostringstream initStream;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500164
Jamie Madill334d6152015-10-22 14:00:28 -0400165 structStream << "struct VS_INPUT\n"
166 << "{\n";
167
168 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400169 unsigned int inputIndex = 0;
170
Cooper Partine6d14cc2015-02-20 12:32:58 -0800171 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
172 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
173 // must be used.
174 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
Jamie Madill334d6152015-10-22 14:00:28 -0400175 bool useInstancedPointSpriteEmulation =
176 usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Cooper Partine6d14cc2015-02-20 12:32:58 -0800177
178 // Instanced PointSprite emulation requires additional entries in the
179 // VS_INPUT structure to support the vertices that make up the quad vertices.
180 // These values must be in sync with the cooresponding values added during inputlayout creation
181 // in InputLayoutCache::applyVertexBuffers().
182 //
183 // The additional entries must appear first in the VS_INPUT layout because
184 // Windows Phone 8 era devices require per vertex data to physically come
185 // before per instance data in the shader.
186 if (useInstancedPointSpriteEmulation)
187 {
Jamie Madill334d6152015-10-22 14:00:28 -0400188 structStream << " float3 spriteVertexPos : SPRITEPOSITION0;\n"
189 << " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
Cooper Partine6d14cc2015-02-20 12:32:58 -0800190 }
191
Jamie Madill3da79b72015-04-27 11:09:17 -0400192 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex)
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500193 {
Jamie Madillf2575982014-06-25 16:04:54 -0400194 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500195 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500196 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400197 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400198 VertexFormatType vertexFormatType =
199 inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID;
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400200
Jamie Madill3b7e2052014-03-17 09:47:43 -0400201 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500202 if (IsMatrixType(shaderAttribute.type))
203 {
204 // Matrix types are always transposed
Jamie Madill334d6152015-10-22 14:00:28 -0400205 structStream << " "
206 << HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500207 }
208 else
209 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400210 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000211
Corentin Wallezb076add2016-01-11 16:45:46 -0500212 if (shaderAttribute.name == "gl_InstanceID" ||
213 shaderAttribute.name == "gl_VertexID")
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000214 {
Corentin Wallezb076add2016-01-11 16:45:46 -0500215 // The input types of the instance ID and vertex ID in HLSL (uint) differs from
216 // the ones in ESSL (int).
Jamie Madill334d6152015-10-22 14:00:28 -0400217 structStream << " uint";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000218 }
219 else
220 {
Jamie Madill10bed9f2017-06-05 12:59:22 -0400221 structStream << " ";
222 HLSLComponentTypeString(structStream, componentType,
223 VariableComponentCount(shaderAttribute.type));
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000224 }
Jamie Madill8664b062014-02-14 16:41:29 -0500225 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500226
Jamie Madill10bed9f2017-06-05 12:59:22 -0400227 structStream << " " << DecorateVariable(shaderAttribute.name) << " : ";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000228
229 if (shaderAttribute.name == "gl_InstanceID")
230 {
Jamie Madill334d6152015-10-22 14:00:28 -0400231 structStream << "SV_InstanceID";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000232 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500233 else if (shaderAttribute.name == "gl_VertexID")
234 {
235 structStream << "SV_VertexID";
236 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000237 else
238 {
Jamie Madill334d6152015-10-22 14:00:28 -0400239 structStream << "TEXCOORD" << semanticIndex;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000240 semanticIndex += VariableRegisterCount(shaderAttribute.type);
241 }
242
Jamie Madill334d6152015-10-22 14:00:28 -0400243 structStream << ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500244
Jamie Madill3b7e2052014-03-17 09:47:43 -0400245 // HLSL code for initialization
Jamie Madill10bed9f2017-06-05 12:59:22 -0400246 initStream << " " << DecorateVariable(shaderAttribute.name) << " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500247
248 // Mismatched vertex attribute to vertex input may result in an undefined
249 // data reinterpretation (eg for pure integer->float, float->pure integer)
250 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400251 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400252 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500253 {
Jamie Madill10bed9f2017-06-05 12:59:22 -0400254 GenerateAttributeConversionHLSL(vertexFormatType, shaderAttribute, initStream);
Jamie Madill8664b062014-02-14 16:41:29 -0500255 }
256 else
257 {
Jamie Madill10bed9f2017-06-05 12:59:22 -0400258 initStream << "input." << DecorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500259 }
260
Jamie Madill334d6152015-10-22 14:00:28 -0400261 initStream << ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400262
Jamie Madillac0a2672014-04-11 13:33:56 -0400263 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
264 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500265 }
266
Jamie Madill334d6152015-10-22 14:00:28 -0400267 structStream << "};\n"
268 "\n"
269 "void initAttributes(VS_INPUT input)\n"
270 "{\n"
271 << initStream.str() << "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400272
273 std::string vertexHLSL(sourceShader);
274
275 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
Jamie Madill51c47682016-10-25 15:50:14 -0400276 vertexHLSL.replace(copyInsertionPos, strlen(VERTEX_ATTRIBUTE_STUB_STRING), structStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400277
278 return vertexHLSL;
279}
280
Jamie Madill334d6152015-10-22 14:00:28 -0400281std::string DynamicHLSL::generatePixelShaderForOutputSignature(
282 const std::string &sourceShader,
283 const std::vector<PixelShaderOutputVariable> &outputVariables,
284 bool usesFragDepth,
285 const std::vector<GLenum> &outputLayout) const
Geoff Lang04fb89a2014-06-09 15:05:36 -0400286{
Jamie Madill334d6152015-10-22 14:00:28 -0400287 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang04fb89a2014-06-09 15:05:36 -0400288 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
Jamie Madill334d6152015-10-22 14:00:28 -0400289 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400290
Jamie Madill10bed9f2017-06-05 12:59:22 -0400291 std::ostringstream declarationStream;
292 std::ostringstream copyStream;
Jamie Madill334d6152015-10-22 14:00:28 -0400293
294 declarationStream << "struct PS_OUTPUT\n"
295 "{\n";
Geoff Lang4ace4232014-06-18 19:12:48 -0400296
Jamie Madill66a08192017-02-03 15:24:25 -0500297 // Workaround for HLSL 3.x: We can't do a depth/stencil only render, the runtime will complain.
298 size_t numOutputs = outputLayout.empty() ? 1u : outputLayout.size();
299 const PixelShaderOutputVariable defaultOutput(GL_FLOAT_VEC4, "dummy", "float4(0, 0, 0, 1)", 0);
300
301 for (size_t layoutIndex = 0; layoutIndex < numOutputs; ++layoutIndex)
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400302 {
Jamie Madill66a08192017-02-03 15:24:25 -0500303 GLenum binding = outputLayout.empty() ? GL_COLOR_ATTACHMENT0 : outputLayout[layoutIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400304
305 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400306 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400307 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
308
Jamie Madill334d6152015-10-22 14:00:28 -0400309 const PixelShaderOutputVariable *outputVariable =
Jamie Madill66a08192017-02-03 15:24:25 -0500310 outputLayout.empty() ? &defaultOutput
311 : FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400312
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000313 // OpenGL ES 3.0 spec $4.2.1
Jamie Madill334d6152015-10-22 14:00:28 -0400314 // If [...] not all user-defined output variables are written, the values of fragment
315 // colors
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000316 // corresponding to unwritten variables are similarly undefined.
317 if (outputVariable)
318 {
Jamie Madill10bed9f2017-06-05 12:59:22 -0400319 declarationStream << " ";
320 HLSLTypeString(declarationStream, outputVariable->type);
321 declarationStream << " " << outputVariable->name << " : " << targetSemantic
Jamie Madill334d6152015-10-22 14:00:28 -0400322 << static_cast<int>(layoutIndex) << ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400323
Jamie Madill334d6152015-10-22 14:00:28 -0400324 copyStream << " output." << outputVariable->name << " = "
325 << outputVariable->source << ";\n";
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000326 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400327 }
328 }
329
330 if (usesFragDepth)
331 {
Jamie Madill334d6152015-10-22 14:00:28 -0400332 declarationStream << " float gl_Depth : " << depthSemantic << ";\n";
333 copyStream << " output.gl_Depth = gl_Depth; \n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400334 }
335
Jamie Madill334d6152015-10-22 14:00:28 -0400336 declarationStream << "};\n"
337 "\n"
338 "PS_OUTPUT generateOutput()\n"
339 "{\n"
340 " PS_OUTPUT output;\n"
341 << copyStream.str() << " return output;\n"
342 "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400343
344 std::string pixelHLSL(sourceShader);
345
346 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
Jamie Madill51c47682016-10-25 15:50:14 -0400347 pixelHLSL.replace(outputInsertionPos, strlen(PIXEL_OUTPUT_STUB_STRING),
Jamie Madill334d6152015-10-22 14:00:28 -0400348 declarationStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400349
350 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500351}
352
Jamie Madill192745a2016-12-22 15:58:21 -0500353void DynamicHLSL::generateVaryingLinkHLSL(const VaryingPacking &varyingPacking,
354 const BuiltinInfo &builtins,
355 bool programUsesPointSize,
Jamie Madill10bed9f2017-06-05 12:59:22 -0400356 std::ostringstream &hlslStream) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400357{
Jamie Madill9fc36822015-11-18 13:08:07 -0500358 ASSERT(builtins.dxPosition.enabled);
Jamie Madill192745a2016-12-22 15:58:21 -0500359 hlslStream << "{\n"
Jamie Madill9fc36822015-11-18 13:08:07 -0500360 << " float4 dx_Position : " << builtins.dxPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700361
Jamie Madill9fc36822015-11-18 13:08:07 -0500362 if (builtins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700363 {
Jamie Madill192745a2016-12-22 15:58:21 -0500364 hlslStream << " float4 gl_Position : " << builtins.glPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700365 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400366
Jamie Madill9fc36822015-11-18 13:08:07 -0500367 if (builtins.glFragCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400368 {
Jamie Madill192745a2016-12-22 15:58:21 -0500369 hlslStream << " float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400370 }
371
Jamie Madill9fc36822015-11-18 13:08:07 -0500372 if (builtins.glPointCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400373 {
Jamie Madill192745a2016-12-22 15:58:21 -0500374 hlslStream << " float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400375 }
376
Jamie Madill9fc36822015-11-18 13:08:07 -0500377 if (builtins.glPointSize.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400378 {
Jamie Madill192745a2016-12-22 15:58:21 -0500379 hlslStream << " float gl_PointSize : " << builtins.glPointSize.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400380 }
381
Jamie Madill192745a2016-12-22 15:58:21 -0500382 std::string varyingSemantic =
383 GetVaryingSemantic(mRenderer->getMajorShaderModel(), programUsesPointSize);
Austin Kinross8b695ee2015-03-12 13:12:20 -0700384
Jamie Madill192745a2016-12-22 15:58:21 -0500385 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
386 {
387 const auto &varying = *registerInfo.packedVarying->varying;
388 ASSERT(!varying.isStruct());
389
390 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many
391 // registers being used.
392 // For example, if there are N registers, and we have N vec3 varyings and 1 float
393 // varying, then D3D will pack them into N registers.
394 // If the float varying has the 'nointerpolation' modifier on it then we would need
395 // N + 1 registers, and D3D compilation will fail.
396
397 switch (registerInfo.packedVarying->interpolation)
398 {
399 case sh::INTERPOLATION_SMOOTH:
400 hlslStream << " ";
401 break;
402 case sh::INTERPOLATION_FLAT:
403 hlslStream << " nointerpolation ";
404 break;
405 case sh::INTERPOLATION_CENTROID:
406 hlslStream << " centroid ";
407 break;
408 default:
409 UNREACHABLE();
410 }
411
412 GLenum transposedType = gl::TransposeMatrixType(varying.type);
413 GLenum componentType = gl::VariableComponentType(transposedType);
414 int columnCount = gl::VariableColumnCount(transposedType);
Jamie Madill10bed9f2017-06-05 12:59:22 -0400415 HLSLComponentTypeString(hlslStream, componentType, columnCount);
Jamie Madill192745a2016-12-22 15:58:21 -0500416 unsigned int semanticIndex = registerInfo.semanticIndex;
417 hlslStream << " v" << semanticIndex << " : " << varyingSemantic << semanticIndex << ";\n";
418 }
419
420 hlslStream << "};\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400421}
422
Jamie Madillbd044ed2017-06-05 12:59:21 -0400423void DynamicHLSL::generateShaderLinkHLSL(const gl::Context *context,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400424 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500425 const ProgramD3DMetadata &programMetadata,
Jamie Madill9fc36822015-11-18 13:08:07 -0500426 const VaryingPacking &varyingPacking,
Jamie Madill192745a2016-12-22 15:58:21 -0500427 const BuiltinVaryingsD3D &builtinsD3D,
Jamie Madill334d6152015-10-22 14:00:28 -0400428 std::string *pixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -0500429 std::string *vertexHLSL) const
Jamie Madill5f562732014-02-14 16:41:24 -0500430{
Jamie Madill334d6152015-10-22 14:00:28 -0400431 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500432
Jamie Madillbd044ed2017-06-05 12:59:21 -0400433 const auto &data = context->getContextState();
434 gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
435 gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
Jamie Madill91445bc2015-09-23 16:47:53 -0400436 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700437 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400438
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800439 // usesViewScale() isn't supported in the D3D9 renderer
440 ASSERT(shaderModel >= 4 || !programMetadata.usesViewScale());
441
Jamie Madill334d6152015-10-22 14:00:28 -0400442 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500443 programMetadata.usesPointSize() &&
444 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400445
Jamie Madill14e95b32015-05-07 10:10:41 -0400446 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400447 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500448
Jamie Madill10bed9f2017-06-05 12:59:22 -0400449 std::ostringstream vertexStream;
Jamie Madillbd044ed2017-06-05 12:59:21 -0400450 vertexStream << vertexShaderGL->getTranslatedSource(context);
Jamie Madill334d6152015-10-22 14:00:28 -0400451
Cooper Partine6664f02015-01-09 16:22:24 -0800452 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400453 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800454 if (useInstancedPointSpriteEmulation)
455 {
Jamie Madill334d6152015-10-22 14:00:28 -0400456 vertexStream << "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700457 << static_cast<int>(data.getCaps().minAliasedPointSize) << ".0f;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400458 << "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700459 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800460 }
461
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500462 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill51c47682016-10-25 15:50:14 -0400463 vertexStream << "\n" << std::string(VERTEX_ATTRIBUTE_STUB_STRING) << "\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500464
Jamie Madill192745a2016-12-22 15:58:21 -0500465 const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX];
466
Jamie Madill9fc36822015-11-18 13:08:07 -0500467 // Write the HLSL input/output declarations
468 vertexStream << "struct VS_OUTPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500469 generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
470 vertexStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400471 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400472 << "VS_OUTPUT main(VS_INPUT input)\n"
473 << "{\n"
474 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500475
Jamie Madill334d6152015-10-22 14:00:28 -0400476 vertexStream << "\n"
477 << " gl_main();\n"
478 << "\n"
479 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700480
Jamie Madill9fc36822015-11-18 13:08:07 -0500481 if (vertexBuiltins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700482 {
Jamie Madill334d6152015-10-22 14:00:28 -0400483 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700484 }
Jamie Madill37997142015-01-28 10:06:34 -0500485
Austin Kinross4fd18b12014-12-22 12:32:05 -0800486 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400487 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500488 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800489 vertexStream << " output.dx_Position.x = gl_Position.x;\n";
490
491 if (programMetadata.usesViewScale())
492 {
493 // This code assumes that dx_ViewScale.y = -1.0f when rendering to texture, and +1.0f
494 // when rendering to the default framebuffer. No other values are valid.
495 vertexStream << " output.dx_Position.y = dx_ViewScale.y * gl_Position.y;\n";
496 }
497 else
498 {
499 vertexStream << " output.dx_Position.y = - gl_Position.y;\n";
500 }
501
502 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400503 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500504 }
505 else
506 {
Jamie Madill334d6152015-10-22 14:00:28 -0400507 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800508 "dx_ViewAdjust.x * gl_Position.w;\n";
509
510 // If usesViewScale() is true and we're using the D3D11 renderer via Feature Level 9_*,
511 // then we need to multiply the gl_Position.y by the viewScale.
512 // usesViewScale() isn't supported when using the D3D9 renderer.
513 if (programMetadata.usesViewScale() &&
514 (shaderModel >= 4 && mRenderer->getShaderModelSuffix() != ""))
515 {
516 vertexStream << " output.dx_Position.y = dx_ViewScale.y * (gl_Position.y * "
517 "dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n";
518 }
519 else
520 {
521 vertexStream << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
522 "dx_ViewAdjust.y * gl_Position.w);\n";
523 }
524
525 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400526 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500527 }
528
Austin Kinross8b695ee2015-03-12 13:12:20 -0700529 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
Jamie Madill9fc36822015-11-18 13:08:07 -0500530 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500531 {
Jamie Madill334d6152015-10-22 14:00:28 -0400532 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500533 }
534
Jamie Madill9fc36822015-11-18 13:08:07 -0500535 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500536 {
Jamie Madill334d6152015-10-22 14:00:28 -0400537 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500538 }
539
Jamie Madill9fc36822015-11-18 13:08:07 -0500540 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500541 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500542 const auto &packedVarying = *registerInfo.packedVarying;
543 const auto &varying = *packedVarying.varying;
544 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400545
Jamie Madill55c25d02015-11-18 13:08:08 -0500546 vertexStream << " output.v" << registerInfo.semanticIndex << " = ";
547
548 if (packedVarying.isStructField())
549 {
Jamie Madill10bed9f2017-06-05 12:59:22 -0400550 vertexStream << DecorateVariable(packedVarying.parentStructName) << ".";
Jamie Madill55c25d02015-11-18 13:08:08 -0500551 }
552
Jamie Madill10bed9f2017-06-05 12:59:22 -0400553 vertexStream << DecorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400554
555 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400556 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500557 WriteArrayString(vertexStream, registerInfo.varyingArrayIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500558 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400559
Jamie Madill55c25d02015-11-18 13:08:08 -0500560 if (VariableRowCount(varying.type) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400561 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500562 WriteArrayString(vertexStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400563 }
564
565 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500566 }
567
Cooper Partine6664f02015-01-09 16:22:24 -0800568 // Instanced PointSprite emulation requires additional entries to calculate
569 // the final output vertex positions of the quad that represents each sprite.
570 if (useInstancedPointSpriteEmulation)
571 {
Jamie Madill334d6152015-10-22 14:00:28 -0400572 vertexStream << "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800573 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n";
574
575 vertexStream << " output.dx_Position.x += (input.spriteVertexPos.x * gl_PointSize / "
576 "(dx_ViewCoords.x*2)) * output.dx_Position.w;";
577
578 if (programMetadata.usesViewScale())
579 {
580 // Multiply by ViewScale to invert the rendering when appropriate
581 vertexStream << " output.dx_Position.y += (-dx_ViewScale.y * "
582 "input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2)) * "
583 "output.dx_Position.w;";
584 }
585 else
586 {
587 vertexStream << " output.dx_Position.y += (input.spriteVertexPos.y * gl_PointSize / "
588 "(dx_ViewCoords.y*2)) * output.dx_Position.w;";
589 }
590
591 vertexStream
592 << " output.dx_Position.z += input.spriteVertexPos.z * output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800593
Jamie Madille39a3f02015-11-17 20:42:15 -0500594 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800595 {
Jamie Madill334d6152015-10-22 14:00:28 -0400596 vertexStream << "\n"
597 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800598 }
599 }
600
Cooper Partin7c89d242015-10-13 12:45:59 -0700601 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
602 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
603 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500604 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700605 {
606 ASSERT(!useInstancedPointSpriteEmulation);
607 vertexStream << "\n"
608 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
609 }
610
Jamie Madill334d6152015-10-22 14:00:28 -0400611 vertexStream << "\n"
612 << " return output;\n"
613 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500614
Jamie Madill192745a2016-12-22 15:58:21 -0500615 const auto &pixelBuiltins = builtinsD3D[SHADER_PIXEL];
616
Jamie Madill10bed9f2017-06-05 12:59:22 -0400617 std::ostringstream pixelStream;
Jamie Madillbd044ed2017-06-05 12:59:21 -0400618 pixelStream << fragmentShaderGL->getTranslatedSource(context);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400619 pixelStream << "struct PS_INPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500620 generateVaryingLinkHLSL(varyingPacking, pixelBuiltins, builtinsD3D.usesPointSize(),
621 pixelStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400622 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500623
Jamie Madill51c47682016-10-25 15:50:14 -0400624 pixelStream << std::string(PIXEL_OUTPUT_STUB_STRING) << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500625
Jamie Madill334d6152015-10-22 14:00:28 -0400626 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500627 {
628 if (shaderModel >= 4)
629 {
Jamie Madill334d6152015-10-22 14:00:28 -0400630 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
631 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500632 }
633 else
634 {
Jamie Madill334d6152015-10-22 14:00:28 -0400635 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
636 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500637 }
638 }
639 else
640 {
Jamie Madill334d6152015-10-22 14:00:28 -0400641 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
642 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500643 }
644
Jamie Madill9fc36822015-11-18 13:08:07 -0500645 if (pixelBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500646 {
Jamie Madill334d6152015-10-22 14:00:28 -0400647 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500648
Austin Kinross588434c2014-12-10 10:41:45 -0800649 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400650 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
651 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800652 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500653 {
Jamie Madill334d6152015-10-22 14:00:28 -0400654 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
655 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500656 }
Austin Kinross588434c2014-12-10 10:41:45 -0800657 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500658 {
Jamie Madill334d6152015-10-22 14:00:28 -0400659 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
660 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500661 }
662 else
663 {
Jamie Madill334d6152015-10-22 14:00:28 -0400664 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
665 // Renderer::setViewport()
666 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
667 "dx_ViewCoords.z;\n"
668 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
669 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500670 }
671
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800672 if (programMetadata.usesViewScale())
673 {
674 // For Feature Level 9_3 and below, we need to correct gl_FragCoord.y to account
675 // for dx_ViewScale. On Feature Level 10_0+, gl_FragCoord.y is calculated above using
676 // dx_ViewCoords and is always correct irrespective of dx_ViewScale's value.
677 // NOTE: usesViewScale() can only be true on D3D11 (i.e. Shader Model 4.0+).
678 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
679 {
680 // Some assumptions:
681 // - dx_ViewScale.y = -1.0f when rendering to texture
682 // - dx_ViewScale.y = +1.0f when rendering to the default framebuffer
683 // - gl_FragCoord.y has been set correctly above.
684 //
685 // When rendering to the backbuffer, the code inverts gl_FragCoord's y coordinate.
686 // This involves subtracting the y coordinate from the height of the area being
687 // rendered to.
688 //
689 // First we calculate the height of the area being rendered to:
690 // render_area_height = (2.0f / (1.0f - input.gl_FragCoord.y * rhw)) *
691 // gl_FragCoord.y
692 //
693 // Note that when we're rendering to default FB, we want our output to be
694 // equivalent to:
695 // "gl_FragCoord.y = render_area_height - gl_FragCoord.y"
696 //
697 // When we're rendering to a texture, we want our output to be equivalent to:
698 // "gl_FragCoord.y = gl_FragCoord.y;"
699 //
700 // If we set scale_factor = ((1.0f + dx_ViewScale.y) / 2.0f), then notice that
701 // - When rendering to default FB: scale_factor = 1.0f
702 // - When rendering to texture: scale_factor = 0.0f
703 //
704 // Therefore, we can get our desired output by setting:
705 // "gl_FragCoord.y = scale_factor * render_area_height - dx_ViewScale.y *
706 // gl_FragCoord.y"
707 //
708 // Simplifying, this becomes:
709 pixelStream
710 << " gl_FragCoord.y = (1.0f + dx_ViewScale.y) * gl_FragCoord.y /"
711 "(1.0f - input.gl_FragCoord.y * rhw) - dx_ViewScale.y * gl_FragCoord.y;\n";
712 }
713 }
714
Jamie Madill334d6152015-10-22 14:00:28 -0400715 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
716 "dx_DepthFront.y;\n"
717 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500718 }
719
Jamie Madill9fc36822015-11-18 13:08:07 -0500720 if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500721 {
Jamie Madill334d6152015-10-22 14:00:28 -0400722 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
723 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500724 }
725
Jamie Madill334d6152015-10-22 14:00:28 -0400726 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500727 {
728 if (shaderModel <= 3)
729 {
Jamie Madill334d6152015-10-22 14:00:28 -0400730 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500731 }
732 else
733 {
Jamie Madill334d6152015-10-22 14:00:28 -0400734 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500735 }
736 }
737
Jamie Madill9fc36822015-11-18 13:08:07 -0500738 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500739 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500740 const auto &packedVarying = *registerInfo.packedVarying;
741 const auto &varying = *packedVarying.varying;
742 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400743
Olli Etuaho06a06f52017-07-12 12:22:15 +0300744 // Don't reference VS-only transform feedback varyings in the PS. Note that we're relying on
745 // that the staticUse flag is set according to usage in the fragment shader.
746 if (packedVarying.vertexOnly || !varying.staticUse)
Jamie Madillca03b352015-09-02 12:38:13 -0400747 continue;
748
Jamie Madill55c25d02015-11-18 13:08:08 -0500749 pixelStream << " ";
750
751 if (packedVarying.isStructField())
752 {
Jamie Madill10bed9f2017-06-05 12:59:22 -0400753 pixelStream << DecorateVariable(packedVarying.parentStructName) << ".";
Jamie Madill55c25d02015-11-18 13:08:08 -0500754 }
755
Jamie Madill10bed9f2017-06-05 12:59:22 -0400756 pixelStream << DecorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400757
758 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400759 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500760 WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400761 }
762
Jamie Madill55c25d02015-11-18 13:08:08 -0500763 GLenum transposedType = TransposeMatrixType(varying.type);
764 if (VariableRowCount(transposedType) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400765 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500766 WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400767 }
768
Jamie Madill9fc36822015-11-18 13:08:07 -0500769 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400770
Jamie Madill55c25d02015-11-18 13:08:08 -0500771 switch (VariableColumnCount(transposedType))
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400772 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500773 case 1:
774 pixelStream << ".x";
775 break;
776 case 2:
777 pixelStream << ".xy";
778 break;
779 case 3:
780 pixelStream << ".xyz";
781 break;
782 case 4:
783 break;
784 default:
785 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500786 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400787 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500788 }
789
Jamie Madill334d6152015-10-22 14:00:28 -0400790 pixelStream << "\n"
791 << " gl_main();\n"
792 << "\n"
793 << " return generateOutput();\n"
794 << "}\n";
795
796 *vertexHLSL = vertexStream.str();
797 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500798}
799
Jamie Madillbd044ed2017-06-05 12:59:21 -0400800std::string DynamicHLSL::generateComputeShaderLinkHLSL(const gl::Context *context,
801 const gl::ProgramState &programData) const
Xinghua Caob1239382016-12-13 15:07:05 +0800802{
Jamie Madillbd044ed2017-06-05 12:59:21 -0400803 gl::Shader *computeShaderGL = programData.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +0800804 std::stringstream computeStream;
Jamie Madillbd044ed2017-06-05 12:59:21 -0400805 std::string translatedSource = computeShaderGL->getTranslatedSource(context);
Xinghua Caob1239382016-12-13 15:07:05 +0800806 computeStream << translatedSource;
807
808 bool usesWorkGroupID = translatedSource.find("GL_USES_WORK_GROUP_ID") != std::string::npos;
809 bool usesLocalInvocationID =
810 translatedSource.find("GL_USES_LOCAL_INVOCATION_ID") != std::string::npos;
811 bool usesGlobalInvocationID =
812 translatedSource.find("GL_USES_GLOBAL_INVOCATION_ID") != std::string::npos;
813 bool usesLocalInvocationIndex =
814 translatedSource.find("GL_USES_LOCAL_INVOCATION_INDEX") != std::string::npos;
815
816 computeStream << "\nstruct CS_INPUT\n{\n";
817 if (usesWorkGroupID)
818 {
819 computeStream << " uint3 dx_WorkGroupID : "
820 << "SV_GroupID;\n";
821 }
822
823 if (usesLocalInvocationID)
824 {
825 computeStream << " uint3 dx_LocalInvocationID : "
826 << "SV_GroupThreadID;\n";
827 }
828
829 if (usesGlobalInvocationID)
830 {
831 computeStream << " uint3 dx_GlobalInvocationID : "
832 << "SV_DispatchThreadID;\n";
833 }
834
835 if (usesLocalInvocationIndex)
836 {
837 computeStream << " uint dx_LocalInvocationIndex : "
838 << "SV_GroupIndex;\n";
839 }
840
841 computeStream << "};\n\n";
842
Jamie Madillbd044ed2017-06-05 12:59:21 -0400843 const sh::WorkGroupSize &localSize = computeShaderGL->getWorkGroupSize(context);
Xinghua Caob1239382016-12-13 15:07:05 +0800844 computeStream << "[numthreads(" << localSize[0] << ", " << localSize[1] << ", " << localSize[2]
845 << ")]\n";
846
847 computeStream << "void main(CS_INPUT input)\n"
848 << "{\n";
849
850 if (usesWorkGroupID)
851 {
852 computeStream << " gl_WorkGroupID = input.dx_WorkGroupID;\n";
853 }
854 if (usesLocalInvocationID)
855 {
856 computeStream << " gl_LocalInvocationID = input.dx_LocalInvocationID;\n";
857 }
858 if (usesGlobalInvocationID)
859 {
860 computeStream << " gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n";
861 }
862 if (usesLocalInvocationIndex)
863 {
864 computeStream << " gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n";
865 }
866
867 computeStream << "\n"
868 << " gl_main();\n"
869 << "}\n";
870
871 return computeStream.str();
872}
873
Jamie Madill192745a2016-12-22 15:58:21 -0500874std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking,
875 const BuiltinVaryingsD3D &builtinsD3D) const
Jamie Madill5f562732014-02-14 16:41:24 -0500876{
Jamie Madill9fc36822015-11-18 13:08:07 -0500877 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400878
Jamie Madill10bed9f2017-06-05 12:59:22 -0400879 std::ostringstream preambleStream;
Jamie Madill4e31ad52015-10-29 10:32:57 -0400880
Jamie Madill192745a2016-12-22 15:58:21 -0500881 const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX];
Jamie Madill9fc36822015-11-18 13:08:07 -0500882
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400883 preambleStream << "struct GS_INPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500884 generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
885 preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400886 preambleStream << "\n"
887 << "struct GS_OUTPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500888 generateVaryingLinkHLSL(varyingPacking, builtinsD3D[SHADER_GEOMETRY],
889 builtinsD3D.usesPointSize(), preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400890 preambleStream
891 << "\n"
892 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
893 << "{\n"
894 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400895
Jamie Madill192745a2016-12-22 15:58:21 -0500896 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400897 {
898 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
899 }
900
Jamie Madill9fc36822015-11-18 13:08:07 -0500901 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400902 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500903 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill55c25d02015-11-18 13:08:08 -0500904 if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400905 {
906 preambleStream << "flat";
907 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500908 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400909 }
910
Jamie Madill192745a2016-12-22 15:58:21 -0500911 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400912 {
913 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
914 }
915
916 // Only write the dx_Position if we aren't using point sprites
917 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
918 << " output.dx_Position = input.dx_Position;\n"
919 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
920 << "}\n";
921
922 return preambleStream.str();
923}
924
925std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
Jamie Madill9082b982016-04-27 15:21:51 -0400926 const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400927 const gl::ProgramState &programData,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800928 const bool useViewScale,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400929 const std::string &preambleString) const
930{
931 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500932
Jamie Madill334d6152015-10-22 14:00:28 -0400933 std::stringstream shaderStream;
934
Jamie Madill4e31ad52015-10-29 10:32:57 -0400935 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
936 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500937
Jamie Madill76f8fa62015-10-29 10:32:56 -0400938 const char *inputPT = nullptr;
939 const char *outputPT = nullptr;
940 int inputSize = 0;
941 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500942
Jamie Madill76f8fa62015-10-29 10:32:56 -0400943 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500944 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400945 case PRIMITIVE_POINTS:
946 inputPT = "point";
947 outputPT = "Triangle";
948 inputSize = 1;
949 maxVertexOutput = 4;
950 break;
951
952 case PRIMITIVE_LINES:
953 case PRIMITIVE_LINE_STRIP:
954 case PRIMITIVE_LINE_LOOP:
955 inputPT = "line";
956 outputPT = "Line";
957 inputSize = 2;
958 maxVertexOutput = 2;
959 break;
960
961 case PRIMITIVE_TRIANGLES:
962 case PRIMITIVE_TRIANGLE_STRIP:
963 case PRIMITIVE_TRIANGLE_FAN:
964 inputPT = "triangle";
965 outputPT = "Triangle";
966 inputSize = 3;
967 maxVertexOutput = 3;
968 break;
969
970 default:
971 UNREACHABLE();
972 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500973 }
974
Jamie Madill76f8fa62015-10-29 10:32:56 -0400975 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500976 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400977 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
978 "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800979 "uniform float4 dx_ViewCoords : register(c1);\n";
980
981 if (useViewScale)
982 {
983 shaderStream << "uniform float2 dx_ViewScale : register(c3);\n";
984 }
985
986 shaderStream << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400987 "static float2 pointSpriteCorners[] = \n"
988 "{\n"
989 " float2( 0.5f, -0.5f),\n"
990 " float2( 0.5f, 0.5f),\n"
991 " float2(-0.5f, -0.5f),\n"
992 " float2(-0.5f, 0.5f)\n"
993 "};\n"
994 "\n"
995 "static float2 pointSpriteTexcoords[] = \n"
996 "{\n"
997 " float2(1.0f, 1.0f),\n"
998 " float2(1.0f, 0.0f),\n"
999 " float2(0.0f, 1.0f),\n"
1000 " float2(0.0f, 0.0f)\n"
1001 "};\n"
1002 "\n"
1003 "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001004 << static_cast<int>(data.getCaps().minAliasedPointSize)
Jamie Madill76f8fa62015-10-29 10:32:56 -04001005 << ".0f;\n"
1006 "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001007 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001008 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001009 }
1010
Jamie Madill4e31ad52015-10-29 10:32:57 -04001011 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001012 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001013 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
1014
1015 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
1016 {
1017 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
1018 }
1019
1020 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001021 << "{\n"
1022 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001023
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001024 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
1025 {
1026 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
1027 }
1028 else
1029 {
1030 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
1031 }
1032
Jamie Madill4e31ad52015-10-29 10:32:57 -04001033 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -05001034 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001035 shaderStream << " copyVertex(output, input[" << vertexIndex
1036 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001037
1038 if (!pointSprites)
1039 {
1040 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001041 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001042 }
1043 }
1044
1045 if (pointSprites)
1046 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001047 shaderStream << "\n"
1048 " float4 dx_Position = input[0].dx_Position;\n"
1049 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
1050 "maxPointSize);\n"
1051 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
1052 "dx_ViewCoords.y) * dx_Position.w;\n";
1053
1054 for (int corner = 0; corner < 4; corner++)
1055 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001056 if (useViewScale)
1057 {
1058 shaderStream << " \n"
1059 " output.dx_Position = dx_Position + float4(1.0f, "
1060 "-dx_ViewScale.y, 1.0f, 1.0f)"
1061 " * float4(pointSpriteCorners["
1062 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1063 }
1064 else
1065 {
1066 shaderStream << "\n"
1067 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
1068 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1069 }
Jamie Madill76f8fa62015-10-29 10:32:56 -04001070
1071 if (usesPointCoord)
1072 {
1073 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
1074 << "];\n";
1075 }
1076
1077 shaderStream << " outStream.Append(output);\n";
1078 }
Jamie Madill5f562732014-02-14 16:41:24 -05001079 }
1080
Jamie Madill334d6152015-10-22 14:00:28 -04001081 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001082 " outStream.RestartStrip();\n"
1083 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001084
Jamie Madill334d6152015-10-22 14:00:28 -04001085 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -05001086}
1087
Jamie Madill10bed9f2017-06-05 12:59:22 -04001088// static
1089void DynamicHLSL::GenerateAttributeConversionHLSL(gl::VertexFormatType vertexFormatType,
1090 const sh::ShaderVariable &shaderAttrib,
1091 std::ostringstream &outStream)
Jamie Madill5f562732014-02-14 16:41:24 -05001092{
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001093 // Matrix
1094 if (IsMatrixType(shaderAttrib.type))
1095 {
Jamie Madill10bed9f2017-06-05 12:59:22 -04001096 outStream << "transpose(input." << DecorateVariable(shaderAttrib.name) << ")";
1097 return;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001098 }
1099
Jamie Madillf2575982014-06-25 16:04:54 -04001100 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001101 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill10bed9f2017-06-05 12:59:22 -04001102 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill8664b062014-02-14 16:41:29 -05001103
Jamie Madill8664b062014-02-14 16:41:29 -05001104 // Perform integer to float conversion (if necessary)
Jamie Madill10bed9f2017-06-05 12:59:22 -04001105 if (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT)
Jamie Madill8664b062014-02-14 16:41:29 -05001106 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001107 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001108 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill10bed9f2017-06-05 12:59:22 -04001109 outStream << "float" << shaderComponentCount << "(input."
1110 << DecorateVariable(shaderAttrib.name) << ")";
1111 return;
Jamie Madill8664b062014-02-14 16:41:29 -05001112 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001113
1114 // No conversion necessary
Jamie Madill10bed9f2017-06-05 12:59:22 -04001115 outStream << "input." << DecorateVariable(shaderAttrib.name);
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001116}
Jamie Madille39a3f02015-11-17 20:42:15 -05001117
Jamie Madill9082b982016-04-27 15:21:51 -04001118void DynamicHLSL::getPixelShaderOutputKey(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001119 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -05001120 const ProgramD3DMetadata &metadata,
1121 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1122{
1123 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1124 // - with a 3.0 context, the output color is copied to channel 0
1125 // - with a 2.0 context, the output color is broadcast to all channels
1126 bool broadcast = metadata.usesBroadcast(data);
1127 const unsigned int numRenderTargets =
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001128 (broadcast || metadata.usesMultipleFragmentOuts() ? data.getCaps().maxDrawBuffers : 1);
Jamie Madille39a3f02015-11-17 20:42:15 -05001129
1130 if (metadata.getMajorShaderVersion() < 300)
1131 {
1132 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1133 renderTargetIndex++)
1134 {
1135 PixelShaderOutputVariable outputKeyVariable;
1136 outputKeyVariable.type = GL_FLOAT_VEC4;
1137 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1138 outputKeyVariable.source =
1139 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1140 outputKeyVariable.outputIndex = renderTargetIndex;
1141
1142 outPixelShaderKey->push_back(outputKeyVariable);
1143 }
1144 }
1145 else
1146 {
1147 const auto &shaderOutputVars =
1148 metadata.getFragmentShader()->getData().getActiveOutputVariables();
1149
jchen1015015f72017-03-16 13:54:21 +08001150 for (auto outputPair : programData.getOutputLocations())
Jamie Madille39a3f02015-11-17 20:42:15 -05001151 {
1152 const VariableLocation &outputLocation = outputPair.second;
1153 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1154 const std::string &variableName = "out_" + outputLocation.name;
1155 const std::string &elementString =
1156 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
1157
1158 ASSERT(outputVariable.staticUse);
1159
1160 PixelShaderOutputVariable outputKeyVariable;
1161 outputKeyVariable.type = outputVariable.type;
1162 outputKeyVariable.name = variableName + elementString;
1163 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
1164 outputKeyVariable.outputIndex = outputPair.first;
1165
1166 outPixelShaderKey->push_back(outputKeyVariable);
1167 }
1168 }
1169}
1170
Jamie Madill192745a2016-12-22 15:58:21 -05001171// BuiltinVarying Implementation.
1172BuiltinVarying::BuiltinVarying() : enabled(false), index(0), systemValue(false)
1173{
1174}
1175
1176std::string BuiltinVarying::str() const
1177{
1178 return (systemValue ? semantic : (semantic + Str(index)));
1179}
1180
1181void BuiltinVarying::enableSystem(const std::string &systemValueSemantic)
1182{
1183 enabled = true;
1184 semantic = systemValueSemantic;
1185 systemValue = true;
1186}
1187
1188void BuiltinVarying::enable(const std::string &semanticVal, unsigned int indexVal)
1189{
1190 enabled = true;
1191 semantic = semanticVal;
1192 index = indexVal;
1193}
1194
1195// BuiltinVaryingsD3D Implementation.
1196BuiltinVaryingsD3D::BuiltinVaryingsD3D(const ProgramD3DMetadata &metadata,
1197 const VaryingPacking &packing)
1198{
1199 updateBuiltins(SHADER_VERTEX, metadata, packing);
1200 updateBuiltins(SHADER_PIXEL, metadata, packing);
1201 if (metadata.getRendererMajorShaderModel() >= 4)
1202 {
1203 updateBuiltins(SHADER_GEOMETRY, metadata, packing);
1204 }
1205}
1206
1207void BuiltinVaryingsD3D::updateBuiltins(ShaderType shaderType,
1208 const ProgramD3DMetadata &metadata,
1209 const VaryingPacking &packing)
1210{
1211 const std::string &userSemantic = GetVaryingSemantic(metadata.getRendererMajorShaderModel(),
1212 metadata.usesSystemValuePointSize());
1213
1214 unsigned int reservedSemanticIndex = packing.getMaxSemanticIndex();
1215
1216 BuiltinInfo *builtins = &mBuiltinInfo[shaderType];
1217
1218 if (metadata.getRendererMajorShaderModel() >= 4)
1219 {
1220 builtins->dxPosition.enableSystem("SV_Position");
1221 }
1222 else if (shaderType == SHADER_PIXEL)
1223 {
1224 builtins->dxPosition.enableSystem("VPOS");
1225 }
1226 else
1227 {
1228 builtins->dxPosition.enableSystem("POSITION");
1229 }
1230
1231 if (metadata.usesTransformFeedbackGLPosition())
1232 {
1233 builtins->glPosition.enable(userSemantic, reservedSemanticIndex++);
1234 }
1235
1236 if (metadata.usesFragCoord())
1237 {
1238 builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++);
1239 }
1240
1241 if (shaderType == SHADER_VERTEX ? metadata.addsPointCoordToVertexShader()
1242 : metadata.usesPointCoord())
1243 {
1244 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
1245 // In D3D11 we manually compute gl_PointCoord in the GS.
1246 if (metadata.getRendererMajorShaderModel() >= 4)
1247 {
1248 builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++);
1249 }
1250 else
1251 {
1252 builtins->glPointCoord.enable("TEXCOORD", 0);
1253 }
1254 }
1255
1256 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
1257 if (metadata.usesSystemValuePointSize() &&
1258 (shaderType != SHADER_PIXEL || metadata.getRendererMajorShaderModel() >= 4))
1259 {
1260 builtins->glPointSize.enableSystem("PSIZE");
1261 }
1262}
1263
Jamie Madill334d6152015-10-22 14:00:28 -04001264} // namespace rx