blob: c8e8e1201be256eda6228f3aa9c0d611fd37ef84 [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
Jamie Madill53cb14d2014-07-08 15:02:35 -040021// For use with ArrayString, see angleutils.h
Jamie Madill334d6152015-10-22 14:00:28 -040022static_assert(GL_INVALID_INDEX == UINT_MAX,
23 "GL_INVALID_INDEX must be equal to the max unsigned int.");
Jamie Madill5f562732014-02-14 16:41:24 -050024
Brandon Jonesd8d72432014-08-22 15:11:23 -070025using namespace gl;
26
Jamie Madill30d6c252014-11-13 10:03:33 -050027namespace rx
28{
29
Jamie Madill3f2e61d2014-09-05 10:38:05 -040030namespace
Jamie Madill5f562732014-02-14 16:41:24 -050031{
Jamie Madill8664b062014-02-14 16:41:29 -050032
33std::string HLSLComponentTypeString(GLenum componentType)
34{
35 switch (componentType)
36 {
Jamie Madill334d6152015-10-22 14:00:28 -040037 case GL_UNSIGNED_INT:
38 return "uint";
39 case GL_INT:
40 return "int";
41 case GL_UNSIGNED_NORMALIZED:
42 case GL_SIGNED_NORMALIZED:
43 case GL_FLOAT:
44 return "float";
45 default:
46 UNREACHABLE();
47 return "not-component-type";
Jamie Madill8664b062014-02-14 16:41:29 -050048 }
Jamie Madill5f562732014-02-14 16:41:24 -050049}
50
Jamie Madill8664b062014-02-14 16:41:29 -050051std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
52{
53 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
54}
55
56std::string HLSLMatrixTypeString(GLenum type)
57{
58 switch (type)
59 {
Jamie Madill334d6152015-10-22 14:00:28 -040060 case GL_FLOAT_MAT2:
61 return "float2x2";
62 case GL_FLOAT_MAT3:
63 return "float3x3";
64 case GL_FLOAT_MAT4:
65 return "float4x4";
66 case GL_FLOAT_MAT2x3:
67 return "float2x3";
68 case GL_FLOAT_MAT3x2:
69 return "float3x2";
70 case GL_FLOAT_MAT2x4:
71 return "float2x4";
72 case GL_FLOAT_MAT4x2:
73 return "float4x2";
74 case GL_FLOAT_MAT3x4:
75 return "float3x4";
76 case GL_FLOAT_MAT4x3:
77 return "float4x3";
78 default:
79 UNREACHABLE();
80 return "not-matrix-type";
Jamie Madill8664b062014-02-14 16:41:29 -050081 }
82}
83
84std::string HLSLTypeString(GLenum type)
85{
86 if (gl::IsMatrixType(type))
87 {
88 return HLSLMatrixTypeString(type);
89 }
90
Jamie Madill334d6152015-10-22 14:00:28 -040091 return HLSLComponentTypeString(gl::VariableComponentType(type),
92 gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050093}
94
Jamie Madill334d6152015-10-22 14:00:28 -040095const PixelShaderOutputVariable *FindOutputAtLocation(
96 const std::vector<PixelShaderOutputVariable> &outputVariables,
97 unsigned int location)
Jamie Madill3f2e61d2014-09-05 10:38:05 -040098{
99 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
100 {
101 if (outputVariables[variableIndex].outputIndex == location)
102 {
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000103 return &outputVariables[variableIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400104 }
105 }
106
Jamie Madill334d6152015-10-22 14:00:28 -0400107 return nullptr;
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400108}
109
Jamie Madill4cff2472015-08-21 16:53:18 -0400110const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
111const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400112} // anonymous namespace
Jamie Madill4cff2472015-08-21 16:53:18 -0400113
Jamie Madill65345da2015-11-13 11:25:23 -0500114std::string GetVaryingSemantic(int majorShaderModel, bool programUsesPointSize)
Geoff Lang48dcae72014-02-05 16:28:24 -0500115{
Jamie Madill65345da2015-11-13 11:25:23 -0500116 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
117 // In D3D11 we manually compute gl_PointCoord in the GS.
118 return ((programUsesPointSize && majorShaderModel < 4) ? "COLOR" : "TEXCOORD");
Jamie Madill5f562732014-02-14 16:41:24 -0500119}
120
Jamie Madill9fc36822015-11-18 13:08:07 -0500121// DynamicHLSL implementation
122
Jamie Madill65345da2015-11-13 11:25:23 -0500123DynamicHLSL::DynamicHLSL(RendererD3D *const renderer) : mRenderer(renderer)
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000124{
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000125}
126
Jamie Madill9fc36822015-11-18 13:08:07 -0500127void DynamicHLSL::generateVaryingHLSL(const VaryingPacking &varyingPacking,
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400128 std::stringstream &hlslStream) const
Jamie Madill5f562732014-02-14 16:41:24 -0500129{
Jamie Madill65345da2015-11-13 11:25:23 -0500130 std::string varyingSemantic =
Jamie Madill9fc36822015-11-18 13:08:07 -0500131 GetVaryingSemantic(mRenderer->getMajorShaderModel(), varyingPacking.usesPointSize());
Jamie Madill5f562732014-02-14 16:41:24 -0500132
Jamie Madill9fc36822015-11-18 13:08:07 -0500133 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500134 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500135 const sh::Varying &varying = *registerInfo.packedVarying->varying;
136 GLenum transposedType = gl::TransposeMatrixType(varying.type);
137 unsigned int semanticIndex = registerInfo.semanticIndex;
Jamie Madill4cff2472015-08-21 16:53:18 -0400138
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400139 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many
140 // registers being used.
141 // For example, if there are N registers, and we have N vec3 varyings and 1 float
142 // varying, then D3D will pack them into N registers.
143 // If the float varying has the 'nointerpolation' modifier on it then we would need
144 // N + 1 registers, and D3D compilation will fail.
Jamie Madill4cff2472015-08-21 16:53:18 -0400145
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400146 switch (varying.interpolation)
Jamie Madill4cff2472015-08-21 16:53:18 -0400147 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400148 case sh::INTERPOLATION_SMOOTH:
149 hlslStream << " ";
150 break;
151 case sh::INTERPOLATION_FLAT:
152 hlslStream << " nointerpolation ";
153 break;
154 case sh::INTERPOLATION_CENTROID:
155 hlslStream << " centroid ";
156 break;
157 default:
158 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500159 }
Jamie Madill5f562732014-02-14 16:41:24 -0500160
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400161 if (varying.isStruct())
162 {
163 // TODO(jmadill): pass back translated name from the shader translator
164 hlslStream << decorateVariable(varying.structName);
165 }
166 else
167 {
168 GLenum componentType = VariableComponentType(transposedType);
169 int columnCount = VariableColumnCount(transposedType);
170 hlslStream << HLSLComponentTypeString(componentType, columnCount);
171 }
172
Jamie Madill9fc36822015-11-18 13:08:07 -0500173 hlslStream << " v" << semanticIndex << " : " << varyingSemantic << semanticIndex << ";\n";
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400174 }
Jamie Madill5f562732014-02-14 16:41:24 -0500175}
176
Jamie Madill334d6152015-10-22 14:00:28 -0400177std::string DynamicHLSL::generateVertexShaderForInputLayout(
178 const std::string &sourceShader,
179 const InputLayout &inputLayout,
180 const std::vector<sh::Attribute> &shaderAttributes) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500181{
Jamie Madill334d6152015-10-22 14:00:28 -0400182 std::stringstream structStream;
183 std::stringstream initStream;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500184
Jamie Madill334d6152015-10-22 14:00:28 -0400185 structStream << "struct VS_INPUT\n"
186 << "{\n";
187
188 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400189 unsigned int inputIndex = 0;
190
Cooper Partine6d14cc2015-02-20 12:32:58 -0800191 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
192 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
193 // must be used.
194 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
Jamie Madill334d6152015-10-22 14:00:28 -0400195 bool useInstancedPointSpriteEmulation =
196 usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Cooper Partine6d14cc2015-02-20 12:32:58 -0800197
198 // Instanced PointSprite emulation requires additional entries in the
199 // VS_INPUT structure to support the vertices that make up the quad vertices.
200 // These values must be in sync with the cooresponding values added during inputlayout creation
201 // in InputLayoutCache::applyVertexBuffers().
202 //
203 // The additional entries must appear first in the VS_INPUT layout because
204 // Windows Phone 8 era devices require per vertex data to physically come
205 // before per instance data in the shader.
206 if (useInstancedPointSpriteEmulation)
207 {
Jamie Madill334d6152015-10-22 14:00:28 -0400208 structStream << " float3 spriteVertexPos : SPRITEPOSITION0;\n"
209 << " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
Cooper Partine6d14cc2015-02-20 12:32:58 -0800210 }
211
Jamie Madill3da79b72015-04-27 11:09:17 -0400212 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex)
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500213 {
Jamie Madillf2575982014-06-25 16:04:54 -0400214 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500215 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500216 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400217 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400218 VertexFormatType vertexFormatType =
219 inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID;
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400220
Jamie Madill3b7e2052014-03-17 09:47:43 -0400221 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500222 if (IsMatrixType(shaderAttribute.type))
223 {
224 // Matrix types are always transposed
Jamie Madill334d6152015-10-22 14:00:28 -0400225 structStream << " "
226 << HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500227 }
228 else
229 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400230 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000231
232 if (shaderAttribute.name == "gl_InstanceID")
233 {
Jamie Madill334d6152015-10-22 14:00:28 -0400234 // The input type of the instance ID in HLSL (uint) differs from the one in ESSL
235 // (int).
236 structStream << " uint";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000237 }
238 else
239 {
Jamie Madill334d6152015-10-22 14:00:28 -0400240 structStream << " " << HLSLComponentTypeString(
241 componentType,
242 VariableComponentCount(shaderAttribute.type));
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000243 }
Jamie Madill8664b062014-02-14 16:41:29 -0500244 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500245
Jamie Madill334d6152015-10-22 14:00:28 -0400246 structStream << " " << decorateVariable(shaderAttribute.name) << " : ";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000247
248 if (shaderAttribute.name == "gl_InstanceID")
249 {
Jamie Madill334d6152015-10-22 14:00:28 -0400250 structStream << "SV_InstanceID";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000251 }
252 else
253 {
Jamie Madill334d6152015-10-22 14:00:28 -0400254 structStream << "TEXCOORD" << semanticIndex;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000255 semanticIndex += VariableRegisterCount(shaderAttribute.type);
256 }
257
Jamie Madill334d6152015-10-22 14:00:28 -0400258 structStream << ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500259
Jamie Madill3b7e2052014-03-17 09:47:43 -0400260 // HLSL code for initialization
Jamie Madill334d6152015-10-22 14:00:28 -0400261 initStream << " " << decorateVariable(shaderAttribute.name) << " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500262
263 // Mismatched vertex attribute to vertex input may result in an undefined
264 // data reinterpretation (eg for pure integer->float, float->pure integer)
265 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400266 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400267 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500268 {
Jamie Madill334d6152015-10-22 14:00:28 -0400269 initStream << generateAttributeConversionHLSL(vertexFormatType, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500270 }
271 else
272 {
Jamie Madill334d6152015-10-22 14:00:28 -0400273 initStream << "input." << decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500274 }
275
Jamie Madill334d6152015-10-22 14:00:28 -0400276 initStream << ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400277
Jamie Madillac0a2672014-04-11 13:33:56 -0400278 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
279 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500280 }
281
Jamie Madill334d6152015-10-22 14:00:28 -0400282 structStream << "};\n"
283 "\n"
284 "void initAttributes(VS_INPUT input)\n"
285 "{\n"
286 << initStream.str() << "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400287
288 std::string vertexHLSL(sourceShader);
289
290 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
Jamie Madill334d6152015-10-22 14:00:28 -0400291 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), structStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400292
293 return vertexHLSL;
294}
295
Jamie Madill334d6152015-10-22 14:00:28 -0400296std::string DynamicHLSL::generatePixelShaderForOutputSignature(
297 const std::string &sourceShader,
298 const std::vector<PixelShaderOutputVariable> &outputVariables,
299 bool usesFragDepth,
300 const std::vector<GLenum> &outputLayout) const
Geoff Lang04fb89a2014-06-09 15:05:36 -0400301{
Jamie Madill334d6152015-10-22 14:00:28 -0400302 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang04fb89a2014-06-09 15:05:36 -0400303 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
Jamie Madill334d6152015-10-22 14:00:28 -0400304 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400305
Jamie Madill334d6152015-10-22 14:00:28 -0400306 std::stringstream declarationStream;
307 std::stringstream copyStream;
308
309 declarationStream << "struct PS_OUTPUT\n"
310 "{\n";
Geoff Lang4ace4232014-06-18 19:12:48 -0400311
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400312 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
313 {
314 GLenum binding = outputLayout[layoutIndex];
315
316 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400317 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400318 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
319
Jamie Madill334d6152015-10-22 14:00:28 -0400320 const PixelShaderOutputVariable *outputVariable =
321 FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400322
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000323 // OpenGL ES 3.0 spec $4.2.1
Jamie Madill334d6152015-10-22 14:00:28 -0400324 // If [...] not all user-defined output variables are written, the values of fragment
325 // colors
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000326 // corresponding to unwritten variables are similarly undefined.
327 if (outputVariable)
328 {
Jamie Madill334d6152015-10-22 14:00:28 -0400329 declarationStream << " " + HLSLTypeString(outputVariable->type) << " "
330 << outputVariable->name << " : " << targetSemantic
331 << static_cast<int>(layoutIndex) << ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400332
Jamie Madill334d6152015-10-22 14:00:28 -0400333 copyStream << " output." << outputVariable->name << " = "
334 << outputVariable->source << ";\n";
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000335 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400336 }
337 }
338
339 if (usesFragDepth)
340 {
Jamie Madill334d6152015-10-22 14:00:28 -0400341 declarationStream << " float gl_Depth : " << depthSemantic << ";\n";
342 copyStream << " output.gl_Depth = gl_Depth; \n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400343 }
344
Jamie Madill334d6152015-10-22 14:00:28 -0400345 declarationStream << "};\n"
346 "\n"
347 "PS_OUTPUT generateOutput()\n"
348 "{\n"
349 " PS_OUTPUT output;\n"
350 << copyStream.str() << " return output;\n"
351 "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400352
353 std::string pixelHLSL(sourceShader);
354
355 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
Jamie Madill334d6152015-10-22 14:00:28 -0400356 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(),
357 declarationStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400358
359 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500360}
361
Jamie Madill9fc36822015-11-18 13:08:07 -0500362void DynamicHLSL::generateVaryingLinkHLSL(ShaderType shaderType,
363 const VaryingPacking &varyingPacking,
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400364 std::stringstream &linkStream) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400365{
Jamie Madill9fc36822015-11-18 13:08:07 -0500366 const auto &builtins = varyingPacking.builtins(shaderType);
367 ASSERT(builtins.dxPosition.enabled);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400368 linkStream << "{\n"
Jamie Madill9fc36822015-11-18 13:08:07 -0500369 << " float4 dx_Position : " << builtins.dxPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700370
Jamie Madill9fc36822015-11-18 13:08:07 -0500371 if (builtins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700372 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500373 linkStream << " float4 gl_Position : " << builtins.glPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700374 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400375
Jamie Madill9fc36822015-11-18 13:08:07 -0500376 if (builtins.glFragCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400377 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500378 linkStream << " float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400379 }
380
Jamie Madill9fc36822015-11-18 13:08:07 -0500381 if (builtins.glPointCoord.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400382 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500383 linkStream << " float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400384 }
385
Jamie Madill9fc36822015-11-18 13:08:07 -0500386 if (builtins.glPointSize.enabled)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400387 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500388 linkStream << " float gl_PointSize : " << builtins.glPointSize.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400389 }
390
Jamie Madill334d6152015-10-22 14:00:28 -0400391 // Do this after glPointSize, to potentially combine gl_PointCoord and gl_PointSize into the
392 // same register.
Jamie Madill9fc36822015-11-18 13:08:07 -0500393 generateVaryingHLSL(varyingPacking, linkStream);
Austin Kinross8b695ee2015-03-12 13:12:20 -0700394
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400395 linkStream << "};\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400396}
397
Jamie Madillada9ecc2015-08-17 12:53:37 -0400398bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data,
Jamie Madill80a6fc02015-08-21 16:53:16 -0400399 const gl::Program::Data &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500400 const ProgramD3DMetadata &programMetadata,
Jamie Madill9fc36822015-11-18 13:08:07 -0500401 const VaryingPacking &varyingPacking,
Jamie Madill334d6152015-10-22 14:00:28 -0400402 std::string *pixelHLSL,
Jamie Madill9fc36822015-11-18 13:08:07 -0500403 std::string *vertexHLSL) const
Jamie Madill5f562732014-02-14 16:41:24 -0500404{
Jamie Madill334d6152015-10-22 14:00:28 -0400405 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500406
Jamie Madill91445bc2015-09-23 16:47:53 -0400407 const gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
408 const ShaderD3D *vertexShader = GetImplAs<ShaderD3D>(vertexShaderGL);
409 const gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
410 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700411 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400412
Jamie Madill334d6152015-10-22 14:00:28 -0400413 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500414 programMetadata.usesPointSize() &&
415 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400416
Jamie Madill14e95b32015-05-07 10:10:41 -0400417 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400418 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500419
Jamie Madill334d6152015-10-22 14:00:28 -0400420 std::stringstream vertexStream;
421 vertexStream << vertexShaderGL->getTranslatedSource();
422
Cooper Partine6664f02015-01-09 16:22:24 -0800423 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400424 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800425 if (useInstancedPointSpriteEmulation)
426 {
Jamie Madill334d6152015-10-22 14:00:28 -0400427 vertexStream << "static float minPointSize = "
428 << static_cast<int>(data.caps->minAliasedPointSize) << ".0f;\n"
429 << "static float maxPointSize = "
430 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800431 }
432
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500433 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill9fc36822015-11-18 13:08:07 -0500434 vertexStream << "\n" << VERTEX_ATTRIBUTE_STUB_STRING + "\n";
435
436 // Write the HLSL input/output declarations
437 vertexStream << "struct VS_OUTPUT\n";
438 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, vertexStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400439 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400440 << "VS_OUTPUT main(VS_INPUT input)\n"
441 << "{\n"
442 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500443
Jamie Madill37997142015-01-28 10:06:34 -0500444 if (vertexShader->usesDeferredInit())
445 {
Jamie Madill334d6152015-10-22 14:00:28 -0400446 vertexStream << "\n"
447 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500448 }
449
Jamie Madill334d6152015-10-22 14:00:28 -0400450 vertexStream << "\n"
451 << " gl_main();\n"
452 << "\n"
453 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700454
Jamie Madill9fc36822015-11-18 13:08:07 -0500455 const auto &vertexBuiltins = varyingPacking.builtins(SHADER_VERTEX);
456
457 if (vertexBuiltins.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700458 {
Jamie Madill334d6152015-10-22 14:00:28 -0400459 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700460 }
Jamie Madill37997142015-01-28 10:06:34 -0500461
Austin Kinross4fd18b12014-12-22 12:32:05 -0800462 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400463 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500464 {
Jamie Madill334d6152015-10-22 14:00:28 -0400465 vertexStream << " output.dx_Position.x = gl_Position.x;\n"
466 << " output.dx_Position.y = -gl_Position.y;\n"
467 << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
468 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500469 }
470 else
471 {
Jamie Madill334d6152015-10-22 14:00:28 -0400472 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
473 "dx_ViewAdjust.x * gl_Position.w;\n"
474 << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
475 "dx_ViewAdjust.y * gl_Position.w);\n"
476 << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
477 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500478 }
479
Austin Kinross8b695ee2015-03-12 13:12:20 -0700480 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
Jamie Madill9fc36822015-11-18 13:08:07 -0500481 if (vertexBuiltins.glPointSize.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500482 {
Jamie Madill334d6152015-10-22 14:00:28 -0400483 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500484 }
485
Jamie Madill9fc36822015-11-18 13:08:07 -0500486 if (vertexBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500487 {
Jamie Madill334d6152015-10-22 14:00:28 -0400488 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500489 }
490
Jamie Madill9fc36822015-11-18 13:08:07 -0500491 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500492 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500493 const sh::Varying &varying = *registerInfo.packedVarying->varying;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400494 GLenum transposedType = TransposeMatrixType(varying.type);
495 unsigned int variableRows =
496 static_cast<unsigned int>(varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill4cff2472015-08-21 16:53:18 -0400497
Jamie Madill9fc36822015-11-18 13:08:07 -0500498 vertexStream << " output.v" << registerInfo.semanticIndex << " = _" + varying.name;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400499
500 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400501 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500502 vertexStream << ArrayString(registerInfo.varyingArrayIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500503 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400504
505 if (variableRows > 1)
506 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500507 vertexStream << ArrayString(registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400508 }
509
510 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500511 }
512
Cooper Partine6664f02015-01-09 16:22:24 -0800513 // Instanced PointSprite emulation requires additional entries to calculate
514 // the final output vertex positions of the quad that represents each sprite.
515 if (useInstancedPointSpriteEmulation)
516 {
Jamie Madill334d6152015-10-22 14:00:28 -0400517 vertexStream << "\n"
518 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"
519 << " output.dx_Position.xyz += float3(input.spriteVertexPos.x * "
520 "gl_PointSize / (dx_ViewCoords.x*2), input.spriteVertexPos.y * "
521 "gl_PointSize / (dx_ViewCoords.y*2), input.spriteVertexPos.z) * "
522 "output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800523
Jamie Madille39a3f02015-11-17 20:42:15 -0500524 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800525 {
Jamie Madill334d6152015-10-22 14:00:28 -0400526 vertexStream << "\n"
527 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800528 }
529 }
530
Cooper Partin7c89d242015-10-13 12:45:59 -0700531 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
532 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
533 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500534 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700535 {
536 ASSERT(!useInstancedPointSpriteEmulation);
537 vertexStream << "\n"
538 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
539 }
540
Jamie Madill334d6152015-10-22 14:00:28 -0400541 vertexStream << "\n"
542 << " return output;\n"
543 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500544
Jamie Madill334d6152015-10-22 14:00:28 -0400545 std::stringstream pixelStream;
546 pixelStream << fragmentShaderGL->getTranslatedSource();
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400547 pixelStream << "struct PS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500548 generateVaryingLinkHLSL(SHADER_PIXEL, varyingPacking, pixelStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400549 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500550
Jamie Madill334d6152015-10-22 14:00:28 -0400551 pixelStream << PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500552
Jamie Madill334d6152015-10-22 14:00:28 -0400553 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500554 {
555 if (shaderModel >= 4)
556 {
Jamie Madill334d6152015-10-22 14:00:28 -0400557 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
558 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500559 }
560 else
561 {
Jamie Madill334d6152015-10-22 14:00:28 -0400562 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
563 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500564 }
565 }
566 else
567 {
Jamie Madill334d6152015-10-22 14:00:28 -0400568 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
569 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500570 }
571
Jamie Madill9fc36822015-11-18 13:08:07 -0500572 const auto &pixelBuiltins = varyingPacking.builtins(SHADER_PIXEL);
573
574 if (pixelBuiltins.glFragCoord.enabled)
Jamie Madill5f562732014-02-14 16:41:24 -0500575 {
Jamie Madill334d6152015-10-22 14:00:28 -0400576 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500577
Austin Kinross588434c2014-12-10 10:41:45 -0800578 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400579 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
580 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800581 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500582 {
Jamie Madill334d6152015-10-22 14:00:28 -0400583 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
584 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500585 }
Austin Kinross588434c2014-12-10 10:41:45 -0800586 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500587 {
Jamie Madill334d6152015-10-22 14:00:28 -0400588 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
589 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500590 }
591 else
592 {
Jamie Madill334d6152015-10-22 14:00:28 -0400593 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
594 // Renderer::setViewport()
595 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
596 "dx_ViewCoords.z;\n"
597 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
598 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500599 }
600
Jamie Madill334d6152015-10-22 14:00:28 -0400601 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
602 "dx_DepthFront.y;\n"
603 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500604 }
605
Jamie Madill9fc36822015-11-18 13:08:07 -0500606 if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500607 {
Jamie Madill334d6152015-10-22 14:00:28 -0400608 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
609 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500610 }
611
Jamie Madill334d6152015-10-22 14:00:28 -0400612 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500613 {
614 if (shaderModel <= 3)
615 {
Jamie Madill334d6152015-10-22 14:00:28 -0400616 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500617 }
618 else
619 {
Jamie Madill334d6152015-10-22 14:00:28 -0400620 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500621 }
622 }
623
Jamie Madill9fc36822015-11-18 13:08:07 -0500624 for (const PackedVaryingRegister &registerInfo : varyingPacking.getRegisterList())
Jamie Madill5f562732014-02-14 16:41:24 -0500625 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500626 const sh::Varying &varying = *registerInfo.packedVarying->varying;
Jamie Madill4cff2472015-08-21 16:53:18 -0400627
Jamie Madillca03b352015-09-02 12:38:13 -0400628 // Don't reference VS-only transform feedback varyings in the PS.
Jamie Madill9fc36822015-11-18 13:08:07 -0500629 if (registerInfo.packedVarying->vertexOnly)
Jamie Madillca03b352015-09-02 12:38:13 -0400630 continue;
631
Jamie Madill4cff2472015-08-21 16:53:18 -0400632 ASSERT(!varying.isBuiltIn());
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400633 GLenum transposedType = TransposeMatrixType(varying.type);
634 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400635 pixelStream << " _" << varying.name;
636
637 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400638 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500639 pixelStream << ArrayString(registerInfo.varyingArrayIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400640 }
641
642 if (variableRows > 1)
643 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500644 pixelStream << ArrayString(registerInfo.varyingRowIndex);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400645 }
646
Jamie Madill9fc36822015-11-18 13:08:07 -0500647 pixelStream << " = input.v" << registerInfo.semanticIndex;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400648
649 if (!varying.isStruct())
650 {
651 switch (VariableColumnCount(transposedType))
Jamie Madill5f562732014-02-14 16:41:24 -0500652 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400653 case 1:
654 pixelStream << ".x";
Jamie Madill4cff2472015-08-21 16:53:18 -0400655 break;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400656 case 2:
657 pixelStream << ".xy";
658 break;
659 case 3:
660 pixelStream << ".xyz";
661 break;
662 case 4:
663 break;
664 default:
665 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500666 }
667 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400668 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500669 }
670
Jamie Madill37997142015-01-28 10:06:34 -0500671 if (fragmentShader->usesDeferredInit())
672 {
Jamie Madill334d6152015-10-22 14:00:28 -0400673 pixelStream << "\n"
674 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500675 }
676
Jamie Madill334d6152015-10-22 14:00:28 -0400677 pixelStream << "\n"
678 << " gl_main();\n"
679 << "\n"
680 << " return generateOutput();\n"
681 << "}\n";
682
683 *vertexHLSL = vertexStream.str();
684 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500685
686 return true;
687}
688
Jamie Madill9fc36822015-11-18 13:08:07 -0500689std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking) const
Jamie Madill5f562732014-02-14 16:41:24 -0500690{
Jamie Madill9fc36822015-11-18 13:08:07 -0500691 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400692
Jamie Madill4e31ad52015-10-29 10:32:57 -0400693 std::stringstream preambleStream;
694
Jamie Madill9fc36822015-11-18 13:08:07 -0500695 const auto &builtins = varyingPacking.builtins(SHADER_VERTEX);
696
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400697 preambleStream << "struct GS_INPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500698 generateVaryingLinkHLSL(SHADER_VERTEX, varyingPacking, preambleStream);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400699 preambleStream << "\n"
700 << "struct GS_OUTPUT\n";
Jamie Madill9fc36822015-11-18 13:08:07 -0500701 generateVaryingLinkHLSL(SHADER_GEOMETRY, varyingPacking, preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400702 preambleStream
703 << "\n"
704 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
705 << "{\n"
706 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400707
Jamie Madill9fc36822015-11-18 13:08:07 -0500708 if (builtins.glPointSize.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400709 {
710 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
711 }
712
Jamie Madill9fc36822015-11-18 13:08:07 -0500713 for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList())
Jamie Madill4e31ad52015-10-29 10:32:57 -0400714 {
Jamie Madill9fc36822015-11-18 13:08:07 -0500715 const sh::Varying &varying = *varyingRegister.packedVarying->varying;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400716
Jamie Madill9fc36822015-11-18 13:08:07 -0500717 preambleStream << " output.v" << varyingRegister.semanticIndex << " = ";
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400718 if (varying.interpolation == sh::INTERPOLATION_FLAT)
719 {
720 preambleStream << "flat";
721 }
Jamie Madill9fc36822015-11-18 13:08:07 -0500722 preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400723 }
724
Jamie Madill9fc36822015-11-18 13:08:07 -0500725 if (builtins.glFragCoord.enabled)
Jamie Madill4e31ad52015-10-29 10:32:57 -0400726 {
727 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
728 }
729
730 // Only write the dx_Position if we aren't using point sprites
731 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
732 << " output.dx_Position = input.dx_Position;\n"
733 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
734 << "}\n";
735
736 return preambleStream.str();
737}
738
739std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
740 const gl::Data &data,
741 const gl::Program::Data &programData,
742 const std::string &preambleString) const
743{
744 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500745
Jamie Madill334d6152015-10-22 14:00:28 -0400746 std::stringstream shaderStream;
747
Jamie Madill4e31ad52015-10-29 10:32:57 -0400748 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
749 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500750
Jamie Madill76f8fa62015-10-29 10:32:56 -0400751 const char *inputPT = nullptr;
752 const char *outputPT = nullptr;
753 int inputSize = 0;
754 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500755
Jamie Madill76f8fa62015-10-29 10:32:56 -0400756 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500757 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400758 case PRIMITIVE_POINTS:
759 inputPT = "point";
760 outputPT = "Triangle";
761 inputSize = 1;
762 maxVertexOutput = 4;
763 break;
764
765 case PRIMITIVE_LINES:
766 case PRIMITIVE_LINE_STRIP:
767 case PRIMITIVE_LINE_LOOP:
768 inputPT = "line";
769 outputPT = "Line";
770 inputSize = 2;
771 maxVertexOutput = 2;
772 break;
773
774 case PRIMITIVE_TRIANGLES:
775 case PRIMITIVE_TRIANGLE_STRIP:
776 case PRIMITIVE_TRIANGLE_FAN:
777 inputPT = "triangle";
778 outputPT = "Triangle";
779 inputSize = 3;
780 maxVertexOutput = 3;
781 break;
782
783 default:
784 UNREACHABLE();
785 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500786 }
787
Jamie Madill76f8fa62015-10-29 10:32:56 -0400788 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500789 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400790 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
791 "\n"
792 "uniform float4 dx_ViewCoords : register(c1);\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400793 "\n"
794 "static float2 pointSpriteCorners[] = \n"
795 "{\n"
796 " float2( 0.5f, -0.5f),\n"
797 " float2( 0.5f, 0.5f),\n"
798 " float2(-0.5f, -0.5f),\n"
799 " float2(-0.5f, 0.5f)\n"
800 "};\n"
801 "\n"
802 "static float2 pointSpriteTexcoords[] = \n"
803 "{\n"
804 " float2(1.0f, 1.0f),\n"
805 " float2(1.0f, 0.0f),\n"
806 " float2(0.0f, 1.0f),\n"
807 " float2(0.0f, 0.0f)\n"
808 "};\n"
809 "\n"
810 "static float minPointSize = "
811 << static_cast<int>(data.caps->minAliasedPointSize)
812 << ".0f;\n"
813 "static float maxPointSize = "
814 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n"
815 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500816 }
817
Jamie Madill4e31ad52015-10-29 10:32:57 -0400818 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400819 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400820 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
821
822 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
823 {
824 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
825 }
826
827 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400828 << "{\n"
829 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500830
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400831 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
832 {
833 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
834 }
835 else
836 {
837 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
838 }
839
Jamie Madill4e31ad52015-10-29 10:32:57 -0400840 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -0500841 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400842 shaderStream << " copyVertex(output, input[" << vertexIndex
843 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400844
845 if (!pointSprites)
846 {
847 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400848 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400849 }
850 }
851
852 if (pointSprites)
853 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400854 shaderStream << "\n"
855 " float4 dx_Position = input[0].dx_Position;\n"
856 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
857 "maxPointSize);\n"
858 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
859 "dx_ViewCoords.y) * dx_Position.w;\n";
860
861 for (int corner = 0; corner < 4; corner++)
862 {
863 shaderStream << "\n"
864 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
865 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
866
867 if (usesPointCoord)
868 {
869 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
870 << "];\n";
871 }
872
873 shaderStream << " outStream.Append(output);\n";
874 }
Jamie Madill5f562732014-02-14 16:41:24 -0500875 }
876
Jamie Madill334d6152015-10-22 14:00:28 -0400877 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400878 " outStream.RestartStrip();\n"
879 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500880
Jamie Madill334d6152015-10-22 14:00:28 -0400881 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500882}
883
884// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -0400885std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -0500886{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400887 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -0500888 {
889 return "_" + name;
890 }
891
892 return name;
893}
894
Jamie Madill334d6152015-10-22 14:00:28 -0400895std::string DynamicHLSL::generateAttributeConversionHLSL(
896 gl::VertexFormatType vertexFormatType,
897 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500898{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400899 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -0400900 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500901
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500902 // Matrix
903 if (IsMatrixType(shaderAttrib.type))
904 {
Jamie Madill8664b062014-02-14 16:41:29 -0500905 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500906 }
907
Jamie Madillf2575982014-06-25 16:04:54 -0400908 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -0400909 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -0500910
Jamie Madill8664b062014-02-14 16:41:29 -0500911 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -0400912 bool requiresTypeConversion =
913 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -0500914
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400915 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -0500916 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400917 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -0400918 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -0400919 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -0500920 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500921
922 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -0500923 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500924}
Jamie Madille39a3f02015-11-17 20:42:15 -0500925
926void DynamicHLSL::getPixelShaderOutputKey(const gl::Data &data,
927 const gl::Program::Data &programData,
928 const ProgramD3DMetadata &metadata,
929 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
930{
931 // Two cases when writing to gl_FragColor and using ESSL 1.0:
932 // - with a 3.0 context, the output color is copied to channel 0
933 // - with a 2.0 context, the output color is broadcast to all channels
934 bool broadcast = metadata.usesBroadcast(data);
935 const unsigned int numRenderTargets =
936 (broadcast || metadata.usesMultipleFragmentOuts() ? data.caps->maxDrawBuffers : 1);
937
938 if (metadata.getMajorShaderVersion() < 300)
939 {
940 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
941 renderTargetIndex++)
942 {
943 PixelShaderOutputVariable outputKeyVariable;
944 outputKeyVariable.type = GL_FLOAT_VEC4;
945 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
946 outputKeyVariable.source =
947 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
948 outputKeyVariable.outputIndex = renderTargetIndex;
949
950 outPixelShaderKey->push_back(outputKeyVariable);
951 }
952 }
953 else
954 {
955 const auto &shaderOutputVars =
956 metadata.getFragmentShader()->getData().getActiveOutputVariables();
957
958 for (auto outputPair : programData.getOutputVariables())
959 {
960 const VariableLocation &outputLocation = outputPair.second;
961 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
962 const std::string &variableName = "out_" + outputLocation.name;
963 const std::string &elementString =
964 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
965
966 ASSERT(outputVariable.staticUse);
967
968 PixelShaderOutputVariable outputKeyVariable;
969 outputKeyVariable.type = outputVariable.type;
970 outputKeyVariable.name = variableName + elementString;
971 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
972 outputKeyVariable.outputIndex = outputPair.first;
973
974 outPixelShaderKey->push_back(outputKeyVariable);
975 }
976 }
977}
978
Jamie Madill334d6152015-10-22 14:00:28 -0400979} // namespace rx