Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2014 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 | // TextureD3D.cpp: Implementations of the Texture interfaces shared betweeen the D3D backends. |
| 8 | |
Jamie Madill | fb0580a | 2014-11-27 14:03:52 -0500 | [diff] [blame] | 9 | #include "libANGLE/renderer/d3d/TextureD3D.h" |
| 10 | |
| 11 | #include "common/mathutil.h" |
| 12 | #include "common/utilities.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 13 | #include "libANGLE/Buffer.h" |
| 14 | #include "libANGLE/Framebuffer.h" |
Jamie Madill | fb0580a | 2014-11-27 14:03:52 -0500 | [diff] [blame] | 15 | #include "libANGLE/Surface.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 16 | #include "libANGLE/Texture.h" |
| 17 | #include "libANGLE/formatutils.h" |
| 18 | #include "libANGLE/renderer/BufferImpl.h" |
| 19 | #include "libANGLE/renderer/RenderTarget.h" |
| 20 | #include "libANGLE/renderer/d3d/BufferD3D.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 21 | #include "libANGLE/renderer/d3d/ImageD3D.h" |
| 22 | #include "libANGLE/renderer/d3d/RendererD3D.h" |
Jamie Madill | fb0580a | 2014-11-27 14:03:52 -0500 | [diff] [blame] | 23 | #include "libANGLE/renderer/d3d/SurfaceD3D.h" |
| 24 | #include "libANGLE/renderer/d3d/TextureStorage.h" |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 25 | |
| 26 | namespace rx |
| 27 | { |
| 28 | |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 29 | namespace |
| 30 | { |
| 31 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 32 | gl::Error GetUnpackPointer(const gl::PixelUnpackState &unpack, const uint8_t *pixels, const uint8_t **pointerOut) |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 33 | { |
| 34 | if (unpack.pixelBuffer.id() != 0) |
| 35 | { |
| 36 | // Do a CPU readback here, if we have an unpack buffer bound and the fast GPU path is not supported |
| 37 | gl::Buffer *pixelBuffer = unpack.pixelBuffer.get(); |
| 38 | ptrdiff_t offset = reinterpret_cast<ptrdiff_t>(pixels); |
| 39 | |
| 40 | // TODO: this is the only place outside of renderer that asks for a buffers raw data. |
| 41 | // This functionality should be moved into renderer and the getData method of BufferImpl removed. |
Jamie Madill | 9ae396b | 2014-10-21 17:46:30 -0400 | [diff] [blame] | 42 | BufferD3D *bufferD3D = BufferD3D::makeBufferD3D(pixelBuffer->getImplementation()); |
| 43 | ASSERT(bufferD3D); |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 44 | const uint8_t *bufferData = NULL; |
Jamie Madill | 9ae396b | 2014-10-21 17:46:30 -0400 | [diff] [blame] | 45 | gl::Error error = bufferD3D->getData(&bufferData); |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 46 | if (error.isError()) |
| 47 | { |
| 48 | return error; |
| 49 | } |
| 50 | |
| 51 | *pointerOut = bufferData + offset; |
| 52 | } |
| 53 | else |
| 54 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 55 | *pointerOut = pixels; |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return gl::Error(GL_NO_ERROR); |
| 59 | } |
| 60 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 61 | bool IsRenderTargetUsage(GLenum usage) |
| 62 | { |
| 63 | return (usage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE); |
| 64 | } |
| 65 | |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 68 | TextureD3D::TextureD3D(RendererD3D *renderer) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 69 | : mRenderer(renderer), |
| 70 | mUsage(GL_NONE), |
| 71 | mDirtyImages(true), |
Jamie Madill | 98553e3 | 2014-09-30 16:33:50 -0400 | [diff] [blame] | 72 | mImmutable(false), |
| 73 | mTexStorage(NULL) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
| 77 | TextureD3D::~TextureD3D() |
| 78 | { |
| 79 | } |
| 80 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 81 | TextureD3D *TextureD3D::makeTextureD3D(TextureImpl *texture) |
| 82 | { |
| 83 | ASSERT(HAS_DYNAMIC_TYPE(TextureD3D*, texture)); |
| 84 | return static_cast<TextureD3D*>(texture); |
| 85 | } |
| 86 | |
Jamie Madill | 2f06dbf | 2014-09-18 15:08:50 -0400 | [diff] [blame] | 87 | TextureStorage *TextureD3D::getNativeTexture() |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 88 | { |
| 89 | // ensure the underlying texture is created |
| 90 | initializeStorage(false); |
| 91 | |
Jamie Madill | 98553e3 | 2014-09-30 16:33:50 -0400 | [diff] [blame] | 92 | if (mTexStorage) |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 93 | { |
| 94 | updateStorage(); |
| 95 | } |
| 96 | |
Jamie Madill | 98553e3 | 2014-09-30 16:33:50 -0400 | [diff] [blame] | 97 | return mTexStorage; |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 100 | GLint TextureD3D::getBaseLevelWidth() const |
| 101 | { |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 102 | const Image *baseImage = getBaseLevelImage(); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 103 | return (baseImage ? baseImage->getWidth() : 0); |
| 104 | } |
| 105 | |
| 106 | GLint TextureD3D::getBaseLevelHeight() const |
| 107 | { |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 108 | const Image *baseImage = getBaseLevelImage(); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 109 | return (baseImage ? baseImage->getHeight() : 0); |
| 110 | } |
| 111 | |
| 112 | GLint TextureD3D::getBaseLevelDepth() const |
| 113 | { |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 114 | const Image *baseImage = getBaseLevelImage(); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 115 | return (baseImage ? baseImage->getDepth() : 0); |
| 116 | } |
| 117 | |
| 118 | // Note: "base level image" is loosely defined to be any image from the base level, |
| 119 | // where in the base of 2D array textures and cube maps there are several. Don't use |
| 120 | // the base level image for anything except querying texture format and size. |
| 121 | GLenum TextureD3D::getBaseLevelInternalFormat() const |
| 122 | { |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 123 | const Image *baseImage = getBaseLevelImage(); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 124 | return (baseImage ? baseImage->getInternalFormat() : GL_NONE); |
| 125 | } |
| 126 | |
Jamie Madill | ec6de4e | 2014-10-20 10:59:56 -0400 | [diff] [blame] | 127 | bool TextureD3D::shouldUseSetData(const Image *image) const |
| 128 | { |
| 129 | if (!mRenderer->getWorkarounds().setDataFasterThanImageUpload) |
| 130 | { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | gl::InternalFormat internalFormat = gl::GetInternalFormatInfo(image->getInternalFormat()); |
| 135 | |
| 136 | // We can only handle full updates for depth-stencil textures, so to avoid complications |
| 137 | // disable them entirely. |
| 138 | if (internalFormat.depthBits > 0 || internalFormat.stencilBits > 0) |
| 139 | { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | // TODO(jmadill): Handle compressed internal formats |
| 144 | return (mTexStorage && !internalFormat.compressed); |
| 145 | } |
| 146 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 147 | gl::Error TextureD3D::setImage(const gl::ImageIndex &index, GLenum type, const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 148 | { |
Jamie Madill | ba6bc95 | 2014-10-06 10:56:22 -0400 | [diff] [blame] | 149 | Image *image = getImage(index); |
Jamie Madill | ec6de4e | 2014-10-20 10:59:56 -0400 | [diff] [blame] | 150 | ASSERT(image); |
Jamie Madill | ba6bc95 | 2014-10-06 10:56:22 -0400 | [diff] [blame] | 151 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 152 | // No-op |
| 153 | if (image->getWidth() == 0 || image->getHeight() == 0 || image->getDepth() == 0) |
| 154 | { |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 155 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // We no longer need the "GLenum format" parameter to TexImage to determine what data format "pixels" contains. |
| 159 | // From our image internal format we know how many channels to expect, and "type" gives the format of pixel's components. |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 160 | const uint8_t *pixelData = NULL; |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 161 | gl::Error error = GetUnpackPointer(unpack, pixels, &pixelData); |
| 162 | if (error.isError()) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 163 | { |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 164 | return error; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (pixelData != NULL) |
| 168 | { |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 169 | gl::Error error(GL_NO_ERROR); |
| 170 | |
Jamie Madill | ec6de4e | 2014-10-20 10:59:56 -0400 | [diff] [blame] | 171 | if (shouldUseSetData(image)) |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 172 | { |
Jamie Madill | ec6de4e | 2014-10-20 10:59:56 -0400 | [diff] [blame] | 173 | error = mTexStorage->setData(index, image, NULL, type, unpack, pixelData); |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 174 | } |
| 175 | else |
| 176 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 177 | gl::Box fullImageArea(0, 0, 0, image->getWidth(), image->getHeight(), image->getDepth()); |
| 178 | error = image->loadData(fullImageArea, unpack.alignment, type, pixelData); |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 181 | if (error.isError()) |
| 182 | { |
| 183 | return error; |
| 184 | } |
| 185 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 186 | mDirtyImages = true; |
| 187 | } |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 188 | |
| 189 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 192 | gl::Error TextureD3D::subImage(const gl::ImageIndex &index, const gl::Box &area, GLenum format, GLenum type, |
| 193 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 194 | { |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 195 | // CPU readback & copy where direct GPU copy is not supported |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 196 | const uint8_t *pixelData = NULL; |
| 197 | gl::Error error = GetUnpackPointer(unpack, pixels, &pixelData); |
| 198 | if (error.isError()) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 199 | { |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 200 | return error; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (pixelData != NULL) |
| 204 | { |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 205 | Image *image = getImage(index); |
| 206 | ASSERT(image); |
| 207 | |
Jamie Madill | ec6de4e | 2014-10-20 10:59:56 -0400 | [diff] [blame] | 208 | if (shouldUseSetData(image)) |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 209 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 210 | return mTexStorage->setData(index, image, &area, type, unpack, pixelData); |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 213 | gl::Error error = image->loadData(area, unpack.alignment, type, pixelData); |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 214 | if (error.isError()) |
| 215 | { |
| 216 | return error; |
| 217 | } |
| 218 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 219 | error = commitRegion(index, area); |
Jamie Madill | e6b6da0 | 2014-10-02 11:03:14 -0400 | [diff] [blame] | 220 | if (error.isError()) |
| 221 | { |
| 222 | return error; |
| 223 | } |
| 224 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 225 | mDirtyImages = true; |
| 226 | } |
| 227 | |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 228 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 231 | gl::Error TextureD3D::setCompressedImage(const gl::ImageIndex &index, const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 232 | { |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 233 | // We no longer need the "GLenum format" parameter to TexImage to determine what data format "pixels" contains. |
| 234 | // From our image internal format we know how many channels to expect, and "type" gives the format of pixel's components. |
| 235 | const uint8_t *pixelData = NULL; |
| 236 | gl::Error error = GetUnpackPointer(unpack, pixels, &pixelData); |
| 237 | if (error.isError()) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 238 | { |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 239 | return error; |
| 240 | } |
| 241 | |
| 242 | if (pixelData != NULL) |
| 243 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 244 | Image *image = getImage(index); |
| 245 | ASSERT(image); |
| 246 | |
| 247 | gl::Box fullImageArea(0, 0, 0, image->getWidth(), image->getHeight(), image->getDepth()); |
| 248 | gl::Error error = image->loadCompressedData(fullImageArea, pixelData); |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 249 | if (error.isError()) |
| 250 | { |
| 251 | return error; |
| 252 | } |
| 253 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 254 | mDirtyImages = true; |
| 255 | } |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 256 | |
| 257 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 260 | gl::Error TextureD3D::subImageCompressed(const gl::ImageIndex &index, const gl::Box &area, GLenum format, |
| 261 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 262 | { |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 263 | const uint8_t *pixelData = NULL; |
| 264 | gl::Error error = GetUnpackPointer(unpack, pixels, &pixelData); |
| 265 | if (error.isError()) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 266 | { |
Jamie Madill | c751d1e | 2014-10-21 17:46:29 -0400 | [diff] [blame] | 267 | return error; |
| 268 | } |
| 269 | |
| 270 | if (pixelData != NULL) |
| 271 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 272 | Image *image = getImage(index); |
| 273 | ASSERT(image); |
| 274 | |
| 275 | gl::Error error = image->loadCompressedData(area, pixelData); |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 276 | if (error.isError()) |
| 277 | { |
| 278 | return error; |
| 279 | } |
| 280 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 281 | mDirtyImages = true; |
| 282 | } |
| 283 | |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 284 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | bool TextureD3D::isFastUnpackable(const gl::PixelUnpackState &unpack, GLenum sizedInternalFormat) |
| 288 | { |
| 289 | return unpack.pixelBuffer.id() != 0 && mRenderer->supportsFastCopyBufferToTexture(sizedInternalFormat); |
| 290 | } |
| 291 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 292 | gl::Error TextureD3D::fastUnpackPixels(const gl::PixelUnpackState &unpack, const uint8_t *pixels, const gl::Box &destArea, |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 293 | GLenum sizedInternalFormat, GLenum type, RenderTarget *destRenderTarget) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 294 | { |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 295 | // No-op |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 296 | if (destArea.width <= 0 && destArea.height <= 0 && destArea.depth <= 0) |
| 297 | { |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 298 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // In order to perform the fast copy through the shader, we must have the right format, and be able |
| 302 | // to create a render target. |
| 303 | ASSERT(mRenderer->supportsFastCopyBufferToTexture(sizedInternalFormat)); |
| 304 | |
Jacek Caban | a5521de | 2014-10-01 17:23:46 +0200 | [diff] [blame] | 305 | uintptr_t offset = reinterpret_cast<uintptr_t>(pixels); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 306 | |
Geoff Lang | ae5122c | 2014-08-27 14:08:43 -0400 | [diff] [blame] | 307 | gl::Error error = mRenderer->fastCopyBufferToTexture(unpack, offset, destRenderTarget, sizedInternalFormat, type, destArea); |
| 308 | if (error.isError()) |
| 309 | { |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 310 | return error; |
Geoff Lang | ae5122c | 2014-08-27 14:08:43 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 313 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | GLint TextureD3D::creationLevels(GLsizei width, GLsizei height, GLsizei depth) const |
| 317 | { |
| 318 | if ((gl::isPow2(width) && gl::isPow2(height) && gl::isPow2(depth)) || mRenderer->getRendererExtensions().textureNPOT) |
| 319 | { |
| 320 | // Maximum number of levels |
| 321 | return gl::log2(std::max(std::max(width, height), depth)) + 1; |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | // OpenGL ES 2.0 without GL_OES_texture_npot does not permit NPOT mipmaps. |
| 326 | return 1; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | int TextureD3D::mipLevels() const |
| 331 | { |
| 332 | return gl::log2(std::max(std::max(getBaseLevelWidth(), getBaseLevelHeight()), getBaseLevelDepth())) + 1; |
| 333 | } |
| 334 | |
Jamie Madill | 98553e3 | 2014-09-30 16:33:50 -0400 | [diff] [blame] | 335 | TextureStorage *TextureD3D::getStorage() |
| 336 | { |
Jamie Madill | dd3bf52 | 2014-10-20 17:04:35 -0400 | [diff] [blame] | 337 | ASSERT(mTexStorage); |
Jamie Madill | 98553e3 | 2014-09-30 16:33:50 -0400 | [diff] [blame] | 338 | return mTexStorage; |
| 339 | } |
| 340 | |
Jamie Madill | 3269bcb | 2014-09-30 16:33:52 -0400 | [diff] [blame] | 341 | Image *TextureD3D::getBaseLevelImage() const |
| 342 | { |
| 343 | return getImage(getImageIndex(0, 0)); |
| 344 | } |
| 345 | |
Geoff Lang | 06ecf3d | 2014-09-23 16:39:50 -0400 | [diff] [blame] | 346 | gl::Error TextureD3D::generateMipmaps() |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 347 | { |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 348 | GLint mipCount = mipLevels(); |
| 349 | |
| 350 | if (mipCount == 1) |
| 351 | { |
Geoff Lang | 06ecf3d | 2014-09-23 16:39:50 -0400 | [diff] [blame] | 352 | return gl::Error(GL_NO_ERROR); // no-op |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Set up proper mipmap chain in our Image array. |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 356 | initMipmapsImages(); |
| 357 | |
| 358 | // We know that all layers have the same dimension, for the texture to be complete |
| 359 | GLint layerCount = static_cast<GLint>(getLayerCount(0)); |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 360 | |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 361 | // When making mipmaps with the setData workaround enabled, the texture storage has |
| 362 | // the image data already. For non-render-target storage, we have to pull it out into |
| 363 | // an image layer. |
| 364 | if (mRenderer->getWorkarounds().setDataFasterThanImageUpload && mTexStorage) |
| 365 | { |
| 366 | if (!mTexStorage->isRenderTarget()) |
| 367 | { |
| 368 | // Copy from the storage mip 0 to Image mip 0 |
| 369 | for (GLint layer = 0; layer < layerCount; ++layer) |
| 370 | { |
| 371 | gl::ImageIndex srcIndex = getImageIndex(0, layer); |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 372 | |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 373 | Image *image = getImage(srcIndex); |
| 374 | gl::Rectangle area(0, 0, image->getWidth(), image->getHeight()); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 375 | gl::Offset offset(0, 0, 0); |
| 376 | gl::Error error = image->copy(offset, area, srcIndex, mTexStorage); |
Geoff Lang | 06ecf3d | 2014-09-23 16:39:50 -0400 | [diff] [blame] | 377 | if (error.isError()) |
| 378 | { |
| 379 | return error; |
| 380 | } |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | else |
| 384 | { |
Geoff Lang | 06ecf3d | 2014-09-23 16:39:50 -0400 | [diff] [blame] | 385 | gl::Error error = updateStorage(); |
| 386 | if (error.isError()) |
| 387 | { |
| 388 | return error; |
| 389 | } |
Jamie Madill | 9aca059 | 2014-10-06 16:26:59 -0400 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | bool renderableStorage = (mTexStorage && mTexStorage->isRenderTarget()); |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 394 | |
| 395 | for (GLint layer = 0; layer < layerCount; ++layer) |
| 396 | { |
| 397 | for (GLint mip = 1; mip < mipCount; ++mip) |
| 398 | { |
| 399 | ASSERT(getLayerCount(mip) == layerCount); |
| 400 | |
| 401 | gl::ImageIndex sourceIndex = getImageIndex(mip - 1, layer); |
| 402 | gl::ImageIndex destIndex = getImageIndex(mip, layer); |
| 403 | |
| 404 | if (renderableStorage) |
| 405 | { |
| 406 | // GPU-side mipmapping |
Geoff Lang | 06ecf3d | 2014-09-23 16:39:50 -0400 | [diff] [blame] | 407 | gl::Error error = mTexStorage->generateMipmap(sourceIndex, destIndex); |
| 408 | if (error.isError()) |
| 409 | { |
| 410 | return error; |
| 411 | } |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 412 | } |
| 413 | else |
| 414 | { |
| 415 | // CPU-side mipmapping |
Geoff Lang | 06ecf3d | 2014-09-23 16:39:50 -0400 | [diff] [blame] | 416 | gl::Error error = mRenderer->generateMipmap(getImage(destIndex), getImage(sourceIndex)); |
| 417 | if (error.isError()) |
| 418 | { |
| 419 | return error; |
| 420 | } |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
Geoff Lang | 06ecf3d | 2014-09-23 16:39:50 -0400 | [diff] [blame] | 424 | |
| 425 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 426 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 427 | |
Jamie Madill | 135570a | 2014-09-30 16:33:51 -0400 | [diff] [blame] | 428 | bool TextureD3D::isBaseImageZeroSize() const |
| 429 | { |
Jamie Madill | 3269bcb | 2014-09-30 16:33:52 -0400 | [diff] [blame] | 430 | Image *baseImage = getBaseLevelImage(); |
Jamie Madill | 135570a | 2014-09-30 16:33:51 -0400 | [diff] [blame] | 431 | |
| 432 | if (!baseImage || baseImage->getWidth() <= 0) |
| 433 | { |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | if (!gl::IsCubemapTextureTarget(baseImage->getTarget()) && baseImage->getHeight() <= 0) |
| 438 | { |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | if (baseImage->getTarget() == GL_TEXTURE_3D && baseImage->getDepth() <= 0) |
| 443 | { |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | if (baseImage->getTarget() == GL_TEXTURE_2D_ARRAY && getLayerCount(0) <= 0) |
| 448 | { |
| 449 | return true; |
| 450 | } |
| 451 | |
| 452 | return false; |
| 453 | } |
| 454 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 455 | gl::Error TextureD3D::ensureRenderTarget() |
Jamie Madill | 135570a | 2014-09-30 16:33:51 -0400 | [diff] [blame] | 456 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 457 | gl::Error error = initializeStorage(true); |
| 458 | if (error.isError()) |
| 459 | { |
| 460 | return error; |
| 461 | } |
Jamie Madill | 135570a | 2014-09-30 16:33:51 -0400 | [diff] [blame] | 462 | |
| 463 | if (!isBaseImageZeroSize()) |
| 464 | { |
| 465 | ASSERT(mTexStorage); |
| 466 | if (!mTexStorage->isRenderTarget()) |
| 467 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 468 | TextureStorage *newRenderTargetStorage = NULL; |
| 469 | error = createCompleteStorage(true, &newRenderTargetStorage); |
| 470 | if (error.isError()) |
Jamie Madill | 135570a | 2014-09-30 16:33:51 -0400 | [diff] [blame] | 471 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 472 | return error; |
Jamie Madill | 135570a | 2014-09-30 16:33:51 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 475 | error = mTexStorage->copyToStorage(newRenderTargetStorage); |
| 476 | if (error.isError()) |
| 477 | { |
| 478 | SafeDelete(newRenderTargetStorage); |
| 479 | return error; |
| 480 | } |
| 481 | |
| 482 | error = setCompleteTexStorage(newRenderTargetStorage); |
| 483 | if (error.isError()) |
| 484 | { |
| 485 | SafeDelete(newRenderTargetStorage); |
| 486 | return error; |
| 487 | } |
Jamie Madill | 135570a | 2014-09-30 16:33:51 -0400 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 491 | return gl::Error(GL_NO_ERROR); |
Jamie Madill | 135570a | 2014-09-30 16:33:51 -0400 | [diff] [blame] | 492 | } |
| 493 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 494 | bool TextureD3D::canCreateRenderTargetForImage(const gl::ImageIndex &index) const |
| 495 | { |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 496 | Image *image = getImage(index); |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 497 | bool levelsComplete = (isImageComplete(index) && isImageComplete(getImageIndex(0, 0))); |
| 498 | return (image->isRenderableFormat() && levelsComplete); |
| 499 | } |
| 500 | |
Jamie Madill | 710e577 | 2014-10-20 17:13:53 -0400 | [diff] [blame] | 501 | gl::Error TextureD3D::commitRegion(const gl::ImageIndex &index, const gl::Box ®ion) |
| 502 | { |
| 503 | if (mTexStorage) |
| 504 | { |
| 505 | ASSERT(isValidIndex(index)); |
| 506 | Image *image = getImage(index); |
| 507 | ImageD3D *imageD3D = ImageD3D::makeImageD3D(image); |
| 508 | gl::Error error = imageD3D->copyToStorage(mTexStorage, index, region); |
| 509 | if (error.isError()) |
| 510 | { |
| 511 | return error; |
| 512 | } |
| 513 | |
| 514 | image->markClean(); |
| 515 | } |
| 516 | |
| 517 | return gl::Error(GL_NO_ERROR); |
| 518 | } |
| 519 | |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 520 | TextureD3D_2D::TextureD3D_2D(RendererD3D *renderer) |
Jamie Madill | 98553e3 | 2014-09-30 16:33:50 -0400 | [diff] [blame] | 521 | : TextureD3D(renderer) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 522 | { |
| 523 | for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i) |
| 524 | { |
| 525 | mImageArray[i] = ImageD3D::makeImageD3D(renderer->createImage()); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | TextureD3D_2D::~TextureD3D_2D() |
| 530 | { |
Austin Kinross | 6982260 | 2014-08-12 15:51:37 -0700 | [diff] [blame] | 531 | // Delete the Images before the TextureStorage. |
| 532 | // Images might be relying on the TextureStorage for some of their data. |
| 533 | // If TextureStorage is deleted before the Images, then their data will be wastefully copied back from the GPU before we delete the Images. |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 534 | for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i) |
| 535 | { |
| 536 | delete mImageArray[i]; |
| 537 | } |
Austin Kinross | 6982260 | 2014-08-12 15:51:37 -0700 | [diff] [blame] | 538 | |
| 539 | SafeDelete(mTexStorage); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 542 | Image *TextureD3D_2D::getImage(int level, int layer) const |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 543 | { |
| 544 | ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 545 | ASSERT(layer == 0); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 546 | return mImageArray[level]; |
| 547 | } |
| 548 | |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 549 | Image *TextureD3D_2D::getImage(const gl::ImageIndex &index) const |
| 550 | { |
| 551 | ASSERT(index.mipIndex < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 552 | ASSERT(!index.hasLayer()); |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 553 | ASSERT(index.type == GL_TEXTURE_2D); |
| 554 | return mImageArray[index.mipIndex]; |
| 555 | } |
| 556 | |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 557 | GLsizei TextureD3D_2D::getLayerCount(int level) const |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 558 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 559 | ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
| 560 | return 1; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 563 | GLsizei TextureD3D_2D::getWidth(GLint level) const |
| 564 | { |
| 565 | if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS) |
| 566 | return mImageArray[level]->getWidth(); |
| 567 | else |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | GLsizei TextureD3D_2D::getHeight(GLint level) const |
| 572 | { |
| 573 | if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS) |
| 574 | return mImageArray[level]->getHeight(); |
| 575 | else |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | GLenum TextureD3D_2D::getInternalFormat(GLint level) const |
| 580 | { |
| 581 | if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS) |
| 582 | return mImageArray[level]->getInternalFormat(); |
| 583 | else |
| 584 | return GL_NONE; |
| 585 | } |
| 586 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 587 | bool TextureD3D_2D::isDepth(GLint level) const |
| 588 | { |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 589 | return gl::GetInternalFormatInfo(getInternalFormat(level)).depthBits > 0; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 592 | gl::Error TextureD3D_2D::setImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, GLenum format, GLenum type, |
| 593 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 594 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 595 | ASSERT(target == GL_TEXTURE_2D && size.depth == 1); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 596 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 597 | GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, type); |
| 598 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 599 | bool fastUnpacked = false; |
| 600 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 601 | redefineImage(level, sizedInternalFormat, size); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 602 | |
Jamie Madill | ba6bc95 | 2014-10-06 10:56:22 -0400 | [diff] [blame] | 603 | gl::ImageIndex index = gl::ImageIndex::Make2D(level); |
| 604 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 605 | // Attempt a fast gpu copy of the pixel data to the surface |
| 606 | if (isFastUnpackable(unpack, sizedInternalFormat) && isLevelComplete(level)) |
| 607 | { |
| 608 | // Will try to create RT storage if it does not exist |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 609 | RenderTarget *destRenderTarget = NULL; |
| 610 | gl::Error error = getRenderTarget(index, &destRenderTarget); |
| 611 | if (error.isError()) |
| 612 | { |
| 613 | return error; |
| 614 | } |
| 615 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 616 | gl::Box destArea(0, 0, 0, getWidth(level), getHeight(level), 1); |
| 617 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 618 | error = fastUnpackPixels(unpack, pixels, destArea, sizedInternalFormat, type, destRenderTarget); |
| 619 | if (error.isError()) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 620 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 621 | return error; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 622 | } |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 623 | |
| 624 | // Ensure we don't overwrite our newly initialized data |
| 625 | mImageArray[level]->markClean(); |
| 626 | |
| 627 | fastUnpacked = true; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | if (!fastUnpacked) |
| 631 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 632 | gl::Error error = TextureD3D::setImage(index, type, unpack, pixels); |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 633 | if (error.isError()) |
| 634 | { |
| 635 | return error; |
| 636 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 637 | } |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 638 | |
| 639 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 642 | gl::Error TextureD3D_2D::setSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, GLenum type, |
| 643 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 644 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 645 | ASSERT(target == GL_TEXTURE_2D && area.depth == 1 && area.z == 0); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 646 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 647 | bool fastUnpacked = false; |
| 648 | |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 649 | gl::ImageIndex index = gl::ImageIndex::Make2D(level); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 650 | if (isFastUnpackable(unpack, getInternalFormat(level)) && isLevelComplete(level)) |
| 651 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 652 | RenderTarget *renderTarget = NULL; |
| 653 | gl::Error error = getRenderTarget(index, &renderTarget); |
| 654 | if (error.isError()) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 655 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 656 | return error; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 657 | } |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 658 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 659 | error = fastUnpackPixels(unpack, pixels, area, getInternalFormat(level), type, renderTarget); |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 660 | if (error.isError()) |
| 661 | { |
| 662 | return error; |
| 663 | } |
| 664 | |
| 665 | // Ensure we don't overwrite our newly initialized data |
| 666 | mImageArray[level]->markClean(); |
| 667 | |
| 668 | fastUnpacked = true; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 671 | if (!fastUnpacked) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 672 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 673 | return TextureD3D::subImage(index, area, format, type, unpack, pixels); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 674 | } |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 675 | |
| 676 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 679 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 680 | gl::Error TextureD3D_2D::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, |
| 681 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
| 682 | { |
| 683 | ASSERT(target == GL_TEXTURE_2D && size.depth == 1); |
| 684 | |
| 685 | // compressed formats don't have separate sized internal formats-- we can just use the compressed format directly |
| 686 | redefineImage(level, internalFormat, size); |
| 687 | |
| 688 | return TextureD3D::setCompressedImage(gl::ImageIndex::Make2D(level), unpack, pixels); |
| 689 | } |
| 690 | |
| 691 | gl::Error TextureD3D_2D::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, |
| 692 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
| 693 | { |
| 694 | ASSERT(target == GL_TEXTURE_2D && area.depth == 1 && area.z == 0); |
| 695 | |
| 696 | gl::ImageIndex index = gl::ImageIndex::Make2D(level); |
| 697 | gl::Error error = TextureD3D::subImageCompressed(index, area, format, unpack, pixels); |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 698 | if (error.isError()) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 699 | { |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 700 | return error; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 701 | } |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 702 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 703 | return commitRegion(index, area); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 706 | gl::Error TextureD3D_2D::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat, |
| 707 | const gl::Framebuffer *source) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 708 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 709 | ASSERT(target == GL_TEXTURE_2D); |
| 710 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 711 | GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE); |
| 712 | redefineImage(level, sizedInternalFormat, gl::Extents(sourceArea.width, sourceArea.height, 1)); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 713 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 714 | gl::ImageIndex index = gl::ImageIndex::Make2D(level); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 715 | gl::Offset destOffset(0, 0, 0); |
Jamie Madill | 82bf0c5 | 2014-10-03 11:50:53 -0400 | [diff] [blame] | 716 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 717 | if (!canCreateRenderTargetForImage(index)) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 718 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 719 | gl::Error error = mImageArray[level]->copy(destOffset, sourceArea, source); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 720 | if (error.isError()) |
| 721 | { |
| 722 | return error; |
| 723 | } |
| 724 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 725 | mDirtyImages = true; |
| 726 | } |
| 727 | else |
| 728 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 729 | gl::Error error = ensureRenderTarget(); |
| 730 | if (error.isError()) |
| 731 | { |
| 732 | return error; |
| 733 | } |
| 734 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 735 | mImageArray[level]->markClean(); |
| 736 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 737 | if (sourceArea.width != 0 && sourceArea.height != 0 && isValidLevel(level)) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 738 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 739 | gl::Error error = mRenderer->copyImage2D(source, sourceArea, internalFormat, destOffset, mTexStorage, level); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 740 | if (error.isError()) |
| 741 | { |
| 742 | return error; |
| 743 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 746 | |
| 747 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 750 | gl::Error TextureD3D_2D::copySubImage(GLenum target, size_t level, const gl::Offset &destOffset, const gl::Rectangle &sourceArea, |
| 751 | const gl::Framebuffer *source) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 752 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 753 | ASSERT(target == GL_TEXTURE_2D && destOffset.z == 0); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 754 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 755 | // can only make our texture storage to a render target if level 0 is defined (with a width & height) and |
| 756 | // the current level we're copying to is defined (with appropriate format, width & height) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 757 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 758 | gl::ImageIndex index = gl::ImageIndex::Make2D(level); |
Jamie Madill | 82bf0c5 | 2014-10-03 11:50:53 -0400 | [diff] [blame] | 759 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 760 | if (!canCreateRenderTargetForImage(index)) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 761 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 762 | gl::Error error = mImageArray[level]->copy(destOffset, sourceArea, source); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 763 | if (error.isError()) |
| 764 | { |
| 765 | return error; |
| 766 | } |
| 767 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 768 | mDirtyImages = true; |
| 769 | } |
| 770 | else |
| 771 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 772 | gl::Error error = ensureRenderTarget(); |
| 773 | if (error.isError()) |
| 774 | { |
| 775 | return error; |
| 776 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 777 | |
| 778 | if (isValidLevel(level)) |
| 779 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 780 | error = updateStorageLevel(level); |
| 781 | if (error.isError()) |
| 782 | { |
| 783 | return error; |
| 784 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 785 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 786 | error = mRenderer->copyImage2D(source, sourceArea, |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 787 | gl::GetInternalFormatInfo(getBaseLevelInternalFormat()).format, |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 788 | destOffset, mTexStorage, level); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 789 | if (error.isError()) |
| 790 | { |
| 791 | return error; |
| 792 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 795 | |
| 796 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 799 | gl::Error TextureD3D_2D::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 800 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 801 | ASSERT(GL_TEXTURE_2D && size.depth == 1); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 802 | |
Geoff Lang | 8fed1f7 | 2015-01-09 11:09:33 -0500 | [diff] [blame^] | 803 | for (size_t level = 0; level < levels; level++) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 804 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 805 | gl::Extents levelSize(std::max(1, size.width >> level), |
| 806 | std::max(1, size.height >> level), |
| 807 | 1); |
| 808 | mImageArray[level]->redefine(GL_TEXTURE_2D, internalFormat, levelSize, true); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | for (int level = levels; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++) |
| 812 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 813 | mImageArray[level]->redefine(GL_TEXTURE_2D, GL_NONE, gl::Extents(0, 0, 0), true); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Geoff Lang | 1f8532b | 2014-09-05 09:46:13 -0400 | [diff] [blame] | 816 | // TODO(geofflang): Verify storage creation had no errors |
Jamie Madill | c483326 | 2014-09-18 16:18:26 -0400 | [diff] [blame] | 817 | bool renderTarget = IsRenderTargetUsage(mUsage); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 818 | TextureStorage *storage = mRenderer->createTextureStorage2D(internalFormat, renderTarget, size.width, size.height, levels); |
Geoff Lang | 1f8532b | 2014-09-05 09:46:13 -0400 | [diff] [blame] | 819 | |
| 820 | gl::Error error = setCompleteTexStorage(storage); |
| 821 | if (error.isError()) |
| 822 | { |
| 823 | SafeDelete(storage); |
| 824 | return error; |
| 825 | } |
| 826 | |
| 827 | mImmutable = true; |
| 828 | |
| 829 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 832 | void TextureD3D_2D::bindTexImage(egl::Surface *surface) |
| 833 | { |
| 834 | GLenum internalformat = surface->getFormat(); |
| 835 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 836 | gl::Extents size(surface->getWidth(), surface->getHeight(), 1); |
| 837 | mImageArray[0]->redefine(GL_TEXTURE_2D, internalformat, size, true); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 838 | |
| 839 | if (mTexStorage) |
| 840 | { |
| 841 | SafeDelete(mTexStorage); |
| 842 | } |
Jamie Madill | 2f06dbf | 2014-09-18 15:08:50 -0400 | [diff] [blame] | 843 | |
Jamie Madill | fb0580a | 2014-11-27 14:03:52 -0500 | [diff] [blame] | 844 | SurfaceD3D *surfaceD3D = SurfaceD3D::makeSurfaceD3D(surface); |
| 845 | ASSERT(surfaceD3D); |
| 846 | |
| 847 | mTexStorage = mRenderer->createTextureStorage2D(surfaceD3D->getSwapChain()); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 848 | |
| 849 | mDirtyImages = true; |
| 850 | } |
| 851 | |
| 852 | void TextureD3D_2D::releaseTexImage() |
| 853 | { |
| 854 | if (mTexStorage) |
| 855 | { |
| 856 | SafeDelete(mTexStorage); |
| 857 | } |
| 858 | |
| 859 | for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) |
| 860 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 861 | mImageArray[i]->redefine(GL_TEXTURE_2D, GL_NONE, gl::Extents(0, 0, 0), true); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 865 | void TextureD3D_2D::initMipmapsImages() |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 866 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 867 | // Purge array levels 1 through q and reset them to represent the generated mipmap levels. |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 868 | int levelCount = mipLevels(); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 869 | for (int level = 1; level < levelCount; level++) |
| 870 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 871 | gl::Extents levelSize(std::max(getBaseLevelWidth() >> level, 1), |
| 872 | std::max(getBaseLevelHeight() >> level, 1), |
| 873 | 1); |
| 874 | |
| 875 | redefineImage(level, getBaseLevelInternalFormat(), levelSize); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 876 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 879 | unsigned int TextureD3D_2D::getRenderTargetSerial(const gl::ImageIndex &index) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 880 | { |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 881 | ASSERT(!index.hasLayer()); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 882 | return (!ensureRenderTarget().isError() ? mTexStorage->getRenderTargetSerial(index) : 0); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 885 | gl::Error TextureD3D_2D::getRenderTarget(const gl::ImageIndex &index, RenderTarget **outRT) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 886 | { |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 887 | ASSERT(!index.hasLayer()); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 888 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 889 | // ensure the underlying texture is created |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 890 | gl::Error error = ensureRenderTarget(); |
| 891 | if (error.isError()) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 892 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 893 | return error; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 896 | error = updateStorageLevel(index.mipIndex); |
| 897 | if (error.isError()) |
| 898 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 899 | return error; |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 900 | } |
| 901 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 902 | return mTexStorage->getRenderTarget(index, outRT); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 905 | bool TextureD3D_2D::isValidLevel(int level) const |
| 906 | { |
| 907 | return (mTexStorage ? (level >= 0 && level < mTexStorage->getLevelCount()) : false); |
| 908 | } |
| 909 | |
| 910 | bool TextureD3D_2D::isLevelComplete(int level) const |
| 911 | { |
| 912 | if (isImmutable()) |
| 913 | { |
| 914 | return true; |
| 915 | } |
| 916 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 917 | const Image *baseImage = getBaseLevelImage(); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 918 | |
| 919 | GLsizei width = baseImage->getWidth(); |
| 920 | GLsizei height = baseImage->getHeight(); |
| 921 | |
| 922 | if (width <= 0 || height <= 0) |
| 923 | { |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | // The base image level is complete if the width and height are positive |
| 928 | if (level == 0) |
| 929 | { |
| 930 | return true; |
| 931 | } |
| 932 | |
| 933 | ASSERT(level >= 1 && level <= (int)ArraySize(mImageArray) && mImageArray[level] != NULL); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 934 | ImageD3D *image = mImageArray[level]; |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 935 | |
| 936 | if (image->getInternalFormat() != baseImage->getInternalFormat()) |
| 937 | { |
| 938 | return false; |
| 939 | } |
| 940 | |
| 941 | if (image->getWidth() != std::max(1, width >> level)) |
| 942 | { |
| 943 | return false; |
| 944 | } |
| 945 | |
| 946 | if (image->getHeight() != std::max(1, height >> level)) |
| 947 | { |
| 948 | return false; |
| 949 | } |
| 950 | |
| 951 | return true; |
| 952 | } |
| 953 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 954 | bool TextureD3D_2D::isImageComplete(const gl::ImageIndex &index) const |
| 955 | { |
| 956 | return isLevelComplete(index.mipIndex); |
| 957 | } |
| 958 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 959 | // Constructs a native texture resource from the texture images |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 960 | gl::Error TextureD3D_2D::initializeStorage(bool renderTarget) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 961 | { |
| 962 | // Only initialize the first time this texture is used as a render target or shader resource |
| 963 | if (mTexStorage) |
| 964 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 965 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | // do not attempt to create storage for nonexistant data |
| 969 | if (!isLevelComplete(0)) |
| 970 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 971 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | bool createRenderTarget = (renderTarget || IsRenderTargetUsage(mUsage)); |
| 975 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 976 | TextureStorage *storage = NULL; |
| 977 | gl::Error error = createCompleteStorage(createRenderTarget, &storage); |
| 978 | if (error.isError()) |
| 979 | { |
| 980 | return error; |
| 981 | } |
| 982 | |
| 983 | error = setCompleteTexStorage(storage); |
| 984 | if (error.isError()) |
| 985 | { |
| 986 | SafeDelete(storage); |
| 987 | return error; |
| 988 | } |
| 989 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 990 | ASSERT(mTexStorage); |
| 991 | |
| 992 | // flush image data to the storage |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 993 | error = updateStorage(); |
| 994 | if (error.isError()) |
| 995 | { |
| 996 | return error; |
| 997 | } |
| 998 | |
| 999 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1002 | gl::Error TextureD3D_2D::createCompleteStorage(bool renderTarget, TextureStorage **outTexStorage) const |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1003 | { |
| 1004 | GLsizei width = getBaseLevelWidth(); |
| 1005 | GLsizei height = getBaseLevelHeight(); |
Jamie Madill | c483326 | 2014-09-18 16:18:26 -0400 | [diff] [blame] | 1006 | GLenum internalFormat = getBaseLevelInternalFormat(); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1007 | |
| 1008 | ASSERT(width > 0 && height > 0); |
| 1009 | |
| 1010 | // use existing storage level count, when previously specified by TexStorage*D |
| 1011 | GLint levels = (mTexStorage ? mTexStorage->getLevelCount() : creationLevels(width, height, 1)); |
| 1012 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1013 | // TODO(geofflang): Determine if the texture creation succeeded |
| 1014 | *outTexStorage = mRenderer->createTextureStorage2D(internalFormat, renderTarget, width, height, levels); |
| 1015 | |
| 1016 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1019 | gl::Error TextureD3D_2D::setCompleteTexStorage(TextureStorage *newCompleteTexStorage) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1020 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1021 | if (newCompleteTexStorage && newCompleteTexStorage->isManaged()) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1022 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1023 | for (int level = 0; level < newCompleteTexStorage->getLevelCount(); level++) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1024 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1025 | gl::Error error = mImageArray[level]->setManagedSurface2D(newCompleteTexStorage, level); |
| 1026 | if (error.isError()) |
| 1027 | { |
| 1028 | return error; |
| 1029 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1033 | SafeDelete(mTexStorage); |
| 1034 | mTexStorage = newCompleteTexStorage; |
| 1035 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1036 | mDirtyImages = true; |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1037 | |
| 1038 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1041 | gl::Error TextureD3D_2D::updateStorage() |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1042 | { |
| 1043 | ASSERT(mTexStorage != NULL); |
| 1044 | GLint storageLevels = mTexStorage->getLevelCount(); |
| 1045 | for (int level = 0; level < storageLevels; level++) |
| 1046 | { |
| 1047 | if (mImageArray[level]->isDirty() && isLevelComplete(level)) |
| 1048 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1049 | gl::Error error = updateStorageLevel(level); |
| 1050 | if (error.isError()) |
| 1051 | { |
| 1052 | return error; |
| 1053 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1054 | } |
| 1055 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1056 | |
| 1057 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1060 | gl::Error TextureD3D_2D::updateStorageLevel(int level) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1061 | { |
| 1062 | ASSERT(level <= (int)ArraySize(mImageArray) && mImageArray[level] != NULL); |
| 1063 | ASSERT(isLevelComplete(level)); |
| 1064 | |
| 1065 | if (mImageArray[level]->isDirty()) |
| 1066 | { |
Jamie Madill | 7cb2b9d | 2014-10-02 11:03:13 -0400 | [diff] [blame] | 1067 | gl::ImageIndex index = gl::ImageIndex::Make2D(level); |
| 1068 | gl::Box region(0, 0, 0, getWidth(level), getHeight(level), 1); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1069 | gl::Error error = commitRegion(index, region); |
| 1070 | if (error.isError()) |
| 1071 | { |
| 1072 | return error; |
| 1073 | } |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1074 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1075 | |
| 1076 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1079 | void TextureD3D_2D::redefineImage(GLint level, GLenum internalformat, const gl::Extents &size) |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1080 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1081 | ASSERT(size.depth == 1); |
| 1082 | |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1083 | // If there currently is a corresponding storage texture image, it has these parameters |
| 1084 | const int storageWidth = std::max(1, getBaseLevelWidth() >> level); |
| 1085 | const int storageHeight = std::max(1, getBaseLevelHeight() >> level); |
| 1086 | const GLenum storageFormat = getBaseLevelInternalFormat(); |
| 1087 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1088 | mImageArray[level]->redefine(GL_TEXTURE_2D, internalformat, size, false); |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1089 | |
| 1090 | if (mTexStorage) |
| 1091 | { |
| 1092 | const int storageLevels = mTexStorage->getLevelCount(); |
| 1093 | |
| 1094 | if ((level >= storageLevels && storageLevels != 0) || |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1095 | size.width != storageWidth || |
| 1096 | size.height != storageHeight || |
Brandon Jones | f47bebc | 2014-07-09 14:28:42 -0700 | [diff] [blame] | 1097 | internalformat != storageFormat) // Discard mismatched storage |
| 1098 | { |
| 1099 | for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) |
| 1100 | { |
| 1101 | mImageArray[i]->markDirty(); |
| 1102 | } |
| 1103 | |
| 1104 | SafeDelete(mTexStorage); |
| 1105 | mDirtyImages = true; |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
Jamie Madill | ef4ac5b | 2014-09-29 10:46:11 -0400 | [diff] [blame] | 1110 | gl::ImageIndexIterator TextureD3D_2D::imageIterator() const |
| 1111 | { |
| 1112 | return gl::ImageIndexIterator::Make2D(0, mTexStorage->getLevelCount()); |
| 1113 | } |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1114 | |
Jamie Madill | cb83dc1 | 2014-09-29 10:46:12 -0400 | [diff] [blame] | 1115 | gl::ImageIndex TextureD3D_2D::getImageIndex(GLint mip, GLint /*layer*/) const |
| 1116 | { |
| 1117 | // "layer" does not apply to 2D Textures. |
| 1118 | return gl::ImageIndex::Make2D(mip); |
| 1119 | } |
| 1120 | |
Jamie Madill | 710e577 | 2014-10-20 17:13:53 -0400 | [diff] [blame] | 1121 | bool TextureD3D_2D::isValidIndex(const gl::ImageIndex &index) const |
| 1122 | { |
| 1123 | return (mTexStorage && index.type == GL_TEXTURE_2D && |
| 1124 | index.mipIndex >= 0 && index.mipIndex < mTexStorage->getLevelCount()); |
| 1125 | } |
| 1126 | |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 1127 | TextureD3D_Cube::TextureD3D_Cube(RendererD3D *renderer) |
Jamie Madill | 98553e3 | 2014-09-30 16:33:50 -0400 | [diff] [blame] | 1128 | : TextureD3D(renderer) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1129 | { |
| 1130 | for (int i = 0; i < 6; i++) |
| 1131 | { |
| 1132 | for (int j = 0; j < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++j) |
| 1133 | { |
| 1134 | mImageArray[i][j] = ImageD3D::makeImageD3D(renderer->createImage()); |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | TextureD3D_Cube::~TextureD3D_Cube() |
| 1140 | { |
Austin Kinross | 6982260 | 2014-08-12 15:51:37 -0700 | [diff] [blame] | 1141 | // Delete the Images before the TextureStorage. |
| 1142 | // Images might be relying on the TextureStorage for some of their data. |
| 1143 | // If TextureStorage is deleted before the Images, then their data will be wastefully copied back from the GPU before we delete the Images. |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1144 | for (int i = 0; i < 6; i++) |
| 1145 | { |
| 1146 | for (int j = 0; j < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++j) |
| 1147 | { |
| 1148 | SafeDelete(mImageArray[i][j]); |
| 1149 | } |
| 1150 | } |
Austin Kinross | 6982260 | 2014-08-12 15:51:37 -0700 | [diff] [blame] | 1151 | |
| 1152 | SafeDelete(mTexStorage); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1155 | Image *TextureD3D_Cube::getImage(int level, int layer) const |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1156 | { |
| 1157 | ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
Jamie Madill | eb32a2e | 2014-12-10 14:27:53 -0500 | [diff] [blame] | 1158 | ASSERT(layer >= 0 && layer < 6); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1159 | return mImageArray[layer][level]; |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 1162 | Image *TextureD3D_Cube::getImage(const gl::ImageIndex &index) const |
| 1163 | { |
| 1164 | ASSERT(index.mipIndex < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
Jamie Madill | eb32a2e | 2014-12-10 14:27:53 -0500 | [diff] [blame] | 1165 | ASSERT(index.layerIndex >= 0 && index.layerIndex < 6); |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 1166 | return mImageArray[index.layerIndex][index.mipIndex]; |
| 1167 | } |
| 1168 | |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1169 | GLsizei TextureD3D_Cube::getLayerCount(int level) const |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1170 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1171 | ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
| 1172 | return 6; |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1175 | GLenum TextureD3D_Cube::getInternalFormat(GLint level, GLint layer) const |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1176 | { |
| 1177 | if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS) |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1178 | return mImageArray[layer][level]->getInternalFormat(); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1179 | else |
| 1180 | return GL_NONE; |
| 1181 | } |
| 1182 | |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1183 | bool TextureD3D_Cube::isDepth(GLint level, GLint layer) const |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1184 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1185 | return gl::GetInternalFormatInfo(getInternalFormat(level, layer)).depthBits > 0; |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1188 | gl::Error TextureD3D_Cube::setImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, GLenum format, GLenum type, |
| 1189 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1190 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1191 | ASSERT(size.depth == 1); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1192 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 1193 | GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, type); |
Jamie Madill | ba6bc95 | 2014-10-06 10:56:22 -0400 | [diff] [blame] | 1194 | gl::ImageIndex index = gl::ImageIndex::MakeCube(target, level); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1195 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1196 | redefineImage(index.layerIndex, level, sizedInternalFormat, size); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1197 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1198 | return TextureD3D::setImage(index, type, unpack, pixels); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1201 | gl::Error TextureD3D_Cube::setSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, GLenum type, |
| 1202 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1203 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1204 | ASSERT(area.depth == 1 && area.z == 0); |
| 1205 | |
| 1206 | gl::ImageIndex index = gl::ImageIndex::MakeCube(target, level); |
| 1207 | return TextureD3D::subImage(index, area, format, type, unpack, pixels); |
| 1208 | } |
| 1209 | |
| 1210 | gl::Error TextureD3D_Cube::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, |
| 1211 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
| 1212 | { |
| 1213 | ASSERT(size.depth == 1); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1214 | |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1215 | // compressed formats don't have separate sized internal formats-- we can just use the compressed format directly |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1216 | int faceIndex = gl::TextureCubeMap::targetToLayerIndex(target); |
| 1217 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1218 | redefineImage(faceIndex, level, internalFormat, size); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1219 | |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 1220 | gl::ImageIndex index = gl::ImageIndex::MakeCube(target, level); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1221 | return TextureD3D::setCompressedImage(index, unpack, pixels); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1224 | gl::Error TextureD3D_Cube::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, |
| 1225 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1226 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1227 | ASSERT(area.depth == 1 && area.z == 0); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1228 | |
Jamie Madill | 7cb2b9d | 2014-10-02 11:03:13 -0400 | [diff] [blame] | 1229 | gl::ImageIndex index = gl::ImageIndex::MakeCube(target, level); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1230 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1231 | gl::Error error = TextureD3D::subImageCompressed(index, area, format, unpack, pixels); |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 1232 | if (error.isError()) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1233 | { |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 1234 | return error; |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1235 | } |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 1236 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1237 | return commitRegion(index, area); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1240 | gl::Error TextureD3D_Cube::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat, |
| 1241 | const gl::Framebuffer *source) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1242 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1243 | int faceIndex = gl::TextureCubeMap::targetToLayerIndex(target); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1244 | GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE); |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 1245 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1246 | gl::Extents size(sourceArea.width, sourceArea.height, 1); |
| 1247 | redefineImage(faceIndex, level, sizedInternalFormat, size); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1248 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 1249 | gl::ImageIndex index = gl::ImageIndex::MakeCube(target, level); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1250 | gl::Offset destOffset(0, 0, 0); |
Jamie Madill | 82bf0c5 | 2014-10-03 11:50:53 -0400 | [diff] [blame] | 1251 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 1252 | if (!canCreateRenderTargetForImage(index)) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1253 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1254 | gl::Error error = mImageArray[faceIndex][level]->copy(destOffset, sourceArea, source); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1255 | if (error.isError()) |
| 1256 | { |
| 1257 | return error; |
| 1258 | } |
| 1259 | |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1260 | mDirtyImages = true; |
| 1261 | } |
| 1262 | else |
| 1263 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1264 | gl::Error error = ensureRenderTarget(); |
| 1265 | if (error.isError()) |
| 1266 | { |
| 1267 | return error; |
| 1268 | } |
| 1269 | |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1270 | mImageArray[faceIndex][level]->markClean(); |
| 1271 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1272 | ASSERT(size.width == size.height); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1273 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1274 | if (size.width > 0 && isValidFaceLevel(faceIndex, level)) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1275 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1276 | error = mRenderer->copyImageCube(source, sourceArea, internalFormat, destOffset, mTexStorage, target, level); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1277 | if (error.isError()) |
| 1278 | { |
| 1279 | return error; |
| 1280 | } |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1283 | |
| 1284 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1287 | gl::Error TextureD3D_Cube::copySubImage(GLenum target, size_t level, const gl::Offset &destOffset, const gl::Rectangle &sourceArea, |
| 1288 | const gl::Framebuffer *source) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1289 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1290 | int faceIndex = gl::TextureCubeMap::targetToLayerIndex(target); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1291 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 1292 | gl::ImageIndex index = gl::ImageIndex::MakeCube(target, level); |
Jamie Madill | 82bf0c5 | 2014-10-03 11:50:53 -0400 | [diff] [blame] | 1293 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 1294 | if (!canCreateRenderTargetForImage(index)) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1295 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1296 | gl::Error error = mImageArray[faceIndex][level]->copy(destOffset, sourceArea, source); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1297 | if (error.isError()) |
| 1298 | { |
| 1299 | return error; |
| 1300 | } |
| 1301 | |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1302 | mDirtyImages = true; |
| 1303 | } |
| 1304 | else |
| 1305 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1306 | gl::Error error = ensureRenderTarget(); |
| 1307 | if (error.isError()) |
| 1308 | { |
| 1309 | return error; |
| 1310 | } |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1311 | |
| 1312 | if (isValidFaceLevel(faceIndex, level)) |
| 1313 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1314 | error = updateStorageFaceLevel(faceIndex, level); |
| 1315 | if (error.isError()) |
| 1316 | { |
| 1317 | return error; |
| 1318 | } |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1319 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1320 | error = mRenderer->copyImageCube(source, sourceArea, gl::GetInternalFormatInfo(getBaseLevelInternalFormat()).format, |
| 1321 | destOffset, mTexStorage, target, level); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1322 | if (error.isError()) |
| 1323 | { |
| 1324 | return error; |
| 1325 | } |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1326 | } |
| 1327 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1328 | |
| 1329 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1332 | gl::Error TextureD3D_Cube::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1333 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1334 | ASSERT(size.width == size.height); |
| 1335 | ASSERT(size.depth == 1); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1336 | |
Geoff Lang | 8fed1f7 | 2015-01-09 11:09:33 -0500 | [diff] [blame^] | 1337 | for (size_t level = 0; level < levels; level++) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1338 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1339 | GLsizei mipSize = std::max(1, size.width >> level); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1340 | for (int faceIndex = 0; faceIndex < 6; faceIndex++) |
| 1341 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1342 | mImageArray[faceIndex][level]->redefine(GL_TEXTURE_CUBE_MAP, internalFormat, gl::Extents(mipSize, mipSize, 1), true); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | for (int level = levels; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++) |
| 1347 | { |
| 1348 | for (int faceIndex = 0; faceIndex < 6; faceIndex++) |
| 1349 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1350 | mImageArray[faceIndex][level]->redefine(GL_TEXTURE_CUBE_MAP, GL_NONE, gl::Extents(0, 0, 0), true); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Geoff Lang | 1f8532b | 2014-09-05 09:46:13 -0400 | [diff] [blame] | 1354 | // TODO(geofflang): Verify storage creation had no errors |
Jamie Madill | c483326 | 2014-09-18 16:18:26 -0400 | [diff] [blame] | 1355 | bool renderTarget = IsRenderTargetUsage(mUsage); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1356 | TextureStorage *storage = mRenderer->createTextureStorageCube(internalFormat, renderTarget, size.width, levels); |
Geoff Lang | 1f8532b | 2014-09-05 09:46:13 -0400 | [diff] [blame] | 1357 | |
| 1358 | gl::Error error = setCompleteTexStorage(storage); |
| 1359 | if (error.isError()) |
| 1360 | { |
| 1361 | SafeDelete(storage); |
| 1362 | return error; |
| 1363 | } |
| 1364 | |
| 1365 | mImmutable = true; |
| 1366 | |
| 1367 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1370 | // Tests for cube texture completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81. |
| 1371 | bool TextureD3D_Cube::isCubeComplete() const |
| 1372 | { |
| 1373 | int baseWidth = getBaseLevelWidth(); |
| 1374 | int baseHeight = getBaseLevelHeight(); |
| 1375 | GLenum baseFormat = getBaseLevelInternalFormat(); |
| 1376 | |
| 1377 | if (baseWidth <= 0 || baseWidth != baseHeight) |
| 1378 | { |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
| 1382 | for (int faceIndex = 1; faceIndex < 6; faceIndex++) |
| 1383 | { |
| 1384 | const ImageD3D &faceBaseImage = *mImageArray[faceIndex][0]; |
| 1385 | |
| 1386 | if (faceBaseImage.getWidth() != baseWidth || |
| 1387 | faceBaseImage.getHeight() != baseHeight || |
| 1388 | faceBaseImage.getInternalFormat() != baseFormat ) |
| 1389 | { |
| 1390 | return false; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | return true; |
| 1395 | } |
| 1396 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 1397 | void TextureD3D_Cube::bindTexImage(egl::Surface *surface) |
| 1398 | { |
| 1399 | UNREACHABLE(); |
| 1400 | } |
| 1401 | |
| 1402 | void TextureD3D_Cube::releaseTexImage() |
| 1403 | { |
| 1404 | UNREACHABLE(); |
| 1405 | } |
| 1406 | |
| 1407 | |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 1408 | void TextureD3D_Cube::initMipmapsImages() |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1409 | { |
| 1410 | // Purge array levels 1 through q and reset them to represent the generated mipmap levels. |
| 1411 | int levelCount = mipLevels(); |
| 1412 | for (int faceIndex = 0; faceIndex < 6; faceIndex++) |
| 1413 | { |
| 1414 | for (int level = 1; level < levelCount; level++) |
| 1415 | { |
| 1416 | int faceLevelSize = (std::max(mImageArray[faceIndex][0]->getWidth() >> level, 1)); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1417 | redefineImage(faceIndex, level, mImageArray[faceIndex][0]->getInternalFormat(), |
| 1418 | gl::Extents(faceLevelSize, faceLevelSize, 1)); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1419 | } |
| 1420 | } |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 1423 | unsigned int TextureD3D_Cube::getRenderTargetSerial(const gl::ImageIndex &index) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1424 | { |
Geoff Lang | 8cb85c4 | 2015-01-07 13:23:26 -0500 | [diff] [blame] | 1425 | return (!ensureRenderTarget().isError() ? mTexStorage->getRenderTargetSerial(index) : 0); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1428 | gl::Error TextureD3D_Cube::getRenderTarget(const gl::ImageIndex &index, RenderTarget **outRT) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1429 | { |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 1430 | ASSERT(gl::IsCubemapTextureTarget(index.type)); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1431 | |
| 1432 | // ensure the underlying texture is created |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1433 | gl::Error error = ensureRenderTarget(); |
| 1434 | if (error.isError()) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1435 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1436 | return error; |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1439 | error = updateStorageFaceLevel(index.layerIndex, index.mipIndex); |
| 1440 | if (error.isError()) |
| 1441 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1442 | return error; |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1443 | } |
| 1444 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1445 | return mTexStorage->getRenderTarget(index, outRT); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1448 | gl::Error TextureD3D_Cube::initializeStorage(bool renderTarget) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1449 | { |
| 1450 | // Only initialize the first time this texture is used as a render target or shader resource |
| 1451 | if (mTexStorage) |
| 1452 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1453 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | // do not attempt to create storage for nonexistant data |
| 1457 | if (!isFaceLevelComplete(0, 0)) |
| 1458 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1459 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | bool createRenderTarget = (renderTarget || IsRenderTargetUsage(mUsage)); |
| 1463 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1464 | TextureStorage *storage = NULL; |
| 1465 | gl::Error error = createCompleteStorage(createRenderTarget, &storage); |
| 1466 | if (error.isError()) |
| 1467 | { |
| 1468 | return error; |
| 1469 | } |
| 1470 | |
| 1471 | error = setCompleteTexStorage(storage); |
| 1472 | if (error.isError()) |
| 1473 | { |
| 1474 | SafeDelete(storage); |
| 1475 | return error; |
| 1476 | } |
| 1477 | |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1478 | ASSERT(mTexStorage); |
| 1479 | |
| 1480 | // flush image data to the storage |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1481 | error = updateStorage(); |
| 1482 | if (error.isError()) |
| 1483 | { |
| 1484 | return error; |
| 1485 | } |
| 1486 | |
| 1487 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1490 | gl::Error TextureD3D_Cube::createCompleteStorage(bool renderTarget, TextureStorage **outTexStorage) const |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1491 | { |
| 1492 | GLsizei size = getBaseLevelWidth(); |
| 1493 | |
| 1494 | ASSERT(size > 0); |
| 1495 | |
| 1496 | // use existing storage level count, when previously specified by TexStorage*D |
| 1497 | GLint levels = (mTexStorage ? mTexStorage->getLevelCount() : creationLevels(size, size, 1)); |
| 1498 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1499 | // TODO (geofflang): detect if storage creation succeeded |
| 1500 | *outTexStorage = mRenderer->createTextureStorageCube(getBaseLevelInternalFormat(), renderTarget, size, levels); |
| 1501 | |
| 1502 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1505 | gl::Error TextureD3D_Cube::setCompleteTexStorage(TextureStorage *newCompleteTexStorage) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1506 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1507 | if (newCompleteTexStorage && newCompleteTexStorage->isManaged()) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1508 | { |
| 1509 | for (int faceIndex = 0; faceIndex < 6; faceIndex++) |
| 1510 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1511 | for (int level = 0; level < newCompleteTexStorage->getLevelCount(); level++) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1512 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1513 | gl::Error error = mImageArray[faceIndex][level]->setManagedSurfaceCube(newCompleteTexStorage, faceIndex, level); |
| 1514 | if (error.isError()) |
| 1515 | { |
| 1516 | return error; |
| 1517 | } |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | } |
| 1521 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1522 | SafeDelete(mTexStorage); |
| 1523 | mTexStorage = newCompleteTexStorage; |
| 1524 | |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1525 | mDirtyImages = true; |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1526 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1529 | gl::Error TextureD3D_Cube::updateStorage() |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1530 | { |
| 1531 | ASSERT(mTexStorage != NULL); |
| 1532 | GLint storageLevels = mTexStorage->getLevelCount(); |
| 1533 | for (int face = 0; face < 6; face++) |
| 1534 | { |
| 1535 | for (int level = 0; level < storageLevels; level++) |
| 1536 | { |
| 1537 | if (mImageArray[face][level]->isDirty() && isFaceLevelComplete(face, level)) |
| 1538 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1539 | gl::Error error = updateStorageFaceLevel(face, level); |
| 1540 | if (error.isError()) |
| 1541 | { |
| 1542 | return error; |
| 1543 | } |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1547 | |
| 1548 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1551 | bool TextureD3D_Cube::isValidFaceLevel(int faceIndex, int level) const |
| 1552 | { |
| 1553 | return (mTexStorage ? (level >= 0 && level < mTexStorage->getLevelCount()) : 0); |
| 1554 | } |
| 1555 | |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1556 | bool TextureD3D_Cube::isFaceLevelComplete(int faceIndex, int level) const |
| 1557 | { |
| 1558 | ASSERT(level >= 0 && faceIndex < 6 && level < (int)ArraySize(mImageArray[faceIndex]) && mImageArray[faceIndex][level] != NULL); |
| 1559 | |
| 1560 | if (isImmutable()) |
| 1561 | { |
| 1562 | return true; |
| 1563 | } |
| 1564 | |
| 1565 | int baseSize = getBaseLevelWidth(); |
| 1566 | |
| 1567 | if (baseSize <= 0) |
| 1568 | { |
| 1569 | return false; |
| 1570 | } |
| 1571 | |
| 1572 | // "isCubeComplete" checks for base level completeness and we must call that |
| 1573 | // to determine if any face at level 0 is complete. We omit that check here |
| 1574 | // to avoid re-checking cube-completeness for every face at level 0. |
| 1575 | if (level == 0) |
| 1576 | { |
| 1577 | return true; |
| 1578 | } |
| 1579 | |
| 1580 | // Check that non-zero levels are consistent with the base level. |
| 1581 | const ImageD3D *faceLevelImage = mImageArray[faceIndex][level]; |
| 1582 | |
| 1583 | if (faceLevelImage->getInternalFormat() != getBaseLevelInternalFormat()) |
| 1584 | { |
| 1585 | return false; |
| 1586 | } |
| 1587 | |
| 1588 | if (faceLevelImage->getWidth() != std::max(1, baseSize >> level)) |
| 1589 | { |
| 1590 | return false; |
| 1591 | } |
| 1592 | |
| 1593 | return true; |
| 1594 | } |
| 1595 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 1596 | bool TextureD3D_Cube::isImageComplete(const gl::ImageIndex &index) const |
| 1597 | { |
| 1598 | return isFaceLevelComplete(index.layerIndex, index.mipIndex); |
| 1599 | } |
| 1600 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1601 | gl::Error TextureD3D_Cube::updateStorageFaceLevel(int faceIndex, int level) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1602 | { |
| 1603 | ASSERT(level >= 0 && faceIndex < 6 && level < (int)ArraySize(mImageArray[faceIndex]) && mImageArray[faceIndex][level] != NULL); |
| 1604 | ImageD3D *image = mImageArray[faceIndex][level]; |
| 1605 | |
| 1606 | if (image->isDirty()) |
| 1607 | { |
Jamie Madill | 7cb2b9d | 2014-10-02 11:03:13 -0400 | [diff] [blame] | 1608 | GLenum faceTarget = gl::TextureCubeMap::layerIndexToTarget(faceIndex); |
| 1609 | gl::ImageIndex index = gl::ImageIndex::MakeCube(faceTarget, level); |
| 1610 | gl::Box region(0, 0, 0, image->getWidth(), image->getHeight(), 1); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1611 | gl::Error error = commitRegion(index, region); |
| 1612 | if (error.isError()) |
| 1613 | { |
| 1614 | return error; |
| 1615 | } |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1616 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1617 | |
| 1618 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1621 | void TextureD3D_Cube::redefineImage(int faceIndex, GLint level, GLenum internalformat, const gl::Extents &size) |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1622 | { |
| 1623 | // If there currently is a corresponding storage texture image, it has these parameters |
| 1624 | const int storageWidth = std::max(1, getBaseLevelWidth() >> level); |
| 1625 | const int storageHeight = std::max(1, getBaseLevelHeight() >> level); |
| 1626 | const GLenum storageFormat = getBaseLevelInternalFormat(); |
| 1627 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1628 | mImageArray[faceIndex][level]->redefine(GL_TEXTURE_CUBE_MAP, internalformat, size, false); |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1629 | |
| 1630 | if (mTexStorage) |
| 1631 | { |
| 1632 | const int storageLevels = mTexStorage->getLevelCount(); |
| 1633 | |
| 1634 | if ((level >= storageLevels && storageLevels != 0) || |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1635 | size.width != storageWidth || |
| 1636 | size.height != storageHeight || |
Brandon Jones | 0511e80 | 2014-07-14 16:27:26 -0700 | [diff] [blame] | 1637 | internalformat != storageFormat) // Discard mismatched storage |
| 1638 | { |
| 1639 | for (int level = 0; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++) |
| 1640 | { |
| 1641 | for (int faceIndex = 0; faceIndex < 6; faceIndex++) |
| 1642 | { |
| 1643 | mImageArray[faceIndex][level]->markDirty(); |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | SafeDelete(mTexStorage); |
| 1648 | |
| 1649 | mDirtyImages = true; |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | |
Jamie Madill | ef4ac5b | 2014-09-29 10:46:11 -0400 | [diff] [blame] | 1654 | gl::ImageIndexIterator TextureD3D_Cube::imageIterator() const |
| 1655 | { |
| 1656 | return gl::ImageIndexIterator::MakeCube(0, mTexStorage->getLevelCount()); |
| 1657 | } |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1658 | |
Jamie Madill | cb83dc1 | 2014-09-29 10:46:12 -0400 | [diff] [blame] | 1659 | gl::ImageIndex TextureD3D_Cube::getImageIndex(GLint mip, GLint layer) const |
| 1660 | { |
| 1661 | // The "layer" of the image index corresponds to the cube face |
| 1662 | return gl::ImageIndex::MakeCube(gl::TextureCubeMap::layerIndexToTarget(layer), mip); |
| 1663 | } |
| 1664 | |
Jamie Madill | 710e577 | 2014-10-20 17:13:53 -0400 | [diff] [blame] | 1665 | bool TextureD3D_Cube::isValidIndex(const gl::ImageIndex &index) const |
| 1666 | { |
| 1667 | return (mTexStorage && gl::IsCubemapTextureTarget(index.type) && |
| 1668 | index.mipIndex >= 0 && index.mipIndex < mTexStorage->getLevelCount()); |
| 1669 | } |
| 1670 | |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 1671 | TextureD3D_3D::TextureD3D_3D(RendererD3D *renderer) |
Jamie Madill | 98553e3 | 2014-09-30 16:33:50 -0400 | [diff] [blame] | 1672 | : TextureD3D(renderer) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1673 | { |
| 1674 | for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i) |
| 1675 | { |
| 1676 | mImageArray[i] = ImageD3D::makeImageD3D(renderer->createImage()); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | TextureD3D_3D::~TextureD3D_3D() |
| 1681 | { |
Austin Kinross | 6982260 | 2014-08-12 15:51:37 -0700 | [diff] [blame] | 1682 | // Delete the Images before the TextureStorage. |
| 1683 | // Images might be relying on the TextureStorage for some of their data. |
| 1684 | // If TextureStorage is deleted before the Images, then their data will be wastefully copied back from the GPU before we delete the Images. |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1685 | for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i) |
| 1686 | { |
| 1687 | delete mImageArray[i]; |
| 1688 | } |
Austin Kinross | 6982260 | 2014-08-12 15:51:37 -0700 | [diff] [blame] | 1689 | |
| 1690 | SafeDelete(mTexStorage); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1693 | Image *TextureD3D_3D::getImage(int level, int layer) const |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1694 | { |
| 1695 | ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1696 | ASSERT(layer == 0); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1697 | return mImageArray[level]; |
| 1698 | } |
| 1699 | |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 1700 | Image *TextureD3D_3D::getImage(const gl::ImageIndex &index) const |
| 1701 | { |
| 1702 | ASSERT(index.mipIndex < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 1703 | ASSERT(!index.hasLayer()); |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 1704 | ASSERT(index.type == GL_TEXTURE_3D); |
| 1705 | return mImageArray[index.mipIndex]; |
| 1706 | } |
| 1707 | |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1708 | GLsizei TextureD3D_3D::getLayerCount(int level) const |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1709 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1710 | ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
| 1711 | return 1; |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1714 | GLsizei TextureD3D_3D::getWidth(GLint level) const |
| 1715 | { |
| 1716 | if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS) |
| 1717 | return mImageArray[level]->getWidth(); |
| 1718 | else |
| 1719 | return 0; |
| 1720 | } |
| 1721 | |
| 1722 | GLsizei TextureD3D_3D::getHeight(GLint level) const |
| 1723 | { |
| 1724 | if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS) |
| 1725 | return mImageArray[level]->getHeight(); |
| 1726 | else |
| 1727 | return 0; |
| 1728 | } |
| 1729 | |
| 1730 | GLsizei TextureD3D_3D::getDepth(GLint level) const |
| 1731 | { |
| 1732 | if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS) |
| 1733 | return mImageArray[level]->getDepth(); |
| 1734 | else |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | GLenum TextureD3D_3D::getInternalFormat(GLint level) const |
| 1739 | { |
| 1740 | if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS) |
| 1741 | return mImageArray[level]->getInternalFormat(); |
| 1742 | else |
| 1743 | return GL_NONE; |
| 1744 | } |
| 1745 | |
| 1746 | bool TextureD3D_3D::isDepth(GLint level) const |
| 1747 | { |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 1748 | return gl::GetInternalFormatInfo(getInternalFormat(level)).depthBits > 0; |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1751 | gl::Error TextureD3D_3D::setImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, GLenum format, GLenum type, |
| 1752 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1753 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1754 | ASSERT(target == GL_TEXTURE_3D); |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 1755 | GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, type); |
| 1756 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1757 | redefineImage(level, sizedInternalFormat, size); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1758 | |
| 1759 | bool fastUnpacked = false; |
| 1760 | |
Jamie Madill | ba6bc95 | 2014-10-06 10:56:22 -0400 | [diff] [blame] | 1761 | gl::ImageIndex index = gl::ImageIndex::Make3D(level); |
| 1762 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1763 | // Attempt a fast gpu copy of the pixel data to the surface if the app bound an unpack buffer |
| 1764 | if (isFastUnpackable(unpack, sizedInternalFormat)) |
| 1765 | { |
| 1766 | // Will try to create RT storage if it does not exist |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1767 | RenderTarget *destRenderTarget = NULL; |
| 1768 | gl::Error error = getRenderTarget(index, &destRenderTarget); |
| 1769 | if (error.isError()) |
| 1770 | { |
| 1771 | return error; |
| 1772 | } |
| 1773 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1774 | gl::Box destArea(0, 0, 0, getWidth(level), getHeight(level), getDepth(level)); |
| 1775 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1776 | error = fastUnpackPixels(unpack, pixels, destArea, sizedInternalFormat, type, destRenderTarget); |
| 1777 | if (error.isError()) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1778 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1779 | return error; |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1780 | } |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1781 | |
| 1782 | // Ensure we don't overwrite our newly initialized data |
| 1783 | mImageArray[level]->markClean(); |
| 1784 | |
| 1785 | fastUnpacked = true; |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | if (!fastUnpacked) |
| 1789 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1790 | gl::Error error = TextureD3D::setImage(index, type, unpack, pixels); |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 1791 | if (error.isError()) |
| 1792 | { |
| 1793 | return error; |
| 1794 | } |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1795 | } |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 1796 | |
| 1797 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1800 | gl::Error TextureD3D_3D::setSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, GLenum type, |
| 1801 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1802 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1803 | ASSERT(target == GL_TEXTURE_3D); |
| 1804 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1805 | bool fastUnpacked = false; |
| 1806 | |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 1807 | gl::ImageIndex index = gl::ImageIndex::Make3D(level); |
| 1808 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1809 | // Attempt a fast gpu copy of the pixel data to the surface if the app bound an unpack buffer |
| 1810 | if (isFastUnpackable(unpack, getInternalFormat(level))) |
| 1811 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1812 | RenderTarget *destRenderTarget = NULL; |
| 1813 | gl::Error error = getRenderTarget(index, &destRenderTarget); |
| 1814 | if (error.isError()) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1815 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1816 | return error; |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1817 | } |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1818 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1819 | error = fastUnpackPixels(unpack, pixels, area, getInternalFormat(level), type, destRenderTarget); |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1820 | if (error.isError()) |
| 1821 | { |
| 1822 | return error; |
| 1823 | } |
| 1824 | |
| 1825 | // Ensure we don't overwrite our newly initialized data |
| 1826 | mImageArray[level]->markClean(); |
| 1827 | |
| 1828 | fastUnpacked = true; |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 1831 | if (!fastUnpacked) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1832 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1833 | return TextureD3D::subImage(index, area, format, type, unpack, pixels); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1834 | } |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 1835 | |
| 1836 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1839 | gl::Error TextureD3D_3D::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, |
| 1840 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1841 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1842 | ASSERT(target == GL_TEXTURE_3D); |
| 1843 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1844 | // compressed formats don't have separate sized internal formats-- we can just use the compressed format directly |
| 1845 | redefineImage(level, internalFormat, size); |
| 1846 | |
| 1847 | gl::ImageIndex index = gl::ImageIndex::Make3D(level); |
| 1848 | return TextureD3D::setCompressedImage(index, unpack, pixels); |
| 1849 | } |
| 1850 | |
| 1851 | gl::Error TextureD3D_3D::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, |
| 1852 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
| 1853 | { |
| 1854 | ASSERT(target == GL_TEXTURE_3D); |
| 1855 | |
| 1856 | gl::ImageIndex index = gl::ImageIndex::Make3D(level); |
| 1857 | gl::Error error = TextureD3D::subImageCompressed(index, area, format, unpack, pixels); |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 1858 | if (error.isError()) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1859 | { |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 1860 | return error; |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1861 | } |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 1862 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1863 | return commitRegion(index, area); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1866 | gl::Error TextureD3D_3D::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat, |
| 1867 | const gl::Framebuffer *source) |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1868 | { |
| 1869 | UNIMPLEMENTED(); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1870 | return gl::Error(GL_INVALID_OPERATION, "Copying 3D textures is unimplemented."); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1873 | gl::Error TextureD3D_3D::copySubImage(GLenum target, size_t level, const gl::Offset &destOffset, const gl::Rectangle &sourceArea, |
| 1874 | const gl::Framebuffer *source) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1875 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1876 | ASSERT(target == GL_TEXTURE_3D); |
| 1877 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 1878 | gl::ImageIndex index = gl::ImageIndex::Make3D(level); |
Jamie Madill | 82bf0c5 | 2014-10-03 11:50:53 -0400 | [diff] [blame] | 1879 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 1880 | if (canCreateRenderTargetForImage(index)) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1881 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1882 | gl::Error error = mImageArray[level]->copy(destOffset, sourceArea, source); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1883 | if (error.isError()) |
| 1884 | { |
| 1885 | return error; |
| 1886 | } |
| 1887 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1888 | mDirtyImages = true; |
| 1889 | } |
| 1890 | else |
| 1891 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1892 | gl::Error error = ensureRenderTarget(); |
| 1893 | if (error.isError()) |
| 1894 | { |
| 1895 | return error; |
| 1896 | } |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1897 | |
| 1898 | if (isValidLevel(level)) |
| 1899 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1900 | error = updateStorageLevel(level); |
| 1901 | if (error.isError()) |
| 1902 | { |
| 1903 | return error; |
| 1904 | } |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1905 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1906 | error = mRenderer->copyImage3D(source, sourceArea, |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1907 | gl::GetInternalFormatInfo(getBaseLevelInternalFormat()).format, |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1908 | destOffset, mTexStorage, level); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1909 | if (error.isError()) |
| 1910 | { |
| 1911 | return error; |
| 1912 | } |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1913 | } |
| 1914 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1915 | |
| 1916 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1919 | gl::Error TextureD3D_3D::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1920 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 1921 | ASSERT(target == GL_TEXTURE_3D); |
| 1922 | |
Geoff Lang | 8fed1f7 | 2015-01-09 11:09:33 -0500 | [diff] [blame^] | 1923 | for (size_t level = 0; level < levels; level++) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1924 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1925 | gl::Extents levelSize(std::max(1, size.width >> level), |
| 1926 | std::max(1, size.height >> level), |
| 1927 | std::max(1, size.depth >> level)); |
| 1928 | mImageArray[level]->redefine(GL_TEXTURE_3D, internalFormat, levelSize, true); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | for (int level = levels; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++) |
| 1932 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1933 | mImageArray[level]->redefine(GL_TEXTURE_3D, GL_NONE, gl::Extents(0, 0, 0), true); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1934 | } |
| 1935 | |
Geoff Lang | 1f8532b | 2014-09-05 09:46:13 -0400 | [diff] [blame] | 1936 | // TODO(geofflang): Verify storage creation had no errors |
Jamie Madill | c483326 | 2014-09-18 16:18:26 -0400 | [diff] [blame] | 1937 | bool renderTarget = IsRenderTargetUsage(mUsage); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1938 | TextureStorage *storage = mRenderer->createTextureStorage3D(internalFormat, renderTarget, size.width, size.height, size.depth, levels); |
Geoff Lang | 1f8532b | 2014-09-05 09:46:13 -0400 | [diff] [blame] | 1939 | |
| 1940 | gl::Error error = setCompleteTexStorage(storage); |
| 1941 | if (error.isError()) |
| 1942 | { |
| 1943 | SafeDelete(storage); |
| 1944 | return error; |
| 1945 | } |
| 1946 | |
| 1947 | mImmutable = true; |
| 1948 | |
| 1949 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 1952 | void TextureD3D_3D::bindTexImage(egl::Surface *surface) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1953 | { |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 1954 | UNREACHABLE(); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 1957 | void TextureD3D_3D::releaseTexImage() |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1958 | { |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 1959 | UNREACHABLE(); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 1962 | |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 1963 | void TextureD3D_3D::initMipmapsImages() |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1964 | { |
| 1965 | // Purge array levels 1 through q and reset them to represent the generated mipmap levels. |
| 1966 | int levelCount = mipLevels(); |
| 1967 | for (int level = 1; level < levelCount; level++) |
| 1968 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 1969 | gl::Extents levelSize(std::max(getBaseLevelWidth() >> level, 1), |
| 1970 | std::max(getBaseLevelHeight() >> level, 1), |
| 1971 | std::max(getBaseLevelDepth() >> level, 1)); |
| 1972 | redefineImage(level, getBaseLevelInternalFormat(), levelSize); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1973 | } |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 1976 | unsigned int TextureD3D_3D::getRenderTargetSerial(const gl::ImageIndex &index) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1977 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1978 | return (!ensureRenderTarget().isError() ? mTexStorage->getRenderTargetSerial(index) : 0); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1981 | gl::Error TextureD3D_3D::getRenderTarget(const gl::ImageIndex &index, RenderTarget **outRT) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1982 | { |
| 1983 | // ensure the underlying texture is created |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1984 | gl::Error error = ensureRenderTarget(); |
| 1985 | if (error.isError()) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1986 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1987 | return error; |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 1990 | if (index.hasLayer()) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 1991 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1992 | error = updateStorage(); |
| 1993 | if (error.isError()) |
| 1994 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 1995 | return error; |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 1996 | } |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 1997 | } |
| 1998 | else |
| 1999 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2000 | error = updateStorageLevel(index.mipIndex); |
| 2001 | if (error.isError()) |
| 2002 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 2003 | return error; |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2004 | } |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 2007 | return mTexStorage->getRenderTarget(index, outRT); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2008 | } |
| 2009 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2010 | gl::Error TextureD3D_3D::initializeStorage(bool renderTarget) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2011 | { |
| 2012 | // Only initialize the first time this texture is used as a render target or shader resource |
| 2013 | if (mTexStorage) |
| 2014 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2015 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | // do not attempt to create storage for nonexistant data |
| 2019 | if (!isLevelComplete(0)) |
| 2020 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2021 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | bool createRenderTarget = (renderTarget || mUsage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE); |
| 2025 | |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 2026 | TextureStorage *storage = NULL; |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2027 | gl::Error error = createCompleteStorage(createRenderTarget, &storage); |
| 2028 | if (error.isError()) |
| 2029 | { |
| 2030 | return error; |
| 2031 | } |
| 2032 | |
| 2033 | error = setCompleteTexStorage(storage); |
| 2034 | if (error.isError()) |
| 2035 | { |
| 2036 | SafeDelete(storage); |
| 2037 | return error; |
| 2038 | } |
| 2039 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2040 | ASSERT(mTexStorage); |
| 2041 | |
| 2042 | // flush image data to the storage |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2043 | error = updateStorage(); |
| 2044 | if (error.isError()) |
| 2045 | { |
| 2046 | return error; |
| 2047 | } |
| 2048 | |
| 2049 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2052 | gl::Error TextureD3D_3D::createCompleteStorage(bool renderTarget, TextureStorage **outStorage) const |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2053 | { |
| 2054 | GLsizei width = getBaseLevelWidth(); |
| 2055 | GLsizei height = getBaseLevelHeight(); |
| 2056 | GLsizei depth = getBaseLevelDepth(); |
Jamie Madill | c483326 | 2014-09-18 16:18:26 -0400 | [diff] [blame] | 2057 | GLenum internalFormat = getBaseLevelInternalFormat(); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2058 | |
| 2059 | ASSERT(width > 0 && height > 0 && depth > 0); |
| 2060 | |
| 2061 | // use existing storage level count, when previously specified by TexStorage*D |
| 2062 | GLint levels = (mTexStorage ? mTexStorage->getLevelCount() : creationLevels(width, height, depth)); |
| 2063 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2064 | // TODO: Verify creation of the storage succeeded |
| 2065 | *outStorage = mRenderer->createTextureStorage3D(internalFormat, renderTarget, width, height, depth, levels); |
| 2066 | |
| 2067 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2068 | } |
| 2069 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2070 | gl::Error TextureD3D_3D::setCompleteTexStorage(TextureStorage *newCompleteTexStorage) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2071 | { |
| 2072 | SafeDelete(mTexStorage); |
| 2073 | mTexStorage = newCompleteTexStorage; |
| 2074 | mDirtyImages = true; |
| 2075 | |
| 2076 | // We do not support managed 3D storage, as that is D3D9/ES2-only |
| 2077 | ASSERT(!mTexStorage->isManaged()); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2078 | |
| 2079 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2080 | } |
| 2081 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2082 | gl::Error TextureD3D_3D::updateStorage() |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2083 | { |
| 2084 | ASSERT(mTexStorage != NULL); |
| 2085 | GLint storageLevels = mTexStorage->getLevelCount(); |
| 2086 | for (int level = 0; level < storageLevels; level++) |
| 2087 | { |
| 2088 | if (mImageArray[level]->isDirty() && isLevelComplete(level)) |
| 2089 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2090 | gl::Error error = updateStorageLevel(level); |
| 2091 | if (error.isError()) |
| 2092 | { |
| 2093 | return error; |
| 2094 | } |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2095 | } |
| 2096 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2097 | |
| 2098 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2101 | bool TextureD3D_3D::isValidLevel(int level) const |
| 2102 | { |
| 2103 | return (mTexStorage ? (level >= 0 && level < mTexStorage->getLevelCount()) : 0); |
| 2104 | } |
| 2105 | |
| 2106 | bool TextureD3D_3D::isLevelComplete(int level) const |
| 2107 | { |
| 2108 | ASSERT(level >= 0 && level < (int)ArraySize(mImageArray) && mImageArray[level] != NULL); |
| 2109 | |
| 2110 | if (isImmutable()) |
| 2111 | { |
| 2112 | return true; |
| 2113 | } |
| 2114 | |
| 2115 | GLsizei width = getBaseLevelWidth(); |
| 2116 | GLsizei height = getBaseLevelHeight(); |
| 2117 | GLsizei depth = getBaseLevelDepth(); |
| 2118 | |
| 2119 | if (width <= 0 || height <= 0 || depth <= 0) |
| 2120 | { |
| 2121 | return false; |
| 2122 | } |
| 2123 | |
| 2124 | if (level == 0) |
| 2125 | { |
| 2126 | return true; |
| 2127 | } |
| 2128 | |
| 2129 | ImageD3D *levelImage = mImageArray[level]; |
| 2130 | |
| 2131 | if (levelImage->getInternalFormat() != getBaseLevelInternalFormat()) |
| 2132 | { |
| 2133 | return false; |
| 2134 | } |
| 2135 | |
| 2136 | if (levelImage->getWidth() != std::max(1, width >> level)) |
| 2137 | { |
| 2138 | return false; |
| 2139 | } |
| 2140 | |
| 2141 | if (levelImage->getHeight() != std::max(1, height >> level)) |
| 2142 | { |
| 2143 | return false; |
| 2144 | } |
| 2145 | |
| 2146 | if (levelImage->getDepth() != std::max(1, depth >> level)) |
| 2147 | { |
| 2148 | return false; |
| 2149 | } |
| 2150 | |
| 2151 | return true; |
| 2152 | } |
| 2153 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 2154 | bool TextureD3D_3D::isImageComplete(const gl::ImageIndex &index) const |
| 2155 | { |
| 2156 | return isLevelComplete(index.mipIndex); |
| 2157 | } |
| 2158 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2159 | gl::Error TextureD3D_3D::updateStorageLevel(int level) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2160 | { |
| 2161 | ASSERT(level >= 0 && level < (int)ArraySize(mImageArray) && mImageArray[level] != NULL); |
| 2162 | ASSERT(isLevelComplete(level)); |
| 2163 | |
| 2164 | if (mImageArray[level]->isDirty()) |
| 2165 | { |
Jamie Madill | 7cb2b9d | 2014-10-02 11:03:13 -0400 | [diff] [blame] | 2166 | gl::ImageIndex index = gl::ImageIndex::Make3D(level); |
| 2167 | gl::Box region(0, 0, 0, getWidth(level), getHeight(level), getDepth(level)); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2168 | gl::Error error = commitRegion(index, region); |
| 2169 | if (error.isError()) |
| 2170 | { |
| 2171 | return error; |
| 2172 | } |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2173 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2174 | |
| 2175 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2178 | void TextureD3D_3D::redefineImage(GLint level, GLenum internalformat, const gl::Extents &size) |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2179 | { |
| 2180 | // If there currently is a corresponding storage texture image, it has these parameters |
| 2181 | const int storageWidth = std::max(1, getBaseLevelWidth() >> level); |
| 2182 | const int storageHeight = std::max(1, getBaseLevelHeight() >> level); |
| 2183 | const int storageDepth = std::max(1, getBaseLevelDepth() >> level); |
| 2184 | const GLenum storageFormat = getBaseLevelInternalFormat(); |
| 2185 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2186 | mImageArray[level]->redefine(GL_TEXTURE_3D, internalformat, size, false); |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2187 | |
| 2188 | if (mTexStorage) |
| 2189 | { |
| 2190 | const int storageLevels = mTexStorage->getLevelCount(); |
| 2191 | |
| 2192 | if ((level >= storageLevels && storageLevels != 0) || |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2193 | size.width != storageWidth || |
| 2194 | size.height != storageHeight || |
| 2195 | size.depth != storageDepth || |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2196 | internalformat != storageFormat) // Discard mismatched storage |
| 2197 | { |
| 2198 | for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) |
| 2199 | { |
| 2200 | mImageArray[i]->markDirty(); |
| 2201 | } |
| 2202 | |
| 2203 | SafeDelete(mTexStorage); |
| 2204 | mDirtyImages = true; |
| 2205 | } |
| 2206 | } |
| 2207 | } |
| 2208 | |
Jamie Madill | ef4ac5b | 2014-09-29 10:46:11 -0400 | [diff] [blame] | 2209 | gl::ImageIndexIterator TextureD3D_3D::imageIterator() const |
| 2210 | { |
| 2211 | return gl::ImageIndexIterator::Make3D(0, mTexStorage->getLevelCount(), |
| 2212 | gl::ImageIndex::ENTIRE_LEVEL, gl::ImageIndex::ENTIRE_LEVEL); |
| 2213 | } |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2214 | |
Jamie Madill | cb83dc1 | 2014-09-29 10:46:12 -0400 | [diff] [blame] | 2215 | gl::ImageIndex TextureD3D_3D::getImageIndex(GLint mip, GLint /*layer*/) const |
| 2216 | { |
| 2217 | // The "layer" here does not apply to 3D images. We use one Image per mip. |
| 2218 | return gl::ImageIndex::Make3D(mip); |
| 2219 | } |
| 2220 | |
Jamie Madill | 710e577 | 2014-10-20 17:13:53 -0400 | [diff] [blame] | 2221 | bool TextureD3D_3D::isValidIndex(const gl::ImageIndex &index) const |
| 2222 | { |
| 2223 | return (mTexStorage && index.type == GL_TEXTURE_3D && |
| 2224 | index.mipIndex >= 0 && index.mipIndex < mTexStorage->getLevelCount()); |
| 2225 | } |
| 2226 | |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 2227 | TextureD3D_2DArray::TextureD3D_2DArray(RendererD3D *renderer) |
Jamie Madill | 98553e3 | 2014-09-30 16:33:50 -0400 | [diff] [blame] | 2228 | : TextureD3D(renderer) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2229 | { |
| 2230 | for (int level = 0; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++level) |
| 2231 | { |
| 2232 | mLayerCounts[level] = 0; |
| 2233 | mImageArray[level] = NULL; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | TextureD3D_2DArray::~TextureD3D_2DArray() |
| 2238 | { |
Austin Kinross | 6982260 | 2014-08-12 15:51:37 -0700 | [diff] [blame] | 2239 | // Delete the Images before the TextureStorage. |
| 2240 | // Images might be relying on the TextureStorage for some of their data. |
| 2241 | // If TextureStorage is deleted before the Images, then their data will be wastefully copied back from the GPU before we delete the Images. |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2242 | deleteImages(); |
Austin Kinross | 6982260 | 2014-08-12 15:51:37 -0700 | [diff] [blame] | 2243 | SafeDelete(mTexStorage); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2244 | } |
| 2245 | |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2246 | Image *TextureD3D_2DArray::getImage(int level, int layer) const |
| 2247 | { |
| 2248 | ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
Jamie Madill | e5685e0 | 2014-11-12 15:37:41 -0500 | [diff] [blame] | 2249 | ASSERT((layer == 0 && mLayerCounts[level] == 0) || |
| 2250 | layer < mLayerCounts[level]); |
| 2251 | return (mImageArray[level] ? mImageArray[level][layer] : NULL); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 2254 | Image *TextureD3D_2DArray::getImage(const gl::ImageIndex &index) const |
| 2255 | { |
| 2256 | ASSERT(index.mipIndex < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
Jamie Madill | e5685e0 | 2014-11-12 15:37:41 -0500 | [diff] [blame] | 2257 | ASSERT((index.layerIndex == 0 && mLayerCounts[index.mipIndex] == 0) || |
| 2258 | index.layerIndex < mLayerCounts[index.mipIndex]); |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 2259 | ASSERT(index.type == GL_TEXTURE_2D_ARRAY); |
Jamie Madill | e5685e0 | 2014-11-12 15:37:41 -0500 | [diff] [blame] | 2260 | return (mImageArray[index.mipIndex] ? mImageArray[index.mipIndex][index.layerIndex] : NULL); |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 2261 | } |
| 2262 | |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2263 | GLsizei TextureD3D_2DArray::getLayerCount(int level) const |
| 2264 | { |
| 2265 | ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS); |
| 2266 | return mLayerCounts[level]; |
| 2267 | } |
| 2268 | |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2269 | GLsizei TextureD3D_2DArray::getWidth(GLint level) const |
| 2270 | { |
| 2271 | return (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS && mLayerCounts[level] > 0) ? mImageArray[level][0]->getWidth() : 0; |
| 2272 | } |
| 2273 | |
| 2274 | GLsizei TextureD3D_2DArray::getHeight(GLint level) const |
| 2275 | { |
| 2276 | return (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS && mLayerCounts[level] > 0) ? mImageArray[level][0]->getHeight() : 0; |
| 2277 | } |
| 2278 | |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2279 | GLenum TextureD3D_2DArray::getInternalFormat(GLint level) const |
| 2280 | { |
| 2281 | return (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS && mLayerCounts[level] > 0) ? mImageArray[level][0]->getInternalFormat() : GL_NONE; |
| 2282 | } |
| 2283 | |
| 2284 | bool TextureD3D_2DArray::isDepth(GLint level) const |
| 2285 | { |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 2286 | return gl::GetInternalFormatInfo(getInternalFormat(level)).depthBits > 0; |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2289 | gl::Error TextureD3D_2DArray::setImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, GLenum format, GLenum type, |
| 2290 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2291 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 2292 | ASSERT(target == GL_TEXTURE_2D_ARRAY); |
| 2293 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 2294 | GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, type); |
| 2295 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2296 | redefineImage(level, sizedInternalFormat, size); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2297 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 2298 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(sizedInternalFormat); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2299 | GLsizei inputDepthPitch = formatInfo.computeDepthPitch(type, size.width, size.height, unpack.alignment); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2300 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2301 | for (int i = 0; i < size.depth; i++) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2302 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2303 | const uint8_t *layerPixels = pixels ? (pixels + (inputDepthPitch * i)) : NULL; |
Jamie Madill | ba6bc95 | 2014-10-06 10:56:22 -0400 | [diff] [blame] | 2304 | gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, i); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2305 | gl::Error error = TextureD3D::setImage(index, type, unpack, layerPixels); |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 2306 | if (error.isError()) |
| 2307 | { |
| 2308 | return error; |
| 2309 | } |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2310 | } |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 2311 | |
| 2312 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2315 | gl::Error TextureD3D_2DArray::setSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, GLenum type, |
| 2316 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2317 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 2318 | ASSERT(target == GL_TEXTURE_2D_ARRAY); |
| 2319 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 2320 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(getInternalFormat(level)); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2321 | GLsizei inputDepthPitch = formatInfo.computeDepthPitch(type, area.width, area.height, unpack.alignment); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2322 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2323 | for (int i = 0; i < area.depth; i++) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2324 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2325 | int layer = area.z + i; |
| 2326 | const uint8_t *layerPixels = pixels ? (pixels + (inputDepthPitch * i)) : NULL; |
| 2327 | |
| 2328 | gl::Box layerArea(area.x, area.y, 0, area.width, area.height, 1); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2329 | |
Jamie Madill | feda4d2 | 2014-09-17 13:03:29 -0400 | [diff] [blame] | 2330 | gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, layer); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2331 | gl::Error error = TextureD3D::subImage(index, layerArea, format, type, unpack, layerPixels); |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 2332 | if (error.isError()) |
| 2333 | { |
| 2334 | return error; |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2335 | } |
| 2336 | } |
Geoff Lang | 1ba6b8d | 2014-08-28 10:57:31 -0400 | [diff] [blame] | 2337 | |
| 2338 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2339 | } |
| 2340 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2341 | gl::Error TextureD3D_2DArray::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, |
| 2342 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
| 2343 | { |
| 2344 | ASSERT(target == GL_TEXTURE_2D_ARRAY); |
| 2345 | |
| 2346 | // compressed formats don't have separate sized internal formats-- we can just use the compressed format directly |
| 2347 | redefineImage(level, internalFormat, size); |
| 2348 | |
| 2349 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat); |
| 2350 | GLsizei inputDepthPitch = formatInfo.computeDepthPitch(GL_UNSIGNED_BYTE, size.width, size.height, 1); |
| 2351 | |
| 2352 | for (int i = 0; i < size.depth; i++) |
| 2353 | { |
| 2354 | const uint8_t *layerPixels = pixels ? (pixels + (inputDepthPitch * i)) : NULL; |
| 2355 | |
| 2356 | gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, i); |
| 2357 | gl::Error error = TextureD3D::setCompressedImage(index, unpack, layerPixels); |
| 2358 | if (error.isError()) |
| 2359 | { |
| 2360 | return error; |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | return gl::Error(GL_NO_ERROR); |
| 2365 | } |
| 2366 | |
| 2367 | gl::Error TextureD3D_2DArray::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, |
| 2368 | const gl::PixelUnpackState &unpack, const uint8_t *pixels) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2369 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 2370 | ASSERT(target == GL_TEXTURE_2D_ARRAY); |
| 2371 | |
Geoff Lang | 5d60138 | 2014-07-22 15:14:06 -0400 | [diff] [blame] | 2372 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2373 | GLsizei inputDepthPitch = formatInfo.computeDepthPitch(GL_UNSIGNED_BYTE, area.width, area.height, 1); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2374 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2375 | for (int i = 0; i < area.depth; i++) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2376 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2377 | int layer = area.z + i; |
| 2378 | const uint8_t *layerPixels = pixels ? (pixels + (inputDepthPitch * i)) : NULL; |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2379 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2380 | gl::Box layerArea(area.x, area.y, 0, area.width, area.height, 1); |
| 2381 | |
| 2382 | gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, layer); |
| 2383 | gl::Error error = TextureD3D::subImageCompressed(index, layerArea, format, unpack, layerPixels); |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 2384 | if (error.isError()) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2385 | { |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 2386 | return error; |
| 2387 | } |
| 2388 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2389 | error = commitRegion(index, layerArea); |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 2390 | if (error.isError()) |
| 2391 | { |
| 2392 | return error; |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2393 | } |
| 2394 | } |
Geoff Lang | b534833 | 2014-09-02 13:16:34 -0400 | [diff] [blame] | 2395 | |
| 2396 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2399 | gl::Error TextureD3D_2DArray::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat, |
| 2400 | const gl::Framebuffer *source) |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 2401 | { |
| 2402 | UNIMPLEMENTED(); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2403 | return gl::Error(GL_INVALID_OPERATION, "Copying 2D array textures is unimplemented."); |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2406 | gl::Error TextureD3D_2DArray::copySubImage(GLenum target, size_t level, const gl::Offset &destOffset, const gl::Rectangle &sourceArea, |
| 2407 | const gl::Framebuffer *source) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2408 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 2409 | ASSERT(target == GL_TEXTURE_2D_ARRAY); |
| 2410 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2411 | gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, destOffset.z); |
Jamie Madill | 82bf0c5 | 2014-10-03 11:50:53 -0400 | [diff] [blame] | 2412 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 2413 | if (canCreateRenderTargetForImage(index)) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2414 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2415 | gl::Offset destLayerOffset(destOffset.x, destOffset.y, 0); |
| 2416 | gl::Error error = mImageArray[level][destOffset.z]->copy(destLayerOffset, sourceArea, source); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2417 | if (error.isError()) |
| 2418 | { |
| 2419 | return error; |
| 2420 | } |
| 2421 | |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2422 | mDirtyImages = true; |
| 2423 | } |
| 2424 | else |
| 2425 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2426 | gl::Error error = ensureRenderTarget(); |
| 2427 | if (error.isError()) |
| 2428 | { |
| 2429 | return error; |
| 2430 | } |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2431 | |
| 2432 | if (isValidLevel(level)) |
| 2433 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2434 | error = updateStorageLevel(level); |
| 2435 | if (error.isError()) |
| 2436 | { |
| 2437 | return error; |
| 2438 | } |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2439 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2440 | error = mRenderer->copyImage2DArray(source, sourceArea, gl::GetInternalFormatInfo(getInternalFormat(0)).format, |
| 2441 | destOffset, mTexStorage, level); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2442 | if (error.isError()) |
| 2443 | { |
| 2444 | return error; |
| 2445 | } |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2446 | } |
| 2447 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2448 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2451 | gl::Error TextureD3D_2DArray::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2452 | { |
Brandon Jones | cef06ff | 2014-08-05 13:27:48 -0700 | [diff] [blame] | 2453 | ASSERT(target == GL_TEXTURE_2D_ARRAY); |
| 2454 | |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2455 | deleteImages(); |
| 2456 | |
Geoff Lang | 8fed1f7 | 2015-01-09 11:09:33 -0500 | [diff] [blame^] | 2457 | for (size_t level = 0; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2458 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2459 | gl::Extents levelLayerSize(std::max(1, size.width >> level), |
| 2460 | std::max(1, size.height >> level), |
| 2461 | 1); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2462 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2463 | mLayerCounts[level] = (level < levels ? size.depth : 0); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2464 | |
| 2465 | if (mLayerCounts[level] > 0) |
| 2466 | { |
| 2467 | // Create new images for this level |
| 2468 | mImageArray[level] = new ImageD3D*[mLayerCounts[level]]; |
| 2469 | |
| 2470 | for (int layer = 0; layer < mLayerCounts[level]; layer++) |
| 2471 | { |
| 2472 | mImageArray[level][layer] = ImageD3D::makeImageD3D(mRenderer->createImage()); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2473 | mImageArray[level][layer]->redefine(GL_TEXTURE_2D_ARRAY, internalFormat, levelLayerSize, true); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | } |
| 2477 | |
Geoff Lang | 1f8532b | 2014-09-05 09:46:13 -0400 | [diff] [blame] | 2478 | // TODO(geofflang): Verify storage creation had no errors |
Jamie Madill | c483326 | 2014-09-18 16:18:26 -0400 | [diff] [blame] | 2479 | bool renderTarget = IsRenderTargetUsage(mUsage); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2480 | TextureStorage *storage = mRenderer->createTextureStorage2DArray(internalFormat, renderTarget, size.width, size.height, size.depth, levels); |
Geoff Lang | 1f8532b | 2014-09-05 09:46:13 -0400 | [diff] [blame] | 2481 | |
| 2482 | gl::Error error = setCompleteTexStorage(storage); |
| 2483 | if (error.isError()) |
| 2484 | { |
| 2485 | SafeDelete(storage); |
| 2486 | return error; |
| 2487 | } |
| 2488 | |
| 2489 | mImmutable = true; |
| 2490 | |
| 2491 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2492 | } |
| 2493 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 2494 | void TextureD3D_2DArray::bindTexImage(egl::Surface *surface) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2495 | { |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 2496 | UNREACHABLE(); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 2499 | void TextureD3D_2DArray::releaseTexImage() |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2500 | { |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 2501 | UNREACHABLE(); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
Brandon Jones | 6053a52 | 2014-07-25 16:22:09 -0700 | [diff] [blame] | 2504 | |
Jamie Madill | 4aa79e1 | 2014-09-29 10:46:14 -0400 | [diff] [blame] | 2505 | void TextureD3D_2DArray::initMipmapsImages() |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2506 | { |
| 2507 | int baseWidth = getBaseLevelWidth(); |
| 2508 | int baseHeight = getBaseLevelHeight(); |
Jamie Madill | f8fccb3 | 2014-11-12 15:05:26 -0500 | [diff] [blame] | 2509 | int baseDepth = getLayerCount(0); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2510 | GLenum baseFormat = getBaseLevelInternalFormat(); |
| 2511 | |
| 2512 | // Purge array levels 1 through q and reset them to represent the generated mipmap levels. |
| 2513 | int levelCount = mipLevels(); |
| 2514 | for (int level = 1; level < levelCount; level++) |
| 2515 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2516 | gl::Extents levelLayerSize(std::max(baseWidth >> level, 1), |
| 2517 | std::max(baseHeight >> level, 1), |
| 2518 | baseDepth); |
| 2519 | redefineImage(level, baseFormat, levelLayerSize); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2520 | } |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2521 | } |
| 2522 | |
Jamie Madill | ac7579c | 2014-09-17 16:59:33 -0400 | [diff] [blame] | 2523 | unsigned int TextureD3D_2DArray::getRenderTargetSerial(const gl::ImageIndex &index) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2524 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2525 | return (!ensureRenderTarget().isError() ? mTexStorage->getRenderTargetSerial(index) : 0); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2526 | } |
| 2527 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 2528 | gl::Error TextureD3D_2DArray::getRenderTarget(const gl::ImageIndex &index, RenderTarget **outRT) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2529 | { |
| 2530 | // ensure the underlying texture is created |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2531 | gl::Error error = ensureRenderTarget(); |
| 2532 | if (error.isError()) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2533 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 2534 | return error; |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2535 | } |
| 2536 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2537 | error = updateStorageLevel(index.mipIndex); |
| 2538 | if (error.isError()) |
| 2539 | { |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 2540 | return error; |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2541 | } |
| 2542 | |
Geoff Lang | 64f23f6 | 2014-09-10 14:40:12 -0400 | [diff] [blame] | 2543 | return mTexStorage->getRenderTarget(index, outRT); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2546 | gl::Error TextureD3D_2DArray::initializeStorage(bool renderTarget) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2547 | { |
| 2548 | // Only initialize the first time this texture is used as a render target or shader resource |
| 2549 | if (mTexStorage) |
| 2550 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2551 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | // do not attempt to create storage for nonexistant data |
| 2555 | if (!isLevelComplete(0)) |
| 2556 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2557 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | bool createRenderTarget = (renderTarget || mUsage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE); |
| 2561 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2562 | TextureStorage *storage = NULL; |
| 2563 | gl::Error error = createCompleteStorage(createRenderTarget, &storage); |
| 2564 | if (error.isError()) |
| 2565 | { |
| 2566 | return error; |
| 2567 | } |
| 2568 | |
| 2569 | error = setCompleteTexStorage(storage); |
| 2570 | if (error.isError()) |
| 2571 | { |
| 2572 | SafeDelete(storage); |
| 2573 | return error; |
| 2574 | } |
| 2575 | |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2576 | ASSERT(mTexStorage); |
| 2577 | |
| 2578 | // flush image data to the storage |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2579 | error = updateStorage(); |
| 2580 | if (error.isError()) |
| 2581 | { |
| 2582 | return error; |
| 2583 | } |
| 2584 | |
| 2585 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2588 | gl::Error TextureD3D_2DArray::createCompleteStorage(bool renderTarget, TextureStorage **outStorage) const |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2589 | { |
| 2590 | GLsizei width = getBaseLevelWidth(); |
| 2591 | GLsizei height = getBaseLevelHeight(); |
Jamie Madill | 3269bcb | 2014-09-30 16:33:52 -0400 | [diff] [blame] | 2592 | GLsizei depth = getLayerCount(0); |
Jamie Madill | c483326 | 2014-09-18 16:18:26 -0400 | [diff] [blame] | 2593 | GLenum internalFormat = getBaseLevelInternalFormat(); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2594 | |
| 2595 | ASSERT(width > 0 && height > 0 && depth > 0); |
| 2596 | |
| 2597 | // use existing storage level count, when previously specified by TexStorage*D |
| 2598 | GLint levels = (mTexStorage ? mTexStorage->getLevelCount() : creationLevels(width, height, 1)); |
| 2599 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2600 | // TODO(geofflang): Verify storage creation succeeds |
| 2601 | *outStorage = mRenderer->createTextureStorage2DArray(internalFormat, renderTarget, width, height, depth, levels); |
| 2602 | |
| 2603 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2604 | } |
| 2605 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2606 | gl::Error TextureD3D_2DArray::setCompleteTexStorage(TextureStorage *newCompleteTexStorage) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2607 | { |
| 2608 | SafeDelete(mTexStorage); |
| 2609 | mTexStorage = newCompleteTexStorage; |
| 2610 | mDirtyImages = true; |
| 2611 | |
| 2612 | // We do not support managed 2D array storage, as managed storage is ES2/D3D9 only |
| 2613 | ASSERT(!mTexStorage->isManaged()); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2614 | |
| 2615 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2616 | } |
| 2617 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2618 | gl::Error TextureD3D_2DArray::updateStorage() |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2619 | { |
| 2620 | ASSERT(mTexStorage != NULL); |
| 2621 | GLint storageLevels = mTexStorage->getLevelCount(); |
| 2622 | for (int level = 0; level < storageLevels; level++) |
| 2623 | { |
| 2624 | if (isLevelComplete(level)) |
| 2625 | { |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2626 | gl::Error error = updateStorageLevel(level); |
| 2627 | if (error.isError()) |
| 2628 | { |
| 2629 | return error; |
| 2630 | } |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2631 | } |
| 2632 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2633 | |
| 2634 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2637 | bool TextureD3D_2DArray::isValidLevel(int level) const |
| 2638 | { |
| 2639 | return (mTexStorage ? (level >= 0 && level < mTexStorage->getLevelCount()) : 0); |
| 2640 | } |
| 2641 | |
| 2642 | bool TextureD3D_2DArray::isLevelComplete(int level) const |
| 2643 | { |
| 2644 | ASSERT(level >= 0 && level < (int)ArraySize(mImageArray)); |
| 2645 | |
| 2646 | if (isImmutable()) |
| 2647 | { |
| 2648 | return true; |
| 2649 | } |
| 2650 | |
| 2651 | GLsizei width = getBaseLevelWidth(); |
| 2652 | GLsizei height = getBaseLevelHeight(); |
Jamie Madill | 3269bcb | 2014-09-30 16:33:52 -0400 | [diff] [blame] | 2653 | GLsizei layers = getLayerCount(0); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2654 | |
| 2655 | if (width <= 0 || height <= 0 || layers <= 0) |
| 2656 | { |
| 2657 | return false; |
| 2658 | } |
| 2659 | |
| 2660 | if (level == 0) |
| 2661 | { |
| 2662 | return true; |
| 2663 | } |
| 2664 | |
| 2665 | if (getInternalFormat(level) != getInternalFormat(0)) |
| 2666 | { |
| 2667 | return false; |
| 2668 | } |
| 2669 | |
| 2670 | if (getWidth(level) != std::max(1, width >> level)) |
| 2671 | { |
| 2672 | return false; |
| 2673 | } |
| 2674 | |
| 2675 | if (getHeight(level) != std::max(1, height >> level)) |
| 2676 | { |
| 2677 | return false; |
| 2678 | } |
| 2679 | |
Jamie Madill | 3269bcb | 2014-09-30 16:33:52 -0400 | [diff] [blame] | 2680 | if (getLayerCount(level) != layers) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2681 | { |
| 2682 | return false; |
| 2683 | } |
| 2684 | |
| 2685 | return true; |
| 2686 | } |
| 2687 | |
Jamie Madill | e76bdda | 2014-10-20 17:13:52 -0400 | [diff] [blame] | 2688 | bool TextureD3D_2DArray::isImageComplete(const gl::ImageIndex &index) const |
| 2689 | { |
| 2690 | return isLevelComplete(index.mipIndex); |
| 2691 | } |
| 2692 | |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2693 | gl::Error TextureD3D_2DArray::updateStorageLevel(int level) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2694 | { |
| 2695 | ASSERT(level >= 0 && level < (int)ArraySize(mLayerCounts)); |
| 2696 | ASSERT(isLevelComplete(level)); |
| 2697 | |
| 2698 | for (int layer = 0; layer < mLayerCounts[level]; layer++) |
| 2699 | { |
| 2700 | ASSERT(mImageArray[level] != NULL && mImageArray[level][layer] != NULL); |
| 2701 | if (mImageArray[level][layer]->isDirty()) |
| 2702 | { |
Jamie Madill | 7cb2b9d | 2014-10-02 11:03:13 -0400 | [diff] [blame] | 2703 | gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, layer); |
| 2704 | gl::Box region(0, 0, 0, getWidth(level), getHeight(level), 1); |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2705 | gl::Error error = commitRegion(index, region); |
| 2706 | if (error.isError()) |
| 2707 | { |
| 2708 | return error; |
| 2709 | } |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2710 | } |
| 2711 | } |
Geoff Lang | ef7b016 | 2014-09-04 13:29:23 -0400 | [diff] [blame] | 2712 | |
| 2713 | return gl::Error(GL_NO_ERROR); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | void TextureD3D_2DArray::deleteImages() |
| 2717 | { |
| 2718 | for (int level = 0; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++level) |
| 2719 | { |
| 2720 | for (int layer = 0; layer < mLayerCounts[level]; ++layer) |
| 2721 | { |
| 2722 | delete mImageArray[level][layer]; |
| 2723 | } |
| 2724 | delete[] mImageArray[level]; |
| 2725 | mImageArray[level] = NULL; |
| 2726 | mLayerCounts[level] = 0; |
| 2727 | } |
| 2728 | } |
| 2729 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2730 | void TextureD3D_2DArray::redefineImage(GLint level, GLenum internalformat, const gl::Extents &size) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2731 | { |
| 2732 | // If there currently is a corresponding storage texture image, it has these parameters |
| 2733 | const int storageWidth = std::max(1, getBaseLevelWidth() >> level); |
| 2734 | const int storageHeight = std::max(1, getBaseLevelHeight() >> level); |
Jamie Madill | 3269bcb | 2014-09-30 16:33:52 -0400 | [diff] [blame] | 2735 | const int storageDepth = getLayerCount(0); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2736 | const GLenum storageFormat = getBaseLevelInternalFormat(); |
| 2737 | |
| 2738 | for (int layer = 0; layer < mLayerCounts[level]; layer++) |
| 2739 | { |
| 2740 | delete mImageArray[level][layer]; |
| 2741 | } |
| 2742 | delete[] mImageArray[level]; |
| 2743 | mImageArray[level] = NULL; |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2744 | mLayerCounts[level] = size.depth; |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2745 | |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2746 | if (size.depth > 0) |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2747 | { |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2748 | mImageArray[level] = new ImageD3D*[size.depth](); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2749 | |
| 2750 | for (int layer = 0; layer < mLayerCounts[level]; layer++) |
| 2751 | { |
| 2752 | mImageArray[level][layer] = ImageD3D::makeImageD3D(mRenderer->createImage()); |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2753 | mImageArray[level][layer]->redefine(GL_TEXTURE_2D_ARRAY, internalformat, |
| 2754 | gl::Extents(size.width, size.height, 1), false); |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2755 | } |
| 2756 | } |
| 2757 | |
| 2758 | if (mTexStorage) |
| 2759 | { |
| 2760 | const int storageLevels = mTexStorage->getLevelCount(); |
| 2761 | |
| 2762 | if ((level >= storageLevels && storageLevels != 0) || |
Geoff Lang | 0a4f1e2 | 2014-12-17 12:33:26 -0500 | [diff] [blame] | 2763 | size.width != storageWidth || |
| 2764 | size.height != storageHeight || |
| 2765 | size.depth != storageDepth || |
Brandon Jones | 142ec42 | 2014-07-16 10:31:30 -0700 | [diff] [blame] | 2766 | internalformat != storageFormat) // Discard mismatched storage |
| 2767 | { |
| 2768 | for (int level = 0; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++) |
| 2769 | { |
| 2770 | for (int layer = 0; layer < mLayerCounts[level]; layer++) |
| 2771 | { |
| 2772 | mImageArray[level][layer]->markDirty(); |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | delete mTexStorage; |
| 2777 | mTexStorage = NULL; |
| 2778 | mDirtyImages = true; |
| 2779 | } |
| 2780 | } |
| 2781 | } |
| 2782 | |
Jamie Madill | ef4ac5b | 2014-09-29 10:46:11 -0400 | [diff] [blame] | 2783 | gl::ImageIndexIterator TextureD3D_2DArray::imageIterator() const |
| 2784 | { |
| 2785 | return gl::ImageIndexIterator::Make2DArray(0, mTexStorage->getLevelCount(), mLayerCounts); |
| 2786 | } |
| 2787 | |
Jamie Madill | cb83dc1 | 2014-09-29 10:46:12 -0400 | [diff] [blame] | 2788 | gl::ImageIndex TextureD3D_2DArray::getImageIndex(GLint mip, GLint layer) const |
| 2789 | { |
| 2790 | return gl::ImageIndex::Make2DArray(mip, layer); |
| 2791 | } |
| 2792 | |
Jamie Madill | 710e577 | 2014-10-20 17:13:53 -0400 | [diff] [blame] | 2793 | bool TextureD3D_2DArray::isValidIndex(const gl::ImageIndex &index) const |
| 2794 | { |
| 2795 | // Check for having a storage and the right type of index |
| 2796 | if (!mTexStorage || index.type != GL_TEXTURE_2D_ARRAY) |
| 2797 | { |
| 2798 | return false; |
| 2799 | } |
| 2800 | |
| 2801 | // Check the mip index |
| 2802 | if (index.mipIndex < 0 || index.mipIndex >= mTexStorage->getLevelCount()) |
| 2803 | { |
| 2804 | return false; |
| 2805 | } |
| 2806 | |
| 2807 | // Check the layer index |
| 2808 | return (!index.hasLayer() || (index.layerIndex >= 0 && index.layerIndex < mLayerCounts[index.mipIndex])); |
| 2809 | } |
| 2810 | |
Brandon Jones | 78b1acd | 2014-07-15 15:33:07 -0700 | [diff] [blame] | 2811 | } |