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