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