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