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 | { |
Geoff Lang | 3a61c32 | 2014-07-10 13:01:54 -0400 | [diff] [blame] | 213 | // TODO (geofflang): Use context's caps |
| 214 | const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 215 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 216 | vertexShader->resetVaryingsRegisterAssignment(); |
| 217 | fragmentShader->resetVaryingsRegisterAssignment(); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 218 | |
| 219 | std::set<std::string> packedVaryings; |
| 220 | |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 221 | std::vector<gl::PackedVarying> &fragmentVaryings = fragmentShader->getVaryings(); |
| 222 | std::vector<gl::PackedVarying> &vertexVaryings = vertexShader->getVaryings(); |
| 223 | for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 224 | { |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 225 | PackedVarying *varying = &fragmentVaryings[varyingIndex]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 226 | if (packVarying(varying, maxVaryingVectors, packing)) |
| 227 | { |
| 228 | packedVaryings.insert(varying->name); |
| 229 | } |
| 230 | else |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 231 | { |
| 232 | infoLog.append("Could not pack varying %s", varying->name.c_str()); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 233 | return -1; |
| 234 | } |
| 235 | } |
| 236 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 237 | for (unsigned int feedbackVaryingIndex = 0; feedbackVaryingIndex < transformFeedbackVaryings.size(); feedbackVaryingIndex++) |
| 238 | { |
| 239 | const std::string &transformFeedbackVarying = transformFeedbackVaryings[feedbackVaryingIndex]; |
| 240 | if (packedVaryings.find(transformFeedbackVarying) == packedVaryings.end()) |
| 241 | { |
| 242 | bool found = false; |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 243 | for (unsigned int varyingIndex = 0; varyingIndex < vertexVaryings.size(); varyingIndex++) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 244 | { |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 245 | PackedVarying *varying = &vertexVaryings[varyingIndex]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 246 | if (transformFeedbackVarying == varying->name) |
| 247 | { |
| 248 | if (!packVarying(varying, maxVaryingVectors, packing)) |
| 249 | { |
| 250 | infoLog.append("Could not pack varying %s", varying->name.c_str()); |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | found = true; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if (!found && transformFeedbackVarying != "gl_Position" && transformFeedbackVarying != "gl_PointSize") |
| 260 | { |
| 261 | infoLog.append("Transform feedback varying %s does not exist in the vertex shader.", transformFeedbackVarying.c_str()); |
| 262 | return -1; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 267 | // Return the number of used registers |
| 268 | int registers = 0; |
| 269 | |
| 270 | for (int r = 0; r < maxVaryingVectors; r++) |
| 271 | { |
| 272 | if (packing[r][0] || packing[r][1] || packing[r][2] || packing[r][3]) |
| 273 | { |
| 274 | registers++; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return registers; |
| 279 | } |
| 280 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 281 | std::string DynamicHLSL::generateVaryingHLSL(rx::VertexShaderD3D *shader) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 282 | { |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 283 | std::string varyingSemantic = getVaryingSemantic(shader->mUsesPointSize); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 284 | std::string varyingHLSL; |
| 285 | |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 286 | std::vector<gl::PackedVarying> &varyings = shader->getVaryings(); |
| 287 | for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 288 | { |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 289 | const PackedVarying &varying = varyings[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 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 610 | void DynamicHLSL::storeUserLinkedVaryings(const rx::VertexShaderD3D *vertexShader, |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 611 | std::vector<LinkedVarying> *linkedVaryings) const |
| 612 | { |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 613 | const std::string &varyingSemantic = getVaryingSemantic(vertexShader->mUsesPointSize); |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 614 | const std::vector<PackedVarying> &varyings = vertexShader->getVaryings(); |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 615 | |
| 616 | for (unsigned int varyingIndex = 0; varyingIndex < varyings.size(); varyingIndex++) |
| 617 | { |
| 618 | const PackedVarying &varying = varyings[varyingIndex]; |
| 619 | if (varying.registerAssigned()) |
| 620 | { |
| 621 | GLenum transposedType = TransposeMatrixType(varying.type); |
| 622 | int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType)); |
| 623 | |
| 624 | linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(), |
| 625 | varyingSemantic, varying.registerIndex, |
| 626 | variableRows * varying.elementCount())); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 631 | bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing, |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 632 | std::string& pixelHLSL, std::string& vertexHLSL, |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 633 | rx::FragmentShaderD3D *fragmentShader, rx::VertexShaderD3D *vertexShader, |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 634 | const std::vector<std::string>& transformFeedbackVaryings, |
| 635 | std::vector<LinkedVarying> *linkedVaryings, |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 636 | std::map<int, VariableLocation> *programOutputVars, |
| 637 | std::vector<PixelShaderOuputVariable> *outPixelShaderKey, |
| 638 | bool *outUsesFragDepth) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 639 | { |
| 640 | if (pixelHLSL.empty() || vertexHLSL.empty()) |
| 641 | { |
| 642 | return false; |
| 643 | } |
| 644 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 645 | bool usesMRT = fragmentShader->mUsesMultipleRenderTargets; |
| 646 | bool usesFragColor = fragmentShader->mUsesFragColor; |
| 647 | bool usesFragData = fragmentShader->mUsesFragData; |
| 648 | bool usesFragCoord = fragmentShader->mUsesFragCoord; |
| 649 | bool usesPointCoord = fragmentShader->mUsesPointCoord; |
| 650 | bool usesPointSize = vertexShader->mUsesPointSize; |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 651 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 652 | if (usesFragColor && usesFragData) |
| 653 | { |
| 654 | infoLog.append("Cannot use both gl_FragColor and gl_FragData in the same fragment shader."); |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | // Write the HLSL input/output declarations |
| 659 | const int shaderModel = mRenderer->getMajorShaderModel(); |
Geoff Lang | 3a61c32 | 2014-07-10 13:01:54 -0400 | [diff] [blame] | 660 | |
| 661 | // TODO (geofflang): Use context's caps |
| 662 | const int maxVaryingVectors = mRenderer->getRendererCaps().maxVaryingVectors; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 663 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 664 | const int registersNeeded = registers + (usesFragCoord ? 1 : 0) + (usesPointCoord ? 1 : 0); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 665 | |
| 666 | // Two cases when writing to gl_FragColor and using ESSL 1.0: |
| 667 | // - with a 3.0 context, the output color is copied to channel 0 |
| 668 | // - 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] | 669 | const bool broadcast = (fragmentShader->mUsesFragColor && mRenderer->getCurrentClientVersion() < 3); |
Geoff Lang | c0b9ef4 | 2014-07-02 10:02:37 -0400 | [diff] [blame] | 670 | const unsigned int numRenderTargets = (broadcast || usesMRT ? mRenderer->getRendererCaps().maxDrawBuffers : 1); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 671 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 672 | int shaderVersion = vertexShader->getShaderVersion(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 673 | |
| 674 | if (registersNeeded > maxVaryingVectors) |
| 675 | { |
| 676 | infoLog.append("No varying registers left to support gl_FragCoord/gl_PointCoord"); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 677 | return false; |
| 678 | } |
| 679 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 680 | const std::string &varyingHLSL = generateVaryingHLSL(vertexShader); |
| 681 | const SemanticInfo &vertexSemantics = getSemanticInfo(registers, usesFragCoord, |
| 682 | false, usesPointSize, false); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 683 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 684 | storeUserLinkedVaryings(vertexShader, linkedVaryings); |
| 685 | storeBuiltinLinkedVaryings(vertexSemantics, linkedVaryings); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 686 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 687 | // 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] | 688 | vertexHLSL += "\n" + VERTEX_ATTRIBUTE_STUB_STRING + "\n" |
| 689 | "struct VS_OUTPUT\n" + generateVaryingLinkHLSL(vertexSemantics, varyingHLSL) + "\n" |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 690 | "VS_OUTPUT main(VS_INPUT input)\n" |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 691 | "{\n" |
| 692 | " initAttributes(input);\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 693 | |
| 694 | if (shaderModel >= 4) |
| 695 | { |
| 696 | vertexHLSL += "\n" |
| 697 | " gl_main();\n" |
| 698 | "\n" |
| 699 | " VS_OUTPUT output;\n" |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 700 | " output.gl_Position = gl_Position;\n" |
Jamie Madill | 2bf8b37 | 2014-06-16 17:18:51 -0400 | [diff] [blame] | 701 | " output.dx_Position.x = gl_Position.x;\n" |
| 702 | " output.dx_Position.y = -gl_Position.y;\n" |
| 703 | " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
| 704 | " output.dx_Position.w = gl_Position.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 705 | } |
| 706 | else |
| 707 | { |
| 708 | vertexHLSL += "\n" |
| 709 | " gl_main();\n" |
| 710 | "\n" |
| 711 | " VS_OUTPUT output;\n" |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 712 | " output.gl_Position = gl_Position;\n" |
Jamie Madill | 2bf8b37 | 2014-06-16 17:18:51 -0400 | [diff] [blame] | 713 | " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + dx_ViewAdjust.x * gl_Position.w;\n" |
| 714 | " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n" |
| 715 | " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
| 716 | " output.dx_Position.w = gl_Position.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 717 | } |
| 718 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 719 | if (usesPointSize && shaderModel >= 3) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 720 | { |
| 721 | vertexHLSL += " output.gl_PointSize = gl_PointSize;\n"; |
| 722 | } |
| 723 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 724 | if (usesFragCoord) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 725 | { |
| 726 | vertexHLSL += " output.gl_FragCoord = gl_Position;\n"; |
| 727 | } |
| 728 | |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 729 | const std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings(); |
| 730 | for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 731 | { |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 732 | const PackedVarying &varying = vertexVaryings[vertVaryingIndex]; |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 733 | if (varying.registerAssigned()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 734 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 735 | for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++) |
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 | int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type))); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 738 | |
| 739 | for (int row = 0; row < variableRows; row++) |
| 740 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 741 | int r = varying.registerIndex + elementIndex * variableRows + row; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 742 | vertexHLSL += " output.v" + Str(r); |
| 743 | |
| 744 | bool sharedRegister = false; // Register used by multiple varyings |
| 745 | |
| 746 | for (int x = 0; x < 4; x++) |
| 747 | { |
| 748 | if (packing[r][x] && packing[r][x] != packing[r][0]) |
| 749 | { |
| 750 | sharedRegister = true; |
| 751 | break; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | if(sharedRegister) |
| 756 | { |
| 757 | vertexHLSL += "."; |
| 758 | |
| 759 | for (int x = 0; x < 4; x++) |
| 760 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 761 | if (packing[r][x] == &varying) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 762 | { |
| 763 | switch(x) |
| 764 | { |
| 765 | case 0: vertexHLSL += "x"; break; |
| 766 | case 1: vertexHLSL += "y"; break; |
| 767 | case 2: vertexHLSL += "z"; break; |
| 768 | case 3: vertexHLSL += "w"; break; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 774 | vertexHLSL += " = _" + varying.name; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 775 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 776 | if (varying.isArray()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 777 | { |
| 778 | vertexHLSL += ArrayString(elementIndex); |
| 779 | } |
| 780 | |
| 781 | if (variableRows > 1) |
| 782 | { |
| 783 | vertexHLSL += ArrayString(row); |
| 784 | } |
| 785 | |
| 786 | vertexHLSL += ";\n"; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | vertexHLSL += "\n" |
| 793 | " return output;\n" |
| 794 | "}\n"; |
| 795 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 796 | const SemanticInfo &pixelSemantics = getSemanticInfo(registers, usesFragCoord, usesPointCoord, |
| 797 | usesPointSize, true); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 798 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 799 | pixelHLSL += "struct PS_INPUT\n" + generateVaryingLinkHLSL(pixelSemantics, varyingHLSL) + "\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 800 | |
| 801 | if (shaderVersion < 300) |
| 802 | { |
| 803 | for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; renderTargetIndex++) |
| 804 | { |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 805 | PixelShaderOuputVariable outputKeyVariable; |
| 806 | outputKeyVariable.type = GL_FLOAT_VEC4; |
| 807 | outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex); |
| 808 | outputKeyVariable.source = broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]"; |
| 809 | outputKeyVariable.outputIndex = renderTargetIndex; |
| 810 | |
| 811 | outPixelShaderKey->push_back(outputKeyVariable); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 812 | } |
| 813 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 814 | *outUsesFragDepth = fragmentShader->mUsesFragDepth; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 815 | } |
| 816 | else |
| 817 | { |
| 818 | defineOutputVariables(fragmentShader, programOutputVars); |
| 819 | |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 820 | const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 821 | for (auto locationIt = programOutputVars->begin(); locationIt != programOutputVars->end(); locationIt++) |
| 822 | { |
| 823 | const VariableLocation &outputLocation = locationIt->second; |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 824 | const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index]; |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 825 | const std::string &variableName = "out_" + outputLocation.name; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 826 | const std::string &elementString = (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element)); |
| 827 | |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 828 | PixelShaderOuputVariable outputKeyVariable; |
| 829 | outputKeyVariable.type = outputVariable.type; |
| 830 | outputKeyVariable.name = variableName + elementString; |
| 831 | outputKeyVariable.source = variableName + ArrayString(outputLocation.element); |
| 832 | outputKeyVariable.outputIndex = locationIt->first; |
| 833 | |
| 834 | outPixelShaderKey->push_back(outputKeyVariable); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 835 | } |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 836 | |
| 837 | *outUsesFragDepth = false; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 838 | } |
| 839 | |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 840 | pixelHLSL += PIXEL_OUTPUT_STUB_STRING + "\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 841 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 842 | if (fragmentShader->mUsesFrontFacing) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 843 | { |
| 844 | if (shaderModel >= 4) |
| 845 | { |
| 846 | pixelHLSL += "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n" |
| 847 | "{\n"; |
| 848 | } |
| 849 | else |
| 850 | { |
| 851 | pixelHLSL += "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n" |
| 852 | "{\n"; |
| 853 | } |
| 854 | } |
| 855 | else |
| 856 | { |
| 857 | pixelHLSL += "PS_OUTPUT main(PS_INPUT input)\n" |
| 858 | "{\n"; |
| 859 | } |
| 860 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 861 | if (usesFragCoord) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 862 | { |
| 863 | pixelHLSL += " float rhw = 1.0 / input.gl_FragCoord.w;\n"; |
| 864 | |
| 865 | if (shaderModel >= 4) |
| 866 | { |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 867 | pixelHLSL += " gl_FragCoord.x = input.dx_Position.x;\n" |
| 868 | " gl_FragCoord.y = input.dx_Position.y;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 869 | } |
| 870 | else if (shaderModel >= 3) |
| 871 | { |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 872 | pixelHLSL += " gl_FragCoord.x = input.dx_Position.x + 0.5;\n" |
| 873 | " gl_FragCoord.y = input.dx_Position.y + 0.5;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 874 | } |
| 875 | else |
| 876 | { |
| 877 | // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See Renderer::setViewport() |
| 878 | pixelHLSL += " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + dx_ViewCoords.z;\n" |
| 879 | " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + dx_ViewCoords.w;\n"; |
| 880 | } |
| 881 | |
| 882 | pixelHLSL += " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + dx_DepthFront.y;\n" |
| 883 | " gl_FragCoord.w = rhw;\n"; |
| 884 | } |
| 885 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 886 | if (usesPointCoord && shaderModel >= 3) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 887 | { |
| 888 | pixelHLSL += " gl_PointCoord.x = input.gl_PointCoord.x;\n"; |
| 889 | pixelHLSL += " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n"; |
| 890 | } |
| 891 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 892 | if (fragmentShader->mUsesFrontFacing) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 893 | { |
| 894 | if (shaderModel <= 3) |
| 895 | { |
| 896 | pixelHLSL += " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n"; |
| 897 | } |
| 898 | else |
| 899 | { |
| 900 | pixelHLSL += " gl_FrontFacing = isFrontFace;\n"; |
| 901 | } |
| 902 | } |
| 903 | |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 904 | const std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings(); |
| 905 | for (unsigned int varyingIndex = 0; varyingIndex < fragmentVaryings.size(); varyingIndex++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 906 | { |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 907 | const PackedVarying &varying = fragmentVaryings[varyingIndex]; |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 908 | if (varying.registerAssigned()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 909 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 910 | for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 911 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 912 | GLenum transposedType = TransposeMatrixType(varying.type); |
| 913 | int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType)); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 914 | for (int row = 0; row < variableRows; row++) |
| 915 | { |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 916 | std::string n = Str(varying.registerIndex + elementIndex * variableRows + row); |
| 917 | pixelHLSL += " _" + varying.name; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 918 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 919 | if (varying.isArray()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 920 | { |
| 921 | pixelHLSL += ArrayString(elementIndex); |
| 922 | } |
| 923 | |
| 924 | if (variableRows > 1) |
| 925 | { |
| 926 | pixelHLSL += ArrayString(row); |
| 927 | } |
| 928 | |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 929 | if (varying.isStruct()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 930 | { |
| 931 | pixelHLSL += " = input.v" + n + ";\n"; break; |
| 932 | } |
| 933 | else |
| 934 | { |
| 935 | switch (VariableColumnCount(transposedType)) |
| 936 | { |
| 937 | case 1: pixelHLSL += " = input.v" + n + ".x;\n"; break; |
| 938 | case 2: pixelHLSL += " = input.v" + n + ".xy;\n"; break; |
| 939 | case 3: pixelHLSL += " = input.v" + n + ".xyz;\n"; break; |
| 940 | case 4: pixelHLSL += " = input.v" + n + ";\n"; break; |
| 941 | default: UNREACHABLE(); |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | else UNREACHABLE(); |
| 948 | } |
| 949 | |
| 950 | pixelHLSL += "\n" |
| 951 | " gl_main();\n" |
| 952 | "\n" |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 953 | " return generateOutput();\n" |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 954 | "}\n"; |
| 955 | |
| 956 | return true; |
| 957 | } |
| 958 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 959 | void DynamicHLSL::defineOutputVariables(rx::FragmentShaderD3D *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 960 | { |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame^] | 961 | const std::vector<sh::Attribute> &shaderOutputVars = fragmentShader->getActiveOutputVariables(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 962 | |
| 963 | for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size(); outputVariableIndex++) |
| 964 | { |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 965 | const sh::Attribute &outputVariable = shaderOutputVars[outputVariableIndex]; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 966 | const int baseLocation = outputVariable.location == -1 ? 0 : outputVariable.location; |
| 967 | |
| 968 | if (outputVariable.arraySize > 0) |
| 969 | { |
| 970 | for (unsigned int elementIndex = 0; elementIndex < outputVariable.arraySize; elementIndex++) |
| 971 | { |
| 972 | const int location = baseLocation + elementIndex; |
| 973 | ASSERT(programOutputVars->count(location) == 0); |
| 974 | (*programOutputVars)[location] = VariableLocation(outputVariable.name, elementIndex, outputVariableIndex); |
| 975 | } |
| 976 | } |
| 977 | else |
| 978 | { |
| 979 | ASSERT(programOutputVars->count(baseLocation) == 0); |
| 980 | (*programOutputVars)[baseLocation] = VariableLocation(outputVariable.name, GL_INVALID_INDEX, outputVariableIndex); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 985 | 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] | 986 | { |
| 987 | // for now we only handle point sprite emulation |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 988 | ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4); |
Jamie Madill | ff0d2ba | 2014-05-14 13:49:10 -0400 | [diff] [blame] | 989 | return generatePointSpriteHLSL(registers, fragmentShader, vertexShader); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 990 | } |
| 991 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 992 | 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] | 993 | { |
| 994 | ASSERT(registers >= 0); |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 995 | ASSERT(vertexShader->mUsesPointSize); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 996 | ASSERT(mRenderer->getMajorShaderModel() >= 4); |
| 997 | |
| 998 | std::string geomHLSL; |
| 999 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 1000 | const SemanticInfo &inSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord, |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 1001 | false, true, false); |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 1002 | const SemanticInfo &outSemantics = getSemanticInfo(registers, fragmentShader->mUsesFragCoord, |
| 1003 | fragmentShader->mUsesPointCoord, true, false); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1004 | |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 1005 | std::string varyingHLSL = generateVaryingHLSL(vertexShader); |
| 1006 | std::string inLinkHLSL = generateVaryingLinkHLSL(inSemantics, varyingHLSL); |
| 1007 | std::string outLinkHLSL = generateVaryingLinkHLSL(outSemantics, varyingHLSL); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1008 | |
Geoff Lang | c0b9ef4 | 2014-07-02 10:02:37 -0400 | [diff] [blame] | 1009 | // TODO(geofflang): use context's caps |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1010 | geomHLSL += "uniform float4 dx_ViewCoords : register(c1);\n" |
| 1011 | "\n" |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 1012 | "struct GS_INPUT\n" + inLinkHLSL + "\n" + |
| 1013 | "struct GS_OUTPUT\n" + outLinkHLSL + "\n" + |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1014 | "\n" |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1015 | "static float2 pointSpriteCorners[] = \n" |
| 1016 | "{\n" |
| 1017 | " float2( 0.5f, -0.5f),\n" |
| 1018 | " float2( 0.5f, 0.5f),\n" |
| 1019 | " float2(-0.5f, -0.5f),\n" |
| 1020 | " float2(-0.5f, 0.5f)\n" |
| 1021 | "};\n" |
| 1022 | "\n" |
| 1023 | "static float2 pointSpriteTexcoords[] = \n" |
| 1024 | "{\n" |
| 1025 | " float2(1.0f, 1.0f),\n" |
| 1026 | " float2(1.0f, 0.0f),\n" |
| 1027 | " float2(0.0f, 1.0f),\n" |
| 1028 | " float2(0.0f, 0.0f)\n" |
| 1029 | "};\n" |
| 1030 | "\n" |
Geoff Lang | c0b9ef4 | 2014-07-02 10:02:37 -0400 | [diff] [blame] | 1031 | "static float minPointSize = " + Str(mRenderer->getRendererCaps().minAliasedPointSize) + ".0f;\n" |
| 1032 | "static float maxPointSize = " + Str(mRenderer->getRendererCaps().maxAliasedPointSize) + ".0f;\n" |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1033 | "\n" |
| 1034 | "[maxvertexcount(4)]\n" |
| 1035 | "void main(point GS_INPUT input[1], inout TriangleStream<GS_OUTPUT> outStream)\n" |
| 1036 | "{\n" |
| 1037 | " GS_OUTPUT output = (GS_OUTPUT)0;\n" |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 1038 | " output.gl_Position = input[0].gl_Position;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1039 | " output.gl_PointSize = input[0].gl_PointSize;\n"; |
| 1040 | |
| 1041 | for (int r = 0; r < registers; r++) |
| 1042 | { |
| 1043 | geomHLSL += " output.v" + Str(r) + " = input[0].v" + Str(r) + ";\n"; |
| 1044 | } |
| 1045 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 1046 | if (fragmentShader->mUsesFragCoord) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1047 | { |
| 1048 | geomHLSL += " output.gl_FragCoord = input[0].gl_FragCoord;\n"; |
| 1049 | } |
| 1050 | |
| 1051 | geomHLSL += " \n" |
| 1052 | " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, maxPointSize);\n" |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 1053 | " float4 dx_Position = input[0].dx_Position;\n" |
| 1054 | " 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] | 1055 | |
| 1056 | for (int corner = 0; corner < 4; corner++) |
| 1057 | { |
| 1058 | geomHLSL += " \n" |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 1059 | " 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] | 1060 | |
Brandon Jones | 7162096 | 2014-08-20 14:04:59 -0700 | [diff] [blame] | 1061 | if (fragmentShader->mUsesPointCoord) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1062 | { |
| 1063 | geomHLSL += " output.gl_PointCoord = pointSpriteTexcoords[" + Str(corner) + "];\n"; |
| 1064 | } |
| 1065 | |
| 1066 | geomHLSL += " outStream.Append(output);\n"; |
| 1067 | } |
| 1068 | |
| 1069 | geomHLSL += " \n" |
| 1070 | " outStream.RestartStrip();\n" |
| 1071 | "}\n"; |
| 1072 | |
| 1073 | return geomHLSL; |
| 1074 | } |
| 1075 | |
| 1076 | // This method needs to match OutputHLSL::decorate |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 1077 | std::string DynamicHLSL::decorateVariable(const std::string &name) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1078 | { |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 1079 | if (name.compare(0, 3, "gl_") != 0) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1080 | { |
| 1081 | return "_" + name; |
| 1082 | } |
| 1083 | |
| 1084 | return name; |
| 1085 | } |
| 1086 | |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 1087 | std::string DynamicHLSL::generateAttributeConversionHLSL(const VertexFormat &vertexFormat, const sh::ShaderVariable &shaderAttrib) const |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1088 | { |
Jamie Madill | a53ab51 | 2014-03-17 09:47:44 -0400 | [diff] [blame] | 1089 | std::string attribString = "input." + decorateVariable(shaderAttrib.name); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1090 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1091 | // Matrix |
| 1092 | if (IsMatrixType(shaderAttrib.type)) |
| 1093 | { |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1094 | return "transpose(" + attribString + ")"; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1095 | } |
| 1096 | |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 1097 | GLenum shaderComponentType = VariableComponentType(shaderAttrib.type); |
| 1098 | int shaderComponentCount = VariableComponentCount(shaderAttrib.type); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1099 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1100 | // Perform integer to float conversion (if necessary) |
| 1101 | bool requiresTypeConversion = (shaderComponentType == GL_FLOAT && vertexFormat.mType != GL_FLOAT); |
| 1102 | |
Jamie Madill | 7a29e4a | 2014-05-02 10:41:48 -0400 | [diff] [blame] | 1103 | if (requiresTypeConversion) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1104 | { |
Jamie Madill | 7a29e4a | 2014-05-02 10:41:48 -0400 | [diff] [blame] | 1105 | // TODO: normalization for 32-bit integer formats |
| 1106 | ASSERT(!vertexFormat.mNormalized && !vertexFormat.mPureInteger); |
| 1107 | return "float" + Str(shaderComponentCount) + "(" + attribString + ")"; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1108 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1109 | |
| 1110 | // No conversion necessary |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1111 | return attribString; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1112 | } |
| 1113 | |
Jamie Madill | 7a29e4a | 2014-05-02 10:41:48 -0400 | [diff] [blame] | 1114 | void DynamicHLSL::getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const |
| 1115 | { |
| 1116 | for (size_t inputIndex = 0; inputIndex < MAX_VERTEX_ATTRIBS; inputIndex++) |
| 1117 | { |
| 1118 | const VertexFormat &vertexFormat = inputLayout[inputIndex]; |
| 1119 | |
| 1120 | if (vertexFormat.mType == GL_NONE) |
| 1121 | { |
| 1122 | signature[inputIndex] = GL_NONE; |
| 1123 | } |
| 1124 | else |
| 1125 | { |
| 1126 | bool gpuConverted = ((mRenderer->getVertexConversionType(vertexFormat) & rx::VERTEX_CONVERT_GPU) != 0); |
| 1127 | signature[inputIndex] = (gpuConverted ? GL_TRUE : GL_FALSE); |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1132 | } |