blob: 99a04f883f01f1b1d11bdb6d4dc26574e27d4af6 [file] [log] [blame]
Jamie Madill5f562732014-02-14 16:41:24 -05001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// DynamicHLSL.cpp: Implementation for link and run-time HLSL generation
7//
8
Brandon Jonesd8d72432014-08-22 15:11:23 -07009#include "libGLESv2/renderer/d3d/DynamicHLSL.h"
Brandon Jonesf05cdee2014-08-27 15:24:07 -070010#include "libGLESv2/renderer/d3d/ShaderD3D.h"
Jamie Madill93e13fb2014-11-06 15:27:25 -050011#include "libGLESv2/renderer/d3d/RendererD3D.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040012#include "libGLESv2/Program.h"
Jamie Madill5f562732014-02-14 16:41:24 -050013#include "libGLESv2/ProgramBinary.h"
Jamie Madillde8892b2014-11-11 13:00:22 -050014#include "libGLESv2/Shader.h"
Jamie Madill8664b062014-02-14 16:41:29 -050015#include "libGLESv2/formatutils.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040016
17#include "common/utilities.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040018#include "common/blocklayout.h"
Jamie Madill5f562732014-02-14 16:41:24 -050019
Jamie Madill53cb14d2014-07-08 15:02:35 -040020// For use with ArrayString, see angleutils.h
21META_ASSERT(GL_INVALID_INDEX == UINT_MAX);
Jamie Madill5f562732014-02-14 16:41:24 -050022
Brandon Jonesd8d72432014-08-22 15:11:23 -070023using namespace gl;
24
Jamie Madill3f2e61d2014-09-05 10:38:05 -040025namespace
Jamie Madill5f562732014-02-14 16:41:24 -050026{
Jamie Madill8664b062014-02-14 16:41:29 -050027
28std::string HLSLComponentTypeString(GLenum componentType)
29{
30 switch (componentType)
31 {
32 case GL_UNSIGNED_INT: return "uint";
33 case GL_INT: return "int";
34 case GL_UNSIGNED_NORMALIZED:
35 case GL_SIGNED_NORMALIZED:
36 case GL_FLOAT: return "float";
37 default: UNREACHABLE(); return "not-component-type";
38 }
Jamie Madill5f562732014-02-14 16:41:24 -050039}
40
Jamie Madill8664b062014-02-14 16:41:29 -050041std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
42{
43 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
44}
45
46std::string HLSLMatrixTypeString(GLenum type)
47{
48 switch (type)
49 {
50 case GL_FLOAT_MAT2: return "float2x2";
51 case GL_FLOAT_MAT3: return "float3x3";
52 case GL_FLOAT_MAT4: return "float4x4";
53 case GL_FLOAT_MAT2x3: return "float2x3";
54 case GL_FLOAT_MAT3x2: return "float3x2";
55 case GL_FLOAT_MAT2x4: return "float2x4";
56 case GL_FLOAT_MAT4x2: return "float4x2";
57 case GL_FLOAT_MAT3x4: return "float3x4";
58 case GL_FLOAT_MAT4x3: return "float4x3";
59 default: UNREACHABLE(); return "not-matrix-type";
60 }
61}
62
63std::string HLSLTypeString(GLenum type)
64{
65 if (gl::IsMatrixType(type))
66 {
67 return HLSLMatrixTypeString(type);
68 }
69
Jamie Madillf2575982014-06-25 16:04:54 -040070 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050071}
72
Jamie Madillf6be8d72014-09-05 10:38:07 -040073const rx::PixelShaderOutputVariable &GetOutputAtLocation(const std::vector<rx::PixelShaderOutputVariable> &outputVariables,
Jamie Madill3f2e61d2014-09-05 10:38:05 -040074 unsigned int location)
75{
76 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
77 {
78 if (outputVariables[variableIndex].outputIndex == location)
79 {
80 return outputVariables[variableIndex];
81 }
82 }
83
84 UNREACHABLE();
85 return outputVariables[0];
86}
87
Jamie Madill8664b062014-02-14 16:41:29 -050088}
89
Brandon Jonesd8d72432014-08-22 15:11:23 -070090namespace rx
Jamie Madill8664b062014-02-14 16:41:29 -050091{
92
Geoff Lang04fb89a2014-06-09 15:05:36 -040093const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
94const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050095
Jamie Madill93e13fb2014-11-06 15:27:25 -050096DynamicHLSL::DynamicHLSL(RendererD3D *const renderer)
Jamie Madill5f562732014-02-14 16:41:24 -050097 : mRenderer(renderer)
98{
99}
100
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400101static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -0500102{
Geoff Lang48dcae72014-02-05 16:28:24 -0500103 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -0500104
Geoff Lang48dcae72014-02-05 16:28:24 -0500105 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -0400106 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -0500107 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500108
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400109 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -0500110 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400111 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -0500112 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500113 bool available = true;
114
115 for (int y = 0; y < registers && available; y++)
116 {
117 for (int x = 0; x < elements && available; x++)
118 {
119 if (packing[r + y][x])
120 {
121 available = false;
122 }
123 }
124 }
125
126 if (available)
127 {
128 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700129 varying->columnIndex = 0;
Geoff Lang48dcae72014-02-05 16:28:24 -0500130
131 for (int y = 0; y < registers; y++)
132 {
133 for (int x = 0; x < elements; x++)
134 {
135 packing[r + y][x] = &*varying;
136 }
137 }
138
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400139 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500140 }
141 }
142
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400143 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500144 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400145 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500146 {
147 bool available = true;
148
149 for (int y = 0; y < registers && available; y++)
150 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500151 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500152 {
153 if (packing[r + y][x])
154 {
155 available = false;
156 }
157 }
158 }
159
160 if (available)
161 {
162 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700163 varying->columnIndex = 2;
Jamie Madill5f562732014-02-14 16:41:24 -0500164
165 for (int y = 0; y < registers; y++)
166 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500167 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500168 {
169 packing[r + y][x] = &*varying;
170 }
171 }
172
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400173 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500174 }
175 }
Jamie Madill5f562732014-02-14 16:41:24 -0500176 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500177 }
178 else if (elements == 1)
179 {
180 int space[4] = { 0 };
181
182 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500183 {
Jamie Madill5f562732014-02-14 16:41:24 -0500184 for (int x = 0; x < 4; x++)
185 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500186 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500187 }
188 }
Jamie Madill5f562732014-02-14 16:41:24 -0500189
Geoff Lang48dcae72014-02-05 16:28:24 -0500190 int column = 0;
191
192 for (int x = 0; x < 4; x++)
193 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700194 if (space[x] >= registers && (space[column] < registers || space[x] < space[column]))
Geoff Lang48dcae72014-02-05 16:28:24 -0500195 {
196 column = x;
197 }
198 }
199
200 if (space[column] >= registers)
201 {
202 for (int r = 0; r < maxVaryingVectors; r++)
203 {
204 if (!packing[r][column])
205 {
206 varying->registerIndex = r;
Austin Kinrossaf875522014-08-25 21:06:07 -0700207 varying->columnIndex = column;
Geoff Lang48dcae72014-02-05 16:28:24 -0500208
209 for (int y = r; y < r + registers; y++)
210 {
211 packing[y][column] = &*varying;
212 }
213
214 break;
215 }
216 }
217
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400218 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500219 }
220 }
221 else UNREACHABLE();
222
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400223 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500224}
225
226// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
227// Returns the number of used varying registers, or -1 if unsuccesful
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400228int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, rx::ShaderD3D *fragmentShader,
Jamie Madillde8892b2014-11-11 13:00:22 -0500229 rx::ShaderD3D *vertexShader, const std::vector<std::string> &transformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -0500230{
Geoff Lang3a61c322014-07-10 13:01:54 -0400231 // TODO (geofflang): Use context's caps
232 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Geoff Lang48dcae72014-02-05 16:28:24 -0500233
Brandon Jones71620962014-08-20 14:04:59 -0700234 vertexShader->resetVaryingsRegisterAssignment();
235 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500236
237 std::set<std::string> packedVaryings;
238
Jamie Madilld15250e2014-09-03 09:40:44 -0400239 std::vector<gl::PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
240 std::vector<gl::PackedVarying> &vertexVaryings = vertexShader->getVaryings();
241 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500242 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400243 PackedVarying *varying = &fragmentVaryings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400244
245 // Do not assign registers to built-in or unreferenced varyings
246 if (varying->isBuiltIn() || !varying->staticUse)
247 {
248 continue;
249 }
250
Geoff Lang48dcae72014-02-05 16:28:24 -0500251 if (packVarying(varying, maxVaryingVectors, packing))
252 {
253 packedVaryings.insert(varying->name);
254 }
255 else
Jamie Madill5f562732014-02-14 16:41:24 -0500256 {
257 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500258 return -1;
259 }
260 }
261
Geoff Lang48dcae72014-02-05 16:28:24 -0500262 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
263 {
264 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
Jamie Madill55d611e2014-10-24 16:28:14 -0400265
266 if (transformFeedbackVarying == "gl_Position" || transformFeedbackVarying == "gl_PointSize")
267 {
268 // do not pack builtin XFB varyings
269 continue;
270 }
271
Geoff Lang48dcae72014-02-05 16:28:24 -0500272 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
273 {
274 bool found = false;
Jamie Madilld15250e2014-09-03 09:40:44 -0400275 for (unsigned int varyingIndex = 0; varyingIndex < vertexVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500276 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400277 PackedVarying *varying = &vertexVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500278 if (transformFeedbackVarying == varying->name)
279 {
280 if (!packVarying(varying, maxVaryingVectors, packing))
281 {
282 infoLog.append("Could not pack varying %s", varying->name.c_str());
283 return -1;
284 }
285
286 found = true;
287 break;
288 }
289 }
290
Jamie Madill55d611e2014-10-24 16:28:14 -0400291 if (!found)
Geoff Lang48dcae72014-02-05 16:28:24 -0500292 {
293 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
294 return -1;
295 }
296 }
297 }
298
Jamie Madill5f562732014-02-14 16:41:24 -0500299 // Return the number of used registers
300 int registers = 0;
301
302 for (int r = 0; r < maxVaryingVectors; r++)
303 {
304 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
305 {
306 registers++;
307 }
308 }
309
310 return registers;
311}
312
Jamie Madill54ad4f82014-09-03 09:40:46 -0400313std::string DynamicHLSL::generateVaryingHLSL(const ShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500314{
Brandon Jones71620962014-08-20 14:04:59 -0700315 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500316 std::string varyingHLSL;
317
Jamie Madill54ad4f82014-09-03 09:40:46 -0400318 const std::vector<gl::PackedVarying> &varyings = shader->getVaryings();
319
Jamie Madilld15250e2014-09-03 09:40:44 -0400320 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500321 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400322 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400323 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500324 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400325 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400326 GLenum transposedType = TransposeMatrixType(varying.type);
327 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500328
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400329 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500330 {
Jamie Madill5f562732014-02-14 16:41:24 -0500331 for (int row = 0; row < variableRows; row++)
332 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700333 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many registers being used.
334 // 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.
335 // If the float varying has the 'nointerpolation' modifier on it then we would need N + 1 registers, and D3D compilation will fail.
336
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400337 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500338 {
Jamie Madillf2575982014-06-25 16:04:54 -0400339 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
340 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
341 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500342 default: UNREACHABLE();
343 }
344
Austin Kinrossaf875522014-08-25 21:06:07 -0700345 unsigned int semanticIndex = elementIndex * variableRows + varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500346 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500347
Jamie Madilla53ab512014-03-17 09:47:44 -0400348 std::string typeString;
349
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400350 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400351 {
352 // matrices within structs are not transposed, so
353 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400354 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400355 }
356 else
357 {
Jamie Madillf2575982014-06-25 16:04:54 -0400358 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400359 int columnCount = VariableColumnCount(transposedType);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400360 typeString = HLSLComponentTypeString(componentType, columnCount);
Jamie Madilla53ab512014-03-17 09:47:44 -0400361 }
Jamie Madill5f562732014-02-14 16:41:24 -0500362 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
363 }
364 }
365 }
Jamie Madill5f562732014-02-14 16:41:24 -0500366 }
367
368 return varyingHLSL;
369}
370
Jamie Madillf2575982014-06-25 16:04:54 -0400371std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
372 const VertexFormat inputLayout[],
373 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500374{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400375 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500376
377 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400378 unsigned int inputIndex = 0;
379
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500380 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
381 {
Jamie Madillf2575982014-06-25 16:04:54 -0400382 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500383 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500384 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400385 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
386 const VertexFormat &vertexFormat = inputLayout[inputIndex];
387
Jamie Madill3b7e2052014-03-17 09:47:43 -0400388 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500389 if (IsMatrixType(shaderAttribute.type))
390 {
391 // Matrix types are always transposed
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400392 structHLSL += " " + HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500393 }
394 else
395 {
396 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400397 structHLSL += " " + HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500398 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500399
Jamie Madilla53ab512014-03-17 09:47:44 -0400400 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400401 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500402
Jamie Madill3b7e2052014-03-17 09:47:43 -0400403 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400404 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500405
406 // Mismatched vertex attribute to vertex input may result in an undefined
407 // data reinterpretation (eg for pure integer->float, float->pure integer)
408 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400409 if (IsMatrixType(shaderAttribute.type) ||
410 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500411 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400412 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500413 }
414 else
415 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400416 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500417 }
418
Jamie Madill3b7e2052014-03-17 09:47:43 -0400419 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400420
Jamie Madillac0a2672014-04-11 13:33:56 -0400421 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
422 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500423 }
424
Geoff Lang04fb89a2014-06-09 15:05:36 -0400425 std::string replacementHLSL = "struct VS_INPUT\n"
426 "{\n" +
427 structHLSL +
428 "};\n"
429 "\n"
430 "void initAttributes(VS_INPUT input)\n"
431 "{\n" +
432 initHLSL +
433 "}\n";
434
435 std::string vertexHLSL(sourceShader);
436
437 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
438 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
439
440 return vertexHLSL;
441}
442
Jamie Madillf6be8d72014-09-05 10:38:07 -0400443std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOutputVariable> &outputVariables,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400444 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
445{
446 const int shaderModel = mRenderer->getMajorShaderModel();
447 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
448 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
449
450 std::string declarationHLSL;
451 std::string copyHLSL;
Geoff Lang4ace4232014-06-18 19:12:48 -0400452
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400453 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
454 {
455 GLenum binding = outputLayout[layoutIndex];
456
457 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400458 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400459 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
460
Jamie Madillf6be8d72014-09-05 10:38:07 -0400461 const PixelShaderOutputVariable &outputVariable = GetOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400462
463 declarationHLSL += " " + HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
464 " : " + targetSemantic + Str(layoutIndex) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400465
466 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
467 }
468 }
469
470 if (usesFragDepth)
471 {
472 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
473 copyHLSL += " output.gl_Depth = gl_Depth; \n";
474 }
475
476 std::string replacementHLSL = "struct PS_OUTPUT\n"
477 "{\n" +
478 declarationHLSL +
479 "};\n"
480 "\n"
481 "PS_OUTPUT generateOutput()\n"
482 "{\n"
483 " PS_OUTPUT output;\n" +
484 copyHLSL +
485 " return output;\n"
486 "}\n";
487
488 std::string pixelHLSL(sourceShader);
489
490 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
491 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
492
493 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500494}
495
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400496std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
497{
498 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
499 // In D3D11 we manually compute gl_PointCoord in the GS.
500 int shaderModel = mRenderer->getMajorShaderModel();
501 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
502}
503
504struct DynamicHLSL::SemanticInfo
505{
506 struct BuiltinInfo
507 {
508 BuiltinInfo()
509 : enabled(false),
510 index(0),
511 systemValue(false)
512 {}
513
514 bool enabled;
515 std::string semantic;
516 unsigned int index;
517 bool systemValue;
518
519 std::string str() const
520 {
521 return (systemValue ? semantic : (semantic + Str(index)));
522 }
523
524 void enableSystem(const std::string &systemValueSemantic)
525 {
526 enabled = true;
527 semantic = systemValueSemantic;
528 systemValue = true;
529 }
530
531 void enable(const std::string &semanticVal, unsigned int indexVal)
532 {
533 enabled = true;
534 semantic = semanticVal;
535 index = indexVal;
536 }
537 };
538
539 BuiltinInfo dxPosition;
540 BuiltinInfo glPosition;
541 BuiltinInfo glFragCoord;
542 BuiltinInfo glPointCoord;
543 BuiltinInfo glPointSize;
544};
545
546DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
547 bool pointSize, bool pixelShader) const
548{
549 SemanticInfo info;
550 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
551 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
552
553 int reservedRegisterIndex = startRegisters;
554
555 if (hlsl4)
556 {
557 info.dxPosition.enableSystem("SV_Position");
558 }
559 else if (pixelShader)
560 {
561 info.dxPosition.enableSystem("VPOS");
562 }
563 else
564 {
565 info.dxPosition.enableSystem("POSITION");
566 }
567
568 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
569
570 if (fragCoord)
571 {
572 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
573 }
574
575 if (pointCoord)
576 {
577 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
578 // In D3D11 we manually compute gl_PointCoord in the GS.
579 if (hlsl4)
580 {
581 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
582 }
583 else
584 {
585 info.glPointCoord.enable("TEXCOORD", 0);
586 }
587 }
588
589 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
590 if (pointSize && (!pixelShader || hlsl4))
591 {
592 info.glPointSize.enableSystem("PSIZE");
593 }
594
595 return info;
596}
597
598std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
599{
600 std::string linkHLSL = "{\n";
601
602 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
603
604 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
605 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
606
607 if (info.glFragCoord.enabled)
608 {
609 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
610 }
611
612 if (info.glPointCoord.enabled)
613 {
614 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
615 }
616
617 linkHLSL += varyingHLSL;
618
619 if (info.glPointSize.enabled)
620 {
621 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
622 }
623
624 linkHLSL += "};\n";
625
626 return linkHLSL;
627}
628
629void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
630 std::vector<LinkedVarying> *linkedVaryings) const
631{
632 ASSERT(info.glPosition.enabled);
633
634 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
635 info.glPosition.index, 1));
636
637 if (info.glFragCoord.enabled)
638 {
639 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
640 info.glFragCoord.index, 1));
641 }
642
643 if (info.glPointSize.enabled)
644 {
645 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
646 }
647}
648
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400649void DynamicHLSL::storeUserLinkedVaryings(const rx::ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400650 std::vector<LinkedVarying> *linkedVaryings) const
651{
Brandon Jones71620962014-08-20 14:04:59 -0700652 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400653 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400654
655 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
656 {
657 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400658
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400659 if (varying.registerAssigned())
660 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400661 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400662 GLenum transposedType = TransposeMatrixType(varying.type);
663 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
664
665 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
666 varyingSemantic, varying.registerIndex,
667 variableRows * varying.elementCount()));
668 }
669 }
670}
671
Jamie Madillde8892b2014-11-11 13:00:22 -0500672bool DynamicHLSL::generateShaderLinkHLSL(const gl::Data &data, InfoLog &infoLog, int registers,
673 const VaryingPacking packing,
674 std::string &pixelHLSL, std::string &vertexHLSL,
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400675 rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader,
Jamie Madillde8892b2014-11-11 13:00:22 -0500676 const std::vector<std::string> &transformFeedbackVaryings,
Geoff Lang48dcae72014-02-05 16:28:24 -0500677 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400678 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400679 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400680 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500681{
682 if (pixelHLSL.empty() || vertexHLSL.empty())
683 {
684 return false;
685 }
686
Brandon Jones71620962014-08-20 14:04:59 -0700687 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
688 bool usesFragColor = fragmentShader->mUsesFragColor;
689 bool usesFragData = fragmentShader->mUsesFragData;
690 bool usesFragCoord = fragmentShader->mUsesFragCoord;
691 bool usesPointCoord = fragmentShader->mUsesPointCoord;
692 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400693
Jamie Madill5f562732014-02-14 16:41:24 -0500694 if (usesFragColor && usesFragData)
695 {
696 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
697 return false;
698 }
699
700 // Write the HLSL input/output declarations
701 const int shaderModel = mRenderer->getMajorShaderModel();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400702 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500703
704 // Two cases when writing to gl_FragColor and using ESSL 1.0:
705 // - with a 3.0 context, the output color is copied to channel 0
706 // - with a 2.0 context, the output color is broadcast to all channels
Jamie Madillde8892b2014-11-11 13:00:22 -0500707 const bool broadcast = (fragmentShader->mUsesFragColor && data.clientVersion < 3);
708 const unsigned int numRenderTargets = (broadcast || usesMRT ? data.caps->maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500709
Brandon Jones71620962014-08-20 14:04:59 -0700710 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500711
Jamie Madillde8892b2014-11-11 13:00:22 -0500712 if (static_cast<GLuint>(registersNeeded) > data.caps->maxVaryingVectors)
Jamie Madill5f562732014-02-14 16:41:24 -0500713 {
714 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500715 return false;
716 }
717
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400718 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
719 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
720 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500721
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400722 storeUserLinkedVaryings(vertexShader, linkedVaryings);
723 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500724
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500725 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400726 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
727 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500728 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500729 "{\n"
730 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500731
732 if (shaderModel >= 4)
733 {
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
875 if (shaderModel >= 4)
876 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400877 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
878 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500879 }
880 else if (shaderModel >= 3)
881 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400882 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
883 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500884 }
885 else
886 {
887 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
888 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
889 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
890 }
891
892 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
893 " gl_FragCoord.w = rhw;\n";
894 }
895
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400896 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500897 {
898 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
899 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
900 }
901
Brandon Jones71620962014-08-20 14:04:59 -0700902 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500903 {
904 if (shaderModel <= 3)
905 {
906 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
907 }
908 else
909 {
910 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
911 }
912 }
913
Jamie Madilld15250e2014-09-03 09:40:44 -0400914 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
915 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500916 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400917 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400918 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500919 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400920 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400921 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500922 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400923 GLenum transposedType = TransposeMatrixType(varying.type);
924 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500925 for (int row = 0; row < variableRows; row++)
926 {
Jamie Madillde8892b2014-11-11 13:00:22 -0500927 std::string n = Str(varying.registerIndex + varying.columnIndex * data.caps->maxVaryingVectors + elementIndex * variableRows + row);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400928 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500929
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400930 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500931 {
932 pixelHLSL += ArrayString(elementIndex);
933 }
934
935 if (variableRows > 1)
936 {
937 pixelHLSL += ArrayString(row);
938 }
939
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400940 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500941 {
942 pixelHLSL += " = input.v" + n + ";\n"; break;
943 }
944 else
945 {
946 switch (VariableColumnCount(transposedType))
947 {
948 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
949 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
950 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
951 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
952 default: UNREACHABLE();
953 }
954 }
955 }
956 }
957 }
Jamie Madill54ad4f82014-09-03 09:40:46 -0400958 else
959 {
960 ASSERT(varying.isBuiltIn() || !varying.staticUse);
961 }
Jamie Madill5f562732014-02-14 16:41:24 -0500962 }
963
964 pixelHLSL += "\n"
965 " gl_main();\n"
966 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400967 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500968 "}\n";
969
970 return true;
971}
972
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400973void DynamicHLSL::defineOutputVariables(rx::ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500974{
Jamie Madilld15250e2014-09-03 09:40:44 -0400975 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500976
977 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
978 {
Jamie Madillf2575982014-06-25 16:04:54 -0400979 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500980 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
981
Jamie Madill54ad4f82014-09-03 09:40:46 -0400982 ASSERT(outputVariable.staticUse);
983
Jamie Madill5f562732014-02-14 16:41:24 -0500984 if (outputVariable.arraySize > 0)
985 {
986 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
987 {
988 const int location = baseLocation + elementIndex;
989 ASSERT(programOutputVars->count(location) == 0);
990 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
991 }
992 }
993 else
994 {
995 ASSERT(programOutputVars->count(baseLocation) == 0);
996 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
997 }
998 }
999}
1000
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001001std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001002{
1003 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001004 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001005 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001006}
1007
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001008std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001009{
1010 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001011 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001012 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1013
1014 std::string geomHLSL;
1015
Brandon Jones71620962014-08-20 14:04:59 -07001016 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001017 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -07001018 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1019 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001020
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001021 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1022 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1023 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001024
Geoff Langc0b9ef42014-07-02 10:02:37 -04001025 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001026 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1027 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001028 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1029 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001030 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001031 "static float2 pointSpriteCorners[] = \n"
1032 "{\n"
1033 " float2( 0.5f, -0.5f),\n"
1034 " float2( 0.5f, 0.5f),\n"
1035 " float2(-0.5f, -0.5f),\n"
1036 " float2(-0.5f, 0.5f)\n"
1037 "};\n"
1038 "\n"
1039 "static float2 pointSpriteTexcoords[] = \n"
1040 "{\n"
1041 " float2(1.0f, 1.0f),\n"
1042 " float2(1.0f, 0.0f),\n"
1043 " float2(0.0f, 1.0f),\n"
1044 " float2(0.0f, 0.0f)\n"
1045 "};\n"
1046 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001047 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1048 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001049 "\n"
1050 "[maxvertexcount(4)]\n"
1051 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1052 "{\n"
1053 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jacek Cabana22cd472014-11-05 11:20:45 +01001054 " output.gl_Position = input[0].gl_Position;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001055 " output.gl_PointSize = input[0].gl_PointSize;\n";
1056
1057 for (int r = 0; r < registers; r++)
1058 {
1059 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1060 }
1061
Brandon Jones71620962014-08-20 14:04:59 -07001062 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001063 {
1064 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1065 }
1066
1067 geomHLSL += " \n"
1068 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001069 " float4 dx_Position = input[0].dx_Position;\n"
1070 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001071
1072 for (int corner = 0; corner < 4; corner++)
1073 {
1074 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001075 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001076
Brandon Jones71620962014-08-20 14:04:59 -07001077 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001078 {
1079 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1080 }
1081
1082 geomHLSL += " outStream.Append(output);\n";
1083 }
1084
1085 geomHLSL += " \n"
1086 " outStream.RestartStrip();\n"
1087 "}\n";
1088
1089 return geomHLSL;
1090}
1091
1092// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001093std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001094{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001095 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001096 {
1097 return "_" + name;
1098 }
1099
1100 return name;
1101}
1102
Jamie Madillf2575982014-06-25 16:04:54 -04001103std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001104{
Jamie Madilla53ab512014-03-17 09:47:44 -04001105 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001106
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001107 // Matrix
1108 if (IsMatrixType(shaderAttrib.type))
1109 {
Jamie Madill8664b062014-02-14 16:41:29 -05001110 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001111 }
1112
Jamie Madillf2575982014-06-25 16:04:54 -04001113 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1114 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001115
Jamie Madill8664b062014-02-14 16:41:29 -05001116 // Perform integer to float conversion (if necessary)
1117 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1118
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001119 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001120 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001121 // TODO: normalization for 32-bit integer formats
1122 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1123 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001124 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001125
1126 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001127 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001128}
1129
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001130void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1131{
1132 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1133 {
1134 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1135
1136 if (vertexFormat.mType == GL_NONE)
1137 {
1138 signature[inputIndex] = GL_NONE;
1139 }
1140 else
1141 {
1142 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1143 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1144 }
1145 }
1146}
1147
Jamie Madill5f562732014-02-14 16:41:24 -05001148}