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