~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to GPU/Directx9/helper/dx_fbo.cpp

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "global.h"
 
19
#include <stdint.h>
 
20
#include <string.h>
 
21
#include <stdio.h>
 
22
#include "base/logging.h"
 
23
#include "dx_fbo.h"
 
24
#include "dx_state.h"
 
25
 
 
26
namespace DX9 {
 
27
        
 
28
struct FBO_DX9 {
 
29
        uint32_t id;
 
30
        LPDIRECT3DSURFACE9 surf;
 
31
        LPDIRECT3DSURFACE9 depthstencil;
 
32
        LPDIRECT3DTEXTURE9 tex;
 
33
        LPDIRECT3DTEXTURE9 depthstenciltex;
 
34
 
 
35
        int width;
 
36
        int height;
 
37
        DX9::FBOColorDepth colorDepth;
 
38
};
 
39
 
 
40
static LPDIRECT3DSURFACE9 deviceRTsurf;
 
41
static LPDIRECT3DSURFACE9 deviceDSsurf;
 
42
static bool supportsINTZ = false;
 
43
 
 
44
#define FB_DIV 1
 
45
#define FOURCC_INTZ ((D3DFORMAT)(MAKEFOURCC('I', 'N', 'T', 'Z')))
 
46
 
 
47
void fbo_init(LPDIRECT3D9 d3d) {
 
48
        pD3Ddevice->GetRenderTarget(0, &deviceRTsurf);
 
49
        pD3Ddevice->GetDepthStencilSurface(&deviceDSsurf);
 
50
 
 
51
        if (d3d) {
 
52
                D3DDISPLAYMODE displayMode;
 
53
                d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
 
54
 
 
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);
 
59
        }
 
60
}
 
61
 
 
62
void fbo_shutdown() {
 
63
        deviceRTsurf->Release();
 
64
        deviceDSsurf->Release();
 
65
}
 
66
 
 
67
FBO_DX9 *fbo_create(int width, int height, int num_color_textures, bool z_stencil, FBOColorDepth colorDepth) {
 
68
        static uint32_t id = 0;
 
69
 
 
70
        FBO_DX9 *fbo = new FBO_DX9();
 
71
        fbo->width = width;
 
72
        fbo->height = height;
 
73
        fbo->colorDepth = colorDepth;
 
74
        fbo->depthstenciltex = nullptr;
 
75
 
 
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");
 
79
                delete fbo;
 
80
                return NULL;
 
81
        }
 
82
        fbo->tex->GetSurfaceLevel(0, &fbo->surf);
 
83
 
 
84
        HRESULT dsResult;
 
85
        if (supportsINTZ) {
 
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);
 
89
                }
 
90
        } else {
 
91
                dsResult = pD3Ddevice->CreateDepthStencilSurface(fbo->width, fbo->height, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &fbo->depthstencil, NULL);
 
92
        }
 
93
        if (FAILED(dsResult)) {
 
94
                ELOG("Failed to create depth buffer");
 
95
                fbo->surf->Release();
 
96
                fbo->tex->Release();
 
97
                if (fbo->depthstenciltex) {
 
98
                        fbo->depthstenciltex->Release();
 
99
                }
 
100
                delete fbo;
 
101
                return NULL;
 
102
        }
 
103
 
 
104
        fbo->id = id++;
 
105
        return fbo;
 
106
}
 
107
 
 
108
void fbo_destroy(FBO_DX9 *fbo) {
 
109
        fbo->tex->Release();
 
110
        fbo->surf->Release();
 
111
        fbo->depthstencil->Release();
 
112
        if (fbo->depthstenciltex) {
 
113
                fbo->depthstenciltex->Release();
 
114
        }
 
115
        delete fbo;
 
116
}
 
117
 
 
118
void fbo_unbind() {
 
119
        pD3Ddevice->SetRenderTarget(0, deviceRTsurf);
 
120
        pD3Ddevice->SetDepthStencilSurface(deviceDSsurf);
 
121
        dxstate.scissorRect.restore();
 
122
        dxstate.viewport.restore();
 
123
}
 
124
 
 
125
void fbo_resolve(FBO_DX9 *fbo) {
 
126
}
 
127
 
 
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();
 
133
}
 
134
 
 
135
 
 
136
LPDIRECT3DTEXTURE9 fbo_get_color_texture(FBO_DX9 *fbo) {
 
137
        return fbo->tex;
 
138
}
 
139
 
 
140
LPDIRECT3DTEXTURE9 fbo_get_depth_texture(FBO_DX9 *fbo) {
 
141
        return fbo->depthstenciltex;
 
142
}
 
143
 
 
144
LPDIRECT3DSURFACE9 fbo_get_color_for_read(FBO_DX9 *fbo) {
 
145
        return fbo->surf;
 
146
}
 
147
 
 
148
LPDIRECT3DSURFACE9 fbo_get_color_for_write(FBO_DX9 *fbo) {
 
149
        return fbo->surf;
 
150
}
 
151
 
 
152
void fbo_bind_color_as_texture(FBO_DX9 *fbo, int color) {
 
153
        pD3Ddevice->SetTexture(0, fbo->tex);
 
154
}
 
155
 
 
156
void fbo_bind_depth_as_texture(FBO_DX9 *fbo) {
 
157
        if (fbo->depthstenciltex) {
 
158
                pD3Ddevice->SetTexture(0, fbo->depthstenciltex);
 
159
        }
 
160
}
 
161
 
 
162
void fbo_get_dimensions(FBO_DX9 *fbo, int *w, int *h) {
 
163
        *w = fbo->width;
 
164
        *h = fbo->height;
 
165
}
 
166
 
 
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);
 
171
}
 
172
 
 
173
}