blob: 7a12d3618cd6b291b186fa2e8f773850f587b59d [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 {
Jamie Madillf6113162015-05-07 11:49:21 -0400265 infoLog << "Could not pack varying " << varying->name;
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 {
Jamie Madillf6113162015-05-07 11:49:21 -0400290 infoLog << "Could not pack varying " << varying->name;
Geoff Lang48dcae72014-02-05 16:28:24 -0500291 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 {
Jamie Madillf6113162015-05-07 11:49:21 -0400301 infoLog << "Transform feedback varying "
302 << transformFeedbackVarying
303 << " does not exist in the vertex shader.";
Geoff Lang48dcae72014-02-05 16:28:24 -0500304 return -1;
305 }
306 }
307 }
308
Jamie Madill5f562732014-02-14 16:41:24 -0500309 // Return the number of used registers
310 int registers = 0;
311
312 for (int r = 0; r < maxVaryingVectors; r++)
313 {
314 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
315 {
316 registers++;
317 }
318 }
319
320 return registers;
321}
322
Jamie Madill54ad4f82014-09-03 09:40:46 -0400323std::string DynamicHLSL::generateVaryingHLSL(const ShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500324{
Brandon Jones71620962014-08-20 14:04:59 -0700325 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500326 std::string varyingHLSL;
327
Jamie Madill54ad4f82014-09-03 09:40:46 -0400328 const std::vector<gl::PackedVarying> &varyings = shader->getVaryings();
329
Jamie Madilld15250e2014-09-03 09:40:44 -0400330 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500331 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400332 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400333 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500334 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400335 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400336 GLenum transposedType = TransposeMatrixType(varying.type);
337 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500338
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400339 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500340 {
Jamie Madill5f562732014-02-14 16:41:24 -0500341 for (int row = 0; row < variableRows; row++)
342 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700343 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many registers being used.
344 // 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.
345 // If the float varying has the 'nointerpolation' modifier on it then we would need N + 1 registers, and D3D compilation will fail.
346
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400347 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500348 {
Jamie Madillf2575982014-06-25 16:04:54 -0400349 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
350 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
351 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500352 default: UNREACHABLE();
353 }
354
Jamie Madillf4780c12015-02-11 16:33:11 -0500355 unsigned int semanticIndex = elementIndex * variableRows +
356 varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors +
357 varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500358 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500359
Jamie Madilla53ab512014-03-17 09:47:44 -0400360 std::string typeString;
361
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400362 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400363 {
Jamie Madillf4780c12015-02-11 16:33:11 -0500364 // TODO(jmadill): pass back translated name from the shader translator
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400365 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400366 }
367 else
368 {
Jamie Madillf2575982014-06-25 16:04:54 -0400369 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400370 int columnCount = VariableColumnCount(transposedType);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400371 typeString = HLSLComponentTypeString(componentType, columnCount);
Jamie Madilla53ab512014-03-17 09:47:44 -0400372 }
Jamie Madill5f562732014-02-14 16:41:24 -0500373 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
374 }
375 }
376 }
Jamie Madill5f562732014-02-14 16:41:24 -0500377 }
378
379 return varyingHLSL;
380}
381
Jamie Madillf2575982014-06-25 16:04:54 -0400382std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
Jamie Madilld3dfda22015-07-06 08:28:49 -0400383 const InputLayout &inputLayout,
Jamie Madill3da79b72015-04-27 11:09:17 -0400384 const std::vector<sh::Attribute> &shaderAttributes) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500385{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400386 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500387
388 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400389 unsigned int inputIndex = 0;
390
Cooper Partine6d14cc2015-02-20 12:32:58 -0800391 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
392 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
393 // must be used.
394 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
395 bool useInstancedPointSpriteEmulation = usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
396
397 // Instanced PointSprite emulation requires additional entries in the
398 // VS_INPUT structure to support the vertices that make up the quad vertices.
399 // These values must be in sync with the cooresponding values added during inputlayout creation
400 // in InputLayoutCache::applyVertexBuffers().
401 //
402 // The additional entries must appear first in the VS_INPUT layout because
403 // Windows Phone 8 era devices require per vertex data to physically come
404 // before per instance data in the shader.
405 if (useInstancedPointSpriteEmulation)
406 {
407 structHLSL += " float3 spriteVertexPos : SPRITEPOSITION0;\n";
408 structHLSL += " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
409 }
410
Jamie Madill3da79b72015-04-27 11:09:17 -0400411 for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex)
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500412 {
Jamie Madillf2575982014-06-25 16:04:54 -0400413 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500414 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500415 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400416 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400417 VertexFormatType vertexFormatType =
418 inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID;
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400419
Jamie Madill3b7e2052014-03-17 09:47:43 -0400420 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500421 if (IsMatrixType(shaderAttribute.type))
422 {
423 // Matrix types are always transposed
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400424 structHLSL += " " + HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500425 }
426 else
427 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400428 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000429
430 if (shaderAttribute.name == "gl_InstanceID")
431 {
432 // The input type of the instance ID in HLSL (uint) differs from the one in ESSL (int).
433 structHLSL += " uint";
434 }
435 else
436 {
437 structHLSL += " " + HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
438 }
Jamie Madill8664b062014-02-14 16:41:29 -0500439 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500440
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000441 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : ";
442
443 if (shaderAttribute.name == "gl_InstanceID")
444 {
445 structHLSL += "SV_InstanceID";
446 }
447 else
448 {
449 structHLSL += "TEXCOORD" + Str(semanticIndex);
450 semanticIndex += VariableRegisterCount(shaderAttribute.type);
451 }
452
453 structHLSL += ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500454
Jamie Madill3b7e2052014-03-17 09:47:43 -0400455 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400456 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500457
458 // Mismatched vertex attribute to vertex input may result in an undefined
459 // data reinterpretation (eg for pure integer->float, float->pure integer)
460 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400461 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400462 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500463 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400464 initHLSL += generateAttributeConversionHLSL(vertexFormatType, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500465 }
466 else
467 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400468 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500469 }
470
Jamie Madill3b7e2052014-03-17 09:47:43 -0400471 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400472
Jamie Madillac0a2672014-04-11 13:33:56 -0400473 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
474 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500475 }
476
Geoff Lang04fb89a2014-06-09 15:05:36 -0400477 std::string replacementHLSL = "struct VS_INPUT\n"
478 "{\n" +
479 structHLSL +
480 "};\n"
481 "\n"
482 "void initAttributes(VS_INPUT input)\n"
483 "{\n" +
484 initHLSL +
485 "}\n";
486
487 std::string vertexHLSL(sourceShader);
488
489 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
490 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
491
492 return vertexHLSL;
493}
494
Jamie Madillf6be8d72014-09-05 10:38:07 -0400495std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOutputVariable> &outputVariables,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400496 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
497{
498 const int shaderModel = mRenderer->getMajorShaderModel();
499 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
500 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
501
502 std::string declarationHLSL;
503 std::string copyHLSL;
Geoff Lang4ace4232014-06-18 19:12:48 -0400504
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400505 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
506 {
507 GLenum binding = outputLayout[layoutIndex];
508
509 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400510 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400511 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
512
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000513 const PixelShaderOutputVariable *outputVariable = FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400514
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000515 // OpenGL ES 3.0 spec $4.2.1
516 // If [...] not all user-defined output variables are written, the values of fragment colors
517 // corresponding to unwritten variables are similarly undefined.
518 if (outputVariable)
519 {
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700520 declarationHLSL += " " + HLSLTypeString(outputVariable->type) + " " +
521 outputVariable->name + " : " + targetSemantic +
522 Str(static_cast<int>(layoutIndex)) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400523
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000524 copyHLSL += " output." + outputVariable->name + " = " + outputVariable->source + ";\n";
525 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400526 }
527 }
528
529 if (usesFragDepth)
530 {
531 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
532 copyHLSL += " output.gl_Depth = gl_Depth; \n";
533 }
534
535 std::string replacementHLSL = "struct PS_OUTPUT\n"
536 "{\n" +
537 declarationHLSL +
538 "};\n"
539 "\n"
540 "PS_OUTPUT generateOutput()\n"
541 "{\n"
542 " PS_OUTPUT output;\n" +
543 copyHLSL +
544 " return output;\n"
545 "}\n";
546
547 std::string pixelHLSL(sourceShader);
548
549 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
550 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
551
552 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500553}
554
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400555std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
556{
557 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
558 // In D3D11 we manually compute gl_PointCoord in the GS.
559 int shaderModel = mRenderer->getMajorShaderModel();
560 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
561}
562
563struct DynamicHLSL::SemanticInfo
564{
565 struct BuiltinInfo
566 {
567 BuiltinInfo()
568 : enabled(false),
569 index(0),
570 systemValue(false)
571 {}
572
573 bool enabled;
574 std::string semantic;
575 unsigned int index;
576 bool systemValue;
577
578 std::string str() const
579 {
580 return (systemValue ? semantic : (semantic + Str(index)));
581 }
582
583 void enableSystem(const std::string &systemValueSemantic)
584 {
585 enabled = true;
586 semantic = systemValueSemantic;
587 systemValue = true;
588 }
589
590 void enable(const std::string &semanticVal, unsigned int indexVal)
591 {
592 enabled = true;
593 semantic = semanticVal;
594 index = indexVal;
595 }
596 };
597
598 BuiltinInfo dxPosition;
599 BuiltinInfo glPosition;
600 BuiltinInfo glFragCoord;
601 BuiltinInfo glPointCoord;
602 BuiltinInfo glPointSize;
603};
604
Austin Kinross8b695ee2015-03-12 13:12:20 -0700605DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool position, bool fragCoord,
606 bool pointCoord, bool pointSize, bool pixelShader) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400607{
608 SemanticInfo info;
609 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
610 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
611
612 int reservedRegisterIndex = startRegisters;
613
614 if (hlsl4)
615 {
616 info.dxPosition.enableSystem("SV_Position");
617 }
618 else if (pixelShader)
619 {
620 info.dxPosition.enableSystem("VPOS");
621 }
622 else
623 {
624 info.dxPosition.enableSystem("POSITION");
625 }
626
Austin Kinross8b695ee2015-03-12 13:12:20 -0700627 if (position)
628 {
629 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
630 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400631
632 if (fragCoord)
633 {
634 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
635 }
636
637 if (pointCoord)
638 {
639 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
640 // In D3D11 we manually compute gl_PointCoord in the GS.
641 if (hlsl4)
642 {
643 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
644 }
645 else
646 {
647 info.glPointCoord.enable("TEXCOORD", 0);
648 }
649 }
650
651 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
652 if (pointSize && (!pixelShader || hlsl4))
653 {
654 info.glPointSize.enableSystem("PSIZE");
655 }
656
657 return info;
658}
659
660std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
661{
662 std::string linkHLSL = "{\n";
663
Austin Kinross8b695ee2015-03-12 13:12:20 -0700664 ASSERT(info.dxPosition.enabled);
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400665 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700666
667 if (info.glPosition.enabled)
668 {
669 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
670 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400671
672 if (info.glFragCoord.enabled)
673 {
674 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
675 }
676
677 if (info.glPointCoord.enabled)
678 {
679 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
680 }
681
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400682 if (info.glPointSize.enabled)
683 {
684 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
685 }
686
Austin Kinross8b695ee2015-03-12 13:12:20 -0700687 // Do this after glPointSize, to potentially combine gl_PointCoord and gl_PointSize into the same register.
688 linkHLSL += varyingHLSL;
689
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400690 linkHLSL += "};\n";
691
692 return linkHLSL;
693}
694
695void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
696 std::vector<LinkedVarying> *linkedVaryings) const
697{
Austin Kinross8b695ee2015-03-12 13:12:20 -0700698 if (info.glPosition.enabled)
699 {
700 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
701 info.glPosition.index, 1));
702 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400703
704 if (info.glFragCoord.enabled)
705 {
706 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
707 info.glFragCoord.index, 1));
708 }
709
710 if (info.glPointSize.enabled)
711 {
712 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
713 }
714}
715
Jamie Madill30d6c252014-11-13 10:03:33 -0500716void DynamicHLSL::storeUserLinkedVaryings(const ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400717 std::vector<LinkedVarying> *linkedVaryings) const
718{
Brandon Jones71620962014-08-20 14:04:59 -0700719 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400720 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400721
722 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
723 {
724 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400725
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400726 if (varying.registerAssigned())
727 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400728 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400729 GLenum transposedType = TransposeMatrixType(varying.type);
730 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
731
732 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
733 varyingSemantic, varying.registerIndex,
734 variableRows * varying.elementCount()));
735 }
736 }
737}
738
Jamie Madillde8892b2014-11-11 13:00:22 -0500739bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data, InfoLog &infoLog, int registers,
740 const VaryingPacking packing,
741 std::string &pixelHLSL, std::string &vertexHLSL,
Jamie Madill30d6c252014-11-13 10:03:33 -0500742 ShaderD3D *fragmentShader, ShaderD3D *vertexShader,
Jamie Madillde8892b2014-11-11 13:00:22 -0500743 const std::vector<std::string> &transformFeedbackVaryings,
Geoff Lang48dcae72014-02-05 16:28:24 -0500744 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400745 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400746 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400747 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500748{
749 if (pixelHLSL.empty() || vertexHLSL.empty())
750 {
751 return false;
752 }
753
Brandon Jones71620962014-08-20 14:04:59 -0700754 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
Brandon Jones71620962014-08-20 14:04:59 -0700755 bool usesFragCoord = fragmentShader->mUsesFragCoord;
756 bool usesPointCoord = fragmentShader->mUsesPointCoord;
757 bool usesPointSize = vertexShader->mUsesPointSize;
Cooper Partine6664f02015-01-09 16:22:24 -0800758 bool useInstancedPointSpriteEmulation = usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400759
Jamie Madill14e95b32015-05-07 10:10:41 -0400760 // Validation done in the compiler
761 ASSERT(!fragmentShader->mUsesFragColor || !fragmentShader->mUsesFragData);
Jamie Madill5f562732014-02-14 16:41:24 -0500762
763 // Write the HLSL input/output declarations
764 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400765 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500766
767 // Two cases when writing to gl_FragColor and using ESSL 1.0:
768 // - with a 3.0 context, the output color is copied to channel 0
769 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madillde8892b2014-11-11 13:00:22 -0500770 const bool broadcast = (fragmentShader->mUsesFragColor && data.clientVersion < 3);
771 const unsigned int numRenderTargets = (broadcast || usesMRT ? data.caps->maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500772
Austin Kinross8b695ee2015-03-12 13:12:20 -0700773 // gl_Position only needs to be outputted from the vertex shader if transform feedback is active.
774 // This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from the vertex shader in this case.
775 // This saves us 1 output vector.
776 bool outputPositionFromVS = !(shaderModel >= 4 && mRenderer->getShaderModelSuffix() != "");
777
Brandon Jones71620962014-08-20 14:04:59 -0700778 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500779
Jamie Madillde8892b2014-11-11 13:00:22 -0500780 if (static_cast<GLuint>(registersNeeded) > data.caps->maxVaryingVectors)
Jamie Madill5f562732014-02-14 16:41:24 -0500781 {
Jamie Madillf6113162015-05-07 11:49:21 -0400782 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madill5f562732014-02-14 16:41:24 -0500783 return false;
784 }
785
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400786 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
Cooper Partine6664f02015-01-09 16:22:24 -0800787
788 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader VS_OUTPUT
789 // structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
790 // GeometryShader PointSprite emulation does not require this additional entry because the
791 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the PS_INPUT of the
792 // generated pixel shader.
Austin Kinross8b695ee2015-03-12 13:12:20 -0700793 // The Geometry Shader point sprite implementation needs gl_PointSize to be in VS_OUTPUT and GS_INPUT.
794 // Instanced point sprites doesn't need gl_PointSize in VS_OUTPUT.
795 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, outputPositionFromVS,
796 usesFragCoord, (useInstancedPointSpriteEmulation && usesPointCoord),
797 (!useInstancedPointSpriteEmulation && usesPointSize), false);
Jamie Madill5f562732014-02-14 16:41:24 -0500798
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400799 storeUserLinkedVaryings(vertexShader, linkedVaryings);
800 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500801
Cooper Partine6664f02015-01-09 16:22:24 -0800802 // Instanced PointSprite emulation requires additional entries originally generated in the
803 // GeometryShader HLSL. These include pointsize clamp values.
804 if (useInstancedPointSpriteEmulation)
805 {
806 vertexHLSL += "static float minPointSize = " + Str((int)mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
807 "static float maxPointSize = " + Str((int)mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n";
808 }
809
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500810 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400811 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
812 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500813 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500814 "{\n"
815 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500816
Jamie Madill37997142015-01-28 10:06:34 -0500817 if (vertexShader->usesDeferredInit())
818 {
819 vertexHLSL += "\n"
820 " initializeDeferredGlobals();\n";
821 }
822
823 vertexHLSL += "\n"
824 " gl_main();\n"
825 "\n"
Austin Kinross8b695ee2015-03-12 13:12:20 -0700826 " VS_OUTPUT output;\n";
827
828 if (outputPositionFromVS)
829 {
830 vertexHLSL += " output.gl_Position = gl_Position;\n";
831 }
Jamie Madill37997142015-01-28 10:06:34 -0500832
Austin Kinross4fd18b12014-12-22 12:32:05 -0800833 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
834 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500835 {
Jamie Madill37997142015-01-28 10:06:34 -0500836 vertexHLSL += " output.dx_Position.x = gl_Position.x;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400837 " output.dx_Position.y = -gl_Position.y;\n"
838 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
839 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500840 }
841 else
842 {
Jamie Madill37997142015-01-28 10:06:34 -0500843 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 -0400844 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
845 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
846 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500847 }
848
Austin Kinross8b695ee2015-03-12 13:12:20 -0700849 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
850 if (usesPointSize && shaderModel >= 3 && !useInstancedPointSpriteEmulation)
Jamie Madill5f562732014-02-14 16:41:24 -0500851 {
852 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
853 }
854
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400855 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500856 {
857 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
858 }
859
Jamie Madilld15250e2014-09-03 09:40:44 -0400860 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
861 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500862 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400863 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400864 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500865 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400866 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500867 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400868 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500869
870 for (int row = 0; row < variableRows; row++)
871 {
Jamie Madillde8892b2014-11-11 13:00:22 -0500872 int r = varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500873 vertexHLSL += " output.v" + Str(r);
874
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400875 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500876
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400877 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500878 {
879 vertexHLSL += ArrayString(elementIndex);
880 }
881
882 if (variableRows > 1)
883 {
884 vertexHLSL += ArrayString(row);
885 }
886
887 vertexHLSL += ";\n";
888 }
889 }
890 }
891 }
892
Cooper Partine6664f02015-01-09 16:22:24 -0800893 // Instanced PointSprite emulation requires additional entries to calculate
894 // the final output vertex positions of the quad that represents each sprite.
895 if (useInstancedPointSpriteEmulation)
896 {
897 vertexHLSL += "\n"
898 " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"
Austin Kinross8b695ee2015-03-12 13:12:20 -0700899 " 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 -0800900
901 if (usesPointCoord)
902 {
903 vertexHLSL += "\n"
904 " output.gl_PointCoord = input.spriteTexCoord;\n";
905 }
906 }
907
Jamie Madill5f562732014-02-14 16:41:24 -0500908 vertexHLSL += "\n"
909 " return output;\n"
910 "}\n";
911
Austin Kinross8b695ee2015-03-12 13:12:20 -0700912 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, outputPositionFromVS, usesFragCoord, usesPointCoord,
913 (!useInstancedPointSpriteEmulation && usesPointSize), true);
Jamie Madill5f562732014-02-14 16:41:24 -0500914
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400915 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500916
917 if (shaderVersion < 300)
918 {
919 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
920 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400921 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400922 outputKeyVariable.type = GL_FLOAT_VEC4;
923 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
924 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
925 outputKeyVariable.outputIndex = renderTargetIndex;
926
927 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500928 }
929
Brandon Jones71620962014-08-20 14:04:59 -0700930 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500931 }
932 else
933 {
934 defineOutputVariables(fragmentShader, programOutputVars);
935
Jamie Madilld15250e2014-09-03 09:40:44 -0400936 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500937 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
938 {
939 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400940 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400941 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500942 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
943
Jamie Madill54ad4f82014-09-03 09:40:46 -0400944 ASSERT(outputVariable.staticUse);
945
Jamie Madillf6be8d72014-09-05 10:38:07 -0400946 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400947 outputKeyVariable.type = outputVariable.type;
948 outputKeyVariable.name = variableName + elementString;
949 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
950 outputKeyVariable.outputIndex = locationIt->first;
951
952 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500953 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400954
955 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500956 }
957
Geoff Lang04fb89a2014-06-09 15:05:36 -0400958 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500959
Brandon Jones71620962014-08-20 14:04:59 -0700960 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500961 {
962 if (shaderModel >= 4)
963 {
964 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
965 "{\n";
966 }
967 else
968 {
969 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
970 "{\n";
971 }
972 }
973 else
974 {
975 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
976 "{\n";
977 }
978
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400979 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500980 {
981 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
982
Austin Kinross588434c2014-12-10 10:41:45 -0800983 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
984 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using dx_ViewCoords.
985 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500986 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400987 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
988 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500989 }
Austin Kinross588434c2014-12-10 10:41:45 -0800990 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500991 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400992 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
993 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500994 }
995 else
996 {
997 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
998 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
999 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
1000 }
1001
1002 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
1003 " gl_FragCoord.w = rhw;\n";
1004 }
1005
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001006 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -05001007 {
1008 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1009 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
1010 }
1011
Brandon Jones71620962014-08-20 14:04:59 -07001012 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -05001013 {
1014 if (shaderModel <= 3)
1015 {
1016 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1017 }
1018 else
1019 {
1020 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1021 }
1022 }
1023
Jamie Madilld15250e2014-09-03 09:40:44 -04001024 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
1025 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -05001026 {
Jamie Madilld15250e2014-09-03 09:40:44 -04001027 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001028 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -05001029 {
Jamie Madill54ad4f82014-09-03 09:40:46 -04001030 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001031 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -05001032 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001033 GLenum transposedType = TransposeMatrixType(varying.type);
1034 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -05001035 for (int row = 0; row < variableRows; row++)
1036 {
Jamie Madillde8892b2014-11-11 13:00:22 -05001037 std::string n = Str(varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001038 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -05001039
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001040 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -05001041 {
1042 pixelHLSL += ArrayString(elementIndex);
1043 }
1044
1045 if (variableRows > 1)
1046 {
1047 pixelHLSL += ArrayString(row);
1048 }
1049
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001050 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -05001051 {
1052 pixelHLSL += " = input.v" + n + ";\n"; break;
1053 }
1054 else
1055 {
1056 switch (VariableColumnCount(transposedType))
1057 {
1058 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1059 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1060 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1061 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1062 default: UNREACHABLE();
1063 }
1064 }
1065 }
1066 }
1067 }
Jamie Madill54ad4f82014-09-03 09:40:46 -04001068 else
1069 {
1070 ASSERT(varying.isBuiltIn() || !varying.staticUse);
1071 }
Jamie Madill5f562732014-02-14 16:41:24 -05001072 }
1073
Jamie Madill37997142015-01-28 10:06:34 -05001074 if (fragmentShader->usesDeferredInit())
1075 {
1076 pixelHLSL += "\n"
1077 " initializeDeferredGlobals();\n";
1078 }
1079
Jamie Madill5f562732014-02-14 16:41:24 -05001080 pixelHLSL += "\n"
1081 " gl_main();\n"
1082 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -04001083 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001084 "}\n";
1085
1086 return true;
1087}
1088
Jamie Madill30d6c252014-11-13 10:03:33 -05001089void DynamicHLSL::defineOutputVariables(ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -05001090{
Jamie Madilld15250e2014-09-03 09:40:44 -04001091 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -05001092
1093 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
1094 {
Jamie Madillf2575982014-06-25 16:04:54 -04001095 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -05001096 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
1097
Jamie Madill54ad4f82014-09-03 09:40:46 -04001098 ASSERT(outputVariable.staticUse);
1099
Jamie Madill5f562732014-02-14 16:41:24 -05001100 if (outputVariable.arraySize > 0)
1101 {
1102 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
1103 {
1104 const int location = baseLocation + elementIndex;
1105 ASSERT(programOutputVars->count(location) == 0);
1106 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
1107 }
1108 }
1109 else
1110 {
1111 ASSERT(programOutputVars->count(baseLocation) == 0);
1112 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
1113 }
1114 }
1115}
1116
Jamie Madill30d6c252014-11-13 10:03:33 -05001117std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001118{
1119 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001120 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001121 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001122}
1123
Jamie Madill30d6c252014-11-13 10:03:33 -05001124std::string DynamicHLSL::generatePointSpriteHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001125{
1126 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001127 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001128 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1129
1130 std::string geomHLSL;
1131
Austin Kinross8b695ee2015-03-12 13:12:20 -07001132 const SemanticInfo &inSemantics = getSemanticInfo(registers, true, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001133 false, true, false);
Austin Kinross8b695ee2015-03-12 13:12:20 -07001134 const SemanticInfo &outSemantics = getSemanticInfo(registers, true, fragmentShader->mUsesFragCoord,
Brandon Jones71620962014-08-20 14:04:59 -07001135 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001136
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001137 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1138 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1139 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001140
Geoff Langc0b9ef42014-07-02 10:02:37 -04001141 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001142 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1143 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001144 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1145 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001146 "\n"
Jamie Madill37997142015-01-28 10:06:34 -05001147 "static float2 pointSpriteCorners[] = \n"
1148 "{\n"
1149 " float2( 0.5f, -0.5f),\n"
1150 " float2( 0.5f, 0.5f),\n"
1151 " float2(-0.5f, -0.5f),\n"
1152 " float2(-0.5f, 0.5f)\n"
1153 "};\n"
1154 "\n"
1155 "static float2 pointSpriteTexcoords[] = \n"
1156 "{\n"
1157 " float2(1.0f, 1.0f),\n"
1158 " float2(1.0f, 0.0f),\n"
1159 " float2(0.0f, 1.0f),\n"
1160 " float2(0.0f, 0.0f)\n"
1161 "};\n"
1162 "\n"
Minmin Gong794e0002015-04-07 18:31:54 -07001163 "static float minPointSize = " + Str(static_cast<int>(mRenderer->getRendererCaps().minAliasedPointSize)) + ".0f;\n"
1164 "static float maxPointSize = " + Str(static_cast<int>(mRenderer->getRendererCaps().maxAliasedPointSize)) + ".0f;\n"
Jamie Madill37997142015-01-28 10:06:34 -05001165 "\n"
1166 "[maxvertexcount(4)]\n"
1167 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1168 "{\n"
1169 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
1170 " output.gl_Position = input[0].gl_Position;\n"
1171 " output.gl_PointSize = input[0].gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001172
1173 for (int r = 0; r < registers; r++)
1174 {
1175 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1176 }
1177
Brandon Jones71620962014-08-20 14:04:59 -07001178 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001179 {
1180 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1181 }
1182
1183 geomHLSL += " \n"
1184 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001185 " float4 dx_Position = input[0].dx_Position;\n"
1186 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001187
1188 for (int corner = 0; corner < 4; corner++)
1189 {
1190 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001191 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001192
Brandon Jones71620962014-08-20 14:04:59 -07001193 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001194 {
1195 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1196 }
1197
1198 geomHLSL += " outStream.Append(output);\n";
1199 }
1200
1201 geomHLSL += " \n"
1202 " outStream.RestartStrip();\n"
1203 "}\n";
1204
1205 return geomHLSL;
1206}
1207
1208// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001209std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001210{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001211 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001212 {
1213 return "_" + name;
1214 }
1215
1216 return name;
1217}
1218
Jamie Madilld3dfda22015-07-06 08:28:49 -04001219std::string DynamicHLSL::generateAttributeConversionHLSL(gl::VertexFormatType vertexFormatType,
1220 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001221{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001222 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madilla53ab512014-03-17 09:47:44 -04001223 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001224
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001225 // Matrix
1226 if (IsMatrixType(shaderAttrib.type))
1227 {
Jamie Madill8664b062014-02-14 16:41:29 -05001228 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001229 }
1230
Jamie Madillf2575982014-06-25 16:04:54 -04001231 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1232 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001233
Jamie Madill8664b062014-02-14 16:41:29 -05001234 // Perform integer to float conversion (if necessary)
Jamie Madilld3dfda22015-07-06 08:28:49 -04001235 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001236
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001237 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001238 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001239 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001240 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001241 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001242 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001243
1244 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001245 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001246}
1247
Jamie Madill5f562732014-02-14 16:41:24 -05001248}