blob: 885936d917608c4918f0f75d37aafbe54c82b1ca [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
273 // matrices within structs are not transposed, hence we do not use the special struct prefix "rm"
Jamie Madill8664b062014-02-14 16:41:29 -0500274 GLenum componentType = gl::UniformComponentType(transposedType);
275 int columnCount = gl::VariableColumnCount(transposedType);
276 std::string componentTypeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
277 std::string typeString = (varying->isStruct() ? "_" + varying->structName : componentTypeString);
Jamie Madill5f562732014-02-14 16:41:24 -0500278 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
279 }
280 }
281 }
282 else UNREACHABLE();
283 }
284
285 return varyingHLSL;
286}
287
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500288std::string DynamicHLSL::generateInputLayoutHLSL(const VertexFormat inputLayout[], const sh::Attribute shaderAttributes[]) const
289{
290 std::string vertexHLSL;
291
292 vertexHLSL += "struct VS_INPUT\n"
293 "{\n";
294
295 int semanticIndex = 0;
296 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
297 {
Jamie Madill8664b062014-02-14 16:41:29 -0500298 const VertexFormat &vertexFormat = inputLayout[attributeIndex];
299 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500300
Jamie Madill8664b062014-02-14 16:41:29 -0500301 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500302 {
Jamie Madill8664b062014-02-14 16:41:29 -0500303 if (IsMatrixType(shaderAttribute.type))
304 {
305 // Matrix types are always transposed
306 vertexHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
307 }
308 else
309 {
310 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
311 vertexHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, UniformComponentCount(shaderAttribute.type));
312 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500313
Jamie Madill8664b062014-02-14 16:41:29 -0500314 vertexHLSL += " " + decorateAttribute(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
315 semanticIndex += AttributeRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500316 }
317 }
318
319 vertexHLSL += "};\n"
320 "\n"
321 "void initAttributes(VS_INPUT input)\n"
322 "{\n";
323
324 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
325 {
Jamie Madill8664b062014-02-14 16:41:29 -0500326 const VertexFormat &vertexFormat = inputLayout[attributeIndex];
327 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500328
Jamie Madill8664b062014-02-14 16:41:29 -0500329 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500330 {
Jamie Madill8664b062014-02-14 16:41:29 -0500331 vertexHLSL += " " + decorateAttribute(shaderAttribute.name) + " = ";
332
333 // Mismatched vertex attribute to vertex input may result in an undefined
334 // data reinterpretation (eg for pure integer->float, float->pure integer)
335 // TODO: issue warning with gl debug info extension, when supported
336 if ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
337 {
338 vertexHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
339 }
340 else
341 {
342 vertexHLSL += "input." + decorateAttribute(shaderAttribute.name);
343 }
344
345 vertexHLSL += ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500346 }
347 }
348
349 vertexHLSL += "}\n";
350
351 return vertexHLSL;
352}
353
Jamie Madill5f562732014-02-14 16:41:24 -0500354bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const sh::ShaderVariable *packing[][4],
355 std::string& pixelHLSL, std::string& vertexHLSL,
356 FragmentShader *fragmentShader, VertexShader *vertexShader,
357 std::map<int, VariableLocation> *programOutputVars) const
358{
359 if (pixelHLSL.empty() || vertexHLSL.empty())
360 {
361 return false;
362 }
363
364 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
365 bool usesFragColor = fragmentShader->mUsesFragColor;
366 bool usesFragData = fragmentShader->mUsesFragData;
367 if (usesFragColor && usesFragData)
368 {
369 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
370 return false;
371 }
372
373 // Write the HLSL input/output declarations
374 const int shaderModel = mRenderer->getMajorShaderModel();
375 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
376
377 const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0);
378
379 // Two cases when writing to gl_FragColor and using ESSL 1.0:
380 // - with a 3.0 context, the output color is copied to channel 0
381 // - with a 2.0 context, the output color is broadcast to all channels
382 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
383 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1);
384
385 int shaderVersion = vertexShader->getShaderVersion();
386
387 if (registersNeeded > maxVaryingVectors)
388 {
389 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
390
391 return false;
392 }
393
394 std::string varyingSemantic = (vertexShader->mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD";
395 std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR";
396 std::string positionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION";
397 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
398
399 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
400
401 // special varyings that use reserved registers
402 int reservedRegisterIndex = registers;
403 std::string fragCoordSemantic;
404 std::string pointCoordSemantic;
405
406 if (fragmentShader->mUsesFragCoord)
407 {
408 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
409 }
410
411 if (fragmentShader->mUsesPointCoord)
412 {
413 // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords.
414 // In DX11 we compute this in the GS.
415 if (shaderModel == 3)
416 {
417 pointCoordSemantic = "TEXCOORD0";
418 }
419 else if (shaderModel >= 4)
420 {
421 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
422 }
423 }
424
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500425 // Add stub string to be replaced when shader is dynamically defined by its layout
426 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500427
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500428 vertexHLSL += "struct VS_OUTPUT\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500429 "{\n";
430
431 if (shaderModel < 4)
432 {
433 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
434 }
435
436 vertexHLSL += varyingHLSL;
437
438 if (fragmentShader->mUsesFragCoord)
439 {
440 vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
441 }
442
443 if (vertexShader->mUsesPointSize && shaderModel >= 3)
444 {
445 vertexHLSL += " float gl_PointSize : PSIZE;\n";
446 }
447
448 if (shaderModel >= 4)
449 {
450 vertexHLSL += " float4 gl_Position : " + positionSemantic + ";\n";
451 }
452
453 vertexHLSL += "};\n"
454 "\n"
455 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500456 "{\n"
457 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500458
459 if (shaderModel >= 4)
460 {
461 vertexHLSL += "\n"
462 " gl_main();\n"
463 "\n"
464 " VS_OUTPUT output;\n"
465 " output.gl_Position.x = gl_Position.x;\n"
466 " output.gl_Position.y = -gl_Position.y;\n"
467 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
468 " output.gl_Position.w = gl_Position.w;\n";
469 }
470 else
471 {
472 vertexHLSL += "\n"
473 " gl_main();\n"
474 "\n"
475 " VS_OUTPUT output;\n"
476 " output.gl_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
477 " output.gl_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
478 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
479 " output.gl_Position.w = gl_Position.w;\n";
480 }
481
482 if (vertexShader->mUsesPointSize && shaderModel >= 3)
483 {
484 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
485 }
486
487 if (fragmentShader->mUsesFragCoord)
488 {
489 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
490 }
491
492 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
493 {
494 sh::Varying *varying = &vertexShader->mVaryings[vertVaryingIndex];
495 if (varying->registerAssigned())
496 {
497 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
498 {
499 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying->type)));
500
501 for (int row = 0; row < variableRows; row++)
502 {
503 int r = varying->registerIndex + elementIndex * variableRows + row;
504 vertexHLSL += " output.v" + Str(r);
505
506 bool sharedRegister = false; // Register used by multiple varyings
507
508 for (int x = 0; x < 4; x++)
509 {
510 if (packing[r][x] && packing[r][x] != packing[r][0])
511 {
512 sharedRegister = true;
513 break;
514 }
515 }
516
517 if(sharedRegister)
518 {
519 vertexHLSL += ".";
520
521 for (int x = 0; x < 4; x++)
522 {
523 if (packing[r][x] == &*varying)
524 {
525 switch(x)
526 {
527 case 0: vertexHLSL += "x"; break;
528 case 1: vertexHLSL += "y"; break;
529 case 2: vertexHLSL += "z"; break;
530 case 3: vertexHLSL += "w"; break;
531 }
532 }
533 }
534 }
535
536 vertexHLSL += " = _" + varying->name;
537
538 if (varying->isArray())
539 {
540 vertexHLSL += ArrayString(elementIndex);
541 }
542
543 if (variableRows > 1)
544 {
545 vertexHLSL += ArrayString(row);
546 }
547
548 vertexHLSL += ";\n";
549 }
550 }
551 }
552 }
553
554 vertexHLSL += "\n"
555 " return output;\n"
556 "}\n";
557
558 pixelHLSL += "struct PS_INPUT\n"
559 "{\n";
560
561 pixelHLSL += varyingHLSL;
562
563 if (fragmentShader->mUsesFragCoord)
564 {
565 pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
566 }
567
568 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
569 {
570 pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
571 }
572
573 // Must consume the PSIZE element if the geometry shader is not active
574 // We won't know if we use a GS until we draw
575 if (vertexShader->mUsesPointSize && shaderModel >= 4)
576 {
577 pixelHLSL += " float gl_PointSize : PSIZE;\n";
578 }
579
580 if (fragmentShader->mUsesFragCoord)
581 {
582 if (shaderModel >= 4)
583 {
584 pixelHLSL += " float4 dx_VPos : SV_Position;\n";
585 }
586 else if (shaderModel >= 3)
587 {
588 pixelHLSL += " float2 dx_VPos : VPOS;\n";
589 }
590 }
591
592 pixelHLSL += "};\n"
593 "\n"
594 "struct PS_OUTPUT\n"
595 "{\n";
596
597 if (shaderVersion < 300)
598 {
599 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
600 {
601 pixelHLSL += " float4 gl_Color" + Str(renderTargetIndex) + " : " + targetSemantic + Str(renderTargetIndex) + ";\n";
602 }
603
604 if (fragmentShader->mUsesFragDepth)
605 {
606 pixelHLSL += " float gl_Depth : " + depthSemantic + ";\n";
607 }
608 }
609 else
610 {
611 defineOutputVariables(fragmentShader, programOutputVars);
612
613 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
614 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
615 {
616 const VariableLocation &outputLocation = locationIt->second;
617 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
618 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
619
Jamie Madill8664b062014-02-14 16:41:29 -0500620 pixelHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) +
Jamie Madill5f562732014-02-14 16:41:24 -0500621 " out_" + outputLocation.name + elementString +
622 " : " + targetSemantic + Str(locationIt->first) + ";\n";
623 }
624 }
625
626 pixelHLSL += "};\n"
627 "\n";
628
629 if (fragmentShader->mUsesFrontFacing)
630 {
631 if (shaderModel >= 4)
632 {
633 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
634 "{\n";
635 }
636 else
637 {
638 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
639 "{\n";
640 }
641 }
642 else
643 {
644 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
645 "{\n";
646 }
647
648 if (fragmentShader->mUsesFragCoord)
649 {
650 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
651
652 if (shaderModel >= 4)
653 {
654 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n"
655 " gl_FragCoord.y = input.dx_VPos.y;\n";
656 }
657 else if (shaderModel >= 3)
658 {
659 pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n"
660 " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n";
661 }
662 else
663 {
664 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
665 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
666 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
667 }
668
669 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
670 " gl_FragCoord.w = rhw;\n";
671 }
672
673 if (fragmentShader->mUsesPointCoord && shaderModel >= 3)
674 {
675 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
676 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
677 }
678
679 if (fragmentShader->mUsesFrontFacing)
680 {
681 if (shaderModel <= 3)
682 {
683 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
684 }
685 else
686 {
687 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
688 }
689 }
690
691 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
692 {
693 sh::Varying *varying = &fragmentShader->mVaryings[varyingIndex];
694 if (varying->registerAssigned())
695 {
696 for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
697 {
698 GLenum transposedType = TransposeMatrixType(varying->type);
699 int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
700 for (int row = 0; row < variableRows; row++)
701 {
702 std::string n = Str(varying->registerIndex + elementIndex * variableRows + row);
703 pixelHLSL += " _" + varying->name;
704
705 if (varying->isArray())
706 {
707 pixelHLSL += ArrayString(elementIndex);
708 }
709
710 if (variableRows > 1)
711 {
712 pixelHLSL += ArrayString(row);
713 }
714
715 if (varying->isStruct())
716 {
717 pixelHLSL += " = input.v" + n + ";\n"; break;
718 }
719 else
720 {
721 switch (VariableColumnCount(transposedType))
722 {
723 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
724 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
725 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
726 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
727 default: UNREACHABLE();
728 }
729 }
730 }
731 }
732 }
733 else UNREACHABLE();
734 }
735
736 pixelHLSL += "\n"
737 " gl_main();\n"
738 "\n"
739 " PS_OUTPUT output;\n";
740
741 if (shaderVersion < 300)
742 {
743 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
744 {
745 unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex;
746
747 pixelHLSL += " output.gl_Color" + Str(renderTargetIndex) + " = gl_Color[" + Str(sourceColorIndex) + "];\n";
748 }
749
750 if (fragmentShader->mUsesFragDepth)
751 {
752 pixelHLSL += " output.gl_Depth = gl_Depth;\n";
753 }
754 }
755 else
756 {
757 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
758 {
759 const VariableLocation &outputLocation = locationIt->second;
760 const std::string &variableName = "out_" + outputLocation.name;
761 const std::string &outVariableName = variableName + (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
762 const std::string &staticVariableName = variableName + ArrayString(outputLocation.element);
763
764 pixelHLSL += " output." + outVariableName + " = " + staticVariableName + ";\n";
765 }
766 }
767
768 pixelHLSL += "\n"
769 " return output;\n"
770 "}\n";
771
772 return true;
773}
774
775void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
776{
777 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
778
779 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
780 {
781 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
782 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
783
784 if (outputVariable.arraySize > 0)
785 {
786 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
787 {
788 const int location = baseLocation + elementIndex;
789 ASSERT(programOutputVars->count(location) == 0);
790 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
791 }
792 }
793 else
794 {
795 ASSERT(programOutputVars->count(baseLocation) == 0);
796 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
797 }
798 }
799}
800
801std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
802{
803 // for now we only handle point sprite emulation
804 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
805 return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
806}
807
808std::string DynamicHLSL::generatePointSpriteHLSL(int registers, const sh::ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
809{
810 ASSERT(registers >= 0);
811 ASSERT(vertexShader->mUsesPointSize);
812 ASSERT(mRenderer->getMajorShaderModel() >= 4);
813
814 std::string geomHLSL;
815
816 std::string varyingSemantic = "TEXCOORD";
817
818 std::string fragCoordSemantic;
819 std::string pointCoordSemantic;
820
821 int reservedRegisterIndex = registers;
822
823 if (fragmentShader->mUsesFragCoord)
824 {
825 fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
826 }
827
828 if (fragmentShader->mUsesPointCoord)
829 {
830 pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++);
831 }
832
833 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
834 "\n"
835 "struct GS_INPUT\n"
836 "{\n";
837
838 std::string varyingHLSL = generateVaryingHLSL(fragmentShader, varyingSemantic);
839
840 geomHLSL += varyingHLSL;
841
842 if (fragmentShader->mUsesFragCoord)
843 {
844 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
845 }
846
847 geomHLSL += " float gl_PointSize : PSIZE;\n"
848 " float4 gl_Position : SV_Position;\n"
849 "};\n"
850 "\n"
851 "struct GS_OUTPUT\n"
852 "{\n";
853
854 geomHLSL += varyingHLSL;
855
856 if (fragmentShader->mUsesFragCoord)
857 {
858 geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n";
859 }
860
861 if (fragmentShader->mUsesPointCoord)
862 {
863 geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n";
864 }
865
866 geomHLSL += " float gl_PointSize : PSIZE;\n"
867 " float4 gl_Position : SV_Position;\n"
868 "};\n"
869 "\n"
870 "static float2 pointSpriteCorners[] = \n"
871 "{\n"
872 " float2( 0.5f, -0.5f),\n"
873 " float2( 0.5f, 0.5f),\n"
874 " float2(-0.5f, -0.5f),\n"
875 " float2(-0.5f, 0.5f)\n"
876 "};\n"
877 "\n"
878 "static float2 pointSpriteTexcoords[] = \n"
879 "{\n"
880 " float2(1.0f, 1.0f),\n"
881 " float2(1.0f, 0.0f),\n"
882 " float2(0.0f, 1.0f),\n"
883 " float2(0.0f, 0.0f)\n"
884 "};\n"
885 "\n"
886 "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n"
887 "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n"
888 "\n"
889 "[maxvertexcount(4)]\n"
890 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
891 "{\n"
892 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
893 " output.gl_PointSize = input[0].gl_PointSize;\n";
894
895 for (int r = 0; r < registers; r++)
896 {
897 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
898 }
899
900 if (fragmentShader->mUsesFragCoord)
901 {
902 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
903 }
904
905 geomHLSL += " \n"
906 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
907 " float4 gl_Position = input[0].gl_Position;\n"
908 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n";
909
910 for (int corner = 0; corner < 4; corner++)
911 {
912 geomHLSL += " \n"
913 " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
914
915 if (fragmentShader->mUsesPointCoord)
916 {
917 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
918 }
919
920 geomHLSL += " outStream.Append(output);\n";
921 }
922
923 geomHLSL += " \n"
924 " outStream.RestartStrip();\n"
925 "}\n";
926
927 return geomHLSL;
928}
929
930// This method needs to match OutputHLSL::decorate
931std::string DynamicHLSL::decorateAttribute(const std::string &name)
932{
933 if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0)
934 {
935 return "_" + name;
936 }
937
938 return name;
939}
940
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500941std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
942{
Jamie Madill8664b062014-02-14 16:41:29 -0500943 std::string attribString = "input." + decorateAttribute(shaderAttrib.name);
944
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500945 // Matrix
946 if (IsMatrixType(shaderAttrib.type))
947 {
Jamie Madill8664b062014-02-14 16:41:29 -0500948 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500949 }
950
Jamie Madill8664b062014-02-14 16:41:29 -0500951 GLenum shaderComponentType = UniformComponentType(shaderAttrib.type);
952 int shaderComponentCount = UniformComponentCount(shaderAttrib.type);
953
954 std::string padString = "";
955
956 // Perform integer to float conversion (if necessary)
957 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
958
959 // TODO: normalization for 32-bit integer formats
960 ASSERT(!requiresTypeConversion || !vertexFormat.mNormalized);
961
962 if (requiresTypeConversion || !padString.empty())
963 {
964 return "float" + Str(shaderComponentCount) + "(" + attribString + padString + ")";
965 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500966
967 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -0500968 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500969}
970
Jamie Madill5f562732014-02-14 16:41:24 -0500971}