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