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