blob: 8ec313880a08f2e86724026abeaa998dd5597260 [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
Jamie Madillc5ede1a2014-02-14 16:41:27 -050085const std::string DynamicHLSL::VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
86
Jamie Madill5f562732014-02-14 16:41:24 -050087DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
88 : mRenderer(renderer)
89{
90}
91
Jamie Madill834e8b72014-04-11 13:33:58 -040092static bool packVarying(Varying *varying, const int maxVaryingVectors, const ShaderVariable *packing[][4])
Jamie Madill5f562732014-02-14 16:41:24 -050093{
Geoff Lang48dcae72014-02-05 16:28:24 -050094 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -050095
Geoff Lang48dcae72014-02-05 16:28:24 -050096 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -040097 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -050098 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
99 bool success = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500100
Geoff Lang48dcae72014-02-05 16:28:24 -0500101 if (elements == 2 || elements == 3 || elements == 4)
Jamie Madill5f562732014-02-14 16:41:24 -0500102 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500103 for (int r = 0; r <= maxVaryingVectors - registers && !success; 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
130 success = true;
131 }
132 }
133
134 if (!success && elements == 2)
135 {
136 for (int r = maxVaryingVectors - registers; r >= 0 && !success; 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
163 success = true;
164 }
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
Geoff Lang48dcae72014-02-05 16:28:24 -0500207 success = true;
208 }
209 }
210 else UNREACHABLE();
211
212 return success;
213}
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 Madill834e8b72014-04-11 13:33:58 -0400217int DynamicHLSL::packVaryings(InfoLog &infoLog, const ShaderVariable *packing[][4], 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 Madill834e8b72014-04-11 13:33:58 -0400229 Varying *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 Madill834e8b72014-04-11 13:33:58 -0400249 Varying *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 Madill834e8b72014-04-11 13:33:58 -0400292 Varying *varying = &shader->mVaryings[varyingIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500293 if (varying->registerAssigned())
294 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500295 GLenum transposedType = TransposeMatrixType(varying->type);
296 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
297
Jamie Madill5f562732014-02-14 16:41:24 -0500298 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
299 {
Jamie Madill5f562732014-02-14 16:41:24 -0500300 for (int row = 0; row < variableRows; row++)
301 {
302 switch (varying->interpolation)
303 {
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
Geoff Lang48dcae72014-02-05 16:28:24 -0500310 unsigned int semanticIndex = elementIndex * variableRows + varying->registerIndex + row;
311 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
315 if (varying->isStruct())
316 {
317 // matrices within structs are not transposed, so
318 // do not use the special struct prefix "rm"
319 typeString = decorateVariable(varying->structName);
320 }
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 {
333 linkedVaryings->push_back(LinkedVarying(varying->name, varying->type, varying->elementCount(),
334 varyingSemantic, varying->registerIndex,
335 variableRows * varying->elementCount()));
336 }
Jamie Madill5f562732014-02-14 16:41:24 -0500337 }
Jamie Madill5f562732014-02-14 16:41:24 -0500338 }
339
340 return varyingHLSL;
341}
342
Jamie Madill834e8b72014-04-11 13:33:58 -0400343std::string DynamicHLSL::generateInputLayoutHLSL(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
Jamie Madill3b7e2052014-03-17 09:47:43 -0400396 return "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";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500405}
406
Jamie Madill834e8b72014-04-11 13:33:58 -0400407bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const ShaderVariable *packing[][4],
Jamie Madill5f562732014-02-14 16:41:24 -0500408 std::string& pixelHLSL, std::string& vertexHLSL,
409 FragmentShader *fragmentShader, VertexShader *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500410 const std::vector<std::string>& transformFeedbackVaryings,
411 std::vector<LinkedVarying> *linkedVaryings,
Jamie Madill5f562732014-02-14 16:41:24 -0500412 std::map<int, VariableLocation> *programOutputVars) const
413{
414 if (pixelHLSL.empty() || vertexHLSL.empty())
415 {
416 return false;
417 }
418
419 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
420 bool usesFragColor = fragmentShader->mUsesFragColor;
421 bool usesFragData = fragmentShader->mUsesFragData;
422 if (usesFragColor && usesFragData)
423 {
424 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
425 return false;
426 }
427
428 // Write the HLSL input/output declarations
429 const int shaderModel = mRenderer->getMajorShaderModel();
430 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
431
432 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
433
434 // Two cases when writing to gl_FragColor and using ESSL 1.0:
435 // - with a 3.0 context, the output color is copied to channel 0
436 // - with a 2.0 context, the output color is broadcast to all channels
437 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
438 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
439
440 int shaderVersion = vertexShader->getShaderVersion();
441
442 if (registersNeeded > maxVaryingVectors)
443 {
444 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
445
446 return false;
447 }
448
449 std::string varyingSemantic = (vertexShader->mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
450 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
Geoff Lang48dcae72014-02-05 16:28:24 -0500451 std::string dxPositionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
Jamie Madill5f562732014-02-14 16:41:24 -0500452 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
453
Geoff Lang48dcae72014-02-05 16:28:24 -0500454 std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500455
456 // special varyings that use reserved registers
457 int reservedRegisterIndex = registers;
Jamie Madill5f562732014-02-14 16:41:24 -0500458
Geoff Lang48dcae72014-02-05 16:28:24 -0500459 unsigned int glPositionSemanticIndex = reservedRegisterIndex++;
460 std::string glPositionSemantic = varyingSemantic;
461
462 std::string fragCoordSemantic;
463 unsigned int fragCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500464 if (fragmentShader->mUsesFragCoord)
465 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500466 fragCoordSemanticIndex = reservedRegisterIndex++;
467 fragCoordSemantic = varyingSemantic;
Jamie Madill5f562732014-02-14 16:41:24 -0500468 }
469
Geoff Lang48dcae72014-02-05 16:28:24 -0500470 std::string pointCoordSemantic;
471 unsigned int pointCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500472 if (fragmentShader->mUsesPointCoord)
473 {
474 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
475 // In DX11 we compute this in the GS.
476 if (shaderModel == 3)
477 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500478 pointCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500479 pointCoordSemantic = "TEXCOORD0";
480 }
481 else if (shaderModel >= 4)
482 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500483 pointCoordSemanticIndex = reservedRegisterIndex++;
484 pointCoordSemantic = varyingSemantic;
Jamie Madill5f562732014-02-14 16:41:24 -0500485 }
486 }
487
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500488 // Add stub string to be replaced when shader is dynamically defined by its layout
489 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500490
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500491 vertexHLSL += "struct VS_OUTPUT\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500492 "{\n";
493
494 if (shaderModel < 4)
495 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500496 vertexHLSL += " float4 _dx_Position : " + dxPositionSemantic + ";\n";
497 vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n";
498 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1));
499
Jamie Madill5f562732014-02-14 16:41:24 -0500500 }
501
502 vertexHLSL += varyingHLSL;
503
504 if (fragmentShader->mUsesFragCoord)
505 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500506 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n";
507 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, fragCoordSemantic, fragCoordSemanticIndex, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500508 }
509
510 if (vertexShader->mUsesPointSize && shaderModel >= 3)
511 {
512 vertexHLSL += " float gl_PointSize : PSIZE;\n";
Geoff Lang48dcae72014-02-05 16:28:24 -0500513 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500514 }
515
516 if (shaderModel >= 4)
517 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500518 vertexHLSL += " float4 _dx_Position : " + dxPositionSemantic + ";\n";
519 vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n";
520 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500521 }
522
523 vertexHLSL += "};\n"
524 "\n"
525 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500526 "{\n"
527 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500528
529 if (shaderModel >= 4)
530 {
531 vertexHLSL += "\n"
532 " gl_main();\n"
533 "\n"
534 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500535 " output.gl_Position = gl_Position;\n"
536 " output._dx_Position.x = gl_Position.x;\n"
537 " output._dx_Position.y = -gl_Position.y;\n"
538 " output._dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
539 " output._dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500540 }
541 else
542 {
543 vertexHLSL += "\n"
544 " gl_main();\n"
545 "\n"
546 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500547 " output.gl_Position = gl_Position;\n"
548 " output._dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
549 " output._dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
550 " output._dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
551 " output._dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500552 }
553
554 if (vertexShader->mUsesPointSize && shaderModel >= 3)
555 {
556 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
557 }
558
559 if (fragmentShader->mUsesFragCoord)
560 {
561 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
562 }
563
564 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
565 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400566 Varying *varying = &vertexShader->mVaryings[vertVaryingIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500567 if (varying->registerAssigned())
568 {
569 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
570 {
571 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying->type)));
572
573 for (int row = 0; row < variableRows; row++)
574 {
575 int r = varying->registerIndex + elementIndex * variableRows + row;
576 vertexHLSL += " output.v" + Str(r);
577
578 bool sharedRegister = false; // Register used by multiple varyings
579
580 for (int x = 0; x < 4; x++)
581 {
582 if (packing[r][x] && packing[r][x] != packing[r][0])
583 {
584 sharedRegister = true;
585 break;
586 }
587 }
588
589 if(sharedRegister)
590 {
591 vertexHLSL += ".";
592
593 for (int x = 0; x < 4; x++)
594 {
595 if (packing[r][x] == &*varying)
596 {
597 switch(x)
598 {
599 case 0: vertexHLSL += "x"; break;
600 case 1: vertexHLSL += "y"; break;
601 case 2: vertexHLSL += "z"; break;
602 case 3: vertexHLSL += "w"; break;
603 }
604 }
605 }
606 }
607
608 vertexHLSL += " = _" + varying->name;
609
610 if (varying->isArray())
611 {
612 vertexHLSL += ArrayString(elementIndex);
613 }
614
615 if (variableRows > 1)
616 {
617 vertexHLSL += ArrayString(row);
618 }
619
620 vertexHLSL += ";\n";
621 }
622 }
623 }
624 }
625
626 vertexHLSL += "\n"
627 " return output;\n"
628 "}\n";
629
630 pixelHLSL += "struct PS_INPUT\n"
631 "{\n";
632
633 pixelHLSL += varyingHLSL;
634
635 if (fragmentShader->mUsesFragCoord)
636 {
Geoff Langb5b02852014-04-16 14:39:36 -0400637 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500638 }
639
640 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
641 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500642 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + Str(pointCoordSemanticIndex) + ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500643 }
644
645 // Must consume the PSIZE element if the geometry shader is not active
646 // We won't know if we use a GS until we draw
647 if (vertexShader->mUsesPointSize && shaderModel >= 4)
648 {
649 pixelHLSL += " float gl_PointSize : PSIZE;\n";
650 }
651
652 if (fragmentShader->mUsesFragCoord)
653 {
654 if (shaderModel >= 4)
655 {
656 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
657 }
658 else if (shaderModel >= 3)
659 {
660 pixelHLSL += " float2 dx_VPos : VPOS;\n";
661 }
662 }
663
664 pixelHLSL += "};\n"
665 "\n"
666 "struct PS_OUTPUT\n"
667 "{\n";
668
669 if (shaderVersion < 300)
670 {
671 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
672 {
673 pixelHLSL += " float4 gl_Color" + Str(renderTargetIndex) + " : " + targetSemantic + Str(renderTargetIndex) + ";\n";
674 }
675
676 if (fragmentShader->mUsesFragDepth)
677 {
678 pixelHLSL += " float gl_Depth : " + depthSemantic + ";\n";
679 }
680 }
681 else
682 {
683 defineOutputVariables(fragmentShader, programOutputVars);
684
Jamie Madill834e8b72014-04-11 13:33:58 -0400685 const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500686 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
687 {
688 const VariableLocation &outputLocation = locationIt->second;
Jamie Madill834e8b72014-04-11 13:33:58 -0400689 const ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Jamie Madill5f562732014-02-14 16:41:24 -0500690 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
691
Jamie Madill8664b062014-02-14 16:41:29 -0500692 pixelHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) +
Jamie Madill5f562732014-02-14 16:41:24 -0500693 " out_" + outputLocation.name + elementString +
694 " : " + targetSemantic + Str(locationIt->first) + ";\n";
695 }
696 }
697
698 pixelHLSL += "};\n"
699 "\n";
700
701 if (fragmentShader->mUsesFrontFacing)
702 {
703 if (shaderModel >= 4)
704 {
705 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
706 "{\n";
707 }
708 else
709 {
710 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
711 "{\n";
712 }
713 }
714 else
715 {
716 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
717 "{\n";
718 }
719
720 if (fragmentShader->mUsesFragCoord)
721 {
722 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
723
724 if (shaderModel >= 4)
725 {
726 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
727 " gl_FragCoord.y = input.dx_VPos.y;\n";
728 }
729 else if (shaderModel >= 3)
730 {
731 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
732 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
733 }
734 else
735 {
736 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
737 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
738 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
739 }
740
741 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
742 " gl_FragCoord.w = rhw;\n";
743 }
744
745 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
746 {
747 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
748 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
749 }
750
751 if (fragmentShader->mUsesFrontFacing)
752 {
753 if (shaderModel <= 3)
754 {
755 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
756 }
757 else
758 {
759 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
760 }
761 }
762
763 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
764 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400765 Varying *varying = &fragmentShader->mVaryings[varyingIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500766 if (varying->registerAssigned())
767 {
768 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
769 {
770 GLenum transposedType = TransposeMatrixType(varying->type);
771 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
772 for (int row = 0; row < variableRows; row++)
773 {
774 std::string n = Str(varying->registerIndex + elementIndex * variableRows + row);
775 pixelHLSL += " _" + varying->name;
776
777 if (varying->isArray())
778 {
779 pixelHLSL += ArrayString(elementIndex);
780 }
781
782 if (variableRows > 1)
783 {
784 pixelHLSL += ArrayString(row);
785 }
786
787 if (varying->isStruct())
788 {
789 pixelHLSL += " = input.v" + n + ";\n"; break;
790 }
791 else
792 {
793 switch (VariableColumnCount(transposedType))
794 {
795 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
796 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
797 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
798 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
799 default: UNREACHABLE();
800 }
801 }
802 }
803 }
804 }
805 else UNREACHABLE();
806 }
807
808 pixelHLSL += "\n"
809 " gl_main();\n"
810 "\n"
811 " PS_OUTPUT output;\n";
812
813 if (shaderVersion < 300)
814 {
815 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
816 {
817 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
818
819 pixelHLSL += " output.gl_Color" + Str(renderTargetIndex) + " = gl_Color[" + Str(sourceColorIndex) + "];\n";
820 }
821
822 if (fragmentShader->mUsesFragDepth)
823 {
824 pixelHLSL += " output.gl_Depth = gl_Depth;\n";
825 }
826 }
827 else
828 {
829 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
830 {
831 const VariableLocation &outputLocation = locationIt->second;
832 const std::string &variableName = "out_" + outputLocation.name;
833 const std::string &outVariableName = variableName + (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
834 const std::string &staticVariableName = variableName + ArrayString(outputLocation.element);
835
836 pixelHLSL += " output." + outVariableName + " = " + staticVariableName + ";\n";
837 }
838 }
839
840 pixelHLSL += "\n"
841 " return output;\n"
842 "}\n";
843
844 return true;
845}
846
847void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
848{
Jamie Madill834e8b72014-04-11 13:33:58 -0400849 const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500850
851 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
852 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400853 const Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500854 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
855
856 if (outputVariable.arraySize > 0)
857 {
858 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
859 {
860 const int location = baseLocation + elementIndex;
861 ASSERT(programOutputVars->count(location) == 0);
862 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
863 }
864 }
865 else
866 {
867 ASSERT(programOutputVars->count(baseLocation) == 0);
868 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
869 }
870 }
871}
872
Jamie Madill834e8b72014-04-11 13:33:58 -0400873std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500874{
875 // for now we only handle point sprite emulation
876 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
877 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
878}
879
Jamie Madill834e8b72014-04-11 13:33:58 -0400880std::string DynamicHLSL::generatePointSpriteHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500881{
882 ASSERT(registers >= 0);
883 ASSERT(vertexShader->mUsesPointSize);
884 ASSERT(mRenderer->getMajorShaderModel() >= 4);
885
886 std::string geomHLSL;
887
888 std::string varyingSemantic = "TEXCOORD";
889
890 std::string fragCoordSemantic;
891 std::string pointCoordSemantic;
892
893 int reservedRegisterIndex = registers;
894
895 if (fragmentShader->mUsesFragCoord)
896 {
897 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
898 }
899
900 if (fragmentShader->mUsesPointCoord)
901 {
902 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
903 }
904
905 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
906 "\n"
907 "struct GS_INPUT\n"
908 "{\n";
909
Geoff Lang48dcae72014-02-05 16:28:24 -0500910 std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, NULL);
Jamie Madill5f562732014-02-14 16:41:24 -0500911
912 geomHLSL += varyingHLSL;
913
914 if (fragmentShader->mUsesFragCoord)
915 {
916 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
917 }
918
919 geomHLSL += " float gl_PointSize : PSIZE;\n"
920 " float4 gl_Position : SV_Position;\n"
921 "};\n"
922 "\n"
923 "struct GS_OUTPUT\n"
924 "{\n";
925
926 geomHLSL += varyingHLSL;
927
928 if (fragmentShader->mUsesFragCoord)
929 {
930 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
931 }
932
933 if (fragmentShader->mUsesPointCoord)
934 {
935 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
936 }
937
938 geomHLSL += " float gl_PointSize : PSIZE;\n"
939 " float4 gl_Position : SV_Position;\n"
940 "};\n"
941 "\n"
942 "static float2 pointSpriteCorners[] = \n"
943 "{\n"
944 " float2( 0.5f, -0.5f),\n"
945 " float2( 0.5f, 0.5f),\n"
946 " float2(-0.5f, -0.5f),\n"
947 " float2(-0.5f, 0.5f)\n"
948 "};\n"
949 "\n"
950 "static float2 pointSpriteTexcoords[] = \n"
951 "{\n"
952 " float2(1.0f, 1.0f),\n"
953 " float2(1.0f, 0.0f),\n"
954 " float2(0.0f, 1.0f),\n"
955 " float2(0.0f, 0.0f)\n"
956 "};\n"
957 "\n"
958 "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
959 "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n"
960 "\n"
961 "[maxvertexcount(4)]\n"
962 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
963 "{\n"
964 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
965 " output.gl_PointSize = input[0].gl_PointSize;\n";
966
967 for (int r = 0; r < registers; r++)
968 {
969 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
970 }
971
972 if (fragmentShader->mUsesFragCoord)
973 {
974 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
975 }
976
977 geomHLSL += " \n"
978 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
979 " float4 gl_Position = input[0].gl_Position;\n"
980 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
981
982 for (int corner = 0; corner < 4; corner++)
983 {
984 geomHLSL += " \n"
985 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
986
987 if (fragmentShader->mUsesPointCoord)
988 {
989 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
990 }
991
992 geomHLSL += " outStream.Append(output);\n";
993 }
994
995 geomHLSL += " \n"
996 " outStream.RestartStrip();\n"
997 "}\n";
998
999 return geomHLSL;
1000}
1001
1002// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001003std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001004{
1005 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
1006 {
1007 return "_" + name;
1008 }
1009
1010 return name;
1011}
1012
Jamie Madill834e8b72014-04-11 13:33:58 -04001013std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001014{
Jamie Madilla53ab512014-03-17 09:47:44 -04001015 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001016
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001017 // Matrix
1018 if (IsMatrixType(shaderAttrib.type))
1019 {
Jamie Madill8664b062014-02-14 16:41:29 -05001020 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001021 }
1022
Jamie Madill8664b062014-02-14 16:41:29 -05001023 GLenum shaderComponentType = UniformComponentType(shaderAttrib.type);
1024 int shaderComponentCount = UniformComponentCount(shaderAttrib.type);
1025
Jamie Madill8664b062014-02-14 16:41:29 -05001026 // Perform integer to float conversion (if necessary)
1027 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1028
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001029 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001030 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001031 // TODO: normalization for 32-bit integer formats
1032 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1033 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001034 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001035
1036 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001037 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001038}
1039
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001040void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1041{
1042 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1043 {
1044 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1045
1046 if (vertexFormat.mType == GL_NONE)
1047 {
1048 signature[inputIndex] = GL_NONE;
1049 }
1050 else
1051 {
1052 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1053 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1054 }
1055 }
1056}
1057
Jamie Madill5f562732014-02-14 16:41:24 -05001058}