blob: b2c1fba060a968b5546f7e8e82e58e25676be2f7 [file] [log] [blame]
Jamie Madill5f562732014-02-14 16:41:24 -05001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// DynamicHLSL.cpp: Implementation for link and run-time HLSL generation
7//
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050010
11#include "common/utilities.h"
Daniel Bratell73941de2015-02-25 14:34:49 +010012#include "compiler/translator/blocklayoutHLSL.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Program.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/Shader.h"
15#include "libANGLE/formatutils.h"
Jamie Madill28afae52015-11-09 15:07:57 -050016#include "libANGLE/renderer/d3d/ProgramD3D.h"
Jamie Madill80a6fc02015-08-21 16:53:16 -040017#include "libANGLE/renderer/d3d/RendererD3D.h"
18#include "libANGLE/renderer/d3d/ShaderD3D.h"
Jamie Madill120040e2016-12-07 14:46:16 -050019#include "libANGLE/renderer/d3d/hlsl/VaryingPacking.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040020
Brandon Jonesd8d72432014-08-22 15:11:23 -070021using namespace gl;
22
Jamie Madill30d6c252014-11-13 10:03:33 -050023namespace rx
24{
25
Jamie Madill3f2e61d2014-09-05 10:38:05 -040026namespace
Jamie Madill5f562732014-02-14 16:41:24 -050027{
Jamie Madill8664b062014-02-14 16:41:29 -050028
29std::string HLSLComponentTypeString(GLenum componentType)
30{
31 switch (componentType)
32 {
Jamie Madill334d6152015-10-22 14:00:28 -040033 case GL_UNSIGNED_INT:
34 return "uint";
35 case GL_INT:
36 return "int";
37 case GL_UNSIGNED_NORMALIZED:
38 case GL_SIGNED_NORMALIZED:
39 case GL_FLOAT:
40 return "float";
41 default:
42 UNREACHABLE();
43 return "not-component-type";
Jamie Madill8664b062014-02-14 16:41:29 -050044 }
Jamie Madill5f562732014-02-14 16:41:24 -050045}
46
Jamie Madill8664b062014-02-14 16:41:29 -050047std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
48{
49 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
50}
51
52std::string HLSLMatrixTypeString(GLenum type)
53{
54 switch (type)
55 {
Jamie Madill334d6152015-10-22 14:00:28 -040056 case GL_FLOAT_MAT2:
57 return "float2x2";
58 case GL_FLOAT_MAT3:
59 return "float3x3";
60 case GL_FLOAT_MAT4:
61 return "float4x4";
62 case GL_FLOAT_MAT2x3:
63 return "float2x3";
64 case GL_FLOAT_MAT3x2:
65 return "float3x2";
66 case GL_FLOAT_MAT2x4:
67 return "float2x4";
68 case GL_FLOAT_MAT4x2:
69 return "float4x2";
70 case GL_FLOAT_MAT3x4:
71 return "float3x4";
72 case GL_FLOAT_MAT4x3:
73 return "float4x3";
74 default:
75 UNREACHABLE();
76 return "not-matrix-type";
Jamie Madill8664b062014-02-14 16:41:29 -050077 }
78}
79
80std::string HLSLTypeString(GLenum type)
81{
82 if (gl::IsMatrixType(type))
83 {
84 return HLSLMatrixTypeString(type);
85 }
86
Jamie Madill334d6152015-10-22 14:00:28 -040087 return HLSLComponentTypeString(gl::VariableComponentType(type),
88 gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050089}
90
Jamie Madill334d6152015-10-22 14:00:28 -040091const PixelShaderOutputVariable *FindOutputAtLocation(
92 const std::vector<PixelShaderOutputVariable> &outputVariables,
93 unsigned int location)
Jamie Madill3f2e61d2014-09-05 10:38:05 -040094{
95 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
96 {
97 if (outputVariables[variableIndex].outputIndex == location)
98 {
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +000099 return &outputVariables[variableIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400100 }
101 }
102
Jamie Madill334d6152015-10-22 14:00:28 -0400103 return nullptr;
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400104}
105
Jamie Madill55c25d02015-11-18 13:08:08 -0500106void WriteArrayString(std::stringstream &strstr, unsigned int i)
107{
108 static_assert(GL_INVALID_INDEX == UINT_MAX,
109 "GL_INVALID_INDEX must be equal to the max unsigned int.");
110 if (i == UINT_MAX)
111 {
112 return;
113 }
114
115 strstr << "[";
116 strstr << i;
117 strstr << "]";
118}
119
Jamie Madill51c47682016-10-25 15:50:14 -0400120constexpr const char *VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
121constexpr const char *PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400122} // anonymous namespace
Jamie Madill4cff2472015-08-21 16:53:18 -0400123
Jamie Madill9fc36822015-11-18 13:08:07 -0500124// DynamicHLSL implementation
125
Jamie Madill65345da2015-11-13 11:25:23 -0500126DynamicHLSL::DynamicHLSL(RendererD3D *const renderer) : mRenderer(renderer)
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000127{
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000128}
129
Jamie Madill9fc36822015-11-18 13:08:07 -0500130void DynamicHLSL::generateVaryingHLSL(const VaryingPacking &varyingPacking,
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400131 std::stringstream &hlslStream) const
Jamie Madill5f562732014-02-14 16:41:24 -0500132{
Jamie Madill65345da2015-11-13 11:25:23 -0500133 std::string varyingSemantic =
Jamie Madill9fc36822015-11-18 13:08:07 -0500134 GetVaryingSemantic(mRenderer->getMajorShaderModel(), varyingPacking.usesPointSize());
Jamie Madill5f562732014-02-14 16:41:24 -0500135
Jamie Madill9fc36822015-11-18 13:08:07 -0500136 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500137 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500138 const auto &varying = *registerInfo.packedVarying->varying;
139 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400140
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400141 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many
142 // registers being used.
143 // For example, if there are N registers, and we have N vec3 varyings and 1 float
144 // varying, then D3D will pack them into N registers.
145 // If the float varying has the 'nointerpolation' modifier on it then we would need
146 // N + 1 registers, and D3D compilation will fail.
Jamie Madill4cff2472015-08-21 16:53:18 -0400147
Jamie Madill55c25d02015-11-18 13:08:08 -0500148 switch (registerInfo.packedVarying->interpolation)
Jamie Madill4cff2472015-08-21 16:53:18 -0400149 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400150 case sh::INTERPOLATION_SMOOTH:
151 hlslStream << " ";
152 break;
153 case sh::INTERPOLATION_FLAT:
154 hlslStream << " nointerpolation ";
155 break;
156 case sh::INTERPOLATION_CENTROID:
157 hlslStream << " centroid ";
158 break;
159 default:
160 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500161 }
Jamie Madill5f562732014-02-14 16:41:24 -0500162
Jamie Madill55c25d02015-11-18 13:08:08 -0500163 GLenum transposedType = gl::TransposeMatrixType(varying.type);
164 GLenum componentType = gl::VariableComponentType(transposedType);
165 int columnCount = gl::VariableColumnCount(transposedType);
166 hlslStream << HLSLComponentTypeString(componentType, columnCount);
167 unsigned int semanticIndex = registerInfo.semanticIndex;
Jamie Madill9fc36822015-11-18 13:08:07 -0500168 hlslStream << " v" << semanticIndex << " : " << varyingSemantic << semanticIndex << ";\n";
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400169 }
Jamie Madill5f562732014-02-14 16:41:24 -0500170}
171
Jamie Madill334d6152015-10-22 14:00:28 -0400172std::string DynamicHLSL::generateVertexShaderForInputLayout(
173 const std::string &sourceShader,
174 const InputLayout &inputLayout,
175 const std::vector<sh::Attribute> &shaderAttributes) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500176{
Jamie Madill334d6152015-10-22 14:00:28 -0400177 std::stringstream structStream;
178 std::stringstream initStream;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500179
Jamie Madill334d6152015-10-22 14:00:28 -0400180 structStream << "struct VS_INPUT\n"
181 << "{\n";
182
183 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400184 unsigned int inputIndex = 0;
185
Cooper Partine6d14cc2015-02-20 12:32:58 -0800186 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
187 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
188 // must be used.
189 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
Jamie Madill334d6152015-10-22 14:00:28 -0400190 bool useInstancedPointSpriteEmulation =
191 usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Cooper Partine6d14cc2015-02-20 12:32:58 -0800192
193 // Instanced PointSprite emulation requires additional entries in the
194 // VS_INPUT structure to support the vertices that make up the quad vertices.
195 // These values must be in sync with the cooresponding values added during inputlayout creation
196 // in InputLayoutCache::applyVertexBuffers().
197 //
198 // The additional entries must appear first in the VS_INPUT layout because
199 // Windows Phone 8 era devices require per vertex data to physically come
200 // before per instance data in the shader.
201 if (useInstancedPointSpriteEmulation)
202 {
Jamie Madill334d6152015-10-22 14:00:28 -0400203 structStream << " float3 spriteVertexPos : SPRITEPOSITION0;\n"
204 << " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
Cooper Partine6d14cc2015-02-20 12:32:58 -0800205 }
206
Jamie Madill3da79b72015-04-27 11:09:17 -0400207 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex)
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500208 {
Jamie Madillf2575982014-06-25 16:04:54 -0400209 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500210 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500211 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400212 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400213 VertexFormatType vertexFormatType =
214 inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID;
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400215
Jamie Madill3b7e2052014-03-17 09:47:43 -0400216 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500217 if (IsMatrixType(shaderAttribute.type))
218 {
219 // Matrix types are always transposed
Jamie Madill334d6152015-10-22 14:00:28 -0400220 structStream << " "
221 << HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500222 }
223 else
224 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400225 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000226
Corentin Wallezb076add2016-01-11 16:45:46 -0500227 if (shaderAttribute.name == "gl_InstanceID" ||
228 shaderAttribute.name == "gl_VertexID")
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000229 {
Corentin Wallezb076add2016-01-11 16:45:46 -0500230 // The input types of the instance ID and vertex ID in HLSL (uint) differs from
231 // the ones in ESSL (int).
Jamie Madill334d6152015-10-22 14:00:28 -0400232 structStream << " uint";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000233 }
234 else
235 {
Jamie Madill334d6152015-10-22 14:00:28 -0400236 structStream << " " << HLSLComponentTypeString(
237 componentType,
238 VariableComponentCount(shaderAttribute.type));
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000239 }
Jamie Madill8664b062014-02-14 16:41:29 -0500240 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500241
Jamie Madill334d6152015-10-22 14:00:28 -0400242 structStream << " " << decorateVariable(shaderAttribute.name) << " : ";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000243
244 if (shaderAttribute.name == "gl_InstanceID")
245 {
Jamie Madill334d6152015-10-22 14:00:28 -0400246 structStream << "SV_InstanceID";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000247 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500248 else if (shaderAttribute.name == "gl_VertexID")
249 {
250 structStream << "SV_VertexID";
251 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000252 else
253 {
Jamie Madill334d6152015-10-22 14:00:28 -0400254 structStream << "TEXCOORD" << semanticIndex;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000255 semanticIndex += VariableRegisterCount(shaderAttribute.type);
256 }
257
Jamie Madill334d6152015-10-22 14:00:28 -0400258 structStream << ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500259
Jamie Madill3b7e2052014-03-17 09:47:43 -0400260 // HLSL code for initialization
Jamie Madill334d6152015-10-22 14:00:28 -0400261 initStream << " " << decorateVariable(shaderAttribute.name) << " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500262
263 // Mismatched vertex attribute to vertex input may result in an undefined
264 // data reinterpretation (eg for pure integer->float, float->pure integer)
265 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400266 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400267 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500268 {
Jamie Madill334d6152015-10-22 14:00:28 -0400269 initStream << generateAttributeConversionHLSL(vertexFormatType, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500270 }
271 else
272 {
Jamie Madill334d6152015-10-22 14:00:28 -0400273 initStream << "input." << decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500274 }
275
Jamie Madill334d6152015-10-22 14:00:28 -0400276 initStream << ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400277
Jamie Madillac0a2672014-04-11 13:33:56 -0400278 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
279 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500280 }
281
Jamie Madill334d6152015-10-22 14:00:28 -0400282 structStream << "};\n"
283 "\n"
284 "void initAttributes(VS_INPUT input)\n"
285 "{\n"
286 << initStream.str() << "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400287
288 std::string vertexHLSL(sourceShader);
289
290 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
Jamie Madill51c47682016-10-25 15:50:14 -0400291 vertexHLSL.replace(copyInsertionPos, strlen(VERTEX_ATTRIBUTE_STUB_STRING), structStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400292
293 return vertexHLSL;
294}
295
Jamie Madill334d6152015-10-22 14:00:28 -0400296std::string DynamicHLSL::generatePixelShaderForOutputSignature(
297 const std::string &sourceShader,
298 const std::vector<PixelShaderOutputVariable> &outputVariables,
299 bool usesFragDepth,
300 const std::vector<GLenum> &outputLayout) const
Geoff Lang04fb89a2014-06-09 15:05:36 -0400301{
Jamie Madill334d6152015-10-22 14:00:28 -0400302 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang04fb89a2014-06-09 15:05:36 -0400303 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
Jamie Madill334d6152015-10-22 14:00:28 -0400304 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400305
Jamie Madill334d6152015-10-22 14:00:28 -0400306 std::stringstream declarationStream;
307 std::stringstream copyStream;
308
309 declarationStream << "struct PS_OUTPUT\n"
310 "{\n";
Geoff Lang4ace4232014-06-18 19:12:48 -0400311
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400312 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
313 {
314 GLenum binding = outputLayout[layoutIndex];
315
316 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400317 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400318 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
319
Jamie Madill334d6152015-10-22 14:00:28 -0400320 const PixelShaderOutputVariable *outputVariable =
321 FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400322
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000323 // OpenGL ES 3.0 spec $4.2.1
Jamie Madill334d6152015-10-22 14:00:28 -0400324 // If [...] not all user-defined output variables are written, the values of fragment
325 // colors
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000326 // corresponding to unwritten variables are similarly undefined.
327 if (outputVariable)
328 {
Jamie Madill334d6152015-10-22 14:00:28 -0400329 declarationStream << " " + HLSLTypeString(outputVariable->type) << " "
330 << outputVariable->name << " : " << targetSemantic
331 << static_cast<int>(layoutIndex) << ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400332
Jamie Madill334d6152015-10-22 14:00:28 -0400333 copyStream << " output." << outputVariable->name << " = "
334 << outputVariable->source << ";\n";
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000335 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400336 }
337 }
338
339 if (usesFragDepth)
340 {
Jamie Madill334d6152015-10-22 14:00:28 -0400341 declarationStream << " float gl_Depth : " << depthSemantic << ";\n";
342 copyStream << " output.gl_Depth = gl_Depth; \n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400343 }
344
Jamie Madill334d6152015-10-22 14:00:28 -0400345 declarationStream << "};\n"
346 "\n"
347 "PS_OUTPUT generateOutput()\n"
348 "{\n"
349 " PS_OUTPUT output;\n"
350 << copyStream.str() << " return output;\n"
351 "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400352
353 std::string pixelHLSL(sourceShader);
354
355 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
Jamie Madill51c47682016-10-25 15:50:14 -0400356 pixelHLSL.replace(outputInsertionPos, strlen(PIXEL_OUTPUT_STUB_STRING),
Jamie Madill334d6152015-10-22 14:00:28 -0400357 declarationStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400358
359 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500360}
361
Jamie Madill9fc36822015-11-18 13:08:07 -0500362void DynamicHLSL::generateVaryingLinkHLSL(ShaderType shaderType,
363 const VaryingPacking &varyingPacking,
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400364 std::stringstream &linkStream) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400365{
Jamie Madill9fc36822015-11-18 13:08:07 -0500366 const auto &builtins = varyingPacking.builtins(shaderType);
367 ASSERT(builtins.dxPosition.enabled);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400368 linkStream << "{\n"
Jamie Madill9fc36822015-11-18 13:08:07 -0500369 << " float4 dx_Position : " << builtins.dxPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700370
Jamie Madill9fc36822015-11-18 13:08:07 -0500371 if (builtins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700372 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500373 linkStream << " float4 gl_Position : " << builtins.glPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700374 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400375
Jamie Madill9fc36822015-11-18 13:08:07 -0500376 if (builtins.glFragCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400377 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500378 linkStream << " float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400379 }
380
Jamie Madill9fc36822015-11-18 13:08:07 -0500381 if (builtins.glPointCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400382 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500383 linkStream << " float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400384 }
385
Jamie Madill9fc36822015-11-18 13:08:07 -0500386 if (builtins.glPointSize.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400387 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500388 linkStream << " float gl_PointSize : " << builtins.glPointSize.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400389 }
390
Corentin Wallez50fcf742016-04-11 13:57:32 -0700391 // Do this after gl_PointSize, to potentially combine gl_PointCoord and gl_PointSize into the
Jamie Madill334d6152015-10-22 14:00:28 -0400392 // same register.
Jamie Madill9fc36822015-11-18 13:08:07 -0500393 generateVaryingHLSL(varyingPacking, linkStream);
Austin Kinross8b695ee2015-03-12 13:12:20 -0700394
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400395 linkStream << "};\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400396}
397
Jamie Madill9082b982016-04-27 15:21:51 -0400398bool DynamicHLSL::generateShaderLinkHLSL(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400399 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500400 const ProgramD3DMetadata &programMetadata,
Jamie Madill9fc36822015-11-18 13:08:07 -0500401 const VaryingPacking &varyingPacking,
Jamie Madill334d6152015-10-22 14:00:28 -0400402 std::string *pixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -0500403 std::string *vertexHLSL) const
Jamie Madill5f562732014-02-14 16:41:24 -0500404{
Jamie Madill334d6152015-10-22 14:00:28 -0400405 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500406
Jamie Madill91445bc2015-09-23 16:47:53 -0400407 const gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
Jamie Madill91445bc2015-09-23 16:47:53 -0400408 const gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
409 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700410 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400411
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800412 // usesViewScale() isn't supported in the D3D9 renderer
413 ASSERT(shaderModel >= 4 || !programMetadata.usesViewScale());
414
Jamie Madill334d6152015-10-22 14:00:28 -0400415 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500416 programMetadata.usesPointSize() &&
417 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400418
Jamie Madill14e95b32015-05-07 10:10:41 -0400419 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400420 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500421
Jamie Madill334d6152015-10-22 14:00:28 -0400422 std::stringstream vertexStream;
423 vertexStream << vertexShaderGL->getTranslatedSource();
424
Cooper Partine6664f02015-01-09 16:22:24 -0800425 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400426 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800427 if (useInstancedPointSpriteEmulation)
428 {
Jamie Madill334d6152015-10-22 14:00:28 -0400429 vertexStream << "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700430 << static_cast<int>(data.getCaps().minAliasedPointSize) << ".0f;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400431 << "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700432 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800433 }
434
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500435 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill51c47682016-10-25 15:50:14 -0400436 vertexStream << "\n" << std::string(VERTEX_ATTRIBUTE_STUB_STRING) << "\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500437
438 // Write the HLSL input/output declarations
439 vertexStream << "struct VS_OUTPUT\n";
440 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, vertexStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400441 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400442 << "VS_OUTPUT main(VS_INPUT input)\n"
443 << "{\n"
444 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500445
Jamie Madill334d6152015-10-22 14:00:28 -0400446 vertexStream << "\n"
447 << " gl_main();\n"
448 << "\n"
449 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700450
Jamie Madill9fc36822015-11-18 13:08:07 -0500451 const auto &vertexBuiltins = varyingPacking.builtins(SHADER_VERTEX);
452
453 if (vertexBuiltins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700454 {
Jamie Madill334d6152015-10-22 14:00:28 -0400455 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700456 }
Jamie Madill37997142015-01-28 10:06:34 -0500457
Austin Kinross4fd18b12014-12-22 12:32:05 -0800458 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400459 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500460 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800461 vertexStream << " output.dx_Position.x = gl_Position.x;\n";
462
463 if (programMetadata.usesViewScale())
464 {
465 // This code assumes that dx_ViewScale.y = -1.0f when rendering to texture, and +1.0f
466 // when rendering to the default framebuffer. No other values are valid.
467 vertexStream << " output.dx_Position.y = dx_ViewScale.y * gl_Position.y;\n";
468 }
469 else
470 {
471 vertexStream << " output.dx_Position.y = - gl_Position.y;\n";
472 }
473
474 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400475 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500476 }
477 else
478 {
Jamie Madill334d6152015-10-22 14:00:28 -0400479 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800480 "dx_ViewAdjust.x * gl_Position.w;\n";
481
482 // If usesViewScale() is true and we're using the D3D11 renderer via Feature Level 9_*,
483 // then we need to multiply the gl_Position.y by the viewScale.
484 // usesViewScale() isn't supported when using the D3D9 renderer.
485 if (programMetadata.usesViewScale() &&
486 (shaderModel >= 4 && mRenderer->getShaderModelSuffix() != ""))
487 {
488 vertexStream << " output.dx_Position.y = dx_ViewScale.y * (gl_Position.y * "
489 "dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n";
490 }
491 else
492 {
493 vertexStream << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
494 "dx_ViewAdjust.y * gl_Position.w);\n";
495 }
496
497 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400498 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500499 }
500
Austin Kinross8b695ee2015-03-12 13:12:20 -0700501 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
Jamie Madill9fc36822015-11-18 13:08:07 -0500502 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500503 {
Jamie Madill334d6152015-10-22 14:00:28 -0400504 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500505 }
506
Jamie Madill9fc36822015-11-18 13:08:07 -0500507 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500508 {
Jamie Madill334d6152015-10-22 14:00:28 -0400509 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500510 }
511
Jamie Madill9fc36822015-11-18 13:08:07 -0500512 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500513 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500514 const auto &packedVarying = *registerInfo.packedVarying;
515 const auto &varying = *packedVarying.varying;
516 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400517
Jamie Madill55c25d02015-11-18 13:08:08 -0500518 vertexStream << " output.v" << registerInfo.semanticIndex << " = ";
519
520 if (packedVarying.isStructField())
521 {
522 vertexStream << decorateVariable(packedVarying.parentStructName) << ".";
523 }
524
525 vertexStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400526
527 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400528 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500529 WriteArrayString(vertexStream, registerInfo.varyingArrayIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500530 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400531
Jamie Madill55c25d02015-11-18 13:08:08 -0500532 if (VariableRowCount(varying.type) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400533 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500534 WriteArrayString(vertexStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400535 }
536
537 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500538 }
539
Cooper Partine6664f02015-01-09 16:22:24 -0800540 // Instanced PointSprite emulation requires additional entries to calculate
541 // the final output vertex positions of the quad that represents each sprite.
542 if (useInstancedPointSpriteEmulation)
543 {
Jamie Madill334d6152015-10-22 14:00:28 -0400544 vertexStream << "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800545 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n";
546
547 vertexStream << " output.dx_Position.x += (input.spriteVertexPos.x * gl_PointSize / "
548 "(dx_ViewCoords.x*2)) * output.dx_Position.w;";
549
550 if (programMetadata.usesViewScale())
551 {
552 // Multiply by ViewScale to invert the rendering when appropriate
553 vertexStream << " output.dx_Position.y += (-dx_ViewScale.y * "
554 "input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2)) * "
555 "output.dx_Position.w;";
556 }
557 else
558 {
559 vertexStream << " output.dx_Position.y += (input.spriteVertexPos.y * gl_PointSize / "
560 "(dx_ViewCoords.y*2)) * output.dx_Position.w;";
561 }
562
563 vertexStream
564 << " output.dx_Position.z += input.spriteVertexPos.z * output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800565
Jamie Madille39a3f02015-11-17 20:42:15 -0500566 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800567 {
Jamie Madill334d6152015-10-22 14:00:28 -0400568 vertexStream << "\n"
569 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800570 }
571 }
572
Cooper Partin7c89d242015-10-13 12:45:59 -0700573 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
574 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
575 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500576 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700577 {
578 ASSERT(!useInstancedPointSpriteEmulation);
579 vertexStream << "\n"
580 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
581 }
582
Jamie Madill334d6152015-10-22 14:00:28 -0400583 vertexStream << "\n"
584 << " return output;\n"
585 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500586
Jamie Madill334d6152015-10-22 14:00:28 -0400587 std::stringstream pixelStream;
588 pixelStream << fragmentShaderGL->getTranslatedSource();
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400589 pixelStream << "struct PS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500590 generateVaryingLinkHLSL(SHADER_PIXEL, varyingPacking, pixelStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400591 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500592
Jamie Madill51c47682016-10-25 15:50:14 -0400593 pixelStream << std::string(PIXEL_OUTPUT_STUB_STRING) << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500594
Jamie Madill334d6152015-10-22 14:00:28 -0400595 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500596 {
597 if (shaderModel >= 4)
598 {
Jamie Madill334d6152015-10-22 14:00:28 -0400599 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
600 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500601 }
602 else
603 {
Jamie Madill334d6152015-10-22 14:00:28 -0400604 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
605 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500606 }
607 }
608 else
609 {
Jamie Madill334d6152015-10-22 14:00:28 -0400610 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
611 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500612 }
613
Jamie Madill9fc36822015-11-18 13:08:07 -0500614 const auto &pixelBuiltins = varyingPacking.builtins(SHADER_PIXEL);
615
616 if (pixelBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500617 {
Jamie Madill334d6152015-10-22 14:00:28 -0400618 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500619
Austin Kinross588434c2014-12-10 10:41:45 -0800620 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400621 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
622 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800623 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500624 {
Jamie Madill334d6152015-10-22 14:00:28 -0400625 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
626 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500627 }
Austin Kinross588434c2014-12-10 10:41:45 -0800628 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500629 {
Jamie Madill334d6152015-10-22 14:00:28 -0400630 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
631 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500632 }
633 else
634 {
Jamie Madill334d6152015-10-22 14:00:28 -0400635 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
636 // Renderer::setViewport()
637 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
638 "dx_ViewCoords.z;\n"
639 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
640 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500641 }
642
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800643 if (programMetadata.usesViewScale())
644 {
645 // For Feature Level 9_3 and below, we need to correct gl_FragCoord.y to account
646 // for dx_ViewScale. On Feature Level 10_0+, gl_FragCoord.y is calculated above using
647 // dx_ViewCoords and is always correct irrespective of dx_ViewScale's value.
648 // NOTE: usesViewScale() can only be true on D3D11 (i.e. Shader Model 4.0+).
649 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
650 {
651 // Some assumptions:
652 // - dx_ViewScale.y = -1.0f when rendering to texture
653 // - dx_ViewScale.y = +1.0f when rendering to the default framebuffer
654 // - gl_FragCoord.y has been set correctly above.
655 //
656 // When rendering to the backbuffer, the code inverts gl_FragCoord's y coordinate.
657 // This involves subtracting the y coordinate from the height of the area being
658 // rendered to.
659 //
660 // First we calculate the height of the area being rendered to:
661 // render_area_height = (2.0f / (1.0f - input.gl_FragCoord.y * rhw)) *
662 // gl_FragCoord.y
663 //
664 // Note that when we're rendering to default FB, we want our output to be
665 // equivalent to:
666 // "gl_FragCoord.y = render_area_height - gl_FragCoord.y"
667 //
668 // When we're rendering to a texture, we want our output to be equivalent to:
669 // "gl_FragCoord.y = gl_FragCoord.y;"
670 //
671 // If we set scale_factor = ((1.0f + dx_ViewScale.y) / 2.0f), then notice that
672 // - When rendering to default FB: scale_factor = 1.0f
673 // - When rendering to texture: scale_factor = 0.0f
674 //
675 // Therefore, we can get our desired output by setting:
676 // "gl_FragCoord.y = scale_factor * render_area_height - dx_ViewScale.y *
677 // gl_FragCoord.y"
678 //
679 // Simplifying, this becomes:
680 pixelStream
681 << " gl_FragCoord.y = (1.0f + dx_ViewScale.y) * gl_FragCoord.y /"
682 "(1.0f - input.gl_FragCoord.y * rhw) - dx_ViewScale.y * gl_FragCoord.y;\n";
683 }
684 }
685
Jamie Madill334d6152015-10-22 14:00:28 -0400686 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
687 "dx_DepthFront.y;\n"
688 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500689 }
690
Jamie Madill9fc36822015-11-18 13:08:07 -0500691 if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500692 {
Jamie Madill334d6152015-10-22 14:00:28 -0400693 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
694 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500695 }
696
Jamie Madill334d6152015-10-22 14:00:28 -0400697 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500698 {
699 if (shaderModel <= 3)
700 {
Jamie Madill334d6152015-10-22 14:00:28 -0400701 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500702 }
703 else
704 {
Jamie Madill334d6152015-10-22 14:00:28 -0400705 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500706 }
707 }
708
Jamie Madill9fc36822015-11-18 13:08:07 -0500709 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500710 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500711 const auto &packedVarying = *registerInfo.packedVarying;
712 const auto &varying = *packedVarying.varying;
713 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400714
Jamie Madillca03b352015-09-02 12:38:13 -0400715 // Don't reference VS-only transform feedback varyings in the PS.
Geoff Lang82a468a2016-06-21 17:18:25 -0400716 // TODO: Consider updating the fragment shader's varyings with a parameter signaling that a
717 // varying is only used in the vertex shader in MergeVaryings
718 if (packedVarying.vertexOnly || (!varying.staticUse && !packedVarying.isStructField()))
Jamie Madillca03b352015-09-02 12:38:13 -0400719 continue;
720
Jamie Madill55c25d02015-11-18 13:08:08 -0500721 pixelStream << " ";
722
723 if (packedVarying.isStructField())
724 {
725 pixelStream << decorateVariable(packedVarying.parentStructName) << ".";
726 }
727
728 pixelStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400729
730 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400731 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500732 WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400733 }
734
Jamie Madill55c25d02015-11-18 13:08:08 -0500735 GLenum transposedType = TransposeMatrixType(varying.type);
736 if (VariableRowCount(transposedType) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400737 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500738 WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400739 }
740
Jamie Madill9fc36822015-11-18 13:08:07 -0500741 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400742
Jamie Madill55c25d02015-11-18 13:08:08 -0500743 switch (VariableColumnCount(transposedType))
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400744 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500745 case 1:
746 pixelStream << ".x";
747 break;
748 case 2:
749 pixelStream << ".xy";
750 break;
751 case 3:
752 pixelStream << ".xyz";
753 break;
754 case 4:
755 break;
756 default:
757 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500758 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400759 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500760 }
761
Jamie Madill334d6152015-10-22 14:00:28 -0400762 pixelStream << "\n"
763 << " gl_main();\n"
764 << "\n"
765 << " return generateOutput();\n"
766 << "}\n";
767
768 *vertexHLSL = vertexStream.str();
769 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500770
771 return true;
772}
773
Jamie Madill9fc36822015-11-18 13:08:07 -0500774std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking) const
Jamie Madill5f562732014-02-14 16:41:24 -0500775{
Jamie Madill9fc36822015-11-18 13:08:07 -0500776 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400777
Jamie Madill4e31ad52015-10-29 10:32:57 -0400778 std::stringstream preambleStream;
779
Jamie Madill9fc36822015-11-18 13:08:07 -0500780 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
781
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400782 preambleStream << "struct GS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500783 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400784 preambleStream << "\n"
785 << "struct GS_OUTPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500786 generateVaryingLinkHLSL(SHADER_GEOMETRY, varyingPacking, preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400787 preambleStream
788 << "\n"
789 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
790 << "{\n"
791 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400792
Jamie Madill9fc36822015-11-18 13:08:07 -0500793 if (builtins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400794 {
795 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
796 }
797
Jamie Madill9fc36822015-11-18 13:08:07 -0500798 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400799 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500800 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill55c25d02015-11-18 13:08:08 -0500801 if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400802 {
803 preambleStream << "flat";
804 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500805 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400806 }
807
Jamie Madill9fc36822015-11-18 13:08:07 -0500808 if (builtins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400809 {
810 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
811 }
812
813 // Only write the dx_Position if we aren't using point sprites
814 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
815 << " output.dx_Position = input.dx_Position;\n"
816 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
817 << "}\n";
818
819 return preambleStream.str();
820}
821
822std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
Jamie Madill9082b982016-04-27 15:21:51 -0400823 const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400824 const gl::ProgramState &programData,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800825 const bool useViewScale,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400826 const std::string &preambleString) const
827{
828 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500829
Jamie Madill334d6152015-10-22 14:00:28 -0400830 std::stringstream shaderStream;
831
Jamie Madill4e31ad52015-10-29 10:32:57 -0400832 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
833 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500834
Jamie Madill76f8fa62015-10-29 10:32:56 -0400835 const char *inputPT = nullptr;
836 const char *outputPT = nullptr;
837 int inputSize = 0;
838 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500839
Jamie Madill76f8fa62015-10-29 10:32:56 -0400840 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500841 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400842 case PRIMITIVE_POINTS:
843 inputPT = "point";
844 outputPT = "Triangle";
845 inputSize = 1;
846 maxVertexOutput = 4;
847 break;
848
849 case PRIMITIVE_LINES:
850 case PRIMITIVE_LINE_STRIP:
851 case PRIMITIVE_LINE_LOOP:
852 inputPT = "line";
853 outputPT = "Line";
854 inputSize = 2;
855 maxVertexOutput = 2;
856 break;
857
858 case PRIMITIVE_TRIANGLES:
859 case PRIMITIVE_TRIANGLE_STRIP:
860 case PRIMITIVE_TRIANGLE_FAN:
861 inputPT = "triangle";
862 outputPT = "Triangle";
863 inputSize = 3;
864 maxVertexOutput = 3;
865 break;
866
867 default:
868 UNREACHABLE();
869 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500870 }
871
Jamie Madill76f8fa62015-10-29 10:32:56 -0400872 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500873 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400874 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
875 "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800876 "uniform float4 dx_ViewCoords : register(c1);\n";
877
878 if (useViewScale)
879 {
880 shaderStream << "uniform float2 dx_ViewScale : register(c3);\n";
881 }
882
883 shaderStream << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400884 "static float2 pointSpriteCorners[] = \n"
885 "{\n"
886 " float2( 0.5f, -0.5f),\n"
887 " float2( 0.5f, 0.5f),\n"
888 " float2(-0.5f, -0.5f),\n"
889 " float2(-0.5f, 0.5f)\n"
890 "};\n"
891 "\n"
892 "static float2 pointSpriteTexcoords[] = \n"
893 "{\n"
894 " float2(1.0f, 1.0f),\n"
895 " float2(1.0f, 0.0f),\n"
896 " float2(0.0f, 1.0f),\n"
897 " float2(0.0f, 0.0f)\n"
898 "};\n"
899 "\n"
900 "static float minPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700901 << static_cast<int>(data.getCaps().minAliasedPointSize)
Jamie Madill76f8fa62015-10-29 10:32:56 -0400902 << ".0f;\n"
903 "static float maxPointSize = "
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700904 << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400905 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500906 }
907
Jamie Madill4e31ad52015-10-29 10:32:57 -0400908 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400909 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400910 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
911
912 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
913 {
914 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
915 }
916
917 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400918 << "{\n"
919 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500920
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400921 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
922 {
923 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
924 }
925 else
926 {
927 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
928 }
929
Jamie Madill4e31ad52015-10-29 10:32:57 -0400930 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -0500931 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400932 shaderStream << " copyVertex(output, input[" << vertexIndex
933 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400934
935 if (!pointSprites)
936 {
937 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400938 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400939 }
940 }
941
942 if (pointSprites)
943 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400944 shaderStream << "\n"
945 " float4 dx_Position = input[0].dx_Position;\n"
946 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
947 "maxPointSize);\n"
948 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
949 "dx_ViewCoords.y) * dx_Position.w;\n";
950
951 for (int corner = 0; corner < 4; corner++)
952 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800953 if (useViewScale)
954 {
955 shaderStream << " \n"
956 " output.dx_Position = dx_Position + float4(1.0f, "
957 "-dx_ViewScale.y, 1.0f, 1.0f)"
958 " * float4(pointSpriteCorners["
959 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
960 }
961 else
962 {
963 shaderStream << "\n"
964 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
965 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
966 }
Jamie Madill76f8fa62015-10-29 10:32:56 -0400967
968 if (usesPointCoord)
969 {
970 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
971 << "];\n";
972 }
973
974 shaderStream << " outStream.Append(output);\n";
975 }
Jamie Madill5f562732014-02-14 16:41:24 -0500976 }
977
Jamie Madill334d6152015-10-22 14:00:28 -0400978 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400979 " outStream.RestartStrip();\n"
980 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500981
Jamie Madill334d6152015-10-22 14:00:28 -0400982 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500983}
984
985// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -0400986std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -0500987{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400988 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -0500989 {
990 return "_" + name;
991 }
992
993 return name;
994}
995
Jamie Madill334d6152015-10-22 14:00:28 -0400996std::string DynamicHLSL::generateAttributeConversionHLSL(
997 gl::VertexFormatType vertexFormatType,
998 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500999{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001000 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -04001001 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001002
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001003 // Matrix
1004 if (IsMatrixType(shaderAttrib.type))
1005 {
Jamie Madill8664b062014-02-14 16:41:29 -05001006 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001007 }
1008
Jamie Madillf2575982014-06-25 16:04:54 -04001009 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001010 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001011
Jamie Madill8664b062014-02-14 16:41:29 -05001012 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -04001013 bool requiresTypeConversion =
1014 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001015
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001016 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001017 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001018 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001019 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001020 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001021 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001022
1023 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001024 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001025}
Jamie Madille39a3f02015-11-17 20:42:15 -05001026
Jamie Madill9082b982016-04-27 15:21:51 -04001027void DynamicHLSL::getPixelShaderOutputKey(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001028 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -05001029 const ProgramD3DMetadata &metadata,
1030 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1031{
1032 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1033 // - with a 3.0 context, the output color is copied to channel 0
1034 // - with a 2.0 context, the output color is broadcast to all channels
1035 bool broadcast = metadata.usesBroadcast(data);
1036 const unsigned int numRenderTargets =
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001037 (broadcast || metadata.usesMultipleFragmentOuts() ? data.getCaps().maxDrawBuffers : 1);
Jamie Madille39a3f02015-11-17 20:42:15 -05001038
1039 if (metadata.getMajorShaderVersion() < 300)
1040 {
1041 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1042 renderTargetIndex++)
1043 {
1044 PixelShaderOutputVariable outputKeyVariable;
1045 outputKeyVariable.type = GL_FLOAT_VEC4;
1046 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1047 outputKeyVariable.source =
1048 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1049 outputKeyVariable.outputIndex = renderTargetIndex;
1050
1051 outPixelShaderKey->push_back(outputKeyVariable);
1052 }
1053 }
1054 else
1055 {
1056 const auto &shaderOutputVars =
1057 metadata.getFragmentShader()->getData().getActiveOutputVariables();
1058
1059 for (auto outputPair : programData.getOutputVariables())
1060 {
1061 const VariableLocation &outputLocation = outputPair.second;
1062 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1063 const std::string &variableName = "out_" + outputLocation.name;
1064 const std::string &elementString =
1065 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
1066
1067 ASSERT(outputVariable.staticUse);
1068
1069 PixelShaderOutputVariable outputKeyVariable;
1070 outputKeyVariable.type = outputVariable.type;
1071 outputKeyVariable.name = variableName + elementString;
1072 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
1073 outputKeyVariable.outputIndex = outputPair.first;
1074
1075 outPixelShaderKey->push_back(outputKeyVariable);
1076 }
1077 }
1078}
1079
Jamie Madill334d6152015-10-22 14:00:28 -04001080} // namespace rx