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" |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 13 | #include "libANGLE/Context.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 14 | #include "libANGLE/Program.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 15 | #include "libANGLE/Shader.h" |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 16 | #include "libANGLE/VaryingPacking.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 17 | #include "libANGLE/formatutils.h" |
Jamie Madill | 28afae5 | 2015-11-09 15:07:57 -0500 | [diff] [blame] | 18 | #include "libANGLE/renderer/d3d/ProgramD3D.h" |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 19 | #include "libANGLE/renderer/d3d/RendererD3D.h" |
| 20 | #include "libANGLE/renderer/d3d/ShaderD3D.h" |
Geoff Lang | 0b7eef7 | 2014-06-12 14:10:47 -0400 | [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 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 30 | // This class needs to match OutputHLSL::decorate |
| 31 | class DecorateVariable final : angle::NonCopyable |
| 32 | { |
| 33 | public: |
| 34 | explicit DecorateVariable(const std::string &str) : mName(str) {} |
| 35 | const std::string &getName() const { return mName; } |
| 36 | |
| 37 | private: |
| 38 | const std::string &mName; |
| 39 | }; |
| 40 | |
| 41 | std::ostream &operator<<(std::ostream &o, const DecorateVariable &dv) |
| 42 | { |
| 43 | if (dv.getName().compare(0, 3, "gl_") != 0) |
| 44 | { |
| 45 | o << "_"; |
| 46 | } |
| 47 | o << dv.getName(); |
| 48 | return o; |
| 49 | } |
| 50 | |
| 51 | const char *HLSLComponentTypeString(GLenum componentType) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 52 | { |
| 53 | switch (componentType) |
| 54 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 55 | case GL_UNSIGNED_INT: |
| 56 | return "uint"; |
| 57 | case GL_INT: |
| 58 | return "int"; |
| 59 | case GL_UNSIGNED_NORMALIZED: |
| 60 | case GL_SIGNED_NORMALIZED: |
| 61 | case GL_FLOAT: |
| 62 | return "float"; |
| 63 | default: |
| 64 | UNREACHABLE(); |
| 65 | return "not-component-type"; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 66 | } |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 69 | void HLSLComponentTypeString(std::ostringstream &ostream, GLenum componentType, int componentCount) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 70 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 71 | ostream << HLSLComponentTypeString(componentType); |
| 72 | if (componentCount > 1) |
| 73 | { |
| 74 | ostream << componentCount; |
| 75 | } |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 78 | const char *HLSLMatrixTypeString(GLenum type) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 79 | { |
| 80 | switch (type) |
| 81 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 82 | case GL_FLOAT_MAT2: |
| 83 | return "float2x2"; |
| 84 | case GL_FLOAT_MAT3: |
| 85 | return "float3x3"; |
| 86 | case GL_FLOAT_MAT4: |
| 87 | return "float4x4"; |
| 88 | case GL_FLOAT_MAT2x3: |
| 89 | return "float2x3"; |
| 90 | case GL_FLOAT_MAT3x2: |
| 91 | return "float3x2"; |
| 92 | case GL_FLOAT_MAT2x4: |
| 93 | return "float2x4"; |
| 94 | case GL_FLOAT_MAT4x2: |
| 95 | return "float4x2"; |
| 96 | case GL_FLOAT_MAT3x4: |
| 97 | return "float3x4"; |
| 98 | case GL_FLOAT_MAT4x3: |
| 99 | return "float4x3"; |
| 100 | default: |
| 101 | UNREACHABLE(); |
| 102 | return "not-matrix-type"; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 106 | void HLSLTypeString(std::ostringstream &ostream, GLenum type) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 107 | { |
| 108 | if (gl::IsMatrixType(type)) |
| 109 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 110 | ostream << HLSLMatrixTypeString(type); |
| 111 | return; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 114 | HLSLComponentTypeString(ostream, gl::VariableComponentType(type), |
| 115 | gl::VariableComponentCount(type)); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 118 | const PixelShaderOutputVariable *FindOutputAtLocation( |
| 119 | const std::vector<PixelShaderOutputVariable> &outputVariables, |
| 120 | unsigned int location) |
Jamie Madill | 3f2e61d | 2014-09-05 10:38:05 -0400 | [diff] [blame] | 121 | { |
| 122 | for (size_t variableIndex = 0; variableIndex < outputVariables.size(); ++variableIndex) |
| 123 | { |
| 124 | if (outputVariables[variableIndex].outputIndex == location) |
| 125 | { |
Gregoire Payen de La Garanderie | e172854 | 2015-01-07 11:31:54 +0000 | [diff] [blame] | 126 | return &outputVariables[variableIndex]; |
Jamie Madill | 3f2e61d | 2014-09-05 10:38:05 -0400 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 130 | return nullptr; |
Jamie Madill | 3f2e61d | 2014-09-05 10:38:05 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 133 | void WriteArrayString(std::ostringstream &strstr, unsigned int i) |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 134 | { |
| 135 | static_assert(GL_INVALID_INDEX == UINT_MAX, |
| 136 | "GL_INVALID_INDEX must be equal to the max unsigned int."); |
| 137 | if (i == UINT_MAX) |
| 138 | { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | strstr << "["; |
| 143 | strstr << i; |
| 144 | strstr << "]"; |
| 145 | } |
| 146 | |
Jamie Madill | 51c4768 | 2016-10-25 15:50:14 -0400 | [diff] [blame] | 147 | constexpr const char *VERTEX_ATTRIBUTE_STUB_STRING = "@@ VERTEX ATTRIBUTES @@"; |
| 148 | constexpr const char *PIXEL_OUTPUT_STUB_STRING = "@@ PIXEL OUTPUT @@"; |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 149 | } // anonymous namespace |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 150 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 151 | // DynamicHLSL implementation |
| 152 | |
Jamie Madill | 65345da | 2015-11-13 11:25:23 -0500 | [diff] [blame] | 153 | DynamicHLSL::DynamicHLSL(RendererD3D *const renderer) : mRenderer(renderer) |
Jamie Madill | bbdeeb1 | 2015-11-12 15:42:16 +0000 | [diff] [blame] | 154 | { |
Jamie Madill | bbdeeb1 | 2015-11-12 15:42:16 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 157 | std::string DynamicHLSL::generateVertexShaderForInputLayout( |
| 158 | const std::string &sourceShader, |
| 159 | const InputLayout &inputLayout, |
| 160 | const std::vector<sh::Attribute> &shaderAttributes) const |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 161 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 162 | std::ostringstream structStream; |
| 163 | std::ostringstream initStream; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 164 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 165 | structStream << "struct VS_INPUT\n" |
| 166 | << "{\n"; |
| 167 | |
| 168 | int semanticIndex = 0; |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 169 | unsigned int inputIndex = 0; |
| 170 | |
Cooper Partin | e6d14cc | 2015-02-20 12:32:58 -0800 | [diff] [blame] | 171 | // If gl_PointSize is used in the shader then pointsprites rendering is expected. |
| 172 | // If the renderer does not support Geometry shaders then Instanced PointSprite emulation |
| 173 | // must be used. |
| 174 | bool usesPointSize = sourceShader.find("GL_USES_POINT_SIZE") != std::string::npos; |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 175 | bool useInstancedPointSpriteEmulation = |
| 176 | usesPointSize && mRenderer->getWorkarounds().useInstancedPointSpriteEmulation; |
Cooper Partin | e6d14cc | 2015-02-20 12:32:58 -0800 | [diff] [blame] | 177 | |
| 178 | // Instanced PointSprite emulation requires additional entries in the |
| 179 | // VS_INPUT structure to support the vertices that make up the quad vertices. |
| 180 | // These values must be in sync with the cooresponding values added during inputlayout creation |
| 181 | // in InputLayoutCache::applyVertexBuffers(). |
| 182 | // |
| 183 | // The additional entries must appear first in the VS_INPUT layout because |
| 184 | // Windows Phone 8 era devices require per vertex data to physically come |
| 185 | // before per instance data in the shader. |
| 186 | if (useInstancedPointSpriteEmulation) |
| 187 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 188 | structStream << " float3 spriteVertexPos : SPRITEPOSITION0;\n" |
| 189 | << " float2 spriteTexCoord : SPRITETEXCOORD0;\n"; |
Cooper Partin | e6d14cc | 2015-02-20 12:32:58 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 192 | for (size_t attributeIndex = 0; attributeIndex < shaderAttributes.size(); ++attributeIndex) |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 193 | { |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 194 | const sh::Attribute &shaderAttribute = shaderAttributes[attributeIndex]; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 195 | if (!shaderAttribute.name.empty()) |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 196 | { |
Geoff Lang | 5ac5ae8 | 2014-09-09 10:14:17 -0400 | [diff] [blame] | 197 | ASSERT(inputIndex < MAX_VERTEX_ATTRIBS); |
Jamie Madill | f8dd7b1 | 2015-08-05 13:50:08 -0400 | [diff] [blame] | 198 | VertexFormatType vertexFormatType = |
| 199 | inputIndex < inputLayout.size() ? inputLayout[inputIndex] : VERTEX_FORMAT_INVALID; |
Geoff Lang | 5ac5ae8 | 2014-09-09 10:14:17 -0400 | [diff] [blame] | 200 | |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 201 | // HLSL code for input structure |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 202 | if (IsMatrixType(shaderAttribute.type)) |
| 203 | { |
| 204 | // Matrix types are always transposed |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 205 | structStream << " " |
| 206 | << HLSLMatrixTypeString(TransposeMatrixType(shaderAttribute.type)); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 207 | } |
| 208 | else |
| 209 | { |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 210 | GLenum componentType = mRenderer->getVertexComponentType(vertexFormatType); |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 211 | |
Corentin Wallez | b076add | 2016-01-11 16:45:46 -0500 | [diff] [blame] | 212 | if (shaderAttribute.name == "gl_InstanceID" || |
| 213 | shaderAttribute.name == "gl_VertexID") |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 214 | { |
Corentin Wallez | b076add | 2016-01-11 16:45:46 -0500 | [diff] [blame] | 215 | // The input types of the instance ID and vertex ID in HLSL (uint) differs from |
| 216 | // the ones in ESSL (int). |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 217 | structStream << " uint"; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 218 | } |
| 219 | else |
| 220 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 221 | structStream << " "; |
| 222 | HLSLComponentTypeString(structStream, componentType, |
| 223 | VariableComponentCount(shaderAttribute.type)); |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 224 | } |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 225 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 226 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 227 | structStream << " " << DecorateVariable(shaderAttribute.name) << " : "; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 228 | |
| 229 | if (shaderAttribute.name == "gl_InstanceID") |
| 230 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 231 | structStream << "SV_InstanceID"; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 232 | } |
Corentin Wallez | b076add | 2016-01-11 16:45:46 -0500 | [diff] [blame] | 233 | else if (shaderAttribute.name == "gl_VertexID") |
| 234 | { |
| 235 | structStream << "SV_VertexID"; |
| 236 | } |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 237 | else |
| 238 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 239 | structStream << "TEXCOORD" << semanticIndex; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 240 | semanticIndex += VariableRegisterCount(shaderAttribute.type); |
| 241 | } |
| 242 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 243 | structStream << ";\n"; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 244 | |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 245 | // HLSL code for initialization |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 246 | initStream << " " << DecorateVariable(shaderAttribute.name) << " = "; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 247 | |
| 248 | // Mismatched vertex attribute to vertex input may result in an undefined |
| 249 | // data reinterpretation (eg for pure integer->float, float->pure integer) |
| 250 | // TODO: issue warning with gl debug info extension, when supported |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 251 | if (IsMatrixType(shaderAttribute.type) || |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 252 | (mRenderer->getVertexConversionType(vertexFormatType) & VERTEX_CONVERT_GPU) != 0) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 253 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 254 | GenerateAttributeConversionHLSL(vertexFormatType, shaderAttribute, initStream); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 255 | } |
| 256 | else |
| 257 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 258 | initStream << "input." << DecorateVariable(shaderAttribute.name); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 259 | } |
| 260 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 261 | initStream << ";\n"; |
Jamie Madill | 3b7e205 | 2014-03-17 09:47:43 -0400 | [diff] [blame] | 262 | |
Jamie Madill | ac0a267 | 2014-04-11 13:33:56 -0400 | [diff] [blame] | 263 | inputIndex += VariableRowCount(TransposeMatrixType(shaderAttribute.type)); |
| 264 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 265 | } |
| 266 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 267 | structStream << "};\n" |
| 268 | "\n" |
| 269 | "void initAttributes(VS_INPUT input)\n" |
| 270 | "{\n" |
| 271 | << initStream.str() << "}\n"; |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 272 | |
| 273 | std::string vertexHLSL(sourceShader); |
| 274 | |
| 275 | size_t copyInsertionPos = vertexHLSL.find(VERTEX_ATTRIBUTE_STUB_STRING); |
Jamie Madill | 51c4768 | 2016-10-25 15:50:14 -0400 | [diff] [blame] | 276 | vertexHLSL.replace(copyInsertionPos, strlen(VERTEX_ATTRIBUTE_STUB_STRING), structStream.str()); |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 277 | |
| 278 | return vertexHLSL; |
| 279 | } |
| 280 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 281 | std::string DynamicHLSL::generatePixelShaderForOutputSignature( |
| 282 | const std::string &sourceShader, |
| 283 | const std::vector<PixelShaderOutputVariable> &outputVariables, |
| 284 | bool usesFragDepth, |
| 285 | const std::vector<GLenum> &outputLayout) const |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 286 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 287 | const int shaderModel = mRenderer->getMajorShaderModel(); |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 288 | std::string targetSemantic = (shaderModel >= 4) ? "SV_TARGET" : "COLOR"; |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 289 | std::string depthSemantic = (shaderModel >= 4) ? "SV_Depth" : "DEPTH"; |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 290 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 291 | std::ostringstream declarationStream; |
| 292 | std::ostringstream copyStream; |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 293 | |
| 294 | declarationStream << "struct PS_OUTPUT\n" |
| 295 | "{\n"; |
Geoff Lang | 4ace423 | 2014-06-18 19:12:48 -0400 | [diff] [blame] | 296 | |
Jamie Madill | 66a0819 | 2017-02-03 15:24:25 -0500 | [diff] [blame] | 297 | // Workaround for HLSL 3.x: We can't do a depth/stencil only render, the runtime will complain. |
| 298 | size_t numOutputs = outputLayout.empty() ? 1u : outputLayout.size(); |
| 299 | const PixelShaderOutputVariable defaultOutput(GL_FLOAT_VEC4, "dummy", "float4(0, 0, 0, 1)", 0); |
| 300 | |
| 301 | for (size_t layoutIndex = 0; layoutIndex < numOutputs; ++layoutIndex) |
Jamie Madill | 3f2e61d | 2014-09-05 10:38:05 -0400 | [diff] [blame] | 302 | { |
Jamie Madill | 66a0819 | 2017-02-03 15:24:25 -0500 | [diff] [blame] | 303 | GLenum binding = outputLayout.empty() ? GL_COLOR_ATTACHMENT0 : outputLayout[layoutIndex]; |
Jamie Madill | 3f2e61d | 2014-09-05 10:38:05 -0400 | [diff] [blame] | 304 | |
| 305 | if (binding != GL_NONE) |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 306 | { |
Jamie Madill | 3f2e61d | 2014-09-05 10:38:05 -0400 | [diff] [blame] | 307 | unsigned int location = (binding - GL_COLOR_ATTACHMENT0); |
| 308 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 309 | const PixelShaderOutputVariable *outputVariable = |
Jamie Madill | 66a0819 | 2017-02-03 15:24:25 -0500 | [diff] [blame] | 310 | outputLayout.empty() ? &defaultOutput |
| 311 | : FindOutputAtLocation(outputVariables, location); |
Jamie Madill | 3f2e61d | 2014-09-05 10:38:05 -0400 | [diff] [blame] | 312 | |
Gregoire Payen de La Garanderie | e172854 | 2015-01-07 11:31:54 +0000 | [diff] [blame] | 313 | // OpenGL ES 3.0 spec $4.2.1 |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 314 | // If [...] not all user-defined output variables are written, the values of fragment |
| 315 | // colors |
Gregoire Payen de La Garanderie | e172854 | 2015-01-07 11:31:54 +0000 | [diff] [blame] | 316 | // corresponding to unwritten variables are similarly undefined. |
| 317 | if (outputVariable) |
| 318 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 319 | declarationStream << " "; |
| 320 | HLSLTypeString(declarationStream, outputVariable->type); |
| 321 | declarationStream << " " << outputVariable->name << " : " << targetSemantic |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 322 | << static_cast<int>(layoutIndex) << ";\n"; |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 323 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 324 | copyStream << " output." << outputVariable->name << " = " |
| 325 | << outputVariable->source << ";\n"; |
Gregoire Payen de La Garanderie | e172854 | 2015-01-07 11:31:54 +0000 | [diff] [blame] | 326 | } |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | if (usesFragDepth) |
| 331 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 332 | declarationStream << " float gl_Depth : " << depthSemantic << ";\n"; |
| 333 | copyStream << " output.gl_Depth = gl_Depth; \n"; |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 336 | declarationStream << "};\n" |
| 337 | "\n" |
| 338 | "PS_OUTPUT generateOutput()\n" |
| 339 | "{\n" |
| 340 | " PS_OUTPUT output;\n" |
| 341 | << copyStream.str() << " return output;\n" |
| 342 | "}\n"; |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 343 | |
| 344 | std::string pixelHLSL(sourceShader); |
| 345 | |
| 346 | size_t outputInsertionPos = pixelHLSL.find(PIXEL_OUTPUT_STUB_STRING); |
Jamie Madill | 51c4768 | 2016-10-25 15:50:14 -0400 | [diff] [blame] | 347 | pixelHLSL.replace(outputInsertionPos, strlen(PIXEL_OUTPUT_STUB_STRING), |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 348 | declarationStream.str()); |
Geoff Lang | 04fb89a | 2014-06-09 15:05:36 -0400 | [diff] [blame] | 349 | |
| 350 | return pixelHLSL; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 353 | void DynamicHLSL::generateVaryingLinkHLSL(const VaryingPacking &varyingPacking, |
| 354 | const BuiltinInfo &builtins, |
| 355 | bool programUsesPointSize, |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 356 | std::ostringstream &hlslStream) const |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 357 | { |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 358 | ASSERT(builtins.dxPosition.enabled); |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 359 | hlslStream << "{\n" |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 360 | << " float4 dx_Position : " << builtins.dxPosition.str() << ";\n"; |
Austin Kinross | 8b695ee | 2015-03-12 13:12:20 -0700 | [diff] [blame] | 361 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 362 | if (builtins.glPosition.enabled) |
Austin Kinross | 8b695ee | 2015-03-12 13:12:20 -0700 | [diff] [blame] | 363 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 364 | hlslStream << " float4 gl_Position : " << builtins.glPosition.str() << ";\n"; |
Austin Kinross | 8b695ee | 2015-03-12 13:12:20 -0700 | [diff] [blame] | 365 | } |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 366 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 367 | if (builtins.glFragCoord.enabled) |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 368 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 369 | hlslStream << " float4 gl_FragCoord : " << builtins.glFragCoord.str() << ";\n"; |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 370 | } |
| 371 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 372 | if (builtins.glPointCoord.enabled) |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 373 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 374 | hlslStream << " float2 gl_PointCoord : " << builtins.glPointCoord.str() << ";\n"; |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 375 | } |
| 376 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 377 | if (builtins.glPointSize.enabled) |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 378 | { |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 379 | hlslStream << " float gl_PointSize : " << builtins.glPointSize.str() << ";\n"; |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 380 | } |
| 381 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 382 | std::string varyingSemantic = |
| 383 | GetVaryingSemantic(mRenderer->getMajorShaderModel(), programUsesPointSize); |
Austin Kinross | 8b695ee | 2015-03-12 13:12:20 -0700 | [diff] [blame] | 384 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 385 | for (const PackedVaryingRegister ®isterInfo : varyingPacking.getRegisterList()) |
| 386 | { |
| 387 | const auto &varying = *registerInfo.packedVarying->varying; |
| 388 | ASSERT(!varying.isStruct()); |
| 389 | |
| 390 | // TODO: Add checks to ensure D3D interpolation modifiers don't result in too many |
| 391 | // registers being used. |
| 392 | // For example, if there are N registers, and we have N vec3 varyings and 1 float |
| 393 | // varying, then D3D will pack them into N registers. |
| 394 | // If the float varying has the 'nointerpolation' modifier on it then we would need |
| 395 | // N + 1 registers, and D3D compilation will fail. |
| 396 | |
| 397 | switch (registerInfo.packedVarying->interpolation) |
| 398 | { |
| 399 | case sh::INTERPOLATION_SMOOTH: |
| 400 | hlslStream << " "; |
| 401 | break; |
| 402 | case sh::INTERPOLATION_FLAT: |
| 403 | hlslStream << " nointerpolation "; |
| 404 | break; |
| 405 | case sh::INTERPOLATION_CENTROID: |
| 406 | hlslStream << " centroid "; |
| 407 | break; |
| 408 | default: |
| 409 | UNREACHABLE(); |
| 410 | } |
| 411 | |
| 412 | GLenum transposedType = gl::TransposeMatrixType(varying.type); |
| 413 | GLenum componentType = gl::VariableComponentType(transposedType); |
| 414 | int columnCount = gl::VariableColumnCount(transposedType); |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 415 | HLSLComponentTypeString(hlslStream, componentType, columnCount); |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 416 | unsigned int semanticIndex = registerInfo.semanticIndex; |
| 417 | hlslStream << " v" << semanticIndex << " : " << varyingSemantic << semanticIndex << ";\n"; |
| 418 | } |
| 419 | |
| 420 | hlslStream << "};\n"; |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 423 | void DynamicHLSL::generateShaderLinkHLSL(const gl::Context *context, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 424 | const gl::ProgramState &programData, |
Jamie Madill | e39a3f0 | 2015-11-17 20:42:15 -0500 | [diff] [blame] | 425 | const ProgramD3DMetadata &programMetadata, |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 426 | const VaryingPacking &varyingPacking, |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 427 | const BuiltinVaryingsD3D &builtinsD3D, |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 428 | std::string *pixelHLSL, |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 429 | std::string *vertexHLSL) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 430 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 431 | ASSERT(pixelHLSL->empty() && vertexHLSL->empty()); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 432 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 433 | const auto &data = context->getContextState(); |
| 434 | gl::Shader *vertexShaderGL = programData.getAttachedVertexShader(); |
| 435 | gl::Shader *fragmentShaderGL = programData.getAttachedFragmentShader(); |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 436 | const ShaderD3D *fragmentShader = GetImplAs<ShaderD3D>(fragmentShaderGL); |
Cooper Partin | 7c89d24 | 2015-10-13 12:45:59 -0700 | [diff] [blame] | 437 | const int shaderModel = mRenderer->getMajorShaderModel(); |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 438 | |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 439 | // usesViewScale() isn't supported in the D3D9 renderer |
| 440 | ASSERT(shaderModel >= 4 || !programMetadata.usesViewScale()); |
| 441 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 442 | bool useInstancedPointSpriteEmulation = |
Jamie Madill | e39a3f0 | 2015-11-17 20:42:15 -0500 | [diff] [blame] | 443 | programMetadata.usesPointSize() && |
| 444 | mRenderer->getWorkarounds().useInstancedPointSpriteEmulation; |
Jamie Madill | febb7ad | 2014-06-18 13:50:51 -0400 | [diff] [blame] | 445 | |
Jamie Madill | 14e95b3 | 2015-05-07 10:10:41 -0400 | [diff] [blame] | 446 | // Validation done in the compiler |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 447 | ASSERT(!fragmentShader->usesFragColor() || !fragmentShader->usesFragData()); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 448 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 449 | std::ostringstream vertexStream; |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 450 | vertexStream << vertexShaderGL->getTranslatedSource(context); |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 451 | |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 452 | // Instanced PointSprite emulation requires additional entries originally generated in the |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 453 | // GeometryShader HLSL. These include pointsize clamp values. |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 454 | if (useInstancedPointSpriteEmulation) |
| 455 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 456 | vertexStream << "static float minPointSize = " |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 457 | << static_cast<int>(data.getCaps().minAliasedPointSize) << ".0f;\n" |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 458 | << "static float maxPointSize = " |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 459 | << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n"; |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 462 | // Add stub string to be replaced when shader is dynamically defined by its layout |
Jamie Madill | 51c4768 | 2016-10-25 15:50:14 -0400 | [diff] [blame] | 463 | vertexStream << "\n" << std::string(VERTEX_ATTRIBUTE_STUB_STRING) << "\n"; |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 464 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 465 | const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX]; |
| 466 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 467 | // Write the HLSL input/output declarations |
| 468 | vertexStream << "struct VS_OUTPUT\n"; |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 469 | generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(), |
| 470 | vertexStream); |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 471 | vertexStream << "\n" |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 472 | << "VS_OUTPUT main(VS_INPUT input)\n" |
| 473 | << "{\n" |
| 474 | << " initAttributes(input);\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 475 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 476 | vertexStream << "\n" |
| 477 | << " gl_main();\n" |
| 478 | << "\n" |
| 479 | << " VS_OUTPUT output;\n"; |
Austin Kinross | 8b695ee | 2015-03-12 13:12:20 -0700 | [diff] [blame] | 480 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 481 | if (vertexBuiltins.glPosition.enabled) |
Austin Kinross | 8b695ee | 2015-03-12 13:12:20 -0700 | [diff] [blame] | 482 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 483 | vertexStream << " output.gl_Position = gl_Position;\n"; |
Austin Kinross | 8b695ee | 2015-03-12 13:12:20 -0700 | [diff] [blame] | 484 | } |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 485 | |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 486 | // On D3D9 or D3D11 Feature Level 9, we need to emulate large viewports using dx_ViewAdjust. |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 487 | if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "") |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 488 | { |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 489 | vertexStream << " output.dx_Position.x = gl_Position.x;\n"; |
| 490 | |
| 491 | if (programMetadata.usesViewScale()) |
| 492 | { |
| 493 | // This code assumes that dx_ViewScale.y = -1.0f when rendering to texture, and +1.0f |
| 494 | // when rendering to the default framebuffer. No other values are valid. |
| 495 | vertexStream << " output.dx_Position.y = dx_ViewScale.y * gl_Position.y;\n"; |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | vertexStream << " output.dx_Position.y = - gl_Position.y;\n"; |
| 500 | } |
| 501 | |
| 502 | vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 503 | << " output.dx_Position.w = gl_Position.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 504 | } |
| 505 | else |
| 506 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 507 | vertexStream << " output.dx_Position.x = gl_Position.x * dx_ViewAdjust.z + " |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 508 | "dx_ViewAdjust.x * gl_Position.w;\n"; |
| 509 | |
| 510 | // If usesViewScale() is true and we're using the D3D11 renderer via Feature Level 9_*, |
| 511 | // then we need to multiply the gl_Position.y by the viewScale. |
| 512 | // usesViewScale() isn't supported when using the D3D9 renderer. |
| 513 | if (programMetadata.usesViewScale() && |
| 514 | (shaderModel >= 4 && mRenderer->getShaderModelSuffix() != "")) |
| 515 | { |
| 516 | vertexStream << " output.dx_Position.y = dx_ViewScale.y * (gl_Position.y * " |
| 517 | "dx_ViewAdjust.w + dx_ViewAdjust.y * gl_Position.w);\n"; |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | vertexStream << " output.dx_Position.y = -(gl_Position.y * dx_ViewAdjust.w + " |
| 522 | "dx_ViewAdjust.y * gl_Position.w);\n"; |
| 523 | } |
| 524 | |
| 525 | vertexStream << " output.dx_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n" |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 526 | << " output.dx_Position.w = gl_Position.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 527 | } |
| 528 | |
Austin Kinross | 8b695ee | 2015-03-12 13:12:20 -0700 | [diff] [blame] | 529 | // We don't need to output gl_PointSize if we use are emulating point sprites via instancing. |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 530 | if (vertexBuiltins.glPointSize.enabled) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 531 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 532 | vertexStream << " output.gl_PointSize = gl_PointSize;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 533 | } |
| 534 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 535 | if (vertexBuiltins.glFragCoord.enabled) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 536 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 537 | vertexStream << " output.gl_FragCoord = gl_Position;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 538 | } |
| 539 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 540 | for (const PackedVaryingRegister ®isterInfo : varyingPacking.getRegisterList()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 541 | { |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 542 | const auto &packedVarying = *registerInfo.packedVarying; |
| 543 | const auto &varying = *packedVarying.varying; |
| 544 | ASSERT(!varying.isStruct()); |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 545 | |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 546 | vertexStream << " output.v" << registerInfo.semanticIndex << " = "; |
| 547 | |
| 548 | if (packedVarying.isStructField()) |
| 549 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 550 | vertexStream << DecorateVariable(packedVarying.parentStructName) << "."; |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 551 | } |
| 552 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 553 | vertexStream << DecorateVariable(varying.name); |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 554 | |
| 555 | if (varying.isArray()) |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 556 | { |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 557 | WriteArrayString(vertexStream, registerInfo.varyingArrayIndex); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 558 | } |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 559 | |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 560 | if (VariableRowCount(varying.type) > 1) |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 561 | { |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 562 | WriteArrayString(vertexStream, registerInfo.varyingRowIndex); |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | vertexStream << ";\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 566 | } |
| 567 | |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 568 | // Instanced PointSprite emulation requires additional entries to calculate |
| 569 | // the final output vertex positions of the quad that represents each sprite. |
| 570 | if (useInstancedPointSpriteEmulation) |
| 571 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 572 | vertexStream << "\n" |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 573 | << " gl_PointSize = clamp(gl_PointSize, minPointSize, maxPointSize);\n"; |
| 574 | |
| 575 | vertexStream << " output.dx_Position.x += (input.spriteVertexPos.x * gl_PointSize / " |
| 576 | "(dx_ViewCoords.x*2)) * output.dx_Position.w;"; |
| 577 | |
| 578 | if (programMetadata.usesViewScale()) |
| 579 | { |
| 580 | // Multiply by ViewScale to invert the rendering when appropriate |
| 581 | vertexStream << " output.dx_Position.y += (-dx_ViewScale.y * " |
| 582 | "input.spriteVertexPos.y * gl_PointSize / (dx_ViewCoords.y*2)) * " |
| 583 | "output.dx_Position.w;"; |
| 584 | } |
| 585 | else |
| 586 | { |
| 587 | vertexStream << " output.dx_Position.y += (input.spriteVertexPos.y * gl_PointSize / " |
| 588 | "(dx_ViewCoords.y*2)) * output.dx_Position.w;"; |
| 589 | } |
| 590 | |
| 591 | vertexStream |
| 592 | << " output.dx_Position.z += input.spriteVertexPos.z * output.dx_Position.w;\n"; |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 593 | |
Jamie Madill | e39a3f0 | 2015-11-17 20:42:15 -0500 | [diff] [blame] | 594 | if (programMetadata.usesPointCoord()) |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 595 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 596 | vertexStream << "\n" |
| 597 | << " output.gl_PointCoord = input.spriteTexCoord;\n"; |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Cooper Partin | 7c89d24 | 2015-10-13 12:45:59 -0700 | [diff] [blame] | 601 | // Renderers that enable instanced pointsprite emulation require the vertex shader output member |
| 602 | // gl_PointCoord to be set to a default value if used without gl_PointSize. 0.5,0.5 is the same |
| 603 | // default value used in the generated pixel shader. |
Jamie Madill | e39a3f0 | 2015-11-17 20:42:15 -0500 | [diff] [blame] | 604 | if (programMetadata.usesInsertedPointCoordValue()) |
Cooper Partin | 7c89d24 | 2015-10-13 12:45:59 -0700 | [diff] [blame] | 605 | { |
| 606 | ASSERT(!useInstancedPointSpriteEmulation); |
| 607 | vertexStream << "\n" |
| 608 | << " output.gl_PointCoord = float2(0.5, 0.5);\n"; |
| 609 | } |
| 610 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 611 | vertexStream << "\n" |
| 612 | << " return output;\n" |
| 613 | << "}\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 614 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 615 | const auto &pixelBuiltins = builtinsD3D[SHADER_PIXEL]; |
| 616 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 617 | std::ostringstream pixelStream; |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 618 | pixelStream << fragmentShaderGL->getTranslatedSource(context); |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 619 | pixelStream << "struct PS_INPUT\n"; |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 620 | generateVaryingLinkHLSL(varyingPacking, pixelBuiltins, builtinsD3D.usesPointSize(), |
| 621 | pixelStream); |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 622 | pixelStream << "\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 623 | |
Jamie Madill | 51c4768 | 2016-10-25 15:50:14 -0400 | [diff] [blame] | 624 | pixelStream << std::string(PIXEL_OUTPUT_STUB_STRING) << "\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 625 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 626 | if (fragmentShader->usesFrontFacing()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 627 | { |
| 628 | if (shaderModel >= 4) |
| 629 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 630 | pixelStream << "PS_OUTPUT main(PS_INPUT input, bool isFrontFace : SV_IsFrontFace)\n" |
| 631 | << "{\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 632 | } |
| 633 | else |
| 634 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 635 | pixelStream << "PS_OUTPUT main(PS_INPUT input, float vFace : VFACE)\n" |
| 636 | << "{\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | else |
| 640 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 641 | pixelStream << "PS_OUTPUT main(PS_INPUT input)\n" |
| 642 | << "{\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 643 | } |
| 644 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 645 | if (pixelBuiltins.glFragCoord.enabled) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 646 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 647 | pixelStream << " float rhw = 1.0 / input.gl_FragCoord.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 648 | |
Austin Kinross | 588434c | 2014-12-10 10:41:45 -0800 | [diff] [blame] | 649 | // Certain Shader Models (4_0+ and 3_0) allow reading from dx_Position in the pixel shader. |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 650 | // Other Shader Models (4_0_level_9_3 and 2_x) don't support this, so we emulate it using |
| 651 | // dx_ViewCoords. |
Austin Kinross | 588434c | 2014-12-10 10:41:45 -0800 | [diff] [blame] | 652 | if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "") |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 653 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 654 | pixelStream << " gl_FragCoord.x = input.dx_Position.x;\n" |
| 655 | << " gl_FragCoord.y = input.dx_Position.y;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 656 | } |
Austin Kinross | 588434c | 2014-12-10 10:41:45 -0800 | [diff] [blame] | 657 | else if (shaderModel == 3) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 658 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 659 | pixelStream << " gl_FragCoord.x = input.dx_Position.x + 0.5;\n" |
| 660 | << " gl_FragCoord.y = input.dx_Position.y + 0.5;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 661 | } |
| 662 | else |
| 663 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 664 | // dx_ViewCoords contains the viewport width/2, height/2, center.x and center.y. See |
| 665 | // Renderer::setViewport() |
| 666 | pixelStream << " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * dx_ViewCoords.x + " |
| 667 | "dx_ViewCoords.z;\n" |
| 668 | << " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * dx_ViewCoords.y + " |
| 669 | "dx_ViewCoords.w;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 670 | } |
| 671 | |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 672 | if (programMetadata.usesViewScale()) |
| 673 | { |
| 674 | // For Feature Level 9_3 and below, we need to correct gl_FragCoord.y to account |
| 675 | // for dx_ViewScale. On Feature Level 10_0+, gl_FragCoord.y is calculated above using |
| 676 | // dx_ViewCoords and is always correct irrespective of dx_ViewScale's value. |
| 677 | // NOTE: usesViewScale() can only be true on D3D11 (i.e. Shader Model 4.0+). |
| 678 | if (shaderModel >= 4 && mRenderer->getShaderModelSuffix() == "") |
| 679 | { |
| 680 | // Some assumptions: |
| 681 | // - dx_ViewScale.y = -1.0f when rendering to texture |
| 682 | // - dx_ViewScale.y = +1.0f when rendering to the default framebuffer |
| 683 | // - gl_FragCoord.y has been set correctly above. |
| 684 | // |
| 685 | // When rendering to the backbuffer, the code inverts gl_FragCoord's y coordinate. |
| 686 | // This involves subtracting the y coordinate from the height of the area being |
| 687 | // rendered to. |
| 688 | // |
| 689 | // First we calculate the height of the area being rendered to: |
| 690 | // render_area_height = (2.0f / (1.0f - input.gl_FragCoord.y * rhw)) * |
| 691 | // gl_FragCoord.y |
| 692 | // |
| 693 | // Note that when we're rendering to default FB, we want our output to be |
| 694 | // equivalent to: |
| 695 | // "gl_FragCoord.y = render_area_height - gl_FragCoord.y" |
| 696 | // |
| 697 | // When we're rendering to a texture, we want our output to be equivalent to: |
| 698 | // "gl_FragCoord.y = gl_FragCoord.y;" |
| 699 | // |
| 700 | // If we set scale_factor = ((1.0f + dx_ViewScale.y) / 2.0f), then notice that |
| 701 | // - When rendering to default FB: scale_factor = 1.0f |
| 702 | // - When rendering to texture: scale_factor = 0.0f |
| 703 | // |
| 704 | // Therefore, we can get our desired output by setting: |
| 705 | // "gl_FragCoord.y = scale_factor * render_area_height - dx_ViewScale.y * |
| 706 | // gl_FragCoord.y" |
| 707 | // |
| 708 | // Simplifying, this becomes: |
| 709 | pixelStream |
| 710 | << " gl_FragCoord.y = (1.0f + dx_ViewScale.y) * gl_FragCoord.y /" |
| 711 | "(1.0f - input.gl_FragCoord.y * rhw) - dx_ViewScale.y * gl_FragCoord.y;\n"; |
| 712 | } |
| 713 | } |
| 714 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 715 | pixelStream << " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * dx_DepthFront.x + " |
| 716 | "dx_DepthFront.y;\n" |
| 717 | << " gl_FragCoord.w = rhw;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 718 | } |
| 719 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 720 | if (pixelBuiltins.glPointCoord.enabled && shaderModel >= 3) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 721 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 722 | pixelStream << " gl_PointCoord.x = input.gl_PointCoord.x;\n" |
| 723 | << " gl_PointCoord.y = 1.0 - input.gl_PointCoord.y;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 724 | } |
| 725 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 726 | if (fragmentShader->usesFrontFacing()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 727 | { |
| 728 | if (shaderModel <= 3) |
| 729 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 730 | pixelStream << " gl_FrontFacing = (vFace * dx_DepthFront.z >= 0.0);\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 731 | } |
| 732 | else |
| 733 | { |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 734 | pixelStream << " gl_FrontFacing = isFrontFace;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 738 | for (const PackedVaryingRegister ®isterInfo : varyingPacking.getRegisterList()) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 739 | { |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 740 | const auto &packedVarying = *registerInfo.packedVarying; |
| 741 | const auto &varying = *packedVarying.varying; |
| 742 | ASSERT(!varying.isBuiltIn() && !varying.isStruct()); |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 743 | |
Olli Etuaho | 06a06f5 | 2017-07-12 12:22:15 +0300 | [diff] [blame^] | 744 | // Don't reference VS-only transform feedback varyings in the PS. Note that we're relying on |
| 745 | // that the staticUse flag is set according to usage in the fragment shader. |
| 746 | if (packedVarying.vertexOnly || !varying.staticUse) |
Jamie Madill | ca03b35 | 2015-09-02 12:38:13 -0400 | [diff] [blame] | 747 | continue; |
| 748 | |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 749 | pixelStream << " "; |
| 750 | |
| 751 | if (packedVarying.isStructField()) |
| 752 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 753 | pixelStream << DecorateVariable(packedVarying.parentStructName) << "."; |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 754 | } |
| 755 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 756 | pixelStream << DecorateVariable(varying.name); |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 757 | |
| 758 | if (varying.isArray()) |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 759 | { |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 760 | WriteArrayString(pixelStream, registerInfo.varyingArrayIndex); |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 761 | } |
| 762 | |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 763 | GLenum transposedType = TransposeMatrixType(varying.type); |
| 764 | if (VariableRowCount(transposedType) > 1) |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 765 | { |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 766 | WriteArrayString(pixelStream, registerInfo.varyingRowIndex); |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 767 | } |
| 768 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 769 | pixelStream << " = input.v" << registerInfo.semanticIndex; |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 770 | |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 771 | switch (VariableColumnCount(transposedType)) |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 772 | { |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 773 | case 1: |
| 774 | pixelStream << ".x"; |
| 775 | break; |
| 776 | case 2: |
| 777 | pixelStream << ".xy"; |
| 778 | break; |
| 779 | case 3: |
| 780 | pixelStream << ".xyz"; |
| 781 | break; |
| 782 | case 4: |
| 783 | break; |
| 784 | default: |
| 785 | UNREACHABLE(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 786 | } |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 787 | pixelStream << ";\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 788 | } |
| 789 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 790 | pixelStream << "\n" |
| 791 | << " gl_main();\n" |
| 792 | << "\n" |
| 793 | << " return generateOutput();\n" |
| 794 | << "}\n"; |
| 795 | |
| 796 | *vertexHLSL = vertexStream.str(); |
| 797 | *pixelHLSL = pixelStream.str(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 798 | } |
| 799 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 800 | std::string DynamicHLSL::generateComputeShaderLinkHLSL(const gl::Context *context, |
| 801 | const gl::ProgramState &programData) const |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 802 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 803 | gl::Shader *computeShaderGL = programData.getAttachedComputeShader(); |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 804 | std::stringstream computeStream; |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 805 | std::string translatedSource = computeShaderGL->getTranslatedSource(context); |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 806 | computeStream << translatedSource; |
| 807 | |
| 808 | bool usesWorkGroupID = translatedSource.find("GL_USES_WORK_GROUP_ID") != std::string::npos; |
| 809 | bool usesLocalInvocationID = |
| 810 | translatedSource.find("GL_USES_LOCAL_INVOCATION_ID") != std::string::npos; |
| 811 | bool usesGlobalInvocationID = |
| 812 | translatedSource.find("GL_USES_GLOBAL_INVOCATION_ID") != std::string::npos; |
| 813 | bool usesLocalInvocationIndex = |
| 814 | translatedSource.find("GL_USES_LOCAL_INVOCATION_INDEX") != std::string::npos; |
| 815 | |
| 816 | computeStream << "\nstruct CS_INPUT\n{\n"; |
| 817 | if (usesWorkGroupID) |
| 818 | { |
| 819 | computeStream << " uint3 dx_WorkGroupID : " |
| 820 | << "SV_GroupID;\n"; |
| 821 | } |
| 822 | |
| 823 | if (usesLocalInvocationID) |
| 824 | { |
| 825 | computeStream << " uint3 dx_LocalInvocationID : " |
| 826 | << "SV_GroupThreadID;\n"; |
| 827 | } |
| 828 | |
| 829 | if (usesGlobalInvocationID) |
| 830 | { |
| 831 | computeStream << " uint3 dx_GlobalInvocationID : " |
| 832 | << "SV_DispatchThreadID;\n"; |
| 833 | } |
| 834 | |
| 835 | if (usesLocalInvocationIndex) |
| 836 | { |
| 837 | computeStream << " uint dx_LocalInvocationIndex : " |
| 838 | << "SV_GroupIndex;\n"; |
| 839 | } |
| 840 | |
| 841 | computeStream << "};\n\n"; |
| 842 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 843 | const sh::WorkGroupSize &localSize = computeShaderGL->getWorkGroupSize(context); |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 844 | computeStream << "[numthreads(" << localSize[0] << ", " << localSize[1] << ", " << localSize[2] |
| 845 | << ")]\n"; |
| 846 | |
| 847 | computeStream << "void main(CS_INPUT input)\n" |
| 848 | << "{\n"; |
| 849 | |
| 850 | if (usesWorkGroupID) |
| 851 | { |
| 852 | computeStream << " gl_WorkGroupID = input.dx_WorkGroupID;\n"; |
| 853 | } |
| 854 | if (usesLocalInvocationID) |
| 855 | { |
| 856 | computeStream << " gl_LocalInvocationID = input.dx_LocalInvocationID;\n"; |
| 857 | } |
| 858 | if (usesGlobalInvocationID) |
| 859 | { |
| 860 | computeStream << " gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n"; |
| 861 | } |
| 862 | if (usesLocalInvocationIndex) |
| 863 | { |
| 864 | computeStream << " gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n"; |
| 865 | } |
| 866 | |
| 867 | computeStream << "\n" |
| 868 | << " gl_main();\n" |
| 869 | << "}\n"; |
| 870 | |
| 871 | return computeStream.str(); |
| 872 | } |
| 873 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 874 | std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking, |
| 875 | const BuiltinVaryingsD3D &builtinsD3D) const |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 876 | { |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 877 | ASSERT(mRenderer->getMajorShaderModel() >= 4); |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 878 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 879 | std::ostringstream preambleStream; |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 880 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 881 | const auto &vertexBuiltins = builtinsD3D[SHADER_VERTEX]; |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 882 | |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 883 | preambleStream << "struct GS_INPUT\n"; |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 884 | generateVaryingLinkHLSL(varyingPacking, vertexBuiltins, builtinsD3D.usesPointSize(), |
| 885 | preambleStream); |
Jamie Madill | f4b2c4f | 2015-10-29 14:38:54 -0400 | [diff] [blame] | 886 | preambleStream << "\n" |
| 887 | << "struct GS_OUTPUT\n"; |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 888 | generateVaryingLinkHLSL(varyingPacking, builtinsD3D[SHADER_GEOMETRY], |
| 889 | builtinsD3D.usesPointSize(), preambleStream); |
Jamie Madill | 3e14e2b | 2015-10-29 14:38:53 -0400 | [diff] [blame] | 890 | preambleStream |
| 891 | << "\n" |
| 892 | << "void copyVertex(inout GS_OUTPUT output, GS_INPUT input, GS_INPUT flatinput)\n" |
| 893 | << "{\n" |
| 894 | << " output.gl_Position = input.gl_Position;\n"; |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 895 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 896 | if (vertexBuiltins.glPointSize.enabled) |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 897 | { |
| 898 | preambleStream << " output.gl_PointSize = input.gl_PointSize;\n"; |
| 899 | } |
| 900 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 901 | for (const PackedVaryingRegister &varyingRegister : varyingPacking.getRegisterList()) |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 902 | { |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 903 | preambleStream << " output.v" << varyingRegister.semanticIndex << " = "; |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 904 | if (varyingRegister.packedVarying->interpolation == sh::INTERPOLATION_FLAT) |
Jamie Madill | 3e14e2b | 2015-10-29 14:38:53 -0400 | [diff] [blame] | 905 | { |
| 906 | preambleStream << "flat"; |
| 907 | } |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 908 | preambleStream << "input.v" << varyingRegister.semanticIndex << "; \n"; |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 909 | } |
| 910 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 911 | if (vertexBuiltins.glFragCoord.enabled) |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 912 | { |
| 913 | preambleStream << " output.gl_FragCoord = input.gl_FragCoord;\n"; |
| 914 | } |
| 915 | |
| 916 | // Only write the dx_Position if we aren't using point sprites |
| 917 | preambleStream << "#ifndef ANGLE_POINT_SPRITE_SHADER\n" |
| 918 | << " output.dx_Position = input.dx_Position;\n" |
| 919 | << "#endif // ANGLE_POINT_SPRITE_SHADER\n" |
| 920 | << "}\n"; |
| 921 | |
| 922 | return preambleStream.str(); |
| 923 | } |
| 924 | |
| 925 | std::string DynamicHLSL::generateGeometryShaderHLSL(gl::PrimitiveType primitiveType, |
Jamie Madill | 9082b98 | 2016-04-27 15:21:51 -0400 | [diff] [blame] | 926 | const gl::ContextState &data, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 927 | const gl::ProgramState &programData, |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 928 | const bool useViewScale, |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 929 | const std::string &preambleString) const |
| 930 | { |
| 931 | ASSERT(mRenderer->getMajorShaderModel() >= 4); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 932 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 933 | std::stringstream shaderStream; |
| 934 | |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 935 | const bool pointSprites = (primitiveType == PRIMITIVE_POINTS); |
| 936 | const bool usesPointCoord = preambleString.find("gl_PointCoord") != std::string::npos; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 937 | |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 938 | const char *inputPT = nullptr; |
| 939 | const char *outputPT = nullptr; |
| 940 | int inputSize = 0; |
| 941 | int maxVertexOutput = 0; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 942 | |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 943 | switch (primitiveType) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 944 | { |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 945 | case PRIMITIVE_POINTS: |
| 946 | inputPT = "point"; |
| 947 | outputPT = "Triangle"; |
| 948 | inputSize = 1; |
| 949 | maxVertexOutput = 4; |
| 950 | break; |
| 951 | |
| 952 | case PRIMITIVE_LINES: |
| 953 | case PRIMITIVE_LINE_STRIP: |
| 954 | case PRIMITIVE_LINE_LOOP: |
| 955 | inputPT = "line"; |
| 956 | outputPT = "Line"; |
| 957 | inputSize = 2; |
| 958 | maxVertexOutput = 2; |
| 959 | break; |
| 960 | |
| 961 | case PRIMITIVE_TRIANGLES: |
| 962 | case PRIMITIVE_TRIANGLE_STRIP: |
| 963 | case PRIMITIVE_TRIANGLE_FAN: |
| 964 | inputPT = "triangle"; |
| 965 | outputPT = "Triangle"; |
| 966 | inputSize = 3; |
| 967 | maxVertexOutput = 3; |
| 968 | break; |
| 969 | |
| 970 | default: |
| 971 | UNREACHABLE(); |
| 972 | break; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 973 | } |
| 974 | |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 975 | if (pointSprites) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 976 | { |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 977 | shaderStream << "#define ANGLE_POINT_SPRITE_SHADER\n" |
| 978 | "\n" |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 979 | "uniform float4 dx_ViewCoords : register(c1);\n"; |
| 980 | |
| 981 | if (useViewScale) |
| 982 | { |
| 983 | shaderStream << "uniform float2 dx_ViewScale : register(c3);\n"; |
| 984 | } |
| 985 | |
| 986 | shaderStream << "\n" |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 987 | "static float2 pointSpriteCorners[] = \n" |
| 988 | "{\n" |
| 989 | " float2( 0.5f, -0.5f),\n" |
| 990 | " float2( 0.5f, 0.5f),\n" |
| 991 | " float2(-0.5f, -0.5f),\n" |
| 992 | " float2(-0.5f, 0.5f)\n" |
| 993 | "};\n" |
| 994 | "\n" |
| 995 | "static float2 pointSpriteTexcoords[] = \n" |
| 996 | "{\n" |
| 997 | " float2(1.0f, 1.0f),\n" |
| 998 | " float2(1.0f, 0.0f),\n" |
| 999 | " float2(0.0f, 1.0f),\n" |
| 1000 | " float2(0.0f, 0.0f)\n" |
| 1001 | "};\n" |
| 1002 | "\n" |
| 1003 | "static float minPointSize = " |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 1004 | << static_cast<int>(data.getCaps().minAliasedPointSize) |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 1005 | << ".0f;\n" |
| 1006 | "static float maxPointSize = " |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 1007 | << static_cast<int>(data.getCaps().maxAliasedPointSize) << ".0f;\n" |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 1008 | << "\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1009 | } |
| 1010 | |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 1011 | shaderStream << preambleString << "\n" |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 1012 | << "[maxvertexcount(" << maxVertexOutput << ")]\n" |
Jamie Madill | c7a92fd | 2015-10-29 10:08:09 -0400 | [diff] [blame] | 1013 | << "void main(" << inputPT << " GS_INPUT input[" << inputSize << "], "; |
| 1014 | |
| 1015 | if (primitiveType == PRIMITIVE_TRIANGLE_STRIP) |
| 1016 | { |
| 1017 | shaderStream << "uint primitiveID : SV_PrimitiveID, "; |
| 1018 | } |
| 1019 | |
| 1020 | shaderStream << " inout " << outputPT << "Stream<GS_OUTPUT> outStream)\n" |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 1021 | << "{\n" |
| 1022 | << " GS_OUTPUT output = (GS_OUTPUT)0;\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1023 | |
Jamie Madill | c7a92fd | 2015-10-29 10:08:09 -0400 | [diff] [blame] | 1024 | if (primitiveType == PRIMITIVE_TRIANGLE_STRIP) |
| 1025 | { |
| 1026 | shaderStream << " uint lastVertexIndex = (primitiveID % 2 == 0 ? 2 : 1);\n"; |
| 1027 | } |
| 1028 | else |
| 1029 | { |
| 1030 | shaderStream << " uint lastVertexIndex = " << (inputSize - 1) << ";\n"; |
| 1031 | } |
| 1032 | |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 1033 | for (int vertexIndex = 0; vertexIndex < inputSize; ++vertexIndex) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1034 | { |
Jamie Madill | c7a92fd | 2015-10-29 10:08:09 -0400 | [diff] [blame] | 1035 | shaderStream << " copyVertex(output, input[" << vertexIndex |
| 1036 | << "], input[lastVertexIndex]);\n"; |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 1037 | |
| 1038 | if (!pointSprites) |
| 1039 | { |
| 1040 | ASSERT(inputSize == maxVertexOutput); |
Jamie Madill | 4e31ad5 | 2015-10-29 10:32:57 -0400 | [diff] [blame] | 1041 | shaderStream << " outStream.Append(output);\n"; |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | if (pointSprites) |
| 1046 | { |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 1047 | shaderStream << "\n" |
| 1048 | " float4 dx_Position = input[0].dx_Position;\n" |
| 1049 | " float gl_PointSize = clamp(input[0].gl_PointSize, minPointSize, " |
| 1050 | "maxPointSize);\n" |
| 1051 | " float2 viewportScale = float2(1.0f / dx_ViewCoords.x, 1.0f / " |
| 1052 | "dx_ViewCoords.y) * dx_Position.w;\n"; |
| 1053 | |
| 1054 | for (int corner = 0; corner < 4; corner++) |
| 1055 | { |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 1056 | if (useViewScale) |
| 1057 | { |
| 1058 | shaderStream << " \n" |
| 1059 | " output.dx_Position = dx_Position + float4(1.0f, " |
| 1060 | "-dx_ViewScale.y, 1.0f, 1.0f)" |
| 1061 | " * float4(pointSpriteCorners[" |
| 1062 | << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n"; |
| 1063 | } |
| 1064 | else |
| 1065 | { |
| 1066 | shaderStream << "\n" |
| 1067 | " output.dx_Position = dx_Position + float4(pointSpriteCorners[" |
| 1068 | << corner << "] * viewportScale * gl_PointSize, 0.0f, 0.0f);\n"; |
| 1069 | } |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 1070 | |
| 1071 | if (usesPointCoord) |
| 1072 | { |
| 1073 | shaderStream << " output.gl_PointCoord = pointSpriteTexcoords[" << corner |
| 1074 | << "];\n"; |
| 1075 | } |
| 1076 | |
| 1077 | shaderStream << " outStream.Append(output);\n"; |
| 1078 | } |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1079 | } |
| 1080 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 1081 | shaderStream << " \n" |
Jamie Madill | 76f8fa6 | 2015-10-29 10:32:56 -0400 | [diff] [blame] | 1082 | " outStream.RestartStrip();\n" |
| 1083 | "}\n"; |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1084 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 1085 | return shaderStream.str(); |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1086 | } |
| 1087 | |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 1088 | // static |
| 1089 | void DynamicHLSL::GenerateAttributeConversionHLSL(gl::VertexFormatType vertexFormatType, |
| 1090 | const sh::ShaderVariable &shaderAttrib, |
| 1091 | std::ostringstream &outStream) |
Jamie Madill | 5f56273 | 2014-02-14 16:41:24 -0500 | [diff] [blame] | 1092 | { |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1093 | // Matrix |
| 1094 | if (IsMatrixType(shaderAttrib.type)) |
| 1095 | { |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 1096 | outStream << "transpose(input." << DecorateVariable(shaderAttrib.name) << ")"; |
| 1097 | return; |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1098 | } |
| 1099 | |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 1100 | GLenum shaderComponentType = VariableComponentType(shaderAttrib.type); |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 1101 | int shaderComponentCount = VariableComponentCount(shaderAttrib.type); |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 1102 | const gl::VertexFormat &vertexFormat = gl::GetVertexFormatFromType(vertexFormatType); |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1103 | |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1104 | // Perform integer to float conversion (if necessary) |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 1105 | if (shaderComponentType == GL_FLOAT && vertexFormat.type != GL_FLOAT) |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1106 | { |
Jamie Madill | 7a29e4a | 2014-05-02 10:41:48 -0400 | [diff] [blame] | 1107 | // TODO: normalization for 32-bit integer formats |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 1108 | ASSERT(!vertexFormat.normalized && !vertexFormat.pureInteger); |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 1109 | outStream << "float" << shaderComponentCount << "(input." |
| 1110 | << DecorateVariable(shaderAttrib.name) << ")"; |
| 1111 | return; |
Jamie Madill | 8664b06 | 2014-02-14 16:41:29 -0500 | [diff] [blame] | 1112 | } |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1113 | |
| 1114 | // No conversion necessary |
Jamie Madill | 10bed9f | 2017-06-05 12:59:22 -0400 | [diff] [blame] | 1115 | outStream << "input." << DecorateVariable(shaderAttrib.name); |
Jamie Madill | c5ede1a | 2014-02-14 16:41:27 -0500 | [diff] [blame] | 1116 | } |
Jamie Madill | e39a3f0 | 2015-11-17 20:42:15 -0500 | [diff] [blame] | 1117 | |
Jamie Madill | 9082b98 | 2016-04-27 15:21:51 -0400 | [diff] [blame] | 1118 | void DynamicHLSL::getPixelShaderOutputKey(const gl::ContextState &data, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 1119 | const gl::ProgramState &programData, |
Jamie Madill | e39a3f0 | 2015-11-17 20:42:15 -0500 | [diff] [blame] | 1120 | const ProgramD3DMetadata &metadata, |
| 1121 | std::vector<PixelShaderOutputVariable> *outPixelShaderKey) |
| 1122 | { |
| 1123 | // Two cases when writing to gl_FragColor and using ESSL 1.0: |
| 1124 | // - with a 3.0 context, the output color is copied to channel 0 |
| 1125 | // - with a 2.0 context, the output color is broadcast to all channels |
| 1126 | bool broadcast = metadata.usesBroadcast(data); |
| 1127 | const unsigned int numRenderTargets = |
Jamie Madill | dfde6ab | 2016-06-09 07:07:18 -0700 | [diff] [blame] | 1128 | (broadcast || metadata.usesMultipleFragmentOuts() ? data.getCaps().maxDrawBuffers : 1); |
Jamie Madill | e39a3f0 | 2015-11-17 20:42:15 -0500 | [diff] [blame] | 1129 | |
| 1130 | if (metadata.getMajorShaderVersion() < 300) |
| 1131 | { |
| 1132 | for (unsigned int renderTargetIndex = 0; renderTargetIndex < numRenderTargets; |
| 1133 | renderTargetIndex++) |
| 1134 | { |
| 1135 | PixelShaderOutputVariable outputKeyVariable; |
| 1136 | outputKeyVariable.type = GL_FLOAT_VEC4; |
| 1137 | outputKeyVariable.name = "gl_Color" + Str(renderTargetIndex); |
| 1138 | outputKeyVariable.source = |
| 1139 | broadcast ? "gl_Color[0]" : "gl_Color[" + Str(renderTargetIndex) + "]"; |
| 1140 | outputKeyVariable.outputIndex = renderTargetIndex; |
| 1141 | |
| 1142 | outPixelShaderKey->push_back(outputKeyVariable); |
| 1143 | } |
| 1144 | } |
| 1145 | else |
| 1146 | { |
| 1147 | const auto &shaderOutputVars = |
| 1148 | metadata.getFragmentShader()->getData().getActiveOutputVariables(); |
| 1149 | |
jchen10 | 15015f7 | 2017-03-16 13:54:21 +0800 | [diff] [blame] | 1150 | for (auto outputPair : programData.getOutputLocations()) |
Jamie Madill | e39a3f0 | 2015-11-17 20:42:15 -0500 | [diff] [blame] | 1151 | { |
| 1152 | const VariableLocation &outputLocation = outputPair.second; |
| 1153 | const sh::ShaderVariable &outputVariable = shaderOutputVars[outputLocation.index]; |
| 1154 | const std::string &variableName = "out_" + outputLocation.name; |
| 1155 | const std::string &elementString = |
| 1156 | (outputLocation.element == GL_INVALID_INDEX ? "" : Str(outputLocation.element)); |
| 1157 | |
| 1158 | ASSERT(outputVariable.staticUse); |
| 1159 | |
| 1160 | PixelShaderOutputVariable outputKeyVariable; |
| 1161 | outputKeyVariable.type = outputVariable.type; |
| 1162 | outputKeyVariable.name = variableName + elementString; |
| 1163 | outputKeyVariable.source = variableName + ArrayString(outputLocation.element); |
| 1164 | outputKeyVariable.outputIndex = outputPair.first; |
| 1165 | |
| 1166 | outPixelShaderKey->push_back(outputKeyVariable); |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
Jamie Madill | 192745a | 2016-12-22 15:58:21 -0500 | [diff] [blame] | 1171 | // BuiltinVarying Implementation. |
| 1172 | BuiltinVarying::BuiltinVarying() : enabled(false), index(0), systemValue(false) |
| 1173 | { |
| 1174 | } |
| 1175 | |
| 1176 | std::string BuiltinVarying::str() const |
| 1177 | { |
| 1178 | return (systemValue ? semantic : (semantic + Str(index))); |
| 1179 | } |
| 1180 | |
| 1181 | void BuiltinVarying::enableSystem(const std::string &systemValueSemantic) |
| 1182 | { |
| 1183 | enabled = true; |
| 1184 | semantic = systemValueSemantic; |
| 1185 | systemValue = true; |
| 1186 | } |
| 1187 | |
| 1188 | void BuiltinVarying::enable(const std::string &semanticVal, unsigned int indexVal) |
| 1189 | { |
| 1190 | enabled = true; |
| 1191 | semantic = semanticVal; |
| 1192 | index = indexVal; |
| 1193 | } |
| 1194 | |
| 1195 | // BuiltinVaryingsD3D Implementation. |
| 1196 | BuiltinVaryingsD3D::BuiltinVaryingsD3D(const ProgramD3DMetadata &metadata, |
| 1197 | const VaryingPacking &packing) |
| 1198 | { |
| 1199 | updateBuiltins(SHADER_VERTEX, metadata, packing); |
| 1200 | updateBuiltins(SHADER_PIXEL, metadata, packing); |
| 1201 | if (metadata.getRendererMajorShaderModel() >= 4) |
| 1202 | { |
| 1203 | updateBuiltins(SHADER_GEOMETRY, metadata, packing); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | void BuiltinVaryingsD3D::updateBuiltins(ShaderType shaderType, |
| 1208 | const ProgramD3DMetadata &metadata, |
| 1209 | const VaryingPacking &packing) |
| 1210 | { |
| 1211 | const std::string &userSemantic = GetVaryingSemantic(metadata.getRendererMajorShaderModel(), |
| 1212 | metadata.usesSystemValuePointSize()); |
| 1213 | |
| 1214 | unsigned int reservedSemanticIndex = packing.getMaxSemanticIndex(); |
| 1215 | |
| 1216 | BuiltinInfo *builtins = &mBuiltinInfo[shaderType]; |
| 1217 | |
| 1218 | if (metadata.getRendererMajorShaderModel() >= 4) |
| 1219 | { |
| 1220 | builtins->dxPosition.enableSystem("SV_Position"); |
| 1221 | } |
| 1222 | else if (shaderType == SHADER_PIXEL) |
| 1223 | { |
| 1224 | builtins->dxPosition.enableSystem("VPOS"); |
| 1225 | } |
| 1226 | else |
| 1227 | { |
| 1228 | builtins->dxPosition.enableSystem("POSITION"); |
| 1229 | } |
| 1230 | |
| 1231 | if (metadata.usesTransformFeedbackGLPosition()) |
| 1232 | { |
| 1233 | builtins->glPosition.enable(userSemantic, reservedSemanticIndex++); |
| 1234 | } |
| 1235 | |
| 1236 | if (metadata.usesFragCoord()) |
| 1237 | { |
| 1238 | builtins->glFragCoord.enable(userSemantic, reservedSemanticIndex++); |
| 1239 | } |
| 1240 | |
| 1241 | if (shaderType == SHADER_VERTEX ? metadata.addsPointCoordToVertexShader() |
| 1242 | : metadata.usesPointCoord()) |
| 1243 | { |
| 1244 | // SM3 reserves the TEXCOORD semantic for point sprite texcoords (gl_PointCoord) |
| 1245 | // In D3D11 we manually compute gl_PointCoord in the GS. |
| 1246 | if (metadata.getRendererMajorShaderModel() >= 4) |
| 1247 | { |
| 1248 | builtins->glPointCoord.enable(userSemantic, reservedSemanticIndex++); |
| 1249 | } |
| 1250 | else |
| 1251 | { |
| 1252 | builtins->glPointCoord.enable("TEXCOORD", 0); |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | // Special case: do not include PSIZE semantic in HLSL 3 pixel shaders |
| 1257 | if (metadata.usesSystemValuePointSize() && |
| 1258 | (shaderType != SHADER_PIXEL || metadata.getRendererMajorShaderModel() >= 4)) |
| 1259 | { |
| 1260 | builtins->glPointSize.enableSystem("PSIZE"); |
| 1261 | } |
| 1262 | } |
| 1263 | |
Jamie Madill | 334d615 | 2015-10-22 14:00:28 -0400 | [diff] [blame] | 1264 | } // namespace rx |