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 | |
| 170 | Error error = mTexture->setImage(target, level, internalFormat, size, format, type, unpack, pixels); |
| 171 | if (error.isError()) |
| 172 | { |
| 173 | return error; |
| 174 | } |
| 175 | |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 176 | releaseTexImage(); |
| 177 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 178 | setImageDesc(target, level, ImageDesc(size, GetSizedInternalFormat(internalFormat, type))); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 179 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 180 | return Error(GL_NO_ERROR); |
| 181 | } |
| 182 | |
| 183 | Error Texture::setSubImage(GLenum target, size_t level, const Box &area, GLenum format, GLenum type, |
| 184 | const PixelUnpackState &unpack, const uint8_t *pixels) |
| 185 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 186 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 187 | |
| 188 | return mTexture->setSubImage(target, level, area, format, type, unpack, pixels); |
| 189 | } |
| 190 | |
| 191 | Error Texture::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const Extents &size, |
| 192 | const PixelUnpackState &unpack, const uint8_t *pixels) |
| 193 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 194 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 195 | |
| 196 | Error error = mTexture->setCompressedImage(target, level, internalFormat, size, unpack, pixels); |
| 197 | if (error.isError()) |
| 198 | { |
| 199 | return error; |
| 200 | } |
| 201 | |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 202 | releaseTexImage(); |
| 203 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 204 | setImageDesc(target, level, ImageDesc(size, GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE))); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 205 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 206 | return Error(GL_NO_ERROR); |
| 207 | } |
| 208 | |
| 209 | Error Texture::setCompressedSubImage(GLenum target, size_t level, const Box &area, GLenum format, |
| 210 | const PixelUnpackState &unpack, const uint8_t *pixels) |
| 211 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 212 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 213 | |
| 214 | return mTexture->setCompressedSubImage(target, level, area, format, unpack, pixels); |
| 215 | } |
| 216 | |
| 217 | Error Texture::copyImage(GLenum target, size_t level, const Rectangle &sourceArea, GLenum internalFormat, |
| 218 | const Framebuffer *source) |
| 219 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 220 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 221 | |
| 222 | Error error = mTexture->copyImage(target, level, sourceArea, internalFormat, source); |
| 223 | if (error.isError()) |
| 224 | { |
| 225 | return error; |
| 226 | } |
| 227 | |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 228 | releaseTexImage(); |
| 229 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 230 | setImageDesc(target, level, ImageDesc(Extents(sourceArea.width, sourceArea.height, 1), |
| 231 | GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE))); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 232 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 233 | return Error(GL_NO_ERROR); |
| 234 | } |
| 235 | |
| 236 | Error Texture::copySubImage(GLenum target, size_t level, const Offset &destOffset, const Rectangle &sourceArea, |
| 237 | const Framebuffer *source) |
| 238 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 239 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 240 | |
| 241 | return mTexture->copySubImage(target, level, destOffset, sourceArea, source); |
| 242 | } |
| 243 | |
| 244 | Error Texture::setStorage(GLenum target, size_t levels, GLenum internalFormat, const Extents &size) |
| 245 | { |
| 246 | ASSERT(target == mTarget); |
| 247 | |
| 248 | Error error = mTexture->setStorage(target, levels, internalFormat, size); |
| 249 | if (error.isError()) |
| 250 | { |
| 251 | return error; |
| 252 | } |
| 253 | |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 254 | releaseTexImage(); |
| 255 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 256 | mImmutableLevelCount = levels; |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 257 | clearImageDescs(); |
| 258 | setImageDescChain(levels, size, internalFormat); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 259 | |
| 260 | return Error(GL_NO_ERROR); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | Error Texture::generateMipmaps() |
| 265 | { |
Gregoire Payen de La Garanderie | 752ce19 | 2015-04-14 11:11:12 +0100 | [diff] [blame] | 266 | Error error = mTexture->generateMipmaps(getSamplerState()); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 267 | if (error.isError()) |
| 268 | { |
| 269 | return error; |
| 270 | } |
| 271 | |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 272 | releaseTexImage(); |
| 273 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 274 | const ImageDesc &baseImageInfo = getImageDesc(getBaseImageTarget(), 0); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 275 | size_t mipLevels = log2(std::max(std::max(baseImageInfo.size.width, baseImageInfo.size.height), baseImageInfo.size.depth)) + 1; |
| 276 | setImageDescChain(mipLevels, baseImageInfo.size, baseImageInfo.internalFormat); |
| 277 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 278 | return Error(GL_NO_ERROR); |
| 279 | } |
| 280 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 281 | void Texture::setImageDescChain(size_t levels, Extents baseSize, GLenum sizedInternalFormat) |
| 282 | { |
| 283 | for (size_t level = 0; level < levels; level++) |
| 284 | { |
| 285 | Extents levelSize(std::max<size_t>(baseSize.width >> level, 1), |
| 286 | std::max<size_t>(baseSize.height >> level, 1), |
| 287 | (mTarget == GL_TEXTURE_2D_ARRAY) ? baseSize.depth : std::max<size_t>(baseSize.depth >> level, 1)); |
| 288 | ImageDesc levelInfo(levelSize, sizedInternalFormat); |
| 289 | |
| 290 | if (mTarget == GL_TEXTURE_CUBE_MAP) |
| 291 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 292 | for (size_t face = FirstCubeMapTextureTarget; face <= LastCubeMapTextureTarget; face++) |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 293 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 294 | setImageDesc(face, level, levelInfo); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | else |
| 298 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 299 | setImageDesc(mTarget, level, levelInfo); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 304 | Texture::ImageDesc::ImageDesc() |
| 305 | : ImageDesc(Extents(0, 0, 0), GL_NONE) |
| 306 | { |
| 307 | } |
| 308 | |
| 309 | Texture::ImageDesc::ImageDesc(const Extents &size, GLenum internalFormat) |
| 310 | : size(size), |
| 311 | internalFormat(internalFormat) |
| 312 | { |
| 313 | } |
| 314 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 315 | const Texture::ImageDesc &Texture::getImageDesc(GLenum target, size_t level) const |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 316 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 317 | size_t descIndex = GetImageDescIndex(target, level); |
| 318 | ASSERT(descIndex < mImageDescs.size()); |
| 319 | return mImageDescs[descIndex]; |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 320 | } |
| 321 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 322 | void Texture::setImageDesc(GLenum target, size_t level, const ImageDesc &desc) |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 323 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 324 | size_t descIndex = GetImageDescIndex(target, level); |
| 325 | ASSERT(descIndex < mImageDescs.size()); |
| 326 | mImageDescs[descIndex] = desc; |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 327 | mCompletenessCache.cacheValid = false; |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 328 | } |
| 329 | |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 330 | void Texture::clearImageDesc(GLenum target, size_t level) |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 331 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 332 | setImageDesc(target, level, ImageDesc()); |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 335 | void Texture::clearImageDescs() |
| 336 | { |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 337 | for (size_t descIndex = 0; descIndex < mImageDescs.size(); descIndex++) |
| 338 | { |
| 339 | mImageDescs[descIndex] = ImageDesc(); |
| 340 | } |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 341 | mCompletenessCache.cacheValid = false; |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 344 | void Texture::bindTexImage(egl::Surface *surface) |
| 345 | { |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 346 | ASSERT(surface); |
| 347 | |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 348 | releaseTexImage(); |
| 349 | mTexture->bindTexImage(surface); |
| 350 | mBoundSurface = surface; |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 351 | |
| 352 | // Set the image info to the size and format of the surface |
| 353 | ASSERT(mTarget == GL_TEXTURE_2D); |
| 354 | Extents size(surface->getWidth(), surface->getHeight(), 1); |
Geoff Lang | c223dc6 | 2015-01-09 13:10:01 -0500 | [diff] [blame] | 355 | ImageDesc desc(size, surface->getConfig()->renderTargetFormat); |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 356 | setImageDesc(mTarget, 0, desc); |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void Texture::releaseTexImage() |
| 360 | { |
| 361 | if (mBoundSurface) |
| 362 | { |
| 363 | mBoundSurface = NULL; |
| 364 | mTexture->releaseTexImage(); |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 365 | |
| 366 | // Erase the image info for level 0 |
| 367 | ASSERT(mTarget == GL_TEXTURE_2D); |
Geoff Lang | 866dd2d | 2015-02-10 11:08:32 -0500 | [diff] [blame] | 368 | clearImageDesc(mTarget, 0); |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 372 | GLenum Texture::getBaseImageTarget() const |
Jamie Madill | 22f843a | 2013-10-24 17:49:36 -0400 | [diff] [blame] | 373 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 374 | return mTarget == GL_TEXTURE_CUBE_MAP ? FirstCubeMapTextureTarget : mTarget; |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 377 | size_t Texture::getExpectedMipLevels() const |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 378 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 379 | const ImageDesc &baseImageDesc = getImageDesc(getBaseImageTarget(), 0); |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 380 | if (mTarget == GL_TEXTURE_3D) |
| 381 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 382 | 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] | 383 | } |
| 384 | else |
| 385 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 386 | return log2(std::max(baseImageDesc.size.width, baseImageDesc.size.height)) + 1; |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 387 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 390 | bool Texture::computeSamplerCompleteness(const SamplerState &samplerState, const Data &data) const |
| 391 | { |
| 392 | const ImageDesc &baseImageDesc = getImageDesc(getBaseImageTarget(), samplerState.baseLevel); |
| 393 | if (baseImageDesc.size.width == 0 || baseImageDesc.size.height == 0 || baseImageDesc.size.depth == 0) |
| 394 | { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | if (mTarget == GL_TEXTURE_CUBE_MAP && baseImageDesc.size.width != baseImageDesc.size.height) |
| 399 | { |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | const TextureCaps &textureCaps = data.textureCaps->get(baseImageDesc.internalFormat); |
| 404 | if (!textureCaps.filterable && !IsPointSampled(samplerState)) |
| 405 | { |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | bool npotSupport = data.extensions->textureNPOT || data.clientVersion >= 3; |
| 410 | if (!npotSupport) |
| 411 | { |
| 412 | if ((samplerState.wrapS != GL_CLAMP_TO_EDGE && !gl::isPow2(baseImageDesc.size.width)) || |
| 413 | (samplerState.wrapT != GL_CLAMP_TO_EDGE && !gl::isPow2(baseImageDesc.size.height))) |
| 414 | { |
| 415 | return false; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | if (IsMipmapFiltered(samplerState)) |
| 420 | { |
| 421 | if (!npotSupport) |
| 422 | { |
| 423 | if (!gl::isPow2(baseImageDesc.size.width) || !gl::isPow2(baseImageDesc.size.height)) |
| 424 | { |
| 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (!computeMipmapCompleteness(samplerState)) |
| 430 | { |
| 431 | return false; |
| 432 | } |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | if (mTarget == GL_TEXTURE_CUBE_MAP && !isCubeComplete()) |
| 437 | { |
| 438 | return false; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // OpenGLES 3.0.2 spec section 3.8.13 states that a texture is not mipmap complete if: |
| 443 | // The internalformat specified for the texture arrays is a sized internal depth or |
| 444 | // depth and stencil format (see table 3.13), the value of TEXTURE_COMPARE_- |
| 445 | // MODE is NONE, and either the magnification filter is not NEAREST or the mini- |
| 446 | // fication filter is neither NEAREST nor NEAREST_MIPMAP_NEAREST. |
| 447 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(baseImageDesc.internalFormat); |
| 448 | if (formatInfo.depthBits > 0 && data.clientVersion > 2) |
| 449 | { |
| 450 | if (samplerState.compareMode == GL_NONE) |
| 451 | { |
| 452 | if ((samplerState.minFilter != GL_NEAREST && samplerState.minFilter != GL_NEAREST_MIPMAP_NEAREST) || |
| 453 | samplerState.magFilter != GL_NEAREST) |
| 454 | { |
| 455 | return false; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | bool Texture::computeMipmapCompleteness(const gl::SamplerState &samplerState) const |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 464 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 465 | size_t expectedMipLevels = getExpectedMipLevels(); |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 466 | |
| 467 | size_t maxLevel = std::min<size_t>(expectedMipLevels, samplerState.maxLevel + 1); |
| 468 | |
| 469 | for (size_t level = samplerState.baseLevel; level < maxLevel; level++) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 470 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 471 | if (mTarget == GL_TEXTURE_CUBE_MAP) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 472 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 473 | for (GLenum face = FirstCubeMapTextureTarget; face <= LastCubeMapTextureTarget; face++) |
| 474 | { |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 475 | if (!computeLevelCompleteness(face, level, samplerState)) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 476 | { |
| 477 | return false; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | else |
| 482 | { |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 483 | if (!computeLevelCompleteness(mTarget, level, samplerState)) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 484 | { |
| 485 | return false; |
| 486 | } |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | return true; |
| 491 | } |
| 492 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 493 | |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 494 | 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] | 495 | { |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 496 | ASSERT(level < IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
| 497 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 498 | if (isImmutable()) |
| 499 | { |
| 500 | return true; |
| 501 | } |
| 502 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 503 | const ImageDesc &baseImageDesc = getImageDesc(getBaseImageTarget(), samplerState.baseLevel); |
| 504 | 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] | 505 | { |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | // The base image level is complete if the width and height are positive |
| 510 | if (level == 0) |
| 511 | { |
| 512 | return true; |
| 513 | } |
| 514 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 515 | const ImageDesc &levelImageDesc = getImageDesc(target, level); |
| 516 | if (levelImageDesc.internalFormat != baseImageDesc.internalFormat) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 517 | { |
| 518 | return false; |
| 519 | } |
| 520 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 521 | if (levelImageDesc.size.width != std::max(1, baseImageDesc.size.width >> level)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 522 | { |
| 523 | return false; |
| 524 | } |
| 525 | |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 526 | if (levelImageDesc.size.height != std::max(1, baseImageDesc.size.height >> level)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 527 | { |
| 528 | return false; |
| 529 | } |
| 530 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 531 | if (mTarget == GL_TEXTURE_3D) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 532 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 533 | if (levelImageDesc.size.depth != std::max(1, baseImageDesc.size.depth >> level)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 534 | { |
| 535 | return false; |
| 536 | } |
| 537 | } |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 538 | else if (mTarget == GL_TEXTURE_2D_ARRAY) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 539 | { |
Geoff Lang | b0eecdf | 2015-02-10 11:09:12 -0500 | [diff] [blame] | 540 | if (levelImageDesc.size.depth != baseImageDesc.size.depth) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 541 | { |
| 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return true; |
| 547 | } |
| 548 | |
Geoff Lang | 0fe4053 | 2015-02-10 12:01:27 -0500 | [diff] [blame] | 549 | Texture::SamplerCompletenessCache::SamplerCompletenessCache() |
| 550 | : cacheValid(false), |
| 551 | samplerState(), |
| 552 | filterable(false), |
| 553 | clientVersion(0), |
| 554 | supportsNPOT(false), |
| 555 | samplerComplete(false) |
| 556 | { |
| 557 | } |
| 558 | |
Jamie Madill | 79481d6 | 2015-04-14 08:13:47 -0400 | [diff] [blame] | 559 | GLsizei Texture::getAttachmentWidth(const gl::FramebufferAttachment::Target &target) const |
| 560 | { |
| 561 | return getWidth(target.textureIndex().type, target.textureIndex().mipIndex); |
| 562 | } |
| 563 | |
| 564 | GLsizei Texture::getAttachmentHeight(const gl::FramebufferAttachment::Target &target) const |
| 565 | { |
| 566 | return getHeight(target.textureIndex().type, target.textureIndex().mipIndex); |
| 567 | } |
| 568 | |
| 569 | GLenum Texture::getAttachmentInternalFormat(const gl::FramebufferAttachment::Target &target) const |
| 570 | { |
| 571 | return getInternalFormat(target.textureIndex().type, target.textureIndex().mipIndex); |
| 572 | } |
| 573 | |
| 574 | GLsizei Texture::getAttachmentSamples(const gl::FramebufferAttachment::Target &/*target*/) const |
| 575 | { |
| 576 | // Multisample textures not currently supported |
| 577 | return 0; |
| 578 | } |
| 579 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 580 | } |