blob: 12a1ed985b304d5f46b31e71995e9f46ab04846f [file] [log] [blame]
Jamie Madill5f562732014-02-14 16:41:24 -05001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// DynamicHLSL.cpp: Implementation for link and run-time HLSL generation
7//
8
9#include "precompiled.h"
10
11#include "libGLESv2/DynamicHLSL.h"
12#include "libGLESv2/Shader.h"
13#include "libGLESv2/Program.h"
14#include "libGLESv2/renderer/Renderer.h"
15#include "common/utilities.h"
16#include "libGLESv2/ProgramBinary.h"
Jamie Madill8664b062014-02-14 16:41:29 -050017#include "libGLESv2/formatutils.h"
Jamie Madill5f562732014-02-14 16:41:24 -050018
19#include "compiler/translator/HLSLLayoutEncoder.h"
20
Jamie Madill5f562732014-02-14 16:41:24 -050021static std::string Str(int i)
22{
23 char buffer[20];
24 snprintf(buffer, sizeof(buffer), "%d", i);
25 return buffer;
26}
27
Jamie Madill8664b062014-02-14 16:41:29 -050028namespace gl_d3d
Jamie Madill5f562732014-02-14 16:41:24 -050029{
Jamie Madill8664b062014-02-14 16:41:29 -050030
31std::string HLSLComponentTypeString(GLenum componentType)
32{
33 switch (componentType)
34 {
35 case GL_UNSIGNED_INT: return "uint";
36 case GL_INT: return "int";
37 case GL_UNSIGNED_NORMALIZED:
38 case GL_SIGNED_NORMALIZED:
39 case GL_FLOAT: return "float";
40 default: UNREACHABLE(); return "not-component-type";
41 }
Jamie Madill5f562732014-02-14 16:41:24 -050042}
43
Jamie Madill8664b062014-02-14 16:41:29 -050044std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
45{
46 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
47}
48
49std::string HLSLMatrixTypeString(GLenum type)
50{
51 switch (type)
52 {
53 case GL_FLOAT_MAT2: return "float2x2";
54 case GL_FLOAT_MAT3: return "float3x3";
55 case GL_FLOAT_MAT4: return "float4x4";
56 case GL_FLOAT_MAT2x3: return "float2x3";
57 case GL_FLOAT_MAT3x2: return "float3x2";
58 case GL_FLOAT_MAT2x4: return "float2x4";
59 case GL_FLOAT_MAT4x2: return "float4x2";
60 case GL_FLOAT_MAT3x4: return "float3x4";
61 case GL_FLOAT_MAT4x3: return "float4x3";
62 default: UNREACHABLE(); return "not-matrix-type";
63 }
64}
65
66std::string HLSLTypeString(GLenum type)
67{
68 if (gl::IsMatrixType(type))
69 {
70 return HLSLMatrixTypeString(type);
71 }
72
73 return HLSLComponentTypeString(gl::UniformComponentType(type), gl::UniformComponentCount(type));
74}
75
76}
77
78namespace gl
79{
80
Jamie Madill5f562732014-02-14 16:41:24 -050081std::string ArrayString(unsigned int i)
82{
83 return (i == GL_INVALID_INDEX ? "" : "[" + Str(i) + "]");
84}
85
Jamie Madillc5ede1a2014-02-14 16:41:27 -050086const std::string DynamicHLSL::VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
87
Jamie Madill5f562732014-02-14 16:41:24 -050088DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
89 : mRenderer(renderer)
90{
91}
92
Geoff Lang48dcae72014-02-05 16:28:24 -050093static bool packVarying(sh::Varying *varying, const int maxVaryingVectors, const sh::ShaderVariable *packing[][4])
Jamie Madill5f562732014-02-14 16:41:24 -050094{
Geoff Lang48dcae72014-02-05 16:28:24 -050095 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -050096
Geoff Lang48dcae72014-02-05 16:28:24 -050097 // matrices within varying structs are not transposed
98 int registers = (varying->isStruct() ? sh::HLSLVariableRegisterCount(*varying) : gl::VariableRowCount(transposedType)) * varying->elementCount();
99 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
100 bool success = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500101
Geoff Lang48dcae72014-02-05 16:28:24 -0500102 if (elements == 2 || elements == 3 || elements == 4)
Jamie Madill5f562732014-02-14 16:41:24 -0500103 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500104 for (int r = 0; r <= maxVaryingVectors - registers && !success; r++)
Jamie Madill5f562732014-02-14 16:41:24 -0500105 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500106 bool available = true;
107
108 for (int y = 0; y < registers && available; y++)
109 {
110 for (int x = 0; x < elements && available; x++)
111 {
112 if (packing[r + y][x])
113 {
114 available = false;
115 }
116 }
117 }
118
119 if (available)
120 {
121 varying->registerIndex = r;
122 varying->elementIndex = 0;
123
124 for (int y = 0; y < registers; y++)
125 {
126 for (int x = 0; x < elements; x++)
127 {
128 packing[r + y][x] = &*varying;
129 }
130 }
131
132 success = true;
133 }
134 }
135
136 if (!success && elements == 2)
137 {
138 for (int r = maxVaryingVectors - registers; r >= 0 && !success; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500139 {
140 bool available = true;
141
142 for (int y = 0; y < registers && available; y++)
143 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500144 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500145 {
146 if (packing[r + y][x])
147 {
148 available = false;
149 }
150 }
151 }
152
153 if (available)
154 {
155 varying->registerIndex = r;
Geoff Lang48dcae72014-02-05 16:28:24 -0500156 varying->elementIndex = 2;
Jamie Madill5f562732014-02-14 16:41:24 -0500157
158 for (int y = 0; y < registers; y++)
159 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500160 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500161 {
162 packing[r + y][x] = &*varying;
163 }
164 }
165
166 success = true;
167 }
168 }
Jamie Madill5f562732014-02-14 16:41:24 -0500169 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500170 }
171 else if (elements == 1)
172 {
173 int space[4] = { 0 };
174
175 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500176 {
Jamie Madill5f562732014-02-14 16:41:24 -0500177 for (int x = 0; x < 4; x++)
178 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500179 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500180 }
181 }
Jamie Madill5f562732014-02-14 16:41:24 -0500182
Geoff Lang48dcae72014-02-05 16:28:24 -0500183 int column = 0;
184
185 for (int x = 0; x < 4; x++)
186 {
187 if (space[x] >= registers && space[x] < space[column])
188 {
189 column = x;
190 }
191 }
192
193 if (space[column] >= registers)
194 {
195 for (int r = 0; r < maxVaryingVectors; r++)
196 {
197 if (!packing[r][column])
198 {
199 varying->registerIndex = r;
200
201 for (int y = r; y < r + registers; y++)
202 {
203 packing[y][column] = &*varying;
204 }
205
206 break;
207 }
208 }
209
210 varying->elementIndex = column;
211
212 success = true;
213 }
214 }
215 else UNREACHABLE();
216
217 return success;
218}
219
220// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
221// Returns the number of used varying registers, or -1 if unsuccesful
222int DynamicHLSL::packVaryings(InfoLog &infoLog, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader,
223 VertexShader *vertexShader, const std::vector<std::string>& transformFeedbackVaryings)
224{
225 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
226
227 vertexShader->resetVaryingsRegisterAssignment();
228 fragmentShader->resetVaryingsRegisterAssignment();
229
230 std::set<std::string> packedVaryings;
231
232 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
233 {
234 sh::Varying *varying = &fragmentShader->mVaryings[varyingIndex];
235 if (packVarying(varying, maxVaryingVectors, packing))
236 {
237 packedVaryings.insert(varying->name);
238 }
239 else
Jamie Madill5f562732014-02-14 16:41:24 -0500240 {
241 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500242 return -1;
243 }
244 }
245
Geoff Lang48dcae72014-02-05 16:28:24 -0500246 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
247 {
248 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
249 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
250 {
251 bool found = false;
252 for (unsigned int varyingIndex = 0; varyingIndex < vertexShader->mVaryings.size(); varyingIndex++)
253 {
254 sh::Varying *varying = &vertexShader->mVaryings[varyingIndex];
255 if (transformFeedbackVarying == varying->name)
256 {
257 if (!packVarying(varying, maxVaryingVectors, packing))
258 {
259 infoLog.append("Could not pack varying %s", varying->name.c_str());
260 return -1;
261 }
262
263 found = true;
264 break;
265 }
266 }
267
268 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
269 {
270 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
271 return -1;
272 }
273 }
274 }
275
Jamie Madill5f562732014-02-14 16:41:24 -0500276 // Return the number of used registers
277 int registers = 0;
278
279 for (int r = 0; r < maxVaryingVectors; r++)
280 {
281 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
282 {
283 registers++;
284 }
285 }
286
287 return registers;
288}
289
Geoff Lang48dcae72014-02-05 16:28:24 -0500290std::string DynamicHLSL::generateVaryingHLSL(VertexShader *shader, const std::string &varyingSemantic,
291 std::vector<LinkedVarying> *linkedVaryings) const
Jamie Madill5f562732014-02-14 16:41:24 -0500292{
293 std::string varyingHLSL;
294
Geoff Lang48dcae72014-02-05 16:28:24 -0500295 for (unsigned int varyingIndex = 0; varyingIndex < shader->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500296 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500297 sh::Varying *varying = &shader->mVaryings[varyingIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500298 if (varying->registerAssigned())
299 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500300 GLenum transposedType = TransposeMatrixType(varying->type);
301 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
302
Jamie Madill5f562732014-02-14 16:41:24 -0500303 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
304 {
Jamie Madill5f562732014-02-14 16:41:24 -0500305 for (int row = 0; row < variableRows; row++)
306 {
307 switch (varying->interpolation)
308 {
309 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
310 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
311 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
312 default: UNREACHABLE();
313 }
314
Geoff Lang48dcae72014-02-05 16:28:24 -0500315 unsigned int semanticIndex = elementIndex * variableRows + varying->registerIndex + row;
316 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500317
Jamie Madilla53ab512014-03-17 09:47:44 -0400318 std::string typeString;
319
320 if (varying->isStruct())
321 {
322 // matrices within structs are not transposed, so
323 // do not use the special struct prefix "rm"
324 typeString = decorateVariable(varying->structName);
325 }
326 else
327 {
328 GLenum componentType = gl::UniformComponentType(transposedType);
329 int columnCount = gl::VariableColumnCount(transposedType);
330 typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
331 }
Jamie Madill5f562732014-02-14 16:41:24 -0500332 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
333 }
334 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500335
336 if (linkedVaryings)
337 {
338 linkedVaryings->push_back(LinkedVarying(varying->name, varying->type, varying->elementCount(),
339 varyingSemantic, varying->registerIndex,
340 variableRows * varying->elementCount()));
341 }
Jamie Madill5f562732014-02-14 16:41:24 -0500342 }
Jamie Madill5f562732014-02-14 16:41:24 -0500343 }
344
345 return varyingHLSL;
346}
347
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500348std::string DynamicHLSL::generateInputLayoutHLSL(const VertexFormat inputLayout[], const sh::Attribute shaderAttributes[]) const
349{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400350 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500351
352 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400353 unsigned int inputIndex = 0;
354
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500355 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
356 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400357 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
358
359 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500360 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500361
Jamie Madill8664b062014-02-14 16:41:29 -0500362 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500363 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400364 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500365 if (IsMatrixType(shaderAttribute.type))
366 {
367 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400368 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500369 }
370 else
371 {
372 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madill3b7e2052014-03-17 09:47:43 -0400373 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, UniformComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500374 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500375
Jamie Madilla53ab512014-03-17 09:47:44 -0400376 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madill8664b062014-02-14 16:41:29 -0500377 semanticIndex += AttributeRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500378
Jamie Madill3b7e2052014-03-17 09:47:43 -0400379 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400380 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500381
382 // Mismatched vertex attribute to vertex input may result in an undefined
383 // data reinterpretation (eg for pure integer->float, float->pure integer)
384 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400385 if (IsMatrixType(shaderAttribute.type) ||
386 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500387 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400388 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500389 }
390 else
391 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400392 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500393 }
394
Jamie Madill3b7e2052014-03-17 09:47:43 -0400395 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400396
Jamie Madillac0a2672014-04-11 13:33:56 -0400397 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
398 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500399 }
400
Jamie Madill3b7e2052014-03-17 09:47:43 -0400401 return "struct VS_INPUT\n"
402 "{\n" +
403 structHLSL +
404 "};\n"
405 "\n"
406 "void initAttributes(VS_INPUT input)\n"
407 "{\n" +
408 initHLSL +
409 "}\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500410}
411
Jamie Madill5f562732014-02-14 16:41:24 -0500412bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const sh::ShaderVariable *packing[][4],
413 std::string& pixelHLSL, std::string& vertexHLSL,
414 FragmentShader *fragmentShader, VertexShader *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500415 const std::vector<std::string>& transformFeedbackVaryings,
416 std::vector<LinkedVarying> *linkedVaryings,
Jamie Madill5f562732014-02-14 16:41:24 -0500417 std::map<int, VariableLocation> *programOutputVars) const
418{
419 if (pixelHLSL.empty() || vertexHLSL.empty())
420 {
421 return false;
422 }
423
424 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
425 bool usesFragColor = fragmentShader->mUsesFragColor;
426 bool usesFragData = fragmentShader->mUsesFragData;
427 if (usesFragColor && usesFragData)
428 {
429 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
430 return false;
431 }
432
433 // Write the HLSL input/output declarations
434 const int shaderModel = mRenderer->getMajorShaderModel();
435 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
436
437 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
438
439 // Two cases when writing to gl_FragColor and using ESSL 1.0:
440 // - with a 3.0 context, the output color is copied to channel 0
441 // - with a 2.0 context, the output color is broadcast to all channels
442 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
443 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
444
445 int shaderVersion = vertexShader->getShaderVersion();
446
447 if (registersNeeded > maxVaryingVectors)
448 {
449 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
450
451 return false;
452 }
453
454 std::string varyingSemantic = (vertexShader->mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
455 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
Geoff Lang48dcae72014-02-05 16:28:24 -0500456 std::string dxPositionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
Jamie Madill5f562732014-02-14 16:41:24 -0500457 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
458
Geoff Lang48dcae72014-02-05 16:28:24 -0500459 std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500460
461 // special varyings that use reserved registers
462 int reservedRegisterIndex = registers;
Jamie Madill5f562732014-02-14 16:41:24 -0500463
Geoff Lang48dcae72014-02-05 16:28:24 -0500464 unsigned int glPositionSemanticIndex = reservedRegisterIndex++;
465 std::string glPositionSemantic = varyingSemantic;
466
467 std::string fragCoordSemantic;
468 unsigned int fragCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500469 if (fragmentShader->mUsesFragCoord)
470 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500471 fragCoordSemanticIndex = reservedRegisterIndex++;
472 fragCoordSemantic = varyingSemantic;
Jamie Madill5f562732014-02-14 16:41:24 -0500473 }
474
Geoff Lang48dcae72014-02-05 16:28:24 -0500475 std::string pointCoordSemantic;
476 unsigned int pointCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500477 if (fragmentShader->mUsesPointCoord)
478 {
479 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
480 // In DX11 we compute this in the GS.
481 if (shaderModel == 3)
482 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500483 pointCoordSemanticIndex = 0;
Jamie Madill5f562732014-02-14 16:41:24 -0500484 pointCoordSemantic = "TEXCOORD0";
485 }
486 else if (shaderModel >= 4)
487 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500488 pointCoordSemanticIndex = reservedRegisterIndex++;
489 pointCoordSemantic = varyingSemantic;
Jamie Madill5f562732014-02-14 16:41:24 -0500490 }
491 }
492
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500493 // Add stub string to be replaced when shader is dynamically defined by its layout
494 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500495
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500496 vertexHLSL += "struct VS_OUTPUT\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500497 "{\n";
498
499 if (shaderModel < 4)
500 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500501 vertexHLSL += " float4 _dx_Position : " + dxPositionSemantic + ";\n";
502 vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n";
503 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1));
504
Jamie Madill5f562732014-02-14 16:41:24 -0500505 }
506
507 vertexHLSL += varyingHLSL;
508
509 if (fragmentShader->mUsesFragCoord)
510 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500511 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n";
512 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, fragCoordSemantic, fragCoordSemanticIndex, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500513 }
514
515 if (vertexShader->mUsesPointSize && shaderModel >= 3)
516 {
517 vertexHLSL += " float gl_PointSize : PSIZE;\n";
Geoff Lang48dcae72014-02-05 16:28:24 -0500518 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500519 }
520
521 if (shaderModel >= 4)
522 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500523 vertexHLSL += " float4 _dx_Position : " + dxPositionSemantic + ";\n";
524 vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n";
525 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1));
Jamie Madill5f562732014-02-14 16:41:24 -0500526 }
527
528 vertexHLSL += "};\n"
529 "\n"
530 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500531 "{\n"
532 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500533
534 if (shaderModel >= 4)
535 {
536 vertexHLSL += "\n"
537 " gl_main();\n"
538 "\n"
539 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500540 " output.gl_Position = gl_Position;\n"
541 " output._dx_Position.x = gl_Position.x;\n"
542 " output._dx_Position.y = -gl_Position.y;\n"
543 " output._dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
544 " output._dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500545 }
546 else
547 {
548 vertexHLSL += "\n"
549 " gl_main();\n"
550 "\n"
551 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500552 " output.gl_Position = gl_Position;\n"
553 " output._dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
554 " output._dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
555 " output._dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
556 " output._dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500557 }
558
559 if (vertexShader->mUsesPointSize && shaderModel >= 3)
560 {
561 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
562 }
563
564 if (fragmentShader->mUsesFragCoord)
565 {
566 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
567 }
568
569 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
570 {
571 sh::Varying *varying = &vertexShader->mVaryings[vertVaryingIndex];
572 if (varying->registerAssigned())
573 {
574 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
575 {
576 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying->type)));
577
578 for (int row = 0; row < variableRows; row++)
579 {
580 int r = varying->registerIndex + elementIndex * variableRows + row;
581 vertexHLSL += " output.v" + Str(r);
582
583 bool sharedRegister = false; // Register used by multiple varyings
584
585 for (int x = 0; x < 4; x++)
586 {
587 if (packing[r][x] && packing[r][x] != packing[r][0])
588 {
589 sharedRegister = true;
590 break;
591 }
592 }
593
594 if(sharedRegister)
595 {
596 vertexHLSL += ".";
597
598 for (int x = 0; x < 4; x++)
599 {
600 if (packing[r][x] == &*varying)
601 {
602 switch(x)
603 {
604 case 0: vertexHLSL += "x"; break;
605 case 1: vertexHLSL += "y"; break;
606 case 2: vertexHLSL += "z"; break;
607 case 3: vertexHLSL += "w"; break;
608 }
609 }
610 }
611 }
612
613 vertexHLSL += " = _" + varying->name;
614
615 if (varying->isArray())
616 {
617 vertexHLSL += ArrayString(elementIndex);
618 }
619
620 if (variableRows > 1)
621 {
622 vertexHLSL += ArrayString(row);
623 }
624
625 vertexHLSL += ";\n";
626 }
627 }
628 }
629 }
630
631 vertexHLSL += "\n"
632 " return output;\n"
633 "}\n";
634
635 pixelHLSL += "struct PS_INPUT\n"
636 "{\n";
637
638 pixelHLSL += varyingHLSL;
639
640 if (fragmentShader->mUsesFragCoord)
641 {
Geoff Langb5b02852014-04-16 14:39:36 -0400642 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500643 }
644
645 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
646 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500647 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + Str(pointCoordSemanticIndex) + ";\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500648 }
649
650 // Must consume the PSIZE element if the geometry shader is not active
651 // We won't know if we use a GS until we draw
652 if (vertexShader->mUsesPointSize && shaderModel >= 4)
653 {
654 pixelHLSL += " float gl_PointSize : PSIZE;\n";
655 }
656
657 if (fragmentShader->mUsesFragCoord)
658 {
659 if (shaderModel >= 4)
660 {
661 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
662 }
663 else if (shaderModel >= 3)
664 {
665 pixelHLSL += " float2 dx_VPos : VPOS;\n";
666 }
667 }
668
669 pixelHLSL += "};\n"
670 "\n"
671 "struct PS_OUTPUT\n"
672 "{\n";
673
674 if (shaderVersion < 300)
675 {
676 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
677 {
678 pixelHLSL += " float4 gl_Color" + Str(renderTargetIndex) + " : " + targetSemantic + Str(renderTargetIndex) + ";\n";
679 }
680
681 if (fragmentShader->mUsesFragDepth)
682 {
683 pixelHLSL += " float gl_Depth : " + depthSemantic + ";\n";
684 }
685 }
686 else
687 {
688 defineOutputVariables(fragmentShader, programOutputVars);
689
690 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
691 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
692 {
693 const VariableLocation &outputLocation = locationIt->second;
694 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
695 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
696
Jamie Madill8664b062014-02-14 16:41:29 -0500697 pixelHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) +
Jamie Madill5f562732014-02-14 16:41:24 -0500698 " out_" + outputLocation.name + elementString +
699 " : " + targetSemantic + Str(locationIt->first) + ";\n";
700 }
701 }
702
703 pixelHLSL += "};\n"
704 "\n";
705
706 if (fragmentShader->mUsesFrontFacing)
707 {
708 if (shaderModel >= 4)
709 {
710 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
711 "{\n";
712 }
713 else
714 {
715 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
716 "{\n";
717 }
718 }
719 else
720 {
721 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
722 "{\n";
723 }
724
725 if (fragmentShader->mUsesFragCoord)
726 {
727 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
728
729 if (shaderModel >= 4)
730 {
731 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
732 " gl_FragCoord.y = input.dx_VPos.y;\n";
733 }
734 else if (shaderModel >= 3)
735 {
736 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
737 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
738 }
739 else
740 {
741 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
742 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
743 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
744 }
745
746 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
747 " gl_FragCoord.w = rhw;\n";
748 }
749
750 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
751 {
752 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
753 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
754 }
755
756 if (fragmentShader->mUsesFrontFacing)
757 {
758 if (shaderModel <= 3)
759 {
760 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
761 }
762 else
763 {
764 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
765 }
766 }
767
768 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
769 {
770 sh::Varying *varying = &fragmentShader->mVaryings[varyingIndex];
771 if (varying->registerAssigned())
772 {
773 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
774 {
775 GLenum transposedType = TransposeMatrixType(varying->type);
776 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
777 for (int row = 0; row < variableRows; row++)
778 {
779 std::string n = Str(varying->registerIndex + elementIndex * variableRows + row);
780 pixelHLSL += " _" + varying->name;
781
782 if (varying->isArray())
783 {
784 pixelHLSL += ArrayString(elementIndex);
785 }
786
787 if (variableRows > 1)
788 {
789 pixelHLSL += ArrayString(row);
790 }
791
792 if (varying->isStruct())
793 {
794 pixelHLSL += " = input.v" + n + ";\n"; break;
795 }
796 else
797 {
798 switch (VariableColumnCount(transposedType))
799 {
800 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
801 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
802 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
803 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
804 default: UNREACHABLE();
805 }
806 }
807 }
808 }
809 }
810 else UNREACHABLE();
811 }
812
813 pixelHLSL += "\n"
814 " gl_main();\n"
815 "\n"
816 " PS_OUTPUT output;\n";
817
818 if (shaderVersion < 300)
819 {
820 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
821 {
822 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
823
824 pixelHLSL += " output.gl_Color" + Str(renderTargetIndex) + " = gl_Color[" + Str(sourceColorIndex) + "];\n";
825 }
826
827 if (fragmentShader->mUsesFragDepth)
828 {
829 pixelHLSL += " output.gl_Depth = gl_Depth;\n";
830 }
831 }
832 else
833 {
834 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
835 {
836 const VariableLocation &outputLocation = locationIt->second;
837 const std::string &variableName = "out_" + outputLocation.name;
838 const std::string &outVariableName = variableName + (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
839 const std::string &staticVariableName = variableName + ArrayString(outputLocation.element);
840
841 pixelHLSL += " output." + outVariableName + " = " + staticVariableName + ";\n";
842 }
843 }
844
845 pixelHLSL += "\n"
846 " return output;\n"
847 "}\n";
848
849 return true;
850}
851
852void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
853{
854 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
855
856 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
857 {
858 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
859 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
860
861 if (outputVariable.arraySize > 0)
862 {
863 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
864 {
865 const int location = baseLocation + elementIndex;
866 ASSERT(programOutputVars->count(location) == 0);
867 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
868 }
869 }
870 else
871 {
872 ASSERT(programOutputVars->count(baseLocation) == 0);
873 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
874 }
875 }
876}
877
878std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
879{
880 // for now we only handle point sprite emulation
881 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
882 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
883}
884
885std::string DynamicHLSL::generatePointSpriteHLSL(int registers, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
886{
887 ASSERT(registers >= 0);
888 ASSERT(vertexShader->mUsesPointSize);
889 ASSERT(mRenderer->getMajorShaderModel() >= 4);
890
891 std::string geomHLSL;
892
893 std::string varyingSemantic = "TEXCOORD";
894
895 std::string fragCoordSemantic;
896 std::string pointCoordSemantic;
897
898 int reservedRegisterIndex = registers;
899
900 if (fragmentShader->mUsesFragCoord)
901 {
902 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
903 }
904
905 if (fragmentShader->mUsesPointCoord)
906 {
907 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
908 }
909
910 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
911 "\n"
912 "struct GS_INPUT\n"
913 "{\n";
914
Geoff Lang48dcae72014-02-05 16:28:24 -0500915 std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, NULL);
Jamie Madill5f562732014-02-14 16:41:24 -0500916
917 geomHLSL += varyingHLSL;
918
919 if (fragmentShader->mUsesFragCoord)
920 {
921 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
922 }
923
924 geomHLSL += " float gl_PointSize : PSIZE;\n"
925 " float4 gl_Position : SV_Position;\n"
926 "};\n"
927 "\n"
928 "struct GS_OUTPUT\n"
929 "{\n";
930
931 geomHLSL += varyingHLSL;
932
933 if (fragmentShader->mUsesFragCoord)
934 {
935 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
936 }
937
938 if (fragmentShader->mUsesPointCoord)
939 {
940 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
941 }
942
943 geomHLSL += " float gl_PointSize : PSIZE;\n"
944 " float4 gl_Position : SV_Position;\n"
945 "};\n"
946 "\n"
947 "static float2 pointSpriteCorners[] = \n"
948 "{\n"
949 " float2( 0.5f, -0.5f),\n"
950 " float2( 0.5f, 0.5f),\n"
951 " float2(-0.5f, -0.5f),\n"
952 " float2(-0.5f, 0.5f)\n"
953 "};\n"
954 "\n"
955 "static float2 pointSpriteTexcoords[] = \n"
956 "{\n"
957 " float2(1.0f, 1.0f),\n"
958 " float2(1.0f, 0.0f),\n"
959 " float2(0.0f, 1.0f),\n"
960 " float2(0.0f, 0.0f)\n"
961 "};\n"
962 "\n"
963 "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
964 "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n"
965 "\n"
966 "[maxvertexcount(4)]\n"
967 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
968 "{\n"
969 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
970 " output.gl_PointSize = input[0].gl_PointSize;\n";
971
972 for (int r = 0; r < registers; r++)
973 {
974 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
975 }
976
977 if (fragmentShader->mUsesFragCoord)
978 {
979 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
980 }
981
982 geomHLSL += " \n"
983 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
984 " float4 gl_Position = input[0].gl_Position;\n"
985 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
986
987 for (int corner = 0; corner < 4; corner++)
988 {
989 geomHLSL += " \n"
990 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
991
992 if (fragmentShader->mUsesPointCoord)
993 {
994 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
995 }
996
997 geomHLSL += " outStream.Append(output);\n";
998 }
999
1000 geomHLSL += " \n"
1001 " outStream.RestartStrip();\n"
1002 "}\n";
1003
1004 return geomHLSL;
1005}
1006
1007// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001008std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001009{
1010 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
1011 {
1012 return "_" + name;
1013 }
1014
1015 return name;
1016}
1017
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001018std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
1019{
Jamie Madilla53ab512014-03-17 09:47:44 -04001020 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001021
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001022 // Matrix
1023 if (IsMatrixType(shaderAttrib.type))
1024 {
Jamie Madill8664b062014-02-14 16:41:29 -05001025 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001026 }
1027
Jamie Madill8664b062014-02-14 16:41:29 -05001028 GLenum shaderComponentType = UniformComponentType(shaderAttrib.type);
1029 int shaderComponentCount = UniformComponentCount(shaderAttrib.type);
1030
1031 std::string padString = "";
1032
1033 // Perform integer to float conversion (if necessary)
1034 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1035
1036 // TODO: normalization for 32-bit integer formats
1037 ASSERT(!requiresTypeConversion || !vertexFormat.mNormalized);
1038
1039 if (requiresTypeConversion || !padString.empty())
1040 {
1041 return "float" + Str(shaderComponentCount) + "(" + attribString + padString + ")";
1042 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001043
1044 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001045 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001046}
1047
Jamie Madill5f562732014-02-14 16:41:24 -05001048}