blob: f09cf63786a55377cea5186fa85b6ade96f7ed4d [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"
Brandon Jonesf05cdee2014-08-27 15:24:07 -070014#include "libGLESv2/renderer/d3d/ShaderD3D.h"
Jamie Madill5f562732014-02-14 16:41:24 -050015#include "libGLESv2/renderer/Renderer.h"
16#include "common/utilities.h"
17#include "libGLESv2/ProgramBinary.h"
Jamie Madill8664b062014-02-14 16:41:29 -050018#include "libGLESv2/formatutils.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040019#include "common/blocklayout.h"
Jamie Madill5f562732014-02-14 16:41:24 -050020
Jamie Madill53cb14d2014-07-08 15:02:35 -040021// For use with ArrayString, see angleutils.h
22META_ASSERT(GL_INVALID_INDEX == UINT_MAX);
Jamie Madill5f562732014-02-14 16:41:24 -050023
Jamie Madill8664b062014-02-14 16:41:29 -050024namespace gl_d3d
Jamie Madill5f562732014-02-14 16:41:24 -050025{
Jamie Madill8664b062014-02-14 16:41:29 -050026
27std::string HLSLComponentTypeString(GLenum componentType)
28{
29 switch (componentType)
30 {
31 case GL_UNSIGNED_INT: return "uint";
32 case GL_INT: return "int";
33 case GL_UNSIGNED_NORMALIZED:
34 case GL_SIGNED_NORMALIZED:
35 case GL_FLOAT: return "float";
36 default: UNREACHABLE(); return "not-component-type";
37 }
Jamie Madill5f562732014-02-14 16:41:24 -050038}
39
Jamie Madill8664b062014-02-14 16:41:29 -050040std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
41{
42 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
43}
44
45std::string HLSLMatrixTypeString(GLenum type)
46{
47 switch (type)
48 {
49 case GL_FLOAT_MAT2: return "float2x2";
50 case GL_FLOAT_MAT3: return "float3x3";
51 case GL_FLOAT_MAT4: return "float4x4";
52 case GL_FLOAT_MAT2x3: return "float2x3";
53 case GL_FLOAT_MAT3x2: return "float3x2";
54 case GL_FLOAT_MAT2x4: return "float2x4";
55 case GL_FLOAT_MAT4x2: return "float4x2";
56 case GL_FLOAT_MAT3x4: return "float3x4";
57 case GL_FLOAT_MAT4x3: return "float4x3";
58 default: UNREACHABLE(); return "not-matrix-type";
59 }
60}
61
62std::string HLSLTypeString(GLenum type)
63{
64 if (gl::IsMatrixType(type))
65 {
66 return HLSLMatrixTypeString(type);
67 }
68
Jamie Madillf2575982014-06-25 16:04:54 -040069 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050070}
71
72}
73
74namespace gl
75{
76
Geoff Lang04fb89a2014-06-09 15:05:36 -040077const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
78const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050079
Jamie Madill5f562732014-02-14 16:41:24 -050080DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
81 : mRenderer(renderer)
82{
83}
84
Jamie Madillff0d2ba2014-05-14 13:49:10 -040085static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -050086{
Geoff Lang48dcae72014-02-05 16:28:24 -050087 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -050088
Geoff Lang48dcae72014-02-05 16:28:24 -050089 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -040090 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -050091 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -050092
Jamie Madillff0d2ba2014-05-14 13:49:10 -040093 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -050094 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -040095 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -050096 {
Geoff Lang48dcae72014-02-05 16:28:24 -050097 bool available = true;
98
99 for (int y = 0; y < registers && available; y++)
100 {
101 for (int x = 0; x < elements && available; x++)
102 {
103 if (packing[r + y][x])
104 {
105 available = false;
106 }
107 }
108 }
109
110 if (available)
111 {
112 varying->registerIndex = r;
Geoff Lang48dcae72014-02-05 16:28:24 -0500113
114 for (int y = 0; y < registers; y++)
115 {
116 for (int x = 0; x < elements; x++)
117 {
118 packing[r + y][x] = &*varying;
119 }
120 }
121
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400122 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500123 }
124 }
125
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400126 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500127 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400128 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500129 {
130 bool available = true;
131
132 for (int y = 0; y < registers && available; y++)
133 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500134 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500135 {
136 if (packing[r + y][x])
137 {
138 available = false;
139 }
140 }
141 }
142
143 if (available)
144 {
145 varying->registerIndex = r;
Jamie Madill5f562732014-02-14 16:41:24 -0500146
147 for (int y = 0; y < registers; y++)
148 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500149 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500150 {
151 packing[r + y][x] = &*varying;
152 }
153 }
154
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400155 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500156 }
157 }
Jamie Madill5f562732014-02-14 16:41:24 -0500158 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500159 }
160 else if (elements == 1)
161 {
162 int space[4] = { 0 };
163
164 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500165 {
Jamie Madill5f562732014-02-14 16:41:24 -0500166 for (int x = 0; x < 4; x++)
167 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500168 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500169 }
170 }
Jamie Madill5f562732014-02-14 16:41:24 -0500171
Geoff Lang48dcae72014-02-05 16:28:24 -0500172 int column = 0;
173
174 for (int x = 0; x < 4; x++)
175 {
176 if (space[x] >= registers && space[x] < space[column])
177 {
178 column = x;
179 }
180 }
181
182 if (space[column] >= registers)
183 {
184 for (int r = 0; r < maxVaryingVectors; r++)
185 {
186 if (!packing[r][column])
187 {
188 varying->registerIndex = r;
189
190 for (int y = r; y < r + registers; y++)
191 {
192 packing[y][column] = &*varying;
193 }
194
195 break;
196 }
197 }
198
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400199 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500200 }
201 }
202 else UNREACHABLE();
203
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400204 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500205}
206
207// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
208// Returns the number of used varying registers, or -1 if unsuccesful
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400209int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, FragmentShader *fragmentShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500210 VertexShader *vertexShader, const std::vector<std::string>& transformFeedbackVaryings)
211{
212 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
213
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700214 rx::VertexShaderD3D *vertexShaderD3D = rx::VertexShaderD3D::makeVertexShaderD3D(vertexShader->getImplementation());
215 rx::FragmentShaderD3D *fragmentShaderD3D = rx::FragmentShaderD3D::makeFragmentShaderD3D(fragmentShader->getImplementation());
216
217 vertexShaderD3D->resetVaryingsRegisterAssignment();
218 fragmentShaderD3D->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500219
220 std::set<std::string> packedVaryings;
221
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700222 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShaderD3D->mVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500223 {
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700224 PackedVarying *varying = &fragmentShaderD3D->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500225 if (packVarying(varying, maxVaryingVectors, packing))
226 {
227 packedVaryings.insert(varying->name);
228 }
229 else
Jamie Madill5f562732014-02-14 16:41:24 -0500230 {
231 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500232 return -1;
233 }
234 }
235
Geoff Lang48dcae72014-02-05 16:28:24 -0500236 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
237 {
238 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
239 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
240 {
241 bool found = false;
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700242 for (unsigned int varyingIndex = 0; varyingIndex < vertexShaderD3D->mVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500243 {
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700244 PackedVarying *varying = &vertexShaderD3D->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500245 if (transformFeedbackVarying == varying->name)
246 {
247 if (!packVarying(varying, maxVaryingVectors, packing))
248 {
249 infoLog.append("Could not pack varying %s", varying->name.c_str());
250 return -1;
251 }
252
253 found = true;
254 break;
255 }
256 }
257
258 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
259 {
260 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
261 return -1;
262 }
263 }
264 }
265
Jamie Madill5f562732014-02-14 16:41:24 -0500266 // Return the number of used registers
267 int registers = 0;
268
269 for (int r = 0; r < maxVaryingVectors; r++)
270 {
271 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
272 {
273 registers++;
274 }
275 }
276
277 return registers;
278}
279
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400280std::string DynamicHLSL::generateVaryingHLSL(VertexShader *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500281{
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700282 rx::VertexShaderD3D *shaderD3D = rx::VertexShaderD3D::makeVertexShaderD3D(shader->getImplementation());
283
284 std::string varyingSemantic = getVaryingSemantic(shaderD3D->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500285 std::string varyingHLSL;
286
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700287 for (unsigned int varyingIndex = 0; varyingIndex < shaderD3D->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500288 {
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700289 const PackedVarying &varying = shaderD3D->mVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400290 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500291 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400292 GLenum transposedType = TransposeMatrixType(varying.type);
293 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500294
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400295 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500296 {
Jamie Madill5f562732014-02-14 16:41:24 -0500297 for (int row = 0; row < variableRows; row++)
298 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400299 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500300 {
Jamie Madillf2575982014-06-25 16:04:54 -0400301 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
302 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
303 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500304 default: UNREACHABLE();
305 }
306
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400307 unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500308 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500309
Jamie Madilla53ab512014-03-17 09:47:44 -0400310 std::string typeString;
311
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400312 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400313 {
314 // matrices within structs are not transposed, so
315 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400316 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400317 }
318 else
319 {
Jamie Madillf2575982014-06-25 16:04:54 -0400320 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400321 int columnCount = VariableColumnCount(transposedType);
Jamie Madilla53ab512014-03-17 09:47:44 -0400322 typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
323 }
Jamie Madill5f562732014-02-14 16:41:24 -0500324 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
325 }
326 }
327 }
Jamie Madill5f562732014-02-14 16:41:24 -0500328 }
329
330 return varyingHLSL;
331}
332
Jamie Madillf2575982014-06-25 16:04:54 -0400333std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
334 const VertexFormat inputLayout[],
335 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500336{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400337 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500338
339 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400340 unsigned int inputIndex = 0;
341
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500342 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
343 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400344 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
345
346 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madillf2575982014-06-25 16:04:54 -0400347 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500348
Jamie Madill8664b062014-02-14 16:41:29 -0500349 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500350 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400351 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500352 if (IsMatrixType(shaderAttribute.type))
353 {
354 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400355 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500356 }
357 else
358 {
359 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madillf2575982014-06-25 16:04:54 -0400360 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500361 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500362
Jamie Madilla53ab512014-03-17 09:47:44 -0400363 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400364 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500365
Jamie Madill3b7e2052014-03-17 09:47:43 -0400366 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400367 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500368
369 // Mismatched vertex attribute to vertex input may result in an undefined
370 // data reinterpretation (eg for pure integer->float, float->pure integer)
371 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400372 if (IsMatrixType(shaderAttribute.type) ||
373 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500374 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400375 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500376 }
377 else
378 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400379 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500380 }
381
Jamie Madill3b7e2052014-03-17 09:47:43 -0400382 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400383
Jamie Madillac0a2672014-04-11 13:33:56 -0400384 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
385 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500386 }
387
Geoff Lang04fb89a2014-06-09 15:05:36 -0400388 std::string replacementHLSL = "struct VS_INPUT\n"
389 "{\n" +
390 structHLSL +
391 "};\n"
392 "\n"
393 "void initAttributes(VS_INPUT input)\n"
394 "{\n" +
395 initHLSL +
396 "}\n";
397
398 std::string vertexHLSL(sourceShader);
399
400 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
401 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
402
403 return vertexHLSL;
404}
405
406std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables,
407 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
408{
409 const int shaderModel = mRenderer->getMajorShaderModel();
410 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
411 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
412
413 std::string declarationHLSL;
414 std::string copyHLSL;
415 for (size_t i = 0; i < outputVariables.size(); i++)
416 {
417 const PixelShaderOuputVariable& outputVariable = outputVariables[i];
418 ASSERT(outputLayout.size() > outputVariable.outputIndex);
Geoff Lang4ace4232014-06-18 19:12:48 -0400419
420 // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers
421 bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE
422 if (outputIndexEnabled)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400423 {
424 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
425 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
426
427 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
428 }
429 }
430
431 if (usesFragDepth)
432 {
433 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
434 copyHLSL += " output.gl_Depth = gl_Depth; \n";
435 }
436
437 std::string replacementHLSL = "struct PS_OUTPUT\n"
438 "{\n" +
439 declarationHLSL +
440 "};\n"
441 "\n"
442 "PS_OUTPUT generateOutput()\n"
443 "{\n"
444 " PS_OUTPUT output;\n" +
445 copyHLSL +
446 " return output;\n"
447 "}\n";
448
449 std::string pixelHLSL(sourceShader);
450
451 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
452 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
453
454 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500455}
456
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400457std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
458{
459 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
460 // In D3D11 we manually compute gl_PointCoord in the GS.
461 int shaderModel = mRenderer->getMajorShaderModel();
462 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
463}
464
465struct DynamicHLSL::SemanticInfo
466{
467 struct BuiltinInfo
468 {
469 BuiltinInfo()
470 : enabled(false),
471 index(0),
472 systemValue(false)
473 {}
474
475 bool enabled;
476 std::string semantic;
477 unsigned int index;
478 bool systemValue;
479
480 std::string str() const
481 {
482 return (systemValue ? semantic : (semantic + Str(index)));
483 }
484
485 void enableSystem(const std::string &systemValueSemantic)
486 {
487 enabled = true;
488 semantic = systemValueSemantic;
489 systemValue = true;
490 }
491
492 void enable(const std::string &semanticVal, unsigned int indexVal)
493 {
494 enabled = true;
495 semantic = semanticVal;
496 index = indexVal;
497 }
498 };
499
500 BuiltinInfo dxPosition;
501 BuiltinInfo glPosition;
502 BuiltinInfo glFragCoord;
503 BuiltinInfo glPointCoord;
504 BuiltinInfo glPointSize;
505};
506
507DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
508 bool pointSize, bool pixelShader) const
509{
510 SemanticInfo info;
511 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
512 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
513
514 int reservedRegisterIndex = startRegisters;
515
516 if (hlsl4)
517 {
518 info.dxPosition.enableSystem("SV_Position");
519 }
520 else if (pixelShader)
521 {
522 info.dxPosition.enableSystem("VPOS");
523 }
524 else
525 {
526 info.dxPosition.enableSystem("POSITION");
527 }
528
529 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
530
531 if (fragCoord)
532 {
533 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
534 }
535
536 if (pointCoord)
537 {
538 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
539 // In D3D11 we manually compute gl_PointCoord in the GS.
540 if (hlsl4)
541 {
542 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
543 }
544 else
545 {
546 info.glPointCoord.enable("TEXCOORD", 0);
547 }
548 }
549
550 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
551 if (pointSize && (!pixelShader || hlsl4))
552 {
553 info.glPointSize.enableSystem("PSIZE");
554 }
555
556 return info;
557}
558
559std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
560{
561 std::string linkHLSL = "{\n";
562
563 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
564
565 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
566 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
567
568 if (info.glFragCoord.enabled)
569 {
570 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
571 }
572
573 if (info.glPointCoord.enabled)
574 {
575 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
576 }
577
578 linkHLSL += varyingHLSL;
579
580 if (info.glPointSize.enabled)
581 {
582 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
583 }
584
585 linkHLSL += "};\n";
586
587 return linkHLSL;
588}
589
590void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
591 std::vector<LinkedVarying> *linkedVaryings) const
592{
593 ASSERT(info.glPosition.enabled);
594
595 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
596 info.glPosition.index, 1));
597
598 if (info.glFragCoord.enabled)
599 {
600 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
601 info.glFragCoord.index, 1));
602 }
603
604 if (info.glPointSize.enabled)
605 {
606 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
607 }
608}
609
610void DynamicHLSL::storeUserLinkedVaryings(const VertexShader *vertexShader,
611 std::vector<LinkedVarying> *linkedVaryings) const
612{
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700613 const rx::VertexShaderD3D *vertexShaderD3D = rx::VertexShaderD3D::makeVertexShaderD3D(vertexShader->getImplementation());
614
615 const std::string &varyingSemantic = getVaryingSemantic(vertexShaderD3D->mUsesPointSize);
616 const std::vector<PackedVarying> &varyings = vertexShaderD3D->mVaryings;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400617
618 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
619 {
620 const PackedVarying &varying = varyings[varyingIndex];
621 if (varying.registerAssigned())
622 {
623 GLenum transposedType = TransposeMatrixType(varying.type);
624 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
625
626 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
627 varyingSemantic, varying.registerIndex,
628 variableRows * varying.elementCount()));
629 }
630 }
631}
632
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400633bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500634 std::string& pixelHLSL, std::string& vertexHLSL,
635 FragmentShader *fragmentShader, VertexShader *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500636 const std::vector<std::string>& transformFeedbackVaryings,
637 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400638 std::map<int, VariableLocation> *programOutputVars,
639 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
640 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500641{
642 if (pixelHLSL.empty() || vertexHLSL.empty())
643 {
644 return false;
645 }
646
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700647 rx::VertexShaderD3D *vertexShaderD3D = rx::VertexShaderD3D::makeVertexShaderD3D(vertexShader->getImplementation());
648 rx::FragmentShaderD3D *fragmentShaderD3D = rx::FragmentShaderD3D::makeFragmentShaderD3D(fragmentShader->getImplementation());
649
650 bool usesMRT = fragmentShaderD3D->mUsesMultipleRenderTargets;
651 bool usesFragColor = fragmentShaderD3D->mUsesFragColor;
652 bool usesFragData = fragmentShaderD3D->mUsesFragData;
653 bool usesFragCoord = fragmentShaderD3D->mUsesFragCoord;
654 bool usesPointCoord = fragmentShaderD3D->mUsesPointCoord;
655 bool usesPointSize = vertexShaderD3D->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400656
Jamie Madill5f562732014-02-14 16:41:24 -0500657 if (usesFragColor && usesFragData)
658 {
659 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
660 return false;
661 }
662
663 // Write the HLSL input/output declarations
664 const int shaderModel = mRenderer->getMajorShaderModel();
665 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
666
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400667 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500668
669 // Two cases when writing to gl_FragColor and using ESSL 1.0:
670 // - with a 3.0 context, the output color is copied to channel 0
671 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700672 const bool broadcast = (fragmentShaderD3D->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400673 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500674
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700675 int shaderVersion = vertexShaderD3D->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500676
677 if (registersNeeded > maxVaryingVectors)
678 {
679 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500680 return false;
681 }
682
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400683 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
684 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
685 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500686
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400687 storeUserLinkedVaryings(vertexShader, linkedVaryings);
688 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500689
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500690 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400691 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
692 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500693 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500694 "{\n"
695 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500696
697 if (shaderModel >= 4)
698 {
699 vertexHLSL += "\n"
700 " gl_main();\n"
701 "\n"
702 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500703 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400704 " output.dx_Position.x = gl_Position.x;\n"
705 " output.dx_Position.y = -gl_Position.y;\n"
706 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
707 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500708 }
709 else
710 {
711 vertexHLSL += "\n"
712 " gl_main();\n"
713 "\n"
714 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500715 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400716 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
717 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
718 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
719 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500720 }
721
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400722 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500723 {
724 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
725 }
726
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400727 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500728 {
729 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
730 }
731
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700732 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShaderD3D->mVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500733 {
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700734 const PackedVarying &varying = vertexShaderD3D->mVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400735 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500736 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400737 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500738 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400739 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500740
741 for (int row = 0; row < variableRows; row++)
742 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400743 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500744 vertexHLSL += " output.v" + Str(r);
745
746 bool sharedRegister = false; // Register used by multiple varyings
747
748 for (int x = 0; x < 4; x++)
749 {
750 if (packing[r][x] && packing[r][x] != packing[r][0])
751 {
752 sharedRegister = true;
753 break;
754 }
755 }
756
757 if(sharedRegister)
758 {
759 vertexHLSL += ".";
760
761 for (int x = 0; x < 4; x++)
762 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400763 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500764 {
765 switch(x)
766 {
767 case 0: vertexHLSL += "x"; break;
768 case 1: vertexHLSL += "y"; break;
769 case 2: vertexHLSL += "z"; break;
770 case 3: vertexHLSL += "w"; break;
771 }
772 }
773 }
774 }
775
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400776 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500777
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400778 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500779 {
780 vertexHLSL += ArrayString(elementIndex);
781 }
782
783 if (variableRows > 1)
784 {
785 vertexHLSL += ArrayString(row);
786 }
787
788 vertexHLSL += ";\n";
789 }
790 }
791 }
792 }
793
794 vertexHLSL += "\n"
795 " return output;\n"
796 "}\n";
797
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400798 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
799 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500800
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400801 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500802
803 if (shaderVersion < 300)
804 {
805 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
806 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400807 PixelShaderOuputVariable outputKeyVariable;
808 outputKeyVariable.type = GL_FLOAT_VEC4;
809 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
810 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
811 outputKeyVariable.outputIndex = renderTargetIndex;
812
813 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500814 }
815
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700816 *outUsesFragDepth = fragmentShaderD3D->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500817 }
818 else
819 {
820 defineOutputVariables(fragmentShader, programOutputVars);
821
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700822 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShaderD3D->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500823 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
824 {
825 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400826 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400827 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500828 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
829
Geoff Lang04fb89a2014-06-09 15:05:36 -0400830 PixelShaderOuputVariable outputKeyVariable;
831 outputKeyVariable.type = outputVariable.type;
832 outputKeyVariable.name = variableName + elementString;
833 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
834 outputKeyVariable.outputIndex = locationIt->first;
835
836 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500837 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400838
839 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500840 }
841
Geoff Lang04fb89a2014-06-09 15:05:36 -0400842 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500843
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700844 if (fragmentShaderD3D->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500845 {
846 if (shaderModel >= 4)
847 {
848 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
849 "{\n";
850 }
851 else
852 {
853 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
854 "{\n";
855 }
856 }
857 else
858 {
859 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
860 "{\n";
861 }
862
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400863 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500864 {
865 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
866
867 if (shaderModel >= 4)
868 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400869 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
870 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500871 }
872 else if (shaderModel >= 3)
873 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400874 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
875 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500876 }
877 else
878 {
879 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
880 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
881 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
882 }
883
884 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
885 " gl_FragCoord.w = rhw;\n";
886 }
887
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400888 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500889 {
890 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
891 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
892 }
893
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700894 if (fragmentShaderD3D->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500895 {
896 if (shaderModel <= 3)
897 {
898 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
899 }
900 else
901 {
902 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
903 }
904 }
905
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700906 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShaderD3D->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500907 {
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700908 const PackedVarying &varying = fragmentShaderD3D->mVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400909 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500910 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400911 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500912 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400913 GLenum transposedType = TransposeMatrixType(varying.type);
914 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500915 for (int row = 0; row < variableRows; row++)
916 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400917 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
918 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500919
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400920 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500921 {
922 pixelHLSL += ArrayString(elementIndex);
923 }
924
925 if (variableRows > 1)
926 {
927 pixelHLSL += ArrayString(row);
928 }
929
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400930 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500931 {
932 pixelHLSL += " = input.v" + n + ";\n"; break;
933 }
934 else
935 {
936 switch (VariableColumnCount(transposedType))
937 {
938 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
939 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
940 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
941 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
942 default: UNREACHABLE();
943 }
944 }
945 }
946 }
947 }
948 else UNREACHABLE();
949 }
950
951 pixelHLSL += "\n"
952 " gl_main();\n"
953 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400954 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500955 "}\n";
956
957 return true;
958}
959
960void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
961{
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700962 rx::FragmentShaderD3D *fragmentShaderD3D = rx::FragmentShaderD3D::makeFragmentShaderD3D(fragmentShader->getImplementation());
963 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShaderD3D->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500964
965 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
966 {
Jamie Madillf2575982014-06-25 16:04:54 -0400967 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500968 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
969
970 if (outputVariable.arraySize > 0)
971 {
972 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
973 {
974 const int location = baseLocation + elementIndex;
975 ASSERT(programOutputVars->count(location) == 0);
976 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
977 }
978 }
979 else
980 {
981 ASSERT(programOutputVars->count(baseLocation) == 0);
982 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
983 }
984 }
985}
986
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400987std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500988{
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700989 rx::VertexShaderD3D *vertexShaderD3D = rx::VertexShaderD3D::makeVertexShaderD3D(vertexShader->getImplementation());
Jamie Madill5f562732014-02-14 16:41:24 -0500990 // for now we only handle point sprite emulation
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700991 ASSERT(vertexShaderD3D->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400992 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -0500993}
994
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400995std::string DynamicHLSL::generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500996{
Brandon Jonesf05cdee2014-08-27 15:24:07 -0700997 rx::VertexShaderD3D *vertexShaderD3D = rx::VertexShaderD3D::makeVertexShaderD3D(vertexShader->getImplementation());
998 rx::FragmentShaderD3D *fragmentShaderD3D = rx::FragmentShaderD3D::makeFragmentShaderD3D(fragmentShader->getImplementation());
Jamie Madill5f562732014-02-14 16:41:24 -0500999 ASSERT(registers >= 0);
Brandon Jonesf05cdee2014-08-27 15:24:07 -07001000 ASSERT(vertexShaderD3D->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001001 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1002
1003 std::string geomHLSL;
1004
Brandon Jonesf05cdee2014-08-27 15:24:07 -07001005 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShaderD3D->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001006 false, true, false);
Brandon Jonesf05cdee2014-08-27 15:24:07 -07001007 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShaderD3D->mUsesFragCoord,
1008 fragmentShaderD3D->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001009
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001010 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1011 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1012 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001013
Geoff Langc0b9ef42014-07-02 10:02:37 -04001014 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001015 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1016 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001017 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1018 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001019 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001020 "static float2 pointSpriteCorners[] = \n"
1021 "{\n"
1022 " float2( 0.5f, -0.5f),\n"
1023 " float2( 0.5f, 0.5f),\n"
1024 " float2(-0.5f, -0.5f),\n"
1025 " float2(-0.5f, 0.5f)\n"
1026 "};\n"
1027 "\n"
1028 "static float2 pointSpriteTexcoords[] = \n"
1029 "{\n"
1030 " float2(1.0f, 1.0f),\n"
1031 " float2(1.0f, 0.0f),\n"
1032 " float2(0.0f, 1.0f),\n"
1033 " float2(0.0f, 0.0f)\n"
1034 "};\n"
1035 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001036 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1037 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001038 "\n"
1039 "[maxvertexcount(4)]\n"
1040 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1041 "{\n"
1042 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001043 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001044 " output.gl_PointSize = input[0].gl_PointSize;\n";
1045
1046 for (int r = 0; r < registers; r++)
1047 {
1048 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1049 }
1050
Brandon Jonesf05cdee2014-08-27 15:24:07 -07001051 if (fragmentShaderD3D->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001052 {
1053 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1054 }
1055
1056 geomHLSL += " \n"
1057 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001058 " float4 dx_Position = input[0].dx_Position;\n"
1059 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001060
1061 for (int corner = 0; corner < 4; corner++)
1062 {
1063 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001064 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001065
Brandon Jonesf05cdee2014-08-27 15:24:07 -07001066 if (fragmentShaderD3D->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001067 {
1068 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1069 }
1070
1071 geomHLSL += " outStream.Append(output);\n";
1072 }
1073
1074 geomHLSL += " \n"
1075 " outStream.RestartStrip();\n"
1076 "}\n";
1077
1078 return geomHLSL;
1079}
1080
1081// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001082std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001083{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001084 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001085 {
1086 return "_" + name;
1087 }
1088
1089 return name;
1090}
1091
Jamie Madillf2575982014-06-25 16:04:54 -04001092std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001093{
Jamie Madilla53ab512014-03-17 09:47:44 -04001094 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001095
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001096 // Matrix
1097 if (IsMatrixType(shaderAttrib.type))
1098 {
Jamie Madill8664b062014-02-14 16:41:29 -05001099 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001100 }
1101
Jamie Madillf2575982014-06-25 16:04:54 -04001102 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1103 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001104
Jamie Madill8664b062014-02-14 16:41:29 -05001105 // Perform integer to float conversion (if necessary)
1106 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1107
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001108 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001109 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001110 // TODO: normalization for 32-bit integer formats
1111 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1112 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001113 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001114
1115 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001116 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001117}
1118
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001119void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1120{
1121 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1122 {
1123 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1124
1125 if (vertexFormat.mType == GL_NONE)
1126 {
1127 signature[inputIndex] = GL_NONE;
1128 }
1129 else
1130 {
1131 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1132 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1133 }
1134 }
1135}
1136
Jamie Madill5f562732014-02-14 16:41:24 -05001137}