blob: 8f05fa192aa7b12fd4f3d9eba2d404eff593e6c6 [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/Shader.h"
13#include "libGLESv2/Program.h"
Jamie Madill5f562732014-02-14 16:41:24 -050014#include "libGLESv2/ProgramBinary.h"
Jamie Madill8664b062014-02-14 16:41:29 -050015#include "libGLESv2/formatutils.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040016
17#include "common/utilities.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040018#include "common/blocklayout.h"
Jamie Madill5f562732014-02-14 16:41:24 -050019
Jamie Madill53cb14d2014-07-08 15:02:35 -040020// For use with ArrayString, see angleutils.h
21META_ASSERT(GL_INVALID_INDEX == UINT_MAX);
Jamie Madill5f562732014-02-14 16:41:24 -050022
Brandon Jonesd8d72432014-08-22 15:11:23 -070023using namespace gl;
24
Jamie Madill3f2e61d2014-09-05 10:38:05 -040025namespace
Jamie Madill5f562732014-02-14 16:41:24 -050026{
Jamie Madill8664b062014-02-14 16:41:29 -050027
28std::string HLSLComponentTypeString(GLenum componentType)
29{
30 switch (componentType)
31 {
32 case GL_UNSIGNED_INT: return "uint";
33 case GL_INT: return "int";
34 case GL_UNSIGNED_NORMALIZED:
35 case GL_SIGNED_NORMALIZED:
36 case GL_FLOAT: return "float";
37 default: UNREACHABLE(); return "not-component-type";
38 }
Jamie Madill5f562732014-02-14 16:41:24 -050039}
40
Jamie Madill8664b062014-02-14 16:41:29 -050041std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
42{
43 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
44}
45
46std::string HLSLMatrixTypeString(GLenum type)
47{
48 switch (type)
49 {
50 case GL_FLOAT_MAT2: return "float2x2";
51 case GL_FLOAT_MAT3: return "float3x3";
52 case GL_FLOAT_MAT4: return "float4x4";
53 case GL_FLOAT_MAT2x3: return "float2x3";
54 case GL_FLOAT_MAT3x2: return "float3x2";
55 case GL_FLOAT_MAT2x4: return "float2x4";
56 case GL_FLOAT_MAT4x2: return "float4x2";
57 case GL_FLOAT_MAT3x4: return "float3x4";
58 case GL_FLOAT_MAT4x3: return "float4x3";
59 default: UNREACHABLE(); return "not-matrix-type";
60 }
61}
62
63std::string HLSLTypeString(GLenum type)
64{
65 if (gl::IsMatrixType(type))
66 {
67 return HLSLMatrixTypeString(type);
68 }
69
Jamie Madillf2575982014-06-25 16:04:54 -040070 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050071}
72
Jamie Madillf6be8d72014-09-05 10:38:07 -040073const rx::PixelShaderOutputVariable &GetOutputAtLocation(const std::vector<rx::PixelShaderOutputVariable> &outputVariables,
Jamie Madill3f2e61d2014-09-05 10:38:05 -040074 unsigned int location)
75{
76 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
77 {
78 if (outputVariables[variableIndex].outputIndex == location)
79 {
80 return outputVariables[variableIndex];
81 }
82 }
83
84 UNREACHABLE();
85 return outputVariables[0];
86}
87
Jamie Madill8664b062014-02-14 16:41:29 -050088}
89
Brandon Jonesd8d72432014-08-22 15:11:23 -070090namespace rx
Jamie Madill8664b062014-02-14 16:41:29 -050091{
92
Geoff Lang04fb89a2014-06-09 15:05:36 -040093const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
94const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050095
Jamie 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,
229 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 Madillff0d2ba2014-05-14 13:49:10 -0400672bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500673 std::string& pixelHLSL, std::string& vertexHLSL,
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400674 rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500675 const std::vector<std::string>& transformFeedbackVaryings,
676 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();
Geoff Lang3a61c322014-07-10 13:01:54 -0400701
702 // TODO (geofflang): Use context's caps
703 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Jamie Madill5f562732014-02-14 16:41:24 -0500704
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400705 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500706
707 // Two cases when writing to gl_FragColor and using ESSL 1.0:
708 // - with a 3.0 context, the output color is copied to channel 0
709 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jones71620962014-08-20 14:04:59 -0700710 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400711 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500712
Brandon Jones71620962014-08-20 14:04:59 -0700713 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500714
715 if (registersNeeded > maxVaryingVectors)
716 {
717 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500718 return false;
719 }
720
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400721 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
722 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
723 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500724
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400725 storeUserLinkedVaryings(vertexShader, linkedVaryings);
726 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500727
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500728 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400729 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
730 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500731 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500732 "{\n"
733 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500734
735 if (shaderModel >= 4)
736 {
737 vertexHLSL += "\n"
738 " gl_main();\n"
739 "\n"
740 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500741 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400742 " output.dx_Position.x = gl_Position.x;\n"
743 " output.dx_Position.y = -gl_Position.y;\n"
744 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
745 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500746 }
747 else
748 {
749 vertexHLSL += "\n"
750 " gl_main();\n"
751 "\n"
752 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500753 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400754 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
755 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
756 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
757 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500758 }
759
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400760 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500761 {
762 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
763 }
764
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400765 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500766 {
767 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
768 }
769
Jamie Madilld15250e2014-09-03 09:40:44 -0400770 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
771 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500772 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400773 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400774 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500775 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400776 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500777 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400778 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500779
780 for (int row = 0; row < variableRows; row++)
781 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700782 int r = varying.registerIndex + varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500783 vertexHLSL += " output.v" + Str(r);
784
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400785 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500786
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400787 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500788 {
789 vertexHLSL += ArrayString(elementIndex);
790 }
791
792 if (variableRows > 1)
793 {
794 vertexHLSL += ArrayString(row);
795 }
796
797 vertexHLSL += ";\n";
798 }
799 }
800 }
801 }
802
803 vertexHLSL += "\n"
804 " return output;\n"
805 "}\n";
806
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400807 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
808 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500809
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400810 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500811
812 if (shaderVersion < 300)
813 {
814 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
815 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400816 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400817 outputKeyVariable.type = GL_FLOAT_VEC4;
818 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
819 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
820 outputKeyVariable.outputIndex = renderTargetIndex;
821
822 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500823 }
824
Brandon Jones71620962014-08-20 14:04:59 -0700825 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500826 }
827 else
828 {
829 defineOutputVariables(fragmentShader, programOutputVars);
830
Jamie Madilld15250e2014-09-03 09:40:44 -0400831 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500832 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
833 {
834 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400835 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400836 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500837 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
838
Jamie Madill54ad4f82014-09-03 09:40:46 -0400839 ASSERT(outputVariable.staticUse);
840
Jamie Madillf6be8d72014-09-05 10:38:07 -0400841 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400842 outputKeyVariable.type = outputVariable.type;
843 outputKeyVariable.name = variableName + elementString;
844 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
845 outputKeyVariable.outputIndex = locationIt->first;
846
847 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500848 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400849
850 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500851 }
852
Geoff Lang04fb89a2014-06-09 15:05:36 -0400853 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500854
Brandon Jones71620962014-08-20 14:04:59 -0700855 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500856 {
857 if (shaderModel >= 4)
858 {
859 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
860 "{\n";
861 }
862 else
863 {
864 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
865 "{\n";
866 }
867 }
868 else
869 {
870 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
871 "{\n";
872 }
873
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400874 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500875 {
876 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
877
878 if (shaderModel >= 4)
879 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400880 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
881 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500882 }
883 else if (shaderModel >= 3)
884 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400885 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
886 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500887 }
888 else
889 {
890 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
891 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
892 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
893 }
894
895 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
896 " gl_FragCoord.w = rhw;\n";
897 }
898
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400899 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500900 {
901 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
902 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
903 }
904
Brandon Jones71620962014-08-20 14:04:59 -0700905 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500906 {
907 if (shaderModel <= 3)
908 {
909 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
910 }
911 else
912 {
913 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
914 }
915 }
916
Jamie Madilld15250e2014-09-03 09:40:44 -0400917 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
918 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500919 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400920 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400921 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500922 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400923 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400924 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500925 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400926 GLenum transposedType = TransposeMatrixType(varying.type);
927 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500928 for (int row = 0; row < variableRows; row++)
929 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700930 std::string n = Str(varying.registerIndex + varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors + elementIndex * variableRows + row);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400931 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500932
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400933 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500934 {
935 pixelHLSL += ArrayString(elementIndex);
936 }
937
938 if (variableRows > 1)
939 {
940 pixelHLSL += ArrayString(row);
941 }
942
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400943 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500944 {
945 pixelHLSL += " = input.v" + n + ";\n"; break;
946 }
947 else
948 {
949 switch (VariableColumnCount(transposedType))
950 {
951 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
952 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
953 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
954 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
955 default: UNREACHABLE();
956 }
957 }
958 }
959 }
960 }
Jamie Madill54ad4f82014-09-03 09:40:46 -0400961 else
962 {
963 ASSERT(varying.isBuiltIn() || !varying.staticUse);
964 }
Jamie Madill5f562732014-02-14 16:41:24 -0500965 }
966
967 pixelHLSL += "\n"
968 " gl_main();\n"
969 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400970 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500971 "}\n";
972
973 return true;
974}
975
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400976void DynamicHLSL::defineOutputVariables(rx::ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500977{
Jamie Madilld15250e2014-09-03 09:40:44 -0400978 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500979
980 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
981 {
Jamie Madillf2575982014-06-25 16:04:54 -0400982 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500983 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
984
Jamie Madill54ad4f82014-09-03 09:40:46 -0400985 ASSERT(outputVariable.staticUse);
986
Jamie Madill5f562732014-02-14 16:41:24 -0500987 if (outputVariable.arraySize > 0)
988 {
989 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
990 {
991 const int location = baseLocation + elementIndex;
992 ASSERT(programOutputVars->count(location) == 0);
993 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
994 }
995 }
996 else
997 {
998 ASSERT(programOutputVars->count(baseLocation) == 0);
999 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
1000 }
1001 }
1002}
1003
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001004std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001005{
1006 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001007 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001008 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001009}
1010
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001011std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001012{
1013 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001014 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001015 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1016
1017 std::string geomHLSL;
1018
Brandon Jones71620962014-08-20 14:04:59 -07001019 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001020 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -07001021 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1022 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001023
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001024 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1025 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1026 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001027
Geoff Langc0b9ef42014-07-02 10:02:37 -04001028 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001029 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1030 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001031 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1032 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001033 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001034 "static float2 pointSpriteCorners[] = \n"
1035 "{\n"
1036 " float2( 0.5f, -0.5f),\n"
1037 " float2( 0.5f, 0.5f),\n"
1038 " float2(-0.5f, -0.5f),\n"
1039 " float2(-0.5f, 0.5f)\n"
1040 "};\n"
1041 "\n"
1042 "static float2 pointSpriteTexcoords[] = \n"
1043 "{\n"
1044 " float2(1.0f, 1.0f),\n"
1045 " float2(1.0f, 0.0f),\n"
1046 " float2(0.0f, 1.0f),\n"
1047 " float2(0.0f, 0.0f)\n"
1048 "};\n"
1049 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001050 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1051 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001052 "\n"
1053 "[maxvertexcount(4)]\n"
1054 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1055 "{\n"
1056 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jacek Cabana22cd472014-11-05 11:20:45 +01001057 " output.gl_Position = input[0].gl_Position;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001058 " output.gl_PointSize = input[0].gl_PointSize;\n";
1059
1060 for (int r = 0; r < registers; r++)
1061 {
1062 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1063 }
1064
Brandon Jones71620962014-08-20 14:04:59 -07001065 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001066 {
1067 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1068 }
1069
1070 geomHLSL += " \n"
1071 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001072 " float4 dx_Position = input[0].dx_Position;\n"
1073 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001074
1075 for (int corner = 0; corner < 4; corner++)
1076 {
1077 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001078 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001079
Brandon Jones71620962014-08-20 14:04:59 -07001080 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001081 {
1082 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1083 }
1084
1085 geomHLSL += " outStream.Append(output);\n";
1086 }
1087
1088 geomHLSL += " \n"
1089 " outStream.RestartStrip();\n"
1090 "}\n";
1091
1092 return geomHLSL;
1093}
1094
1095// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001096std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001097{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001098 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001099 {
1100 return "_" + name;
1101 }
1102
1103 return name;
1104}
1105
Jamie Madillf2575982014-06-25 16:04:54 -04001106std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001107{
Jamie Madilla53ab512014-03-17 09:47:44 -04001108 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001109
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001110 // Matrix
1111 if (IsMatrixType(shaderAttrib.type))
1112 {
Jamie Madill8664b062014-02-14 16:41:29 -05001113 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001114 }
1115
Jamie Madillf2575982014-06-25 16:04:54 -04001116 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1117 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001118
Jamie Madill8664b062014-02-14 16:41:29 -05001119 // Perform integer to float conversion (if necessary)
1120 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1121
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001122 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001123 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001124 // TODO: normalization for 32-bit integer formats
1125 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1126 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001127 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001128
1129 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001130 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001131}
1132
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001133void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1134{
1135 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1136 {
1137 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1138
1139 if (vertexFormat.mType == GL_NONE)
1140 {
1141 signature[inputIndex] = GL_NONE;
1142 }
1143 else
1144 {
1145 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1146 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1147 }
1148 }
1149}
1150
Jamie Madill5f562732014-02-14 16:41:24 -05001151}