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 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 285 | std::string DynamicHLSL::generateVaryingHLSL(VertexShader *shader) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 286 | { |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 287 | std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 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 | } |
| 330 | } |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return varyingHLSL; |
| 334 | } |
| 335 | |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 336 | 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] | 337 | { |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 338 | std::string structHLSL, initHLSL; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 339 | |
| 340 | int semanticIndex = 0; |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 341 | unsigned int inputIndex = 0; |
| 342 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 343 | for (unsigned int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 344 | { |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 345 | ASSERT(inputIndex < MAX_VERTEX_ATTRIBS); |
| 346 | |
| 347 | const VertexFormat &vertexFormat = inputLayout[inputIndex]; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 348 | const Attribute &shaderAttribute = shaderAttributes[attributeIndex]; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 349 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 350 | if (!shaderAttribute.name.empty()) |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 351 | { |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 352 | // HLSL code for input structure |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 353 | if (IsMatrixType(shaderAttribute.type)) |
| 354 | { |
| 355 | // Matrix types are always transposed |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 356 | structHLSL += " " + gl_d3d::HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type)); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 357 | } |
| 358 | else |
| 359 | { |
| 360 | GLenum componentType = mRenderer->getVertexComponentType(vertexFormat); |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 361 | structHLSL += " " + gl_d3d::HLSLComponentTypeString(componentType, UniformComponentCount(shaderAttribute.type)); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 362 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 363 | |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 364 | structHLSL += " " + decorateVariable(shaderAttribute.name) + " : TEXCOORD" + Str(semanticIndex) + ";\n"; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 365 | semanticIndex += AttributeRegisterCount(shaderAttribute.type); |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 366 | |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 367 | // HLSL code for initialization |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 368 | initHLSL += " " + decorateVariable(shaderAttribute.name) + " = "; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 369 | |
| 370 | // Mismatched vertex attribute to vertex input may result in an undefined |
| 371 | // data reinterpretation (eg for pure integer->float, float->pure integer) |
| 372 | // TODO: issue warning with gl debug info extension, when supported |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 373 | if (IsMatrixType(shaderAttribute.type) || |
| 374 | (mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 375 | { |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 376 | initHLSL += generateAttributeConversionHLSL(vertexFormat, shaderAttribute); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 377 | } |
| 378 | else |
| 379 | { |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 380 | initHLSL += "input." + decorateVariable(shaderAttribute.name); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 381 | } |
| 382 | |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 383 | initHLSL += ";\n"; |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 384 | |
Jamie Madill | ac0a267 | 2014-04-11 13:33:56 -0400 | [diff] [blame] | 385 | inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type)); |
| 386 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 387 | } |
| 388 | |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 389 | std::string replacementHLSL = "struct VS_INPUT\n" |
| 390 | "{\n" + |
| 391 | structHLSL + |
| 392 | "};\n" |
| 393 | "\n" |
| 394 | "void initAttributes(VS_INPUT input)\n" |
| 395 | "{\n" + |
| 396 | initHLSL + |
| 397 | "}\n"; |
| 398 | |
| 399 | std::string vertexHLSL(sourceShader); |
| 400 | |
| 401 | size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING); |
| 402 | vertexHLSL.replace(copyInsertionPos, VERTEX_ATTRIBUTE_STUB_STRING.length(), replacementHLSL); |
| 403 | |
| 404 | return vertexHLSL; |
| 405 | } |
| 406 | |
| 407 | std::string DynamicHLSL::generatePixelShaderForOutputSignature(const std::string &sourceShader, const std::vector<PixelShaderOuputVariable> &outputVariables, |
| 408 | bool usesFragDepth, const std::vector<GLenum> &outputLayout) const |
| 409 | { |
| 410 | const int shaderModel = mRenderer->getMajorShaderModel(); |
| 411 | std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR"; |
| 412 | std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH"; |
| 413 | |
| 414 | std::string declarationHLSL; |
| 415 | std::string copyHLSL; |
| 416 | for (size_t i = 0; i < outputVariables.size(); i++) |
| 417 | { |
| 418 | const PixelShaderOuputVariable& outputVariable = outputVariables[i]; |
| 419 | ASSERT(outputLayout.size() > outputVariable.outputIndex); |
Geoff Lang | 4ace423 | 2014-06-18 19:12:48 -0400 | [diff] [blame] | 420 | |
| 421 | // FIXME(geofflang): Work around NVIDIA driver bug by repacking buffers |
| 422 | bool outputIndexEnabled = true; // outputLayout[outputVariable.outputIndex] != GL_NONE |
| 423 | if (outputIndexEnabled) |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 424 | { |
| 425 | declarationHLSL += " " + gl_d3d::HLSLTypeString(outputVariable.type) + " " + outputVariable.name + |
| 426 | " : " + targetSemantic + Str(outputVariable.outputIndex) + ";\n"; |
| 427 | |
| 428 | copyHLSL += " output." + outputVariable.name + " = " + outputVariable.source + ";\n"; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (usesFragDepth) |
| 433 | { |
| 434 | declarationHLSL += " float gl_Depth : " + depthSemantic + ";\n"; |
| 435 | copyHLSL += " output.gl_Depth = gl_Depth; \n"; |
| 436 | } |
| 437 | |
| 438 | std::string replacementHLSL = "struct PS_OUTPUT\n" |
| 439 | "{\n" + |
| 440 | declarationHLSL + |
| 441 | "};\n" |
| 442 | "\n" |
| 443 | "PS_OUTPUT generateOutput()\n" |
| 444 | "{\n" |
| 445 | " PS_OUTPUT output;\n" + |
| 446 | copyHLSL + |
| 447 | " return output;\n" |
| 448 | "}\n"; |
| 449 | |
| 450 | std::string pixelHLSL(sourceShader); |
| 451 | |
| 452 | size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING); |
| 453 | pixelHLSL.replace(outputInsertionPos, PIXEL_OUTPUT_STUB_STRING.length(), replacementHLSL); |
| 454 | |
| 455 | return pixelHLSL; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 456 | } |
| 457 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 458 | std::string DynamicHLSL::getVaryingSemantic(bool pointSize) const |
| 459 | { |
| 460 | // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord) |
| 461 | // In D3D11 we manually compute gl_PointCoord in the GS. |
| 462 | int shaderModel = mRenderer->getMajorShaderModel(); |
| 463 | return ((pointSize && shaderModel < 4) ? "COLOR" : "TEXCOORD"); |
| 464 | } |
| 465 | |
| 466 | struct DynamicHLSL::SemanticInfo |
| 467 | { |
| 468 | struct BuiltinInfo |
| 469 | { |
| 470 | BuiltinInfo() |
| 471 | : enabled(false), |
| 472 | index(0), |
| 473 | systemValue(false) |
| 474 | {} |
| 475 | |
| 476 | bool enabled; |
| 477 | std::string semantic; |
| 478 | unsigned int index; |
| 479 | bool systemValue; |
| 480 | |
| 481 | std::string str() const |
| 482 | { |
| 483 | return (systemValue ? semantic : (semantic + Str(index))); |
| 484 | } |
| 485 | |
| 486 | void enableSystem(const std::string &systemValueSemantic) |
| 487 | { |
| 488 | enabled = true; |
| 489 | semantic = systemValueSemantic; |
| 490 | systemValue = true; |
| 491 | } |
| 492 | |
| 493 | void enable(const std::string &semanticVal, unsigned int indexVal) |
| 494 | { |
| 495 | enabled = true; |
| 496 | semantic = semanticVal; |
| 497 | index = indexVal; |
| 498 | } |
| 499 | }; |
| 500 | |
| 501 | BuiltinInfo dxPosition; |
| 502 | BuiltinInfo glPosition; |
| 503 | BuiltinInfo glFragCoord; |
| 504 | BuiltinInfo glPointCoord; |
| 505 | BuiltinInfo glPointSize; |
| 506 | }; |
| 507 | |
| 508 | DynamicHLSL::SemanticInfo DynamicHLSL::getSemanticInfo(int startRegisters, bool fragCoord, bool pointCoord, |
| 509 | bool pointSize, bool pixelShader) const |
| 510 | { |
| 511 | SemanticInfo info; |
| 512 | bool hlsl4 = (mRenderer->getMajorShaderModel() >= 4); |
| 513 | const std::string &varyingSemantic = getVaryingSemantic(pointSize); |
| 514 | |
| 515 | int reservedRegisterIndex = startRegisters; |
| 516 | |
| 517 | if (hlsl4) |
| 518 | { |
| 519 | info.dxPosition.enableSystem("SV_Position"); |
| 520 | } |
| 521 | else if (pixelShader) |
| 522 | { |
| 523 | info.dxPosition.enableSystem("VPOS"); |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | info.dxPosition.enableSystem("POSITION"); |
| 528 | } |
| 529 | |
| 530 | info.glPosition.enable(varyingSemantic, reservedRegisterIndex++); |
| 531 | |
| 532 | if (fragCoord) |
| 533 | { |
| 534 | info.glFragCoord.enable(varyingSemantic, reservedRegisterIndex++); |
| 535 | } |
| 536 | |
| 537 | if (pointCoord) |
| 538 | { |
| 539 | // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord) |
| 540 | // In D3D11 we manually compute gl_PointCoord in the GS. |
| 541 | if (hlsl4) |
| 542 | { |
| 543 | info.glPointCoord.enable(varyingSemantic, reservedRegisterIndex++); |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | info.glPointCoord.enable("TEXCOORD", 0); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders |
| 552 | if (pointSize && (!pixelShader || hlsl4)) |
| 553 | { |
| 554 | info.glPointSize.enableSystem("PSIZE"); |
| 555 | } |
| 556 | |
| 557 | return info; |
| 558 | } |
| 559 | |
| 560 | std::string DynamicHLSL::generateVaryingLinkHLSL(const SemanticInfo &info, const std::string &varyingHLSL) const |
| 561 | { |
| 562 | std::string linkHLSL = "{\n"; |
| 563 | |
| 564 | ASSERT(info.dxPosition.enabled && info.glPosition.enabled); |
| 565 | |
| 566 | linkHLSL += " float4 dx_Position : " + info.dxPosition.str() + ";\n"; |
| 567 | linkHLSL += " float4 gl_Position : " + info.glPosition.str() + ";\n"; |
| 568 | |
| 569 | if (info.glFragCoord.enabled) |
| 570 | { |
| 571 | linkHLSL += " float4 gl_FragCoord : " + info.glFragCoord.str() + ";\n"; |
| 572 | } |
| 573 | |
| 574 | if (info.glPointCoord.enabled) |
| 575 | { |
| 576 | linkHLSL += " float2 gl_PointCoord : " + info.glPointCoord.str() + ";\n"; |
| 577 | } |
| 578 | |
| 579 | linkHLSL += varyingHLSL; |
| 580 | |
| 581 | if (info.glPointSize.enabled) |
| 582 | { |
| 583 | linkHLSL += " float gl_PointSize : " + info.glPointSize.str() + ";\n"; |
| 584 | } |
| 585 | |
| 586 | linkHLSL += "};\n"; |
| 587 | |
| 588 | return linkHLSL; |
| 589 | } |
| 590 | |
| 591 | void DynamicHLSL::storeBuiltinLinkedVaryings(const SemanticInfo &info, |
| 592 | std::vector<LinkedVarying> *linkedVaryings) const |
| 593 | { |
| 594 | ASSERT(info.glPosition.enabled); |
| 595 | |
| 596 | linkedVaryings->push_back(LinkedVarying("gl_Position", GL_FLOAT_VEC4, 1, info.glPosition.semantic, |
| 597 | info.glPosition.index, 1)); |
| 598 | |
| 599 | if (info.glFragCoord.enabled) |
| 600 | { |
| 601 | linkedVaryings->push_back(LinkedVarying("gl_FragCoord", GL_FLOAT_VEC4, 1, info.glFragCoord.semantic, |
| 602 | info.glFragCoord.index, 1)); |
| 603 | } |
| 604 | |
| 605 | if (info.glPointSize.enabled) |
| 606 | { |
| 607 | linkedVaryings->push_back(LinkedVarying("gl_PointSize", GL_FLOAT, 1, "PSIZE", 0, 1)); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | void DynamicHLSL::storeUserLinkedVaryings(const VertexShader *vertexShader, |
| 612 | std::vector<LinkedVarying> *linkedVaryings) const |
| 613 | { |
| 614 | const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize); |
| 615 | const std::vector<PackedVarying> &varyings = vertexShader->mVaryings; |
| 616 | |
| 617 | for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++) |
| 618 | { |
| 619 | const PackedVarying &varying = varyings[varyingIndex]; |
| 620 | if (varying.registerAssigned()) |
| 621 | { |
| 622 | GLenum transposedType = TransposeMatrixType(varying.type); |
| 623 | int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType)); |
| 624 | |
| 625 | linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(), |
| 626 | varyingSemantic, varying.registerIndex, |
| 627 | variableRows * varying.elementCount())); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 632 | bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing, |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 633 | std::string& pixelHLSL, std::string& vertexHLSL, |
| 634 | FragmentShader *fragmentShader, VertexShader *vertexShader, |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 635 | const std::vector<std::string>& transformFeedbackVaryings, |
| 636 | std::vector<LinkedVarying> *linkedVaryings, |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 637 | std::map<int, VariableLocation> *programOutputVars, |
| 638 | std::vector<PixelShaderOuputVariable> *outPixelShaderKey, |
| 639 | bool *outUsesFragDepth) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 640 | { |
| 641 | if (pixelHLSL.empty() || vertexHLSL.empty()) |
| 642 | { |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | bool usesMRT = fragmentShader->mUsesMultipleRenderTargets; |
| 647 | bool usesFragColor = fragmentShader->mUsesFragColor; |
| 648 | bool usesFragData = fragmentShader->mUsesFragData; |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 649 | bool usesFragCoord = fragmentShader->mUsesFragCoord; |
| 650 | bool usesPointCoord = fragmentShader->mUsesPointCoord; |
| 651 | bool usesPointSize = vertexShader->mUsesPointSize; |
| 652 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 653 | if (usesFragColor && usesFragData) |
| 654 | { |
| 655 | infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader."); |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | // Write the HLSL input/output declarations |
| 660 | const int shaderModel = mRenderer->getMajorShaderModel(); |
| 661 | const int maxVaryingVectors = mRenderer->getMaxVaryingVectors(); |
| 662 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 663 | const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 664 | |
| 665 | // Two cases when writing to gl_FragColor and using ESSL 1.0: |
| 666 | // - with a 3.0 context, the output color is copied to channel 0 |
| 667 | // - with a 2.0 context, the output color is broadcast to all channels |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 668 | const bool broadcast = (usesFragColor && mRenderer->getCurrentClientVersion() < 3); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 669 | const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getMaxRenderTargets() : 1); |
| 670 | |
| 671 | int shaderVersion = vertexShader->getShaderVersion(); |
| 672 | |
| 673 | if (registersNeeded > maxVaryingVectors) |
| 674 | { |
| 675 | infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord"); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 676 | return false; |
| 677 | } |
| 678 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 679 | const std::string &varyingHLSL = generateVaryingHLSL(vertexShader); |
| 680 | const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord, |
| 681 | false, usesPointSize, false); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 682 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 683 | storeUserLinkedVaryings(vertexShader, linkedVaryings); |
| 684 | storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 685 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 686 | // Add stub string to be replaced when shader is dynamically defined by its layout |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 687 | vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n" |
| 688 | "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n" |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 689 | "VS_OUTPUT main(VS_INPUT input)\n" |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 690 | "{\n" |
| 691 | " initAttributes(input);\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 692 | |
| 693 | if (shaderModel >= 4) |
| 694 | { |
| 695 | vertexHLSL += "\n" |
| 696 | " gl_main();\n" |
| 697 | "\n" |
| 698 | " VS_OUTPUT output;\n" |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 699 | " output.gl_Position = gl_Position;\n" |
Jamie Madill | 2bf8b37 | 2014-06-16 17:18:51 -0400 | [diff] [blame] | 700 | " output.dx_Position.x = gl_Position.x;\n" |
| 701 | " output.dx_Position.y = -gl_Position.y;\n" |
| 702 | " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
| 703 | " output.dx_Position.w = gl_Position.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 704 | } |
| 705 | else |
| 706 | { |
| 707 | vertexHLSL += "\n" |
| 708 | " gl_main();\n" |
| 709 | "\n" |
| 710 | " VS_OUTPUT output;\n" |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 711 | " output.gl_Position = gl_Position;\n" |
Jamie Madill | 2bf8b37 | 2014-06-16 17:18:51 -0400 | [diff] [blame] | 712 | " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n" |
| 713 | " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n" |
| 714 | " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
| 715 | " output.dx_Position.w = gl_Position.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 716 | } |
| 717 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 718 | if (usesPointSize && shaderModel >= 3) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 719 | { |
| 720 | vertexHLSL += " output.gl_PointSize = gl_PointSize;\n"; |
| 721 | } |
| 722 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 723 | if (usesFragCoord) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 724 | { |
| 725 | vertexHLSL += " output.gl_FragCoord = gl_Position;\n"; |
| 726 | } |
| 727 | |
| 728 | for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++) |
| 729 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 730 | const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex]; |
| 731 | if (varying.registerAssigned()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 732 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 733 | for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 734 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 735 | int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type))); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 736 | |
| 737 | for (int row = 0; row < variableRows; row++) |
| 738 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 739 | int r = varying.registerIndex + elementIndex * variableRows + row; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 740 | vertexHLSL += " output.v" + Str(r); |
| 741 | |
| 742 | bool sharedRegister = false; // Register used by multiple varyings |
| 743 | |
| 744 | for (int x = 0; x < 4; x++) |
| 745 | { |
| 746 | if (packing[r][x] && packing[r][x] != packing[r][0]) |
| 747 | { |
| 748 | sharedRegister = true; |
| 749 | break; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if(sharedRegister) |
| 754 | { |
| 755 | vertexHLSL += "."; |
| 756 | |
| 757 | for (int x = 0; x < 4; x++) |
| 758 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 759 | if (packing[r][x] == &varying) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 760 | { |
| 761 | switch(x) |
| 762 | { |
| 763 | case 0: vertexHLSL += "x"; break; |
| 764 | case 1: vertexHLSL += "y"; break; |
| 765 | case 2: vertexHLSL += "z"; break; |
| 766 | case 3: vertexHLSL += "w"; break; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 772 | vertexHLSL += " = _" + varying.name; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 773 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 774 | if (varying.isArray()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 775 | { |
| 776 | vertexHLSL += ArrayString(elementIndex); |
| 777 | } |
| 778 | |
| 779 | if (variableRows > 1) |
| 780 | { |
| 781 | vertexHLSL += ArrayString(row); |
| 782 | } |
| 783 | |
| 784 | vertexHLSL += ";\n"; |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | vertexHLSL += "\n" |
| 791 | " return output;\n" |
| 792 | "}\n"; |
| 793 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 794 | const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord, |
| 795 | usesPointSize, true); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 796 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 797 | pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 798 | |
| 799 | if (shaderVersion < 300) |
| 800 | { |
| 801 | for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++) |
| 802 | { |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 803 | PixelShaderOuputVariable outputKeyVariable; |
| 804 | outputKeyVariable.type = GL_FLOAT_VEC4; |
| 805 | outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex); |
| 806 | outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]"; |
| 807 | outputKeyVariable.outputIndex = renderTargetIndex; |
| 808 | |
| 809 | outPixelShaderKey->push_back(outputKeyVariable); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 810 | } |
| 811 | |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 812 | *outUsesFragDepth = fragmentShader->mUsesFragDepth; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 813 | } |
| 814 | else |
| 815 | { |
| 816 | defineOutputVariables(fragmentShader, programOutputVars); |
| 817 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 818 | const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 819 | for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++) |
| 820 | { |
| 821 | const VariableLocation &outputLocation = locationIt->second; |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 822 | const ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index]; |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 823 | const std::string &variableName = "out_" + outputLocation.name; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 824 | const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element)); |
| 825 | |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 826 | PixelShaderOuputVariable outputKeyVariable; |
| 827 | outputKeyVariable.type = outputVariable.type; |
| 828 | outputKeyVariable.name = variableName + elementString; |
| 829 | outputKeyVariable.source = variableName + ArrayString(outputLocation.element); |
| 830 | outputKeyVariable.outputIndex = locationIt->first; |
| 831 | |
| 832 | outPixelShaderKey->push_back(outputKeyVariable); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 833 | } |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 834 | |
| 835 | *outUsesFragDepth = false; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 836 | } |
| 837 | |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 838 | pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 839 | |
| 840 | if (fragmentShader->mUsesFrontFacing) |
| 841 | { |
| 842 | if (shaderModel >= 4) |
| 843 | { |
| 844 | pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n" |
| 845 | "{\n"; |
| 846 | } |
| 847 | else |
| 848 | { |
| 849 | pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n" |
| 850 | "{\n"; |
| 851 | } |
| 852 | } |
| 853 | else |
| 854 | { |
| 855 | pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n" |
| 856 | "{\n"; |
| 857 | } |
| 858 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 859 | if (usesFragCoord) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 860 | { |
| 861 | pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n"; |
| 862 | |
| 863 | if (shaderModel >= 4) |
| 864 | { |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 865 | pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n" |
| 866 | " gl_FragCoord.y = input.dx_Position.y;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 867 | } |
| 868 | else if (shaderModel >= 3) |
| 869 | { |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 870 | pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n" |
| 871 | " gl_FragCoord.y = input.dx_Position.y + 0.5;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 872 | } |
| 873 | else |
| 874 | { |
| 875 | // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport() |
| 876 | pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n" |
| 877 | " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n"; |
| 878 | } |
| 879 | |
| 880 | pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n" |
| 881 | " gl_FragCoord.w = rhw;\n"; |
| 882 | } |
| 883 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 884 | if (usesPointCoord && shaderModel >= 3) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 885 | { |
| 886 | pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n"; |
| 887 | pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n"; |
| 888 | } |
| 889 | |
| 890 | if (fragmentShader->mUsesFrontFacing) |
| 891 | { |
| 892 | if (shaderModel <= 3) |
| 893 | { |
| 894 | pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n"; |
| 895 | } |
| 896 | else |
| 897 | { |
| 898 | pixelHLSL += " gl_FrontFacing = isFrontFace;\n"; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++) |
| 903 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 904 | const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex]; |
| 905 | if (varying.registerAssigned()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 906 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 907 | for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 908 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 909 | GLenum transposedType = TransposeMatrixType(varying.type); |
| 910 | int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType)); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 911 | for (int row = 0; row < variableRows; row++) |
| 912 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 913 | std::string n = Str(varying.registerIndex + elementIndex * variableRows + row); |
| 914 | pixelHLSL += " _" + varying.name; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 915 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 916 | if (varying.isArray()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 917 | { |
| 918 | pixelHLSL += ArrayString(elementIndex); |
| 919 | } |
| 920 | |
| 921 | if (variableRows > 1) |
| 922 | { |
| 923 | pixelHLSL += ArrayString(row); |
| 924 | } |
| 925 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 926 | if (varying.isStruct()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 927 | { |
| 928 | pixelHLSL += " = input.v" + n + ";\n"; break; |
| 929 | } |
| 930 | else |
| 931 | { |
| 932 | switch (VariableColumnCount(transposedType)) |
| 933 | { |
| 934 | case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break; |
| 935 | case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break; |
| 936 | case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break; |
| 937 | case 4: pixelHLSL += " = input.v" + n + ";\n"; break; |
| 938 | default: UNREACHABLE(); |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | else UNREACHABLE(); |
| 945 | } |
| 946 | |
| 947 | pixelHLSL += "\n" |
| 948 | " gl_main();\n" |
| 949 | "\n" |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 950 | " return generateOutput();\n" |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 951 | "}\n"; |
| 952 | |
| 953 | return true; |
| 954 | } |
| 955 | |
| 956 | void DynamicHLSL::defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const |
| 957 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 958 | const std::vector<Attribute> &shaderOutputVars = fragmentShader->getOutputVariables(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 959 | |
| 960 | for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++) |
| 961 | { |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 962 | const Attribute &outputVariable = shaderOutputVars[outputVariableIndex]; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 963 | const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location; |
| 964 | |
| 965 | if (outputVariable.arraySize > 0) |
| 966 | { |
| 967 | for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++) |
| 968 | { |
| 969 | const int location = baseLocation + elementIndex; |
| 970 | ASSERT(programOutputVars->count(location) == 0); |
| 971 | (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex); |
| 972 | } |
| 973 | } |
| 974 | else |
| 975 | { |
| 976 | ASSERT(programOutputVars->count(baseLocation) == 0); |
| 977 | (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex); |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 982 | std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 983 | { |
| 984 | // for now we only handle point sprite emulation |
| 985 | ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4); |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 986 | return generatePointSpriteHLSL(registers, fragmentShader, vertexShader); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 987 | } |
| 988 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 989 | std::string DynamicHLSL::generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 990 | { |
| 991 | ASSERT(registers >= 0); |
| 992 | ASSERT(vertexShader->mUsesPointSize); |
| 993 | ASSERT(mRenderer->getMajorShaderModel() >= 4); |
| 994 | |
| 995 | std::string geomHLSL; |
| 996 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 997 | const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord, |
| 998 | false, true, false); |
| 999 | const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord, |
| 1000 | fragmentShader->mUsesPointCoord, true, false); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1001 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 1002 | std::string varyingHLSL = generateVaryingHLSL(vertexShader); |
| 1003 | std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL); |
| 1004 | std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1005 | |
| 1006 | geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n" |
| 1007 | "\n" |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 1008 | "struct GS_INPUT\n" + inLinkHLSL + "\n" + |
| 1009 | "struct GS_OUTPUT\n" + outLinkHLSL + "\n" + |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1010 | "\n" |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1011 | "static float2 pointSpriteCorners[] = \n" |
| 1012 | "{\n" |
| 1013 | " float2( 0.5f, -0.5f),\n" |
| 1014 | " float2( 0.5f, 0.5f),\n" |
| 1015 | " float2(-0.5f, -0.5f),\n" |
| 1016 | " float2(-0.5f, 0.5f)\n" |
| 1017 | "};\n" |
| 1018 | "\n" |
| 1019 | "static float2 pointSpriteTexcoords[] = \n" |
| 1020 | "{\n" |
| 1021 | " float2(1.0f, 1.0f),\n" |
| 1022 | " float2(1.0f, 0.0f),\n" |
| 1023 | " float2(0.0f, 1.0f),\n" |
| 1024 | " float2(0.0f, 0.0f)\n" |
| 1025 | "};\n" |
| 1026 | "\n" |
| 1027 | "static float minPointSize = " + Str(ALIASED_POINT_SIZE_RANGE_MIN) + ".0f;\n" |
| 1028 | "static float maxPointSize = " + Str(mRenderer->getMaxPointSize()) + ".0f;\n" |
| 1029 | "\n" |
| 1030 | "[maxvertexcount(4)]\n" |
| 1031 | "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n" |
| 1032 | "{\n" |
| 1033 | " GS_OUTPUT output = (GS_OUTPUT)0;\n" |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 1034 | " output.gl_Position = input[0].gl_Position;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1035 | " output.gl_PointSize = input[0].gl_PointSize;\n"; |
| 1036 | |
| 1037 | for (int r = 0; r < registers; r++) |
| 1038 | { |
| 1039 | geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n"; |
| 1040 | } |
| 1041 | |
| 1042 | if (fragmentShader->mUsesFragCoord) |
| 1043 | { |
| 1044 | geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n"; |
| 1045 | } |
| 1046 | |
| 1047 | geomHLSL += " \n" |
| 1048 | " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n" |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 1049 | " float4 dx_Position = input[0].dx_Position;\n" |
| 1050 | " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / dx_ViewCoords.y) * dx_Position.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1051 | |
| 1052 | for (int corner = 0; corner < 4; corner++) |
| 1053 | { |
| 1054 | geomHLSL += " \n" |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame^] | 1055 | " output.dx_Position = dx_Position + float4(pointSpriteCorners[" + Str(corner) + "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1056 | |
| 1057 | if (fragmentShader->mUsesPointCoord) |
| 1058 | { |
| 1059 | geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n"; |
| 1060 | } |
| 1061 | |
| 1062 | geomHLSL += " outStream.Append(output);\n"; |
| 1063 | } |
| 1064 | |
| 1065 | geomHLSL += " \n" |
| 1066 | " outStream.RestartStrip();\n" |
| 1067 | "}\n"; |
| 1068 | |
| 1069 | return geomHLSL; |
| 1070 | } |
| 1071 | |
| 1072 | // This method needs to match OutputHLSL::decorate |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 1073 | std::string DynamicHLSL::decorateVariable(const std::string &name) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1074 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1075 | if (name.compare(0, 3, "gl_")) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1076 | { |
| 1077 | return "_" + name; |
| 1078 | } |
| 1079 | |
| 1080 | return name; |
| 1081 | } |
| 1082 | |
Jamie Madill | 834e8b7 | 2014-04-11 13:33:58 -0400 | [diff] [blame] | 1083 | std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const ShaderVariable &shaderAttrib) const |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1084 | { |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 1085 | std::string attribString = "input." + decorateVariable(shaderAttrib.name); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1086 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1087 | // Matrix |
| 1088 | if (IsMatrixType(shaderAttrib.type)) |
| 1089 | { |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1090 | return "transpose(" + attribString + ")"; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1091 | } |
| 1092 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1093 | GLenum shaderComponentType = UniformComponentType(shaderAttrib.type); |
| 1094 | int shaderComponentCount = UniformComponentCount(shaderAttrib.type); |
| 1095 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1096 | // Perform integer to float conversion (if necessary) |
| 1097 | bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT); |
| 1098 | |
Jamie Madill | 7a29e4a | 2014-05-02 10:41:48 -0400 | [diff] [blame] | 1099 | if (requiresTypeConversion) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1100 | { |
Jamie Madill | 7a29e4a | 2014-05-02 10:41:48 -0400 | [diff] [blame] | 1101 | // TODO: normalization for 32-bit integer formats |
| 1102 | ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger); |
| 1103 | return "float" + Str(shaderComponentCount) + "(" + attribString + ")"; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1104 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1105 | |
| 1106 | // No conversion necessary |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1107 | return attribString; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1108 | } |
| 1109 | |
Jamie Madill | 7a29e4a | 2014-05-02 10:41:48 -0400 | [diff] [blame] | 1110 | void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const |
| 1111 | { |
| 1112 | for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++) |
| 1113 | { |
| 1114 | const VertexFormat &vertexFormat = inputLayout[inputIndex]; |
| 1115 | |
| 1116 | if (vertexFormat.mType == GL_NONE) |
| 1117 | { |
| 1118 | signature[inputIndex] = GL_NONE; |
| 1119 | } |
| 1120 | else |
| 1121 | { |
| 1122 | bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0); |
| 1123 | signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE); |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1128 | } |