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