Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Common/DepthBufferCommon.cpp
3186 views
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 <algorithm>
19
20
#include "Common/GPU/OpenGL/GLFeatures.h"
21
#include "Common/LogReporting.h"
22
#include "GPU/Common/GPUStateUtils.h"
23
#include "GPU/Common/DrawEngineCommon.h"
24
#include "GPU/Common/FramebufferManagerCommon.h"
25
#include "GPU/Common/TextureCacheCommon.h"
26
#include "Common/GPU/ShaderWriter.h"
27
28
29
static const InputDef vs_inputs[] = {
30
{ "vec2", "a_position", Draw::SEM_POSITION },
31
};
32
33
struct DepthUB {
34
float u_depthFactor[4];
35
float u_depthShift[4];
36
float u_depthTo8[4];
37
};
38
39
const UniformDef depthUniforms[] = {
40
{ "vec4", "u_depthFactor", 0 },
41
{ "vec4", "u_depthShift", 1},
42
{ "vec4", "u_depthTo8", 2},
43
};
44
45
const UniformBufferDesc depthUBDesc{ sizeof(DepthUB), {
46
{ "u_depthFactor", -1, -1, UniformType::FLOAT4, 0 },
47
{ "u_depthShift", -1, -1, UniformType::FLOAT4, 16 },
48
{ "u_depthTo8", -1, -1, UniformType::FLOAT4, 32 },
49
} };
50
51
static const SamplerDef samplers[] = {
52
{ 0, "tex" },
53
};
54
55
static const VaryingDef varyings[] = {
56
{ "vec2", "v_texcoord", Draw::SEM_TEXCOORD0, 0, "highp" },
57
};
58
59
void GenerateDepthDownloadFs(ShaderWriter &writer) {
60
writer.DeclareSamplers(samplers);
61
writer.BeginFSMain(depthUniforms, varyings);
62
writer.C(" float depth = ").SampleTexture2D("tex", "v_texcoord").C(".r; \n");
63
// At this point, clamped maps [0, 1] to [0, 65535].
64
writer.C(" float clamped = clamp((depth - u_depthFactor.x) * u_depthFactor.y, 0.0, 1.0);\n");
65
writer.C(" vec4 enc = u_depthShift * clamped;\n");
66
writer.C(" enc = floor(mod(enc, 256.0)) * u_depthTo8;\n");
67
writer.C(" vec4 outColor = enc.yzww;\n"); // Let's ignore the bits outside 16 bit precision.
68
writer.EndFSMain("outColor");
69
}
70
71
void GenerateDepthDownloadVs(ShaderWriter &writer) {
72
writer.BeginVSMain(vs_inputs, Slice<UniformDef>::empty(), varyings);
73
writer.C("v_texcoord = a_position * 2.0;\n");
74
writer.C("gl_Position = vec4(v_texcoord * 2.0 - vec2(1.0, 1.0), 0.0, 1.0);");
75
writer.EndVSMain(varyings);
76
}
77
78
static const char * const stencil_dl_fs = R"(
79
#ifdef GL_ES
80
#ifdef GL_FRAGMENT_PRECISION_HIGH
81
precision highp float;
82
#else
83
precision mediump float;
84
#endif
85
#endif
86
#if __VERSION__ >= 130
87
#define varying in
88
#define texture2D texture
89
#define gl_FragColor fragColor0
90
out vec4 fragColor0;
91
#endif
92
varying vec2 v_texcoord;
93
lowp uniform usampler2D tex;
94
void main() {
95
uint stencil = texture2D(tex, v_texcoord).r;
96
float scaled = float(stencil) / 255.0;
97
gl_FragColor = vec4(scaled, scaled, scaled, scaled);
98
}
99
)";
100
101
static const char * const stencil_vs = R"(
102
#ifdef GL_ES
103
precision highp float;
104
#endif
105
#if __VERSION__ >= 130
106
#define attribute in
107
#define varying out
108
#endif
109
attribute vec2 a_position;
110
varying vec2 v_texcoord;
111
void main() {
112
v_texcoord = a_position * 2.0;
113
gl_Position = vec4(v_texcoord * 2.0 - vec2(1.0, 1.0), 0.0, 1.0);
114
}
115
)";
116
117
static bool SupportsDepthTexturing() {
118
if (gl_extensions.IsGLES) {
119
return gl_extensions.OES_packed_depth_stencil && (gl_extensions.OES_depth_texture || gl_extensions.GLES3);
120
}
121
return gl_extensions.ARB_texture_float;
122
}
123
124
Draw::Pipeline *CreateReadbackPipeline(Draw::DrawContext *draw, const char *tag, const UniformBufferDesc *ubDesc, const char *fs, const char *fsTag, const char *vs, const char *vsTag) {
125
using namespace Draw;
126
127
const ShaderLanguageDesc &shaderLanguageDesc = draw->GetShaderLanguageDesc();
128
129
ShaderModule *readbackFs = draw->CreateShaderModule(ShaderStage::Fragment, shaderLanguageDesc.shaderLanguage, (const uint8_t *)fs, strlen(fs), fsTag);
130
ShaderModule *readbackVs = draw->CreateShaderModule(ShaderStage::Vertex, shaderLanguageDesc.shaderLanguage, (const uint8_t *)vs, strlen(vs), vsTag);
131
_assert_(readbackFs && readbackVs);
132
133
static const InputLayoutDesc desc = {
134
8,
135
{
136
{ SEM_POSITION, DataFormat::R32G32_FLOAT, 0 },
137
},
138
};
139
InputLayout *inputLayout = draw->CreateInputLayout(desc);
140
141
BlendState *blendOff = draw->CreateBlendState({ false, 0xF });
142
DepthStencilState *stencilIgnore = draw->CreateDepthStencilState({});
143
RasterState *rasterNoCull = draw->CreateRasterState({});
144
145
PipelineDesc readbackDesc{
146
Primitive::TRIANGLE_LIST,
147
{ readbackVs, readbackFs },
148
inputLayout, stencilIgnore, blendOff, rasterNoCull, ubDesc,
149
};
150
Draw::Pipeline *pipeline = draw->CreateGraphicsPipeline(readbackDesc, tag);
151
_assert_(pipeline);
152
153
rasterNoCull->Release();
154
blendOff->Release();
155
stencilIgnore->Release();
156
inputLayout->Release();
157
158
readbackFs->Release();
159
readbackVs->Release();
160
161
return pipeline;
162
}
163
164
bool FramebufferManagerCommon::ReadbackDepthbuffer(Draw::Framebuffer *fbo, int x, int y, int w, int h, uint16_t *pixels, int pixelsStride, int destW, int destH, Draw::ReadbackMode mode) {
165
using namespace Draw;
166
167
if (!fbo) {
168
ERROR_LOG_REPORT_ONCE(vfbfbozero, Log::sceGe, "ReadbackDepthbufferSync: bad fbo");
169
return false;
170
}
171
// Old desktop GL can download depth, but not upload.
172
if (gl_extensions.IsGLES && !SupportsDepthTexturing()) {
173
return false;
174
}
175
176
// Pixel size always 4 here because we always request float or RGBA.
177
const u32 bufSize = destW * destH * 4;
178
if (!convBuf_ || convBufSize_ < bufSize) {
179
delete[] convBuf_;
180
convBuf_ = new u8[bufSize];
181
convBufSize_ = bufSize;
182
}
183
184
float scaleX = (float)destW / w;
185
float scaleY = (float)destH / h;
186
187
bool useColorPath = gl_extensions.IsGLES || scaleX != 1.0f || scaleY != 1.0f;
188
bool format16Bit = false;
189
190
if (useColorPath) {
191
if (!depthReadbackPipeline_) {
192
const ShaderLanguageDesc &shaderLanguageDesc = draw_->GetShaderLanguageDesc();
193
char depth_dl_fs[1024];
194
char depth_dl_vs[1024];
195
ShaderWriter fsWriter(depth_dl_fs, shaderLanguageDesc, ShaderStage::Fragment);
196
ShaderWriter vsWriter(depth_dl_vs, shaderLanguageDesc, ShaderStage::Vertex);
197
GenerateDepthDownloadFs(fsWriter);
198
GenerateDepthDownloadVs(vsWriter);
199
depthReadbackPipeline_ = CreateReadbackPipeline(draw_, "depth_dl", &depthUBDesc, depth_dl_fs, "depth_dl_fs", depth_dl_vs, "depth_dl_vs");
200
depthReadbackSampler_ = draw_->CreateSamplerState({});
201
}
202
203
shaderManager_->DirtyLastShader();
204
auto *blitFBO = GetTempFBO(TempFBO::Z_COPY, fbo->Width() * scaleX, fbo->Height() * scaleY);
205
draw_->BindFramebufferAsRenderTarget(blitFBO, { RPAction::DONT_CARE, RPAction::DONT_CARE, RPAction::DONT_CARE }, "ReadbackDepthbufferSync");
206
Draw::Viewport viewport = { 0.0f, 0.0f, (float)destW, (float)destH, 0.0f, 1.0f };
207
draw_->SetViewport(viewport);
208
draw_->SetScissorRect(0, 0, fbo->Width() * scaleX, fbo->Height() * scaleY);
209
210
draw_->BindFramebufferAsTexture(fbo, TEX_SLOT_PSP_TEXTURE, Aspect::DEPTH_BIT, 0);
211
draw_->BindSamplerStates(TEX_SLOT_PSP_TEXTURE, 1, &depthReadbackSampler_);
212
213
// We must bind the program after starting the render pass.
214
draw_->BindPipeline(depthReadbackPipeline_);
215
216
DepthUB ub{};
217
218
DepthScaleFactors depthScale = GetDepthScaleFactors(gstate_c.UseFlags());
219
ub.u_depthFactor[0] = depthScale.Offset();
220
ub.u_depthFactor[1] = depthScale.Scale();
221
222
// These are for packing a float in u8x4 colors. We should support more suitable readback formats on APIs that can do it.
223
static constexpr float shifts[] = { 16777215.0f, 16777215.0f / 256.0f, 16777215.0f / 65536.0f, 16777215.0f / 16777216.0f };
224
memcpy(ub.u_depthShift, shifts, sizeof(shifts));
225
static constexpr float to8[] = { 1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f };
226
memcpy(ub.u_depthTo8, to8, sizeof(to8));
227
228
draw_->UpdateDynamicUniformBuffer(&ub, sizeof(ub));
229
230
// Fullscreen triangle coordinates.
231
static const float positions[6] = {
232
0.0f, 0.0f,
233
1.0f, 0.0f,
234
0.0f, 1.0f,
235
};
236
draw_->DrawUP(positions, 3);
237
238
draw_->CopyFramebufferToMemory(blitFBO, Aspect::COLOR_BIT,
239
x * scaleX, y * scaleY, w * scaleX, h * scaleY,
240
DataFormat::R8G8B8A8_UNORM, convBuf_, destW, mode, "ReadbackDepthbufferSync");
241
242
textureCache_->ForgetLastTexture();
243
// TODO: Use 4444 (or better, R16_UNORM) so we can copy lines directly (instead of 32 -> 16 on CPU)?
244
format16Bit = true;
245
} else {
246
draw_->CopyFramebufferToMemory(fbo, Aspect::DEPTH_BIT, x, y, w, h, DataFormat::D32F, convBuf_, w, mode, "ReadbackDepthbufferSync");
247
format16Bit = false;
248
}
249
250
// TODO: Move this conversion into the backends.
251
if (format16Bit) {
252
// In this case, we used the shader to apply depth scale factors.
253
// This can be SSE'd or NEON'd very efficiently, though ideally we would avoid this conversion by using R16_UNORM for readback.
254
uint16_t *dest = pixels;
255
const u32_le *packed32 = (u32_le *)convBuf_;
256
for (int yp = 0; yp < destH; ++yp) {
257
for (int xp = 0; xp < destW; ++xp) {
258
dest[xp] = packed32[xp] & 0xFFFF;
259
}
260
dest += pixelsStride;
261
packed32 += destW;
262
}
263
} else {
264
// TODO: Apply this in the shader? May have precision issues if it becomes important to match.
265
// We downloaded float values directly in this case.
266
uint16_t *dest = pixels;
267
const float *packedf = (float *)convBuf_;
268
DepthScaleFactors depthScale = GetDepthScaleFactors(gstate_c.UseFlags());
269
for (int yp = 0; yp < destH; ++yp) {
270
for (int xp = 0; xp < destW; ++xp) {
271
float scaled = depthScale.DecodeToU16(packedf[xp]);
272
if (scaled <= 0.0f) {
273
dest[xp] = 0;
274
} else if (scaled >= 65535.0f) {
275
dest[xp] = 65535;
276
} else {
277
dest[xp] = (int)scaled;
278
}
279
}
280
dest += pixelsStride;
281
packedf += destW;
282
}
283
}
284
285
gstate_c.Dirty(DIRTY_ALL_RENDER_STATE);
286
return true;
287
}
288
289