blob: 941393ef969ca9437ca07bbd23003d5baaf0518f [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 Madilld3dfda22015-07-06 08:28:49 -0400417 VertexFormatType vertexFormatType = inputLayout[inputIndex];
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400418
Jamie Madill3b7e2052014-03-17 09:47:43 -0400419 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500420 if (IsMatrixType(shaderAttribute.type))
421 {
422 // Matrix types are always transposed
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400423 structHLSL += " " + HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500424 }
425 else
426 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400427 GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000428
429 if (shaderAttribute.name == "gl_InstanceID")
430 {
431 // The input type of the instance ID in HLSL (uint) differs from the one in ESSL (int).
432 structHLSL += " uint";
433 }
434 else
435 {
436 structHLSL += " " + HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
437 }
Jamie Madill8664b062014-02-14 16:41:29 -0500438 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500439
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000440 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : ";
441
442 if (shaderAttribute.name == "gl_InstanceID")
443 {
444 structHLSL += "SV_InstanceID";
445 }
446 else
447 {
448 structHLSL += "TEXCOORD" + Str(semanticIndex);
449 semanticIndex += VariableRegisterCount(shaderAttribute.type);
450 }
451
452 structHLSL += ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500453
Jamie Madill3b7e2052014-03-17 09:47:43 -0400454 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400455 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500456
457 // Mismatched vertex attribute to vertex input may result in an undefined
458 // data reinterpretation (eg for pure integer->float, float->pure integer)
459 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400460 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madilld3dfda22015-07-06 08:28:49 -0400461 (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500462 {
Jamie Madilld3dfda22015-07-06 08:28:49 -0400463 initHLSL += generateAttributeConversionHLSL(vertexFormatType, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500464 }
465 else
466 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400467 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500468 }
469
Jamie Madill3b7e2052014-03-17 09:47:43 -0400470 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400471
Jamie Madillac0a2672014-04-11 13:33:56 -0400472 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
473 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500474 }
475
Geoff Lang04fb89a2014-06-09 15:05:36 -0400476 std::string replacementHLSL = "struct VS_INPUT\n"
477 "{\n" +
478 structHLSL +
479 "};\n"
480 "\n"
481 "void initAttributes(VS_INPUT input)\n"
482 "{\n" +
483 initHLSL +
484 "}\n";
485
486 std::string vertexHLSL(sourceShader);
487
488 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
489 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
490
491 return vertexHLSL;
492}
493
Jamie Madillf6be8d72014-09-05 10:38:07 -0400494std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOutputVariable> &outputVariables,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400495 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
496{
497 const int shaderModel = mRenderer->getMajorShaderModel();
498 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
499 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
500
501 std::string declarationHLSL;
502 std::string copyHLSL;
Geoff Lang4ace4232014-06-18 19:12:48 -0400503
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400504 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
505 {
506 GLenum binding = outputLayout[layoutIndex];
507
508 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400509 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400510 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
511
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000512 const PixelShaderOutputVariable *outputVariable = FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400513
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000514 // OpenGL ES 3.0 spec $4.2.1
515 // If [...] not all user-defined output variables are written, the values of fragment colors
516 // corresponding to unwritten variables are similarly undefined.
517 if (outputVariable)
518 {
519 declarationHLSL += " " + HLSLTypeString(outputVariable->type) + " " + outputVariable->name +
520 " : " + targetSemantic + Str(layoutIndex) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400521
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000522 copyHLSL += " output." + outputVariable->name + " = " + outputVariable->source + ";\n";
523 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400524 }
525 }
526
527 if (usesFragDepth)
528 {
529 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
530 copyHLSL += " output.gl_Depth = gl_Depth; \n";
531 }
532
533 std::string replacementHLSL = "struct PS_OUTPUT\n"
534 "{\n" +
535 declarationHLSL +
536 "};\n"
537 "\n"
538 "PS_OUTPUT generateOutput()\n"
539 "{\n"
540 " PS_OUTPUT output;\n" +
541 copyHLSL +
542 " return output;\n"
543 "}\n";
544
545 std::string pixelHLSL(sourceShader);
546
547 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
548 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
549
550 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500551}
552
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400553std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
554{
555 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
556 // In D3D11 we manually compute gl_PointCoord in the GS.
557 int shaderModel = mRenderer->getMajorShaderModel();
558 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
559}
560
561struct DynamicHLSL::SemanticInfo
562{
563 struct BuiltinInfo
564 {
565 BuiltinInfo()
566 : enabled(false),
567 index(0),
568 systemValue(false)
569 {}
570
571 bool enabled;
572 std::string semantic;
573 unsigned int index;
574 bool systemValue;
575
576 std::string str() const
577 {
578 return (systemValue ? semantic : (semantic + Str(index)));
579 }
580
581 void enableSystem(const std::string &systemValueSemantic)
582 {
583 enabled = true;
584 semantic = systemValueSemantic;
585 systemValue = true;
586 }
587
588 void enable(const std::string &semanticVal, unsigned int indexVal)
589 {
590 enabled = true;
591 semantic = semanticVal;
592 index = indexVal;
593 }
594 };
595
596 BuiltinInfo dxPosition;
597 BuiltinInfo glPosition;
598 BuiltinInfo glFragCoord;
599 BuiltinInfo glPointCoord;
600 BuiltinInfo glPointSize;
601};
602
Austin Kinross8b695ee2015-03-12 13:12:20 -0700603DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool position, bool fragCoord,
604 bool pointCoord, bool pointSize, bool pixelShader) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400605{
606 SemanticInfo info;
607 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
608 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
609
610 int reservedRegisterIndex = startRegisters;
611
612 if (hlsl4)
613 {
614 info.dxPosition.enableSystem("SV_Position");
615 }
616 else if (pixelShader)
617 {
618 info.dxPosition.enableSystem("VPOS");
619 }
620 else
621 {
622 info.dxPosition.enableSystem("POSITION");
623 }
624
Austin Kinross8b695ee2015-03-12 13:12:20 -0700625 if (position)
626 {
627 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
628 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400629
630 if (fragCoord)
631 {
632 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
633 }
634
635 if (pointCoord)
636 {
637 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
638 // In D3D11 we manually compute gl_PointCoord in the GS.
639 if (hlsl4)
640 {
641 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
642 }
643 else
644 {
645 info.glPointCoord.enable("TEXCOORD", 0);
646 }
647 }
648
649 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
650 if (pointSize && (!pixelShader || hlsl4))
651 {
652 info.glPointSize.enableSystem("PSIZE");
653 }
654
655 return info;
656}
657
658std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
659{
660 std::string linkHLSL = "{\n";
661
Austin Kinross8b695ee2015-03-12 13:12:20 -0700662 ASSERT(info.dxPosition.enabled);
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400663 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700664
665 if (info.glPosition.enabled)
666 {
667 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
668 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400669
670 if (info.glFragCoord.enabled)
671 {
672 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
673 }
674
675 if (info.glPointCoord.enabled)
676 {
677 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
678 }
679
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400680 if (info.glPointSize.enabled)
681 {
682 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
683 }
684
Austin Kinross8b695ee2015-03-12 13:12:20 -0700685 // Do this after glPointSize, to potentially combine gl_PointCoord and gl_PointSize into the same register.
686 linkHLSL += varyingHLSL;
687
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400688 linkHLSL += "};\n";
689
690 return linkHLSL;
691}
692
693void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
694 std::vector<LinkedVarying> *linkedVaryings) const
695{
Austin Kinross8b695ee2015-03-12 13:12:20 -0700696 if (info.glPosition.enabled)
697 {
698 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
699 info.glPosition.index, 1));
700 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400701
702 if (info.glFragCoord.enabled)
703 {
704 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
705 info.glFragCoord.index, 1));
706 }
707
708 if (info.glPointSize.enabled)
709 {
710 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
711 }
712}
713
Jamie Madill30d6c252014-11-13 10:03:33 -0500714void DynamicHLSL::storeUserLinkedVaryings(const ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400715 std::vector<LinkedVarying> *linkedVaryings) const
716{
Brandon Jones71620962014-08-20 14:04:59 -0700717 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400718 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400719
720 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
721 {
722 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400723
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400724 if (varying.registerAssigned())
725 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400726 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400727 GLenum transposedType = TransposeMatrixType(varying.type);
728 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
729
730 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
731 varyingSemantic, varying.registerIndex,
732 variableRows * varying.elementCount()));
733 }
734 }
735}
736
Jamie Madillde8892b2014-11-11 13:00:22 -0500737bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data, InfoLog &infoLog, int registers,
738 const VaryingPacking packing,
739 std::string &pixelHLSL, std::string &vertexHLSL,
Jamie Madill30d6c252014-11-13 10:03:33 -0500740 ShaderD3D *fragmentShader, ShaderD3D *vertexShader,
Jamie Madillde8892b2014-11-11 13:00:22 -0500741 const std::vector<std::string> &transformFeedbackVaryings,
Geoff Lang48dcae72014-02-05 16:28:24 -0500742 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400743 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400744 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400745 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500746{
747 if (pixelHLSL.empty() || vertexHLSL.empty())
748 {
749 return false;
750 }
751
Brandon Jones71620962014-08-20 14:04:59 -0700752 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
Brandon Jones71620962014-08-20 14:04:59 -0700753 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 Madill14e95b32015-05-07 10:10:41 -0400758 // Validation done in the compiler
759 ASSERT(!fragmentShader->mUsesFragColor || !fragmentShader->mUsesFragData);
Jamie Madill5f562732014-02-14 16:41:24 -0500760
761 // Write the HLSL input/output declarations
762 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400763 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500764
765 // Two cases when writing to gl_FragColor and using ESSL 1.0:
766 // - with a 3.0 context, the output color is copied to channel 0
767 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madillde8892b2014-11-11 13:00:22 -0500768 const bool broadcast = (fragmentShader->mUsesFragColor && data.clientVersion < 3);
769 const unsigned int numRenderTargets = (broadcast || usesMRT ? data.caps->maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500770
Austin Kinross8b695ee2015-03-12 13:12:20 -0700771 // gl_Position only needs to be outputted from the vertex shader if transform feedback is active.
772 // This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from the vertex shader in this case.
773 // This saves us 1 output vector.
774 bool outputPositionFromVS = !(shaderModel >= 4 && mRenderer->getShaderModelSuffix() != "");
775
Brandon Jones71620962014-08-20 14:04:59 -0700776 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500777
Jamie Madillde8892b2014-11-11 13:00:22 -0500778 if (static_cast<GLuint>(registersNeeded) > data.caps->maxVaryingVectors)
Jamie Madill5f562732014-02-14 16:41:24 -0500779 {
Jamie Madillf6113162015-05-07 11:49:21 -0400780 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madill5f562732014-02-14 16:41:24 -0500781 return false;
782 }
783
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400784 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
Cooper Partine6664f02015-01-09 16:22:24 -0800785
786 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader VS_OUTPUT
787 // structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
788 // GeometryShader PointSprite emulation does not require this additional entry because the
789 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the PS_INPUT of the
790 // generated pixel shader.
Austin Kinross8b695ee2015-03-12 13:12:20 -0700791 // The Geometry Shader point sprite implementation needs gl_PointSize to be in VS_OUTPUT and GS_INPUT.
792 // Instanced point sprites doesn't need gl_PointSize in VS_OUTPUT.
793 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, outputPositionFromVS,
794 usesFragCoord, (useInstancedPointSpriteEmulation && usesPointCoord),
795 (!useInstancedPointSpriteEmulation && usesPointSize), false);
Jamie Madill5f562732014-02-14 16:41:24 -0500796
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400797 storeUserLinkedVaryings(vertexShader, linkedVaryings);
798 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500799
Cooper Partine6664f02015-01-09 16:22:24 -0800800 // Instanced PointSprite emulation requires additional entries originally generated in the
801 // GeometryShader HLSL. These include pointsize clamp values.
802 if (useInstancedPointSpriteEmulation)
803 {
804 vertexHLSL += "static float minPointSize = " + Str((int)mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
805 "static float maxPointSize = " + Str((int)mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n";
806 }
807
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500808 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400809 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
810 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500811 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500812 "{\n"
813 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500814
Jamie Madill37997142015-01-28 10:06:34 -0500815 if (vertexShader->usesDeferredInit())
816 {
817 vertexHLSL += "\n"
818 " initializeDeferredGlobals();\n";
819 }
820
821 vertexHLSL += "\n"
822 " gl_main();\n"
823 "\n"
Austin Kinross8b695ee2015-03-12 13:12:20 -0700824 " VS_OUTPUT output;\n";
825
826 if (outputPositionFromVS)
827 {
828 vertexHLSL += " output.gl_Position = gl_Position;\n";
829 }
Jamie Madill37997142015-01-28 10:06:34 -0500830
Austin Kinross4fd18b12014-12-22 12:32:05 -0800831 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
832 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500833 {
Jamie Madill37997142015-01-28 10:06:34 -0500834 vertexHLSL += " output.dx_Position.x = gl_Position.x;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400835 " output.dx_Position.y = -gl_Position.y;\n"
836 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
837 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500838 }
839 else
840 {
Jamie Madill37997142015-01-28 10:06:34 -0500841 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 -0400842 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
843 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
844 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500845 }
846
Austin Kinross8b695ee2015-03-12 13:12:20 -0700847 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
848 if (usesPointSize && shaderModel >= 3 && !useInstancedPointSpriteEmulation)
Jamie Madill5f562732014-02-14 16:41:24 -0500849 {
850 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
851 }
852
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400853 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500854 {
855 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
856 }
857
Jamie Madilld15250e2014-09-03 09:40:44 -0400858 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
859 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500860 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400861 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400862 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500863 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400864 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500865 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400866 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500867
868 for (int row = 0; row < variableRows; row++)
869 {
Jamie Madillde8892b2014-11-11 13:00:22 -0500870 int r = varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500871 vertexHLSL += " output.v" + Str(r);
872
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400873 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500874
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400875 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500876 {
877 vertexHLSL += ArrayString(elementIndex);
878 }
879
880 if (variableRows > 1)
881 {
882 vertexHLSL += ArrayString(row);
883 }
884
885 vertexHLSL += ";\n";
886 }
887 }
888 }
889 }
890
Cooper Partine6664f02015-01-09 16:22:24 -0800891 // Instanced PointSprite emulation requires additional entries to calculate
892 // the final output vertex positions of the quad that represents each sprite.
893 if (useInstancedPointSpriteEmulation)
894 {
895 vertexHLSL += "\n"
896 " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"
Austin Kinross8b695ee2015-03-12 13:12:20 -0700897 " 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 -0800898
899 if (usesPointCoord)
900 {
901 vertexHLSL += "\n"
902 " output.gl_PointCoord = input.spriteTexCoord;\n";
903 }
904 }
905
Jamie Madill5f562732014-02-14 16:41:24 -0500906 vertexHLSL += "\n"
907 " return output;\n"
908 "}\n";
909
Austin Kinross8b695ee2015-03-12 13:12:20 -0700910 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, outputPositionFromVS, usesFragCoord, usesPointCoord,
911 (!useInstancedPointSpriteEmulation && usesPointSize), true);
Jamie Madill5f562732014-02-14 16:41:24 -0500912
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400913 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500914
915 if (shaderVersion < 300)
916 {
917 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
918 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400919 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400920 outputKeyVariable.type = GL_FLOAT_VEC4;
921 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
922 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
923 outputKeyVariable.outputIndex = renderTargetIndex;
924
925 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500926 }
927
Brandon Jones71620962014-08-20 14:04:59 -0700928 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500929 }
930 else
931 {
932 defineOutputVariables(fragmentShader, programOutputVars);
933
Jamie Madilld15250e2014-09-03 09:40:44 -0400934 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500935 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
936 {
937 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400938 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400939 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500940 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
941
Jamie Madill54ad4f82014-09-03 09:40:46 -0400942 ASSERT(outputVariable.staticUse);
943
Jamie Madillf6be8d72014-09-05 10:38:07 -0400944 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400945 outputKeyVariable.type = outputVariable.type;
946 outputKeyVariable.name = variableName + elementString;
947 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
948 outputKeyVariable.outputIndex = locationIt->first;
949
950 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500951 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400952
953 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500954 }
955
Geoff Lang04fb89a2014-06-09 15:05:36 -0400956 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500957
Brandon Jones71620962014-08-20 14:04:59 -0700958 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500959 {
960 if (shaderModel >= 4)
961 {
962 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
963 "{\n";
964 }
965 else
966 {
967 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
968 "{\n";
969 }
970 }
971 else
972 {
973 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
974 "{\n";
975 }
976
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400977 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500978 {
979 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
980
Austin Kinross588434c2014-12-10 10:41:45 -0800981 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
982 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using dx_ViewCoords.
983 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500984 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400985 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
986 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500987 }
Austin Kinross588434c2014-12-10 10:41:45 -0800988 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500989 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400990 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
991 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500992 }
993 else
994 {
995 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
996 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
997 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
998 }
999
1000 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
1001 " gl_FragCoord.w = rhw;\n";
1002 }
1003
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001004 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -05001005 {
1006 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1007 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
1008 }
1009
Brandon Jones71620962014-08-20 14:04:59 -07001010 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -05001011 {
1012 if (shaderModel <= 3)
1013 {
1014 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1015 }
1016 else
1017 {
1018 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1019 }
1020 }
1021
Jamie Madilld15250e2014-09-03 09:40:44 -04001022 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
1023 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -05001024 {
Jamie Madilld15250e2014-09-03 09:40:44 -04001025 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001026 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -05001027 {
Jamie Madill54ad4f82014-09-03 09:40:46 -04001028 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001029 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -05001030 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001031 GLenum transposedType = TransposeMatrixType(varying.type);
1032 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -05001033 for (int row = 0; row < variableRows; row++)
1034 {
Jamie Madillde8892b2014-11-11 13:00:22 -05001035 std::string n = Str(varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001036 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -05001037
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001038 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -05001039 {
1040 pixelHLSL += ArrayString(elementIndex);
1041 }
1042
1043 if (variableRows > 1)
1044 {
1045 pixelHLSL += ArrayString(row);
1046 }
1047
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001048 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -05001049 {
1050 pixelHLSL += " = input.v" + n + ";\n"; break;
1051 }
1052 else
1053 {
1054 switch (VariableColumnCount(transposedType))
1055 {
1056 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1057 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1058 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1059 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1060 default: UNREACHABLE();
1061 }
1062 }
1063 }
1064 }
1065 }
Jamie Madill54ad4f82014-09-03 09:40:46 -04001066 else
1067 {
1068 ASSERT(varying.isBuiltIn() || !varying.staticUse);
1069 }
Jamie Madill5f562732014-02-14 16:41:24 -05001070 }
1071
Jamie Madill37997142015-01-28 10:06:34 -05001072 if (fragmentShader->usesDeferredInit())
1073 {
1074 pixelHLSL += "\n"
1075 " initializeDeferredGlobals();\n";
1076 }
1077
Jamie Madill5f562732014-02-14 16:41:24 -05001078 pixelHLSL += "\n"
1079 " gl_main();\n"
1080 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -04001081 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001082 "}\n";
1083
1084 return true;
1085}
1086
Jamie Madill30d6c252014-11-13 10:03:33 -05001087void DynamicHLSL::defineOutputVariables(ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -05001088{
Jamie Madilld15250e2014-09-03 09:40:44 -04001089 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -05001090
1091 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
1092 {
Jamie Madillf2575982014-06-25 16:04:54 -04001093 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -05001094 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
1095
Jamie Madill54ad4f82014-09-03 09:40:46 -04001096 ASSERT(outputVariable.staticUse);
1097
Jamie Madill5f562732014-02-14 16:41:24 -05001098 if (outputVariable.arraySize > 0)
1099 {
1100 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
1101 {
1102 const int location = baseLocation + elementIndex;
1103 ASSERT(programOutputVars->count(location) == 0);
1104 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
1105 }
1106 }
1107 else
1108 {
1109 ASSERT(programOutputVars->count(baseLocation) == 0);
1110 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
1111 }
1112 }
1113}
1114
Jamie Madill30d6c252014-11-13 10:03:33 -05001115std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001116{
1117 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001118 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001119 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001120}
1121
Jamie Madill30d6c252014-11-13 10:03:33 -05001122std::string DynamicHLSL::generatePointSpriteHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001123{
1124 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001125 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001126 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1127
1128 std::string geomHLSL;
1129
Austin Kinross8b695ee2015-03-12 13:12:20 -07001130 const SemanticInfo &inSemantics = getSemanticInfo(registers, true, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001131 false, true, false);
Austin Kinross8b695ee2015-03-12 13:12:20 -07001132 const SemanticInfo &outSemantics = getSemanticInfo(registers, true, fragmentShader->mUsesFragCoord,
Brandon Jones71620962014-08-20 14:04:59 -07001133 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001134
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001135 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1136 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1137 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001138
Geoff Langc0b9ef42014-07-02 10:02:37 -04001139 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001140 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1141 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001142 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1143 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001144 "\n"
Jamie Madill37997142015-01-28 10:06:34 -05001145 "static float2 pointSpriteCorners[] = \n"
1146 "{\n"
1147 " float2( 0.5f, -0.5f),\n"
1148 " float2( 0.5f, 0.5f),\n"
1149 " float2(-0.5f, -0.5f),\n"
1150 " float2(-0.5f, 0.5f)\n"
1151 "};\n"
1152 "\n"
1153 "static float2 pointSpriteTexcoords[] = \n"
1154 "{\n"
1155 " float2(1.0f, 1.0f),\n"
1156 " float2(1.0f, 0.0f),\n"
1157 " float2(0.0f, 1.0f),\n"
1158 " float2(0.0f, 0.0f)\n"
1159 "};\n"
1160 "\n"
Minmin Gong794e0002015-04-07 18:31:54 -07001161 "static float minPointSize = " + Str(static_cast<int>(mRenderer->getRendererCaps().minAliasedPointSize)) + ".0f;\n"
1162 "static float maxPointSize = " + Str(static_cast<int>(mRenderer->getRendererCaps().maxAliasedPointSize)) + ".0f;\n"
Jamie Madill37997142015-01-28 10:06:34 -05001163 "\n"
1164 "[maxvertexcount(4)]\n"
1165 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1166 "{\n"
1167 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
1168 " output.gl_Position = input[0].gl_Position;\n"
1169 " output.gl_PointSize = input[0].gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001170
1171 for (int r = 0; r < registers; r++)
1172 {
1173 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1174 }
1175
Brandon Jones71620962014-08-20 14:04:59 -07001176 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001177 {
1178 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1179 }
1180
1181 geomHLSL += " \n"
1182 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001183 " float4 dx_Position = input[0].dx_Position;\n"
1184 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001185
1186 for (int corner = 0; corner < 4; corner++)
1187 {
1188 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001189 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001190
Brandon Jones71620962014-08-20 14:04:59 -07001191 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001192 {
1193 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1194 }
1195
1196 geomHLSL += " outStream.Append(output);\n";
1197 }
1198
1199 geomHLSL += " \n"
1200 " outStream.RestartStrip();\n"
1201 "}\n";
1202
1203 return geomHLSL;
1204}
1205
1206// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001207std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001208{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001209 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001210 {
1211 return "_" + name;
1212 }
1213
1214 return name;
1215}
1216
Jamie Madilld3dfda22015-07-06 08:28:49 -04001217std::string DynamicHLSL::generateAttributeConversionHLSL(gl::VertexFormatType vertexFormatType,
1218 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001219{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001220 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madilla53ab512014-03-17 09:47:44 -04001221 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001222
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001223 // Matrix
1224 if (IsMatrixType(shaderAttrib.type))
1225 {
Jamie Madill8664b062014-02-14 16:41:29 -05001226 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001227 }
1228
Jamie Madillf2575982014-06-25 16:04:54 -04001229 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1230 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001231
Jamie Madill8664b062014-02-14 16:41:29 -05001232 // Perform integer to float conversion (if necessary)
Jamie Madilld3dfda22015-07-06 08:28:49 -04001233 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001234
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001235 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001236 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001237 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001238 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001239 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001240 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001241
1242 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001243 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001244}
1245
Jamie Madill5f562732014-02-14 16:41:24 -05001246}