Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1 | // |
| 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 | |
| 9 | #include "precompiled.h" |
| 10 | |
| 11 | #include "libGLESv2/DynamicHLSL.h" |
| 12 | #include "libGLESv2/Shader.h" |
| 13 | #include "libGLESv2/Program.h" |
| 14 | #include "libGLESv2/renderer/Renderer.h" |
| 15 | #include "common/utilities.h" |
| 16 | #include "libGLESv2/ProgramBinary.h" |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 17 | #include "libGLESv2/formatutils.h" |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 18 | #include "common/blocklayout.h" |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 19 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 20 | static std::string Str(int i) |
| 21 | { |
| 22 | char buffer[20]; |
| 23 | snprintf(buffer, sizeof(buffer), "%d", i); |
| 24 | return buffer; |
| 25 | } |
| 26 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 27 | namespace gl_d3d |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 28 | { |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 29 | |
| 30 | std::string HLSLComponentTypeString(GLenum componentType) |
| 31 | { |
| 32 | switch (componentType) |
| 33 | { |
| 34 | case GL_UNSIGNED_INT: return "uint"; |
| 35 | case GL_INT: return "int"; |
| 36 | case GL_UNSIGNED_NORMALIZED: |
| 37 | case GL_SIGNED_NORMALIZED: |
| 38 | case GL_FLOAT: return "float"; |
| 39 | default: UNREACHABLE(); return "not-component-type"; |
| 40 | } |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 43 | std::string HLSLComponentTypeString(GLenum componentType, int componentCount) |
| 44 | { |
| 45 | return HLSLComponentTypeString(componentType) + (componentCount > 1 ? Str(componentCount) : ""); |
| 46 | } |
| 47 | |
| 48 | std::string HLSLMatrixTypeString(GLenum type) |
| 49 | { |
| 50 | switch (type) |
| 51 | { |
| 52 | case GL_FLOAT_MAT2: return "float2x2"; |
| 53 | case GL_FLOAT_MAT3: return "float3x3"; |
| 54 | case GL_FLOAT_MAT4: return "float4x4"; |
| 55 | case GL_FLOAT_MAT2x3: return "float2x3"; |
| 56 | case GL_FLOAT_MAT3x2: return "float3x2"; |
| 57 | case GL_FLOAT_MAT2x4: return "float2x4"; |
| 58 | case GL_FLOAT_MAT4x2: return "float4x2"; |
| 59 | case GL_FLOAT_MAT3x4: return "float3x4"; |
| 60 | case GL_FLOAT_MAT4x3: return "float4x3"; |
| 61 | default: UNREACHABLE(); return "not-matrix-type"; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | std::string HLSLTypeString(GLenum type) |
| 66 | { |
| 67 | if (gl::IsMatrixType(type)) |
| 68 | { |
| 69 | return HLSLMatrixTypeString(type); |
| 70 | } |
| 71 | |
| 72 | return HLSLComponentTypeString(gl::UniformComponentType(type), gl::UniformComponentCount(type)); |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | |
| 77 | namespace gl |
| 78 | { |
| 79 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 80 | std::string ArrayString(unsigned int i) |
| 81 | { |
| 82 | return (i == GL_INVALID_INDEX ? "" : "[" + Str(i) + "]"); |
| 83 | } |
| 84 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 85 | const std::string DynamicHLSL::VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@"; |
| 86 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 87 | DynamicHLSL::DynamicHLSL(rx::Renderer *const renderer) |
| 88 | : mRenderer(renderer) |
| 89 | { |
| 90 | } |
| 91 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 92 | static bool packVarying(Varying *varying, const int maxVaryingVectors, const ShaderVariable *packing[][4]) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 93 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 94 | GLenum transposedType = TransposeMatrixType(varying->type); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 95 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 96 | // matrices within varying structs are not transposed |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 97 | int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount(); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 98 | int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType)); |
| 99 | bool success = false; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 100 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 101 | if (elements == 2 || elements == 3 || elements == 4) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 102 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 103 | for (int r = 0; r <= maxVaryingVectors - registers && !success; r++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 104 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 105 | bool available = true; |
| 106 | |
| 107 | for (int y = 0; y < registers && available; y++) |
| 108 | { |
| 109 | for (int x = 0; x < elements && available; x++) |
| 110 | { |
| 111 | if (packing[r + y][x]) |
| 112 | { |
| 113 | available = false; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (available) |
| 119 | { |
| 120 | varying->registerIndex = r; |
| 121 | varying->elementIndex = 0; |
| 122 | |
| 123 | for (int y = 0; y < registers; y++) |
| 124 | { |
| 125 | for (int x = 0; x < elements; x++) |
| 126 | { |
| 127 | packing[r + y][x] = &*varying; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | success = true; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (!success && elements == 2) |
| 136 | { |
| 137 | for (int r = maxVaryingVectors - registers; r >= 0 && !success; r--) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 138 | { |
| 139 | bool available = true; |
| 140 | |
| 141 | for (int y = 0; y < registers && available; y++) |
| 142 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 143 | for (int x = 2; x < 4 && available; x++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 144 | { |
| 145 | if (packing[r + y][x]) |
| 146 | { |
| 147 | available = false; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (available) |
| 153 | { |
| 154 | varying->registerIndex = r; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 155 | varying->elementIndex = 2; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 156 | |
| 157 | for (int y = 0; y < registers; y++) |
| 158 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 159 | for (int x = 2; x < 4; x++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 160 | { |
| 161 | packing[r + y][x] = &*varying; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | success = true; |
| 166 | } |
| 167 | } |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 168 | } |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 169 | } |
| 170 | else if (elements == 1) |
| 171 | { |
| 172 | int space[4] = { 0 }; |
| 173 | |
| 174 | for (int y = 0; y < maxVaryingVectors; y++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 175 | { |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 176 | for (int x = 0; x < 4; x++) |
| 177 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 178 | space[x] += packing[y][x] ? 0 : 1; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 179 | } |
| 180 | } |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 181 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 182 | int column = 0; |
| 183 | |
| 184 | for (int x = 0; x < 4; x++) |
| 185 | { |
| 186 | if (space[x] >= registers && space[x] < space[column]) |
| 187 | { |
| 188 | column = x; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (space[column] >= registers) |
| 193 | { |
| 194 | for (int r = 0; r < maxVaryingVectors; r++) |
| 195 | { |
| 196 | if (!packing[r][column]) |
| 197 | { |
| 198 | varying->registerIndex = r; |
| 199 | |
| 200 | for (int y = r; y < r + registers; y++) |
| 201 | { |
| 202 | packing[y][column] = &*varying; |
| 203 | } |
| 204 | |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | varying->elementIndex = column; |
| 210 | |
| 211 | success = true; |
| 212 | } |
| 213 | } |
| 214 | else UNREACHABLE(); |
| 215 | |
| 216 | return success; |
| 217 | } |
| 218 | |
| 219 | // Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111 |
| 220 | // Returns the number of used varying registers, or -1 if unsuccesful |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 221 | int DynamicHLSL::packVaryings(InfoLog &infoLog, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 222 | VertexShader *vertexShader, const std::vector<std::string>& transformFeedbackVaryings) |
| 223 | { |
| 224 | const int maxVaryingVectors = mRenderer->getMaxVaryingVectors(); |
| 225 | |
| 226 | vertexShader->resetVaryingsRegisterAssignment(); |
| 227 | fragmentShader->resetVaryingsRegisterAssignment(); |
| 228 | |
| 229 | std::set<std::string> packedVaryings; |
| 230 | |
| 231 | for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++) |
| 232 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 233 | Varying *varying = &fragmentShader->mVaryings[varyingIndex]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 234 | if (packVarying(varying, maxVaryingVectors, packing)) |
| 235 | { |
| 236 | packedVaryings.insert(varying->name); |
| 237 | } |
| 238 | else |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 239 | { |
| 240 | infoLog.append("Could not pack varying %s", varying->name.c_str()); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 241 | return -1; |
| 242 | } |
| 243 | } |
| 244 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 245 | for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++) |
| 246 | { |
| 247 | const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex]; |
| 248 | if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end()) |
| 249 | { |
| 250 | bool found = false; |
| 251 | for (unsigned int varyingIndex = 0; varyingIndex < vertexShader->mVaryings.size(); varyingIndex++) |
| 252 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 253 | Varying *varying = &vertexShader->mVaryings[varyingIndex]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 254 | if (transformFeedbackVarying == varying->name) |
| 255 | { |
| 256 | if (!packVarying(varying, maxVaryingVectors, packing)) |
| 257 | { |
| 258 | infoLog.append("Could not pack varying %s", varying->name.c_str()); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | found = true; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize") |
| 268 | { |
| 269 | infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str()); |
| 270 | return -1; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 275 | // Return the number of used registers |
| 276 | int registers = 0; |
| 277 | |
| 278 | for (int r = 0; r < maxVaryingVectors; r++) |
| 279 | { |
| 280 | if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3]) |
| 281 | { |
| 282 | registers++; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return registers; |
| 287 | } |
| 288 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 289 | std::string DynamicHLSL::generateVaryingHLSL(VertexShader *shader, const std::string &varyingSemantic, |
| 290 | std::vector<LinkedVarying> *linkedVaryings) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 291 | { |
| 292 | std::string varyingHLSL; |
| 293 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 294 | for (unsigned int varyingIndex = 0; varyingIndex < shader->mVaryings.size(); varyingIndex++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 295 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 296 | Varying *varying = &shader->mVaryings[varyingIndex]; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 297 | if (varying->registerAssigned()) |
| 298 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 299 | GLenum transposedType = TransposeMatrixType(varying->type); |
| 300 | int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType)); |
| 301 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 302 | for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++) |
| 303 | { |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 304 | for (int row = 0; row < variableRows; row++) |
| 305 | { |
| 306 | switch (varying->interpolation) |
| 307 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 308 | case INTERPOLATION_SMOOTH: varyingHLSL += " "; break; |
| 309 | case INTERPOLATION_FLAT: varyingHLSL += " nointerpolation "; break; |
| 310 | case INTERPOLATION_CENTROID: varyingHLSL += " centroid "; break; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 311 | default: UNREACHABLE(); |
| 312 | } |
| 313 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 314 | unsigned int semanticIndex = elementIndex * variableRows + varying->registerIndex + row; |
| 315 | std::string n = Str(semanticIndex); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 316 | |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 317 | std::string typeString; |
| 318 | |
| 319 | if (varying->isStruct()) |
| 320 | { |
| 321 | // matrices within structs are not transposed, so |
| 322 | // do not use the special struct prefix "rm" |
| 323 | typeString = decorateVariable(varying->structName); |
| 324 | } |
| 325 | else |
| 326 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 327 | GLenum componentType = UniformComponentType(transposedType); |
| 328 | int columnCount = VariableColumnCount(transposedType); |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 329 | typeString = gl_d3d::HLSLComponentTypeString(componentType, columnCount); |
| 330 | } |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 331 | varyingHLSL += typeString + " v" + n + " : " + varyingSemantic + n + ";\n"; |
| 332 | } |
| 333 | } |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 334 | |
| 335 | if (linkedVaryings) |
| 336 | { |
| 337 | linkedVaryings->push_back(LinkedVarying(varying->name, varying->type, varying->elementCount(), |
| 338 | varyingSemantic, varying->registerIndex, |
| 339 | variableRows * varying->elementCount())); |
| 340 | } |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 341 | } |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | return varyingHLSL; |
| 345 | } |
| 346 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 347 | std::string DynamicHLSL::generateInputLayoutHLSL(const VertexFormat inputLayout[], const Attribute shaderAttributes[]) const |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 348 | { |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 349 | std::string structHLSL, initHLSL; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 350 | |
| 351 | int semanticIndex = 0; |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 352 | unsigned int inputIndex = 0; |
| 353 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 354 | for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 355 | { |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 356 | ASSERT(inputIndex < MAX_VERTEX_ATTRIBS); |
| 357 | |
| 358 | const VertexFormat &vertexFormat = inputLayout[inputIndex]; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 359 | const Attribute &shaderAttribute = shaderAttributes[attributeIndex]; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 360 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 361 | if (!shaderAttribute.name.empty()) |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 362 | { |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 363 | // HLSL code for input structure |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 364 | if (IsMatrixType(shaderAttribute.type)) |
| 365 | { |
| 366 | // Matrix types are always transposed |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 367 | structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type)); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 368 | } |
| 369 | else |
| 370 | { |
| 371 | GLenum componentType = mRenderer->getVertexComponentType(vertexFormat); |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 372 | structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, UniformComponentCount(shaderAttribute.type)); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 373 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 374 | |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 375 | structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n"; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 376 | semanticIndex += AttributeRegisterCount(shaderAttribute.type); |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 377 | |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 378 | // HLSL code for initialization |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 379 | initHLSL += " " + decorateVariable(shaderAttribute.name) + " = "; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 380 | |
| 381 | // Mismatched vertex attribute to vertex input may result in an undefined |
| 382 | // data reinterpretation (eg for pure integer->float, float->pure integer) |
| 383 | // TODO: issue warning with gl debug info extension, when supported |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 384 | if (IsMatrixType(shaderAttribute.type) || |
| 385 | (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 386 | { |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 387 | initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 388 | } |
| 389 | else |
| 390 | { |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 391 | initHLSL += "input." + decorateVariable(shaderAttribute.name); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 394 | initHLSL += ";\n"; |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 395 | |
Jamie Madill | ac0a267 | 2014-04-11 13:33:56 -0400 | [diff] [blame] | 396 | inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type)); |
| 397 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 400 | return "struct VS_INPUT\n" |
| 401 | "{\n" + |
| 402 | structHLSL + |
| 403 | "};\n" |
| 404 | "\n" |
| 405 | "void initAttributes(VS_INPUT input)\n" |
| 406 | "{\n" + |
| 407 | initHLSL + |
| 408 | "}\n"; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 409 | } |
| 410 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 411 | bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const ShaderVariable *packing[][4], |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 412 | std::string& pixelHLSL, std::string& vertexHLSL, |
| 413 | FragmentShader *fragmentShader, VertexShader *vertexShader, |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 414 | const std::vector<std::string>& transformFeedbackVaryings, |
| 415 | std::vector<LinkedVarying> *linkedVaryings, |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 416 | std::map<int, VariableLocation> *programOutputVars) const |
| 417 | { |
| 418 | if (pixelHLSL.empty() || vertexHLSL.empty()) |
| 419 | { |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | bool usesMRT = fragmentShader->mUsesMultipleRenderTargets; |
| 424 | bool usesFragColor = fragmentShader->mUsesFragColor; |
| 425 | bool usesFragData = fragmentShader->mUsesFragData; |
| 426 | if (usesFragColor && usesFragData) |
| 427 | { |
| 428 | infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader."); |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | // Write the HLSL input/output declarations |
| 433 | const int shaderModel = mRenderer->getMajorShaderModel(); |
| 434 | const int maxVaryingVectors = mRenderer->getMaxVaryingVectors(); |
| 435 | |
| 436 | const int registersNeeded = registers + (fragmentShader->mUsesFragCoord ? 1 : 0) + (fragmentShader->mUsesPointCoord ? 1 : 0); |
| 437 | |
| 438 | // Two cases when writing to gl_FragColor and using ESSL 1.0: |
| 439 | // - with a 3.0 context, the output color is copied to channel 0 |
| 440 | // - with a 2.0 context, the output color is broadcast to all channels |
| 441 | const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3); |
| 442 | const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1); |
| 443 | |
| 444 | int shaderVersion = vertexShader->getShaderVersion(); |
| 445 | |
| 446 | if (registersNeeded > maxVaryingVectors) |
| 447 | { |
| 448 | infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord"); |
| 449 | |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | std::string varyingSemantic = (vertexShader->mUsesPointSize && shaderModel == 3) ? "COLOR" : "TEXCOORD"; |
| 454 | std::string targetSemantic = (shaderModel >= 4) ? "SV_Target" : "COLOR"; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 455 | std::string dxPositionSemantic = (shaderModel >= 4) ? "SV_Position" : "POSITION"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 456 | std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH"; |
| 457 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 458 | std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, linkedVaryings); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 459 | |
| 460 | // special varyings that use reserved registers |
| 461 | int reservedRegisterIndex = registers; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 462 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 463 | unsigned int glPositionSemanticIndex = reservedRegisterIndex++; |
| 464 | std::string glPositionSemantic = varyingSemantic; |
| 465 | |
| 466 | std::string fragCoordSemantic; |
| 467 | unsigned int fragCoordSemanticIndex = 0; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 468 | if (fragmentShader->mUsesFragCoord) |
| 469 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 470 | fragCoordSemanticIndex = reservedRegisterIndex++; |
| 471 | fragCoordSemantic = varyingSemantic; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 472 | } |
| 473 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 474 | std::string pointCoordSemantic; |
| 475 | unsigned int pointCoordSemanticIndex = 0; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 476 | if (fragmentShader->mUsesPointCoord) |
| 477 | { |
| 478 | // Shader model 3 uses a special TEXCOORD semantic for point sprite texcoords. |
| 479 | // In DX11 we compute this in the GS. |
| 480 | if (shaderModel == 3) |
| 481 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 482 | pointCoordSemanticIndex = 0; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 483 | pointCoordSemantic = "TEXCOORD0"; |
| 484 | } |
| 485 | else if (shaderModel >= 4) |
| 486 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 487 | pointCoordSemanticIndex = reservedRegisterIndex++; |
| 488 | pointCoordSemantic = varyingSemantic; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 492 | // Add stub string to be replaced when shader is dynamically defined by its layout |
| 493 | vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 494 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 495 | vertexHLSL += "struct VS_OUTPUT\n" |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 496 | "{\n"; |
| 497 | |
| 498 | if (shaderModel < 4) |
| 499 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 500 | vertexHLSL += " float4 _dx_Position : " + dxPositionSemantic + ";\n"; |
| 501 | vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n"; |
| 502 | linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1)); |
| 503 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | vertexHLSL += varyingHLSL; |
| 507 | |
| 508 | if (fragmentShader->mUsesFragCoord) |
| 509 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 510 | vertexHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n"; |
| 511 | linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, fragCoordSemantic, fragCoordSemanticIndex, 1)); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | if (vertexShader->mUsesPointSize && shaderModel >= 3) |
| 515 | { |
| 516 | vertexHLSL += " float gl_PointSize : PSIZE;\n"; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 517 | linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1)); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | if (shaderModel >= 4) |
| 521 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 522 | vertexHLSL += " float4 _dx_Position : " + dxPositionSemantic + ";\n"; |
| 523 | vertexHLSL += " float4 gl_Position : " + glPositionSemantic + Str(glPositionSemanticIndex) + ";\n"; |
| 524 | linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, glPositionSemantic, glPositionSemanticIndex, 1)); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | vertexHLSL += "};\n" |
| 528 | "\n" |
| 529 | "VS_OUTPUT main(VS_INPUT input)\n" |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 530 | "{\n" |
| 531 | " initAttributes(input);\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 532 | |
| 533 | if (shaderModel >= 4) |
| 534 | { |
| 535 | vertexHLSL += "\n" |
| 536 | " gl_main();\n" |
| 537 | "\n" |
| 538 | " VS_OUTPUT output;\n" |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 539 | " output.gl_Position = gl_Position;\n" |
| 540 | " output._dx_Position.x = gl_Position.x;\n" |
| 541 | " output._dx_Position.y = -gl_Position.y;\n" |
| 542 | " output._dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
| 543 | " output._dx_Position.w = gl_Position.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 544 | } |
| 545 | else |
| 546 | { |
| 547 | vertexHLSL += "\n" |
| 548 | " gl_main();\n" |
| 549 | "\n" |
| 550 | " VS_OUTPUT output;\n" |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 551 | " output.gl_Position = gl_Position;\n" |
| 552 | " output._dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n" |
| 553 | " output._dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n" |
| 554 | " output._dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
| 555 | " output._dx_Position.w = gl_Position.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | if (vertexShader->mUsesPointSize && shaderModel >= 3) |
| 559 | { |
| 560 | vertexHLSL += " output.gl_PointSize = gl_PointSize;\n"; |
| 561 | } |
| 562 | |
| 563 | if (fragmentShader->mUsesFragCoord) |
| 564 | { |
| 565 | vertexHLSL += " output.gl_FragCoord = gl_Position;\n"; |
| 566 | } |
| 567 | |
| 568 | for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++) |
| 569 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 570 | Varying *varying = &vertexShader->mVaryings[vertVaryingIndex]; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 571 | if (varying->registerAssigned()) |
| 572 | { |
| 573 | for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++) |
| 574 | { |
| 575 | int variableRows = (varying->isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying->type))); |
| 576 | |
| 577 | for (int row = 0; row < variableRows; row++) |
| 578 | { |
| 579 | int r = varying->registerIndex + elementIndex * variableRows + row; |
| 580 | vertexHLSL += " output.v" + Str(r); |
| 581 | |
| 582 | bool sharedRegister = false; // Register used by multiple varyings |
| 583 | |
| 584 | for (int x = 0; x < 4; x++) |
| 585 | { |
| 586 | if (packing[r][x] && packing[r][x] != packing[r][0]) |
| 587 | { |
| 588 | sharedRegister = true; |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if(sharedRegister) |
| 594 | { |
| 595 | vertexHLSL += "."; |
| 596 | |
| 597 | for (int x = 0; x < 4; x++) |
| 598 | { |
| 599 | if (packing[r][x] == &*varying) |
| 600 | { |
| 601 | switch(x) |
| 602 | { |
| 603 | case 0: vertexHLSL += "x"; break; |
| 604 | case 1: vertexHLSL += "y"; break; |
| 605 | case 2: vertexHLSL += "z"; break; |
| 606 | case 3: vertexHLSL += "w"; break; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | vertexHLSL += " = _" + varying->name; |
| 613 | |
| 614 | if (varying->isArray()) |
| 615 | { |
| 616 | vertexHLSL += ArrayString(elementIndex); |
| 617 | } |
| 618 | |
| 619 | if (variableRows > 1) |
| 620 | { |
| 621 | vertexHLSL += ArrayString(row); |
| 622 | } |
| 623 | |
| 624 | vertexHLSL += ";\n"; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | vertexHLSL += "\n" |
| 631 | " return output;\n" |
| 632 | "}\n"; |
| 633 | |
| 634 | pixelHLSL += "struct PS_INPUT\n" |
| 635 | "{\n"; |
| 636 | |
| 637 | pixelHLSL += varyingHLSL; |
| 638 | |
| 639 | if (fragmentShader->mUsesFragCoord) |
| 640 | { |
Geoff Lang | b5b0285 | 2014-04-16 14:39:36 -0400 | [diff] [blame] | 641 | pixelHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + Str(fragCoordSemanticIndex) + ";\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | if (fragmentShader->mUsesPointCoord && shaderModel >= 3) |
| 645 | { |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 646 | pixelHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + Str(pointCoordSemanticIndex) + ";\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // Must consume the PSIZE element if the geometry shader is not active |
| 650 | // We won't know if we use a GS until we draw |
| 651 | if (vertexShader->mUsesPointSize && shaderModel >= 4) |
| 652 | { |
| 653 | pixelHLSL += " float gl_PointSize : PSIZE;\n"; |
| 654 | } |
| 655 | |
| 656 | if (fragmentShader->mUsesFragCoord) |
| 657 | { |
| 658 | if (shaderModel >= 4) |
| 659 | { |
| 660 | pixelHLSL += " float4 dx_VPos : SV_Position;\n"; |
| 661 | } |
| 662 | else if (shaderModel >= 3) |
| 663 | { |
| 664 | pixelHLSL += " float2 dx_VPos : VPOS;\n"; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | pixelHLSL += "};\n" |
| 669 | "\n" |
| 670 | "struct PS_OUTPUT\n" |
| 671 | "{\n"; |
| 672 | |
| 673 | if (shaderVersion < 300) |
| 674 | { |
| 675 | for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++) |
| 676 | { |
| 677 | pixelHLSL += " float4 gl_Color" + Str(renderTargetIndex) + " : " + targetSemantic + Str(renderTargetIndex) + ";\n"; |
| 678 | } |
| 679 | |
| 680 | if (fragmentShader->mUsesFragDepth) |
| 681 | { |
| 682 | pixelHLSL += " float gl_Depth : " + depthSemantic + ";\n"; |
| 683 | } |
| 684 | } |
| 685 | else |
| 686 | { |
| 687 | defineOutputVariables(fragmentShader, programOutputVars); |
| 688 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 689 | const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 690 | for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++) |
| 691 | { |
| 692 | const VariableLocation &outputLocation = locationIt->second; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 693 | const ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index]; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 694 | const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element)); |
| 695 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 696 | pixelHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 697 | " out_" + outputLocation.name + elementString + |
| 698 | " : " + targetSemantic + Str(locationIt->first) + ";\n"; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | pixelHLSL += "};\n" |
| 703 | "\n"; |
| 704 | |
| 705 | if (fragmentShader->mUsesFrontFacing) |
| 706 | { |
| 707 | if (shaderModel >= 4) |
| 708 | { |
| 709 | pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n" |
| 710 | "{\n"; |
| 711 | } |
| 712 | else |
| 713 | { |
| 714 | pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n" |
| 715 | "{\n"; |
| 716 | } |
| 717 | } |
| 718 | else |
| 719 | { |
| 720 | pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n" |
| 721 | "{\n"; |
| 722 | } |
| 723 | |
| 724 | if (fragmentShader->mUsesFragCoord) |
| 725 | { |
| 726 | pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n"; |
| 727 | |
| 728 | if (shaderModel >= 4) |
| 729 | { |
| 730 | pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x;\n" |
| 731 | " gl_FragCoord.y = input.dx_VPos.y;\n"; |
| 732 | } |
| 733 | else if (shaderModel >= 3) |
| 734 | { |
| 735 | pixelHLSL += " gl_FragCoord.x = input.dx_VPos.x + 0.5;\n" |
| 736 | " gl_FragCoord.y = input.dx_VPos.y + 0.5;\n"; |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport() |
| 741 | pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n" |
| 742 | " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n"; |
| 743 | } |
| 744 | |
| 745 | pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n" |
| 746 | " gl_FragCoord.w = rhw;\n"; |
| 747 | } |
| 748 | |
| 749 | if (fragmentShader->mUsesPointCoord && shaderModel >= 3) |
| 750 | { |
| 751 | pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n"; |
| 752 | pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n"; |
| 753 | } |
| 754 | |
| 755 | if (fragmentShader->mUsesFrontFacing) |
| 756 | { |
| 757 | if (shaderModel <= 3) |
| 758 | { |
| 759 | pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n"; |
| 760 | } |
| 761 | else |
| 762 | { |
| 763 | pixelHLSL += " gl_FrontFacing = isFrontFace;\n"; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++) |
| 768 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 769 | Varying *varying = &fragmentShader->mVaryings[varyingIndex]; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 770 | if (varying->registerAssigned()) |
| 771 | { |
| 772 | for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++) |
| 773 | { |
| 774 | GLenum transposedType = TransposeMatrixType(varying->type); |
| 775 | int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType)); |
| 776 | for (int row = 0; row < variableRows; row++) |
| 777 | { |
| 778 | std::string n = Str(varying->registerIndex + elementIndex * variableRows + row); |
| 779 | pixelHLSL += " _" + varying->name; |
| 780 | |
| 781 | if (varying->isArray()) |
| 782 | { |
| 783 | pixelHLSL += ArrayString(elementIndex); |
| 784 | } |
| 785 | |
| 786 | if (variableRows > 1) |
| 787 | { |
| 788 | pixelHLSL += ArrayString(row); |
| 789 | } |
| 790 | |
| 791 | if (varying->isStruct()) |
| 792 | { |
| 793 | pixelHLSL += " = input.v" + n + ";\n"; break; |
| 794 | } |
| 795 | else |
| 796 | { |
| 797 | switch (VariableColumnCount(transposedType)) |
| 798 | { |
| 799 | case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break; |
| 800 | case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break; |
| 801 | case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break; |
| 802 | case 4: pixelHLSL += " = input.v" + n + ";\n"; break; |
| 803 | default: UNREACHABLE(); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | else UNREACHABLE(); |
| 810 | } |
| 811 | |
| 812 | pixelHLSL += "\n" |
| 813 | " gl_main();\n" |
| 814 | "\n" |
| 815 | " PS_OUTPUT output;\n"; |
| 816 | |
| 817 | if (shaderVersion < 300) |
| 818 | { |
| 819 | for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++) |
| 820 | { |
| 821 | unsigned int sourceColorIndex = broadcast ? 0 : renderTargetIndex; |
| 822 | |
| 823 | pixelHLSL += " output.gl_Color" + Str(renderTargetIndex) + " = gl_Color[" + Str(sourceColorIndex) + "];\n"; |
| 824 | } |
| 825 | |
| 826 | if (fragmentShader->mUsesFragDepth) |
| 827 | { |
| 828 | pixelHLSL += " output.gl_Depth = gl_Depth;\n"; |
| 829 | } |
| 830 | } |
| 831 | else |
| 832 | { |
| 833 | for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++) |
| 834 | { |
| 835 | const VariableLocation &outputLocation = locationIt->second; |
| 836 | const std::string &variableName = "out_" + outputLocation.name; |
| 837 | const std::string &outVariableName = variableName + (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element)); |
| 838 | const std::string &staticVariableName = variableName + ArrayString(outputLocation.element); |
| 839 | |
| 840 | pixelHLSL += " output." + outVariableName + " = " + staticVariableName + ";\n"; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | pixelHLSL += "\n" |
| 845 | " return output;\n" |
| 846 | "}\n"; |
| 847 | |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const |
| 852 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 853 | const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 854 | |
| 855 | for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++) |
| 856 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 857 | const Attribute &outputVariable = shaderOutputVars[outputVariableIndex]; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 858 | const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location; |
| 859 | |
| 860 | if (outputVariable.arraySize > 0) |
| 861 | { |
| 862 | for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++) |
| 863 | { |
| 864 | const int location = baseLocation + elementIndex; |
| 865 | ASSERT(programOutputVars->count(location) == 0); |
| 866 | (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex); |
| 867 | } |
| 868 | } |
| 869 | else |
| 870 | { |
| 871 | ASSERT(programOutputVars->count(baseLocation) == 0); |
| 872 | (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 877 | std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 878 | { |
| 879 | // for now we only handle point sprite emulation |
| 880 | ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4); |
| 881 | return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader); |
| 882 | } |
| 883 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 884 | std::string DynamicHLSL::generatePointSpriteHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 885 | { |
| 886 | ASSERT(registers >= 0); |
| 887 | ASSERT(vertexShader->mUsesPointSize); |
| 888 | ASSERT(mRenderer->getMajorShaderModel() >= 4); |
| 889 | |
| 890 | std::string geomHLSL; |
| 891 | |
| 892 | std::string varyingSemantic = "TEXCOORD"; |
| 893 | |
| 894 | std::string fragCoordSemantic; |
| 895 | std::string pointCoordSemantic; |
| 896 | |
| 897 | int reservedRegisterIndex = registers; |
| 898 | |
| 899 | if (fragmentShader->mUsesFragCoord) |
| 900 | { |
| 901 | fragCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++); |
| 902 | } |
| 903 | |
| 904 | if (fragmentShader->mUsesPointCoord) |
| 905 | { |
| 906 | pointCoordSemantic = varyingSemantic + Str(reservedRegisterIndex++); |
| 907 | } |
| 908 | |
| 909 | geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n" |
| 910 | "\n" |
| 911 | "struct GS_INPUT\n" |
| 912 | "{\n"; |
| 913 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 914 | std::string varyingHLSL = generateVaryingHLSL(vertexShader, varyingSemantic, NULL); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 915 | |
| 916 | geomHLSL += varyingHLSL; |
| 917 | |
| 918 | if (fragmentShader->mUsesFragCoord) |
| 919 | { |
| 920 | geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n"; |
| 921 | } |
| 922 | |
| 923 | geomHLSL += " float gl_PointSize : PSIZE;\n" |
| 924 | " float4 gl_Position : SV_Position;\n" |
| 925 | "};\n" |
| 926 | "\n" |
| 927 | "struct GS_OUTPUT\n" |
| 928 | "{\n"; |
| 929 | |
| 930 | geomHLSL += varyingHLSL; |
| 931 | |
| 932 | if (fragmentShader->mUsesFragCoord) |
| 933 | { |
| 934 | geomHLSL += " float4 gl_FragCoord : " + fragCoordSemantic + ";\n"; |
| 935 | } |
| 936 | |
| 937 | if (fragmentShader->mUsesPointCoord) |
| 938 | { |
| 939 | geomHLSL += " float2 gl_PointCoord : " + pointCoordSemantic + ";\n"; |
| 940 | } |
| 941 | |
| 942 | geomHLSL += " float gl_PointSize : PSIZE;\n" |
| 943 | " float4 gl_Position : SV_Position;\n" |
| 944 | "};\n" |
| 945 | "\n" |
| 946 | "static float2 pointSpriteCorners[] = \n" |
| 947 | "{\n" |
| 948 | " float2( 0.5f, -0.5f),\n" |
| 949 | " float2( 0.5f, 0.5f),\n" |
| 950 | " float2(-0.5f, -0.5f),\n" |
| 951 | " float2(-0.5f, 0.5f)\n" |
| 952 | "};\n" |
| 953 | "\n" |
| 954 | "static float2 pointSpriteTexcoords[] = \n" |
| 955 | "{\n" |
| 956 | " float2(1.0f, 1.0f),\n" |
| 957 | " float2(1.0f, 0.0f),\n" |
| 958 | " float2(0.0f, 1.0f),\n" |
| 959 | " float2(0.0f, 0.0f)\n" |
| 960 | "};\n" |
| 961 | "\n" |
| 962 | "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n" |
| 963 | "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n" |
| 964 | "\n" |
| 965 | "[maxvertexcount(4)]\n" |
| 966 | "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n" |
| 967 | "{\n" |
| 968 | " GS_OUTPUT output = (GS_OUTPUT)0;\n" |
| 969 | " output.gl_PointSize = input[0].gl_PointSize;\n"; |
| 970 | |
| 971 | for (int r = 0; r < registers; r++) |
| 972 | { |
| 973 | geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n"; |
| 974 | } |
| 975 | |
| 976 | if (fragmentShader->mUsesFragCoord) |
| 977 | { |
| 978 | geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n"; |
| 979 | } |
| 980 | |
| 981 | geomHLSL += " \n" |
| 982 | " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n" |
| 983 | " float4 gl_Position = input[0].gl_Position;\n" |
| 984 | " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * gl_Position.w;\n"; |
| 985 | |
| 986 | for (int corner = 0; corner < 4; corner++) |
| 987 | { |
| 988 | geomHLSL += " \n" |
| 989 | " output.gl_Position = gl_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n"; |
| 990 | |
| 991 | if (fragmentShader->mUsesPointCoord) |
| 992 | { |
| 993 | geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n"; |
| 994 | } |
| 995 | |
| 996 | geomHLSL += " outStream.Append(output);\n"; |
| 997 | } |
| 998 | |
| 999 | geomHLSL += " \n" |
| 1000 | " outStream.RestartStrip();\n" |
| 1001 | "}\n"; |
| 1002 | |
| 1003 | return geomHLSL; |
| 1004 | } |
| 1005 | |
| 1006 | // This method needs to match OutputHLSL::decorate |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 1007 | std::string DynamicHLSL::decorateVariable(const std::string &name) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1008 | { |
| 1009 | if (name.compare(0, 3, "gl_") != 0 && name.compare(0, 3, "dx_") != 0) |
| 1010 | { |
| 1011 | return "_" + name; |
| 1012 | } |
| 1013 | |
| 1014 | return name; |
| 1015 | } |
| 1016 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 1017 | std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const ShaderVariable &shaderAttrib) const |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1018 | { |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 1019 | std::string attribString = "input." + decorateVariable(shaderAttrib.name); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1020 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1021 | // Matrix |
| 1022 | if (IsMatrixType(shaderAttrib.type)) |
| 1023 | { |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1024 | return "transpose(" + attribString + ")"; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1025 | } |
| 1026 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1027 | GLenum shaderComponentType = UniformComponentType(shaderAttrib.type); |
| 1028 | int shaderComponentCount = UniformComponentCount(shaderAttrib.type); |
| 1029 | |
| 1030 | std::string padString = ""; |
| 1031 | |
| 1032 | // Perform integer to float conversion (if necessary) |
| 1033 | bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT); |
| 1034 | |
| 1035 | // TODO: normalization for 32-bit integer formats |
| 1036 | ASSERT(!requiresTypeConversion || !vertexFormat.mNormalized); |
| 1037 | |
| 1038 | if (requiresTypeConversion || !padString.empty()) |
| 1039 | { |
| 1040 | return "float" + Str(shaderComponentCount) + "(" + attribString + padString + ")"; |
| 1041 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1042 | |
| 1043 | // No conversion necessary |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1044 | return attribString; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1045 | } |
| 1046 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1047 | } |