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