blob: fb65639e5c86496bb1993a6e08303cb42504213a [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);
Geoff Lang4ace4232014-06-18 19:12:48 -0400427
428 // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers
429 bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE
430 if (outputIndexEnabled)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400431 {
432 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
433 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
434
435 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
436 }
437 }
438
439 if (usesFragDepth)
440 {
441 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
442 copyHLSL += " output.gl_Depth = gl_Depth; \n";
443 }
444
445 std::string replacementHLSL = "struct PS_OUTPUT\n"
446 "{\n" +
447 declarationHLSL +
448 "};\n"
449 "\n"
450 "PS_OUTPUT generateOutput()\n"
451 "{\n"
452 " PS_OUTPUT output;\n" +
453 copyHLSL +
454 " return output;\n"
455 "}\n";
456
457 std::string pixelHLSL(sourceShader);
458
459 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
460 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
461
462 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500463}
464
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400465bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500466 std::string& pixelHLSL, std::string& vertexHLSL,
467 FragmentShader *fragmentShader, VertexShader *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500468 const std::vector<std::string>& transformFeedbackVaryings,
469 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400470 std::map<int, VariableLocation> *programOutputVars,
471 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
472 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500473{
474 if (pixelHLSL.empty() || vertexHLSL.empty())
475 {
476 return false;
477 }
478
479 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
480 bool usesFragColor = fragmentShader->mUsesFragColor;
481 bool usesFragData = fragmentShader->mUsesFragData;
482 if (usesFragColor && usesFragData)
483 {
484 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
485 return false;
486 }
487
488 // Write the HLSL input/output declarations
489 const int shaderModel = mRenderer->getMajorShaderModel();
490 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
491
492 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
493
494 // Two cases when writing to gl_FragColor and using ESSL 1.0:
495 // - with a 3.0 context, the output color is copied to channel 0
496 // - with a 2.0 context, the output color is broadcast to all channels
497 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
498 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
499
500 int shaderVersion = vertexShader->getShaderVersion();
501
502 if (registersNeeded > maxVaryingVectors)
503 {
504 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
505
506 return false;
507 }
508
509 std::string varyingSemantic = (vertexShader->mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
510 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
Geoff Lang48dcae72014-02-05 16:28:24 -0500511 std::string dxPositionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
Jamie Madill5f562732014-02-14 16:41:24 -0500512
Geoff Lang48dcae72014-02-05 16:28:24 -0500513 std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500514
515 // special varyings that use reserved registers
516 int reservedRegisterIndex = registers;
Jamie Madill5f562732014-02-14 16:41:24 -0500517
Geoff Lang48dcae72014-02-05 16:28:24 -0500518 unsigned int glPositionSemanticIndex = reservedRegisterIndex++;
519 std::string glPositionSemantic = varyingSemantic;
520
521 std::string fragCoordSemantic;
522 unsigned int fragCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500523 if (fragmentShader->mUsesFragCoord)
524 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500525 fragCoordSemanticIndex = reservedRegisterIndex++;
526 fragCoordSemantic = varyingSemantic;
Jamie Madill5f562732014-02-14 16:41:24 -0500527 }
528
Geoff Lang48dcae72014-02-05 16:28:24 -0500529 std::string pointCoordSemantic;
530 unsigned int pointCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500531 if (fragmentShader->mUsesPointCoord)
532 {
533 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
534 // In DX11 we compute this in the GS.
535 if (shaderModel == 3)
536 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500537 pointCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500538 pointCoordSemantic = "TEXCOORD0";
539 }
540 else if (shaderModel >= 4)
541 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500542 pointCoordSemanticIndex = reservedRegisterIndex++;
543 pointCoordSemantic = varyingSemantic;
Jamie Madill5f562732014-02-14 16:41:24 -0500544 }
545 }
546
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500547 // Add stub string to be replaced when shader is dynamically defined by its layout
548 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500549
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500550 vertexHLSL += "struct VS_OUTPUT\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500551 "{\n";
552
553 if (shaderModel < 4)
554 {
Jamie Madill2bf8b372014-06-16 17:18:51 -0400555 vertexHLSL += " float4 dx_Position : " + dxPositionSemantic + ";\n";
Geoff Lang48dcae72014-02-05 16:28:24 -0500556 vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n";
557 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1));
558
Jamie Madill5f562732014-02-14 16:41:24 -0500559 }
560
561 vertexHLSL += varyingHLSL;
562
563 if (fragmentShader->mUsesFragCoord)
564 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500565 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n";
566 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, fragCoordSemantic, fragCoordSemanticIndex, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500567 }
568
569 if (vertexShader->mUsesPointSize && shaderModel >= 3)
570 {
571 vertexHLSL += " float gl_PointSize : PSIZE;\n";
Geoff Lang48dcae72014-02-05 16:28:24 -0500572 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500573 }
574
575 if (shaderModel >= 4)
576 {
Jamie Madill2bf8b372014-06-16 17:18:51 -0400577 vertexHLSL += " float4 dx_Position : " + dxPositionSemantic + ";\n";
Geoff Lang48dcae72014-02-05 16:28:24 -0500578 vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n";
579 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500580 }
581
582 vertexHLSL += "};\n"
583 "\n"
584 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500585 "{\n"
586 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500587
588 if (shaderModel >= 4)
589 {
590 vertexHLSL += "\n"
591 " gl_main();\n"
592 "\n"
593 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500594 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400595 " output.dx_Position.x = gl_Position.x;\n"
596 " output.dx_Position.y = -gl_Position.y;\n"
597 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
598 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500599 }
600 else
601 {
602 vertexHLSL += "\n"
603 " gl_main();\n"
604 "\n"
605 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500606 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400607 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
608 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
609 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
610 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500611 }
612
613 if (vertexShader->mUsesPointSize && shaderModel >= 3)
614 {
615 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
616 }
617
618 if (fragmentShader->mUsesFragCoord)
619 {
620 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
621 }
622
623 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
624 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400625 const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex];
626 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500627 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400628 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500629 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400630 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500631
632 for (int row = 0; row < variableRows; row++)
633 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400634 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500635 vertexHLSL += " output.v" + Str(r);
636
637 bool sharedRegister = false; // Register used by multiple varyings
638
639 for (int x = 0; x < 4; x++)
640 {
641 if (packing[r][x] && packing[r][x] != packing[r][0])
642 {
643 sharedRegister = true;
644 break;
645 }
646 }
647
648 if(sharedRegister)
649 {
650 vertexHLSL += ".";
651
652 for (int x = 0; x < 4; x++)
653 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400654 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500655 {
656 switch(x)
657 {
658 case 0: vertexHLSL += "x"; break;
659 case 1: vertexHLSL += "y"; break;
660 case 2: vertexHLSL += "z"; break;
661 case 3: vertexHLSL += "w"; break;
662 }
663 }
664 }
665 }
666
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400667 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500668
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400669 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500670 {
671 vertexHLSL += ArrayString(elementIndex);
672 }
673
674 if (variableRows > 1)
675 {
676 vertexHLSL += ArrayString(row);
677 }
678
679 vertexHLSL += ";\n";
680 }
681 }
682 }
683 }
684
685 vertexHLSL += "\n"
686 " return output;\n"
687 "}\n";
688
689 pixelHLSL += "struct PS_INPUT\n"
690 "{\n";
691
692 pixelHLSL += varyingHLSL;
693
694 if (fragmentShader->mUsesFragCoord)
695 {
Geoff Langb5b02852014-04-16 14:39:36 -0400696 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500697 }
698
699 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
700 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500701 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + Str(pointCoordSemanticIndex) + ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500702 }
703
704 // Must consume the PSIZE element if the geometry shader is not active
705 // We won't know if we use a GS until we draw
706 if (vertexShader->mUsesPointSize && shaderModel >= 4)
707 {
708 pixelHLSL += " float gl_PointSize : PSIZE;\n";
709 }
710
711 if (fragmentShader->mUsesFragCoord)
712 {
713 if (shaderModel >= 4)
714 {
715 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
716 }
717 else if (shaderModel >= 3)
718 {
719 pixelHLSL += " float2 dx_VPos : VPOS;\n";
720 }
721 }
722
Geoff Lang04fb89a2014-06-09 15:05:36 -0400723 pixelHLSL += "};\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500724
725 if (shaderVersion < 300)
726 {
727 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
728 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400729 PixelShaderOuputVariable outputKeyVariable;
730 outputKeyVariable.type = GL_FLOAT_VEC4;
731 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
732 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
733 outputKeyVariable.outputIndex = renderTargetIndex;
734
735 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500736 }
737
Geoff Lang04fb89a2014-06-09 15:05:36 -0400738 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500739 }
740 else
741 {
742 defineOutputVariables(fragmentShader, programOutputVars);
743
Jamie Madill834e8b72014-04-11 13:33:58 -0400744 const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500745 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
746 {
747 const VariableLocation &outputLocation = locationIt->second;
Jamie Madill834e8b72014-04-11 13:33:58 -0400748 const ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400749 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500750 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
751
Geoff Lang04fb89a2014-06-09 15:05:36 -0400752 PixelShaderOuputVariable outputKeyVariable;
753 outputKeyVariable.type = outputVariable.type;
754 outputKeyVariable.name = variableName + elementString;
755 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
756 outputKeyVariable.outputIndex = locationIt->first;
757
758 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500759 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400760
761 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500762 }
763
Geoff Lang04fb89a2014-06-09 15:05:36 -0400764 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500765
766 if (fragmentShader->mUsesFrontFacing)
767 {
768 if (shaderModel >= 4)
769 {
770 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
771 "{\n";
772 }
773 else
774 {
775 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
776 "{\n";
777 }
778 }
779 else
780 {
781 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
782 "{\n";
783 }
784
785 if (fragmentShader->mUsesFragCoord)
786 {
787 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
788
789 if (shaderModel >= 4)
790 {
791 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
792 " gl_FragCoord.y = input.dx_VPos.y;\n";
793 }
794 else if (shaderModel >= 3)
795 {
796 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
797 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
798 }
799 else
800 {
801 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
802 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
803 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
804 }
805
806 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
807 " gl_FragCoord.w = rhw;\n";
808 }
809
810 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
811 {
812 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
813 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
814 }
815
816 if (fragmentShader->mUsesFrontFacing)
817 {
818 if (shaderModel <= 3)
819 {
820 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
821 }
822 else
823 {
824 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
825 }
826 }
827
828 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
829 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400830 const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex];
831 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500832 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400833 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500834 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400835 GLenum transposedType = TransposeMatrixType(varying.type);
836 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500837 for (int row = 0; row < variableRows; row++)
838 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400839 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
840 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500841
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400842 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500843 {
844 pixelHLSL += ArrayString(elementIndex);
845 }
846
847 if (variableRows > 1)
848 {
849 pixelHLSL += ArrayString(row);
850 }
851
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400852 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500853 {
854 pixelHLSL += " = input.v" + n + ";\n"; break;
855 }
856 else
857 {
858 switch (VariableColumnCount(transposedType))
859 {
860 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
861 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
862 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
863 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
864 default: UNREACHABLE();
865 }
866 }
867 }
868 }
869 }
870 else UNREACHABLE();
871 }
872
873 pixelHLSL += "\n"
874 " gl_main();\n"
875 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400876 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500877 "}\n";
878
879 return true;
880}
881
882void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
883{
Jamie Madill834e8b72014-04-11 13:33:58 -0400884 const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500885
886 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
887 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400888 const Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500889 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
890
891 if (outputVariable.arraySize > 0)
892 {
893 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
894 {
895 const int location = baseLocation + elementIndex;
896 ASSERT(programOutputVars->count(location) == 0);
897 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
898 }
899 }
900 else
901 {
902 ASSERT(programOutputVars->count(baseLocation) == 0);
903 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
904 }
905 }
906}
907
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400908std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500909{
910 // for now we only handle point sprite emulation
911 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400912 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -0500913}
914
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400915std::string DynamicHLSL::generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500916{
917 ASSERT(registers >= 0);
918 ASSERT(vertexShader->mUsesPointSize);
919 ASSERT(mRenderer->getMajorShaderModel() >= 4);
920
921 std::string geomHLSL;
922
923 std::string varyingSemantic = "TEXCOORD";
924
925 std::string fragCoordSemantic;
926 std::string pointCoordSemantic;
927
928 int reservedRegisterIndex = registers;
929
930 if (fragmentShader->mUsesFragCoord)
931 {
932 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
933 }
934
935 if (fragmentShader->mUsesPointCoord)
936 {
937 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
938 }
939
940 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
941 "\n"
942 "struct GS_INPUT\n"
943 "{\n";
944
Geoff Lang48dcae72014-02-05 16:28:24 -0500945 std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, NULL);
Jamie Madill5f562732014-02-14 16:41:24 -0500946
947 geomHLSL += varyingHLSL;
948
949 if (fragmentShader->mUsesFragCoord)
950 {
951 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
952 }
953
954 geomHLSL += " float gl_PointSize : PSIZE;\n"
955 " float4 gl_Position : SV_Position;\n"
956 "};\n"
957 "\n"
958 "struct GS_OUTPUT\n"
959 "{\n";
960
961 geomHLSL += varyingHLSL;
962
963 if (fragmentShader->mUsesFragCoord)
964 {
965 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
966 }
967
968 if (fragmentShader->mUsesPointCoord)
969 {
970 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
971 }
972
973 geomHLSL += " float gl_PointSize : PSIZE;\n"
974 " float4 gl_Position : SV_Position;\n"
975 "};\n"
976 "\n"
977 "static float2 pointSpriteCorners[] = \n"
978 "{\n"
979 " float2( 0.5f, -0.5f),\n"
980 " float2( 0.5f, 0.5f),\n"
981 " float2(-0.5f, -0.5f),\n"
982 " float2(-0.5f, 0.5f)\n"
983 "};\n"
984 "\n"
985 "static float2 pointSpriteTexcoords[] = \n"
986 "{\n"
987 " float2(1.0f, 1.0f),\n"
988 " float2(1.0f, 0.0f),\n"
989 " float2(0.0f, 1.0f),\n"
990 " float2(0.0f, 0.0f)\n"
991 "};\n"
992 "\n"
993 "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
994 "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n"
995 "\n"
996 "[maxvertexcount(4)]\n"
997 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
998 "{\n"
999 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
1000 " output.gl_PointSize = input[0].gl_PointSize;\n";
1001
1002 for (int r = 0; r < registers; r++)
1003 {
1004 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1005 }
1006
1007 if (fragmentShader->mUsesFragCoord)
1008 {
1009 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1010 }
1011
1012 geomHLSL += " \n"
1013 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
1014 " float4 gl_Position = input[0].gl_Position;\n"
1015 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
1016
1017 for (int corner = 0; corner < 4; corner++)
1018 {
1019 geomHLSL += " \n"
1020 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
1021
1022 if (fragmentShader->mUsesPointCoord)
1023 {
1024 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1025 }
1026
1027 geomHLSL += " outStream.Append(output);\n";
1028 }
1029
1030 geomHLSL += " \n"
1031 " outStream.RestartStrip();\n"
1032 "}\n";
1033
1034 return geomHLSL;
1035}
1036
1037// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001038std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001039{
Jamie Madill033dae62014-06-18 12:56:28 -04001040 if (name.compare(0, 3, "gl_"))
Jamie Madill5f562732014-02-14 16:41:24 -05001041 {
1042 return "_" + name;
1043 }
1044
1045 return name;
1046}
1047
Jamie Madill834e8b72014-04-11 13:33:58 -04001048std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001049{
Jamie Madilla53ab512014-03-17 09:47:44 -04001050 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001051
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001052 // Matrix
1053 if (IsMatrixType(shaderAttrib.type))
1054 {
Jamie Madill8664b062014-02-14 16:41:29 -05001055 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001056 }
1057
Jamie Madill8664b062014-02-14 16:41:29 -05001058 GLenum shaderComponentType = UniformComponentType(shaderAttrib.type);
1059 int shaderComponentCount = UniformComponentCount(shaderAttrib.type);
1060
Jamie Madill8664b062014-02-14 16:41:29 -05001061 // Perform integer to float conversion (if necessary)
1062 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1063
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001064 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001065 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001066 // TODO: normalization for 32-bit integer formats
1067 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1068 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001069 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001070
1071 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001072 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001073}
1074
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001075void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1076{
1077 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1078 {
1079 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1080
1081 if (vertexFormat.mType == GL_NONE)
1082 {
1083 signature[inputIndex] = GL_NONE;
1084 }
1085 else
1086 {
1087 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1088 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1089 }
1090 }
1091}
1092
Jamie Madill5f562732014-02-14 16:41:24 -05001093}