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