blob: dc5da681ab46212f5da83b035d1813206b78b419 [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
Jamie Madillea247592014-08-28 10:37:08 -04009#include "libGLESv2/common_includes.h"
Jamie Madill5f562732014-02-14 16:41:24 -050010
11#include "libGLESv2/DynamicHLSL.h"
12#include "libGLESv2/Shader.h"
13#include "libGLESv2/Program.h"
Brandon Jonesf05cdee2014-08-27 15:24:07 -070014#include "libGLESv2/renderer/d3d/ShaderD3D.h"
Jamie Madill5f562732014-02-14 16:41:24 -050015#include "libGLESv2/renderer/Renderer.h"
16#include "common/utilities.h"
17#include "libGLESv2/ProgramBinary.h"
Jamie Madill8664b062014-02-14 16:41:29 -050018#include "libGLESv2/formatutils.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040019#include "common/blocklayout.h"
Jamie Madill5f562732014-02-14 16:41:24 -050020
Jamie Madill53cb14d2014-07-08 15:02:35 -040021// For use with ArrayString, see angleutils.h
22META_ASSERT(GL_INVALID_INDEX == UINT_MAX);
Jamie Madill5f562732014-02-14 16:41:24 -050023
Jamie Madill8664b062014-02-14 16:41:29 -050024namespace gl_d3d
Jamie Madill5f562732014-02-14 16:41:24 -050025{
Jamie Madill8664b062014-02-14 16:41:29 -050026
27std::string HLSLComponentTypeString(GLenum componentType)
28{
29 switch (componentType)
30 {
31 case GL_UNSIGNED_INT: return "uint";
32 case GL_INT: return "int";
33 case GL_UNSIGNED_NORMALIZED:
34 case GL_SIGNED_NORMALIZED:
35 case GL_FLOAT: return "float";
36 default: UNREACHABLE(); return "not-component-type";
37 }
Jamie Madill5f562732014-02-14 16:41:24 -050038}
39
Jamie Madill8664b062014-02-14 16:41:29 -050040std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
41{
42 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
43}
44
45std::string HLSLMatrixTypeString(GLenum type)
46{
47 switch (type)
48 {
49 case GL_FLOAT_MAT2: return "float2x2";
50 case GL_FLOAT_MAT3: return "float3x3";
51 case GL_FLOAT_MAT4: return "float4x4";
52 case GL_FLOAT_MAT2x3: return "float2x3";
53 case GL_FLOAT_MAT3x2: return "float3x2";
54 case GL_FLOAT_MAT2x4: return "float2x4";
55 case GL_FLOAT_MAT4x2: return "float4x2";
56 case GL_FLOAT_MAT3x4: return "float3x4";
57 case GL_FLOAT_MAT4x3: return "float4x3";
58 default: UNREACHABLE(); return "not-matrix-type";
59 }
60}
61
62std::string HLSLTypeString(GLenum type)
63{
64 if (gl::IsMatrixType(type))
65 {
66 return HLSLMatrixTypeString(type);
67 }
68
Jamie Madillf2575982014-06-25 16:04:54 -040069 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050070}
71
72}
73
74namespace gl
75{
76
Geoff Lang04fb89a2014-06-09 15:05:36 -040077const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
78const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050079
Jamie Madill5f562732014-02-14 16:41:24 -050080DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
81 : mRenderer(renderer)
82{
83}
84
Jamie Madillff0d2ba2014-05-14 13:49:10 -040085static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -050086{
Geoff Lang48dcae72014-02-05 16:28:24 -050087 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -050088
Geoff Lang48dcae72014-02-05 16:28:24 -050089 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -040090 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -050091 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -050092
Jamie Madillff0d2ba2014-05-14 13:49:10 -040093 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -050094 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -040095 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -050096 {
Geoff Lang48dcae72014-02-05 16:28:24 -050097 bool available = true;
98
99 for (int y = 0; y < registers && available; y++)
100 {
101 for (int x = 0; x < elements && available; x++)
102 {
103 if (packing[r + y][x])
104 {
105 available = false;
106 }
107 }
108 }
109
110 if (available)
111 {
112 varying->registerIndex = r;
Geoff Lang48dcae72014-02-05 16:28:24 -0500113
114 for (int y = 0; y < registers; y++)
115 {
116 for (int x = 0; x < elements; x++)
117 {
118 packing[r + y][x] = &*varying;
119 }
120 }
121
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400122 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500123 }
124 }
125
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400126 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500127 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400128 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500129 {
130 bool available = true;
131
132 for (int y = 0; y < registers && available; y++)
133 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500134 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500135 {
136 if (packing[r + y][x])
137 {
138 available = false;
139 }
140 }
141 }
142
143 if (available)
144 {
145 varying->registerIndex = r;
Jamie Madill5f562732014-02-14 16:41:24 -0500146
147 for (int y = 0; y < registers; y++)
148 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500149 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500150 {
151 packing[r + y][x] = &*varying;
152 }
153 }
154
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400155 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500156 }
157 }
Jamie Madill5f562732014-02-14 16:41:24 -0500158 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500159 }
160 else if (elements == 1)
161 {
162 int space[4] = { 0 };
163
164 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500165 {
Jamie Madill5f562732014-02-14 16:41:24 -0500166 for (int x = 0; x < 4; x++)
167 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500168 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500169 }
170 }
Jamie Madill5f562732014-02-14 16:41:24 -0500171
Geoff Lang48dcae72014-02-05 16:28:24 -0500172 int column = 0;
173
174 for (int x = 0; x < 4; x++)
175 {
176 if (space[x] >= registers && space[x] < space[column])
177 {
178 column = x;
179 }
180 }
181
182 if (space[column] >= registers)
183 {
184 for (int r = 0; r < maxVaryingVectors; r++)
185 {
186 if (!packing[r][column])
187 {
188 varying->registerIndex = r;
189
190 for (int y = r; y < r + registers; y++)
191 {
192 packing[y][column] = &*varying;
193 }
194
195 break;
196 }
197 }
198
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400199 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500200 }
201 }
202 else UNREACHABLE();
203
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400204 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500205}
206
207// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
208// Returns the number of used varying registers, or -1 if unsuccesful
Brandon Jones71620962014-08-20 14:04:59 -0700209int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, rx::FragmentShaderD3D *fragmentShader,
210 rx::VertexShaderD3D *vertexShader, const std::vector<std::string>& transformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -0500211{
212 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
213
Brandon Jones71620962014-08-20 14:04:59 -0700214 vertexShader->resetVaryingsRegisterAssignment();
215 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500216
217 std::set<std::string> packedVaryings;
218
Brandon Jones71620962014-08-20 14:04:59 -0700219 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500220 {
Brandon Jones71620962014-08-20 14:04:59 -0700221 PackedVarying *varying = &fragmentShader->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500222 if (packVarying(varying, maxVaryingVectors, packing))
223 {
224 packedVaryings.insert(varying->name);
225 }
226 else
Jamie Madill5f562732014-02-14 16:41:24 -0500227 {
228 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500229 return -1;
230 }
231 }
232
Geoff Lang48dcae72014-02-05 16:28:24 -0500233 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
234 {
235 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
236 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
237 {
238 bool found = false;
Brandon Jones71620962014-08-20 14:04:59 -0700239 for (unsigned int varyingIndex = 0; varyingIndex < vertexShader->mVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500240 {
Brandon Jones71620962014-08-20 14:04:59 -0700241 PackedVarying *varying = &vertexShader->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500242 if (transformFeedbackVarying == varying->name)
243 {
244 if (!packVarying(varying, maxVaryingVectors, packing))
245 {
246 infoLog.append("Could not pack varying %s", varying->name.c_str());
247 return -1;
248 }
249
250 found = true;
251 break;
252 }
253 }
254
255 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
256 {
257 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
258 return -1;
259 }
260 }
261 }
262
Jamie Madill5f562732014-02-14 16:41:24 -0500263 // Return the number of used registers
264 int registers = 0;
265
266 for (int r = 0; r < maxVaryingVectors; r++)
267 {
268 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
269 {
270 registers++;
271 }
272 }
273
274 return registers;
275}
276
Brandon Jones71620962014-08-20 14:04:59 -0700277std::string DynamicHLSL::generateVaryingHLSL(rx::VertexShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500278{
Brandon Jones71620962014-08-20 14:04:59 -0700279 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500280 std::string varyingHLSL;
281
Brandon Jones71620962014-08-20 14:04:59 -0700282 for (unsigned int varyingIndex = 0; varyingIndex < shader->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500283 {
Brandon Jones71620962014-08-20 14:04:59 -0700284 const PackedVarying &varying = shader->mVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400285 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500286 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400287 GLenum transposedType = TransposeMatrixType(varying.type);
288 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500289
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400290 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500291 {
Jamie Madill5f562732014-02-14 16:41:24 -0500292 for (int row = 0; row < variableRows; row++)
293 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400294 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500295 {
Jamie Madillf2575982014-06-25 16:04:54 -0400296 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
297 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
298 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500299 default: UNREACHABLE();
300 }
301
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400302 unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500303 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500304
Jamie Madilla53ab512014-03-17 09:47:44 -0400305 std::string typeString;
306
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400307 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400308 {
309 // matrices within structs are not transposed, so
310 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400311 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400312 }
313 else
314 {
Jamie Madillf2575982014-06-25 16:04:54 -0400315 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400316 int columnCount = VariableColumnCount(transposedType);
Jamie Madilla53ab512014-03-17 09:47:44 -0400317 typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
318 }
Jamie Madill5f562732014-02-14 16:41:24 -0500319 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
320 }
321 }
322 }
Jamie Madill5f562732014-02-14 16:41:24 -0500323 }
324
325 return varyingHLSL;
326}
327
Jamie Madillf2575982014-06-25 16:04:54 -0400328std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
329 const VertexFormat inputLayout[],
330 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500331{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400332 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500333
334 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400335 unsigned int inputIndex = 0;
336
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500337 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
338 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400339 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
340
341 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madillf2575982014-06-25 16:04:54 -0400342 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500343
Jamie Madill8664b062014-02-14 16:41:29 -0500344 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500345 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400346 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500347 if (IsMatrixType(shaderAttribute.type))
348 {
349 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400350 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500351 }
352 else
353 {
354 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madillf2575982014-06-25 16:04:54 -0400355 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500356 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500357
Jamie Madilla53ab512014-03-17 09:47:44 -0400358 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400359 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500360
Jamie Madill3b7e2052014-03-17 09:47:43 -0400361 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400362 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500363
364 // Mismatched vertex attribute to vertex input may result in an undefined
365 // data reinterpretation (eg for pure integer->float, float->pure integer)
366 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400367 if (IsMatrixType(shaderAttribute.type) ||
368 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500369 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400370 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500371 }
372 else
373 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400374 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500375 }
376
Jamie Madill3b7e2052014-03-17 09:47:43 -0400377 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400378
Jamie Madillac0a2672014-04-11 13:33:56 -0400379 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
380 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500381 }
382
Geoff Lang04fb89a2014-06-09 15:05:36 -0400383 std::string replacementHLSL = "struct VS_INPUT\n"
384 "{\n" +
385 structHLSL +
386 "};\n"
387 "\n"
388 "void initAttributes(VS_INPUT input)\n"
389 "{\n" +
390 initHLSL +
391 "}\n";
392
393 std::string vertexHLSL(sourceShader);
394
395 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
396 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
397
398 return vertexHLSL;
399}
400
401std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables,
402 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
403{
404 const int shaderModel = mRenderer->getMajorShaderModel();
405 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
406 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
407
408 std::string declarationHLSL;
409 std::string copyHLSL;
410 for (size_t i = 0; i < outputVariables.size(); i++)
411 {
412 const PixelShaderOuputVariable& outputVariable = outputVariables[i];
413 ASSERT(outputLayout.size() > outputVariable.outputIndex);
Geoff Lang4ace4232014-06-18 19:12:48 -0400414
415 // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers
416 bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE
417 if (outputIndexEnabled)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400418 {
419 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
420 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
421
422 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
423 }
424 }
425
426 if (usesFragDepth)
427 {
428 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
429 copyHLSL += " output.gl_Depth = gl_Depth; \n";
430 }
431
432 std::string replacementHLSL = "struct PS_OUTPUT\n"
433 "{\n" +
434 declarationHLSL +
435 "};\n"
436 "\n"
437 "PS_OUTPUT generateOutput()\n"
438 "{\n"
439 " PS_OUTPUT output;\n" +
440 copyHLSL +
441 " return output;\n"
442 "}\n";
443
444 std::string pixelHLSL(sourceShader);
445
446 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
447 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
448
449 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500450}
451
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400452std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
453{
454 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
455 // In D3D11 we manually compute gl_PointCoord in the GS.
456 int shaderModel = mRenderer->getMajorShaderModel();
457 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
458}
459
460struct DynamicHLSL::SemanticInfo
461{
462 struct BuiltinInfo
463 {
464 BuiltinInfo()
465 : enabled(false),
466 index(0),
467 systemValue(false)
468 {}
469
470 bool enabled;
471 std::string semantic;
472 unsigned int index;
473 bool systemValue;
474
475 std::string str() const
476 {
477 return (systemValue ? semantic : (semantic + Str(index)));
478 }
479
480 void enableSystem(const std::string &systemValueSemantic)
481 {
482 enabled = true;
483 semantic = systemValueSemantic;
484 systemValue = true;
485 }
486
487 void enable(const std::string &semanticVal, unsigned int indexVal)
488 {
489 enabled = true;
490 semantic = semanticVal;
491 index = indexVal;
492 }
493 };
494
495 BuiltinInfo dxPosition;
496 BuiltinInfo glPosition;
497 BuiltinInfo glFragCoord;
498 BuiltinInfo glPointCoord;
499 BuiltinInfo glPointSize;
500};
501
502DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
503 bool pointSize, bool pixelShader) const
504{
505 SemanticInfo info;
506 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
507 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
508
509 int reservedRegisterIndex = startRegisters;
510
511 if (hlsl4)
512 {
513 info.dxPosition.enableSystem("SV_Position");
514 }
515 else if (pixelShader)
516 {
517 info.dxPosition.enableSystem("VPOS");
518 }
519 else
520 {
521 info.dxPosition.enableSystem("POSITION");
522 }
523
524 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
525
526 if (fragCoord)
527 {
528 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
529 }
530
531 if (pointCoord)
532 {
533 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
534 // In D3D11 we manually compute gl_PointCoord in the GS.
535 if (hlsl4)
536 {
537 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
538 }
539 else
540 {
541 info.glPointCoord.enable("TEXCOORD", 0);
542 }
543 }
544
545 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
546 if (pointSize && (!pixelShader || hlsl4))
547 {
548 info.glPointSize.enableSystem("PSIZE");
549 }
550
551 return info;
552}
553
554std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
555{
556 std::string linkHLSL = "{\n";
557
558 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
559
560 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
561 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
562
563 if (info.glFragCoord.enabled)
564 {
565 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
566 }
567
568 if (info.glPointCoord.enabled)
569 {
570 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
571 }
572
573 linkHLSL += varyingHLSL;
574
575 if (info.glPointSize.enabled)
576 {
577 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
578 }
579
580 linkHLSL += "};\n";
581
582 return linkHLSL;
583}
584
585void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
586 std::vector<LinkedVarying> *linkedVaryings) const
587{
588 ASSERT(info.glPosition.enabled);
589
590 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
591 info.glPosition.index, 1));
592
593 if (info.glFragCoord.enabled)
594 {
595 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
596 info.glFragCoord.index, 1));
597 }
598
599 if (info.glPointSize.enabled)
600 {
601 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
602 }
603}
604
Brandon Jones71620962014-08-20 14:04:59 -0700605void DynamicHLSL::storeUserLinkedVaryings(const rx::VertexShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400606 std::vector<LinkedVarying> *linkedVaryings) const
607{
Brandon Jones71620962014-08-20 14:04:59 -0700608 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
609 const std::vector<PackedVarying> &varyings = vertexShader->mVaryings;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400610
611 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
612 {
613 const PackedVarying &varying = varyings[varyingIndex];
614 if (varying.registerAssigned())
615 {
616 GLenum transposedType = TransposeMatrixType(varying.type);
617 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
618
619 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
620 varyingSemantic, varying.registerIndex,
621 variableRows * varying.elementCount()));
622 }
623 }
624}
625
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400626bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500627 std::string& pixelHLSL, std::string& vertexHLSL,
Brandon Jones71620962014-08-20 14:04:59 -0700628 rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500629 const std::vector<std::string>& transformFeedbackVaryings,
630 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400631 std::map<int, VariableLocation> *programOutputVars,
632 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
633 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500634{
635 if (pixelHLSL.empty() || vertexHLSL.empty())
636 {
637 return false;
638 }
639
Brandon Jones71620962014-08-20 14:04:59 -0700640 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
641 bool usesFragColor = fragmentShader->mUsesFragColor;
642 bool usesFragData = fragmentShader->mUsesFragData;
643 bool usesFragCoord = fragmentShader->mUsesFragCoord;
644 bool usesPointCoord = fragmentShader->mUsesPointCoord;
645 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400646
Jamie Madill5f562732014-02-14 16:41:24 -0500647 if (usesFragColor && usesFragData)
648 {
649 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
650 return false;
651 }
652
653 // Write the HLSL input/output declarations
654 const int shaderModel = mRenderer->getMajorShaderModel();
655 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
656
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400657 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500658
659 // Two cases when writing to gl_FragColor and using ESSL 1.0:
660 // - with a 3.0 context, the output color is copied to channel 0
661 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jones71620962014-08-20 14:04:59 -0700662 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400663 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500664
Brandon Jones71620962014-08-20 14:04:59 -0700665 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500666
667 if (registersNeeded > maxVaryingVectors)
668 {
669 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500670 return false;
671 }
672
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400673 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
674 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
675 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500676
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400677 storeUserLinkedVaryings(vertexShader, linkedVaryings);
678 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500679
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500680 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400681 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
682 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500683 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500684 "{\n"
685 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500686
687 if (shaderModel >= 4)
688 {
689 vertexHLSL += "\n"
690 " gl_main();\n"
691 "\n"
692 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500693 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400694 " output.dx_Position.x = gl_Position.x;\n"
695 " output.dx_Position.y = -gl_Position.y;\n"
696 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
697 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500698 }
699 else
700 {
701 vertexHLSL += "\n"
702 " gl_main();\n"
703 "\n"
704 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500705 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400706 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
707 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
708 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
709 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500710 }
711
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400712 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500713 {
714 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
715 }
716
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400717 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500718 {
719 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
720 }
721
Brandon Jones71620962014-08-20 14:04:59 -0700722 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500723 {
Brandon Jones71620962014-08-20 14:04:59 -0700724 const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400725 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500726 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400727 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500728 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400729 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500730
731 for (int row = 0; row < variableRows; row++)
732 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400733 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500734 vertexHLSL += " output.v" + Str(r);
735
736 bool sharedRegister = false; // Register used by multiple varyings
737
738 for (int x = 0; x < 4; x++)
739 {
740 if (packing[r][x] && packing[r][x] != packing[r][0])
741 {
742 sharedRegister = true;
743 break;
744 }
745 }
746
747 if(sharedRegister)
748 {
749 vertexHLSL += ".";
750
751 for (int x = 0; x < 4; x++)
752 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400753 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500754 {
755 switch(x)
756 {
757 case 0: vertexHLSL += "x"; break;
758 case 1: vertexHLSL += "y"; break;
759 case 2: vertexHLSL += "z"; break;
760 case 3: vertexHLSL += "w"; break;
761 }
762 }
763 }
764 }
765
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400766 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500767
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400768 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500769 {
770 vertexHLSL += ArrayString(elementIndex);
771 }
772
773 if (variableRows > 1)
774 {
775 vertexHLSL += ArrayString(row);
776 }
777
778 vertexHLSL += ";\n";
779 }
780 }
781 }
782 }
783
784 vertexHLSL += "\n"
785 " return output;\n"
786 "}\n";
787
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400788 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
789 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500790
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400791 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500792
793 if (shaderVersion < 300)
794 {
795 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
796 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400797 PixelShaderOuputVariable outputKeyVariable;
798 outputKeyVariable.type = GL_FLOAT_VEC4;
799 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
800 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
801 outputKeyVariable.outputIndex = renderTargetIndex;
802
803 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500804 }
805
Brandon Jones71620962014-08-20 14:04:59 -0700806 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500807 }
808 else
809 {
810 defineOutputVariables(fragmentShader, programOutputVars);
811
Brandon Jones71620962014-08-20 14:04:59 -0700812 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500813 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
814 {
815 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400816 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400817 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500818 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
819
Geoff Lang04fb89a2014-06-09 15:05:36 -0400820 PixelShaderOuputVariable outputKeyVariable;
821 outputKeyVariable.type = outputVariable.type;
822 outputKeyVariable.name = variableName + elementString;
823 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
824 outputKeyVariable.outputIndex = locationIt->first;
825
826 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500827 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400828
829 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500830 }
831
Geoff Lang04fb89a2014-06-09 15:05:36 -0400832 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500833
Brandon Jones71620962014-08-20 14:04:59 -0700834 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500835 {
836 if (shaderModel >= 4)
837 {
838 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
839 "{\n";
840 }
841 else
842 {
843 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
844 "{\n";
845 }
846 }
847 else
848 {
849 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
850 "{\n";
851 }
852
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400853 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500854 {
855 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
856
857 if (shaderModel >= 4)
858 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400859 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
860 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500861 }
862 else if (shaderModel >= 3)
863 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400864 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
865 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500866 }
867 else
868 {
869 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
870 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
871 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
872 }
873
874 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
875 " gl_FragCoord.w = rhw;\n";
876 }
877
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400878 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500879 {
880 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
881 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
882 }
883
Brandon Jones71620962014-08-20 14:04:59 -0700884 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500885 {
886 if (shaderModel <= 3)
887 {
888 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
889 }
890 else
891 {
892 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
893 }
894 }
895
Brandon Jones71620962014-08-20 14:04:59 -0700896 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500897 {
Brandon Jones71620962014-08-20 14:04:59 -0700898 const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400899 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500900 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400901 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500902 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400903 GLenum transposedType = TransposeMatrixType(varying.type);
904 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500905 for (int row = 0; row < variableRows; row++)
906 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400907 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
908 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500909
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400910 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500911 {
912 pixelHLSL += ArrayString(elementIndex);
913 }
914
915 if (variableRows > 1)
916 {
917 pixelHLSL += ArrayString(row);
918 }
919
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400920 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500921 {
922 pixelHLSL += " = input.v" + n + ";\n"; break;
923 }
924 else
925 {
926 switch (VariableColumnCount(transposedType))
927 {
928 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
929 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
930 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
931 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
932 default: UNREACHABLE();
933 }
934 }
935 }
936 }
937 }
938 else UNREACHABLE();
939 }
940
941 pixelHLSL += "\n"
942 " gl_main();\n"
943 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400944 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500945 "}\n";
946
947 return true;
948}
949
Brandon Jones71620962014-08-20 14:04:59 -0700950void DynamicHLSL::defineOutputVariables(rx::FragmentShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500951{
Brandon Jones71620962014-08-20 14:04:59 -0700952 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500953
954 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
955 {
Jamie Madillf2575982014-06-25 16:04:54 -0400956 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500957 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
958
959 if (outputVariable.arraySize > 0)
960 {
961 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
962 {
963 const int location = baseLocation + elementIndex;
964 ASSERT(programOutputVars->count(location) == 0);
965 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
966 }
967 }
968 else
969 {
970 ASSERT(programOutputVars->count(baseLocation) == 0);
971 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
972 }
973 }
974}
975
Brandon Jones71620962014-08-20 14:04:59 -0700976std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500977{
978 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -0700979 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400980 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -0500981}
982
Brandon Jones71620962014-08-20 14:04:59 -0700983std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500984{
985 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -0700986 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500987 ASSERT(mRenderer->getMajorShaderModel() >= 4);
988
989 std::string geomHLSL;
990
Brandon Jones71620962014-08-20 14:04:59 -0700991 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400992 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -0700993 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
994 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500995
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400996 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
997 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
998 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -0500999
Geoff Langc0b9ef42014-07-02 10:02:37 -04001000 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001001 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1002 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001003 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1004 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001005 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001006 "static float2 pointSpriteCorners[] = \n"
1007 "{\n"
1008 " float2( 0.5f, -0.5f),\n"
1009 " float2( 0.5f, 0.5f),\n"
1010 " float2(-0.5f, -0.5f),\n"
1011 " float2(-0.5f, 0.5f)\n"
1012 "};\n"
1013 "\n"
1014 "static float2 pointSpriteTexcoords[] = \n"
1015 "{\n"
1016 " float2(1.0f, 1.0f),\n"
1017 " float2(1.0f, 0.0f),\n"
1018 " float2(0.0f, 1.0f),\n"
1019 " float2(0.0f, 0.0f)\n"
1020 "};\n"
1021 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001022 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1023 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001024 "\n"
1025 "[maxvertexcount(4)]\n"
1026 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1027 "{\n"
1028 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001029 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001030 " output.gl_PointSize = input[0].gl_PointSize;\n";
1031
1032 for (int r = 0; r < registers; r++)
1033 {
1034 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1035 }
1036
Brandon Jones71620962014-08-20 14:04:59 -07001037 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001038 {
1039 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1040 }
1041
1042 geomHLSL += " \n"
1043 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001044 " float4 dx_Position = input[0].dx_Position;\n"
1045 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001046
1047 for (int corner = 0; corner < 4; corner++)
1048 {
1049 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001050 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001051
Brandon Jones71620962014-08-20 14:04:59 -07001052 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001053 {
1054 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1055 }
1056
1057 geomHLSL += " outStream.Append(output);\n";
1058 }
1059
1060 geomHLSL += " \n"
1061 " outStream.RestartStrip();\n"
1062 "}\n";
1063
1064 return geomHLSL;
1065}
1066
1067// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001068std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001069{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001070 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001071 {
1072 return "_" + name;
1073 }
1074
1075 return name;
1076}
1077
Jamie Madillf2575982014-06-25 16:04:54 -04001078std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001079{
Jamie Madilla53ab512014-03-17 09:47:44 -04001080 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001081
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001082 // Matrix
1083 if (IsMatrixType(shaderAttrib.type))
1084 {
Jamie Madill8664b062014-02-14 16:41:29 -05001085 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001086 }
1087
Jamie Madillf2575982014-06-25 16:04:54 -04001088 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1089 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001090
Jamie Madill8664b062014-02-14 16:41:29 -05001091 // Perform integer to float conversion (if necessary)
1092 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1093
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001094 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001095 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001096 // TODO: normalization for 32-bit integer formats
1097 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1098 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001099 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001100
1101 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001102 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001103}
1104
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001105void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1106{
1107 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1108 {
1109 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1110
1111 if (vertexFormat.mType == GL_NONE)
1112 {
1113 signature[inputIndex] = GL_NONE;
1114 }
1115 else
1116 {
1117 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1118 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1119 }
1120 }
1121}
1122
Jamie Madill5f562732014-02-14 16:41:24 -05001123}