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