]> git.cworth.org Git - apitrace/blob - retrace/d3d10state_images.cpp
1fc44926c467304c45234c2df327b7f7751f0156
[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 "os.hpp"
33 #include "json.hpp"
34 #include "d3d10imports.hpp"
35 #include "d3dstate.hpp"
36 #include "dxgistate.hpp"
37
38
39 namespace d3dstate {
40
41 static HRESULT
42 stageResource(ID3D10Device *pDevice,
43               ID3D10Resource *pResource,
44               ID3D10Resource **ppStagingResource,
45               UINT *pWidth, UINT *pHeight, UINT *pDepth) {
46     D3D10_USAGE Usage = D3D10_USAGE_STAGING;
47     UINT BindFlags = 0;
48     UINT CPUAccessFlags = D3D10_CPU_ACCESS_READ;
49     UINT MiscFlags = 0;
50     union {
51          ID3D10Resource *pStagingResource;
52          ID3D10Buffer *pStagingBuffer;
53          ID3D10Texture1D *pStagingTexture1D;
54          ID3D10Texture2D *pStagingTexture2D;
55          ID3D10Texture3D *pStagingTexture3D;
56     };
57     HRESULT hr;
58
59     D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
60     pResource->GetType(&Type);
61     switch (Type) {
62     case D3D10_RESOURCE_DIMENSION_BUFFER:
63         {
64             D3D10_BUFFER_DESC Desc;
65             static_cast<ID3D10Buffer *>(pResource)->GetDesc(&Desc);
66             Desc.Usage = Usage;
67             Desc.BindFlags = BindFlags;
68             Desc.CPUAccessFlags = CPUAccessFlags;
69             Desc.MiscFlags = MiscFlags;
70
71             *pWidth = Desc.ByteWidth;
72             *pHeight = 1;
73             *pDepth = 1;
74
75             hr = pDevice->CreateBuffer(&Desc, NULL, &pStagingBuffer);
76         }
77         break;
78     case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
79         {
80             D3D10_TEXTURE1D_DESC Desc;
81             static_cast<ID3D10Texture1D *>(pResource)->GetDesc(&Desc);
82             Desc.Usage = Usage;
83             Desc.BindFlags = BindFlags;
84             Desc.CPUAccessFlags = CPUAccessFlags;
85             Desc.MiscFlags = MiscFlags;
86
87             *pWidth = Desc.Width;
88             *pHeight = 1;
89             *pDepth = 1;
90
91             hr = pDevice->CreateTexture1D(&Desc, NULL, &pStagingTexture1D);
92         }
93         break;
94     case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
95         {
96             D3D10_TEXTURE2D_DESC Desc;
97             static_cast<ID3D10Texture2D *>(pResource)->GetDesc(&Desc);
98             Desc.Usage = Usage;
99             Desc.BindFlags = BindFlags;
100             Desc.CPUAccessFlags = CPUAccessFlags;
101             Desc.MiscFlags &= D3D10_RESOURCE_MISC_TEXTURECUBE;
102
103             *pWidth = Desc.Width;
104             *pHeight = Desc.Height;
105             *pDepth = 1;
106
107             hr = pDevice->CreateTexture2D(&Desc, NULL, &pStagingTexture2D);
108         }
109         break;
110     case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
111         {
112             D3D10_TEXTURE3D_DESC Desc;
113             static_cast<ID3D10Texture3D *>(pResource)->GetDesc(&Desc);
114             Desc.Usage = Usage;
115             Desc.BindFlags = BindFlags;
116             Desc.CPUAccessFlags = CPUAccessFlags;
117             Desc.MiscFlags = MiscFlags;
118
119             *pWidth = Desc.Width;
120             *pHeight = Desc.Height;
121             *pDepth = Desc.Depth;
122
123             hr = pDevice->CreateTexture3D(&Desc, NULL, &pStagingTexture3D);
124         }
125         break;
126     default:
127         assert(0);
128         hr = E_NOTIMPL;
129         break;
130     }
131
132     if (SUCCEEDED(hr)) {
133         *ppStagingResource = pStagingResource;
134         pDevice->CopyResource(pStagingResource, pResource);
135     }
136     
137     return hr;
138 }
139
140 static HRESULT
141 mapResource(ID3D10Resource *pResource,
142             UINT Subresource, D3D10_MAP MapType, UINT MapFlags,
143             D3D10_MAPPED_TEXTURE3D *pMappedSubresource) {
144     D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
145     pResource->GetType(&Type);
146     switch (Type) {
147     case D3D10_RESOURCE_DIMENSION_BUFFER:
148         assert(Subresource == 0);
149         return static_cast<ID3D10Buffer *>(pResource)->Map(MapType, MapFlags, &pMappedSubresource->pData);
150     case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
151         return static_cast<ID3D10Texture1D *>(pResource)->Map(Subresource, MapType, MapFlags, &pMappedSubresource->pData);
152     case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
153         return static_cast<ID3D10Texture2D *>(pResource)->Map(Subresource, MapType, MapFlags, reinterpret_cast<D3D10_MAPPED_TEXTURE2D *>(pMappedSubresource));
154     case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
155         return static_cast<ID3D10Texture3D *>(pResource)->Map(Subresource, MapType, MapFlags, pMappedSubresource);
156     default:
157         assert(0);
158         return E_NOTIMPL;
159     }
160 }
161
162 static void
163 unmapResource(ID3D10Resource *pResource, UINT Subresource) {
164     D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
165     pResource->GetType(&Type);
166     switch (Type) {
167     case D3D10_RESOURCE_DIMENSION_BUFFER:
168         assert(Subresource == 0);
169         static_cast<ID3D10Buffer *>(pResource)->Unmap();
170         break;
171     case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
172         static_cast<ID3D10Texture1D *>(pResource)->Unmap(Subresource);
173         break;
174     case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
175         static_cast<ID3D10Texture2D *>(pResource)->Unmap(Subresource);
176         break;
177     case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
178         static_cast<ID3D10Texture3D *>(pResource)->Unmap(Subresource);
179         break;
180     default:
181         assert(0);
182     }
183 }
184
185 static image::Image *
186 getRenderTargetViewImage(ID3D10Device *pDevice,
187                          ID3D10RenderTargetView *pRenderTargetView) {
188     image::Image *image = NULL;
189     D3D10_RENDER_TARGET_VIEW_DESC Desc;
190     ID3D10Resource *pResource = NULL;
191     ID3D10Resource *pStagingResource = NULL;
192     UINT Width, Height, Depth;
193     UINT MipSlice;
194     UINT Subresource;
195     D3D10_MAPPED_TEXTURE3D MappedSubresource;
196     HRESULT hr;
197
198     if (!pRenderTargetView) {
199         return NULL;
200     }
201
202     pRenderTargetView->GetResource(&pResource);
203     assert(pResource);
204
205     pRenderTargetView->GetDesc(&Desc);
206
207     hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
208     if (FAILED(hr)) {
209         goto no_staging;
210     }
211
212     // TODO: Take the slice in consideration
213     switch (Desc.ViewDimension) {
214     case D3D10_RTV_DIMENSION_BUFFER:
215         MipSlice = 0;
216         break;
217     case D3D10_RTV_DIMENSION_TEXTURE1D:
218         MipSlice = Desc.Texture1D.MipSlice;
219         break;
220     case D3D10_RTV_DIMENSION_TEXTURE1DARRAY:
221         MipSlice = Desc.Texture1DArray.MipSlice;
222         break;
223     case D3D10_RTV_DIMENSION_TEXTURE2D:
224         MipSlice = Desc.Texture2D.MipSlice;
225         MipSlice = 0;
226         break;
227     case D3D10_RTV_DIMENSION_TEXTURE2DARRAY:
228         MipSlice = Desc.Texture2DArray.MipSlice;
229         break;
230     case D3D10_RTV_DIMENSION_TEXTURE2DMS:
231         MipSlice = 0;
232         break;
233     case D3D10_RTV_DIMENSION_TEXTURE2DMSARRAY:
234         MipSlice = 0;
235         break;
236     case D3D10_RTV_DIMENSION_TEXTURE3D:
237         MipSlice = Desc.Texture3D.MipSlice;
238         break;
239     case D3D10_SRV_DIMENSION_UNKNOWN:
240     default:
241         assert(0);
242         goto no_map;
243     }
244     Subresource = MipSlice;
245
246     Width  = std::max(Width  >> MipSlice, 1U);
247     Height = std::max(Height >> MipSlice, 1U);
248     Depth  = std::max(Depth  >> MipSlice, 1U);
249
250     hr = mapResource(pStagingResource, Subresource, D3D10_MAP_READ, 0, &MappedSubresource);
251     if (FAILED(hr)) {
252         goto no_map;
253     }
254
255     image = ConvertImage(Desc.Format,
256                          MappedSubresource.pData,
257                          MappedSubresource.RowPitch,
258                          Width, Height);
259
260     unmapResource(pStagingResource, Subresource);
261 no_map:
262     if (pStagingResource) {
263         pStagingResource->Release();
264     }
265 no_staging:
266     if (pResource) {
267         pResource->Release();
268     }
269     return image;
270 }
271
272 static image::Image *
273 getDepthStencilViewImage(ID3D10Device *pDevice,
274                          ID3D10DepthStencilView *pDepthStencilView) {
275     image::Image *image = NULL;
276     D3D10_DEPTH_STENCIL_VIEW_DESC Desc;
277     ID3D10Resource *pResource = NULL;
278     ID3D10Resource *pStagingResource = NULL;
279     UINT Width, Height, Depth;
280     UINT MipSlice;
281     UINT Subresource;
282     D3D10_MAPPED_TEXTURE3D MappedSubresource;
283     HRESULT hr;
284
285     if (!pDepthStencilView) {
286         return NULL;
287     }
288
289     pDepthStencilView->GetResource(&pResource);
290     assert(pResource);
291
292     pDepthStencilView->GetDesc(&Desc);
293
294     hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
295     if (FAILED(hr)) {
296         goto no_staging;
297     }
298
299     // TODO: Take the slice in consideration
300     switch (Desc.ViewDimension) {
301     case D3D10_DSV_DIMENSION_TEXTURE1D:
302         MipSlice = Desc.Texture1D.MipSlice;
303         break;
304     case D3D10_DSV_DIMENSION_TEXTURE1DARRAY:
305         MipSlice = Desc.Texture1DArray.MipSlice;
306         break;
307     case D3D10_DSV_DIMENSION_TEXTURE2D:
308         MipSlice = Desc.Texture2D.MipSlice;
309         MipSlice = 0;
310         break;
311     case D3D10_DSV_DIMENSION_TEXTURE2DARRAY:
312         MipSlice = Desc.Texture2DArray.MipSlice;
313         break;
314     case D3D10_DSV_DIMENSION_TEXTURE2DMS:
315         MipSlice = 0;
316         break;
317     case D3D10_DSV_DIMENSION_TEXTURE2DMSARRAY:
318         MipSlice = 0;
319         break;
320     case D3D10_SRV_DIMENSION_UNKNOWN:
321     default:
322         assert(0);
323         goto no_map;
324     }
325     Subresource = MipSlice;
326
327     Width  = std::max(Width  >> MipSlice, 1U);
328     Height = std::max(Height >> MipSlice, 1U);
329     Depth  = std::max(Depth  >> MipSlice, 1U);
330
331     hr = mapResource(pStagingResource, Subresource, D3D10_MAP_READ, 0, &MappedSubresource);
332     if (FAILED(hr)) {
333         goto no_map;
334     }
335
336     image = ConvertImage(Desc.Format,
337                          MappedSubresource.pData,
338                          MappedSubresource.RowPitch,
339                          Width, Height);
340
341     unmapResource(pStagingResource, Subresource);
342 no_map:
343     if (pStagingResource) {
344         pStagingResource->Release();
345     }
346 no_staging:
347     if (pResource) {
348         pResource->Release();
349     }
350     return image;
351 }
352
353
354 image::Image *
355 getRenderTargetImage(ID3D10Device *pDevice) {
356     ID3D10RenderTargetView *pRenderTargetView = NULL;
357     pDevice->OMGetRenderTargets(1, &pRenderTargetView, NULL);
358
359     image::Image *image = NULL;
360     if (pRenderTargetView) {
361         image = getRenderTargetViewImage(pDevice, pRenderTargetView);
362         pRenderTargetView->Release();
363     }
364
365     return image;
366 }
367
368
369 void
370 dumpFramebuffer(JSONWriter &json, ID3D10Device *pDevice)
371 {
372     json.beginMember("framebuffer");
373     json.beginObject();
374
375     ID3D10RenderTargetView *pRenderTargetViews[D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT];
376     ID3D10DepthStencilView *pDepthStencilView;
377     pDevice->OMGetRenderTargets(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT, pRenderTargetViews,
378                                 &pDepthStencilView);
379
380     for (UINT i = 0; i < D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) {
381         if (!pRenderTargetViews[i]) {
382             continue;
383         }
384
385         image::Image *image;
386         image = getRenderTargetViewImage(pDevice, pRenderTargetViews[i]);
387         if (image) {
388             char label[64];
389             _snprintf(label, sizeof label, "RENDER_TARGET_%u", i);
390             json.beginMember(label);
391             json.writeImage(image, "UNKNOWN");
392             json.endMember(); // RENDER_TARGET_*
393         }
394
395         pRenderTargetViews[i]->Release();
396     }
397
398     if (pDepthStencilView) {
399         image::Image *image;
400         image = getDepthStencilViewImage(pDevice, pDepthStencilView);
401         if (image) {
402             json.beginMember("DEPTH_STENCIL");
403             json.writeImage(image, "UNKNOWN");
404             json.endMember();
405         }
406
407         pDepthStencilView->Release();
408
409     }
410
411     json.endObject();
412     json.endMember(); // framebuffer
413 }
414
415
416 } /* namespace d3dstate */