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