Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/GLES/StencilBufferGLES.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/DrawEngineCommon.h"
23
#include "GPU/Common/TextureCacheCommon.h"
24
#include "GPU/GLES/FramebufferManagerGLES.h"
25
#include "Common/GPU/ShaderWriter.h"
26
27
static const InputDef vs_inputs[] = {
28
{ "vec2", "a_position", Draw::SEM_POSITION },
29
};
30
31
struct DepthUB {
32
float u_depthFactor[4];
33
float u_depthShift[4];
34
float u_depthTo8[4];
35
};
36
37
const UniformDef depthUniforms[] = {
38
{ "vec4", "u_depthFactor", 0 },
39
{ "vec4", "u_depthShift", 1},
40
{ "vec4", "u_depthTo8", 2},
41
};
42
43
const UniformBufferDesc depthUBDesc{ sizeof(DepthUB), {
44
{ "u_depthFactor", -1, -1, UniformType::FLOAT4, 0 },
45
{ "u_depthShift", -1, -1, UniformType::FLOAT4, 16 },
46
{ "u_depthTo8", -1, -1, UniformType::FLOAT4, 32 },
47
} };
48
49
static const SamplerDef samplers[] = {
50
{ 0, "tex" },
51
};
52
53
static const VaryingDef varyings[] = {
54
{ "vec2", "v_texcoord", Draw::SEM_TEXCOORD0, 0, "highp" },
55
};
56
57
static const char * const stencil_dl_fs = R"(
58
#ifdef GL_ES
59
#ifdef GL_FRAGMENT_PRECISION_HIGH
60
precision highp float;
61
#else
62
precision mediump float;
63
#endif
64
#endif
65
#if __VERSION__ >= 130
66
#define varying in
67
#define texture2D texture
68
#define gl_FragColor fragColor0
69
out vec4 fragColor0;
70
#endif
71
varying vec2 v_texcoord;
72
lowp uniform usampler2D tex;
73
void main() {
74
uint stencil = texture2D(tex, v_texcoord).r;
75
float scaled = float(stencil) / 255.0;
76
gl_FragColor = vec4(scaled, scaled, scaled, scaled);
77
}
78
)";
79
80
static const char * const stencil_vs = R"(
81
#ifdef GL_ES
82
precision highp float;
83
#endif
84
#if __VERSION__ >= 130
85
#define attribute in
86
#define varying out
87
#endif
88
attribute vec2 a_position;
89
varying vec2 v_texcoord;
90
void main() {
91
v_texcoord = a_position * 2.0;
92
gl_Position = vec4(v_texcoord * 2.0 - vec2(1.0, 1.0), 0.0, 1.0);
93
}
94
)";
95
96
Draw::Pipeline *CreateReadbackPipeline(Draw::DrawContext *draw, const char *tag, const UniformBufferDesc *ubDesc, const char *fs, const char *fsTag, const char *vs, const char *vsTag);
97
98
// Well, this is not depth, but it's depth/stencil related.
99
bool FramebufferManagerGLES::ReadbackStencilbuffer(Draw::Framebuffer *fbo, int x, int y, int w, int h, uint8_t *pixels, int pixelsStride, Draw::ReadbackMode mode) {
100
using namespace Draw;
101
102
if (!fbo) {
103
ERROR_LOG_REPORT_ONCE(vfbfbozero, Log::sceGe, "ReadbackStencilbufferSync: bad fbo");
104
return false;
105
}
106
107
const bool useColorPath = gl_extensions.IsGLES;
108
if (!useColorPath) {
109
return draw_->CopyFramebufferToMemory(fbo, Aspect::STENCIL_BIT, x, y, w, h, DataFormat::S8, pixels, pixelsStride, ReadbackMode::BLOCK, "ReadbackStencilbufferSync");
110
}
111
112
// Unsupported below GLES 3.1 or without ARB_stencil_texturing.
113
// OES_texture_stencil8 is related, but used to specify texture data.
114
if ((gl_extensions.IsGLES && !gl_extensions.VersionGEThan(3, 1)) && !gl_extensions.ARB_stencil_texturing)
115
return false;
116
117
// Pixel size always 4 here because we always request RGBA back.
118
const u32 bufSize = w * h * 4;
119
if (!convBuf_ || convBufSize_ < bufSize) {
120
delete[] convBuf_;
121
convBuf_ = new u8[bufSize];
122
convBufSize_ = bufSize;
123
}
124
125
if (!stencilReadbackPipeline_) {
126
stencilReadbackPipeline_ = CreateReadbackPipeline(draw_, "stencil_dl", &depthUBDesc, stencil_dl_fs, "stencil_dl_fs", stencil_vs, "stencil_vs");
127
stencilReadbackSampler_ = draw_->CreateSamplerState({});
128
}
129
130
shaderManager_->DirtyLastShader();
131
auto *blitFBO = GetTempFBO(TempFBO::Z_COPY, fbo->Width(), fbo->Height());
132
draw_->BindFramebufferAsRenderTarget(blitFBO, { RPAction::DONT_CARE, RPAction::DONT_CARE, RPAction::DONT_CARE }, "ReadbackStencilbufferSync");
133
Draw::Viewport viewport = { 0.0f, 0.0f, (float)fbo->Width(), (float)fbo->Height(), 0.0f, 1.0f };
134
draw_->SetViewport(viewport);
135
136
draw_->BindFramebufferAsTexture(fbo, TEX_SLOT_PSP_TEXTURE, Aspect::STENCIL_BIT, 0);
137
draw_->BindSamplerStates(TEX_SLOT_PSP_TEXTURE, 1, &stencilReadbackSampler_);
138
139
// We must bind the program after starting the render pass.
140
draw_->SetScissorRect(0, 0, w, h);
141
draw_->BindPipeline(stencilReadbackPipeline_);
142
143
// Fullscreen triangle coordinates.
144
static const float positions[6] = {
145
0.0, 0.0,
146
1.0, 0.0,
147
0.0, 1.0,
148
};
149
draw_->DrawUP(positions, 3);
150
151
draw_->CopyFramebufferToMemory(blitFBO, Aspect::COLOR_BIT, x, y, w, h, DataFormat::R8G8B8A8_UNORM, convBuf_, w, mode, "ReadbackStencilbufferSync");
152
153
textureCache_->ForgetLastTexture();
154
155
// TODO: Use 1/4 width to write all values directly and skip CPU conversion?
156
uint8_t *dest = pixels;
157
const u32_le *packed32 = (u32_le *)convBuf_;
158
for (int yp = 0; yp < h; ++yp) {
159
for (int xp = 0; xp < w; ++xp) {
160
dest[xp] = packed32[xp] & 0xFF;
161
}
162
dest += pixelsStride;
163
packed32 += w;
164
}
165
166
gstate_c.Dirty(DIRTY_ALL_RENDER_STATE);
167
return true;
168
}
169
170