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