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