blob: bd3dbf3a653c256fe17c97bcfdd2c2fc105d2f0d [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00002//
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.com46cf2492013-01-11 04:06:43 +000013#include "libGLESv2/renderer/TextureStorage11.h"
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +000014#include "libGLESv2/Framebuffer.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000015#include "libGLESv2/Renderbuffer.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000016
17#include "libGLESv2/main.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000018#include "libGLESv2/utilities.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000019#include "libGLESv2/renderer/renderer11_utils.h"
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000020#include "libGLESv2/renderer/generatemip.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000021
22namespace rx
23{
24
25Image11::Image11()
26{
27 mStagingTexture = NULL;
28 mRenderer = NULL;
29 mDXGIFormat = DXGI_FORMAT_UNKNOWN;
30}
31
32Image11::~Image11()
33{
34 if (mStagingTexture)
35 {
36 mStagingTexture->Release();
37 }
38}
39
40Image11 *Image11::makeImage11(Image *img)
41{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +000042 ASSERT(HAS_DYNAMIC_TYPE(rx::Image11*, img));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000043 return static_cast<rx::Image11*>(img);
44}
45
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000046void 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
52 D3D11_MAPPED_SUBRESOURCE destMapped, srcMapped;
53 dest->map(&destMapped);
54 src->map(&srcMapped);
55
56 const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(srcMapped.pData);
57 unsigned char *destData = reinterpret_cast<unsigned char*>(destMapped.pData);
58
59 if (sourceData && destData)
60 {
61 switch (src->getDXGIFormat())
62 {
63 case DXGI_FORMAT_R8G8B8A8_UNORM:
64 case DXGI_FORMAT_B8G8R8A8_UNORM:
65 GenerateMip<R8G8B8A8>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
66 break;
67 case DXGI_FORMAT_A8_UNORM:
68 GenerateMip<A8>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
69 break;
70 case DXGI_FORMAT_R8_UNORM:
71 GenerateMip<R8>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
72 break;
73 case DXGI_FORMAT_R32G32B32A32_FLOAT:
74 GenerateMip<A32B32G32R32F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
75 break;
76 case DXGI_FORMAT_R32G32B32_FLOAT:
77 GenerateMip<R32G32B32F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
78 break;
79 case DXGI_FORMAT_R16G16B16A16_FLOAT:
80 GenerateMip<A16B16G16R16F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
81 break;
82 case DXGI_FORMAT_R8G8_UNORM:
83 GenerateMip<R8G8>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
84 break;
85 case DXGI_FORMAT_R16_FLOAT:
86 GenerateMip<R16F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
87 break;
88 case DXGI_FORMAT_R16G16_FLOAT:
89 GenerateMip<R16G16F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
90 break;
91 case DXGI_FORMAT_R32_FLOAT:
92 GenerateMip<R32F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
93 break;
94 case DXGI_FORMAT_R32G32_FLOAT:
95 GenerateMip<R32G32F>(src->getWidth(), src->getHeight(), sourceData, srcMapped.RowPitch, destData, destMapped.RowPitch);
96 break;
97 default:
98 UNREACHABLE();
99 break;
100 }
101
102 dest->unmap();
103 src->unmap();
104 }
105
106 dest->markDirty();
107}
108
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000109bool Image11::isDirty() const
110{
111 return (mStagingTexture && mDirty);
112}
113
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000114bool Image11::updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000115{
daniel@transgaming.com46cf2492013-01-11 04:06:43 +0000116 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000117 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, width, height);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000118}
119
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000120bool Image11::updateSurface(TextureStorageInterfaceCube *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000121{
daniel@transgaming.com46cf2492013-01-11 04:06:43 +0000122 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000123 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, face, xoffset, yoffset, width, height);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000124}
125
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000126bool Image11::redefine(Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, bool forceRelease)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000127{
128 if (mWidth != width ||
129 mHeight != height ||
130 mInternalFormat != internalformat ||
131 forceRelease)
132 {
133 mRenderer = Renderer11::makeRenderer11(renderer);
134
135 mWidth = width;
136 mHeight = height;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000137 mDepth = depth;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000138 mInternalFormat = internalformat;
139 // compute the d3d format that will be used
140 mDXGIFormat = gl_d3d11::ConvertTextureFormat(internalformat);
141 mActualFormat = d3d11_gl::ConvertTextureInternalFormat(mDXGIFormat);
142
143 if (mStagingTexture)
144 {
145 mStagingTexture->Release();
146 mStagingTexture = NULL;
147 }
148
149 return true;
150 }
151
152 return false;
153}
154
155bool Image11::isRenderableFormat() const
156{
shannon.woods@transgaming.com2a0a39e2013-01-25 21:50:15 +0000157 return TextureStorage11::IsTextureFormatRenderable(mDXGIFormat);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000158}
159
160DXGI_FORMAT Image11::getDXGIFormat() const
161{
162 // this should only happen if the image hasn't been redefined first
163 // which would be a bug by the caller
164 ASSERT(mDXGIFormat != DXGI_FORMAT_UNKNOWN);
165
166 return mDXGIFormat;
167}
168
169// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
170// into the target pixel rectangle.
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000171void Image11::loadData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000172 GLint unpackAlignment, const void *input)
173{
174 D3D11_MAPPED_SUBRESOURCE mappedImage;
175 HRESULT result = map(&mappedImage);
176 if (FAILED(result))
177 {
178 ERR("Could not map image for loading.");
179 return;
180 }
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000181
182 GLsizei inputRowPitch = gl::ComputeRowPitch(width, mInternalFormat, unpackAlignment);
183 GLsizei inputDepthPitch = gl::ComputeDepthPitch(width, height, mInternalFormat, unpackAlignment);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000184 size_t pixelSize = d3d11::ComputePixelSizeBits(mDXGIFormat) / 8;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000185 void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + (yoffset * mappedImage.RowPitch + xoffset * pixelSize + zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000186
187 switch (mInternalFormat)
188 {
189 case GL_ALPHA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000190 loadAlphaDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000191 break;
192 case GL_LUMINANCE8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000193 loadLuminanceDataToNativeOrBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData, false);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000194 break;
195 case GL_ALPHA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000196 loadAlphaFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000197 break;
198 case GL_LUMINANCE32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000199 loadLuminanceFloatDataToRGB(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000200 break;
201 case GL_ALPHA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000202 loadAlphaHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000203 break;
204 case GL_LUMINANCE16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000205 loadLuminanceHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000206 break;
207 case GL_LUMINANCE8_ALPHA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000208 loadLuminanceAlphaDataToNativeOrBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData, false);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000209 break;
210 case GL_LUMINANCE_ALPHA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000211 loadLuminanceAlphaFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000212 break;
213 case GL_LUMINANCE_ALPHA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000214 loadLuminanceAlphaHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000215 break;
216 case GL_RGB8_OES:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000217 loadRGBUByteDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000218 break;
219 case GL_RGB565:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000220 loadRGB565DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000221 break;
222 case GL_RGBA8_OES:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000223 loadRGBAUByteDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000224 break;
225 case GL_RGBA4:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000226 loadRGBA4444DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000227 break;
228 case GL_RGB5_A1:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000229 loadRGBA5551DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000230 break;
231 case GL_BGRA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000232 loadBGRADataToBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000233 break;
234 case GL_RGB32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000235 loadRGBFloatDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000236 break;
237 case GL_RGB16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000238 loadRGBHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000239 break;
240 case GL_RGBA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000241 loadRGBAFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000242 break;
243 case GL_RGBA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000244 loadRGBAHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000245 break;
246 default: UNREACHABLE();
247 }
248
249 unmap();
250}
251
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000252void Image11::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
253 const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000254{
255 ASSERT(xoffset % 4 == 0);
256 ASSERT(yoffset % 4 == 0);
257
258 D3D11_MAPPED_SUBRESOURCE mappedImage;
259 HRESULT result = map(&mappedImage);
260 if (FAILED(result))
261 {
262 ERR("Could not map image for loading.");
263 return;
264 }
265
266 // Size computation assumes a 4x4 block compressed texture format
267 size_t blockSize = d3d11::ComputeBlockSizeBits(mDXGIFormat) / 8;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000268 void* offsetMappedData = (void*)((BYTE*)mappedImage.pData + ((yoffset / 4) * mappedImage.RowPitch + (xoffset / 4) * blockSize + zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000269
270 GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000271 GLsizei inputRowPitch = gl::ComputeCompressedRowPitch(width, mInternalFormat);
272 GLsizei inputDepthPitch = gl::ComputeCompressedDepthPitch(width, height, mInternalFormat);
273 int rows = inputSize / inputRowPitch;
274
275 for (int z = 0; z < depth; ++z)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000276 {
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000277 for (int y = 0; y < rows; ++y)
278 {
279 void *source = (void*)((BYTE*)input + y * inputRowPitch + z * inputDepthPitch);
280 void *dest = (void*)((BYTE*)offsetMappedData + y * mappedImage.RowPitch + z * mappedImage.DepthPitch);
281
282 memcpy(dest, source, inputRowPitch);
283 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000284 }
285
286 unmap();
287}
288
289void Image11::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source)
290{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000291 gl::Renderbuffer *colorbuffer = source->getReadColorbuffer();
292
293 if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat)
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000294 {
295 // No conversion needed-- use copyback fastpath
296 ID3D11Texture2D *colorBufferTexture = NULL;
297 unsigned int subresourceIndex = 0;
298
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000299 if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000300 {
301 D3D11_TEXTURE2D_DESC textureDesc;
302 colorBufferTexture->GetDesc(&textureDesc);
303
304 ID3D11Device *device = mRenderer->getDevice();
305 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
306
307 ID3D11Texture2D* srcTex = NULL;
308 if (textureDesc.SampleDesc.Count > 1)
309 {
310 D3D11_TEXTURE2D_DESC resolveDesc;
311 resolveDesc.Width = textureDesc.Width;
312 resolveDesc.Height = textureDesc.Height;
313 resolveDesc.MipLevels = 1;
314 resolveDesc.ArraySize = 1;
315 resolveDesc.Format = textureDesc.Format;
316 resolveDesc.SampleDesc.Count = 1;
317 resolveDesc.SampleDesc.Quality = 0;
318 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
319 resolveDesc.BindFlags = 0;
320 resolveDesc.CPUAccessFlags = 0;
321 resolveDesc.MiscFlags = 0;
322
323 HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex);
324 if (FAILED(result))
325 {
326 ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result);
327 return;
328 }
329
330 deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format);
331 subresourceIndex = 0;
332 }
333 else
334 {
335 srcTex = colorBufferTexture;
336 srcTex->AddRef();
337 }
338
339 D3D11_BOX srcBox;
340 srcBox.left = x;
341 srcBox.right = x + width;
342 srcBox.top = y;
343 srcBox.bottom = y + height;
344 srcBox.front = 0;
345 srcBox.back = 1;
346
347 deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, 0, srcTex, subresourceIndex, &srcBox);
348
349 srcTex->Release();
350 colorBufferTexture->Release();
351 }
352 }
353 else
354 {
355 // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels
356 D3D11_MAPPED_SUBRESOURCE mappedImage;
357 HRESULT result = map(&mappedImage);
358
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000359 // determine the offset coordinate into the destination buffer
360 GLsizei rowOffset = gl::ComputePixelSize(mActualFormat) * xoffset;
361 void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset;
362
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000363 mRenderer->readPixels(source, x, y, width, height, gl::ExtractFormat(mInternalFormat),
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000364 gl::ExtractType(mInternalFormat), mappedImage.RowPitch, false, 4, dataOffset);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000365
366 unmap();
367 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000368}
369
shannon.woods@transgaming.comcabb17c2013-02-28 23:20:45 +0000370ID3D11Texture2D *Image11::getStagingTexture()
371{
372 createStagingTexture();
373
374 return mStagingTexture;
375}
376
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000377unsigned int Image11::getStagingSubresource()
378{
379 createStagingTexture();
380
381 return mStagingSubresource;
382}
383
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000384void Image11::createStagingTexture()
385{
386 if (mStagingTexture)
387 {
388 return;
389 }
390
391 ID3D11Texture2D *newTexture = NULL;
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000392 int lodOffset = 1;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000393 const DXGI_FORMAT dxgiFormat = getDXGIFormat();
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000394 ASSERT(!d3d11::IsDepthStencilFormat(dxgiFormat)); // We should never get here for depth textures
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000395
396 if (mWidth != 0 && mHeight != 0)
397 {
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000398 GLsizei width = mWidth;
399 GLsizei height = mHeight;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000400
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000401 // adjust size if needed for compressed textures
402 gl::MakeValidSize(false, d3d11::IsCompressed(dxgiFormat), &width, &height, &lodOffset);
403 ID3D11Device *device = mRenderer->getDevice();
shannon.woods@transgaming.com7ae9e7f2013-02-28 23:13:27 +0000404
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000405 D3D11_TEXTURE2D_DESC desc;
shannon.woods@transgaming.com7ae9e7f2013-02-28 23:13:27 +0000406 desc.Width = width;
407 desc.Height = height;
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000408 desc.MipLevels = lodOffset + 1;
409 desc.ArraySize = 1;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000410 desc.Format = dxgiFormat;
411 desc.SampleDesc.Count = 1;
412 desc.SampleDesc.Quality = 0;
413 desc.Usage = D3D11_USAGE_STAGING;
414 desc.BindFlags = 0;
415 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
416 desc.MiscFlags = 0;
417
418 HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture);
419
420 if (FAILED(result))
421 {
422 ASSERT(result == E_OUTOFMEMORY);
423 ERR("Creating image failed.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +0000424 return gl::error(GL_OUT_OF_MEMORY);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000425 }
426 }
427
428 mStagingTexture = newTexture;
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000429 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000430 mDirty = false;
431}
432
433HRESULT Image11::map(D3D11_MAPPED_SUBRESOURCE *map)
434{
435 createStagingTexture();
436
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000437 HRESULT result = E_FAIL;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000438
439 if (mStagingTexture)
440 {
441 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000442 result = deviceContext->Map(mStagingTexture, mStagingSubresource, D3D11_MAP_WRITE, 0, map);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000443
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000444 // this can fail if the device is removed (from TDR)
445 if (d3d11::isDeviceLostError(result))
446 {
447 mRenderer->notifyDeviceLost();
448 }
449 else if (SUCCEEDED(result))
450 {
451 mDirty = true;
452 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000453 }
454
455 return result;
456}
457
458void Image11::unmap()
459{
460 if (mStagingTexture)
461 {
462 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000463 deviceContext->Unmap(mStagingTexture, mStagingSubresource);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000464 }
465}
466
467}