daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 1 | // |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 7 | // Texture.cpp: Implements the gl::Texture class. [OpenGL ES 2.0.24] section 3.7 page 63. |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 8 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 9 | #include "libANGLE/Texture.h" |
Geoff Lang | 0b7eef7 | 2014-06-12 14:10:47 -0400 | [diff] [blame] | 10 | |
| 11 | #include "common/mathutil.h" |
| 12 | #include "common/utilities.h" |
Jamie Madill | 79481d6 | 2015-04-14 08:13:47 -0400 | [diff] [blame] | 13 | #include "libANGLE/Config.h" |
| 14 | #include "libANGLE/Data.h" |
| 15 | #include "libANGLE/Surface.h" |
| 16 | #include "libANGLE/formatutils.h" |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 17 | |
| 18 | namespace gl |
| 19 | { |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 20 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 21 | bool IsMipmapFiltered(const gl::SamplerState &samplerState) |
| 22 | { |
| 23 | switch (samplerState.minFilter) |
| 24 | { |
| 25 | case GL_NEAREST: |
| 26 | case GL_LINEAR: |
| 27 | return false; |
| 28 | case GL_NEAREST_MIPMAP_NEAREST: |
| 29 | case GL_LINEAR_MIPMAP_NEAREST: |
| 30 | case GL_NEAREST_MIPMAP_LINEAR: |
| 31 | case GL_LINEAR_MIPMAP_LINEAR: |
| 32 | return true; |
| 33 | default: UNREACHABLE(); |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | bool IsPointSampled(const gl::SamplerState &samplerState) |
| 39 | { |
| 40 | return (samplerState.magFilter == GL_NEAREST && (samplerState.minFilter == GL_NEAREST || samplerState.minFilter == GL_NEAREST_MIPMAP_NEAREST)); |
| 41 | } |
| 42 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 43 | static size_t GetImageDescIndex(GLenum target, size_t level) |
| 44 | { |
| 45 | return IsCubeMapTextureTarget(target) ? ((level * 6) + CubeMapTextureTargetToLayerIndex(target)) : level; |
| 46 | } |
| 47 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 48 | Texture::Texture(rx::TextureImpl *impl, GLuint id, GLenum target) |
Jamie Madill | 79481d6 | 2015-04-14 08:13:47 -0400 | [diff] [blame] | 49 | : FramebufferAttachmentObject(id), |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 50 | mTexture(impl), |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 51 | mUsage(GL_NONE), |
Jamie Madill | 6948e30 | 2014-10-20 17:04:33 -0400 | [diff] [blame] | 52 | mImmutableLevelCount(0), |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 53 | mTarget(target), |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 54 | mImageDescs(IMPLEMENTATION_MAX_TEXTURE_LEVELS * (target == GL_TEXTURE_CUBE_MAP ? 6 : 1)), |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 55 | mCompletenessCache(), |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 56 | mBoundSurface(NULL) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 57 | { |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | Texture::~Texture() |
| 61 | { |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 62 | if (mBoundSurface) |
| 63 | { |
| 64 | mBoundSurface->releaseTexImage(EGL_BACK_BUFFER); |
| 65 | mBoundSurface = NULL; |
| 66 | } |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 67 | SafeDelete(mTexture); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Geoff Lang | 4907f2c | 2013-07-25 12:53:57 -0400 | [diff] [blame] | 70 | GLenum Texture::getTarget() const |
| 71 | { |
| 72 | return mTarget; |
| 73 | } |
| 74 | |
Geoff Lang | 63b5f1f | 2013-09-23 14:52:14 -0400 | [diff] [blame] | 75 | void Texture::setUsage(GLenum usage) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 76 | { |
Geoff Lang | 63b5f1f | 2013-09-23 14:52:14 -0400 | [diff] [blame] | 77 | mUsage = usage; |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 78 | getImplementation()->setUsage(usage); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 79 | } |
| 80 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 81 | GLenum Texture::getUsage() const |
| 82 | { |
| 83 | return mUsage; |
| 84 | } |
| 85 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 86 | size_t Texture::getWidth(GLenum target, size_t level) const |
Jamie Madill | d3d2a34 | 2013-10-07 10:46:35 -0400 | [diff] [blame] | 87 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 88 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 89 | return getImageDesc(target, level).size.width; |
Jamie Madill | d3d2a34 | 2013-10-07 10:46:35 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 92 | size_t Texture::getHeight(GLenum target, size_t level) const |
Jamie Madill | d3d2a34 | 2013-10-07 10:46:35 -0400 | [diff] [blame] | 93 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 94 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 95 | return getImageDesc(target, level).size.height; |
Jamie Madill | d3d2a34 | 2013-10-07 10:46:35 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 98 | size_t Texture::getDepth(GLenum target, size_t level) const |
Jamie Madill | d3d2a34 | 2013-10-07 10:46:35 -0400 | [diff] [blame] | 99 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 100 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 101 | return getImageDesc(target, level).size.depth; |
Jamie Madill | d3d2a34 | 2013-10-07 10:46:35 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 104 | GLenum Texture::getInternalFormat(GLenum target, size_t level) const |
Jamie Madill | d3d2a34 | 2013-10-07 10:46:35 -0400 | [diff] [blame] | 105 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 106 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 107 | return getImageDesc(target, level).internalFormat; |
Jamie Madill | 945f732 | 2014-09-03 15:07:14 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 110 | bool Texture::isSamplerComplete(const SamplerState &samplerState, const Data &data) const |
| 111 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 112 | const ImageDesc &baseImageDesc = getImageDesc(getBaseImageTarget(), samplerState.baseLevel); |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 113 | const TextureCaps &textureCaps = data.textureCaps->get(baseImageDesc.internalFormat); |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 114 | if (!mCompletenessCache.cacheValid || |
| 115 | mCompletenessCache.samplerState != samplerState || |
| 116 | mCompletenessCache.filterable != textureCaps.filterable || |
| 117 | mCompletenessCache.clientVersion != data.clientVersion || |
| 118 | mCompletenessCache.supportsNPOT != data.extensions->textureNPOT) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 119 | { |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 120 | mCompletenessCache.cacheValid = true; |
| 121 | mCompletenessCache.samplerState = samplerState; |
| 122 | mCompletenessCache.filterable = textureCaps.filterable; |
| 123 | mCompletenessCache.clientVersion = data.clientVersion; |
| 124 | mCompletenessCache.supportsNPOT = data.extensions->textureNPOT; |
| 125 | mCompletenessCache.samplerComplete = computeSamplerCompleteness(samplerState, data); |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 126 | } |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 127 | return mCompletenessCache.samplerComplete; |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Tests for cube texture completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81. |
| 131 | bool Texture::isCubeComplete() const |
| 132 | { |
| 133 | ASSERT(mTarget == GL_TEXTURE_CUBE_MAP); |
| 134 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 135 | const ImageDesc &baseImageDesc = getImageDesc(FirstCubeMapTextureTarget, 0); |
| 136 | if (baseImageDesc.size.width == 0 || baseImageDesc.size.width != baseImageDesc.size.height) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 137 | { |
| 138 | return false; |
| 139 | } |
| 140 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 141 | for (GLenum face = FirstCubeMapTextureTarget + 1; face <= LastCubeMapTextureTarget; face++) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 142 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 143 | const ImageDesc &faceImageDesc = getImageDesc(face, 0); |
| 144 | if (faceImageDesc.size.width != baseImageDesc.size.width || |
| 145 | faceImageDesc.size.height != baseImageDesc.size.height || |
| 146 | faceImageDesc.internalFormat != baseImageDesc.internalFormat) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 147 | { |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 155 | bool Texture::isImmutable() const |
| 156 | { |
Jamie Madill | 6948e30 | 2014-10-20 17:04:33 -0400 | [diff] [blame] | 157 | return (mImmutableLevelCount > 0); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | int Texture::immutableLevelCount() |
| 161 | { |
Jamie Madill | 6948e30 | 2014-10-20 17:04:33 -0400 | [diff] [blame] | 162 | return mImmutableLevelCount; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 165 | Error Texture::setImage(GLenum target, size_t level, GLenum internalFormat, const Extents &size, GLenum format, GLenum type, |
| 166 | const PixelUnpackState &unpack, const uint8_t *pixels) |
| 167 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 168 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 169 | |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 170 | // Release from previous calls to eglBindTexImage, to avoid calling the Impl after |
| 171 | releaseTexImageInternal(); |
| 172 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 173 | Error error = mTexture->setImage(target, level, internalFormat, size, format, type, unpack, pixels); |
| 174 | if (error.isError()) |
| 175 | { |
| 176 | return error; |
| 177 | } |
| 178 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 179 | setImageDesc(target, level, ImageDesc(size, GetSizedInternalFormat(internalFormat, type))); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 180 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 181 | return Error(GL_NO_ERROR); |
| 182 | } |
| 183 | |
| 184 | Error Texture::setSubImage(GLenum target, size_t level, const Box &area, GLenum format, GLenum type, |
| 185 | const PixelUnpackState &unpack, const uint8_t *pixels) |
| 186 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 187 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 188 | |
| 189 | return mTexture->setSubImage(target, level, area, format, type, unpack, pixels); |
| 190 | } |
| 191 | |
| 192 | Error Texture::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const Extents &size, |
| 193 | const PixelUnpackState &unpack, const uint8_t *pixels) |
| 194 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 195 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 196 | |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 197 | // Release from previous calls to eglBindTexImage, to avoid calling the Impl after |
| 198 | releaseTexImageInternal(); |
| 199 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 200 | Error error = mTexture->setCompressedImage(target, level, internalFormat, size, unpack, pixels); |
| 201 | if (error.isError()) |
| 202 | { |
| 203 | return error; |
| 204 | } |
| 205 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 206 | setImageDesc(target, level, ImageDesc(size, GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE))); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 207 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 208 | return Error(GL_NO_ERROR); |
| 209 | } |
| 210 | |
| 211 | Error Texture::setCompressedSubImage(GLenum target, size_t level, const Box &area, GLenum format, |
| 212 | const PixelUnpackState &unpack, const uint8_t *pixels) |
| 213 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 214 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 215 | |
| 216 | return mTexture->setCompressedSubImage(target, level, area, format, unpack, pixels); |
| 217 | } |
| 218 | |
| 219 | Error Texture::copyImage(GLenum target, size_t level, const Rectangle &sourceArea, GLenum internalFormat, |
| 220 | const Framebuffer *source) |
| 221 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 222 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 223 | |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 224 | // Release from previous calls to eglBindTexImage, to avoid calling the Impl after |
| 225 | releaseTexImageInternal(); |
| 226 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 227 | Error error = mTexture->copyImage(target, level, sourceArea, internalFormat, source); |
| 228 | if (error.isError()) |
| 229 | { |
| 230 | return error; |
| 231 | } |
| 232 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 233 | setImageDesc(target, level, ImageDesc(Extents(sourceArea.width, sourceArea.height, 1), |
| 234 | GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE))); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 235 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 236 | return Error(GL_NO_ERROR); |
| 237 | } |
| 238 | |
| 239 | Error Texture::copySubImage(GLenum target, size_t level, const Offset &destOffset, const Rectangle &sourceArea, |
| 240 | const Framebuffer *source) |
| 241 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 242 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 243 | |
| 244 | return mTexture->copySubImage(target, level, destOffset, sourceArea, source); |
| 245 | } |
| 246 | |
| 247 | Error Texture::setStorage(GLenum target, size_t levels, GLenum internalFormat, const Extents &size) |
| 248 | { |
| 249 | ASSERT(target == mTarget); |
| 250 | |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 251 | // Release from previous calls to eglBindTexImage, to avoid calling the Impl after |
| 252 | releaseTexImageInternal(); |
| 253 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 254 | Error error = mTexture->setStorage(target, levels, internalFormat, size); |
| 255 | if (error.isError()) |
| 256 | { |
| 257 | return error; |
| 258 | } |
| 259 | |
| 260 | mImmutableLevelCount = levels; |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 261 | clearImageDescs(); |
| 262 | setImageDescChain(levels, size, internalFormat); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 263 | |
| 264 | return Error(GL_NO_ERROR); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | Error Texture::generateMipmaps() |
| 269 | { |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 270 | // Release from previous calls to eglBindTexImage, to avoid calling the Impl after |
| 271 | releaseTexImageInternal(); |
| 272 | |
Gregoire Payen de La Garanderie | 752ce19 | 2015-04-14 11:11:12 +0100 | [diff] [blame] | 273 | Error error = mTexture->generateMipmaps(getSamplerState()); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 274 | if (error.isError()) |
| 275 | { |
| 276 | return error; |
| 277 | } |
| 278 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 279 | const ImageDesc &baseImageInfo = getImageDesc(getBaseImageTarget(), 0); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 280 | size_t mipLevels = log2(std::max(std::max(baseImageInfo.size.width, baseImageInfo.size.height), baseImageInfo.size.depth)) + 1; |
| 281 | setImageDescChain(mipLevels, baseImageInfo.size, baseImageInfo.internalFormat); |
| 282 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 283 | return Error(GL_NO_ERROR); |
| 284 | } |
| 285 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 286 | void Texture::setImageDescChain(size_t levels, Extents baseSize, GLenum sizedInternalFormat) |
| 287 | { |
| 288 | for (size_t level = 0; level < levels; level++) |
| 289 | { |
| 290 | Extents levelSize(std::max<size_t>(baseSize.width >> level, 1), |
| 291 | std::max<size_t>(baseSize.height >> level, 1), |
| 292 | (mTarget == GL_TEXTURE_2D_ARRAY) ? baseSize.depth : std::max<size_t>(baseSize.depth >> level, 1)); |
| 293 | ImageDesc levelInfo(levelSize, sizedInternalFormat); |
| 294 | |
| 295 | if (mTarget == GL_TEXTURE_CUBE_MAP) |
| 296 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 297 | for (size_t face = FirstCubeMapTextureTarget; face <= LastCubeMapTextureTarget; face++) |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 298 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 299 | setImageDesc(face, level, levelInfo); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | else |
| 303 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 304 | setImageDesc(mTarget, level, levelInfo); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 309 | Texture::ImageDesc::ImageDesc() |
| 310 | : ImageDesc(Extents(0, 0, 0), GL_NONE) |
| 311 | { |
| 312 | } |
| 313 | |
| 314 | Texture::ImageDesc::ImageDesc(const Extents &size, GLenum internalFormat) |
| 315 | : size(size), |
| 316 | internalFormat(internalFormat) |
| 317 | { |
| 318 | } |
| 319 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 320 | const Texture::ImageDesc &Texture::getImageDesc(GLenum target, size_t level) const |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 321 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 322 | size_t descIndex = GetImageDescIndex(target, level); |
| 323 | ASSERT(descIndex < mImageDescs.size()); |
| 324 | return mImageDescs[descIndex]; |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 325 | } |
| 326 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 327 | void Texture::setImageDesc(GLenum target, size_t level, const ImageDesc &desc) |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 328 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 329 | size_t descIndex = GetImageDescIndex(target, level); |
| 330 | ASSERT(descIndex < mImageDescs.size()); |
| 331 | mImageDescs[descIndex] = desc; |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 332 | mCompletenessCache.cacheValid = false; |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 333 | } |
| 334 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 335 | void Texture::clearImageDesc(GLenum target, size_t level) |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 336 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 337 | setImageDesc(target, level, ImageDesc()); |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 340 | void Texture::clearImageDescs() |
| 341 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 342 | for (size_t descIndex = 0; descIndex < mImageDescs.size(); descIndex++) |
| 343 | { |
| 344 | mImageDescs[descIndex] = ImageDesc(); |
| 345 | } |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 346 | mCompletenessCache.cacheValid = false; |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 349 | void Texture::bindTexImageFromSurface(egl::Surface *surface) |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 350 | { |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 351 | ASSERT(surface); |
| 352 | |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 353 | if (mBoundSurface) |
| 354 | { |
| 355 | releaseTexImageFromSurface(); |
| 356 | } |
| 357 | |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 358 | mTexture->bindTexImage(surface); |
| 359 | mBoundSurface = surface; |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 360 | |
| 361 | // Set the image info to the size and format of the surface |
| 362 | ASSERT(mTarget == GL_TEXTURE_2D); |
| 363 | Extents size(surface->getWidth(), surface->getHeight(), 1); |
Geoff Lang | c223dc6 | 2015-01-09 13:10:01 -0500 | [diff] [blame] | 364 | ImageDesc desc(size, surface->getConfig()->renderTargetFormat); |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 365 | setImageDesc(mTarget, 0, desc); |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 368 | void Texture::releaseTexImageFromSurface() |
| 369 | { |
| 370 | ASSERT(mBoundSurface); |
| 371 | mBoundSurface = nullptr; |
| 372 | mTexture->releaseTexImage(); |
| 373 | |
| 374 | // Erase the image info for level 0 |
| 375 | ASSERT(mTarget == GL_TEXTURE_2D); |
| 376 | clearImageDesc(mTarget, 0); |
| 377 | } |
| 378 | |
| 379 | void Texture::releaseTexImageInternal() |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 380 | { |
| 381 | if (mBoundSurface) |
| 382 | { |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 383 | // Notify the surface |
| 384 | mBoundSurface->releaseTexImageFromTexture(); |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 385 | |
Jamie Madill | bb714f7 | 2015-05-20 10:22:18 -0400 | [diff] [blame^] | 386 | // Then, call the same method as from the surface |
| 387 | releaseTexImageFromSurface(); |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 391 | GLenum Texture::getBaseImageTarget() const |
Jamie Madill | 22f843a | 2013-10-24 17:49:36 -0400 | [diff] [blame] | 392 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 393 | return mTarget == GL_TEXTURE_CUBE_MAP ? FirstCubeMapTextureTarget : mTarget; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 396 | size_t Texture::getExpectedMipLevels() const |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 397 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 398 | const ImageDesc &baseImageDesc = getImageDesc(getBaseImageTarget(), 0); |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 399 | if (mTarget == GL_TEXTURE_3D) |
| 400 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 401 | return log2(std::max(std::max(baseImageDesc.size.width, baseImageDesc.size.height), baseImageDesc.size.depth)) + 1; |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 402 | } |
| 403 | else |
| 404 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 405 | return log2(std::max(baseImageDesc.size.width, baseImageDesc.size.height)) + 1; |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 406 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 409 | bool Texture::computeSamplerCompleteness(const SamplerState &samplerState, const Data &data) const |
| 410 | { |
| 411 | const ImageDesc &baseImageDesc = getImageDesc(getBaseImageTarget(), samplerState.baseLevel); |
| 412 | if (baseImageDesc.size.width == 0 || baseImageDesc.size.height == 0 || baseImageDesc.size.depth == 0) |
| 413 | { |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | if (mTarget == GL_TEXTURE_CUBE_MAP && baseImageDesc.size.width != baseImageDesc.size.height) |
| 418 | { |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | const TextureCaps &textureCaps = data.textureCaps->get(baseImageDesc.internalFormat); |
| 423 | if (!textureCaps.filterable && !IsPointSampled(samplerState)) |
| 424 | { |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | bool npotSupport = data.extensions->textureNPOT || data.clientVersion >= 3; |
| 429 | if (!npotSupport) |
| 430 | { |
| 431 | if ((samplerState.wrapS != GL_CLAMP_TO_EDGE && !gl::isPow2(baseImageDesc.size.width)) || |
| 432 | (samplerState.wrapT != GL_CLAMP_TO_EDGE && !gl::isPow2(baseImageDesc.size.height))) |
| 433 | { |
| 434 | return false; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if (IsMipmapFiltered(samplerState)) |
| 439 | { |
| 440 | if (!npotSupport) |
| 441 | { |
| 442 | if (!gl::isPow2(baseImageDesc.size.width) || !gl::isPow2(baseImageDesc.size.height)) |
| 443 | { |
| 444 | return false; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | if (!computeMipmapCompleteness(samplerState)) |
| 449 | { |
| 450 | return false; |
| 451 | } |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | if (mTarget == GL_TEXTURE_CUBE_MAP && !isCubeComplete()) |
| 456 | { |
| 457 | return false; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // OpenGLES 3.0.2 spec section 3.8.13 states that a texture is not mipmap complete if: |
| 462 | // The internalformat specified for the texture arrays is a sized internal depth or |
| 463 | // depth and stencil format (see table 3.13), the value of TEXTURE_COMPARE_- |
| 464 | // MODE is NONE, and either the magnification filter is not NEAREST or the mini- |
| 465 | // fication filter is neither NEAREST nor NEAREST_MIPMAP_NEAREST. |
| 466 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(baseImageDesc.internalFormat); |
| 467 | if (formatInfo.depthBits > 0 && data.clientVersion > 2) |
| 468 | { |
| 469 | if (samplerState.compareMode == GL_NONE) |
| 470 | { |
| 471 | if ((samplerState.minFilter != GL_NEAREST && samplerState.minFilter != GL_NEAREST_MIPMAP_NEAREST) || |
| 472 | samplerState.magFilter != GL_NEAREST) |
| 473 | { |
| 474 | return false; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | bool Texture::computeMipmapCompleteness(const gl::SamplerState &samplerState) const |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 483 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 484 | size_t expectedMipLevels = getExpectedMipLevels(); |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 485 | |
| 486 | size_t maxLevel = std::min<size_t>(expectedMipLevels, samplerState.maxLevel + 1); |
| 487 | |
| 488 | for (size_t level = samplerState.baseLevel; level < maxLevel; level++) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 489 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 490 | if (mTarget == GL_TEXTURE_CUBE_MAP) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 491 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 492 | for (GLenum face = FirstCubeMapTextureTarget; face <= LastCubeMapTextureTarget; face++) |
| 493 | { |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 494 | if (!computeLevelCompleteness(face, level, samplerState)) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 495 | { |
| 496 | return false; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | else |
| 501 | { |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 502 | if (!computeLevelCompleteness(mTarget, level, samplerState)) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 503 | { |
| 504 | return false; |
| 505 | } |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | return true; |
| 510 | } |
| 511 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 512 | |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 513 | bool Texture::computeLevelCompleteness(GLenum target, size_t level, const gl::SamplerState &samplerState) const |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 514 | { |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 515 | ASSERT(level < IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
| 516 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 517 | if (isImmutable()) |
| 518 | { |
| 519 | return true; |
| 520 | } |
| 521 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 522 | const ImageDesc &baseImageDesc = getImageDesc(getBaseImageTarget(), samplerState.baseLevel); |
| 523 | if (baseImageDesc.size.width == 0 || baseImageDesc.size.height == 0 || baseImageDesc.size.depth == 0) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 524 | { |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | // The base image level is complete if the width and height are positive |
| 529 | if (level == 0) |
| 530 | { |
| 531 | return true; |
| 532 | } |
| 533 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 534 | const ImageDesc &levelImageDesc = getImageDesc(target, level); |
| 535 | if (levelImageDesc.internalFormat != baseImageDesc.internalFormat) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 536 | { |
| 537 | return false; |
| 538 | } |
| 539 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 540 | if (levelImageDesc.size.width != std::max(1, baseImageDesc.size.width >> level)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 541 | { |
| 542 | return false; |
| 543 | } |
| 544 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 545 | if (levelImageDesc.size.height != std::max(1, baseImageDesc.size.height >> level)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 546 | { |
| 547 | return false; |
| 548 | } |
| 549 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 550 | if (mTarget == GL_TEXTURE_3D) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 551 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 552 | if (levelImageDesc.size.depth != std::max(1, baseImageDesc.size.depth >> level)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 553 | { |
| 554 | return false; |
| 555 | } |
| 556 | } |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 557 | else if (mTarget == GL_TEXTURE_2D_ARRAY) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 558 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 559 | if (levelImageDesc.size.depth != baseImageDesc.size.depth) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 560 | { |
| 561 | return false; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return true; |
| 566 | } |
| 567 | |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 568 | Texture::SamplerCompletenessCache::SamplerCompletenessCache() |
| 569 | : cacheValid(false), |
| 570 | samplerState(), |
| 571 | filterable(false), |
| 572 | clientVersion(0), |
| 573 | supportsNPOT(false), |
| 574 | samplerComplete(false) |
| 575 | { |
| 576 | } |
| 577 | |
Jamie Madill | 79481d6 | 2015-04-14 08:13:47 -0400 | [diff] [blame] | 578 | GLsizei Texture::getAttachmentWidth(const gl::FramebufferAttachment::Target &target) const |
| 579 | { |
| 580 | return getWidth(target.textureIndex().type, target.textureIndex().mipIndex); |
| 581 | } |
| 582 | |
| 583 | GLsizei Texture::getAttachmentHeight(const gl::FramebufferAttachment::Target &target) const |
| 584 | { |
| 585 | return getHeight(target.textureIndex().type, target.textureIndex().mipIndex); |
| 586 | } |
| 587 | |
| 588 | GLenum Texture::getAttachmentInternalFormat(const gl::FramebufferAttachment::Target &target) const |
| 589 | { |
| 590 | return getInternalFormat(target.textureIndex().type, target.textureIndex().mipIndex); |
| 591 | } |
| 592 | |
| 593 | GLsizei Texture::getAttachmentSamples(const gl::FramebufferAttachment::Target &/*target*/) const |
| 594 | { |
| 595 | // Multisample textures not currently supported |
| 596 | return 0; |
| 597 | } |
| 598 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 599 | } |