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