1
// Copyright (c) 2012- PPSSPP Project.
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.
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.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
22
#include "base/logging.h"
30
LPDIRECT3DSURFACE9 surf;
31
LPDIRECT3DSURFACE9 depthstencil;
32
LPDIRECT3DTEXTURE9 tex;
33
LPDIRECT3DTEXTURE9 depthstenciltex;
37
DX9::FBOColorDepth colorDepth;
40
static LPDIRECT3DSURFACE9 deviceRTsurf;
41
static LPDIRECT3DSURFACE9 deviceDSsurf;
42
static bool supportsINTZ = false;
45
#define FOURCC_INTZ ((D3DFORMAT)(MAKEFOURCC('I', 'N', 'T', 'Z')))
47
void fbo_init(LPDIRECT3D9 d3d) {
48
pD3Ddevice->GetRenderTarget(0, &deviceRTsurf);
49
pD3Ddevice->GetDepthStencilSurface(&deviceDSsurf);
52
D3DDISPLAYMODE displayMode;
53
d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
55
// To be safe, make sure both the display format and the FBO format support INTZ.
56
HRESULT displayINTZ = d3d->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, displayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, FOURCC_INTZ);
57
HRESULT fboINTZ = d3d->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_A8R8G8B8, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, FOURCC_INTZ);
58
supportsINTZ = SUCCEEDED(displayINTZ) && SUCCEEDED(fboINTZ);
63
deviceRTsurf->Release();
64
deviceDSsurf->Release();
67
FBO_DX9 *fbo_create(int width, int height, int num_color_textures, bool z_stencil, FBOColorDepth colorDepth) {
68
static uint32_t id = 0;
70
FBO_DX9 *fbo = new FBO_DX9();
73
fbo->colorDepth = colorDepth;
74
fbo->depthstenciltex = nullptr;
76
HRESULT rtResult = pD3Ddevice->CreateTexture(fbo->width, fbo->height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &fbo->tex, NULL);
77
if (FAILED(rtResult)) {
78
ELOG("Failed to create render target");
82
fbo->tex->GetSurfaceLevel(0, &fbo->surf);
86
dsResult = pD3Ddevice->CreateTexture(fbo->width, fbo->height, 1, D3DUSAGE_DEPTHSTENCIL, FOURCC_INTZ, D3DPOOL_DEFAULT, &fbo->depthstenciltex, NULL);
87
if (SUCCEEDED(dsResult)) {
88
dsResult = fbo->depthstenciltex->GetSurfaceLevel(0, &fbo->depthstencil);
91
dsResult = pD3Ddevice->CreateDepthStencilSurface(fbo->width, fbo->height, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &fbo->depthstencil, NULL);
93
if (FAILED(dsResult)) {
94
ELOG("Failed to create depth buffer");
97
if (fbo->depthstenciltex) {
98
fbo->depthstenciltex->Release();
108
void fbo_destroy(FBO_DX9 *fbo) {
110
fbo->surf->Release();
111
fbo->depthstencil->Release();
112
if (fbo->depthstenciltex) {
113
fbo->depthstenciltex->Release();
119
pD3Ddevice->SetRenderTarget(0, deviceRTsurf);
120
pD3Ddevice->SetDepthStencilSurface(deviceDSsurf);
121
dxstate.scissorRect.restore();
122
dxstate.viewport.restore();
125
void fbo_resolve(FBO_DX9 *fbo) {
128
void fbo_bind_as_render_target(FBO_DX9 *fbo) {
129
pD3Ddevice->SetRenderTarget(0, fbo->surf);
130
pD3Ddevice->SetDepthStencilSurface(fbo->depthstencil);
131
dxstate.scissorRect.restore();
132
dxstate.viewport.restore();
136
LPDIRECT3DTEXTURE9 fbo_get_color_texture(FBO_DX9 *fbo) {
140
LPDIRECT3DTEXTURE9 fbo_get_depth_texture(FBO_DX9 *fbo) {
141
return fbo->depthstenciltex;
144
LPDIRECT3DSURFACE9 fbo_get_color_for_read(FBO_DX9 *fbo) {
148
LPDIRECT3DSURFACE9 fbo_get_color_for_write(FBO_DX9 *fbo) {
152
void fbo_bind_color_as_texture(FBO_DX9 *fbo, int color) {
153
pD3Ddevice->SetTexture(0, fbo->tex);
156
void fbo_bind_depth_as_texture(FBO_DX9 *fbo) {
157
if (fbo->depthstenciltex) {
158
pD3Ddevice->SetTexture(0, fbo->depthstenciltex);
162
void fbo_get_dimensions(FBO_DX9 *fbo, int *w, int *h) {
167
HRESULT fbo_blit_color(FBO_DX9 *src, const RECT *srcRect, FBO_DX9 *dst, const RECT *dstRect, D3DTEXTUREFILTERTYPE filter) {
168
LPDIRECT3DSURFACE9 srcSurf = src ? src->surf : deviceRTsurf;
169
LPDIRECT3DSURFACE9 dstSurf = dst ? dst->surf : deviceRTsurf;
170
return pD3Ddevice->StretchRect(srcSurf, srcRect, dstSurf, dstRect, filter);