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 "d3d10imports.hpp"
39 stageResource(ID3D10Device *pDevice,
40 ID3D10Resource *pResource,
41 ID3D10Resource **ppStagingResource,
42 UINT *pWidth, UINT *pHeight, UINT *pDepth) {
43 D3D10_USAGE Usage = D3D10_USAGE_STAGING;
45 UINT CPUAccessFlags = D3D10_CPU_ACCESS_READ;
48 ID3D10Resource *pStagingResource;
49 ID3D10Buffer *pStagingBuffer;
50 ID3D10Texture1D *pStagingTexture1D;
51 ID3D10Texture2D *pStagingTexture2D;
52 ID3D10Texture3D *pStagingTexture3D;
56 D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
57 pResource->GetType(&Type);
59 case D3D10_RESOURCE_DIMENSION_BUFFER:
61 D3D10_BUFFER_DESC Desc;
62 static_cast<ID3D10Buffer *>(pResource)->GetDesc(&Desc);
64 Desc.BindFlags = BindFlags;
65 Desc.CPUAccessFlags = CPUAccessFlags;
66 Desc.MiscFlags = MiscFlags;
68 *pWidth = Desc.ByteWidth;
72 hr = pDevice->CreateBuffer(&Desc, NULL, &pStagingBuffer);
75 case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
77 D3D10_TEXTURE1D_DESC Desc;
78 static_cast<ID3D10Texture1D *>(pResource)->GetDesc(&Desc);
80 Desc.BindFlags = BindFlags;
81 Desc.CPUAccessFlags = CPUAccessFlags;
82 Desc.MiscFlags = MiscFlags;
88 hr = pDevice->CreateTexture1D(&Desc, NULL, &pStagingTexture1D);
91 case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
93 D3D10_TEXTURE2D_DESC Desc;
94 static_cast<ID3D10Texture2D *>(pResource)->GetDesc(&Desc);
96 Desc.BindFlags = BindFlags;
97 Desc.CPUAccessFlags = CPUAccessFlags;
98 Desc.MiscFlags &= ~D3D10_RESOURCE_MISC_TEXTURECUBE;
100 *pWidth = Desc.Width;
101 *pHeight = Desc.Height;
104 hr = pDevice->CreateTexture2D(&Desc, NULL, &pStagingTexture2D);
107 case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
109 D3D10_TEXTURE3D_DESC Desc;
110 static_cast<ID3D10Texture3D *>(pResource)->GetDesc(&Desc);
112 Desc.BindFlags = BindFlags;
113 Desc.CPUAccessFlags = CPUAccessFlags;
114 Desc.MiscFlags = MiscFlags;
116 *pWidth = Desc.Width;
117 *pHeight = Desc.Height;
118 *pDepth = Desc.Depth;
120 hr = pDevice->CreateTexture3D(&Desc, NULL, &pStagingTexture3D);
130 *ppStagingResource = pStagingResource;
131 pDevice->CopyResource(pStagingResource, pResource);
138 mapResource(ID3D10Resource *pResource,
139 UINT Subresource, D3D10_MAP MapType, UINT MapFlags,
140 D3D10_MAPPED_TEXTURE3D *pMappedSubresource) {
141 D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
142 pResource->GetType(&Type);
144 case D3D10_RESOURCE_DIMENSION_BUFFER:
145 assert(Subresource == 0);
146 return static_cast<ID3D10Buffer *>(pResource)->Map(MapType, MapFlags, &pMappedSubresource->pData);
147 case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
148 return static_cast<ID3D10Texture1D *>(pResource)->Map(Subresource, MapType, MapFlags, &pMappedSubresource->pData);
149 case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
150 return static_cast<ID3D10Texture2D *>(pResource)->Map(Subresource, MapType, MapFlags, reinterpret_cast<D3D10_MAPPED_TEXTURE2D *>(pMappedSubresource));
151 case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
152 return static_cast<ID3D10Texture3D *>(pResource)->Map(Subresource, MapType, MapFlags, pMappedSubresource);
160 unmapResource(ID3D10Resource *pResource, UINT Subresource) {
161 D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
162 pResource->GetType(&Type);
164 case D3D10_RESOURCE_DIMENSION_BUFFER:
165 assert(Subresource == 0);
166 static_cast<ID3D10Buffer *>(pResource)->Unmap();
168 case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
169 static_cast<ID3D10Texture1D *>(pResource)->Unmap(Subresource);
171 case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
172 static_cast<ID3D10Texture2D *>(pResource)->Unmap(Subresource);
174 case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
175 static_cast<ID3D10Texture3D *>(pResource)->Unmap(Subresource);
183 getRenderTargetImage(ID3D10Device *pDevice) {
184 image::Image *image = NULL;
185 ID3D10RenderTargetView *pRenderTargetView = NULL;
186 D3D10_RENDER_TARGET_VIEW_DESC Desc;
187 ID3D10Resource *pResource = NULL;
188 ID3D10Resource *pStagingResource = NULL;
189 UINT Width, Height, Depth;
192 D3D10_MAPPED_TEXTURE3D MappedSubresource;
194 const unsigned char *src;
197 pDevice->OMGetRenderTargets(1, &pRenderTargetView, NULL);
198 if (!pRenderTargetView) {
199 goto no_rendertarget;
202 pRenderTargetView->GetResource(&pResource);
205 pRenderTargetView->GetDesc(&Desc);
206 if (Desc.Format != DXGI_FORMAT_R8G8B8A8_UNORM &&
207 Desc.Format != DXGI_FORMAT_R32G32B32A32_FLOAT &&
208 Desc.Format != DXGI_FORMAT_B8G8R8A8_UNORM) {
209 std::cerr << "warning: unsupported DXGI format " << Desc.Format << "\n";
213 hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
218 // TODO: Take the slice in consideration
219 switch (Desc.ViewDimension) {
220 case D3D10_RTV_DIMENSION_BUFFER:
223 case D3D10_RTV_DIMENSION_TEXTURE1D:
224 MipSlice = Desc.Texture1D.MipSlice;
226 case D3D10_RTV_DIMENSION_TEXTURE1DARRAY:
227 MipSlice = Desc.Texture1DArray.MipSlice;
229 case D3D10_RTV_DIMENSION_TEXTURE2D:
230 MipSlice = Desc.Texture2D.MipSlice;
233 case D3D10_RTV_DIMENSION_TEXTURE2DARRAY:
234 MipSlice = Desc.Texture2DArray.MipSlice;
236 case D3D10_RTV_DIMENSION_TEXTURE2DMS:
239 case D3D10_RTV_DIMENSION_TEXTURE2DMSARRAY:
242 case D3D10_RTV_DIMENSION_TEXTURE3D:
243 MipSlice = Desc.Texture3D.MipSlice;
245 case D3D10_SRV_DIMENSION_UNKNOWN:
250 Subresource = MipSlice;
252 Width = std::max(Width >> MipSlice, 1U);
253 Height = std::max(Height >> MipSlice, 1U);
254 Depth = std::max(Depth >> MipSlice, 1U);
256 hr = mapResource(pStagingResource, Subresource, D3D10_MAP_READ, 0, &MappedSubresource);
261 image = new image::Image(Width, Height, 4, true);
266 dst = image->start();
267 src = (const unsigned char *)MappedSubresource.pData;
268 for (unsigned y = 0; y < Height; ++y) {
269 if (Desc.Format == DXGI_FORMAT_R8G8B8A8_UNORM) {
270 memcpy(dst, src, Width * 4);
271 } else if (Desc.Format == DXGI_FORMAT_R32G32B32A32_FLOAT) {
272 float scale = 1.0f/255.0f;
273 for (unsigned x = 0; x < Width; ++x) {
274 dst[4*x + 0] = ((float *)src)[4*x + 0] * scale;
275 dst[4*x + 1] = ((float *)src)[4*x + 1] * scale;
276 dst[4*x + 2] = ((float *)src)[4*x + 2] * scale;
277 dst[4*x + 3] = ((float *)src)[4*x + 3] * scale;
279 } else if (Desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM) {
280 for (unsigned x = 0; x < Width; ++x) {
281 dst[4*x + 0] = src[4*x + 2];
282 dst[4*x + 1] = src[4*x + 1];
283 dst[4*x + 2] = src[4*x + 0];
284 dst[4*x + 3] = src[4*x + 3];
289 src += MappedSubresource.RowPitch;
290 dst += image->stride();
294 unmapResource(pStagingResource, Subresource);
296 if (pStagingResource) {
297 pStagingResource->Release();
301 pResource->Release();
303 if (pRenderTargetView) {
304 pRenderTargetView->Release();
311 } /* namespace d3dstate */