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