Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [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 | |
| 7 | // ProgramD3D.cpp: Defines the rx::ProgramD3D class which implements rx::ProgramImpl. |
| 8 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 9 | #include "libANGLE/renderer/d3d/ProgramD3D.h" |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 10 | |
| 11 | #include "common/utilities.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 12 | #include "libANGLE/Framebuffer.h" |
| 13 | #include "libANGLE/FramebufferAttachment.h" |
| 14 | #include "libANGLE/Program.h" |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 15 | #include "libANGLE/VertexArray.h" |
Jamie Madill | 6df9b37 | 2015-02-18 21:28:19 +0000 | [diff] [blame] | 16 | #include "libANGLE/features.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 17 | #include "libANGLE/renderer/d3d/DynamicHLSL.h" |
Jamie Madill | 85a1804 | 2015-03-05 15:41:41 -0500 | [diff] [blame] | 18 | #include "libANGLE/renderer/d3d/FramebufferD3D.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 19 | #include "libANGLE/renderer/d3d/RendererD3D.h" |
| 20 | #include "libANGLE/renderer/d3d/ShaderD3D.h" |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 21 | #include "libANGLE/renderer/d3d/ShaderExecutableD3D.h" |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 22 | #include "libANGLE/renderer/d3d/VertexDataManager.h" |
Geoff Lang | 2207213 | 2014-11-20 15:15:01 -0500 | [diff] [blame] | 23 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 24 | namespace rx |
| 25 | { |
| 26 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 27 | namespace |
| 28 | { |
| 29 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 30 | GLenum GetTextureType(GLenum samplerType) |
| 31 | { |
| 32 | switch (samplerType) |
| 33 | { |
| 34 | case GL_SAMPLER_2D: |
| 35 | case GL_INT_SAMPLER_2D: |
| 36 | case GL_UNSIGNED_INT_SAMPLER_2D: |
| 37 | case GL_SAMPLER_2D_SHADOW: |
| 38 | return GL_TEXTURE_2D; |
| 39 | case GL_SAMPLER_3D: |
| 40 | case GL_INT_SAMPLER_3D: |
| 41 | case GL_UNSIGNED_INT_SAMPLER_3D: |
| 42 | return GL_TEXTURE_3D; |
| 43 | case GL_SAMPLER_CUBE: |
| 44 | case GL_SAMPLER_CUBE_SHADOW: |
| 45 | return GL_TEXTURE_CUBE_MAP; |
| 46 | case GL_INT_SAMPLER_CUBE: |
| 47 | case GL_UNSIGNED_INT_SAMPLER_CUBE: |
| 48 | return GL_TEXTURE_CUBE_MAP; |
| 49 | case GL_SAMPLER_2D_ARRAY: |
| 50 | case GL_INT_SAMPLER_2D_ARRAY: |
| 51 | case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY: |
| 52 | case GL_SAMPLER_2D_ARRAY_SHADOW: |
| 53 | return GL_TEXTURE_2D_ARRAY; |
| 54 | default: UNREACHABLE(); |
| 55 | } |
| 56 | |
| 57 | return GL_TEXTURE_2D; |
| 58 | } |
| 59 | |
Jamie Madill | f8dd7b1 | 2015-08-05 13:50:08 -0400 | [diff] [blame] | 60 | gl::InputLayout GetDefaultInputLayoutFromShader(const gl::Shader *vertexShader) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 61 | { |
Jamie Madill | bd136f9 | 2015-08-10 14:51:37 -0400 | [diff] [blame] | 62 | gl::InputLayout defaultLayout; |
| 63 | for (const sh::Attribute &shaderAttr : vertexShader->getActiveAttributes()) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 64 | { |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 65 | if (shaderAttr.type != GL_NONE) |
| 66 | { |
| 67 | GLenum transposedType = gl::TransposeMatrixType(shaderAttr.type); |
| 68 | |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 69 | for (size_t rowIndex = 0; |
| 70 | static_cast<int>(rowIndex) < gl::VariableRowCount(transposedType); |
| 71 | ++rowIndex) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 72 | { |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 73 | GLenum componentType = gl::VariableComponentType(transposedType); |
| 74 | GLuint components = static_cast<GLuint>(gl::VariableColumnCount(transposedType)); |
| 75 | bool pureInt = (componentType != GL_FLOAT); |
| 76 | gl::VertexFormatType defaultType = gl::GetVertexFormatType( |
| 77 | componentType, GL_FALSE, components, pureInt); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 78 | |
Jamie Madill | bd136f9 | 2015-08-10 14:51:37 -0400 | [diff] [blame] | 79 | defaultLayout.push_back(defaultType); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
Jamie Madill | f8dd7b1 | 2015-08-05 13:50:08 -0400 | [diff] [blame] | 83 | |
| 84 | return defaultLayout; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | std::vector<GLenum> GetDefaultOutputLayoutFromShader(const std::vector<PixelShaderOutputVariable> &shaderOutputVars) |
| 88 | { |
Jamie Madill | b446314 | 2014-12-19 14:56:54 -0500 | [diff] [blame] | 89 | std::vector<GLenum> defaultPixelOutput; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 90 | |
Jamie Madill | b446314 | 2014-12-19 14:56:54 -0500 | [diff] [blame] | 91 | if (!shaderOutputVars.empty()) |
| 92 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 93 | defaultPixelOutput.push_back(GL_COLOR_ATTACHMENT0 + |
| 94 | static_cast<unsigned int>(shaderOutputVars[0].outputIndex)); |
Jamie Madill | b446314 | 2014-12-19 14:56:54 -0500 | [diff] [blame] | 95 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 96 | |
| 97 | return defaultPixelOutput; |
| 98 | } |
| 99 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 100 | bool IsRowMajorLayout(const sh::InterfaceBlockField &var) |
| 101 | { |
| 102 | return var.isRowMajorLayout; |
| 103 | } |
| 104 | |
| 105 | bool IsRowMajorLayout(const sh::ShaderVariable &var) |
| 106 | { |
| 107 | return false; |
| 108 | } |
| 109 | |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 110 | struct AttributeSorter |
| 111 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 112 | AttributeSorter(const ProgramD3D::SemanticIndexArray &semanticIndices) |
Jamie Madill | 80d934b | 2015-02-19 10:16:12 -0500 | [diff] [blame] | 113 | : originalIndices(&semanticIndices) |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 114 | { |
| 115 | } |
| 116 | |
| 117 | bool operator()(int a, int b) |
| 118 | { |
Jamie Madill | 80d934b | 2015-02-19 10:16:12 -0500 | [diff] [blame] | 119 | int indexA = (*originalIndices)[a]; |
| 120 | int indexB = (*originalIndices)[b]; |
| 121 | |
| 122 | if (indexA == -1) return false; |
| 123 | if (indexB == -1) return true; |
| 124 | return (indexA < indexB); |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 127 | const ProgramD3D::SemanticIndexArray *originalIndices; |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 128 | }; |
| 129 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 130 | bool LinkVaryingRegisters(gl::InfoLog &infoLog, |
| 131 | ShaderD3D *vertexShaderD3D, |
| 132 | ShaderD3D *fragmentShaderD3D) |
| 133 | { |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 134 | for (PackedVarying &input : fragmentShaderD3D->getPackedVaryings()) |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 135 | { |
| 136 | bool matched = false; |
| 137 | |
| 138 | // Built-in varyings obey special rules |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 139 | if (input.varying->isBuiltIn()) |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 140 | { |
| 141 | continue; |
| 142 | } |
| 143 | |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 144 | for (PackedVarying &output : vertexShaderD3D->getPackedVaryings()) |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 145 | { |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 146 | if (output.varying->name == input.varying->name) |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 147 | { |
| 148 | output.registerIndex = input.registerIndex; |
| 149 | output.columnIndex = input.columnIndex; |
| 150 | |
| 151 | matched = true; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // We permit unmatched, unreferenced varyings |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 157 | ASSERT(matched || !input.varying->staticUse); |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | return true; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 163 | } // anonymous namespace |
| 164 | |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 165 | ProgramD3D::VertexExecutable::VertexExecutable(const gl::InputLayout &inputLayout, |
| 166 | const Signature &signature, |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 167 | ShaderExecutableD3D *shaderExecutable) |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 168 | : mInputs(inputLayout), |
| 169 | mSignature(signature), |
| 170 | mShaderExecutable(shaderExecutable) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 171 | { |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | ProgramD3D::VertexExecutable::~VertexExecutable() |
| 175 | { |
| 176 | SafeDelete(mShaderExecutable); |
| 177 | } |
| 178 | |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 179 | // static |
| 180 | void ProgramD3D::VertexExecutable::getSignature(RendererD3D *renderer, |
| 181 | const gl::InputLayout &inputLayout, |
| 182 | Signature *signatureOut) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 183 | { |
Jamie Madill | bd136f9 | 2015-08-10 14:51:37 -0400 | [diff] [blame] | 184 | signatureOut->resize(inputLayout.size()); |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 185 | |
| 186 | for (size_t index = 0; index < inputLayout.size(); ++index) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 187 | { |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 188 | gl::VertexFormatType vertexFormatType = inputLayout[index]; |
Jamie Madill | bd136f9 | 2015-08-10 14:51:37 -0400 | [diff] [blame] | 189 | bool converted = false; |
Jamie Madill | f8dd7b1 | 2015-08-05 13:50:08 -0400 | [diff] [blame] | 190 | if (vertexFormatType != gl::VERTEX_FORMAT_INVALID) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 191 | { |
Jamie Madill | f8dd7b1 | 2015-08-05 13:50:08 -0400 | [diff] [blame] | 192 | VertexConversionType conversionType = |
| 193 | renderer->getVertexConversionType(vertexFormatType); |
Jamie Madill | bd136f9 | 2015-08-10 14:51:37 -0400 | [diff] [blame] | 194 | converted = ((conversionType & VERTEX_CONVERT_GPU) != 0); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 195 | } |
Jamie Madill | bd136f9 | 2015-08-10 14:51:37 -0400 | [diff] [blame] | 196 | |
| 197 | (*signatureOut)[index] = converted; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 198 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 201 | bool ProgramD3D::VertexExecutable::matchesSignature(const Signature &signature) const |
| 202 | { |
Jamie Madill | bd136f9 | 2015-08-10 14:51:37 -0400 | [diff] [blame] | 203 | size_t limit = std::max(mSignature.size(), signature.size()); |
| 204 | for (size_t index = 0; index < limit; ++index) |
| 205 | { |
| 206 | // treat undefined indexes as 'not converted' |
| 207 | bool a = index < signature.size() ? signature[index] : false; |
| 208 | bool b = index < mSignature.size() ? mSignature[index] : false; |
| 209 | if (a != b) |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | return true; |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | ProgramD3D::PixelExecutable::PixelExecutable(const std::vector<GLenum> &outputSignature, |
| 217 | ShaderExecutableD3D *shaderExecutable) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 218 | : mOutputSignature(outputSignature), |
| 219 | mShaderExecutable(shaderExecutable) |
| 220 | { |
| 221 | } |
| 222 | |
| 223 | ProgramD3D::PixelExecutable::~PixelExecutable() |
| 224 | { |
| 225 | SafeDelete(mShaderExecutable); |
| 226 | } |
| 227 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 228 | ProgramD3D::Sampler::Sampler() : active(false), logicalTextureUnit(0), textureType(GL_TEXTURE_2D) |
| 229 | { |
| 230 | } |
| 231 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 232 | unsigned int ProgramD3D::mCurrentSerial = 1; |
| 233 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 234 | ProgramD3D::ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer) |
| 235 | : ProgramImpl(data), |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 236 | mRenderer(renderer), |
| 237 | mDynamicHLSL(NULL), |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 238 | mGeometryExecutable(NULL), |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 239 | mUsesPointSize(false), |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 240 | mVertexUniformStorage(NULL), |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 241 | mFragmentUniformStorage(NULL), |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 242 | mUsedVertexSamplerRange(0), |
| 243 | mUsedPixelSamplerRange(0), |
| 244 | mDirtySamplerMapping(true), |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 245 | mTextureUnitTypesCache(renderer->getRendererCaps().maxCombinedTextureImageUnits), |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 246 | mShaderVersion(100), |
| 247 | mSerial(issueSerial()) |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 248 | { |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 249 | mDynamicHLSL = new DynamicHLSL(renderer); |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | ProgramD3D::~ProgramD3D() |
| 253 | { |
| 254 | reset(); |
| 255 | SafeDelete(mDynamicHLSL); |
| 256 | } |
| 257 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 258 | bool ProgramD3D::usesPointSpriteEmulation() const |
| 259 | { |
| 260 | return mUsesPointSize && mRenderer->getMajorShaderModel() >= 4; |
| 261 | } |
| 262 | |
| 263 | bool ProgramD3D::usesGeometryShader() const |
| 264 | { |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 265 | return usesPointSpriteEmulation() && !usesInstancedPointSpriteEmulation(); |
| 266 | } |
| 267 | |
| 268 | bool ProgramD3D::usesInstancedPointSpriteEmulation() const |
| 269 | { |
| 270 | return mRenderer->getWorkarounds().useInstancedPointSpriteEmulation; |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 273 | GLint ProgramD3D::getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const |
| 274 | { |
| 275 | GLint logicalTextureUnit = -1; |
| 276 | |
| 277 | switch (type) |
| 278 | { |
| 279 | case gl::SAMPLER_PIXEL: |
| 280 | ASSERT(samplerIndex < caps.maxTextureImageUnits); |
| 281 | if (samplerIndex < mSamplersPS.size() && mSamplersPS[samplerIndex].active) |
| 282 | { |
| 283 | logicalTextureUnit = mSamplersPS[samplerIndex].logicalTextureUnit; |
| 284 | } |
| 285 | break; |
| 286 | case gl::SAMPLER_VERTEX: |
| 287 | ASSERT(samplerIndex < caps.maxVertexTextureImageUnits); |
| 288 | if (samplerIndex < mSamplersVS.size() && mSamplersVS[samplerIndex].active) |
| 289 | { |
| 290 | logicalTextureUnit = mSamplersVS[samplerIndex].logicalTextureUnit; |
| 291 | } |
| 292 | break; |
| 293 | default: UNREACHABLE(); |
| 294 | } |
| 295 | |
| 296 | if (logicalTextureUnit >= 0 && logicalTextureUnit < static_cast<GLint>(caps.maxCombinedTextureImageUnits)) |
| 297 | { |
| 298 | return logicalTextureUnit; |
| 299 | } |
| 300 | |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | // Returns the texture type for a given Direct3D 9 sampler type and |
| 305 | // index (0-15 for the pixel shader and 0-3 for the vertex shader). |
| 306 | GLenum ProgramD3D::getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const |
| 307 | { |
| 308 | switch (type) |
| 309 | { |
| 310 | case gl::SAMPLER_PIXEL: |
| 311 | ASSERT(samplerIndex < mSamplersPS.size()); |
| 312 | ASSERT(mSamplersPS[samplerIndex].active); |
| 313 | return mSamplersPS[samplerIndex].textureType; |
| 314 | case gl::SAMPLER_VERTEX: |
| 315 | ASSERT(samplerIndex < mSamplersVS.size()); |
| 316 | ASSERT(mSamplersVS[samplerIndex].active); |
| 317 | return mSamplersVS[samplerIndex].textureType; |
| 318 | default: UNREACHABLE(); |
| 319 | } |
| 320 | |
| 321 | return GL_TEXTURE_2D; |
| 322 | } |
| 323 | |
| 324 | GLint ProgramD3D::getUsedSamplerRange(gl::SamplerType type) const |
| 325 | { |
| 326 | switch (type) |
| 327 | { |
| 328 | case gl::SAMPLER_PIXEL: |
| 329 | return mUsedPixelSamplerRange; |
| 330 | case gl::SAMPLER_VERTEX: |
| 331 | return mUsedVertexSamplerRange; |
| 332 | default: |
| 333 | UNREACHABLE(); |
| 334 | return 0; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void ProgramD3D::updateSamplerMapping() |
| 339 | { |
| 340 | if (!mDirtySamplerMapping) |
| 341 | { |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | mDirtySamplerMapping = false; |
| 346 | |
| 347 | // Retrieve sampler uniform values |
| 348 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
| 349 | { |
| 350 | gl::LinkedUniform *targetUniform = mUniforms[uniformIndex]; |
| 351 | |
| 352 | if (targetUniform->dirty) |
| 353 | { |
Geoff Lang | 2ec386b | 2014-12-03 14:44:38 -0500 | [diff] [blame] | 354 | if (gl::IsSamplerType(targetUniform->type)) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 355 | { |
| 356 | int count = targetUniform->elementCount(); |
| 357 | GLint (*v)[4] = reinterpret_cast<GLint(*)[4]>(targetUniform->data); |
| 358 | |
| 359 | if (targetUniform->isReferencedByFragmentShader()) |
| 360 | { |
| 361 | unsigned int firstIndex = targetUniform->psRegisterIndex; |
| 362 | |
| 363 | for (int i = 0; i < count; i++) |
| 364 | { |
| 365 | unsigned int samplerIndex = firstIndex + i; |
| 366 | |
| 367 | if (samplerIndex < mSamplersPS.size()) |
| 368 | { |
| 369 | ASSERT(mSamplersPS[samplerIndex].active); |
| 370 | mSamplersPS[samplerIndex].logicalTextureUnit = v[i][0]; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (targetUniform->isReferencedByVertexShader()) |
| 376 | { |
| 377 | unsigned int firstIndex = targetUniform->vsRegisterIndex; |
| 378 | |
| 379 | for (int i = 0; i < count; i++) |
| 380 | { |
| 381 | unsigned int samplerIndex = firstIndex + i; |
| 382 | |
| 383 | if (samplerIndex < mSamplersVS.size()) |
| 384 | { |
| 385 | ASSERT(mSamplersVS[samplerIndex].active); |
| 386 | mSamplersVS[samplerIndex].logicalTextureUnit = v[i][0]; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | bool ProgramD3D::validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps) |
| 396 | { |
Jamie Madill | 1377689 | 2015-04-28 12:39:06 -0400 | [diff] [blame] | 397 | // Skip cache if we're using an infolog, so we get the full error. |
| 398 | // Also skip the cache if the sample mapping has changed, or if we haven't ever validated. |
| 399 | if (!mDirtySamplerMapping && infoLog == nullptr && mCachedValidateSamplersResult.valid()) |
| 400 | { |
| 401 | return mCachedValidateSamplersResult.value(); |
| 402 | } |
| 403 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 404 | // if any two active samplers in a program are of different types, but refer to the same |
| 405 | // texture image unit, and this is the current program, then ValidateProgram will fail, and |
| 406 | // DrawArrays and DrawElements will issue the INVALID_OPERATION error. |
| 407 | updateSamplerMapping(); |
| 408 | |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 409 | std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 410 | |
| 411 | for (unsigned int i = 0; i < mUsedPixelSamplerRange; ++i) |
| 412 | { |
| 413 | if (mSamplersPS[i].active) |
| 414 | { |
| 415 | unsigned int unit = mSamplersPS[i].logicalTextureUnit; |
| 416 | |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 417 | if (unit >= caps.maxCombinedTextureImageUnits) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 418 | { |
| 419 | if (infoLog) |
| 420 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 421 | (*infoLog) << "Sampler uniform (" << unit |
| 422 | << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (" |
| 423 | << caps.maxCombinedTextureImageUnits << ")"; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Jamie Madill | 1377689 | 2015-04-28 12:39:06 -0400 | [diff] [blame] | 426 | mCachedValidateSamplersResult = false; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 427 | return false; |
| 428 | } |
| 429 | |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 430 | if (mTextureUnitTypesCache[unit] != GL_NONE) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 431 | { |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 432 | if (mSamplersPS[i].textureType != mTextureUnitTypesCache[unit]) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 433 | { |
| 434 | if (infoLog) |
| 435 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 436 | (*infoLog) << "Samplers of conflicting types refer to the same texture image unit (" |
| 437 | << unit << ")."; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Jamie Madill | 1377689 | 2015-04-28 12:39:06 -0400 | [diff] [blame] | 440 | mCachedValidateSamplersResult = false; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | else |
| 445 | { |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 446 | mTextureUnitTypesCache[unit] = mSamplersPS[i].textureType; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | for (unsigned int i = 0; i < mUsedVertexSamplerRange; ++i) |
| 452 | { |
| 453 | if (mSamplersVS[i].active) |
| 454 | { |
| 455 | unsigned int unit = mSamplersVS[i].logicalTextureUnit; |
| 456 | |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 457 | if (unit >= caps.maxCombinedTextureImageUnits) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 458 | { |
| 459 | if (infoLog) |
| 460 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 461 | (*infoLog) << "Sampler uniform (" << unit |
| 462 | << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (" |
| 463 | << caps.maxCombinedTextureImageUnits << ")"; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Jamie Madill | 1377689 | 2015-04-28 12:39:06 -0400 | [diff] [blame] | 466 | mCachedValidateSamplersResult = false; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 467 | return false; |
| 468 | } |
| 469 | |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 470 | if (mTextureUnitTypesCache[unit] != GL_NONE) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 471 | { |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 472 | if (mSamplersVS[i].textureType != mTextureUnitTypesCache[unit]) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 473 | { |
| 474 | if (infoLog) |
| 475 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 476 | (*infoLog) << "Samplers of conflicting types refer to the same texture image unit (" |
| 477 | << unit << ")."; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Jamie Madill | 1377689 | 2015-04-28 12:39:06 -0400 | [diff] [blame] | 480 | mCachedValidateSamplersResult = false; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 481 | return false; |
| 482 | } |
| 483 | } |
| 484 | else |
| 485 | { |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 486 | mTextureUnitTypesCache[unit] = mSamplersVS[i].textureType; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
Jamie Madill | 1377689 | 2015-04-28 12:39:06 -0400 | [diff] [blame] | 491 | mCachedValidateSamplersResult = true; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 492 | return true; |
| 493 | } |
| 494 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 495 | LinkResult ProgramD3D::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 496 | { |
Austin Kinross | 137b151 | 2015-06-17 16:14:53 -0700 | [diff] [blame] | 497 | DeviceIdentifier binaryDeviceIdentifier = { 0 }; |
| 498 | stream->readBytes(reinterpret_cast<unsigned char*>(&binaryDeviceIdentifier), sizeof(DeviceIdentifier)); |
| 499 | |
| 500 | DeviceIdentifier identifier = mRenderer->getAdapterIdentifier(); |
| 501 | if (memcmp(&identifier, &binaryDeviceIdentifier, sizeof(DeviceIdentifier)) != 0) |
| 502 | { |
| 503 | infoLog << "Invalid program binary, device configuration has changed."; |
| 504 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
| 505 | } |
| 506 | |
Jamie Madill | 2db1fbb | 2014-12-03 10:58:55 -0500 | [diff] [blame] | 507 | int compileFlags = stream->readInt<int>(); |
| 508 | if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL) |
| 509 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 510 | infoLog << "Mismatched compilation flags."; |
Jamie Madill | 2db1fbb | 2014-12-03 10:58:55 -0500 | [diff] [blame] | 511 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
| 512 | } |
| 513 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 514 | stream->readInt(&mShaderVersion); |
| 515 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 516 | // TODO(jmadill): replace MAX_VERTEX_ATTRIBS |
| 517 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i) |
| 518 | { |
| 519 | stream->readInt(&mSemanticIndexes[i]); |
| 520 | } |
| 521 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 522 | const unsigned int psSamplerCount = stream->readInt<unsigned int>(); |
| 523 | for (unsigned int i = 0; i < psSamplerCount; ++i) |
| 524 | { |
| 525 | Sampler sampler; |
| 526 | stream->readBool(&sampler.active); |
| 527 | stream->readInt(&sampler.logicalTextureUnit); |
| 528 | stream->readInt(&sampler.textureType); |
| 529 | mSamplersPS.push_back(sampler); |
| 530 | } |
| 531 | const unsigned int vsSamplerCount = stream->readInt<unsigned int>(); |
| 532 | for (unsigned int i = 0; i < vsSamplerCount; ++i) |
| 533 | { |
| 534 | Sampler sampler; |
| 535 | stream->readBool(&sampler.active); |
| 536 | stream->readInt(&sampler.logicalTextureUnit); |
| 537 | stream->readInt(&sampler.textureType); |
| 538 | mSamplersVS.push_back(sampler); |
| 539 | } |
| 540 | |
| 541 | stream->readInt(&mUsedVertexSamplerRange); |
| 542 | stream->readInt(&mUsedPixelSamplerRange); |
| 543 | |
| 544 | const unsigned int uniformCount = stream->readInt<unsigned int>(); |
| 545 | if (stream->error()) |
| 546 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 547 | infoLog << "Invalid program binary."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 548 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | mUniforms.resize(uniformCount); |
| 552 | for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; uniformIndex++) |
| 553 | { |
| 554 | GLenum type = stream->readInt<GLenum>(); |
| 555 | GLenum precision = stream->readInt<GLenum>(); |
| 556 | std::string name = stream->readString(); |
| 557 | unsigned int arraySize = stream->readInt<unsigned int>(); |
| 558 | int blockIndex = stream->readInt<int>(); |
| 559 | |
| 560 | int offset = stream->readInt<int>(); |
| 561 | int arrayStride = stream->readInt<int>(); |
| 562 | int matrixStride = stream->readInt<int>(); |
| 563 | bool isRowMajorMatrix = stream->readBool(); |
| 564 | |
| 565 | const sh::BlockMemberInfo blockInfo(offset, arrayStride, matrixStride, isRowMajorMatrix); |
| 566 | |
| 567 | gl::LinkedUniform *uniform = new gl::LinkedUniform(type, precision, name, arraySize, blockIndex, blockInfo); |
| 568 | |
| 569 | stream->readInt(&uniform->psRegisterIndex); |
| 570 | stream->readInt(&uniform->vsRegisterIndex); |
| 571 | stream->readInt(&uniform->registerCount); |
| 572 | stream->readInt(&uniform->registerElement); |
| 573 | |
| 574 | mUniforms[uniformIndex] = uniform; |
| 575 | } |
| 576 | |
| 577 | const unsigned int uniformIndexCount = stream->readInt<unsigned int>(); |
| 578 | if (stream->error()) |
| 579 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 580 | infoLog << "Invalid program binary."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 581 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 584 | for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount; uniformIndexIndex++) |
| 585 | { |
Geoff Lang | 9513784 | 2015-06-02 15:38:43 -0400 | [diff] [blame] | 586 | GLuint location; |
| 587 | stream->readInt(&location); |
| 588 | |
| 589 | gl::VariableLocation variable; |
| 590 | stream->readString(&variable.name); |
| 591 | stream->readInt(&variable.element); |
| 592 | stream->readInt(&variable.index); |
| 593 | |
| 594 | mUniformIndex[location] = variable; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | unsigned int uniformBlockCount = stream->readInt<unsigned int>(); |
| 598 | if (stream->error()) |
| 599 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 600 | infoLog << "Invalid program binary."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 601 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | mUniformBlocks.resize(uniformBlockCount); |
| 605 | for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount; ++uniformBlockIndex) |
| 606 | { |
| 607 | std::string name = stream->readString(); |
| 608 | unsigned int elementIndex = stream->readInt<unsigned int>(); |
| 609 | unsigned int dataSize = stream->readInt<unsigned int>(); |
| 610 | |
| 611 | gl::UniformBlock *uniformBlock = new gl::UniformBlock(name, elementIndex, dataSize); |
| 612 | |
| 613 | stream->readInt(&uniformBlock->psRegisterIndex); |
| 614 | stream->readInt(&uniformBlock->vsRegisterIndex); |
| 615 | |
| 616 | unsigned int numMembers = stream->readInt<unsigned int>(); |
| 617 | uniformBlock->memberUniformIndexes.resize(numMembers); |
| 618 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++) |
| 619 | { |
| 620 | stream->readInt(&uniformBlock->memberUniformIndexes[blockMemberIndex]); |
| 621 | } |
| 622 | |
| 623 | mUniformBlocks[uniformBlockIndex] = uniformBlock; |
| 624 | } |
| 625 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 626 | const unsigned int transformFeedbackVaryingCount = stream->readInt<unsigned int>(); |
| 627 | mTransformFeedbackLinkedVaryings.resize(transformFeedbackVaryingCount); |
| 628 | for (unsigned int varyingIndex = 0; varyingIndex < transformFeedbackVaryingCount; varyingIndex++) |
| 629 | { |
| 630 | gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[varyingIndex]; |
| 631 | |
| 632 | stream->readString(&varying.name); |
| 633 | stream->readInt(&varying.type); |
| 634 | stream->readInt(&varying.size); |
| 635 | stream->readString(&varying.semanticName); |
| 636 | stream->readInt(&varying.semanticIndex); |
| 637 | stream->readInt(&varying.semanticIndexCount); |
| 638 | } |
| 639 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 640 | stream->readString(&mVertexHLSL); |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 641 | stream->readBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 642 | stream->readString(&mPixelHLSL); |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 643 | stream->readBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 644 | stream->readBool(&mUsesFragDepth); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 645 | stream->readBool(&mUsesPointSize); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 646 | |
| 647 | const size_t pixelShaderKeySize = stream->readInt<unsigned int>(); |
| 648 | mPixelShaderKey.resize(pixelShaderKeySize); |
| 649 | for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKeySize; pixelShaderKeyIndex++) |
| 650 | { |
| 651 | stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].type); |
| 652 | stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].name); |
| 653 | stream->readString(&mPixelShaderKey[pixelShaderKeyIndex].source); |
| 654 | stream->readInt(&mPixelShaderKey[pixelShaderKeyIndex].outputIndex); |
| 655 | } |
| 656 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 657 | const unsigned char* binary = reinterpret_cast<const unsigned char*>(stream->data()); |
| 658 | |
| 659 | const unsigned int vertexShaderCount = stream->readInt<unsigned int>(); |
| 660 | for (unsigned int vertexShaderIndex = 0; vertexShaderIndex < vertexShaderCount; vertexShaderIndex++) |
| 661 | { |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 662 | size_t inputLayoutSize = stream->readInt<size_t>(); |
Jamie Madill | f8dd7b1 | 2015-08-05 13:50:08 -0400 | [diff] [blame] | 663 | gl::InputLayout inputLayout(inputLayoutSize, gl::VERTEX_FORMAT_INVALID); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 664 | |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 665 | for (size_t inputIndex = 0; inputIndex < inputLayoutSize; inputIndex++) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 666 | { |
Jamie Madill | f8dd7b1 | 2015-08-05 13:50:08 -0400 | [diff] [blame] | 667 | inputLayout[inputIndex] = stream->readInt<gl::VertexFormatType>(); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | unsigned int vertexShaderSize = stream->readInt<unsigned int>(); |
| 671 | const unsigned char *vertexShaderFunction = binary + stream->offset(); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 672 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 673 | ShaderExecutableD3D *shaderExecutable = nullptr; |
| 674 | |
| 675 | gl::Error error = mRenderer->loadExecutable( |
| 676 | vertexShaderFunction, vertexShaderSize, SHADER_VERTEX, mTransformFeedbackLinkedVaryings, |
| 677 | (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 678 | if (error.isError()) |
| 679 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 680 | return LinkResult(false, error); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 681 | } |
| 682 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 683 | if (!shaderExecutable) |
| 684 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 685 | infoLog << "Could not create vertex shader."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 686 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | // generated converted input layout |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 690 | VertexExecutable::Signature signature; |
| 691 | VertexExecutable::getSignature(mRenderer, inputLayout, &signature); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 692 | |
| 693 | // add new binary |
| 694 | mVertexExecutables.push_back(new VertexExecutable(inputLayout, signature, shaderExecutable)); |
| 695 | |
| 696 | stream->skip(vertexShaderSize); |
| 697 | } |
| 698 | |
| 699 | const size_t pixelShaderCount = stream->readInt<unsigned int>(); |
| 700 | for (size_t pixelShaderIndex = 0; pixelShaderIndex < pixelShaderCount; pixelShaderIndex++) |
| 701 | { |
| 702 | const size_t outputCount = stream->readInt<unsigned int>(); |
| 703 | std::vector<GLenum> outputs(outputCount); |
| 704 | for (size_t outputIndex = 0; outputIndex < outputCount; outputIndex++) |
| 705 | { |
| 706 | stream->readInt(&outputs[outputIndex]); |
| 707 | } |
| 708 | |
| 709 | const size_t pixelShaderSize = stream->readInt<unsigned int>(); |
| 710 | const unsigned char *pixelShaderFunction = binary + stream->offset(); |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 711 | ShaderExecutableD3D *shaderExecutable = nullptr; |
| 712 | |
| 713 | gl::Error error = mRenderer->loadExecutable( |
| 714 | pixelShaderFunction, pixelShaderSize, SHADER_PIXEL, mTransformFeedbackLinkedVaryings, |
| 715 | (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &shaderExecutable); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 716 | if (error.isError()) |
| 717 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 718 | return LinkResult(false, error); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 719 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 720 | |
| 721 | if (!shaderExecutable) |
| 722 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 723 | infoLog << "Could not create pixel shader."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 724 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | // add new binary |
| 728 | mPixelExecutables.push_back(new PixelExecutable(outputs, shaderExecutable)); |
| 729 | |
| 730 | stream->skip(pixelShaderSize); |
| 731 | } |
| 732 | |
| 733 | unsigned int geometryShaderSize = stream->readInt<unsigned int>(); |
| 734 | |
| 735 | if (geometryShaderSize > 0) |
| 736 | { |
| 737 | const unsigned char *geometryShaderFunction = binary + stream->offset(); |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 738 | gl::Error error = mRenderer->loadExecutable( |
| 739 | geometryShaderFunction, geometryShaderSize, SHADER_GEOMETRY, |
| 740 | mTransformFeedbackLinkedVaryings, |
| 741 | (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), &mGeometryExecutable); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 742 | if (error.isError()) |
| 743 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 744 | return LinkResult(false, error); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 745 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 746 | |
| 747 | if (!mGeometryExecutable) |
| 748 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 749 | infoLog << "Could not create geometry shader."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 750 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 751 | } |
| 752 | stream->skip(geometryShaderSize); |
| 753 | } |
| 754 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 755 | initializeUniformStorage(); |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 756 | initAttributesByLayout(); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 757 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 758 | return LinkResult(true, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 761 | gl::Error ProgramD3D::save(gl::BinaryOutputStream *stream) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 762 | { |
Austin Kinross | 137b151 | 2015-06-17 16:14:53 -0700 | [diff] [blame] | 763 | // Output the DeviceIdentifier before we output any shader code |
| 764 | // When we load the binary again later, we can validate the device identifier before trying to compile any HLSL |
| 765 | DeviceIdentifier binaryIdentifier = mRenderer->getAdapterIdentifier(); |
| 766 | stream->writeBytes(reinterpret_cast<unsigned char*>(&binaryIdentifier), sizeof(DeviceIdentifier)); |
| 767 | |
Jamie Madill | 2db1fbb | 2014-12-03 10:58:55 -0500 | [diff] [blame] | 768 | stream->writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL); |
| 769 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 770 | stream->writeInt(mShaderVersion); |
| 771 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 772 | // TODO(jmadill): replace MAX_VERTEX_ATTRIBS |
| 773 | for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; ++i) |
| 774 | { |
| 775 | stream->writeInt(mSemanticIndexes[i]); |
| 776 | } |
| 777 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 778 | stream->writeInt(mSamplersPS.size()); |
| 779 | for (unsigned int i = 0; i < mSamplersPS.size(); ++i) |
| 780 | { |
| 781 | stream->writeInt(mSamplersPS[i].active); |
| 782 | stream->writeInt(mSamplersPS[i].logicalTextureUnit); |
| 783 | stream->writeInt(mSamplersPS[i].textureType); |
| 784 | } |
| 785 | |
| 786 | stream->writeInt(mSamplersVS.size()); |
| 787 | for (unsigned int i = 0; i < mSamplersVS.size(); ++i) |
| 788 | { |
| 789 | stream->writeInt(mSamplersVS[i].active); |
| 790 | stream->writeInt(mSamplersVS[i].logicalTextureUnit); |
| 791 | stream->writeInt(mSamplersVS[i].textureType); |
| 792 | } |
| 793 | |
| 794 | stream->writeInt(mUsedVertexSamplerRange); |
| 795 | stream->writeInt(mUsedPixelSamplerRange); |
| 796 | |
| 797 | stream->writeInt(mUniforms.size()); |
| 798 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex) |
| 799 | { |
| 800 | const gl::LinkedUniform &uniform = *mUniforms[uniformIndex]; |
| 801 | |
| 802 | stream->writeInt(uniform.type); |
| 803 | stream->writeInt(uniform.precision); |
| 804 | stream->writeString(uniform.name); |
| 805 | stream->writeInt(uniform.arraySize); |
| 806 | stream->writeInt(uniform.blockIndex); |
| 807 | |
| 808 | stream->writeInt(uniform.blockInfo.offset); |
| 809 | stream->writeInt(uniform.blockInfo.arrayStride); |
| 810 | stream->writeInt(uniform.blockInfo.matrixStride); |
| 811 | stream->writeInt(uniform.blockInfo.isRowMajorMatrix); |
| 812 | |
| 813 | stream->writeInt(uniform.psRegisterIndex); |
| 814 | stream->writeInt(uniform.vsRegisterIndex); |
| 815 | stream->writeInt(uniform.registerCount); |
| 816 | stream->writeInt(uniform.registerElement); |
| 817 | } |
| 818 | |
| 819 | stream->writeInt(mUniformIndex.size()); |
Geoff Lang | 9513784 | 2015-06-02 15:38:43 -0400 | [diff] [blame] | 820 | for (const auto &uniform : mUniformIndex) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 821 | { |
Geoff Lang | 9513784 | 2015-06-02 15:38:43 -0400 | [diff] [blame] | 822 | GLuint location = uniform.first; |
| 823 | stream->writeInt(location); |
| 824 | |
| 825 | const gl::VariableLocation &variable = uniform.second; |
| 826 | stream->writeString(variable.name); |
| 827 | stream->writeInt(variable.element); |
| 828 | stream->writeInt(variable.index); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | stream->writeInt(mUniformBlocks.size()); |
| 832 | for (size_t uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); ++uniformBlockIndex) |
| 833 | { |
| 834 | const gl::UniformBlock& uniformBlock = *mUniformBlocks[uniformBlockIndex]; |
| 835 | |
| 836 | stream->writeString(uniformBlock.name); |
| 837 | stream->writeInt(uniformBlock.elementIndex); |
| 838 | stream->writeInt(uniformBlock.dataSize); |
| 839 | |
| 840 | stream->writeInt(uniformBlock.memberUniformIndexes.size()); |
| 841 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++) |
| 842 | { |
| 843 | stream->writeInt(uniformBlock.memberUniformIndexes[blockMemberIndex]); |
| 844 | } |
| 845 | |
| 846 | stream->writeInt(uniformBlock.psRegisterIndex); |
| 847 | stream->writeInt(uniformBlock.vsRegisterIndex); |
| 848 | } |
| 849 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 850 | stream->writeInt(mTransformFeedbackLinkedVaryings.size()); |
| 851 | for (size_t i = 0; i < mTransformFeedbackLinkedVaryings.size(); i++) |
| 852 | { |
| 853 | const gl::LinkedVarying &varying = mTransformFeedbackLinkedVaryings[i]; |
| 854 | |
| 855 | stream->writeString(varying.name); |
| 856 | stream->writeInt(varying.type); |
| 857 | stream->writeInt(varying.size); |
| 858 | stream->writeString(varying.semanticName); |
| 859 | stream->writeInt(varying.semanticIndex); |
| 860 | stream->writeInt(varying.semanticIndexCount); |
| 861 | } |
| 862 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 863 | stream->writeString(mVertexHLSL); |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 864 | stream->writeBytes(reinterpret_cast<unsigned char*>(&mVertexWorkarounds), sizeof(D3DCompilerWorkarounds)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 865 | stream->writeString(mPixelHLSL); |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 866 | stream->writeBytes(reinterpret_cast<unsigned char*>(&mPixelWorkarounds), sizeof(D3DCompilerWorkarounds)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 867 | stream->writeInt(mUsesFragDepth); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 868 | stream->writeInt(mUsesPointSize); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 869 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 870 | const std::vector<PixelShaderOutputVariable> &pixelShaderKey = mPixelShaderKey; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 871 | stream->writeInt(pixelShaderKey.size()); |
| 872 | for (size_t pixelShaderKeyIndex = 0; pixelShaderKeyIndex < pixelShaderKey.size(); pixelShaderKeyIndex++) |
| 873 | { |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 874 | const PixelShaderOutputVariable &variable = pixelShaderKey[pixelShaderKeyIndex]; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 875 | stream->writeInt(variable.type); |
| 876 | stream->writeString(variable.name); |
| 877 | stream->writeString(variable.source); |
| 878 | stream->writeInt(variable.outputIndex); |
| 879 | } |
| 880 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 881 | stream->writeInt(mVertexExecutables.size()); |
| 882 | for (size_t vertexExecutableIndex = 0; vertexExecutableIndex < mVertexExecutables.size(); vertexExecutableIndex++) |
| 883 | { |
| 884 | VertexExecutable *vertexExecutable = mVertexExecutables[vertexExecutableIndex]; |
| 885 | |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 886 | const auto &inputLayout = vertexExecutable->inputs(); |
| 887 | stream->writeInt(inputLayout.size()); |
| 888 | |
| 889 | for (size_t inputIndex = 0; inputIndex < inputLayout.size(); inputIndex++) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 890 | { |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 891 | stream->writeInt(inputLayout[inputIndex]); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | size_t vertexShaderSize = vertexExecutable->shaderExecutable()->getLength(); |
| 895 | stream->writeInt(vertexShaderSize); |
| 896 | |
| 897 | const uint8_t *vertexBlob = vertexExecutable->shaderExecutable()->getFunction(); |
| 898 | stream->writeBytes(vertexBlob, vertexShaderSize); |
| 899 | } |
| 900 | |
| 901 | stream->writeInt(mPixelExecutables.size()); |
| 902 | for (size_t pixelExecutableIndex = 0; pixelExecutableIndex < mPixelExecutables.size(); pixelExecutableIndex++) |
| 903 | { |
| 904 | PixelExecutable *pixelExecutable = mPixelExecutables[pixelExecutableIndex]; |
| 905 | |
| 906 | const std::vector<GLenum> outputs = pixelExecutable->outputSignature(); |
| 907 | stream->writeInt(outputs.size()); |
| 908 | for (size_t outputIndex = 0; outputIndex < outputs.size(); outputIndex++) |
| 909 | { |
| 910 | stream->writeInt(outputs[outputIndex]); |
| 911 | } |
| 912 | |
| 913 | size_t pixelShaderSize = pixelExecutable->shaderExecutable()->getLength(); |
| 914 | stream->writeInt(pixelShaderSize); |
| 915 | |
| 916 | const uint8_t *pixelBlob = pixelExecutable->shaderExecutable()->getFunction(); |
| 917 | stream->writeBytes(pixelBlob, pixelShaderSize); |
| 918 | } |
| 919 | |
| 920 | size_t geometryShaderSize = (mGeometryExecutable != NULL) ? mGeometryExecutable->getLength() : 0; |
| 921 | stream->writeInt(geometryShaderSize); |
| 922 | |
| 923 | if (mGeometryExecutable != NULL && geometryShaderSize > 0) |
| 924 | { |
| 925 | const uint8_t *geometryBlob = mGeometryExecutable->getFunction(); |
| 926 | stream->writeBytes(geometryBlob, geometryShaderSize); |
| 927 | } |
| 928 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 929 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 932 | gl::Error ProgramD3D::getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExecutable) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 933 | { |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 934 | mPixelShaderOutputFormatCache.clear(); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 935 | |
Jamie Madill | 85a1804 | 2015-03-05 15:41:41 -0500 | [diff] [blame] | 936 | const FramebufferD3D *fboD3D = GetImplAs<FramebufferD3D>(fbo); |
| 937 | const gl::AttachmentList &colorbuffers = fboD3D->getColorAttachmentsForRender(mRenderer->getWorkarounds()); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 938 | |
| 939 | for (size_t colorAttachment = 0; colorAttachment < colorbuffers.size(); ++colorAttachment) |
| 940 | { |
| 941 | const gl::FramebufferAttachment *colorbuffer = colorbuffers[colorAttachment]; |
| 942 | |
| 943 | if (colorbuffer) |
| 944 | { |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 945 | mPixelShaderOutputFormatCache.push_back(colorbuffer->getBinding() == GL_BACK ? GL_COLOR_ATTACHMENT0 : colorbuffer->getBinding()); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 946 | } |
| 947 | else |
| 948 | { |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 949 | mPixelShaderOutputFormatCache.push_back(GL_NONE); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 953 | return getPixelExecutableForOutputLayout(mPixelShaderOutputFormatCache, outExecutable, nullptr); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Jamie Madill | 9739923 | 2014-12-23 12:31:15 -0500 | [diff] [blame] | 956 | gl::Error ProgramD3D::getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputSignature, |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 957 | ShaderExecutableD3D **outExectuable, |
Jamie Madill | 9739923 | 2014-12-23 12:31:15 -0500 | [diff] [blame] | 958 | gl::InfoLog *infoLog) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 959 | { |
| 960 | for (size_t executableIndex = 0; executableIndex < mPixelExecutables.size(); executableIndex++) |
| 961 | { |
| 962 | if (mPixelExecutables[executableIndex]->matchesSignature(outputSignature)) |
| 963 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 964 | *outExectuable = mPixelExecutables[executableIndex]->shaderExecutable(); |
| 965 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 969 | std::string finalPixelHLSL = mDynamicHLSL->generatePixelShaderForOutputSignature(mPixelHLSL, mPixelShaderKey, mUsesFragDepth, |
| 970 | outputSignature); |
| 971 | |
| 972 | // Generate new pixel executable |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 973 | ShaderExecutableD3D *pixelExecutable = NULL; |
Jamie Madill | 9739923 | 2014-12-23 12:31:15 -0500 | [diff] [blame] | 974 | |
| 975 | gl::InfoLog tempInfoLog; |
| 976 | gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog; |
| 977 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 978 | gl::Error error = mRenderer->compileToExecutable( |
| 979 | *currentInfoLog, finalPixelHLSL, SHADER_PIXEL, mTransformFeedbackLinkedVaryings, |
| 980 | (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mPixelWorkarounds, |
| 981 | &pixelExecutable); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 982 | if (error.isError()) |
| 983 | { |
| 984 | return error; |
| 985 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 986 | |
Jamie Madill | 9739923 | 2014-12-23 12:31:15 -0500 | [diff] [blame] | 987 | if (pixelExecutable) |
| 988 | { |
| 989 | mPixelExecutables.push_back(new PixelExecutable(outputSignature, pixelExecutable)); |
| 990 | } |
| 991 | else if (!infoLog) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 992 | { |
| 993 | std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 994 | tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 995 | ERR("Error compiling dynamic pixel executable:\n%s\n", &tempCharBuffer[0]); |
| 996 | } |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 997 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 998 | *outExectuable = pixelExecutable; |
| 999 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 1002 | gl::Error ProgramD3D::getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout, |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 1003 | ShaderExecutableD3D **outExectuable, |
Jamie Madill | 9739923 | 2014-12-23 12:31:15 -0500 | [diff] [blame] | 1004 | gl::InfoLog *infoLog) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1005 | { |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 1006 | VertexExecutable::getSignature(mRenderer, inputLayout, &mCachedVertexSignature); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1007 | |
| 1008 | for (size_t executableIndex = 0; executableIndex < mVertexExecutables.size(); executableIndex++) |
| 1009 | { |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 1010 | if (mVertexExecutables[executableIndex]->matchesSignature(mCachedVertexSignature)) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1011 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1012 | *outExectuable = mVertexExecutables[executableIndex]->shaderExecutable(); |
| 1013 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1017 | // Generate new dynamic layout with attribute conversions |
Jamie Madill | c349ec0 | 2015-08-21 16:53:12 -0400 | [diff] [blame] | 1018 | std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout( |
| 1019 | mVertexHLSL, inputLayout, mData.getAttributes()); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | // Generate new vertex executable |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 1022 | ShaderExecutableD3D *vertexExecutable = NULL; |
Jamie Madill | 9739923 | 2014-12-23 12:31:15 -0500 | [diff] [blame] | 1023 | |
| 1024 | gl::InfoLog tempInfoLog; |
| 1025 | gl::InfoLog *currentInfoLog = infoLog ? infoLog : &tempInfoLog; |
| 1026 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1027 | gl::Error error = mRenderer->compileToExecutable( |
| 1028 | *currentInfoLog, finalVertexHLSL, SHADER_VERTEX, mTransformFeedbackLinkedVaryings, |
| 1029 | (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), mVertexWorkarounds, |
| 1030 | &vertexExecutable); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1031 | if (error.isError()) |
| 1032 | { |
| 1033 | return error; |
| 1034 | } |
| 1035 | |
Jamie Madill | 9739923 | 2014-12-23 12:31:15 -0500 | [diff] [blame] | 1036 | if (vertexExecutable) |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1037 | { |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 1038 | mVertexExecutables.push_back(new VertexExecutable(inputLayout, mCachedVertexSignature, vertexExecutable)); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1039 | } |
Jamie Madill | 9739923 | 2014-12-23 12:31:15 -0500 | [diff] [blame] | 1040 | else if (!infoLog) |
| 1041 | { |
| 1042 | std::vector<char> tempCharBuffer(tempInfoLog.getLength() + 3); |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1043 | tempInfoLog.getLog(static_cast<GLsizei>(tempInfoLog.getLength()), NULL, &tempCharBuffer[0]); |
Jamie Madill | 9739923 | 2014-12-23 12:31:15 -0500 | [diff] [blame] | 1044 | ERR("Error compiling dynamic vertex executable:\n%s\n", &tempCharBuffer[0]); |
| 1045 | } |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1046 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1047 | *outExectuable = vertexExecutable; |
| 1048 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 1051 | LinkResult ProgramD3D::compileProgramExecutables(gl::InfoLog &infoLog, int registers) |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1052 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 1053 | const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedVertexShader()); |
| 1054 | const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(mData.getAttachedFragmentShader()); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1055 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 1056 | const gl::InputLayout &defaultInputLayout = |
| 1057 | GetDefaultInputLayoutFromShader(mData.getAttachedVertexShader()); |
Jamie Madill | e4ea202 | 2015-03-26 20:35:05 +0000 | [diff] [blame] | 1058 | ShaderExecutableD3D *defaultVertexExecutable = NULL; |
| 1059 | gl::Error error = getVertexExecutableForInputLayout(defaultInputLayout, &defaultVertexExecutable, &infoLog); |
| 1060 | if (error.isError()) |
Austin Kinross | 434953e | 2015-02-20 10:49:51 -0800 | [diff] [blame] | 1061 | { |
Jamie Madill | e4ea202 | 2015-03-26 20:35:05 +0000 | [diff] [blame] | 1062 | return LinkResult(false, error); |
| 1063 | } |
Austin Kinross | 434953e | 2015-02-20 10:49:51 -0800 | [diff] [blame] | 1064 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1065 | std::vector<GLenum> defaultPixelOutput = GetDefaultOutputLayoutFromShader(getPixelShaderKey()); |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 1066 | ShaderExecutableD3D *defaultPixelExecutable = NULL; |
Jamie Madill | e4ea202 | 2015-03-26 20:35:05 +0000 | [diff] [blame] | 1067 | error = getPixelExecutableForOutputLayout(defaultPixelOutput, &defaultPixelExecutable, &infoLog); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1068 | if (error.isError()) |
| 1069 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1070 | return LinkResult(false, error); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1071 | } |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1072 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1073 | if (usesGeometryShader()) |
| 1074 | { |
| 1075 | std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShaderD3D, vertexShaderD3D); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1076 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1077 | error = mRenderer->compileToExecutable( |
| 1078 | infoLog, geometryHLSL, SHADER_GEOMETRY, mTransformFeedbackLinkedVaryings, |
| 1079 | (mData.getTransformFeedbackBufferMode() == GL_SEPARATE_ATTRIBS), |
| 1080 | D3DCompilerWorkarounds(), &mGeometryExecutable); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1081 | if (error.isError()) |
| 1082 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1083 | return LinkResult(false, error); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1084 | } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Brandon Jones | 091540d | 2014-10-29 11:32:04 -0700 | [diff] [blame] | 1087 | #if ANGLE_SHADER_DEBUG_INFO == ANGLE_ENABLED |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 1088 | if (usesGeometryShader() && mGeometryExecutable) |
| 1089 | { |
| 1090 | // Geometry shaders are currently only used internally, so there is no corresponding shader object at the interface level |
| 1091 | // For now the geometry shader debug info is pre-pended to the vertex shader, this is a bit of a clutch |
| 1092 | vertexShaderD3D->appendDebugInfo("// GEOMETRY SHADER BEGIN\n\n"); |
| 1093 | vertexShaderD3D->appendDebugInfo(mGeometryExecutable->getDebugInfo()); |
| 1094 | vertexShaderD3D->appendDebugInfo("\nGEOMETRY SHADER END\n\n\n"); |
| 1095 | } |
| 1096 | |
| 1097 | if (defaultVertexExecutable) |
| 1098 | { |
| 1099 | vertexShaderD3D->appendDebugInfo(defaultVertexExecutable->getDebugInfo()); |
| 1100 | } |
| 1101 | |
| 1102 | if (defaultPixelExecutable) |
| 1103 | { |
| 1104 | fragmentShaderD3D->appendDebugInfo(defaultPixelExecutable->getDebugInfo()); |
| 1105 | } |
| 1106 | #endif |
| 1107 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 1108 | bool linkSuccess = (defaultVertexExecutable && defaultPixelExecutable && (!usesGeometryShader() || mGeometryExecutable)); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1109 | return LinkResult(linkSuccess, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 1112 | LinkResult ProgramD3D::link(const gl::Data &data, |
| 1113 | gl::InfoLog &infoLog, |
| 1114 | gl::Shader *fragmentShader, |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 1115 | gl::Shader *vertexShader) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1116 | { |
Jamie Madill | f4bf381 | 2015-04-01 16:15:32 -0400 | [diff] [blame] | 1117 | ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader); |
| 1118 | ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 1119 | |
Jamie Madill | de8892b | 2014-11-11 13:00:22 -0500 | [diff] [blame] | 1120 | mSamplersPS.resize(data.caps->maxTextureImageUnits); |
| 1121 | mSamplersVS.resize(data.caps->maxVertexTextureImageUnits); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1122 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1123 | mPixelHLSL = fragmentShaderD3D->getTranslatedSource(); |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 1124 | fragmentShaderD3D->generateWorkarounds(&mPixelWorkarounds); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1125 | |
| 1126 | mVertexHLSL = vertexShaderD3D->getTranslatedSource(); |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 1127 | vertexShaderD3D->generateWorkarounds(&mVertexWorkarounds); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1128 | mShaderVersion = vertexShaderD3D->getShaderVersion(); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1129 | |
Austin Kinross | 02df796 | 2015-07-01 10:03:42 -0700 | [diff] [blame] | 1130 | if (mRenderer->getRendererLimitations().noFrontFacingSupport) |
| 1131 | { |
| 1132 | if (fragmentShaderD3D->usesFrontFacing()) |
| 1133 | { |
| 1134 | infoLog << "The current renderer doesn't support gl_FrontFacing"; |
| 1135 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
| 1136 | } |
| 1137 | } |
| 1138 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1139 | // Map the varyings to the register file |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 1140 | int registers = mDynamicHLSL->packVaryings(infoLog, fragmentShaderD3D, vertexShaderD3D, |
Jamie Madill | 31c8c56 | 2015-08-19 14:08:03 -0400 | [diff] [blame] | 1141 | mData.getTransformFeedbackVaryingNames()); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1142 | |
Jamie Madill | 31c8c56 | 2015-08-19 14:08:03 -0400 | [diff] [blame] | 1143 | if (registers < 0) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1144 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1145 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Jamie Madill | ada9ecc | 2015-08-17 12:53:37 -0400 | [diff] [blame] | 1148 | LinkVaryingRegisters(infoLog, vertexShaderD3D, fragmentShaderD3D); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1149 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 1150 | std::vector<gl::LinkedVarying> linkedVaryings; |
Jamie Madill | 4cff247 | 2015-08-21 16:53:18 -0400 | [diff] [blame] | 1151 | if (!mDynamicHLSL->generateShaderLinkHLSL(data, mData, infoLog, registers, mPixelHLSL, |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 1152 | mVertexHLSL, &linkedVaryings, &mPixelShaderKey, |
| 1153 | &mUsesFragDepth)) |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1154 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1155 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 1158 | mUsesPointSize = vertexShaderD3D->usesPointSize(); |
| 1159 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 1160 | initSemanticIndex(); |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 1161 | |
Jamie Madill | ea918db | 2015-08-18 14:48:59 -0400 | [diff] [blame] | 1162 | if (!defineUniforms(infoLog, *data.caps)) |
| 1163 | { |
| 1164 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
| 1165 | } |
| 1166 | |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 1167 | defineUniformBlocks(*data.caps); |
| 1168 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 1169 | gatherTransformFeedbackVaryings(linkedVaryings); |
| 1170 | |
Jamie Madill | 31c8c56 | 2015-08-19 14:08:03 -0400 | [diff] [blame] | 1171 | LinkResult result = compileProgramExecutables(infoLog, registers); |
| 1172 | if (result.error.isError() || !result.linkSuccess) |
| 1173 | { |
| 1174 | infoLog << "Failed to create D3D shaders."; |
| 1175 | return result; |
| 1176 | } |
| 1177 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1178 | return LinkResult(true, gl::Error(GL_NO_ERROR)); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Jamie Madill | 36cfd6a | 2015-08-18 10:46:20 -0400 | [diff] [blame] | 1181 | GLboolean ProgramD3D::validate(const gl::Caps &caps, gl::InfoLog *infoLog) |
| 1182 | { |
| 1183 | applyUniforms(); |
| 1184 | return validateSamplers(infoLog, caps); |
| 1185 | } |
| 1186 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1187 | void ProgramD3D::initializeUniformStorage() |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1188 | { |
| 1189 | // Compute total default block size |
| 1190 | unsigned int vertexRegisters = 0; |
| 1191 | unsigned int fragmentRegisters = 0; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1192 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1193 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1194 | const gl::LinkedUniform &uniform = *mUniforms[uniformIndex]; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1195 | |
Geoff Lang | 2ec386b | 2014-12-03 14:44:38 -0500 | [diff] [blame] | 1196 | if (!gl::IsSamplerType(uniform.type)) |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1197 | { |
| 1198 | if (uniform.isReferencedByVertexShader()) |
| 1199 | { |
| 1200 | vertexRegisters = std::max(vertexRegisters, uniform.vsRegisterIndex + uniform.registerCount); |
| 1201 | } |
| 1202 | if (uniform.isReferencedByFragmentShader()) |
| 1203 | { |
| 1204 | fragmentRegisters = std::max(fragmentRegisters, uniform.psRegisterIndex + uniform.registerCount); |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | mVertexUniformStorage = mRenderer->createUniformStorage(vertexRegisters * 16u); |
| 1210 | mFragmentUniformStorage = mRenderer->createUniformStorage(fragmentRegisters * 16u); |
| 1211 | } |
| 1212 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1213 | gl::Error ProgramD3D::applyUniforms() |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1214 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1215 | updateSamplerMapping(); |
| 1216 | |
| 1217 | gl::Error error = mRenderer->applyUniforms(*this, mUniforms); |
| 1218 | if (error.isError()) |
| 1219 | { |
| 1220 | return error; |
| 1221 | } |
| 1222 | |
| 1223 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
| 1224 | { |
| 1225 | mUniforms[uniformIndex]->dirty = false; |
| 1226 | } |
| 1227 | |
| 1228 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Jamie Madill | d1fe164 | 2015-08-21 16:26:04 -0400 | [diff] [blame] | 1231 | gl::Error ProgramD3D::applyUniformBuffers(const gl::Data &data) |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1232 | { |
Jamie Madill | 03260fa | 2015-06-22 13:57:22 -0400 | [diff] [blame] | 1233 | mVertexUBOCache.clear(); |
| 1234 | mFragmentUBOCache.clear(); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1235 | |
| 1236 | const unsigned int reservedBuffersInVS = mRenderer->getReservedVertexUniformBuffers(); |
| 1237 | const unsigned int reservedBuffersInFS = mRenderer->getReservedFragmentUniformBuffers(); |
| 1238 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1239 | for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < mUniformBlocks.size(); uniformBlockIndex++) |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1240 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1241 | gl::UniformBlock *uniformBlock = mUniformBlocks[uniformBlockIndex]; |
Jamie Madill | d1fe164 | 2015-08-21 16:26:04 -0400 | [diff] [blame] | 1242 | GLuint blockBinding = mData.getUniformBlockBinding(uniformBlockIndex); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1243 | |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1244 | ASSERT(uniformBlock); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1245 | |
| 1246 | // Unnecessary to apply an unreferenced standard or shared UBO |
| 1247 | if (!uniformBlock->isReferencedByVertexShader() && !uniformBlock->isReferencedByFragmentShader()) |
| 1248 | { |
| 1249 | continue; |
| 1250 | } |
| 1251 | |
| 1252 | if (uniformBlock->isReferencedByVertexShader()) |
| 1253 | { |
| 1254 | unsigned int registerIndex = uniformBlock->vsRegisterIndex - reservedBuffersInVS; |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1255 | ASSERT(registerIndex < data.caps->maxVertexUniformBlocks); |
Jamie Madill | 03260fa | 2015-06-22 13:57:22 -0400 | [diff] [blame] | 1256 | |
Jamie Madill | 969194d | 2015-07-20 14:36:56 -0400 | [diff] [blame] | 1257 | if (mVertexUBOCache.size() <= registerIndex) |
Jamie Madill | 03260fa | 2015-06-22 13:57:22 -0400 | [diff] [blame] | 1258 | { |
| 1259 | mVertexUBOCache.resize(registerIndex + 1, -1); |
| 1260 | } |
| 1261 | |
| 1262 | ASSERT(mVertexUBOCache[registerIndex] == -1); |
| 1263 | mVertexUBOCache[registerIndex] = blockBinding; |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | if (uniformBlock->isReferencedByFragmentShader()) |
| 1267 | { |
| 1268 | unsigned int registerIndex = uniformBlock->psRegisterIndex - reservedBuffersInFS; |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1269 | ASSERT(registerIndex < data.caps->maxFragmentUniformBlocks); |
Jamie Madill | 03260fa | 2015-06-22 13:57:22 -0400 | [diff] [blame] | 1270 | |
| 1271 | if (mFragmentUBOCache.size() <= registerIndex) |
| 1272 | { |
| 1273 | mFragmentUBOCache.resize(registerIndex + 1, -1); |
| 1274 | } |
| 1275 | |
| 1276 | ASSERT(mFragmentUBOCache[registerIndex] == -1); |
| 1277 | mFragmentUBOCache[registerIndex] = blockBinding; |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
Jamie Madill | 03260fa | 2015-06-22 13:57:22 -0400 | [diff] [blame] | 1281 | return mRenderer->setUniformBuffers(data, mVertexUBOCache, mFragmentUBOCache); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 1284 | void ProgramD3D::assignUniformBlockRegister(gl::UniformBlock *uniformBlock, |
| 1285 | GLenum shader, |
| 1286 | unsigned int registerIndex, |
| 1287 | const gl::Caps &caps) |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1288 | { |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 1289 | // Validation done in the GL-level Program. |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1290 | if (shader == GL_VERTEX_SHADER) |
| 1291 | { |
| 1292 | uniformBlock->vsRegisterIndex = registerIndex; |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 1293 | ASSERT(registerIndex < caps.maxVertexUniformBlocks); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1294 | } |
| 1295 | else if (shader == GL_FRAGMENT_SHADER) |
| 1296 | { |
| 1297 | uniformBlock->psRegisterIndex = registerIndex; |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 1298 | ASSERT(registerIndex < caps.maxFragmentUniformBlocks); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1299 | } |
| 1300 | else UNREACHABLE(); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1303 | void ProgramD3D::dirtyAllUniforms() |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1304 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1305 | unsigned int numUniforms = static_cast<unsigned int>(mUniforms.size()); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1306 | for (unsigned int index = 0; index < numUniforms; index++) |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1307 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1308 | mUniforms[index]->dirty = true; |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1309 | } |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | void ProgramD3D::setUniform1fv(GLint location, GLsizei count, const GLfloat* v) |
| 1313 | { |
| 1314 | setUniform(location, count, v, GL_FLOAT); |
| 1315 | } |
| 1316 | |
| 1317 | void ProgramD3D::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 1318 | { |
| 1319 | setUniform(location, count, v, GL_FLOAT_VEC2); |
| 1320 | } |
| 1321 | |
| 1322 | void ProgramD3D::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 1323 | { |
| 1324 | setUniform(location, count, v, GL_FLOAT_VEC3); |
| 1325 | } |
| 1326 | |
| 1327 | void ProgramD3D::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 1328 | { |
| 1329 | setUniform(location, count, v, GL_FLOAT_VEC4); |
| 1330 | } |
| 1331 | |
| 1332 | void ProgramD3D::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1333 | { |
| 1334 | setUniformMatrixfv<2, 2>(location, count, transpose, value, GL_FLOAT_MAT2); |
| 1335 | } |
| 1336 | |
| 1337 | void ProgramD3D::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1338 | { |
| 1339 | setUniformMatrixfv<3, 3>(location, count, transpose, value, GL_FLOAT_MAT3); |
| 1340 | } |
| 1341 | |
| 1342 | void ProgramD3D::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1343 | { |
| 1344 | setUniformMatrixfv<4, 4>(location, count, transpose, value, GL_FLOAT_MAT4); |
| 1345 | } |
| 1346 | |
| 1347 | void ProgramD3D::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1348 | { |
| 1349 | setUniformMatrixfv<2, 3>(location, count, transpose, value, GL_FLOAT_MAT2x3); |
| 1350 | } |
| 1351 | |
| 1352 | void ProgramD3D::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1353 | { |
| 1354 | setUniformMatrixfv<3, 2>(location, count, transpose, value, GL_FLOAT_MAT3x2); |
| 1355 | } |
| 1356 | |
| 1357 | void ProgramD3D::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1358 | { |
| 1359 | setUniformMatrixfv<2, 4>(location, count, transpose, value, GL_FLOAT_MAT2x4); |
| 1360 | } |
| 1361 | |
| 1362 | void ProgramD3D::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1363 | { |
| 1364 | setUniformMatrixfv<4, 2>(location, count, transpose, value, GL_FLOAT_MAT4x2); |
| 1365 | } |
| 1366 | |
| 1367 | void ProgramD3D::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1368 | { |
| 1369 | setUniformMatrixfv<3, 4>(location, count, transpose, value, GL_FLOAT_MAT3x4); |
| 1370 | } |
| 1371 | |
| 1372 | void ProgramD3D::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 1373 | { |
| 1374 | setUniformMatrixfv<4, 3>(location, count, transpose, value, GL_FLOAT_MAT4x3); |
| 1375 | } |
| 1376 | |
| 1377 | void ProgramD3D::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 1378 | { |
| 1379 | setUniform(location, count, v, GL_INT); |
| 1380 | } |
| 1381 | |
| 1382 | void ProgramD3D::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 1383 | { |
| 1384 | setUniform(location, count, v, GL_INT_VEC2); |
| 1385 | } |
| 1386 | |
| 1387 | void ProgramD3D::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 1388 | { |
| 1389 | setUniform(location, count, v, GL_INT_VEC3); |
| 1390 | } |
| 1391 | |
| 1392 | void ProgramD3D::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 1393 | { |
| 1394 | setUniform(location, count, v, GL_INT_VEC4); |
| 1395 | } |
| 1396 | |
| 1397 | void ProgramD3D::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 1398 | { |
| 1399 | setUniform(location, count, v, GL_UNSIGNED_INT); |
| 1400 | } |
| 1401 | |
| 1402 | void ProgramD3D::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 1403 | { |
| 1404 | setUniform(location, count, v, GL_UNSIGNED_INT_VEC2); |
| 1405 | } |
| 1406 | |
| 1407 | void ProgramD3D::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 1408 | { |
| 1409 | setUniform(location, count, v, GL_UNSIGNED_INT_VEC3); |
| 1410 | } |
| 1411 | |
| 1412 | void ProgramD3D::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 1413 | { |
| 1414 | setUniform(location, count, v, GL_UNSIGNED_INT_VEC4); |
| 1415 | } |
| 1416 | |
| 1417 | void ProgramD3D::getUniformfv(GLint location, GLfloat *params) |
| 1418 | { |
| 1419 | getUniformv(location, params, GL_FLOAT); |
| 1420 | } |
| 1421 | |
| 1422 | void ProgramD3D::getUniformiv(GLint location, GLint *params) |
| 1423 | { |
| 1424 | getUniformv(location, params, GL_INT); |
| 1425 | } |
| 1426 | |
| 1427 | void ProgramD3D::getUniformuiv(GLint location, GLuint *params) |
| 1428 | { |
| 1429 | getUniformv(location, params, GL_UNSIGNED_INT); |
| 1430 | } |
| 1431 | |
Jamie Madill | ea918db | 2015-08-18 14:48:59 -0400 | [diff] [blame] | 1432 | bool ProgramD3D::defineUniforms(gl::InfoLog &infoLog, const gl::Caps &caps) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1433 | { |
Jamie Madill | ea918db | 2015-08-18 14:48:59 -0400 | [diff] [blame] | 1434 | const gl::Shader *vertexShader = mData.getAttachedVertexShader(); |
| 1435 | const std::vector<sh::Uniform> &vertexUniforms = vertexShader->getUniforms(); |
| 1436 | const ShaderD3D *vertexShaderD3D = GetImplAs<ShaderD3D>(vertexShader); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1437 | |
Jamie Madill | ea918db | 2015-08-18 14:48:59 -0400 | [diff] [blame] | 1438 | for (const sh::Uniform &uniform : vertexUniforms) |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1439 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1440 | if (uniform.staticUse) |
| 1441 | { |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1442 | unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX : |
| 1443 | vertexShaderD3D->getUniformRegister(uniform.name); |
| 1444 | defineUniformBase(vertexShaderD3D, uniform, registerBase); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | |
Jamie Madill | ea918db | 2015-08-18 14:48:59 -0400 | [diff] [blame] | 1448 | const gl::Shader *fragmentShader = mData.getAttachedFragmentShader(); |
| 1449 | const std::vector<sh::Uniform> &fragmentUniforms = fragmentShader->getUniforms(); |
| 1450 | const ShaderD3D *fragmentShaderD3D = GetImplAs<ShaderD3D>(fragmentShader); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1451 | |
Jamie Madill | ea918db | 2015-08-18 14:48:59 -0400 | [diff] [blame] | 1452 | for (const sh::Uniform &uniform : fragmentUniforms) |
| 1453 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1454 | if (uniform.staticUse) |
| 1455 | { |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1456 | unsigned int registerBase = uniform.isBuiltIn() ? GL_INVALID_INDEX : |
| 1457 | fragmentShaderD3D->getUniformRegister(uniform.name); |
| 1458 | defineUniformBase(fragmentShaderD3D, uniform, registerBase); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
Jamie Madill | ea918db | 2015-08-18 14:48:59 -0400 | [diff] [blame] | 1462 | // TODO(jmadill): move the validation part to gl::Program |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1463 | if (!indexUniforms(infoLog, caps)) |
| 1464 | { |
| 1465 | return false; |
| 1466 | } |
| 1467 | |
| 1468 | initializeUniformStorage(); |
| 1469 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1470 | return true; |
| 1471 | } |
| 1472 | |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 1473 | void ProgramD3D::defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1474 | { |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1475 | if (uniformRegister == GL_INVALID_INDEX) |
| 1476 | { |
| 1477 | defineUniform(shader, uniform, uniform.name, nullptr); |
| 1478 | return; |
| 1479 | } |
| 1480 | |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 1481 | ShShaderOutput outputType = shader->getCompilerOutputType(); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1482 | sh::HLSLBlockEncoder encoder(sh::HLSLBlockEncoder::GetStrategyFor(outputType)); |
| 1483 | encoder.skipRegisters(uniformRegister); |
| 1484 | |
| 1485 | defineUniform(shader, uniform, uniform.name, &encoder); |
| 1486 | } |
| 1487 | |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 1488 | void ProgramD3D::defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform, |
| 1489 | const std::string &fullName, sh::HLSLBlockEncoder *encoder) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1490 | { |
| 1491 | if (uniform.isStruct()) |
| 1492 | { |
| 1493 | for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++) |
| 1494 | { |
| 1495 | const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : ""); |
| 1496 | |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1497 | if (encoder) |
| 1498 | encoder->enterAggregateType(); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1499 | |
| 1500 | for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++) |
| 1501 | { |
| 1502 | const sh::ShaderVariable &field = uniform.fields[fieldIndex]; |
| 1503 | const std::string &fieldFullName = (fullName + elementString + "." + field.name); |
| 1504 | |
| 1505 | defineUniform(shader, field, fieldFullName, encoder); |
| 1506 | } |
| 1507 | |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1508 | if (encoder) |
| 1509 | encoder->exitAggregateType(); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | else // Not a struct |
| 1513 | { |
| 1514 | // Arrays are treated as aggregate types |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1515 | if (uniform.isArray() && encoder) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1516 | { |
| 1517 | encoder->enterAggregateType(); |
| 1518 | } |
| 1519 | |
| 1520 | gl::LinkedUniform *linkedUniform = getUniformByName(fullName); |
| 1521 | |
Jamie Madill | 2857f48 | 2015-02-09 15:35:29 -0500 | [diff] [blame] | 1522 | // Advance the uniform offset, to track registers allocation for structs |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1523 | sh::BlockMemberInfo blockInfo = encoder ? |
| 1524 | encoder->encodeType(uniform.type, uniform.arraySize, false) : |
| 1525 | sh::BlockMemberInfo::getDefaultBlockInfo(); |
Jamie Madill | 2857f48 | 2015-02-09 15:35:29 -0500 | [diff] [blame] | 1526 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1527 | if (!linkedUniform) |
| 1528 | { |
| 1529 | linkedUniform = new gl::LinkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize, |
Jamie Madill | 2857f48 | 2015-02-09 15:35:29 -0500 | [diff] [blame] | 1530 | -1, sh::BlockMemberInfo::getDefaultBlockInfo()); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1531 | ASSERT(linkedUniform); |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1532 | |
| 1533 | if (encoder) |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1534 | linkedUniform->registerElement = static_cast<unsigned int>( |
| 1535 | sh::HLSLBlockEncoder::getBlockRegisterElement(blockInfo)); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1536 | mUniforms.push_back(linkedUniform); |
| 1537 | } |
| 1538 | |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1539 | if (encoder) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1540 | { |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1541 | if (shader->getShaderType() == GL_FRAGMENT_SHADER) |
| 1542 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1543 | linkedUniform->psRegisterIndex = |
| 1544 | static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo)); |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1545 | } |
| 1546 | else if (shader->getShaderType() == GL_VERTEX_SHADER) |
| 1547 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1548 | linkedUniform->vsRegisterIndex = |
| 1549 | static_cast<unsigned int>(sh::HLSLBlockEncoder::getBlockRegister(blockInfo)); |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1550 | } |
| 1551 | else UNREACHABLE(); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1552 | } |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1553 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1554 | // Arrays are treated as aggregate types |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 1555 | if (uniform.isArray() && encoder) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1556 | { |
| 1557 | encoder->exitAggregateType(); |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 1562 | void ProgramD3D::defineUniformBlocks(const gl::Caps &caps) |
| 1563 | { |
| 1564 | const gl::Shader *vertexShader = mData.getAttachedVertexShader(); |
| 1565 | |
| 1566 | for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks()) |
| 1567 | { |
| 1568 | if (vertexBlock.staticUse || vertexBlock.layout != sh::BLOCKLAYOUT_PACKED) |
| 1569 | { |
| 1570 | defineUniformBlock(*vertexShader, vertexBlock, caps); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | const gl::Shader *fragmentShader = mData.getAttachedFragmentShader(); |
| 1575 | |
| 1576 | for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks()) |
| 1577 | { |
| 1578 | if (fragmentBlock.staticUse || fragmentBlock.layout != sh::BLOCKLAYOUT_PACKED) |
| 1579 | { |
| 1580 | defineUniformBlock(*fragmentShader, fragmentBlock, caps); |
| 1581 | } |
| 1582 | } |
| 1583 | } |
| 1584 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1585 | template <typename T> |
| 1586 | static inline void SetIfDirty(T *dest, const T& source, bool *dirtyFlag) |
| 1587 | { |
| 1588 | ASSERT(dest != NULL); |
| 1589 | ASSERT(dirtyFlag != NULL); |
| 1590 | |
| 1591 | *dirtyFlag = *dirtyFlag || (memcmp(dest, &source, sizeof(T)) != 0); |
| 1592 | *dest = source; |
| 1593 | } |
| 1594 | |
| 1595 | template <typename T> |
| 1596 | void ProgramD3D::setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType) |
| 1597 | { |
| 1598 | const int components = gl::VariableComponentCount(targetUniformType); |
| 1599 | const GLenum targetBoolType = gl::VariableBoolVectorType(targetUniformType); |
| 1600 | |
| 1601 | gl::LinkedUniform *targetUniform = getUniformByLocation(location); |
| 1602 | |
| 1603 | int elementCount = targetUniform->elementCount(); |
| 1604 | |
| 1605 | count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
| 1606 | |
| 1607 | if (targetUniform->type == targetUniformType) |
| 1608 | { |
| 1609 | T *target = reinterpret_cast<T*>(targetUniform->data) + mUniformIndex[location].element * 4; |
| 1610 | |
| 1611 | for (int i = 0; i < count; i++) |
| 1612 | { |
| 1613 | T *dest = target + (i * 4); |
| 1614 | const T *source = v + (i * components); |
| 1615 | |
| 1616 | for (int c = 0; c < components; c++) |
| 1617 | { |
| 1618 | SetIfDirty(dest + c, source[c], &targetUniform->dirty); |
| 1619 | } |
| 1620 | for (int c = components; c < 4; c++) |
| 1621 | { |
| 1622 | SetIfDirty(dest + c, T(0), &targetUniform->dirty); |
| 1623 | } |
| 1624 | } |
| 1625 | } |
| 1626 | else if (targetUniform->type == targetBoolType) |
| 1627 | { |
| 1628 | GLint *boolParams = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4; |
| 1629 | |
| 1630 | for (int i = 0; i < count; i++) |
| 1631 | { |
| 1632 | GLint *dest = boolParams + (i * 4); |
| 1633 | const T *source = v + (i * components); |
| 1634 | |
| 1635 | for (int c = 0; c < components; c++) |
| 1636 | { |
| 1637 | SetIfDirty(dest + c, (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE, &targetUniform->dirty); |
| 1638 | } |
| 1639 | for (int c = components; c < 4; c++) |
| 1640 | { |
| 1641 | SetIfDirty(dest + c, GL_FALSE, &targetUniform->dirty); |
| 1642 | } |
| 1643 | } |
| 1644 | } |
Geoff Lang | 2ec386b | 2014-12-03 14:44:38 -0500 | [diff] [blame] | 1645 | else if (gl::IsSamplerType(targetUniform->type)) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1646 | { |
| 1647 | ASSERT(targetUniformType == GL_INT); |
| 1648 | |
| 1649 | GLint *target = reinterpret_cast<GLint*>(targetUniform->data) + mUniformIndex[location].element * 4; |
| 1650 | |
| 1651 | bool wasDirty = targetUniform->dirty; |
| 1652 | |
| 1653 | for (int i = 0; i < count; i++) |
| 1654 | { |
| 1655 | GLint *dest = target + (i * 4); |
| 1656 | const GLint *source = reinterpret_cast<const GLint*>(v) + (i * components); |
| 1657 | |
| 1658 | SetIfDirty(dest + 0, source[0], &targetUniform->dirty); |
| 1659 | SetIfDirty(dest + 1, 0, &targetUniform->dirty); |
| 1660 | SetIfDirty(dest + 2, 0, &targetUniform->dirty); |
| 1661 | SetIfDirty(dest + 3, 0, &targetUniform->dirty); |
| 1662 | } |
| 1663 | |
| 1664 | if (!wasDirty && targetUniform->dirty) |
| 1665 | { |
| 1666 | mDirtySamplerMapping = true; |
| 1667 | } |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1668 | } |
| 1669 | else UNREACHABLE(); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1670 | } |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 1671 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1672 | template<typename T> |
| 1673 | bool transposeMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight) |
| 1674 | { |
| 1675 | bool dirty = false; |
| 1676 | int copyWidth = std::min(targetHeight, srcWidth); |
| 1677 | int copyHeight = std::min(targetWidth, srcHeight); |
| 1678 | |
| 1679 | for (int x = 0; x < copyWidth; x++) |
| 1680 | { |
| 1681 | for (int y = 0; y < copyHeight; y++) |
| 1682 | { |
| 1683 | SetIfDirty(target + (x * targetWidth + y), static_cast<T>(value[y * srcWidth + x]), &dirty); |
| 1684 | } |
| 1685 | } |
| 1686 | // clear unfilled right side |
| 1687 | for (int y = 0; y < copyWidth; y++) |
| 1688 | { |
| 1689 | for (int x = copyHeight; x < targetWidth; x++) |
| 1690 | { |
| 1691 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty); |
| 1692 | } |
| 1693 | } |
| 1694 | // clear unfilled bottom. |
| 1695 | for (int y = copyWidth; y < targetHeight; y++) |
| 1696 | { |
| 1697 | for (int x = 0; x < targetWidth; x++) |
| 1698 | { |
| 1699 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty); |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | return dirty; |
| 1704 | } |
| 1705 | |
| 1706 | template<typename T> |
| 1707 | bool expandMatrix(T *target, const GLfloat *value, int targetWidth, int targetHeight, int srcWidth, int srcHeight) |
| 1708 | { |
| 1709 | bool dirty = false; |
| 1710 | int copyWidth = std::min(targetWidth, srcWidth); |
| 1711 | int copyHeight = std::min(targetHeight, srcHeight); |
| 1712 | |
| 1713 | for (int y = 0; y < copyHeight; y++) |
| 1714 | { |
| 1715 | for (int x = 0; x < copyWidth; x++) |
| 1716 | { |
| 1717 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(value[y * srcWidth + x]), &dirty); |
| 1718 | } |
| 1719 | } |
| 1720 | // clear unfilled right side |
| 1721 | for (int y = 0; y < copyHeight; y++) |
| 1722 | { |
| 1723 | for (int x = copyWidth; x < targetWidth; x++) |
| 1724 | { |
| 1725 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty); |
| 1726 | } |
| 1727 | } |
| 1728 | // clear unfilled bottom. |
| 1729 | for (int y = copyHeight; y < targetHeight; y++) |
| 1730 | { |
| 1731 | for (int x = 0; x < targetWidth; x++) |
| 1732 | { |
| 1733 | SetIfDirty(target + (y * targetWidth + x), static_cast<T>(0), &dirty); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | return dirty; |
| 1738 | } |
| 1739 | |
| 1740 | template <int cols, int rows> |
| 1741 | void ProgramD3D::setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType) |
| 1742 | { |
| 1743 | gl::LinkedUniform *targetUniform = getUniformByLocation(location); |
| 1744 | |
| 1745 | int elementCount = targetUniform->elementCount(); |
| 1746 | |
| 1747 | count = std::min(elementCount - (int)mUniformIndex[location].element, count); |
| 1748 | const unsigned int targetMatrixStride = (4 * rows); |
| 1749 | GLfloat *target = (GLfloat*)(targetUniform->data + mUniformIndex[location].element * sizeof(GLfloat) * targetMatrixStride); |
| 1750 | |
| 1751 | for (int i = 0; i < count; i++) |
| 1752 | { |
| 1753 | // Internally store matrices as transposed versions to accomodate HLSL matrix indexing |
| 1754 | if (transpose == GL_FALSE) |
| 1755 | { |
| 1756 | targetUniform->dirty = transposeMatrix<GLfloat>(target, value, 4, rows, rows, cols) || targetUniform->dirty; |
| 1757 | } |
| 1758 | else |
| 1759 | { |
| 1760 | targetUniform->dirty = expandMatrix<GLfloat>(target, value, 4, rows, cols, rows) || targetUniform->dirty; |
| 1761 | } |
| 1762 | target += targetMatrixStride; |
| 1763 | value += cols * rows; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | template <typename T> |
| 1768 | void ProgramD3D::getUniformv(GLint location, T *params, GLenum uniformType) |
| 1769 | { |
| 1770 | gl::LinkedUniform *targetUniform = mUniforms[mUniformIndex[location].index]; |
| 1771 | |
| 1772 | if (gl::IsMatrixType(targetUniform->type)) |
| 1773 | { |
| 1774 | const int rows = gl::VariableRowCount(targetUniform->type); |
| 1775 | const int cols = gl::VariableColumnCount(targetUniform->type); |
| 1776 | transposeMatrix(params, (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4 * rows, rows, cols, 4, rows); |
| 1777 | } |
| 1778 | else if (uniformType == gl::VariableComponentType(targetUniform->type)) |
| 1779 | { |
| 1780 | unsigned int size = gl::VariableComponentCount(targetUniform->type); |
| 1781 | memcpy(params, targetUniform->data + mUniformIndex[location].element * 4 * sizeof(T), |
| 1782 | size * sizeof(T)); |
| 1783 | } |
| 1784 | else |
| 1785 | { |
| 1786 | unsigned int size = gl::VariableComponentCount(targetUniform->type); |
| 1787 | switch (gl::VariableComponentType(targetUniform->type)) |
| 1788 | { |
| 1789 | case GL_BOOL: |
| 1790 | { |
| 1791 | GLint *boolParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
| 1792 | |
| 1793 | for (unsigned int i = 0; i < size; i++) |
| 1794 | { |
| 1795 | params[i] = (boolParams[i] == GL_FALSE) ? static_cast<T>(0) : static_cast<T>(1); |
| 1796 | } |
| 1797 | } |
| 1798 | break; |
| 1799 | |
| 1800 | case GL_FLOAT: |
| 1801 | { |
| 1802 | GLfloat *floatParams = (GLfloat*)targetUniform->data + mUniformIndex[location].element * 4; |
| 1803 | |
| 1804 | for (unsigned int i = 0; i < size; i++) |
| 1805 | { |
| 1806 | params[i] = static_cast<T>(floatParams[i]); |
| 1807 | } |
| 1808 | } |
| 1809 | break; |
| 1810 | |
| 1811 | case GL_INT: |
| 1812 | { |
| 1813 | GLint *intParams = (GLint*)targetUniform->data + mUniformIndex[location].element * 4; |
| 1814 | |
| 1815 | for (unsigned int i = 0; i < size; i++) |
| 1816 | { |
| 1817 | params[i] = static_cast<T>(intParams[i]); |
| 1818 | } |
| 1819 | } |
| 1820 | break; |
| 1821 | |
| 1822 | case GL_UNSIGNED_INT: |
| 1823 | { |
| 1824 | GLuint *uintParams = (GLuint*)targetUniform->data + mUniformIndex[location].element * 4; |
| 1825 | |
| 1826 | for (unsigned int i = 0; i < size; i++) |
| 1827 | { |
| 1828 | params[i] = static_cast<T>(uintParams[i]); |
| 1829 | } |
| 1830 | } |
| 1831 | break; |
| 1832 | |
| 1833 | default: UNREACHABLE(); |
| 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | template <typename VarT> |
| 1839 | void ProgramD3D::defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex, |
| 1840 | sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes, |
| 1841 | bool inRowMajorLayout) |
| 1842 | { |
| 1843 | for (unsigned int uniformIndex = 0; uniformIndex < fields.size(); uniformIndex++) |
| 1844 | { |
| 1845 | const VarT &field = fields[uniformIndex]; |
| 1846 | const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name); |
| 1847 | |
| 1848 | if (field.isStruct()) |
| 1849 | { |
| 1850 | bool rowMajorLayout = (inRowMajorLayout || IsRowMajorLayout(field)); |
| 1851 | |
| 1852 | for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++) |
| 1853 | { |
| 1854 | encoder->enterAggregateType(); |
| 1855 | |
| 1856 | const std::string uniformElementName = fieldName + (field.isArray() ? ArrayString(arrayElement) : ""); |
| 1857 | defineUniformBlockMembers(field.fields, uniformElementName, blockIndex, encoder, blockUniformIndexes, rowMajorLayout); |
| 1858 | |
| 1859 | encoder->exitAggregateType(); |
| 1860 | } |
| 1861 | } |
| 1862 | else |
| 1863 | { |
| 1864 | bool isRowMajorMatrix = (gl::IsMatrixType(field.type) && inRowMajorLayout); |
| 1865 | |
| 1866 | sh::BlockMemberInfo memberInfo = encoder->encodeType(field.type, field.arraySize, isRowMajorMatrix); |
| 1867 | |
| 1868 | gl::LinkedUniform *newUniform = new gl::LinkedUniform(field.type, field.precision, fieldName, field.arraySize, |
| 1869 | blockIndex, memberInfo); |
| 1870 | |
| 1871 | // add to uniform list, but not index, since uniform block uniforms have no location |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1872 | blockUniformIndexes->push_back(static_cast<GLenum>(mUniforms.size())); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1873 | mUniforms.push_back(newUniform); |
| 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 1878 | void ProgramD3D::defineUniformBlock(const gl::Shader &shader, |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 1879 | const sh::InterfaceBlock &interfaceBlock, |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1880 | const gl::Caps &caps) |
| 1881 | { |
Jamie Madill | f4bf381 | 2015-04-01 16:15:32 -0400 | [diff] [blame] | 1882 | const ShaderD3D* shaderD3D = GetImplAs<ShaderD3D>(&shader); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1883 | |
| 1884 | // create uniform block entries if they do not exist |
| 1885 | if (getUniformBlockIndex(interfaceBlock.name) == GL_INVALID_INDEX) |
| 1886 | { |
| 1887 | std::vector<unsigned int> blockUniformIndexes; |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1888 | const unsigned int blockIndex = static_cast<unsigned int>(mUniformBlocks.size()); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1889 | |
| 1890 | // define member uniforms |
| 1891 | sh::BlockLayoutEncoder *encoder = NULL; |
| 1892 | |
| 1893 | if (interfaceBlock.layout == sh::BLOCKLAYOUT_STANDARD) |
| 1894 | { |
| 1895 | encoder = new sh::Std140BlockEncoder; |
| 1896 | } |
| 1897 | else |
| 1898 | { |
| 1899 | encoder = new sh::HLSLBlockEncoder(sh::HLSLBlockEncoder::ENCODE_PACKED); |
| 1900 | } |
| 1901 | ASSERT(encoder); |
| 1902 | |
| 1903 | defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, encoder, &blockUniformIndexes, interfaceBlock.isRowMajorLayout); |
| 1904 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1905 | unsigned int dataSize = static_cast<unsigned int>(encoder->getBlockSize()); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1906 | |
| 1907 | // create all the uniform blocks |
| 1908 | if (interfaceBlock.arraySize > 0) |
| 1909 | { |
| 1910 | for (unsigned int uniformBlockElement = 0; uniformBlockElement < interfaceBlock.arraySize; uniformBlockElement++) |
| 1911 | { |
| 1912 | gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, uniformBlockElement, dataSize); |
| 1913 | newUniformBlock->memberUniformIndexes = blockUniformIndexes; |
| 1914 | mUniformBlocks.push_back(newUniformBlock); |
| 1915 | } |
| 1916 | } |
| 1917 | else |
| 1918 | { |
| 1919 | gl::UniformBlock *newUniformBlock = new gl::UniformBlock(interfaceBlock.name, GL_INVALID_INDEX, dataSize); |
| 1920 | newUniformBlock->memberUniformIndexes = blockUniformIndexes; |
| 1921 | mUniformBlocks.push_back(newUniformBlock); |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | if (interfaceBlock.staticUse) |
| 1926 | { |
| 1927 | // Assign registers to the uniform blocks |
| 1928 | const GLuint blockIndex = getUniformBlockIndex(interfaceBlock.name); |
| 1929 | const unsigned int elementCount = std::max(1u, interfaceBlock.arraySize); |
| 1930 | ASSERT(blockIndex != GL_INVALID_INDEX); |
| 1931 | ASSERT(blockIndex + elementCount <= mUniformBlocks.size()); |
| 1932 | |
| 1933 | unsigned int interfaceBlockRegister = shaderD3D->getInterfaceBlockRegister(interfaceBlock.name); |
| 1934 | |
| 1935 | for (unsigned int uniformBlockElement = 0; uniformBlockElement < elementCount; uniformBlockElement++) |
| 1936 | { |
| 1937 | gl::UniformBlock *uniformBlock = mUniformBlocks[blockIndex + uniformBlockElement]; |
| 1938 | ASSERT(uniformBlock->name == interfaceBlock.name); |
| 1939 | |
Jamie Madill | e473dee | 2015-08-18 14:49:01 -0400 | [diff] [blame] | 1940 | assignUniformBlockRegister(uniformBlock, shader.getType(), |
| 1941 | interfaceBlockRegister + uniformBlockElement, caps); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1942 | } |
| 1943 | } |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | bool ProgramD3D::assignSamplers(unsigned int startSamplerIndex, |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 1947 | GLenum samplerType, |
| 1948 | unsigned int samplerCount, |
| 1949 | std::vector<Sampler> &outSamplers, |
| 1950 | GLuint *outUsedRange) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1951 | { |
| 1952 | unsigned int samplerIndex = startSamplerIndex; |
| 1953 | |
| 1954 | do |
| 1955 | { |
| 1956 | if (samplerIndex < outSamplers.size()) |
| 1957 | { |
| 1958 | Sampler& sampler = outSamplers[samplerIndex]; |
| 1959 | sampler.active = true; |
| 1960 | sampler.textureType = GetTextureType(samplerType); |
| 1961 | sampler.logicalTextureUnit = 0; |
| 1962 | *outUsedRange = std::max(samplerIndex + 1, *outUsedRange); |
| 1963 | } |
| 1964 | else |
| 1965 | { |
| 1966 | return false; |
| 1967 | } |
| 1968 | |
| 1969 | samplerIndex++; |
| 1970 | } while (samplerIndex < startSamplerIndex + samplerCount); |
| 1971 | |
| 1972 | return true; |
| 1973 | } |
| 1974 | |
| 1975 | bool ProgramD3D::indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps) |
| 1976 | { |
Geoff Lang | 2ec386b | 2014-12-03 14:44:38 -0500 | [diff] [blame] | 1977 | ASSERT(gl::IsSamplerType(uniform.type)); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1978 | ASSERT(uniform.vsRegisterIndex != GL_INVALID_INDEX || uniform.psRegisterIndex != GL_INVALID_INDEX); |
| 1979 | |
| 1980 | if (uniform.vsRegisterIndex != GL_INVALID_INDEX) |
| 1981 | { |
| 1982 | if (!assignSamplers(uniform.vsRegisterIndex, uniform.type, uniform.arraySize, mSamplersVS, |
| 1983 | &mUsedVertexSamplerRange)) |
| 1984 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1985 | infoLog << "Vertex shader sampler count exceeds the maximum vertex texture units (" |
| 1986 | << mSamplersVS.size() << ")."; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1987 | return false; |
| 1988 | } |
| 1989 | |
| 1990 | unsigned int maxVertexVectors = mRenderer->getReservedVertexUniformVectors() + caps.maxVertexUniformVectors; |
| 1991 | if (uniform.vsRegisterIndex + uniform.registerCount > maxVertexVectors) |
| 1992 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1993 | infoLog << "Vertex shader active uniforms exceed GL_MAX_VERTEX_UNIFORM_VECTORS (" |
| 1994 | << caps.maxVertexUniformVectors << ")."; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 1995 | return false; |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | if (uniform.psRegisterIndex != GL_INVALID_INDEX) |
| 2000 | { |
| 2001 | if (!assignSamplers(uniform.psRegisterIndex, uniform.type, uniform.arraySize, mSamplersPS, |
| 2002 | &mUsedPixelSamplerRange)) |
| 2003 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2004 | infoLog << "Pixel shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (" |
| 2005 | << mSamplersPS.size() << ")."; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 2006 | return false; |
| 2007 | } |
| 2008 | |
| 2009 | unsigned int maxFragmentVectors = mRenderer->getReservedFragmentUniformVectors() + caps.maxFragmentUniformVectors; |
| 2010 | if (uniform.psRegisterIndex + uniform.registerCount > maxFragmentVectors) |
| 2011 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 2012 | infoLog << "Fragment shader active uniforms exceed GL_MAX_FRAGMENT_UNIFORM_VECTORS (" |
| 2013 | << caps.maxFragmentUniformVectors << ")."; |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 2014 | return false; |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | return true; |
| 2019 | } |
| 2020 | |
| 2021 | bool ProgramD3D::indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps) |
| 2022 | { |
| 2023 | for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); uniformIndex++) |
| 2024 | { |
| 2025 | const gl::LinkedUniform &uniform = *mUniforms[uniformIndex]; |
| 2026 | |
Geoff Lang | 2ec386b | 2014-12-03 14:44:38 -0500 | [diff] [blame] | 2027 | if (gl::IsSamplerType(uniform.type)) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 2028 | { |
| 2029 | if (!indexSamplerUniform(uniform, infoLog, caps)) |
| 2030 | { |
| 2031 | return false; |
| 2032 | } |
| 2033 | } |
| 2034 | |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 2035 | for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++) |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 2036 | { |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 2037 | if (!uniform.isBuiltIn()) |
| 2038 | { |
Geoff Lang | 9513784 | 2015-06-02 15:38:43 -0400 | [diff] [blame] | 2039 | // Assign in-order uniform locations |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 2040 | mUniformIndex[static_cast<GLuint>(mUniformIndex.size())] = gl::VariableLocation( |
| 2041 | uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)); |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 2042 | } |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | return true; |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 2049 | void ProgramD3D::reset() |
| 2050 | { |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 2051 | ProgramImpl::reset(); |
| 2052 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 2053 | SafeDeleteContainer(mVertexExecutables); |
| 2054 | SafeDeleteContainer(mPixelExecutables); |
| 2055 | SafeDelete(mGeometryExecutable); |
| 2056 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 2057 | mVertexHLSL.clear(); |
Geoff Lang | 6941a55 | 2015-07-27 11:06:45 -0400 | [diff] [blame] | 2058 | mVertexWorkarounds = D3DCompilerWorkarounds(); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 2059 | mShaderVersion = 100; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 2060 | |
| 2061 | mPixelHLSL.clear(); |
Geoff Lang | 6941a55 | 2015-07-27 11:06:45 -0400 | [diff] [blame] | 2062 | mPixelWorkarounds = D3DCompilerWorkarounds(); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 2063 | mUsesFragDepth = false; |
| 2064 | mPixelShaderKey.clear(); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 2065 | mUsesPointSize = false; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 2066 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 2067 | SafeDelete(mVertexUniformStorage); |
| 2068 | SafeDelete(mFragmentUniformStorage); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 2069 | |
| 2070 | mSamplersPS.clear(); |
| 2071 | mSamplersVS.clear(); |
| 2072 | |
| 2073 | mUsedVertexSamplerRange = 0; |
| 2074 | mUsedPixelSamplerRange = 0; |
| 2075 | mDirtySamplerMapping = true; |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 2076 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2077 | std::fill(mSemanticIndexes, mSemanticIndexes + ArraySize(mSemanticIndexes), -1); |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 2078 | std::fill(mAttributesByLayout, mAttributesByLayout + ArraySize(mAttributesByLayout), -1); |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2079 | |
| 2080 | mTransformFeedbackLinkedVaryings.clear(); |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 2083 | unsigned int ProgramD3D::getSerial() const |
| 2084 | { |
| 2085 | return mSerial; |
| 2086 | } |
| 2087 | |
| 2088 | unsigned int ProgramD3D::issueSerial() |
| 2089 | { |
| 2090 | return mCurrentSerial++; |
| 2091 | } |
| 2092 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2093 | void ProgramD3D::initSemanticIndex() |
| 2094 | { |
| 2095 | const gl::Shader *vertexShader = mData.getAttachedVertexShader(); |
| 2096 | ASSERT(vertexShader != nullptr); |
| 2097 | |
| 2098 | // Init semantic index |
| 2099 | for (const sh::Attribute &attribute : mData.getAttributes()) |
| 2100 | { |
| 2101 | int attributeIndex = attribute.location; |
| 2102 | int index = vertexShader->getSemanticIndex(attribute.name); |
| 2103 | int regs = gl::VariableRegisterCount(attribute.type); |
| 2104 | |
| 2105 | for (int reg = 0; reg < regs; ++reg) |
| 2106 | { |
| 2107 | mSemanticIndexes[attributeIndex + reg] = index + reg; |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | initAttributesByLayout(); |
| 2112 | } |
| 2113 | |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 2114 | void ProgramD3D::initAttributesByLayout() |
| 2115 | { |
| 2116 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 2117 | { |
| 2118 | mAttributesByLayout[i] = i; |
| 2119 | } |
| 2120 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2121 | std::sort(&mAttributesByLayout[0], &mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS], |
| 2122 | AttributeSorter(mSemanticIndexes)); |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 2123 | } |
| 2124 | |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 2125 | void ProgramD3D::sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes, |
Jamie Madill | f9327d3 | 2015-06-22 13:57:16 -0400 | [diff] [blame] | 2126 | int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS], |
| 2127 | const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 2128 | { |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 2129 | for (size_t attribIndex = 0; attribIndex < unsortedAttributes.size(); ++attribIndex) |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 2130 | { |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 2131 | int oldIndex = mAttributesByLayout[attribIndex]; |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2132 | sortedSemanticIndicesOut[attribIndex] = mSemanticIndexes[oldIndex]; |
Jamie Madill | 476682e | 2015-06-30 10:04:29 -0400 | [diff] [blame] | 2133 | sortedAttributesOut[attribIndex] = &unsortedAttributes[oldIndex]; |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2137 | void ProgramD3D::updateCachedInputLayout(const gl::State &state) |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 2138 | { |
Jamie Madill | bd136f9 | 2015-08-10 14:51:37 -0400 | [diff] [blame] | 2139 | mCachedInputLayout.clear(); |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 2140 | const auto &vertexAttributes = state.getVertexArray()->getVertexAttributes(); |
Jamie Madill | f8dd7b1 | 2015-08-05 13:50:08 -0400 | [diff] [blame] | 2141 | |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 2142 | for (unsigned int attributeIndex = 0; attributeIndex < vertexAttributes.size(); attributeIndex++) |
| 2143 | { |
Jamie Madill | 63805b4 | 2015-08-25 13:17:39 -0400 | [diff] [blame] | 2144 | int semanticIndex = mSemanticIndexes[attributeIndex]; |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 2145 | |
| 2146 | if (semanticIndex != -1) |
| 2147 | { |
Jamie Madill | bd136f9 | 2015-08-10 14:51:37 -0400 | [diff] [blame] | 2148 | if (mCachedInputLayout.size() < static_cast<size_t>(semanticIndex + 1)) |
| 2149 | { |
| 2150 | mCachedInputLayout.resize(semanticIndex + 1, gl::VERTEX_FORMAT_INVALID); |
| 2151 | } |
Jamie Madill | d3dfda2 | 2015-07-06 08:28:49 -0400 | [diff] [blame] | 2152 | mCachedInputLayout[semanticIndex] = |
| 2153 | GetVertexFormatType(vertexAttributes[attributeIndex], |
| 2154 | state.getVertexAttribCurrentValue(attributeIndex).Type); |
| 2155 | } |
| 2156 | } |
| 2157 | } |
| 2158 | |
Jamie Madill | ccdf74b | 2015-08-18 10:46:12 -0400 | [diff] [blame] | 2159 | void ProgramD3D::gatherTransformFeedbackVaryings( |
| 2160 | const std::vector<gl::LinkedVarying> &linkedVaryings) |
| 2161 | { |
| 2162 | // Gather the linked varyings that are used for transform feedback, they should all exist. |
| 2163 | mTransformFeedbackLinkedVaryings.clear(); |
| 2164 | for (const std::string &tfVaryingName : mData.getTransformFeedbackVaryingNames()) |
| 2165 | { |
| 2166 | for (const gl::LinkedVarying &linkedVarying : linkedVaryings) |
| 2167 | { |
| 2168 | if (tfVaryingName == linkedVarying.name) |
| 2169 | { |
| 2170 | mTransformFeedbackLinkedVaryings.push_back(linkedVarying); |
| 2171 | break; |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | } |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 2176 | } |