blob: 585c0153073953cb33b2cbef84945e400d085cc7 [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
93// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
94// Returns the number of used varying registers, or -1 if unsuccesful
95int DynamicHLSL::packVaryings(InfoLog &infoLog, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader)
96{
97 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
98
99 fragmentShader->resetVaryingsRegisterAssignment();
100
101 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
102 {
103 sh::Varying *varying = &fragmentShader->mVaryings[varyingIndex];
104 GLenum transposedType = TransposeMatrixType(varying->type);
105
106 // matrices within varying structs are not transposed
107 int registers = (varying->isStruct() ? sh::HLSLVariableRegisterCount(*varying) : gl::VariableRowCount(transposedType)) * varying->elementCount();
108 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
109 bool success = false;
110
111 if (elements == 2 || elements == 3 || elements == 4)
112 {
113 for (int r = 0; r <= maxVaryingVectors - registers && !success; r++)
114 {
115 bool available = true;
116
117 for (int y = 0; y < registers && available; y++)
118 {
119 for (int x = 0; x < elements && available; x++)
120 {
121 if (packing[r + y][x])
122 {
123 available = false;
124 }
125 }
126 }
127
128 if (available)
129 {
130 varying->registerIndex = r;
131 varying->elementIndex = 0;
132
133 for (int y = 0; y < registers; y++)
134 {
135 for (int x = 0; x < elements; x++)
136 {
137 packing[r + y][x] = &*varying;
138 }
139 }
140
141 success = true;
142 }
143 }
144
145 if (!success && elements == 2)
146 {
147 for (int r = maxVaryingVectors - registers; r >= 0 && !success; r--)
148 {
149 bool available = true;
150
151 for (int y = 0; y < registers && available; y++)
152 {
153 for (int x = 2; x < 4 && available; x++)
154 {
155 if (packing[r + y][x])
156 {
157 available = false;
158 }
159 }
160 }
161
162 if (available)
163 {
164 varying->registerIndex = r;
165 varying->elementIndex = 2;
166
167 for (int y = 0; y < registers; y++)
168 {
169 for (int x = 2; x < 4; x++)
170 {
171 packing[r + y][x] = &*varying;
172 }
173 }
174
175 success = true;
176 }
177 }
178 }
179 }
180 else if (elements == 1)
181 {
182 int space[4] = {0};
183
184 for (int y = 0; y < maxVaryingVectors; y++)
185 {
186 for (int x = 0; x < 4; x++)
187 {
188 space[x] += packing[y][x] ? 0 : 1;
189 }
190 }
191
192 int column = 0;
193
194 for (int x = 0; x < 4; x++)
195 {
196 if (space[x] >= registers && space[x] < space[column])
197 {
198 column = x;
199 }
200 }
201
202 if (space[column] >= registers)
203 {
204 for (int r = 0; r < maxVaryingVectors; r++)
205 {
206 if (!packing[r][column])
207 {
208 varying->registerIndex = r;
209
210 for (int y = r; y < r + registers; y++)
211 {
212 packing[y][column] = &*varying;
213 }
214
215 break;
216 }
217 }
218
219 varying->elementIndex = column;
220
221 success = true;
222 }
223 }
224 else UNREACHABLE();
225
226 if (!success)
227 {
228 infoLog.append("Could not pack varying %s", varying->name.c_str());
229
230 return -1;
231 }
232 }
233
234 // Return the number of used registers
235 int registers = 0;
236
237 for (int r = 0; r < maxVaryingVectors; r++)
238 {
239 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
240 {
241 registers++;
242 }
243 }
244
245 return registers;
246}
247
248std::string DynamicHLSL::generateVaryingHLSL(FragmentShader *fragmentShader, const std::string &varyingSemantic) const
249{
250 std::string varyingHLSL;
251
252 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
253 {
254 sh::Varying *varying = &fragmentShader->mVaryings[varyingIndex];
255 if (varying->registerAssigned())
256 {
257 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
258 {
259 GLenum transposedType = TransposeMatrixType(varying->type);
260 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
261 for (int row = 0; row < variableRows; row++)
262 {
263 switch (varying->interpolation)
264 {
265 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
266 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
267 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
268 default: UNREACHABLE();
269 }
270
271 std::string n = Str(varying->registerIndex + elementIndex * variableRows + row);
272
Jamie Madilla53ab512014-03-17 09:47:44 -0400273 std::string typeString;
274
275 if (varying->isStruct())
276 {
277 // matrices within structs are not transposed, so
278 // do not use the special struct prefix "rm"
279 typeString = decorateVariable(varying->structName);
280 }
281 else
282 {
283 GLenum componentType = gl::UniformComponentType(transposedType);
284 int columnCount = gl::VariableColumnCount(transposedType);
285 typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
286 }
Jamie Madill5f562732014-02-14 16:41:24 -0500287 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
288 }
289 }
290 }
291 else UNREACHABLE();
292 }
293
294 return varyingHLSL;
295}
296
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500297std::string DynamicHLSL::generateInputLayoutHLSL(const VertexFormat inputLayout[], const sh::Attribute shaderAttributes[]) const
298{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400299 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500300
301 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400302 unsigned int inputIndex = 0;
303
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500304 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
305 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400306 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
307
308 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500309 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500310
Jamie Madill8664b062014-02-14 16:41:29 -0500311 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500312 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400313 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500314 if (IsMatrixType(shaderAttribute.type))
315 {
316 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400317 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500318 }
319 else
320 {
321 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madill3b7e2052014-03-17 09:47:43 -0400322 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, UniformComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500323 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500324
Jamie Madilla53ab512014-03-17 09:47:44 -0400325 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madill8664b062014-02-14 16:41:29 -0500326 semanticIndex += AttributeRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500327
Jamie Madill3b7e2052014-03-17 09:47:43 -0400328 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400329 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500330
331 // Mismatched vertex attribute to vertex input may result in an undefined
332 // data reinterpretation (eg for pure integer->float, float->pure integer)
333 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400334 if (IsMatrixType(shaderAttribute.type) ||
335 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500336 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400337 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500338 }
339 else
340 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400341 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500342 }
343
Jamie Madill3b7e2052014-03-17 09:47:43 -0400344 initHLSL += ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500345 }
Jamie Madill3b7e2052014-03-17 09:47:43 -0400346
347 inputIndex += VariableRowCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500348 }
349
Jamie Madill3b7e2052014-03-17 09:47:43 -0400350 return "struct VS_INPUT\n"
351 "{\n" +
352 structHLSL +
353 "};\n"
354 "\n"
355 "void initAttributes(VS_INPUT input)\n"
356 "{\n" +
357 initHLSL +
358 "}\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500359}
360
Jamie Madill5f562732014-02-14 16:41:24 -0500361bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const sh::ShaderVariable *packing[][4],
362 std::string& pixelHLSL, std::string& vertexHLSL,
363 FragmentShader *fragmentShader, VertexShader *vertexShader,
364 std::map<int, VariableLocation> *programOutputVars) const
365{
366 if (pixelHLSL.empty() || vertexHLSL.empty())
367 {
368 return false;
369 }
370
371 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
372 bool usesFragColor = fragmentShader->mUsesFragColor;
373 bool usesFragData = fragmentShader->mUsesFragData;
374 if (usesFragColor && usesFragData)
375 {
376 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
377 return false;
378 }
379
380 // Write the HLSL input/output declarations
381 const int shaderModel = mRenderer->getMajorShaderModel();
382 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
383
384 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
385
386 // Two cases when writing to gl_FragColor and using ESSL 1.0:
387 // - with a 3.0 context, the output color is copied to channel 0
388 // - with a 2.0 context, the output color is broadcast to all channels
389 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
390 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
391
392 int shaderVersion = vertexShader->getShaderVersion();
393
394 if (registersNeeded > maxVaryingVectors)
395 {
396 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
397
398 return false;
399 }
400
401 std::string varyingSemantic = (vertexShader->mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
402 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
403 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
404 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
405
406 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
407
408 // special varyings that use reserved registers
409 int reservedRegisterIndex = registers;
410 std::string fragCoordSemantic;
411 std::string pointCoordSemantic;
412
413 if (fragmentShader->mUsesFragCoord)
414 {
415 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
416 }
417
418 if (fragmentShader->mUsesPointCoord)
419 {
420 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
421 // In DX11 we compute this in the GS.
422 if (shaderModel == 3)
423 {
424 pointCoordSemantic = "TEXCOORD0";
425 }
426 else if (shaderModel >= 4)
427 {
428 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
429 }
430 }
431
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500432 // Add stub string to be replaced when shader is dynamically defined by its layout
433 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500434
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500435 vertexHLSL += "struct VS_OUTPUT\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500436 "{\n";
437
438 if (shaderModel < 4)
439 {
440 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
441 }
442
443 vertexHLSL += varyingHLSL;
444
445 if (fragmentShader->mUsesFragCoord)
446 {
447 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
448 }
449
450 if (vertexShader->mUsesPointSize && shaderModel >= 3)
451 {
452 vertexHLSL += " float gl_PointSize : PSIZE;\n";
453 }
454
455 if (shaderModel >= 4)
456 {
457 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
458 }
459
460 vertexHLSL += "};\n"
461 "\n"
462 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500463 "{\n"
464 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500465
466 if (shaderModel >= 4)
467 {
468 vertexHLSL += "\n"
469 " gl_main();\n"
470 "\n"
471 " VS_OUTPUT output;\n"
472 " output.gl_Position.x = gl_Position.x;\n"
473 " output.gl_Position.y = -gl_Position.y;\n"
474 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
475 " output.gl_Position.w = gl_Position.w;\n";
476 }
477 else
478 {
479 vertexHLSL += "\n"
480 " gl_main();\n"
481 "\n"
482 " VS_OUTPUT output;\n"
483 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
484 " output.gl_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
485 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
486 " output.gl_Position.w = gl_Position.w;\n";
487 }
488
489 if (vertexShader->mUsesPointSize && shaderModel >= 3)
490 {
491 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
492 }
493
494 if (fragmentShader->mUsesFragCoord)
495 {
496 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
497 }
498
499 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
500 {
501 sh::Varying *varying = &vertexShader->mVaryings[vertVaryingIndex];
502 if (varying->registerAssigned())
503 {
504 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
505 {
506 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying->type)));
507
508 for (int row = 0; row < variableRows; row++)
509 {
510 int r = varying->registerIndex + elementIndex * variableRows + row;
511 vertexHLSL += " output.v" + Str(r);
512
513 bool sharedRegister = false; // Register used by multiple varyings
514
515 for (int x = 0; x < 4; x++)
516 {
517 if (packing[r][x] && packing[r][x] != packing[r][0])
518 {
519 sharedRegister = true;
520 break;
521 }
522 }
523
524 if(sharedRegister)
525 {
526 vertexHLSL += ".";
527
528 for (int x = 0; x < 4; x++)
529 {
530 if (packing[r][x] == &*varying)
531 {
532 switch(x)
533 {
534 case 0: vertexHLSL += "x"; break;
535 case 1: vertexHLSL += "y"; break;
536 case 2: vertexHLSL += "z"; break;
537 case 3: vertexHLSL += "w"; break;
538 }
539 }
540 }
541 }
542
543 vertexHLSL += " = _" + varying->name;
544
545 if (varying->isArray())
546 {
547 vertexHLSL += ArrayString(elementIndex);
548 }
549
550 if (variableRows > 1)
551 {
552 vertexHLSL += ArrayString(row);
553 }
554
555 vertexHLSL += ";\n";
556 }
557 }
558 }
559 }
560
561 vertexHLSL += "\n"
562 " return output;\n"
563 "}\n";
564
565 pixelHLSL += "struct PS_INPUT\n"
566 "{\n";
567
568 pixelHLSL += varyingHLSL;
569
570 if (fragmentShader->mUsesFragCoord)
571 {
572 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
573 }
574
575 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
576 {
577 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
578 }
579
580 // Must consume the PSIZE element if the geometry shader is not active
581 // We won't know if we use a GS until we draw
582 if (vertexShader->mUsesPointSize && shaderModel >= 4)
583 {
584 pixelHLSL += " float gl_PointSize : PSIZE;\n";
585 }
586
587 if (fragmentShader->mUsesFragCoord)
588 {
589 if (shaderModel >= 4)
590 {
591 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
592 }
593 else if (shaderModel >= 3)
594 {
595 pixelHLSL += " float2 dx_VPos : VPOS;\n";
596 }
597 }
598
599 pixelHLSL += "};\n"
600 "\n"
601 "struct PS_OUTPUT\n"
602 "{\n";
603
604 if (shaderVersion < 300)
605 {
606 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
607 {
608 pixelHLSL += " float4 gl_Color" + Str(renderTargetIndex) + " : " + targetSemantic + Str(renderTargetIndex) + ";\n";
609 }
610
611 if (fragmentShader->mUsesFragDepth)
612 {
613 pixelHLSL += " float gl_Depth : " + depthSemantic + ";\n";
614 }
615 }
616 else
617 {
618 defineOutputVariables(fragmentShader, programOutputVars);
619
620 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
621 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
622 {
623 const VariableLocation &outputLocation = locationIt->second;
624 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
625 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
626
Jamie Madill8664b062014-02-14 16:41:29 -0500627 pixelHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) +
Jamie Madill5f562732014-02-14 16:41:24 -0500628 " out_" + outputLocation.name + elementString +
629 " : " + targetSemantic + Str(locationIt->first) + ";\n";
630 }
631 }
632
633 pixelHLSL += "};\n"
634 "\n";
635
636 if (fragmentShader->mUsesFrontFacing)
637 {
638 if (shaderModel >= 4)
639 {
640 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
641 "{\n";
642 }
643 else
644 {
645 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
646 "{\n";
647 }
648 }
649 else
650 {
651 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
652 "{\n";
653 }
654
655 if (fragmentShader->mUsesFragCoord)
656 {
657 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
658
659 if (shaderModel >= 4)
660 {
661 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
662 " gl_FragCoord.y = input.dx_VPos.y;\n";
663 }
664 else if (shaderModel >= 3)
665 {
666 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
667 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
668 }
669 else
670 {
671 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
672 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
673 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
674 }
675
676 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
677 " gl_FragCoord.w = rhw;\n";
678 }
679
680 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
681 {
682 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
683 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
684 }
685
686 if (fragmentShader->mUsesFrontFacing)
687 {
688 if (shaderModel <= 3)
689 {
690 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
691 }
692 else
693 {
694 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
695 }
696 }
697
698 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
699 {
700 sh::Varying *varying = &fragmentShader->mVaryings[varyingIndex];
701 if (varying->registerAssigned())
702 {
703 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
704 {
705 GLenum transposedType = TransposeMatrixType(varying->type);
706 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
707 for (int row = 0; row < variableRows; row++)
708 {
709 std::string n = Str(varying->registerIndex + elementIndex * variableRows + row);
710 pixelHLSL += " _" + varying->name;
711
712 if (varying->isArray())
713 {
714 pixelHLSL += ArrayString(elementIndex);
715 }
716
717 if (variableRows > 1)
718 {
719 pixelHLSL += ArrayString(row);
720 }
721
722 if (varying->isStruct())
723 {
724 pixelHLSL += " = input.v" + n + ";\n"; break;
725 }
726 else
727 {
728 switch (VariableColumnCount(transposedType))
729 {
730 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
731 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
732 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
733 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
734 default: UNREACHABLE();
735 }
736 }
737 }
738 }
739 }
740 else UNREACHABLE();
741 }
742
743 pixelHLSL += "\n"
744 " gl_main();\n"
745 "\n"
746 " PS_OUTPUT output;\n";
747
748 if (shaderVersion < 300)
749 {
750 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
751 {
752 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
753
754 pixelHLSL += " output.gl_Color" + Str(renderTargetIndex) + " = gl_Color[" + Str(sourceColorIndex) + "];\n";
755 }
756
757 if (fragmentShader->mUsesFragDepth)
758 {
759 pixelHLSL += " output.gl_Depth = gl_Depth;\n";
760 }
761 }
762 else
763 {
764 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
765 {
766 const VariableLocation &outputLocation = locationIt->second;
767 const std::string &variableName = "out_" + outputLocation.name;
768 const std::string &outVariableName = variableName + (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
769 const std::string &staticVariableName = variableName + ArrayString(outputLocation.element);
770
771 pixelHLSL += " output." + outVariableName + " = " + staticVariableName + ";\n";
772 }
773 }
774
775 pixelHLSL += "\n"
776 " return output;\n"
777 "}\n";
778
779 return true;
780}
781
782void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
783{
784 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
785
786 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
787 {
788 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
789 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
790
791 if (outputVariable.arraySize > 0)
792 {
793 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
794 {
795 const int location = baseLocation + elementIndex;
796 ASSERT(programOutputVars->count(location) == 0);
797 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
798 }
799 }
800 else
801 {
802 ASSERT(programOutputVars->count(baseLocation) == 0);
803 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
804 }
805 }
806}
807
808std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
809{
810 // for now we only handle point sprite emulation
811 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
812 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
813}
814
815std::string DynamicHLSL::generatePointSpriteHLSL(int registers, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
816{
817 ASSERT(registers >= 0);
818 ASSERT(vertexShader->mUsesPointSize);
819 ASSERT(mRenderer->getMajorShaderModel() >= 4);
820
821 std::string geomHLSL;
822
823 std::string varyingSemantic = "TEXCOORD";
824
825 std::string fragCoordSemantic;
826 std::string pointCoordSemantic;
827
828 int reservedRegisterIndex = registers;
829
830 if (fragmentShader->mUsesFragCoord)
831 {
832 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
833 }
834
835 if (fragmentShader->mUsesPointCoord)
836 {
837 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
838 }
839
840 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
841 "\n"
842 "struct GS_INPUT\n"
843 "{\n";
844
845 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
846
847 geomHLSL += varyingHLSL;
848
849 if (fragmentShader->mUsesFragCoord)
850 {
851 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
852 }
853
854 geomHLSL += " float gl_PointSize : PSIZE;\n"
855 " float4 gl_Position : SV_Position;\n"
856 "};\n"
857 "\n"
858 "struct GS_OUTPUT\n"
859 "{\n";
860
861 geomHLSL += varyingHLSL;
862
863 if (fragmentShader->mUsesFragCoord)
864 {
865 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
866 }
867
868 if (fragmentShader->mUsesPointCoord)
869 {
870 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
871 }
872
873 geomHLSL += " float gl_PointSize : PSIZE;\n"
874 " float4 gl_Position : SV_Position;\n"
875 "};\n"
876 "\n"
877 "static float2 pointSpriteCorners[] = \n"
878 "{\n"
879 " float2( 0.5f, -0.5f),\n"
880 " float2( 0.5f, 0.5f),\n"
881 " float2(-0.5f, -0.5f),\n"
882 " float2(-0.5f, 0.5f)\n"
883 "};\n"
884 "\n"
885 "static float2 pointSpriteTexcoords[] = \n"
886 "{\n"
887 " float2(1.0f, 1.0f),\n"
888 " float2(1.0f, 0.0f),\n"
889 " float2(0.0f, 1.0f),\n"
890 " float2(0.0f, 0.0f)\n"
891 "};\n"
892 "\n"
893 "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
894 "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n"
895 "\n"
896 "[maxvertexcount(4)]\n"
897 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
898 "{\n"
899 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
900 " output.gl_PointSize = input[0].gl_PointSize;\n";
901
902 for (int r = 0; r < registers; r++)
903 {
904 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
905 }
906
907 if (fragmentShader->mUsesFragCoord)
908 {
909 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
910 }
911
912 geomHLSL += " \n"
913 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
914 " float4 gl_Position = input[0].gl_Position;\n"
915 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
916
917 for (int corner = 0; corner < 4; corner++)
918 {
919 geomHLSL += " \n"
920 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
921
922 if (fragmentShader->mUsesPointCoord)
923 {
924 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
925 }
926
927 geomHLSL += " outStream.Append(output);\n";
928 }
929
930 geomHLSL += " \n"
931 " outStream.RestartStrip();\n"
932 "}\n";
933
934 return geomHLSL;
935}
936
937// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -0400938std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -0500939{
940 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
941 {
942 return "_" + name;
943 }
944
945 return name;
946}
947
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500948std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
949{
Jamie Madilla53ab512014-03-17 09:47:44 -0400950 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500951
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500952 // Matrix
953 if (IsMatrixType(shaderAttrib.type))
954 {
Jamie Madill8664b062014-02-14 16:41:29 -0500955 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500956 }
957
Jamie Madill8664b062014-02-14 16:41:29 -0500958 GLenum shaderComponentType = UniformComponentType(shaderAttrib.type);
959 int shaderComponentCount = UniformComponentCount(shaderAttrib.type);
960
961 std::string padString = "";
962
963 // Perform integer to float conversion (if necessary)
964 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
965
966 // TODO: normalization for 32-bit integer formats
967 ASSERT(!requiresTypeConversion || !vertexFormat.mNormalized);
968
969 if (requiresTypeConversion || !padString.empty())
970 {
971 return "float" + Str(shaderComponentCount) + "(" + attribString + padString + ")";
972 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500973
974 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -0500975 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500976}
977
Jamie Madill5f562732014-02-14 16:41:24 -0500978}