blob: b33c6aa7dfb0cf790e01a00f94668ce4aab092d6 [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"
17
18#include "compiler/translator/HLSLLayoutEncoder.h"
19
20namespace gl_d3d
21{
22
23std::string TypeString(GLenum type)
24{
25 switch (type)
26 {
27 case GL_FLOAT: return "float";
28 case GL_FLOAT_VEC2: return "float2";
29 case GL_FLOAT_VEC3: return "float3";
30 case GL_FLOAT_VEC4: return "float4";
31 case GL_INT: return "int";
32 case GL_INT_VEC2: return "int2";
33 case GL_INT_VEC3: return "int3";
34 case GL_INT_VEC4: return "int4";
35 case GL_UNSIGNED_INT: return "uint";
36 case GL_UNSIGNED_INT_VEC2: return "uint2";
37 case GL_UNSIGNED_INT_VEC3: return "uint3";
38 case GL_UNSIGNED_INT_VEC4: return "uint4";
39 case GL_FLOAT_MAT2: return "float2x2";
40 case GL_FLOAT_MAT3: return "float3x3";
41 case GL_FLOAT_MAT4: return "float4x4";
42 case GL_FLOAT_MAT2x3: return "float2x3";
43 case GL_FLOAT_MAT3x2: return "float3x2";
44 case GL_FLOAT_MAT2x4: return "float2x4";
45 case GL_FLOAT_MAT4x2: return "float4x2";
46 case GL_FLOAT_MAT3x4: return "float3x4";
47 case GL_FLOAT_MAT4x3: return "float4x3";
48 default: UNREACHABLE(); return "invalid-gl-type";
49 }
50}
51
52}
53
54namespace gl
55{
56
57static std::string Str(int i)
58{
59 char buffer[20];
60 snprintf(buffer, sizeof(buffer), "%d", i);
61 return buffer;
62}
63
64static std::string ArrayString(int i)
65{
66 return "[" + Str(i) + "]";
67}
68
69std::string ArrayString(unsigned int i)
70{
71 return (i == GL_INVALID_INDEX ? "" : "[" + Str(i) + "]");
72}
73
74DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
75 : mRenderer(renderer)
76{
77}
78
79// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
80// Returns the number of used varying registers, or -1 if unsuccesful
81int DynamicHLSL::packVaryings(InfoLog &infoLog, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader)
82{
83 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
84
85 fragmentShader->resetVaryingsRegisterAssignment();
86
87 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
88 {
89 sh::Varying *varying = &fragmentShader->mVaryings[varyingIndex];
90 GLenum transposedType = TransposeMatrixType(varying->type);
91
92 // matrices within varying structs are not transposed
93 int registers = (varying->isStruct() ? sh::HLSLVariableRegisterCount(*varying) : gl::VariableRowCount(transposedType)) * varying->elementCount();
94 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
95 bool success = false;
96
97 if (elements == 2 || elements == 3 || elements == 4)
98 {
99 for (int r = 0; r <= maxVaryingVectors - registers && !success; r++)
100 {
101 bool available = true;
102
103 for (int y = 0; y < registers && available; y++)
104 {
105 for (int x = 0; x < elements && available; x++)
106 {
107 if (packing[r + y][x])
108 {
109 available = false;
110 }
111 }
112 }
113
114 if (available)
115 {
116 varying->registerIndex = r;
117 varying->elementIndex = 0;
118
119 for (int y = 0; y < registers; y++)
120 {
121 for (int x = 0; x < elements; x++)
122 {
123 packing[r + y][x] = &*varying;
124 }
125 }
126
127 success = true;
128 }
129 }
130
131 if (!success && elements == 2)
132 {
133 for (int r = maxVaryingVectors - registers; r >= 0 && !success; r--)
134 {
135 bool available = true;
136
137 for (int y = 0; y < registers && available; y++)
138 {
139 for (int x = 2; x < 4 && available; x++)
140 {
141 if (packing[r + y][x])
142 {
143 available = false;
144 }
145 }
146 }
147
148 if (available)
149 {
150 varying->registerIndex = r;
151 varying->elementIndex = 2;
152
153 for (int y = 0; y < registers; y++)
154 {
155 for (int x = 2; x < 4; x++)
156 {
157 packing[r + y][x] = &*varying;
158 }
159 }
160
161 success = true;
162 }
163 }
164 }
165 }
166 else if (elements == 1)
167 {
168 int space[4] = {0};
169
170 for (int y = 0; y < maxVaryingVectors; y++)
171 {
172 for (int x = 0; x < 4; x++)
173 {
174 space[x] += packing[y][x] ? 0 : 1;
175 }
176 }
177
178 int column = 0;
179
180 for (int x = 0; x < 4; x++)
181 {
182 if (space[x] >= registers && space[x] < space[column])
183 {
184 column = x;
185 }
186 }
187
188 if (space[column] >= registers)
189 {
190 for (int r = 0; r < maxVaryingVectors; r++)
191 {
192 if (!packing[r][column])
193 {
194 varying->registerIndex = r;
195
196 for (int y = r; y < r + registers; y++)
197 {
198 packing[y][column] = &*varying;
199 }
200
201 break;
202 }
203 }
204
205 varying->elementIndex = column;
206
207 success = true;
208 }
209 }
210 else UNREACHABLE();
211
212 if (!success)
213 {
214 infoLog.append("Could not pack varying %s", varying->name.c_str());
215
216 return -1;
217 }
218 }
219
220 // Return the number of used registers
221 int registers = 0;
222
223 for (int r = 0; r < maxVaryingVectors; r++)
224 {
225 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
226 {
227 registers++;
228 }
229 }
230
231 return registers;
232}
233
234std::string DynamicHLSL::generateVaryingHLSL(FragmentShader *fragmentShader, const std::string &varyingSemantic) const
235{
236 std::string varyingHLSL;
237
238 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
239 {
240 sh::Varying *varying = &fragmentShader->mVaryings[varyingIndex];
241 if (varying->registerAssigned())
242 {
243 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
244 {
245 GLenum transposedType = TransposeMatrixType(varying->type);
246 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
247 for (int row = 0; row < variableRows; row++)
248 {
249 switch (varying->interpolation)
250 {
251 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
252 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
253 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
254 default: UNREACHABLE();
255 }
256
257 std::string n = Str(varying->registerIndex + elementIndex * variableRows + row);
258
259 // matrices within structs are not transposed, hence we do not use the special struct prefix "rm"
260 std::string typeString = varying->isStruct() ? "_" + varying->structName :
261 gl_d3d::TypeString(UniformComponentType(transposedType)) + Str(VariableColumnCount(transposedType));
262
263 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
264 }
265 }
266 }
267 else UNREACHABLE();
268 }
269
270 return varyingHLSL;
271}
272
273bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const sh::ShaderVariable *packing[][4],
274 std::string& pixelHLSL, std::string& vertexHLSL,
275 FragmentShader *fragmentShader, VertexShader *vertexShader,
276 std::map<int, VariableLocation> *programOutputVars) const
277{
278 if (pixelHLSL.empty() || vertexHLSL.empty())
279 {
280 return false;
281 }
282
283 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
284 bool usesFragColor = fragmentShader->mUsesFragColor;
285 bool usesFragData = fragmentShader->mUsesFragData;
286 if (usesFragColor && usesFragData)
287 {
288 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
289 return false;
290 }
291
292 // Write the HLSL input/output declarations
293 const int shaderModel = mRenderer->getMajorShaderModel();
294 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
295
296 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
297
298 // Two cases when writing to gl_FragColor and using ESSL 1.0:
299 // - with a 3.0 context, the output color is copied to channel 0
300 // - with a 2.0 context, the output color is broadcast to all channels
301 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
302 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
303
304 int shaderVersion = vertexShader->getShaderVersion();
305
306 if (registersNeeded > maxVaryingVectors)
307 {
308 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
309
310 return false;
311 }
312
313 std::string varyingSemantic = (vertexShader->mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
314 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
315 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
316 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
317
318 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
319
320 // special varyings that use reserved registers
321 int reservedRegisterIndex = registers;
322 std::string fragCoordSemantic;
323 std::string pointCoordSemantic;
324
325 if (fragmentShader->mUsesFragCoord)
326 {
327 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
328 }
329
330 if (fragmentShader->mUsesPointCoord)
331 {
332 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
333 // In DX11 we compute this in the GS.
334 if (shaderModel == 3)
335 {
336 pointCoordSemantic = "TEXCOORD0";
337 }
338 else if (shaderModel >= 4)
339 {
340 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
341 }
342 }
343
344 vertexHLSL += "struct VS_INPUT\n"
345 "{\n";
346
347 int semanticIndex = 0;
348 const std::vector<sh::Attribute> &activeAttributes = vertexShader->mActiveAttributes;
349 for (unsigned int attributeIndex = 0; attributeIndex < activeAttributes.size(); attributeIndex++)
350 {
351 const sh::Attribute &attribute = activeAttributes[attributeIndex];
352 vertexHLSL += " " + gl_d3d::TypeString(TransposeMatrixType(attribute.type)) + " ";
353 vertexHLSL += decorateAttribute(attribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
354
355 semanticIndex += AttributeRegisterCount(attribute.type);
356 }
357
358 vertexHLSL += "};\n"
359 "\n"
360 "struct VS_OUTPUT\n"
361 "{\n";
362
363 if (shaderModel < 4)
364 {
365 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
366 }
367
368 vertexHLSL += varyingHLSL;
369
370 if (fragmentShader->mUsesFragCoord)
371 {
372 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
373 }
374
375 if (vertexShader->mUsesPointSize && shaderModel >= 3)
376 {
377 vertexHLSL += " float gl_PointSize : PSIZE;\n";
378 }
379
380 if (shaderModel >= 4)
381 {
382 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
383 }
384
385 vertexHLSL += "};\n"
386 "\n"
387 "VS_OUTPUT main(VS_INPUT input)\n"
388 "{\n";
389
390 for (unsigned int attributeIndex = 0; attributeIndex < activeAttributes.size(); attributeIndex++)
391 {
392 const sh::ShaderVariable &attribute = activeAttributes[attributeIndex];
393 vertexHLSL += " " + decorateAttribute(attribute.name) + " = ";
394
395 if (IsMatrixType(attribute.type)) // Matrix
396 {
397 vertexHLSL += "transpose";
398 }
399
400 vertexHLSL += "(input." + decorateAttribute(attribute.name) + ");\n";
401 }
402
403 if (shaderModel >= 4)
404 {
405 vertexHLSL += "\n"
406 " gl_main();\n"
407 "\n"
408 " VS_OUTPUT output;\n"
409 " output.gl_Position.x = gl_Position.x;\n"
410 " output.gl_Position.y = -gl_Position.y;\n"
411 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
412 " output.gl_Position.w = gl_Position.w;\n";
413 }
414 else
415 {
416 vertexHLSL += "\n"
417 " gl_main();\n"
418 "\n"
419 " VS_OUTPUT output;\n"
420 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
421 " output.gl_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
422 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
423 " output.gl_Position.w = gl_Position.w;\n";
424 }
425
426 if (vertexShader->mUsesPointSize && shaderModel >= 3)
427 {
428 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
429 }
430
431 if (fragmentShader->mUsesFragCoord)
432 {
433 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
434 }
435
436 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
437 {
438 sh::Varying *varying = &vertexShader->mVaryings[vertVaryingIndex];
439 if (varying->registerAssigned())
440 {
441 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
442 {
443 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying->type)));
444
445 for (int row = 0; row < variableRows; row++)
446 {
447 int r = varying->registerIndex + elementIndex * variableRows + row;
448 vertexHLSL += " output.v" + Str(r);
449
450 bool sharedRegister = false; // Register used by multiple varyings
451
452 for (int x = 0; x < 4; x++)
453 {
454 if (packing[r][x] && packing[r][x] != packing[r][0])
455 {
456 sharedRegister = true;
457 break;
458 }
459 }
460
461 if(sharedRegister)
462 {
463 vertexHLSL += ".";
464
465 for (int x = 0; x < 4; x++)
466 {
467 if (packing[r][x] == &*varying)
468 {
469 switch(x)
470 {
471 case 0: vertexHLSL += "x"; break;
472 case 1: vertexHLSL += "y"; break;
473 case 2: vertexHLSL += "z"; break;
474 case 3: vertexHLSL += "w"; break;
475 }
476 }
477 }
478 }
479
480 vertexHLSL += " = _" + varying->name;
481
482 if (varying->isArray())
483 {
484 vertexHLSL += ArrayString(elementIndex);
485 }
486
487 if (variableRows > 1)
488 {
489 vertexHLSL += ArrayString(row);
490 }
491
492 vertexHLSL += ";\n";
493 }
494 }
495 }
496 }
497
498 vertexHLSL += "\n"
499 " return output;\n"
500 "}\n";
501
502 pixelHLSL += "struct PS_INPUT\n"
503 "{\n";
504
505 pixelHLSL += varyingHLSL;
506
507 if (fragmentShader->mUsesFragCoord)
508 {
509 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
510 }
511
512 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
513 {
514 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
515 }
516
517 // Must consume the PSIZE element if the geometry shader is not active
518 // We won't know if we use a GS until we draw
519 if (vertexShader->mUsesPointSize && shaderModel >= 4)
520 {
521 pixelHLSL += " float gl_PointSize : PSIZE;\n";
522 }
523
524 if (fragmentShader->mUsesFragCoord)
525 {
526 if (shaderModel >= 4)
527 {
528 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
529 }
530 else if (shaderModel >= 3)
531 {
532 pixelHLSL += " float2 dx_VPos : VPOS;\n";
533 }
534 }
535
536 pixelHLSL += "};\n"
537 "\n"
538 "struct PS_OUTPUT\n"
539 "{\n";
540
541 if (shaderVersion < 300)
542 {
543 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
544 {
545 pixelHLSL += " float4 gl_Color" + Str(renderTargetIndex) + " : " + targetSemantic + Str(renderTargetIndex) + ";\n";
546 }
547
548 if (fragmentShader->mUsesFragDepth)
549 {
550 pixelHLSL += " float gl_Depth : " + depthSemantic + ";\n";
551 }
552 }
553 else
554 {
555 defineOutputVariables(fragmentShader, programOutputVars);
556
557 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
558 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
559 {
560 const VariableLocation &outputLocation = locationIt->second;
561 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
562 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
563
564 pixelHLSL += " " + gl_d3d::TypeString(outputVariable.type) +
565 " out_" + outputLocation.name + elementString +
566 " : " + targetSemantic + Str(locationIt->first) + ";\n";
567 }
568 }
569
570 pixelHLSL += "};\n"
571 "\n";
572
573 if (fragmentShader->mUsesFrontFacing)
574 {
575 if (shaderModel >= 4)
576 {
577 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
578 "{\n";
579 }
580 else
581 {
582 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
583 "{\n";
584 }
585 }
586 else
587 {
588 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
589 "{\n";
590 }
591
592 if (fragmentShader->mUsesFragCoord)
593 {
594 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
595
596 if (shaderModel >= 4)
597 {
598 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
599 " gl_FragCoord.y = input.dx_VPos.y;\n";
600 }
601 else if (shaderModel >= 3)
602 {
603 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
604 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
605 }
606 else
607 {
608 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
609 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
610 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
611 }
612
613 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
614 " gl_FragCoord.w = rhw;\n";
615 }
616
617 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
618 {
619 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
620 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
621 }
622
623 if (fragmentShader->mUsesFrontFacing)
624 {
625 if (shaderModel <= 3)
626 {
627 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
628 }
629 else
630 {
631 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
632 }
633 }
634
635 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
636 {
637 sh::Varying *varying = &fragmentShader->mVaryings[varyingIndex];
638 if (varying->registerAssigned())
639 {
640 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
641 {
642 GLenum transposedType = TransposeMatrixType(varying->type);
643 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
644 for (int row = 0; row < variableRows; row++)
645 {
646 std::string n = Str(varying->registerIndex + elementIndex * variableRows + row);
647 pixelHLSL += " _" + varying->name;
648
649 if (varying->isArray())
650 {
651 pixelHLSL += ArrayString(elementIndex);
652 }
653
654 if (variableRows > 1)
655 {
656 pixelHLSL += ArrayString(row);
657 }
658
659 if (varying->isStruct())
660 {
661 pixelHLSL += " = input.v" + n + ";\n"; break;
662 }
663 else
664 {
665 switch (VariableColumnCount(transposedType))
666 {
667 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
668 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
669 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
670 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
671 default: UNREACHABLE();
672 }
673 }
674 }
675 }
676 }
677 else UNREACHABLE();
678 }
679
680 pixelHLSL += "\n"
681 " gl_main();\n"
682 "\n"
683 " PS_OUTPUT output;\n";
684
685 if (shaderVersion < 300)
686 {
687 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
688 {
689 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
690
691 pixelHLSL += " output.gl_Color" + Str(renderTargetIndex) + " = gl_Color[" + Str(sourceColorIndex) + "];\n";
692 }
693
694 if (fragmentShader->mUsesFragDepth)
695 {
696 pixelHLSL += " output.gl_Depth = gl_Depth;\n";
697 }
698 }
699 else
700 {
701 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
702 {
703 const VariableLocation &outputLocation = locationIt->second;
704 const std::string &variableName = "out_" + outputLocation.name;
705 const std::string &outVariableName = variableName + (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
706 const std::string &staticVariableName = variableName + ArrayString(outputLocation.element);
707
708 pixelHLSL += " output." + outVariableName + " = " + staticVariableName + ";\n";
709 }
710 }
711
712 pixelHLSL += "\n"
713 " return output;\n"
714 "}\n";
715
716 return true;
717}
718
719void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
720{
721 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
722
723 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
724 {
725 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
726 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
727
728 if (outputVariable.arraySize > 0)
729 {
730 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
731 {
732 const int location = baseLocation + elementIndex;
733 ASSERT(programOutputVars->count(location) == 0);
734 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
735 }
736 }
737 else
738 {
739 ASSERT(programOutputVars->count(baseLocation) == 0);
740 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
741 }
742 }
743}
744
745std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
746{
747 // for now we only handle point sprite emulation
748 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
749 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
750}
751
752std::string DynamicHLSL::generatePointSpriteHLSL(int registers, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
753{
754 ASSERT(registers >= 0);
755 ASSERT(vertexShader->mUsesPointSize);
756 ASSERT(mRenderer->getMajorShaderModel() >= 4);
757
758 std::string geomHLSL;
759
760 std::string varyingSemantic = "TEXCOORD";
761
762 std::string fragCoordSemantic;
763 std::string pointCoordSemantic;
764
765 int reservedRegisterIndex = registers;
766
767 if (fragmentShader->mUsesFragCoord)
768 {
769 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
770 }
771
772 if (fragmentShader->mUsesPointCoord)
773 {
774 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
775 }
776
777 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
778 "\n"
779 "struct GS_INPUT\n"
780 "{\n";
781
782 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
783
784 geomHLSL += varyingHLSL;
785
786 if (fragmentShader->mUsesFragCoord)
787 {
788 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
789 }
790
791 geomHLSL += " float gl_PointSize : PSIZE;\n"
792 " float4 gl_Position : SV_Position;\n"
793 "};\n"
794 "\n"
795 "struct GS_OUTPUT\n"
796 "{\n";
797
798 geomHLSL += varyingHLSL;
799
800 if (fragmentShader->mUsesFragCoord)
801 {
802 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
803 }
804
805 if (fragmentShader->mUsesPointCoord)
806 {
807 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
808 }
809
810 geomHLSL += " float gl_PointSize : PSIZE;\n"
811 " float4 gl_Position : SV_Position;\n"
812 "};\n"
813 "\n"
814 "static float2 pointSpriteCorners[] = \n"
815 "{\n"
816 " float2( 0.5f, -0.5f),\n"
817 " float2( 0.5f, 0.5f),\n"
818 " float2(-0.5f, -0.5f),\n"
819 " float2(-0.5f, 0.5f)\n"
820 "};\n"
821 "\n"
822 "static float2 pointSpriteTexcoords[] = \n"
823 "{\n"
824 " float2(1.0f, 1.0f),\n"
825 " float2(1.0f, 0.0f),\n"
826 " float2(0.0f, 1.0f),\n"
827 " float2(0.0f, 0.0f)\n"
828 "};\n"
829 "\n"
830 "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
831 "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n"
832 "\n"
833 "[maxvertexcount(4)]\n"
834 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
835 "{\n"
836 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
837 " output.gl_PointSize = input[0].gl_PointSize;\n";
838
839 for (int r = 0; r < registers; r++)
840 {
841 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
842 }
843
844 if (fragmentShader->mUsesFragCoord)
845 {
846 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
847 }
848
849 geomHLSL += " \n"
850 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
851 " float4 gl_Position = input[0].gl_Position;\n"
852 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
853
854 for (int corner = 0; corner < 4; corner++)
855 {
856 geomHLSL += " \n"
857 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
858
859 if (fragmentShader->mUsesPointCoord)
860 {
861 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
862 }
863
864 geomHLSL += " outStream.Append(output);\n";
865 }
866
867 geomHLSL += " \n"
868 " outStream.RestartStrip();\n"
869 "}\n";
870
871 return geomHLSL;
872}
873
874// This method needs to match OutputHLSL::decorate
875std::string DynamicHLSL::decorateAttribute(const std::string &name)
876{
877 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
878 {
879 return "_" + name;
880 }
881
882 return name;
883}
884
885}