]> git.cworth.org Git - apitrace/blob - retrace/d3d10state_images.cpp
fa27fee2ff7ff1715bdd3a5a3ad24213e52baf8b
[apitrace] / retrace / d3d10state_images.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <assert.h>
28
29 #include <iostream>
30 #include <algorithm>
31
32 #include "image.hpp"
33 #include "json.hpp"
34 #include "d3d10imports.hpp"
35 #include "d3dstate.hpp"
36
37
38 namespace d3dstate {
39
40 static HRESULT
41 stageResource(ID3D10Device *pDevice,
42               ID3D10Resource *pResource,
43               ID3D10Resource **ppStagingResource,
44               UINT *pWidth, UINT *pHeight, UINT *pDepth) {
45     D3D10_USAGE Usage = D3D10_USAGE_STAGING;
46     UINT BindFlags = 0;
47     UINT CPUAccessFlags = D3D10_CPU_ACCESS_READ;
48     UINT MiscFlags = 0;
49     union {
50          ID3D10Resource *pStagingResource;
51          ID3D10Buffer *pStagingBuffer;
52          ID3D10Texture1D *pStagingTexture1D;
53          ID3D10Texture2D *pStagingTexture2D;
54          ID3D10Texture3D *pStagingTexture3D;
55     };
56     HRESULT hr;
57
58     D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
59     pResource->GetType(&Type);
60     switch (Type) {
61     case D3D10_RESOURCE_DIMENSION_BUFFER:
62         {
63             D3D10_BUFFER_DESC Desc;
64             static_cast<ID3D10Buffer *>(pResource)->GetDesc(&Desc);
65             Desc.Usage = Usage;
66             Desc.BindFlags = BindFlags;
67             Desc.CPUAccessFlags = CPUAccessFlags;
68             Desc.MiscFlags = MiscFlags;
69
70             *pWidth = Desc.ByteWidth;
71             *pHeight = 1;
72             *pDepth = 1;
73
74             hr = pDevice->CreateBuffer(&Desc, NULL, &pStagingBuffer);
75         }
76         break;
77     case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
78         {
79             D3D10_TEXTURE1D_DESC Desc;
80             static_cast<ID3D10Texture1D *>(pResource)->GetDesc(&Desc);
81             Desc.Usage = Usage;
82             Desc.BindFlags = BindFlags;
83             Desc.CPUAccessFlags = CPUAccessFlags;
84             Desc.MiscFlags = MiscFlags;
85
86             *pWidth = Desc.Width;
87             *pHeight = 1;
88             *pDepth = 1;
89
90             hr = pDevice->CreateTexture1D(&Desc, NULL, &pStagingTexture1D);
91         }
92         break;
93     case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
94         {
95             D3D10_TEXTURE2D_DESC Desc;
96             static_cast<ID3D10Texture2D *>(pResource)->GetDesc(&Desc);
97             Desc.Usage = Usage;
98             Desc.BindFlags = BindFlags;
99             Desc.CPUAccessFlags = CPUAccessFlags;
100             Desc.MiscFlags &= D3D10_RESOURCE_MISC_TEXTURECUBE;
101
102             *pWidth = Desc.Width;
103             *pHeight = Desc.Height;
104             *pDepth = 1;
105
106             hr = pDevice->CreateTexture2D(&Desc, NULL, &pStagingTexture2D);
107         }
108         break;
109     case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
110         {
111             D3D10_TEXTURE3D_DESC Desc;
112             static_cast<ID3D10Texture3D *>(pResource)->GetDesc(&Desc);
113             Desc.Usage = Usage;
114             Desc.BindFlags = BindFlags;
115             Desc.CPUAccessFlags = CPUAccessFlags;
116             Desc.MiscFlags = MiscFlags;
117
118             *pWidth = Desc.Width;
119             *pHeight = Desc.Height;
120             *pDepth = Desc.Depth;
121
122             hr = pDevice->CreateTexture3D(&Desc, NULL, &pStagingTexture3D);
123         }
124         break;
125     default:
126         assert(0);
127         hr = E_NOTIMPL;
128         break;
129     }
130
131     if (SUCCEEDED(hr)) {
132         *ppStagingResource = pStagingResource;
133         pDevice->CopyResource(pStagingResource, pResource);
134     }
135     
136     return hr;
137 }
138
139 static HRESULT
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);
145     switch (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);
155     default:
156         assert(0);
157         return E_NOTIMPL;
158     }
159 }
160
161 static void
162 unmapResource(ID3D10Resource *pResource, UINT Subresource) {
163     D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
164     pResource->GetType(&Type);
165     switch (Type) {
166     case D3D10_RESOURCE_DIMENSION_BUFFER:
167         assert(Subresource == 0);
168         static_cast<ID3D10Buffer *>(pResource)->Unmap();
169         break;
170     case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
171         static_cast<ID3D10Texture1D *>(pResource)->Unmap(Subresource);
172         break;
173     case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
174         static_cast<ID3D10Texture2D *>(pResource)->Unmap(Subresource);
175         break;
176     case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
177         static_cast<ID3D10Texture3D *>(pResource)->Unmap(Subresource);
178         break;
179     default:
180         assert(0);
181     }
182 }
183
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;
192     UINT MipSlice;
193     UINT Subresource;
194     D3D10_MAPPED_TEXTURE3D MappedSubresource;
195     HRESULT hr;
196     const unsigned char *src;
197     unsigned char *dst;
198
199     if (!pRenderTargetView) {
200         return NULL;
201     }
202
203     pRenderTargetView->GetResource(&pResource);
204     assert(pResource);
205
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";
211         goto no_staging;
212     }
213
214     hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
215     if (FAILED(hr)) {
216         goto no_staging;
217     }
218
219     // TODO: Take the slice in consideration
220     switch (Desc.ViewDimension) {
221     case D3D10_RTV_DIMENSION_BUFFER:
222         MipSlice = 0;
223         break;
224     case D3D10_RTV_DIMENSION_TEXTURE1D:
225         MipSlice = Desc.Texture1D.MipSlice;
226         break;
227     case D3D10_RTV_DIMENSION_TEXTURE1DARRAY:
228         MipSlice = Desc.Texture1DArray.MipSlice;
229         break;
230     case D3D10_RTV_DIMENSION_TEXTURE2D:
231         MipSlice = Desc.Texture2D.MipSlice;
232         MipSlice = 0;
233         break;
234     case D3D10_RTV_DIMENSION_TEXTURE2DARRAY:
235         MipSlice = Desc.Texture2DArray.MipSlice;
236         break;
237     case D3D10_RTV_DIMENSION_TEXTURE2DMS:
238         MipSlice = 0;
239         break;
240     case D3D10_RTV_DIMENSION_TEXTURE2DMSARRAY:
241         MipSlice = 0;
242         break;
243     case D3D10_RTV_DIMENSION_TEXTURE3D:
244         MipSlice = Desc.Texture3D.MipSlice;
245         break;
246     case D3D10_SRV_DIMENSION_UNKNOWN:
247     default:
248         assert(0);
249         goto no_map;
250     }
251     Subresource = MipSlice;
252
253     Width  = std::max(Width  >> MipSlice, 1U);
254     Height = std::max(Height >> MipSlice, 1U);
255     Depth  = std::max(Depth  >> MipSlice, 1U);
256
257     hr = mapResource(pStagingResource, Subresource, D3D10_MAP_READ, 0, &MappedSubresource);
258     if (FAILED(hr)) {
259         goto no_map;
260     }
261
262     image = new image::Image(Width, Height, 4, true);
263     if (!image) {
264         goto no_image;
265     }
266
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;
279             }
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];
286             }
287         } else {
288             assert(0);
289         }
290         src += MappedSubresource.RowPitch;
291         dst += image->stride();
292     }
293
294 no_image:
295     unmapResource(pStagingResource, Subresource);
296 no_map:
297     if (pStagingResource) {
298         pStagingResource->Release();
299     }
300 no_staging:
301     if (pResource) {
302         pResource->Release();
303     }
304     return image;
305 }
306
307
308 image::Image *
309 getRenderTargetImage(ID3D10Device *pDevice) {
310     ID3D10RenderTargetView *pRenderTargetView = NULL;
311     pDevice->OMGetRenderTargets(1, &pRenderTargetView, NULL);
312
313     image::Image *image = NULL;
314     if (pRenderTargetView) {
315         image = getRenderTargetViewImage(pDevice, pRenderTargetView);
316         pRenderTargetView->Release();
317     }
318
319     return image;
320 }
321
322
323 void
324 dumpFramebuffer(JSONWriter &json, ID3D10Device *pDevice)
325 {
326     json.beginMember("framebuffer");
327     json.beginObject();
328
329     ID3D10RenderTargetView *pRenderTargetViews[D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT];
330     pDevice->OMGetRenderTargets(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT, pRenderTargetViews, NULL);
331
332     for (UINT i = 0; i < D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) {
333         if (!pRenderTargetViews[i]) {
334             continue;
335         }
336
337         image::Image *image;
338         image = getRenderTargetViewImage(pDevice, pRenderTargetViews[i]);
339         if (image) {
340             char label[64];
341             _snprintf(label, sizeof label, "RENDER_TARGET_%u", i);
342             json.beginMember(label);
343             json.writeImage(image, "UNKNOWN");
344             json.endMember(); // RENDER_TARGET_*
345         }
346
347         pRenderTargetViews[i]->Release();
348     }
349
350     json.endObject();
351     json.endMember(); // framebuffer
352 }
353
354
355 } /* namespace d3dstate */