blob: 8f5f0063ee8794cd58b8486bb0e46bc954d06aca [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 Madill65345da2015-11-13 11:25:23 -0500121DynamicHLSL::DynamicHLSL(RendererD3D *const renderer) : mRenderer(renderer)
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000122{
Jamie Madillbbdeeb12015-11-12 15:42:16 +0000123}
124
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400125void DynamicHLSL::generateVaryingHLSL(const gl::Caps &caps,
126 const std::vector<PackedVarying> &varyings,
127 bool programUsesPointSize,
128 std::stringstream &hlslStream) const
Jamie Madill5f562732014-02-14 16:41:24 -0500129{
Jamie Madill65345da2015-11-13 11:25:23 -0500130 std::string varyingSemantic =
131 GetVaryingSemantic(mRenderer->getMajorShaderModel(), programUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500132
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400133 for (const PackedVaryingRegister &registerInfo : PackedVaryingIterator(varyings))
Jamie Madill5f562732014-02-14 16:41:24 -0500134 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400135 const PackedVarying &packedVarying = varyings[registerInfo.varyingIndex];
Jamie Madill4cff2472015-08-21 16:53:18 -0400136 const sh::Varying &varying = *packedVarying.varying;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400137 GLenum transposedType = gl::TransposeMatrixType(varying.type);
138 unsigned int registerIndex = registerInfo.registerIndex(caps, varyings);
Jamie Madill4cff2472015-08-21 16:53:18 -0400139
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400140 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many
141 // registers being used.
142 // For example, if there are N registers, and we have N vec3 varyings and 1 float
143 // varying, then D3D will pack them into N registers.
144 // If the float varying has the 'nointerpolation' modifier on it then we would need
145 // N + 1 registers, and D3D compilation will fail.
Jamie Madill4cff2472015-08-21 16:53:18 -0400146
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400147 switch (varying.interpolation)
Jamie Madill4cff2472015-08-21 16:53:18 -0400148 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400149 case sh::INTERPOLATION_SMOOTH:
150 hlslStream << " ";
151 break;
152 case sh::INTERPOLATION_FLAT:
153 hlslStream << " nointerpolation ";
154 break;
155 case sh::INTERPOLATION_CENTROID:
156 hlslStream << " centroid ";
157 break;
158 default:
159 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500160 }
Jamie Madill5f562732014-02-14 16:41:24 -0500161
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400162 if (varying.isStruct())
163 {
164 // TODO(jmadill): pass back translated name from the shader translator
165 hlslStream << decorateVariable(varying.structName);
166 }
167 else
168 {
169 GLenum componentType = VariableComponentType(transposedType);
170 int columnCount = VariableColumnCount(transposedType);
171 hlslStream << HLSLComponentTypeString(componentType, columnCount);
172 }
173
174 hlslStream << " v" << registerIndex << " : " << varyingSemantic << registerIndex << ";\n";
175 }
Jamie Madill5f562732014-02-14 16:41:24 -0500176}
177
Jamie Madill334d6152015-10-22 14:00:28 -0400178std::string DynamicHLSL::generateVertexShaderForInputLayout(
179 const std::string &sourceShader,
180 const InputLayout &inputLayout,
181 const std::vector<sh::Attribute> &shaderAttributes) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500182{
Jamie Madill334d6152015-10-22 14:00:28 -0400183 std::stringstream structStream;
184 std::stringstream initStream;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500185
Jamie Madill334d6152015-10-22 14:00:28 -0400186 structStream << "struct VS_INPUT\n"
187 << "{\n";
188
189 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400190 unsigned int inputIndex = 0;
191
Cooper Partine6d14cc2015-02-20 12:32:58 -0800192 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
193 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
194 // must be used.
195 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
Jamie Madill334d6152015-10-22 14:00:28 -0400196 bool useInstancedPointSpriteEmulation =
197 usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Cooper Partine6d14cc2015-02-20 12:32:58 -0800198
199 // Instanced PointSprite emulation requires additional entries in the
200 // VS_INPUT structure to support the vertices that make up the quad vertices.
201 // These values must be in sync with the cooresponding values added during inputlayout creation
202 // in InputLayoutCache::applyVertexBuffers().
203 //
204 // The additional entries must appear first in the VS_INPUT layout because
205 // Windows Phone 8 era devices require per vertex data to physically come
206 // before per instance data in the shader.
207 if (useInstancedPointSpriteEmulation)
208 {
Jamie Madill334d6152015-10-22 14:00:28 -0400209 structStream << " float3 spriteVertexPos : SPRITEPOSITION0;\n"
210 << " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
Cooper Partine6d14cc2015-02-20 12:32:58 -0800211 }
212
Jamie Madill3da79b72015-04-27 11:09:17 -0400213 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex)
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500214 {
Jamie Madillf2575982014-06-25 16:04:54 -0400215 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500216 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500217 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400218 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400219 VertexFormatType vertexFormatType =
220 inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID;
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400221
Jamie Madill3b7e2052014-03-17 09:47:43 -0400222 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500223 if (IsMatrixType(shaderAttribute.type))
224 {
225 // Matrix types are always transposed
Jamie Madill334d6152015-10-22 14:00:28 -0400226 structStream << " "
227 << HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500228 }
229 else
230 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400231 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000232
233 if (shaderAttribute.name == "gl_InstanceID")
234 {
Jamie Madill334d6152015-10-22 14:00:28 -0400235 // The input type of the instance ID in HLSL (uint) differs from the one in ESSL
236 // (int).
237 structStream << " uint";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000238 }
239 else
240 {
Jamie Madill334d6152015-10-22 14:00:28 -0400241 structStream << " " << HLSLComponentTypeString(
242 componentType,
243 VariableComponentCount(shaderAttribute.type));
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000244 }
Jamie Madill8664b062014-02-14 16:41:29 -0500245 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500246
Jamie Madill334d6152015-10-22 14:00:28 -0400247 structStream << " " << decorateVariable(shaderAttribute.name) << " : ";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000248
249 if (shaderAttribute.name == "gl_InstanceID")
250 {
Jamie Madill334d6152015-10-22 14:00:28 -0400251 structStream << "SV_InstanceID";
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000252 }
253 else
254 {
Jamie Madill334d6152015-10-22 14:00:28 -0400255 structStream << "TEXCOORD" << semanticIndex;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000256 semanticIndex += VariableRegisterCount(shaderAttribute.type);
257 }
258
Jamie Madill334d6152015-10-22 14:00:28 -0400259 structStream << ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500260
Jamie Madill3b7e2052014-03-17 09:47:43 -0400261 // HLSL code for initialization
Jamie Madill334d6152015-10-22 14:00:28 -0400262 initStream << " " << decorateVariable(shaderAttribute.name) << " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500263
264 // Mismatched vertex attribute to vertex input may result in an undefined
265 // data reinterpretation (eg for pure integer->float, float->pure integer)
266 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400267 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400268 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500269 {
Jamie Madill334d6152015-10-22 14:00:28 -0400270 initStream << generateAttributeConversionHLSL(vertexFormatType, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500271 }
272 else
273 {
Jamie Madill334d6152015-10-22 14:00:28 -0400274 initStream << "input." << decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500275 }
276
Jamie Madill334d6152015-10-22 14:00:28 -0400277 initStream << ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400278
Jamie Madillac0a2672014-04-11 13:33:56 -0400279 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
280 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500281 }
282
Jamie Madill334d6152015-10-22 14:00:28 -0400283 structStream << "};\n"
284 "\n"
285 "void initAttributes(VS_INPUT input)\n"
286 "{\n"
287 << initStream.str() << "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400288
289 std::string vertexHLSL(sourceShader);
290
291 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
Jamie Madill334d6152015-10-22 14:00:28 -0400292 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), structStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400293
294 return vertexHLSL;
295}
296
Jamie Madill334d6152015-10-22 14:00:28 -0400297std::string DynamicHLSL::generatePixelShaderForOutputSignature(
298 const std::string &sourceShader,
299 const std::vector<PixelShaderOutputVariable> &outputVariables,
300 bool usesFragDepth,
301 const std::vector<GLenum> &outputLayout) const
Geoff Lang04fb89a2014-06-09 15:05:36 -0400302{
Jamie Madill334d6152015-10-22 14:00:28 -0400303 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang04fb89a2014-06-09 15:05:36 -0400304 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
Jamie Madill334d6152015-10-22 14:00:28 -0400305 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400306
Jamie Madill334d6152015-10-22 14:00:28 -0400307 std::stringstream declarationStream;
308 std::stringstream copyStream;
309
310 declarationStream << "struct PS_OUTPUT\n"
311 "{\n";
Geoff Lang4ace4232014-06-18 19:12:48 -0400312
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400313 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
314 {
315 GLenum binding = outputLayout[layoutIndex];
316
317 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400318 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400319 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
320
Jamie Madill334d6152015-10-22 14:00:28 -0400321 const PixelShaderOutputVariable *outputVariable =
322 FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400323
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000324 // OpenGL ES 3.0 spec $4.2.1
Jamie Madill334d6152015-10-22 14:00:28 -0400325 // If [...] not all user-defined output variables are written, the values of fragment
326 // colors
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000327 // corresponding to unwritten variables are similarly undefined.
328 if (outputVariable)
329 {
Jamie Madill334d6152015-10-22 14:00:28 -0400330 declarationStream << " " + HLSLTypeString(outputVariable->type) << " "
331 << outputVariable->name << " : " << targetSemantic
332 << static_cast<int>(layoutIndex) << ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400333
Jamie Madill334d6152015-10-22 14:00:28 -0400334 copyStream << " output." << outputVariable->name << " = "
335 << outputVariable->source << ";\n";
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000336 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400337 }
338 }
339
340 if (usesFragDepth)
341 {
Jamie Madill334d6152015-10-22 14:00:28 -0400342 declarationStream << " float gl_Depth : " << depthSemantic << ";\n";
343 copyStream << " output.gl_Depth = gl_Depth; \n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400344 }
345
Jamie Madill334d6152015-10-22 14:00:28 -0400346 declarationStream << "};\n"
347 "\n"
348 "PS_OUTPUT generateOutput()\n"
349 "{\n"
350 " PS_OUTPUT output;\n"
351 << copyStream.str() << " return output;\n"
352 "}\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400353
354 std::string pixelHLSL(sourceShader);
355
356 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
Jamie Madill334d6152015-10-22 14:00:28 -0400357 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(),
358 declarationStream.str());
Geoff Lang04fb89a2014-06-09 15:05:36 -0400359
360 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500361}
362
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400363void DynamicHLSL::generateVaryingLinkHLSL(const gl::Caps &caps,
364 bool programUsesPointSize,
365 const SemanticInfo &info,
366 const std::vector<PackedVarying> &packedVaryings,
367 std::stringstream &linkStream) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400368{
Austin Kinross8b695ee2015-03-12 13:12:20 -0700369 ASSERT(info.dxPosition.enabled);
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400370 linkStream << "{\n"
371 << " float4 dx_Position : " << info.dxPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700372
373 if (info.glPosition.enabled)
374 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400375 linkStream << " float4 gl_Position : " << info.glPosition.str() << ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700376 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400377
378 if (info.glFragCoord.enabled)
379 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400380 linkStream << " float4 gl_FragCoord : " << info.glFragCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400381 }
382
383 if (info.glPointCoord.enabled)
384 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400385 linkStream << " float2 gl_PointCoord : " << info.glPointCoord.str() << ";\n";
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400386 }
387
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400388 if (info.glPointSize.enabled)
389 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400390 linkStream << " float gl_PointSize : " << info.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 Madillf4b2c4f2015-10-29 14:38:54 -0400395 generateVaryingHLSL(caps, packedVaryings, programUsesPointSize, 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 Madill28afae52015-11-09 15:07:57 -0500400void DynamicHLSL::storeBuiltinVaryings(const SemanticInfo &info,
401 std::vector<D3DVarying> *d3dVaryingsOut) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400402{
Austin Kinross8b695ee2015-03-12 13:12:20 -0700403 if (info.glPosition.enabled)
404 {
Jamie Madill28afae52015-11-09 15:07:57 -0500405 d3dVaryingsOut->push_back(D3DVarying("gl_Position", GL_FLOAT_VEC4, 1,
406 info.glPosition.semantic, info.glPosition.index, 1));
Austin Kinross8b695ee2015-03-12 13:12:20 -0700407 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400408
409 if (info.glFragCoord.enabled)
410 {
Jamie Madill28afae52015-11-09 15:07:57 -0500411 d3dVaryingsOut->push_back(D3DVarying("gl_FragCoord", GL_FLOAT_VEC4, 1,
412 info.glFragCoord.semantic, info.glFragCoord.index, 1));
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400413 }
414
415 if (info.glPointSize.enabled)
416 {
Jamie Madill28afae52015-11-09 15:07:57 -0500417 d3dVaryingsOut->push_back(D3DVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400418 }
419}
420
Jamie Madill28afae52015-11-09 15:07:57 -0500421void DynamicHLSL::storeUserVaryings(const std::vector<PackedVarying> &packedVaryings,
422 bool programUsesPointSize,
423 std::vector<D3DVarying> *d3dVaryingsOut) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400424{
Jamie Madill65345da2015-11-13 11:25:23 -0500425 const std::string &varyingSemantic =
426 GetVaryingSemantic(mRenderer->getMajorShaderModel(), programUsesPointSize);
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400427
Jamie Madillca03b352015-09-02 12:38:13 -0400428 for (const PackedVarying &packedVarying : packedVaryings)
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400429 {
Jamie Madill4cff2472015-08-21 16:53:18 -0400430 if (packedVarying.registerAssigned())
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400431 {
Jamie Madill4cff2472015-08-21 16:53:18 -0400432 const sh::Varying &varying = *packedVarying.varying;
433
Jamie Madill54ad4f82014-09-03 09:40:46 -0400434 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400435 GLenum transposedType = TransposeMatrixType(varying.type);
Jamie Madill334d6152015-10-22 14:00:28 -0400436 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400437
Jamie Madill28afae52015-11-09 15:07:57 -0500438 d3dVaryingsOut->push_back(D3DVarying(varying.name, varying.type, varying.elementCount(),
439 varyingSemantic, packedVarying.registerIndex,
440 variableRows * varying.elementCount()));
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400441 }
442 }
443}
444
Jamie Madillada9ecc2015-08-17 12:53:37 -0400445bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data,
Jamie Madill80a6fc02015-08-21 16:53:16 -0400446 const gl::Program::Data &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500447 const ProgramD3DMetadata &programMetadata,
Jamie Madillada9ecc2015-08-17 12:53:37 -0400448 InfoLog &infoLog,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400449 unsigned int registerCount,
Jamie Madill334d6152015-10-22 14:00:28 -0400450 std::string *pixelHLSL,
451 std::string *vertexHLSL,
Jamie Madillca03b352015-09-02 12:38:13 -0400452 const std::vector<PackedVarying> &packedVaryings,
Jamie Madille39a3f02015-11-17 20:42:15 -0500453 std::vector<D3DVarying> *d3dVaryingsOut) const
Jamie Madill5f562732014-02-14 16:41:24 -0500454{
Jamie Madill334d6152015-10-22 14:00:28 -0400455 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500456
Jamie Madill91445bc2015-09-23 16:47:53 -0400457 const gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
458 const ShaderD3D *vertexShader = GetImplAs<ShaderD3D>(vertexShaderGL);
459 const gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
460 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700461 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400462
Jamie Madille39a3f02015-11-17 20:42:15 -0500463 bool usesFragCoord = programMetadata.usesFragCoord();
Jamie Madill334d6152015-10-22 14:00:28 -0400464 bool usesPointCoord = fragmentShader->usesPointCoord();
465 bool usesPointSize = vertexShader->usesPointSize();
466 bool useInstancedPointSpriteEmulation =
Jamie Madille39a3f02015-11-17 20:42:15 -0500467 programMetadata.usesPointSize() &&
468 mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400469
Jamie Madill14e95b32015-05-07 10:10:41 -0400470 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400471 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500472
473 // Write the HLSL input/output declarations
Jamie Madill4e31ad52015-10-29 10:32:57 -0400474 const unsigned int registersNeeded =
475 registerCount + (usesFragCoord ? 1u : 0u) + (usesPointCoord ? 1u : 0u);
Jamie Madill5f562732014-02-14 16:41:24 -0500476
Jamie Madillde8892b2014-11-11 13:00:22 -0500477 if (static_cast<GLuint>(registersNeeded) > data.caps->maxVaryingVectors)
Jamie Madill5f562732014-02-14 16:41:24 -0500478 {
Jamie Madillf6113162015-05-07 11:49:21 -0400479 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madill5f562732014-02-14 16:41:24 -0500480 return false;
481 }
482
Jamie Madill334d6152015-10-22 14:00:28 -0400483 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
484 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Cooper Partine6664f02015-01-09 16:22:24 -0800485 // GeometryShader PointSprite emulation does not require this additional entry because the
Jamie Madill334d6152015-10-22 14:00:28 -0400486 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
487 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
488 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
489 // gl_PointSize in VS_OUTPUT.
Jamie Madille39a3f02015-11-17 20:42:15 -0500490 const SemanticInfo &vertexSemantics =
491 GetSemanticInfo(SHADER_VERTEX, programMetadata, registerCount);
Jamie Madill5f562732014-02-14 16:41:24 -0500492
Jamie Madill28afae52015-11-09 15:07:57 -0500493 storeUserVaryings(packedVaryings, usesPointSize, d3dVaryingsOut);
494 storeBuiltinVaryings(vertexSemantics, d3dVaryingsOut);
Jamie Madill5f562732014-02-14 16:41:24 -0500495
Jamie Madill334d6152015-10-22 14:00:28 -0400496 std::stringstream vertexStream;
497 vertexStream << vertexShaderGL->getTranslatedSource();
498
Cooper Partine6664f02015-01-09 16:22:24 -0800499 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400500 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800501 if (useInstancedPointSpriteEmulation)
502 {
Jamie Madill334d6152015-10-22 14:00:28 -0400503 vertexStream << "static float minPointSize = "
504 << static_cast<int>(data.caps->minAliasedPointSize) << ".0f;\n"
505 << "static float maxPointSize = "
506 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800507 }
508
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500509 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill334d6152015-10-22 14:00:28 -0400510 vertexStream << "\n" << VERTEX_ATTRIBUTE_STUB_STRING + "\n"
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400511 << "struct VS_OUTPUT\n";
512 generateVaryingLinkHLSL(*data.caps, usesPointSize, vertexSemantics, packedVaryings,
513 vertexStream);
514 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400515 << "VS_OUTPUT main(VS_INPUT input)\n"
516 << "{\n"
517 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500518
Jamie Madill37997142015-01-28 10:06:34 -0500519 if (vertexShader->usesDeferredInit())
520 {
Jamie Madill334d6152015-10-22 14:00:28 -0400521 vertexStream << "\n"
522 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500523 }
524
Jamie Madill334d6152015-10-22 14:00:28 -0400525 vertexStream << "\n"
526 << " gl_main();\n"
527 << "\n"
528 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700529
Jamie Madille39a3f02015-11-17 20:42:15 -0500530 if (vertexSemantics.glPosition.enabled)
Austin Kinross8b695ee2015-03-12 13:12:20 -0700531 {
Jamie Madill334d6152015-10-22 14:00:28 -0400532 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700533 }
Jamie Madill37997142015-01-28 10:06:34 -0500534
Austin Kinross4fd18b12014-12-22 12:32:05 -0800535 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400536 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500537 {
Jamie Madill334d6152015-10-22 14:00:28 -0400538 vertexStream << " output.dx_Position.x = gl_Position.x;\n"
539 << " output.dx_Position.y = -gl_Position.y;\n"
540 << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
541 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500542 }
543 else
544 {
Jamie Madill334d6152015-10-22 14:00:28 -0400545 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
546 "dx_ViewAdjust.x * gl_Position.w;\n"
547 << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
548 "dx_ViewAdjust.y * gl_Position.w);\n"
549 << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
550 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500551 }
552
Austin Kinross8b695ee2015-03-12 13:12:20 -0700553 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
554 if (usesPointSize && shaderModel >= 3 && !useInstancedPointSpriteEmulation)
Jamie Madill5f562732014-02-14 16:41:24 -0500555 {
Jamie Madill334d6152015-10-22 14:00:28 -0400556 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500557 }
558
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400559 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500560 {
Jamie Madill334d6152015-10-22 14:00:28 -0400561 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500562 }
563
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400564 for (const PackedVaryingRegister &registerInfo : PackedVaryingIterator(packedVaryings))
Jamie Madill5f562732014-02-14 16:41:24 -0500565 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400566 const PackedVarying &packedVarying = packedVaryings[registerInfo.varyingIndex];
Jamie Madill4cff2472015-08-21 16:53:18 -0400567 const sh::Varying &varying = *packedVarying.varying;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400568 GLenum transposedType = TransposeMatrixType(varying.type);
569 unsigned int variableRows =
570 static_cast<unsigned int>(varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill4cff2472015-08-21 16:53:18 -0400571
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400572 int r = registerInfo.registerIndex(*data.caps, packedVaryings);
573 vertexStream << " output.v" << r << " = _" + varying.name;
574
575 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400576 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400577 vertexStream << ArrayString(registerInfo.elementIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500578 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400579
580 if (variableRows > 1)
581 {
582 vertexStream << ArrayString(registerInfo.rowIndex);
583 }
584
585 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500586 }
587
Cooper Partine6664f02015-01-09 16:22:24 -0800588 // Instanced PointSprite emulation requires additional entries to calculate
589 // the final output vertex positions of the quad that represents each sprite.
590 if (useInstancedPointSpriteEmulation)
591 {
Jamie Madill334d6152015-10-22 14:00:28 -0400592 vertexStream << "\n"
593 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"
594 << " output.dx_Position.xyz += float3(input.spriteVertexPos.x * "
595 "gl_PointSize / (dx_ViewCoords.x*2), input.spriteVertexPos.y * "
596 "gl_PointSize / (dx_ViewCoords.y*2), input.spriteVertexPos.z) * "
597 "output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800598
Jamie Madille39a3f02015-11-17 20:42:15 -0500599 if (programMetadata.usesPointCoord())
Cooper Partine6664f02015-01-09 16:22:24 -0800600 {
Jamie Madill334d6152015-10-22 14:00:28 -0400601 vertexStream << "\n"
602 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800603 }
604 }
605
Cooper Partin7c89d242015-10-13 12:45:59 -0700606 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
607 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
608 // default value used in the generated pixel shader.
Jamie Madille39a3f02015-11-17 20:42:15 -0500609 if (programMetadata.usesInsertedPointCoordValue())
Cooper Partin7c89d242015-10-13 12:45:59 -0700610 {
611 ASSERT(!useInstancedPointSpriteEmulation);
612 vertexStream << "\n"
613 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
614 }
615
Jamie Madill334d6152015-10-22 14:00:28 -0400616 vertexStream << "\n"
617 << " return output;\n"
618 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500619
Jamie Madill334d6152015-10-22 14:00:28 -0400620 std::stringstream pixelStream;
621 pixelStream << fragmentShaderGL->getTranslatedSource();
Jamie Madill5f562732014-02-14 16:41:24 -0500622
Jamie Madille39a3f02015-11-17 20:42:15 -0500623 const SemanticInfo &pixelSemantics =
624 GetSemanticInfo(SHADER_PIXEL, programMetadata, registerCount);
Jamie Madill334d6152015-10-22 14:00:28 -0400625
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400626 pixelStream << "struct PS_INPUT\n";
627 generateVaryingLinkHLSL(*data.caps, usesPointSize, pixelSemantics, packedVaryings, pixelStream);
628 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500629
Jamie Madill334d6152015-10-22 14:00:28 -0400630 pixelStream << PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500631
Jamie Madill334d6152015-10-22 14:00:28 -0400632 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500633 {
634 if (shaderModel >= 4)
635 {
Jamie Madill334d6152015-10-22 14:00:28 -0400636 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
637 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500638 }
639 else
640 {
Jamie Madill334d6152015-10-22 14:00:28 -0400641 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
642 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500643 }
644 }
645 else
646 {
Jamie Madill334d6152015-10-22 14:00:28 -0400647 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
648 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500649 }
650
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400651 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500652 {
Jamie Madill334d6152015-10-22 14:00:28 -0400653 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500654
Austin Kinross588434c2014-12-10 10:41:45 -0800655 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400656 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
657 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800658 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500659 {
Jamie Madill334d6152015-10-22 14:00:28 -0400660 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
661 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500662 }
Austin Kinross588434c2014-12-10 10:41:45 -0800663 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500664 {
Jamie Madill334d6152015-10-22 14:00:28 -0400665 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
666 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500667 }
668 else
669 {
Jamie Madill334d6152015-10-22 14:00:28 -0400670 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
671 // Renderer::setViewport()
672 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
673 "dx_ViewCoords.z;\n"
674 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
675 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500676 }
677
Jamie Madill334d6152015-10-22 14:00:28 -0400678 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
679 "dx_DepthFront.y;\n"
680 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500681 }
682
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400683 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500684 {
Jamie Madill334d6152015-10-22 14:00:28 -0400685 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
686 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500687 }
688
Jamie Madill334d6152015-10-22 14:00:28 -0400689 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500690 {
691 if (shaderModel <= 3)
692 {
Jamie Madill334d6152015-10-22 14:00:28 -0400693 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500694 }
695 else
696 {
Jamie Madill334d6152015-10-22 14:00:28 -0400697 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500698 }
699 }
700
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400701 for (const PackedVaryingRegister &registerInfo : PackedVaryingIterator(packedVaryings))
Jamie Madill5f562732014-02-14 16:41:24 -0500702 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400703 const PackedVarying &packedVarying = packedVaryings[registerInfo.varyingIndex];
Jamie Madill4cff2472015-08-21 16:53:18 -0400704 const sh::Varying &varying = *packedVarying.varying;
705
Jamie Madillca03b352015-09-02 12:38:13 -0400706 // Don't reference VS-only transform feedback varyings in the PS.
707 if (packedVarying.vertexOnly)
708 continue;
709
Jamie Madill4cff2472015-08-21 16:53:18 -0400710 ASSERT(!varying.isBuiltIn());
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400711 GLenum transposedType = TransposeMatrixType(varying.type);
712 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
713 unsigned int registerIndex = registerInfo.registerIndex(*data.caps, packedVaryings);
714 pixelStream << " _" << varying.name;
715
716 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400717 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400718 pixelStream << ArrayString(registerInfo.elementIndex);
719 }
720
721 if (variableRows > 1)
722 {
723 pixelStream << ArrayString(registerInfo.rowIndex);
724 }
725
726 pixelStream << " = input.v" << registerIndex;
727
728 if (!varying.isStruct())
729 {
730 switch (VariableColumnCount(transposedType))
Jamie Madill5f562732014-02-14 16:41:24 -0500731 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400732 case 1:
733 pixelStream << ".x";
Jamie Madill4cff2472015-08-21 16:53:18 -0400734 break;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400735 case 2:
736 pixelStream << ".xy";
737 break;
738 case 3:
739 pixelStream << ".xyz";
740 break;
741 case 4:
742 break;
743 default:
744 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500745 }
746 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400747 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500748 }
749
Jamie Madill37997142015-01-28 10:06:34 -0500750 if (fragmentShader->usesDeferredInit())
751 {
Jamie Madill334d6152015-10-22 14:00:28 -0400752 pixelStream << "\n"
753 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500754 }
755
Jamie Madill334d6152015-10-22 14:00:28 -0400756 pixelStream << "\n"
757 << " gl_main();\n"
758 << "\n"
759 << " return generateOutput();\n"
760 << "}\n";
761
762 *vertexHLSL = vertexStream.str();
763 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500764
765 return true;
766}
767
Jamie Madill4e31ad52015-10-29 10:32:57 -0400768std::string DynamicHLSL::generateGeometryShaderPreamble(
Jamie Madill334d6152015-10-22 14:00:28 -0400769 const gl::Data &data,
Jamie Madill76f8fa62015-10-29 10:32:56 -0400770 const gl::Program::Data &programData,
Jamie Madille39a3f02015-11-17 20:42:15 -0500771 const ProgramD3DMetadata &programMetadata,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400772 unsigned int registerCount,
Jamie Madillca03b352015-09-02 12:38:13 -0400773 const std::vector<PackedVarying> &packedVaryings) const
Jamie Madill5f562732014-02-14 16:41:24 -0500774{
Jamie Madill65345da2015-11-13 11:25:23 -0500775 int majorShaderModel = mRenderer->getMajorShaderModel();
776
Jamie Madill4e31ad52015-10-29 10:32:57 -0400777 ASSERT(registerCount >= 0 && registerCount <= data.caps->maxVaryingVectors);
Jamie Madill65345da2015-11-13 11:25:23 -0500778 ASSERT(majorShaderModel >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500779
Jamie Madill4e31ad52015-10-29 10:32:57 -0400780 // Must be called during link, not from a binary load.
Jamie Madill76f8fa62015-10-29 10:32:56 -0400781 const ShaderD3D *vertexShader = GetImplAs<ShaderD3D>(programData.getAttachedVertexShader());
782 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(programData.getAttachedFragmentShader());
Jamie Madill4e31ad52015-10-29 10:32:57 -0400783 ASSERT(vertexShader && fragmentShader);
Jamie Madill76f8fa62015-10-29 10:32:56 -0400784
Jamie Madill334d6152015-10-22 14:00:28 -0400785 bool usesFragCoord = fragmentShader->usesFragCoord();
Jamie Madill76f8fa62015-10-29 10:32:56 -0400786 bool usesPointSize = vertexShader->usesPointSize();
787
Jamie Madille39a3f02015-11-17 20:42:15 -0500788 const SemanticInfo &inSemantics =
789 GetSemanticInfo(SHADER_VERTEX, programMetadata, registerCount);
Jamie Madill65345da2015-11-13 11:25:23 -0500790 const SemanticInfo &outSemantics =
Jamie Madille39a3f02015-11-17 20:42:15 -0500791 GetSemanticInfo(SHADER_GEOMETRY, programMetadata, registerCount);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400792
Jamie Madill4e31ad52015-10-29 10:32:57 -0400793 std::stringstream preambleStream;
794
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400795 preambleStream << "struct GS_INPUT\n";
796 generateVaryingLinkHLSL(*data.caps, usesPointSize, inSemantics, packedVaryings, preambleStream);
797 preambleStream << "\n"
798 << "struct GS_OUTPUT\n";
799 generateVaryingLinkHLSL(*data.caps, usesPointSize, outSemantics, packedVaryings,
800 preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400801 preambleStream
802 << "\n"
803 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
804 << "{\n"
805 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400806
807 if (usesPointSize)
808 {
809 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
810 }
811
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400812 for (const PackedVaryingRegister &varyingRegister : PackedVaryingIterator(packedVaryings))
Jamie Madill4e31ad52015-10-29 10:32:57 -0400813 {
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400814 const sh::Varying &varying = *packedVaryings[varyingRegister.varyingIndex].varying;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400815 unsigned int registerIndex = varyingRegister.registerIndex(*data.caps, packedVaryings);
816
817 preambleStream << " output.v" << registerIndex << " = ";
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400818 if (varying.interpolation == sh::INTERPOLATION_FLAT)
819 {
820 preambleStream << "flat";
821 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400822 preambleStream << "input.v" << registerIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400823 }
824
825 if (usesFragCoord)
826 {
827 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
828 }
829
830 // Only write the dx_Position if we aren't using point sprites
831 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
832 << " output.dx_Position = input.dx_Position;\n"
833 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
834 << "}\n";
835
836 return preambleStream.str();
837}
838
839std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
840 const gl::Data &data,
841 const gl::Program::Data &programData,
842 const std::string &preambleString) const
843{
844 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500845
Jamie Madill334d6152015-10-22 14:00:28 -0400846 std::stringstream shaderStream;
847
Jamie Madill4e31ad52015-10-29 10:32:57 -0400848 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
849 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500850
Jamie Madill76f8fa62015-10-29 10:32:56 -0400851 const char *inputPT = nullptr;
852 const char *outputPT = nullptr;
853 int inputSize = 0;
854 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500855
Jamie Madill76f8fa62015-10-29 10:32:56 -0400856 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500857 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400858 case PRIMITIVE_POINTS:
859 inputPT = "point";
860 outputPT = "Triangle";
861 inputSize = 1;
862 maxVertexOutput = 4;
863 break;
864
865 case PRIMITIVE_LINES:
866 case PRIMITIVE_LINE_STRIP:
867 case PRIMITIVE_LINE_LOOP:
868 inputPT = "line";
869 outputPT = "Line";
870 inputSize = 2;
871 maxVertexOutput = 2;
872 break;
873
874 case PRIMITIVE_TRIANGLES:
875 case PRIMITIVE_TRIANGLE_STRIP:
876 case PRIMITIVE_TRIANGLE_FAN:
877 inputPT = "triangle";
878 outputPT = "Triangle";
879 inputSize = 3;
880 maxVertexOutput = 3;
881 break;
882
883 default:
884 UNREACHABLE();
885 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500886 }
887
Jamie Madill76f8fa62015-10-29 10:32:56 -0400888 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500889 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400890 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
891 "\n"
892 "uniform float4 dx_ViewCoords : register(c1);\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400893 "\n"
894 "static float2 pointSpriteCorners[] = \n"
895 "{\n"
896 " float2( 0.5f, -0.5f),\n"
897 " float2( 0.5f, 0.5f),\n"
898 " float2(-0.5f, -0.5f),\n"
899 " float2(-0.5f, 0.5f)\n"
900 "};\n"
901 "\n"
902 "static float2 pointSpriteTexcoords[] = \n"
903 "{\n"
904 " float2(1.0f, 1.0f),\n"
905 " float2(1.0f, 0.0f),\n"
906 " float2(0.0f, 1.0f),\n"
907 " float2(0.0f, 0.0f)\n"
908 "};\n"
909 "\n"
910 "static float minPointSize = "
911 << static_cast<int>(data.caps->minAliasedPointSize)
912 << ".0f;\n"
913 "static float maxPointSize = "
914 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n"
915 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500916 }
917
Jamie Madill4e31ad52015-10-29 10:32:57 -0400918 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400919 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400920 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
921
922 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
923 {
924 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
925 }
926
927 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400928 << "{\n"
929 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500930
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400931 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
932 {
933 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
934 }
935 else
936 {
937 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
938 }
939
Jamie Madill4e31ad52015-10-29 10:32:57 -0400940 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -0500941 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400942 shaderStream << " copyVertex(output, input[" << vertexIndex
943 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400944
945 if (!pointSprites)
946 {
947 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400948 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -0400949 }
950 }
951
952 if (pointSprites)
953 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400954 shaderStream << "\n"
955 " float4 dx_Position = input[0].dx_Position;\n"
956 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
957 "maxPointSize);\n"
958 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
959 "dx_ViewCoords.y) * dx_Position.w;\n";
960
961 for (int corner = 0; corner < 4; corner++)
962 {
963 shaderStream << "\n"
964 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
965 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
966
967 if (usesPointCoord)
968 {
969 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
970 << "];\n";
971 }
972
973 shaderStream << " outStream.Append(output);\n";
974 }
Jamie Madill5f562732014-02-14 16:41:24 -0500975 }
976
Jamie Madill334d6152015-10-22 14:00:28 -0400977 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400978 " outStream.RestartStrip();\n"
979 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500980
Jamie Madill334d6152015-10-22 14:00:28 -0400981 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500982}
983
984// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -0400985std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -0500986{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400987 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -0500988 {
989 return "_" + name;
990 }
991
992 return name;
993}
994
Jamie Madill334d6152015-10-22 14:00:28 -0400995std::string DynamicHLSL::generateAttributeConversionHLSL(
996 gl::VertexFormatType vertexFormatType,
997 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500998{
Jamie Madilld3dfda22015-07-06 08:28:49 -0400999 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -04001000 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001001
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001002 // Matrix
1003 if (IsMatrixType(shaderAttrib.type))
1004 {
Jamie Madill8664b062014-02-14 16:41:29 -05001005 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001006 }
1007
Jamie Madillf2575982014-06-25 16:04:54 -04001008 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001009 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001010
Jamie Madill8664b062014-02-14 16:41:29 -05001011 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -04001012 bool requiresTypeConversion =
1013 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001014
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001015 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001016 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001017 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001018 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001019 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001020 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001021
1022 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001023 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001024}
Jamie Madille39a3f02015-11-17 20:42:15 -05001025
1026void DynamicHLSL::getPixelShaderOutputKey(const gl::Data &data,
1027 const gl::Program::Data &programData,
1028 const ProgramD3DMetadata &metadata,
1029 std::vector<PixelShaderOutputVariable> *outPixelShaderKey)
1030{
1031 // Two cases when writing to gl_FragColor and using ESSL 1.0:
1032 // - with a 3.0 context, the output color is copied to channel 0
1033 // - with a 2.0 context, the output color is broadcast to all channels
1034 bool broadcast = metadata.usesBroadcast(data);
1035 const unsigned int numRenderTargets =
1036 (broadcast || metadata.usesMultipleFragmentOuts() ? data.caps->maxDrawBuffers : 1);
1037
1038 if (metadata.getMajorShaderVersion() < 300)
1039 {
1040 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
1041 renderTargetIndex++)
1042 {
1043 PixelShaderOutputVariable outputKeyVariable;
1044 outputKeyVariable.type = GL_FLOAT_VEC4;
1045 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
1046 outputKeyVariable.source =
1047 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
1048 outputKeyVariable.outputIndex = renderTargetIndex;
1049
1050 outPixelShaderKey->push_back(outputKeyVariable);
1051 }
1052 }
1053 else
1054 {
1055 const auto &shaderOutputVars =
1056 metadata.getFragmentShader()->getData().getActiveOutputVariables();
1057
1058 for (auto outputPair : programData.getOutputVariables())
1059 {
1060 const VariableLocation &outputLocation = outputPair.second;
1061 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
1062 const std::string &variableName = "out_" + outputLocation.name;
1063 const std::string &elementString =
1064 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
1065
1066 ASSERT(outputVariable.staticUse);
1067
1068 PixelShaderOutputVariable outputKeyVariable;
1069 outputKeyVariable.type = outputVariable.type;
1070 outputKeyVariable.name = variableName + elementString;
1071 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
1072 outputKeyVariable.outputIndex = outputPair.first;
1073
1074 outPixelShaderKey->push_back(outputKeyVariable);
1075 }
1076 }
1077}
1078
Jamie Madill334d6152015-10-22 14:00:28 -04001079} // namespace rx