blob: 1cb2e335edc5d006cdd7d210fe00ba6e8bd3ccc1 [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
Corentin Wallezb076add2016-01-11 16:45:46 -0500234 if (shaderAttribute.name == "gl_InstanceID" ||
235 shaderAttribute.name == "gl_VertexID")
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000236 {
Corentin Wallezb076add2016-01-11 16:45:46 -0500237 // The input types of the instance ID and vertex ID in HLSL (uint) differs from
238 // the ones in ESSL (int).
Jamie Madill334d6152015-10-22 14:00:28 -0400239 structStream << " uint";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000240 }
241 else
242 {
Jamie Madill334d6152015-10-22 14:00:28 -0400243 structStream << " " << HLSLComponentTypeString(
244 componentType,
245 VariableComponentCount(shaderAttribute.type));
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000246 }
Jamie Madill8664b062014-02-14 16:41:29 -0500247 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500248
Jamie Madill334d6152015-10-22 14:00:28 -0400249 structStream << " " << decorateVariable(shaderAttribute.name) << " : ";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000250
251 if (shaderAttribute.name == "gl_InstanceID")
252 {
Jamie Madill334d6152015-10-22 14:00:28 -0400253 structStream << "SV_InstanceID";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000254 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500255 else if (shaderAttribute.name == "gl_VertexID")
256 {
257 structStream << "SV_VertexID";
258 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000259 else
260 {
Jamie Madill334d6152015-10-22 14:00:28 -0400261 structStream << "TEXCOORD" << semanticIndex;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000262 semanticIndex += VariableRegisterCount(shaderAttribute.type);
263 }
264
Jamie Madill334d6152015-10-22 14:00:28 -0400265 structStream << ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500266
Jamie Madill3b7e2052014-03-17 09:47:43 -0400267 // HLSL code for initialization
Jamie Madill334d6152015-10-22 14:00:28 -0400268 initStream << " " << decorateVariable(shaderAttribute.name) << " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500269
270 // Mismatched vertex attribute to vertex input may result in an undefined
271 // data reinterpretation (eg for pure integer->float, float->pure integer)
272 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400273 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400274 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500275 {
Jamie Madill334d6152015-10-22 14:00:28 -0400276 initStream << generateAttributeConversionHLSL(vertexFormatType, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500277 }
278 else
279 {
Jamie Madill334d6152015-10-22 14:00:28 -0400280 initStream << "input." << decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500281 }
282
Jamie Madill334d6152015-10-22 14:00:28 -0400283 initStream << ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400284
Jamie Madillac0a2672014-04-11 13:33:56 -0400285 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
286 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500287 }
288
Jamie Madill334d6152015-10-22 14:00:28 -0400289 structStream << "};\n"
290 "\n"
291 "void initAttributes(VS_INPUT input)\n"
292 "{\n"
293 << initStream.str() << "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400294
295 std::string vertexHLSL(sourceShader);
296
297 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
Jamie Madill334d6152015-10-22 14:00:28 -0400298 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), structStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400299
300 return vertexHLSL;
301}
302
Jamie Madill334d6152015-10-22 14:00:28 -0400303std::string DynamicHLSL::generatePixelShaderForOutputSignature(
304 const std::string &sourceShader,
305 const std::vector<PixelShaderOutputVariable> &outputVariables,
306 bool usesFragDepth,
307 const std::vector<GLenum> &outputLayout) const
Geoff Lang04fb89a2014-06-09 15:05:36 -0400308{
Jamie Madill334d6152015-10-22 14:00:28 -0400309 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang04fb89a2014-06-09 15:05:36 -0400310 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
Jamie Madill334d6152015-10-22 14:00:28 -0400311 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400312
Jamie Madill334d6152015-10-22 14:00:28 -0400313 std::stringstream declarationStream;
314 std::stringstream copyStream;
315
316 declarationStream << "struct PS_OUTPUT\n"
317 "{\n";
Geoff Lang4ace4232014-06-18 19:12:48 -0400318
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400319 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
320 {
321 GLenum binding = outputLayout[layoutIndex];
322
323 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400324 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400325 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
326
Jamie Madill334d6152015-10-22 14:00:28 -0400327 const PixelShaderOutputVariable *outputVariable =
328 FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400329
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000330 // OpenGL ES 3.0 spec $4.2.1
Jamie Madill334d6152015-10-22 14:00:28 -0400331 // If [...] not all user-defined output variables are written, the values of fragment
332 // colors
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000333 // corresponding to unwritten variables are similarly undefined.
334 if (outputVariable)
335 {
Jamie Madill334d6152015-10-22 14:00:28 -0400336 declarationStream << " " + HLSLTypeString(outputVariable->type) << " "
337 << outputVariable->name << " : " << targetSemantic
338 << static_cast<int>(layoutIndex) << ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400339
Jamie Madill334d6152015-10-22 14:00:28 -0400340 copyStream << " output." << outputVariable->name << " = "
341 << outputVariable->source << ";\n";
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000342 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400343 }
344 }
345
346 if (usesFragDepth)
347 {
Jamie Madill334d6152015-10-22 14:00:28 -0400348 declarationStream << " float gl_Depth : " << depthSemantic << ";\n";
349 copyStream << " output.gl_Depth = gl_Depth; \n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400350 }
351
Jamie Madill334d6152015-10-22 14:00:28 -0400352 declarationStream << "};\n"
353 "\n"
354 "PS_OUTPUT generateOutput()\n"
355 "{\n"
356 " PS_OUTPUT output;\n"
357 << copyStream.str() << " return output;\n"
358 "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400359
360 std::string pixelHLSL(sourceShader);
361
362 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
Jamie Madill334d6152015-10-22 14:00:28 -0400363 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(),
364 declarationStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400365
366 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500367}
368
Jamie Madill9fc36822015-11-18 13:08:07 -0500369void DynamicHLSL::generateVaryingLinkHLSL(ShaderType shaderType,
370 const VaryingPacking &varyingPacking,
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400371 std::stringstream &linkStream) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400372{
Jamie Madill9fc36822015-11-18 13:08:07 -0500373 const auto &builtins = varyingPacking.builtins(shaderType);
374 ASSERT(builtins.dxPosition.enabled);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400375 linkStream << "{\n"
Jamie Madill9fc36822015-11-18 13:08:07 -0500376 << " float4 dx_Position : " << builtins.dxPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700377
Jamie Madill9fc36822015-11-18 13:08:07 -0500378 if (builtins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700379 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500380 linkStream << " float4 gl_Position : " << builtins.glPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700381 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400382
Jamie Madill9fc36822015-11-18 13:08:07 -0500383 if (builtins.glFragCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400384 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500385 linkStream << " float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400386 }
387
Jamie Madill9fc36822015-11-18 13:08:07 -0500388 if (builtins.glPointCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400389 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500390 linkStream << " float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400391 }
392
Jamie Madill9fc36822015-11-18 13:08:07 -0500393 if (builtins.glPointSize.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400394 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500395 linkStream << " float gl_PointSize : " << builtins.glPointSize.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400396 }
397
Corentin Wallez50fcf742016-04-11 13:57:32 -0700398 // Do this after gl_PointSize, to potentially combine gl_PointCoord and gl_PointSize into the
Jamie Madill334d6152015-10-22 14:00:28 -0400399 // same register.
Jamie Madill9fc36822015-11-18 13:08:07 -0500400 generateVaryingHLSL(varyingPacking, linkStream);
Austin Kinross8b695ee2015-03-12 13:12:20 -0700401
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400402 linkStream << "};\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400403}
404
Jamie Madill9082b982016-04-27 15:21:51 -0400405bool DynamicHLSL::generateShaderLinkHLSL(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400406 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500407 const ProgramD3DMetadata &programMetadata,
Jamie Madill9fc36822015-11-18 13:08:07 -0500408 const VaryingPacking &varyingPacking,
Jamie Madill334d6152015-10-22 14:00:28 -0400409 std::string *pixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -0500410 std::string *vertexHLSL) const
Jamie Madill5f562732014-02-14 16:41:24 -0500411{
Jamie Madill334d6152015-10-22 14:00:28 -0400412 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500413
Jamie Madill91445bc2015-09-23 16:47:53 -0400414 const gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
Jamie Madill91445bc2015-09-23 16:47:53 -0400415 const gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
416 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700417 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400418
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800419 // usesViewScale() isn't supported in the D3D9 renderer
420 ASSERT(shaderModel >= 4 || !programMetadata.usesViewScale());
421
Jamie Madill334d6152015-10-22 14:00:28 -0400422 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500423 programMetadata.usesPointSize() &&
424 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400425
Jamie Madill14e95b32015-05-07 10:10:41 -0400426 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400427 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500428
Jamie Madill334d6152015-10-22 14:00:28 -0400429 std::stringstream vertexStream;
430 vertexStream << vertexShaderGL->getTranslatedSource();
431
Cooper Partine6664f02015-01-09 16:22:24 -0800432 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400433 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800434 if (useInstancedPointSpriteEmulation)
435 {
Jamie Madill334d6152015-10-22 14:00:28 -0400436 vertexStream << "static float minPointSize = "
437 << static_cast<int>(data.caps->minAliasedPointSize) << ".0f;\n"
438 << "static float maxPointSize = "
439 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800440 }
441
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500442 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill9fc36822015-11-18 13:08:07 -0500443 vertexStream << "\n" << VERTEX_ATTRIBUTE_STUB_STRING + "\n";
444
445 // Write the HLSL input/output declarations
446 vertexStream << "struct VS_OUTPUT\n";
447 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, vertexStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400448 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400449 << "VS_OUTPUT main(VS_INPUT input)\n"
450 << "{\n"
451 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500452
Jamie Madill334d6152015-10-22 14:00:28 -0400453 vertexStream << "\n"
454 << " gl_main();\n"
455 << "\n"
456 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700457
Jamie Madill9fc36822015-11-18 13:08:07 -0500458 const auto &vertexBuiltins = varyingPacking.builtins(SHADER_VERTEX);
459
460 if (vertexBuiltins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700461 {
Jamie Madill334d6152015-10-22 14:00:28 -0400462 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700463 }
Jamie Madill37997142015-01-28 10:06:34 -0500464
Austin Kinross4fd18b12014-12-22 12:32:05 -0800465 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400466 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500467 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800468 vertexStream << " output.dx_Position.x = gl_Position.x;\n";
469
470 if (programMetadata.usesViewScale())
471 {
472 // This code assumes that dx_ViewScale.y = -1.0f when rendering to texture, and +1.0f
473 // when rendering to the default framebuffer. No other values are valid.
474 vertexStream << " output.dx_Position.y = dx_ViewScale.y * gl_Position.y;\n";
475 }
476 else
477 {
478 vertexStream << " output.dx_Position.y = - gl_Position.y;\n";
479 }
480
481 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400482 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500483 }
484 else
485 {
Jamie Madill334d6152015-10-22 14:00:28 -0400486 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800487 "dx_ViewAdjust.x * gl_Position.w;\n";
488
489 // If usesViewScale() is true and we're using the D3D11 renderer via Feature Level 9_*,
490 // then we need to multiply the gl_Position.y by the viewScale.
491 // usesViewScale() isn't supported when using the D3D9 renderer.
492 if (programMetadata.usesViewScale() &&
493 (shaderModel >= 4 && mRenderer->getShaderModelSuffix() != ""))
494 {
495 vertexStream << " output.dx_Position.y = dx_ViewScale.y * (gl_Position.y * "
496 "dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n";
497 }
498 else
499 {
500 vertexStream << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
501 "dx_ViewAdjust.y * gl_Position.w);\n";
502 }
503
504 vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400505 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500506 }
507
Austin Kinross8b695ee2015-03-12 13:12:20 -0700508 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
Jamie Madill9fc36822015-11-18 13:08:07 -0500509 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500510 {
Jamie Madill334d6152015-10-22 14:00:28 -0400511 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500512 }
513
Jamie Madill9fc36822015-11-18 13:08:07 -0500514 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500515 {
Jamie Madill334d6152015-10-22 14:00:28 -0400516 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500517 }
518
Jamie Madill9fc36822015-11-18 13:08:07 -0500519 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500520 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500521 const auto &packedVarying = *registerInfo.packedVarying;
522 const auto &varying = *packedVarying.varying;
523 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400524
Jamie Madill55c25d02015-11-18 13:08:08 -0500525 vertexStream << " output.v" << registerInfo.semanticIndex << " = ";
526
527 if (packedVarying.isStructField())
528 {
529 vertexStream << decorateVariable(packedVarying.parentStructName) << ".";
530 }
531
532 vertexStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400533
534 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400535 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500536 WriteArrayString(vertexStream, registerInfo.varyingArrayIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500537 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400538
Jamie Madill55c25d02015-11-18 13:08:08 -0500539 if (VariableRowCount(varying.type) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400540 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500541 WriteArrayString(vertexStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400542 }
543
544 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500545 }
546
Cooper Partine6664f02015-01-09 16:22:24 -0800547 // Instanced PointSprite emulation requires additional entries to calculate
548 // the final output vertex positions of the quad that represents each sprite.
549 if (useInstancedPointSpriteEmulation)
550 {
Jamie Madill334d6152015-10-22 14:00:28 -0400551 vertexStream << "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800552 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n";
553
554 vertexStream << " output.dx_Position.x += (input.spriteVertexPos.x * gl_PointSize / "
555 "(dx_ViewCoords.x*2)) * output.dx_Position.w;";
556
557 if (programMetadata.usesViewScale())
558 {
559 // Multiply by ViewScale to invert the rendering when appropriate
560 vertexStream << " output.dx_Position.y += (-dx_ViewScale.y * "
561 "input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2)) * "
562 "output.dx_Position.w;";
563 }
564 else
565 {
566 vertexStream << " output.dx_Position.y += (input.spriteVertexPos.y * gl_PointSize / "
567 "(dx_ViewCoords.y*2)) * output.dx_Position.w;";
568 }
569
570 vertexStream
571 << " output.dx_Position.z += input.spriteVertexPos.z * output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800572
Jamie Madille39a3f02015-11-17 20:42:15 -0500573 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800574 {
Jamie Madill334d6152015-10-22 14:00:28 -0400575 vertexStream << "\n"
576 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800577 }
578 }
579
Cooper Partin7c89d242015-10-13 12:45:59 -0700580 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
581 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
582 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500583 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700584 {
585 ASSERT(!useInstancedPointSpriteEmulation);
586 vertexStream << "\n"
587 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
588 }
589
Jamie Madill334d6152015-10-22 14:00:28 -0400590 vertexStream << "\n"
591 << " return output;\n"
592 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500593
Jamie Madill334d6152015-10-22 14:00:28 -0400594 std::stringstream pixelStream;
595 pixelStream << fragmentShaderGL->getTranslatedSource();
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400596 pixelStream << "struct PS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500597 generateVaryingLinkHLSL(SHADER_PIXEL, varyingPacking, pixelStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400598 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500599
Jamie Madill334d6152015-10-22 14:00:28 -0400600 pixelStream << PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500601
Jamie Madill334d6152015-10-22 14:00:28 -0400602 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500603 {
604 if (shaderModel >= 4)
605 {
Jamie Madill334d6152015-10-22 14:00:28 -0400606 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
607 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500608 }
609 else
610 {
Jamie Madill334d6152015-10-22 14:00:28 -0400611 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
612 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500613 }
614 }
615 else
616 {
Jamie Madill334d6152015-10-22 14:00:28 -0400617 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
618 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500619 }
620
Jamie Madill9fc36822015-11-18 13:08:07 -0500621 const auto &pixelBuiltins = varyingPacking.builtins(SHADER_PIXEL);
622
623 if (pixelBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500624 {
Jamie Madill334d6152015-10-22 14:00:28 -0400625 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500626
Austin Kinross588434c2014-12-10 10:41:45 -0800627 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400628 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
629 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800630 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500631 {
Jamie Madill334d6152015-10-22 14:00:28 -0400632 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
633 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500634 }
Austin Kinross588434c2014-12-10 10:41:45 -0800635 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500636 {
Jamie Madill334d6152015-10-22 14:00:28 -0400637 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
638 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500639 }
640 else
641 {
Jamie Madill334d6152015-10-22 14:00:28 -0400642 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
643 // Renderer::setViewport()
644 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
645 "dx_ViewCoords.z;\n"
646 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
647 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500648 }
649
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800650 if (programMetadata.usesViewScale())
651 {
652 // For Feature Level 9_3 and below, we need to correct gl_FragCoord.y to account
653 // for dx_ViewScale. On Feature Level 10_0+, gl_FragCoord.y is calculated above using
654 // dx_ViewCoords and is always correct irrespective of dx_ViewScale's value.
655 // NOTE: usesViewScale() can only be true on D3D11 (i.e. Shader Model 4.0+).
656 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
657 {
658 // Some assumptions:
659 // - dx_ViewScale.y = -1.0f when rendering to texture
660 // - dx_ViewScale.y = +1.0f when rendering to the default framebuffer
661 // - gl_FragCoord.y has been set correctly above.
662 //
663 // When rendering to the backbuffer, the code inverts gl_FragCoord's y coordinate.
664 // This involves subtracting the y coordinate from the height of the area being
665 // rendered to.
666 //
667 // First we calculate the height of the area being rendered to:
668 // render_area_height = (2.0f / (1.0f - input.gl_FragCoord.y * rhw)) *
669 // gl_FragCoord.y
670 //
671 // Note that when we're rendering to default FB, we want our output to be
672 // equivalent to:
673 // "gl_FragCoord.y = render_area_height - gl_FragCoord.y"
674 //
675 // When we're rendering to a texture, we want our output to be equivalent to:
676 // "gl_FragCoord.y = gl_FragCoord.y;"
677 //
678 // If we set scale_factor = ((1.0f + dx_ViewScale.y) / 2.0f), then notice that
679 // - When rendering to default FB: scale_factor = 1.0f
680 // - When rendering to texture: scale_factor = 0.0f
681 //
682 // Therefore, we can get our desired output by setting:
683 // "gl_FragCoord.y = scale_factor * render_area_height - dx_ViewScale.y *
684 // gl_FragCoord.y"
685 //
686 // Simplifying, this becomes:
687 pixelStream
688 << " gl_FragCoord.y = (1.0f + dx_ViewScale.y) * gl_FragCoord.y /"
689 "(1.0f - input.gl_FragCoord.y * rhw) - dx_ViewScale.y * gl_FragCoord.y;\n";
690 }
691 }
692
Jamie Madill334d6152015-10-22 14:00:28 -0400693 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
694 "dx_DepthFront.y;\n"
695 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500696 }
697
Jamie Madill9fc36822015-11-18 13:08:07 -0500698 if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500699 {
Jamie Madill334d6152015-10-22 14:00:28 -0400700 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
701 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500702 }
703
Jamie Madill334d6152015-10-22 14:00:28 -0400704 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500705 {
706 if (shaderModel <= 3)
707 {
Jamie Madill334d6152015-10-22 14:00:28 -0400708 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500709 }
710 else
711 {
Jamie Madill334d6152015-10-22 14:00:28 -0400712 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500713 }
714 }
715
Jamie Madill9fc36822015-11-18 13:08:07 -0500716 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500717 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500718 const auto &packedVarying = *registerInfo.packedVarying;
719 const auto &varying = *packedVarying.varying;
720 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400721
Jamie Madillca03b352015-09-02 12:38:13 -0400722 // Don't reference VS-only transform feedback varyings in the PS.
Jamie Madill9fc36822015-11-18 13:08:07 -0500723 if (registerInfo.packedVarying->vertexOnly)
Jamie Madillca03b352015-09-02 12:38:13 -0400724 continue;
725
Jamie Madill55c25d02015-11-18 13:08:08 -0500726 pixelStream << " ";
727
728 if (packedVarying.isStructField())
729 {
730 pixelStream << decorateVariable(packedVarying.parentStructName) << ".";
731 }
732
733 pixelStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400734
735 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400736 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500737 WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400738 }
739
Jamie Madill55c25d02015-11-18 13:08:08 -0500740 GLenum transposedType = TransposeMatrixType(varying.type);
741 if (VariableRowCount(transposedType) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400742 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500743 WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400744 }
745
Jamie Madill9fc36822015-11-18 13:08:07 -0500746 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400747
Jamie Madill55c25d02015-11-18 13:08:08 -0500748 switch (VariableColumnCount(transposedType))
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400749 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500750 case 1:
751 pixelStream << ".x";
752 break;
753 case 2:
754 pixelStream << ".xy";
755 break;
756 case 3:
757 pixelStream << ".xyz";
758 break;
759 case 4:
760 break;
761 default:
762 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500763 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400764 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500765 }
766
Jamie Madill334d6152015-10-22 14:00:28 -0400767 pixelStream << "\n"
768 << " gl_main();\n"
769 << "\n"
770 << " return generateOutput();\n"
771 << "}\n";
772
773 *vertexHLSL = vertexStream.str();
774 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500775
776 return true;
777}
778
Jamie Madill9fc36822015-11-18 13:08:07 -0500779std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking) const
Jamie Madill5f562732014-02-14 16:41:24 -0500780{
Jamie Madill9fc36822015-11-18 13:08:07 -0500781 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400782
Jamie Madill4e31ad52015-10-29 10:32:57 -0400783 std::stringstream preambleStream;
784
Jamie Madill9fc36822015-11-18 13:08:07 -0500785 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
786
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400787 preambleStream << "struct GS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500788 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400789 preambleStream << "\n"
790 << "struct GS_OUTPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500791 generateVaryingLinkHLSL(SHADER_GEOMETRY, varyingPacking, preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400792 preambleStream
793 << "\n"
794 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
795 << "{\n"
796 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400797
Jamie Madill9fc36822015-11-18 13:08:07 -0500798 if (builtins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400799 {
800 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
801 }
802
Jamie Madill9fc36822015-11-18 13:08:07 -0500803 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400804 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500805 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill55c25d02015-11-18 13:08:08 -0500806 if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400807 {
808 preambleStream << "flat";
809 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500810 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400811 }
812
Jamie Madill9fc36822015-11-18 13:08:07 -0500813 if (builtins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400814 {
815 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
816 }
817
818 // Only write the dx_Position if we aren't using point sprites
819 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
820 << " output.dx_Position = input.dx_Position;\n"
821 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
822 << "}\n";
823
824 return preambleStream.str();
825}
826
827std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
Jamie Madill9082b982016-04-27 15:21:51 -0400828 const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -0400829 const gl::ProgramState &programData,
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800830 const bool useViewScale,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400831 const std::string &preambleString) const
832{
833 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500834
Jamie Madill334d6152015-10-22 14:00:28 -0400835 std::stringstream shaderStream;
836
Jamie Madill4e31ad52015-10-29 10:32:57 -0400837 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
838 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500839
Jamie Madill76f8fa62015-10-29 10:32:56 -0400840 const char *inputPT = nullptr;
841 const char *outputPT = nullptr;
842 int inputSize = 0;
843 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500844
Jamie Madill76f8fa62015-10-29 10:32:56 -0400845 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500846 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400847 case PRIMITIVE_POINTS:
848 inputPT = "point";
849 outputPT = "Triangle";
850 inputSize = 1;
851 maxVertexOutput = 4;
852 break;
853
854 case PRIMITIVE_LINES:
855 case PRIMITIVE_LINE_STRIP:
856 case PRIMITIVE_LINE_LOOP:
857 inputPT = "line";
858 outputPT = "Line";
859 inputSize = 2;
860 maxVertexOutput = 2;
861 break;
862
863 case PRIMITIVE_TRIANGLES:
864 case PRIMITIVE_TRIANGLE_STRIP:
865 case PRIMITIVE_TRIANGLE_FAN:
866 inputPT = "triangle";
867 outputPT = "Triangle";
868 inputSize = 3;
869 maxVertexOutput = 3;
870 break;
871
872 default:
873 UNREACHABLE();
874 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500875 }
876
Jamie Madill76f8fa62015-10-29 10:32:56 -0400877 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500878 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400879 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
880 "\n"
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800881 "uniform float4 dx_ViewCoords : register(c1);\n";
882
883 if (useViewScale)
884 {
885 shaderStream << "uniform float2 dx_ViewScale : register(c3);\n";
886 }
887
888 shaderStream << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400889 "static float2 pointSpriteCorners[] = \n"
890 "{\n"
891 " float2( 0.5f, -0.5f),\n"
892 " float2( 0.5f, 0.5f),\n"
893 " float2(-0.5f, -0.5f),\n"
894 " float2(-0.5f, 0.5f)\n"
895 "};\n"
896 "\n"
897 "static float2 pointSpriteTexcoords[] = \n"
898 "{\n"
899 " float2(1.0f, 1.0f),\n"
900 " float2(1.0f, 0.0f),\n"
901 " float2(0.0f, 1.0f),\n"
902 " float2(0.0f, 0.0f)\n"
903 "};\n"
904 "\n"
905 "static float minPointSize = "
906 << static_cast<int>(data.caps->minAliasedPointSize)
907 << ".0f;\n"
908 "static float maxPointSize = "
909 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n"
910 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500911 }
912
Jamie Madill4e31ad52015-10-29 10:32:57 -0400913 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400914 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400915 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
916
917 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
918 {
919 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
920 }
921
922 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400923 << "{\n"
924 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500925
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400926 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
927 {
928 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
929 }
930 else
931 {
932 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
933 }
934
Jamie Madill4e31ad52015-10-29 10:32:57 -0400935 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -0500936 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400937 shaderStream << " copyVertex(output, input[" << vertexIndex
938 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400939
940 if (!pointSprites)
941 {
942 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400943 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400944 }
945 }
946
947 if (pointSprites)
948 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400949 shaderStream << "\n"
950 " float4 dx_Position = input[0].dx_Position;\n"
951 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
952 "maxPointSize);\n"
953 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
954 "dx_ViewCoords.y) * dx_Position.w;\n";
955
956 for (int corner = 0; corner < 4; corner++)
957 {
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800958 if (useViewScale)
959 {
960 shaderStream << " \n"
961 " output.dx_Position = dx_Position + float4(1.0f, "
962 "-dx_ViewScale.y, 1.0f, 1.0f)"
963 " * float4(pointSpriteCorners["
964 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
965 }
966 else
967 {
968 shaderStream << "\n"
969 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
970 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
971 }
Jamie Madill76f8fa62015-10-29 10:32:56 -0400972
973 if (usesPointCoord)
974 {
975 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
976 << "];\n";
977 }
978
979 shaderStream << " outStream.Append(output);\n";
980 }
Jamie Madill5f562732014-02-14 16:41:24 -0500981 }
982
Jamie Madill334d6152015-10-22 14:00:28 -0400983 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400984 " outStream.RestartStrip();\n"
985 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500986
Jamie Madill334d6152015-10-22 14:00:28 -0400987 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500988}
989
990// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -0400991std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -0500992{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400993 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -0500994 {
995 return "_" + name;
996 }
997
998 return name;
999}
1000
Jamie Madill334d6152015-10-22 14:00:28 -04001001std::string DynamicHLSL::generateAttributeConversionHLSL(
1002 gl::VertexFormatType vertexFormatType,
1003 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001004{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001005 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -04001006 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001007
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001008 // Matrix
1009 if (IsMatrixType(shaderAttrib.type))
1010 {
Jamie Madill8664b062014-02-14 16:41:29 -05001011 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001012 }
1013
Jamie Madillf2575982014-06-25 16:04:54 -04001014 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001015 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001016
Jamie Madill8664b062014-02-14 16:41:29 -05001017 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -04001018 bool requiresTypeConversion =
1019 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001020
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001021 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001022 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001023 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001024 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001025 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001026 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001027
1028 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001029 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001030}
Jamie Madille39a3f02015-11-17 20:42:15 -05001031
Jamie Madill9082b982016-04-27 15:21:51 -04001032void DynamicHLSL::getPixelShaderOutputKey(const gl::ContextState &data,
Jamie Madill48ef11b2016-04-27 15:21:52 -04001033 const gl::ProgramState &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -05001034 const ProgramD3DMetadata &metadata,
1035 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1036{
1037 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1038 // - with a 3.0 context, the output color is copied to channel 0
1039 // - with a 2.0 context, the output color is broadcast to all channels
1040 bool broadcast = metadata.usesBroadcast(data);
1041 const unsigned int numRenderTargets =
1042 (broadcast || metadata.usesMultipleFragmentOuts() ? data.caps->maxDrawBuffers : 1);
1043
1044 if (metadata.getMajorShaderVersion() < 300)
1045 {
1046 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1047 renderTargetIndex++)
1048 {
1049 PixelShaderOutputVariable outputKeyVariable;
1050 outputKeyVariable.type = GL_FLOAT_VEC4;
1051 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1052 outputKeyVariable.source =
1053 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1054 outputKeyVariable.outputIndex = renderTargetIndex;
1055
1056 outPixelShaderKey->push_back(outputKeyVariable);
1057 }
1058 }
1059 else
1060 {
1061 const auto &shaderOutputVars =
1062 metadata.getFragmentShader()->getData().getActiveOutputVariables();
1063
1064 for (auto outputPair : programData.getOutputVariables())
1065 {
1066 const VariableLocation &outputLocation = outputPair.second;
1067 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1068 const std::string &variableName = "out_" + outputLocation.name;
1069 const std::string &elementString =
1070 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
1071
1072 ASSERT(outputVariable.staticUse);
1073
1074 PixelShaderOutputVariable outputKeyVariable;
1075 outputKeyVariable.type = outputVariable.type;
1076 outputKeyVariable.name = variableName + elementString;
1077 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
1078 outputKeyVariable.outputIndex = outputPair.first;
1079
1080 outPixelShaderKey->push_back(outputKeyVariable);
1081 }
1082 }
1083}
1084
Jamie Madill334d6152015-10-22 14:00:28 -04001085} // namespace rx