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