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