blob: fd95e581cb08741be57543892ff21eb031164662 [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"
21
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
shannonwoods@chromium.orgcead8ad2013-05-30 00:08:52 +000052 MipGenerationFunction mipFunction = d3d11::GetMipGenerationFunction(src->getDXGIFormat());
53 ASSERT(mipFunction != NULL);
54
55 D3D11_MAPPED_SUBRESOURCE destMapped;
56 HRESULT destMapResult = dest->map(D3D11_MAP_WRITE, &destMapped);
57 if (FAILED(destMapResult))
58 {
59 ERR("Failed to map destination image for mip map generation. HRESULT:0x%X", destMapResult);
60 return;
61 }
62
63 D3D11_MAPPED_SUBRESOURCE srcMapped;
64 HRESULT srcMapResult = src->map(D3D11_MAP_READ, &srcMapped);
65 if (FAILED(srcMapResult))
66 {
67 ERR("Failed to map source image for mip map generation. HRESULT:0x%X", srcMapResult);
68
69 dest->unmap();
70 return;
71 }
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000072
73 const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(srcMapped.pData);
74 unsigned char *destData = reinterpret_cast<unsigned char*>(destMapped.pData);
75
shannonwoods@chromium.orgcead8ad2013-05-30 00:08:52 +000076 mipFunction(src->getWidth(), src->getHeight(), src->getDepth(), sourceData, srcMapped.RowPitch, srcMapped.DepthPitch,
77 destData, destMapped.RowPitch, destMapped.DepthPitch);
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000078
shannonwoods@chromium.orgcead8ad2013-05-30 00:08:52 +000079 dest->unmap();
80 src->unmap();
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +000081
82 dest->markDirty();
83}
84
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000085bool Image11::isDirty() const
86{
87 return (mStagingTexture && mDirty);
88}
89
daniel@transgaming.com87705f82012-12-20 21:10:45 +000090bool Image11::updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000091{
daniel@transgaming.com46cf2492013-01-11 04:06:43 +000092 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +000093 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, 0, width, height, 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000094}
95
daniel@transgaming.com87705f82012-12-20 21:10:45 +000096bool Image11::updateSurface(TextureStorageInterfaceCube *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +000097{
daniel@transgaming.com46cf2492013-01-11 04:06:43 +000098 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +000099 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, face, xoffset, yoffset, 0, width, height, 1);
100}
101
102bool Image11::updateSurface(TextureStorageInterface3D *storage, int level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth)
103{
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000104 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
105 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, 0, xoffset, yoffset, zoffset, width, height, depth);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000106}
107
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +0000108bool Image11::updateSurface(TextureStorageInterface2DArray *storage, int level, GLint xoffset, GLint yoffset, GLint arrayLayer, GLsizei width, GLsizei height)
109{
110 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
111 return storage11->updateSubresourceLevel(getStagingTexture(), getStagingSubresource(), level, arrayLayer, xoffset, yoffset, 0, width, height, 1);
112}
113
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000114bool Image11::redefine(Renderer *renderer, GLenum target, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, bool forceRelease)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000115{
116 if (mWidth != width ||
117 mHeight != height ||
118 mInternalFormat != internalformat ||
119 forceRelease)
120 {
121 mRenderer = Renderer11::makeRenderer11(renderer);
122
123 mWidth = width;
124 mHeight = height;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000125 mDepth = depth;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000126 mInternalFormat = internalformat;
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000127 mTarget = target;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000128 // compute the d3d format that will be used
129 mDXGIFormat = gl_d3d11::ConvertTextureFormat(internalformat);
130 mActualFormat = d3d11_gl::ConvertTextureInternalFormat(mDXGIFormat);
131
132 if (mStagingTexture)
133 {
134 mStagingTexture->Release();
135 mStagingTexture = NULL;
136 }
137
138 return true;
139 }
140
141 return false;
142}
143
144bool Image11::isRenderableFormat() const
145{
shannon.woods@transgaming.com2a0a39e2013-01-25 21:50:15 +0000146 return TextureStorage11::IsTextureFormatRenderable(mDXGIFormat);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000147}
148
149DXGI_FORMAT Image11::getDXGIFormat() const
150{
151 // this should only happen if the image hasn't been redefined first
152 // which would be a bug by the caller
153 ASSERT(mDXGIFormat != DXGI_FORMAT_UNKNOWN);
154
155 return mDXGIFormat;
156}
157
158// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
159// into the target pixel rectangle.
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000160void Image11::loadData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +0000161 GLint unpackAlignment, GLenum type, const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000162{
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +0000163 GLuint clientVersion = mRenderer->getCurrentClientVersion();
164 GLsizei inputRowPitch = gl::GetRowPitch(mInternalFormat, type, clientVersion, width, unpackAlignment);
165 GLsizei inputDepthPitch = gl::GetDepthPitch(mInternalFormat, type, clientVersion, width, height, unpackAlignment);
166 GLuint outputPixelSize = d3d11::GetFormatPixelBytes(mDXGIFormat);
167
168 LoadImageFunction loadFunction = d3d11::GetImageLoadFunction(mInternalFormat, type, clientVersion);
169 ASSERT(loadFunction != NULL);
170
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000171 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000172 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000173 if (FAILED(result))
174 {
175 ERR("Could not map image for loading.");
176 return;
177 }
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000178
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +0000179 void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + (yoffset * mappedImage.RowPitch + xoffset * outputPixelSize + zoffset * mappedImage.DepthPitch));
180 loadFunction(width, height, depth, input, inputRowPitch, inputDepthPitch, offsetMappedData, mappedImage.RowPitch, mappedImage.DepthPitch);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000181
182 unmap();
183}
184
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000185void Image11::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
186 const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000187{
shannonwoods@chromium.orgb8cabd52013-05-30 00:08:37 +0000188 GLuint clientVersion = mRenderer->getCurrentClientVersion();
189 GLsizei inputRowPitch = gl::GetRowPitch(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion, width, 1);
190 GLsizei inputDepthPitch = gl::GetDepthPitch(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion, width, height, 1);
191
192 GLuint outputPixelSize = d3d11::GetFormatPixelBytes(mDXGIFormat);
193 GLuint outputBlockWidth = d3d11::GetBlockWidth(mDXGIFormat);
194 GLuint outputBlockHeight = d3d11::GetBlockHeight(mDXGIFormat);
195
196 ASSERT(xoffset % outputBlockWidth == 0);
197 ASSERT(yoffset % outputBlockHeight == 0);
198
199 LoadImageFunction loadFunction = d3d11::GetImageLoadFunction(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion);
200 ASSERT(loadFunction != NULL);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000201
202 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000203 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000204 if (FAILED(result))
205 {
206 ERR("Could not map image for loading.");
207 return;
208 }
209
shannonwoods@chromium.orgb8cabd52013-05-30 00:08:37 +0000210 void* offsetMappedData = (void*)((BYTE*)mappedImage.pData + ((yoffset / outputBlockHeight) * mappedImage.RowPitch +
211 (xoffset / outputBlockWidth) * outputPixelSize +
212 zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000213
shannonwoods@chromium.orgb8cabd52013-05-30 00:08:37 +0000214 loadFunction(width, height, depth, input, inputRowPitch, inputDepthPitch,
215 offsetMappedData, mappedImage.RowPitch, mappedImage.DepthPitch);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000216
217 unmap();
218}
219
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000220void 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 +0000221{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000222 gl::Renderbuffer *colorbuffer = source->getReadColorbuffer();
223
224 if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat)
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000225 {
226 // No conversion needed-- use copyback fastpath
227 ID3D11Texture2D *colorBufferTexture = NULL;
228 unsigned int subresourceIndex = 0;
229
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000230 if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000231 {
232 D3D11_TEXTURE2D_DESC textureDesc;
233 colorBufferTexture->GetDesc(&textureDesc);
234
235 ID3D11Device *device = mRenderer->getDevice();
236 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
237
238 ID3D11Texture2D* srcTex = NULL;
239 if (textureDesc.SampleDesc.Count > 1)
240 {
241 D3D11_TEXTURE2D_DESC resolveDesc;
242 resolveDesc.Width = textureDesc.Width;
243 resolveDesc.Height = textureDesc.Height;
244 resolveDesc.MipLevels = 1;
245 resolveDesc.ArraySize = 1;
246 resolveDesc.Format = textureDesc.Format;
247 resolveDesc.SampleDesc.Count = 1;
248 resolveDesc.SampleDesc.Quality = 0;
249 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
250 resolveDesc.BindFlags = 0;
251 resolveDesc.CPUAccessFlags = 0;
252 resolveDesc.MiscFlags = 0;
253
254 HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex);
255 if (FAILED(result))
256 {
257 ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result);
258 return;
259 }
260
261 deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format);
262 subresourceIndex = 0;
263 }
264 else
265 {
266 srcTex = colorBufferTexture;
267 srcTex->AddRef();
268 }
269
270 D3D11_BOX srcBox;
271 srcBox.left = x;
272 srcBox.right = x + width;
273 srcBox.top = y;
274 srcBox.bottom = y + height;
275 srcBox.front = 0;
276 srcBox.back = 1;
277
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000278 deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, zoffset, srcTex, subresourceIndex, &srcBox);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000279
280 srcTex->Release();
281 colorBufferTexture->Release();
282 }
283 }
284 else
285 {
286 // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels
287 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000288 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000289
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000290 // determine the offset coordinate into the destination buffer
291 GLsizei rowOffset = gl::ComputePixelSize(mActualFormat) * xoffset;
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000292 void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset + zoffset * mappedImage.DepthPitch;
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000293
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000294 mRenderer->readPixels(source, x, y, width, height, gl::ExtractFormat(mInternalFormat),
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000295 gl::ExtractType(mInternalFormat), mappedImage.RowPitch, false, 4, dataOffset);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000296
297 unmap();
298 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000299}
300
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000301ID3D11Resource *Image11::getStagingTexture()
shannon.woods@transgaming.comcabb17c2013-02-28 23:20:45 +0000302{
303 createStagingTexture();
304
305 return mStagingTexture;
306}
307
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000308unsigned int Image11::getStagingSubresource()
309{
310 createStagingTexture();
311
312 return mStagingSubresource;
313}
314
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000315void Image11::createStagingTexture()
316{
317 if (mStagingTexture)
318 {
319 return;
320 }
321
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000322 const DXGI_FORMAT dxgiFormat = getDXGIFormat();
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000323 ASSERT(!d3d11::IsDepthStencilFormat(dxgiFormat)); // We should never get here for depth textures
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000324
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000325 if (mWidth > 0 && mHeight > 0 && mDepth > 0)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000326 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000327 ID3D11Device *device = mRenderer->getDevice();
328
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000329 int lodOffset = 1;
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000330 GLsizei width = mWidth;
331 GLsizei height = mHeight;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000332
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000333 // adjust size if needed for compressed textures
334 gl::MakeValidSize(false, d3d11::IsCompressed(dxgiFormat), &width, &height, &lodOffset);
shannon.woods@transgaming.com7ae9e7f2013-02-28 23:13:27 +0000335
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000336 if (mTarget == GL_TEXTURE_3D)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000337 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000338 ID3D11Texture3D *newTexture = NULL;
339
340 D3D11_TEXTURE3D_DESC desc;
341 desc.Width = width;
342 desc.Height = height;
343 desc.Depth = mDepth;
344 desc.MipLevels = lodOffset + 1;
345 desc.Format = dxgiFormat;
346 desc.Usage = D3D11_USAGE_STAGING;
347 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000348 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000349 desc.MiscFlags = 0;
350
351 HRESULT result = device->CreateTexture3D(&desc, NULL, &newTexture);
352 if (FAILED(result))
353 {
354 ASSERT(result == E_OUTOFMEMORY);
355 ERR("Creating image failed.");
356 return gl::error(GL_OUT_OF_MEMORY);
357 }
358
359 mStagingTexture = newTexture;
360 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
361 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000362 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 +0000363 {
364 ID3D11Texture2D *newTexture = NULL;
365
366 D3D11_TEXTURE2D_DESC desc;
367 desc.Width = width;
368 desc.Height = height;
369 desc.MipLevels = lodOffset + 1;
370 desc.ArraySize = 1;
371 desc.Format = dxgiFormat;
372 desc.SampleDesc.Count = 1;
373 desc.SampleDesc.Quality = 0;
374 desc.Usage = D3D11_USAGE_STAGING;
375 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000376 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000377 desc.MiscFlags = 0;
378
379 HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture);
380
381 if (FAILED(result))
382 {
383 ASSERT(result == E_OUTOFMEMORY);
384 ERR("Creating image failed.");
385 return gl::error(GL_OUT_OF_MEMORY);
386 }
387
388 mStagingTexture = newTexture;
389 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000390 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000391 else
392 {
393 UNREACHABLE();
394 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000395 }
396
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000397 mDirty = false;
398}
399
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000400HRESULT Image11::map(D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000401{
402 createStagingTexture();
403
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000404 HRESULT result = E_FAIL;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000405
406 if (mStagingTexture)
407 {
408 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000409 result = deviceContext->Map(mStagingTexture, mStagingSubresource, mapType, 0, map);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000410
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000411 // this can fail if the device is removed (from TDR)
412 if (d3d11::isDeviceLostError(result))
413 {
414 mRenderer->notifyDeviceLost();
415 }
416 else if (SUCCEEDED(result))
417 {
418 mDirty = true;
419 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000420 }
421
422 return result;
423}
424
425void Image11::unmap()
426{
427 if (mStagingTexture)
428 {
429 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000430 deviceContext->Unmap(mStagingTexture, mStagingSubresource);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000431 }
432}
433
434}