blob: 6aa0d137863a64b5ec70f9c3109b4f3586030ee8 [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
Brandon Jonesd8d72432014-08-22 15:11:23 -07009#include "libGLESv2/renderer/d3d/DynamicHLSL.h"
Brandon Jonesf05cdee2014-08-27 15:24:07 -070010#include "libGLESv2/renderer/d3d/ShaderD3D.h"
Jamie Madill5f562732014-02-14 16:41:24 -050011#include "libGLESv2/renderer/Renderer.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040012#include "libGLESv2/Shader.h"
13#include "libGLESv2/Program.h"
Jamie Madill5f562732014-02-14 16:41:24 -050014#include "libGLESv2/ProgramBinary.h"
Jamie Madill8664b062014-02-14 16:41:29 -050015#include "libGLESv2/formatutils.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040016
17#include "common/utilities.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040018#include "common/blocklayout.h"
Jamie Madill5f562732014-02-14 16:41:24 -050019
Jamie Madill53cb14d2014-07-08 15:02:35 -040020// For use with ArrayString, see angleutils.h
21META_ASSERT(GL_INVALID_INDEX == UINT_MAX);
Jamie Madill5f562732014-02-14 16:41:24 -050022
Brandon Jonesd8d72432014-08-22 15:11:23 -070023using namespace gl;
24
Jamie Madill8664b062014-02-14 16:41:29 -050025namespace gl_d3d
Jamie Madill5f562732014-02-14 16:41:24 -050026{
Jamie Madill8664b062014-02-14 16:41:29 -050027
28std::string HLSLComponentTypeString(GLenum componentType)
29{
30 switch (componentType)
31 {
32 case GL_UNSIGNED_INT: return "uint";
33 case GL_INT: return "int";
34 case GL_UNSIGNED_NORMALIZED:
35 case GL_SIGNED_NORMALIZED:
36 case GL_FLOAT: return "float";
37 default: UNREACHABLE(); return "not-component-type";
38 }
Jamie Madill5f562732014-02-14 16:41:24 -050039}
40
Jamie Madill8664b062014-02-14 16:41:29 -050041std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
42{
43 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
44}
45
46std::string HLSLMatrixTypeString(GLenum type)
47{
48 switch (type)
49 {
50 case GL_FLOAT_MAT2: return "float2x2";
51 case GL_FLOAT_MAT3: return "float3x3";
52 case GL_FLOAT_MAT4: return "float4x4";
53 case GL_FLOAT_MAT2x3: return "float2x3";
54 case GL_FLOAT_MAT3x2: return "float3x2";
55 case GL_FLOAT_MAT2x4: return "float2x4";
56 case GL_FLOAT_MAT4x2: return "float4x2";
57 case GL_FLOAT_MAT3x4: return "float3x4";
58 case GL_FLOAT_MAT4x3: return "float4x3";
59 default: UNREACHABLE(); return "not-matrix-type";
60 }
61}
62
63std::string HLSLTypeString(GLenum type)
64{
65 if (gl::IsMatrixType(type))
66 {
67 return HLSLMatrixTypeString(type);
68 }
69
Jamie Madillf2575982014-06-25 16:04:54 -040070 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050071}
72
73}
74
Brandon Jonesd8d72432014-08-22 15:11:23 -070075namespace rx
Jamie Madill8664b062014-02-14 16:41:29 -050076{
77
Geoff Lang04fb89a2014-06-09 15:05:36 -040078const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
79const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050080
Jamie Madill5f562732014-02-14 16:41:24 -050081DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
82 : mRenderer(renderer)
83{
84}
85
Jamie Madillff0d2ba2014-05-14 13:49:10 -040086static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -050087{
Geoff Lang48dcae72014-02-05 16:28:24 -050088 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -050089
Geoff Lang48dcae72014-02-05 16:28:24 -050090 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -040091 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -050092 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -050093
Jamie Madillff0d2ba2014-05-14 13:49:10 -040094 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -050095 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -040096 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -050097 {
Geoff Lang48dcae72014-02-05 16:28:24 -050098 bool available = true;
99
100 for (int y = 0; y < registers && available; y++)
101 {
102 for (int x = 0; x < elements && available; x++)
103 {
104 if (packing[r + y][x])
105 {
106 available = false;
107 }
108 }
109 }
110
111 if (available)
112 {
113 varying->registerIndex = r;
Geoff Lang48dcae72014-02-05 16:28:24 -0500114
115 for (int y = 0; y < registers; y++)
116 {
117 for (int x = 0; x < elements; x++)
118 {
119 packing[r + y][x] = &*varying;
120 }
121 }
122
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400123 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500124 }
125 }
126
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400127 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500128 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400129 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500130 {
131 bool available = true;
132
133 for (int y = 0; y < registers && available; y++)
134 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500135 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500136 {
137 if (packing[r + y][x])
138 {
139 available = false;
140 }
141 }
142 }
143
144 if (available)
145 {
146 varying->registerIndex = r;
Jamie Madill5f562732014-02-14 16:41:24 -0500147
148 for (int y = 0; y < registers; y++)
149 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500150 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500151 {
152 packing[r + y][x] = &*varying;
153 }
154 }
155
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400156 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500157 }
158 }
Jamie Madill5f562732014-02-14 16:41:24 -0500159 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500160 }
161 else if (elements == 1)
162 {
163 int space[4] = { 0 };
164
165 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500166 {
Jamie Madill5f562732014-02-14 16:41:24 -0500167 for (int x = 0; x < 4; x++)
168 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500169 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500170 }
171 }
Jamie Madill5f562732014-02-14 16:41:24 -0500172
Geoff Lang48dcae72014-02-05 16:28:24 -0500173 int column = 0;
174
175 for (int x = 0; x < 4; x++)
176 {
177 if (space[x] >= registers && space[x] < space[column])
178 {
179 column = x;
180 }
181 }
182
183 if (space[column] >= registers)
184 {
185 for (int r = 0; r < maxVaryingVectors; r++)
186 {
187 if (!packing[r][column])
188 {
189 varying->registerIndex = r;
190
191 for (int y = r; y < r + registers; y++)
192 {
193 packing[y][column] = &*varying;
194 }
195
196 break;
197 }
198 }
199
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400200 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500201 }
202 }
203 else UNREACHABLE();
204
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400205 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500206}
207
208// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
209// Returns the number of used varying registers, or -1 if unsuccesful
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400210int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, rx::ShaderD3D *fragmentShader,
211 rx::ShaderD3D *vertexShader, const std::vector<std::string>& transformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -0500212{
Geoff Lang3a61c322014-07-10 13:01:54 -0400213 // TODO (geofflang): Use context's caps
214 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Geoff Lang48dcae72014-02-05 16:28:24 -0500215
Brandon Jones71620962014-08-20 14:04:59 -0700216 vertexShader->resetVaryingsRegisterAssignment();
217 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500218
219 std::set<std::string> packedVaryings;
220
Jamie Madilld15250e2014-09-03 09:40:44 -0400221 std::vector<gl::PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
222 std::vector<gl::PackedVarying> &vertexVaryings = vertexShader->getVaryings();
223 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500224 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400225 PackedVarying *varying = &fragmentVaryings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400226
227 // Do not assign registers to built-in or unreferenced varyings
228 if (varying->isBuiltIn() || !varying->staticUse)
229 {
230 continue;
231 }
232
Geoff Lang48dcae72014-02-05 16:28:24 -0500233 if (packVarying(varying, maxVaryingVectors, packing))
234 {
235 packedVaryings.insert(varying->name);
236 }
237 else
Jamie Madill5f562732014-02-14 16:41:24 -0500238 {
239 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500240 return -1;
241 }
242 }
243
Geoff Lang48dcae72014-02-05 16:28:24 -0500244 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
245 {
246 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
247 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
248 {
249 bool found = false;
Jamie Madilld15250e2014-09-03 09:40:44 -0400250 for (unsigned int varyingIndex = 0; varyingIndex < vertexVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500251 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400252 PackedVarying *varying = &vertexVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500253 if (transformFeedbackVarying == varying->name)
254 {
255 if (!packVarying(varying, maxVaryingVectors, packing))
256 {
257 infoLog.append("Could not pack varying %s", varying->name.c_str());
258 return -1;
259 }
260
261 found = true;
262 break;
263 }
264 }
265
266 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
267 {
268 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
269 return -1;
270 }
271 }
272 }
273
Jamie Madill5f562732014-02-14 16:41:24 -0500274 // Return the number of used registers
275 int registers = 0;
276
277 for (int r = 0; r < maxVaryingVectors; r++)
278 {
279 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
280 {
281 registers++;
282 }
283 }
284
285 return registers;
286}
287
Jamie Madill54ad4f82014-09-03 09:40:46 -0400288std::string DynamicHLSL::generateVaryingHLSL(const ShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500289{
Brandon Jones71620962014-08-20 14:04:59 -0700290 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500291 std::string varyingHLSL;
292
Jamie Madill54ad4f82014-09-03 09:40:46 -0400293 const std::vector<gl::PackedVarying> &varyings = shader->getVaryings();
294
Jamie Madilld15250e2014-09-03 09:40:44 -0400295 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500296 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400297 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400298 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500299 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400300 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400301 GLenum transposedType = TransposeMatrixType(varying.type);
302 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500303
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400304 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500305 {
Jamie Madill5f562732014-02-14 16:41:24 -0500306 for (int row = 0; row < variableRows; row++)
307 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400308 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500309 {
Jamie Madillf2575982014-06-25 16:04:54 -0400310 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
311 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
312 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500313 default: UNREACHABLE();
314 }
315
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400316 unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500317 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500318
Jamie Madilla53ab512014-03-17 09:47:44 -0400319 std::string typeString;
320
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400321 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400322 {
323 // matrices within structs are not transposed, so
324 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400325 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400326 }
327 else
328 {
Jamie Madillf2575982014-06-25 16:04:54 -0400329 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400330 int columnCount = VariableColumnCount(transposedType);
Jamie Madilla53ab512014-03-17 09:47:44 -0400331 typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
332 }
Jamie Madill5f562732014-02-14 16:41:24 -0500333 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
334 }
335 }
336 }
Jamie Madill5f562732014-02-14 16:41:24 -0500337 }
338
339 return varyingHLSL;
340}
341
Jamie Madillf2575982014-06-25 16:04:54 -0400342std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
343 const VertexFormat inputLayout[],
344 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500345{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400346 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500347
348 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400349 unsigned int inputIndex = 0;
350
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500351 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
352 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400353 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
354
355 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madillf2575982014-06-25 16:04:54 -0400356 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500357
Jamie Madill8664b062014-02-14 16:41:29 -0500358 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500359 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400360 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500361 if (IsMatrixType(shaderAttribute.type))
362 {
363 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400364 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500365 }
366 else
367 {
368 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madillf2575982014-06-25 16:04:54 -0400369 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500370 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500371
Jamie Madilla53ab512014-03-17 09:47:44 -0400372 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400373 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500374
Jamie Madill3b7e2052014-03-17 09:47:43 -0400375 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400376 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500377
378 // Mismatched vertex attribute to vertex input may result in an undefined
379 // data reinterpretation (eg for pure integer->float, float->pure integer)
380 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400381 if (IsMatrixType(shaderAttribute.type) ||
382 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500383 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400384 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500385 }
386 else
387 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400388 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500389 }
390
Jamie Madill3b7e2052014-03-17 09:47:43 -0400391 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400392
Jamie Madillac0a2672014-04-11 13:33:56 -0400393 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
394 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500395 }
396
Geoff Lang04fb89a2014-06-09 15:05:36 -0400397 std::string replacementHLSL = "struct VS_INPUT\n"
398 "{\n" +
399 structHLSL +
400 "};\n"
401 "\n"
402 "void initAttributes(VS_INPUT input)\n"
403 "{\n" +
404 initHLSL +
405 "}\n";
406
407 std::string vertexHLSL(sourceShader);
408
409 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
410 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
411
412 return vertexHLSL;
413}
414
415std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables,
416 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
417{
418 const int shaderModel = mRenderer->getMajorShaderModel();
419 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
420 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
421
422 std::string declarationHLSL;
423 std::string copyHLSL;
424 for (size_t i = 0; i < outputVariables.size(); i++)
425 {
426 const PixelShaderOuputVariable& outputVariable = outputVariables[i];
427 ASSERT(outputLayout.size() > outputVariable.outputIndex);
Geoff Lang4ace4232014-06-18 19:12:48 -0400428
429 // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers
430 bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE
431 if (outputIndexEnabled)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400432 {
433 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
434 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
435
436 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
437 }
438 }
439
440 if (usesFragDepth)
441 {
442 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
443 copyHLSL += " output.gl_Depth = gl_Depth; \n";
444 }
445
446 std::string replacementHLSL = "struct PS_OUTPUT\n"
447 "{\n" +
448 declarationHLSL +
449 "};\n"
450 "\n"
451 "PS_OUTPUT generateOutput()\n"
452 "{\n"
453 " PS_OUTPUT output;\n" +
454 copyHLSL +
455 " return output;\n"
456 "}\n";
457
458 std::string pixelHLSL(sourceShader);
459
460 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
461 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
462
463 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500464}
465
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400466std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
467{
468 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
469 // In D3D11 we manually compute gl_PointCoord in the GS.
470 int shaderModel = mRenderer->getMajorShaderModel();
471 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
472}
473
474struct DynamicHLSL::SemanticInfo
475{
476 struct BuiltinInfo
477 {
478 BuiltinInfo()
479 : enabled(false),
480 index(0),
481 systemValue(false)
482 {}
483
484 bool enabled;
485 std::string semantic;
486 unsigned int index;
487 bool systemValue;
488
489 std::string str() const
490 {
491 return (systemValue ? semantic : (semantic + Str(index)));
492 }
493
494 void enableSystem(const std::string &systemValueSemantic)
495 {
496 enabled = true;
497 semantic = systemValueSemantic;
498 systemValue = true;
499 }
500
501 void enable(const std::string &semanticVal, unsigned int indexVal)
502 {
503 enabled = true;
504 semantic = semanticVal;
505 index = indexVal;
506 }
507 };
508
509 BuiltinInfo dxPosition;
510 BuiltinInfo glPosition;
511 BuiltinInfo glFragCoord;
512 BuiltinInfo glPointCoord;
513 BuiltinInfo glPointSize;
514};
515
516DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
517 bool pointSize, bool pixelShader) const
518{
519 SemanticInfo info;
520 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
521 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
522
523 int reservedRegisterIndex = startRegisters;
524
525 if (hlsl4)
526 {
527 info.dxPosition.enableSystem("SV_Position");
528 }
529 else if (pixelShader)
530 {
531 info.dxPosition.enableSystem("VPOS");
532 }
533 else
534 {
535 info.dxPosition.enableSystem("POSITION");
536 }
537
538 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
539
540 if (fragCoord)
541 {
542 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
543 }
544
545 if (pointCoord)
546 {
547 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
548 // In D3D11 we manually compute gl_PointCoord in the GS.
549 if (hlsl4)
550 {
551 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
552 }
553 else
554 {
555 info.glPointCoord.enable("TEXCOORD", 0);
556 }
557 }
558
559 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
560 if (pointSize && (!pixelShader || hlsl4))
561 {
562 info.glPointSize.enableSystem("PSIZE");
563 }
564
565 return info;
566}
567
568std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
569{
570 std::string linkHLSL = "{\n";
571
572 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
573
574 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
575 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
576
577 if (info.glFragCoord.enabled)
578 {
579 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
580 }
581
582 if (info.glPointCoord.enabled)
583 {
584 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
585 }
586
587 linkHLSL += varyingHLSL;
588
589 if (info.glPointSize.enabled)
590 {
591 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
592 }
593
594 linkHLSL += "};\n";
595
596 return linkHLSL;
597}
598
599void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
600 std::vector<LinkedVarying> *linkedVaryings) const
601{
602 ASSERT(info.glPosition.enabled);
603
604 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
605 info.glPosition.index, 1));
606
607 if (info.glFragCoord.enabled)
608 {
609 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
610 info.glFragCoord.index, 1));
611 }
612
613 if (info.glPointSize.enabled)
614 {
615 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
616 }
617}
618
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400619void DynamicHLSL::storeUserLinkedVaryings(const rx::ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400620 std::vector<LinkedVarying> *linkedVaryings) const
621{
Brandon Jones71620962014-08-20 14:04:59 -0700622 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400623 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400624
625 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
626 {
627 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400628
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400629 if (varying.registerAssigned())
630 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400631 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400632 GLenum transposedType = TransposeMatrixType(varying.type);
633 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
634
635 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
636 varyingSemantic, varying.registerIndex,
637 variableRows * varying.elementCount()));
638 }
639 }
640}
641
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400642bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500643 std::string& pixelHLSL, std::string& vertexHLSL,
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400644 rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500645 const std::vector<std::string>& transformFeedbackVaryings,
646 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400647 std::map<int, VariableLocation> *programOutputVars,
648 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
649 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500650{
651 if (pixelHLSL.empty() || vertexHLSL.empty())
652 {
653 return false;
654 }
655
Brandon Jones71620962014-08-20 14:04:59 -0700656 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
657 bool usesFragColor = fragmentShader->mUsesFragColor;
658 bool usesFragData = fragmentShader->mUsesFragData;
659 bool usesFragCoord = fragmentShader->mUsesFragCoord;
660 bool usesPointCoord = fragmentShader->mUsesPointCoord;
661 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400662
Jamie Madill5f562732014-02-14 16:41:24 -0500663 if (usesFragColor && usesFragData)
664 {
665 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
666 return false;
667 }
668
669 // Write the HLSL input/output declarations
670 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang3a61c322014-07-10 13:01:54 -0400671
672 // TODO (geofflang): Use context's caps
673 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Jamie Madill5f562732014-02-14 16:41:24 -0500674
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400675 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500676
677 // Two cases when writing to gl_FragColor and using ESSL 1.0:
678 // - with a 3.0 context, the output color is copied to channel 0
679 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jones71620962014-08-20 14:04:59 -0700680 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400681 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500682
Brandon Jones71620962014-08-20 14:04:59 -0700683 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500684
685 if (registersNeeded > maxVaryingVectors)
686 {
687 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500688 return false;
689 }
690
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400691 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
692 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
693 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500694
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400695 storeUserLinkedVaryings(vertexShader, linkedVaryings);
696 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500697
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500698 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400699 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
700 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500701 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500702 "{\n"
703 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500704
705 if (shaderModel >= 4)
706 {
707 vertexHLSL += "\n"
708 " gl_main();\n"
709 "\n"
710 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500711 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400712 " output.dx_Position.x = gl_Position.x;\n"
713 " output.dx_Position.y = -gl_Position.y;\n"
714 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
715 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500716 }
717 else
718 {
719 vertexHLSL += "\n"
720 " gl_main();\n"
721 "\n"
722 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500723 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400724 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
725 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
726 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
727 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500728 }
729
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400730 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500731 {
732 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
733 }
734
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400735 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500736 {
737 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
738 }
739
Jamie Madilld15250e2014-09-03 09:40:44 -0400740 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
741 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500742 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400743 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400744 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500745 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400746 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500747 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400748 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500749
750 for (int row = 0; row < variableRows; row++)
751 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400752 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500753 vertexHLSL += " output.v" + Str(r);
754
755 bool sharedRegister = false; // Register used by multiple varyings
756
757 for (int x = 0; x < 4; x++)
758 {
759 if (packing[r][x] && packing[r][x] != packing[r][0])
760 {
761 sharedRegister = true;
762 break;
763 }
764 }
765
766 if(sharedRegister)
767 {
768 vertexHLSL += ".";
769
770 for (int x = 0; x < 4; x++)
771 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400772 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500773 {
774 switch(x)
775 {
776 case 0: vertexHLSL += "x"; break;
777 case 1: vertexHLSL += "y"; break;
778 case 2: vertexHLSL += "z"; break;
779 case 3: vertexHLSL += "w"; break;
780 }
781 }
782 }
783 }
784
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400785 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500786
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400787 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500788 {
789 vertexHLSL += ArrayString(elementIndex);
790 }
791
792 if (variableRows > 1)
793 {
794 vertexHLSL += ArrayString(row);
795 }
796
797 vertexHLSL += ";\n";
798 }
799 }
800 }
801 }
802
803 vertexHLSL += "\n"
804 " return output;\n"
805 "}\n";
806
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400807 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
808 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500809
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400810 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500811
812 if (shaderVersion < 300)
813 {
814 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
815 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400816 PixelShaderOuputVariable outputKeyVariable;
817 outputKeyVariable.type = GL_FLOAT_VEC4;
818 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
819 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
820 outputKeyVariable.outputIndex = renderTargetIndex;
821
822 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500823 }
824
Brandon Jones71620962014-08-20 14:04:59 -0700825 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500826 }
827 else
828 {
829 defineOutputVariables(fragmentShader, programOutputVars);
830
Jamie Madilld15250e2014-09-03 09:40:44 -0400831 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500832 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
833 {
834 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400835 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400836 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500837 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
838
Jamie Madill54ad4f82014-09-03 09:40:46 -0400839 ASSERT(outputVariable.staticUse);
840
Geoff Lang04fb89a2014-06-09 15:05:36 -0400841 PixelShaderOuputVariable outputKeyVariable;
842 outputKeyVariable.type = outputVariable.type;
843 outputKeyVariable.name = variableName + elementString;
844 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
845 outputKeyVariable.outputIndex = locationIt->first;
846
847 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500848 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400849
850 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500851 }
852
Geoff Lang04fb89a2014-06-09 15:05:36 -0400853 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500854
Brandon Jones71620962014-08-20 14:04:59 -0700855 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500856 {
857 if (shaderModel >= 4)
858 {
859 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
860 "{\n";
861 }
862 else
863 {
864 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
865 "{\n";
866 }
867 }
868 else
869 {
870 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
871 "{\n";
872 }
873
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400874 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500875 {
876 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
877
878 if (shaderModel >= 4)
879 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400880 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
881 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500882 }
883 else if (shaderModel >= 3)
884 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400885 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
886 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500887 }
888 else
889 {
890 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
891 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
892 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
893 }
894
895 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
896 " gl_FragCoord.w = rhw;\n";
897 }
898
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400899 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500900 {
901 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
902 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
903 }
904
Brandon Jones71620962014-08-20 14:04:59 -0700905 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500906 {
907 if (shaderModel <= 3)
908 {
909 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
910 }
911 else
912 {
913 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
914 }
915 }
916
Jamie Madilld15250e2014-09-03 09:40:44 -0400917 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
918 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500919 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400920 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400921 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500922 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400923 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400924 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500925 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400926 GLenum transposedType = TransposeMatrixType(varying.type);
927 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500928 for (int row = 0; row < variableRows; row++)
929 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400930 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
931 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500932
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400933 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500934 {
935 pixelHLSL += ArrayString(elementIndex);
936 }
937
938 if (variableRows > 1)
939 {
940 pixelHLSL += ArrayString(row);
941 }
942
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400943 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500944 {
945 pixelHLSL += " = input.v" + n + ";\n"; break;
946 }
947 else
948 {
949 switch (VariableColumnCount(transposedType))
950 {
951 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
952 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
953 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
954 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
955 default: UNREACHABLE();
956 }
957 }
958 }
959 }
960 }
Jamie Madill54ad4f82014-09-03 09:40:46 -0400961 else
962 {
963 ASSERT(varying.isBuiltIn() || !varying.staticUse);
964 }
Jamie Madill5f562732014-02-14 16:41:24 -0500965 }
966
967 pixelHLSL += "\n"
968 " gl_main();\n"
969 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400970 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500971 "}\n";
972
973 return true;
974}
975
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400976void DynamicHLSL::defineOutputVariables(rx::ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500977{
Jamie Madilld15250e2014-09-03 09:40:44 -0400978 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500979
980 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
981 {
Jamie Madillf2575982014-06-25 16:04:54 -0400982 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500983 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
984
Jamie Madill54ad4f82014-09-03 09:40:46 -0400985 ASSERT(outputVariable.staticUse);
986
Jamie Madill5f562732014-02-14 16:41:24 -0500987 if (outputVariable.arraySize > 0)
988 {
989 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
990 {
991 const int location = baseLocation + elementIndex;
992 ASSERT(programOutputVars->count(location) == 0);
993 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
994 }
995 }
996 else
997 {
998 ASSERT(programOutputVars->count(baseLocation) == 0);
999 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
1000 }
1001 }
1002}
1003
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001004std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001005{
1006 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001007 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001008 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001009}
1010
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001011std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001012{
1013 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001014 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001015 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1016
1017 std::string geomHLSL;
1018
Brandon Jones71620962014-08-20 14:04:59 -07001019 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001020 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -07001021 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1022 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001023
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001024 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1025 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1026 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001027
Geoff Langc0b9ef42014-07-02 10:02:37 -04001028 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001029 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1030 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001031 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1032 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001033 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001034 "static float2 pointSpriteCorners[] = \n"
1035 "{\n"
1036 " float2( 0.5f, -0.5f),\n"
1037 " float2( 0.5f, 0.5f),\n"
1038 " float2(-0.5f, -0.5f),\n"
1039 " float2(-0.5f, 0.5f)\n"
1040 "};\n"
1041 "\n"
1042 "static float2 pointSpriteTexcoords[] = \n"
1043 "{\n"
1044 " float2(1.0f, 1.0f),\n"
1045 " float2(1.0f, 0.0f),\n"
1046 " float2(0.0f, 1.0f),\n"
1047 " float2(0.0f, 0.0f)\n"
1048 "};\n"
1049 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001050 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1051 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001052 "\n"
1053 "[maxvertexcount(4)]\n"
1054 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1055 "{\n"
1056 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001057 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001058 " output.gl_PointSize = input[0].gl_PointSize;\n";
1059
1060 for (int r = 0; r < registers; r++)
1061 {
1062 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1063 }
1064
Brandon Jones71620962014-08-20 14:04:59 -07001065 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001066 {
1067 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1068 }
1069
1070 geomHLSL += " \n"
1071 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001072 " float4 dx_Position = input[0].dx_Position;\n"
1073 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001074
1075 for (int corner = 0; corner < 4; corner++)
1076 {
1077 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001078 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001079
Brandon Jones71620962014-08-20 14:04:59 -07001080 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001081 {
1082 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1083 }
1084
1085 geomHLSL += " outStream.Append(output);\n";
1086 }
1087
1088 geomHLSL += " \n"
1089 " outStream.RestartStrip();\n"
1090 "}\n";
1091
1092 return geomHLSL;
1093}
1094
1095// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001096std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001097{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001098 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001099 {
1100 return "_" + name;
1101 }
1102
1103 return name;
1104}
1105
Jamie Madillf2575982014-06-25 16:04:54 -04001106std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001107{
Jamie Madilla53ab512014-03-17 09:47:44 -04001108 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001109
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001110 // Matrix
1111 if (IsMatrixType(shaderAttrib.type))
1112 {
Jamie Madill8664b062014-02-14 16:41:29 -05001113 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001114 }
1115
Jamie Madillf2575982014-06-25 16:04:54 -04001116 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1117 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001118
Jamie Madill8664b062014-02-14 16:41:29 -05001119 // Perform integer to float conversion (if necessary)
1120 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1121
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001122 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001123 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001124 // TODO: normalization for 32-bit integer formats
1125 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1126 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001127 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001128
1129 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001130 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001131}
1132
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001133void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1134{
1135 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1136 {
1137 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1138
1139 if (vertexFormat.mType == GL_NONE)
1140 {
1141 signature[inputIndex] = GL_NONE;
1142 }
1143 else
1144 {
1145 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1146 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1147 }
1148 }
1149}
1150
Jamie Madill5f562732014-02-14 16:41:24 -05001151}