blob: 0126f8f6eec388638cbbd2c4d90cb5c60702c9ad [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{
213 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
214
Brandon Jones71620962014-08-20 14:04:59 -0700215 vertexShader->resetVaryingsRegisterAssignment();
216 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500217
218 std::set<std::string> packedVaryings;
219
Brandon Jones71620962014-08-20 14:04:59 -0700220 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500221 {
Brandon Jones71620962014-08-20 14:04:59 -0700222 PackedVarying *varying = &fragmentShader->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500223 if (packVarying(varying, maxVaryingVectors, packing))
224 {
225 packedVaryings.insert(varying->name);
226 }
227 else
Jamie Madill5f562732014-02-14 16:41:24 -0500228 {
229 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500230 return -1;
231 }
232 }
233
Geoff Lang48dcae72014-02-05 16:28:24 -0500234 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
235 {
236 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
237 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
238 {
239 bool found = false;
Brandon Jones71620962014-08-20 14:04:59 -0700240 for (unsigned int varyingIndex = 0; varyingIndex < vertexShader->mVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500241 {
Brandon Jones71620962014-08-20 14:04:59 -0700242 PackedVarying *varying = &vertexShader->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500243 if (transformFeedbackVarying == varying->name)
244 {
245 if (!packVarying(varying, maxVaryingVectors, packing))
246 {
247 infoLog.append("Could not pack varying %s", varying->name.c_str());
248 return -1;
249 }
250
251 found = true;
252 break;
253 }
254 }
255
256 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
257 {
258 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
259 return -1;
260 }
261 }
262 }
263
Jamie Madill5f562732014-02-14 16:41:24 -0500264 // Return the number of used registers
265 int registers = 0;
266
267 for (int r = 0; r < maxVaryingVectors; r++)
268 {
269 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
270 {
271 registers++;
272 }
273 }
274
275 return registers;
276}
277
Brandon Jones71620962014-08-20 14:04:59 -0700278std::string DynamicHLSL::generateVaryingHLSL(rx::VertexShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500279{
Brandon Jones71620962014-08-20 14:04:59 -0700280 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500281 std::string varyingHLSL;
282
Brandon Jones71620962014-08-20 14:04:59 -0700283 for (unsigned int varyingIndex = 0; varyingIndex < shader->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500284 {
Brandon Jones71620962014-08-20 14:04:59 -0700285 const PackedVarying &varying = shader->mVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400286 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500287 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400288 GLenum transposedType = TransposeMatrixType(varying.type);
289 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500290
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400291 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500292 {
Jamie Madill5f562732014-02-14 16:41:24 -0500293 for (int row = 0; row < variableRows; row++)
294 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400295 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500296 {
Jamie Madillf2575982014-06-25 16:04:54 -0400297 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
298 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
299 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500300 default: UNREACHABLE();
301 }
302
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400303 unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500304 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500305
Jamie Madilla53ab512014-03-17 09:47:44 -0400306 std::string typeString;
307
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400308 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400309 {
310 // matrices within structs are not transposed, so
311 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400312 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400313 }
314 else
315 {
Jamie Madillf2575982014-06-25 16:04:54 -0400316 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400317 int columnCount = VariableColumnCount(transposedType);
Jamie Madilla53ab512014-03-17 09:47:44 -0400318 typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
319 }
Jamie Madill5f562732014-02-14 16:41:24 -0500320 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
321 }
322 }
323 }
Jamie Madill5f562732014-02-14 16:41:24 -0500324 }
325
326 return varyingHLSL;
327}
328
Jamie Madillf2575982014-06-25 16:04:54 -0400329std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
330 const VertexFormat inputLayout[],
331 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500332{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400333 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500334
335 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400336 unsigned int inputIndex = 0;
337
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500338 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
339 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400340 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
341
342 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madillf2575982014-06-25 16:04:54 -0400343 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500344
Jamie Madill8664b062014-02-14 16:41:29 -0500345 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500346 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400347 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500348 if (IsMatrixType(shaderAttribute.type))
349 {
350 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400351 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500352 }
353 else
354 {
355 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madillf2575982014-06-25 16:04:54 -0400356 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500357 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500358
Jamie Madilla53ab512014-03-17 09:47:44 -0400359 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400360 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500361
Jamie Madill3b7e2052014-03-17 09:47:43 -0400362 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400363 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500364
365 // Mismatched vertex attribute to vertex input may result in an undefined
366 // data reinterpretation (eg for pure integer->float, float->pure integer)
367 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400368 if (IsMatrixType(shaderAttribute.type) ||
369 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500370 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400371 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500372 }
373 else
374 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400375 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500376 }
377
Jamie Madill3b7e2052014-03-17 09:47:43 -0400378 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400379
Jamie Madillac0a2672014-04-11 13:33:56 -0400380 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
381 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500382 }
383
Geoff Lang04fb89a2014-06-09 15:05:36 -0400384 std::string replacementHLSL = "struct VS_INPUT\n"
385 "{\n" +
386 structHLSL +
387 "};\n"
388 "\n"
389 "void initAttributes(VS_INPUT input)\n"
390 "{\n" +
391 initHLSL +
392 "}\n";
393
394 std::string vertexHLSL(sourceShader);
395
396 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
397 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
398
399 return vertexHLSL;
400}
401
402std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables,
403 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
404{
405 const int shaderModel = mRenderer->getMajorShaderModel();
406 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
407 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
408
409 std::string declarationHLSL;
410 std::string copyHLSL;
411 for (size_t i = 0; i < outputVariables.size(); i++)
412 {
413 const PixelShaderOuputVariable& outputVariable = outputVariables[i];
414 ASSERT(outputLayout.size() > outputVariable.outputIndex);
Geoff Lang4ace4232014-06-18 19:12:48 -0400415
416 // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers
417 bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE
418 if (outputIndexEnabled)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400419 {
420 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
421 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
422
423 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
424 }
425 }
426
427 if (usesFragDepth)
428 {
429 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
430 copyHLSL += " output.gl_Depth = gl_Depth; \n";
431 }
432
433 std::string replacementHLSL = "struct PS_OUTPUT\n"
434 "{\n" +
435 declarationHLSL +
436 "};\n"
437 "\n"
438 "PS_OUTPUT generateOutput()\n"
439 "{\n"
440 " PS_OUTPUT output;\n" +
441 copyHLSL +
442 " return output;\n"
443 "}\n";
444
445 std::string pixelHLSL(sourceShader);
446
447 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
448 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
449
450 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500451}
452
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400453std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
454{
455 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
456 // In D3D11 we manually compute gl_PointCoord in the GS.
457 int shaderModel = mRenderer->getMajorShaderModel();
458 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
459}
460
461struct DynamicHLSL::SemanticInfo
462{
463 struct BuiltinInfo
464 {
465 BuiltinInfo()
466 : enabled(false),
467 index(0),
468 systemValue(false)
469 {}
470
471 bool enabled;
472 std::string semantic;
473 unsigned int index;
474 bool systemValue;
475
476 std::string str() const
477 {
478 return (systemValue ? semantic : (semantic + Str(index)));
479 }
480
481 void enableSystem(const std::string &systemValueSemantic)
482 {
483 enabled = true;
484 semantic = systemValueSemantic;
485 systemValue = true;
486 }
487
488 void enable(const std::string &semanticVal, unsigned int indexVal)
489 {
490 enabled = true;
491 semantic = semanticVal;
492 index = indexVal;
493 }
494 };
495
496 BuiltinInfo dxPosition;
497 BuiltinInfo glPosition;
498 BuiltinInfo glFragCoord;
499 BuiltinInfo glPointCoord;
500 BuiltinInfo glPointSize;
501};
502
503DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
504 bool pointSize, bool pixelShader) const
505{
506 SemanticInfo info;
507 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
508 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
509
510 int reservedRegisterIndex = startRegisters;
511
512 if (hlsl4)
513 {
514 info.dxPosition.enableSystem("SV_Position");
515 }
516 else if (pixelShader)
517 {
518 info.dxPosition.enableSystem("VPOS");
519 }
520 else
521 {
522 info.dxPosition.enableSystem("POSITION");
523 }
524
525 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
526
527 if (fragCoord)
528 {
529 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
530 }
531
532 if (pointCoord)
533 {
534 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
535 // In D3D11 we manually compute gl_PointCoord in the GS.
536 if (hlsl4)
537 {
538 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
539 }
540 else
541 {
542 info.glPointCoord.enable("TEXCOORD", 0);
543 }
544 }
545
546 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
547 if (pointSize && (!pixelShader || hlsl4))
548 {
549 info.glPointSize.enableSystem("PSIZE");
550 }
551
552 return info;
553}
554
555std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
556{
557 std::string linkHLSL = "{\n";
558
559 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
560
561 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
562 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
563
564 if (info.glFragCoord.enabled)
565 {
566 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
567 }
568
569 if (info.glPointCoord.enabled)
570 {
571 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
572 }
573
574 linkHLSL += varyingHLSL;
575
576 if (info.glPointSize.enabled)
577 {
578 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
579 }
580
581 linkHLSL += "};\n";
582
583 return linkHLSL;
584}
585
586void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
587 std::vector<LinkedVarying> *linkedVaryings) const
588{
589 ASSERT(info.glPosition.enabled);
590
591 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
592 info.glPosition.index, 1));
593
594 if (info.glFragCoord.enabled)
595 {
596 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
597 info.glFragCoord.index, 1));
598 }
599
600 if (info.glPointSize.enabled)
601 {
602 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
603 }
604}
605
Brandon Jones71620962014-08-20 14:04:59 -0700606void DynamicHLSL::storeUserLinkedVaryings(const rx::VertexShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400607 std::vector<LinkedVarying> *linkedVaryings) const
608{
Brandon Jones71620962014-08-20 14:04:59 -0700609 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
610 const std::vector<PackedVarying> &varyings = vertexShader->mVaryings;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400611
612 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
613 {
614 const PackedVarying &varying = varyings[varyingIndex];
615 if (varying.registerAssigned())
616 {
617 GLenum transposedType = TransposeMatrixType(varying.type);
618 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
619
620 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
621 varyingSemantic, varying.registerIndex,
622 variableRows * varying.elementCount()));
623 }
624 }
625}
626
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400627bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500628 std::string& pixelHLSL, std::string& vertexHLSL,
Brandon Jones71620962014-08-20 14:04:59 -0700629 rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500630 const std::vector<std::string>& transformFeedbackVaryings,
631 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400632 std::map<int, VariableLocation> *programOutputVars,
633 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
634 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500635{
636 if (pixelHLSL.empty() || vertexHLSL.empty())
637 {
638 return false;
639 }
640
Brandon Jones71620962014-08-20 14:04:59 -0700641 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
642 bool usesFragColor = fragmentShader->mUsesFragColor;
643 bool usesFragData = fragmentShader->mUsesFragData;
644 bool usesFragCoord = fragmentShader->mUsesFragCoord;
645 bool usesPointCoord = fragmentShader->mUsesPointCoord;
646 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400647
Jamie Madill5f562732014-02-14 16:41:24 -0500648 if (usesFragColor && usesFragData)
649 {
650 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
651 return false;
652 }
653
654 // Write the HLSL input/output declarations
655 const int shaderModel = mRenderer->getMajorShaderModel();
656 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
657
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400658 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500659
660 // Two cases when writing to gl_FragColor and using ESSL 1.0:
661 // - with a 3.0 context, the output color is copied to channel 0
662 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jones71620962014-08-20 14:04:59 -0700663 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400664 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500665
Brandon Jones71620962014-08-20 14:04:59 -0700666 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500667
668 if (registersNeeded > maxVaryingVectors)
669 {
670 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500671 return false;
672 }
673
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400674 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
675 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
676 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500677
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400678 storeUserLinkedVaryings(vertexShader, linkedVaryings);
679 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500680
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500681 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400682 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
683 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500684 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500685 "{\n"
686 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500687
688 if (shaderModel >= 4)
689 {
690 vertexHLSL += "\n"
691 " gl_main();\n"
692 "\n"
693 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500694 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400695 " output.dx_Position.x = gl_Position.x;\n"
696 " output.dx_Position.y = -gl_Position.y;\n"
697 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
698 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500699 }
700 else
701 {
702 vertexHLSL += "\n"
703 " gl_main();\n"
704 "\n"
705 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500706 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400707 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
708 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
709 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
710 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500711 }
712
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400713 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500714 {
715 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
716 }
717
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400718 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500719 {
720 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
721 }
722
Brandon Jones71620962014-08-20 14:04:59 -0700723 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500724 {
Brandon Jones71620962014-08-20 14:04:59 -0700725 const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400726 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500727 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400728 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500729 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400730 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500731
732 for (int row = 0; row < variableRows; row++)
733 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400734 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500735 vertexHLSL += " output.v" + Str(r);
736
737 bool sharedRegister = false; // Register used by multiple varyings
738
739 for (int x = 0; x < 4; x++)
740 {
741 if (packing[r][x] && packing[r][x] != packing[r][0])
742 {
743 sharedRegister = true;
744 break;
745 }
746 }
747
748 if(sharedRegister)
749 {
750 vertexHLSL += ".";
751
752 for (int x = 0; x < 4; x++)
753 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400754 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500755 {
756 switch(x)
757 {
758 case 0: vertexHLSL += "x"; break;
759 case 1: vertexHLSL += "y"; break;
760 case 2: vertexHLSL += "z"; break;
761 case 3: vertexHLSL += "w"; break;
762 }
763 }
764 }
765 }
766
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400767 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500768
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400769 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500770 {
771 vertexHLSL += ArrayString(elementIndex);
772 }
773
774 if (variableRows > 1)
775 {
776 vertexHLSL += ArrayString(row);
777 }
778
779 vertexHLSL += ";\n";
780 }
781 }
782 }
783 }
784
785 vertexHLSL += "\n"
786 " return output;\n"
787 "}\n";
788
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400789 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
790 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500791
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400792 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500793
794 if (shaderVersion < 300)
795 {
796 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
797 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400798 PixelShaderOuputVariable outputKeyVariable;
799 outputKeyVariable.type = GL_FLOAT_VEC4;
800 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
801 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
802 outputKeyVariable.outputIndex = renderTargetIndex;
803
804 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500805 }
806
Brandon Jones71620962014-08-20 14:04:59 -0700807 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500808 }
809 else
810 {
811 defineOutputVariables(fragmentShader, programOutputVars);
812
Brandon Jones71620962014-08-20 14:04:59 -0700813 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500814 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
815 {
816 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400817 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400818 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500819 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
820
Geoff Lang04fb89a2014-06-09 15:05:36 -0400821 PixelShaderOuputVariable outputKeyVariable;
822 outputKeyVariable.type = outputVariable.type;
823 outputKeyVariable.name = variableName + elementString;
824 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
825 outputKeyVariable.outputIndex = locationIt->first;
826
827 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500828 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400829
830 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500831 }
832
Geoff Lang04fb89a2014-06-09 15:05:36 -0400833 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500834
Brandon Jones71620962014-08-20 14:04:59 -0700835 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500836 {
837 if (shaderModel >= 4)
838 {
839 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
840 "{\n";
841 }
842 else
843 {
844 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
845 "{\n";
846 }
847 }
848 else
849 {
850 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
851 "{\n";
852 }
853
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400854 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500855 {
856 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
857
858 if (shaderModel >= 4)
859 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400860 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
861 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500862 }
863 else if (shaderModel >= 3)
864 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400865 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
866 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500867 }
868 else
869 {
870 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
871 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
872 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
873 }
874
875 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
876 " gl_FragCoord.w = rhw;\n";
877 }
878
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400879 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500880 {
881 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
882 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
883 }
884
Brandon Jones71620962014-08-20 14:04:59 -0700885 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500886 {
887 if (shaderModel <= 3)
888 {
889 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
890 }
891 else
892 {
893 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
894 }
895 }
896
Brandon Jones71620962014-08-20 14:04:59 -0700897 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500898 {
Brandon Jones71620962014-08-20 14:04:59 -0700899 const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400900 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500901 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400902 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500903 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400904 GLenum transposedType = TransposeMatrixType(varying.type);
905 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500906 for (int row = 0; row < variableRows; row++)
907 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400908 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
909 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500910
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400911 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500912 {
913 pixelHLSL += ArrayString(elementIndex);
914 }
915
916 if (variableRows > 1)
917 {
918 pixelHLSL += ArrayString(row);
919 }
920
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400921 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500922 {
923 pixelHLSL += " = input.v" + n + ";\n"; break;
924 }
925 else
926 {
927 switch (VariableColumnCount(transposedType))
928 {
929 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
930 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
931 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
932 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
933 default: UNREACHABLE();
934 }
935 }
936 }
937 }
938 }
939 else UNREACHABLE();
940 }
941
942 pixelHLSL += "\n"
943 " gl_main();\n"
944 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400945 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500946 "}\n";
947
948 return true;
949}
950
Brandon Jones71620962014-08-20 14:04:59 -0700951void DynamicHLSL::defineOutputVariables(rx::FragmentShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500952{
Brandon Jones71620962014-08-20 14:04:59 -0700953 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500954
955 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
956 {
Jamie Madillf2575982014-06-25 16:04:54 -0400957 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500958 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
959
960 if (outputVariable.arraySize > 0)
961 {
962 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
963 {
964 const int location = baseLocation + elementIndex;
965 ASSERT(programOutputVars->count(location) == 0);
966 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
967 }
968 }
969 else
970 {
971 ASSERT(programOutputVars->count(baseLocation) == 0);
972 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
973 }
974 }
975}
976
Brandon Jones71620962014-08-20 14:04:59 -0700977std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500978{
979 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -0700980 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400981 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -0500982}
983
Brandon Jones71620962014-08-20 14:04:59 -0700984std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500985{
986 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -0700987 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500988 ASSERT(mRenderer->getMajorShaderModel() >= 4);
989
990 std::string geomHLSL;
991
Brandon Jones71620962014-08-20 14:04:59 -0700992 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400993 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -0700994 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
995 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500996
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400997 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
998 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
999 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001000
Geoff Langc0b9ef42014-07-02 10:02:37 -04001001 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001002 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1003 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001004 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1005 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001006 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001007 "static float2 pointSpriteCorners[] = \n"
1008 "{\n"
1009 " float2( 0.5f, -0.5f),\n"
1010 " float2( 0.5f, 0.5f),\n"
1011 " float2(-0.5f, -0.5f),\n"
1012 " float2(-0.5f, 0.5f)\n"
1013 "};\n"
1014 "\n"
1015 "static float2 pointSpriteTexcoords[] = \n"
1016 "{\n"
1017 " float2(1.0f, 1.0f),\n"
1018 " float2(1.0f, 0.0f),\n"
1019 " float2(0.0f, 1.0f),\n"
1020 " float2(0.0f, 0.0f)\n"
1021 "};\n"
1022 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001023 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1024 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001025 "\n"
1026 "[maxvertexcount(4)]\n"
1027 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1028 "{\n"
1029 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001030 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001031 " output.gl_PointSize = input[0].gl_PointSize;\n";
1032
1033 for (int r = 0; r < registers; r++)
1034 {
1035 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1036 }
1037
Brandon Jones71620962014-08-20 14:04:59 -07001038 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001039 {
1040 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1041 }
1042
1043 geomHLSL += " \n"
1044 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001045 " float4 dx_Position = input[0].dx_Position;\n"
1046 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001047
1048 for (int corner = 0; corner < 4; corner++)
1049 {
1050 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001051 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001052
Brandon Jones71620962014-08-20 14:04:59 -07001053 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001054 {
1055 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1056 }
1057
1058 geomHLSL += " outStream.Append(output);\n";
1059 }
1060
1061 geomHLSL += " \n"
1062 " outStream.RestartStrip();\n"
1063 "}\n";
1064
1065 return geomHLSL;
1066}
1067
1068// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001069std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001070{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001071 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001072 {
1073 return "_" + name;
1074 }
1075
1076 return name;
1077}
1078
Jamie Madillf2575982014-06-25 16:04:54 -04001079std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001080{
Jamie Madilla53ab512014-03-17 09:47:44 -04001081 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001082
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001083 // Matrix
1084 if (IsMatrixType(shaderAttrib.type))
1085 {
Jamie Madill8664b062014-02-14 16:41:29 -05001086 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001087 }
1088
Jamie Madillf2575982014-06-25 16:04:54 -04001089 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1090 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001091
Jamie Madill8664b062014-02-14 16:41:29 -05001092 // Perform integer to float conversion (if necessary)
1093 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1094
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001095 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001096 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001097 // TODO: normalization for 32-bit integer formats
1098 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1099 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001100 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001101
1102 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001103 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001104}
1105
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001106void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1107{
1108 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1109 {
1110 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1111
1112 if (vertexFormat.mType == GL_NONE)
1113 {
1114 signature[inputIndex] = GL_NONE;
1115 }
1116 else
1117 {
1118 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1119 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1120 }
1121 }
1122}
1123
Jamie Madill5f562732014-02-14 16:41:24 -05001124}