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