blob: 7086f47bf2ee9ce6f1c9bc9836e3b7a32635e7bd [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 Madill8664b062014-02-14 16:41:29 -050025namespace gl_d3d
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
73}
74
Brandon Jonesd8d72432014-08-22 15:11:23 -070075namespace rx
Jamie Madill8664b062014-02-14 16:41:29 -050076{
77
Geoff Lang04fb89a2014-06-09 15:05:36 -040078const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
79const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050080
Jamie Madill5f562732014-02-14 16:41:24 -050081DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
82 : mRenderer(renderer)
83{
84}
85
Jamie Madillff0d2ba2014-05-14 13:49:10 -040086static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -050087{
Geoff Lang48dcae72014-02-05 16:28:24 -050088 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -050089
Geoff Lang48dcae72014-02-05 16:28:24 -050090 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -040091 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -050092 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -050093
Jamie Madillff0d2ba2014-05-14 13:49:10 -040094 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -050095 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -040096 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -050097 {
Geoff Lang48dcae72014-02-05 16:28:24 -050098 bool available = true;
99
100 for (int y = 0; y < registers && available; y++)
101 {
102 for (int x = 0; x < elements && available; x++)
103 {
104 if (packing[r + y][x])
105 {
106 available = false;
107 }
108 }
109 }
110
111 if (available)
112 {
113 varying->registerIndex = r;
Geoff Lang48dcae72014-02-05 16:28:24 -0500114
115 for (int y = 0; y < registers; y++)
116 {
117 for (int x = 0; x < elements; x++)
118 {
119 packing[r + y][x] = &*varying;
120 }
121 }
122
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400123 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500124 }
125 }
126
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400127 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500128 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400129 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500130 {
131 bool available = true;
132
133 for (int y = 0; y < registers && available; y++)
134 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500135 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500136 {
137 if (packing[r + y][x])
138 {
139 available = false;
140 }
141 }
142 }
143
144 if (available)
145 {
146 varying->registerIndex = r;
Jamie Madill5f562732014-02-14 16:41:24 -0500147
148 for (int y = 0; y < registers; y++)
149 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500150 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500151 {
152 packing[r + y][x] = &*varying;
153 }
154 }
155
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400156 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500157 }
158 }
Jamie Madill5f562732014-02-14 16:41:24 -0500159 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500160 }
161 else if (elements == 1)
162 {
163 int space[4] = { 0 };
164
165 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500166 {
Jamie Madill5f562732014-02-14 16:41:24 -0500167 for (int x = 0; x < 4; x++)
168 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500169 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500170 }
171 }
Jamie Madill5f562732014-02-14 16:41:24 -0500172
Geoff Lang48dcae72014-02-05 16:28:24 -0500173 int column = 0;
174
175 for (int x = 0; x < 4; x++)
176 {
177 if (space[x] >= registers && space[x] < space[column])
178 {
179 column = x;
180 }
181 }
182
183 if (space[column] >= registers)
184 {
185 for (int r = 0; r < maxVaryingVectors; r++)
186 {
187 if (!packing[r][column])
188 {
189 varying->registerIndex = r;
190
191 for (int y = r; y < r + registers; y++)
192 {
193 packing[y][column] = &*varying;
194 }
195
196 break;
197 }
198 }
199
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400200 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500201 }
202 }
203 else UNREACHABLE();
204
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400205 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500206}
207
208// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
209// Returns the number of used varying registers, or -1 if unsuccesful
Brandon Jones71620962014-08-20 14:04:59 -0700210int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, rx::FragmentShaderD3D *fragmentShader,
211 rx::VertexShaderD3D *vertexShader, const std::vector<std::string>& transformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -0500212{
Geoff Lang3a61c322014-07-10 13:01:54 -0400213 // TODO (geofflang): Use context's caps
214 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Geoff Lang48dcae72014-02-05 16:28:24 -0500215
Brandon Jones71620962014-08-20 14:04:59 -0700216 vertexShader->resetVaryingsRegisterAssignment();
217 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500218
219 std::set<std::string> packedVaryings;
220
Jamie Madilld15250e2014-09-03 09:40:44 -0400221 std::vector<gl::PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
222 std::vector<gl::PackedVarying> &vertexVaryings = vertexShader->getVaryings();
223 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500224 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400225 PackedVarying *varying = &fragmentVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500226 if (packVarying(varying, maxVaryingVectors, packing))
227 {
228 packedVaryings.insert(varying->name);
229 }
230 else
Jamie Madill5f562732014-02-14 16:41:24 -0500231 {
232 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500233 return -1;
234 }
235 }
236
Geoff Lang48dcae72014-02-05 16:28:24 -0500237 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
238 {
239 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
240 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
241 {
242 bool found = false;
Jamie Madilld15250e2014-09-03 09:40:44 -0400243 for (unsigned int varyingIndex = 0; varyingIndex < vertexVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500244 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400245 PackedVarying *varying = &vertexVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500246 if (transformFeedbackVarying == varying->name)
247 {
248 if (!packVarying(varying, maxVaryingVectors, packing))
249 {
250 infoLog.append("Could not pack varying %s", varying->name.c_str());
251 return -1;
252 }
253
254 found = true;
255 break;
256 }
257 }
258
259 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
260 {
261 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
262 return -1;
263 }
264 }
265 }
266
Jamie Madill5f562732014-02-14 16:41:24 -0500267 // Return the number of used registers
268 int registers = 0;
269
270 for (int r = 0; r < maxVaryingVectors; r++)
271 {
272 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
273 {
274 registers++;
275 }
276 }
277
278 return registers;
279}
280
Brandon Jones71620962014-08-20 14:04:59 -0700281std::string DynamicHLSL::generateVaryingHLSL(rx::VertexShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500282{
Brandon Jones71620962014-08-20 14:04:59 -0700283 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500284 std::string varyingHLSL;
285
Jamie Madilld15250e2014-09-03 09:40:44 -0400286 std::vector<gl::PackedVarying> &varyings = shader->getVaryings();
287 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500288 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400289 const PackedVarying &varying = varyings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400290 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500291 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400292 GLenum transposedType = TransposeMatrixType(varying.type);
293 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500294
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400295 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500296 {
Jamie Madill5f562732014-02-14 16:41:24 -0500297 for (int row = 0; row < variableRows; row++)
298 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400299 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500300 {
Jamie Madillf2575982014-06-25 16:04:54 -0400301 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
302 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
303 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500304 default: UNREACHABLE();
305 }
306
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400307 unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500308 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500309
Jamie Madilla53ab512014-03-17 09:47:44 -0400310 std::string typeString;
311
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400312 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400313 {
314 // matrices within structs are not transposed, so
315 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400316 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400317 }
318 else
319 {
Jamie Madillf2575982014-06-25 16:04:54 -0400320 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400321 int columnCount = VariableColumnCount(transposedType);
Jamie Madilla53ab512014-03-17 09:47:44 -0400322 typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
323 }
Jamie Madill5f562732014-02-14 16:41:24 -0500324 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
325 }
326 }
327 }
Jamie Madill5f562732014-02-14 16:41:24 -0500328 }
329
330 return varyingHLSL;
331}
332
Jamie Madillf2575982014-06-25 16:04:54 -0400333std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
334 const VertexFormat inputLayout[],
335 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500336{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400337 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500338
339 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400340 unsigned int inputIndex = 0;
341
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500342 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
343 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400344 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
345
346 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madillf2575982014-06-25 16:04:54 -0400347 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500348
Jamie Madill8664b062014-02-14 16:41:29 -0500349 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500350 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400351 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500352 if (IsMatrixType(shaderAttribute.type))
353 {
354 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400355 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500356 }
357 else
358 {
359 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madillf2575982014-06-25 16:04:54 -0400360 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500361 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500362
Jamie Madilla53ab512014-03-17 09:47:44 -0400363 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400364 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500365
Jamie Madill3b7e2052014-03-17 09:47:43 -0400366 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400367 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500368
369 // Mismatched vertex attribute to vertex input may result in an undefined
370 // data reinterpretation (eg for pure integer->float, float->pure integer)
371 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400372 if (IsMatrixType(shaderAttribute.type) ||
373 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500374 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400375 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500376 }
377 else
378 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400379 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500380 }
381
Jamie Madill3b7e2052014-03-17 09:47:43 -0400382 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400383
Jamie Madillac0a2672014-04-11 13:33:56 -0400384 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
385 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500386 }
387
Geoff Lang04fb89a2014-06-09 15:05:36 -0400388 std::string replacementHLSL = "struct VS_INPUT\n"
389 "{\n" +
390 structHLSL +
391 "};\n"
392 "\n"
393 "void initAttributes(VS_INPUT input)\n"
394 "{\n" +
395 initHLSL +
396 "}\n";
397
398 std::string vertexHLSL(sourceShader);
399
400 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
401 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
402
403 return vertexHLSL;
404}
405
406std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables,
407 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
408{
409 const int shaderModel = mRenderer->getMajorShaderModel();
410 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
411 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
412
413 std::string declarationHLSL;
414 std::string copyHLSL;
415 for (size_t i = 0; i < outputVariables.size(); i++)
416 {
417 const PixelShaderOuputVariable& outputVariable = outputVariables[i];
418 ASSERT(outputLayout.size() > outputVariable.outputIndex);
Geoff Lang4ace4232014-06-18 19:12:48 -0400419
420 // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers
421 bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE
422 if (outputIndexEnabled)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400423 {
424 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
425 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
426
427 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
428 }
429 }
430
431 if (usesFragDepth)
432 {
433 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
434 copyHLSL += " output.gl_Depth = gl_Depth; \n";
435 }
436
437 std::string replacementHLSL = "struct PS_OUTPUT\n"
438 "{\n" +
439 declarationHLSL +
440 "};\n"
441 "\n"
442 "PS_OUTPUT generateOutput()\n"
443 "{\n"
444 " PS_OUTPUT output;\n" +
445 copyHLSL +
446 " return output;\n"
447 "}\n";
448
449 std::string pixelHLSL(sourceShader);
450
451 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
452 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
453
454 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500455}
456
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400457std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
458{
459 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
460 // In D3D11 we manually compute gl_PointCoord in the GS.
461 int shaderModel = mRenderer->getMajorShaderModel();
462 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
463}
464
465struct DynamicHLSL::SemanticInfo
466{
467 struct BuiltinInfo
468 {
469 BuiltinInfo()
470 : enabled(false),
471 index(0),
472 systemValue(false)
473 {}
474
475 bool enabled;
476 std::string semantic;
477 unsigned int index;
478 bool systemValue;
479
480 std::string str() const
481 {
482 return (systemValue ? semantic : (semantic + Str(index)));
483 }
484
485 void enableSystem(const std::string &systemValueSemantic)
486 {
487 enabled = true;
488 semantic = systemValueSemantic;
489 systemValue = true;
490 }
491
492 void enable(const std::string &semanticVal, unsigned int indexVal)
493 {
494 enabled = true;
495 semantic = semanticVal;
496 index = indexVal;
497 }
498 };
499
500 BuiltinInfo dxPosition;
501 BuiltinInfo glPosition;
502 BuiltinInfo glFragCoord;
503 BuiltinInfo glPointCoord;
504 BuiltinInfo glPointSize;
505};
506
507DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
508 bool pointSize, bool pixelShader) const
509{
510 SemanticInfo info;
511 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
512 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
513
514 int reservedRegisterIndex = startRegisters;
515
516 if (hlsl4)
517 {
518 info.dxPosition.enableSystem("SV_Position");
519 }
520 else if (pixelShader)
521 {
522 info.dxPosition.enableSystem("VPOS");
523 }
524 else
525 {
526 info.dxPosition.enableSystem("POSITION");
527 }
528
529 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
530
531 if (fragCoord)
532 {
533 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
534 }
535
536 if (pointCoord)
537 {
538 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
539 // In D3D11 we manually compute gl_PointCoord in the GS.
540 if (hlsl4)
541 {
542 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
543 }
544 else
545 {
546 info.glPointCoord.enable("TEXCOORD", 0);
547 }
548 }
549
550 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
551 if (pointSize && (!pixelShader || hlsl4))
552 {
553 info.glPointSize.enableSystem("PSIZE");
554 }
555
556 return info;
557}
558
559std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
560{
561 std::string linkHLSL = "{\n";
562
563 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
564
565 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
566 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
567
568 if (info.glFragCoord.enabled)
569 {
570 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
571 }
572
573 if (info.glPointCoord.enabled)
574 {
575 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
576 }
577
578 linkHLSL += varyingHLSL;
579
580 if (info.glPointSize.enabled)
581 {
582 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
583 }
584
585 linkHLSL += "};\n";
586
587 return linkHLSL;
588}
589
590void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
591 std::vector<LinkedVarying> *linkedVaryings) const
592{
593 ASSERT(info.glPosition.enabled);
594
595 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
596 info.glPosition.index, 1));
597
598 if (info.glFragCoord.enabled)
599 {
600 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
601 info.glFragCoord.index, 1));
602 }
603
604 if (info.glPointSize.enabled)
605 {
606 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
607 }
608}
609
Brandon Jones71620962014-08-20 14:04:59 -0700610void DynamicHLSL::storeUserLinkedVaryings(const rx::VertexShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400611 std::vector<LinkedVarying> *linkedVaryings) const
612{
Brandon Jones71620962014-08-20 14:04:59 -0700613 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
Jamie Madilld15250e2014-09-03 09:40:44 -0400614 const std::vector<PackedVarying> &varyings = vertexShader->getVaryings();
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400615
616 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
617 {
618 const PackedVarying &varying = varyings[varyingIndex];
619 if (varying.registerAssigned())
620 {
621 GLenum transposedType = TransposeMatrixType(varying.type);
622 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
623
624 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
625 varyingSemantic, varying.registerIndex,
626 variableRows * varying.elementCount()));
627 }
628 }
629}
630
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400631bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500632 std::string& pixelHLSL, std::string& vertexHLSL,
Brandon Jones71620962014-08-20 14:04:59 -0700633 rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500634 const std::vector<std::string>& transformFeedbackVaryings,
635 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400636 std::map<int, VariableLocation> *programOutputVars,
637 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
638 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500639{
640 if (pixelHLSL.empty() || vertexHLSL.empty())
641 {
642 return false;
643 }
644
Brandon Jones71620962014-08-20 14:04:59 -0700645 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
646 bool usesFragColor = fragmentShader->mUsesFragColor;
647 bool usesFragData = fragmentShader->mUsesFragData;
648 bool usesFragCoord = fragmentShader->mUsesFragCoord;
649 bool usesPointCoord = fragmentShader->mUsesPointCoord;
650 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400651
Jamie Madill5f562732014-02-14 16:41:24 -0500652 if (usesFragColor && usesFragData)
653 {
654 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
655 return false;
656 }
657
658 // Write the HLSL input/output declarations
659 const int shaderModel = mRenderer->getMajorShaderModel();
Geoff Lang3a61c322014-07-10 13:01:54 -0400660
661 // TODO (geofflang): Use context's caps
662 const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors;
Jamie Madill5f562732014-02-14 16:41:24 -0500663
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400664 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500665
666 // Two cases when writing to gl_FragColor and using ESSL 1.0:
667 // - with a 3.0 context, the output color is copied to channel 0
668 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jones71620962014-08-20 14:04:59 -0700669 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400670 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500671
Brandon Jones71620962014-08-20 14:04:59 -0700672 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500673
674 if (registersNeeded > maxVaryingVectors)
675 {
676 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500677 return false;
678 }
679
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400680 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
681 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
682 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500683
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400684 storeUserLinkedVaryings(vertexShader, linkedVaryings);
685 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500686
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500687 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400688 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
689 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500690 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500691 "{\n"
692 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500693
694 if (shaderModel >= 4)
695 {
696 vertexHLSL += "\n"
697 " gl_main();\n"
698 "\n"
699 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500700 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400701 " output.dx_Position.x = gl_Position.x;\n"
702 " output.dx_Position.y = -gl_Position.y;\n"
703 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
704 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500705 }
706 else
707 {
708 vertexHLSL += "\n"
709 " gl_main();\n"
710 "\n"
711 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500712 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400713 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
714 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
715 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
716 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500717 }
718
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400719 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500720 {
721 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
722 }
723
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400724 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500725 {
726 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
727 }
728
Jamie Madilld15250e2014-09-03 09:40:44 -0400729 const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
730 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500731 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400732 const PackedVarying &varying = vertexVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400733 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500734 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400735 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500736 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400737 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500738
739 for (int row = 0; row < variableRows; row++)
740 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400741 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500742 vertexHLSL += " output.v" + Str(r);
743
744 bool sharedRegister = false; // Register used by multiple varyings
745
746 for (int x = 0; x < 4; x++)
747 {
748 if (packing[r][x] && packing[r][x] != packing[r][0])
749 {
750 sharedRegister = true;
751 break;
752 }
753 }
754
755 if(sharedRegister)
756 {
757 vertexHLSL += ".";
758
759 for (int x = 0; x < 4; x++)
760 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400761 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500762 {
763 switch(x)
764 {
765 case 0: vertexHLSL += "x"; break;
766 case 1: vertexHLSL += "y"; break;
767 case 2: vertexHLSL += "z"; break;
768 case 3: vertexHLSL += "w"; break;
769 }
770 }
771 }
772 }
773
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400774 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500775
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400776 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500777 {
778 vertexHLSL += ArrayString(elementIndex);
779 }
780
781 if (variableRows > 1)
782 {
783 vertexHLSL += ArrayString(row);
784 }
785
786 vertexHLSL += ";\n";
787 }
788 }
789 }
790 }
791
792 vertexHLSL += "\n"
793 " return output;\n"
794 "}\n";
795
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400796 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
797 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500798
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400799 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500800
801 if (shaderVersion < 300)
802 {
803 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
804 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400805 PixelShaderOuputVariable outputKeyVariable;
806 outputKeyVariable.type = GL_FLOAT_VEC4;
807 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
808 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
809 outputKeyVariable.outputIndex = renderTargetIndex;
810
811 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500812 }
813
Brandon Jones71620962014-08-20 14:04:59 -0700814 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500815 }
816 else
817 {
818 defineOutputVariables(fragmentShader, programOutputVars);
819
Jamie Madilld15250e2014-09-03 09:40:44 -0400820 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500821 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
822 {
823 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400824 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400825 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500826 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
827
Geoff Lang04fb89a2014-06-09 15:05:36 -0400828 PixelShaderOuputVariable outputKeyVariable;
829 outputKeyVariable.type = outputVariable.type;
830 outputKeyVariable.name = variableName + elementString;
831 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
832 outputKeyVariable.outputIndex = locationIt->first;
833
834 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500835 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400836
837 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500838 }
839
Geoff Lang04fb89a2014-06-09 15:05:36 -0400840 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500841
Brandon Jones71620962014-08-20 14:04:59 -0700842 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500843 {
844 if (shaderModel >= 4)
845 {
846 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
847 "{\n";
848 }
849 else
850 {
851 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
852 "{\n";
853 }
854 }
855 else
856 {
857 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
858 "{\n";
859 }
860
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400861 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500862 {
863 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
864
865 if (shaderModel >= 4)
866 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400867 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
868 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500869 }
870 else if (shaderModel >= 3)
871 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400872 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
873 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500874 }
875 else
876 {
877 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
878 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
879 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
880 }
881
882 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
883 " gl_FragCoord.w = rhw;\n";
884 }
885
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400886 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500887 {
888 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
889 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
890 }
891
Brandon Jones71620962014-08-20 14:04:59 -0700892 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500893 {
894 if (shaderModel <= 3)
895 {
896 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
897 }
898 else
899 {
900 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
901 }
902 }
903
Jamie Madilld15250e2014-09-03 09:40:44 -0400904 const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
905 for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500906 {
Jamie Madilld15250e2014-09-03 09:40:44 -0400907 const PackedVarying &varying = fragmentVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400908 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500909 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400910 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500911 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400912 GLenum transposedType = TransposeMatrixType(varying.type);
913 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500914 for (int row = 0; row < variableRows; row++)
915 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400916 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
917 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500918
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400919 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500920 {
921 pixelHLSL += ArrayString(elementIndex);
922 }
923
924 if (variableRows > 1)
925 {
926 pixelHLSL += ArrayString(row);
927 }
928
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400929 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500930 {
931 pixelHLSL += " = input.v" + n + ";\n"; break;
932 }
933 else
934 {
935 switch (VariableColumnCount(transposedType))
936 {
937 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
938 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
939 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
940 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
941 default: UNREACHABLE();
942 }
943 }
944 }
945 }
946 }
947 else UNREACHABLE();
948 }
949
950 pixelHLSL += "\n"
951 " gl_main();\n"
952 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400953 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500954 "}\n";
955
956 return true;
957}
958
Brandon Jones71620962014-08-20 14:04:59 -0700959void DynamicHLSL::defineOutputVariables(rx::FragmentShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500960{
Jamie Madilld15250e2014-09-03 09:40:44 -0400961 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500962
963 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
964 {
Jamie Madillf2575982014-06-25 16:04:54 -0400965 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500966 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
967
968 if (outputVariable.arraySize > 0)
969 {
970 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
971 {
972 const int location = baseLocation + elementIndex;
973 ASSERT(programOutputVars->count(location) == 0);
974 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
975 }
976 }
977 else
978 {
979 ASSERT(programOutputVars->count(baseLocation) == 0);
980 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
981 }
982 }
983}
984
Brandon Jones71620962014-08-20 14:04:59 -0700985std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500986{
987 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -0700988 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400989 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -0500990}
991
Brandon Jones71620962014-08-20 14:04:59 -0700992std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500993{
994 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -0700995 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500996 ASSERT(mRenderer->getMajorShaderModel() >= 4);
997
998 std::string geomHLSL;
999
Brandon Jones71620962014-08-20 14:04:59 -07001000 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001001 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -07001002 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
1003 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -05001004
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001005 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
1006 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1007 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001008
Geoff Langc0b9ef42014-07-02 10:02:37 -04001009 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001010 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1011 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001012 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1013 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001014 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001015 "static float2 pointSpriteCorners[] = \n"
1016 "{\n"
1017 " float2( 0.5f, -0.5f),\n"
1018 " float2( 0.5f, 0.5f),\n"
1019 " float2(-0.5f, -0.5f),\n"
1020 " float2(-0.5f, 0.5f)\n"
1021 "};\n"
1022 "\n"
1023 "static float2 pointSpriteTexcoords[] = \n"
1024 "{\n"
1025 " float2(1.0f, 1.0f),\n"
1026 " float2(1.0f, 0.0f),\n"
1027 " float2(0.0f, 1.0f),\n"
1028 " float2(0.0f, 0.0f)\n"
1029 "};\n"
1030 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001031 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1032 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001033 "\n"
1034 "[maxvertexcount(4)]\n"
1035 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1036 "{\n"
1037 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001038 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001039 " output.gl_PointSize = input[0].gl_PointSize;\n";
1040
1041 for (int r = 0; r < registers; r++)
1042 {
1043 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1044 }
1045
Brandon Jones71620962014-08-20 14:04:59 -07001046 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001047 {
1048 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1049 }
1050
1051 geomHLSL += " \n"
1052 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001053 " float4 dx_Position = input[0].dx_Position;\n"
1054 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001055
1056 for (int corner = 0; corner < 4; corner++)
1057 {
1058 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001059 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001060
Brandon Jones71620962014-08-20 14:04:59 -07001061 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001062 {
1063 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1064 }
1065
1066 geomHLSL += " outStream.Append(output);\n";
1067 }
1068
1069 geomHLSL += " \n"
1070 " outStream.RestartStrip();\n"
1071 "}\n";
1072
1073 return geomHLSL;
1074}
1075
1076// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001077std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001078{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001079 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001080 {
1081 return "_" + name;
1082 }
1083
1084 return name;
1085}
1086
Jamie Madillf2575982014-06-25 16:04:54 -04001087std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001088{
Jamie Madilla53ab512014-03-17 09:47:44 -04001089 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001090
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001091 // Matrix
1092 if (IsMatrixType(shaderAttrib.type))
1093 {
Jamie Madill8664b062014-02-14 16:41:29 -05001094 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001095 }
1096
Jamie Madillf2575982014-06-25 16:04:54 -04001097 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1098 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001099
Jamie Madill8664b062014-02-14 16:41:29 -05001100 // Perform integer to float conversion (if necessary)
1101 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1102
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001103 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001104 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001105 // TODO: normalization for 32-bit integer formats
1106 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1107 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001108 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001109
1110 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001111 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001112}
1113
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001114void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1115{
1116 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1117 {
1118 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1119
1120 if (vertexFormat.mType == GL_NONE)
1121 {
1122 signature[inputIndex] = GL_NONE;
1123 }
1124 else
1125 {
1126 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1127 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1128 }
1129 }
1130}
1131
Jamie Madill5f562732014-02-14 16:41:24 -05001132}