blob: 2f690144dbbfa77572e5ec043d18099e4fa7ab5c [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{
213 ASSERT(xoffset % 4 == 0);
214 ASSERT(yoffset % 4 == 0);
215
216 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000217 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000218 if (FAILED(result))
219 {
220 ERR("Could not map image for loading.");
221 return;
222 }
223
224 // Size computation assumes a 4x4 block compressed texture format
225 size_t blockSize = d3d11::ComputeBlockSizeBits(mDXGIFormat) / 8;
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000226 void* offsetMappedData = (void*)((BYTE*)mappedImage.pData + ((yoffset / 4) * mappedImage.RowPitch + (xoffset / 4) * blockSize + zoffset * mappedImage.DepthPitch));
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000227
228 GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat);
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000229 GLsizei inputRowPitch = gl::ComputeCompressedRowPitch(width, mInternalFormat);
230 GLsizei inputDepthPitch = gl::ComputeCompressedDepthPitch(width, height, mInternalFormat);
231 int rows = inputSize / inputRowPitch;
232
233 for (int z = 0; z < depth; ++z)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000234 {
shannon.woods%transgaming.com@gtempaccount.com4760c562013-04-13 03:42:30 +0000235 for (int y = 0; y < rows; ++y)
236 {
237 void *source = (void*)((BYTE*)input + y * inputRowPitch + z * inputDepthPitch);
238 void *dest = (void*)((BYTE*)offsetMappedData + y * mappedImage.RowPitch + z * mappedImage.DepthPitch);
239
240 memcpy(dest, source, inputRowPitch);
241 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000242 }
243
244 unmap();
245}
246
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000247void 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 +0000248{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000249 gl::Renderbuffer *colorbuffer = source->getReadColorbuffer();
250
251 if (colorbuffer && colorbuffer->getActualFormat() == (GLuint)mActualFormat)
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000252 {
253 // No conversion needed-- use copyback fastpath
254 ID3D11Texture2D *colorBufferTexture = NULL;
255 unsigned int subresourceIndex = 0;
256
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +0000257 if (mRenderer->getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000258 {
259 D3D11_TEXTURE2D_DESC textureDesc;
260 colorBufferTexture->GetDesc(&textureDesc);
261
262 ID3D11Device *device = mRenderer->getDevice();
263 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
264
265 ID3D11Texture2D* srcTex = NULL;
266 if (textureDesc.SampleDesc.Count > 1)
267 {
268 D3D11_TEXTURE2D_DESC resolveDesc;
269 resolveDesc.Width = textureDesc.Width;
270 resolveDesc.Height = textureDesc.Height;
271 resolveDesc.MipLevels = 1;
272 resolveDesc.ArraySize = 1;
273 resolveDesc.Format = textureDesc.Format;
274 resolveDesc.SampleDesc.Count = 1;
275 resolveDesc.SampleDesc.Quality = 0;
276 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
277 resolveDesc.BindFlags = 0;
278 resolveDesc.CPUAccessFlags = 0;
279 resolveDesc.MiscFlags = 0;
280
281 HRESULT result = device->CreateTexture2D(&resolveDesc, NULL, &srcTex);
282 if (FAILED(result))
283 {
284 ERR("Failed to create resolve texture for Image11::copy, HRESULT: 0x%X.", result);
285 return;
286 }
287
288 deviceContext->ResolveSubresource(srcTex, 0, colorBufferTexture, subresourceIndex, textureDesc.Format);
289 subresourceIndex = 0;
290 }
291 else
292 {
293 srcTex = colorBufferTexture;
294 srcTex->AddRef();
295 }
296
297 D3D11_BOX srcBox;
298 srcBox.left = x;
299 srcBox.right = x + width;
300 srcBox.top = y;
301 srcBox.bottom = y + height;
302 srcBox.front = 0;
303 srcBox.back = 1;
304
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000305 deviceContext->CopySubresourceRegion(mStagingTexture, 0, xoffset, yoffset, zoffset, srcTex, subresourceIndex, &srcBox);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000306
307 srcTex->Release();
308 colorBufferTexture->Release();
309 }
310 }
311 else
312 {
313 // This format requires conversion, so we must copy the texture to staging and manually convert via readPixels
314 D3D11_MAPPED_SUBRESOURCE mappedImage;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000315 HRESULT result = map(D3D11_MAP_WRITE, &mappedImage);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000316
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000317 // determine the offset coordinate into the destination buffer
318 GLsizei rowOffset = gl::ComputePixelSize(mActualFormat) * xoffset;
shannon.woods%transgaming.com@gtempaccount.come5dcce72013-04-13 03:44:33 +0000319 void *dataOffset = static_cast<unsigned char*>(mappedImage.pData) + mappedImage.RowPitch * yoffset + rowOffset + zoffset * mappedImage.DepthPitch;
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000320
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000321 mRenderer->readPixels(source, x, y, width, height, gl::ExtractFormat(mInternalFormat),
shannon.woods@transgaming.coma7c7bc42013-02-28 23:14:02 +0000322 gl::ExtractType(mInternalFormat), mappedImage.RowPitch, false, 4, dataOffset);
shannon.woods@transgaming.comc8cd7f62013-01-25 21:52:40 +0000323
324 unmap();
325 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000326}
327
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000328ID3D11Resource *Image11::getStagingTexture()
shannon.woods@transgaming.comcabb17c2013-02-28 23:20:45 +0000329{
330 createStagingTexture();
331
332 return mStagingTexture;
333}
334
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000335unsigned int Image11::getStagingSubresource()
336{
337 createStagingTexture();
338
339 return mStagingSubresource;
340}
341
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000342void Image11::createStagingTexture()
343{
344 if (mStagingTexture)
345 {
346 return;
347 }
348
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000349 const DXGI_FORMAT dxgiFormat = getDXGIFormat();
shannon.woods@transgaming.comeec5c632013-02-28 23:04:21 +0000350 ASSERT(!d3d11::IsDepthStencilFormat(dxgiFormat)); // We should never get here for depth textures
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000351
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000352 if (mWidth > 0 && mHeight > 0 && mDepth > 0)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000353 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000354 ID3D11Device *device = mRenderer->getDevice();
355
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000356 int lodOffset = 1;
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000357 GLsizei width = mWidth;
358 GLsizei height = mHeight;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000359
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000360 // adjust size if needed for compressed textures
361 gl::MakeValidSize(false, d3d11::IsCompressed(dxgiFormat), &width, &height, &lodOffset);
shannon.woods@transgaming.com7ae9e7f2013-02-28 23:13:27 +0000362
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000363 if (mTarget == GL_TEXTURE_3D)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000364 {
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000365 ID3D11Texture3D *newTexture = NULL;
366
367 D3D11_TEXTURE3D_DESC desc;
368 desc.Width = width;
369 desc.Height = height;
370 desc.Depth = mDepth;
371 desc.MipLevels = lodOffset + 1;
372 desc.Format = dxgiFormat;
373 desc.Usage = D3D11_USAGE_STAGING;
374 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000375 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000376 desc.MiscFlags = 0;
377
378 HRESULT result = device->CreateTexture3D(&desc, NULL, &newTexture);
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);
388 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000389 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 +0000390 {
391 ID3D11Texture2D *newTexture = NULL;
392
393 D3D11_TEXTURE2D_DESC desc;
394 desc.Width = width;
395 desc.Height = height;
396 desc.MipLevels = lodOffset + 1;
397 desc.ArraySize = 1;
398 desc.Format = dxgiFormat;
399 desc.SampleDesc.Count = 1;
400 desc.SampleDesc.Quality = 0;
401 desc.Usage = D3D11_USAGE_STAGING;
402 desc.BindFlags = 0;
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000403 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
shannon.woods%transgaming.com@gtempaccount.com5d009bb2013-04-13 03:43:07 +0000404 desc.MiscFlags = 0;
405
406 HRESULT result = device->CreateTexture2D(&desc, NULL, &newTexture);
407
408 if (FAILED(result))
409 {
410 ASSERT(result == E_OUTOFMEMORY);
411 ERR("Creating image failed.");
412 return gl::error(GL_OUT_OF_MEMORY);
413 }
414
415 mStagingTexture = newTexture;
416 mStagingSubresource = D3D11CalcSubresource(lodOffset, 0, lodOffset + 1);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000417 }
shannon.woods%transgaming.com@gtempaccount.com56074f32013-04-13 03:45:30 +0000418 else
419 {
420 UNREACHABLE();
421 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000422 }
423
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000424 mDirty = false;
425}
426
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000427HRESULT Image11::map(D3D11_MAP mapType, D3D11_MAPPED_SUBRESOURCE *map)
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000428{
429 createStagingTexture();
430
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000431 HRESULT result = E_FAIL;
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000432
433 if (mStagingTexture)
434 {
435 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannonwoods@chromium.org44b27682013-05-30 00:03:06 +0000436 result = deviceContext->Map(mStagingTexture, mStagingSubresource, mapType, 0, map);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000437
shannon.woods@transgaming.comddd6c802013-02-28 23:05:14 +0000438 // this can fail if the device is removed (from TDR)
439 if (d3d11::isDeviceLostError(result))
440 {
441 mRenderer->notifyDeviceLost();
442 }
443 else if (SUCCEEDED(result))
444 {
445 mDirty = true;
446 }
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000447 }
448
449 return result;
450}
451
452void Image11::unmap()
453{
454 if (mStagingTexture)
455 {
456 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
shannon.woods@transgaming.com81ae58a2013-02-28 23:20:51 +0000457 deviceContext->Unmap(mStagingTexture, mStagingSubresource);
daniel@transgaming.coma8aac672012-12-20 21:08:00 +0000458 }
459}
460
461}