blob: 8ce35ac1f765abd3021f1ebccf6d5dc62e15ccf7 [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"
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +000019#include "libGLESv2/renderer/formatutils11.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000020#include "libGLESv2/renderer/renderer11_utils.h"
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000021#include "libGLESv2/renderer/generatemip.h"
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000022
23namespace rx
24{
25
26Image11::Image11()
27{
28 mStagingTexture = NULL;
29 mRenderer = NULL;
30 mDXGIFormat = DXGI_FORMAT_UNKNOWN;
31}
32
33Image11::~Image11()
34{
35 if (mStagingTexture)
36 {
37 mStagingTexture->Release();
38 }
39}
40
41Image11 *Image11::makeImage11(Image *img)
42{
apatrick@chromium.org8b400b12013-01-30 21:53:40 +000043 ASSERT(HAS_DYNAMIC_TYPE(rx::Image11*, img));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000044 return static_cast<rx::Image11*>(img);
45}
46
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000047void Image11::generateMipmap(Image11 *dest, Image11 *src)
48{
49 ASSERT(src->getDXGIFormat() == dest->getDXGIFormat());
50 ASSERT(src->getWidth() == 1 || src->getWidth() / 2 == dest->getWidth());
51 ASSERT(src->getHeight() == 1 || src->getHeight() / 2 == dest->getHeight());
52
53 D3D11_MAPPED_SUBRESOURCE destMapped, srcMapped;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +000054 dest->map(D3D11_MAP_WRITE, &destMapped);
55 src->map(D3D11_MAP_READ, &srcMapped);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000056
57 const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(srcMapped.pData);
58 unsigned char *destData = reinterpret_cast<unsigned char*>(destMapped.pData);
59
60 if (sourceData && destData)
61 {
62 switch (src->getDXGIFormat())
63 {
64 case DXGI_FORMAT_R8G8B8A8_UNORM:
65 case DXGI_FORMAT_B8G8R8A8_UNORM:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000066 GenerateMip<R8G8B8A8>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000067 break;
68 case DXGI_FORMAT_A8_UNORM:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000069 GenerateMip<A8>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000070 break;
71 case DXGI_FORMAT_R8_UNORM:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000072 GenerateMip<R8>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000073 break;
74 case DXGI_FORMAT_R32G32B32A32_FLOAT:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000075 GenerateMip<A32B32G32R32F>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000076 break;
77 case DXGI_FORMAT_R32G32B32_FLOAT:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000078 GenerateMip<R32G32B32F>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000079 break;
80 case DXGI_FORMAT_R16G16B16A16_FLOAT:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000081 GenerateMip<A16B16G16R16F>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000082 break;
83 case DXGI_FORMAT_R8G8_UNORM:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000084 GenerateMip<R8G8>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000085 break;
86 case DXGI_FORMAT_R16_FLOAT:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000087 GenerateMip<R16F>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000088 break;
89 case DXGI_FORMAT_R16G16_FLOAT:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000090 GenerateMip<R16G16F>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000091 break;
92 case DXGI_FORMAT_R32_FLOAT:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000093 GenerateMip<R32F>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000094 break;
95 case DXGI_FORMAT_R32G32_FLOAT:
shannonwoods@chromium.orge5b26d32013-05-30 00:03:20 +000096 GenerateMip<R32G32F>(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch, destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000097 break;
98 default:
99 UNREACHABLE();
100 break;
101 }
102
103 dest->unmap();
104 src->unmap();
105 }
106
107 dest->markDirty();
108}
109
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000110bool Image11::isDirty() const
111{
112 return (mStagingTexture && mDirty);
113}
114
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000115bool Image11::updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000116{
daniel@transgaming.com46cf2492013-01-11 04:06:43 +0000117 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +0000118 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, 0, width, height, 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000119}
120
daniel@transgaming.com87705f82012-12-20 21:10:45 +0000121bool Image11::updateSurface(TextureStorageInterfaceCube *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000122{
daniel@transgaming.com46cf2492013-01-11 04:06:43 +0000123 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +0000124 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, face, xoffset, yoffset, 0, width, height, 1);
125}
126
127bool Image11::updateSurface(TextureStorageInterface3D *storage, int level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth)
128{
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000129 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
130 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, zoffset, width, height, depth);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000131}
132
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +0000133bool Image11::updateSurface(TextureStorageInterface2DArray *storage, int level, GLint xoffset, GLint yoffset, GLint arrayLayer, GLsizei width, GLsizei height)
134{
135 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
136 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, arrayLayer, xoffset, yoffset, 0, width, height, 1);
137}
138
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000139bool Image11::redefine(Renderer *renderer, GLenum target, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, bool forceRelease)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000140{
141 if (mWidth != width ||
142 mHeight != height ||
143 mInternalFormat != internalformat ||
144 forceRelease)
145 {
146 mRenderer = Renderer11::makeRenderer11(renderer);
147
148 mWidth = width;
149 mHeight = height;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000150 mDepth = depth;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000151 mInternalFormat = internalformat;
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000152 mTarget = target;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000153 // compute the d3d format that will be used
154 mDXGIFormat = gl_d3d11::ConvertTextureFormat(internalformat);
155 mActualFormat = d3d11_gl::ConvertTextureInternalFormat(mDXGIFormat);
156
157 if (mStagingTexture)
158 {
159 mStagingTexture->Release();
160 mStagingTexture = NULL;
161 }
162
163 return true;
164 }
165
166 return false;
167}
168
169bool Image11::isRenderableFormat() const
170{
shannon.woods@transgaming.com2a0a39e2013-01-25 21:50:15 +0000171 return TextureStorage11::IsTextureFormatRenderable(mDXGIFormat);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000172}
173
174DXGI_FORMAT Image11::getDXGIFormat() const
175{
176 // this should only happen if the image hasn't been redefined first
177 // which would be a bug by the caller
178 ASSERT(mDXGIFormat != DXGI_FORMAT_UNKNOWN);
179
180 return mDXGIFormat;
181}
182
183// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
184// into the target pixel rectangle.
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000185void Image11::loadData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +0000186 GLint unpackAlignment, GLenum type, const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000187{
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +0000188 GLuint clientVersion = mRenderer->getCurrentClientVersion();
189 GLsizei inputRowPitch = gl::GetRowPitch(mInternalFormat, type, clientVersion, width, unpackAlignment);
190 GLsizei inputDepthPitch = gl::GetDepthPitch(mInternalFormat, type, clientVersion, width, height, unpackAlignment);
191 GLuint outputPixelSize = d3d11::GetFormatPixelBytes(mDXGIFormat);
192
193 LoadImageFunction loadFunction = d3d11::GetImageLoadFunction(mInternalFormat, type, clientVersion);
194 ASSERT(loadFunction != NULL);
195
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000196 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000197 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000198 if (FAILED(result))
199 {
200 ERR("Could not map image for loading.");
201 return;
202 }
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000203
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +0000204 void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + (yoffset * mappedImage.RowPitch + xoffset * outputPixelSize + zoffset * mappedImage.DepthPitch));
205 loadFunction(width, height, depth, input, inputRowPitch, inputDepthPitch, offsetMappedData, mappedImage.RowPitch, mappedImage.DepthPitch);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000206
207 unmap();
208}
209
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000210void Image11::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
211 const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000212{
shannonwoods@chromium.orgb8cabd52013-05-30 00:08:37 +0000213 GLuint clientVersion = mRenderer->getCurrentClientVersion();
214 GLsizei inputRowPitch = gl::GetRowPitch(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion, width, 1);
215 GLsizei inputDepthPitch = gl::GetDepthPitch(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion, width, height, 1);
216
217 GLuint outputPixelSize = d3d11::GetFormatPixelBytes(mDXGIFormat);
218 GLuint outputBlockWidth = d3d11::GetBlockWidth(mDXGIFormat);
219 GLuint outputBlockHeight = d3d11::GetBlockHeight(mDXGIFormat);
220
221 ASSERT(xoffset % outputBlockWidth == 0);
222 ASSERT(yoffset % outputBlockHeight == 0);
223
224 LoadImageFunction loadFunction = d3d11::GetImageLoadFunction(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion);
225 ASSERT(loadFunction != NULL);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000226
227 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000228 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000229 if (FAILED(result))
230 {
231 ERR("Could not map image for loading.");
232 return;
233 }
234
shannonwoods@chromium.orgb8cabd52013-05-30 00:08:37 +0000235 void* offsetMappedData = (void*)((BYTE*)mappedImage.pData + ((yoffset / outputBlockHeight) * mappedImage.RowPitch +
236 (xoffset / outputBlockWidth) * outputPixelSize +
237 zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000238
shannonwoods@chromium.orgb8cabd52013-05-30 00:08:37 +0000239 loadFunction(width, height, depth, input, inputRowPitch, inputDepthPitch,
240 offsetMappedData, mappedImage.RowPitch, mappedImage.DepthPitch);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000241
242 unmap();
243}
244
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000245void 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 +0000246{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000247 gl::Renderbuffer *colorbuffer = source->getReadColorbuffer();
248
249 if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat)
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000250 {
251 // No conversion needed-- use copyback fastpath
252 ID3D11Texture2D *colorBufferTexture = NULL;
253 unsigned int subresourceIndex = 0;
254
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000255 if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000256 {
257 D3D11_TEXTURE2D_DESC textureDesc;
258 colorBufferTexture->GetDesc(&textureDesc);
259
260 ID3D11Device *device = mRenderer->getDevice();
261 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
262
263 ID3D11Texture2D* srcTex = NULL;
264 if (textureDesc.SampleDesc.Count > 1)
265 {
266 D3D11_TEXTURE2D_DESC resolveDesc;
267 resolveDesc.Width = textureDesc.Width;
268 resolveDesc.Height = textureDesc.Height;
269 resolveDesc.MipLevels = 1;
270 resolveDesc.ArraySize = 1;
271 resolveDesc.Format = textureDesc.Format;
272 resolveDesc.SampleDesc.Count = 1;
273 resolveDesc.SampleDesc.Quality = 0;
274 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
275 resolveDesc.BindFlags = 0;
276 resolveDesc.CPUAccessFlags = 0;
277 resolveDesc.MiscFlags = 0;
278
279 HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex);
280 if (FAILED(result))
281 {
282 ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result);
283 return;
284 }
285
286 deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format);
287 subresourceIndex = 0;
288 }
289 else
290 {
291 srcTex = colorBufferTexture;
292 srcTex->AddRef();
293 }
294
295 D3D11_BOX srcBox;
296 srcBox.left = x;
297 srcBox.right = x + width;
298 srcBox.top = y;
299 srcBox.bottom = y + height;
300 srcBox.front = 0;
301 srcBox.back = 1;
302
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000303 deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, zoffset, srcTex, subresourceIndex, &srcBox);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000304
305 srcTex->Release();
306 colorBufferTexture->Release();
307 }
308 }
309 else
310 {
311 // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels
312 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000313 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000314
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000315 // determine the offset coordinate into the destination buffer
316 GLsizei rowOffset = gl::ComputePixelSize(mActualFormat) * xoffset;
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000317 void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset + zoffset * mappedImage.DepthPitch;
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000318
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000319 mRenderer->readPixels(source, x, y, width, height, gl::ExtractFormat(mInternalFormat),
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000320 gl::ExtractType(mInternalFormat), mappedImage.RowPitch, false, 4, dataOffset);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000321
322 unmap();
323 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000324}
325
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000326ID3D11Resource *Image11::getStagingTexture()
shannon.woods@transgaming.comcabb17c2013-02-28 23:20:45 +0000327{
328 createStagingTexture();
329
330 return mStagingTexture;
331}
332
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000333unsigned int Image11::getStagingSubresource()
334{
335 createStagingTexture();
336
337 return mStagingSubresource;
338}
339
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000340void Image11::createStagingTexture()
341{
342 if (mStagingTexture)
343 {
344 return;
345 }
346
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000347 const DXGI_FORMAT dxgiFormat = getDXGIFormat();
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000348 ASSERT(!d3d11::IsDepthStencilFormat(dxgiFormat)); // We should never get here for depth textures
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000349
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000350 if (mWidth > 0 && mHeight > 0 && mDepth > 0)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000351 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000352 ID3D11Device *device = mRenderer->getDevice();
353
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000354 int lodOffset = 1;
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000355 GLsizei width = mWidth;
356 GLsizei height = mHeight;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000357
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000358 // adjust size if needed for compressed textures
359 gl::MakeValidSize(false, d3d11::IsCompressed(dxgiFormat), &width, &height, &lodOffset);
shannon.woods@transgaming.com7ae9e7f2013-02-28 23:13:27 +0000360
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000361 if (mTarget == GL_TEXTURE_3D)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000362 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000363 ID3D11Texture3D *newTexture = NULL;
364
365 D3D11_TEXTURE3D_DESC desc;
366 desc.Width = width;
367 desc.Height = height;
368 desc.Depth = mDepth;
369 desc.MipLevels = lodOffset + 1;
370 desc.Format = dxgiFormat;
371 desc.Usage = D3D11_USAGE_STAGING;
372 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000373 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000374 desc.MiscFlags = 0;
375
376 HRESULT result = device->CreateTexture3D(&desc, NULL, &newTexture);
377 if (FAILED(result))
378 {
379 ASSERT(result == E_OUTOFMEMORY);
380 ERR("Creating image failed.");
381 return gl::error(GL_OUT_OF_MEMORY);
382 }
383
384 mStagingTexture = newTexture;
385 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
386 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000387 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 +0000388 {
389 ID3D11Texture2D *newTexture = NULL;
390
391 D3D11_TEXTURE2D_DESC desc;
392 desc.Width = width;
393 desc.Height = height;
394 desc.MipLevels = lodOffset + 1;
395 desc.ArraySize = 1;
396 desc.Format = dxgiFormat;
397 desc.SampleDesc.Count = 1;
398 desc.SampleDesc.Quality = 0;
399 desc.Usage = D3D11_USAGE_STAGING;
400 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000401 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000402 desc.MiscFlags = 0;
403
404 HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture);
405
406 if (FAILED(result))
407 {
408 ASSERT(result == E_OUTOFMEMORY);
409 ERR("Creating image failed.");
410 return gl::error(GL_OUT_OF_MEMORY);
411 }
412
413 mStagingTexture = newTexture;
414 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000415 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000416 else
417 {
418 UNREACHABLE();
419 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000420 }
421
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000422 mDirty = false;
423}
424
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000425HRESULT Image11::map(D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000426{
427 createStagingTexture();
428
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000429 HRESULT result = E_FAIL;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000430
431 if (mStagingTexture)
432 {
433 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000434 result = deviceContext->Map(mStagingTexture, mStagingSubresource, mapType, 0, map);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000435
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000436 // this can fail if the device is removed (from TDR)
437 if (d3d11::isDeviceLostError(result))
438 {
439 mRenderer->notifyDeviceLost();
440 }
441 else if (SUCCEEDED(result))
442 {
443 mDirty = true;
444 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000445 }
446
447 return result;
448}
449
450void Image11::unmap()
451{
452 if (mStagingTexture)
453 {
454 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000455 deviceContext->Unmap(mStagingTexture, mStagingSubresource);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000456 }
457}
458
459}