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