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 "d3d9imports.hpp"
34 #include "d3dstate.hpp"
41 getSurfaceImage(IDirect3DDevice9 *pDevice,
42 IDirect3DSurface9 *pSurface) {
43 image::Image *image = NULL;
45 D3DLOCKED_RECT LockedRect;
46 const unsigned char *src;
54 hr = pSurface->GetDesc(&Desc);
55 assert(SUCCEEDED(hr));
58 image::ChannelType channelType;
59 switch (Desc.Format) {
64 channelType = image::TYPE_UNORM8;
67 case D3DFMT_D16_LOCKABLE:
69 channelType = image::TYPE_FLOAT;
72 std::cerr << "warning: unsupported D3DFORMAT " << Desc.Format << "\n";
76 hr = pSurface->LockRect(&LockedRect, NULL, D3DLOCK_READONLY);
81 image = new image::Image(Desc.Width, Desc.Height, numChannels, true, channelType);
87 src = (const unsigned char *)LockedRect.pBits;
88 for (unsigned y = 0; y < Desc.Height; ++y) {
89 switch (Desc.Format) {
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);
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];
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);
117 src += LockedRect.Pitch;
118 dst += image->stride();
122 pSurface->UnlockRect();
128 static image::Image *
129 getRenderTargetImage(IDirect3DDevice9 *pDevice,
130 IDirect3DSurface9 *pRenderTarget) {
131 image::Image *image = NULL;
132 D3DSURFACE_DESC Desc;
133 IDirect3DSurface9 *pStagingSurface = NULL;
136 if (!pRenderTarget) {
140 hr = pRenderTarget->GetDesc(&Desc);
141 assert(SUCCEEDED(hr));
143 hr = pDevice->CreateOffscreenPlainSurface(Desc.Width, Desc.Height, Desc.Format, D3DPOOL_SYSTEMMEM, &pStagingSurface, NULL);
148 hr = pDevice->GetRenderTargetData(pRenderTarget, pStagingSurface);
150 goto no_rendertargetdata;
153 image = getSurfaceImage(pDevice, pStagingSurface);
156 pStagingSurface->Release();
163 getRenderTargetImage(IDirect3DDevice9 *pDevice) {
166 IDirect3DSurface9 *pRenderTarget = NULL;
167 hr = pDevice->GetRenderTarget(0, &pRenderTarget);
171 assert(pRenderTarget);
173 image::Image *image = NULL;
175 image = getRenderTargetImage(pDevice, pRenderTarget);
176 pRenderTarget->Release();
184 dumpFramebuffer(JSONWriter &json, IDirect3DDevice9 *pDevice)
188 json.beginMember("framebuffer");
192 pDevice->GetDeviceCaps(&Caps);
194 for (UINT i = 0; i < Caps.NumSimultaneousRTs; ++i) {
195 IDirect3DSurface9 *pRenderTarget = NULL;
196 hr = pDevice->GetRenderTarget(i, &pRenderTarget);
201 if (!pRenderTarget) {
206 image = getRenderTargetImage(pDevice, pRenderTarget);
209 _snprintf(label, sizeof label, "RENDER_TARGET_%u", i);
210 json.beginMember(label);
211 json.writeImage(image, "UNKNOWN");
212 json.endMember(); // RENDER_TARGET_*
215 pRenderTarget->Release();
218 IDirect3DSurface9 *pDepthStencil = NULL;
219 hr = pDevice->GetDepthStencilSurface(&pDepthStencil);
220 if (SUCCEEDED(hr) && pDepthStencil) {
222 image = getSurfaceImage(pDevice, pDepthStencil);
224 json.beginMember("DEPTH_STENCIL");
225 json.writeImage(image, "UNKNOWN");
226 json.endMember(); // RENDER_TARGET_*
232 json.endMember(); // framebuffer
236 } /* namespace d3dstate */