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 | |
Jamie Madill | 10ef215 | 2014-10-20 17:04:34 -0400 | [diff] [blame] | 44 | unsigned int Texture::mCurrentTextureSerial = 1; |
| 45 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 46 | Texture::Texture(rx::TextureImpl *impl, GLuint id, GLenum target) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 47 | : RefCountObject(id), |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 48 | mTexture(impl), |
Jamie Madill | 10ef215 | 2014-10-20 17:04:34 -0400 | [diff] [blame] | 49 | mTextureSerial(issueTextureSerial()), |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 50 | mUsage(GL_NONE), |
Jamie Madill | 6948e30 | 2014-10-20 17:04:33 -0400 | [diff] [blame] | 51 | mImmutableLevelCount(0), |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 52 | mTarget(target), |
| 53 | mBoundSurface(NULL) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 54 | { |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | Texture::~Texture() |
| 58 | { |
Geoff Lang | 7c973ea | 2014-12-19 15:58:28 -0500 | [diff] [blame] | 59 | if (mBoundSurface) |
| 60 | { |
| 61 | mBoundSurface->releaseTexImage(EGL_BACK_BUFFER); |
| 62 | mBoundSurface = NULL; |
| 63 | } |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 64 | SafeDelete(mTexture); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Geoff Lang | 4907f2c | 2013-07-25 12:53:57 -0400 | [diff] [blame] | 67 | GLenum Texture::getTarget() const |
| 68 | { |
| 69 | return mTarget; |
| 70 | } |
| 71 | |
Geoff Lang | 63b5f1f | 2013-09-23 14:52:14 -0400 | [diff] [blame] | 72 | void Texture::setUsage(GLenum usage) |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 73 | { |
Geoff Lang | 63b5f1f | 2013-09-23 14:52:14 -0400 | [diff] [blame] | 74 | mUsage = usage; |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 75 | getImplementation()->setUsage(usage); |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 76 | } |
| 77 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 78 | GLenum Texture::getUsage() const |
| 79 | { |
| 80 | return mUsage; |
| 81 | } |
| 82 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 83 | size_t Texture::getWidth(GLenum target, size_t level) const |
Jamie Madill | d3d2a34 | 2013-10-07 10:46:35 -0400 | [diff] [blame] | 84 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 85 | ASSERT(target == mTarget || (mTarget == GL_TEXTURE_CUBE_MAP && IsCubeMapTextureTarget(target))); |
Geoff Lang | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 86 | return getImageDesc(ImageIndex::MakeGeneric(target, level)).size.width; |
Jamie Madill | d3d2a34 | 2013-10-07 10:46:35 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 89 | size_t Texture::getHeight(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 | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 92 | return getImageDesc(ImageIndex::MakeGeneric(target, level)).size.height; |
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::getDepth(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 | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 98 | return getImageDesc(ImageIndex::MakeGeneric(target, level)).size.depth; |
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 | GLenum Texture::getInternalFormat(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 | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 104 | return getImageDesc(ImageIndex::MakeGeneric(target, level)).internalFormat; |
Jamie Madill | 945f732 | 2014-09-03 15:07:14 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 107 | bool Texture::isSamplerComplete(const SamplerState &samplerState, const Data &data) const |
| 108 | { |
| 109 | GLenum baseTarget = getBaseImageTarget(); |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 110 | size_t width = getWidth(baseTarget, samplerState.baseLevel); |
| 111 | size_t height = getHeight(baseTarget, samplerState.baseLevel); |
| 112 | size_t depth = getDepth(baseTarget, samplerState.baseLevel); |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 113 | if (width == 0 || height == 0 || depth == 0) |
| 114 | { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | if (mTarget == GL_TEXTURE_CUBE_MAP && width != height) |
| 119 | { |
| 120 | return false; |
| 121 | } |
| 122 | |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 123 | GLenum internalFormat = getInternalFormat(baseTarget, samplerState.baseLevel); |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 124 | const TextureCaps &textureCaps = data.textureCaps->get(internalFormat); |
| 125 | if (!textureCaps.filterable && !IsPointSampled(samplerState)) |
| 126 | { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | bool npotSupport = data.extensions->textureNPOT || data.clientVersion >= 3; |
| 131 | if (!npotSupport) |
| 132 | { |
| 133 | if ((samplerState.wrapS != GL_CLAMP_TO_EDGE && !gl::isPow2(width)) || |
| 134 | (samplerState.wrapT != GL_CLAMP_TO_EDGE && !gl::isPow2(height))) |
| 135 | { |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (IsMipmapFiltered(samplerState)) |
| 141 | { |
| 142 | if (!npotSupport) |
| 143 | { |
| 144 | if (!gl::isPow2(width) || !gl::isPow2(height)) |
| 145 | { |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 150 | if (!isMipmapComplete(samplerState)) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 151 | { |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | if (mTarget == GL_TEXTURE_CUBE_MAP && !isCubeComplete()) |
| 158 | { |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // OpenGLES 3.0.2 spec section 3.8.13 states that a texture is not mipmap complete if: |
| 164 | // The internalformat specified for the texture arrays is a sized internal depth or |
| 165 | // depth and stencil format (see table 3.13), the value of TEXTURE_COMPARE_- |
| 166 | // MODE is NONE, and either the magnification filter is not NEAREST or the mini- |
| 167 | // fication filter is neither NEAREST nor NEAREST_MIPMAP_NEAREST. |
| 168 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat); |
| 169 | if (formatInfo.depthBits > 0 && data.clientVersion > 2) |
| 170 | { |
| 171 | if (samplerState.compareMode == GL_NONE) |
| 172 | { |
| 173 | if ((samplerState.minFilter != GL_NEAREST && samplerState.minFilter != GL_NEAREST_MIPMAP_NEAREST) || |
| 174 | samplerState.magFilter != GL_NEAREST) |
| 175 | { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | // Tests for cube texture completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81. |
| 185 | bool Texture::isCubeComplete() const |
| 186 | { |
| 187 | ASSERT(mTarget == GL_TEXTURE_CUBE_MAP); |
| 188 | |
| 189 | GLenum baseTarget = FirstCubeMapTextureTarget; |
| 190 | size_t width = getWidth(baseTarget, 0); |
| 191 | size_t height = getWidth(baseTarget, 0); |
| 192 | if (width == 0 || width != height) |
| 193 | { |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | GLenum internalFormat = getInternalFormat(baseTarget, 0); |
| 198 | for (GLenum face = baseTarget + 1; face <= LastCubeMapTextureTarget; face++) |
| 199 | { |
| 200 | if (getWidth(face, 0) != width || |
| 201 | getHeight(face, 0) != height || |
| 202 | getInternalFormat(face, 0) != internalFormat) |
| 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 | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 244 | setImageDesc(ImageIndex::MakeGeneric(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 | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 270 | setImageDesc(ImageIndex::MakeGeneric(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 | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 296 | setImageDesc(ImageIndex::MakeGeneric(target, level), ImageDesc(Extents(sourceArea.width, sourceArea.height, 1), |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 297 | GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE))); |
| 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 | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 340 | ImageIndex baseLevel = ImageIndex::MakeGeneric(getBaseImageTarget(), 0); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 341 | const ImageDesc &baseImageInfo = getImageDesc(baseLevel); |
| 342 | size_t mipLevels = log2(std::max(std::max(baseImageInfo.size.width, baseImageInfo.size.height), baseImageInfo.size.depth)) + 1; |
| 343 | setImageDescChain(mipLevels, baseImageInfo.size, baseImageInfo.internalFormat); |
| 344 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 345 | return Error(GL_NO_ERROR); |
| 346 | } |
| 347 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 348 | void Texture::setImageDescChain(size_t levels, Extents baseSize, GLenum sizedInternalFormat) |
| 349 | { |
| 350 | for (size_t level = 0; level < levels; level++) |
| 351 | { |
| 352 | Extents levelSize(std::max<size_t>(baseSize.width >> level, 1), |
| 353 | std::max<size_t>(baseSize.height >> level, 1), |
| 354 | (mTarget == GL_TEXTURE_2D_ARRAY) ? baseSize.depth : std::max<size_t>(baseSize.depth >> level, 1)); |
| 355 | ImageDesc levelInfo(levelSize, sizedInternalFormat); |
| 356 | |
| 357 | if (mTarget == GL_TEXTURE_CUBE_MAP) |
| 358 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 359 | for (size_t face = FirstCubeMapTextureTarget; face <= LastCubeMapTextureTarget; face++) |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 360 | { |
Geoff Lang | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 361 | setImageDesc(ImageIndex::MakeGeneric(face, level), levelInfo); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | else |
| 365 | { |
Geoff Lang | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 366 | setImageDesc(ImageIndex::MakeGeneric(mTarget, level), levelInfo); |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 371 | Texture::ImageDesc::ImageDesc() |
| 372 | : ImageDesc(Extents(0, 0, 0), GL_NONE) |
| 373 | { |
| 374 | } |
| 375 | |
| 376 | Texture::ImageDesc::ImageDesc(const Extents &size, GLenum internalFormat) |
| 377 | : size(size), |
| 378 | internalFormat(internalFormat) |
| 379 | { |
| 380 | } |
| 381 | |
Geoff Lang | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 382 | const Texture::ImageDesc &Texture::getImageDesc(const ImageIndex &index) const |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 383 | { |
| 384 | static const Texture::ImageDesc defaultDesc; |
| 385 | ImageDescMap::const_iterator iter = mImageDescs.find(index); |
| 386 | return (iter != mImageDescs.end()) ? iter->second : defaultDesc; |
| 387 | } |
| 388 | |
Geoff Lang | 909b90c | 2015-01-12 14:07:26 -0500 | [diff] [blame] | 389 | void Texture::setImageDesc(const ImageIndex &index, const ImageDesc &desc) |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 390 | { |
| 391 | mImageDescs[index] = desc; |
| 392 | } |
| 393 | |
Geoff Lang | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 394 | void Texture::clearImageDesc(const ImageIndex &index) |
| 395 | { |
| 396 | ImageDescMap::iterator iter = mImageDescs.find(index); |
| 397 | if (iter != mImageDescs.end()) |
| 398 | { |
| 399 | mImageDescs.erase(iter); |
| 400 | } |
| 401 | } |
| 402 | |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 403 | void Texture::clearImageDescs() |
| 404 | { |
| 405 | mImageDescs.clear(); |
| 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 | b926627 | 2015-01-29 13:25:14 +0000 | [diff] [blame] | 420 | setImageDesc(ImageIndex::MakeGeneric(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); |
| 432 | clearImageDesc(ImageIndex::MakeGeneric(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 | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 443 | GLenum baseTarget = getBaseImageTarget(); |
| 444 | size_t width = getWidth(baseTarget, 0); |
| 445 | size_t height = getHeight(baseTarget, 0); |
| 446 | if (mTarget == GL_TEXTURE_3D) |
| 447 | { |
| 448 | size_t depth = getDepth(baseTarget, 0); |
| 449 | return log2(std::max(std::max(width, height), depth)) + 1; |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | return log2(std::max(width, height)) + 1; |
| 454 | } |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 457 | bool Texture::isMipmapComplete(const gl::SamplerState &samplerState) const |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 458 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 459 | size_t expectedMipLevels = getExpectedMipLevels(); |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 460 | |
| 461 | size_t maxLevel = std::min<size_t>(expectedMipLevels, samplerState.maxLevel + 1); |
| 462 | |
| 463 | for (size_t level = samplerState.baseLevel; level < maxLevel; level++) |
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 | if (mTarget == GL_TEXTURE_CUBE_MAP) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 466 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 467 | for (GLenum face = FirstCubeMapTextureTarget; face <= LastCubeMapTextureTarget; face++) |
| 468 | { |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 469 | if (!isLevelComplete(face, level, samplerState)) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 470 | { |
| 471 | return false; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | else |
| 476 | { |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 477 | if (!isLevelComplete(mTarget, level, samplerState)) |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 478 | { |
| 479 | return false; |
| 480 | } |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | return true; |
| 485 | } |
| 486 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 487 | |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 488 | bool Texture::isLevelComplete(GLenum target, size_t level, |
| 489 | const gl::SamplerState &samplerState) const |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 490 | { |
Geoff Lang | a9be0dc | 2014-12-17 12:34:40 -0500 | [diff] [blame] | 491 | ASSERT(level < IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
| 492 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 493 | if (isImmutable()) |
| 494 | { |
| 495 | return true; |
| 496 | } |
| 497 | |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 498 | size_t width = getWidth(target, samplerState.baseLevel); |
| 499 | size_t height = getHeight(target, samplerState.baseLevel); |
Jamie Madill | 448d3db | 2015-01-28 16:51:10 -0500 | [diff] [blame] | 500 | size_t depth = getDepth(target, samplerState.baseLevel); |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 501 | if (width == 0 || height == 0 || depth == 0) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 502 | { |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | // The base image level is complete if the width and height are positive |
| 507 | if (level == 0) |
| 508 | { |
| 509 | return true; |
| 510 | } |
| 511 | |
Jamie Madill | 19d438d | 2015-01-19 13:42:12 -0500 | [diff] [blame] | 512 | if (getInternalFormat(target, level) != getInternalFormat(target, samplerState.baseLevel)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 513 | { |
| 514 | return false; |
| 515 | } |
| 516 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 517 | if (getWidth(target, level) != std::max<size_t>(1, width >> level)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 518 | { |
| 519 | return false; |
| 520 | } |
| 521 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 522 | if (getHeight(target, level) != std::max<size_t>(1, height >> level)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 523 | { |
| 524 | return false; |
| 525 | } |
| 526 | |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 527 | if (mTarget == GL_TEXTURE_3D) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 528 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 529 | if (getDepth(target, level) != std::max<size_t>(1, depth >> level)) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 530 | { |
| 531 | return false; |
| 532 | } |
| 533 | } |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 534 | else if (mTarget == GL_TEXTURE_2D_ARRAY) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 535 | { |
Geoff Lang | 691e58c | 2014-12-19 17:03:25 -0500 | [diff] [blame] | 536 | if (getDepth(target, level) != depth) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 537 | { |
| 538 | return false; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | return true; |
| 543 | } |
| 544 | |
daniel@transgaming.com | 95a758f | 2012-07-12 15:17:06 +0000 | [diff] [blame] | 545 | } |