blob: 47b5192ced886e510cd7d63a749ca652930563a4 [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.com@gtempaccount.com2058d642013-04-13 03:42:50 +0000117 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, 0, width, height, 1);
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.com@gtempaccount.com2058d642013-04-13 03:42:50 +0000123 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, face, xoffset, yoffset, 0, width, height, 1);
124}
125
126bool Image11::updateSurface(TextureStorageInterface3D *storage, int level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth)
127{
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000128 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
129 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, zoffset, width, height, depth);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000130}
131
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000132bool Image11::redefine(Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, bool forceRelease)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000133{
134 if (mWidth != width ||
135 mHeight != height ||
136 mInternalFormat != internalformat ||
137 forceRelease)
138 {
139 mRenderer = Renderer11::makeRenderer11(renderer);
140
141 mWidth = width;
142 mHeight = height;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000143 mDepth = depth;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000144 mInternalFormat = internalformat;
145 // compute the d3d format that will be used
146 mDXGIFormat = gl_d3d11::ConvertTextureFormat(internalformat);
147 mActualFormat = d3d11_gl::ConvertTextureInternalFormat(mDXGIFormat);
148
149 if (mStagingTexture)
150 {
151 mStagingTexture->Release();
152 mStagingTexture = NULL;
153 }
154
155 return true;
156 }
157
158 return false;
159}
160
161bool Image11::isRenderableFormat() const
162{
shannon.woods@transgaming.com2a0a39e2013-01-25 21:50:15 +0000163 return TextureStorage11::IsTextureFormatRenderable(mDXGIFormat);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000164}
165
166DXGI_FORMAT Image11::getDXGIFormat() const
167{
168 // this should only happen if the image hasn't been redefined first
169 // which would be a bug by the caller
170 ASSERT(mDXGIFormat != DXGI_FORMAT_UNKNOWN);
171
172 return mDXGIFormat;
173}
174
175// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
176// into the target pixel rectangle.
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000177void Image11::loadData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000178 GLint unpackAlignment, const void *input)
179{
180 D3D11_MAPPED_SUBRESOURCE mappedImage;
181 HRESULT result = map(&mappedImage);
182 if (FAILED(result))
183 {
184 ERR("Could not map image for loading.");
185 return;
186 }
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000187
188 GLsizei inputRowPitch = gl::ComputeRowPitch(width, mInternalFormat, unpackAlignment);
189 GLsizei inputDepthPitch = gl::ComputeDepthPitch(width, height, mInternalFormat, unpackAlignment);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000190 size_t pixelSize = d3d11::ComputePixelSizeBits(mDXGIFormat) / 8;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000191 void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + (yoffset * mappedImage.RowPitch + xoffset * pixelSize + zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000192
193 switch (mInternalFormat)
194 {
195 case GL_ALPHA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000196 loadAlphaDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000197 break;
198 case GL_LUMINANCE8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000199 loadLuminanceDataToNativeOrBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData, false);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000200 break;
201 case GL_ALPHA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000202 loadAlphaFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000203 break;
204 case GL_LUMINANCE32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000205 loadLuminanceFloatDataToRGB(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000206 break;
207 case GL_ALPHA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000208 loadAlphaHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000209 break;
210 case GL_LUMINANCE16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000211 loadLuminanceHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000212 break;
213 case GL_LUMINANCE8_ALPHA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000214 loadLuminanceAlphaDataToNativeOrBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData, false);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000215 break;
216 case GL_LUMINANCE_ALPHA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000217 loadLuminanceAlphaFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000218 break;
219 case GL_LUMINANCE_ALPHA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000220 loadLuminanceAlphaHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000221 break;
222 case GL_RGB8_OES:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000223 loadRGBUByteDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000224 break;
225 case GL_RGB565:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000226 loadRGB565DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000227 break;
228 case GL_RGBA8_OES:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000229 loadRGBAUByteDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000230 break;
231 case GL_RGBA4:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000232 loadRGBA4444DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000233 break;
234 case GL_RGB5_A1:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000235 loadRGBA5551DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000236 break;
237 case GL_BGRA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000238 loadBGRADataToBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000239 break;
240 case GL_RGB32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000241 loadRGBFloatDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000242 break;
243 case GL_RGB16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000244 loadRGBHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000245 break;
246 case GL_RGBA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000247 loadRGBAFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000248 break;
249 case GL_RGBA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000250 loadRGBAHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000251 break;
252 default: UNREACHABLE();
253 }
254
255 unmap();
256}
257
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000258void Image11::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
259 const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000260{
261 ASSERT(xoffset % 4 == 0);
262 ASSERT(yoffset % 4 == 0);
263
264 D3D11_MAPPED_SUBRESOURCE mappedImage;
265 HRESULT result = map(&mappedImage);
266 if (FAILED(result))
267 {
268 ERR("Could not map image for loading.");
269 return;
270 }
271
272 // Size computation assumes a 4x4 block compressed texture format
273 size_t blockSize = d3d11::ComputeBlockSizeBits(mDXGIFormat) / 8;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000274 void* offsetMappedData = (void*)((BYTE*)mappedImage.pData + ((yoffset / 4) * mappedImage.RowPitch + (xoffset / 4) * blockSize + zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000275
276 GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000277 GLsizei inputRowPitch = gl::ComputeCompressedRowPitch(width, mInternalFormat);
278 GLsizei inputDepthPitch = gl::ComputeCompressedDepthPitch(width, height, mInternalFormat);
279 int rows = inputSize / inputRowPitch;
280
281 for (int z = 0; z < depth; ++z)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000282 {
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000283 for (int y = 0; y < rows; ++y)
284 {
285 void *source = (void*)((BYTE*)input + y * inputRowPitch + z * inputDepthPitch);
286 void *dest = (void*)((BYTE*)offsetMappedData + y * mappedImage.RowPitch + z * mappedImage.DepthPitch);
287
288 memcpy(dest, source, inputRowPitch);
289 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000290 }
291
292 unmap();
293}
294
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000295void Image11::copy(GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000296{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000297 gl::Renderbuffer *colorbuffer = source->getReadColorbuffer();
298
299 if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat)
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000300 {
301 // No conversion needed-- use copyback fastpath
302 ID3D11Texture2D *colorBufferTexture = NULL;
303 unsigned int subresourceIndex = 0;
304
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000305 if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000306 {
307 D3D11_TEXTURE2D_DESC textureDesc;
308 colorBufferTexture->GetDesc(&textureDesc);
309
310 ID3D11Device *device = mRenderer->getDevice();
311 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
312
313 ID3D11Texture2D* srcTex = NULL;
314 if (textureDesc.SampleDesc.Count > 1)
315 {
316 D3D11_TEXTURE2D_DESC resolveDesc;
317 resolveDesc.Width = textureDesc.Width;
318 resolveDesc.Height = textureDesc.Height;
319 resolveDesc.MipLevels = 1;
320 resolveDesc.ArraySize = 1;
321 resolveDesc.Format = textureDesc.Format;
322 resolveDesc.SampleDesc.Count = 1;
323 resolveDesc.SampleDesc.Quality = 0;
324 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
325 resolveDesc.BindFlags = 0;
326 resolveDesc.CPUAccessFlags = 0;
327 resolveDesc.MiscFlags = 0;
328
329 HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex);
330 if (FAILED(result))
331 {
332 ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result);
333 return;
334 }
335
336 deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format);
337 subresourceIndex = 0;
338 }
339 else
340 {
341 srcTex = colorBufferTexture;
342 srcTex->AddRef();
343 }
344
345 D3D11_BOX srcBox;
346 srcBox.left = x;
347 srcBox.right = x + width;
348 srcBox.top = y;
349 srcBox.bottom = y + height;
350 srcBox.front = 0;
351 srcBox.back = 1;
352
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000353 deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, zoffset, srcTex, subresourceIndex, &srcBox);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000354
355 srcTex->Release();
356 colorBufferTexture->Release();
357 }
358 }
359 else
360 {
361 // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels
362 D3D11_MAPPED_SUBRESOURCE mappedImage;
363 HRESULT result = map(&mappedImage);
364
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000365 // determine the offset coordinate into the destination buffer
366 GLsizei rowOffset = gl::ComputePixelSize(mActualFormat) * xoffset;
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000367 void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset + zoffset * mappedImage.DepthPitch;
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000368
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000369 mRenderer->readPixels(source, x, y, width, height, gl::ExtractFormat(mInternalFormat),
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000370 gl::ExtractType(mInternalFormat), mappedImage.RowPitch, false, 4, dataOffset);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000371
372 unmap();
373 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000374}
375
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000376ID3D11Resource *Image11::getStagingTexture()
shannon.woods@transgaming.comcabb17c2013-02-28 23:20:45 +0000377{
378 createStagingTexture();
379
380 return mStagingTexture;
381}
382
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000383unsigned int Image11::getStagingSubresource()
384{
385 createStagingTexture();
386
387 return mStagingSubresource;
388}
389
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000390void Image11::createStagingTexture()
391{
392 if (mStagingTexture)
393 {
394 return;
395 }
396
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000397 int lodOffset = 1;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000398 const DXGI_FORMAT dxgiFormat = getDXGIFormat();
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000399 ASSERT(!d3d11::IsDepthStencilFormat(dxgiFormat)); // We should never get here for depth textures
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000400
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000401 if (mWidth > 0 && mHeight > 0 && mDepth > 0)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000402 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000403 ID3D11Device *device = mRenderer->getDevice();
404
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000405 GLsizei width = mWidth;
406 GLsizei height = mHeight;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000407
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000408 // adjust size if needed for compressed textures
409 gl::MakeValidSize(false, d3d11::IsCompressed(dxgiFormat), &width, &height, &lodOffset);
shannon.woods@transgaming.com7ae9e7f2013-02-28 23:13:27 +0000410
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000411 if (mDepth > 1)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000412 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000413 ID3D11Texture3D *newTexture = NULL;
414
415 D3D11_TEXTURE3D_DESC desc;
416 desc.Width = width;
417 desc.Height = height;
418 desc.Depth = mDepth;
419 desc.MipLevels = lodOffset + 1;
420 desc.Format = dxgiFormat;
421 desc.Usage = D3D11_USAGE_STAGING;
422 desc.BindFlags = 0;
423 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
424 desc.MiscFlags = 0;
425
426 HRESULT result = device->CreateTexture3D(&desc, NULL, &newTexture);
427 if (FAILED(result))
428 {
429 ASSERT(result == E_OUTOFMEMORY);
430 ERR("Creating image failed.");
431 return gl::error(GL_OUT_OF_MEMORY);
432 }
433
434 mStagingTexture = newTexture;
435 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
436 }
437 else
438 {
439 ID3D11Texture2D *newTexture = NULL;
440
441 D3D11_TEXTURE2D_DESC desc;
442 desc.Width = width;
443 desc.Height = height;
444 desc.MipLevels = lodOffset + 1;
445 desc.ArraySize = 1;
446 desc.Format = dxgiFormat;
447 desc.SampleDesc.Count = 1;
448 desc.SampleDesc.Quality = 0;
449 desc.Usage = D3D11_USAGE_STAGING;
450 desc.BindFlags = 0;
451 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
452 desc.MiscFlags = 0;
453
454 HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture);
455
456 if (FAILED(result))
457 {
458 ASSERT(result == E_OUTOFMEMORY);
459 ERR("Creating image failed.");
460 return gl::error(GL_OUT_OF_MEMORY);
461 }
462
463 mStagingTexture = newTexture;
464 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000465 }
466 }
467
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000468 mDirty = false;
469}
470
471HRESULT Image11::map(D3D11_MAPPED_SUBRESOURCE *map)
472{
473 createStagingTexture();
474
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000475 HRESULT result = E_FAIL;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000476
477 if (mStagingTexture)
478 {
479 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000480 result = deviceContext->Map(mStagingTexture, mStagingSubresource, D3D11_MAP_WRITE, 0, map);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000481
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000482 // this can fail if the device is removed (from TDR)
483 if (d3d11::isDeviceLostError(result))
484 {
485 mRenderer->notifyDeviceLost();
486 }
487 else if (SUCCEEDED(result))
488 {
489 mDirty = true;
490 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000491 }
492
493 return result;
494}
495
496void Image11::unmap()
497{
498 if (mStagingTexture)
499 {
500 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000501 deviceContext->Unmap(mStagingTexture, mStagingSubresource);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000502 }
503}
504
505}