blob: c32b4073b48153f7f09a90909a25ece8d4751b09 [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
Jamie Madillf2575982014-06-25 16:04:54 -040072 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050073}
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 Madillf2575982014-06-25 16:04:54 -0400304 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
305 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
306 case sh::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 Madillf2575982014-06-25 16:04:54 -0400323 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400324 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
Jamie Madillf2575982014-06-25 16:04:54 -0400336std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
337 const VertexFormat inputLayout[],
338 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500339{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400340 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500341
342 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400343 unsigned int inputIndex = 0;
344
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500345 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
346 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400347 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
348
349 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madillf2575982014-06-25 16:04:54 -0400350 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500351
Jamie Madill8664b062014-02-14 16:41:29 -0500352 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500353 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400354 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500355 if (IsMatrixType(shaderAttribute.type))
356 {
357 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400358 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500359 }
360 else
361 {
362 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madillf2575982014-06-25 16:04:54 -0400363 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500364 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500365
Jamie Madilla53ab512014-03-17 09:47:44 -0400366 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400367 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500368
Jamie Madill3b7e2052014-03-17 09:47:43 -0400369 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400370 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500371
372 // Mismatched vertex attribute to vertex input may result in an undefined
373 // data reinterpretation (eg for pure integer->float, float->pure integer)
374 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400375 if (IsMatrixType(shaderAttribute.type) ||
376 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500377 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400378 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500379 }
380 else
381 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400382 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500383 }
384
Jamie Madill3b7e2052014-03-17 09:47:43 -0400385 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400386
Jamie Madillac0a2672014-04-11 13:33:56 -0400387 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
388 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500389 }
390
Geoff Lang04fb89a2014-06-09 15:05:36 -0400391 std::string replacementHLSL = "struct VS_INPUT\n"
392 "{\n" +
393 structHLSL +
394 "};\n"
395 "\n"
396 "void initAttributes(VS_INPUT input)\n"
397 "{\n" +
398 initHLSL +
399 "}\n";
400
401 std::string vertexHLSL(sourceShader);
402
403 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
404 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
405
406 return vertexHLSL;
407}
408
409std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables,
410 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
411{
412 const int shaderModel = mRenderer->getMajorShaderModel();
413 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
414 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
415
416 std::string declarationHLSL;
417 std::string copyHLSL;
418 for (size_t i = 0; i < outputVariables.size(); i++)
419 {
420 const PixelShaderOuputVariable& outputVariable = outputVariables[i];
421 ASSERT(outputLayout.size() > outputVariable.outputIndex);
Geoff Lang4ace4232014-06-18 19:12:48 -0400422
423 // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers
424 bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE
425 if (outputIndexEnabled)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400426 {
427 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
428 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
429
430 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
431 }
432 }
433
434 if (usesFragDepth)
435 {
436 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
437 copyHLSL += " output.gl_Depth = gl_Depth; \n";
438 }
439
440 std::string replacementHLSL = "struct PS_OUTPUT\n"
441 "{\n" +
442 declarationHLSL +
443 "};\n"
444 "\n"
445 "PS_OUTPUT generateOutput()\n"
446 "{\n"
447 " PS_OUTPUT output;\n" +
448 copyHLSL +
449 " return output;\n"
450 "}\n";
451
452 std::string pixelHLSL(sourceShader);
453
454 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
455 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
456
457 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500458}
459
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400460std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
461{
462 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
463 // In D3D11 we manually compute gl_PointCoord in the GS.
464 int shaderModel = mRenderer->getMajorShaderModel();
465 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
466}
467
468struct DynamicHLSL::SemanticInfo
469{
470 struct BuiltinInfo
471 {
472 BuiltinInfo()
473 : enabled(false),
474 index(0),
475 systemValue(false)
476 {}
477
478 bool enabled;
479 std::string semantic;
480 unsigned int index;
481 bool systemValue;
482
483 std::string str() const
484 {
485 return (systemValue ? semantic : (semantic + Str(index)));
486 }
487
488 void enableSystem(const std::string &systemValueSemantic)
489 {
490 enabled = true;
491 semantic = systemValueSemantic;
492 systemValue = true;
493 }
494
495 void enable(const std::string &semanticVal, unsigned int indexVal)
496 {
497 enabled = true;
498 semantic = semanticVal;
499 index = indexVal;
500 }
501 };
502
503 BuiltinInfo dxPosition;
504 BuiltinInfo glPosition;
505 BuiltinInfo glFragCoord;
506 BuiltinInfo glPointCoord;
507 BuiltinInfo glPointSize;
508};
509
510DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
511 bool pointSize, bool pixelShader) const
512{
513 SemanticInfo info;
514 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
515 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
516
517 int reservedRegisterIndex = startRegisters;
518
519 if (hlsl4)
520 {
521 info.dxPosition.enableSystem("SV_Position");
522 }
523 else if (pixelShader)
524 {
525 info.dxPosition.enableSystem("VPOS");
526 }
527 else
528 {
529 info.dxPosition.enableSystem("POSITION");
530 }
531
532 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
533
534 if (fragCoord)
535 {
536 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
537 }
538
539 if (pointCoord)
540 {
541 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
542 // In D3D11 we manually compute gl_PointCoord in the GS.
543 if (hlsl4)
544 {
545 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
546 }
547 else
548 {
549 info.glPointCoord.enable("TEXCOORD", 0);
550 }
551 }
552
553 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
554 if (pointSize && (!pixelShader || hlsl4))
555 {
556 info.glPointSize.enableSystem("PSIZE");
557 }
558
559 return info;
560}
561
562std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
563{
564 std::string linkHLSL = "{\n";
565
566 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
567
568 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
569 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
570
571 if (info.glFragCoord.enabled)
572 {
573 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
574 }
575
576 if (info.glPointCoord.enabled)
577 {
578 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
579 }
580
581 linkHLSL += varyingHLSL;
582
583 if (info.glPointSize.enabled)
584 {
585 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
586 }
587
588 linkHLSL += "};\n";
589
590 return linkHLSL;
591}
592
593void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
594 std::vector<LinkedVarying> *linkedVaryings) const
595{
596 ASSERT(info.glPosition.enabled);
597
598 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
599 info.glPosition.index, 1));
600
601 if (info.glFragCoord.enabled)
602 {
603 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
604 info.glFragCoord.index, 1));
605 }
606
607 if (info.glPointSize.enabled)
608 {
609 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
610 }
611}
612
613void DynamicHLSL::storeUserLinkedVaryings(const VertexShader *vertexShader,
614 std::vector<LinkedVarying> *linkedVaryings) const
615{
616 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
617 const std::vector<PackedVarying> &varyings = vertexShader->mVaryings;
618
619 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
620 {
621 const PackedVarying &varying = varyings[varyingIndex];
622 if (varying.registerAssigned())
623 {
624 GLenum transposedType = TransposeMatrixType(varying.type);
625 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
626
627 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
628 varyingSemantic, varying.registerIndex,
629 variableRows * varying.elementCount()));
630 }
631 }
632}
633
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400634bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500635 std::string& pixelHLSL, std::string& vertexHLSL,
636 FragmentShader *fragmentShader, VertexShader *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500637 const std::vector<std::string>& transformFeedbackVaryings,
638 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400639 std::map<int, VariableLocation> *programOutputVars,
640 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
641 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500642{
643 if (pixelHLSL.empty() || vertexHLSL.empty())
644 {
645 return false;
646 }
647
648 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
649 bool usesFragColor = fragmentShader->mUsesFragColor;
650 bool usesFragData = fragmentShader->mUsesFragData;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400651 bool usesFragCoord = fragmentShader->mUsesFragCoord;
652 bool usesPointCoord = fragmentShader->mUsesPointCoord;
653 bool usesPointSize = vertexShader->mUsesPointSize;
654
Jamie Madill5f562732014-02-14 16:41:24 -0500655 if (usesFragColor && usesFragData)
656 {
657 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
658 return false;
659 }
660
661 // Write the HLSL input/output declarations
662 const int shaderModel = mRenderer->getMajorShaderModel();
663 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
664
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400665 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500666
667 // Two cases when writing to gl_FragColor and using ESSL 1.0:
668 // - with a 3.0 context, the output color is copied to channel 0
669 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400670 const bool broadcast = (usesFragColor && mRenderer->getCurrentClientVersion() < 3);
Jamie Madill5f562732014-02-14 16:41:24 -0500671 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
672
673 int shaderVersion = vertexShader->getShaderVersion();
674
675 if (registersNeeded > maxVaryingVectors)
676 {
677 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500678 return false;
679 }
680
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400681 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
682 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
683 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500684
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400685 storeUserLinkedVaryings(vertexShader, linkedVaryings);
686 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500687
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500688 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400689 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
690 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500691 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500692 "{\n"
693 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500694
695 if (shaderModel >= 4)
696 {
697 vertexHLSL += "\n"
698 " gl_main();\n"
699 "\n"
700 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500701 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400702 " output.dx_Position.x = gl_Position.x;\n"
703 " output.dx_Position.y = -gl_Position.y;\n"
704 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
705 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500706 }
707 else
708 {
709 vertexHLSL += "\n"
710 " gl_main();\n"
711 "\n"
712 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500713 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400714 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
715 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
716 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
717 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500718 }
719
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400720 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500721 {
722 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
723 }
724
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400725 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500726 {
727 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
728 }
729
730 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
731 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400732 const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex];
733 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500734 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400735 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500736 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400737 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500738
739 for (int row = 0; row < variableRows; row++)
740 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400741 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500742 vertexHLSL += " output.v" + Str(r);
743
744 bool sharedRegister = false; // Register used by multiple varyings
745
746 for (int x = 0; x < 4; x++)
747 {
748 if (packing[r][x] && packing[r][x] != packing[r][0])
749 {
750 sharedRegister = true;
751 break;
752 }
753 }
754
755 if(sharedRegister)
756 {
757 vertexHLSL += ".";
758
759 for (int x = 0; x < 4; x++)
760 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400761 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500762 {
763 switch(x)
764 {
765 case 0: vertexHLSL += "x"; break;
766 case 1: vertexHLSL += "y"; break;
767 case 2: vertexHLSL += "z"; break;
768 case 3: vertexHLSL += "w"; break;
769 }
770 }
771 }
772 }
773
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400774 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500775
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400776 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500777 {
778 vertexHLSL += ArrayString(elementIndex);
779 }
780
781 if (variableRows > 1)
782 {
783 vertexHLSL += ArrayString(row);
784 }
785
786 vertexHLSL += ";\n";
787 }
788 }
789 }
790 }
791
792 vertexHLSL += "\n"
793 " return output;\n"
794 "}\n";
795
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400796 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
797 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500798
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400799 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500800
801 if (shaderVersion < 300)
802 {
803 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
804 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400805 PixelShaderOuputVariable outputKeyVariable;
806 outputKeyVariable.type = GL_FLOAT_VEC4;
807 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
808 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
809 outputKeyVariable.outputIndex = renderTargetIndex;
810
811 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500812 }
813
Geoff Lang04fb89a2014-06-09 15:05:36 -0400814 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500815 }
816 else
817 {
818 defineOutputVariables(fragmentShader, programOutputVars);
819
Jamie Madillf2575982014-06-25 16:04:54 -0400820 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500821 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
822 {
823 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400824 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400825 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500826 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
827
Geoff Lang04fb89a2014-06-09 15:05:36 -0400828 PixelShaderOuputVariable outputKeyVariable;
829 outputKeyVariable.type = outputVariable.type;
830 outputKeyVariable.name = variableName + elementString;
831 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
832 outputKeyVariable.outputIndex = locationIt->first;
833
834 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500835 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400836
837 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500838 }
839
Geoff Lang04fb89a2014-06-09 15:05:36 -0400840 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500841
842 if (fragmentShader->mUsesFrontFacing)
843 {
844 if (shaderModel >= 4)
845 {
846 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
847 "{\n";
848 }
849 else
850 {
851 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
852 "{\n";
853 }
854 }
855 else
856 {
857 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
858 "{\n";
859 }
860
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400861 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500862 {
863 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
864
865 if (shaderModel >= 4)
866 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400867 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
868 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500869 }
870 else if (shaderModel >= 3)
871 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400872 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
873 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500874 }
875 else
876 {
877 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
878 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
879 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
880 }
881
882 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
883 " gl_FragCoord.w = rhw;\n";
884 }
885
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400886 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500887 {
888 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
889 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
890 }
891
892 if (fragmentShader->mUsesFrontFacing)
893 {
894 if (shaderModel <= 3)
895 {
896 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
897 }
898 else
899 {
900 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
901 }
902 }
903
904 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
905 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400906 const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex];
907 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500908 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400909 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500910 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400911 GLenum transposedType = TransposeMatrixType(varying.type);
912 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500913 for (int row = 0; row < variableRows; row++)
914 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400915 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
916 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500917
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400918 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500919 {
920 pixelHLSL += ArrayString(elementIndex);
921 }
922
923 if (variableRows > 1)
924 {
925 pixelHLSL += ArrayString(row);
926 }
927
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400928 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500929 {
930 pixelHLSL += " = input.v" + n + ";\n"; break;
931 }
932 else
933 {
934 switch (VariableColumnCount(transposedType))
935 {
936 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
937 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
938 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
939 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
940 default: UNREACHABLE();
941 }
942 }
943 }
944 }
945 }
946 else UNREACHABLE();
947 }
948
949 pixelHLSL += "\n"
950 " gl_main();\n"
951 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400952 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500953 "}\n";
954
955 return true;
956}
957
958void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
959{
Jamie Madillf2575982014-06-25 16:04:54 -0400960 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500961
962 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
963 {
Jamie Madillf2575982014-06-25 16:04:54 -0400964 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500965 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
966
967 if (outputVariable.arraySize > 0)
968 {
969 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
970 {
971 const int location = baseLocation + elementIndex;
972 ASSERT(programOutputVars->count(location) == 0);
973 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
974 }
975 }
976 else
977 {
978 ASSERT(programOutputVars->count(baseLocation) == 0);
979 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
980 }
981 }
982}
983
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400984std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500985{
986 // for now we only handle point sprite emulation
987 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400988 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -0500989}
990
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400991std::string DynamicHLSL::generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500992{
993 ASSERT(registers >= 0);
994 ASSERT(vertexShader->mUsesPointSize);
995 ASSERT(mRenderer->getMajorShaderModel() >= 4);
996
997 std::string geomHLSL;
998
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400999 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1000 false, true, false);
1001 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1002 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001003
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001004 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1005 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1006 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001007
1008 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1009 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001010 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1011 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001012 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001013 "static float2 pointSpriteCorners[] = \n"
1014 "{\n"
1015 " float2( 0.5f, -0.5f),\n"
1016 " float2( 0.5f, 0.5f),\n"
1017 " float2(-0.5f, -0.5f),\n"
1018 " float2(-0.5f, 0.5f)\n"
1019 "};\n"
1020 "\n"
1021 "static float2 pointSpriteTexcoords[] = \n"
1022 "{\n"
1023 " float2(1.0f, 1.0f),\n"
1024 " float2(1.0f, 0.0f),\n"
1025 " float2(0.0f, 1.0f),\n"
1026 " float2(0.0f, 0.0f)\n"
1027 "};\n"
1028 "\n"
1029 "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
1030 "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n"
1031 "\n"
1032 "[maxvertexcount(4)]\n"
1033 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1034 "{\n"
1035 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001036 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001037 " output.gl_PointSize = input[0].gl_PointSize;\n";
1038
1039 for (int r = 0; r < registers; r++)
1040 {
1041 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1042 }
1043
1044 if (fragmentShader->mUsesFragCoord)
1045 {
1046 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1047 }
1048
1049 geomHLSL += " \n"
1050 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001051 " float4 dx_Position = input[0].dx_Position;\n"
1052 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001053
1054 for (int corner = 0; corner < 4; corner++)
1055 {
1056 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001057 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001058
1059 if (fragmentShader->mUsesPointCoord)
1060 {
1061 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1062 }
1063
1064 geomHLSL += " outStream.Append(output);\n";
1065 }
1066
1067 geomHLSL += " \n"
1068 " outStream.RestartStrip();\n"
1069 "}\n";
1070
1071 return geomHLSL;
1072}
1073
1074// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001075std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001076{
Jamie Madill033dae62014-06-18 12:56:28 -04001077 if (name.compare(0, 3, "gl_"))
Jamie Madill5f562732014-02-14 16:41:24 -05001078 {
1079 return "_" + name;
1080 }
1081
1082 return name;
1083}
1084
Jamie Madillf2575982014-06-25 16:04:54 -04001085std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001086{
Jamie Madilla53ab512014-03-17 09:47:44 -04001087 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001088
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001089 // Matrix
1090 if (IsMatrixType(shaderAttrib.type))
1091 {
Jamie Madill8664b062014-02-14 16:41:29 -05001092 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001093 }
1094
Jamie Madillf2575982014-06-25 16:04:54 -04001095 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1096 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001097
Jamie Madill8664b062014-02-14 16:41:29 -05001098 // Perform integer to float conversion (if necessary)
1099 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1100
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001101 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001102 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001103 // TODO: normalization for 32-bit integer formats
1104 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1105 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001106 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001107
1108 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001109 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001110}
1111
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001112void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1113{
1114 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1115 {
1116 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1117
1118 if (vertexFormat.mType == GL_NONE)
1119 {
1120 signature[inputIndex] = GL_NONE;
1121 }
1122 else
1123 {
1124 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1125 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1126 }
1127 }
1128}
1129
Jamie Madill5f562732014-02-14 16:41:24 -05001130}