]> git.cworth.org Git - apitrace/blob - retrace/d3d9state_images.cpp
d3dretrace: Dump D3D9 depth stencil.
[apitrace] / retrace / d3d9state_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 #include <stdint.h>
29 #include <stdio.h>
30
31 #include "image.hpp"
32 #include "json.hpp"
33 #include "d3d9imports.hpp"
34 #include "d3dstate.hpp"
35
36
37 namespace d3dstate {
38
39
40 static image::Image *
41 getSurfaceImage(IDirect3DDevice9 *pDevice,
42                 IDirect3DSurface9 *pSurface) {
43     image::Image *image = NULL;
44     D3DSURFACE_DESC Desc;
45     D3DLOCKED_RECT LockedRect;
46     const unsigned char *src;
47     unsigned char *dst;
48     HRESULT hr;
49
50     if (!pSurface) {
51         return NULL;
52     }
53
54     hr = pSurface->GetDesc(&Desc);
55     assert(SUCCEEDED(hr));
56
57     unsigned numChannels;
58     image::ChannelType channelType;
59     switch (Desc.Format) {
60     case D3DFMT_X8R8G8B8:
61     case D3DFMT_A8R8G8B8:
62     case D3DFMT_R5G6B5:
63         numChannels = 3;
64         channelType = image::TYPE_UNORM8;
65         break;
66     case D3DFMT_D16:
67     case D3DFMT_D16_LOCKABLE:
68         numChannels = 1;
69         channelType = image::TYPE_FLOAT;
70         break;
71     default:
72         std::cerr << "warning: unsupported D3DFORMAT " << Desc.Format << "\n";
73         goto no_lock;
74     }
75
76     hr = pSurface->LockRect(&LockedRect, NULL, D3DLOCK_READONLY);
77     if (FAILED(hr)) {
78         goto no_lock;
79     }
80
81     image = new image::Image(Desc.Width, Desc.Height, numChannels, true, channelType);
82     if (!image) {
83         goto no_image;
84     }
85
86     dst = image->start();
87     src = (const unsigned char *)LockedRect.pBits;
88     for (unsigned y = 0; y < Desc.Height; ++y) {
89         switch (Desc.Format) {
90         case D3DFMT_R5G6B5:
91             for (unsigned x = 0; x < Desc.Width; ++x) {
92                 uint32_t pixel = ((const uint16_t *)src)[x];
93                 dst[3*x + 0] = (( pixel        & 0x1f) * (2*0xff) + 0x1f) / (2*0x1f);
94                 dst[3*x + 1] = (((pixel >>  5) & 0x3f) * (2*0xff) + 0x3f) / (2*0x3f);
95                 dst[3*x + 2] = (( pixel >> 11        ) * (2*0xff) + 0x1f) / (2*0x1f);
96             }
97             break;
98         case D3DFMT_X8R8G8B8:
99         case D3DFMT_A8R8G8B8:
100             for (unsigned x = 0; x < Desc.Width; ++x) {
101                 dst[3*x + 0] = src[4*x + 2];
102                 dst[3*x + 1] = src[4*x + 1];
103                 dst[3*x + 2] = src[4*x + 0];
104             }
105             break;
106         case D3DFMT_D16:
107         case D3DFMT_D16_LOCKABLE:
108             for (unsigned x = 0; x < Desc.Width; ++x) {
109                 ((float *)dst)[x] = ((const uint16_t *)src)[x] * (1.0f / 0xffff);
110             }
111             break;
112         default:
113             assert(0);
114             break;
115         }
116
117         src += LockedRect.Pitch;
118         dst += image->stride();
119     }
120
121 no_image:
122     pSurface->UnlockRect();
123 no_lock:
124     return image;
125 }
126
127
128 static image::Image *
129 getRenderTargetImage(IDirect3DDevice9 *pDevice,
130                      IDirect3DSurface9 *pRenderTarget) {
131     image::Image *image = NULL;
132     D3DSURFACE_DESC Desc;
133     IDirect3DSurface9 *pStagingSurface = NULL;
134     HRESULT hr;
135
136     if (!pRenderTarget) {
137         return NULL;
138     }
139
140     hr = pRenderTarget->GetDesc(&Desc);
141     assert(SUCCEEDED(hr));
142
143     hr = pDevice->CreateOffscreenPlainSurface(Desc.Width, Desc.Height, Desc.Format, D3DPOOL_SYSTEMMEM, &pStagingSurface, NULL);
144     if (FAILED(hr)) {
145         goto no_staging;
146     }
147
148     hr = pDevice->GetRenderTargetData(pRenderTarget, pStagingSurface);
149     if (FAILED(hr)) {
150         goto no_rendertargetdata;
151     }
152
153     image = getSurfaceImage(pDevice, pStagingSurface);
154
155 no_rendertargetdata:
156     pStagingSurface->Release();
157 no_staging:
158     return image;
159 }
160
161
162 image::Image *
163 getRenderTargetImage(IDirect3DDevice9 *pDevice) {
164     HRESULT hr;
165
166     IDirect3DSurface9 *pRenderTarget = NULL;
167     hr = pDevice->GetRenderTarget(0, &pRenderTarget);
168     if (FAILED(hr)) {
169         return NULL;
170     }
171     assert(pRenderTarget);
172
173     image::Image *image = NULL;
174     if (pRenderTarget) {
175         image = getRenderTargetImage(pDevice, pRenderTarget);
176         pRenderTarget->Release();
177     }
178
179     return image;
180 }
181
182
183 void
184 dumpFramebuffer(JSONWriter &json, IDirect3DDevice9 *pDevice)
185 {
186     HRESULT hr;
187
188     json.beginMember("framebuffer");
189     json.beginObject();
190
191     D3DCAPS9 Caps;
192     pDevice->GetDeviceCaps(&Caps);
193
194     for (UINT i = 0; i < Caps.NumSimultaneousRTs; ++i) {
195         IDirect3DSurface9 *pRenderTarget = NULL;
196         hr = pDevice->GetRenderTarget(i, &pRenderTarget);
197         if (FAILED(hr)) {
198             continue;
199         }
200
201         if (!pRenderTarget) {
202             continue;
203         }
204
205         image::Image *image;
206         image = getRenderTargetImage(pDevice, pRenderTarget);
207         if (image) {
208             char label[64];
209             _snprintf(label, sizeof label, "RENDER_TARGET_%u", i);
210             json.beginMember(label);
211             json.writeImage(image, "UNKNOWN");
212             json.endMember(); // RENDER_TARGET_*
213         }
214
215         pRenderTarget->Release();
216     }
217
218     IDirect3DSurface9 *pDepthStencil = NULL;
219     hr = pDevice->GetDepthStencilSurface(&pDepthStencil);
220     if (SUCCEEDED(hr) && pDepthStencil) {
221         image::Image *image;
222         image = getSurfaceImage(pDevice, pDepthStencil);
223         if (image) {
224             json.beginMember("DEPTH_STENCIL");
225             json.writeImage(image, "UNKNOWN");
226             json.endMember(); // RENDER_TARGET_*
227         }
228     }
229
230
231     json.endObject();
232     json.endMember(); // framebuffer
233 }
234
235
236 } /* namespace d3dstate */