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