blob: 31a73ab6b7710f3e0a214ab27876b6e44c22fd41 [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
Jamie Madillca03b352015-09-02 12:38:13 -0400744 // Don't reference VS-only transform feedback varyings in the PS.
Geoff Lang82a468a2016-06-21 17:18:25 -0400745 // TODO: Consider updating the fragment shader's varyings with a parameter signaling that a
746 // varying is only used in the vertex shader in MergeVaryings
747 if (packedVarying.vertexOnly || (!varying.staticUse && !packedVarying.isStructField()))
Jamie Madillca03b352015-09-02 12:38:13 -0400748 continue;
749
Jamie Madill55c25d02015-11-18 13:08:08 -0500750 pixelStream << " ";
751
752 if (packedVarying.isStructField())
753 {
Jamie Madill10bed9f2017-06-05 12:59:22 -0400754 pixelStream << DecorateVariable(packedVarying.parentStructName) << ".";
Jamie Madill55c25d02015-11-18 13:08:08 -0500755 }
756
Jamie Madill10bed9f2017-06-05 12:59:22 -0400757 pixelStream << DecorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400758
759 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400760 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500761 WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400762 }
763
Jamie Madill55c25d02015-11-18 13:08:08 -0500764 GLenum transposedType = TransposeMatrixType(varying.type);
765 if (VariableRowCount(transposedType) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400766 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500767 WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400768 }
769
Jamie Madill9fc36822015-11-18 13:08:07 -0500770 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400771
Jamie Madill55c25d02015-11-18 13:08:08 -0500772 switch (VariableColumnCount(transposedType))
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400773 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500774 case 1:
775 pixelStream << ".x";
776 break;
777 case 2:
778 pixelStream << ".xy";
779 break;
780 case 3:
781 pixelStream << ".xyz";
782 break;
783 case 4:
784 break;
785 default:
786 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500787 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400788 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500789 }
790
Jamie Madill334d6152015-10-22 14:00:28 -0400791 pixelStream << "\n"
792 << " gl_main();\n"
793 << "\n"
794 << " return generateOutput();\n"
795 << "}\n";
796
797 *vertexHLSL = vertexStream.str();
798 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500799}
800
Jamie Madillbd044ed2017-06-05 12:59:21 -0400801std::string DynamicHLSL::generateComputeShaderLinkHLSL(const gl::Context *context,
802 const gl::ProgramState &programData) const
Xinghua Caob1239382016-12-13 15:07:05 +0800803{
Jamie Madillbd044ed2017-06-05 12:59:21 -0400804 gl::Shader *computeShaderGL = programData.getAttachedComputeShader();
Xinghua Caob1239382016-12-13 15:07:05 +0800805 std::stringstream computeStream;
Jamie Madillbd044ed2017-06-05 12:59:21 -0400806 std::string translatedSource = computeShaderGL->getTranslatedSource(context);
Xinghua Caob1239382016-12-13 15:07:05 +0800807 computeStream << translatedSource;
808
809 bool usesWorkGroupID = translatedSource.find("GL_USES_WORK_GROUP_ID") != std::string::npos;
810 bool usesLocalInvocationID =
811 translatedSource.find("GL_USES_LOCAL_INVOCATION_ID") != std::string::npos;
812 bool usesGlobalInvocationID =
813 translatedSource.find("GL_USES_GLOBAL_INVOCATION_ID") != std::string::npos;
814 bool usesLocalInvocationIndex =
815 translatedSource.find("GL_USES_LOCAL_INVOCATION_INDEX") != std::string::npos;
816
817 computeStream << "\nstruct CS_INPUT\n{\n";
818 if (usesWorkGroupID)
819 {
820 computeStream << " uint3 dx_WorkGroupID : "
821 << "SV_GroupID;\n";
822 }
823
824 if (usesLocalInvocationID)
825 {
826 computeStream << " uint3 dx_LocalInvocationID : "
827 << "SV_GroupThreadID;\n";
828 }
829
830 if (usesGlobalInvocationID)
831 {
832 computeStream << " uint3 dx_GlobalInvocationID : "
833 << "SV_DispatchThreadID;\n";
834 }
835
836 if (usesLocalInvocationIndex)
837 {
838 computeStream << " uint dx_LocalInvocationIndex : "
839 << "SV_GroupIndex;\n";
840 }
841
842 computeStream << "};\n\n";
843
Jamie Madillbd044ed2017-06-05 12:59:21 -0400844 const sh::WorkGroupSize &localSize = computeShaderGL->getWorkGroupSize(context);
Xinghua Caob1239382016-12-13 15:07:05 +0800845 computeStream << "[numthreads(" << localSize[0] << ", " << localSize[1] << ", " << localSize[2]
846 << ")]\n";
847
848 computeStream << "void main(CS_INPUT input)\n"
849 << "{\n";
850
851 if (usesWorkGroupID)
852 {
853 computeStream << " gl_WorkGroupID = input.dx_WorkGroupID;\n";
854 }
855 if (usesLocalInvocationID)
856 {
857 computeStream << " gl_LocalInvocationID = input.dx_LocalInvocationID;\n";
858 }
859 if (usesGlobalInvocationID)
860 {
861 computeStream << " gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n";
862 }
863 if (usesLocalInvocationIndex)
864 {
865 computeStream << " gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n";
866 }
867
868 computeStream << "\n"
869 << " gl_main();\n"
870 << "}\n";
871
872 return computeStream.str();
873}
874
Jamie Madill192745a2016-12-22 15:58:21 -0500875std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking,
876 const BuiltinVaryingsD3D &builtinsD3D) const
Jamie Madill5f562732014-02-14 16:41:24 -0500877{
Jamie Madill9fc36822015-11-18 13:08:07 -0500878 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400879
Jamie Madill10bed9f2017-06-05 12:59:22 -0400880 std::ostringstream preambleStream;
Jamie Madill4e31ad52015-10-29 10:32:57 -0400881
Jamie Madill192745a2016-12-22 15:58:21 -0500882 const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX];
Jamie Madill9fc36822015-11-18 13:08:07 -0500883
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400884 preambleStream << "struct GS_INPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500885 generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(),
886 preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400887 preambleStream << "\n"
888 << "struct GS_OUTPUT\n";
Jamie Madill192745a2016-12-22 15:58:21 -0500889 generateVaryingLinkHLSL(varyingPacking, builtinsD3D[SHADER_GEOMETRY],
890 builtinsD3D.usesPointSize(), preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400891 preambleStream
892 << "\n"
893 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
894 << "{\n"
895 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400896
Jamie Madill192745a2016-12-22 15:58:21 -0500897 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400898 {
899 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
900 }
901
Jamie Madill9fc36822015-11-18 13:08:07 -0500902 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400903 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500904 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill55c25d02015-11-18 13:08:08 -0500905 if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400906 {
907 preambleStream << "flat";
908 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500909 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400910 }
911
Jamie Madill192745a2016-12-22 15:58:21 -0500912 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400913 {
914 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
915 }
916
917 // Only write the dx_Position if we aren't using point sprites
918 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
919 << " output.dx_Position = input.dx_Position;\n"
920 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
921 << "}\n";
922
923 return preambleStream.str();
924}
925
926std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
Jamie Madill9082b982016-04-27 15:21:51 -0400927 const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400928 const gl::ProgramState &programData,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800929 const bool useViewScale,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400930 const std::string &preambleString) const
931{
932 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500933
Jamie Madill334d6152015-10-22 14:00:28 -0400934 std::stringstream shaderStream;
935
Jamie Madill4e31ad52015-10-29 10:32:57 -0400936 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
937 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500938
Jamie Madill76f8fa62015-10-29 10:32:56 -0400939 const char *inputPT = nullptr;
940 const char *outputPT = nullptr;
941 int inputSize = 0;
942 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500943
Jamie Madill76f8fa62015-10-29 10:32:56 -0400944 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500945 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400946 case PRIMITIVE_POINTS:
947 inputPT = "point";
948 outputPT = "Triangle";
949 inputSize = 1;
950 maxVertexOutput = 4;
951 break;
952
953 case PRIMITIVE_LINES:
954 case PRIMITIVE_LINE_STRIP:
955 case PRIMITIVE_LINE_LOOP:
956 inputPT = "line";
957 outputPT = "Line";
958 inputSize = 2;
959 maxVertexOutput = 2;
960 break;
961
962 case PRIMITIVE_TRIANGLES:
963 case PRIMITIVE_TRIANGLE_STRIP:
964 case PRIMITIVE_TRIANGLE_FAN:
965 inputPT = "triangle";
966 outputPT = "Triangle";
967 inputSize = 3;
968 maxVertexOutput = 3;
969 break;
970
971 default:
972 UNREACHABLE();
973 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500974 }
975
Jamie Madill76f8fa62015-10-29 10:32:56 -0400976 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500977 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400978 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
979 "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800980 "uniform float4 dx_ViewCoords : register(c1);\n";
981
982 if (useViewScale)
983 {
984 shaderStream << "uniform float2 dx_ViewScale : register(c3);\n";
985 }
986
987 shaderStream << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400988 "static float2 pointSpriteCorners[] = \n"
989 "{\n"
990 " float2( 0.5f, -0.5f),\n"
991 " float2( 0.5f, 0.5f),\n"
992 " float2(-0.5f, -0.5f),\n"
993 " float2(-0.5f, 0.5f)\n"
994 "};\n"
995 "\n"
996 "static float2 pointSpriteTexcoords[] = \n"
997 "{\n"
998 " float2(1.0f, 1.0f),\n"
999 " float2(1.0f, 0.0f),\n"
1000 " float2(0.0f, 1.0f),\n"
1001 " float2(0.0f, 0.0f)\n"
1002 "};\n"
1003 "\n"
1004 "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001005 << static_cast<int>(data.getCaps().minAliasedPointSize)
Jamie Madill76f8fa62015-10-29 10:32:56 -04001006 << ".0f;\n"
1007 "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001008 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001009 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001010 }
1011
Jamie Madill4e31ad52015-10-29 10:32:57 -04001012 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001013 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001014 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
1015
1016 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
1017 {
1018 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
1019 }
1020
1021 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001022 << "{\n"
1023 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001024
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001025 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
1026 {
1027 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
1028 }
1029 else
1030 {
1031 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
1032 }
1033
Jamie Madill4e31ad52015-10-29 10:32:57 -04001034 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -05001035 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001036 shaderStream << " copyVertex(output, input[" << vertexIndex
1037 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001038
1039 if (!pointSprites)
1040 {
1041 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001042 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001043 }
1044 }
1045
1046 if (pointSprites)
1047 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001048 shaderStream << "\n"
1049 " float4 dx_Position = input[0].dx_Position;\n"
1050 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
1051 "maxPointSize);\n"
1052 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
1053 "dx_ViewCoords.y) * dx_Position.w;\n";
1054
1055 for (int corner = 0; corner < 4; corner++)
1056 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -08001057 if (useViewScale)
1058 {
1059 shaderStream << " \n"
1060 " output.dx_Position = dx_Position + float4(1.0f, "
1061 "-dx_ViewScale.y, 1.0f, 1.0f)"
1062 " * float4(pointSpriteCorners["
1063 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1064 }
1065 else
1066 {
1067 shaderStream << "\n"
1068 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
1069 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1070 }
Jamie Madill76f8fa62015-10-29 10:32:56 -04001071
1072 if (usesPointCoord)
1073 {
1074 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
1075 << "];\n";
1076 }
1077
1078 shaderStream << " outStream.Append(output);\n";
1079 }
Jamie Madill5f562732014-02-14 16:41:24 -05001080 }
1081
Jamie Madill334d6152015-10-22 14:00:28 -04001082 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001083 " outStream.RestartStrip();\n"
1084 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001085
Jamie Madill334d6152015-10-22 14:00:28 -04001086 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -05001087}
1088
Jamie Madill10bed9f2017-06-05 12:59:22 -04001089// static
1090void DynamicHLSL::GenerateAttributeConversionHLSL(gl::VertexFormatType vertexFormatType,
1091 const sh::ShaderVariable &shaderAttrib,
1092 std::ostringstream &outStream)
Jamie Madill5f562732014-02-14 16:41:24 -05001093{
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001094 // Matrix
1095 if (IsMatrixType(shaderAttrib.type))
1096 {
Jamie Madill10bed9f2017-06-05 12:59:22 -04001097 outStream << "transpose(input." << DecorateVariable(shaderAttrib.name) << ")";
1098 return;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001099 }
1100
Jamie Madillf2575982014-06-25 16:04:54 -04001101 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001102 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill10bed9f2017-06-05 12:59:22 -04001103 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill8664b062014-02-14 16:41:29 -05001104
Jamie Madill8664b062014-02-14 16:41:29 -05001105 // Perform integer to float conversion (if necessary)
Jamie Madill10bed9f2017-06-05 12:59:22 -04001106 if (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT)
Jamie Madill8664b062014-02-14 16:41:29 -05001107 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001108 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001109 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill10bed9f2017-06-05 12:59:22 -04001110 outStream << "float" << shaderComponentCount << "(input."
1111 << DecorateVariable(shaderAttrib.name) << ")";
1112 return;
Jamie Madill8664b062014-02-14 16:41:29 -05001113 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001114
1115 // No conversion necessary
Jamie Madill10bed9f2017-06-05 12:59:22 -04001116 outStream << "input." << DecorateVariable(shaderAttrib.name);
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001117}
Jamie Madille39a3f02015-11-17 20:42:15 -05001118
Jamie Madill9082b982016-04-27 15:21:51 -04001119void DynamicHLSL::getPixelShaderOutputKey(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001120 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -05001121 const ProgramD3DMetadata &metadata,
1122 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1123{
1124 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1125 // - with a 3.0 context, the output color is copied to channel 0
1126 // - with a 2.0 context, the output color is broadcast to all channels
1127 bool broadcast = metadata.usesBroadcast(data);
1128 const unsigned int numRenderTargets =
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001129 (broadcast || metadata.usesMultipleFragmentOuts() ? data.getCaps().maxDrawBuffers : 1);
Jamie Madille39a3f02015-11-17 20:42:15 -05001130
1131 if (metadata.getMajorShaderVersion() < 300)
1132 {
1133 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1134 renderTargetIndex++)
1135 {
1136 PixelShaderOutputVariable outputKeyVariable;
1137 outputKeyVariable.type = GL_FLOAT_VEC4;
1138 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1139 outputKeyVariable.source =
1140 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1141 outputKeyVariable.outputIndex = renderTargetIndex;
1142
1143 outPixelShaderKey->push_back(outputKeyVariable);
1144 }
1145 }
1146 else
1147 {
1148 const auto &shaderOutputVars =
1149 metadata.getFragmentShader()->getData().getActiveOutputVariables();
1150
jchen1015015f72017-03-16 13:54:21 +08001151 for (auto outputPair : programData.getOutputLocations())
Jamie Madille39a3f02015-11-17 20:42:15 -05001152 {
1153 const VariableLocation &outputLocation = outputPair.second;
1154 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1155 const std::string &variableName = "out_" + outputLocation.name;
1156 const std::string &elementString =
1157 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
1158
1159 ASSERT(outputVariable.staticUse);
1160
1161 PixelShaderOutputVariable outputKeyVariable;
1162 outputKeyVariable.type = outputVariable.type;
1163 outputKeyVariable.name = variableName + elementString;
1164 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
1165 outputKeyVariable.outputIndex = outputPair.first;
1166
1167 outPixelShaderKey->push_back(outputKeyVariable);
1168 }
1169 }
1170}
1171
Jamie Madill192745a2016-12-22 15:58:21 -05001172// BuiltinVarying Implementation.
1173BuiltinVarying::BuiltinVarying() : enabled(false), index(0), systemValue(false)
1174{
1175}
1176
1177std::string BuiltinVarying::str() const
1178{
1179 return (systemValue ? semantic : (semantic + Str(index)));
1180}
1181
1182void BuiltinVarying::enableSystem(const std::string &systemValueSemantic)
1183{
1184 enabled = true;
1185 semantic = systemValueSemantic;
1186 systemValue = true;
1187}
1188
1189void BuiltinVarying::enable(const std::string &semanticVal, unsigned int indexVal)
1190{
1191 enabled = true;
1192 semantic = semanticVal;
1193 index = indexVal;
1194}
1195
1196// BuiltinVaryingsD3D Implementation.
1197BuiltinVaryingsD3D::BuiltinVaryingsD3D(const ProgramD3DMetadata &metadata,
1198 const VaryingPacking &packing)
1199{
1200 updateBuiltins(SHADER_VERTEX, metadata, packing);
1201 updateBuiltins(SHADER_PIXEL, metadata, packing);
1202 if (metadata.getRendererMajorShaderModel() >= 4)
1203 {
1204 updateBuiltins(SHADER_GEOMETRY, metadata, packing);
1205 }
1206}
1207
1208void BuiltinVaryingsD3D::updateBuiltins(ShaderType shaderType,
1209 const ProgramD3DMetadata &metadata,
1210 const VaryingPacking &packing)
1211{
1212 const std::string &userSemantic = GetVaryingSemantic(metadata.getRendererMajorShaderModel(),
1213 metadata.usesSystemValuePointSize());
1214
1215 unsigned int reservedSemanticIndex = packing.getMaxSemanticIndex();
1216
1217 BuiltinInfo *builtins = &mBuiltinInfo[shaderType];
1218
1219 if (metadata.getRendererMajorShaderModel() >= 4)
1220 {
1221 builtins->dxPosition.enableSystem("SV_Position");
1222 }
1223 else if (shaderType == SHADER_PIXEL)
1224 {
1225 builtins->dxPosition.enableSystem("VPOS");
1226 }
1227 else
1228 {
1229 builtins->dxPosition.enableSystem("POSITION");
1230 }
1231
1232 if (metadata.usesTransformFeedbackGLPosition())
1233 {
1234 builtins->glPosition.enable(userSemantic, reservedSemanticIndex++);
1235 }
1236
1237 if (metadata.usesFragCoord())
1238 {
1239 builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++);
1240 }
1241
1242 if (shaderType == SHADER_VERTEX ? metadata.addsPointCoordToVertexShader()
1243 : metadata.usesPointCoord())
1244 {
1245 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
1246 // In D3D11 we manually compute gl_PointCoord in the GS.
1247 if (metadata.getRendererMajorShaderModel() >= 4)
1248 {
1249 builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++);
1250 }
1251 else
1252 {
1253 builtins->glPointCoord.enable("TEXCOORD", 0);
1254 }
1255 }
1256
1257 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
1258 if (metadata.usesSystemValuePointSize() &&
1259 (shaderType != SHADER_PIXEL || metadata.getRendererMajorShaderModel() >= 4))
1260 {
1261 builtins->glPointSize.enableSystem("PSIZE");
1262 }
1263}
1264
Jamie Madill334d6152015-10-22 14:00:28 -04001265} // namespace rx