blob: ba893fef081f5523b12531a17d188b63853b2e4c [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
Brandon Jonesd8d72432014-08-22 15:11:23 -070011#include "libGLESv2/renderer/d3d/DynamicHLSL.h"
Jamie Madill5f562732014-02-14 16:41:24 -050012#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
Brandon Jonesd8d72432014-08-22 15:11:23 -070024using namespace gl;
25
Jamie Madill8664b062014-02-14 16:41:29 -050026namespace gl_d3d
Jamie Madill5f562732014-02-14 16:41:24 -050027{
Jamie Madill8664b062014-02-14 16:41:29 -050028
29std::string HLSLComponentTypeString(GLenum componentType)
30{
31 switch (componentType)
32 {
33 case GL_UNSIGNED_INT: return "uint";
34 case GL_INT: return "int";
35 case GL_UNSIGNED_NORMALIZED:
36 case GL_SIGNED_NORMALIZED:
37 case GL_FLOAT: return "float";
38 default: UNREACHABLE(); return "not-component-type";
39 }
Jamie Madill5f562732014-02-14 16:41:24 -050040}
41
Jamie Madill8664b062014-02-14 16:41:29 -050042std::string HLSLComponentTypeString(GLenum componentType, int componentCount)
43{
44 return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : "");
45}
46
47std::string HLSLMatrixTypeString(GLenum type)
48{
49 switch (type)
50 {
51 case GL_FLOAT_MAT2: return "float2x2";
52 case GL_FLOAT_MAT3: return "float3x3";
53 case GL_FLOAT_MAT4: return "float4x4";
54 case GL_FLOAT_MAT2x3: return "float2x3";
55 case GL_FLOAT_MAT3x2: return "float3x2";
56 case GL_FLOAT_MAT2x4: return "float2x4";
57 case GL_FLOAT_MAT4x2: return "float4x2";
58 case GL_FLOAT_MAT3x4: return "float3x4";
59 case GL_FLOAT_MAT4x3: return "float4x3";
60 default: UNREACHABLE(); return "not-matrix-type";
61 }
62}
63
64std::string HLSLTypeString(GLenum type)
65{
66 if (gl::IsMatrixType(type))
67 {
68 return HLSLMatrixTypeString(type);
69 }
70
Jamie Madillf2575982014-06-25 16:04:54 -040071 return HLSLComponentTypeString(gl::VariableComponentType(type), gl::VariableComponentCount(type));
Jamie Madill8664b062014-02-14 16:41:29 -050072}
73
74}
75
Brandon Jonesd8d72432014-08-22 15:11:23 -070076namespace rx
Jamie Madill8664b062014-02-14 16:41:29 -050077{
78
Geoff Lang04fb89a2014-06-09 15:05:36 -040079const std::string VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@";
80const std::string PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@";
Jamie Madillc5ede1a2014-02-14 16:41:27 -050081
Jamie Madill5f562732014-02-14 16:41:24 -050082DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer)
83 : mRenderer(renderer)
84{
85}
86
Jamie Madillff0d2ba2014-05-14 13:49:10 -040087static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
Jamie Madill5f562732014-02-14 16:41:24 -050088{
Geoff Lang48dcae72014-02-05 16:28:24 -050089 GLenum transposedType = TransposeMatrixType(varying->type);
Jamie Madill5f562732014-02-14 16:41:24 -050090
Geoff Lang48dcae72014-02-05 16:28:24 -050091 // matrices within varying structs are not transposed
Jamie Madill834e8b72014-04-11 13:33:58 -040092 int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -050093 int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -050094
Jamie Madillff0d2ba2014-05-14 13:49:10 -040095 if (elements >= 2 && elements <= 4)
Jamie Madill5f562732014-02-14 16:41:24 -050096 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -040097 for (int r = 0; r <= maxVaryingVectors - registers; r++)
Jamie Madill5f562732014-02-14 16:41:24 -050098 {
Geoff Lang48dcae72014-02-05 16:28:24 -050099 bool available = true;
100
101 for (int y = 0; y < registers && available; y++)
102 {
103 for (int x = 0; x < elements && available; x++)
104 {
105 if (packing[r + y][x])
106 {
107 available = false;
108 }
109 }
110 }
111
112 if (available)
113 {
114 varying->registerIndex = r;
Geoff Lang48dcae72014-02-05 16:28:24 -0500115
116 for (int y = 0; y < registers; y++)
117 {
118 for (int x = 0; x < elements; x++)
119 {
120 packing[r + y][x] = &*varying;
121 }
122 }
123
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400124 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500125 }
126 }
127
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400128 if (elements == 2)
Geoff Lang48dcae72014-02-05 16:28:24 -0500129 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400130 for (int r = maxVaryingVectors - registers; r >= 0; r--)
Jamie Madill5f562732014-02-14 16:41:24 -0500131 {
132 bool available = true;
133
134 for (int y = 0; y < registers && available; y++)
135 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500136 for (int x = 2; x < 4 && available; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500137 {
138 if (packing[r + y][x])
139 {
140 available = false;
141 }
142 }
143 }
144
145 if (available)
146 {
147 varying->registerIndex = r;
Jamie Madill5f562732014-02-14 16:41:24 -0500148
149 for (int y = 0; y < registers; y++)
150 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500151 for (int x = 2; x < 4; x++)
Jamie Madill5f562732014-02-14 16:41:24 -0500152 {
153 packing[r + y][x] = &*varying;
154 }
155 }
156
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400157 return true;
Jamie Madill5f562732014-02-14 16:41:24 -0500158 }
159 }
Jamie Madill5f562732014-02-14 16:41:24 -0500160 }
Geoff Lang48dcae72014-02-05 16:28:24 -0500161 }
162 else if (elements == 1)
163 {
164 int space[4] = { 0 };
165
166 for (int y = 0; y < maxVaryingVectors; y++)
Jamie Madill5f562732014-02-14 16:41:24 -0500167 {
Jamie Madill5f562732014-02-14 16:41:24 -0500168 for (int x = 0; x < 4; x++)
169 {
Geoff Lang48dcae72014-02-05 16:28:24 -0500170 space[x] += packing[y][x] ? 0 : 1;
Jamie Madill5f562732014-02-14 16:41:24 -0500171 }
172 }
Jamie Madill5f562732014-02-14 16:41:24 -0500173
Geoff Lang48dcae72014-02-05 16:28:24 -0500174 int column = 0;
175
176 for (int x = 0; x < 4; x++)
177 {
178 if (space[x] >= registers && space[x] < space[column])
179 {
180 column = x;
181 }
182 }
183
184 if (space[column] >= registers)
185 {
186 for (int r = 0; r < maxVaryingVectors; r++)
187 {
188 if (!packing[r][column])
189 {
190 varying->registerIndex = r;
191
192 for (int y = r; y < r + registers; y++)
193 {
194 packing[y][column] = &*varying;
195 }
196
197 break;
198 }
199 }
200
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400201 return true;
Geoff Lang48dcae72014-02-05 16:28:24 -0500202 }
203 }
204 else UNREACHABLE();
205
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400206 return false;
Geoff Lang48dcae72014-02-05 16:28:24 -0500207}
208
209// Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
210// Returns the number of used varying registers, or -1 if unsuccesful
Brandon Jones71620962014-08-20 14:04:59 -0700211int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, rx::FragmentShaderD3D *fragmentShader,
212 rx::VertexShaderD3D *vertexShader, const std::vector<std::string>& transformFeedbackVaryings)
Geoff Lang48dcae72014-02-05 16:28:24 -0500213{
214 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
215
Brandon Jones71620962014-08-20 14:04:59 -0700216 vertexShader->resetVaryingsRegisterAssignment();
217 fragmentShader->resetVaryingsRegisterAssignment();
Geoff Lang48dcae72014-02-05 16:28:24 -0500218
219 std::set<std::string> packedVaryings;
220
Brandon Jones71620962014-08-20 14:04:59 -0700221 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500222 {
Brandon Jones71620962014-08-20 14:04:59 -0700223 PackedVarying *varying = &fragmentShader->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500224 if (packVarying(varying, maxVaryingVectors, packing))
225 {
226 packedVaryings.insert(varying->name);
227 }
228 else
Jamie Madill5f562732014-02-14 16:41:24 -0500229 {
230 infoLog.append("Could not pack varying %s", varying->name.c_str());
Jamie Madill5f562732014-02-14 16:41:24 -0500231 return -1;
232 }
233 }
234
Geoff Lang48dcae72014-02-05 16:28:24 -0500235 for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++)
236 {
237 const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex];
238 if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end())
239 {
240 bool found = false;
Brandon Jones71620962014-08-20 14:04:59 -0700241 for (unsigned int varyingIndex = 0; varyingIndex < vertexShader->mVaryings.size(); varyingIndex++)
Geoff Lang48dcae72014-02-05 16:28:24 -0500242 {
Brandon Jones71620962014-08-20 14:04:59 -0700243 PackedVarying *varying = &vertexShader->mVaryings[varyingIndex];
Geoff Lang48dcae72014-02-05 16:28:24 -0500244 if (transformFeedbackVarying == varying->name)
245 {
246 if (!packVarying(varying, maxVaryingVectors, packing))
247 {
248 infoLog.append("Could not pack varying %s", varying->name.c_str());
249 return -1;
250 }
251
252 found = true;
253 break;
254 }
255 }
256
257 if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize")
258 {
259 infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str());
260 return -1;
261 }
262 }
263 }
264
Jamie Madill5f562732014-02-14 16:41:24 -0500265 // Return the number of used registers
266 int registers = 0;
267
268 for (int r = 0; r < maxVaryingVectors; r++)
269 {
270 if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3])
271 {
272 registers++;
273 }
274 }
275
276 return registers;
277}
278
Brandon Jones71620962014-08-20 14:04:59 -0700279std::string DynamicHLSL::generateVaryingHLSL(rx::VertexShaderD3D *shader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500280{
Brandon Jones71620962014-08-20 14:04:59 -0700281 std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500282 std::string varyingHLSL;
283
Brandon Jones71620962014-08-20 14:04:59 -0700284 for (unsigned int varyingIndex = 0; varyingIndex < shader->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500285 {
Brandon Jones71620962014-08-20 14:04:59 -0700286 const PackedVarying &varying = shader->mVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400287 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500288 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400289 GLenum transposedType = TransposeMatrixType(varying.type);
290 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Geoff Lang48dcae72014-02-05 16:28:24 -0500291
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400292 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500293 {
Jamie Madill5f562732014-02-14 16:41:24 -0500294 for (int row = 0; row < variableRows; row++)
295 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400296 switch (varying.interpolation)
Jamie Madill5f562732014-02-14 16:41:24 -0500297 {
Jamie Madillf2575982014-06-25 16:04:54 -0400298 case sh::INTERPOLATION_SMOOTH: varyingHLSL += " "; break;
299 case sh::INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break;
300 case sh::INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break;
Jamie Madill5f562732014-02-14 16:41:24 -0500301 default: UNREACHABLE();
302 }
303
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400304 unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row;
Geoff Lang48dcae72014-02-05 16:28:24 -0500305 std::string n = Str(semanticIndex);
Jamie Madill5f562732014-02-14 16:41:24 -0500306
Jamie Madilla53ab512014-03-17 09:47:44 -0400307 std::string typeString;
308
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400309 if (varying.isStruct())
Jamie Madilla53ab512014-03-17 09:47:44 -0400310 {
311 // matrices within structs are not transposed, so
312 // do not use the special struct prefix "rm"
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400313 typeString = decorateVariable(varying.structName);
Jamie Madilla53ab512014-03-17 09:47:44 -0400314 }
315 else
316 {
Jamie Madillf2575982014-06-25 16:04:54 -0400317 GLenum componentType = VariableComponentType(transposedType);
Jamie Madill834e8b72014-04-11 13:33:58 -0400318 int columnCount = VariableColumnCount(transposedType);
Jamie Madilla53ab512014-03-17 09:47:44 -0400319 typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount);
320 }
Jamie Madill5f562732014-02-14 16:41:24 -0500321 varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n";
322 }
323 }
324 }
Jamie Madill5f562732014-02-14 16:41:24 -0500325 }
326
327 return varyingHLSL;
328}
329
Jamie Madillf2575982014-06-25 16:04:54 -0400330std::string DynamicHLSL::generateVertexShaderForInputLayout(const std::string &sourceShader,
331 const VertexFormat inputLayout[],
332 const sh::Attribute shaderAttributes[]) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500333{
Jamie Madill3b7e2052014-03-17 09:47:43 -0400334 std::string structHLSL, initHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500335
336 int semanticIndex = 0;
Jamie Madill3b7e2052014-03-17 09:47:43 -0400337 unsigned int inputIndex = 0;
338
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500339 for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
340 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400341 ASSERT(inputIndex < MAX_VERTEX_ATTRIBS);
342
343 const VertexFormat &vertexFormat = inputLayout[inputIndex];
Jamie Madillf2575982014-06-25 16:04:54 -0400344 const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex];
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500345
Jamie Madill8664b062014-02-14 16:41:29 -0500346 if (!shaderAttribute.name.empty())
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500347 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400348 // HLSL code for input structure
Jamie Madill8664b062014-02-14 16:41:29 -0500349 if (IsMatrixType(shaderAttribute.type))
350 {
351 // Matrix types are always transposed
Jamie Madill3b7e2052014-03-17 09:47:43 -0400352 structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500353 }
354 else
355 {
356 GLenum componentType = mRenderer->getVertexComponentType(vertexFormat);
Jamie Madillf2575982014-06-25 16:04:54 -0400357 structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, VariableComponentCount(shaderAttribute.type));
Jamie Madill8664b062014-02-14 16:41:29 -0500358 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500359
Jamie Madilla53ab512014-03-17 09:47:44 -0400360 structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n";
Jamie Madillf2575982014-06-25 16:04:54 -0400361 semanticIndex += VariableRegisterCount(shaderAttribute.type);
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500362
Jamie Madill3b7e2052014-03-17 09:47:43 -0400363 // HLSL code for initialization
Jamie Madilla53ab512014-03-17 09:47:44 -0400364 initHLSL += " " + decorateVariable(shaderAttribute.name) + " = ";
Jamie Madill8664b062014-02-14 16:41:29 -0500365
366 // Mismatched vertex attribute to vertex input may result in an undefined
367 // data reinterpretation (eg for pure integer->float, float->pure integer)
368 // TODO: issue warning with gl debug info extension, when supported
Jamie Madill3b7e2052014-03-17 09:47:43 -0400369 if (IsMatrixType(shaderAttribute.type) ||
370 (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0)
Jamie Madill8664b062014-02-14 16:41:29 -0500371 {
Jamie Madill3b7e2052014-03-17 09:47:43 -0400372 initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute);
Jamie Madill8664b062014-02-14 16:41:29 -0500373 }
374 else
375 {
Jamie Madilla53ab512014-03-17 09:47:44 -0400376 initHLSL += "input." + decorateVariable(shaderAttribute.name);
Jamie Madill8664b062014-02-14 16:41:29 -0500377 }
378
Jamie Madill3b7e2052014-03-17 09:47:43 -0400379 initHLSL += ";\n";
Jamie Madill3b7e2052014-03-17 09:47:43 -0400380
Jamie Madillac0a2672014-04-11 13:33:56 -0400381 inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type));
382 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500383 }
384
Geoff Lang04fb89a2014-06-09 15:05:36 -0400385 std::string replacementHLSL = "struct VS_INPUT\n"
386 "{\n" +
387 structHLSL +
388 "};\n"
389 "\n"
390 "void initAttributes(VS_INPUT input)\n"
391 "{\n" +
392 initHLSL +
393 "}\n";
394
395 std::string vertexHLSL(sourceShader);
396
397 size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING);
398 vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL);
399
400 return vertexHLSL;
401}
402
403std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables,
404 bool usesFragDepth, const std::vector<GLenum> &outputLayout) const
405{
406 const int shaderModel = mRenderer->getMajorShaderModel();
407 std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR";
408 std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH";
409
410 std::string declarationHLSL;
411 std::string copyHLSL;
412 for (size_t i = 0; i < outputVariables.size(); i++)
413 {
414 const PixelShaderOuputVariable& outputVariable = outputVariables[i];
415 ASSERT(outputLayout.size() > outputVariable.outputIndex);
Geoff Lang4ace4232014-06-18 19:12:48 -0400416
417 // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers
418 bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE
419 if (outputIndexEnabled)
Geoff Lang04fb89a2014-06-09 15:05:36 -0400420 {
421 declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name +
422 " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n";
423
424 copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n";
425 }
426 }
427
428 if (usesFragDepth)
429 {
430 declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n";
431 copyHLSL += " output.gl_Depth = gl_Depth; \n";
432 }
433
434 std::string replacementHLSL = "struct PS_OUTPUT\n"
435 "{\n" +
436 declarationHLSL +
437 "};\n"
438 "\n"
439 "PS_OUTPUT generateOutput()\n"
440 "{\n"
441 " PS_OUTPUT output;\n" +
442 copyHLSL +
443 " return output;\n"
444 "}\n";
445
446 std::string pixelHLSL(sourceShader);
447
448 size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING);
449 pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL);
450
451 return pixelHLSL;
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500452}
453
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400454std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const
455{
456 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
457 // In D3D11 we manually compute gl_PointCoord in the GS.
458 int shaderModel = mRenderer->getMajorShaderModel();
459 return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD");
460}
461
462struct DynamicHLSL::SemanticInfo
463{
464 struct BuiltinInfo
465 {
466 BuiltinInfo()
467 : enabled(false),
468 index(0),
469 systemValue(false)
470 {}
471
472 bool enabled;
473 std::string semantic;
474 unsigned int index;
475 bool systemValue;
476
477 std::string str() const
478 {
479 return (systemValue ? semantic : (semantic + Str(index)));
480 }
481
482 void enableSystem(const std::string &systemValueSemantic)
483 {
484 enabled = true;
485 semantic = systemValueSemantic;
486 systemValue = true;
487 }
488
489 void enable(const std::string &semanticVal, unsigned int indexVal)
490 {
491 enabled = true;
492 semantic = semanticVal;
493 index = indexVal;
494 }
495 };
496
497 BuiltinInfo dxPosition;
498 BuiltinInfo glPosition;
499 BuiltinInfo glFragCoord;
500 BuiltinInfo glPointCoord;
501 BuiltinInfo glPointSize;
502};
503
504DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord,
505 bool pointSize, bool pixelShader) const
506{
507 SemanticInfo info;
508 bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4);
509 const std::string &varyingSemantic = getVaryingSemantic(pointSize);
510
511 int reservedRegisterIndex = startRegisters;
512
513 if (hlsl4)
514 {
515 info.dxPosition.enableSystem("SV_Position");
516 }
517 else if (pixelShader)
518 {
519 info.dxPosition.enableSystem("VPOS");
520 }
521 else
522 {
523 info.dxPosition.enableSystem("POSITION");
524 }
525
526 info.glPosition.enable(varyingSemantic, reservedRegisterIndex++);
527
528 if (fragCoord)
529 {
530 info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++);
531 }
532
533 if (pointCoord)
534 {
535 // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord)
536 // In D3D11 we manually compute gl_PointCoord in the GS.
537 if (hlsl4)
538 {
539 info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++);
540 }
541 else
542 {
543 info.glPointCoord.enable("TEXCOORD", 0);
544 }
545 }
546
547 // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders
548 if (pointSize && (!pixelShader || hlsl4))
549 {
550 info.glPointSize.enableSystem("PSIZE");
551 }
552
553 return info;
554}
555
556std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const
557{
558 std::string linkHLSL = "{\n";
559
560 ASSERT(info.dxPosition.enabled && info.glPosition.enabled);
561
562 linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n";
563 linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n";
564
565 if (info.glFragCoord.enabled)
566 {
567 linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n";
568 }
569
570 if (info.glPointCoord.enabled)
571 {
572 linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n";
573 }
574
575 linkHLSL += varyingHLSL;
576
577 if (info.glPointSize.enabled)
578 {
579 linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n";
580 }
581
582 linkHLSL += "};\n";
583
584 return linkHLSL;
585}
586
587void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info,
588 std::vector<LinkedVarying> *linkedVaryings) const
589{
590 ASSERT(info.glPosition.enabled);
591
592 linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic,
593 info.glPosition.index, 1));
594
595 if (info.glFragCoord.enabled)
596 {
597 linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic,
598 info.glFragCoord.index, 1));
599 }
600
601 if (info.glPointSize.enabled)
602 {
603 linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1));
604 }
605}
606
Brandon Jones71620962014-08-20 14:04:59 -0700607void DynamicHLSL::storeUserLinkedVaryings(const rx::VertexShaderD3D *vertexShader,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400608 std::vector<LinkedVarying> *linkedVaryings) const
609{
Brandon Jones71620962014-08-20 14:04:59 -0700610 const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize);
611 const std::vector<PackedVarying> &varyings = vertexShader->mVaryings;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400612
613 for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++)
614 {
615 const PackedVarying &varying = varyings[varyingIndex];
616 if (varying.registerAssigned())
617 {
618 GLenum transposedType = TransposeMatrixType(varying.type);
619 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
620
621 linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
622 varyingSemantic, varying.registerIndex,
623 variableRows * varying.elementCount()));
624 }
625 }
626}
627
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400628bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
Jamie Madill5f562732014-02-14 16:41:24 -0500629 std::string& pixelHLSL, std::string& vertexHLSL,
Brandon Jones71620962014-08-20 14:04:59 -0700630 rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader,
Geoff Lang48dcae72014-02-05 16:28:24 -0500631 const std::vector<std::string>& transformFeedbackVaryings,
632 std::vector<LinkedVarying> *linkedVaryings,
Geoff Lang04fb89a2014-06-09 15:05:36 -0400633 std::map<int, VariableLocation> *programOutputVars,
634 std::vector<PixelShaderOuputVariable> *outPixelShaderKey,
635 bool *outUsesFragDepth) const
Jamie Madill5f562732014-02-14 16:41:24 -0500636{
637 if (pixelHLSL.empty() || vertexHLSL.empty())
638 {
639 return false;
640 }
641
Brandon Jones71620962014-08-20 14:04:59 -0700642 bool usesMRT = fragmentShader->mUsesMultipleRenderTargets;
643 bool usesFragColor = fragmentShader->mUsesFragColor;
644 bool usesFragData = fragmentShader->mUsesFragData;
645 bool usesFragCoord = fragmentShader->mUsesFragCoord;
646 bool usesPointCoord = fragmentShader->mUsesPointCoord;
647 bool usesPointSize = vertexShader->mUsesPointSize;
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400648
Jamie Madill5f562732014-02-14 16:41:24 -0500649 if (usesFragColor && usesFragData)
650 {
651 infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader.");
652 return false;
653 }
654
655 // Write the HLSL input/output declarations
656 const int shaderModel = mRenderer->getMajorShaderModel();
657 const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
658
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400659 const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0);
Jamie Madill5f562732014-02-14 16:41:24 -0500660
661 // Two cases when writing to gl_FragColor and using ESSL 1.0:
662 // - with a 3.0 context, the output color is copied to channel 0
663 // - with a 2.0 context, the output color is broadcast to all channels
Brandon Jones71620962014-08-20 14:04:59 -0700664 const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3);
Geoff Langc0b9ef42014-07-02 10:02:37 -0400665 const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1);
Jamie Madill5f562732014-02-14 16:41:24 -0500666
Brandon Jones71620962014-08-20 14:04:59 -0700667 int shaderVersion = vertexShader->getShaderVersion();
Jamie Madill5f562732014-02-14 16:41:24 -0500668
669 if (registersNeeded > maxVaryingVectors)
670 {
671 infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord");
Jamie Madill5f562732014-02-14 16:41:24 -0500672 return false;
673 }
674
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400675 const std::string &varyingHLSL = generateVaryingHLSL(vertexShader);
676 const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord,
677 false, usesPointSize, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500678
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400679 storeUserLinkedVaryings(vertexShader, linkedVaryings);
680 storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings);
Jamie Madill5f562732014-02-14 16:41:24 -0500681
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500682 // Add stub string to be replaced when shader is dynamically defined by its layout
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400683 vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"
684 "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500685 "VS_OUTPUT main(VS_INPUT input)\n"
Jamie Madillc5ede1a2014-02-14 16:41:27 -0500686 "{\n"
687 " initAttributes(input);\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500688
689 if (shaderModel >= 4)
690 {
691 vertexHLSL += "\n"
692 " gl_main();\n"
693 "\n"
694 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500695 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400696 " output.dx_Position.x = gl_Position.x;\n"
697 " output.dx_Position.y = -gl_Position.y;\n"
698 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
699 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500700 }
701 else
702 {
703 vertexHLSL += "\n"
704 " gl_main();\n"
705 "\n"
706 " VS_OUTPUT output;\n"
Geoff Lang48dcae72014-02-05 16:28:24 -0500707 " output.gl_Position = gl_Position;\n"
Jamie Madill2bf8b372014-06-16 17:18:51 -0400708 " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n"
709 " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"
710 " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
711 " output.dx_Position.w = gl_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500712 }
713
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400714 if (usesPointSize && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500715 {
716 vertexHLSL += " output.gl_PointSize = gl_PointSize;\n";
717 }
718
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400719 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500720 {
721 vertexHLSL += " output.gl_FragCoord = gl_Position;\n";
722 }
723
Brandon Jones71620962014-08-20 14:04:59 -0700724 for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500725 {
Brandon Jones71620962014-08-20 14:04:59 -0700726 const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400727 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500728 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400729 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500730 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400731 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
Jamie Madill5f562732014-02-14 16:41:24 -0500732
733 for (int row = 0; row < variableRows; row++)
734 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400735 int r = varying.registerIndex + elementIndex * variableRows + row;
Jamie Madill5f562732014-02-14 16:41:24 -0500736 vertexHLSL += " output.v" + Str(r);
737
738 bool sharedRegister = false; // Register used by multiple varyings
739
740 for (int x = 0; x < 4; x++)
741 {
742 if (packing[r][x] && packing[r][x] != packing[r][0])
743 {
744 sharedRegister = true;
745 break;
746 }
747 }
748
749 if(sharedRegister)
750 {
751 vertexHLSL += ".";
752
753 for (int x = 0; x < 4; x++)
754 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400755 if (packing[r][x] == &varying)
Jamie Madill5f562732014-02-14 16:41:24 -0500756 {
757 switch(x)
758 {
759 case 0: vertexHLSL += "x"; break;
760 case 1: vertexHLSL += "y"; break;
761 case 2: vertexHLSL += "z"; break;
762 case 3: vertexHLSL += "w"; break;
763 }
764 }
765 }
766 }
767
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400768 vertexHLSL += " = _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500769
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400770 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500771 {
772 vertexHLSL += ArrayString(elementIndex);
773 }
774
775 if (variableRows > 1)
776 {
777 vertexHLSL += ArrayString(row);
778 }
779
780 vertexHLSL += ";\n";
781 }
782 }
783 }
784 }
785
786 vertexHLSL += "\n"
787 " return output;\n"
788 "}\n";
789
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400790 const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord,
791 usesPointSize, true);
Jamie Madill5f562732014-02-14 16:41:24 -0500792
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400793 pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500794
795 if (shaderVersion < 300)
796 {
797 for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++)
798 {
Geoff Lang04fb89a2014-06-09 15:05:36 -0400799 PixelShaderOuputVariable outputKeyVariable;
800 outputKeyVariable.type = GL_FLOAT_VEC4;
801 outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex);
802 outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]";
803 outputKeyVariable.outputIndex = renderTargetIndex;
804
805 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500806 }
807
Brandon Jones71620962014-08-20 14:04:59 -0700808 *outUsesFragDepth = fragmentShader->mUsesFragDepth;
Jamie Madill5f562732014-02-14 16:41:24 -0500809 }
810 else
811 {
812 defineOutputVariables(fragmentShader, programOutputVars);
813
Brandon Jones71620962014-08-20 14:04:59 -0700814 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500815 for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++)
816 {
817 const VariableLocation &outputLocation = locationIt->second;
Jamie Madillf2575982014-06-25 16:04:54 -0400818 const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index];
Geoff Lang04fb89a2014-06-09 15:05:36 -0400819 const std::string &variableName = "out_" + outputLocation.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500820 const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element));
821
Geoff Lang04fb89a2014-06-09 15:05:36 -0400822 PixelShaderOuputVariable outputKeyVariable;
823 outputKeyVariable.type = outputVariable.type;
824 outputKeyVariable.name = variableName + elementString;
825 outputKeyVariable.source = variableName + ArrayString(outputLocation.element);
826 outputKeyVariable.outputIndex = locationIt->first;
827
828 outPixelShaderKey->push_back(outputKeyVariable);
Jamie Madill5f562732014-02-14 16:41:24 -0500829 }
Geoff Lang04fb89a2014-06-09 15:05:36 -0400830
831 *outUsesFragDepth = false;
Jamie Madill5f562732014-02-14 16:41:24 -0500832 }
833
Geoff Lang04fb89a2014-06-09 15:05:36 -0400834 pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500835
Brandon Jones71620962014-08-20 14:04:59 -0700836 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500837 {
838 if (shaderModel >= 4)
839 {
840 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n"
841 "{\n";
842 }
843 else
844 {
845 pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n"
846 "{\n";
847 }
848 }
849 else
850 {
851 pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n"
852 "{\n";
853 }
854
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400855 if (usesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -0500856 {
857 pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n";
858
859 if (shaderModel >= 4)
860 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400861 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n"
862 " gl_FragCoord.y = input.dx_Position.y;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500863 }
864 else if (shaderModel >= 3)
865 {
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400866 pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n"
867 " gl_FragCoord.y = input.dx_Position.y + 0.5;\n";
Jamie Madill5f562732014-02-14 16:41:24 -0500868 }
869 else
870 {
871 // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport()
872 pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n"
873 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n";
874 }
875
876 pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n"
877 " gl_FragCoord.w = rhw;\n";
878 }
879
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400880 if (usesPointCoord && shaderModel >= 3)
Jamie Madill5f562732014-02-14 16:41:24 -0500881 {
882 pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n";
883 pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n";
884 }
885
Brandon Jones71620962014-08-20 14:04:59 -0700886 if (fragmentShader->mUsesFrontFacing)
Jamie Madill5f562732014-02-14 16:41:24 -0500887 {
888 if (shaderModel <= 3)
889 {
890 pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n";
891 }
892 else
893 {
894 pixelHLSL += " gl_FrontFacing = isFrontFace;\n";
895 }
896 }
897
Brandon Jones71620962014-08-20 14:04:59 -0700898 for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500899 {
Brandon Jones71620962014-08-20 14:04:59 -0700900 const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex];
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400901 if (varying.registerAssigned())
Jamie Madill5f562732014-02-14 16:41:24 -0500902 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400903 for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
Jamie Madill5f562732014-02-14 16:41:24 -0500904 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400905 GLenum transposedType = TransposeMatrixType(varying.type);
906 int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
Jamie Madill5f562732014-02-14 16:41:24 -0500907 for (int row = 0; row < variableRows; row++)
908 {
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400909 std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
910 pixelHLSL += " _" + varying.name;
Jamie Madill5f562732014-02-14 16:41:24 -0500911
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400912 if (varying.isArray())
Jamie Madill5f562732014-02-14 16:41:24 -0500913 {
914 pixelHLSL += ArrayString(elementIndex);
915 }
916
917 if (variableRows > 1)
918 {
919 pixelHLSL += ArrayString(row);
920 }
921
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400922 if (varying.isStruct())
Jamie Madill5f562732014-02-14 16:41:24 -0500923 {
924 pixelHLSL += " = input.v" + n + ";\n"; break;
925 }
926 else
927 {
928 switch (VariableColumnCount(transposedType))
929 {
930 case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break;
931 case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break;
932 case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break;
933 case 4: pixelHLSL += " = input.v" + n + ";\n"; break;
934 default: UNREACHABLE();
935 }
936 }
937 }
938 }
939 }
940 else UNREACHABLE();
941 }
942
943 pixelHLSL += "\n"
944 " gl_main();\n"
945 "\n"
Geoff Lang04fb89a2014-06-09 15:05:36 -0400946 " return generateOutput();\n"
Jamie Madill5f562732014-02-14 16:41:24 -0500947 "}\n";
948
949 return true;
950}
951
Brandon Jones71620962014-08-20 14:04:59 -0700952void DynamicHLSL::defineOutputVariables(rx::FragmentShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const
Jamie Madill5f562732014-02-14 16:41:24 -0500953{
Brandon Jones71620962014-08-20 14:04:59 -0700954 const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getOutputVariables();
Jamie Madill5f562732014-02-14 16:41:24 -0500955
956 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++)
957 {
Jamie Madillf2575982014-06-25 16:04:54 -0400958 const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill5f562732014-02-14 16:41:24 -0500959 const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location;
960
961 if (outputVariable.arraySize > 0)
962 {
963 for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++)
964 {
965 const int location = baseLocation + elementIndex;
966 ASSERT(programOutputVars->count(location) == 0);
967 (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex);
968 }
969 }
970 else
971 {
972 ASSERT(programOutputVars->count(baseLocation) == 0);
973 (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex);
974 }
975 }
976}
977
Brandon Jones71620962014-08-20 14:04:59 -0700978std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500979{
980 // for now we only handle point sprite emulation
Brandon Jones71620962014-08-20 14:04:59 -0700981 ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
Jamie Madillff0d2ba2014-05-14 13:49:10 -0400982 return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
Jamie Madill5f562732014-02-14 16:41:24 -0500983}
984
Brandon Jones71620962014-08-20 14:04:59 -0700985std::string DynamicHLSL::generatePointSpriteHLSL(int registers, rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader) const
Jamie Madill5f562732014-02-14 16:41:24 -0500986{
987 ASSERT(registers >= 0);
Brandon Jones71620962014-08-20 14:04:59 -0700988 ASSERT(vertexShader->mUsesPointSize);
Jamie Madill5f562732014-02-14 16:41:24 -0500989 ASSERT(mRenderer->getMajorShaderModel() >= 4);
990
991 std::string geomHLSL;
992
Brandon Jones71620962014-08-20 14:04:59 -0700993 const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400994 false, true, false);
Brandon Jones71620962014-08-20 14:04:59 -0700995 const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord,
996 fragmentShader->mUsesPointCoord, true, false);
Jamie Madill5f562732014-02-14 16:41:24 -0500997
Jamie Madillfebb7ad2014-06-18 13:50:51 -0400998 std::string varyingHLSL = generateVaryingHLSL(vertexShader);
999 std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL);
1000 std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL);
Jamie Madill5f562732014-02-14 16:41:24 -05001001
Geoff Langc0b9ef42014-07-02 10:02:37 -04001002 // TODO(geofflang): use context's caps
Jamie Madill5f562732014-02-14 16:41:24 -05001003 geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n"
1004 "\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001005 "struct GS_INPUT\n" + inLinkHLSL + "\n" +
1006 "struct GS_OUTPUT\n" + outLinkHLSL + "\n" +
Jamie Madill5f562732014-02-14 16:41:24 -05001007 "\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001008 "static float2 pointSpriteCorners[] = \n"
1009 "{\n"
1010 " float2( 0.5f, -0.5f),\n"
1011 " float2( 0.5f, 0.5f),\n"
1012 " float2(-0.5f, -0.5f),\n"
1013 " float2(-0.5f, 0.5f)\n"
1014 "};\n"
1015 "\n"
1016 "static float2 pointSpriteTexcoords[] = \n"
1017 "{\n"
1018 " float2(1.0f, 1.0f),\n"
1019 " float2(1.0f, 0.0f),\n"
1020 " float2(0.0f, 1.0f),\n"
1021 " float2(0.0f, 0.0f)\n"
1022 "};\n"
1023 "\n"
Geoff Langc0b9ef42014-07-02 10:02:37 -04001024 "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n"
1025 "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n"
Jamie Madill5f562732014-02-14 16:41:24 -05001026 "\n"
1027 "[maxvertexcount(4)]\n"
1028 "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n"
1029 "{\n"
1030 " GS_OUTPUT output = (GS_OUTPUT)0;\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001031 " output.gl_Position = input[0].gl_Position;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001032 " output.gl_PointSize = input[0].gl_PointSize;\n";
1033
1034 for (int r = 0; r < registers; r++)
1035 {
1036 geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n";
1037 }
1038
Brandon Jones71620962014-08-20 14:04:59 -07001039 if (fragmentShader->mUsesFragCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001040 {
1041 geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n";
1042 }
1043
1044 geomHLSL += " \n"
1045 " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001046 " float4 dx_Position = input[0].dx_Position;\n"
1047 " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001048
1049 for (int corner = 0; corner < 4; corner++)
1050 {
1051 geomHLSL += " \n"
Jamie Madillfebb7ad2014-06-18 13:50:51 -04001052 " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n";
Jamie Madill5f562732014-02-14 16:41:24 -05001053
Brandon Jones71620962014-08-20 14:04:59 -07001054 if (fragmentShader->mUsesPointCoord)
Jamie Madill5f562732014-02-14 16:41:24 -05001055 {
1056 geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n";
1057 }
1058
1059 geomHLSL += " outStream.Append(output);\n";
1060 }
1061
1062 geomHLSL += " \n"
1063 " outStream.RestartStrip();\n"
1064 "}\n";
1065
1066 return geomHLSL;
1067}
1068
1069// This method needs to match OutputHLSL::decorate
Jamie Madilla53ab512014-03-17 09:47:44 -04001070std::string DynamicHLSL::decorateVariable(const std::string &name)
Jamie Madill5f562732014-02-14 16:41:24 -05001071{
Jamie Madilld5512cd2014-07-10 17:50:08 -04001072 if (name.compare(0, 3, "gl_") != 0)
Jamie Madill5f562732014-02-14 16:41:24 -05001073 {
1074 return "_" + name;
1075 }
1076
1077 return name;
1078}
1079
Jamie Madillf2575982014-06-25 16:04:54 -04001080std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001081{
Jamie Madilla53ab512014-03-17 09:47:44 -04001082 std::string attribString = "input." + decorateVariable(shaderAttrib.name);
Jamie Madill8664b062014-02-14 16:41:29 -05001083
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001084 // Matrix
1085 if (IsMatrixType(shaderAttrib.type))
1086 {
Jamie Madill8664b062014-02-14 16:41:29 -05001087 return "transpose(" + attribString + ")";
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001088 }
1089
Jamie Madillf2575982014-06-25 16:04:54 -04001090 GLenum shaderComponentType = VariableComponentType(shaderAttrib.type);
1091 int shaderComponentCount = VariableComponentCount(shaderAttrib.type);
Jamie Madill8664b062014-02-14 16:41:29 -05001092
Jamie Madill8664b062014-02-14 16:41:29 -05001093 // Perform integer to float conversion (if necessary)
1094 bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT);
1095
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001096 if (requiresTypeConversion)
Jamie Madill8664b062014-02-14 16:41:29 -05001097 {
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001098 // TODO: normalization for 32-bit integer formats
1099 ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger);
1100 return "float" + Str(shaderComponentCount) + "(" + attribString + ")";
Jamie Madill8664b062014-02-14 16:41:29 -05001101 }
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001102
1103 // No conversion necessary
Jamie Madill8664b062014-02-14 16:41:29 -05001104 return attribString;
Jamie Madillc5ede1a2014-02-14 16:41:27 -05001105}
1106
Jamie Madill7a29e4a2014-05-02 10:41:48 -04001107void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const
1108{
1109 for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++)
1110 {
1111 const VertexFormat &vertexFormat = inputLayout[inputIndex];
1112
1113 if (vertexFormat.mType == GL_NONE)
1114 {
1115 signature[inputIndex] = GL_NONE;
1116 }
1117 else
1118 {
1119 bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0);
1120 signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE);
1121 }
1122 }
1123}
1124
Jamie Madill5f562732014-02-14 16:41:24 -05001125}