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