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