blob: c91008a83ef186b2be9167e67a8b1d84176cae27 [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 Madill3f2e61d2014-09-05 10:38:05 -040025namespace
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
Jamie Madillf6be8d72014-09-05 10:38:07 -040073const rx::PixelShaderOutputVariable &GetOutputAtLocation(const std::vector<rx::PixelShaderOutputVariable> &outputVariables,
Jamie Madill3f2e61d2014-09-05 10:38:05 -040074 unsigned int location)
75{
76 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
77 {
78 if (outputVariables[variableIndex].outputIndex == location)
79 {
80 return outputVariables[variableIndex];
81 }
82 }
83
84 UNREACHABLE();
85 return outputVariables[0];
86}
87
Jamie Madill8664b062014-02-14 16:41:29 -050088}
89
Brandon Jonesd8d72432014-08-22 15:11:23 -070090namespace rx
Jamie Madill8664b062014-02-14 16:41:29 -050091{
92
Geoff Lang04fb89a2014-06-09 15:05:36 -040093const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
94const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050095
Jamie Madill5f562732014-02-14 16:41:24 -050096DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
97 : mRenderer(renderer)
98{
99}
100
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400101static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -0500102{
Geoff Lang48dcae72014-02-05 16:28:24 -0500103 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -0500104
Geoff Lang48dcae72014-02-05 16:28:24 -0500105 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -0400106 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -0500107 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500108
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400109 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -0500110 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400111 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -0500112 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500113 bool available = true;
114
115 for (int y = 0; y < registers && available; y++)
116 {
117 for (int x = 0; x < elements && available; x++)
118 {
119 if (packing[r + y][x])
120 {
121 available = false;
122 }
123 }
124 }
125
126 if (available)
127 {
128 varying->registerIndex = r;
Geoff Lang48dcae72014-02-05 16:28:24 -0500129
130 for (int y = 0; y < registers; y++)
131 {
132 for (int x = 0; x < elements; x++)
133 {
134 packing[r + y][x] = &*varying;
135 }
136 }
137
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400138 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500139 }
140 }
141
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400142 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500143 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400144 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500145 {
146 bool available = true;
147
148 for (int y = 0; y < registers && available; y++)
149 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500150 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500151 {
152 if (packing[r + y][x])
153 {
154 available = false;
155 }
156 }
157 }
158
159 if (available)
160 {
161 varying->registerIndex = r;
Jamie Madill5f562732014-02-14 16:41:24 -0500162
163 for (int y = 0; y < registers; y++)
164 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500165 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500166 {
167 packing[r + y][x] = &*varying;
168 }
169 }
170
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400171 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500172 }
173 }
Jamie Madill5f562732014-02-14 16:41:24 -0500174 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500175 }
176 else if (elements == 1)
177 {
178 int space[4] = { 0 };
179
180 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500181 {
Jamie Madill5f562732014-02-14 16:41:24 -0500182 for (int x = 0; x < 4; x++)
183 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500184 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500185 }
186 }
Jamie Madill5f562732014-02-14 16:41:24 -0500187
Geoff Lang48dcae72014-02-05 16:28:24 -0500188 int column = 0;
189
190 for (int x = 0; x < 4; x++)
191 {
192 if (space[x] >= registers && space[x] < space[column])
193 {
194 column = x;
195 }
196 }
197
198 if (space[column] >= registers)
199 {
200 for (int r = 0; r < maxVaryingVectors; r++)
201 {
202 if (!packing[r][column])
203 {
204 varying->registerIndex = r;
205
206 for (int y = r; y < r + registers; y++)
207 {
208 packing[y][column] = &*varying;
209 }
210
211 break;
212 }
213 }
214
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400215 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500216 }
217 }
218 else UNREACHABLE();
219
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400220 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500221}
222
223// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
224// Returns the number of used varying registers, or -1 if unsuccesful
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400225int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, rx::ShaderD3D *fragmentShader,
226 rx::ShaderD3D *vertexShader, const std::vector<std::string>& transformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -0500227{
Geoff Lang3a61c322014-07-10 13:01:54 -0400228 // TODO (geofflang): Use context's caps
229 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Geoff Lang48dcae72014-02-05 16:28:24 -0500230
Brandon Jones71620962014-08-20 14:04:59 -0700231 vertexShader->resetVaryingsRegisterAssignment();
232 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500233
234 std::set<std::string> packedVaryings;
235
Jamie Madilld15250e2014-09-03 09:40:44 -0400236 std::vector<gl::PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
237 std::vector<gl::PackedVarying> &vertexVaryings = vertexShader->getVaryings();
238 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500239 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400240 PackedVarying *varying = &fragmentVaryings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400241
242 // Do not assign registers to built-in or unreferenced varyings
243 if (varying->isBuiltIn() || !varying->staticUse)
244 {
245 continue;
246 }
247
Geoff Lang48dcae72014-02-05 16:28:24 -0500248 if (packVarying(varying, maxVaryingVectors, packing))
249 {
250 packedVaryings.insert(varying->name);
251 }
252 else
Jamie Madill5f562732014-02-14 16:41:24 -0500253 {
254 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500255 return -1;
256 }
257 }
258
Geoff Lang48dcae72014-02-05 16:28:24 -0500259 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
260 {
261 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
262 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
263 {
264 bool found = false;
Jamie Madilld15250e2014-09-03 09:40:44 -0400265 for (unsigned int varyingIndex = 0; varyingIndex < vertexVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500266 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400267 PackedVarying *varying = &vertexVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500268 if (transformFeedbackVarying == varying->name)
269 {
270 if (!packVarying(varying, maxVaryingVectors, packing))
271 {
272 infoLog.append("Could not pack varying %s", varying->name.c_str());
273 return -1;
274 }
275
276 found = true;
277 break;
278 }
279 }
280
281 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
282 {
283 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
284 return -1;
285 }
286 }
287 }
288
Jamie Madill5f562732014-02-14 16:41:24 -0500289 // Return the number of used registers
290 int registers = 0;
291
292 for (int r = 0; r < maxVaryingVectors; r++)
293 {
294 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
295 {
296 registers++;
297 }
298 }
299
300 return registers;
301}
302
Jamie Madill54ad4f82014-09-03 09:40:46 -0400303std::string DynamicHLSL::generateVaryingHLSL(const ShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500304{
Brandon Jones71620962014-08-20 14:04:59 -0700305 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500306 std::string varyingHLSL;
307
Jamie Madill54ad4f82014-09-03 09:40:46 -0400308 const std::vector<gl::PackedVarying> &varyings = shader->getVaryings();
309
Jamie Madilld15250e2014-09-03 09:40:44 -0400310 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500311 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400312 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400313 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500314 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400315 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400316 GLenum transposedType = TransposeMatrixType(varying.type);
317 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500318
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400319 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500320 {
Jamie Madill5f562732014-02-14 16:41:24 -0500321 for (int row = 0; row < variableRows; row++)
322 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400323 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500324 {
Jamie Madillf2575982014-06-25 16:04:54 -0400325 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
326 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
327 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500328 default: UNREACHABLE();
329 }
330
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400331 unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500332 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500333
Jamie Madilla53ab512014-03-17 09:47:44 -0400334 std::string typeString;
335
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400336 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400337 {
338 // matrices within structs are not transposed, so
339 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400340 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400341 }
342 else
343 {
Jamie Madillf2575982014-06-25 16:04:54 -0400344 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400345 int columnCount = VariableColumnCount(transposedType);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400346 typeString = HLSLComponentTypeString(componentType, columnCount);
Jamie Madilla53ab512014-03-17 09:47:44 -0400347 }
Jamie Madill5f562732014-02-14 16:41:24 -0500348 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
349 }
350 }
351 }
Jamie Madill5f562732014-02-14 16:41:24 -0500352 }
353
354 return varyingHLSL;
355}
356
Jamie Madillf2575982014-06-25 16:04:54 -0400357std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
358 const VertexFormat inputLayout[],
359 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500360{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400361 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500362
363 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400364 unsigned int inputIndex = 0;
365
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500366 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
367 {
Jamie Madillf2575982014-06-25 16:04:54 -0400368 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500369 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500370 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400371 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
372 const VertexFormat &vertexFormat = inputLayout[inputIndex];
373
Jamie Madill3b7e2052014-03-17 09:47:43 -0400374 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500375 if (IsMatrixType(shaderAttribute.type))
376 {
377 // Matrix types are always transposed
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400378 structHLSL += " " + HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500379 }
380 else
381 {
382 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400383 structHLSL += " " + HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500384 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500385
Jamie Madilla53ab512014-03-17 09:47:44 -0400386 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400387 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500388
Jamie Madill3b7e2052014-03-17 09:47:43 -0400389 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400390 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500391
392 // Mismatched vertex attribute to vertex input may result in an undefined
393 // data reinterpretation (eg for pure integer->float, float->pure integer)
394 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400395 if (IsMatrixType(shaderAttribute.type) ||
396 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500397 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400398 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500399 }
400 else
401 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400402 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500403 }
404
Jamie Madill3b7e2052014-03-17 09:47:43 -0400405 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400406
Jamie Madillac0a2672014-04-11 13:33:56 -0400407 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
408 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500409 }
410
Geoff Lang04fb89a2014-06-09 15:05:36 -0400411 std::string replacementHLSL = "struct VS_INPUT\n"
412 "{\n" +
413 structHLSL +
414 "};\n"
415 "\n"
416 "void initAttributes(VS_INPUT input)\n"
417 "{\n" +
418 initHLSL +
419 "}\n";
420
421 std::string vertexHLSL(sourceShader);
422
423 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
424 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
425
426 return vertexHLSL;
427}
428
Jamie Madillf6be8d72014-09-05 10:38:07 -0400429std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOutputVariable> &outputVariables,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400430 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
431{
432 const int shaderModel = mRenderer->getMajorShaderModel();
433 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
434 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
435
436 std::string declarationHLSL;
437 std::string copyHLSL;
Geoff Lang4ace4232014-06-18 19:12:48 -0400438
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400439 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
440 {
441 GLenum binding = outputLayout[layoutIndex];
442
443 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400444 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400445 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
446
Jamie Madillf6be8d72014-09-05 10:38:07 -0400447 const PixelShaderOutputVariable &outputVariable = GetOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400448
449 declarationHLSL += " " + HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
450 " : " + targetSemantic + Str(layoutIndex) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400451
452 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
453 }
454 }
455
456 if (usesFragDepth)
457 {
458 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
459 copyHLSL += " output.gl_Depth = gl_Depth; \n";
460 }
461
462 std::string replacementHLSL = "struct PS_OUTPUT\n"
463 "{\n" +
464 declarationHLSL +
465 "};\n"
466 "\n"
467 "PS_OUTPUT generateOutput()\n"
468 "{\n"
469 " PS_OUTPUT output;\n" +
470 copyHLSL +
471 " return output;\n"
472 "}\n";
473
474 std::string pixelHLSL(sourceShader);
475
476 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
477 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
478
479 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500480}
481
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400482std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
483{
484 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
485 // In D3D11 we manually compute gl_PointCoord in the GS.
486 int shaderModel = mRenderer->getMajorShaderModel();
487 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
488}
489
490struct DynamicHLSL::SemanticInfo
491{
492 struct BuiltinInfo
493 {
494 BuiltinInfo()
495 : enabled(false),
496 index(0),
497 systemValue(false)
498 {}
499
500 bool enabled;
501 std::string semantic;
502 unsigned int index;
503 bool systemValue;
504
505 std::string str() const
506 {
507 return (systemValue ? semantic : (semantic + Str(index)));
508 }
509
510 void enableSystem(const std::string &systemValueSemantic)
511 {
512 enabled = true;
513 semantic = systemValueSemantic;
514 systemValue = true;
515 }
516
517 void enable(const std::string &semanticVal, unsigned int indexVal)
518 {
519 enabled = true;
520 semantic = semanticVal;
521 index = indexVal;
522 }
523 };
524
525 BuiltinInfo dxPosition;
526 BuiltinInfo glPosition;
527 BuiltinInfo glFragCoord;
528 BuiltinInfo glPointCoord;
529 BuiltinInfo glPointSize;
530};
531
532DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
533 bool pointSize, bool pixelShader) const
534{
535 SemanticInfo info;
536 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
537 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
538
539 int reservedRegisterIndex = startRegisters;
540
541 if (hlsl4)
542 {
543 info.dxPosition.enableSystem("SV_Position");
544 }
545 else if (pixelShader)
546 {
547 info.dxPosition.enableSystem("VPOS");
548 }
549 else
550 {
551 info.dxPosition.enableSystem("POSITION");
552 }
553
554 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
555
556 if (fragCoord)
557 {
558 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
559 }
560
561 if (pointCoord)
562 {
563 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
564 // In D3D11 we manually compute gl_PointCoord in the GS.
565 if (hlsl4)
566 {
567 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
568 }
569 else
570 {
571 info.glPointCoord.enable("TEXCOORD", 0);
572 }
573 }
574
575 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
576 if (pointSize && (!pixelShader || hlsl4))
577 {
578 info.glPointSize.enableSystem("PSIZE");
579 }
580
581 return info;
582}
583
584std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
585{
586 std::string linkHLSL = "{\n";
587
588 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
589
590 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
591 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
592
593 if (info.glFragCoord.enabled)
594 {
595 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
596 }
597
598 if (info.glPointCoord.enabled)
599 {
600 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
601 }
602
603 linkHLSL += varyingHLSL;
604
605 if (info.glPointSize.enabled)
606 {
607 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
608 }
609
610 linkHLSL += "};\n";
611
612 return linkHLSL;
613}
614
615void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
616 std::vector<LinkedVarying> *linkedVaryings) const
617{
618 ASSERT(info.glPosition.enabled);
619
620 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
621 info.glPosition.index, 1));
622
623 if (info.glFragCoord.enabled)
624 {
625 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
626 info.glFragCoord.index, 1));
627 }
628
629 if (info.glPointSize.enabled)
630 {
631 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
632 }
633}
634
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400635void DynamicHLSL::storeUserLinkedVaryings(const rx::ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400636 std::vector<LinkedVarying> *linkedVaryings) const
637{
Brandon Jones71620962014-08-20 14:04:59 -0700638 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400639 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400640
641 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
642 {
643 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400644
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400645 if (varying.registerAssigned())
646 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400647 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400648 GLenum transposedType = TransposeMatrixType(varying.type);
649 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
650
651 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
652 varyingSemantic, varying.registerIndex,
653 variableRows * varying.elementCount()));
654 }
655 }
656}
657
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400658bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500659 std::string& pixelHLSL, std::string& vertexHLSL,
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400660 rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500661 const std::vector<std::string>& transformFeedbackVaryings,
662 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400663 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400664 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400665 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500666{
667 if (pixelHLSL.empty() || vertexHLSL.empty())
668 {
669 return false;
670 }
671
Brandon Jones71620962014-08-20 14:04:59 -0700672 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
673 bool usesFragColor = fragmentShader->mUsesFragColor;
674 bool usesFragData = fragmentShader->mUsesFragData;
675 bool usesFragCoord = fragmentShader->mUsesFragCoord;
676 bool usesPointCoord = fragmentShader->mUsesPointCoord;
677 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400678
Jamie Madill5f562732014-02-14 16:41:24 -0500679 if (usesFragColor && usesFragData)
680 {
681 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
682 return false;
683 }
684
685 // Write the HLSL input/output declarations
686 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang3a61c322014-07-10 13:01:54 -0400687
688 // TODO (geofflang): Use context's caps
689 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Jamie Madill5f562732014-02-14 16:41:24 -0500690
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400691 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500692
693 // Two cases when writing to gl_FragColor and using ESSL 1.0:
694 // - with a 3.0 context, the output color is copied to channel 0
695 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jones71620962014-08-20 14:04:59 -0700696 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400697 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500698
Brandon Jones71620962014-08-20 14:04:59 -0700699 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500700
701 if (registersNeeded > maxVaryingVectors)
702 {
703 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500704 return false;
705 }
706
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400707 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
708 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
709 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500710
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400711 storeUserLinkedVaryings(vertexShader, linkedVaryings);
712 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500713
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500714 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400715 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
716 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500717 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500718 "{\n"
719 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500720
721 if (shaderModel >= 4)
722 {
723 vertexHLSL += "\n"
724 " gl_main();\n"
725 "\n"
726 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500727 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400728 " output.dx_Position.x = gl_Position.x;\n"
729 " output.dx_Position.y = -gl_Position.y;\n"
730 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
731 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500732 }
733 else
734 {
735 vertexHLSL += "\n"
736 " gl_main();\n"
737 "\n"
738 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500739 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400740 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
741 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
742 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
743 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500744 }
745
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400746 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500747 {
748 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
749 }
750
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400751 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500752 {
753 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
754 }
755
Jamie Madilld15250e2014-09-03 09:40:44 -0400756 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
757 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500758 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400759 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400760 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500761 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400762 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500763 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400764 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500765
766 for (int row = 0; row < variableRows; row++)
767 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400768 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500769 vertexHLSL += " output.v" + Str(r);
770
771 bool sharedRegister = false; // Register used by multiple varyings
772
773 for (int x = 0; x < 4; x++)
774 {
775 if (packing[r][x] && packing[r][x] != packing[r][0])
776 {
777 sharedRegister = true;
778 break;
779 }
780 }
781
782 if(sharedRegister)
783 {
784 vertexHLSL += ".";
785
786 for (int x = 0; x < 4; x++)
787 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400788 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500789 {
790 switch(x)
791 {
792 case 0: vertexHLSL += "x"; break;
793 case 1: vertexHLSL += "y"; break;
794 case 2: vertexHLSL += "z"; break;
795 case 3: vertexHLSL += "w"; break;
796 }
797 }
798 }
799 }
800
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400801 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500802
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400803 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500804 {
805 vertexHLSL += ArrayString(elementIndex);
806 }
807
808 if (variableRows > 1)
809 {
810 vertexHLSL += ArrayString(row);
811 }
812
813 vertexHLSL += ";\n";
814 }
815 }
816 }
817 }
818
819 vertexHLSL += "\n"
820 " return output;\n"
821 "}\n";
822
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400823 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
824 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500825
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400826 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500827
828 if (shaderVersion < 300)
829 {
830 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
831 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400832 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400833 outputKeyVariable.type = GL_FLOAT_VEC4;
834 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
835 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
836 outputKeyVariable.outputIndex = renderTargetIndex;
837
838 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500839 }
840
Brandon Jones71620962014-08-20 14:04:59 -0700841 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500842 }
843 else
844 {
845 defineOutputVariables(fragmentShader, programOutputVars);
846
Jamie Madilld15250e2014-09-03 09:40:44 -0400847 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500848 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
849 {
850 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400851 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400852 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500853 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
854
Jamie Madill54ad4f82014-09-03 09:40:46 -0400855 ASSERT(outputVariable.staticUse);
856
Jamie Madillf6be8d72014-09-05 10:38:07 -0400857 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400858 outputKeyVariable.type = outputVariable.type;
859 outputKeyVariable.name = variableName + elementString;
860 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
861 outputKeyVariable.outputIndex = locationIt->first;
862
863 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500864 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400865
866 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500867 }
868
Geoff Lang04fb89a2014-06-09 15:05:36 -0400869 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500870
Brandon Jones71620962014-08-20 14:04:59 -0700871 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500872 {
873 if (shaderModel >= 4)
874 {
875 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
876 "{\n";
877 }
878 else
879 {
880 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
881 "{\n";
882 }
883 }
884 else
885 {
886 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
887 "{\n";
888 }
889
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400890 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500891 {
892 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
893
894 if (shaderModel >= 4)
895 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400896 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
897 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500898 }
899 else if (shaderModel >= 3)
900 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400901 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
902 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500903 }
904 else
905 {
906 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
907 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
908 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
909 }
910
911 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
912 " gl_FragCoord.w = rhw;\n";
913 }
914
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400915 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500916 {
917 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
918 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
919 }
920
Brandon Jones71620962014-08-20 14:04:59 -0700921 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500922 {
923 if (shaderModel <= 3)
924 {
925 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
926 }
927 else
928 {
929 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
930 }
931 }
932
Jamie Madilld15250e2014-09-03 09:40:44 -0400933 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
934 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500935 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400936 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400937 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500938 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400939 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400940 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500941 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400942 GLenum transposedType = TransposeMatrixType(varying.type);
943 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500944 for (int row = 0; row < variableRows; row++)
945 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400946 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
947 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500948
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400949 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500950 {
951 pixelHLSL += ArrayString(elementIndex);
952 }
953
954 if (variableRows > 1)
955 {
956 pixelHLSL += ArrayString(row);
957 }
958
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400959 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500960 {
961 pixelHLSL += " = input.v" + n + ";\n"; break;
962 }
963 else
964 {
965 switch (VariableColumnCount(transposedType))
966 {
967 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
968 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
969 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
970 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
971 default: UNREACHABLE();
972 }
973 }
974 }
975 }
976 }
Jamie Madill54ad4f82014-09-03 09:40:46 -0400977 else
978 {
979 ASSERT(varying.isBuiltIn() || !varying.staticUse);
980 }
Jamie Madill5f562732014-02-14 16:41:24 -0500981 }
982
983 pixelHLSL += "\n"
984 " gl_main();\n"
985 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400986 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500987 "}\n";
988
989 return true;
990}
991
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400992void DynamicHLSL::defineOutputVariables(rx::ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500993{
Jamie Madilld15250e2014-09-03 09:40:44 -0400994 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500995
996 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
997 {
Jamie Madillf2575982014-06-25 16:04:54 -0400998 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500999 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
1000
Jamie Madill54ad4f82014-09-03 09:40:46 -04001001 ASSERT(outputVariable.staticUse);
1002
Jamie Madill5f562732014-02-14 16:41:24 -05001003 if (outputVariable.arraySize > 0)
1004 {
1005 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
1006 {
1007 const int location = baseLocation + elementIndex;
1008 ASSERT(programOutputVars->count(location) == 0);
1009 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
1010 }
1011 }
1012 else
1013 {
1014 ASSERT(programOutputVars->count(baseLocation) == 0);
1015 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
1016 }
1017 }
1018}
1019
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001020std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001021{
1022 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001023 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001024 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001025}
1026
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001027std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001028{
1029 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001030 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001031 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1032
1033 std::string geomHLSL;
1034
Brandon Jones71620962014-08-20 14:04:59 -07001035 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001036 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -07001037 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1038 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001039
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001040 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1041 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1042 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001043
Geoff Langc0b9ef42014-07-02 10:02:37 -04001044 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001045 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1046 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001047 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1048 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001049 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001050 "static float2 pointSpriteCorners[] = \n"
1051 "{\n"
1052 " float2( 0.5f, -0.5f),\n"
1053 " float2( 0.5f, 0.5f),\n"
1054 " float2(-0.5f, -0.5f),\n"
1055 " float2(-0.5f, 0.5f)\n"
1056 "};\n"
1057 "\n"
1058 "static float2 pointSpriteTexcoords[] = \n"
1059 "{\n"
1060 " float2(1.0f, 1.0f),\n"
1061 " float2(1.0f, 0.0f),\n"
1062 " float2(0.0f, 1.0f),\n"
1063 " float2(0.0f, 0.0f)\n"
1064 "};\n"
1065 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001066 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1067 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001068 "\n"
1069 "[maxvertexcount(4)]\n"
1070 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1071 "{\n"
1072 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001073 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001074 " output.gl_PointSize = input[0].gl_PointSize;\n";
1075
1076 for (int r = 0; r < registers; r++)
1077 {
1078 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1079 }
1080
Brandon Jones71620962014-08-20 14:04:59 -07001081 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001082 {
1083 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1084 }
1085
1086 geomHLSL += " \n"
1087 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001088 " float4 dx_Position = input[0].dx_Position;\n"
1089 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001090
1091 for (int corner = 0; corner < 4; corner++)
1092 {
1093 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001094 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001095
Brandon Jones71620962014-08-20 14:04:59 -07001096 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001097 {
1098 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1099 }
1100
1101 geomHLSL += " outStream.Append(output);\n";
1102 }
1103
1104 geomHLSL += " \n"
1105 " outStream.RestartStrip();\n"
1106 "}\n";
1107
1108 return geomHLSL;
1109}
1110
1111// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001112std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001113{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001114 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001115 {
1116 return "_" + name;
1117 }
1118
1119 return name;
1120}
1121
Jamie Madillf2575982014-06-25 16:04:54 -04001122std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001123{
Jamie Madilla53ab512014-03-17 09:47:44 -04001124 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001125
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001126 // Matrix
1127 if (IsMatrixType(shaderAttrib.type))
1128 {
Jamie Madill8664b062014-02-14 16:41:29 -05001129 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001130 }
1131
Jamie Madillf2575982014-06-25 16:04:54 -04001132 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1133 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001134
Jamie Madill8664b062014-02-14 16:41:29 -05001135 // Perform integer to float conversion (if necessary)
1136 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1137
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001138 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001139 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001140 // TODO: normalization for 32-bit integer formats
1141 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1142 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001143 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001144
1145 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001146 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001147}
1148
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001149void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1150{
1151 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1152 {
1153 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1154
1155 if (vertexFormat.mType == GL_NONE)
1156 {
1157 signature[inputIndex] = GL_NONE;
1158 }
1159 else
1160 {
1161 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1162 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1163 }
1164 }
1165}
1166
Jamie Madill5f562732014-02-14 16:41:24 -05001167}