1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
33 #include "d3d11imports.hpp"
39 stageResource(ID3D11DeviceContext *pDeviceContext,
40 ID3D11Resource *pResource,
41 ID3D11Resource **ppStagingResource,
42 UINT *pWidth, UINT *pHeight, UINT *pDepth) {
43 D3D11_USAGE Usage = D3D11_USAGE_STAGING;
45 UINT CPUAccessFlags = D3D11_CPU_ACCESS_READ;
48 ID3D11Resource *pStagingResource;
49 ID3D11Buffer *pStagingBuffer;
50 ID3D11Texture1D *pStagingTexture1D;
51 ID3D11Texture2D *pStagingTexture2D;
52 ID3D11Texture3D *pStagingTexture3D;
56 ID3D11Device *pDevice = NULL;
57 pDeviceContext->GetDevice(&pDevice);
59 D3D11_RESOURCE_DIMENSION Type = D3D11_RESOURCE_DIMENSION_UNKNOWN;
60 pResource->GetType(&Type);
62 case D3D11_RESOURCE_DIMENSION_BUFFER:
64 D3D11_BUFFER_DESC Desc;
65 static_cast<ID3D11Buffer *>(pResource)->GetDesc(&Desc);
67 Desc.BindFlags = BindFlags;
68 Desc.CPUAccessFlags = CPUAccessFlags;
69 Desc.MiscFlags = MiscFlags;
71 *pWidth = Desc.ByteWidth;
75 hr = pDevice->CreateBuffer(&Desc, NULL, &pStagingBuffer);
78 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
80 D3D11_TEXTURE1D_DESC Desc;
81 static_cast<ID3D11Texture1D *>(pResource)->GetDesc(&Desc);
83 Desc.BindFlags = BindFlags;
84 Desc.CPUAccessFlags = CPUAccessFlags;
85 Desc.MiscFlags = MiscFlags;
91 hr = pDevice->CreateTexture1D(&Desc, NULL, &pStagingTexture1D);
94 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
96 D3D11_TEXTURE2D_DESC Desc;
97 static_cast<ID3D11Texture2D *>(pResource)->GetDesc(&Desc);
99 Desc.BindFlags = BindFlags;
100 Desc.CPUAccessFlags = CPUAccessFlags;
101 Desc.MiscFlags &= ~D3D11_RESOURCE_MISC_TEXTURECUBE;
103 *pWidth = Desc.Width;
104 *pHeight = Desc.Height;
107 hr = pDevice->CreateTexture2D(&Desc, NULL, &pStagingTexture2D);
110 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
112 D3D11_TEXTURE3D_DESC Desc;
113 static_cast<ID3D11Texture3D *>(pResource)->GetDesc(&Desc);
115 Desc.BindFlags = BindFlags;
116 Desc.CPUAccessFlags = CPUAccessFlags;
117 Desc.MiscFlags = MiscFlags;
119 *pWidth = Desc.Width;
120 *pHeight = Desc.Height;
121 *pDepth = Desc.Depth;
123 hr = pDevice->CreateTexture3D(&Desc, NULL, &pStagingTexture3D);
133 *ppStagingResource = pStagingResource;
134 pDeviceContext->CopyResource(pStagingResource, pResource);
143 getRenderTargetImage(ID3D11DeviceContext *pDevice) {
144 image::Image *image = NULL;
145 ID3D11RenderTargetView *pRenderTargetView = NULL;
146 D3D11_RENDER_TARGET_VIEW_DESC Desc;
147 ID3D11Resource *pResource = NULL;
148 ID3D11Resource *pStagingResource = NULL;
149 UINT Width, Height, Depth;
152 D3D11_MAPPED_SUBRESOURCE MappedSubresource;
154 const unsigned char *src;
157 pDevice->OMGetRenderTargets(1, &pRenderTargetView, NULL);
158 if (!pRenderTargetView) {
159 goto no_rendertarget;
162 pRenderTargetView->GetResource(&pResource);
165 pRenderTargetView->GetDesc(&Desc);
166 if (Desc.Format != DXGI_FORMAT_R8G8B8A8_UNORM &&
167 Desc.Format != DXGI_FORMAT_R32G32B32A32_FLOAT) {
168 std::cerr << "warning: unsupported DXGI format " << Desc.Format << "\n";
172 hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
177 // TODO: Take the slice in consideration
178 switch (Desc.ViewDimension) {
179 case D3D11_RTV_DIMENSION_BUFFER:
182 case D3D11_RTV_DIMENSION_TEXTURE1D:
183 MipSlice = Desc.Texture1D.MipSlice;
185 case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
186 MipSlice = Desc.Texture1DArray.MipSlice;
188 case D3D11_RTV_DIMENSION_TEXTURE2D:
189 MipSlice = Desc.Texture2D.MipSlice;
192 case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
193 MipSlice = Desc.Texture2DArray.MipSlice;
195 case D3D11_RTV_DIMENSION_TEXTURE2DMS:
198 case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
201 case D3D11_RTV_DIMENSION_TEXTURE3D:
202 MipSlice = Desc.Texture3D.MipSlice;
204 case D3D11_SRV_DIMENSION_UNKNOWN:
209 Subresource = MipSlice;
211 Width = std::max(Width >> MipSlice, 1U);
212 Height = std::max(Height >> MipSlice, 1U);
213 Depth = std::max(Depth >> MipSlice, 1U);
215 hr = pDevice->Map(pStagingResource, Subresource, D3D11_MAP_READ, 0, &MappedSubresource);
220 image = new image::Image(Width, Height, 4, true);
225 dst = image->start();
226 src = (const unsigned char *)MappedSubresource.pData;
227 for (unsigned y = 0; y < Height; ++y) {
228 if (Desc.Format == DXGI_FORMAT_R8G8B8A8_UNORM) {
229 memcpy(dst, src, Width * 4);
230 } else if (Desc.Format == DXGI_FORMAT_R32G32B32A32_FLOAT) {
231 float scale = 1.0f/255.0f;
232 for (unsigned x = 0; x < Width; ++x) {
233 dst[4*x + 0] = ((float *)src)[4*x + 0] * scale;
234 dst[4*x + 1] = ((float *)src)[4*x + 1] * scale;
235 dst[4*x + 2] = ((float *)src)[4*x + 2] * scale;
236 dst[4*x + 3] = ((float *)src)[4*x + 3] * scale;
241 src += MappedSubresource.RowPitch;
242 dst += image->stride();
246 pDevice->Unmap(pStagingResource, Subresource);
248 if (pStagingResource) {
249 pStagingResource->Release();
253 pResource->Release();
255 if (pRenderTargetView) {
256 pRenderTargetView->Release();
263 } /* namespace d3dstate */