blob: 215c71f3600e5e47127bf5d08e4ca0349a43f719 [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"
10#include "libANGLE/renderer/d3d/ShaderD3D.h"
11#include "libANGLE/renderer/d3d/RendererD3D.h"
12#include "libANGLE/Program.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050013#include "libANGLE/Shader.h"
14#include "libANGLE/formatutils.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040015
16#include "common/utilities.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040017#include "common/blocklayout.h"
Jamie Madill5f562732014-02-14 16:41:24 -050018
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
Jamie Madill30d6c252014-11-13 10:03:33 -050075const PixelShaderOutputVariable &GetOutputAtLocation(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 {
82 return outputVariables[variableIndex];
83 }
84 }
85
86 UNREACHABLE();
87 return outputVariables[0];
88}
89
Geoff Lang04fb89a2014-06-09 15:05:36 -040090const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
91const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050092
Jamie Madill30d6c252014-11-13 10:03:33 -050093}
94
Jamie Madill93e13fb2014-11-06 15:27:25 -050095DynamicHLSL::DynamicHLSL(RendererD3D *const renderer)
Jamie Madill5f562732014-02-14 16:41:24 -050096 : mRenderer(renderer)
97{
98}
99
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400100static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -0500101{
Geoff Lang48dcae72014-02-05 16:28:24 -0500102 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -0500103
Geoff Lang48dcae72014-02-05 16:28:24 -0500104 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -0400105 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -0500106 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500107
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400108 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -0500109 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400110 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -0500111 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500112 bool available = true;
113
114 for (int y = 0; y < registers && available; y++)
115 {
116 for (int x = 0; x < elements && available; x++)
117 {
118 if (packing[r + y][x])
119 {
120 available = false;
121 }
122 }
123 }
124
125 if (available)
126 {
127 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700128 varying->columnIndex = 0;
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;
Austin Kinrossaf875522014-08-25 21:06:07 -0700162 varying->columnIndex = 2;
Jamie Madill5f562732014-02-14 16:41:24 -0500163
164 for (int y = 0; y < registers; y++)
165 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500166 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500167 {
168 packing[r + y][x] = &*varying;
169 }
170 }
171
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400172 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500173 }
174 }
Jamie Madill5f562732014-02-14 16:41:24 -0500175 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500176 }
177 else if (elements == 1)
178 {
179 int space[4] = { 0 };
180
181 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500182 {
Jamie Madill5f562732014-02-14 16:41:24 -0500183 for (int x = 0; x < 4; x++)
184 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500185 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500186 }
187 }
Jamie Madill5f562732014-02-14 16:41:24 -0500188
Geoff Lang48dcae72014-02-05 16:28:24 -0500189 int column = 0;
190
191 for (int x = 0; x < 4; x++)
192 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700193 if (space[x] >= registers && (space[column] < registers || space[x] < space[column]))
Geoff Lang48dcae72014-02-05 16:28:24 -0500194 {
195 column = x;
196 }
197 }
198
199 if (space[column] >= registers)
200 {
201 for (int r = 0; r < maxVaryingVectors; r++)
202 {
203 if (!packing[r][column])
204 {
205 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700206 varying->columnIndex = column;
Geoff Lang48dcae72014-02-05 16:28:24 -0500207
208 for (int y = r; y < r + registers; y++)
209 {
210 packing[y][column] = &*varying;
211 }
212
213 break;
214 }
215 }
216
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400217 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500218 }
219 }
220 else UNREACHABLE();
221
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400222 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500223}
224
225// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
226// Returns the number of used varying registers, or -1 if unsuccesful
Jamie Madill30d6c252014-11-13 10:03:33 -0500227int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, ShaderD3D *fragmentShader,
228 ShaderD3D *vertexShader, const std::vector<std::string> &transformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -0500229{
Geoff Lang3a61c322014-07-10 13:01:54 -0400230 // TODO (geofflang): Use context's caps
231 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Geoff Lang48dcae72014-02-05 16:28:24 -0500232
Brandon Jones71620962014-08-20 14:04:59 -0700233 vertexShader->resetVaryingsRegisterAssignment();
234 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500235
236 std::set<std::string> packedVaryings;
237
Jamie Madilld15250e2014-09-03 09:40:44 -0400238 std::vector<gl::PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
239 std::vector<gl::PackedVarying> &vertexVaryings = vertexShader->getVaryings();
240 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500241 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400242 PackedVarying *varying = &fragmentVaryings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400243
244 // Do not assign registers to built-in or unreferenced varyings
245 if (varying->isBuiltIn() || !varying->staticUse)
246 {
247 continue;
248 }
249
Geoff Lang48dcae72014-02-05 16:28:24 -0500250 if (packVarying(varying, maxVaryingVectors, packing))
251 {
252 packedVaryings.insert(varying->name);
253 }
254 else
Jamie Madill5f562732014-02-14 16:41:24 -0500255 {
256 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500257 return -1;
258 }
259 }
260
Geoff Lang48dcae72014-02-05 16:28:24 -0500261 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
262 {
263 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
Jamie Madill55d611e2014-10-24 16:28:14 -0400264
265 if (transformFeedbackVarying == "gl_Position" || transformFeedbackVarying == "gl_PointSize")
266 {
267 // do not pack builtin XFB varyings
268 continue;
269 }
270
Geoff Lang48dcae72014-02-05 16:28:24 -0500271 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
272 {
273 bool found = false;
Jamie Madilld15250e2014-09-03 09:40:44 -0400274 for (unsigned int varyingIndex = 0; varyingIndex < vertexVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500275 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400276 PackedVarying *varying = &vertexVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500277 if (transformFeedbackVarying == varying->name)
278 {
279 if (!packVarying(varying, maxVaryingVectors, packing))
280 {
281 infoLog.append("Could not pack varying %s", varying->name.c_str());
282 return -1;
283 }
284
285 found = true;
286 break;
287 }
288 }
289
Jamie Madill55d611e2014-10-24 16:28:14 -0400290 if (!found)
Geoff Lang48dcae72014-02-05 16:28:24 -0500291 {
292 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
293 return -1;
294 }
295 }
296 }
297
Jamie Madill5f562732014-02-14 16:41:24 -0500298 // Return the number of used registers
299 int registers = 0;
300
301 for (int r = 0; r < maxVaryingVectors; r++)
302 {
303 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
304 {
305 registers++;
306 }
307 }
308
309 return registers;
310}
311
Jamie Madill54ad4f82014-09-03 09:40:46 -0400312std::string DynamicHLSL::generateVaryingHLSL(const ShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500313{
Brandon Jones71620962014-08-20 14:04:59 -0700314 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500315 std::string varyingHLSL;
316
Jamie Madill54ad4f82014-09-03 09:40:46 -0400317 const std::vector<gl::PackedVarying> &varyings = shader->getVaryings();
318
Jamie Madilld15250e2014-09-03 09:40:44 -0400319 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500320 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400321 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400322 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500323 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400324 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400325 GLenum transposedType = TransposeMatrixType(varying.type);
326 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500327
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400328 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500329 {
Jamie Madill5f562732014-02-14 16:41:24 -0500330 for (int row = 0; row < variableRows; row++)
331 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700332 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many registers being used.
333 // 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.
334 // If the float varying has the 'nointerpolation' modifier on it then we would need N + 1 registers, and D3D compilation will fail.
335
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400336 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500337 {
Jamie Madillf2575982014-06-25 16:04:54 -0400338 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
339 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
340 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500341 default: UNREACHABLE();
342 }
343
Austin Kinrossaf875522014-08-25 21:06:07 -0700344 unsigned int semanticIndex = elementIndex * variableRows + varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500345 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500346
Jamie Madilla53ab512014-03-17 09:47:44 -0400347 std::string typeString;
348
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400349 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400350 {
351 // matrices within structs are not transposed, so
352 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400353 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400354 }
355 else
356 {
Jamie Madillf2575982014-06-25 16:04:54 -0400357 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400358 int columnCount = VariableColumnCount(transposedType);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400359 typeString = HLSLComponentTypeString(componentType, columnCount);
Jamie Madilla53ab512014-03-17 09:47:44 -0400360 }
Jamie Madill5f562732014-02-14 16:41:24 -0500361 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
362 }
363 }
364 }
Jamie Madill5f562732014-02-14 16:41:24 -0500365 }
366
367 return varyingHLSL;
368}
369
Jamie Madillf2575982014-06-25 16:04:54 -0400370std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
371 const VertexFormat inputLayout[],
372 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500373{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400374 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500375
376 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400377 unsigned int inputIndex = 0;
378
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500379 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
380 {
Jamie Madillf2575982014-06-25 16:04:54 -0400381 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500382 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500383 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400384 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
385 const VertexFormat &vertexFormat = inputLayout[inputIndex];
386
Jamie Madill3b7e2052014-03-17 09:47:43 -0400387 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500388 if (IsMatrixType(shaderAttribute.type))
389 {
390 // Matrix types are always transposed
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400391 structHLSL += " " + HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500392 }
393 else
394 {
395 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400396 structHLSL += " " + HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500397 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500398
Jamie Madilla53ab512014-03-17 09:47:44 -0400399 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400400 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500401
Jamie Madill3b7e2052014-03-17 09:47:43 -0400402 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400403 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500404
405 // Mismatched vertex attribute to vertex input may result in an undefined
406 // data reinterpretation (eg for pure integer->float, float->pure integer)
407 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400408 if (IsMatrixType(shaderAttribute.type) ||
Jamie Madill30d6c252014-11-13 10:03:33 -0500409 (mRenderer->getVertexConversionType(vertexFormat) & VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500410 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400411 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500412 }
413 else
414 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400415 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500416 }
417
Jamie Madill3b7e2052014-03-17 09:47:43 -0400418 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400419
Jamie Madillac0a2672014-04-11 13:33:56 -0400420 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
421 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500422 }
423
Geoff Lang04fb89a2014-06-09 15:05:36 -0400424 std::string replacementHLSL = "struct VS_INPUT\n"
425 "{\n" +
426 structHLSL +
427 "};\n"
428 "\n"
429 "void initAttributes(VS_INPUT input)\n"
430 "{\n" +
431 initHLSL +
432 "}\n";
433
434 std::string vertexHLSL(sourceShader);
435
436 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
437 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
438
439 return vertexHLSL;
440}
441
Jamie Madillf6be8d72014-09-05 10:38:07 -0400442std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOutputVariable> &outputVariables,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400443 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
444{
445 const int shaderModel = mRenderer->getMajorShaderModel();
446 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
447 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
448
449 std::string declarationHLSL;
450 std::string copyHLSL;
Geoff Lang4ace4232014-06-18 19:12:48 -0400451
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400452 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
453 {
454 GLenum binding = outputLayout[layoutIndex];
455
456 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400457 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400458 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
459
Jamie Madillf6be8d72014-09-05 10:38:07 -0400460 const PixelShaderOutputVariable &outputVariable = GetOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400461
462 declarationHLSL += " " + HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
463 " : " + targetSemantic + Str(layoutIndex) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400464
465 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
466 }
467 }
468
469 if (usesFragDepth)
470 {
471 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
472 copyHLSL += " output.gl_Depth = gl_Depth; \n";
473 }
474
475 std::string replacementHLSL = "struct PS_OUTPUT\n"
476 "{\n" +
477 declarationHLSL +
478 "};\n"
479 "\n"
480 "PS_OUTPUT generateOutput()\n"
481 "{\n"
482 " PS_OUTPUT output;\n" +
483 copyHLSL +
484 " return output;\n"
485 "}\n";
486
487 std::string pixelHLSL(sourceShader);
488
489 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
490 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
491
492 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500493}
494
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400495std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
496{
497 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
498 // In D3D11 we manually compute gl_PointCoord in the GS.
499 int shaderModel = mRenderer->getMajorShaderModel();
500 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
501}
502
503struct DynamicHLSL::SemanticInfo
504{
505 struct BuiltinInfo
506 {
507 BuiltinInfo()
508 : enabled(false),
509 index(0),
510 systemValue(false)
511 {}
512
513 bool enabled;
514 std::string semantic;
515 unsigned int index;
516 bool systemValue;
517
518 std::string str() const
519 {
520 return (systemValue ? semantic : (semantic + Str(index)));
521 }
522
523 void enableSystem(const std::string &systemValueSemantic)
524 {
525 enabled = true;
526 semantic = systemValueSemantic;
527 systemValue = true;
528 }
529
530 void enable(const std::string &semanticVal, unsigned int indexVal)
531 {
532 enabled = true;
533 semantic = semanticVal;
534 index = indexVal;
535 }
536 };
537
538 BuiltinInfo dxPosition;
539 BuiltinInfo glPosition;
540 BuiltinInfo glFragCoord;
541 BuiltinInfo glPointCoord;
542 BuiltinInfo glPointSize;
543};
544
545DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
546 bool pointSize, bool pixelShader) const
547{
548 SemanticInfo info;
549 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
550 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
551
552 int reservedRegisterIndex = startRegisters;
553
554 if (hlsl4)
555 {
556 info.dxPosition.enableSystem("SV_Position");
557 }
558 else if (pixelShader)
559 {
560 info.dxPosition.enableSystem("VPOS");
561 }
562 else
563 {
564 info.dxPosition.enableSystem("POSITION");
565 }
566
567 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
568
569 if (fragCoord)
570 {
571 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
572 }
573
574 if (pointCoord)
575 {
576 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
577 // In D3D11 we manually compute gl_PointCoord in the GS.
578 if (hlsl4)
579 {
580 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
581 }
582 else
583 {
584 info.glPointCoord.enable("TEXCOORD", 0);
585 }
586 }
587
588 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
589 if (pointSize && (!pixelShader || hlsl4))
590 {
591 info.glPointSize.enableSystem("PSIZE");
592 }
593
594 return info;
595}
596
597std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
598{
599 std::string linkHLSL = "{\n";
600
601 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
602
603 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
604 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
605
606 if (info.glFragCoord.enabled)
607 {
608 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
609 }
610
611 if (info.glPointCoord.enabled)
612 {
613 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
614 }
615
616 linkHLSL += varyingHLSL;
617
618 if (info.glPointSize.enabled)
619 {
620 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
621 }
622
623 linkHLSL += "};\n";
624
625 return linkHLSL;
626}
627
628void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
629 std::vector<LinkedVarying> *linkedVaryings) const
630{
631 ASSERT(info.glPosition.enabled);
632
633 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
634 info.glPosition.index, 1));
635
636 if (info.glFragCoord.enabled)
637 {
638 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
639 info.glFragCoord.index, 1));
640 }
641
642 if (info.glPointSize.enabled)
643 {
644 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
645 }
646}
647
Jamie Madill30d6c252014-11-13 10:03:33 -0500648void DynamicHLSL::storeUserLinkedVaryings(const ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400649 std::vector<LinkedVarying> *linkedVaryings) const
650{
Brandon Jones71620962014-08-20 14:04:59 -0700651 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400652 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400653
654 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
655 {
656 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400657
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400658 if (varying.registerAssigned())
659 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400660 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400661 GLenum transposedType = TransposeMatrixType(varying.type);
662 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
663
664 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
665 varyingSemantic, varying.registerIndex,
666 variableRows * varying.elementCount()));
667 }
668 }
669}
670
Jamie Madillde8892b2014-11-11 13:00:22 -0500671bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data, InfoLog &infoLog, int registers,
672 const VaryingPacking packing,
673 std::string &pixelHLSL, std::string &vertexHLSL,
Jamie Madill30d6c252014-11-13 10:03:33 -0500674 ShaderD3D *fragmentShader, ShaderD3D *vertexShader,
Jamie Madillde8892b2014-11-11 13:00:22 -0500675 const std::vector<std::string> &transformFeedbackVaryings,
Geoff Lang48dcae72014-02-05 16:28:24 -0500676 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400677 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400678 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400679 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500680{
681 if (pixelHLSL.empty() || vertexHLSL.empty())
682 {
683 return false;
684 }
685
Brandon Jones71620962014-08-20 14:04:59 -0700686 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
687 bool usesFragColor = fragmentShader->mUsesFragColor;
688 bool usesFragData = fragmentShader->mUsesFragData;
689 bool usesFragCoord = fragmentShader->mUsesFragCoord;
690 bool usesPointCoord = fragmentShader->mUsesPointCoord;
691 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400692
Jamie Madill5f562732014-02-14 16:41:24 -0500693 if (usesFragColor && usesFragData)
694 {
695 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
696 return false;
697 }
698
699 // Write the HLSL input/output declarations
700 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400701 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500702
703 // Two cases when writing to gl_FragColor and using ESSL 1.0:
704 // - with a 3.0 context, the output color is copied to channel 0
705 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madillde8892b2014-11-11 13:00:22 -0500706 const bool broadcast = (fragmentShader->mUsesFragColor && data.clientVersion < 3);
707 const unsigned int numRenderTargets = (broadcast || usesMRT ? data.caps->maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500708
Brandon Jones71620962014-08-20 14:04:59 -0700709 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500710
Jamie Madillde8892b2014-11-11 13:00:22 -0500711 if (static_cast<GLuint>(registersNeeded) > data.caps->maxVaryingVectors)
Jamie Madill5f562732014-02-14 16:41:24 -0500712 {
713 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500714 return false;
715 }
716
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400717 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
718 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
719 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500720
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400721 storeUserLinkedVaryings(vertexShader, linkedVaryings);
722 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500723
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500724 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400725 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
726 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500727 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500728 "{\n"
729 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500730
Austin Kinross4fd18b12014-12-22 12:32:05 -0800731 // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust.
732 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500733 {
734 vertexHLSL += "\n"
735 " gl_main();\n"
736 "\n"
737 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500738 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400739 " output.dx_Position.x = gl_Position.x;\n"
740 " output.dx_Position.y = -gl_Position.y;\n"
741 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
742 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500743 }
744 else
745 {
746 vertexHLSL += "\n"
747 " gl_main();\n"
748 "\n"
749 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500750 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400751 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
752 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
753 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
754 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500755 }
756
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400757 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500758 {
759 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
760 }
761
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400762 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500763 {
764 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
765 }
766
Jamie Madilld15250e2014-09-03 09:40:44 -0400767 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
768 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500769 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400770 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400771 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500772 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400773 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500774 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400775 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500776
777 for (int row = 0; row < variableRows; row++)
778 {
Jamie Madillde8892b2014-11-11 13:00:22 -0500779 int r = varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500780 vertexHLSL += " output.v" + Str(r);
781
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400782 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500783
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400784 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500785 {
786 vertexHLSL += ArrayString(elementIndex);
787 }
788
789 if (variableRows > 1)
790 {
791 vertexHLSL += ArrayString(row);
792 }
793
794 vertexHLSL += ";\n";
795 }
796 }
797 }
798 }
799
800 vertexHLSL += "\n"
801 " return output;\n"
802 "}\n";
803
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400804 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
805 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500806
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400807 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500808
809 if (shaderVersion < 300)
810 {
811 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
812 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400813 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400814 outputKeyVariable.type = GL_FLOAT_VEC4;
815 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
816 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
817 outputKeyVariable.outputIndex = renderTargetIndex;
818
819 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500820 }
821
Brandon Jones71620962014-08-20 14:04:59 -0700822 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500823 }
824 else
825 {
826 defineOutputVariables(fragmentShader, programOutputVars);
827
Jamie Madilld15250e2014-09-03 09:40:44 -0400828 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500829 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
830 {
831 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400832 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400833 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500834 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
835
Jamie Madill54ad4f82014-09-03 09:40:46 -0400836 ASSERT(outputVariable.staticUse);
837
Jamie Madillf6be8d72014-09-05 10:38:07 -0400838 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400839 outputKeyVariable.type = outputVariable.type;
840 outputKeyVariable.name = variableName + elementString;
841 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
842 outputKeyVariable.outputIndex = locationIt->first;
843
844 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500845 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400846
847 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500848 }
849
Geoff Lang04fb89a2014-06-09 15:05:36 -0400850 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500851
Brandon Jones71620962014-08-20 14:04:59 -0700852 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500853 {
854 if (shaderModel >= 4)
855 {
856 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
857 "{\n";
858 }
859 else
860 {
861 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
862 "{\n";
863 }
864 }
865 else
866 {
867 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
868 "{\n";
869 }
870
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400871 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500872 {
873 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
874
Austin Kinross588434c2014-12-10 10:41:45 -0800875 // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader.
876 // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using dx_ViewCoords.
877 if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "")
Jamie Madill5f562732014-02-14 16:41:24 -0500878 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400879 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
880 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500881 }
Austin Kinross588434c2014-12-10 10:41:45 -0800882 else if (shaderModel == 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500883 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400884 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
885 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500886 }
887 else
888 {
889 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
890 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
891 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
892 }
893
894 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
895 " gl_FragCoord.w = rhw;\n";
896 }
897
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400898 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500899 {
900 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
901 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
902 }
903
Brandon Jones71620962014-08-20 14:04:59 -0700904 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500905 {
906 if (shaderModel <= 3)
907 {
908 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
909 }
910 else
911 {
912 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
913 }
914 }
915
Jamie Madilld15250e2014-09-03 09:40:44 -0400916 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
917 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500918 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400919 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400920 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500921 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400922 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400923 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500924 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400925 GLenum transposedType = TransposeMatrixType(varying.type);
926 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500927 for (int row = 0; row < variableRows; row++)
928 {
Jamie Madillde8892b2014-11-11 13:00:22 -0500929 std::string n = Str(varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400930 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500931
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400932 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500933 {
934 pixelHLSL += ArrayString(elementIndex);
935 }
936
937 if (variableRows > 1)
938 {
939 pixelHLSL += ArrayString(row);
940 }
941
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400942 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500943 {
944 pixelHLSL += " = input.v" + n + ";\n"; break;
945 }
946 else
947 {
948 switch (VariableColumnCount(transposedType))
949 {
950 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
951 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
952 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
953 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
954 default: UNREACHABLE();
955 }
956 }
957 }
958 }
959 }
Jamie Madill54ad4f82014-09-03 09:40:46 -0400960 else
961 {
962 ASSERT(varying.isBuiltIn() || !varying.staticUse);
963 }
Jamie Madill5f562732014-02-14 16:41:24 -0500964 }
965
966 pixelHLSL += "\n"
967 " gl_main();\n"
968 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400969 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500970 "}\n";
971
972 return true;
973}
974
Jamie Madill30d6c252014-11-13 10:03:33 -0500975void DynamicHLSL::defineOutputVariables(ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500976{
Jamie Madilld15250e2014-09-03 09:40:44 -0400977 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500978
979 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
980 {
Jamie Madillf2575982014-06-25 16:04:54 -0400981 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500982 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
983
Jamie Madill54ad4f82014-09-03 09:40:46 -0400984 ASSERT(outputVariable.staticUse);
985
Jamie Madill5f562732014-02-14 16:41:24 -0500986 if (outputVariable.arraySize > 0)
987 {
988 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
989 {
990 const int location = baseLocation + elementIndex;
991 ASSERT(programOutputVars->count(location) == 0);
992 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
993 }
994 }
995 else
996 {
997 ASSERT(programOutputVars->count(baseLocation) == 0);
998 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
999 }
1000 }
1001}
1002
Jamie Madill30d6c252014-11-13 10:03:33 -05001003std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001004{
1005 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001006 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001007 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001008}
1009
Jamie Madill30d6c252014-11-13 10:03:33 -05001010std::string DynamicHLSL::generatePointSpriteHLSL(int registers, ShaderD3D *fragmentShader, ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001011{
1012 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001013 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001014 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1015
1016 std::string geomHLSL;
1017
Brandon Jones71620962014-08-20 14:04:59 -07001018 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001019 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -07001020 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1021 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001022
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001023 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1024 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1025 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001026
Geoff Langc0b9ef42014-07-02 10:02:37 -04001027 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001028 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1029 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001030 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1031 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001032 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001033 "static float2 pointSpriteCorners[] = \n"
1034 "{\n"
1035 " float2( 0.5f, -0.5f),\n"
1036 " float2( 0.5f, 0.5f),\n"
1037 " float2(-0.5f, -0.5f),\n"
1038 " float2(-0.5f, 0.5f)\n"
1039 "};\n"
1040 "\n"
1041 "static float2 pointSpriteTexcoords[] = \n"
1042 "{\n"
1043 " float2(1.0f, 1.0f),\n"
1044 " float2(1.0f, 0.0f),\n"
1045 " float2(0.0f, 1.0f),\n"
1046 " float2(0.0f, 0.0f)\n"
1047 "};\n"
1048 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001049 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1050 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001051 "\n"
1052 "[maxvertexcount(4)]\n"
1053 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1054 "{\n"
1055 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jacek Cabana22cd472014-11-05 11:20:45 +01001056 " output.gl_Position = input[0].gl_Position;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001057 " output.gl_PointSize = input[0].gl_PointSize;\n";
1058
1059 for (int r = 0; r < registers; r++)
1060 {
1061 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1062 }
1063
Brandon Jones71620962014-08-20 14:04:59 -07001064 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001065 {
1066 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1067 }
1068
1069 geomHLSL += " \n"
1070 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001071 " float4 dx_Position = input[0].dx_Position;\n"
1072 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001073
1074 for (int corner = 0; corner < 4; corner++)
1075 {
1076 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001077 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001078
Brandon Jones71620962014-08-20 14:04:59 -07001079 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001080 {
1081 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1082 }
1083
1084 geomHLSL += " outStream.Append(output);\n";
1085 }
1086
1087 geomHLSL += " \n"
1088 " outStream.RestartStrip();\n"
1089 "}\n";
1090
1091 return geomHLSL;
1092}
1093
1094// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001095std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001096{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001097 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001098 {
1099 return "_" + name;
1100 }
1101
1102 return name;
1103}
1104
Jamie Madillf2575982014-06-25 16:04:54 -04001105std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001106{
Jamie Madilla53ab512014-03-17 09:47:44 -04001107 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001108
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001109 // Matrix
1110 if (IsMatrixType(shaderAttrib.type))
1111 {
Jamie Madill8664b062014-02-14 16:41:29 -05001112 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001113 }
1114
Jamie Madillf2575982014-06-25 16:04:54 -04001115 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1116 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001117
Jamie Madill8664b062014-02-14 16:41:29 -05001118 // Perform integer to float conversion (if necessary)
1119 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1120
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001121 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001122 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001123 // TODO: normalization for 32-bit integer formats
1124 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1125 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001126 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001127
1128 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001129 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001130}
1131
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001132void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1133{
1134 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1135 {
1136 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1137
1138 if (vertexFormat.mType == GL_NONE)
1139 {
1140 signature[inputIndex] = GL_NONE;
1141 }
1142 else
1143 {
Jamie Madill30d6c252014-11-13 10:03:33 -05001144 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & VERTEX_CONVERT_GPU) != 0);
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001145 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1146 }
1147 }
1148}
1149
Jamie Madill5f562732014-02-14 16:41:24 -05001150}