blob: 42a534f5734b908e49dda774cf8fb5ebae5b774e [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 Madill65345da2015-11-13 11:25:23 -050019#include "libANGLE/renderer/d3d/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 Madill4cff2472015-08-21 16:53:18 -0400120const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
121const std::string 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 Madill65345da2015-11-13 11:25:23 -0500124std::string GetVaryingSemantic(int majorShaderModel, bool programUsesPointSize)
Geoff Lang48dcae72014-02-05 16:28:24 -0500125{
Jamie Madill65345da2015-11-13 11:25:23 -0500126 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
127 // In D3D11 we manually compute gl_PointCoord in the GS.
128 return ((programUsesPointSize && majorShaderModel < 4) ? "COLOR" : "TEXCOORD");
Jamie Madill5f562732014-02-14 16:41:24 -0500129}
130
Jamie Madill9fc36822015-11-18 13:08:07 -0500131// DynamicHLSL implementation
132
Jamie Madill65345da2015-11-13 11:25:23 -0500133DynamicHLSL::DynamicHLSL(RendererD3D *const renderer) : mRenderer(renderer)
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000134{
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000135}
136
Jamie Madill9fc36822015-11-18 13:08:07 -0500137void DynamicHLSL::generateVaryingHLSL(const VaryingPacking &varyingPacking,
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400138 std::stringstream &hlslStream) const
Jamie Madill5f562732014-02-14 16:41:24 -0500139{
Jamie Madill65345da2015-11-13 11:25:23 -0500140 std::string varyingSemantic =
Jamie Madill9fc36822015-11-18 13:08:07 -0500141 GetVaryingSemantic(mRenderer->getMajorShaderModel(), varyingPacking.usesPointSize());
Jamie Madill5f562732014-02-14 16:41:24 -0500142
Jamie Madill9fc36822015-11-18 13:08:07 -0500143 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500144 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500145 const auto &varying = *registerInfo.packedVarying->varying;
146 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400147
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400148 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many
149 // registers being used.
150 // For example, if there are N registers, and we have N vec3 varyings and 1 float
151 // varying, then D3D will pack them into N registers.
152 // If the float varying has the 'nointerpolation' modifier on it then we would need
153 // N + 1 registers, and D3D compilation will fail.
Jamie Madill4cff2472015-08-21 16:53:18 -0400154
Jamie Madill55c25d02015-11-18 13:08:08 -0500155 switch (registerInfo.packedVarying->interpolation)
Jamie Madill4cff2472015-08-21 16:53:18 -0400156 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400157 case sh::INTERPOLATION_SMOOTH:
158 hlslStream << " ";
159 break;
160 case sh::INTERPOLATION_FLAT:
161 hlslStream << " nointerpolation ";
162 break;
163 case sh::INTERPOLATION_CENTROID:
164 hlslStream << " centroid ";
165 break;
166 default:
167 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500168 }
Jamie Madill5f562732014-02-14 16:41:24 -0500169
Jamie Madill55c25d02015-11-18 13:08:08 -0500170 GLenum transposedType = gl::TransposeMatrixType(varying.type);
171 GLenum componentType = gl::VariableComponentType(transposedType);
172 int columnCount = gl::VariableColumnCount(transposedType);
173 hlslStream << HLSLComponentTypeString(componentType, columnCount);
174 unsigned int semanticIndex = registerInfo.semanticIndex;
Jamie Madill9fc36822015-11-18 13:08:07 -0500175 hlslStream << " v" << semanticIndex << " : " << varyingSemantic << semanticIndex << ";\n";
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400176 }
Jamie Madill5f562732014-02-14 16:41:24 -0500177}
178
Jamie Madill334d6152015-10-22 14:00:28 -0400179std::string DynamicHLSL::generateVertexShaderForInputLayout(
180 const std::string &sourceShader,
181 const InputLayout &inputLayout,
182 const std::vector<sh::Attribute> &shaderAttributes) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500183{
Jamie Madill334d6152015-10-22 14:00:28 -0400184 std::stringstream structStream;
185 std::stringstream initStream;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500186
Jamie Madill334d6152015-10-22 14:00:28 -0400187 structStream << "struct VS_INPUT\n"
188 << "{\n";
189
190 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400191 unsigned int inputIndex = 0;
192
Cooper Partine6d14cc2015-02-20 12:32:58 -0800193 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
194 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
195 // must be used.
196 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
Jamie Madill334d6152015-10-22 14:00:28 -0400197 bool useInstancedPointSpriteEmulation =
198 usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Cooper Partine6d14cc2015-02-20 12:32:58 -0800199
200 // Instanced PointSprite emulation requires additional entries in the
201 // VS_INPUT structure to support the vertices that make up the quad vertices.
202 // These values must be in sync with the cooresponding values added during inputlayout creation
203 // in InputLayoutCache::applyVertexBuffers().
204 //
205 // The additional entries must appear first in the VS_INPUT layout because
206 // Windows Phone 8 era devices require per vertex data to physically come
207 // before per instance data in the shader.
208 if (useInstancedPointSpriteEmulation)
209 {
Jamie Madill334d6152015-10-22 14:00:28 -0400210 structStream << " float3 spriteVertexPos : SPRITEPOSITION0;\n"
211 << " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
Cooper Partine6d14cc2015-02-20 12:32:58 -0800212 }
213
Jamie Madill3da79b72015-04-27 11:09:17 -0400214 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex)
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500215 {
Jamie Madillf2575982014-06-25 16:04:54 -0400216 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500217 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500218 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400219 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400220 VertexFormatType vertexFormatType =
221 inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID;
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400222
Jamie Madill3b7e2052014-03-17 09:47:43 -0400223 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500224 if (IsMatrixType(shaderAttribute.type))
225 {
226 // Matrix types are always transposed
Jamie Madill334d6152015-10-22 14:00:28 -0400227 structStream << " "
228 << HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500229 }
230 else
231 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400232 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000233
234 if (shaderAttribute.name == "gl_InstanceID")
235 {
Jamie Madill334d6152015-10-22 14:00:28 -0400236 // The input type of the instance ID in HLSL (uint) differs from the one in ESSL
237 // (int).
238 structStream << " uint";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000239 }
240 else
241 {
Jamie Madill334d6152015-10-22 14:00:28 -0400242 structStream << " " << HLSLComponentTypeString(
243 componentType,
244 VariableComponentCount(shaderAttribute.type));
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000245 }
Jamie Madill8664b062014-02-14 16:41:29 -0500246 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500247
Jamie Madill334d6152015-10-22 14:00:28 -0400248 structStream << " " << decorateVariable(shaderAttribute.name) << " : ";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000249
250 if (shaderAttribute.name == "gl_InstanceID")
251 {
Jamie Madill334d6152015-10-22 14:00:28 -0400252 structStream << "SV_InstanceID";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000253 }
254 else
255 {
Jamie Madill334d6152015-10-22 14:00:28 -0400256 structStream << "TEXCOORD" << semanticIndex;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000257 semanticIndex += VariableRegisterCount(shaderAttribute.type);
258 }
259
Jamie Madill334d6152015-10-22 14:00:28 -0400260 structStream << ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500261
Jamie Madill3b7e2052014-03-17 09:47:43 -0400262 // HLSL code for initialization
Jamie Madill334d6152015-10-22 14:00:28 -0400263 initStream << " " << decorateVariable(shaderAttribute.name) << " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500264
265 // Mismatched vertex attribute to vertex input may result in an undefined
266 // data reinterpretation (eg for pure integer->float, float->pure integer)
267 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400268 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400269 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500270 {
Jamie Madill334d6152015-10-22 14:00:28 -0400271 initStream << generateAttributeConversionHLSL(vertexFormatType, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500272 }
273 else
274 {
Jamie Madill334d6152015-10-22 14:00:28 -0400275 initStream << "input." << decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500276 }
277
Jamie Madill334d6152015-10-22 14:00:28 -0400278 initStream << ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400279
Jamie Madillac0a2672014-04-11 13:33:56 -0400280 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
281 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500282 }
283
Jamie Madill334d6152015-10-22 14:00:28 -0400284 structStream << "};\n"
285 "\n"
286 "void initAttributes(VS_INPUT input)\n"
287 "{\n"
288 << initStream.str() << "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400289
290 std::string vertexHLSL(sourceShader);
291
292 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
Jamie Madill334d6152015-10-22 14:00:28 -0400293 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), structStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400294
295 return vertexHLSL;
296}
297
Jamie Madill334d6152015-10-22 14:00:28 -0400298std::string DynamicHLSL::generatePixelShaderForOutputSignature(
299 const std::string &sourceShader,
300 const std::vector<PixelShaderOutputVariable> &outputVariables,
301 bool usesFragDepth,
302 const std::vector<GLenum> &outputLayout) const
Geoff Lang04fb89a2014-06-09 15:05:36 -0400303{
Jamie Madill334d6152015-10-22 14:00:28 -0400304 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang04fb89a2014-06-09 15:05:36 -0400305 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
Jamie Madill334d6152015-10-22 14:00:28 -0400306 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400307
Jamie Madill334d6152015-10-22 14:00:28 -0400308 std::stringstream declarationStream;
309 std::stringstream copyStream;
310
311 declarationStream << "struct PS_OUTPUT\n"
312 "{\n";
Geoff Lang4ace4232014-06-18 19:12:48 -0400313
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400314 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
315 {
316 GLenum binding = outputLayout[layoutIndex];
317
318 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400319 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400320 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
321
Jamie Madill334d6152015-10-22 14:00:28 -0400322 const PixelShaderOutputVariable *outputVariable =
323 FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400324
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000325 // OpenGL ES 3.0 spec $4.2.1
Jamie Madill334d6152015-10-22 14:00:28 -0400326 // If [...] not all user-defined output variables are written, the values of fragment
327 // colors
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000328 // corresponding to unwritten variables are similarly undefined.
329 if (outputVariable)
330 {
Jamie Madill334d6152015-10-22 14:00:28 -0400331 declarationStream << " " + HLSLTypeString(outputVariable->type) << " "
332 << outputVariable->name << " : " << targetSemantic
333 << static_cast<int>(layoutIndex) << ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400334
Jamie Madill334d6152015-10-22 14:00:28 -0400335 copyStream << " output." << outputVariable->name << " = "
336 << outputVariable->source << ";\n";
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000337 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400338 }
339 }
340
341 if (usesFragDepth)
342 {
Jamie Madill334d6152015-10-22 14:00:28 -0400343 declarationStream << " float gl_Depth : " << depthSemantic << ";\n";
344 copyStream << " output.gl_Depth = gl_Depth; \n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400345 }
346
Jamie Madill334d6152015-10-22 14:00:28 -0400347 declarationStream << "};\n"
348 "\n"
349 "PS_OUTPUT generateOutput()\n"
350 "{\n"
351 " PS_OUTPUT output;\n"
352 << copyStream.str() << " return output;\n"
353 "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400354
355 std::string pixelHLSL(sourceShader);
356
357 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
Jamie Madill334d6152015-10-22 14:00:28 -0400358 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(),
359 declarationStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400360
361 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500362}
363
Jamie Madill9fc36822015-11-18 13:08:07 -0500364void DynamicHLSL::generateVaryingLinkHLSL(ShaderType shaderType,
365 const VaryingPacking &varyingPacking,
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400366 std::stringstream &linkStream) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400367{
Jamie Madill9fc36822015-11-18 13:08:07 -0500368 const auto &builtins = varyingPacking.builtins(shaderType);
369 ASSERT(builtins.dxPosition.enabled);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400370 linkStream << "{\n"
Jamie Madill9fc36822015-11-18 13:08:07 -0500371 << " float4 dx_Position : " << builtins.dxPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700372
Jamie Madill9fc36822015-11-18 13:08:07 -0500373 if (builtins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700374 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500375 linkStream << " float4 gl_Position : " << builtins.glPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700376 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400377
Jamie Madill9fc36822015-11-18 13:08:07 -0500378 if (builtins.glFragCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400379 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500380 linkStream << " float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400381 }
382
Jamie Madill9fc36822015-11-18 13:08:07 -0500383 if (builtins.glPointCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400384 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500385 linkStream << " float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400386 }
387
Jamie Madill9fc36822015-11-18 13:08:07 -0500388 if (builtins.glPointSize.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400389 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500390 linkStream << " float gl_PointSize : " << builtins.glPointSize.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400391 }
392
Jamie Madill334d6152015-10-22 14:00:28 -0400393 // Do this after glPointSize, to potentially combine gl_PointCoord and gl_PointSize into the
394 // same register.
Jamie Madill9fc36822015-11-18 13:08:07 -0500395 generateVaryingHLSL(varyingPacking, linkStream);
Austin Kinross8b695ee2015-03-12 13:12:20 -0700396
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400397 linkStream << "};\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400398}
399
Jamie Madillada9ecc2015-08-17 12:53:37 -0400400bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data,
Jamie Madill80a6fc02015-08-21 16:53:16 -0400401 const gl::Program::Data &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500402 const ProgramD3DMetadata &programMetadata,
Jamie Madill9fc36822015-11-18 13:08:07 -0500403 const VaryingPacking &varyingPacking,
Jamie Madill334d6152015-10-22 14:00:28 -0400404 std::string *pixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -0500405 std::string *vertexHLSL) const
Jamie Madill5f562732014-02-14 16:41:24 -0500406{
Jamie Madill334d6152015-10-22 14:00:28 -0400407 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500408
Jamie Madill91445bc2015-09-23 16:47:53 -0400409 const gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
410 const ShaderD3D *vertexShader = GetImplAs<ShaderD3D>(vertexShaderGL);
411 const gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
412 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700413 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400414
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800415 // usesViewScale() isn't supported in the D3D9 renderer
416 ASSERT(shaderModel >= 4 || !programMetadata.usesViewScale());
417
Jamie Madill334d6152015-10-22 14:00:28 -0400418 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500419 programMetadata.usesPointSize() &&
420 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400421
Jamie Madill14e95b32015-05-07 10:10:41 -0400422 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400423 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500424
Jamie Madill334d6152015-10-22 14:00:28 -0400425 std::stringstream vertexStream;
426 vertexStream << vertexShaderGL->getTranslatedSource();
427
Cooper Partine6664f02015-01-09 16:22:24 -0800428 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400429 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800430 if (useInstancedPointSpriteEmulation)
431 {
Jamie Madill334d6152015-10-22 14:00:28 -0400432 vertexStream << "static float minPointSize = "
433 << static_cast<int>(data.caps->minAliasedPointSize) << ".0f;\n"
434 << "static float maxPointSize = "
435 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800436 }
437
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500438 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill9fc36822015-11-18 13:08:07 -0500439 vertexStream << "\n" << VERTEX_ATTRIBUTE_STUB_STRING + "\n";
440
441 // Write the HLSL input/output declarations
442 vertexStream << "struct VS_OUTPUT\n";
443 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, vertexStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400444 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400445 << "VS_OUTPUT main(VS_INPUT input)\n"
446 << "{\n"
447 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500448
Jamie Madill37997142015-01-28 10:06:34 -0500449 if (vertexShader->usesDeferredInit())
450 {
Jamie Madill334d6152015-10-22 14:00:28 -0400451 vertexStream << "\n"
452 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500453 }
454
Jamie Madill334d6152015-10-22 14:00:28 -0400455 vertexStream << "\n"
456 << " gl_main();\n"
457 << "\n"
458 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700459
Jamie Madill9fc36822015-11-18 13:08:07 -0500460 const auto &vertexBuiltins = varyingPacking.builtins(SHADER_VERTEX);
461
462 if (vertexBuiltins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700463 {
Jamie Madill334d6152015-10-22 14:00:28 -0400464 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700465 }
Jamie Madill37997142015-01-28 10:06:34 -0500466
Austin Kinross4fd18b12014-12-22 12:32:05 -0800467 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400468 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500469 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800470 vertexStream << " output.dx_Position.x = gl_Position.x;\n";
471
472 if (programMetadata.usesViewScale())
473 {
474 // This code assumes that dx_ViewScale.y = -1.0f when rendering to texture, and +1.0f
475 // when rendering to the default framebuffer. No other values are valid.
476 vertexStream << " output.dx_Position.y = dx_ViewScale.y * gl_Position.y;\n";
477 }
478 else
479 {
480 vertexStream << " output.dx_Position.y = - gl_Position.y;\n";
481 }
482
483 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400484 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500485 }
486 else
487 {
Jamie Madill334d6152015-10-22 14:00:28 -0400488 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800489 "dx_ViewAdjust.x * gl_Position.w;\n";
490
491 // If usesViewScale() is true and we're using the D3D11 renderer via Feature Level 9_*,
492 // then we need to multiply the gl_Position.y by the viewScale.
493 // usesViewScale() isn't supported when using the D3D9 renderer.
494 if (programMetadata.usesViewScale() &&
495 (shaderModel >= 4 && mRenderer->getShaderModelSuffix() != ""))
496 {
497 vertexStream << " output.dx_Position.y = dx_ViewScale.y * (gl_Position.y * "
498 "dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n";
499 }
500 else
501 {
502 vertexStream << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
503 "dx_ViewAdjust.y * gl_Position.w);\n";
504 }
505
506 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400507 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500508 }
509
Austin Kinross8b695ee2015-03-12 13:12:20 -0700510 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
Jamie Madill9fc36822015-11-18 13:08:07 -0500511 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500512 {
Jamie Madill334d6152015-10-22 14:00:28 -0400513 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500514 }
515
Jamie Madill9fc36822015-11-18 13:08:07 -0500516 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500517 {
Jamie Madill334d6152015-10-22 14:00:28 -0400518 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500519 }
520
Jamie Madill9fc36822015-11-18 13:08:07 -0500521 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500522 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500523 const auto &packedVarying = *registerInfo.packedVarying;
524 const auto &varying = *packedVarying.varying;
525 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400526
Jamie Madill55c25d02015-11-18 13:08:08 -0500527 vertexStream << " output.v" << registerInfo.semanticIndex << " = ";
528
529 if (packedVarying.isStructField())
530 {
531 vertexStream << decorateVariable(packedVarying.parentStructName) << ".";
532 }
533
534 vertexStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400535
536 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400537 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500538 WriteArrayString(vertexStream, registerInfo.varyingArrayIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500539 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400540
Jamie Madill55c25d02015-11-18 13:08:08 -0500541 if (VariableRowCount(varying.type) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400542 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500543 WriteArrayString(vertexStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400544 }
545
546 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500547 }
548
Cooper Partine6664f02015-01-09 16:22:24 -0800549 // Instanced PointSprite emulation requires additional entries to calculate
550 // the final output vertex positions of the quad that represents each sprite.
551 if (useInstancedPointSpriteEmulation)
552 {
Jamie Madill334d6152015-10-22 14:00:28 -0400553 vertexStream << "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800554 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n";
555
556 vertexStream << " output.dx_Position.x += (input.spriteVertexPos.x * gl_PointSize / "
557 "(dx_ViewCoords.x*2)) * output.dx_Position.w;";
558
559 if (programMetadata.usesViewScale())
560 {
561 // Multiply by ViewScale to invert the rendering when appropriate
562 vertexStream << " output.dx_Position.y += (-dx_ViewScale.y * "
563 "input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2)) * "
564 "output.dx_Position.w;";
565 }
566 else
567 {
568 vertexStream << " output.dx_Position.y += (input.spriteVertexPos.y * gl_PointSize / "
569 "(dx_ViewCoords.y*2)) * output.dx_Position.w;";
570 }
571
572 vertexStream
573 << " output.dx_Position.z += input.spriteVertexPos.z * output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800574
Jamie Madille39a3f02015-11-17 20:42:15 -0500575 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800576 {
Jamie Madill334d6152015-10-22 14:00:28 -0400577 vertexStream << "\n"
578 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800579 }
580 }
581
Cooper Partin7c89d242015-10-13 12:45:59 -0700582 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
583 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
584 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500585 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700586 {
587 ASSERT(!useInstancedPointSpriteEmulation);
588 vertexStream << "\n"
589 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
590 }
591
Jamie Madill334d6152015-10-22 14:00:28 -0400592 vertexStream << "\n"
593 << " return output;\n"
594 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500595
Jamie Madill334d6152015-10-22 14:00:28 -0400596 std::stringstream pixelStream;
597 pixelStream << fragmentShaderGL->getTranslatedSource();
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400598 pixelStream << "struct PS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500599 generateVaryingLinkHLSL(SHADER_PIXEL, varyingPacking, pixelStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400600 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500601
Jamie Madill334d6152015-10-22 14:00:28 -0400602 pixelStream << PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500603
Jamie Madill334d6152015-10-22 14:00:28 -0400604 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500605 {
606 if (shaderModel >= 4)
607 {
Jamie Madill334d6152015-10-22 14:00:28 -0400608 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
609 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500610 }
611 else
612 {
Jamie Madill334d6152015-10-22 14:00:28 -0400613 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
614 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500615 }
616 }
617 else
618 {
Jamie Madill334d6152015-10-22 14:00:28 -0400619 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
620 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500621 }
622
Jamie Madill9fc36822015-11-18 13:08:07 -0500623 const auto &pixelBuiltins = varyingPacking.builtins(SHADER_PIXEL);
624
625 if (pixelBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500626 {
Jamie Madill334d6152015-10-22 14:00:28 -0400627 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500628
Austin Kinross588434c2014-12-10 10:41:45 -0800629 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400630 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
631 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800632 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500633 {
Jamie Madill334d6152015-10-22 14:00:28 -0400634 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
635 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500636 }
Austin Kinross588434c2014-12-10 10:41:45 -0800637 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500638 {
Jamie Madill334d6152015-10-22 14:00:28 -0400639 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
640 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500641 }
642 else
643 {
Jamie Madill334d6152015-10-22 14:00:28 -0400644 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
645 // Renderer::setViewport()
646 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
647 "dx_ViewCoords.z;\n"
648 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
649 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500650 }
651
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800652 if (programMetadata.usesViewScale())
653 {
654 // For Feature Level 9_3 and below, we need to correct gl_FragCoord.y to account
655 // for dx_ViewScale. On Feature Level 10_0+, gl_FragCoord.y is calculated above using
656 // dx_ViewCoords and is always correct irrespective of dx_ViewScale's value.
657 // NOTE: usesViewScale() can only be true on D3D11 (i.e. Shader Model 4.0+).
658 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
659 {
660 // Some assumptions:
661 // - dx_ViewScale.y = -1.0f when rendering to texture
662 // - dx_ViewScale.y = +1.0f when rendering to the default framebuffer
663 // - gl_FragCoord.y has been set correctly above.
664 //
665 // When rendering to the backbuffer, the code inverts gl_FragCoord's y coordinate.
666 // This involves subtracting the y coordinate from the height of the area being
667 // rendered to.
668 //
669 // First we calculate the height of the area being rendered to:
670 // render_area_height = (2.0f / (1.0f - input.gl_FragCoord.y * rhw)) *
671 // gl_FragCoord.y
672 //
673 // Note that when we're rendering to default FB, we want our output to be
674 // equivalent to:
675 // "gl_FragCoord.y = render_area_height - gl_FragCoord.y"
676 //
677 // When we're rendering to a texture, we want our output to be equivalent to:
678 // "gl_FragCoord.y = gl_FragCoord.y;"
679 //
680 // If we set scale_factor = ((1.0f + dx_ViewScale.y) / 2.0f), then notice that
681 // - When rendering to default FB: scale_factor = 1.0f
682 // - When rendering to texture: scale_factor = 0.0f
683 //
684 // Therefore, we can get our desired output by setting:
685 // "gl_FragCoord.y = scale_factor * render_area_height - dx_ViewScale.y *
686 // gl_FragCoord.y"
687 //
688 // Simplifying, this becomes:
689 pixelStream
690 << " gl_FragCoord.y = (1.0f + dx_ViewScale.y) * gl_FragCoord.y /"
691 "(1.0f - input.gl_FragCoord.y * rhw) - dx_ViewScale.y * gl_FragCoord.y;\n";
692 }
693 }
694
Jamie Madill334d6152015-10-22 14:00:28 -0400695 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
696 "dx_DepthFront.y;\n"
697 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500698 }
699
Jamie Madill9fc36822015-11-18 13:08:07 -0500700 if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500701 {
Jamie Madill334d6152015-10-22 14:00:28 -0400702 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
703 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500704 }
705
Jamie Madill334d6152015-10-22 14:00:28 -0400706 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500707 {
708 if (shaderModel <= 3)
709 {
Jamie Madill334d6152015-10-22 14:00:28 -0400710 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500711 }
712 else
713 {
Jamie Madill334d6152015-10-22 14:00:28 -0400714 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500715 }
716 }
717
Jamie Madill9fc36822015-11-18 13:08:07 -0500718 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500719 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500720 const auto &packedVarying = *registerInfo.packedVarying;
721 const auto &varying = *packedVarying.varying;
722 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400723
Jamie Madillca03b352015-09-02 12:38:13 -0400724 // Don't reference VS-only transform feedback varyings in the PS.
Jamie Madill9fc36822015-11-18 13:08:07 -0500725 if (registerInfo.packedVarying->vertexOnly)
Jamie Madillca03b352015-09-02 12:38:13 -0400726 continue;
727
Jamie Madill55c25d02015-11-18 13:08:08 -0500728 pixelStream << " ";
729
730 if (packedVarying.isStructField())
731 {
732 pixelStream << decorateVariable(packedVarying.parentStructName) << ".";
733 }
734
735 pixelStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400736
737 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400738 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500739 WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400740 }
741
Jamie Madill55c25d02015-11-18 13:08:08 -0500742 GLenum transposedType = TransposeMatrixType(varying.type);
743 if (VariableRowCount(transposedType) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400744 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500745 WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400746 }
747
Jamie Madill9fc36822015-11-18 13:08:07 -0500748 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400749
Jamie Madill55c25d02015-11-18 13:08:08 -0500750 switch (VariableColumnCount(transposedType))
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400751 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500752 case 1:
753 pixelStream << ".x";
754 break;
755 case 2:
756 pixelStream << ".xy";
757 break;
758 case 3:
759 pixelStream << ".xyz";
760 break;
761 case 4:
762 break;
763 default:
764 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500765 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400766 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500767 }
768
Jamie Madill37997142015-01-28 10:06:34 -0500769 if (fragmentShader->usesDeferredInit())
770 {
Jamie Madill334d6152015-10-22 14:00:28 -0400771 pixelStream << "\n"
772 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500773 }
774
Jamie Madill334d6152015-10-22 14:00:28 -0400775 pixelStream << "\n"
776 << " gl_main();\n"
777 << "\n"
778 << " return generateOutput();\n"
779 << "}\n";
780
781 *vertexHLSL = vertexStream.str();
782 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500783
784 return true;
785}
786
Jamie Madill9fc36822015-11-18 13:08:07 -0500787std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking) const
Jamie Madill5f562732014-02-14 16:41:24 -0500788{
Jamie Madill9fc36822015-11-18 13:08:07 -0500789 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400790
Jamie Madill4e31ad52015-10-29 10:32:57 -0400791 std::stringstream preambleStream;
792
Jamie Madill9fc36822015-11-18 13:08:07 -0500793 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
794
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400795 preambleStream << "struct GS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500796 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400797 preambleStream << "\n"
798 << "struct GS_OUTPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500799 generateVaryingLinkHLSL(SHADER_GEOMETRY, varyingPacking, preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400800 preambleStream
801 << "\n"
802 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
803 << "{\n"
804 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400805
Jamie Madill9fc36822015-11-18 13:08:07 -0500806 if (builtins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400807 {
808 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
809 }
810
Jamie Madill9fc36822015-11-18 13:08:07 -0500811 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400812 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500813 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill55c25d02015-11-18 13:08:08 -0500814 if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400815 {
816 preambleStream << "flat";
817 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500818 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400819 }
820
Jamie Madill9fc36822015-11-18 13:08:07 -0500821 if (builtins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400822 {
823 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
824 }
825
826 // Only write the dx_Position if we aren't using point sprites
827 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
828 << " output.dx_Position = input.dx_Position;\n"
829 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
830 << "}\n";
831
832 return preambleStream.str();
833}
834
835std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
836 const gl::Data &data,
837 const gl::Program::Data &programData,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800838 const bool useViewScale,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400839 const std::string &preambleString) const
840{
841 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500842
Jamie Madill334d6152015-10-22 14:00:28 -0400843 std::stringstream shaderStream;
844
Jamie Madill4e31ad52015-10-29 10:32:57 -0400845 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
846 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500847
Jamie Madill76f8fa62015-10-29 10:32:56 -0400848 const char *inputPT = nullptr;
849 const char *outputPT = nullptr;
850 int inputSize = 0;
851 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500852
Jamie Madill76f8fa62015-10-29 10:32:56 -0400853 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500854 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400855 case PRIMITIVE_POINTS:
856 inputPT = "point";
857 outputPT = "Triangle";
858 inputSize = 1;
859 maxVertexOutput = 4;
860 break;
861
862 case PRIMITIVE_LINES:
863 case PRIMITIVE_LINE_STRIP:
864 case PRIMITIVE_LINE_LOOP:
865 inputPT = "line";
866 outputPT = "Line";
867 inputSize = 2;
868 maxVertexOutput = 2;
869 break;
870
871 case PRIMITIVE_TRIANGLES:
872 case PRIMITIVE_TRIANGLE_STRIP:
873 case PRIMITIVE_TRIANGLE_FAN:
874 inputPT = "triangle";
875 outputPT = "Triangle";
876 inputSize = 3;
877 maxVertexOutput = 3;
878 break;
879
880 default:
881 UNREACHABLE();
882 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500883 }
884
Jamie Madill76f8fa62015-10-29 10:32:56 -0400885 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500886 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400887 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
888 "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800889 "uniform float4 dx_ViewCoords : register(c1);\n";
890
891 if (useViewScale)
892 {
893 shaderStream << "uniform float2 dx_ViewScale : register(c3);\n";
894 }
895
896 shaderStream << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400897 "static float2 pointSpriteCorners[] = \n"
898 "{\n"
899 " float2( 0.5f, -0.5f),\n"
900 " float2( 0.5f, 0.5f),\n"
901 " float2(-0.5f, -0.5f),\n"
902 " float2(-0.5f, 0.5f)\n"
903 "};\n"
904 "\n"
905 "static float2 pointSpriteTexcoords[] = \n"
906 "{\n"
907 " float2(1.0f, 1.0f),\n"
908 " float2(1.0f, 0.0f),\n"
909 " float2(0.0f, 1.0f),\n"
910 " float2(0.0f, 0.0f)\n"
911 "};\n"
912 "\n"
913 "static float minPointSize = "
914 << static_cast<int>(data.caps->minAliasedPointSize)
915 << ".0f;\n"
916 "static float maxPointSize = "
917 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n"
918 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500919 }
920
Jamie Madill4e31ad52015-10-29 10:32:57 -0400921 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400922 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400923 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
924
925 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
926 {
927 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
928 }
929
930 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400931 << "{\n"
932 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500933
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400934 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
935 {
936 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
937 }
938 else
939 {
940 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
941 }
942
Jamie Madill4e31ad52015-10-29 10:32:57 -0400943 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -0500944 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400945 shaderStream << " copyVertex(output, input[" << vertexIndex
946 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400947
948 if (!pointSprites)
949 {
950 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400951 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400952 }
953 }
954
955 if (pointSprites)
956 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400957 shaderStream << "\n"
958 " float4 dx_Position = input[0].dx_Position;\n"
959 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
960 "maxPointSize);\n"
961 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
962 "dx_ViewCoords.y) * dx_Position.w;\n";
963
964 for (int corner = 0; corner < 4; corner++)
965 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800966 if (useViewScale)
967 {
968 shaderStream << " \n"
969 " output.dx_Position = dx_Position + float4(1.0f, "
970 "-dx_ViewScale.y, 1.0f, 1.0f)"
971 " * float4(pointSpriteCorners["
972 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
973 }
974 else
975 {
976 shaderStream << "\n"
977 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
978 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
979 }
Jamie Madill76f8fa62015-10-29 10:32:56 -0400980
981 if (usesPointCoord)
982 {
983 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
984 << "];\n";
985 }
986
987 shaderStream << " outStream.Append(output);\n";
988 }
Jamie Madill5f562732014-02-14 16:41:24 -0500989 }
990
Jamie Madill334d6152015-10-22 14:00:28 -0400991 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400992 " outStream.RestartStrip();\n"
993 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500994
Jamie Madill334d6152015-10-22 14:00:28 -0400995 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500996}
997
998// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -0400999std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001000{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001001 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001002 {
1003 return "_" + name;
1004 }
1005
1006 return name;
1007}
1008
Jamie Madill334d6152015-10-22 14:00:28 -04001009std::string DynamicHLSL::generateAttributeConversionHLSL(
1010 gl::VertexFormatType vertexFormatType,
1011 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001012{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001013 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -04001014 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001015
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001016 // Matrix
1017 if (IsMatrixType(shaderAttrib.type))
1018 {
Jamie Madill8664b062014-02-14 16:41:29 -05001019 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001020 }
1021
Jamie Madillf2575982014-06-25 16:04:54 -04001022 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001023 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001024
Jamie Madill8664b062014-02-14 16:41:29 -05001025 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -04001026 bool requiresTypeConversion =
1027 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001028
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001029 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001030 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001031 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001032 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001033 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001034 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001035
1036 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001037 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001038}
Jamie Madille39a3f02015-11-17 20:42:15 -05001039
1040void DynamicHLSL::getPixelShaderOutputKey(const gl::Data &data,
1041 const gl::Program::Data &programData,
1042 const ProgramD3DMetadata &metadata,
1043 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1044{
1045 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1046 // - with a 3.0 context, the output color is copied to channel 0
1047 // - with a 2.0 context, the output color is broadcast to all channels
1048 bool broadcast = metadata.usesBroadcast(data);
1049 const unsigned int numRenderTargets =
1050 (broadcast || metadata.usesMultipleFragmentOuts() ? data.caps->maxDrawBuffers : 1);
1051
1052 if (metadata.getMajorShaderVersion() < 300)
1053 {
1054 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1055 renderTargetIndex++)
1056 {
1057 PixelShaderOutputVariable outputKeyVariable;
1058 outputKeyVariable.type = GL_FLOAT_VEC4;
1059 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1060 outputKeyVariable.source =
1061 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1062 outputKeyVariable.outputIndex = renderTargetIndex;
1063
1064 outPixelShaderKey->push_back(outputKeyVariable);
1065 }
1066 }
1067 else
1068 {
1069 const auto &shaderOutputVars =
1070 metadata.getFragmentShader()->getData().getActiveOutputVariables();
1071
1072 for (auto outputPair : programData.getOutputVariables())
1073 {
1074 const VariableLocation &outputLocation = outputPair.second;
1075 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1076 const std::string &variableName = "out_" + outputLocation.name;
1077 const std::string &elementString =
1078 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
1079
1080 ASSERT(outputVariable.staticUse);
1081
1082 PixelShaderOutputVariable outputKeyVariable;
1083 outputKeyVariable.type = outputVariable.type;
1084 outputKeyVariable.name = variableName + elementString;
1085 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
1086 outputKeyVariable.outputIndex = outputPair.first;
1087
1088 outPixelShaderKey->push_back(outputKeyVariable);
1089 }
1090 }
1091}
1092
Jamie Madill334d6152015-10-22 14:00:28 -04001093} // namespace rx