blob: 19450ecd0628c215aedcd9edf2c2595ad451c92a [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
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050010
11#include "common/utilities.h"
Daniel Bratell73941de2015-02-25 14:34:49 +010012#include "compiler/translator/blocklayoutHLSL.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/renderer/d3d/ShaderD3D.h"
14#include "libANGLE/renderer/d3d/RendererD3D.h"
15#include "libANGLE/Program.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/Shader.h"
17#include "libANGLE/formatutils.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040018
Jamie Madill53cb14d2014-07-08 15:02:35 -040019// For use with ArrayString, see angleutils.h
Geoff Langd4475812015-03-18 10:53:05 -040020static_assert(GL_INVALID_INDEX == UINT_MAX, "GL_INVALID_INDEX must be equal to the max unsigned int.");
Jamie Madill5f562732014-02-14 16:41:24 -050021
Brandon Jonesd8d72432014-08-22 15:11:23 -070022using namespace gl;
23
Jamie Madill30d6c252014-11-13 10:03:33 -050024namespace rx
25{
26
Jamie Madill3f2e61d2014-09-05 10:38:05 -040027namespace
Jamie Madill5f562732014-02-14 16:41:24 -050028{
Jamie Madill8664b062014-02-14 16:41:29 -050029
30std::string HLSLComponentTypeString(GLenum componentType)
31{
32 switch (componentType)
33 {
34 case GL_UNSIGNED_INT: return "uint";
35 case GL_INT: return "int";
36 case GL_UNSIGNED_NORMALIZED:
37 case GL_SIGNED_NORMALIZED:
38 case GL_FLOAT: return "float";
39 default: UNREACHABLE(); return "not-component-type";
40 }
Jamie Madill5f562732014-02-14 16:41:24 -050041}
42
Jamie Madill8664b062014-02-14 16:41:29 -050043std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
44{
45 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
46}
47
48std::string HLSLMatrixTypeString(GLenum type)
49{
50 switch (type)
51 {
52 case GL_FLOAT_MAT2: return "float2x2";
53 case GL_FLOAT_MAT3: return "float3x3";
54 case GL_FLOAT_MAT4: return "float4x4";
55 case GL_FLOAT_MAT2x3: return "float2x3";
56 case GL_FLOAT_MAT3x2: return "float3x2";
57 case GL_FLOAT_MAT2x4: return "float2x4";
58 case GL_FLOAT_MAT4x2: return "float4x2";
59 case GL_FLOAT_MAT3x4: return "float3x4";
60 case GL_FLOAT_MAT4x3: return "float4x3";
61 default: UNREACHABLE(); return "not-matrix-type";
62 }
63}
64
65std::string HLSLTypeString(GLenum type)
66{
67 if (gl::IsMatrixType(type))
68 {
69 return HLSLMatrixTypeString(type);
70 }
71
Jamie Madillf2575982014-06-25 16:04:54 -040072 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050073}
74
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +000075const PixelShaderOutputVariable *FindOutputAtLocation(const std::vector<PixelShaderOutputVariable> &outputVariables,
Jamie Madill3f2e61d2014-09-05 10:38:05 -040076 unsigned int location)
77{
78 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
79 {
80 if (outputVariables[variableIndex].outputIndex == location)
81 {
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +000082 return &outputVariables[variableIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -040083 }
84 }
85
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +000086 return NULL;
Jamie Madill3f2e61d2014-09-05 10:38:05 -040087}
88
Geoff Lang04fb89a2014-06-09 15:05:36 -040089const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
90const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050091
Jamie Madill30d6c252014-11-13 10:03:33 -050092}
93
Jamie Madill93e13fb2014-11-06 15:27:25 -050094DynamicHLSL::DynamicHLSL(RendererD3D *const renderer)
Jamie Madill5f562732014-02-14 16:41:24 -050095 : mRenderer(renderer)
96{
97}
98
Jamie Madillff0d2ba2014-05-14 13:49:10 -040099static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -0500100{
Jamie Madillf4780c12015-02-11 16:33:11 -0500101 // Make sure we use transposed matrix types to count registers correctly.
102 int registers = 0;
103 int elements = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500104
Jamie Madillf4780c12015-02-11 16:33:11 -0500105 if (varying->isStruct())
106 {
107 registers = HLSLVariableRegisterCount(*varying, true) * varying->elementCount();
108 elements = 4;
109 }
110 else
111 {
112 GLenum transposedType = TransposeMatrixType(varying->type);
113 registers = VariableRowCount(transposedType) * varying->elementCount();
114 elements = VariableColumnCount(transposedType);
115 }
Jamie Madill5f562732014-02-14 16:41:24 -0500116
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400117 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -0500118 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400119 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -0500120 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500121 bool available = true;
122
123 for (int y = 0; y < registers && available; y++)
124 {
125 for (int x = 0; x < elements && available; x++)
126 {
127 if (packing[r + y][x])
128 {
129 available = false;
130 }
131 }
132 }
133
134 if (available)
135 {
136 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700137 varying->columnIndex = 0;
Geoff Lang48dcae72014-02-05 16:28:24 -0500138
139 for (int y = 0; y < registers; y++)
140 {
141 for (int x = 0; x < elements; x++)
142 {
143 packing[r + y][x] = &*varying;
144 }
145 }
146
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400147 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500148 }
149 }
150
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400151 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500152 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400153 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500154 {
155 bool available = true;
156
157 for (int y = 0; y < registers && available; y++)
158 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500159 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500160 {
161 if (packing[r + y][x])
162 {
163 available = false;
164 }
165 }
166 }
167
168 if (available)
169 {
170 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700171 varying->columnIndex = 2;
Jamie Madill5f562732014-02-14 16:41:24 -0500172
173 for (int y = 0; y < registers; y++)
174 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500175 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500176 {
177 packing[r + y][x] = &*varying;
178 }
179 }
180
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400181 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500182 }
183 }
Jamie Madill5f562732014-02-14 16:41:24 -0500184 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500185 }
186 else if (elements == 1)
187 {
188 int space[4] = { 0 };
189
190 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500191 {
Jamie Madill5f562732014-02-14 16:41:24 -0500192 for (int x = 0; x < 4; x++)
193 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500194 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500195 }
196 }
Jamie Madill5f562732014-02-14 16:41:24 -0500197
Geoff Lang48dcae72014-02-05 16:28:24 -0500198 int column = 0;
199
200 for (int x = 0; x < 4; x++)
201 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700202 if (space[x] >= registers && (space[column] < registers || space[x] < space[column]))
Geoff Lang48dcae72014-02-05 16:28:24 -0500203 {
204 column = x;
205 }
206 }
207
208 if (space[column] >= registers)
209 {
210 for (int r = 0; r < maxVaryingVectors; r++)
211 {
212 if (!packing[r][column])
213 {
214 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700215 varying->columnIndex = column;
Geoff Lang48dcae72014-02-05 16:28:24 -0500216
217 for (int y = r; y < r + registers; y++)
218 {
219 packing[y][column] = &*varying;
220 }
221
222 break;
223 }
224 }
225
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400226 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500227 }
228 }
229 else UNREACHABLE();
230
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400231 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500232}
233
234// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
235// Returns the number of used varying registers, or -1 if unsuccesful
Jamie Madill30d6c252014-11-13 10:03:33 -0500236int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, ShaderD3D *fragmentShader,
237 ShaderD3D *vertexShader, const std::vector<std::string> &transformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -0500238{
Geoff Lang3a61c322014-07-10 13:01:54 -0400239 // TODO (geofflang): Use context's caps
240 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Geoff Lang48dcae72014-02-05 16:28:24 -0500241
Brandon Jones71620962014-08-20 14:04:59 -0700242 vertexShader->resetVaryingsRegisterAssignment();
243 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500244
245 std::set<std::string> packedVaryings;
246
Jamie Madilld15250e2014-09-03 09:40:44 -0400247 std::vector<gl::PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
248 std::vector<gl::PackedVarying> &vertexVaryings = vertexShader->getVaryings();
249 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500250 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400251 PackedVarying *varying = &fragmentVaryings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400252
253 // Do not assign registers to built-in or unreferenced varyings
254 if (varying->isBuiltIn() || !varying->staticUse)
255 {
256 continue;
257 }
258
Geoff Lang48dcae72014-02-05 16:28:24 -0500259 if (packVarying(varying, maxVaryingVectors, packing))
260 {
261 packedVaryings.insert(varying->name);
262 }
263 else
Jamie Madill5f562732014-02-14 16:41:24 -0500264 {
265 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500266 return -1;
267 }
268 }
269
Geoff Lang48dcae72014-02-05 16:28:24 -0500270 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
271 {
272 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
Jamie Madill55d611e2014-10-24 16:28:14 -0400273
274 if (transformFeedbackVarying == "gl_Position" || transformFeedbackVarying == "gl_PointSize")
275 {
276 // do not pack builtin XFB varyings
277 continue;
278 }
279
Geoff Lang48dcae72014-02-05 16:28:24 -0500280 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
281 {
282 bool found = false;
Jamie Madilld15250e2014-09-03 09:40:44 -0400283 for (unsigned int varyingIndex = 0; varyingIndex < vertexVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500284 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400285 PackedVarying *varying = &vertexVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500286 if (transformFeedbackVarying == varying->name)
287 {
288 if (!packVarying(varying, maxVaryingVectors, packing))
289 {
290 infoLog.append("Could not pack varying %s", varying->name.c_str());
291 return -1;
292 }
293
294 found = true;
295 break;
296 }
297 }
298
Jamie Madill55d611e2014-10-24 16:28:14 -0400299 if (!found)
Geoff Lang48dcae72014-02-05 16:28:24 -0500300 {
301 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
302 return -1;
303 }
304 }
305 }
306
Jamie Madill5f562732014-02-14 16:41:24 -0500307 // Return the number of used registers
308 int registers = 0;
309
310 for (int r = 0; r < maxVaryingVectors; r++)
311 {
312 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
313 {
314 registers++;
315 }
316 }
317
318 return registers;
319}
320
Jamie Madill54ad4f82014-09-03 09:40:46 -0400321std::string DynamicHLSL::generateVaryingHLSL(const ShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500322{
Brandon Jones71620962014-08-20 14:04:59 -0700323 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500324 std::string varyingHLSL;
325
Jamie Madill54ad4f82014-09-03 09:40:46 -0400326 const std::vector<gl::PackedVarying> &varyings = shader->getVaryings();
327
Jamie Madilld15250e2014-09-03 09:40:44 -0400328 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500329 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400330 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400331 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500332 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400333 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400334 GLenum transposedType = TransposeMatrixType(varying.type);
335 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500336
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400337 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500338 {
Jamie Madill5f562732014-02-14 16:41:24 -0500339 for (int row = 0; row < variableRows; row++)
340 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700341 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many registers being used.
342 // For example, if there are N registers, and we have N vec3 varyings and 1 float varying, then D3D will pack them into N registers.
343 // If the float varying has the 'nointerpolation' modifier on it then we would need N + 1 registers, and D3D compilation will fail.
344
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400345 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500346 {
Jamie Madillf2575982014-06-25 16:04:54 -0400347 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
348 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
349 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500350 default: UNREACHABLE();
351 }
352
Jamie Madillf4780c12015-02-11 16:33:11 -0500353 unsigned int semanticIndex = elementIndex * variableRows +
354 varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors +
355 varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500356 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500357
Jamie Madilla53ab512014-03-17 09:47:44 -0400358 std::string typeString;
359
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400360 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400361 {
Jamie Madillf4780c12015-02-11 16:33:11 -0500362 // TODO(jmadill): pass back translated name from the shader translator
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400363 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400364 }
365 else
366 {
Jamie Madillf2575982014-06-25 16:04:54 -0400367 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400368 int columnCount = VariableColumnCount(transposedType);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400369 typeString = HLSLComponentTypeString(componentType, columnCount);
Jamie Madilla53ab512014-03-17 09:47:44 -0400370 }
Jamie Madill5f562732014-02-14 16:41:24 -0500371 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
372 }
373 }
374 }
Jamie Madill5f562732014-02-14 16:41:24 -0500375 }
376
377 return varyingHLSL;
378}
379
Jamie Madillf2575982014-06-25 16:04:54 -0400380std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
381 const VertexFormat inputLayout[],
382 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500383{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400384 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500385
386 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400387 unsigned int inputIndex = 0;
388
Cooper Partine6d14cc2015-02-20 12:32:58 -0800389 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
390 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
391 // must be used.
392 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
393 bool useInstancedPointSpriteEmulation = usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
394
395 // Instanced PointSprite emulation requires additional entries in the
396 // VS_INPUT structure to support the vertices that make up the quad vertices.
397 // These values must be in sync with the cooresponding values added during inputlayout creation
398 // in InputLayoutCache::applyVertexBuffers().
399 //
400 // The additional entries must appear first in the VS_INPUT layout because
401 // Windows Phone 8 era devices require per vertex data to physically come
402 // before per instance data in the shader.
403 if (useInstancedPointSpriteEmulation)
404 {
405 structHLSL += " float3 spriteVertexPos : SPRITEPOSITION0;\n";
406 structHLSL += " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
407 }
408
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500409 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
410 {
Jamie Madillf2575982014-06-25 16:04:54 -0400411 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500412 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500413 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400414 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
415 const VertexFormat &vertexFormat = inputLayout[inputIndex];
416
Jamie Madill3b7e2052014-03-17 09:47:43 -0400417 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500418 if (IsMatrixType(shaderAttribute.type))
419 {
420 // Matrix types are always transposed
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400421 structHLSL += " " + HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500422 }
423 else
424 {
425 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000426
427 if (shaderAttribute.name == "gl_InstanceID")
428 {
429 // The input type of the instance ID in HLSL (uint) differs from the one in ESSL (int).
430 structHLSL += " uint";
431 }
432 else
433 {
434 structHLSL += " " + HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
435 }
Jamie Madill8664b062014-02-14 16:41:29 -0500436 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500437
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000438 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : ";
439
440 if (shaderAttribute.name == "gl_InstanceID")
441 {
442 structHLSL += "SV_InstanceID";
443 }
444 else
445 {
446 structHLSL += "TEXCOORD" + Str(semanticIndex);
447 semanticIndex += VariableRegisterCount(shaderAttribute.type);
448 }
449
450 structHLSL += ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500451
Jamie Madill3b7e2052014-03-17 09:47:43 -0400452 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400453 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500454
455 // Mismatched vertex attribute to vertex input may result in an undefined
456 // data reinterpretation (eg for pure integer->float, float->pure integer)
457 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400458 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madill30d6c252014-11-13 10:03:33 -0500459 (mRenderer->getVertexConversionType(vertexFormat) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500460 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400461 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500462 }
463 else
464 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400465 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500466 }
467
Jamie Madill3b7e2052014-03-17 09:47:43 -0400468 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400469
Jamie Madillac0a2672014-04-11 13:33:56 -0400470 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
471 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500472 }
473
Geoff Lang04fb89a2014-06-09 15:05:36 -0400474 std::string replacementHLSL = "struct VS_INPUT\n"
475 "{\n" +
476 structHLSL +
477 "};\n"
478 "\n"
479 "void initAttributes(VS_INPUT input)\n"
480 "{\n" +
481 initHLSL +
482 "}\n";
483
484 std::string vertexHLSL(sourceShader);
485
486 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
487 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
488
489 return vertexHLSL;
490}
491
Jamie Madillf6be8d72014-09-05 10:38:07 -0400492std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOutputVariable> &outputVariables,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400493 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
494{
495 const int shaderModel = mRenderer->getMajorShaderModel();
496 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
497 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
498
499 std::string declarationHLSL;
500 std::string copyHLSL;
Geoff Lang4ace4232014-06-18 19:12:48 -0400501
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400502 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
503 {
504 GLenum binding = outputLayout[layoutIndex];
505
506 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400507 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400508 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
509
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000510 const PixelShaderOutputVariable *outputVariable = FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400511
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000512 // OpenGL ES 3.0 spec $4.2.1
513 // If [...] not all user-defined output variables are written, the values of fragment colors
514 // corresponding to unwritten variables are similarly undefined.
515 if (outputVariable)
516 {
517 declarationHLSL += " " + HLSLTypeString(outputVariable->type) + " " + outputVariable->name +
518 " : " + targetSemantic + Str(layoutIndex) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400519
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000520 copyHLSL += " output." + outputVariable->name + " = " + outputVariable->source + ";\n";
521 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400522 }
523 }
524
525 if (usesFragDepth)
526 {
527 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
528 copyHLSL += " output.gl_Depth = gl_Depth; \n";
529 }
530
531 std::string replacementHLSL = "struct PS_OUTPUT\n"
532 "{\n" +
533 declarationHLSL +
534 "};\n"
535 "\n"
536 "PS_OUTPUT generateOutput()\n"
537 "{\n"
538 " PS_OUTPUT output;\n" +
539 copyHLSL +
540 " return output;\n"
541 "}\n";
542
543 std::string pixelHLSL(sourceShader);
544
545 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
546 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
547
548 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500549}
550
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400551std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
552{
553 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
554 // In D3D11 we manually compute gl_PointCoord in the GS.
555 int shaderModel = mRenderer->getMajorShaderModel();
556 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
557}
558
559struct DynamicHLSL::SemanticInfo
560{
561 struct BuiltinInfo
562 {
563 BuiltinInfo()
564 : enabled(false),
565 index(0),
566 systemValue(false)
567 {}
568
569 bool enabled;
570 std::string semantic;
571 unsigned int index;
572 bool systemValue;
573
574 std::string str() const
575 {
576 return (systemValue ? semantic : (semantic + Str(index)));
577 }
578
579 void enableSystem(const std::string &systemValueSemantic)
580 {
581 enabled = true;
582 semantic = systemValueSemantic;
583 systemValue = true;
584 }
585
586 void enable(const std::string &semanticVal, unsigned int indexVal)
587 {
588 enabled = true;
589 semantic = semanticVal;
590 index = indexVal;
591 }
592 };
593
594 BuiltinInfo dxPosition;
595 BuiltinInfo glPosition;
596 BuiltinInfo glFragCoord;
597 BuiltinInfo glPointCoord;
598 BuiltinInfo glPointSize;
599};
600
Austin Kinross8b695ee2015-03-12 13:12:20 -0700601DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool position, bool fragCoord,
602 bool pointCoord, bool pointSize, bool pixelShader) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400603{
604 SemanticInfo info;
605 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
606 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
607
608 int reservedRegisterIndex = startRegisters;
609
610 if (hlsl4)
611 {
612 info.dxPosition.enableSystem("SV_Position");
613 }
614 else if (pixelShader)
615 {
616 info.dxPosition.enableSystem("VPOS");
617 }
618 else
619 {
620 info.dxPosition.enableSystem("POSITION");
621 }
622
Austin Kinross8b695ee2015-03-12 13:12:20 -0700623 if (position)
624 {
625 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
626 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400627
628 if (fragCoord)
629 {
630 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
631 }
632
633 if (pointCoord)
634 {
635 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
636 // In D3D11 we manually compute gl_PointCoord in the GS.
637 if (hlsl4)
638 {
639 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
640 }
641 else
642 {
643 info.glPointCoord.enable("TEXCOORD", 0);
644 }
645 }
646
647 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
648 if (pointSize && (!pixelShader || hlsl4))
649 {
650 info.glPointSize.enableSystem("PSIZE");
651 }
652
653 return info;
654}
655
656std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
657{
658 std::string linkHLSL = "{\n";
659
Austin Kinross8b695ee2015-03-12 13:12:20 -0700660 ASSERT(info.dxPosition.enabled);
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400661 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700662
663 if (info.glPosition.enabled)
664 {
665 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
666 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400667
668 if (info.glFragCoord.enabled)
669 {
670 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
671 }
672
673 if (info.glPointCoord.enabled)
674 {
675 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
676 }
677
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400678 if (info.glPointSize.enabled)
679 {
680 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
681 }
682
Austin Kinross8b695ee2015-03-12 13:12:20 -0700683 // Do this after glPointSize, to potentially combine gl_PointCoord and gl_PointSize into the same register.
684 linkHLSL += varyingHLSL;
685
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400686 linkHLSL += "};\n";
687
688 return linkHLSL;
689}
690
691void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
692 std::vector<LinkedVarying> *linkedVaryings) const
693{
Austin Kinross8b695ee2015-03-12 13:12:20 -0700694 if (info.glPosition.enabled)
695 {
696 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
697 info.glPosition.index, 1));
698 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400699
700 if (info.glFragCoord.enabled)
701 {
702 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
703 info.glFragCoord.index, 1));
704 }
705
706 if (info.glPointSize.enabled)
707 {
708 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
709 }
710}
711
Jamie Madill30d6c252014-11-13 10:03:33 -0500712void DynamicHLSL::storeUserLinkedVaryings(const ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400713 std::vector<LinkedVarying> *linkedVaryings) const
714{
Brandon Jones71620962014-08-20 14:04:59 -0700715 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400716 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400717
718 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
719 {
720 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400721
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400722 if (varying.registerAssigned())
723 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400724 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400725 GLenum transposedType = TransposeMatrixType(varying.type);
726 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
727
728 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
729 varyingSemantic, varying.registerIndex,
730 variableRows * varying.elementCount()));
731 }
732 }
733}
734
Jamie Madillde8892b2014-11-11 13:00:22 -0500735bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data, InfoLog &infoLog, int registers,
736 const VaryingPacking packing,
737 std::string &pixelHLSL, std::string &vertexHLSL,
Jamie Madill30d6c252014-11-13 10:03:33 -0500738 ShaderD3D *fragmentShader, ShaderD3D *vertexShader,
Jamie Madillde8892b2014-11-11 13:00:22 -0500739 const std::vector<std::string> &transformFeedbackVaryings,
Geoff Lang48dcae72014-02-05 16:28:24 -0500740 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400741 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400742 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400743 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500744{
745 if (pixelHLSL.empty() || vertexHLSL.empty())
746 {
747 return false;
748 }
749
Brandon Jones71620962014-08-20 14:04:59 -0700750 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
751 bool usesFragColor = fragmentShader->mUsesFragColor;
752 bool usesFragData = fragmentShader->mUsesFragData;
753 bool usesFragCoord = fragmentShader->mUsesFragCoord;
754 bool usesPointCoord = fragmentShader->mUsesPointCoord;
755 bool usesPointSize = vertexShader->mUsesPointSize;
Cooper Partine6664f02015-01-09 16:22:24 -0800756 bool useInstancedPointSpriteEmulation = usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400757
Jamie Madill5f562732014-02-14 16:41:24 -0500758 if (usesFragColor && usesFragData)
759 {
760 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
761 return false;
762 }
763
764 // Write the HLSL input/output declarations
765 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400766 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500767
768 // Two cases when writing to gl_FragColor and using ESSL 1.0:
769 // - with a 3.0 context, the output color is copied to channel 0
770 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madillde8892b2014-11-11 13:00:22 -0500771 const bool broadcast = (fragmentShader->mUsesFragColor && data.clientVersion < 3);
772 const unsigned int numRenderTargets = (broadcast || usesMRT ? data.caps->maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500773
Austin Kinross8b695ee2015-03-12 13:12:20 -0700774 // gl_Position only needs to be outputted from the vertex shader if transform feedback is active.
775 // This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from the vertex shader in this case.
776 // This saves us 1 output vector.
777 bool outputPositionFromVS = !(shaderModel >= 4 && mRenderer->getShaderModelSuffix() != "");
778
Brandon Jones71620962014-08-20 14:04:59 -0700779 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500780
Jamie Madillde8892b2014-11-11 13:00:22 -0500781 if (static_cast<GLuint>(registersNeeded) > data.caps->maxVaryingVectors)
Jamie Madill5f562732014-02-14 16:41:24 -0500782 {
783 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500784 return false;
785 }
786
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400787 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
Cooper Partine6664f02015-01-09 16:22:24 -0800788
789 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader VS_OUTPUT
790 // structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
791 // GeometryShader PointSprite emulation does not require this additional entry because the
792 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the PS_INPUT of the
793 // generated pixel shader.
Austin Kinross8b695ee2015-03-12 13:12:20 -0700794 // The Geometry Shader point sprite implementation needs gl_PointSize to be in VS_OUTPUT and GS_INPUT.
795 // Instanced point sprites doesn't need gl_PointSize in VS_OUTPUT.
796 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, outputPositionFromVS,
797 usesFragCoord, (useInstancedPointSpriteEmulation && usesPointCoord),
798 (!useInstancedPointSpriteEmulation && usesPointSize), false);
Jamie Madill5f562732014-02-14 16:41:24 -0500799
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400800 storeUserLinkedVaryings(vertexShader, linkedVaryings);
801 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500802
Cooper Partine6664f02015-01-09 16:22:24 -0800803 // Instanced PointSprite emulation requires additional entries originally generated in the
804 // GeometryShader HLSL. These include pointsize clamp values.
805 if (useInstancedPointSpriteEmulation)
806 {
807 vertexHLSL += "static float minPointSize = " + Str((int)mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
808 "static float maxPointSize = " + Str((int)mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n";
809 }
810
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500811 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400812 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
813 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500814 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500815 "{\n"
816 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500817
Jamie Madill37997142015-01-28 10:06:34 -0500818 if (vertexShader->usesDeferredInit())
819 {
820 vertexHLSL += "\n"
821 " initializeDeferredGlobals();\n";
822 }
823
824 vertexHLSL += "\n"
825 " gl_main();\n"
826 "\n"
Austin Kinross8b695ee2015-03-12 13:12:20 -0700827 " VS_OUTPUT output;\n";
828
829 if (outputPositionFromVS)
830 {
831 vertexHLSL += " output.gl_Position = gl_Position;\n";
832 }
Jamie Madill37997142015-01-28 10:06:34 -0500833
Austin Kinross4fd18b12014-12-22 12:32:05 -0800834 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
835 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500836 {
Jamie Madill37997142015-01-28 10:06:34 -0500837 vertexHLSL += " output.dx_Position.x = gl_Position.x;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400838 " output.dx_Position.y = -gl_Position.y;\n"
839 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
840 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500841 }
842 else
843 {
Jamie Madill37997142015-01-28 10:06:34 -0500844 vertexHLSL += " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400845 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
846 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
847 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500848 }
849
Austin Kinross8b695ee2015-03-12 13:12:20 -0700850 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
851 if (usesPointSize && shaderModel >= 3 && !useInstancedPointSpriteEmulation)
Jamie Madill5f562732014-02-14 16:41:24 -0500852 {
853 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
854 }
855
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400856 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500857 {
858 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
859 }
860
Jamie Madilld15250e2014-09-03 09:40:44 -0400861 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
862 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500863 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400864 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400865 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500866 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400867 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500868 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400869 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500870
871 for (int row = 0; row < variableRows; row++)
872 {
Jamie Madillde8892b2014-11-11 13:00:22 -0500873 int r = varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500874 vertexHLSL += " output.v" + Str(r);
875
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400876 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500877
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400878 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500879 {
880 vertexHLSL += ArrayString(elementIndex);
881 }
882
883 if (variableRows > 1)
884 {
885 vertexHLSL += ArrayString(row);
886 }
887
888 vertexHLSL += ";\n";
889 }
890 }
891 }
892 }
893
Cooper Partine6664f02015-01-09 16:22:24 -0800894 // Instanced PointSprite emulation requires additional entries to calculate
895 // the final output vertex positions of the quad that represents each sprite.
896 if (useInstancedPointSpriteEmulation)
897 {
898 vertexHLSL += "\n"
899 " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"
Austin Kinross8b695ee2015-03-12 13:12:20 -0700900 " output.dx_Position.xyz += float3(input.spriteVertexPos.x * gl_PointSize / (dx_ViewCoords.x*2), input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2), input.spriteVertexPos.z) * output.dx_Position.w;\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800901
902 if (usesPointCoord)
903 {
904 vertexHLSL += "\n"
905 " output.gl_PointCoord = input.spriteTexCoord;\n";
906 }
907 }
908
Jamie Madill5f562732014-02-14 16:41:24 -0500909 vertexHLSL += "\n"
910 " return output;\n"
911 "}\n";
912
Austin Kinross8b695ee2015-03-12 13:12:20 -0700913 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, outputPositionFromVS, usesFragCoord, usesPointCoord,
914 (!useInstancedPointSpriteEmulation && usesPointSize), true);
Jamie Madill5f562732014-02-14 16:41:24 -0500915
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400916 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500917
918 if (shaderVersion < 300)
919 {
920 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
921 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400922 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400923 outputKeyVariable.type = GL_FLOAT_VEC4;
924 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
925 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
926 outputKeyVariable.outputIndex = renderTargetIndex;
927
928 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500929 }
930
Brandon Jones71620962014-08-20 14:04:59 -0700931 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500932 }
933 else
934 {
935 defineOutputVariables(fragmentShader, programOutputVars);
936
Jamie Madilld15250e2014-09-03 09:40:44 -0400937 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500938 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
939 {
940 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400941 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400942 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500943 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
944
Jamie Madill54ad4f82014-09-03 09:40:46 -0400945 ASSERT(outputVariable.staticUse);
946
Jamie Madillf6be8d72014-09-05 10:38:07 -0400947 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400948 outputKeyVariable.type = outputVariable.type;
949 outputKeyVariable.name = variableName + elementString;
950 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
951 outputKeyVariable.outputIndex = locationIt->first;
952
953 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500954 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400955
956 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500957 }
958
Geoff Lang04fb89a2014-06-09 15:05:36 -0400959 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500960
Brandon Jones71620962014-08-20 14:04:59 -0700961 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500962 {
963 if (shaderModel >= 4)
964 {
965 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
966 "{\n";
967 }
968 else
969 {
970 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
971 "{\n";
972 }
973 }
974 else
975 {
976 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
977 "{\n";
978 }
979
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400980 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500981 {
982 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
983
Austin Kinross588434c2014-12-10 10:41:45 -0800984 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
985 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using dx_ViewCoords.
986 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500987 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400988 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
989 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500990 }
Austin Kinross588434c2014-12-10 10:41:45 -0800991 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500992 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400993 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
994 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500995 }
996 else
997 {
998 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
999 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
1000 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
1001 }
1002
1003 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
1004 " gl_FragCoord.w = rhw;\n";
1005 }
1006
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001007 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -05001008 {
1009 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1010 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
1011 }
1012
Brandon Jones71620962014-08-20 14:04:59 -07001013 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -05001014 {
1015 if (shaderModel <= 3)
1016 {
1017 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1018 }
1019 else
1020 {
1021 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1022 }
1023 }
1024
Jamie Madilld15250e2014-09-03 09:40:44 -04001025 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
1026 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -05001027 {
Jamie Madilld15250e2014-09-03 09:40:44 -04001028 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001029 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -05001030 {
Jamie Madill54ad4f82014-09-03 09:40:46 -04001031 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001032 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -05001033 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001034 GLenum transposedType = TransposeMatrixType(varying.type);
1035 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -05001036 for (int row = 0; row < variableRows; row++)
1037 {
Jamie Madillde8892b2014-11-11 13:00:22 -05001038 std::string n = Str(varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001039 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -05001040
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001041 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -05001042 {
1043 pixelHLSL += ArrayString(elementIndex);
1044 }
1045
1046 if (variableRows > 1)
1047 {
1048 pixelHLSL += ArrayString(row);
1049 }
1050
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001051 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -05001052 {
1053 pixelHLSL += " = input.v" + n + ";\n"; break;
1054 }
1055 else
1056 {
1057 switch (VariableColumnCount(transposedType))
1058 {
1059 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1060 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1061 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1062 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1063 default: UNREACHABLE();
1064 }
1065 }
1066 }
1067 }
1068 }
Jamie Madill54ad4f82014-09-03 09:40:46 -04001069 else
1070 {
1071 ASSERT(varying.isBuiltIn() || !varying.staticUse);
1072 }
Jamie Madill5f562732014-02-14 16:41:24 -05001073 }
1074
Jamie Madill37997142015-01-28 10:06:34 -05001075 if (fragmentShader->usesDeferredInit())
1076 {
1077 pixelHLSL += "\n"
1078 " initializeDeferredGlobals();\n";
1079 }
1080
Jamie Madill5f562732014-02-14 16:41:24 -05001081 pixelHLSL += "\n"
1082 " gl_main();\n"
1083 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -04001084 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001085 "}\n";
1086
1087 return true;
1088}
1089
Jamie Madill30d6c252014-11-13 10:03:33 -05001090void DynamicHLSL::defineOutputVariables(ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -05001091{
Jamie Madilld15250e2014-09-03 09:40:44 -04001092 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -05001093
1094 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
1095 {
Jamie Madillf2575982014-06-25 16:04:54 -04001096 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -05001097 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
1098
Jamie Madill54ad4f82014-09-03 09:40:46 -04001099 ASSERT(outputVariable.staticUse);
1100
Jamie Madill5f562732014-02-14 16:41:24 -05001101 if (outputVariable.arraySize > 0)
1102 {
1103 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
1104 {
1105 const int location = baseLocation + elementIndex;
1106 ASSERT(programOutputVars->count(location) == 0);
1107 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
1108 }
1109 }
1110 else
1111 {
1112 ASSERT(programOutputVars->count(baseLocation) == 0);
1113 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
1114 }
1115 }
1116}
1117
Jamie Madill30d6c252014-11-13 10:03:33 -05001118std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001119{
1120 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001121 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001122 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001123}
1124
Jamie Madill30d6c252014-11-13 10:03:33 -05001125std::string DynamicHLSL::generatePointSpriteHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001126{
1127 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001128 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001129 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1130
1131 std::string geomHLSL;
1132
Austin Kinross8b695ee2015-03-12 13:12:20 -07001133 const SemanticInfo &inSemantics = getSemanticInfo(registers, true, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001134 false, true, false);
Austin Kinross8b695ee2015-03-12 13:12:20 -07001135 const SemanticInfo &outSemantics = getSemanticInfo(registers, true, fragmentShader->mUsesFragCoord,
Brandon Jones71620962014-08-20 14:04:59 -07001136 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001137
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001138 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1139 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1140 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001141
Geoff Langc0b9ef42014-07-02 10:02:37 -04001142 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001143 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1144 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001145 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1146 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001147 "\n"
Jamie Madill37997142015-01-28 10:06:34 -05001148 "static float2 pointSpriteCorners[] = \n"
1149 "{\n"
1150 " float2( 0.5f, -0.5f),\n"
1151 " float2( 0.5f, 0.5f),\n"
1152 " float2(-0.5f, -0.5f),\n"
1153 " float2(-0.5f, 0.5f)\n"
1154 "};\n"
1155 "\n"
1156 "static float2 pointSpriteTexcoords[] = \n"
1157 "{\n"
1158 " float2(1.0f, 1.0f),\n"
1159 " float2(1.0f, 0.0f),\n"
1160 " float2(0.0f, 1.0f),\n"
1161 " float2(0.0f, 0.0f)\n"
1162 "};\n"
1163 "\n"
Minmin Gong794e0002015-04-07 18:31:54 -07001164 "static float minPointSize = " + Str(static_cast<int>(mRenderer->getRendererCaps().minAliasedPointSize)) + ".0f;\n"
1165 "static float maxPointSize = " + Str(static_cast<int>(mRenderer->getRendererCaps().maxAliasedPointSize)) + ".0f;\n"
Jamie Madill37997142015-01-28 10:06:34 -05001166 "\n"
1167 "[maxvertexcount(4)]\n"
1168 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1169 "{\n"
1170 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
1171 " output.gl_Position = input[0].gl_Position;\n"
1172 " output.gl_PointSize = input[0].gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001173
1174 for (int r = 0; r < registers; r++)
1175 {
1176 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1177 }
1178
Brandon Jones71620962014-08-20 14:04:59 -07001179 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001180 {
1181 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1182 }
1183
1184 geomHLSL += " \n"
1185 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001186 " float4 dx_Position = input[0].dx_Position;\n"
1187 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001188
1189 for (int corner = 0; corner < 4; corner++)
1190 {
1191 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001192 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001193
Brandon Jones71620962014-08-20 14:04:59 -07001194 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001195 {
1196 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1197 }
1198
1199 geomHLSL += " outStream.Append(output);\n";
1200 }
1201
1202 geomHLSL += " \n"
1203 " outStream.RestartStrip();\n"
1204 "}\n";
1205
1206 return geomHLSL;
1207}
1208
1209// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001210std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001211{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001212 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001213 {
1214 return "_" + name;
1215 }
1216
1217 return name;
1218}
1219
Jamie Madillf2575982014-06-25 16:04:54 -04001220std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001221{
Jamie Madilla53ab512014-03-17 09:47:44 -04001222 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001223
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001224 // Matrix
1225 if (IsMatrixType(shaderAttrib.type))
1226 {
Jamie Madill8664b062014-02-14 16:41:29 -05001227 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001228 }
1229
Jamie Madillf2575982014-06-25 16:04:54 -04001230 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1231 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001232
Jamie Madill8664b062014-02-14 16:41:29 -05001233 // Perform integer to float conversion (if necessary)
1234 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1235
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001236 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001237 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001238 // TODO: normalization for 32-bit integer formats
1239 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1240 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001241 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001242
1243 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001244 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001245}
1246
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001247void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1248{
1249 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1250 {
1251 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1252
1253 if (vertexFormat.mType == GL_NONE)
1254 {
1255 signature[inputIndex] = GL_NONE;
1256 }
1257 else
1258 {
Jamie Madill30d6c252014-11-13 10:03:33 -05001259 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & VERTEX_CONVERT_GPU) != 0);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001260 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1261 }
1262 }
1263}
1264
Jamie Madill5f562732014-02-14 16:41:24 -05001265}