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