blob: 5f75902a56adbe886106f803d3c7acb44d080419 [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
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050010
11#include "common/utilities.h"
12#include "compiler/translator/blocklayout.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/renderer/d3d/ShaderD3D.h"
14#include "libANGLE/renderer/d3d/RendererD3D.h"
15#include "libANGLE/Program.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050016#include "libANGLE/Shader.h"
17#include "libANGLE/formatutils.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040018
Jamie Madill53cb14d2014-07-08 15:02:35 -040019// For use with ArrayString, see angleutils.h
20META_ASSERT(GL_INVALID_INDEX == UINT_MAX);
Jamie Madill5f562732014-02-14 16:41:24 -050021
Brandon Jonesd8d72432014-08-22 15:11:23 -070022using namespace gl;
23
Jamie Madill30d6c252014-11-13 10:03:33 -050024namespace rx
25{
26
Jamie Madill3f2e61d2014-09-05 10:38:05 -040027namespace
Jamie Madill5f562732014-02-14 16:41:24 -050028{
Jamie Madill8664b062014-02-14 16:41:29 -050029
30std::string HLSLComponentTypeString(GLenum componentType)
31{
32 switch (componentType)
33 {
34 case GL_UNSIGNED_INT: return "uint";
35 case GL_INT: return "int";
36 case GL_UNSIGNED_NORMALIZED:
37 case GL_SIGNED_NORMALIZED:
38 case GL_FLOAT: return "float";
39 default: UNREACHABLE(); return "not-component-type";
40 }
Jamie Madill5f562732014-02-14 16:41:24 -050041}
42
Jamie Madill8664b062014-02-14 16:41:29 -050043std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
44{
45 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
46}
47
48std::string HLSLMatrixTypeString(GLenum type)
49{
50 switch (type)
51 {
52 case GL_FLOAT_MAT2: return "float2x2";
53 case GL_FLOAT_MAT3: return "float3x3";
54 case GL_FLOAT_MAT4: return "float4x4";
55 case GL_FLOAT_MAT2x3: return "float2x3";
56 case GL_FLOAT_MAT3x2: return "float3x2";
57 case GL_FLOAT_MAT2x4: return "float2x4";
58 case GL_FLOAT_MAT4x2: return "float4x2";
59 case GL_FLOAT_MAT3x4: return "float3x4";
60 case GL_FLOAT_MAT4x3: return "float4x3";
61 default: UNREACHABLE(); return "not-matrix-type";
62 }
63}
64
65std::string HLSLTypeString(GLenum type)
66{
67 if (gl::IsMatrixType(type))
68 {
69 return HLSLMatrixTypeString(type);
70 }
71
Jamie Madillf2575982014-06-25 16:04:54 -040072 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050073}
74
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +000075const PixelShaderOutputVariable *FindOutputAtLocation(const std::vector<PixelShaderOutputVariable> &outputVariables,
Jamie Madill3f2e61d2014-09-05 10:38:05 -040076 unsigned int location)
77{
78 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
79 {
80 if (outputVariables[variableIndex].outputIndex == location)
81 {
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +000082 return &outputVariables[variableIndex];
Jamie Madill3f2e61d2014-09-05 10:38:05 -040083 }
84 }
85
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +000086 return NULL;
Jamie Madill3f2e61d2014-09-05 10:38:05 -040087}
88
Geoff Lang04fb89a2014-06-09 15:05:36 -040089const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
90const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050091
Jamie Madill30d6c252014-11-13 10:03:33 -050092}
93
Jamie Madill93e13fb2014-11-06 15:27:25 -050094DynamicHLSL::DynamicHLSL(RendererD3D *const renderer)
Jamie Madill5f562732014-02-14 16:41:24 -050095 : mRenderer(renderer)
96{
97}
98
Jamie Madillff0d2ba2014-05-14 13:49:10 -040099static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -0500100{
Geoff Lang48dcae72014-02-05 16:28:24 -0500101 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -0500102
Geoff Lang48dcae72014-02-05 16:28:24 -0500103 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -0400104 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -0500105 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500106
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400107 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -0500108 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400109 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -0500110 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500111 bool available = true;
112
113 for (int y = 0; y < registers && available; y++)
114 {
115 for (int x = 0; x < elements && available; x++)
116 {
117 if (packing[r + y][x])
118 {
119 available = false;
120 }
121 }
122 }
123
124 if (available)
125 {
126 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700127 varying->columnIndex = 0;
Geoff Lang48dcae72014-02-05 16:28:24 -0500128
129 for (int y = 0; y < registers; y++)
130 {
131 for (int x = 0; x < elements; x++)
132 {
133 packing[r + y][x] = &*varying;
134 }
135 }
136
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400137 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500138 }
139 }
140
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400141 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500142 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400143 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500144 {
145 bool available = true;
146
147 for (int y = 0; y < registers && available; y++)
148 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500149 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500150 {
151 if (packing[r + y][x])
152 {
153 available = false;
154 }
155 }
156 }
157
158 if (available)
159 {
160 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700161 varying->columnIndex = 2;
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 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700192 if (space[x] >= registers && (space[column] < registers || space[x] < space[column]))
Geoff Lang48dcae72014-02-05 16:28:24 -0500193 {
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;
Austin Kinrossaf875522014-08-25 21:06:07 -0700205 varying->columnIndex = column;
Geoff Lang48dcae72014-02-05 16:28:24 -0500206
207 for (int y = r; y < r + registers; y++)
208 {
209 packing[y][column] = &*varying;
210 }
211
212 break;
213 }
214 }
215
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400216 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500217 }
218 }
219 else UNREACHABLE();
220
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400221 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500222}
223
224// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
225// Returns the number of used varying registers, or -1 if unsuccesful
Jamie Madill30d6c252014-11-13 10:03:33 -0500226int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, ShaderD3D *fragmentShader,
227 ShaderD3D *vertexShader, const std::vector<std::string> &transformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -0500228{
Geoff Lang3a61c322014-07-10 13:01:54 -0400229 // TODO (geofflang): Use context's caps
230 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Geoff Lang48dcae72014-02-05 16:28:24 -0500231
Brandon Jones71620962014-08-20 14:04:59 -0700232 vertexShader->resetVaryingsRegisterAssignment();
233 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500234
235 std::set<std::string> packedVaryings;
236
Jamie Madilld15250e2014-09-03 09:40:44 -0400237 std::vector<gl::PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
238 std::vector<gl::PackedVarying> &vertexVaryings = vertexShader->getVaryings();
239 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500240 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400241 PackedVarying *varying = &fragmentVaryings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400242
243 // Do not assign registers to built-in or unreferenced varyings
244 if (varying->isBuiltIn() || !varying->staticUse)
245 {
246 continue;
247 }
248
Geoff Lang48dcae72014-02-05 16:28:24 -0500249 if (packVarying(varying, maxVaryingVectors, packing))
250 {
251 packedVaryings.insert(varying->name);
252 }
253 else
Jamie Madill5f562732014-02-14 16:41:24 -0500254 {
255 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500256 return -1;
257 }
258 }
259
Geoff Lang48dcae72014-02-05 16:28:24 -0500260 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
261 {
262 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
Jamie Madill55d611e2014-10-24 16:28:14 -0400263
264 if (transformFeedbackVarying == "gl_Position" || transformFeedbackVarying == "gl_PointSize")
265 {
266 // do not pack builtin XFB varyings
267 continue;
268 }
269
Geoff Lang48dcae72014-02-05 16:28:24 -0500270 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
271 {
272 bool found = false;
Jamie Madilld15250e2014-09-03 09:40:44 -0400273 for (unsigned int varyingIndex = 0; varyingIndex < vertexVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500274 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400275 PackedVarying *varying = &vertexVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500276 if (transformFeedbackVarying == varying->name)
277 {
278 if (!packVarying(varying, maxVaryingVectors, packing))
279 {
280 infoLog.append("Could not pack varying %s", varying->name.c_str());
281 return -1;
282 }
283
284 found = true;
285 break;
286 }
287 }
288
Jamie Madill55d611e2014-10-24 16:28:14 -0400289 if (!found)
Geoff Lang48dcae72014-02-05 16:28:24 -0500290 {
291 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
292 return -1;
293 }
294 }
295 }
296
Jamie Madill5f562732014-02-14 16:41:24 -0500297 // Return the number of used registers
298 int registers = 0;
299
300 for (int r = 0; r < maxVaryingVectors; r++)
301 {
302 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
303 {
304 registers++;
305 }
306 }
307
308 return registers;
309}
310
Jamie Madill54ad4f82014-09-03 09:40:46 -0400311std::string DynamicHLSL::generateVaryingHLSL(const ShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500312{
Brandon Jones71620962014-08-20 14:04:59 -0700313 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500314 std::string varyingHLSL;
315
Jamie Madill54ad4f82014-09-03 09:40:46 -0400316 const std::vector<gl::PackedVarying> &varyings = shader->getVaryings();
317
Jamie Madilld15250e2014-09-03 09:40:44 -0400318 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500319 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400320 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400321 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500322 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400323 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400324 GLenum transposedType = TransposeMatrixType(varying.type);
325 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500326
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400327 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500328 {
Jamie Madill5f562732014-02-14 16:41:24 -0500329 for (int row = 0; row < variableRows; row++)
330 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700331 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many registers being used.
332 // For example, if there are N registers, and we have N vec3 varyings and 1 float varying, then D3D will pack them into N registers.
333 // If the float varying has the 'nointerpolation' modifier on it then we would need N + 1 registers, and D3D compilation will fail.
334
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400335 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500336 {
Jamie Madillf2575982014-06-25 16:04:54 -0400337 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
338 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
339 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500340 default: UNREACHABLE();
341 }
342
Austin Kinrossaf875522014-08-25 21:06:07 -0700343 unsigned int semanticIndex = elementIndex * variableRows + varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500344 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500345
Jamie Madilla53ab512014-03-17 09:47:44 -0400346 std::string typeString;
347
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400348 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400349 {
350 // matrices within structs are not transposed, so
351 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400352 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400353 }
354 else
355 {
Jamie Madillf2575982014-06-25 16:04:54 -0400356 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400357 int columnCount = VariableColumnCount(transposedType);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400358 typeString = HLSLComponentTypeString(componentType, columnCount);
Jamie Madilla53ab512014-03-17 09:47:44 -0400359 }
Jamie Madill5f562732014-02-14 16:41:24 -0500360 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
361 }
362 }
363 }
Jamie Madill5f562732014-02-14 16:41:24 -0500364 }
365
366 return varyingHLSL;
367}
368
Jamie Madillf2575982014-06-25 16:04:54 -0400369std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
370 const VertexFormat inputLayout[],
371 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500372{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400373 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500374
375 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400376 unsigned int inputIndex = 0;
377
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500378 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
379 {
Jamie Madillf2575982014-06-25 16:04:54 -0400380 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500381 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500382 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400383 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
384 const VertexFormat &vertexFormat = inputLayout[inputIndex];
385
Jamie Madill3b7e2052014-03-17 09:47:43 -0400386 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500387 if (IsMatrixType(shaderAttribute.type))
388 {
389 // Matrix types are always transposed
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400390 structHLSL += " " + HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500391 }
392 else
393 {
394 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000395
396 if (shaderAttribute.name == "gl_InstanceID")
397 {
398 // The input type of the instance ID in HLSL (uint) differs from the one in ESSL (int).
399 structHLSL += " uint";
400 }
401 else
402 {
403 structHLSL += " " + HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
404 }
Jamie Madill8664b062014-02-14 16:41:29 -0500405 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500406
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000407 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : ";
408
409 if (shaderAttribute.name == "gl_InstanceID")
410 {
411 structHLSL += "SV_InstanceID";
412 }
413 else
414 {
415 structHLSL += "TEXCOORD" + Str(semanticIndex);
416 semanticIndex += VariableRegisterCount(shaderAttribute.type);
417 }
418
419 structHLSL += ";\n";
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500420
Jamie Madill3b7e2052014-03-17 09:47:43 -0400421 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400422 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500423
424 // Mismatched vertex attribute to vertex input may result in an undefined
425 // data reinterpretation (eg for pure integer->float, float->pure integer)
426 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400427 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madill30d6c252014-11-13 10:03:33 -0500428 (mRenderer->getVertexConversionType(vertexFormat) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500429 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400430 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500431 }
432 else
433 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400434 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500435 }
436
Jamie Madill3b7e2052014-03-17 09:47:43 -0400437 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400438
Jamie Madillac0a2672014-04-11 13:33:56 -0400439 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
440 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500441 }
442
Cooper Partine6664f02015-01-09 16:22:24 -0800443 // If gl_PointSize is used in the shader then pointsprites rendering is expected.
444 // If the renderer does not support Geometry shaders then Instanced PointSprite emulation
445 // may must be used.
446 bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos;
447 bool useInstancedPointSpriteEmulation = usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
448
449 // Instanced PointSprite emulation requires additional entries in the
450 // VS_INPUT structure to support the vertices that make up the quad vertices.
451 // These values must be in sync with the cooresponding values added during inputlayout creation
452 // in InputLayoutCache::applyVertexBuffers().
453 if (useInstancedPointSpriteEmulation)
454 {
455 structHLSL += " float3 spriteVertexPos : SPRITEPOSITION0;\n";
456 structHLSL += " float2 spriteTexCoord : SPRITETEXCOORD0;\n";
457 }
458
Geoff Lang04fb89a2014-06-09 15:05:36 -0400459 std::string replacementHLSL = "struct VS_INPUT\n"
460 "{\n" +
461 structHLSL +
462 "};\n"
463 "\n"
464 "void initAttributes(VS_INPUT input)\n"
465 "{\n" +
466 initHLSL +
467 "}\n";
468
469 std::string vertexHLSL(sourceShader);
470
471 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
472 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
473
474 return vertexHLSL;
475}
476
Jamie Madillf6be8d72014-09-05 10:38:07 -0400477std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOutputVariable> &outputVariables,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400478 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
479{
480 const int shaderModel = mRenderer->getMajorShaderModel();
481 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
482 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
483
484 std::string declarationHLSL;
485 std::string copyHLSL;
Geoff Lang4ace4232014-06-18 19:12:48 -0400486
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400487 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
488 {
489 GLenum binding = outputLayout[layoutIndex];
490
491 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400492 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400493 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
494
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000495 const PixelShaderOutputVariable *outputVariable = FindOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400496
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000497 // OpenGL ES 3.0 spec $4.2.1
498 // If [...] not all user-defined output variables are written, the values of fragment colors
499 // corresponding to unwritten variables are similarly undefined.
500 if (outputVariable)
501 {
502 declarationHLSL += " " + HLSLTypeString(outputVariable->type) + " " + outputVariable->name +
503 " : " + targetSemantic + Str(layoutIndex) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400504
Gregoire Payen de La Garanderiee1728542015-01-07 11:31:54 +0000505 copyHLSL += " output." + outputVariable->name + " = " + outputVariable->source + ";\n";
506 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400507 }
508 }
509
510 if (usesFragDepth)
511 {
512 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
513 copyHLSL += " output.gl_Depth = gl_Depth; \n";
514 }
515
516 std::string replacementHLSL = "struct PS_OUTPUT\n"
517 "{\n" +
518 declarationHLSL +
519 "};\n"
520 "\n"
521 "PS_OUTPUT generateOutput()\n"
522 "{\n"
523 " PS_OUTPUT output;\n" +
524 copyHLSL +
525 " return output;\n"
526 "}\n";
527
528 std::string pixelHLSL(sourceShader);
529
530 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
531 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
532
533 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500534}
535
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400536std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
537{
538 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
539 // In D3D11 we manually compute gl_PointCoord in the GS.
540 int shaderModel = mRenderer->getMajorShaderModel();
541 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
542}
543
544struct DynamicHLSL::SemanticInfo
545{
546 struct BuiltinInfo
547 {
548 BuiltinInfo()
549 : enabled(false),
550 index(0),
551 systemValue(false)
552 {}
553
554 bool enabled;
555 std::string semantic;
556 unsigned int index;
557 bool systemValue;
558
559 std::string str() const
560 {
561 return (systemValue ? semantic : (semantic + Str(index)));
562 }
563
564 void enableSystem(const std::string &systemValueSemantic)
565 {
566 enabled = true;
567 semantic = systemValueSemantic;
568 systemValue = true;
569 }
570
571 void enable(const std::string &semanticVal, unsigned int indexVal)
572 {
573 enabled = true;
574 semantic = semanticVal;
575 index = indexVal;
576 }
577 };
578
579 BuiltinInfo dxPosition;
580 BuiltinInfo glPosition;
581 BuiltinInfo glFragCoord;
582 BuiltinInfo glPointCoord;
583 BuiltinInfo glPointSize;
584};
585
586DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
587 bool pointSize, bool pixelShader) const
588{
589 SemanticInfo info;
590 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
591 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
592
593 int reservedRegisterIndex = startRegisters;
594
595 if (hlsl4)
596 {
597 info.dxPosition.enableSystem("SV_Position");
598 }
599 else if (pixelShader)
600 {
601 info.dxPosition.enableSystem("VPOS");
602 }
603 else
604 {
605 info.dxPosition.enableSystem("POSITION");
606 }
607
608 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
609
610 if (fragCoord)
611 {
612 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
613 }
614
615 if (pointCoord)
616 {
617 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
618 // In D3D11 we manually compute gl_PointCoord in the GS.
619 if (hlsl4)
620 {
621 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
622 }
623 else
624 {
625 info.glPointCoord.enable("TEXCOORD", 0);
626 }
627 }
628
629 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
630 if (pointSize && (!pixelShader || hlsl4))
631 {
632 info.glPointSize.enableSystem("PSIZE");
633 }
634
635 return info;
636}
637
638std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
639{
640 std::string linkHLSL = "{\n";
641
642 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
643
644 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
645 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
646
647 if (info.glFragCoord.enabled)
648 {
649 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
650 }
651
652 if (info.glPointCoord.enabled)
653 {
654 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
655 }
656
657 linkHLSL += varyingHLSL;
658
659 if (info.glPointSize.enabled)
660 {
661 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
662 }
663
664 linkHLSL += "};\n";
665
666 return linkHLSL;
667}
668
669void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
670 std::vector<LinkedVarying> *linkedVaryings) const
671{
672 ASSERT(info.glPosition.enabled);
673
674 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
675 info.glPosition.index, 1));
676
677 if (info.glFragCoord.enabled)
678 {
679 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
680 info.glFragCoord.index, 1));
681 }
682
683 if (info.glPointSize.enabled)
684 {
685 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
686 }
687}
688
Jamie Madill30d6c252014-11-13 10:03:33 -0500689void DynamicHLSL::storeUserLinkedVaryings(const ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400690 std::vector<LinkedVarying> *linkedVaryings) const
691{
Brandon Jones71620962014-08-20 14:04:59 -0700692 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400693 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400694
695 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
696 {
697 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400698
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400699 if (varying.registerAssigned())
700 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400701 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400702 GLenum transposedType = TransposeMatrixType(varying.type);
703 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
704
705 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
706 varyingSemantic, varying.registerIndex,
707 variableRows * varying.elementCount()));
708 }
709 }
710}
711
Jamie Madillde8892b2014-11-11 13:00:22 -0500712bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data, InfoLog &infoLog, int registers,
713 const VaryingPacking packing,
714 std::string &pixelHLSL, std::string &vertexHLSL,
Jamie Madill30d6c252014-11-13 10:03:33 -0500715 ShaderD3D *fragmentShader, ShaderD3D *vertexShader,
Jamie Madillde8892b2014-11-11 13:00:22 -0500716 const std::vector<std::string> &transformFeedbackVaryings,
Geoff Lang48dcae72014-02-05 16:28:24 -0500717 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400718 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400719 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400720 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500721{
722 if (pixelHLSL.empty() || vertexHLSL.empty())
723 {
724 return false;
725 }
726
Brandon Jones71620962014-08-20 14:04:59 -0700727 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
728 bool usesFragColor = fragmentShader->mUsesFragColor;
729 bool usesFragData = fragmentShader->mUsesFragData;
730 bool usesFragCoord = fragmentShader->mUsesFragCoord;
731 bool usesPointCoord = fragmentShader->mUsesPointCoord;
732 bool usesPointSize = vertexShader->mUsesPointSize;
Cooper Partine6664f02015-01-09 16:22:24 -0800733 bool useInstancedPointSpriteEmulation = usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400734
Jamie Madill5f562732014-02-14 16:41:24 -0500735 if (usesFragColor && usesFragData)
736 {
737 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
738 return false;
739 }
740
741 // Write the HLSL input/output declarations
742 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400743 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500744
745 // Two cases when writing to gl_FragColor and using ESSL 1.0:
746 // - with a 3.0 context, the output color is copied to channel 0
747 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madillde8892b2014-11-11 13:00:22 -0500748 const bool broadcast = (fragmentShader->mUsesFragColor && data.clientVersion < 3);
749 const unsigned int numRenderTargets = (broadcast || usesMRT ? data.caps->maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500750
Brandon Jones71620962014-08-20 14:04:59 -0700751 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500752
Jamie Madillde8892b2014-11-11 13:00:22 -0500753 if (static_cast<GLuint>(registersNeeded) > data.caps->maxVaryingVectors)
Jamie Madill5f562732014-02-14 16:41:24 -0500754 {
755 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500756 return false;
757 }
758
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400759 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
Cooper Partine6664f02015-01-09 16:22:24 -0800760
761 // Instanced PointSprite emulation requires that gl_PointCoord is present in the vertex shader VS_OUTPUT
762 // structure to ensure compatibility with the generated PS_INPUT of the pixel shader.
763 // GeometryShader PointSprite emulation does not require this additional entry because the
764 // GS_OUTPUT of the Geometry shader contains the pointCoord value and already matches the PS_INPUT of the
765 // generated pixel shader.
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400766 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
Cooper Partine6664f02015-01-09 16:22:24 -0800767 (useInstancedPointSpriteEmulation && usesPointCoord),
768 usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500769
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400770 storeUserLinkedVaryings(vertexShader, linkedVaryings);
771 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500772
Cooper Partine6664f02015-01-09 16:22:24 -0800773 // Instanced PointSprite emulation requires additional entries originally generated in the
774 // GeometryShader HLSL. These include pointsize clamp values.
775 if (useInstancedPointSpriteEmulation)
776 {
777 vertexHLSL += "static float minPointSize = " + Str((int)mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
778 "static float maxPointSize = " + Str((int)mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n";
779 }
780
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500781 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400782 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
783 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500784 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500785 "{\n"
786 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500787
Jamie Madill37997142015-01-28 10:06:34 -0500788 if (vertexShader->usesDeferredInit())
789 {
790 vertexHLSL += "\n"
791 " initializeDeferredGlobals();\n";
792 }
793
794 vertexHLSL += "\n"
795 " gl_main();\n"
796 "\n"
797 " VS_OUTPUT output;\n"
798 " output.gl_Position = gl_Position;\n";
799
Austin Kinross4fd18b12014-12-22 12:32:05 -0800800 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
801 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500802 {
Jamie Madill37997142015-01-28 10:06:34 -0500803 vertexHLSL += " output.dx_Position.x = gl_Position.x;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400804 " output.dx_Position.y = -gl_Position.y;\n"
805 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
806 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500807 }
808 else
809 {
Jamie Madill37997142015-01-28 10:06:34 -0500810 vertexHLSL += " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400811 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
812 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
813 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500814 }
815
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400816 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500817 {
818 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
819 }
820
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400821 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500822 {
823 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
824 }
825
Jamie Madilld15250e2014-09-03 09:40:44 -0400826 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
827 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500828 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400829 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400830 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500831 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400832 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500833 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400834 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500835
836 for (int row = 0; row < variableRows; row++)
837 {
Jamie Madillde8892b2014-11-11 13:00:22 -0500838 int r = varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500839 vertexHLSL += " output.v" + Str(r);
840
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400841 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500842
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400843 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500844 {
845 vertexHLSL += ArrayString(elementIndex);
846 }
847
848 if (variableRows > 1)
849 {
850 vertexHLSL += ArrayString(row);
851 }
852
853 vertexHLSL += ";\n";
854 }
855 }
856 }
857 }
858
Cooper Partine6664f02015-01-09 16:22:24 -0800859 // Instanced PointSprite emulation requires additional entries to calculate
860 // the final output vertex positions of the quad that represents each sprite.
861 if (useInstancedPointSpriteEmulation)
862 {
863 vertexHLSL += "\n"
864 " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"
865 " output.dx_Position.xyz += float3(input.spriteVertexPos.x * gl_PointSize / (dx_ViewCoords.x*2), input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2), input.spriteVertexPos.z) * output.dx_Position.w;\n"
866 " output.gl_PointSize = gl_PointSize;\n";
867
868 if (usesPointCoord)
869 {
870 vertexHLSL += "\n"
871 " output.gl_PointCoord = input.spriteTexCoord;\n";
872 }
873 }
874
Jamie Madill5f562732014-02-14 16:41:24 -0500875 vertexHLSL += "\n"
876 " return output;\n"
877 "}\n";
878
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400879 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
880 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500881
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400882 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500883
884 if (shaderVersion < 300)
885 {
886 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
887 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400888 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400889 outputKeyVariable.type = GL_FLOAT_VEC4;
890 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
891 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
892 outputKeyVariable.outputIndex = renderTargetIndex;
893
894 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500895 }
896
Brandon Jones71620962014-08-20 14:04:59 -0700897 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500898 }
899 else
900 {
901 defineOutputVariables(fragmentShader, programOutputVars);
902
Jamie Madilld15250e2014-09-03 09:40:44 -0400903 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500904 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
905 {
906 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400907 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400908 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500909 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
910
Jamie Madill54ad4f82014-09-03 09:40:46 -0400911 ASSERT(outputVariable.staticUse);
912
Jamie Madillf6be8d72014-09-05 10:38:07 -0400913 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400914 outputKeyVariable.type = outputVariable.type;
915 outputKeyVariable.name = variableName + elementString;
916 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
917 outputKeyVariable.outputIndex = locationIt->first;
918
919 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500920 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400921
922 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500923 }
924
Geoff Lang04fb89a2014-06-09 15:05:36 -0400925 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500926
Brandon Jones71620962014-08-20 14:04:59 -0700927 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500928 {
929 if (shaderModel >= 4)
930 {
931 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
932 "{\n";
933 }
934 else
935 {
936 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
937 "{\n";
938 }
939 }
940 else
941 {
942 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
943 "{\n";
944 }
945
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400946 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500947 {
948 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
949
Austin Kinross588434c2014-12-10 10:41:45 -0800950 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
951 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using dx_ViewCoords.
952 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500953 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400954 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
955 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500956 }
Austin Kinross588434c2014-12-10 10:41:45 -0800957 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500958 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400959 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
960 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500961 }
962 else
963 {
964 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
965 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
966 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
967 }
968
969 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
970 " gl_FragCoord.w = rhw;\n";
971 }
972
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400973 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500974 {
975 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
976 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
977 }
978
Brandon Jones71620962014-08-20 14:04:59 -0700979 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500980 {
981 if (shaderModel <= 3)
982 {
983 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
984 }
985 else
986 {
987 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
988 }
989 }
990
Jamie Madilld15250e2014-09-03 09:40:44 -0400991 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
992 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500993 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400994 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400995 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500996 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400997 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400998 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500999 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001000 GLenum transposedType = TransposeMatrixType(varying.type);
1001 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -05001002 for (int row = 0; row < variableRows; row++)
1003 {
Jamie Madillde8892b2014-11-11 13:00:22 -05001004 std::string n = Str(varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001005 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -05001006
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001007 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -05001008 {
1009 pixelHLSL += ArrayString(elementIndex);
1010 }
1011
1012 if (variableRows > 1)
1013 {
1014 pixelHLSL += ArrayString(row);
1015 }
1016
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001017 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -05001018 {
1019 pixelHLSL += " = input.v" + n + ";\n"; break;
1020 }
1021 else
1022 {
1023 switch (VariableColumnCount(transposedType))
1024 {
1025 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
1026 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
1027 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
1028 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
1029 default: UNREACHABLE();
1030 }
1031 }
1032 }
1033 }
1034 }
Jamie Madill54ad4f82014-09-03 09:40:46 -04001035 else
1036 {
1037 ASSERT(varying.isBuiltIn() || !varying.staticUse);
1038 }
Jamie Madill5f562732014-02-14 16:41:24 -05001039 }
1040
Jamie Madill37997142015-01-28 10:06:34 -05001041 if (fragmentShader->usesDeferredInit())
1042 {
1043 pixelHLSL += "\n"
1044 " initializeDeferredGlobals();\n";
1045 }
1046
Jamie Madill5f562732014-02-14 16:41:24 -05001047 pixelHLSL += "\n"
1048 " gl_main();\n"
1049 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -04001050 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001051 "}\n";
1052
1053 return true;
1054}
1055
Jamie Madill30d6c252014-11-13 10:03:33 -05001056void DynamicHLSL::defineOutputVariables(ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -05001057{
Jamie Madilld15250e2014-09-03 09:40:44 -04001058 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -05001059
1060 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
1061 {
Jamie Madillf2575982014-06-25 16:04:54 -04001062 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -05001063 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
1064
Jamie Madill54ad4f82014-09-03 09:40:46 -04001065 ASSERT(outputVariable.staticUse);
1066
Jamie Madill5f562732014-02-14 16:41:24 -05001067 if (outputVariable.arraySize > 0)
1068 {
1069 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
1070 {
1071 const int location = baseLocation + elementIndex;
1072 ASSERT(programOutputVars->count(location) == 0);
1073 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
1074 }
1075 }
1076 else
1077 {
1078 ASSERT(programOutputVars->count(baseLocation) == 0);
1079 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
1080 }
1081 }
1082}
1083
Jamie Madill30d6c252014-11-13 10:03:33 -05001084std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001085{
1086 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001087 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001088 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001089}
1090
Jamie Madill30d6c252014-11-13 10:03:33 -05001091std::string DynamicHLSL::generatePointSpriteHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001092{
1093 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001094 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001095 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1096
1097 std::string geomHLSL;
1098
Brandon Jones71620962014-08-20 14:04:59 -07001099 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001100 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -07001101 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1102 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001103
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001104 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1105 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1106 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001107
Geoff Langc0b9ef42014-07-02 10:02:37 -04001108 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001109 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1110 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001111 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1112 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001113 "\n"
Jamie Madill37997142015-01-28 10:06:34 -05001114 "static float2 pointSpriteCorners[] = \n"
1115 "{\n"
1116 " float2( 0.5f, -0.5f),\n"
1117 " float2( 0.5f, 0.5f),\n"
1118 " float2(-0.5f, -0.5f),\n"
1119 " float2(-0.5f, 0.5f)\n"
1120 "};\n"
1121 "\n"
1122 "static float2 pointSpriteTexcoords[] = \n"
1123 "{\n"
1124 " float2(1.0f, 1.0f),\n"
1125 " float2(1.0f, 0.0f),\n"
1126 " float2(0.0f, 1.0f),\n"
1127 " float2(0.0f, 0.0f)\n"
1128 "};\n"
1129 "\n"
1130 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1131 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
1132 "\n"
1133 "[maxvertexcount(4)]\n"
1134 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1135 "{\n"
1136 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
1137 " output.gl_Position = input[0].gl_Position;\n"
1138 " output.gl_PointSize = input[0].gl_PointSize;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001139
1140 for (int r = 0; r < registers; r++)
1141 {
1142 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1143 }
1144
Brandon Jones71620962014-08-20 14:04:59 -07001145 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001146 {
1147 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1148 }
1149
1150 geomHLSL += " \n"
1151 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001152 " float4 dx_Position = input[0].dx_Position;\n"
1153 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001154
1155 for (int corner = 0; corner < 4; corner++)
1156 {
1157 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001158 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001159
Brandon Jones71620962014-08-20 14:04:59 -07001160 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001161 {
1162 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1163 }
1164
1165 geomHLSL += " outStream.Append(output);\n";
1166 }
1167
1168 geomHLSL += " \n"
1169 " outStream.RestartStrip();\n"
1170 "}\n";
1171
1172 return geomHLSL;
1173}
1174
1175// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001176std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001177{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001178 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001179 {
1180 return "_" + name;
1181 }
1182
1183 return name;
1184}
1185
Jamie Madillf2575982014-06-25 16:04:54 -04001186std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001187{
Jamie Madilla53ab512014-03-17 09:47:44 -04001188 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001189
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001190 // Matrix
1191 if (IsMatrixType(shaderAttrib.type))
1192 {
Jamie Madill8664b062014-02-14 16:41:29 -05001193 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001194 }
1195
Jamie Madillf2575982014-06-25 16:04:54 -04001196 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1197 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001198
Jamie Madill8664b062014-02-14 16:41:29 -05001199 // Perform integer to float conversion (if necessary)
1200 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1201
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001202 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001203 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001204 // TODO: normalization for 32-bit integer formats
1205 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1206 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001207 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001208
1209 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001210 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001211}
1212
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001213void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1214{
1215 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1216 {
1217 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1218
1219 if (vertexFormat.mType == GL_NONE)
1220 {
1221 signature[inputIndex] = GL_NONE;
1222 }
1223 else
1224 {
Jamie Madill30d6c252014-11-13 10:03:33 -05001225 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & VERTEX_CONVERT_GPU) != 0);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001226 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1227 }
1228 }
1229}
1230
Jamie Madill5f562732014-02-14 16:41:24 -05001231}