blob: 63f06ab456dfb47df37aab21a1d158b9bff507b8 [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 Madill3b7e2052014-03-17 09:47:43 -0400368 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
369
370 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madillf2575982014-06-25 16:04:54 -0400371 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500372
Jamie Madill8664b062014-02-14 16:41:29 -0500373 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500374 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400375 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500376 if (IsMatrixType(shaderAttribute.type))
377 {
378 // Matrix types are always transposed
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400379 structHLSL += " " + HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500380 }
381 else
382 {
383 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400384 structHLSL += " " + HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500385 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500386
Jamie Madilla53ab512014-03-17 09:47:44 -0400387 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400388 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500389
Jamie Madill3b7e2052014-03-17 09:47:43 -0400390 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400391 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500392
393 // Mismatched vertex attribute to vertex input may result in an undefined
394 // data reinterpretation (eg for pure integer->float, float->pure integer)
395 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400396 if (IsMatrixType(shaderAttribute.type) ||
397 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500398 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400399 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500400 }
401 else
402 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400403 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500404 }
405
Jamie Madill3b7e2052014-03-17 09:47:43 -0400406 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400407
Jamie Madillac0a2672014-04-11 13:33:56 -0400408 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
409 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500410 }
411
Geoff Lang04fb89a2014-06-09 15:05:36 -0400412 std::string replacementHLSL = "struct VS_INPUT\n"
413 "{\n" +
414 structHLSL +
415 "};\n"
416 "\n"
417 "void initAttributes(VS_INPUT input)\n"
418 "{\n" +
419 initHLSL +
420 "}\n";
421
422 std::string vertexHLSL(sourceShader);
423
424 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
425 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
426
427 return vertexHLSL;
428}
429
Jamie Madillf6be8d72014-09-05 10:38:07 -0400430std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOutputVariable> &outputVariables,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400431 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
432{
433 const int shaderModel = mRenderer->getMajorShaderModel();
434 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
435 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
436
437 std::string declarationHLSL;
438 std::string copyHLSL;
Geoff Lang4ace4232014-06-18 19:12:48 -0400439
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400440 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
441 {
442 GLenum binding = outputLayout[layoutIndex];
443
444 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400445 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400446 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
447
Jamie Madillf6be8d72014-09-05 10:38:07 -0400448 const PixelShaderOutputVariable &outputVariable = GetOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400449
450 declarationHLSL += " " + HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
451 " : " + targetSemantic + Str(layoutIndex) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400452
453 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
454 }
455 }
456
457 if (usesFragDepth)
458 {
459 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
460 copyHLSL += " output.gl_Depth = gl_Depth; \n";
461 }
462
463 std::string replacementHLSL = "struct PS_OUTPUT\n"
464 "{\n" +
465 declarationHLSL +
466 "};\n"
467 "\n"
468 "PS_OUTPUT generateOutput()\n"
469 "{\n"
470 " PS_OUTPUT output;\n" +
471 copyHLSL +
472 " return output;\n"
473 "}\n";
474
475 std::string pixelHLSL(sourceShader);
476
477 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
478 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
479
480 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500481}
482
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400483std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
484{
485 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
486 // In D3D11 we manually compute gl_PointCoord in the GS.
487 int shaderModel = mRenderer->getMajorShaderModel();
488 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
489}
490
491struct DynamicHLSL::SemanticInfo
492{
493 struct BuiltinInfo
494 {
495 BuiltinInfo()
496 : enabled(false),
497 index(0),
498 systemValue(false)
499 {}
500
501 bool enabled;
502 std::string semantic;
503 unsigned int index;
504 bool systemValue;
505
506 std::string str() const
507 {
508 return (systemValue ? semantic : (semantic + Str(index)));
509 }
510
511 void enableSystem(const std::string &systemValueSemantic)
512 {
513 enabled = true;
514 semantic = systemValueSemantic;
515 systemValue = true;
516 }
517
518 void enable(const std::string &semanticVal, unsigned int indexVal)
519 {
520 enabled = true;
521 semantic = semanticVal;
522 index = indexVal;
523 }
524 };
525
526 BuiltinInfo dxPosition;
527 BuiltinInfo glPosition;
528 BuiltinInfo glFragCoord;
529 BuiltinInfo glPointCoord;
530 BuiltinInfo glPointSize;
531};
532
533DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
534 bool pointSize, bool pixelShader) const
535{
536 SemanticInfo info;
537 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
538 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
539
540 int reservedRegisterIndex = startRegisters;
541
542 if (hlsl4)
543 {
544 info.dxPosition.enableSystem("SV_Position");
545 }
546 else if (pixelShader)
547 {
548 info.dxPosition.enableSystem("VPOS");
549 }
550 else
551 {
552 info.dxPosition.enableSystem("POSITION");
553 }
554
555 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
556
557 if (fragCoord)
558 {
559 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
560 }
561
562 if (pointCoord)
563 {
564 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
565 // In D3D11 we manually compute gl_PointCoord in the GS.
566 if (hlsl4)
567 {
568 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
569 }
570 else
571 {
572 info.glPointCoord.enable("TEXCOORD", 0);
573 }
574 }
575
576 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
577 if (pointSize && (!pixelShader || hlsl4))
578 {
579 info.glPointSize.enableSystem("PSIZE");
580 }
581
582 return info;
583}
584
585std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
586{
587 std::string linkHLSL = "{\n";
588
589 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
590
591 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
592 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
593
594 if (info.glFragCoord.enabled)
595 {
596 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
597 }
598
599 if (info.glPointCoord.enabled)
600 {
601 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
602 }
603
604 linkHLSL += varyingHLSL;
605
606 if (info.glPointSize.enabled)
607 {
608 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
609 }
610
611 linkHLSL += "};\n";
612
613 return linkHLSL;
614}
615
616void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
617 std::vector<LinkedVarying> *linkedVaryings) const
618{
619 ASSERT(info.glPosition.enabled);
620
621 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
622 info.glPosition.index, 1));
623
624 if (info.glFragCoord.enabled)
625 {
626 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
627 info.glFragCoord.index, 1));
628 }
629
630 if (info.glPointSize.enabled)
631 {
632 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
633 }
634}
635
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400636void DynamicHLSL::storeUserLinkedVaryings(const rx::ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400637 std::vector<LinkedVarying> *linkedVaryings) const
638{
Brandon Jones71620962014-08-20 14:04:59 -0700639 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400640 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400641
642 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
643 {
644 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400645
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400646 if (varying.registerAssigned())
647 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400648 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400649 GLenum transposedType = TransposeMatrixType(varying.type);
650 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
651
652 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
653 varyingSemantic, varying.registerIndex,
654 variableRows * varying.elementCount()));
655 }
656 }
657}
658
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400659bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500660 std::string& pixelHLSL, std::string& vertexHLSL,
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400661 rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500662 const std::vector<std::string>& transformFeedbackVaryings,
663 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400664 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400665 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400666 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500667{
668 if (pixelHLSL.empty() || vertexHLSL.empty())
669 {
670 return false;
671 }
672
Brandon Jones71620962014-08-20 14:04:59 -0700673 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
674 bool usesFragColor = fragmentShader->mUsesFragColor;
675 bool usesFragData = fragmentShader->mUsesFragData;
676 bool usesFragCoord = fragmentShader->mUsesFragCoord;
677 bool usesPointCoord = fragmentShader->mUsesPointCoord;
678 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400679
Jamie Madill5f562732014-02-14 16:41:24 -0500680 if (usesFragColor && usesFragData)
681 {
682 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
683 return false;
684 }
685
686 // Write the HLSL input/output declarations
687 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang3a61c322014-07-10 13:01:54 -0400688
689 // TODO (geofflang): Use context's caps
690 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Jamie Madill5f562732014-02-14 16:41:24 -0500691
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400692 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500693
694 // Two cases when writing to gl_FragColor and using ESSL 1.0:
695 // - with a 3.0 context, the output color is copied to channel 0
696 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jones71620962014-08-20 14:04:59 -0700697 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400698 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500699
Brandon Jones71620962014-08-20 14:04:59 -0700700 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500701
702 if (registersNeeded > maxVaryingVectors)
703 {
704 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500705 return false;
706 }
707
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400708 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
709 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
710 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500711
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400712 storeUserLinkedVaryings(vertexShader, linkedVaryings);
713 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500714
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500715 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400716 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
717 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500718 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500719 "{\n"
720 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500721
722 if (shaderModel >= 4)
723 {
724 vertexHLSL += "\n"
725 " gl_main();\n"
726 "\n"
727 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500728 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400729 " output.dx_Position.x = gl_Position.x;\n"
730 " output.dx_Position.y = -gl_Position.y;\n"
731 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
732 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500733 }
734 else
735 {
736 vertexHLSL += "\n"
737 " gl_main();\n"
738 "\n"
739 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500740 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400741 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
742 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
743 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
744 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500745 }
746
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400747 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500748 {
749 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
750 }
751
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400752 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500753 {
754 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
755 }
756
Jamie Madilld15250e2014-09-03 09:40:44 -0400757 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
758 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500759 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400760 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400761 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500762 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400763 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500764 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400765 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500766
767 for (int row = 0; row < variableRows; row++)
768 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400769 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500770 vertexHLSL += " output.v" + Str(r);
771
772 bool sharedRegister = false; // Register used by multiple varyings
773
774 for (int x = 0; x < 4; x++)
775 {
776 if (packing[r][x] && packing[r][x] != packing[r][0])
777 {
778 sharedRegister = true;
779 break;
780 }
781 }
782
783 if(sharedRegister)
784 {
785 vertexHLSL += ".";
786
787 for (int x = 0; x < 4; x++)
788 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400789 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500790 {
791 switch(x)
792 {
793 case 0: vertexHLSL += "x"; break;
794 case 1: vertexHLSL += "y"; break;
795 case 2: vertexHLSL += "z"; break;
796 case 3: vertexHLSL += "w"; break;
797 }
798 }
799 }
800 }
801
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400802 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500803
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400804 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500805 {
806 vertexHLSL += ArrayString(elementIndex);
807 }
808
809 if (variableRows > 1)
810 {
811 vertexHLSL += ArrayString(row);
812 }
813
814 vertexHLSL += ";\n";
815 }
816 }
817 }
818 }
819
820 vertexHLSL += "\n"
821 " return output;\n"
822 "}\n";
823
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400824 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
825 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500826
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400827 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500828
829 if (shaderVersion < 300)
830 {
831 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
832 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400833 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400834 outputKeyVariable.type = GL_FLOAT_VEC4;
835 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
836 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
837 outputKeyVariable.outputIndex = renderTargetIndex;
838
839 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500840 }
841
Brandon Jones71620962014-08-20 14:04:59 -0700842 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500843 }
844 else
845 {
846 defineOutputVariables(fragmentShader, programOutputVars);
847
Jamie Madilld15250e2014-09-03 09:40:44 -0400848 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500849 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
850 {
851 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400852 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400853 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500854 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
855
Jamie Madill54ad4f82014-09-03 09:40:46 -0400856 ASSERT(outputVariable.staticUse);
857
Jamie Madillf6be8d72014-09-05 10:38:07 -0400858 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400859 outputKeyVariable.type = outputVariable.type;
860 outputKeyVariable.name = variableName + elementString;
861 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
862 outputKeyVariable.outputIndex = locationIt->first;
863
864 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500865 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400866
867 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500868 }
869
Geoff Lang04fb89a2014-06-09 15:05:36 -0400870 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500871
Brandon Jones71620962014-08-20 14:04:59 -0700872 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500873 {
874 if (shaderModel >= 4)
875 {
876 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
877 "{\n";
878 }
879 else
880 {
881 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
882 "{\n";
883 }
884 }
885 else
886 {
887 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
888 "{\n";
889 }
890
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400891 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500892 {
893 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
894
895 if (shaderModel >= 4)
896 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400897 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
898 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500899 }
900 else if (shaderModel >= 3)
901 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400902 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
903 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500904 }
905 else
906 {
907 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
908 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
909 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
910 }
911
912 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
913 " gl_FragCoord.w = rhw;\n";
914 }
915
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400916 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500917 {
918 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
919 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
920 }
921
Brandon Jones71620962014-08-20 14:04:59 -0700922 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500923 {
924 if (shaderModel <= 3)
925 {
926 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
927 }
928 else
929 {
930 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
931 }
932 }
933
Jamie Madilld15250e2014-09-03 09:40:44 -0400934 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
935 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500936 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400937 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400938 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500939 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400940 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400941 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500942 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400943 GLenum transposedType = TransposeMatrixType(varying.type);
944 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500945 for (int row = 0; row < variableRows; row++)
946 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400947 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
948 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500949
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400950 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500951 {
952 pixelHLSL += ArrayString(elementIndex);
953 }
954
955 if (variableRows > 1)
956 {
957 pixelHLSL += ArrayString(row);
958 }
959
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400960 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500961 {
962 pixelHLSL += " = input.v" + n + ";\n"; break;
963 }
964 else
965 {
966 switch (VariableColumnCount(transposedType))
967 {
968 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
969 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
970 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
971 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
972 default: UNREACHABLE();
973 }
974 }
975 }
976 }
977 }
Jamie Madill54ad4f82014-09-03 09:40:46 -0400978 else
979 {
980 ASSERT(varying.isBuiltIn() || !varying.staticUse);
981 }
Jamie Madill5f562732014-02-14 16:41:24 -0500982 }
983
984 pixelHLSL += "\n"
985 " gl_main();\n"
986 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400987 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500988 "}\n";
989
990 return true;
991}
992
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400993void DynamicHLSL::defineOutputVariables(rx::ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500994{
Jamie Madilld15250e2014-09-03 09:40:44 -0400995 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500996
997 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
998 {
Jamie Madillf2575982014-06-25 16:04:54 -0400999 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -05001000 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
1001
Jamie Madill54ad4f82014-09-03 09:40:46 -04001002 ASSERT(outputVariable.staticUse);
1003
Jamie Madill5f562732014-02-14 16:41:24 -05001004 if (outputVariable.arraySize > 0)
1005 {
1006 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
1007 {
1008 const int location = baseLocation + elementIndex;
1009 ASSERT(programOutputVars->count(location) == 0);
1010 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
1011 }
1012 }
1013 else
1014 {
1015 ASSERT(programOutputVars->count(baseLocation) == 0);
1016 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
1017 }
1018 }
1019}
1020
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001021std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001022{
1023 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001024 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001025 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001026}
1027
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001028std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001029{
1030 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001031 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001032 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1033
1034 std::string geomHLSL;
1035
Brandon Jones71620962014-08-20 14:04:59 -07001036 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001037 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -07001038 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1039 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001040
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001041 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1042 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1043 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001044
Geoff Langc0b9ef42014-07-02 10:02:37 -04001045 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001046 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1047 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001048 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1049 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001050 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001051 "static float2 pointSpriteCorners[] = \n"
1052 "{\n"
1053 " float2( 0.5f, -0.5f),\n"
1054 " float2( 0.5f, 0.5f),\n"
1055 " float2(-0.5f, -0.5f),\n"
1056 " float2(-0.5f, 0.5f)\n"
1057 "};\n"
1058 "\n"
1059 "static float2 pointSpriteTexcoords[] = \n"
1060 "{\n"
1061 " float2(1.0f, 1.0f),\n"
1062 " float2(1.0f, 0.0f),\n"
1063 " float2(0.0f, 1.0f),\n"
1064 " float2(0.0f, 0.0f)\n"
1065 "};\n"
1066 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001067 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1068 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001069 "\n"
1070 "[maxvertexcount(4)]\n"
1071 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1072 "{\n"
1073 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001074 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001075 " output.gl_PointSize = input[0].gl_PointSize;\n";
1076
1077 for (int r = 0; r < registers; r++)
1078 {
1079 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1080 }
1081
Brandon Jones71620962014-08-20 14:04:59 -07001082 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001083 {
1084 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1085 }
1086
1087 geomHLSL += " \n"
1088 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001089 " float4 dx_Position = input[0].dx_Position;\n"
1090 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001091
1092 for (int corner = 0; corner < 4; corner++)
1093 {
1094 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001095 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001096
Brandon Jones71620962014-08-20 14:04:59 -07001097 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001098 {
1099 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1100 }
1101
1102 geomHLSL += " outStream.Append(output);\n";
1103 }
1104
1105 geomHLSL += " \n"
1106 " outStream.RestartStrip();\n"
1107 "}\n";
1108
1109 return geomHLSL;
1110}
1111
1112// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001113std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001114{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001115 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001116 {
1117 return "_" + name;
1118 }
1119
1120 return name;
1121}
1122
Jamie Madillf2575982014-06-25 16:04:54 -04001123std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001124{
Jamie Madilla53ab512014-03-17 09:47:44 -04001125 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001126
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001127 // Matrix
1128 if (IsMatrixType(shaderAttrib.type))
1129 {
Jamie Madill8664b062014-02-14 16:41:29 -05001130 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001131 }
1132
Jamie Madillf2575982014-06-25 16:04:54 -04001133 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1134 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001135
Jamie Madill8664b062014-02-14 16:41:29 -05001136 // Perform integer to float conversion (if necessary)
1137 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1138
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001139 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001140 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001141 // TODO: normalization for 32-bit integer formats
1142 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1143 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001144 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001145
1146 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001147 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001148}
1149
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001150void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1151{
1152 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1153 {
1154 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1155
1156 if (vertexFormat.mType == GL_NONE)
1157 {
1158 signature[inputIndex] = GL_NONE;
1159 }
1160 else
1161 {
1162 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1163 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1164 }
1165 }
1166}
1167
Jamie Madill5f562732014-02-14 16:41:24 -05001168}