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