blob: 1d07b95c3334105bce66713b10cebbe4aa85fb6b [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
Jamie Madill334d6152015-10-22 14:00:28 -0400415 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500416 programMetadata.usesPointSize() &&
417 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400418
Jamie Madill14e95b32015-05-07 10:10:41 -0400419 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400420 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500421
Jamie Madill334d6152015-10-22 14:00:28 -0400422 std::stringstream vertexStream;
423 vertexStream << vertexShaderGL->getTranslatedSource();
424
Cooper Partine6664f02015-01-09 16:22:24 -0800425 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400426 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800427 if (useInstancedPointSpriteEmulation)
428 {
Jamie Madill334d6152015-10-22 14:00:28 -0400429 vertexStream << "static float minPointSize = "
430 << static_cast<int>(data.caps->minAliasedPointSize) << ".0f;\n"
431 << "static float maxPointSize = "
432 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800433 }
434
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500435 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill9fc36822015-11-18 13:08:07 -0500436 vertexStream << "\n" << VERTEX_ATTRIBUTE_STUB_STRING + "\n";
437
438 // Write the HLSL input/output declarations
439 vertexStream << "struct VS_OUTPUT\n";
440 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, vertexStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400441 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400442 << "VS_OUTPUT main(VS_INPUT input)\n"
443 << "{\n"
444 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500445
Jamie Madill37997142015-01-28 10:06:34 -0500446 if (vertexShader->usesDeferredInit())
447 {
Jamie Madill334d6152015-10-22 14:00:28 -0400448 vertexStream << "\n"
449 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500450 }
451
Jamie Madill334d6152015-10-22 14:00:28 -0400452 vertexStream << "\n"
453 << " gl_main();\n"
454 << "\n"
455 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700456
Jamie Madill9fc36822015-11-18 13:08:07 -0500457 const auto &vertexBuiltins = varyingPacking.builtins(SHADER_VERTEX);
458
459 if (vertexBuiltins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700460 {
Jamie Madill334d6152015-10-22 14:00:28 -0400461 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700462 }
Jamie Madill37997142015-01-28 10:06:34 -0500463
Austin Kinross4fd18b12014-12-22 12:32:05 -0800464 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400465 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500466 {
Jamie Madill334d6152015-10-22 14:00:28 -0400467 vertexStream << " output.dx_Position.x = gl_Position.x;\n"
468 << " output.dx_Position.y = -gl_Position.y;\n"
469 << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
470 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500471 }
472 else
473 {
Jamie Madill334d6152015-10-22 14:00:28 -0400474 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
475 "dx_ViewAdjust.x * gl_Position.w;\n"
476 << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
477 "dx_ViewAdjust.y * gl_Position.w);\n"
478 << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
479 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500480 }
481
Austin Kinross8b695ee2015-03-12 13:12:20 -0700482 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
Jamie Madill9fc36822015-11-18 13:08:07 -0500483 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500484 {
Jamie Madill334d6152015-10-22 14:00:28 -0400485 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500486 }
487
Jamie Madill9fc36822015-11-18 13:08:07 -0500488 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500489 {
Jamie Madill334d6152015-10-22 14:00:28 -0400490 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500491 }
492
Jamie Madill9fc36822015-11-18 13:08:07 -0500493 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500494 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500495 const auto &packedVarying = *registerInfo.packedVarying;
496 const auto &varying = *packedVarying.varying;
497 ASSERT(!varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400498
Jamie Madill55c25d02015-11-18 13:08:08 -0500499 vertexStream << " output.v" << registerInfo.semanticIndex << " = ";
500
501 if (packedVarying.isStructField())
502 {
503 vertexStream << decorateVariable(packedVarying.parentStructName) << ".";
504 }
505
506 vertexStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400507
508 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400509 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500510 WriteArrayString(vertexStream, registerInfo.varyingArrayIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500511 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400512
Jamie Madill55c25d02015-11-18 13:08:08 -0500513 if (VariableRowCount(varying.type) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400514 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500515 WriteArrayString(vertexStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400516 }
517
518 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500519 }
520
Cooper Partine6664f02015-01-09 16:22:24 -0800521 // Instanced PointSprite emulation requires additional entries to calculate
522 // the final output vertex positions of the quad that represents each sprite.
523 if (useInstancedPointSpriteEmulation)
524 {
Jamie Madill334d6152015-10-22 14:00:28 -0400525 vertexStream << "\n"
526 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"
527 << " output.dx_Position.xyz += float3(input.spriteVertexPos.x * "
528 "gl_PointSize / (dx_ViewCoords.x*2), input.spriteVertexPos.y * "
529 "gl_PointSize / (dx_ViewCoords.y*2), input.spriteVertexPos.z) * "
530 "output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800531
Jamie Madille39a3f02015-11-17 20:42:15 -0500532 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800533 {
Jamie Madill334d6152015-10-22 14:00:28 -0400534 vertexStream << "\n"
535 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800536 }
537 }
538
Cooper Partin7c89d242015-10-13 12:45:59 -0700539 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
540 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
541 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500542 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700543 {
544 ASSERT(!useInstancedPointSpriteEmulation);
545 vertexStream << "\n"
546 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
547 }
548
Jamie Madill334d6152015-10-22 14:00:28 -0400549 vertexStream << "\n"
550 << " return output;\n"
551 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500552
Jamie Madill334d6152015-10-22 14:00:28 -0400553 std::stringstream pixelStream;
554 pixelStream << fragmentShaderGL->getTranslatedSource();
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400555 pixelStream << "struct PS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500556 generateVaryingLinkHLSL(SHADER_PIXEL, varyingPacking, pixelStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400557 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500558
Jamie Madill334d6152015-10-22 14:00:28 -0400559 pixelStream << PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500560
Jamie Madill334d6152015-10-22 14:00:28 -0400561 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500562 {
563 if (shaderModel >= 4)
564 {
Jamie Madill334d6152015-10-22 14:00:28 -0400565 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
566 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500567 }
568 else
569 {
Jamie Madill334d6152015-10-22 14:00:28 -0400570 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
571 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500572 }
573 }
574 else
575 {
Jamie Madill334d6152015-10-22 14:00:28 -0400576 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
577 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500578 }
579
Jamie Madill9fc36822015-11-18 13:08:07 -0500580 const auto &pixelBuiltins = varyingPacking.builtins(SHADER_PIXEL);
581
582 if (pixelBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500583 {
Jamie Madill334d6152015-10-22 14:00:28 -0400584 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500585
Austin Kinross588434c2014-12-10 10:41:45 -0800586 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400587 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
588 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800589 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500590 {
Jamie Madill334d6152015-10-22 14:00:28 -0400591 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
592 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500593 }
Austin Kinross588434c2014-12-10 10:41:45 -0800594 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500595 {
Jamie Madill334d6152015-10-22 14:00:28 -0400596 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
597 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500598 }
599 else
600 {
Jamie Madill334d6152015-10-22 14:00:28 -0400601 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
602 // Renderer::setViewport()
603 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
604 "dx_ViewCoords.z;\n"
605 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
606 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500607 }
608
Jamie Madill334d6152015-10-22 14:00:28 -0400609 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
610 "dx_DepthFront.y;\n"
611 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500612 }
613
Jamie Madill9fc36822015-11-18 13:08:07 -0500614 if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500615 {
Jamie Madill334d6152015-10-22 14:00:28 -0400616 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
617 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500618 }
619
Jamie Madill334d6152015-10-22 14:00:28 -0400620 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500621 {
622 if (shaderModel <= 3)
623 {
Jamie Madill334d6152015-10-22 14:00:28 -0400624 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500625 }
626 else
627 {
Jamie Madill334d6152015-10-22 14:00:28 -0400628 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500629 }
630 }
631
Jamie Madill9fc36822015-11-18 13:08:07 -0500632 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500633 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500634 const auto &packedVarying = *registerInfo.packedVarying;
635 const auto &varying = *packedVarying.varying;
636 ASSERT(!varying.isBuiltIn() && !varying.isStruct());
Jamie Madill4cff2472015-08-21 16:53:18 -0400637
Jamie Madillca03b352015-09-02 12:38:13 -0400638 // Don't reference VS-only transform feedback varyings in the PS.
Jamie Madill9fc36822015-11-18 13:08:07 -0500639 if (registerInfo.packedVarying->vertexOnly)
Jamie Madillca03b352015-09-02 12:38:13 -0400640 continue;
641
Jamie Madill55c25d02015-11-18 13:08:08 -0500642 pixelStream << " ";
643
644 if (packedVarying.isStructField())
645 {
646 pixelStream << decorateVariable(packedVarying.parentStructName) << ".";
647 }
648
649 pixelStream << decorateVariable(varying.name);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400650
651 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400652 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500653 WriteArrayString(pixelStream, registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400654 }
655
Jamie Madill55c25d02015-11-18 13:08:08 -0500656 GLenum transposedType = TransposeMatrixType(varying.type);
657 if (VariableRowCount(transposedType) > 1)
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400658 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500659 WriteArrayString(pixelStream, registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400660 }
661
Jamie Madill9fc36822015-11-18 13:08:07 -0500662 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400663
Jamie Madill55c25d02015-11-18 13:08:08 -0500664 switch (VariableColumnCount(transposedType))
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400665 {
Jamie Madill55c25d02015-11-18 13:08:08 -0500666 case 1:
667 pixelStream << ".x";
668 break;
669 case 2:
670 pixelStream << ".xy";
671 break;
672 case 3:
673 pixelStream << ".xyz";
674 break;
675 case 4:
676 break;
677 default:
678 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500679 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400680 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500681 }
682
Jamie Madill37997142015-01-28 10:06:34 -0500683 if (fragmentShader->usesDeferredInit())
684 {
Jamie Madill334d6152015-10-22 14:00:28 -0400685 pixelStream << "\n"
686 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500687 }
688
Jamie Madill334d6152015-10-22 14:00:28 -0400689 pixelStream << "\n"
690 << " gl_main();\n"
691 << "\n"
692 << " return generateOutput();\n"
693 << "}\n";
694
695 *vertexHLSL = vertexStream.str();
696 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500697
698 return true;
699}
700
Jamie Madill9fc36822015-11-18 13:08:07 -0500701std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking) const
Jamie Madill5f562732014-02-14 16:41:24 -0500702{
Jamie Madill9fc36822015-11-18 13:08:07 -0500703 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400704
Jamie Madill4e31ad52015-10-29 10:32:57 -0400705 std::stringstream preambleStream;
706
Jamie Madill9fc36822015-11-18 13:08:07 -0500707 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
708
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400709 preambleStream << "struct GS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500710 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400711 preambleStream << "\n"
712 << "struct GS_OUTPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500713 generateVaryingLinkHLSL(SHADER_GEOMETRY, varyingPacking, preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400714 preambleStream
715 << "\n"
716 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
717 << "{\n"
718 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400719
Jamie Madill9fc36822015-11-18 13:08:07 -0500720 if (builtins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400721 {
722 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
723 }
724
Jamie Madill9fc36822015-11-18 13:08:07 -0500725 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400726 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500727 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill55c25d02015-11-18 13:08:08 -0500728 if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT)
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400729 {
730 preambleStream << "flat";
731 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500732 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400733 }
734
Jamie Madill9fc36822015-11-18 13:08:07 -0500735 if (builtins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400736 {
737 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
738 }
739
740 // Only write the dx_Position if we aren't using point sprites
741 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
742 << " output.dx_Position = input.dx_Position;\n"
743 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
744 << "}\n";
745
746 return preambleStream.str();
747}
748
749std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
750 const gl::Data &data,
751 const gl::Program::Data &programData,
752 const std::string &preambleString) const
753{
754 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500755
Jamie Madill334d6152015-10-22 14:00:28 -0400756 std::stringstream shaderStream;
757
Jamie Madill4e31ad52015-10-29 10:32:57 -0400758 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
759 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500760
Jamie Madill76f8fa62015-10-29 10:32:56 -0400761 const char *inputPT = nullptr;
762 const char *outputPT = nullptr;
763 int inputSize = 0;
764 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500765
Jamie Madill76f8fa62015-10-29 10:32:56 -0400766 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500767 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400768 case PRIMITIVE_POINTS:
769 inputPT = "point";
770 outputPT = "Triangle";
771 inputSize = 1;
772 maxVertexOutput = 4;
773 break;
774
775 case PRIMITIVE_LINES:
776 case PRIMITIVE_LINE_STRIP:
777 case PRIMITIVE_LINE_LOOP:
778 inputPT = "line";
779 outputPT = "Line";
780 inputSize = 2;
781 maxVertexOutput = 2;
782 break;
783
784 case PRIMITIVE_TRIANGLES:
785 case PRIMITIVE_TRIANGLE_STRIP:
786 case PRIMITIVE_TRIANGLE_FAN:
787 inputPT = "triangle";
788 outputPT = "Triangle";
789 inputSize = 3;
790 maxVertexOutput = 3;
791 break;
792
793 default:
794 UNREACHABLE();
795 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500796 }
797
Jamie Madill76f8fa62015-10-29 10:32:56 -0400798 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500799 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400800 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
801 "\n"
802 "uniform float4 dx_ViewCoords : register(c1);\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400803 "\n"
804 "static float2 pointSpriteCorners[] = \n"
805 "{\n"
806 " float2( 0.5f, -0.5f),\n"
807 " float2( 0.5f, 0.5f),\n"
808 " float2(-0.5f, -0.5f),\n"
809 " float2(-0.5f, 0.5f)\n"
810 "};\n"
811 "\n"
812 "static float2 pointSpriteTexcoords[] = \n"
813 "{\n"
814 " float2(1.0f, 1.0f),\n"
815 " float2(1.0f, 0.0f),\n"
816 " float2(0.0f, 1.0f),\n"
817 " float2(0.0f, 0.0f)\n"
818 "};\n"
819 "\n"
820 "static float minPointSize = "
821 << static_cast<int>(data.caps->minAliasedPointSize)
822 << ".0f;\n"
823 "static float maxPointSize = "
824 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n"
825 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500826 }
827
Jamie Madill4e31ad52015-10-29 10:32:57 -0400828 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400829 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400830 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
831
832 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
833 {
834 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
835 }
836
837 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400838 << "{\n"
839 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500840
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400841 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
842 {
843 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
844 }
845 else
846 {
847 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
848 }
849
Jamie Madill4e31ad52015-10-29 10:32:57 -0400850 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -0500851 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400852 shaderStream << " copyVertex(output, input[" << vertexIndex
853 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400854
855 if (!pointSprites)
856 {
857 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400858 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400859 }
860 }
861
862 if (pointSprites)
863 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400864 shaderStream << "\n"
865 " float4 dx_Position = input[0].dx_Position;\n"
866 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
867 "maxPointSize);\n"
868 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
869 "dx_ViewCoords.y) * dx_Position.w;\n";
870
871 for (int corner = 0; corner < 4; corner++)
872 {
873 shaderStream << "\n"
874 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
875 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
876
877 if (usesPointCoord)
878 {
879 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
880 << "];\n";
881 }
882
883 shaderStream << " outStream.Append(output);\n";
884 }
Jamie Madill5f562732014-02-14 16:41:24 -0500885 }
886
Jamie Madill334d6152015-10-22 14:00:28 -0400887 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400888 " outStream.RestartStrip();\n"
889 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500890
Jamie Madill334d6152015-10-22 14:00:28 -0400891 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500892}
893
894// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -0400895std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -0500896{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400897 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -0500898 {
899 return "_" + name;
900 }
901
902 return name;
903}
904
Jamie Madill334d6152015-10-22 14:00:28 -0400905std::string DynamicHLSL::generateAttributeConversionHLSL(
906 gl::VertexFormatType vertexFormatType,
907 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500908{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400909 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -0400910 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500911
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500912 // Matrix
913 if (IsMatrixType(shaderAttrib.type))
914 {
Jamie Madill8664b062014-02-14 16:41:29 -0500915 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500916 }
917
Jamie Madillf2575982014-06-25 16:04:54 -0400918 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -0400919 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -0500920
Jamie Madill8664b062014-02-14 16:41:29 -0500921 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -0400922 bool requiresTypeConversion =
923 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -0500924
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400925 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -0500926 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400927 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -0400928 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400929 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -0500930 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500931
932 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -0500933 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500934}
Jamie Madille39a3f02015-11-17 20:42:15 -0500935
936void DynamicHLSL::getPixelShaderOutputKey(const gl::Data &data,
937 const gl::Program::Data &programData,
938 const ProgramD3DMetadata &metadata,
939 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
940{
941 // Two cases when writing to gl_FragColor and using ESSL 1.0:
942 // - with a 3.0 context, the output color is copied to channel 0
943 // - with a 2.0 context, the output color is broadcast to all channels
944 bool broadcast = metadata.usesBroadcast(data);
945 const unsigned int numRenderTargets =
946 (broadcast || metadata.usesMultipleFragmentOuts() ? data.caps->maxDrawBuffers : 1);
947
948 if (metadata.getMajorShaderVersion() < 300)
949 {
950 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
951 renderTargetIndex++)
952 {
953 PixelShaderOutputVariable outputKeyVariable;
954 outputKeyVariable.type = GL_FLOAT_VEC4;
955 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
956 outputKeyVariable.source =
957 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
958 outputKeyVariable.outputIndex = renderTargetIndex;
959
960 outPixelShaderKey->push_back(outputKeyVariable);
961 }
962 }
963 else
964 {
965 const auto &shaderOutputVars =
966 metadata.getFragmentShader()->getData().getActiveOutputVariables();
967
968 for (auto outputPair : programData.getOutputVariables())
969 {
970 const VariableLocation &outputLocation = outputPair.second;
971 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
972 const std::string &variableName = "out_" + outputLocation.name;
973 const std::string &elementString =
974 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
975
976 ASSERT(outputVariable.staticUse);
977
978 PixelShaderOutputVariable outputKeyVariable;
979 outputKeyVariable.type = outputVariable.type;
980 outputKeyVariable.name = variableName + elementString;
981 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
982 outputKeyVariable.outputIndex = outputPair.first;
983
984 outPixelShaderKey->push_back(outputKeyVariable);
985 }
986 }
987}
988
Jamie Madill334d6152015-10-22 14:00:28 -0400989} // namespace rx