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