blob: ea7fd416db5883f859cdbfb3b157a23fa475f0a5 [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.com56074f32013-04-13 03:45:30 +0000132bool Image11::redefine(Renderer *renderer, GLenum target, 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;
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000145 mTarget = target;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000146 // compute the d3d format that will be used
147 mDXGIFormat = gl_d3d11::ConvertTextureFormat(internalformat);
148 mActualFormat = d3d11_gl::ConvertTextureInternalFormat(mDXGIFormat);
149
150 if (mStagingTexture)
151 {
152 mStagingTexture->Release();
153 mStagingTexture = NULL;
154 }
155
156 return true;
157 }
158
159 return false;
160}
161
162bool Image11::isRenderableFormat() const
163{
shannon.woods@transgaming.com2a0a39e2013-01-25 21:50:15 +0000164 return TextureStorage11::IsTextureFormatRenderable(mDXGIFormat);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000165}
166
167DXGI_FORMAT Image11::getDXGIFormat() const
168{
169 // this should only happen if the image hasn't been redefined first
170 // which would be a bug by the caller
171 ASSERT(mDXGIFormat != DXGI_FORMAT_UNKNOWN);
172
173 return mDXGIFormat;
174}
175
176// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
177// into the target pixel rectangle.
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000178void Image11::loadData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000179 GLint unpackAlignment, const void *input)
180{
181 D3D11_MAPPED_SUBRESOURCE mappedImage;
182 HRESULT result = map(&mappedImage);
183 if (FAILED(result))
184 {
185 ERR("Could not map image for loading.");
186 return;
187 }
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000188
189 GLsizei inputRowPitch = gl::ComputeRowPitch(width, mInternalFormat, unpackAlignment);
190 GLsizei inputDepthPitch = gl::ComputeDepthPitch(width, height, mInternalFormat, unpackAlignment);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000191 size_t pixelSize = d3d11::ComputePixelSizeBits(mDXGIFormat) / 8;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000192 void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + (yoffset * mappedImage.RowPitch + xoffset * pixelSize + zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000193
194 switch (mInternalFormat)
195 {
196 case GL_ALPHA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000197 loadAlphaDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000198 break;
199 case GL_LUMINANCE8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000200 loadLuminanceDataToNativeOrBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData, false);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000201 break;
202 case GL_ALPHA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000203 loadAlphaFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000204 break;
205 case GL_LUMINANCE32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000206 loadLuminanceFloatDataToRGB(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000207 break;
208 case GL_ALPHA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000209 loadAlphaHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000210 break;
211 case GL_LUMINANCE16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000212 loadLuminanceHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000213 break;
214 case GL_LUMINANCE8_ALPHA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000215 loadLuminanceAlphaDataToNativeOrBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData, false);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000216 break;
217 case GL_LUMINANCE_ALPHA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000218 loadLuminanceAlphaFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000219 break;
220 case GL_LUMINANCE_ALPHA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000221 loadLuminanceAlphaHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000222 break;
223 case GL_RGB8_OES:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000224 loadRGBUByteDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000225 break;
226 case GL_RGB565:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000227 loadRGB565DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000228 break;
229 case GL_RGBA8_OES:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000230 loadRGBAUByteDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000231 break;
232 case GL_RGBA4:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000233 loadRGBA4444DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000234 break;
235 case GL_RGB5_A1:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000236 loadRGBA5551DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000237 break;
238 case GL_BGRA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000239 loadBGRADataToBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000240 break;
241 case GL_RGB32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000242 loadRGBFloatDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000243 break;
244 case GL_RGB16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000245 loadRGBHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000246 break;
247 case GL_RGBA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000248 loadRGBAFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000249 break;
250 case GL_RGBA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000251 loadRGBAHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000252 break;
253 default: UNREACHABLE();
254 }
255
256 unmap();
257}
258
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000259void Image11::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
260 const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000261{
262 ASSERT(xoffset % 4 == 0);
263 ASSERT(yoffset % 4 == 0);
264
265 D3D11_MAPPED_SUBRESOURCE mappedImage;
266 HRESULT result = map(&mappedImage);
267 if (FAILED(result))
268 {
269 ERR("Could not map image for loading.");
270 return;
271 }
272
273 // Size computation assumes a 4x4 block compressed texture format
274 size_t blockSize = d3d11::ComputeBlockSizeBits(mDXGIFormat) / 8;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000275 void* offsetMappedData = (void*)((BYTE*)mappedImage.pData + ((yoffset / 4) * mappedImage.RowPitch + (xoffset / 4) * blockSize + zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000276
277 GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000278 GLsizei inputRowPitch = gl::ComputeCompressedRowPitch(width, mInternalFormat);
279 GLsizei inputDepthPitch = gl::ComputeCompressedDepthPitch(width, height, mInternalFormat);
280 int rows = inputSize / inputRowPitch;
281
282 for (int z = 0; z < depth; ++z)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000283 {
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000284 for (int y = 0; y < rows; ++y)
285 {
286 void *source = (void*)((BYTE*)input + y * inputRowPitch + z * inputDepthPitch);
287 void *dest = (void*)((BYTE*)offsetMappedData + y * mappedImage.RowPitch + z * mappedImage.DepthPitch);
288
289 memcpy(dest, source, inputRowPitch);
290 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000291 }
292
293 unmap();
294}
295
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000296void 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 +0000297{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000298 gl::Renderbuffer *colorbuffer = source->getReadColorbuffer();
299
300 if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat)
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000301 {
302 // No conversion needed-- use copyback fastpath
303 ID3D11Texture2D *colorBufferTexture = NULL;
304 unsigned int subresourceIndex = 0;
305
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000306 if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000307 {
308 D3D11_TEXTURE2D_DESC textureDesc;
309 colorBufferTexture->GetDesc(&textureDesc);
310
311 ID3D11Device *device = mRenderer->getDevice();
312 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
313
314 ID3D11Texture2D* srcTex = NULL;
315 if (textureDesc.SampleDesc.Count > 1)
316 {
317 D3D11_TEXTURE2D_DESC resolveDesc;
318 resolveDesc.Width = textureDesc.Width;
319 resolveDesc.Height = textureDesc.Height;
320 resolveDesc.MipLevels = 1;
321 resolveDesc.ArraySize = 1;
322 resolveDesc.Format = textureDesc.Format;
323 resolveDesc.SampleDesc.Count = 1;
324 resolveDesc.SampleDesc.Quality = 0;
325 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
326 resolveDesc.BindFlags = 0;
327 resolveDesc.CPUAccessFlags = 0;
328 resolveDesc.MiscFlags = 0;
329
330 HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex);
331 if (FAILED(result))
332 {
333 ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result);
334 return;
335 }
336
337 deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format);
338 subresourceIndex = 0;
339 }
340 else
341 {
342 srcTex = colorBufferTexture;
343 srcTex->AddRef();
344 }
345
346 D3D11_BOX srcBox;
347 srcBox.left = x;
348 srcBox.right = x + width;
349 srcBox.top = y;
350 srcBox.bottom = y + height;
351 srcBox.front = 0;
352 srcBox.back = 1;
353
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000354 deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, zoffset, srcTex, subresourceIndex, &srcBox);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000355
356 srcTex->Release();
357 colorBufferTexture->Release();
358 }
359 }
360 else
361 {
362 // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels
363 D3D11_MAPPED_SUBRESOURCE mappedImage;
364 HRESULT result = map(&mappedImage);
365
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000366 // determine the offset coordinate into the destination buffer
367 GLsizei rowOffset = gl::ComputePixelSize(mActualFormat) * xoffset;
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000368 void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset + zoffset * mappedImage.DepthPitch;
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000369
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000370 mRenderer->readPixels(source, x, y, width, height, gl::ExtractFormat(mInternalFormat),
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000371 gl::ExtractType(mInternalFormat), mappedImage.RowPitch, false, 4, dataOffset);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000372
373 unmap();
374 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000375}
376
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000377ID3D11Resource *Image11::getStagingTexture()
shannon.woods@transgaming.comcabb17c2013-02-28 23:20:45 +0000378{
379 createStagingTexture();
380
381 return mStagingTexture;
382}
383
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000384unsigned int Image11::getStagingSubresource()
385{
386 createStagingTexture();
387
388 return mStagingSubresource;
389}
390
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000391void Image11::createStagingTexture()
392{
393 if (mStagingTexture)
394 {
395 return;
396 }
397
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.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000405 int lodOffset = 1;
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000406 GLsizei width = mWidth;
407 GLsizei height = mHeight;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000408
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000409 // adjust size if needed for compressed textures
410 gl::MakeValidSize(false, d3d11::IsCompressed(dxgiFormat), &width, &height, &lodOffset);
shannon.woods@transgaming.com7ae9e7f2013-02-28 23:13:27 +0000411
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000412 if (mTarget == GL_TEXTURE_3D)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000413 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000414 ID3D11Texture3D *newTexture = NULL;
415
416 D3D11_TEXTURE3D_DESC desc;
417 desc.Width = width;
418 desc.Height = height;
419 desc.Depth = mDepth;
420 desc.MipLevels = lodOffset + 1;
421 desc.Format = dxgiFormat;
422 desc.Usage = D3D11_USAGE_STAGING;
423 desc.BindFlags = 0;
424 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
425 desc.MiscFlags = 0;
426
427 HRESULT result = device->CreateTexture3D(&desc, NULL, &newTexture);
428 if (FAILED(result))
429 {
430 ASSERT(result == E_OUTOFMEMORY);
431 ERR("Creating image failed.");
432 return gl::error(GL_OUT_OF_MEMORY);
433 }
434
435 mStagingTexture = newTexture;
436 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
437 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000438 else if (mTarget == GL_TEXTURE_2D || mTarget == GL_TEXTURE_2D_ARRAY || mTarget == GL_TEXTURE_CUBE_MAP)
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000439 {
440 ID3D11Texture2D *newTexture = NULL;
441
442 D3D11_TEXTURE2D_DESC desc;
443 desc.Width = width;
444 desc.Height = height;
445 desc.MipLevels = lodOffset + 1;
446 desc.ArraySize = 1;
447 desc.Format = dxgiFormat;
448 desc.SampleDesc.Count = 1;
449 desc.SampleDesc.Quality = 0;
450 desc.Usage = D3D11_USAGE_STAGING;
451 desc.BindFlags = 0;
452 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
453 desc.MiscFlags = 0;
454
455 HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture);
456
457 if (FAILED(result))
458 {
459 ASSERT(result == E_OUTOFMEMORY);
460 ERR("Creating image failed.");
461 return gl::error(GL_OUT_OF_MEMORY);
462 }
463
464 mStagingTexture = newTexture;
465 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000466 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000467 else
468 {
469 UNREACHABLE();
470 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000471 }
472
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000473 mDirty = false;
474}
475
476HRESULT Image11::map(D3D11_MAPPED_SUBRESOURCE *map)
477{
478 createStagingTexture();
479
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000480 HRESULT result = E_FAIL;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000481
482 if (mStagingTexture)
483 {
484 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000485 result = deviceContext->Map(mStagingTexture, mStagingSubresource, D3D11_MAP_WRITE, 0, map);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000486
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000487 // this can fail if the device is removed (from TDR)
488 if (d3d11::isDeviceLostError(result))
489 {
490 mRenderer->notifyDeviceLost();
491 }
492 else if (SUCCEEDED(result))
493 {
494 mDirty = true;
495 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000496 }
497
498 return result;
499}
500
501void Image11::unmap()
502{
503 if (mStagingTexture)
504 {
505 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000506 deviceContext->Unmap(mStagingTexture, mStagingSubresource);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000507 }
508}
509
510}