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