shannon.woods@transgaming.com | bdf2d80 | 2013-02-28 23:16:20 +0000 | [diff] [blame] | 1 | #include "precompiled.h" |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 2 | // |
| 3 | // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style license that can be |
| 5 | // found in the LICENSE file. |
| 6 | // |
| 7 | |
| 8 | // Image11.h: Implements the rx::Image11 class, which acts as the interface to |
| 9 | // the actual underlying resources of a Texture |
| 10 | |
| 11 | #include "libGLESv2/renderer/Renderer11.h" |
| 12 | #include "libGLESv2/renderer/Image11.h" |
daniel@transgaming.com | 46cf249 | 2013-01-11 04:06:43 +0000 | [diff] [blame] | 13 | #include "libGLESv2/renderer/TextureStorage11.h" |
shannon.woods@transgaming.com | c8cd7f6 | 2013-01-25 21:52:40 +0000 | [diff] [blame] | 14 | #include "libGLESv2/Framebuffer.h" |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 15 | #include "libGLESv2/Renderbuffer.h" |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 16 | |
| 17 | #include "libGLESv2/main.h" |
shannonwoods@chromium.org | a2ecfcc | 2013-05-30 00:11:59 +0000 | [diff] [blame^] | 18 | #include "common/utilities.h" |
shannonwoods@chromium.org | 557aab0 | 2013-05-30 00:08:27 +0000 | [diff] [blame] | 19 | #include "libGLESv2/renderer/formatutils11.h" |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 20 | #include "libGLESv2/renderer/renderer11_utils.h" |
| 21 | |
| 22 | namespace rx |
| 23 | { |
| 24 | |
| 25 | Image11::Image11() |
| 26 | { |
| 27 | mStagingTexture = NULL; |
| 28 | mRenderer = NULL; |
| 29 | mDXGIFormat = DXGI_FORMAT_UNKNOWN; |
| 30 | } |
| 31 | |
| 32 | Image11::~Image11() |
| 33 | { |
| 34 | if (mStagingTexture) |
| 35 | { |
| 36 | mStagingTexture->Release(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | Image11 *Image11::makeImage11(Image *img) |
| 41 | { |
apatrick@chromium.org | 8b400b1 | 2013-01-30 21:53:40 +0000 | [diff] [blame] | 42 | ASSERT(HAS_DYNAMIC_TYPE(rx::Image11*, img)); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 43 | return static_cast<rx::Image11*>(img); |
| 44 | } |
| 45 | |
shannon.woods@transgaming.com | 2b132f4 | 2013-01-25 21:52:47 +0000 | [diff] [blame] | 46 | void Image11::generateMipmap(Image11 *dest, Image11 *src) |
| 47 | { |
| 48 | ASSERT(src->getDXGIFormat() == dest->getDXGIFormat()); |
| 49 | ASSERT(src->getWidth() == 1 || src->getWidth() / 2 == dest->getWidth()); |
| 50 | ASSERT(src->getHeight() == 1 || src->getHeight() / 2 == dest->getHeight()); |
| 51 | |
shannonwoods@chromium.org | cead8ad | 2013-05-30 00:08:52 +0000 | [diff] [blame] | 52 | MipGenerationFunction mipFunction = d3d11::GetMipGenerationFunction(src->getDXGIFormat()); |
| 53 | ASSERT(mipFunction != NULL); |
| 54 | |
| 55 | D3D11_MAPPED_SUBRESOURCE destMapped; |
| 56 | HRESULT destMapResult = dest->map(D3D11_MAP_WRITE, &destMapped); |
| 57 | if (FAILED(destMapResult)) |
| 58 | { |
| 59 | ERR("Failed to map destination image for mip map generation. HRESULT:0x%X", destMapResult); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | D3D11_MAPPED_SUBRESOURCE srcMapped; |
| 64 | HRESULT srcMapResult = src->map(D3D11_MAP_READ, &srcMapped); |
| 65 | if (FAILED(srcMapResult)) |
| 66 | { |
| 67 | ERR("Failed to map source image for mip map generation. HRESULT:0x%X", srcMapResult); |
| 68 | |
| 69 | dest->unmap(); |
| 70 | return; |
| 71 | } |
shannon.woods@transgaming.com | 2b132f4 | 2013-01-25 21:52:47 +0000 | [diff] [blame] | 72 | |
| 73 | const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(srcMapped.pData); |
| 74 | unsigned char *destData = reinterpret_cast<unsigned char*>(destMapped.pData); |
| 75 | |
shannonwoods@chromium.org | cead8ad | 2013-05-30 00:08:52 +0000 | [diff] [blame] | 76 | mipFunction(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, |
| 77 | destData, destMapped.RowPitch, destMapped.DepthPitch); |
shannon.woods@transgaming.com | 2b132f4 | 2013-01-25 21:52:47 +0000 | [diff] [blame] | 78 | |
shannonwoods@chromium.org | cead8ad | 2013-05-30 00:08:52 +0000 | [diff] [blame] | 79 | dest->unmap(); |
| 80 | src->unmap(); |
shannon.woods@transgaming.com | 2b132f4 | 2013-01-25 21:52:47 +0000 | [diff] [blame] | 81 | |
| 82 | dest->markDirty(); |
| 83 | } |
| 84 | |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 85 | bool Image11::isDirty() const |
| 86 | { |
| 87 | return (mStagingTexture && mDirty); |
| 88 | } |
| 89 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 90 | bool Image11::updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 91 | { |
daniel@transgaming.com | 46cf249 | 2013-01-11 04:06:43 +0000 | [diff] [blame] | 92 | TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance()); |
shannon.woods%transgaming.com@gtempaccount.com | 2058d64 | 2013-04-13 03:42:50 +0000 | [diff] [blame] | 93 | return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, 0, width, height, 1); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 94 | } |
| 95 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 96 | bool Image11::updateSurface(TextureStorageInterfaceCube *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 97 | { |
daniel@transgaming.com | 46cf249 | 2013-01-11 04:06:43 +0000 | [diff] [blame] | 98 | TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance()); |
shannon.woods%transgaming.com@gtempaccount.com | 2058d64 | 2013-04-13 03:42:50 +0000 | [diff] [blame] | 99 | return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, face, xoffset, yoffset, 0, width, height, 1); |
| 100 | } |
| 101 | |
| 102 | bool Image11::updateSurface(TextureStorageInterface3D *storage, int level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth) |
| 103 | { |
shannon.woods%transgaming.com@gtempaccount.com | 5d009bb | 2013-04-13 03:43:07 +0000 | [diff] [blame] | 104 | TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance()); |
| 105 | return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, zoffset, width, height, depth); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 106 | } |
| 107 | |
shannon.woods%transgaming.com@gtempaccount.com | 6c86bd5 | 2013-04-13 03:45:45 +0000 | [diff] [blame] | 108 | bool Image11::updateSurface(TextureStorageInterface2DArray *storage, int level, GLint xoffset, GLint yoffset, GLint arrayLayer, GLsizei width, GLsizei height) |
| 109 | { |
| 110 | TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance()); |
| 111 | return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, arrayLayer, xoffset, yoffset, 0, width, height, 1); |
| 112 | } |
| 113 | |
shannon.woods%transgaming.com@gtempaccount.com | 56074f3 | 2013-04-13 03:45:30 +0000 | [diff] [blame] | 114 | bool Image11::redefine(Renderer *renderer, GLenum target, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, bool forceRelease) |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 115 | { |
| 116 | if (mWidth != width || |
| 117 | mHeight != height || |
| 118 | mInternalFormat != internalformat || |
| 119 | forceRelease) |
| 120 | { |
| 121 | mRenderer = Renderer11::makeRenderer11(renderer); |
shannonwoods@chromium.org | 803be0a | 2013-05-30 00:08:59 +0000 | [diff] [blame] | 122 | GLuint clientVersion = mRenderer->getCurrentClientVersion(); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 123 | |
| 124 | mWidth = width; |
| 125 | mHeight = height; |
shannon.woods%transgaming.com@gtempaccount.com | 4760c56 | 2013-04-13 03:42:30 +0000 | [diff] [blame] | 126 | mDepth = depth; |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 127 | mInternalFormat = internalformat; |
shannon.woods%transgaming.com@gtempaccount.com | 56074f3 | 2013-04-13 03:45:30 +0000 | [diff] [blame] | 128 | mTarget = target; |
shannonwoods@chromium.org | 803be0a | 2013-05-30 00:08:59 +0000 | [diff] [blame] | 129 | |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 130 | // compute the d3d format that will be used |
shannonwoods@chromium.org | 803be0a | 2013-05-30 00:08:59 +0000 | [diff] [blame] | 131 | mDXGIFormat = gl_d3d11::GetTexFormat(internalformat, clientVersion); |
| 132 | mActualFormat = d3d11_gl::GetInternalFormat(mDXGIFormat); |
| 133 | mRenderable = gl_d3d11::GetTexFormat(internalformat, clientVersion) != DXGI_FORMAT_UNKNOWN; |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 134 | |
| 135 | if (mStagingTexture) |
| 136 | { |
| 137 | mStagingTexture->Release(); |
| 138 | mStagingTexture = NULL; |
| 139 | } |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 147 | DXGI_FORMAT Image11::getDXGIFormat() const |
| 148 | { |
| 149 | // this should only happen if the image hasn't been redefined first |
| 150 | // which would be a bug by the caller |
| 151 | ASSERT(mDXGIFormat != DXGI_FORMAT_UNKNOWN); |
| 152 | |
| 153 | return mDXGIFormat; |
| 154 | } |
| 155 | |
| 156 | // Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input |
| 157 | // into the target pixel rectangle. |
shannon.woods%transgaming.com@gtempaccount.com | 4760c56 | 2013-04-13 03:42:30 +0000 | [diff] [blame] | 158 | void Image11::loadData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, |
shannonwoods@chromium.org | 557aab0 | 2013-05-30 00:08:27 +0000 | [diff] [blame] | 159 | GLint unpackAlignment, GLenum type, const void *input) |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 160 | { |
shannonwoods@chromium.org | 557aab0 | 2013-05-30 00:08:27 +0000 | [diff] [blame] | 161 | GLuint clientVersion = mRenderer->getCurrentClientVersion(); |
| 162 | GLsizei inputRowPitch = gl::GetRowPitch(mInternalFormat, type, clientVersion, width, unpackAlignment); |
| 163 | GLsizei inputDepthPitch = gl::GetDepthPitch(mInternalFormat, type, clientVersion, width, height, unpackAlignment); |
| 164 | GLuint outputPixelSize = d3d11::GetFormatPixelBytes(mDXGIFormat); |
| 165 | |
| 166 | LoadImageFunction loadFunction = d3d11::GetImageLoadFunction(mInternalFormat, type, clientVersion); |
| 167 | ASSERT(loadFunction != NULL); |
| 168 | |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 169 | D3D11_MAPPED_SUBRESOURCE mappedImage; |
shannonwoods@chromium.org | 44b2768 | 2013-05-30 00:03:06 +0000 | [diff] [blame] | 170 | HRESULT result = map(D3D11_MAP_WRITE, &mappedImage); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 171 | if (FAILED(result)) |
| 172 | { |
| 173 | ERR("Could not map image for loading."); |
| 174 | return; |
| 175 | } |
shannon.woods%transgaming.com@gtempaccount.com | 4760c56 | 2013-04-13 03:42:30 +0000 | [diff] [blame] | 176 | |
shannonwoods@chromium.org | 557aab0 | 2013-05-30 00:08:27 +0000 | [diff] [blame] | 177 | void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + (yoffset * mappedImage.RowPitch + xoffset * outputPixelSize + zoffset * mappedImage.DepthPitch)); |
| 178 | loadFunction(width, height, depth, input, inputRowPitch, inputDepthPitch, offsetMappedData, mappedImage.RowPitch, mappedImage.DepthPitch); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 179 | |
| 180 | unmap(); |
| 181 | } |
| 182 | |
shannon.woods%transgaming.com@gtempaccount.com | 4760c56 | 2013-04-13 03:42:30 +0000 | [diff] [blame] | 183 | void Image11::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, |
| 184 | const void *input) |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 185 | { |
shannonwoods@chromium.org | b8cabd5 | 2013-05-30 00:08:37 +0000 | [diff] [blame] | 186 | GLuint clientVersion = mRenderer->getCurrentClientVersion(); |
| 187 | GLsizei inputRowPitch = gl::GetRowPitch(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion, width, 1); |
| 188 | GLsizei inputDepthPitch = gl::GetDepthPitch(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion, width, height, 1); |
| 189 | |
| 190 | GLuint outputPixelSize = d3d11::GetFormatPixelBytes(mDXGIFormat); |
| 191 | GLuint outputBlockWidth = d3d11::GetBlockWidth(mDXGIFormat); |
| 192 | GLuint outputBlockHeight = d3d11::GetBlockHeight(mDXGIFormat); |
| 193 | |
| 194 | ASSERT(xoffset % outputBlockWidth == 0); |
| 195 | ASSERT(yoffset % outputBlockHeight == 0); |
| 196 | |
| 197 | LoadImageFunction loadFunction = d3d11::GetImageLoadFunction(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion); |
| 198 | ASSERT(loadFunction != NULL); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 199 | |
| 200 | D3D11_MAPPED_SUBRESOURCE mappedImage; |
shannonwoods@chromium.org | 44b2768 | 2013-05-30 00:03:06 +0000 | [diff] [blame] | 201 | HRESULT result = map(D3D11_MAP_WRITE, &mappedImage); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 202 | if (FAILED(result)) |
| 203 | { |
| 204 | ERR("Could not map image for loading."); |
| 205 | return; |
| 206 | } |
| 207 | |
shannonwoods@chromium.org | b8cabd5 | 2013-05-30 00:08:37 +0000 | [diff] [blame] | 208 | void* offsetMappedData = (void*)((BYTE*)mappedImage.pData + ((yoffset / outputBlockHeight) * mappedImage.RowPitch + |
| 209 | (xoffset / outputBlockWidth) * outputPixelSize + |
| 210 | zoffset * mappedImage.DepthPitch)); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 211 | |
shannonwoods@chromium.org | b8cabd5 | 2013-05-30 00:08:37 +0000 | [diff] [blame] | 212 | loadFunction(width, height, depth, input, inputRowPitch, inputDepthPitch, |
| 213 | offsetMappedData, mappedImage.RowPitch, mappedImage.DepthPitch); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 214 | |
| 215 | unmap(); |
| 216 | } |
| 217 | |
shannon.woods%transgaming.com@gtempaccount.com | e5dcce7 | 2013-04-13 03:44:33 +0000 | [diff] [blame] | 218 | void Image11::copy(GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source) |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 219 | { |
shannon.woods%transgaming.com@gtempaccount.com | 00e3f0c | 2013-04-13 03:31:02 +0000 | [diff] [blame] | 220 | gl::Renderbuffer *colorbuffer = source->getReadColorbuffer(); |
| 221 | |
| 222 | if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat) |
shannon.woods@transgaming.com | c8cd7f6 | 2013-01-25 21:52:40 +0000 | [diff] [blame] | 223 | { |
| 224 | // No conversion needed-- use copyback fastpath |
| 225 | ID3D11Texture2D *colorBufferTexture = NULL; |
| 226 | unsigned int subresourceIndex = 0; |
| 227 | |
shannon.woods%transgaming.com@gtempaccount.com | 00e3f0c | 2013-04-13 03:31:02 +0000 | [diff] [blame] | 228 | if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture)) |
shannon.woods@transgaming.com | c8cd7f6 | 2013-01-25 21:52:40 +0000 | [diff] [blame] | 229 | { |
| 230 | D3D11_TEXTURE2D_DESC textureDesc; |
| 231 | colorBufferTexture->GetDesc(&textureDesc); |
| 232 | |
| 233 | ID3D11Device *device = mRenderer->getDevice(); |
| 234 | ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); |
| 235 | |
| 236 | ID3D11Texture2D* srcTex = NULL; |
| 237 | if (textureDesc.SampleDesc.Count > 1) |
| 238 | { |
| 239 | D3D11_TEXTURE2D_DESC resolveDesc; |
| 240 | resolveDesc.Width = textureDesc.Width; |
| 241 | resolveDesc.Height = textureDesc.Height; |
| 242 | resolveDesc.MipLevels = 1; |
| 243 | resolveDesc.ArraySize = 1; |
| 244 | resolveDesc.Format = textureDesc.Format; |
| 245 | resolveDesc.SampleDesc.Count = 1; |
| 246 | resolveDesc.SampleDesc.Quality = 0; |
| 247 | resolveDesc.Usage = D3D11_USAGE_DEFAULT; |
| 248 | resolveDesc.BindFlags = 0; |
| 249 | resolveDesc.CPUAccessFlags = 0; |
| 250 | resolveDesc.MiscFlags = 0; |
| 251 | |
| 252 | HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex); |
| 253 | if (FAILED(result)) |
| 254 | { |
| 255 | ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format); |
| 260 | subresourceIndex = 0; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | srcTex = colorBufferTexture; |
| 265 | srcTex->AddRef(); |
| 266 | } |
| 267 | |
| 268 | D3D11_BOX srcBox; |
| 269 | srcBox.left = x; |
| 270 | srcBox.right = x + width; |
| 271 | srcBox.top = y; |
| 272 | srcBox.bottom = y + height; |
| 273 | srcBox.front = 0; |
| 274 | srcBox.back = 1; |
| 275 | |
shannon.woods%transgaming.com@gtempaccount.com | e5dcce7 | 2013-04-13 03:44:33 +0000 | [diff] [blame] | 276 | deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, zoffset, srcTex, subresourceIndex, &srcBox); |
shannon.woods@transgaming.com | c8cd7f6 | 2013-01-25 21:52:40 +0000 | [diff] [blame] | 277 | |
| 278 | srcTex->Release(); |
| 279 | colorBufferTexture->Release(); |
| 280 | } |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels |
| 285 | D3D11_MAPPED_SUBRESOURCE mappedImage; |
shannonwoods@chromium.org | 44b2768 | 2013-05-30 00:03:06 +0000 | [diff] [blame] | 286 | HRESULT result = map(D3D11_MAP_WRITE, &mappedImage); |
shannonwoods@chromium.org | 803be0a | 2013-05-30 00:08:59 +0000 | [diff] [blame] | 287 | |
shannon.woods@transgaming.com | a7c7bc4 | 2013-02-28 23:14:02 +0000 | [diff] [blame] | 288 | // determine the offset coordinate into the destination buffer |
shannonwoods@chromium.org | 803be0a | 2013-05-30 00:08:59 +0000 | [diff] [blame] | 289 | GLuint clientVersion = mRenderer->getCurrentClientVersion(); |
| 290 | GLsizei rowOffset = gl::GetPixelBytes(mActualFormat, clientVersion) * xoffset; |
shannon.woods%transgaming.com@gtempaccount.com | e5dcce7 | 2013-04-13 03:44:33 +0000 | [diff] [blame] | 291 | void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset + zoffset * mappedImage.DepthPitch; |
shannon.woods@transgaming.com | a7c7bc4 | 2013-02-28 23:14:02 +0000 | [diff] [blame] | 292 | |
shannonwoods@chromium.org | 803be0a | 2013-05-30 00:08:59 +0000 | [diff] [blame] | 293 | mRenderer->readPixels(source, x, y, width, height, gl::GetFormat(mInternalFormat, clientVersion), |
| 294 | gl::GetType(mInternalFormat, clientVersion), mappedImage.RowPitch, false, 4, dataOffset); |
shannon.woods@transgaming.com | c8cd7f6 | 2013-01-25 21:52:40 +0000 | [diff] [blame] | 295 | |
| 296 | unmap(); |
| 297 | } |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 298 | } |
| 299 | |
shannon.woods%transgaming.com@gtempaccount.com | 5d009bb | 2013-04-13 03:43:07 +0000 | [diff] [blame] | 300 | ID3D11Resource *Image11::getStagingTexture() |
shannon.woods@transgaming.com | cabb17c | 2013-02-28 23:20:45 +0000 | [diff] [blame] | 301 | { |
| 302 | createStagingTexture(); |
| 303 | |
| 304 | return mStagingTexture; |
| 305 | } |
| 306 | |
shannon.woods@transgaming.com | 81ae58a | 2013-02-28 23:20:51 +0000 | [diff] [blame] | 307 | unsigned int Image11::getStagingSubresource() |
| 308 | { |
| 309 | createStagingTexture(); |
| 310 | |
| 311 | return mStagingSubresource; |
| 312 | } |
| 313 | |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 314 | void Image11::createStagingTexture() |
| 315 | { |
| 316 | if (mStagingTexture) |
| 317 | { |
| 318 | return; |
| 319 | } |
| 320 | |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 321 | const DXGI_FORMAT dxgiFormat = getDXGIFormat(); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 322 | |
shannon.woods%transgaming.com@gtempaccount.com | 5d009bb | 2013-04-13 03:43:07 +0000 | [diff] [blame] | 323 | if (mWidth > 0 && mHeight > 0 && mDepth > 0) |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 324 | { |
shannon.woods%transgaming.com@gtempaccount.com | 5d009bb | 2013-04-13 03:43:07 +0000 | [diff] [blame] | 325 | ID3D11Device *device = mRenderer->getDevice(); |
| 326 | |
shannon.woods%transgaming.com@gtempaccount.com | 56074f3 | 2013-04-13 03:45:30 +0000 | [diff] [blame] | 327 | int lodOffset = 1; |
shannon.woods@transgaming.com | 81ae58a | 2013-02-28 23:20:51 +0000 | [diff] [blame] | 328 | GLsizei width = mWidth; |
| 329 | GLsizei height = mHeight; |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 330 | |
shannon.woods@transgaming.com | 81ae58a | 2013-02-28 23:20:51 +0000 | [diff] [blame] | 331 | // adjust size if needed for compressed textures |
shannonwoods@chromium.org | 803be0a | 2013-05-30 00:08:59 +0000 | [diff] [blame] | 332 | d3d11::MakeValidSize(false, dxgiFormat, &width, &height, &lodOffset); |
shannon.woods@transgaming.com | 7ae9e7f | 2013-02-28 23:13:27 +0000 | [diff] [blame] | 333 | |
shannon.woods%transgaming.com@gtempaccount.com | 56074f3 | 2013-04-13 03:45:30 +0000 | [diff] [blame] | 334 | if (mTarget == GL_TEXTURE_3D) |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 335 | { |
shannon.woods%transgaming.com@gtempaccount.com | 5d009bb | 2013-04-13 03:43:07 +0000 | [diff] [blame] | 336 | ID3D11Texture3D *newTexture = NULL; |
| 337 | |
| 338 | D3D11_TEXTURE3D_DESC desc; |
| 339 | desc.Width = width; |
| 340 | desc.Height = height; |
| 341 | desc.Depth = mDepth; |
| 342 | desc.MipLevels = lodOffset + 1; |
| 343 | desc.Format = dxgiFormat; |
| 344 | desc.Usage = D3D11_USAGE_STAGING; |
| 345 | desc.BindFlags = 0; |
shannonwoods@chromium.org | 44b2768 | 2013-05-30 00:03:06 +0000 | [diff] [blame] | 346 | desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; |
shannon.woods%transgaming.com@gtempaccount.com | 5d009bb | 2013-04-13 03:43:07 +0000 | [diff] [blame] | 347 | desc.MiscFlags = 0; |
| 348 | |
| 349 | HRESULT result = device->CreateTexture3D(&desc, NULL, &newTexture); |
| 350 | if (FAILED(result)) |
| 351 | { |
| 352 | ASSERT(result == E_OUTOFMEMORY); |
| 353 | ERR("Creating image failed."); |
| 354 | return gl::error(GL_OUT_OF_MEMORY); |
| 355 | } |
| 356 | |
| 357 | mStagingTexture = newTexture; |
| 358 | mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1); |
| 359 | } |
shannon.woods%transgaming.com@gtempaccount.com | 56074f3 | 2013-04-13 03:45:30 +0000 | [diff] [blame] | 360 | else if (mTarget == GL_TEXTURE_2D || mTarget == GL_TEXTURE_2D_ARRAY || mTarget == GL_TEXTURE_CUBE_MAP) |
shannon.woods%transgaming.com@gtempaccount.com | 5d009bb | 2013-04-13 03:43:07 +0000 | [diff] [blame] | 361 | { |
| 362 | ID3D11Texture2D *newTexture = NULL; |
| 363 | |
| 364 | D3D11_TEXTURE2D_DESC desc; |
| 365 | desc.Width = width; |
| 366 | desc.Height = height; |
| 367 | desc.MipLevels = lodOffset + 1; |
| 368 | desc.ArraySize = 1; |
| 369 | desc.Format = dxgiFormat; |
| 370 | desc.SampleDesc.Count = 1; |
| 371 | desc.SampleDesc.Quality = 0; |
| 372 | desc.Usage = D3D11_USAGE_STAGING; |
| 373 | desc.BindFlags = 0; |
shannonwoods@chromium.org | 44b2768 | 2013-05-30 00:03:06 +0000 | [diff] [blame] | 374 | desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; |
shannon.woods%transgaming.com@gtempaccount.com | 5d009bb | 2013-04-13 03:43:07 +0000 | [diff] [blame] | 375 | desc.MiscFlags = 0; |
| 376 | |
| 377 | HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture); |
| 378 | |
| 379 | if (FAILED(result)) |
| 380 | { |
| 381 | ASSERT(result == E_OUTOFMEMORY); |
| 382 | ERR("Creating image failed."); |
| 383 | return gl::error(GL_OUT_OF_MEMORY); |
| 384 | } |
| 385 | |
| 386 | mStagingTexture = newTexture; |
| 387 | mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 388 | } |
shannon.woods%transgaming.com@gtempaccount.com | 56074f3 | 2013-04-13 03:45:30 +0000 | [diff] [blame] | 389 | else |
| 390 | { |
| 391 | UNREACHABLE(); |
| 392 | } |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 393 | } |
| 394 | |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 395 | mDirty = false; |
| 396 | } |
| 397 | |
shannonwoods@chromium.org | 44b2768 | 2013-05-30 00:03:06 +0000 | [diff] [blame] | 398 | HRESULT Image11::map(D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map) |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 399 | { |
| 400 | createStagingTexture(); |
| 401 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 402 | HRESULT result = E_FAIL; |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 403 | |
| 404 | if (mStagingTexture) |
| 405 | { |
| 406 | ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); |
shannonwoods@chromium.org | 44b2768 | 2013-05-30 00:03:06 +0000 | [diff] [blame] | 407 | result = deviceContext->Map(mStagingTexture, mStagingSubresource, mapType, 0, map); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 408 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 409 | // this can fail if the device is removed (from TDR) |
| 410 | if (d3d11::isDeviceLostError(result)) |
| 411 | { |
| 412 | mRenderer->notifyDeviceLost(); |
| 413 | } |
| 414 | else if (SUCCEEDED(result)) |
| 415 | { |
| 416 | mDirty = true; |
| 417 | } |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return result; |
| 421 | } |
| 422 | |
| 423 | void Image11::unmap() |
| 424 | { |
| 425 | if (mStagingTexture) |
| 426 | { |
| 427 | ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); |
shannon.woods@transgaming.com | 81ae58a | 2013-02-28 23:20:51 +0000 | [diff] [blame] | 428 | deviceContext->Unmap(mStagingTexture, mStagingSubresource); |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | } |