blob: a9fac31be2e2134745275d066ab29b7cde5bd924 [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
9#include "precompiled.h"
10
11#include "libGLESv2/DynamicHLSL.h"
12#include "libGLESv2/Shader.h"
13#include "libGLESv2/Program.h"
14#include "libGLESv2/renderer/Renderer.h"
15#include "common/utilities.h"
16#include "libGLESv2/ProgramBinary.h"
Jamie Madill8664b062014-02-14 16:41:29 -050017#include "libGLESv2/formatutils.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040018#include "common/blocklayout.h"
Jamie Madill5f562732014-02-14 16:41:24 -050019
Jamie Madill5f562732014-02-14 16:41:24 -050020static std::string Str(int i)
21{
22 char buffer[20];
23 snprintf(buffer, sizeof(buffer), "%d", i);
24 return buffer;
25}
26
Jamie Madill8664b062014-02-14 16:41:29 -050027namespace gl_d3d
Jamie Madill5f562732014-02-14 16:41:24 -050028{
Jamie Madill8664b062014-02-14 16:41:29 -050029
30std::string HLSLComponentTypeString(GLenum componentType)
31{
32 switch (componentType)
33 {
34 case GL_UNSIGNED_INT: return "uint";
35 case GL_INT: return "int";
36 case GL_UNSIGNED_NORMALIZED:
37 case GL_SIGNED_NORMALIZED:
38 case GL_FLOAT: return "float";
39 default: UNREACHABLE(); return "not-component-type";
40 }
Jamie Madill5f562732014-02-14 16:41:24 -050041}
42
Jamie Madill8664b062014-02-14 16:41:29 -050043std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
44{
45 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
46}
47
48std::string HLSLMatrixTypeString(GLenum type)
49{
50 switch (type)
51 {
52 case GL_FLOAT_MAT2: return "float2x2";
53 case GL_FLOAT_MAT3: return "float3x3";
54 case GL_FLOAT_MAT4: return "float4x4";
55 case GL_FLOAT_MAT2x3: return "float2x3";
56 case GL_FLOAT_MAT3x2: return "float3x2";
57 case GL_FLOAT_MAT2x4: return "float2x4";
58 case GL_FLOAT_MAT4x2: return "float4x2";
59 case GL_FLOAT_MAT3x4: return "float3x4";
60 case GL_FLOAT_MAT4x3: return "float4x3";
61 default: UNREACHABLE(); return "not-matrix-type";
62 }
63}
64
65std::string HLSLTypeString(GLenum type)
66{
67 if (gl::IsMatrixType(type))
68 {
69 return HLSLMatrixTypeString(type);
70 }
71
72 return HLSLComponentTypeString(gl::UniformComponentType(type), gl::UniformComponentCount(type));
73}
74
75}
76
77namespace gl
78{
79
Jamie Madill5f562732014-02-14 16:41:24 -050080std::string ArrayString(unsigned int i)
81{
82 return (i == GL_INVALID_INDEX ? "" : "[" + Str(i) + "]");
83}
84
Geoff Lang04fb89a2014-06-09 15:05:36 -040085const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
86const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050087
Jamie Madill5f562732014-02-14 16:41:24 -050088DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
89 : mRenderer(renderer)
90{
91}
92
Jamie Madillff0d2ba2014-05-14 13:49:10 -040093static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -050094{
Geoff Lang48dcae72014-02-05 16:28:24 -050095 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -050096
Geoff Lang48dcae72014-02-05 16:28:24 -050097 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -040098 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -050099 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500100
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400101 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -0500102 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400103 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -0500104 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500105 bool available = true;
106
107 for (int y = 0; y < registers && available; y++)
108 {
109 for (int x = 0; x < elements && available; x++)
110 {
111 if (packing[r + y][x])
112 {
113 available = false;
114 }
115 }
116 }
117
118 if (available)
119 {
120 varying->registerIndex = r;
Geoff Lang48dcae72014-02-05 16:28:24 -0500121
122 for (int y = 0; y < registers; y++)
123 {
124 for (int x = 0; x < elements; x++)
125 {
126 packing[r + y][x] = &*varying;
127 }
128 }
129
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400130 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500131 }
132 }
133
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400134 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500135 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400136 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500137 {
138 bool available = true;
139
140 for (int y = 0; y < registers && available; y++)
141 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500142 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500143 {
144 if (packing[r + y][x])
145 {
146 available = false;
147 }
148 }
149 }
150
151 if (available)
152 {
153 varying->registerIndex = r;
Jamie Madill5f562732014-02-14 16:41:24 -0500154
155 for (int y = 0; y < registers; y++)
156 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500157 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500158 {
159 packing[r + y][x] = &*varying;
160 }
161 }
162
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400163 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500164 }
165 }
Jamie Madill5f562732014-02-14 16:41:24 -0500166 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500167 }
168 else if (elements == 1)
169 {
170 int space[4] = { 0 };
171
172 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500173 {
Jamie Madill5f562732014-02-14 16:41:24 -0500174 for (int x = 0; x < 4; x++)
175 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500176 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500177 }
178 }
Jamie Madill5f562732014-02-14 16:41:24 -0500179
Geoff Lang48dcae72014-02-05 16:28:24 -0500180 int column = 0;
181
182 for (int x = 0; x < 4; x++)
183 {
184 if (space[x] >= registers && space[x] < space[column])
185 {
186 column = x;
187 }
188 }
189
190 if (space[column] >= registers)
191 {
192 for (int r = 0; r < maxVaryingVectors; r++)
193 {
194 if (!packing[r][column])
195 {
196 varying->registerIndex = r;
197
198 for (int y = r; y < r + registers; y++)
199 {
200 packing[y][column] = &*varying;
201 }
202
203 break;
204 }
205 }
206
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400207 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500208 }
209 }
210 else UNREACHABLE();
211
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400212 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500213}
214
215// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
216// Returns the number of used varying registers, or -1 if unsuccesful
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400217int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, FragmentShader *fragmentShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500218 VertexShader *vertexShader, const std::vector<std::string>& transformFeedbackVaryings)
219{
220 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
221
222 vertexShader->resetVaryingsRegisterAssignment();
223 fragmentShader->resetVaryingsRegisterAssignment();
224
225 std::set<std::string> packedVaryings;
226
227 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
228 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400229 PackedVarying *varying = &fragmentShader->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500230 if (packVarying(varying, maxVaryingVectors, packing))
231 {
232 packedVaryings.insert(varying->name);
233 }
234 else
Jamie Madill5f562732014-02-14 16:41:24 -0500235 {
236 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500237 return -1;
238 }
239 }
240
Geoff Lang48dcae72014-02-05 16:28:24 -0500241 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
242 {
243 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
244 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
245 {
246 bool found = false;
247 for (unsigned int varyingIndex = 0; varyingIndex < vertexShader->mVaryings.size(); varyingIndex++)
248 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400249 PackedVarying *varying = &vertexShader->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500250 if (transformFeedbackVarying == varying->name)
251 {
252 if (!packVarying(varying, maxVaryingVectors, packing))
253 {
254 infoLog.append("Could not pack varying %s", varying->name.c_str());
255 return -1;
256 }
257
258 found = true;
259 break;
260 }
261 }
262
263 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
264 {
265 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
266 return -1;
267 }
268 }
269 }
270
Jamie Madill5f562732014-02-14 16:41:24 -0500271 // Return the number of used registers
272 int registers = 0;
273
274 for (int r = 0; r < maxVaryingVectors; r++)
275 {
276 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
277 {
278 registers++;
279 }
280 }
281
282 return registers;
283}
284
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400285std::string DynamicHLSL::generateVaryingHLSL(VertexShader *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500286{
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400287 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500288 std::string varyingHLSL;
289
Geoff Lang48dcae72014-02-05 16:28:24 -0500290 for (unsigned int varyingIndex = 0; varyingIndex < shader->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500291 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400292 const PackedVarying &varying = shader->mVaryings[varyingIndex];
293 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500294 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400295 GLenum transposedType = TransposeMatrixType(varying.type);
296 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500297
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400298 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500299 {
Jamie Madill5f562732014-02-14 16:41:24 -0500300 for (int row = 0; row < variableRows; row++)
301 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400302 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500303 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400304 case INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
305 case INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
306 case INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500307 default: UNREACHABLE();
308 }
309
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400310 unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500311 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500312
Jamie Madilla53ab512014-03-17 09:47:44 -0400313 std::string typeString;
314
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400315 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400316 {
317 // matrices within structs are not transposed, so
318 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400319 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400320 }
321 else
322 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400323 GLenum componentType = UniformComponentType(transposedType);
324 int columnCount = VariableColumnCount(transposedType);
Jamie Madilla53ab512014-03-17 09:47:44 -0400325 typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
326 }
Jamie Madill5f562732014-02-14 16:41:24 -0500327 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
328 }
329 }
330 }
Jamie Madill5f562732014-02-14 16:41:24 -0500331 }
332
333 return varyingHLSL;
334}
335
Geoff Lang04fb89a2014-06-09 15:05:36 -0400336std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader, const VertexFormat inputLayout[], const Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500337{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400338 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500339
340 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400341 unsigned int inputIndex = 0;
342
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500343 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
344 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400345 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
346
347 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madill834e8b72014-04-11 13:33:58 -0400348 const Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500349
Jamie Madill8664b062014-02-14 16:41:29 -0500350 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500351 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400352 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500353 if (IsMatrixType(shaderAttribute.type))
354 {
355 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400356 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500357 }
358 else
359 {
360 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madill3b7e2052014-03-17 09:47:43 -0400361 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, UniformComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500362 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500363
Jamie Madilla53ab512014-03-17 09:47:44 -0400364 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madill8664b062014-02-14 16:41:29 -0500365 semanticIndex += AttributeRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500366
Jamie Madill3b7e2052014-03-17 09:47:43 -0400367 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400368 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500369
370 // Mismatched vertex attribute to vertex input may result in an undefined
371 // data reinterpretation (eg for pure integer->float, float->pure integer)
372 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400373 if (IsMatrixType(shaderAttribute.type) ||
374 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500375 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400376 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500377 }
378 else
379 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400380 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500381 }
382
Jamie Madill3b7e2052014-03-17 09:47:43 -0400383 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400384
Jamie Madillac0a2672014-04-11 13:33:56 -0400385 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
386 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500387 }
388
Geoff Lang04fb89a2014-06-09 15:05:36 -0400389 std::string replacementHLSL = "struct VS_INPUT\n"
390 "{\n" +
391 structHLSL +
392 "};\n"
393 "\n"
394 "void initAttributes(VS_INPUT input)\n"
395 "{\n" +
396 initHLSL +
397 "}\n";
398
399 std::string vertexHLSL(sourceShader);
400
401 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
402 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
403
404 return vertexHLSL;
405}
406
407std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables,
408 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
409{
410 const int shaderModel = mRenderer->getMajorShaderModel();
411 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
412 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
413
414 std::string declarationHLSL;
415 std::string copyHLSL;
416 for (size_t i = 0; i < outputVariables.size(); i++)
417 {
418 const PixelShaderOuputVariable& outputVariable = outputVariables[i];
419 ASSERT(outputLayout.size() > outputVariable.outputIndex);
Geoff Lang4ace4232014-06-18 19:12:48 -0400420
421 // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers
422 bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE
423 if (outputIndexEnabled)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400424 {
425 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
426 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
427
428 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
429 }
430 }
431
432 if (usesFragDepth)
433 {
434 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
435 copyHLSL += " output.gl_Depth = gl_Depth; \n";
436 }
437
438 std::string replacementHLSL = "struct PS_OUTPUT\n"
439 "{\n" +
440 declarationHLSL +
441 "};\n"
442 "\n"
443 "PS_OUTPUT generateOutput()\n"
444 "{\n"
445 " PS_OUTPUT output;\n" +
446 copyHLSL +
447 " return output;\n"
448 "}\n";
449
450 std::string pixelHLSL(sourceShader);
451
452 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
453 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
454
455 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500456}
457
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400458std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
459{
460 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
461 // In D3D11 we manually compute gl_PointCoord in the GS.
462 int shaderModel = mRenderer->getMajorShaderModel();
463 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
464}
465
466struct DynamicHLSL::SemanticInfo
467{
468 struct BuiltinInfo
469 {
470 BuiltinInfo()
471 : enabled(false),
472 index(0),
473 systemValue(false)
474 {}
475
476 bool enabled;
477 std::string semantic;
478 unsigned int index;
479 bool systemValue;
480
481 std::string str() const
482 {
483 return (systemValue ? semantic : (semantic + Str(index)));
484 }
485
486 void enableSystem(const std::string &systemValueSemantic)
487 {
488 enabled = true;
489 semantic = systemValueSemantic;
490 systemValue = true;
491 }
492
493 void enable(const std::string &semanticVal, unsigned int indexVal)
494 {
495 enabled = true;
496 semantic = semanticVal;
497 index = indexVal;
498 }
499 };
500
501 BuiltinInfo dxPosition;
502 BuiltinInfo glPosition;
503 BuiltinInfo glFragCoord;
504 BuiltinInfo glPointCoord;
505 BuiltinInfo glPointSize;
506};
507
508DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
509 bool pointSize, bool pixelShader) const
510{
511 SemanticInfo info;
512 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
513 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
514
515 int reservedRegisterIndex = startRegisters;
516
517 if (hlsl4)
518 {
519 info.dxPosition.enableSystem("SV_Position");
520 }
521 else if (pixelShader)
522 {
523 info.dxPosition.enableSystem("VPOS");
524 }
525 else
526 {
527 info.dxPosition.enableSystem("POSITION");
528 }
529
530 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
531
532 if (fragCoord)
533 {
534 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
535 }
536
537 if (pointCoord)
538 {
539 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
540 // In D3D11 we manually compute gl_PointCoord in the GS.
541 if (hlsl4)
542 {
543 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
544 }
545 else
546 {
547 info.glPointCoord.enable("TEXCOORD", 0);
548 }
549 }
550
551 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
552 if (pointSize && (!pixelShader || hlsl4))
553 {
554 info.glPointSize.enableSystem("PSIZE");
555 }
556
557 return info;
558}
559
560std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
561{
562 std::string linkHLSL = "{\n";
563
564 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
565
566 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
567 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
568
569 if (info.glFragCoord.enabled)
570 {
571 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
572 }
573
574 if (info.glPointCoord.enabled)
575 {
576 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
577 }
578
579 linkHLSL += varyingHLSL;
580
581 if (info.glPointSize.enabled)
582 {
583 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
584 }
585
586 linkHLSL += "};\n";
587
588 return linkHLSL;
589}
590
591void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
592 std::vector<LinkedVarying> *linkedVaryings) const
593{
594 ASSERT(info.glPosition.enabled);
595
596 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
597 info.glPosition.index, 1));
598
599 if (info.glFragCoord.enabled)
600 {
601 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
602 info.glFragCoord.index, 1));
603 }
604
605 if (info.glPointSize.enabled)
606 {
607 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
608 }
609}
610
611void DynamicHLSL::storeUserLinkedVaryings(const VertexShader *vertexShader,
612 std::vector<LinkedVarying> *linkedVaryings) const
613{
614 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
615 const std::vector<PackedVarying> &varyings = vertexShader->mVaryings;
616
617 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
618 {
619 const PackedVarying &varying = varyings[varyingIndex];
620 if (varying.registerAssigned())
621 {
622 GLenum transposedType = TransposeMatrixType(varying.type);
623 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
624
625 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
626 varyingSemantic, varying.registerIndex,
627 variableRows * varying.elementCount()));
628 }
629 }
630}
631
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400632bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500633 std::string& pixelHLSL, std::string& vertexHLSL,
634 FragmentShader *fragmentShader, VertexShader *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500635 const std::vector<std::string>& transformFeedbackVaryings,
636 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400637 std::map<int, VariableLocation> *programOutputVars,
638 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
639 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500640{
641 if (pixelHLSL.empty() || vertexHLSL.empty())
642 {
643 return false;
644 }
645
646 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
647 bool usesFragColor = fragmentShader->mUsesFragColor;
648 bool usesFragData = fragmentShader->mUsesFragData;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400649 bool usesFragCoord = fragmentShader->mUsesFragCoord;
650 bool usesPointCoord = fragmentShader->mUsesPointCoord;
651 bool usesPointSize = vertexShader->mUsesPointSize;
652
Jamie Madill5f562732014-02-14 16:41:24 -0500653 if (usesFragColor && usesFragData)
654 {
655 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
656 return false;
657 }
658
659 // Write the HLSL input/output declarations
660 const int shaderModel = mRenderer->getMajorShaderModel();
661 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
662
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400663 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500664
665 // Two cases when writing to gl_FragColor and using ESSL 1.0:
666 // - with a 3.0 context, the output color is copied to channel 0
667 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400668 const bool broadcast = (usesFragColor && mRenderer->getCurrentClientVersion() < 3);
Jamie Madill5f562732014-02-14 16:41:24 -0500669 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
670
671 int shaderVersion = vertexShader->getShaderVersion();
672
673 if (registersNeeded > maxVaryingVectors)
674 {
675 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500676 return false;
677 }
678
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400679 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
680 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
681 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500682
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400683 storeUserLinkedVaryings(vertexShader, linkedVaryings);
684 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500685
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500686 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400687 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
688 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500689 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500690 "{\n"
691 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500692
693 if (shaderModel >= 4)
694 {
695 vertexHLSL += "\n"
696 " gl_main();\n"
697 "\n"
698 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500699 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400700 " output.dx_Position.x = gl_Position.x;\n"
701 " output.dx_Position.y = -gl_Position.y;\n"
702 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
703 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500704 }
705 else
706 {
707 vertexHLSL += "\n"
708 " gl_main();\n"
709 "\n"
710 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500711 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400712 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
713 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
714 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
715 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500716 }
717
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400718 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500719 {
720 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
721 }
722
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400723 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500724 {
725 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
726 }
727
728 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
729 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400730 const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex];
731 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500732 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400733 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500734 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400735 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500736
737 for (int row = 0; row < variableRows; row++)
738 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400739 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500740 vertexHLSL += " output.v" + Str(r);
741
742 bool sharedRegister = false; // Register used by multiple varyings
743
744 for (int x = 0; x < 4; x++)
745 {
746 if (packing[r][x] && packing[r][x] != packing[r][0])
747 {
748 sharedRegister = true;
749 break;
750 }
751 }
752
753 if(sharedRegister)
754 {
755 vertexHLSL += ".";
756
757 for (int x = 0; x < 4; x++)
758 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400759 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500760 {
761 switch(x)
762 {
763 case 0: vertexHLSL += "x"; break;
764 case 1: vertexHLSL += "y"; break;
765 case 2: vertexHLSL += "z"; break;
766 case 3: vertexHLSL += "w"; break;
767 }
768 }
769 }
770 }
771
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400772 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500773
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400774 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500775 {
776 vertexHLSL += ArrayString(elementIndex);
777 }
778
779 if (variableRows > 1)
780 {
781 vertexHLSL += ArrayString(row);
782 }
783
784 vertexHLSL += ";\n";
785 }
786 }
787 }
788 }
789
790 vertexHLSL += "\n"
791 " return output;\n"
792 "}\n";
793
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400794 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
795 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500796
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400797 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500798
799 if (shaderVersion < 300)
800 {
801 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
802 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400803 PixelShaderOuputVariable outputKeyVariable;
804 outputKeyVariable.type = GL_FLOAT_VEC4;
805 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
806 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
807 outputKeyVariable.outputIndex = renderTargetIndex;
808
809 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500810 }
811
Geoff Lang04fb89a2014-06-09 15:05:36 -0400812 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500813 }
814 else
815 {
816 defineOutputVariables(fragmentShader, programOutputVars);
817
Jamie Madill834e8b72014-04-11 13:33:58 -0400818 const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500819 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
820 {
821 const VariableLocation &outputLocation = locationIt->second;
Jamie Madill834e8b72014-04-11 13:33:58 -0400822 const ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400823 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500824 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
825
Geoff Lang04fb89a2014-06-09 15:05:36 -0400826 PixelShaderOuputVariable outputKeyVariable;
827 outputKeyVariable.type = outputVariable.type;
828 outputKeyVariable.name = variableName + elementString;
829 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
830 outputKeyVariable.outputIndex = locationIt->first;
831
832 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500833 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400834
835 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500836 }
837
Geoff Lang04fb89a2014-06-09 15:05:36 -0400838 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500839
840 if (fragmentShader->mUsesFrontFacing)
841 {
842 if (shaderModel >= 4)
843 {
844 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
845 "{\n";
846 }
847 else
848 {
849 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
850 "{\n";
851 }
852 }
853 else
854 {
855 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
856 "{\n";
857 }
858
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400859 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500860 {
861 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
862
863 if (shaderModel >= 4)
864 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400865 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
866 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500867 }
868 else if (shaderModel >= 3)
869 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400870 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
871 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500872 }
873 else
874 {
875 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
876 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
877 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
878 }
879
880 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
881 " gl_FragCoord.w = rhw;\n";
882 }
883
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400884 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500885 {
886 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
887 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
888 }
889
890 if (fragmentShader->mUsesFrontFacing)
891 {
892 if (shaderModel <= 3)
893 {
894 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
895 }
896 else
897 {
898 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
899 }
900 }
901
902 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
903 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400904 const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex];
905 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500906 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400907 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500908 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400909 GLenum transposedType = TransposeMatrixType(varying.type);
910 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500911 for (int row = 0; row < variableRows; row++)
912 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400913 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
914 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500915
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400916 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500917 {
918 pixelHLSL += ArrayString(elementIndex);
919 }
920
921 if (variableRows > 1)
922 {
923 pixelHLSL += ArrayString(row);
924 }
925
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400926 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500927 {
928 pixelHLSL += " = input.v" + n + ";\n"; break;
929 }
930 else
931 {
932 switch (VariableColumnCount(transposedType))
933 {
934 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
935 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
936 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
937 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
938 default: UNREACHABLE();
939 }
940 }
941 }
942 }
943 }
944 else UNREACHABLE();
945 }
946
947 pixelHLSL += "\n"
948 " gl_main();\n"
949 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400950 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500951 "}\n";
952
953 return true;
954}
955
956void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
957{
Jamie Madill834e8b72014-04-11 13:33:58 -0400958 const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500959
960 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
961 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400962 const Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500963 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
964
965 if (outputVariable.arraySize > 0)
966 {
967 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
968 {
969 const int location = baseLocation + elementIndex;
970 ASSERT(programOutputVars->count(location) == 0);
971 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
972 }
973 }
974 else
975 {
976 ASSERT(programOutputVars->count(baseLocation) == 0);
977 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
978 }
979 }
980}
981
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400982std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500983{
984 // for now we only handle point sprite emulation
985 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400986 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -0500987}
988
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400989std::string DynamicHLSL::generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500990{
991 ASSERT(registers >= 0);
992 ASSERT(vertexShader->mUsesPointSize);
993 ASSERT(mRenderer->getMajorShaderModel() >= 4);
994
995 std::string geomHLSL;
996
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400997 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
998 false, true, false);
999 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1000 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001001
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001002 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1003 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1004 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001005
1006 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1007 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001008 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1009 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001010 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001011 "static float2 pointSpriteCorners[] = \n"
1012 "{\n"
1013 " float2( 0.5f, -0.5f),\n"
1014 " float2( 0.5f, 0.5f),\n"
1015 " float2(-0.5f, -0.5f),\n"
1016 " float2(-0.5f, 0.5f)\n"
1017 "};\n"
1018 "\n"
1019 "static float2 pointSpriteTexcoords[] = \n"
1020 "{\n"
1021 " float2(1.0f, 1.0f),\n"
1022 " float2(1.0f, 0.0f),\n"
1023 " float2(0.0f, 1.0f),\n"
1024 " float2(0.0f, 0.0f)\n"
1025 "};\n"
1026 "\n"
1027 "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
1028 "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n"
1029 "\n"
1030 "[maxvertexcount(4)]\n"
1031 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1032 "{\n"
1033 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001034 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001035 " output.gl_PointSize = input[0].gl_PointSize;\n";
1036
1037 for (int r = 0; r < registers; r++)
1038 {
1039 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1040 }
1041
1042 if (fragmentShader->mUsesFragCoord)
1043 {
1044 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1045 }
1046
1047 geomHLSL += " \n"
1048 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001049 " float4 dx_Position = input[0].dx_Position;\n"
1050 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001051
1052 for (int corner = 0; corner < 4; corner++)
1053 {
1054 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001055 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001056
1057 if (fragmentShader->mUsesPointCoord)
1058 {
1059 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1060 }
1061
1062 geomHLSL += " outStream.Append(output);\n";
1063 }
1064
1065 geomHLSL += " \n"
1066 " outStream.RestartStrip();\n"
1067 "}\n";
1068
1069 return geomHLSL;
1070}
1071
1072// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001073std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001074{
Jamie Madill033dae62014-06-18 12:56:28 -04001075 if (name.compare(0, 3, "gl_"))
Jamie Madill5f562732014-02-14 16:41:24 -05001076 {
1077 return "_" + name;
1078 }
1079
1080 return name;
1081}
1082
Jamie Madill834e8b72014-04-11 13:33:58 -04001083std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001084{
Jamie Madilla53ab512014-03-17 09:47:44 -04001085 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001086
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001087 // Matrix
1088 if (IsMatrixType(shaderAttrib.type))
1089 {
Jamie Madill8664b062014-02-14 16:41:29 -05001090 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001091 }
1092
Jamie Madill8664b062014-02-14 16:41:29 -05001093 GLenum shaderComponentType = UniformComponentType(shaderAttrib.type);
1094 int shaderComponentCount = UniformComponentCount(shaderAttrib.type);
1095
Jamie Madill8664b062014-02-14 16:41:29 -05001096 // Perform integer to float conversion (if necessary)
1097 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1098
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001099 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001100 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001101 // TODO: normalization for 32-bit integer formats
1102 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1103 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001104 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001105
1106 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001107 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001108}
1109
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001110void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1111{
1112 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1113 {
1114 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1115
1116 if (vertexFormat.mType == GL_NONE)
1117 {
1118 signature[inputIndex] = GL_NONE;
1119 }
1120 else
1121 {
1122 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1123 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1124 }
1125 }
1126}
1127
Jamie Madill5f562732014-02-14 16:41:24 -05001128}