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