blob: 13411ebe64a703535533ee7c51536bd934742491 [file] [log] [blame]
Jamie Madill5f562732014-02-14 16:41:24 -05001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// DynamicHLSL.cpp: Implementation for link and run-time HLSL generation
7//
8
Brandon Jonesd8d72432014-08-22 15:11:23 -07009#include "libGLESv2/renderer/d3d/DynamicHLSL.h"
Brandon Jonesf05cdee2014-08-27 15:24:07 -070010#include "libGLESv2/renderer/d3d/ShaderD3D.h"
Jamie Madill5f562732014-02-14 16:41:24 -050011#include "libGLESv2/renderer/Renderer.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040012#include "libGLESv2/Shader.h"
13#include "libGLESv2/Program.h"
Jamie Madill5f562732014-02-14 16:41:24 -050014#include "libGLESv2/ProgramBinary.h"
Jamie Madill8664b062014-02-14 16:41:29 -050015#include "libGLESv2/formatutils.h"
Geoff Lang0b7eef72014-06-12 14:10:47 -040016
17#include "common/utilities.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040018#include "common/blocklayout.h"
Jamie Madill5f562732014-02-14 16:41:24 -050019
Jamie Madill53cb14d2014-07-08 15:02:35 -040020// For use with ArrayString, see angleutils.h
21META_ASSERT(GL_INVALID_INDEX == UINT_MAX);
Jamie Madill5f562732014-02-14 16:41:24 -050022
Brandon Jonesd8d72432014-08-22 15:11:23 -070023using namespace gl;
24
Jamie Madill3f2e61d2014-09-05 10:38:05 -040025namespace
Jamie Madill5f562732014-02-14 16:41:24 -050026{
Jamie Madill8664b062014-02-14 16:41:29 -050027
28std::string HLSLComponentTypeString(GLenum componentType)
29{
30 switch (componentType)
31 {
32 case GL_UNSIGNED_INT: return "uint";
33 case GL_INT: return "int";
34 case GL_UNSIGNED_NORMALIZED:
35 case GL_SIGNED_NORMALIZED:
36 case GL_FLOAT: return "float";
37 default: UNREACHABLE(); return "not-component-type";
38 }
Jamie Madill5f562732014-02-14 16:41:24 -050039}
40
Jamie Madill8664b062014-02-14 16:41:29 -050041std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
42{
43 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
44}
45
46std::string HLSLMatrixTypeString(GLenum type)
47{
48 switch (type)
49 {
50 case GL_FLOAT_MAT2: return "float2x2";
51 case GL_FLOAT_MAT3: return "float3x3";
52 case GL_FLOAT_MAT4: return "float4x4";
53 case GL_FLOAT_MAT2x3: return "float2x3";
54 case GL_FLOAT_MAT3x2: return "float3x2";
55 case GL_FLOAT_MAT2x4: return "float2x4";
56 case GL_FLOAT_MAT4x2: return "float4x2";
57 case GL_FLOAT_MAT3x4: return "float3x4";
58 case GL_FLOAT_MAT4x3: return "float4x3";
59 default: UNREACHABLE(); return "not-matrix-type";
60 }
61}
62
63std::string HLSLTypeString(GLenum type)
64{
65 if (gl::IsMatrixType(type))
66 {
67 return HLSLMatrixTypeString(type);
68 }
69
Jamie Madillf2575982014-06-25 16:04:54 -040070 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050071}
72
Jamie Madillf6be8d72014-09-05 10:38:07 -040073const rx::PixelShaderOutputVariable &GetOutputAtLocation(const std::vector<rx::PixelShaderOutputVariable> &outputVariables,
Jamie Madill3f2e61d2014-09-05 10:38:05 -040074 unsigned int location)
75{
76 for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex)
77 {
78 if (outputVariables[variableIndex].outputIndex == location)
79 {
80 return outputVariables[variableIndex];
81 }
82 }
83
84 UNREACHABLE();
85 return outputVariables[0];
86}
87
Jamie Madill8664b062014-02-14 16:41:29 -050088}
89
Brandon Jonesd8d72432014-08-22 15:11:23 -070090namespace rx
Jamie Madill8664b062014-02-14 16:41:29 -050091{
92
Geoff Lang04fb89a2014-06-09 15:05:36 -040093const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
94const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050095
Jamie Madill5f562732014-02-14 16:41:24 -050096DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
97 : mRenderer(renderer)
98{
99}
100
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400101static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -0500102{
Geoff Lang48dcae72014-02-05 16:28:24 -0500103 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -0500104
Geoff Lang48dcae72014-02-05 16:28:24 -0500105 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -0400106 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -0500107 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500108
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400109 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -0500110 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400111 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -0500112 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500113 bool available = true;
114
115 for (int y = 0; y < registers && available; y++)
116 {
117 for (int x = 0; x < elements && available; x++)
118 {
119 if (packing[r + y][x])
120 {
121 available = false;
122 }
123 }
124 }
125
126 if (available)
127 {
128 varying->registerIndex = r;
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];
265 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
266 {
267 bool found = false;
Jamie Madilld15250e2014-09-03 09:40:44 -0400268 for (unsigned int varyingIndex = 0; varyingIndex < vertexVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500269 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400270 PackedVarying *varying = &vertexVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500271 if (transformFeedbackVarying == varying->name)
272 {
273 if (!packVarying(varying, maxVaryingVectors, packing))
274 {
275 infoLog.append("Could not pack varying %s", varying->name.c_str());
276 return -1;
277 }
278
279 found = true;
280 break;
281 }
282 }
283
284 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
285 {
286 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
287 return -1;
288 }
289 }
290 }
291
Jamie Madill5f562732014-02-14 16:41:24 -0500292 // Return the number of used registers
293 int registers = 0;
294
295 for (int r = 0; r < maxVaryingVectors; r++)
296 {
297 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
298 {
299 registers++;
300 }
301 }
302
303 return registers;
304}
305
Jamie Madill54ad4f82014-09-03 09:40:46 -0400306std::string DynamicHLSL::generateVaryingHLSL(const ShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500307{
Brandon Jones71620962014-08-20 14:04:59 -0700308 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500309 std::string varyingHLSL;
310
Jamie Madill54ad4f82014-09-03 09:40:46 -0400311 const std::vector<gl::PackedVarying> &varyings = shader->getVaryings();
312
Jamie Madilld15250e2014-09-03 09:40:44 -0400313 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500314 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400315 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400316 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500317 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400318 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400319 GLenum transposedType = TransposeMatrixType(varying.type);
320 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500321
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400322 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500323 {
Jamie Madill5f562732014-02-14 16:41:24 -0500324 for (int row = 0; row < variableRows; row++)
325 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700326 // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many registers being used.
327 // 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.
328 // If the float varying has the 'nointerpolation' modifier on it then we would need N + 1 registers, and D3D compilation will fail.
329
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400330 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500331 {
Jamie Madillf2575982014-06-25 16:04:54 -0400332 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
333 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
334 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500335 default: UNREACHABLE();
336 }
337
Austin Kinrossaf875522014-08-25 21:06:07 -0700338 unsigned int semanticIndex = elementIndex * variableRows + varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500339 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500340
Jamie Madilla53ab512014-03-17 09:47:44 -0400341 std::string typeString;
342
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400343 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400344 {
345 // matrices within structs are not transposed, so
346 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400347 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400348 }
349 else
350 {
Jamie Madillf2575982014-06-25 16:04:54 -0400351 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400352 int columnCount = VariableColumnCount(transposedType);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400353 typeString = HLSLComponentTypeString(componentType, columnCount);
Jamie Madilla53ab512014-03-17 09:47:44 -0400354 }
Jamie Madill5f562732014-02-14 16:41:24 -0500355 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
356 }
357 }
358 }
Jamie Madill5f562732014-02-14 16:41:24 -0500359 }
360
361 return varyingHLSL;
362}
363
Jamie Madillf2575982014-06-25 16:04:54 -0400364std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
365 const VertexFormat inputLayout[],
366 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500367{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400368 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500369
370 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400371 unsigned int inputIndex = 0;
372
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500373 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
374 {
Jamie Madillf2575982014-06-25 16:04:54 -0400375 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madill8664b062014-02-14 16:41:29 -0500376 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500377 {
Geoff Lang5ac5ae82014-09-09 10:14:17 -0400378 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
379 const VertexFormat &vertexFormat = inputLayout[inputIndex];
380
Jamie Madill3b7e2052014-03-17 09:47:43 -0400381 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500382 if (IsMatrixType(shaderAttribute.type))
383 {
384 // Matrix types are always transposed
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400385 structHLSL += " " + HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500386 }
387 else
388 {
389 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400390 structHLSL += " " + HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500391 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500392
Jamie Madilla53ab512014-03-17 09:47:44 -0400393 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400394 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500395
Jamie Madill3b7e2052014-03-17 09:47:43 -0400396 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400397 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500398
399 // Mismatched vertex attribute to vertex input may result in an undefined
400 // data reinterpretation (eg for pure integer->float, float->pure integer)
401 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400402 if (IsMatrixType(shaderAttribute.type) ||
403 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500404 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400405 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500406 }
407 else
408 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400409 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500410 }
411
Jamie Madill3b7e2052014-03-17 09:47:43 -0400412 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400413
Jamie Madillac0a2672014-04-11 13:33:56 -0400414 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
415 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500416 }
417
Geoff Lang04fb89a2014-06-09 15:05:36 -0400418 std::string replacementHLSL = "struct VS_INPUT\n"
419 "{\n" +
420 structHLSL +
421 "};\n"
422 "\n"
423 "void initAttributes(VS_INPUT input)\n"
424 "{\n" +
425 initHLSL +
426 "}\n";
427
428 std::string vertexHLSL(sourceShader);
429
430 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
431 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
432
433 return vertexHLSL;
434}
435
Jamie Madillf6be8d72014-09-05 10:38:07 -0400436std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOutputVariable> &outputVariables,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400437 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
438{
439 const int shaderModel = mRenderer->getMajorShaderModel();
440 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
441 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
442
443 std::string declarationHLSL;
444 std::string copyHLSL;
Geoff Lang4ace4232014-06-18 19:12:48 -0400445
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400446 for (size_t layoutIndex = 0; layoutIndex < outputLayout.size(); ++layoutIndex)
447 {
448 GLenum binding = outputLayout[layoutIndex];
449
450 if (binding != GL_NONE)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400451 {
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400452 unsigned int location = (binding - GL_COLOR_ATTACHMENT0);
453
Jamie Madillf6be8d72014-09-05 10:38:07 -0400454 const PixelShaderOutputVariable &outputVariable = GetOutputAtLocation(outputVariables, location);
Jamie Madill3f2e61d2014-09-05 10:38:05 -0400455
456 declarationHLSL += " " + HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
457 " : " + targetSemantic + Str(layoutIndex) + ";\n";
Geoff Lang04fb89a2014-06-09 15:05:36 -0400458
459 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
460 }
461 }
462
463 if (usesFragDepth)
464 {
465 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
466 copyHLSL += " output.gl_Depth = gl_Depth; \n";
467 }
468
469 std::string replacementHLSL = "struct PS_OUTPUT\n"
470 "{\n" +
471 declarationHLSL +
472 "};\n"
473 "\n"
474 "PS_OUTPUT generateOutput()\n"
475 "{\n"
476 " PS_OUTPUT output;\n" +
477 copyHLSL +
478 " return output;\n"
479 "}\n";
480
481 std::string pixelHLSL(sourceShader);
482
483 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
484 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
485
486 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500487}
488
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400489std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
490{
491 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
492 // In D3D11 we manually compute gl_PointCoord in the GS.
493 int shaderModel = mRenderer->getMajorShaderModel();
494 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
495}
496
497struct DynamicHLSL::SemanticInfo
498{
499 struct BuiltinInfo
500 {
501 BuiltinInfo()
502 : enabled(false),
503 index(0),
504 systemValue(false)
505 {}
506
507 bool enabled;
508 std::string semantic;
509 unsigned int index;
510 bool systemValue;
511
512 std::string str() const
513 {
514 return (systemValue ? semantic : (semantic + Str(index)));
515 }
516
517 void enableSystem(const std::string &systemValueSemantic)
518 {
519 enabled = true;
520 semantic = systemValueSemantic;
521 systemValue = true;
522 }
523
524 void enable(const std::string &semanticVal, unsigned int indexVal)
525 {
526 enabled = true;
527 semantic = semanticVal;
528 index = indexVal;
529 }
530 };
531
532 BuiltinInfo dxPosition;
533 BuiltinInfo glPosition;
534 BuiltinInfo glFragCoord;
535 BuiltinInfo glPointCoord;
536 BuiltinInfo glPointSize;
537};
538
539DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
540 bool pointSize, bool pixelShader) const
541{
542 SemanticInfo info;
543 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
544 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
545
546 int reservedRegisterIndex = startRegisters;
547
548 if (hlsl4)
549 {
550 info.dxPosition.enableSystem("SV_Position");
551 }
552 else if (pixelShader)
553 {
554 info.dxPosition.enableSystem("VPOS");
555 }
556 else
557 {
558 info.dxPosition.enableSystem("POSITION");
559 }
560
561 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
562
563 if (fragCoord)
564 {
565 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
566 }
567
568 if (pointCoord)
569 {
570 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
571 // In D3D11 we manually compute gl_PointCoord in the GS.
572 if (hlsl4)
573 {
574 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
575 }
576 else
577 {
578 info.glPointCoord.enable("TEXCOORD", 0);
579 }
580 }
581
582 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
583 if (pointSize && (!pixelShader || hlsl4))
584 {
585 info.glPointSize.enableSystem("PSIZE");
586 }
587
588 return info;
589}
590
591std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
592{
593 std::string linkHLSL = "{\n";
594
595 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
596
597 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
598 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
599
600 if (info.glFragCoord.enabled)
601 {
602 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
603 }
604
605 if (info.glPointCoord.enabled)
606 {
607 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
608 }
609
610 linkHLSL += varyingHLSL;
611
612 if (info.glPointSize.enabled)
613 {
614 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
615 }
616
617 linkHLSL += "};\n";
618
619 return linkHLSL;
620}
621
622void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
623 std::vector<LinkedVarying> *linkedVaryings) const
624{
625 ASSERT(info.glPosition.enabled);
626
627 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
628 info.glPosition.index, 1));
629
630 if (info.glFragCoord.enabled)
631 {
632 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
633 info.glFragCoord.index, 1));
634 }
635
636 if (info.glPointSize.enabled)
637 {
638 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
639 }
640}
641
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400642void DynamicHLSL::storeUserLinkedVaryings(const rx::ShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400643 std::vector<LinkedVarying> *linkedVaryings) const
644{
Brandon Jones71620962014-08-20 14:04:59 -0700645 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400646 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400647
648 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
649 {
650 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madill54ad4f82014-09-03 09:40:46 -0400651
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400652 if (varying.registerAssigned())
653 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400654 ASSERT(!varying.isBuiltIn());
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400655 GLenum transposedType = TransposeMatrixType(varying.type);
656 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
657
658 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
659 varyingSemantic, varying.registerIndex,
660 variableRows * varying.elementCount()));
661 }
662 }
663}
664
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400665bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500666 std::string& pixelHLSL, std::string& vertexHLSL,
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400667 rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500668 const std::vector<std::string>& transformFeedbackVaryings,
669 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400670 std::map<int, VariableLocation> *programOutputVars,
Jamie Madillf6be8d72014-09-05 10:38:07 -0400671 std::vector<PixelShaderOutputVariable> *outPixelShaderKey,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400672 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500673{
674 if (pixelHLSL.empty() || vertexHLSL.empty())
675 {
676 return false;
677 }
678
Brandon Jones71620962014-08-20 14:04:59 -0700679 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
680 bool usesFragColor = fragmentShader->mUsesFragColor;
681 bool usesFragData = fragmentShader->mUsesFragData;
682 bool usesFragCoord = fragmentShader->mUsesFragCoord;
683 bool usesPointCoord = fragmentShader->mUsesPointCoord;
684 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400685
Jamie Madill5f562732014-02-14 16:41:24 -0500686 if (usesFragColor && usesFragData)
687 {
688 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
689 return false;
690 }
691
692 // Write the HLSL input/output declarations
693 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang3a61c322014-07-10 13:01:54 -0400694
695 // TODO (geofflang): Use context's caps
696 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Jamie Madill5f562732014-02-14 16:41:24 -0500697
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400698 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500699
700 // Two cases when writing to gl_FragColor and using ESSL 1.0:
701 // - with a 3.0 context, the output color is copied to channel 0
702 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jones71620962014-08-20 14:04:59 -0700703 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400704 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500705
Brandon Jones71620962014-08-20 14:04:59 -0700706 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500707
708 if (registersNeeded > maxVaryingVectors)
709 {
710 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500711 return false;
712 }
713
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400714 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
715 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
716 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500717
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400718 storeUserLinkedVaryings(vertexShader, linkedVaryings);
719 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500720
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500721 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400722 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
723 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500724 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500725 "{\n"
726 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500727
728 if (shaderModel >= 4)
729 {
730 vertexHLSL += "\n"
731 " gl_main();\n"
732 "\n"
733 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500734 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400735 " output.dx_Position.x = gl_Position.x;\n"
736 " output.dx_Position.y = -gl_Position.y;\n"
737 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
738 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500739 }
740 else
741 {
742 vertexHLSL += "\n"
743 " gl_main();\n"
744 "\n"
745 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500746 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400747 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
748 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
749 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
750 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500751 }
752
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400753 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500754 {
755 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
756 }
757
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400758 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500759 {
760 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
761 }
762
Jamie Madilld15250e2014-09-03 09:40:44 -0400763 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
764 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500765 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400766 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400767 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500768 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400769 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500770 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400771 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500772
773 for (int row = 0; row < variableRows; row++)
774 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700775 int r = varying.registerIndex + varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500776 vertexHLSL += " output.v" + Str(r);
777
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400778 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500779
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400780 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500781 {
782 vertexHLSL += ArrayString(elementIndex);
783 }
784
785 if (variableRows > 1)
786 {
787 vertexHLSL += ArrayString(row);
788 }
789
790 vertexHLSL += ";\n";
791 }
792 }
793 }
794 }
795
796 vertexHLSL += "\n"
797 " return output;\n"
798 "}\n";
799
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400800 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
801 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500802
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400803 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500804
805 if (shaderVersion < 300)
806 {
807 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
808 {
Jamie Madillf6be8d72014-09-05 10:38:07 -0400809 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400810 outputKeyVariable.type = GL_FLOAT_VEC4;
811 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
812 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
813 outputKeyVariable.outputIndex = renderTargetIndex;
814
815 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500816 }
817
Brandon Jones71620962014-08-20 14:04:59 -0700818 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500819 }
820 else
821 {
822 defineOutputVariables(fragmentShader, programOutputVars);
823
Jamie Madilld15250e2014-09-03 09:40:44 -0400824 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500825 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
826 {
827 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400828 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400829 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500830 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
831
Jamie Madill54ad4f82014-09-03 09:40:46 -0400832 ASSERT(outputVariable.staticUse);
833
Jamie Madillf6be8d72014-09-05 10:38:07 -0400834 PixelShaderOutputVariable outputKeyVariable;
Geoff Lang04fb89a2014-06-09 15:05:36 -0400835 outputKeyVariable.type = outputVariable.type;
836 outputKeyVariable.name = variableName + elementString;
837 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
838 outputKeyVariable.outputIndex = locationIt->first;
839
840 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500841 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400842
843 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500844 }
845
Geoff Lang04fb89a2014-06-09 15:05:36 -0400846 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500847
Brandon Jones71620962014-08-20 14:04:59 -0700848 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500849 {
850 if (shaderModel >= 4)
851 {
852 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
853 "{\n";
854 }
855 else
856 {
857 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
858 "{\n";
859 }
860 }
861 else
862 {
863 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
864 "{\n";
865 }
866
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400867 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500868 {
869 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
870
871 if (shaderModel >= 4)
872 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400873 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
874 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500875 }
876 else if (shaderModel >= 3)
877 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400878 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
879 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500880 }
881 else
882 {
883 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
884 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
885 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
886 }
887
888 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
889 " gl_FragCoord.w = rhw;\n";
890 }
891
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400892 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500893 {
894 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
895 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
896 }
897
Brandon Jones71620962014-08-20 14:04:59 -0700898 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500899 {
900 if (shaderModel <= 3)
901 {
902 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
903 }
904 else
905 {
906 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
907 }
908 }
909
Jamie Madilld15250e2014-09-03 09:40:44 -0400910 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
911 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500912 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400913 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400914 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500915 {
Jamie Madill54ad4f82014-09-03 09:40:46 -0400916 ASSERT(!varying.isBuiltIn());
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400917 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500918 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400919 GLenum transposedType = TransposeMatrixType(varying.type);
920 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500921 for (int row = 0; row < variableRows; row++)
922 {
Austin Kinrossaf875522014-08-25 21:06:07 -0700923 std::string n = Str(varying.registerIndex + varying.columnIndex * mRenderer->getRendererCaps().maxVaryingVectors + elementIndex * variableRows + row);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400924 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500925
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400926 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500927 {
928 pixelHLSL += ArrayString(elementIndex);
929 }
930
931 if (variableRows > 1)
932 {
933 pixelHLSL += ArrayString(row);
934 }
935
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400936 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500937 {
938 pixelHLSL += " = input.v" + n + ";\n"; break;
939 }
940 else
941 {
942 switch (VariableColumnCount(transposedType))
943 {
944 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
945 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
946 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
947 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
948 default: UNREACHABLE();
949 }
950 }
951 }
952 }
953 }
Jamie Madill54ad4f82014-09-03 09:40:46 -0400954 else
955 {
956 ASSERT(varying.isBuiltIn() || !varying.staticUse);
957 }
Jamie Madill5f562732014-02-14 16:41:24 -0500958 }
959
960 pixelHLSL += "\n"
961 " gl_main();\n"
962 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400963 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500964 "}\n";
965
966 return true;
967}
968
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400969void DynamicHLSL::defineOutputVariables(rx::ShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500970{
Jamie Madilld15250e2014-09-03 09:40:44 -0400971 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500972
973 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
974 {
Jamie Madillf2575982014-06-25 16:04:54 -0400975 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500976 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
977
Jamie Madill54ad4f82014-09-03 09:40:46 -0400978 ASSERT(outputVariable.staticUse);
979
Jamie Madill5f562732014-02-14 16:41:24 -0500980 if (outputVariable.arraySize > 0)
981 {
982 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
983 {
984 const int location = baseLocation + elementIndex;
985 ASSERT(programOutputVars->count(location) == 0);
986 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
987 }
988 }
989 else
990 {
991 ASSERT(programOutputVars->count(baseLocation) == 0);
992 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
993 }
994 }
995}
996
Jamie Madill2ad1dc42014-09-03 09:40:45 -0400997std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500998{
999 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -07001000 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -04001001 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -05001002}
1003
Jamie Madill2ad1dc42014-09-03 09:40:45 -04001004std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::ShaderD3D *fragmentShader, rx::ShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -05001005{
1006 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -07001007 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -05001008 ASSERT(mRenderer->getMajorShaderModel() >= 4);
1009
1010 std::string geomHLSL;
1011
Brandon Jones71620962014-08-20 14:04:59 -07001012 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001013 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -07001014 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1015 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001016
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001017 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1018 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1019 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001020
Geoff Langc0b9ef42014-07-02 10:02:37 -04001021 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001022 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1023 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001024 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1025 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001026 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001027 "static float2 pointSpriteCorners[] = \n"
1028 "{\n"
1029 " float2( 0.5f, -0.5f),\n"
1030 " float2( 0.5f, 0.5f),\n"
1031 " float2(-0.5f, -0.5f),\n"
1032 " float2(-0.5f, 0.5f)\n"
1033 "};\n"
1034 "\n"
1035 "static float2 pointSpriteTexcoords[] = \n"
1036 "{\n"
1037 " float2(1.0f, 1.0f),\n"
1038 " float2(1.0f, 0.0f),\n"
1039 " float2(0.0f, 1.0f),\n"
1040 " float2(0.0f, 0.0f)\n"
1041 "};\n"
1042 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001043 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1044 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001045 "\n"
1046 "[maxvertexcount(4)]\n"
1047 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1048 "{\n"
1049 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001050 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001051 " output.gl_PointSize = input[0].gl_PointSize;\n";
1052
1053 for (int r = 0; r < registers; r++)
1054 {
1055 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1056 }
1057
Brandon Jones71620962014-08-20 14:04:59 -07001058 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001059 {
1060 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1061 }
1062
1063 geomHLSL += " \n"
1064 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001065 " float4 dx_Position = input[0].dx_Position;\n"
1066 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001067
1068 for (int corner = 0; corner < 4; corner++)
1069 {
1070 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001071 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001072
Brandon Jones71620962014-08-20 14:04:59 -07001073 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001074 {
1075 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1076 }
1077
1078 geomHLSL += " outStream.Append(output);\n";
1079 }
1080
1081 geomHLSL += " \n"
1082 " outStream.RestartStrip();\n"
1083 "}\n";
1084
1085 return geomHLSL;
1086}
1087
1088// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001089std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001090{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001091 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001092 {
1093 return "_" + name;
1094 }
1095
1096 return name;
1097}
1098
Jamie Madillf2575982014-06-25 16:04:54 -04001099std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001100{
Jamie Madilla53ab512014-03-17 09:47:44 -04001101 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001102
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001103 // Matrix
1104 if (IsMatrixType(shaderAttrib.type))
1105 {
Jamie Madill8664b062014-02-14 16:41:29 -05001106 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001107 }
1108
Jamie Madillf2575982014-06-25 16:04:54 -04001109 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1110 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001111
Jamie Madill8664b062014-02-14 16:41:29 -05001112 // Perform integer to float conversion (if necessary)
1113 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1114
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001115 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001116 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001117 // TODO: normalization for 32-bit integer formats
1118 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1119 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001120 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001121
1122 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001123 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001124}
1125
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001126void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1127{
1128 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1129 {
1130 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1131
1132 if (vertexFormat.mType == GL_NONE)
1133 {
1134 signature[inputIndex] = GL_NONE;
1135 }
1136 else
1137 {
1138 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1139 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1140 }
1141 }
1142}
1143
Jamie Madill5f562732014-02-14 16:41:24 -05001144}