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