blob: 3717f354e077ad7a731a6145f03be3b33608ff28 [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 {
520 declarationHLSL += " " + HLSLTypeString(outputVariable->type) + " " + outputVariable->name +
521 " : " + targetSemantic + Str(layoutIndex) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400522
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000523 copyHLSL += " output." + outputVariable->name + " = " + outputVariable->source + ";\n";
524 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400525 }
526 }
527
528 if (usesFragDepth)
529 {
530 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
531 copyHLSL += " output.gl_Depth = gl_Depth; \n";
532 }
533
534 std::string replacementHLSL = "struct PS_OUTPUT\n"
535 "{\n" +
536 declarationHLSL +
537 "};\n"
538 "\n"
539 "PS_OUTPUT generateOutput()\n"
540 "{\n"
541 " PS_OUTPUT output;\n" +
542 copyHLSL +
543 " return output;\n"
544 "}\n";
545
546 std::string pixelHLSL(sourceShader);
547
548 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
549 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
550
551 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500552}
553
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400554std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
555{
556 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
557 // In D3D11 we manually compute gl_PointCoord in the GS.
558 int shaderModel = mRenderer->getMajorShaderModel();
559 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
560}
561
562struct DynamicHLSL::SemanticInfo
563{
564 struct BuiltinInfo
565 {
566 BuiltinInfo()
567 : enabled(false),
568 index(0),
569 systemValue(false)
570 {}
571
572 bool enabled;
573 std::string semantic;
574 unsigned int index;
575 bool systemValue;
576
577 std::string str() const
578 {
579 return (systemValue ? semantic : (semantic + Str(index)));
580 }
581
582 void enableSystem(const std::string &systemValueSemantic)
583 {
584 enabled = true;
585 semantic = systemValueSemantic;
586 systemValue = true;
587 }
588
589 void enable(const std::string &semanticVal, unsigned int indexVal)
590 {
591 enabled = true;
592 semantic = semanticVal;
593 index = indexVal;
594 }
595 };
596
597 BuiltinInfo dxPosition;
598 BuiltinInfo glPosition;
599 BuiltinInfo glFragCoord;
600 BuiltinInfo glPointCoord;
601 BuiltinInfo glPointSize;
602};
603
Austin Kinross8b695ee2015-03-12 13:12:20 -0700604DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool position, bool fragCoord,
605 bool pointCoord, bool pointSize, bool pixelShader) const
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400606{
607 SemanticInfo info;
608 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
609 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
610
611 int reservedRegisterIndex = startRegisters;
612
613 if (hlsl4)
614 {
615 info.dxPosition.enableSystem("SV_Position");
616 }
617 else if (pixelShader)
618 {
619 info.dxPosition.enableSystem("VPOS");
620 }
621 else
622 {
623 info.dxPosition.enableSystem("POSITION");
624 }
625
Austin Kinross8b695ee2015-03-12 13:12:20 -0700626 if (position)
627 {
628 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
629 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400630
631 if (fragCoord)
632 {
633 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
634 }
635
636 if (pointCoord)
637 {
638 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
639 // In D3D11 we manually compute gl_PointCoord in the GS.
640 if (hlsl4)
641 {
642 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
643 }
644 else
645 {
646 info.glPointCoord.enable("TEXCOORD", 0);
647 }
648 }
649
650 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
651 if (pointSize && (!pixelShader || hlsl4))
652 {
653 info.glPointSize.enableSystem("PSIZE");
654 }
655
656 return info;
657}
658
659std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
660{
661 std::string linkHLSL = "{\n";
662
Austin Kinross8b695ee2015-03-12 13:12:20 -0700663 ASSERT(info.dxPosition.enabled);
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400664 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
Austin Kinross8b695ee2015-03-12 13:12:20 -0700665
666 if (info.glPosition.enabled)
667 {
668 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
669 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400670
671 if (info.glFragCoord.enabled)
672 {
673 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
674 }
675
676 if (info.glPointCoord.enabled)
677 {
678 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
679 }
680
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400681 if (info.glPointSize.enabled)
682 {
683 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
684 }
685
Austin Kinross8b695ee2015-03-12 13:12:20 -0700686 // Do this after glPointSize, to potentially combine gl_PointCoord and gl_PointSize into the same register.
687 linkHLSL += varyingHLSL;
688
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400689 linkHLSL += "};\n";
690
691 return linkHLSL;
692}
693
694void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
695 std::vector<LinkedVarying> *linkedVaryings) const
696{
Austin Kinross8b695ee2015-03-12 13:12:20 -0700697 if (info.glPosition.enabled)
698 {
699 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
700 info.glPosition.index, 1));
701 }
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400702
703 if (info.glFragCoord.enabled)
704 {
705 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
706 info.glFragCoord.index, 1));
707 }
708
709 if (info.glPointSize.enabled)
710 {
711 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
712 }
713}
714
Jamie Madill30d6c252014-11-13 10:03:33 -0500715void DynamicHLSL::storeUserLinkedVaryings(const ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400716 std::vector<LinkedVarying> *linkedVaryings) const
717{
Brandon Jones71620962014-08-20 14:04:59 -0700718 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400719 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400720
721 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
722 {
723 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400724
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400725 if (varying.registerAssigned())
726 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400727 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400728 GLenum transposedType = TransposeMatrixType(varying.type);
729 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
730
731 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
732 varyingSemantic, varying.registerIndex,
733 variableRows * varying.elementCount()));
734 }
735 }
736}
737
Jamie Madillde8892b2014-11-11 13:00:22 -0500738bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data, InfoLog &infoLog, int registers,
739 const VaryingPacking packing,
740 std::string &pixelHLSL, std::string &vertexHLSL,
Jamie Madill30d6c252014-11-13 10:03:33 -0500741 ShaderD3D *fragmentShader, ShaderD3D *vertexShader,
Jamie Madillde8892b2014-11-11 13:00:22 -0500742 const std::vector<std::string> &transformFeedbackVaryings,
Geoff Lang48dcae72014-02-05 16:28:24 -0500743 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400744 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400745 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400746 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500747{
748 if (pixelHLSL.empty() || vertexHLSL.empty())
749 {
750 return false;
751 }
752
Brandon Jones71620962014-08-20 14:04:59 -0700753 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
Brandon Jones71620962014-08-20 14:04:59 -0700754 bool usesFragCoord = fragmentShader->mUsesFragCoord;
755 bool usesPointCoord = fragmentShader->mUsesPointCoord;
756 bool usesPointSize = vertexShader->mUsesPointSize;
Cooper Partine6664f02015-01-09 16:22:24 -0800757 bool useInstancedPointSpriteEmulation = usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400758
Jamie Madill14e95b32015-05-07 10:10:41 -0400759 // Validation done in the compiler
760 ASSERT(!fragmentShader->mUsesFragColor || !fragmentShader->mUsesFragData);
Jamie Madill5f562732014-02-14 16:41:24 -0500761
762 // Write the HLSL input/output declarations
763 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400764 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500765
766 // Two cases when writing to gl_FragColor and using ESSL 1.0:
767 // - with a 3.0 context, the output color is copied to channel 0
768 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madillde8892b2014-11-11 13:00:22 -0500769 const bool broadcast = (fragmentShader->mUsesFragColor && data.clientVersion < 3);
770 const unsigned int numRenderTargets = (broadcast || usesMRT ? data.caps->maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500771
Austin Kinross8b695ee2015-03-12 13:12:20 -0700772 // gl_Position only needs to be outputted from the vertex shader if transform feedback is active.
773 // This isn't supported on D3D11 Feature Level 9_3, so we don't output gl_Position from the vertex shader in this case.
774 // This saves us 1 output vector.
775 bool outputPositionFromVS = !(shaderModel >= 4 && mRenderer->getShaderModelSuffix() != "");
776
Brandon Jones71620962014-08-20 14:04:59 -0700777 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500778
Jamie Madillde8892b2014-11-11 13:00:22 -0500779 if (static_cast<GLuint>(registersNeeded) > data.caps->maxVaryingVectors)
Jamie Madill5f562732014-02-14 16:41:24 -0500780 {
Jamie Madillf6113162015-05-07 11:49:21 -0400781 infoLog << "No varying registers left to support gl_FragCoord/gl_PointCoord";
Jamie Madill5f562732014-02-14 16:41:24 -0500782 return false;
783 }
784
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400785 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
Cooper Partine6664f02015-01-09 16:22:24 -0800786
787 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader VS_OUTPUT
788 // structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
789 // GeometryShader PointSprite emulation does not require this additional entry because the
790 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the PS_INPUT of the
791 // generated pixel shader.
Austin Kinross8b695ee2015-03-12 13:12:20 -0700792 // The Geometry Shader point sprite implementation needs gl_PointSize to be in VS_OUTPUT and GS_INPUT.
793 // Instanced point sprites doesn't need gl_PointSize in VS_OUTPUT.
794 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, outputPositionFromVS,
795 usesFragCoord, (useInstancedPointSpriteEmulation && usesPointCoord),
796 (!useInstancedPointSpriteEmulation && usesPointSize), false);
Jamie Madill5f562732014-02-14 16:41:24 -0500797
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400798 storeUserLinkedVaryings(vertexShader, linkedVaryings);
799 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500800
Cooper Partine6664f02015-01-09 16:22:24 -0800801 // Instanced PointSprite emulation requires additional entries originally generated in the
802 // GeometryShader HLSL. These include pointsize clamp values.
803 if (useInstancedPointSpriteEmulation)
804 {
805 vertexHLSL += "static float minPointSize = " + Str((int)mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
806 "static float maxPointSize = " + Str((int)mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n";
807 }
808
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500809 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400810 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
811 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500812 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500813 "{\n"
814 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500815
Jamie Madill37997142015-01-28 10:06:34 -0500816 if (vertexShader->usesDeferredInit())
817 {
818 vertexHLSL += "\n"
819 " initializeDeferredGlobals();\n";
820 }
821
822 vertexHLSL += "\n"
823 " gl_main();\n"
824 "\n"
Austin Kinross8b695ee2015-03-12 13:12:20 -0700825 " VS_OUTPUT output;\n";
826
827 if (outputPositionFromVS)
828 {
829 vertexHLSL += " output.gl_Position = gl_Position;\n";
830 }
Jamie Madill37997142015-01-28 10:06:34 -0500831
Austin Kinross4fd18b12014-12-22 12:32:05 -0800832 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
833 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500834 {
Jamie Madill37997142015-01-28 10:06:34 -0500835 vertexHLSL += " output.dx_Position.x = gl_Position.x;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400836 " output.dx_Position.y = -gl_Position.y;\n"
837 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
838 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500839 }
840 else
841 {
Jamie Madill37997142015-01-28 10:06:34 -0500842 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 -0400843 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
844 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
845 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500846 }
847
Austin Kinross8b695ee2015-03-12 13:12:20 -0700848 // We don't need to output gl_PointSize if we use are emulating point sprites via instancing.
849 if (usesPointSize && shaderModel >= 3 && !useInstancedPointSpriteEmulation)
Jamie Madill5f562732014-02-14 16:41:24 -0500850 {
851 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
852 }
853
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400854 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500855 {
856 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
857 }
858
Jamie Madilld15250e2014-09-03 09:40:44 -0400859 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
860 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500861 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400862 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400863 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500864 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400865 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500866 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400867 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500868
869 for (int row = 0; row < variableRows; row++)
870 {
Jamie Madillde8892b2014-11-11 13:00:22 -0500871 int r = varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500872 vertexHLSL += " output.v" + Str(r);
873
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400874 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500875
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400876 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500877 {
878 vertexHLSL += ArrayString(elementIndex);
879 }
880
881 if (variableRows > 1)
882 {
883 vertexHLSL += ArrayString(row);
884 }
885
886 vertexHLSL += ";\n";
887 }
888 }
889 }
890 }
891
Cooper Partine6664f02015-01-09 16:22:24 -0800892 // Instanced PointSprite emulation requires additional entries to calculate
893 // the final output vertex positions of the quad that represents each sprite.
894 if (useInstancedPointSpriteEmulation)
895 {
896 vertexHLSL += "\n"
897 " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"
Austin Kinross8b695ee2015-03-12 13:12:20 -0700898 " 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 -0800899
900 if (usesPointCoord)
901 {
902 vertexHLSL += "\n"
903 " output.gl_PointCoord = input.spriteTexCoord;\n";
904 }
905 }
906
Jamie Madill5f562732014-02-14 16:41:24 -0500907 vertexHLSL += "\n"
908 " return output;\n"
909 "}\n";
910
Austin Kinross8b695ee2015-03-12 13:12:20 -0700911 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, outputPositionFromVS, usesFragCoord, usesPointCoord,
912 (!useInstancedPointSpriteEmulation && usesPointSize), true);
Jamie Madill5f562732014-02-14 16:41:24 -0500913
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400914 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500915
916 if (shaderVersion < 300)
917 {
918 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
919 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400920 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400921 outputKeyVariable.type = GL_FLOAT_VEC4;
922 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
923 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
924 outputKeyVariable.outputIndex = renderTargetIndex;
925
926 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500927 }
928
Brandon Jones71620962014-08-20 14:04:59 -0700929 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500930 }
931 else
932 {
933 defineOutputVariables(fragmentShader, programOutputVars);
934
Jamie Madilld15250e2014-09-03 09:40:44 -0400935 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500936 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
937 {
938 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400939 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400940 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500941 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
942
Jamie Madill54ad4f82014-09-03 09:40:46 -0400943 ASSERT(outputVariable.staticUse);
944
Jamie Madillf6be8d72014-09-05 10:38:07 -0400945 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400946 outputKeyVariable.type = outputVariable.type;
947 outputKeyVariable.name = variableName + elementString;
948 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
949 outputKeyVariable.outputIndex = locationIt->first;
950
951 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500952 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400953
954 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500955 }
956
Geoff Lang04fb89a2014-06-09 15:05:36 -0400957 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500958
Brandon Jones71620962014-08-20 14:04:59 -0700959 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500960 {
961 if (shaderModel >= 4)
962 {
963 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
964 "{\n";
965 }
966 else
967 {
968 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
969 "{\n";
970 }
971 }
972 else
973 {
974 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
975 "{\n";
976 }
977
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400978 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500979 {
980 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
981
Austin Kinross588434c2014-12-10 10:41:45 -0800982 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
983 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using dx_ViewCoords.
984 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500985 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400986 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
987 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500988 }
Austin Kinross588434c2014-12-10 10:41:45 -0800989 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500990 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400991 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
992 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500993 }
994 else
995 {
996 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
997 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
998 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
999 }
1000
1001 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
1002 " gl_FragCoord.w = rhw;\n";
1003 }
1004
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001005 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -05001006 {
1007 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
1008 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
1009 }
1010
Brandon Jones71620962014-08-20 14:04:59 -07001011 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -05001012 {
1013 if (shaderModel <= 3)
1014 {
1015 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
1016 }
1017 else
1018 {
1019 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
1020 }
1021 }
1022
Jamie Madilld15250e2014-09-03 09:40:44 -04001023 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
1024 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -05001025 {
Jamie Madilld15250e2014-09-03 09:40:44 -04001026 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001027 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -05001028 {
Jamie Madill54ad4f82014-09-03 09:40:46 -04001029 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001030 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -05001031 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001032 GLenum transposedType = TransposeMatrixType(varying.type);
1033 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -05001034 for (int row = 0; row < variableRows; row++)
1035 {
Jamie Madillde8892b2014-11-11 13:00:22 -05001036 std::string n = Str(varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001037 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -05001038
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001039 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -05001040 {
1041 pixelHLSL += ArrayString(elementIndex);
1042 }
1043
1044 if (variableRows > 1)
1045 {
1046 pixelHLSL += ArrayString(row);
1047 }
1048
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001049 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -05001050 {
1051 pixelHLSL += " = input.v" + n + ";\n"; break;
1052 }
1053 else
1054 {
1055 switch (VariableColumnCount(transposedType))
1056 {
1057 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1058 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1059 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1060 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1061 default: UNREACHABLE();
1062 }
1063 }
1064 }
1065 }
1066 }
Jamie Madill54ad4f82014-09-03 09:40:46 -04001067 else
1068 {
1069 ASSERT(varying.isBuiltIn() || !varying.staticUse);
1070 }
Jamie Madill5f562732014-02-14 16:41:24 -05001071 }
1072
Jamie Madill37997142015-01-28 10:06:34 -05001073 if (fragmentShader->usesDeferredInit())
1074 {
1075 pixelHLSL += "\n"
1076 " initializeDeferredGlobals();\n";
1077 }
1078
Jamie Madill5f562732014-02-14 16:41:24 -05001079 pixelHLSL += "\n"
1080 " gl_main();\n"
1081 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -04001082 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001083 "}\n";
1084
1085 return true;
1086}
1087
Jamie Madill30d6c252014-11-13 10:03:33 -05001088void DynamicHLSL::defineOutputVariables(ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -05001089{
Jamie Madilld15250e2014-09-03 09:40:44 -04001090 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -05001091
1092 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
1093 {
Jamie Madillf2575982014-06-25 16:04:54 -04001094 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -05001095 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
1096
Jamie Madill54ad4f82014-09-03 09:40:46 -04001097 ASSERT(outputVariable.staticUse);
1098
Jamie Madill5f562732014-02-14 16:41:24 -05001099 if (outputVariable.arraySize > 0)
1100 {
1101 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
1102 {
1103 const int location = baseLocation + elementIndex;
1104 ASSERT(programOutputVars->count(location) == 0);
1105 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
1106 }
1107 }
1108 else
1109 {
1110 ASSERT(programOutputVars->count(baseLocation) == 0);
1111 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
1112 }
1113 }
1114}
1115
Jamie Madill30d6c252014-11-13 10:03:33 -05001116std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001117{
1118 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001119 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001120 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001121}
1122
Jamie Madill30d6c252014-11-13 10:03:33 -05001123std::string DynamicHLSL::generatePointSpriteHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001124{
1125 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001126 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001127 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1128
1129 std::string geomHLSL;
1130
Austin Kinross8b695ee2015-03-12 13:12:20 -07001131 const SemanticInfo &inSemantics = getSemanticInfo(registers, true, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001132 false, true, false);
Austin Kinross8b695ee2015-03-12 13:12:20 -07001133 const SemanticInfo &outSemantics = getSemanticInfo(registers, true, fragmentShader->mUsesFragCoord,
Brandon Jones71620962014-08-20 14:04:59 -07001134 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001135
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001136 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1137 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1138 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001139
Geoff Langc0b9ef42014-07-02 10:02:37 -04001140 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001141 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1142 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001143 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1144 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001145 "\n"
Jamie Madill37997142015-01-28 10:06:34 -05001146 "static float2 pointSpriteCorners[] = \n"
1147 "{\n"
1148 " float2( 0.5f, -0.5f),\n"
1149 " float2( 0.5f, 0.5f),\n"
1150 " float2(-0.5f, -0.5f),\n"
1151 " float2(-0.5f, 0.5f)\n"
1152 "};\n"
1153 "\n"
1154 "static float2 pointSpriteTexcoords[] = \n"
1155 "{\n"
1156 " float2(1.0f, 1.0f),\n"
1157 " float2(1.0f, 0.0f),\n"
1158 " float2(0.0f, 1.0f),\n"
1159 " float2(0.0f, 0.0f)\n"
1160 "};\n"
1161 "\n"
Minmin Gong794e0002015-04-07 18:31:54 -07001162 "static float minPointSize = " + Str(static_cast<int>(mRenderer->getRendererCaps().minAliasedPointSize)) + ".0f;\n"
1163 "static float maxPointSize = " + Str(static_cast<int>(mRenderer->getRendererCaps().maxAliasedPointSize)) + ".0f;\n"
Jamie Madill37997142015-01-28 10:06:34 -05001164 "\n"
1165 "[maxvertexcount(4)]\n"
1166 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1167 "{\n"
1168 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
1169 " output.gl_Position = input[0].gl_Position;\n"
1170 " output.gl_PointSize = input[0].gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001171
1172 for (int r = 0; r < registers; r++)
1173 {
1174 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1175 }
1176
Brandon Jones71620962014-08-20 14:04:59 -07001177 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001178 {
1179 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1180 }
1181
1182 geomHLSL += " \n"
1183 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001184 " float4 dx_Position = input[0].dx_Position;\n"
1185 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001186
1187 for (int corner = 0; corner < 4; corner++)
1188 {
1189 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001190 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001191
Brandon Jones71620962014-08-20 14:04:59 -07001192 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001193 {
1194 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1195 }
1196
1197 geomHLSL += " outStream.Append(output);\n";
1198 }
1199
1200 geomHLSL += " \n"
1201 " outStream.RestartStrip();\n"
1202 "}\n";
1203
1204 return geomHLSL;
1205}
1206
1207// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001208std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001209{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001210 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001211 {
1212 return "_" + name;
1213 }
1214
1215 return name;
1216}
1217
Jamie Madilld3dfda22015-07-06 08:28:49 -04001218std::string DynamicHLSL::generateAttributeConversionHLSL(gl::VertexFormatType vertexFormatType,
1219 const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001220{
Jamie Madilld3dfda22015-07-06 08:28:49 -04001221 const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType);
Jamie Madilla53ab512014-03-17 09:47:44 -04001222 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001223
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001224 // Matrix
1225 if (IsMatrixType(shaderAttrib.type))
1226 {
Jamie Madill8664b062014-02-14 16:41:29 -05001227 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001228 }
1229
Jamie Madillf2575982014-06-25 16:04:54 -04001230 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1231 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001232
Jamie Madill8664b062014-02-14 16:41:29 -05001233 // Perform integer to float conversion (if necessary)
Jamie Madilld3dfda22015-07-06 08:28:49 -04001234 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT);
Jamie Madill8664b062014-02-14 16:41:29 -05001235
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001236 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001237 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001238 // TODO: normalization for 32-bit integer formats
Jamie Madilld3dfda22015-07-06 08:28:49 -04001239 ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001240 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001241 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001242
1243 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001244 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001245}
1246
Jamie Madill5f562732014-02-14 16:41:24 -05001247}