blob: 507cf81d49bd0e943267f63ec7babb6f415526e7 [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
Geoff Lang48dcae72014-02-05 16:28:24 -0500285std::string DynamicHLSL::generateVaryingHLSL(VertexShader *shader, const std::string &varyingSemantic,
286 std::vector<LinkedVarying> *linkedVaryings) const
Jamie Madill5f562732014-02-14 16:41:24 -0500287{
288 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 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500330
331 if (linkedVaryings)
332 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400333 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
334 varyingSemantic, varying.registerIndex,
335 variableRows * varying.elementCount()));
Geoff Lang48dcae72014-02-05 16:28:24 -0500336 }
Jamie Madill5f562732014-02-14 16:41:24 -0500337 }
Jamie Madill5f562732014-02-14 16:41:24 -0500338 }
339
340 return varyingHLSL;
341}
342
Geoff Lang04fb89a2014-06-09 15:05:36 -0400343std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader, const VertexFormat inputLayout[], const Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500344{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400345 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500346
347 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400348 unsigned int inputIndex = 0;
349
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500350 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
351 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400352 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
353
354 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madill834e8b72014-04-11 13:33:58 -0400355 const Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500356
Jamie Madill8664b062014-02-14 16:41:29 -0500357 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500358 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400359 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500360 if (IsMatrixType(shaderAttribute.type))
361 {
362 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400363 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500364 }
365 else
366 {
367 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madill3b7e2052014-03-17 09:47:43 -0400368 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, UniformComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500369 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500370
Jamie Madilla53ab512014-03-17 09:47:44 -0400371 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madill8664b062014-02-14 16:41:29 -0500372 semanticIndex += AttributeRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500373
Jamie Madill3b7e2052014-03-17 09:47:43 -0400374 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400375 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500376
377 // Mismatched vertex attribute to vertex input may result in an undefined
378 // data reinterpretation (eg for pure integer->float, float->pure integer)
379 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400380 if (IsMatrixType(shaderAttribute.type) ||
381 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500382 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400383 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500384 }
385 else
386 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400387 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500388 }
389
Jamie Madill3b7e2052014-03-17 09:47:43 -0400390 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400391
Jamie Madillac0a2672014-04-11 13:33:56 -0400392 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
393 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500394 }
395
Geoff Lang04fb89a2014-06-09 15:05:36 -0400396 std::string replacementHLSL = "struct VS_INPUT\n"
397 "{\n" +
398 structHLSL +
399 "};\n"
400 "\n"
401 "void initAttributes(VS_INPUT input)\n"
402 "{\n" +
403 initHLSL +
404 "}\n";
405
406 std::string vertexHLSL(sourceShader);
407
408 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
409 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
410
411 return vertexHLSL;
412}
413
414std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables,
415 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
416{
417 const int shaderModel = mRenderer->getMajorShaderModel();
418 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
419 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
420
421 std::string declarationHLSL;
422 std::string copyHLSL;
423 for (size_t i = 0; i < outputVariables.size(); i++)
424 {
425 const PixelShaderOuputVariable& outputVariable = outputVariables[i];
426 ASSERT(outputLayout.size() > outputVariable.outputIndex);
427 if (outputLayout[outputVariable.outputIndex] != GL_NONE)
428 {
429 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
430 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
431
432 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
433 }
434 }
435
436 if (usesFragDepth)
437 {
438 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
439 copyHLSL += " output.gl_Depth = gl_Depth; \n";
440 }
441
442 std::string replacementHLSL = "struct PS_OUTPUT\n"
443 "{\n" +
444 declarationHLSL +
445 "};\n"
446 "\n"
447 "PS_OUTPUT generateOutput()\n"
448 "{\n"
449 " PS_OUTPUT output;\n" +
450 copyHLSL +
451 " return output;\n"
452 "}\n";
453
454 std::string pixelHLSL(sourceShader);
455
456 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
457 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
458
459 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500460}
461
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400462bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500463 std::string& pixelHLSL, std::string& vertexHLSL,
464 FragmentShader *fragmentShader, VertexShader *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500465 const std::vector<std::string>& transformFeedbackVaryings,
466 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400467 std::map<int, VariableLocation> *programOutputVars,
468 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
469 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500470{
471 if (pixelHLSL.empty() || vertexHLSL.empty())
472 {
473 return false;
474 }
475
476 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
477 bool usesFragColor = fragmentShader->mUsesFragColor;
478 bool usesFragData = fragmentShader->mUsesFragData;
479 if (usesFragColor && usesFragData)
480 {
481 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
482 return false;
483 }
484
485 // Write the HLSL input/output declarations
486 const int shaderModel = mRenderer->getMajorShaderModel();
487 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
488
489 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
490
491 // Two cases when writing to gl_FragColor and using ESSL 1.0:
492 // - with a 3.0 context, the output color is copied to channel 0
493 // - with a 2.0 context, the output color is broadcast to all channels
494 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
495 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
496
497 int shaderVersion = vertexShader->getShaderVersion();
498
499 if (registersNeeded > maxVaryingVectors)
500 {
501 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
502
503 return false;
504 }
505
506 std::string varyingSemantic = (vertexShader->mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
507 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
Geoff Lang48dcae72014-02-05 16:28:24 -0500508 std::string dxPositionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
Jamie Madill5f562732014-02-14 16:41:24 -0500509
Geoff Lang48dcae72014-02-05 16:28:24 -0500510 std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500511
512 // special varyings that use reserved registers
513 int reservedRegisterIndex = registers;
Jamie Madill5f562732014-02-14 16:41:24 -0500514
Geoff Lang48dcae72014-02-05 16:28:24 -0500515 unsigned int glPositionSemanticIndex = reservedRegisterIndex++;
516 std::string glPositionSemantic = varyingSemantic;
517
518 std::string fragCoordSemantic;
519 unsigned int fragCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500520 if (fragmentShader->mUsesFragCoord)
521 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500522 fragCoordSemanticIndex = reservedRegisterIndex++;
523 fragCoordSemantic = varyingSemantic;
Jamie Madill5f562732014-02-14 16:41:24 -0500524 }
525
Geoff Lang48dcae72014-02-05 16:28:24 -0500526 std::string pointCoordSemantic;
527 unsigned int pointCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500528 if (fragmentShader->mUsesPointCoord)
529 {
530 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
531 // In DX11 we compute this in the GS.
532 if (shaderModel == 3)
533 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500534 pointCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500535 pointCoordSemantic = "TEXCOORD0";
536 }
537 else if (shaderModel >= 4)
538 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500539 pointCoordSemanticIndex = reservedRegisterIndex++;
540 pointCoordSemantic = varyingSemantic;
Jamie Madill5f562732014-02-14 16:41:24 -0500541 }
542 }
543
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500544 // Add stub string to be replaced when shader is dynamically defined by its layout
545 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500546
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500547 vertexHLSL += "struct VS_OUTPUT\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500548 "{\n";
549
550 if (shaderModel < 4)
551 {
Jamie Madill2bf8b372014-06-16 17:18:51 -0400552 vertexHLSL += " float4 dx_Position : " + dxPositionSemantic + ";\n";
Geoff Lang48dcae72014-02-05 16:28:24 -0500553 vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n";
554 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1));
555
Jamie Madill5f562732014-02-14 16:41:24 -0500556 }
557
558 vertexHLSL += varyingHLSL;
559
560 if (fragmentShader->mUsesFragCoord)
561 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500562 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n";
563 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, fragCoordSemantic, fragCoordSemanticIndex, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500564 }
565
566 if (vertexShader->mUsesPointSize && shaderModel >= 3)
567 {
568 vertexHLSL += " float gl_PointSize : PSIZE;\n";
Geoff Lang48dcae72014-02-05 16:28:24 -0500569 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500570 }
571
572 if (shaderModel >= 4)
573 {
Jamie Madill2bf8b372014-06-16 17:18:51 -0400574 vertexHLSL += " float4 dx_Position : " + dxPositionSemantic + ";\n";
Geoff Lang48dcae72014-02-05 16:28:24 -0500575 vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n";
576 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500577 }
578
579 vertexHLSL += "};\n"
580 "\n"
581 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500582 "{\n"
583 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500584
585 if (shaderModel >= 4)
586 {
587 vertexHLSL += "\n"
588 " gl_main();\n"
589 "\n"
590 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500591 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400592 " output.dx_Position.x = gl_Position.x;\n"
593 " output.dx_Position.y = -gl_Position.y;\n"
594 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
595 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500596 }
597 else
598 {
599 vertexHLSL += "\n"
600 " gl_main();\n"
601 "\n"
602 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500603 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400604 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
605 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
606 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
607 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500608 }
609
610 if (vertexShader->mUsesPointSize && shaderModel >= 3)
611 {
612 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
613 }
614
615 if (fragmentShader->mUsesFragCoord)
616 {
617 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
618 }
619
620 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
621 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400622 const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex];
623 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500624 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400625 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500626 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400627 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500628
629 for (int row = 0; row < variableRows; row++)
630 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400631 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500632 vertexHLSL += " output.v" + Str(r);
633
634 bool sharedRegister = false; // Register used by multiple varyings
635
636 for (int x = 0; x < 4; x++)
637 {
638 if (packing[r][x] && packing[r][x] != packing[r][0])
639 {
640 sharedRegister = true;
641 break;
642 }
643 }
644
645 if(sharedRegister)
646 {
647 vertexHLSL += ".";
648
649 for (int x = 0; x < 4; x++)
650 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400651 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500652 {
653 switch(x)
654 {
655 case 0: vertexHLSL += "x"; break;
656 case 1: vertexHLSL += "y"; break;
657 case 2: vertexHLSL += "z"; break;
658 case 3: vertexHLSL += "w"; break;
659 }
660 }
661 }
662 }
663
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400664 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500665
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400666 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500667 {
668 vertexHLSL += ArrayString(elementIndex);
669 }
670
671 if (variableRows > 1)
672 {
673 vertexHLSL += ArrayString(row);
674 }
675
676 vertexHLSL += ";\n";
677 }
678 }
679 }
680 }
681
682 vertexHLSL += "\n"
683 " return output;\n"
684 "}\n";
685
686 pixelHLSL += "struct PS_INPUT\n"
687 "{\n";
688
689 pixelHLSL += varyingHLSL;
690
691 if (fragmentShader->mUsesFragCoord)
692 {
Geoff Langb5b02852014-04-16 14:39:36 -0400693 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500694 }
695
696 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
697 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500698 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + Str(pointCoordSemanticIndex) + ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500699 }
700
701 // Must consume the PSIZE element if the geometry shader is not active
702 // We won't know if we use a GS until we draw
703 if (vertexShader->mUsesPointSize && shaderModel >= 4)
704 {
705 pixelHLSL += " float gl_PointSize : PSIZE;\n";
706 }
707
708 if (fragmentShader->mUsesFragCoord)
709 {
710 if (shaderModel >= 4)
711 {
712 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
713 }
714 else if (shaderModel >= 3)
715 {
716 pixelHLSL += " float2 dx_VPos : VPOS;\n";
717 }
718 }
719
Geoff Lang04fb89a2014-06-09 15:05:36 -0400720 pixelHLSL += "};\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500721
722 if (shaderVersion < 300)
723 {
724 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
725 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400726 PixelShaderOuputVariable outputKeyVariable;
727 outputKeyVariable.type = GL_FLOAT_VEC4;
728 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
729 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
730 outputKeyVariable.outputIndex = renderTargetIndex;
731
732 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500733 }
734
Geoff Lang04fb89a2014-06-09 15:05:36 -0400735 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500736 }
737 else
738 {
739 defineOutputVariables(fragmentShader, programOutputVars);
740
Jamie Madill834e8b72014-04-11 13:33:58 -0400741 const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500742 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
743 {
744 const VariableLocation &outputLocation = locationIt->second;
Jamie Madill834e8b72014-04-11 13:33:58 -0400745 const ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400746 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500747 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
748
Geoff Lang04fb89a2014-06-09 15:05:36 -0400749 PixelShaderOuputVariable outputKeyVariable;
750 outputKeyVariable.type = outputVariable.type;
751 outputKeyVariable.name = variableName + elementString;
752 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
753 outputKeyVariable.outputIndex = locationIt->first;
754
755 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500756 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400757
758 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500759 }
760
Geoff Lang04fb89a2014-06-09 15:05:36 -0400761 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500762
763 if (fragmentShader->mUsesFrontFacing)
764 {
765 if (shaderModel >= 4)
766 {
767 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
768 "{\n";
769 }
770 else
771 {
772 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
773 "{\n";
774 }
775 }
776 else
777 {
778 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
779 "{\n";
780 }
781
782 if (fragmentShader->mUsesFragCoord)
783 {
784 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
785
786 if (shaderModel >= 4)
787 {
788 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
789 " gl_FragCoord.y = input.dx_VPos.y;\n";
790 }
791 else if (shaderModel >= 3)
792 {
793 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
794 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
795 }
796 else
797 {
798 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
799 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
800 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
801 }
802
803 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
804 " gl_FragCoord.w = rhw;\n";
805 }
806
807 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
808 {
809 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
810 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
811 }
812
813 if (fragmentShader->mUsesFrontFacing)
814 {
815 if (shaderModel <= 3)
816 {
817 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
818 }
819 else
820 {
821 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
822 }
823 }
824
825 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
826 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400827 const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex];
828 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500829 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400830 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500831 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400832 GLenum transposedType = TransposeMatrixType(varying.type);
833 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500834 for (int row = 0; row < variableRows; row++)
835 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400836 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
837 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500838
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400839 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500840 {
841 pixelHLSL += ArrayString(elementIndex);
842 }
843
844 if (variableRows > 1)
845 {
846 pixelHLSL += ArrayString(row);
847 }
848
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400849 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500850 {
851 pixelHLSL += " = input.v" + n + ";\n"; break;
852 }
853 else
854 {
855 switch (VariableColumnCount(transposedType))
856 {
857 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
858 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
859 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
860 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
861 default: UNREACHABLE();
862 }
863 }
864 }
865 }
866 }
867 else UNREACHABLE();
868 }
869
870 pixelHLSL += "\n"
871 " gl_main();\n"
872 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400873 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500874 "}\n";
875
876 return true;
877}
878
879void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
880{
Jamie Madill834e8b72014-04-11 13:33:58 -0400881 const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500882
883 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
884 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400885 const Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500886 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
887
888 if (outputVariable.arraySize > 0)
889 {
890 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
891 {
892 const int location = baseLocation + elementIndex;
893 ASSERT(programOutputVars->count(location) == 0);
894 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
895 }
896 }
897 else
898 {
899 ASSERT(programOutputVars->count(baseLocation) == 0);
900 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
901 }
902 }
903}
904
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400905std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500906{
907 // for now we only handle point sprite emulation
908 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400909 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -0500910}
911
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400912std::string DynamicHLSL::generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500913{
914 ASSERT(registers >= 0);
915 ASSERT(vertexShader->mUsesPointSize);
916 ASSERT(mRenderer->getMajorShaderModel() >= 4);
917
918 std::string geomHLSL;
919
920 std::string varyingSemantic = "TEXCOORD";
921
922 std::string fragCoordSemantic;
923 std::string pointCoordSemantic;
924
925 int reservedRegisterIndex = registers;
926
927 if (fragmentShader->mUsesFragCoord)
928 {
929 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
930 }
931
932 if (fragmentShader->mUsesPointCoord)
933 {
934 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
935 }
936
937 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
938 "\n"
939 "struct GS_INPUT\n"
940 "{\n";
941
Geoff Lang48dcae72014-02-05 16:28:24 -0500942 std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, NULL);
Jamie Madill5f562732014-02-14 16:41:24 -0500943
944 geomHLSL += varyingHLSL;
945
946 if (fragmentShader->mUsesFragCoord)
947 {
948 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
949 }
950
951 geomHLSL += " float gl_PointSize : PSIZE;\n"
952 " float4 gl_Position : SV_Position;\n"
953 "};\n"
954 "\n"
955 "struct GS_OUTPUT\n"
956 "{\n";
957
958 geomHLSL += varyingHLSL;
959
960 if (fragmentShader->mUsesFragCoord)
961 {
962 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
963 }
964
965 if (fragmentShader->mUsesPointCoord)
966 {
967 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
968 }
969
970 geomHLSL += " float gl_PointSize : PSIZE;\n"
971 " float4 gl_Position : SV_Position;\n"
972 "};\n"
973 "\n"
974 "static float2 pointSpriteCorners[] = \n"
975 "{\n"
976 " float2( 0.5f, -0.5f),\n"
977 " float2( 0.5f, 0.5f),\n"
978 " float2(-0.5f, -0.5f),\n"
979 " float2(-0.5f, 0.5f)\n"
980 "};\n"
981 "\n"
982 "static float2 pointSpriteTexcoords[] = \n"
983 "{\n"
984 " float2(1.0f, 1.0f),\n"
985 " float2(1.0f, 0.0f),\n"
986 " float2(0.0f, 1.0f),\n"
987 " float2(0.0f, 0.0f)\n"
988 "};\n"
989 "\n"
990 "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
991 "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n"
992 "\n"
993 "[maxvertexcount(4)]\n"
994 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
995 "{\n"
996 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
997 " output.gl_PointSize = input[0].gl_PointSize;\n";
998
999 for (int r = 0; r < registers; r++)
1000 {
1001 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1002 }
1003
1004 if (fragmentShader->mUsesFragCoord)
1005 {
1006 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1007 }
1008
1009 geomHLSL += " \n"
1010 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
1011 " float4 gl_Position = input[0].gl_Position;\n"
1012 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
1013
1014 for (int corner = 0; corner < 4; corner++)
1015 {
1016 geomHLSL += " \n"
1017 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1018
1019 if (fragmentShader->mUsesPointCoord)
1020 {
1021 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1022 }
1023
1024 geomHLSL += " outStream.Append(output);\n";
1025 }
1026
1027 geomHLSL += " \n"
1028 " outStream.RestartStrip();\n"
1029 "}\n";
1030
1031 return geomHLSL;
1032}
1033
1034// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001035std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001036{
Jamie Madill033dae62014-06-18 12:56:28 -04001037 if (name.compare(0, 3, "gl_"))
Jamie Madill5f562732014-02-14 16:41:24 -05001038 {
1039 return "_" + name;
1040 }
1041
1042 return name;
1043}
1044
Jamie Madill834e8b72014-04-11 13:33:58 -04001045std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001046{
Jamie Madilla53ab512014-03-17 09:47:44 -04001047 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001048
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001049 // Matrix
1050 if (IsMatrixType(shaderAttrib.type))
1051 {
Jamie Madill8664b062014-02-14 16:41:29 -05001052 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001053 }
1054
Jamie Madill8664b062014-02-14 16:41:29 -05001055 GLenum shaderComponentType = UniformComponentType(shaderAttrib.type);
1056 int shaderComponentCount = UniformComponentCount(shaderAttrib.type);
1057
Jamie Madill8664b062014-02-14 16:41:29 -05001058 // Perform integer to float conversion (if necessary)
1059 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1060
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001061 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001062 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001063 // TODO: normalization for 32-bit integer formats
1064 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1065 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001066 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001067
1068 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001069 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001070}
1071
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001072void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1073{
1074 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1075 {
1076 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1077
1078 if (vertexFormat.mType == GL_NONE)
1079 {
1080 signature[inputIndex] = GL_NONE;
1081 }
1082 else
1083 {
1084 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1085 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1086 }
1087 }
1088}
1089
Jamie Madill5f562732014-02-14 16:41:24 -05001090}