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 **************************************************************************/
34 #include "d3d10imports.hpp"
35 #include "d3dstate.hpp"
41 stageResource(ID3D10Device *pDevice,
42 ID3D10Resource *pResource,
43 ID3D10Resource **ppStagingResource,
44 UINT *pWidth, UINT *pHeight, UINT *pDepth) {
45 D3D10_USAGE Usage = D3D10_USAGE_STAGING;
47 UINT CPUAccessFlags = D3D10_CPU_ACCESS_READ;
50 ID3D10Resource *pStagingResource;
51 ID3D10Buffer *pStagingBuffer;
52 ID3D10Texture1D *pStagingTexture1D;
53 ID3D10Texture2D *pStagingTexture2D;
54 ID3D10Texture3D *pStagingTexture3D;
58 D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
59 pResource->GetType(&Type);
61 case D3D10_RESOURCE_DIMENSION_BUFFER:
63 D3D10_BUFFER_DESC Desc;
64 static_cast<ID3D10Buffer *>(pResource)->GetDesc(&Desc);
66 Desc.BindFlags = BindFlags;
67 Desc.CPUAccessFlags = CPUAccessFlags;
68 Desc.MiscFlags = MiscFlags;
70 *pWidth = Desc.ByteWidth;
74 hr = pDevice->CreateBuffer(&Desc, NULL, &pStagingBuffer);
77 case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
79 D3D10_TEXTURE1D_DESC Desc;
80 static_cast<ID3D10Texture1D *>(pResource)->GetDesc(&Desc);
82 Desc.BindFlags = BindFlags;
83 Desc.CPUAccessFlags = CPUAccessFlags;
84 Desc.MiscFlags = MiscFlags;
90 hr = pDevice->CreateTexture1D(&Desc, NULL, &pStagingTexture1D);
93 case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
95 D3D10_TEXTURE2D_DESC Desc;
96 static_cast<ID3D10Texture2D *>(pResource)->GetDesc(&Desc);
98 Desc.BindFlags = BindFlags;
99 Desc.CPUAccessFlags = CPUAccessFlags;
100 Desc.MiscFlags &= D3D10_RESOURCE_MISC_TEXTURECUBE;
102 *pWidth = Desc.Width;
103 *pHeight = Desc.Height;
106 hr = pDevice->CreateTexture2D(&Desc, NULL, &pStagingTexture2D);
109 case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
111 D3D10_TEXTURE3D_DESC Desc;
112 static_cast<ID3D10Texture3D *>(pResource)->GetDesc(&Desc);
114 Desc.BindFlags = BindFlags;
115 Desc.CPUAccessFlags = CPUAccessFlags;
116 Desc.MiscFlags = MiscFlags;
118 *pWidth = Desc.Width;
119 *pHeight = Desc.Height;
120 *pDepth = Desc.Depth;
122 hr = pDevice->CreateTexture3D(&Desc, NULL, &pStagingTexture3D);
132 *ppStagingResource = pStagingResource;
133 pDevice->CopyResource(pStagingResource, pResource);
140 mapResource(ID3D10Resource *pResource,
141 UINT Subresource, D3D10_MAP MapType, UINT MapFlags,
142 D3D10_MAPPED_TEXTURE3D *pMappedSubresource) {
143 D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
144 pResource->GetType(&Type);
146 case D3D10_RESOURCE_DIMENSION_BUFFER:
147 assert(Subresource == 0);
148 return static_cast<ID3D10Buffer *>(pResource)->Map(MapType, MapFlags, &pMappedSubresource->pData);
149 case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
150 return static_cast<ID3D10Texture1D *>(pResource)->Map(Subresource, MapType, MapFlags, &pMappedSubresource->pData);
151 case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
152 return static_cast<ID3D10Texture2D *>(pResource)->Map(Subresource, MapType, MapFlags, reinterpret_cast<D3D10_MAPPED_TEXTURE2D *>(pMappedSubresource));
153 case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
154 return static_cast<ID3D10Texture3D *>(pResource)->Map(Subresource, MapType, MapFlags, pMappedSubresource);
162 unmapResource(ID3D10Resource *pResource, UINT Subresource) {
163 D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
164 pResource->GetType(&Type);
166 case D3D10_RESOURCE_DIMENSION_BUFFER:
167 assert(Subresource == 0);
168 static_cast<ID3D10Buffer *>(pResource)->Unmap();
170 case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
171 static_cast<ID3D10Texture1D *>(pResource)->Unmap(Subresource);
173 case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
174 static_cast<ID3D10Texture2D *>(pResource)->Unmap(Subresource);
176 case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
177 static_cast<ID3D10Texture3D *>(pResource)->Unmap(Subresource);
184 static image::Image *
185 getRenderTargetViewImage(ID3D10Device *pDevice,
186 ID3D10RenderTargetView *pRenderTargetView) {
187 image::Image *image = NULL;
188 D3D10_RENDER_TARGET_VIEW_DESC Desc;
189 ID3D10Resource *pResource = NULL;
190 ID3D10Resource *pStagingResource = NULL;
191 UINT Width, Height, Depth;
194 D3D10_MAPPED_TEXTURE3D MappedSubresource;
196 const unsigned char *src;
199 if (!pRenderTargetView) {
203 pRenderTargetView->GetResource(&pResource);
206 pRenderTargetView->GetDesc(&Desc);
207 if (Desc.Format != DXGI_FORMAT_R8G8B8A8_UNORM &&
208 Desc.Format != DXGI_FORMAT_R32G32B32A32_FLOAT &&
209 Desc.Format != DXGI_FORMAT_B8G8R8A8_UNORM) {
210 std::cerr << "warning: unsupported DXGI format " << Desc.Format << "\n";
214 hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
219 // TODO: Take the slice in consideration
220 switch (Desc.ViewDimension) {
221 case D3D10_RTV_DIMENSION_BUFFER:
224 case D3D10_RTV_DIMENSION_TEXTURE1D:
225 MipSlice = Desc.Texture1D.MipSlice;
227 case D3D10_RTV_DIMENSION_TEXTURE1DARRAY:
228 MipSlice = Desc.Texture1DArray.MipSlice;
230 case D3D10_RTV_DIMENSION_TEXTURE2D:
231 MipSlice = Desc.Texture2D.MipSlice;
234 case D3D10_RTV_DIMENSION_TEXTURE2DARRAY:
235 MipSlice = Desc.Texture2DArray.MipSlice;
237 case D3D10_RTV_DIMENSION_TEXTURE2DMS:
240 case D3D10_RTV_DIMENSION_TEXTURE2DMSARRAY:
243 case D3D10_RTV_DIMENSION_TEXTURE3D:
244 MipSlice = Desc.Texture3D.MipSlice;
246 case D3D10_SRV_DIMENSION_UNKNOWN:
251 Subresource = MipSlice;
253 Width = std::max(Width >> MipSlice, 1U);
254 Height = std::max(Height >> MipSlice, 1U);
255 Depth = std::max(Depth >> MipSlice, 1U);
257 hr = mapResource(pStagingResource, Subresource, D3D10_MAP_READ, 0, &MappedSubresource);
262 image = new image::Image(Width, Height, 4, true);
267 dst = image->start();
268 src = (const unsigned char *)MappedSubresource.pData;
269 for (unsigned y = 0; y < Height; ++y) {
270 if (Desc.Format == DXGI_FORMAT_R8G8B8A8_UNORM) {
271 memcpy(dst, src, Width * 4);
272 } else if (Desc.Format == DXGI_FORMAT_R32G32B32A32_FLOAT) {
273 float scale = 1.0f/255.0f;
274 for (unsigned x = 0; x < Width; ++x) {
275 dst[4*x + 0] = ((float *)src)[4*x + 0] * scale;
276 dst[4*x + 1] = ((float *)src)[4*x + 1] * scale;
277 dst[4*x + 2] = ((float *)src)[4*x + 2] * scale;
278 dst[4*x + 3] = ((float *)src)[4*x + 3] * scale;
280 } else if (Desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM) {
281 for (unsigned x = 0; x < Width; ++x) {
282 dst[4*x + 0] = src[4*x + 2];
283 dst[4*x + 1] = src[4*x + 1];
284 dst[4*x + 2] = src[4*x + 0];
285 dst[4*x + 3] = src[4*x + 3];
290 src += MappedSubresource.RowPitch;
291 dst += image->stride();
295 unmapResource(pStagingResource, Subresource);
297 if (pStagingResource) {
298 pStagingResource->Release();
302 pResource->Release();
309 getRenderTargetImage(ID3D10Device *pDevice) {
310 ID3D10RenderTargetView *pRenderTargetView = NULL;
311 pDevice->OMGetRenderTargets(1, &pRenderTargetView, NULL);
313 image::Image *image = NULL;
314 if (pRenderTargetView) {
315 image = getRenderTargetViewImage(pDevice, pRenderTargetView);
316 pRenderTargetView->Release();
324 dumpFramebuffer(JSONWriter &json, ID3D10Device *pDevice)
326 json.beginMember("framebuffer");
329 ID3D10RenderTargetView *pRenderTargetViews[D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT];
330 pDevice->OMGetRenderTargets(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT, pRenderTargetViews, NULL);
332 for (UINT i = 0; i < D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) {
333 if (!pRenderTargetViews[i]) {
338 image = getRenderTargetViewImage(pDevice, pRenderTargetViews[i]);
341 _snprintf(label, sizeof label, "RENDER_TARGET_%u", i);
342 json.beginMember(label);
343 json.writeImage(image, "UNKNOWN");
344 json.endMember(); // RENDER_TARGET_*
347 pRenderTargetViews[i]->Release();
351 json.endMember(); // framebuffer
355 } /* namespace d3dstate */