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