Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2015 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // TextureGL.cpp: Implements the class methods for TextureGL. |
| 8 | |
| 9 | #include "libANGLE/renderer/gl/TextureGL.h" |
| 10 | |
| 11 | #include "common/debug.h" |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 12 | #include "common/utilities.h" |
Jamie Madill | 67102f0 | 2015-03-16 10:41:42 -0400 | [diff] [blame] | 13 | #include "libANGLE/State.h" |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 14 | #include "libANGLE/angletypes.h" |
| 15 | #include "libANGLE/formatutils.h" |
| 16 | #include "libANGLE/renderer/gl/BufferGL.h" |
Geoff Lang | fbfa47c | 2015-03-31 11:26:00 -0400 | [diff] [blame] | 17 | #include "libANGLE/renderer/gl/FramebufferGL.h" |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 18 | #include "libANGLE/renderer/gl/FunctionsGL.h" |
| 19 | #include "libANGLE/renderer/gl/StateManagerGL.h" |
Geoff Lang | 14389cc | 2015-07-23 10:57:20 -0400 | [diff] [blame] | 20 | #include "libANGLE/renderer/gl/WorkaroundsGL.h" |
Geoff Lang | fd216c4 | 2015-05-27 16:12:30 -0400 | [diff] [blame] | 21 | #include "libANGLE/renderer/gl/formatutilsgl.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 22 | |
| 23 | namespace rx |
| 24 | { |
| 25 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 26 | static bool UseTexImage2D(GLenum textureType) |
| 27 | { |
| 28 | return textureType == GL_TEXTURE_2D || textureType == GL_TEXTURE_CUBE_MAP; |
| 29 | } |
| 30 | |
| 31 | static bool UseTexImage3D(GLenum textureType) |
| 32 | { |
| 33 | return textureType == GL_TEXTURE_2D_ARRAY || textureType == GL_TEXTURE_3D; |
| 34 | } |
| 35 | |
Geoff Lang | 968992e | 2015-03-17 18:01:49 -0400 | [diff] [blame] | 36 | static bool CompatibleTextureTarget(GLenum textureType, GLenum textureTarget) |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 37 | { |
| 38 | if (textureType != GL_TEXTURE_CUBE_MAP) |
| 39 | { |
| 40 | return textureType == textureTarget; |
| 41 | } |
| 42 | else |
| 43 | { |
Geoff Lang | fb2a559 | 2015-03-20 11:25:37 -0400 | [diff] [blame] | 44 | return gl::IsCubeMapTextureTarget(textureTarget); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Geoff Lang | 14389cc | 2015-07-23 10:57:20 -0400 | [diff] [blame] | 48 | TextureGL::TextureGL(GLenum type, |
| 49 | const FunctionsGL *functions, |
| 50 | const WorkaroundsGL &workarounds, |
| 51 | StateManagerGL *stateManager) |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 52 | : TextureImpl(), |
| 53 | mTextureType(type), |
| 54 | mFunctions(functions), |
Geoff Lang | 14389cc | 2015-07-23 10:57:20 -0400 | [diff] [blame] | 55 | mWorkarounds(workarounds), |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 56 | mStateManager(stateManager), |
| 57 | mAppliedSamplerState(), |
| 58 | mTextureID(0) |
| 59 | { |
| 60 | ASSERT(mFunctions); |
| 61 | ASSERT(mStateManager); |
| 62 | |
| 63 | mFunctions->genTextures(1, &mTextureID); |
Geoff Lang | 90d98af | 2015-05-26 16:41:38 -0400 | [diff] [blame] | 64 | mStateManager->bindTexture(mTextureType, mTextureID); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 65 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 66 | |
| 67 | TextureGL::~TextureGL() |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 68 | { |
Geoff Lang | 1eb708e | 2015-05-04 14:58:23 -0400 | [diff] [blame] | 69 | mStateManager->deleteTexture(mTextureID); |
| 70 | mTextureID = 0; |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 71 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 72 | |
| 73 | void TextureGL::setUsage(GLenum usage) |
| 74 | { |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 75 | // GL_ANGLE_texture_usage not implemented for desktop GL |
| 76 | UNREACHABLE(); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | gl::Error TextureGL::setImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, GLenum format, GLenum type, |
| 80 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
| 81 | { |
Geoff Lang | fb2a559 | 2015-03-20 11:25:37 -0400 | [diff] [blame] | 82 | UNUSED_ASSERTION_VARIABLE(&CompatibleTextureTarget); // Reference this function to avoid warnings. |
Geoff Lang | 968992e | 2015-03-17 18:01:49 -0400 | [diff] [blame] | 83 | ASSERT(CompatibleTextureTarget(mTextureType, target)); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 84 | |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 85 | nativegl::TexImageFormat texImageFormat = |
| 86 | nativegl::GetTexImageFormat(mFunctions, mWorkarounds, internalFormat, format, type); |
Geoff Lang | fd216c4 | 2015-05-27 16:12:30 -0400 | [diff] [blame] | 87 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 88 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 89 | if (UseTexImage2D(mTextureType)) |
Jamie Madill | 67102f0 | 2015-03-16 10:41:42 -0400 | [diff] [blame] | 90 | { |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 91 | ASSERT(size.depth == 1); |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 92 | mFunctions->texImage2D(target, level, texImageFormat.internalFormat, size.width, |
| 93 | size.height, 0, texImageFormat.format, texImageFormat.type, pixels); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 94 | } |
| 95 | else if (UseTexImage3D(mTextureType)) |
| 96 | { |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 97 | mFunctions->texImage3D(target, level, texImageFormat.internalFormat, size.width, |
| 98 | size.height, size.depth, 0, texImageFormat.format, |
| 99 | texImageFormat.type, pixels); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 100 | } |
| 101 | else |
| 102 | { |
| 103 | UNREACHABLE(); |
Jamie Madill | 67102f0 | 2015-03-16 10:41:42 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 106 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | gl::Error TextureGL::setSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, GLenum type, |
| 110 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
| 111 | { |
Geoff Lang | 968992e | 2015-03-17 18:01:49 -0400 | [diff] [blame] | 112 | ASSERT(CompatibleTextureTarget(mTextureType, target)); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 113 | |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 114 | nativegl::TexSubImageFormat texSubImageFormat = |
| 115 | nativegl::GetTexSubImageFormat(mFunctions, mWorkarounds, format, type); |
| 116 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 117 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 118 | if (UseTexImage2D(mTextureType)) |
Jamie Madill | 67102f0 | 2015-03-16 10:41:42 -0400 | [diff] [blame] | 119 | { |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 120 | ASSERT(area.z == 0 && area.depth == 1); |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 121 | mFunctions->texSubImage2D(target, level, area.x, area.y, area.width, area.height, |
| 122 | texSubImageFormat.format, texSubImageFormat.type, pixels); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 123 | } |
| 124 | else if (UseTexImage3D(mTextureType)) |
| 125 | { |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 126 | mFunctions->texSubImage3D(target, level, area.x, area.y, area.z, area.width, area.height, |
| 127 | area.depth, texSubImageFormat.format, texSubImageFormat.type, |
| 128 | pixels); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
| 132 | UNREACHABLE(); |
Jamie Madill | 67102f0 | 2015-03-16 10:41:42 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 135 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | gl::Error TextureGL::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, |
Geoff Lang | 8509d86 | 2015-05-20 14:06:13 -0400 | [diff] [blame] | 139 | const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 140 | { |
Geoff Lang | 968992e | 2015-03-17 18:01:49 -0400 | [diff] [blame] | 141 | ASSERT(CompatibleTextureTarget(mTextureType, target)); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 142 | |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 143 | nativegl::CompressedTexImageFormat compressedTexImageFormat = |
| 144 | nativegl::GetCompressedTexImageFormat(mFunctions, mWorkarounds, internalFormat); |
Geoff Lang | fd216c4 | 2015-05-27 16:12:30 -0400 | [diff] [blame] | 145 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 146 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 147 | if (UseTexImage2D(mTextureType)) |
Jamie Madill | 67102f0 | 2015-03-16 10:41:42 -0400 | [diff] [blame] | 148 | { |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 149 | ASSERT(size.depth == 1); |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 150 | mFunctions->compressedTexImage2D(target, level, compressedTexImageFormat.internalFormat, |
| 151 | size.width, size.height, 0, imageSize, pixels); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 152 | } |
| 153 | else if (UseTexImage3D(mTextureType)) |
| 154 | { |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 155 | mFunctions->compressedTexImage3D(target, level, compressedTexImageFormat.internalFormat, |
| 156 | size.width, size.height, size.depth, 0, imageSize, pixels); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 157 | } |
| 158 | else |
| 159 | { |
| 160 | UNREACHABLE(); |
Jamie Madill | 67102f0 | 2015-03-16 10:41:42 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 163 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | gl::Error TextureGL::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, |
Geoff Lang | 8509d86 | 2015-05-20 14:06:13 -0400 | [diff] [blame] | 167 | const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 168 | { |
Geoff Lang | 968992e | 2015-03-17 18:01:49 -0400 | [diff] [blame] | 169 | ASSERT(CompatibleTextureTarget(mTextureType, target)); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 170 | |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 171 | nativegl::CompressedTexSubImageFormat compressedTexSubImageFormat = |
| 172 | nativegl::GetCompressedSubTexImageFormat(mFunctions, mWorkarounds, format); |
| 173 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 174 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 175 | if (UseTexImage2D(mTextureType)) |
Jamie Madill | 67102f0 | 2015-03-16 10:41:42 -0400 | [diff] [blame] | 176 | { |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 177 | ASSERT(area.z == 0 && area.depth == 1); |
Geoff Lang | 14389cc | 2015-07-23 10:57:20 -0400 | [diff] [blame] | 178 | mFunctions->compressedTexSubImage2D(target, level, area.x, area.y, area.width, area.height, |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 179 | compressedTexSubImageFormat.format, imageSize, pixels); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 180 | } |
| 181 | else if (UseTexImage3D(mTextureType)) |
| 182 | { |
Geoff Lang | 14389cc | 2015-07-23 10:57:20 -0400 | [diff] [blame] | 183 | mFunctions->compressedTexSubImage3D(target, level, area.x, area.y, area.z, area.width, |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 184 | area.height, area.depth, |
| 185 | compressedTexSubImageFormat.format, imageSize, pixels); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 186 | } |
| 187 | else |
| 188 | { |
| 189 | UNREACHABLE(); |
Jamie Madill | 67102f0 | 2015-03-16 10:41:42 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 192 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | gl::Error TextureGL::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat, |
| 196 | const gl::Framebuffer *source) |
| 197 | { |
Geoff Lang | fbfa47c | 2015-03-31 11:26:00 -0400 | [diff] [blame] | 198 | const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(source); |
| 199 | |
| 200 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 201 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID()); |
| 202 | |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 203 | nativegl::CopyTexImageImageFormat copyTexImageFormat = nativegl::GetCopyTexImageImageFormat( |
| 204 | mFunctions, mWorkarounds, internalFormat, source->getImplementationColorReadType()); |
| 205 | |
Geoff Lang | fbfa47c | 2015-03-31 11:26:00 -0400 | [diff] [blame] | 206 | if (UseTexImage2D(mTextureType)) |
| 207 | { |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 208 | mFunctions->copyTexImage2D(target, level, copyTexImageFormat.internalFormat, sourceArea.x, |
| 209 | sourceArea.y, sourceArea.width, sourceArea.height, 0); |
Geoff Lang | fbfa47c | 2015-03-31 11:26:00 -0400 | [diff] [blame] | 210 | } |
| 211 | else |
| 212 | { |
| 213 | UNREACHABLE(); |
| 214 | } |
| 215 | |
| 216 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | gl::Error TextureGL::copySubImage(GLenum target, size_t level, const gl::Offset &destOffset, const gl::Rectangle &sourceArea, |
| 220 | const gl::Framebuffer *source) |
| 221 | { |
Geoff Lang | fbfa47c | 2015-03-31 11:26:00 -0400 | [diff] [blame] | 222 | const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(source); |
| 223 | |
| 224 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 225 | mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID()); |
| 226 | |
| 227 | if (UseTexImage2D(mTextureType)) |
| 228 | { |
| 229 | ASSERT(destOffset.z == 0); |
| 230 | mFunctions->copyTexSubImage2D(target, level, destOffset.x, destOffset.y, |
| 231 | sourceArea.x, sourceArea.y, sourceArea.width, sourceArea.height); |
| 232 | } |
| 233 | else if (UseTexImage3D(mTextureType)) |
| 234 | { |
| 235 | mFunctions->copyTexSubImage3D(target, level, destOffset.x, destOffset.y, destOffset.z, |
| 236 | sourceArea.x, sourceArea.y, sourceArea.width, sourceArea.height); |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | UNREACHABLE(); |
| 241 | } |
| 242 | |
| 243 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | gl::Error TextureGL::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size) |
| 247 | { |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 248 | // TODO: emulate texture storage with TexImage calls if on GL version <4.2 or the |
| 249 | // ARB_texture_storage extension is not available. |
| 250 | |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 251 | nativegl::TexStorageFormat texStorageFormat = |
| 252 | nativegl::GetTexStorageFormat(mFunctions, mWorkarounds, internalFormat); |
Geoff Lang | fd216c4 | 2015-05-27 16:12:30 -0400 | [diff] [blame] | 253 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 254 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 255 | if (UseTexImage2D(mTextureType)) |
| 256 | { |
| 257 | ASSERT(size.depth == 1); |
Geoff Lang | 1c0ad62 | 2015-03-24 10:27:45 -0400 | [diff] [blame] | 258 | if (mFunctions->texStorage2D) |
| 259 | { |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 260 | mFunctions->texStorage2D(target, levels, texStorageFormat.internalFormat, size.width, |
| 261 | size.height); |
Geoff Lang | 1c0ad62 | 2015-03-24 10:27:45 -0400 | [diff] [blame] | 262 | } |
| 263 | else |
| 264 | { |
| 265 | // Make sure no pixel unpack buffer is bound |
| 266 | mStateManager->bindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); |
| 267 | |
| 268 | const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat); |
| 269 | |
| 270 | // Internal format must be sized |
| 271 | ASSERT(internalFormatInfo.pixelBytes != 0); |
| 272 | |
| 273 | for (size_t level = 0; level < levels; level++) |
| 274 | { |
| 275 | gl::Extents levelSize(std::max(size.width >> level, 1), |
| 276 | std::max(size.height >> level, 1), |
| 277 | 1); |
| 278 | |
| 279 | if (mTextureType == GL_TEXTURE_2D) |
| 280 | { |
Geoff Lang | 42c98f6 | 2015-05-20 14:09:25 -0400 | [diff] [blame] | 281 | if (internalFormatInfo.compressed) |
| 282 | { |
| 283 | size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, levelSize.width, levelSize.height); |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 284 | mFunctions->compressedTexImage2D( |
| 285 | target, level, texStorageFormat.internalFormat, levelSize.width, |
| 286 | levelSize.height, 0, dataSize, nullptr); |
Geoff Lang | 42c98f6 | 2015-05-20 14:09:25 -0400 | [diff] [blame] | 287 | } |
| 288 | else |
| 289 | { |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 290 | mFunctions->texImage2D(target, level, texStorageFormat.internalFormat, |
| 291 | levelSize.width, levelSize.height, 0, |
| 292 | internalFormatInfo.format, internalFormatInfo.type, |
| 293 | nullptr); |
Geoff Lang | 42c98f6 | 2015-05-20 14:09:25 -0400 | [diff] [blame] | 294 | } |
Geoff Lang | 1c0ad62 | 2015-03-24 10:27:45 -0400 | [diff] [blame] | 295 | } |
| 296 | else if (mTextureType == GL_TEXTURE_CUBE_MAP) |
| 297 | { |
| 298 | for (GLenum face = gl::FirstCubeMapTextureTarget; face <= gl::LastCubeMapTextureTarget; face++) |
| 299 | { |
Geoff Lang | 42c98f6 | 2015-05-20 14:09:25 -0400 | [diff] [blame] | 300 | if (internalFormatInfo.compressed) |
| 301 | { |
| 302 | size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, levelSize.width, levelSize.height); |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 303 | mFunctions->compressedTexImage2D( |
| 304 | face, level, texStorageFormat.internalFormat, levelSize.width, |
| 305 | levelSize.height, 0, dataSize, nullptr); |
Geoff Lang | 42c98f6 | 2015-05-20 14:09:25 -0400 | [diff] [blame] | 306 | } |
| 307 | else |
| 308 | { |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 309 | mFunctions->texImage2D(face, level, texStorageFormat.internalFormat, |
| 310 | levelSize.width, levelSize.height, 0, |
| 311 | internalFormatInfo.format, |
| 312 | internalFormatInfo.type, nullptr); |
Geoff Lang | 42c98f6 | 2015-05-20 14:09:25 -0400 | [diff] [blame] | 313 | } |
Geoff Lang | 1c0ad62 | 2015-03-24 10:27:45 -0400 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | UNREACHABLE(); |
| 319 | } |
| 320 | } |
| 321 | } |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 322 | } |
| 323 | else if (UseTexImage3D(mTextureType)) |
| 324 | { |
Geoff Lang | 1c0ad62 | 2015-03-24 10:27:45 -0400 | [diff] [blame] | 325 | if (mFunctions->texStorage3D) |
| 326 | { |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 327 | mFunctions->texStorage3D(target, levels, texStorageFormat.internalFormat, size.width, |
| 328 | size.height, size.depth); |
Geoff Lang | 1c0ad62 | 2015-03-24 10:27:45 -0400 | [diff] [blame] | 329 | } |
| 330 | else |
| 331 | { |
| 332 | // Make sure no pixel unpack buffer is bound |
| 333 | mStateManager->bindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); |
| 334 | |
| 335 | const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat); |
| 336 | |
| 337 | // Internal format must be sized |
| 338 | ASSERT(internalFormatInfo.pixelBytes != 0); |
| 339 | |
| 340 | for (size_t i = 0; i < levels; i++) |
| 341 | { |
| 342 | gl::Extents levelSize(std::max(size.width >> i, 1), |
| 343 | std::max(size.height >> i, 1), |
| 344 | mTextureType == GL_TEXTURE_3D ? std::max(size.depth >> i, 1) : size.depth); |
| 345 | |
Geoff Lang | 42c98f6 | 2015-05-20 14:09:25 -0400 | [diff] [blame] | 346 | if (internalFormatInfo.compressed) |
| 347 | { |
| 348 | size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, levelSize.width, levelSize.height) * levelSize.depth; |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 349 | mFunctions->compressedTexImage3D(target, i, texStorageFormat.internalFormat, |
| 350 | levelSize.width, levelSize.height, |
| 351 | levelSize.depth, 0, dataSize, nullptr); |
Geoff Lang | 42c98f6 | 2015-05-20 14:09:25 -0400 | [diff] [blame] | 352 | } |
| 353 | else |
| 354 | { |
Geoff Lang | 23a2ae0 | 2015-07-28 12:42:52 -0400 | [diff] [blame] | 355 | mFunctions->texImage3D(target, i, texStorageFormat.internalFormat, |
| 356 | levelSize.width, levelSize.height, levelSize.depth, 0, |
| 357 | internalFormatInfo.format, internalFormatInfo.type, |
| 358 | nullptr); |
Geoff Lang | 42c98f6 | 2015-05-20 14:09:25 -0400 | [diff] [blame] | 359 | } |
Geoff Lang | 1c0ad62 | 2015-03-24 10:27:45 -0400 | [diff] [blame] | 360 | } |
| 361 | } |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 362 | } |
| 363 | else |
| 364 | { |
| 365 | UNREACHABLE(); |
| 366 | } |
| 367 | |
| 368 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Gregoire Payen de La Garanderie | 752ce19 | 2015-04-14 11:11:12 +0100 | [diff] [blame] | 371 | gl::Error TextureGL::generateMipmaps(const gl::SamplerState &samplerState) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 372 | { |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 373 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 374 | mFunctions->generateMipmap(mTextureType); |
| 375 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void TextureGL::bindTexImage(egl::Surface *surface) |
| 379 | { |
Geoff Lang | 0305320 | 2015-04-09 11:21:13 -0400 | [diff] [blame] | 380 | ASSERT(mTextureType == GL_TEXTURE_2D); |
| 381 | |
| 382 | // Make sure this texture is bound |
| 383 | mStateManager->bindTexture(mTextureType, mTextureID); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void TextureGL::releaseTexImage() |
| 387 | { |
Geoff Lang | 0305320 | 2015-04-09 11:21:13 -0400 | [diff] [blame] | 388 | // Not all Surface implementations reset the size of mip 0 when releasing, do it manually |
| 389 | ASSERT(mTextureType == GL_TEXTURE_2D); |
| 390 | |
| 391 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 392 | if (UseTexImage2D(mTextureType)) |
| 393 | { |
| 394 | mFunctions->texImage2D(mTextureType, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | UNREACHABLE(); |
| 399 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 400 | } |
| 401 | |
Geoff Lang | a840617 | 2015-07-21 16:53:39 -0400 | [diff] [blame] | 402 | gl::Error TextureGL::setEGLImageTarget(GLenum target, egl::Image *image) |
| 403 | { |
| 404 | UNIMPLEMENTED(); |
| 405 | return gl::Error(GL_INVALID_OPERATION); |
| 406 | } |
| 407 | |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 408 | template <typename T> |
| 409 | static inline void SyncSamplerStateMember(const FunctionsGL *functions, const gl::SamplerState &newState, |
| 410 | gl::SamplerState &curState, GLenum textureType, GLenum name, |
| 411 | T(gl::SamplerState::*samplerMember)) |
| 412 | { |
| 413 | if (curState.*samplerMember != newState.*samplerMember) |
| 414 | { |
| 415 | curState.*samplerMember = newState.*samplerMember; |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 416 | functions->texParameterf(textureType, name, static_cast<GLfloat>(curState.*samplerMember)); |
Geoff Lang | c05f706 | 2015-03-10 09:50:57 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | void TextureGL::syncSamplerState(const gl::SamplerState &samplerState) const |
| 421 | { |
| 422 | if (mAppliedSamplerState != samplerState) |
| 423 | { |
| 424 | mStateManager->bindTexture(mTextureType, mTextureID); |
| 425 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MIN_FILTER, &gl::SamplerState::minFilter); |
| 426 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAG_FILTER, &gl::SamplerState::magFilter); |
| 427 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_S, &gl::SamplerState::wrapS); |
| 428 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_T, &gl::SamplerState::wrapT); |
| 429 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_R, &gl::SamplerState::wrapR); |
| 430 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_ANISOTROPY_EXT, &gl::SamplerState::maxAnisotropy); |
| 431 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_BASE_LEVEL, &gl::SamplerState::baseLevel); |
| 432 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_LEVEL, &gl::SamplerState::maxLevel); |
| 433 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MIN_LOD, &gl::SamplerState::minLod); |
| 434 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_LOD, &gl::SamplerState::maxLod); |
| 435 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_COMPARE_MODE, &gl::SamplerState::compareMode); |
| 436 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_COMPARE_FUNC, &gl::SamplerState::compareFunc); |
| 437 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_R, &gl::SamplerState::swizzleRed); |
| 438 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_G, &gl::SamplerState::swizzleGreen); |
| 439 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_B, &gl::SamplerState::swizzleBlue); |
| 440 | SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_A, &gl::SamplerState::swizzleAlpha); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | GLuint TextureGL::getTextureID() const |
| 445 | { |
| 446 | return mTextureID; |
| 447 | } |
| 448 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 449 | } |