CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Directx9/FramebufferManagerDX9.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <d3d9.h>
19
20
#include "Common/Common.h"
21
#include "Common/GPU/thin3d.h"
22
23
#include "GPU/Common/FramebufferManagerCommon.h"
24
#include "GPU/Common/GPUStateUtils.h"
25
#include "GPU/Common/PresentationCommon.h"
26
#include "GPU/Common/TextureDecoder.h"
27
#include "GPU/Directx9/FramebufferManagerDX9.h"
28
29
FramebufferManagerDX9::FramebufferManagerDX9(Draw::DrawContext *draw)
30
: FramebufferManagerCommon(draw) {
31
presentation_->SetLanguage(HLSL_D3D9);
32
preferredPixelsFormat_ = Draw::DataFormat::B8G8R8A8_UNORM;
33
}
34
35
bool FramebufferManagerDX9::ReadbackDepthbuffer(Draw::Framebuffer *fbo, int x, int y, int w, int h, uint16_t *pixels, int pixelsStride, int destW, int destH, Draw::ReadbackMode mode) {
36
// Don't yet support stretched readbacks here.
37
if (destW != w || destH != h) {
38
return false;
39
}
40
41
// We always read the depth buffer in 24_8 format.
42
LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)draw_->GetFramebufferAPITexture(fbo, Draw::FB_DEPTH_BIT, 0);
43
if (!tex)
44
return false;
45
46
// This is separate from the thin3d way because it applies DepthScaleFactors.
47
D3DSURFACE_DESC desc;
48
D3DLOCKED_RECT locked;
49
tex->GetLevelDesc(0, &desc);
50
51
RECT rect = {(LONG)x, (LONG)y, (LONG)w, (LONG)h};
52
HRESULT hr = tex->LockRect(0, &locked, &rect, D3DLOCK_READONLY);
53
if (!SUCCEEDED(hr))
54
return false;
55
56
const u32 *packed = (const u32 *)locked.pBits;
57
u16 *depth = (u16 *)pixels;
58
59
DepthScaleFactors depthScale = GetDepthScaleFactors(gstate_c.UseFlags());
60
// TODO: Optimize.
61
for (int yp = 0; yp < h; ++yp) {
62
for (int xp = 0; xp < w; ++xp) {
63
const int offset = (yp + y) * pixelsStride + x + xp;
64
65
float scaled = depthScale.DecodeToU16((packed[offset] & 0x00FFFFFF) * (1.0f / 16777215.0f));
66
if (scaled <= 0.0f) {
67
depth[offset] = 0;
68
} else if (scaled >= 65535.0f) {
69
depth[offset] = 65535;
70
} else {
71
depth[offset] = (int)scaled;
72
}
73
}
74
}
75
76
tex->UnlockRect(0);
77
return true;
78
}
79
80