blob: 6e343b37a8b47cfe8786471be7198b739424a9e5 [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"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000018#include "common/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);
shannonwoods@chromium.org803be0a2013-05-30 00:08:59 +0000122 GLuint clientVersion = mRenderer->getCurrentClientVersion();
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000123
124 mWidth = width;
125 mHeight = height;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000126 mDepth = depth;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000127 mInternalFormat = internalformat;
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000128 mTarget = target;
shannonwoods@chromium.org803be0a2013-05-30 00:08:59 +0000129
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000130 // compute the d3d format that will be used
shannonwoods@chromium.org803be0a2013-05-30 00:08:59 +0000131 mDXGIFormat = gl_d3d11::GetTexFormat(internalformat, clientVersion);
132 mActualFormat = d3d11_gl::GetInternalFormat(mDXGIFormat);
133 mRenderable = gl_d3d11::GetTexFormat(internalformat, clientVersion) != DXGI_FORMAT_UNKNOWN;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000134
135 if (mStagingTexture)
136 {
137 mStagingTexture->Release();
138 mStagingTexture = NULL;
139 }
140
141 return true;
142 }
143
144 return false;
145}
146
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000147DXGI_FORMAT Image11::getDXGIFormat() const
148{
149 // this should only happen if the image hasn't been redefined first
150 // which would be a bug by the caller
151 ASSERT(mDXGIFormat != DXGI_FORMAT_UNKNOWN);
152
153 return mDXGIFormat;
154}
155
156// Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input
157// into the target pixel rectangle.
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000158void Image11::loadData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +0000159 GLint unpackAlignment, GLenum type, const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000160{
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +0000161 GLuint clientVersion = mRenderer->getCurrentClientVersion();
162 GLsizei inputRowPitch = gl::GetRowPitch(mInternalFormat, type, clientVersion, width, unpackAlignment);
163 GLsizei inputDepthPitch = gl::GetDepthPitch(mInternalFormat, type, clientVersion, width, height, unpackAlignment);
164 GLuint outputPixelSize = d3d11::GetFormatPixelBytes(mDXGIFormat);
165
166 LoadImageFunction loadFunction = d3d11::GetImageLoadFunction(mInternalFormat, type, clientVersion);
167 ASSERT(loadFunction != NULL);
168
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000169 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000170 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000171 if (FAILED(result))
172 {
173 ERR("Could not map image for loading.");
174 return;
175 }
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000176
shannonwoods@chromium.org557aab02013-05-30 00:08:27 +0000177 void* offsetMappedData = (void*)((BYTE *)mappedImage.pData + (yoffset * mappedImage.RowPitch + xoffset * outputPixelSize + zoffset * mappedImage.DepthPitch));
178 loadFunction(width, height, depth, input, inputRowPitch, inputDepthPitch, offsetMappedData, mappedImage.RowPitch, mappedImage.DepthPitch);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000179
180 unmap();
181}
182
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000183void Image11::loadCompressedData(GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth,
184 const void *input)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000185{
shannonwoods@chromium.orgb8cabd52013-05-30 00:08:37 +0000186 GLuint clientVersion = mRenderer->getCurrentClientVersion();
187 GLsizei inputRowPitch = gl::GetRowPitch(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion, width, 1);
188 GLsizei inputDepthPitch = gl::GetDepthPitch(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion, width, height, 1);
189
190 GLuint outputPixelSize = d3d11::GetFormatPixelBytes(mDXGIFormat);
191 GLuint outputBlockWidth = d3d11::GetBlockWidth(mDXGIFormat);
192 GLuint outputBlockHeight = d3d11::GetBlockHeight(mDXGIFormat);
193
194 ASSERT(xoffset % outputBlockWidth == 0);
195 ASSERT(yoffset % outputBlockHeight == 0);
196
197 LoadImageFunction loadFunction = d3d11::GetImageLoadFunction(mInternalFormat, GL_UNSIGNED_BYTE, clientVersion);
198 ASSERT(loadFunction != NULL);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000199
200 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000201 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000202 if (FAILED(result))
203 {
204 ERR("Could not map image for loading.");
205 return;
206 }
207
shannonwoods@chromium.orgb8cabd52013-05-30 00:08:37 +0000208 void* offsetMappedData = (void*)((BYTE*)mappedImage.pData + ((yoffset / outputBlockHeight) * mappedImage.RowPitch +
209 (xoffset / outputBlockWidth) * outputPixelSize +
210 zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000211
shannonwoods@chromium.orgb8cabd52013-05-30 00:08:37 +0000212 loadFunction(width, height, depth, input, inputRowPitch, inputDepthPitch,
213 offsetMappedData, mappedImage.RowPitch, mappedImage.DepthPitch);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000214
215 unmap();
216}
217
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000218void 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 +0000219{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000220 gl::Renderbuffer *colorbuffer = source->getReadColorbuffer();
221
222 if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat)
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000223 {
224 // No conversion needed-- use copyback fastpath
225 ID3D11Texture2D *colorBufferTexture = NULL;
226 unsigned int subresourceIndex = 0;
227
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000228 if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000229 {
230 D3D11_TEXTURE2D_DESC textureDesc;
231 colorBufferTexture->GetDesc(&textureDesc);
232
233 ID3D11Device *device = mRenderer->getDevice();
234 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
235
236 ID3D11Texture2D* srcTex = NULL;
237 if (textureDesc.SampleDesc.Count > 1)
238 {
239 D3D11_TEXTURE2D_DESC resolveDesc;
240 resolveDesc.Width = textureDesc.Width;
241 resolveDesc.Height = textureDesc.Height;
242 resolveDesc.MipLevels = 1;
243 resolveDesc.ArraySize = 1;
244 resolveDesc.Format = textureDesc.Format;
245 resolveDesc.SampleDesc.Count = 1;
246 resolveDesc.SampleDesc.Quality = 0;
247 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
248 resolveDesc.BindFlags = 0;
249 resolveDesc.CPUAccessFlags = 0;
250 resolveDesc.MiscFlags = 0;
251
252 HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex);
253 if (FAILED(result))
254 {
255 ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result);
256 return;
257 }
258
259 deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format);
260 subresourceIndex = 0;
261 }
262 else
263 {
264 srcTex = colorBufferTexture;
265 srcTex->AddRef();
266 }
267
268 D3D11_BOX srcBox;
269 srcBox.left = x;
270 srcBox.right = x + width;
271 srcBox.top = y;
272 srcBox.bottom = y + height;
273 srcBox.front = 0;
274 srcBox.back = 1;
275
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000276 deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, zoffset, srcTex, subresourceIndex, &srcBox);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000277
278 srcTex->Release();
279 colorBufferTexture->Release();
280 }
281 }
282 else
283 {
284 // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels
285 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000286 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
shannonwoods@chromium.org803be0a2013-05-30 00:08:59 +0000287
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000288 // determine the offset coordinate into the destination buffer
shannonwoods@chromium.org803be0a2013-05-30 00:08:59 +0000289 GLuint clientVersion = mRenderer->getCurrentClientVersion();
290 GLsizei rowOffset = gl::GetPixelBytes(mActualFormat, clientVersion) * xoffset;
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000291 void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset + zoffset * mappedImage.DepthPitch;
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000292
shannonwoods@chromium.org803be0a2013-05-30 00:08:59 +0000293 mRenderer->readPixels(source, x, y, width, height, gl::GetFormat(mInternalFormat, clientVersion),
294 gl::GetType(mInternalFormat, clientVersion), mappedImage.RowPitch, false, 4, dataOffset);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000295
296 unmap();
297 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000298}
299
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000300ID3D11Resource *Image11::getStagingTexture()
shannon.woods@transgaming.comcabb17c2013-02-28 23:20:45 +0000301{
302 createStagingTexture();
303
304 return mStagingTexture;
305}
306
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000307unsigned int Image11::getStagingSubresource()
308{
309 createStagingTexture();
310
311 return mStagingSubresource;
312}
313
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000314void Image11::createStagingTexture()
315{
316 if (mStagingTexture)
317 {
318 return;
319 }
320
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000321 const DXGI_FORMAT dxgiFormat = getDXGIFormat();
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000322
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000323 if (mWidth > 0 && mHeight > 0 && mDepth > 0)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000324 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000325 ID3D11Device *device = mRenderer->getDevice();
326
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000327 int lodOffset = 1;
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000328 GLsizei width = mWidth;
329 GLsizei height = mHeight;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000330
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000331 // adjust size if needed for compressed textures
shannonwoods@chromium.org803be0a2013-05-30 00:08:59 +0000332 d3d11::MakeValidSize(false, dxgiFormat, &width, &height, &lodOffset);
shannon.woods@transgaming.com7ae9e7f2013-02-28 23:13:27 +0000333
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000334 if (mTarget == GL_TEXTURE_3D)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000335 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000336 ID3D11Texture3D *newTexture = NULL;
337
338 D3D11_TEXTURE3D_DESC desc;
339 desc.Width = width;
340 desc.Height = height;
341 desc.Depth = mDepth;
342 desc.MipLevels = lodOffset + 1;
343 desc.Format = dxgiFormat;
344 desc.Usage = D3D11_USAGE_STAGING;
345 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000346 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000347 desc.MiscFlags = 0;
348
349 HRESULT result = device->CreateTexture3D(&desc, NULL, &newTexture);
350 if (FAILED(result))
351 {
352 ASSERT(result == E_OUTOFMEMORY);
353 ERR("Creating image failed.");
354 return gl::error(GL_OUT_OF_MEMORY);
355 }
356
357 mStagingTexture = newTexture;
358 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
359 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000360 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 +0000361 {
362 ID3D11Texture2D *newTexture = NULL;
363
364 D3D11_TEXTURE2D_DESC desc;
365 desc.Width = width;
366 desc.Height = height;
367 desc.MipLevels = lodOffset + 1;
368 desc.ArraySize = 1;
369 desc.Format = dxgiFormat;
370 desc.SampleDesc.Count = 1;
371 desc.SampleDesc.Quality = 0;
372 desc.Usage = D3D11_USAGE_STAGING;
373 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000374 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000375 desc.MiscFlags = 0;
376
377 HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture);
378
379 if (FAILED(result))
380 {
381 ASSERT(result == E_OUTOFMEMORY);
382 ERR("Creating image failed.");
383 return gl::error(GL_OUT_OF_MEMORY);
384 }
385
386 mStagingTexture = newTexture;
387 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000388 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000389 else
390 {
391 UNREACHABLE();
392 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000393 }
394
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000395 mDirty = false;
396}
397
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000398HRESULT Image11::map(D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000399{
400 createStagingTexture();
401
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000402 HRESULT result = E_FAIL;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000403
404 if (mStagingTexture)
405 {
406 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000407 result = deviceContext->Map(mStagingTexture, mStagingSubresource, mapType, 0, map);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000408
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000409 // this can fail if the device is removed (from TDR)
410 if (d3d11::isDeviceLostError(result))
411 {
412 mRenderer->notifyDeviceLost();
413 }
414 else if (SUCCEEDED(result))
415 {
416 mDirty = true;
417 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000418 }
419
420 return result;
421}
422
423void Image11::unmap()
424{
425 if (mStagingTexture)
426 {
427 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000428 deviceContext->Unmap(mStagingTexture, mStagingSubresource);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000429 }
430}
431
432}