blob: 064040759f96a5e21848d9a1b9ad4e8a4702dd0a [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 Madillada9ecc2015-08-17 12:53:37 -0400447 InfoLog &infoLog,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400448 unsigned int registerCount,
Jamie Madill334d6152015-10-22 14:00:28 -0400449 std::string *pixelHLSL,
450 std::string *vertexHLSL,
Jamie Madillca03b352015-09-02 12:38:13 -0400451 const std::vector<PackedVarying> &packedVaryings,
Jamie Madill28afae52015-11-09 15:07:57 -0500452 std::vector<D3DVarying> *d3dVaryingsOut,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400453 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400454 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500455{
Jamie Madill334d6152015-10-22 14:00:28 -0400456 ASSERT(pixelHLSL->empty() && vertexHLSL->empty());
Jamie Madill5f562732014-02-14 16:41:24 -0500457
Jamie Madill91445bc2015-09-23 16:47:53 -0400458 const gl::Shader *vertexShaderGL = programData.getAttachedVertexShader();
459 const ShaderD3D *vertexShader = GetImplAs<ShaderD3D>(vertexShaderGL);
460 const gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader();
461 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL);
Cooper Partin7c89d242015-10-13 12:45:59 -0700462 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400463
Jamie Madill334d6152015-10-22 14:00:28 -0400464 bool usesMRT = fragmentShader->usesMultipleRenderTargets();
465 bool usesFragCoord = fragmentShader->usesFragCoord();
466 bool usesPointCoord = fragmentShader->usesPointCoord();
467 bool usesPointSize = vertexShader->usesPointSize();
468 bool useInstancedPointSpriteEmulation =
469 usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Cooper Partin7c89d242015-10-13 12:45:59 -0700470 bool insertDummyPointCoordValue = !usesPointSize && usesPointCoord && shaderModel >= 4;
471 bool addPointCoord =
472 (useInstancedPointSpriteEmulation && usesPointCoord) || insertDummyPointCoordValue;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400473
Jamie Madill14e95b32015-05-07 10:10:41 -0400474 // Validation done in the compiler
Jamie Madill334d6152015-10-22 14:00:28 -0400475 ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData());
Jamie Madill5f562732014-02-14 16:41:24 -0500476
477 // Write the HLSL input/output declarations
Jamie Madill4e31ad52015-10-29 10:32:57 -0400478 const unsigned int registersNeeded =
479 registerCount + (usesFragCoord ? 1u : 0u) + (usesPointCoord ? 1u : 0u);
Jamie Madill5f562732014-02-14 16:41:24 -0500480
481 // Two cases when writing to gl_FragColor and using ESSL 1.0:
482 // - with a 3.0 context, the output color is copied to channel 0
483 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madill334d6152015-10-22 14:00:28 -0400484 const bool broadcast = (fragmentShader->usesFragColor() && data.clientVersion < 3);
Jamie Madillde8892b2014-11-11 13:00:22 -0500485 const unsigned int numRenderTargets = (broadcast || usesMRT ? data.caps->maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500486
Jamie Madill334d6152015-10-22 14:00:28 -0400487 // gl_Position only needs to be outputted from the vertex shader if transform feedback is
488 // active. This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from
489 // the vertex shader in this case. This saves us 1 output vector.
Austin Kinross8b695ee2015-03-12 13:12:20 -0700490 bool outputPositionFromVS = !(shaderModel >= 4 && mRenderer->getShaderModelSuffix() != "");
491
Jamie Madill91445bc2015-09-23 16:47:53 -0400492 int shaderVersion = vertexShaderGL->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500493
Jamie Madillde8892b2014-11-11 13:00:22 -0500494 if (static_cast<GLuint>(registersNeeded) > data.caps->maxVaryingVectors)
Jamie Madill5f562732014-02-14 16:41:24 -0500495 {
Jamie Madillf6113162015-05-07 11:49:21 -0400496 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madill5f562732014-02-14 16:41:24 -0500497 return false;
498 }
499
Jamie Madill334d6152015-10-22 14:00:28 -0400500 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader
501 // VS_OUTPUT structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
Cooper Partine6664f02015-01-09 16:22:24 -0800502 // GeometryShader PointSprite emulation does not require this additional entry because the
Jamie Madill334d6152015-10-22 14:00:28 -0400503 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the
504 // PS_INPUT of the generated pixel shader. The Geometry Shader point sprite implementation needs
505 // gl_PointSize to be in VS_OUTPUT and GS_INPUT. Instanced point sprites doesn't need
506 // gl_PointSize in VS_OUTPUT.
Jamie Madill65345da2015-11-13 11:25:23 -0500507 const SemanticInfo &vertexSemantics = GetSemanticInfo(
508 SHADER_VERTEX, shaderModel, registerCount, outputPositionFromVS, usesFragCoord,
509 addPointCoord, (!useInstancedPointSpriteEmulation && usesPointSize));
Jamie Madill5f562732014-02-14 16:41:24 -0500510
Jamie Madill28afae52015-11-09 15:07:57 -0500511 storeUserVaryings(packedVaryings, usesPointSize, d3dVaryingsOut);
512 storeBuiltinVaryings(vertexSemantics, d3dVaryingsOut);
Jamie Madill5f562732014-02-14 16:41:24 -0500513
Jamie Madill334d6152015-10-22 14:00:28 -0400514 std::stringstream vertexStream;
515 vertexStream << vertexShaderGL->getTranslatedSource();
516
Cooper Partine6664f02015-01-09 16:22:24 -0800517 // Instanced PointSprite emulation requires additional entries originally generated in the
Jamie Madill334d6152015-10-22 14:00:28 -0400518 // GeometryShader HLSL. These include pointsize clamp values.
Cooper Partine6664f02015-01-09 16:22:24 -0800519 if (useInstancedPointSpriteEmulation)
520 {
Jamie Madill334d6152015-10-22 14:00:28 -0400521 vertexStream << "static float minPointSize = "
522 << static_cast<int>(data.caps->minAliasedPointSize) << ".0f;\n"
523 << "static float maxPointSize = "
524 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800525 }
526
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500527 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madill334d6152015-10-22 14:00:28 -0400528 vertexStream << "\n" << VERTEX_ATTRIBUTE_STUB_STRING + "\n"
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400529 << "struct VS_OUTPUT\n";
530 generateVaryingLinkHLSL(*data.caps, usesPointSize, vertexSemantics, packedVaryings,
531 vertexStream);
532 vertexStream << "\n"
Jamie Madill334d6152015-10-22 14:00:28 -0400533 << "VS_OUTPUT main(VS_INPUT input)\n"
534 << "{\n"
535 << " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500536
Jamie Madill37997142015-01-28 10:06:34 -0500537 if (vertexShader->usesDeferredInit())
538 {
Jamie Madill334d6152015-10-22 14:00:28 -0400539 vertexStream << "\n"
540 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500541 }
542
Jamie Madill334d6152015-10-22 14:00:28 -0400543 vertexStream << "\n"
544 << " gl_main();\n"
545 << "\n"
546 << " VS_OUTPUT output;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700547
548 if (outputPositionFromVS)
549 {
Jamie Madill334d6152015-10-22 14:00:28 -0400550 vertexStream << " output.gl_Position = gl_Position;\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700551 }
Jamie Madill37997142015-01-28 10:06:34 -0500552
Austin Kinross4fd18b12014-12-22 12:32:05 -0800553 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
Jamie Madill334d6152015-10-22 14:00:28 -0400554 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500555 {
Jamie Madill334d6152015-10-22 14:00:28 -0400556 vertexStream << " output.dx_Position.x = gl_Position.x;\n"
557 << " output.dx_Position.y = -gl_Position.y;\n"
558 << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
559 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500560 }
561 else
562 {
Jamie Madill334d6152015-10-22 14:00:28 -0400563 vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + "
564 "dx_ViewAdjust.x * gl_Position.w;\n"
565 << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + "
566 "dx_ViewAdjust.y * gl_Position.w);\n"
567 << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
568 << " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500569 }
570
Austin Kinross8b695ee2015-03-12 13:12:20 -0700571 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
572 if (usesPointSize && shaderModel >= 3 && !useInstancedPointSpriteEmulation)
Jamie Madill5f562732014-02-14 16:41:24 -0500573 {
Jamie Madill334d6152015-10-22 14:00:28 -0400574 vertexStream << " output.gl_PointSize = gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500575 }
576
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400577 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500578 {
Jamie Madill334d6152015-10-22 14:00:28 -0400579 vertexStream << " output.gl_FragCoord = gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500580 }
581
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400582 for (const PackedVaryingRegister &registerInfo : PackedVaryingIterator(packedVaryings))
Jamie Madill5f562732014-02-14 16:41:24 -0500583 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400584 const PackedVarying &packedVarying = packedVaryings[registerInfo.varyingIndex];
Jamie Madill4cff2472015-08-21 16:53:18 -0400585 const sh::Varying &varying = *packedVarying.varying;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400586 GLenum transposedType = TransposeMatrixType(varying.type);
587 unsigned int variableRows =
588 static_cast<unsigned int>(varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill4cff2472015-08-21 16:53:18 -0400589
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400590 int r = registerInfo.registerIndex(*data.caps, packedVaryings);
591 vertexStream << " output.v" << r << " = _" + varying.name;
592
593 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400594 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400595 vertexStream << ArrayString(registerInfo.elementIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500596 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400597
598 if (variableRows > 1)
599 {
600 vertexStream << ArrayString(registerInfo.rowIndex);
601 }
602
603 vertexStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500604 }
605
Cooper Partine6664f02015-01-09 16:22:24 -0800606 // Instanced PointSprite emulation requires additional entries to calculate
607 // the final output vertex positions of the quad that represents each sprite.
608 if (useInstancedPointSpriteEmulation)
609 {
Jamie Madill334d6152015-10-22 14:00:28 -0400610 vertexStream << "\n"
611 << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"
612 << " output.dx_Position.xyz += float3(input.spriteVertexPos.x * "
613 "gl_PointSize / (dx_ViewCoords.x*2), input.spriteVertexPos.y * "
614 "gl_PointSize / (dx_ViewCoords.y*2), input.spriteVertexPos.z) * "
615 "output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800616
617 if (usesPointCoord)
618 {
Jamie Madill334d6152015-10-22 14:00:28 -0400619 vertexStream << "\n"
620 << " output.gl_PointCoord = input.spriteTexCoord;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800621 }
622 }
623
Cooper Partin7c89d242015-10-13 12:45:59 -0700624 // Renderers that enable instanced pointsprite emulation require the vertex shader output member
625 // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same
626 // default value used in the generated pixel shader.
627 if (insertDummyPointCoordValue)
628 {
629 ASSERT(!useInstancedPointSpriteEmulation);
630 vertexStream << "\n"
631 << " output.gl_PointCoord = float2(0.5, 0.5);\n";
632 }
633
Jamie Madill334d6152015-10-22 14:00:28 -0400634 vertexStream << "\n"
635 << " return output;\n"
636 << "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500637
Jamie Madill334d6152015-10-22 14:00:28 -0400638 std::stringstream pixelStream;
639 pixelStream << fragmentShaderGL->getTranslatedSource();
Jamie Madill5f562732014-02-14 16:41:24 -0500640
Jamie Madill65345da2015-11-13 11:25:23 -0500641 const SemanticInfo &pixelSemantics = GetSemanticInfo(
642 SHADER_PIXEL, shaderModel, registerCount, outputPositionFromVS, usesFragCoord,
643 usesPointCoord, (!useInstancedPointSpriteEmulation && usesPointSize));
Jamie Madill334d6152015-10-22 14:00:28 -0400644
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400645 pixelStream << "struct PS_INPUT\n";
646 generateVaryingLinkHLSL(*data.caps, usesPointSize, pixelSemantics, packedVaryings, pixelStream);
647 pixelStream << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500648
649 if (shaderVersion < 300)
650 {
Jamie Madill334d6152015-10-22 14:00:28 -0400651 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets;
652 renderTargetIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500653 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400654 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400655 outputKeyVariable.type = GL_FLOAT_VEC4;
656 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
Jamie Madill334d6152015-10-22 14:00:28 -0400657 outputKeyVariable.source =
658 broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400659 outputKeyVariable.outputIndex = renderTargetIndex;
660
661 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500662 }
663
Jamie Madill334d6152015-10-22 14:00:28 -0400664 *outUsesFragDepth = fragmentShader->usesFragDepth();
Jamie Madill5f562732014-02-14 16:41:24 -0500665 }
666 else
667 {
Jamie Madill91445bc2015-09-23 16:47:53 -0400668 const auto &shaderOutputVars = fragmentShaderGL->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500669
Jamie Madilla0a9e122015-09-02 15:54:30 -0400670 for (auto outputPair : programData.getOutputVariables())
Jamie Madill5f562732014-02-14 16:41:24 -0500671 {
Jamie Madill80a6fc02015-08-21 16:53:16 -0400672 const VariableLocation &outputLocation = outputPair.second;
Jamie Madillf2575982014-06-25 16:04:54 -0400673 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400674 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill334d6152015-10-22 14:00:28 -0400675 const std::string &elementString =
676 (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
Jamie Madill5f562732014-02-14 16:41:24 -0500677
Jamie Madill54ad4f82014-09-03 09:40:46 -0400678 ASSERT(outputVariable.staticUse);
679
Jamie Madillf6be8d72014-09-05 10:38:07 -0400680 PixelShaderOutputVariable outputKeyVariable;
Jamie Madill334d6152015-10-22 14:00:28 -0400681 outputKeyVariable.type = outputVariable.type;
682 outputKeyVariable.name = variableName + elementString;
683 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400684 outputKeyVariable.outputIndex = outputPair.first;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400685
686 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500687 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400688
689 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500690 }
691
Jamie Madill334d6152015-10-22 14:00:28 -0400692 pixelStream << PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500693
Jamie Madill334d6152015-10-22 14:00:28 -0400694 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500695 {
696 if (shaderModel >= 4)
697 {
Jamie Madill334d6152015-10-22 14:00:28 -0400698 pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
699 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500700 }
701 else
702 {
Jamie Madill334d6152015-10-22 14:00:28 -0400703 pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
704 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500705 }
706 }
707 else
708 {
Jamie Madill334d6152015-10-22 14:00:28 -0400709 pixelStream << "PS_OUTPUT main(PS_INPUT input)\n"
710 << "{\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500711 }
712
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400713 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500714 {
Jamie Madill334d6152015-10-22 14:00:28 -0400715 pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500716
Austin Kinross588434c2014-12-10 10:41:45 -0800717 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
Jamie Madill334d6152015-10-22 14:00:28 -0400718 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using
719 // dx_ViewCoords.
Austin Kinross588434c2014-12-10 10:41:45 -0800720 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500721 {
Jamie Madill334d6152015-10-22 14:00:28 -0400722 pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n"
723 << " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500724 }
Austin Kinross588434c2014-12-10 10:41:45 -0800725 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500726 {
Jamie Madill334d6152015-10-22 14:00:28 -0400727 pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
728 << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500729 }
730 else
731 {
Jamie Madill334d6152015-10-22 14:00:28 -0400732 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See
733 // Renderer::setViewport()
734 pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + "
735 "dx_ViewCoords.z;\n"
736 << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + "
737 "dx_ViewCoords.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500738 }
739
Jamie Madill334d6152015-10-22 14:00:28 -0400740 pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + "
741 "dx_DepthFront.y;\n"
742 << " gl_FragCoord.w = rhw;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500743 }
744
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400745 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500746 {
Jamie Madill334d6152015-10-22 14:00:28 -0400747 pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n"
748 << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500749 }
750
Jamie Madill334d6152015-10-22 14:00:28 -0400751 if (fragmentShader->usesFrontFacing())
Jamie Madill5f562732014-02-14 16:41:24 -0500752 {
753 if (shaderModel <= 3)
754 {
Jamie Madill334d6152015-10-22 14:00:28 -0400755 pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500756 }
757 else
758 {
Jamie Madill334d6152015-10-22 14:00:28 -0400759 pixelStream << " gl_FrontFacing = isFrontFace;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500760 }
761 }
762
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400763 for (const PackedVaryingRegister &registerInfo : PackedVaryingIterator(packedVaryings))
Jamie Madill5f562732014-02-14 16:41:24 -0500764 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400765 const PackedVarying &packedVarying = packedVaryings[registerInfo.varyingIndex];
Jamie Madill4cff2472015-08-21 16:53:18 -0400766 const sh::Varying &varying = *packedVarying.varying;
767
Jamie Madillca03b352015-09-02 12:38:13 -0400768 // Don't reference VS-only transform feedback varyings in the PS.
769 if (packedVarying.vertexOnly)
770 continue;
771
Jamie Madill4cff2472015-08-21 16:53:18 -0400772 ASSERT(!varying.isBuiltIn());
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400773 GLenum transposedType = TransposeMatrixType(varying.type);
774 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
775 unsigned int registerIndex = registerInfo.registerIndex(*data.caps, packedVaryings);
776 pixelStream << " _" << varying.name;
777
778 if (varying.isArray())
Jamie Madill4cff2472015-08-21 16:53:18 -0400779 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400780 pixelStream << ArrayString(registerInfo.elementIndex);
781 }
782
783 if (variableRows > 1)
784 {
785 pixelStream << ArrayString(registerInfo.rowIndex);
786 }
787
788 pixelStream << " = input.v" << registerIndex;
789
790 if (!varying.isStruct())
791 {
792 switch (VariableColumnCount(transposedType))
Jamie Madill5f562732014-02-14 16:41:24 -0500793 {
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400794 case 1:
795 pixelStream << ".x";
Jamie Madill4cff2472015-08-21 16:53:18 -0400796 break;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400797 case 2:
798 pixelStream << ".xy";
799 break;
800 case 3:
801 pixelStream << ".xyz";
802 break;
803 case 4:
804 break;
805 default:
806 UNREACHABLE();
Jamie Madill5f562732014-02-14 16:41:24 -0500807 }
808 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400809 pixelStream << ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500810 }
811
Jamie Madill37997142015-01-28 10:06:34 -0500812 if (fragmentShader->usesDeferredInit())
813 {
Jamie Madill334d6152015-10-22 14:00:28 -0400814 pixelStream << "\n"
815 << " initializeDeferredGlobals();\n";
Jamie Madill37997142015-01-28 10:06:34 -0500816 }
817
Jamie Madill334d6152015-10-22 14:00:28 -0400818 pixelStream << "\n"
819 << " gl_main();\n"
820 << "\n"
821 << " return generateOutput();\n"
822 << "}\n";
823
824 *vertexHLSL = vertexStream.str();
825 *pixelHLSL = pixelStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -0500826
827 return true;
828}
829
Jamie Madill4e31ad52015-10-29 10:32:57 -0400830std::string DynamicHLSL::generateGeometryShaderPreamble(
Jamie Madill334d6152015-10-22 14:00:28 -0400831 const gl::Data &data,
Jamie Madill76f8fa62015-10-29 10:32:56 -0400832 const gl::Program::Data &programData,
Jamie Madill4e31ad52015-10-29 10:32:57 -0400833 unsigned int registerCount,
Jamie Madillca03b352015-09-02 12:38:13 -0400834 const std::vector<PackedVarying> &packedVaryings) const
Jamie Madill5f562732014-02-14 16:41:24 -0500835{
Jamie Madill65345da2015-11-13 11:25:23 -0500836 int majorShaderModel = mRenderer->getMajorShaderModel();
837
Jamie Madill4e31ad52015-10-29 10:32:57 -0400838 ASSERT(registerCount >= 0 && registerCount <= data.caps->maxVaryingVectors);
Jamie Madill65345da2015-11-13 11:25:23 -0500839 ASSERT(majorShaderModel >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500840
Jamie Madill4e31ad52015-10-29 10:32:57 -0400841 // Must be called during link, not from a binary load.
Jamie Madill76f8fa62015-10-29 10:32:56 -0400842 const ShaderD3D *vertexShader = GetImplAs<ShaderD3D>(programData.getAttachedVertexShader());
843 const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(programData.getAttachedFragmentShader());
Jamie Madill4e31ad52015-10-29 10:32:57 -0400844 ASSERT(vertexShader && fragmentShader);
Jamie Madill76f8fa62015-10-29 10:32:56 -0400845
Jamie Madill334d6152015-10-22 14:00:28 -0400846 bool usesFragCoord = fragmentShader->usesFragCoord();
847 bool usesPointCoord = fragmentShader->usesPointCoord();
Jamie Madill76f8fa62015-10-29 10:32:56 -0400848 bool usesPointSize = vertexShader->usesPointSize();
849
Jamie Madill65345da2015-11-13 11:25:23 -0500850 const SemanticInfo &inSemantics = GetSemanticInfo(
851 SHADER_VERTEX, majorShaderModel, registerCount, true, usesFragCoord, false, usesPointSize);
852 const SemanticInfo &outSemantics =
853 GetSemanticInfo(SHADER_GEOMETRY, majorShaderModel, registerCount, true, usesFragCoord,
854 usesPointCoord, usesPointSize);
Jamie Madill4e31ad52015-10-29 10:32:57 -0400855
Jamie Madill4e31ad52015-10-29 10:32:57 -0400856 std::stringstream preambleStream;
857
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400858 preambleStream << "struct GS_INPUT\n";
859 generateVaryingLinkHLSL(*data.caps, usesPointSize, inSemantics, packedVaryings, preambleStream);
860 preambleStream << "\n"
861 << "struct GS_OUTPUT\n";
862 generateVaryingLinkHLSL(*data.caps, usesPointSize, outSemantics, packedVaryings,
863 preambleStream);
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400864 preambleStream
865 << "\n"
866 << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n"
867 << "{\n"
868 << " output.gl_Position = input.gl_Position;\n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400869
870 if (usesPointSize)
871 {
872 preambleStream << " output.gl_PointSize = input.gl_PointSize;\n";
873 }
874
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400875 for (const PackedVaryingRegister &varyingRegister : PackedVaryingIterator(packedVaryings))
Jamie Madill4e31ad52015-10-29 10:32:57 -0400876 {
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400877 const sh::Varying &varying = *packedVaryings[varyingRegister.varyingIndex].varying;
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400878 unsigned int registerIndex = varyingRegister.registerIndex(*data.caps, packedVaryings);
879
880 preambleStream << " output.v" << registerIndex << " = ";
Jamie Madill3e14e2b2015-10-29 14:38:53 -0400881 if (varying.interpolation == sh::INTERPOLATION_FLAT)
882 {
883 preambleStream << "flat";
884 }
Jamie Madillf4b2c4f2015-10-29 14:38:54 -0400885 preambleStream << "input.v" << registerIndex << "; \n";
Jamie Madill4e31ad52015-10-29 10:32:57 -0400886 }
887
888 if (usesFragCoord)
889 {
890 preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n";
891 }
892
893 // Only write the dx_Position if we aren't using point sprites
894 preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n"
895 << " output.dx_Position = input.dx_Position;\n"
896 << "#endif // ANGLE_POINT_SPRITE_SHADER\n"
897 << "}\n";
898
899 return preambleStream.str();
900}
901
902std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType,
903 const gl::Data &data,
904 const gl::Program::Data &programData,
905 const std::string &preambleString) const
906{
907 ASSERT(mRenderer->getMajorShaderModel() >= 4);
Jamie Madill5f562732014-02-14 16:41:24 -0500908
Jamie Madill334d6152015-10-22 14:00:28 -0400909 std::stringstream shaderStream;
910
Jamie Madill4e31ad52015-10-29 10:32:57 -0400911 const bool pointSprites = (primitiveType == PRIMITIVE_POINTS);
912 const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos;
Jamie Madill5f562732014-02-14 16:41:24 -0500913
Jamie Madill76f8fa62015-10-29 10:32:56 -0400914 const char *inputPT = nullptr;
915 const char *outputPT = nullptr;
916 int inputSize = 0;
917 int maxVertexOutput = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500918
Jamie Madill76f8fa62015-10-29 10:32:56 -0400919 switch (primitiveType)
Jamie Madill5f562732014-02-14 16:41:24 -0500920 {
Jamie Madill76f8fa62015-10-29 10:32:56 -0400921 case PRIMITIVE_POINTS:
922 inputPT = "point";
923 outputPT = "Triangle";
924 inputSize = 1;
925 maxVertexOutput = 4;
926 break;
927
928 case PRIMITIVE_LINES:
929 case PRIMITIVE_LINE_STRIP:
930 case PRIMITIVE_LINE_LOOP:
931 inputPT = "line";
932 outputPT = "Line";
933 inputSize = 2;
934 maxVertexOutput = 2;
935 break;
936
937 case PRIMITIVE_TRIANGLES:
938 case PRIMITIVE_TRIANGLE_STRIP:
939 case PRIMITIVE_TRIANGLE_FAN:
940 inputPT = "triangle";
941 outputPT = "Triangle";
942 inputSize = 3;
943 maxVertexOutput = 3;
944 break;
945
946 default:
947 UNREACHABLE();
948 break;
Jamie Madill5f562732014-02-14 16:41:24 -0500949 }
950
Jamie Madill76f8fa62015-10-29 10:32:56 -0400951 if (pointSprites)
Jamie Madill5f562732014-02-14 16:41:24 -0500952 {
Jamie Madill4e31ad52015-10-29 10:32:57 -0400953 shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n"
954 "\n"
955 "uniform float4 dx_ViewCoords : register(c1);\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400956 "\n"
957 "static float2 pointSpriteCorners[] = \n"
958 "{\n"
959 " float2( 0.5f, -0.5f),\n"
960 " float2( 0.5f, 0.5f),\n"
961 " float2(-0.5f, -0.5f),\n"
962 " float2(-0.5f, 0.5f)\n"
963 "};\n"
964 "\n"
965 "static float2 pointSpriteTexcoords[] = \n"
966 "{\n"
967 " float2(1.0f, 1.0f),\n"
968 " float2(1.0f, 0.0f),\n"
969 " float2(0.0f, 1.0f),\n"
970 " float2(0.0f, 0.0f)\n"
971 "};\n"
972 "\n"
973 "static float minPointSize = "
974 << static_cast<int>(data.caps->minAliasedPointSize)
975 << ".0f;\n"
976 "static float maxPointSize = "
977 << static_cast<int>(data.caps->maxAliasedPointSize) << ".0f;\n"
978 << "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500979 }
980
Jamie Madill4e31ad52015-10-29 10:32:57 -0400981 shaderStream << preambleString << "\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400982 << "[maxvertexcount(" << maxVertexOutput << ")]\n"
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400983 << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], ";
984
985 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
986 {
987 shaderStream << "uint primitiveID : SV_PrimitiveID, ";
988 }
989
990 shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n"
Jamie Madill76f8fa62015-10-29 10:32:56 -0400991 << "{\n"
992 << " GS_OUTPUT output = (GS_OUTPUT)0;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500993
Jamie Madillc7a92fd2015-10-29 10:08:09 -0400994 if (primitiveType == PRIMITIVE_TRIANGLE_STRIP)
995 {
996 shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n";
997 }
998 else
999 {
1000 shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n";
1001 }
1002
Jamie Madill4e31ad52015-10-29 10:32:57 -04001003 for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex)
Jamie Madill5f562732014-02-14 16:41:24 -05001004 {
Jamie Madillc7a92fd2015-10-29 10:08:09 -04001005 shaderStream << " copyVertex(output, input[" << vertexIndex
1006 << "], input[lastVertexIndex]);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001007
1008 if (!pointSprites)
1009 {
1010 ASSERT(inputSize == maxVertexOutput);
Jamie Madill4e31ad52015-10-29 10:32:57 -04001011 shaderStream << " outStream.Append(output);\n";
Jamie Madill76f8fa62015-10-29 10:32:56 -04001012 }
1013 }
1014
1015 if (pointSprites)
1016 {
Jamie Madill76f8fa62015-10-29 10:32:56 -04001017 shaderStream << "\n"
1018 " float4 dx_Position = input[0].dx_Position;\n"
1019 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, "
1020 "maxPointSize);\n"
1021 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / "
1022 "dx_ViewCoords.y) * dx_Position.w;\n";
1023
1024 for (int corner = 0; corner < 4; corner++)
1025 {
1026 shaderStream << "\n"
1027 " output.dx_Position = dx_Position + float4(pointSpriteCorners["
1028 << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1029
1030 if (usesPointCoord)
1031 {
1032 shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner
1033 << "];\n";
1034 }
1035
1036 shaderStream << " outStream.Append(output);\n";
1037 }
Jamie Madill5f562732014-02-14 16:41:24 -05001038 }
1039
Jamie Madill334d6152015-10-22 14:00:28 -04001040 shaderStream << " \n"
Jamie Madill76f8fa62015-10-29 10:32:56 -04001041 " outStream.RestartStrip();\n"
1042 "}\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001043
Jamie Madill334d6152015-10-22 14:00:28 -04001044 return shaderStream.str();
Jamie Madill5f562732014-02-14 16:41:24 -05001045}
1046
1047// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001048std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001049{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001050 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001051 {
1052 return "_" + name;
1053 }
1054
1055 return name;
1056}
1057
Jamie Madill334d6152015-10-22 14:00:28 -04001058std::string DynamicHLSL::generateAttributeConversionHLSL(
1059 gl::VertexFormatType vertexFormatType,
1060 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001061{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001062 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madill334d6152015-10-22 14:00:28 -04001063 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001064
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001065 // Matrix
1066 if (IsMatrixType(shaderAttrib.type))
1067 {
Jamie Madill8664b062014-02-14 16:41:29 -05001068 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001069 }
1070
Jamie Madillf2575982014-06-25 16:04:54 -04001071 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
Jamie Madill334d6152015-10-22 14:00:28 -04001072 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001073
Jamie Madill8664b062014-02-14 16:41:29 -05001074 // Perform integer to float conversion (if necessary)
Jamie Madill334d6152015-10-22 14:00:28 -04001075 bool requiresTypeConversion =
1076 (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001077
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001078 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001079 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001080 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001081 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001082 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001083 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001084
1085 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001086 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001087}
Jamie Madill334d6152015-10-22 14:00:28 -04001088} // namespace rx