blob: 03164c97605dad38f9412e9b4388d51f5f14acd5 [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;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +000053 dest->map(D3D11_MAP_WRITE, &destMapped);
54 src->map(D3D11_MAP_READ, &srcMapped);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000055
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.com6c86bd52013-04-13 03:45:45 +0000132bool Image11::updateSurface(TextureStorageInterface2DArray *storage, int level, GLint xoffset, GLint yoffset, GLint arrayLayer, GLsizei width, GLsizei height)
133{
134 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
135 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, arrayLayer, xoffset, yoffset, 0, width, height, 1);
136}
137
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000138bool Image11::redefine(Renderer *renderer, GLenum target, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, bool forceRelease)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000139{
140 if (mWidth != width ||
141 mHeight != height ||
142 mInternalFormat != internalformat ||
143 forceRelease)
144 {
145 mRenderer = Renderer11::makeRenderer11(renderer);
146
147 mWidth = width;
148 mHeight = height;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000149 mDepth = depth;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000150 mInternalFormat = internalformat;
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000151 mTarget = target;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000152 // compute the d3d format that will be used
153 mDXGIFormat = gl_d3d11::ConvertTextureFormat(internalformat);
154 mActualFormat = d3d11_gl::ConvertTextureInternalFormat(mDXGIFormat);
155
156 if (mStagingTexture)
157 {
158 mStagingTexture->Release();
159 mStagingTexture = NULL;
160 }
161
162 return true;
163 }
164
165 return false;
166}
167
168bool Image11::isRenderableFormat() const
169{
shannon.woods@transgaming.com2a0a39e2013-01-25 21:50:15 +0000170 return TextureStorage11::IsTextureFormatRenderable(mDXGIFormat);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000171}
172
173DXGI_FORMAT Image11::getDXGIFormat() const
174{
175 // this should only happen if the image hasn't been redefined first
176 // which would be a bug by the caller
177 ASSERT(mDXGIFormat != DXGI_FORMAT_UNKNOWN);
178
179 return mDXGIFormat;
180}
181
182// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
183// into the target pixel rectangle.
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000184void Image11::loadData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000185 GLint unpackAlignment, const void *input)
186{
187 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000188 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000189 if (FAILED(result))
190 {
191 ERR("Could not map image for loading.");
192 return;
193 }
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000194
195 GLsizei inputRowPitch = gl::ComputeRowPitch(width, mInternalFormat, unpackAlignment);
196 GLsizei inputDepthPitch = gl::ComputeDepthPitch(width, height, mInternalFormat, unpackAlignment);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000197 size_t pixelSize = d3d11::ComputePixelSizeBits(mDXGIFormat) / 8;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000198 void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + (yoffset * mappedImage.RowPitch + xoffset * pixelSize + zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000199
200 switch (mInternalFormat)
201 {
202 case GL_ALPHA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000203 loadAlphaDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000204 break;
205 case GL_LUMINANCE8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000206 loadLuminanceDataToNativeOrBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData, false);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000207 break;
208 case GL_ALPHA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000209 loadAlphaFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000210 break;
211 case GL_LUMINANCE32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000212 loadLuminanceFloatDataToRGB(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000213 break;
214 case GL_ALPHA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000215 loadAlphaHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000216 break;
217 case GL_LUMINANCE16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000218 loadLuminanceHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000219 break;
220 case GL_LUMINANCE8_ALPHA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000221 loadLuminanceAlphaDataToNativeOrBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData, false);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000222 break;
223 case GL_LUMINANCE_ALPHA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000224 loadLuminanceAlphaFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000225 break;
226 case GL_LUMINANCE_ALPHA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000227 loadLuminanceAlphaHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000228 break;
229 case GL_RGB8_OES:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000230 loadRGBUByteDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000231 break;
232 case GL_RGB565:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000233 loadRGB565DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000234 break;
235 case GL_RGBA8_OES:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000236 loadRGBAUByteDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000237 break;
238 case GL_RGBA4:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000239 loadRGBA4444DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000240 break;
241 case GL_RGB5_A1:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000242 loadRGBA5551DataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000243 break;
244 case GL_BGRA8_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000245 loadBGRADataToBGRA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000246 break;
247 case GL_RGB32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000248 loadRGBFloatDataToNative(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000249 break;
250 case GL_RGB16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000251 loadRGBHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000252 break;
253 case GL_RGBA32F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000254 loadRGBAFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000255 break;
256 case GL_RGBA16F_EXT:
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000257 loadRGBAHalfFloatDataToRGBA(width, height, depth, inputRowPitch, inputDepthPitch, input, mappedImage.RowPitch, mappedImage.DepthPitch, offsetMappedData);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000258 break;
259 default: UNREACHABLE();
260 }
261
262 unmap();
263}
264
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000265void Image11::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
266 const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000267{
268 ASSERT(xoffset % 4 == 0);
269 ASSERT(yoffset % 4 == 0);
270
271 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000272 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000273 if (FAILED(result))
274 {
275 ERR("Could not map image for loading.");
276 return;
277 }
278
279 // Size computation assumes a 4x4 block compressed texture format
280 size_t blockSize = d3d11::ComputeBlockSizeBits(mDXGIFormat) / 8;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000281 void* offsetMappedData = (void*)((BYTE*)mappedImage.pData + ((yoffset / 4) * mappedImage.RowPitch + (xoffset / 4) * blockSize + zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000282
283 GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000284 GLsizei inputRowPitch = gl::ComputeCompressedRowPitch(width, mInternalFormat);
285 GLsizei inputDepthPitch = gl::ComputeCompressedDepthPitch(width, height, mInternalFormat);
286 int rows = inputSize / inputRowPitch;
287
288 for (int z = 0; z < depth; ++z)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000289 {
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000290 for (int y = 0; y < rows; ++y)
291 {
292 void *source = (void*)((BYTE*)input + y * inputRowPitch + z * inputDepthPitch);
293 void *dest = (void*)((BYTE*)offsetMappedData + y * mappedImage.RowPitch + z * mappedImage.DepthPitch);
294
295 memcpy(dest, source, inputRowPitch);
296 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000297 }
298
299 unmap();
300}
301
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000302void 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 +0000303{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000304 gl::Renderbuffer *colorbuffer = source->getReadColorbuffer();
305
306 if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat)
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000307 {
308 // No conversion needed-- use copyback fastpath
309 ID3D11Texture2D *colorBufferTexture = NULL;
310 unsigned int subresourceIndex = 0;
311
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000312 if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000313 {
314 D3D11_TEXTURE2D_DESC textureDesc;
315 colorBufferTexture->GetDesc(&textureDesc);
316
317 ID3D11Device *device = mRenderer->getDevice();
318 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
319
320 ID3D11Texture2D* srcTex = NULL;
321 if (textureDesc.SampleDesc.Count > 1)
322 {
323 D3D11_TEXTURE2D_DESC resolveDesc;
324 resolveDesc.Width = textureDesc.Width;
325 resolveDesc.Height = textureDesc.Height;
326 resolveDesc.MipLevels = 1;
327 resolveDesc.ArraySize = 1;
328 resolveDesc.Format = textureDesc.Format;
329 resolveDesc.SampleDesc.Count = 1;
330 resolveDesc.SampleDesc.Quality = 0;
331 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
332 resolveDesc.BindFlags = 0;
333 resolveDesc.CPUAccessFlags = 0;
334 resolveDesc.MiscFlags = 0;
335
336 HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex);
337 if (FAILED(result))
338 {
339 ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result);
340 return;
341 }
342
343 deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format);
344 subresourceIndex = 0;
345 }
346 else
347 {
348 srcTex = colorBufferTexture;
349 srcTex->AddRef();
350 }
351
352 D3D11_BOX srcBox;
353 srcBox.left = x;
354 srcBox.right = x + width;
355 srcBox.top = y;
356 srcBox.bottom = y + height;
357 srcBox.front = 0;
358 srcBox.back = 1;
359
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000360 deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, zoffset, srcTex, subresourceIndex, &srcBox);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000361
362 srcTex->Release();
363 colorBufferTexture->Release();
364 }
365 }
366 else
367 {
368 // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels
369 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000370 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000371
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000372 // determine the offset coordinate into the destination buffer
373 GLsizei rowOffset = gl::ComputePixelSize(mActualFormat) * xoffset;
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000374 void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset + zoffset * mappedImage.DepthPitch;
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000375
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000376 mRenderer->readPixels(source, x, y, width, height, gl::ExtractFormat(mInternalFormat),
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000377 gl::ExtractType(mInternalFormat), mappedImage.RowPitch, false, 4, dataOffset);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000378
379 unmap();
380 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000381}
382
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000383ID3D11Resource *Image11::getStagingTexture()
shannon.woods@transgaming.comcabb17c2013-02-28 23:20:45 +0000384{
385 createStagingTexture();
386
387 return mStagingTexture;
388}
389
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000390unsigned int Image11::getStagingSubresource()
391{
392 createStagingTexture();
393
394 return mStagingSubresource;
395}
396
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000397void Image11::createStagingTexture()
398{
399 if (mStagingTexture)
400 {
401 return;
402 }
403
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000404 const DXGI_FORMAT dxgiFormat = getDXGIFormat();
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000405 ASSERT(!d3d11::IsDepthStencilFormat(dxgiFormat)); // We should never get here for depth textures
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000406
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000407 if (mWidth > 0 && mHeight > 0 && mDepth > 0)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000408 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000409 ID3D11Device *device = mRenderer->getDevice();
410
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000411 int lodOffset = 1;
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000412 GLsizei width = mWidth;
413 GLsizei height = mHeight;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000414
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000415 // adjust size if needed for compressed textures
416 gl::MakeValidSize(false, d3d11::IsCompressed(dxgiFormat), &width, &height, &lodOffset);
shannon.woods@transgaming.com7ae9e7f2013-02-28 23:13:27 +0000417
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000418 if (mTarget == GL_TEXTURE_3D)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000419 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000420 ID3D11Texture3D *newTexture = NULL;
421
422 D3D11_TEXTURE3D_DESC desc;
423 desc.Width = width;
424 desc.Height = height;
425 desc.Depth = mDepth;
426 desc.MipLevels = lodOffset + 1;
427 desc.Format = dxgiFormat;
428 desc.Usage = D3D11_USAGE_STAGING;
429 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000430 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000431 desc.MiscFlags = 0;
432
433 HRESULT result = device->CreateTexture3D(&desc, NULL, &newTexture);
434 if (FAILED(result))
435 {
436 ASSERT(result == E_OUTOFMEMORY);
437 ERR("Creating image failed.");
438 return gl::error(GL_OUT_OF_MEMORY);
439 }
440
441 mStagingTexture = newTexture;
442 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
443 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000444 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 +0000445 {
446 ID3D11Texture2D *newTexture = NULL;
447
448 D3D11_TEXTURE2D_DESC desc;
449 desc.Width = width;
450 desc.Height = height;
451 desc.MipLevels = lodOffset + 1;
452 desc.ArraySize = 1;
453 desc.Format = dxgiFormat;
454 desc.SampleDesc.Count = 1;
455 desc.SampleDesc.Quality = 0;
456 desc.Usage = D3D11_USAGE_STAGING;
457 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000458 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000459 desc.MiscFlags = 0;
460
461 HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture);
462
463 if (FAILED(result))
464 {
465 ASSERT(result == E_OUTOFMEMORY);
466 ERR("Creating image failed.");
467 return gl::error(GL_OUT_OF_MEMORY);
468 }
469
470 mStagingTexture = newTexture;
471 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000472 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000473 else
474 {
475 UNREACHABLE();
476 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000477 }
478
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000479 mDirty = false;
480}
481
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000482HRESULT Image11::map(D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000483{
484 createStagingTexture();
485
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000486 HRESULT result = E_FAIL;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000487
488 if (mStagingTexture)
489 {
490 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000491 result = deviceContext->Map(mStagingTexture, mStagingSubresource, mapType, 0, map);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000492
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000493 // this can fail if the device is removed (from TDR)
494 if (d3d11::isDeviceLostError(result))
495 {
496 mRenderer->notifyDeviceLost();
497 }
498 else if (SUCCEEDED(result))
499 {
500 mDirty = true;
501 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000502 }
503
504 return result;
505}
506
507void Image11::unmap()
508{
509 if (mStagingTexture)
510 {
511 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000512 deviceContext->Unmap(mStagingTexture, mStagingSubresource);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000513 }
514}
515
516}