1 // Copyright (c) 2014- 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 "Common/GPU/OpenGL/GLSLProgram.h"
19 #include "Core/Config.h"
20 #include "Core/ConfigValues.h"
21 #include "Core/Reporting.h"
22 #include "GPU/Common/StencilCommon.h"
23 #include "GPU/GLES/DrawEngineGLES.h"
24 #include "GPU/GLES/FramebufferManagerGLES.h"
25 #include "GPU/GLES/ShaderManagerGLES.h"
26 #include "GPU/GLES/TextureCacheGLES.h"
27
28 static const char *stencil_fs = R"(
29 #ifdef GL_ES
30 #ifdef GL_FRAGMENT_PRECISION_HIGH
31 precision highp float;
32 #else
33 precision mediump float; // just hope it's enough..
34 #endif
35 #endif
36 #if __VERSION__ >= 130
37 #define varying in
38 #define texture2D texture
39 #define gl_FragColor fragColor0
40 out vec4 fragColor0;
41 #endif
42 varying vec2 v_texcoord0;
43 uniform float u_stencilValue;
44 uniform sampler2D tex;
45 float roundAndScaleTo255f(in float x) { return floor(x * 255.99); }
46 void main() {
47 vec4 index = texture2D(tex, v_texcoord0);
48 gl_FragColor = vec4(index.a);
49 float shifted = roundAndScaleTo255f(index.a) / roundAndScaleTo255f(u_stencilValue);
50 if (mod(floor(shifted), 2.0) < 0.99) discard;
51 }
52 )";
53
54 static const char *stencil_vs = R"(
55 #ifdef GL_ES
56 precision highp float;
57 #endif
58 #if __VERSION__ >= 130
59 #define attribute in
60 #define varying out
61 #endif
62 attribute vec4 a_position;
63 attribute vec2 a_texcoord0;
64 varying vec2 v_texcoord0;
65 void main() {
66 v_texcoord0 = a_texcoord0;
67 gl_Position = a_position;
68 }
69 )";
70
NotifyStencilUpload(u32 addr,int size,StencilUpload flags)71 bool FramebufferManagerGLES::NotifyStencilUpload(u32 addr, int size, StencilUpload flags) {
72 addr &= 0x3FFFFFFF;
73 if (!MayIntersectFramebuffer(addr)) {
74 return false;
75 }
76
77 VirtualFramebuffer *dstBuffer = 0;
78 for (size_t i = 0; i < vfbs_.size(); ++i) {
79 VirtualFramebuffer *vfb = vfbs_[i];
80 if (vfb->fb_address == addr) {
81 dstBuffer = vfb;
82 }
83 }
84 if (!dstBuffer) {
85 return false;
86 }
87
88 int values = 0;
89 u8 usedBits = 0;
90
91 const u8 *src = Memory::GetPointer(addr);
92 if (!src)
93 return false;
94
95 switch (dstBuffer->format) {
96 case GE_FORMAT_565:
97 // Well, this doesn't make much sense.
98 return false;
99 case GE_FORMAT_5551:
100 usedBits = StencilBits5551(src, dstBuffer->fb_stride * dstBuffer->bufferHeight);
101 values = 2;
102 break;
103 case GE_FORMAT_4444:
104 usedBits = StencilBits4444(src, dstBuffer->fb_stride * dstBuffer->bufferHeight);
105 values = 16;
106 break;
107 case GE_FORMAT_8888:
108 usedBits = StencilBits8888(src, dstBuffer->fb_stride * dstBuffer->bufferHeight);
109 values = 256;
110 break;
111 case GE_FORMAT_INVALID:
112 case GE_FORMAT_DEPTH16:
113 // Inconceivable.
114 _assert_(false);
115 break;
116 }
117
118 if (usedBits == 0) {
119 if (flags == StencilUpload::STENCIL_IS_ZERO) {
120 // Common when creating buffers, it's already 0. We're done.
121 return false;
122 }
123 shaderManagerGL_->DirtyLastShader();
124
125 // Let's not bother with the shader if it's just zero.
126 if (dstBuffer->fbo) {
127 draw_->BindFramebufferAsRenderTarget(dstBuffer->fbo, { Draw::RPAction::KEEP, Draw::RPAction::KEEP, Draw::RPAction::CLEAR }, "NotifyStencilUpload_Clear");
128 }
129 render_->Clear(0, 0, 0, GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT, 0x8, 0, 0, 0, 0);
130 gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_VIEWPORTSCISSOR_STATE);
131 return true;
132 }
133
134 if (!stencilUploadProgram_) {
135 std::string errorString;
136 static std::string vs_code, fs_code;
137 vs_code = ApplyGLSLPrelude(stencil_vs, GL_VERTEX_SHADER);
138 fs_code = ApplyGLSLPrelude(stencil_fs, GL_FRAGMENT_SHADER);
139 std::vector<GLRShader *> shaders;
140 shaders.push_back(render_->CreateShader(GL_VERTEX_SHADER, vs_code, "stencil"));
141 shaders.push_back(render_->CreateShader(GL_FRAGMENT_SHADER, fs_code, "stencil"));
142 std::vector<GLRProgram::Semantic> semantics;
143 semantics.push_back({ 0, "a_position" });
144 semantics.push_back({ 1, "a_texcoord0" });
145 std::vector<GLRProgram::UniformLocQuery> queries;
146 queries.push_back({ &u_stencilUploadTex, "tex" });
147 queries.push_back({ &u_stencilValue, "u_stencilValue" });
148 std::vector<GLRProgram::Initializer> inits;
149 inits.push_back({ &u_stencilUploadTex, 0, TEX_SLOT_PSP_TEXTURE });
150 stencilUploadProgram_ = render_->CreateProgram(shaders, semantics, queries, inits, false);
151 for (auto iter : shaders) {
152 render_->DeleteShader(iter);
153 }
154 if (!stencilUploadProgram_) {
155 ERROR_LOG_REPORT(G3D, "Failed to compile stencilUploadProgram! This shouldn't happen.\n%s", errorString.c_str());
156 }
157 }
158
159 shaderManagerGL_->DirtyLastShader();
160
161 bool useBlit = gstate_c.Supports(GPU_SUPPORTS_FRAMEBUFFER_BLIT);
162
163 // Our fragment shader (and discard) is slow. Since the source is 1x, we can stencil to 1x.
164 // Then after we're done, we'll just blit it across and stretch it there.
165 if (dstBuffer->width == dstBuffer->renderWidth || !dstBuffer->fbo) {
166 useBlit = false;
167 }
168 u16 w = useBlit ? dstBuffer->width : dstBuffer->renderWidth;
169 u16 h = useBlit ? dstBuffer->height : dstBuffer->renderHeight;
170
171 Draw::Framebuffer *blitFBO = nullptr;
172 if (useBlit) {
173 blitFBO = GetTempFBO(TempFBO::STENCIL, w, h);
174 draw_->BindFramebufferAsRenderTarget(blitFBO, { Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE, Draw::RPAction::DONT_CARE }, "NotifyStencilUpload_Blit");
175 } else if (dstBuffer->fbo) {
176 draw_->BindFramebufferAsRenderTarget(dstBuffer->fbo, { Draw::RPAction::KEEP, Draw::RPAction::KEEP, Draw::RPAction::DONT_CARE }, "NotifyStencilUpload_NoBlit");
177 }
178 render_->SetViewport({ 0, 0, (float)w, (float)h, 0.0f, 1.0f });
179
180 float u1 = 1.0f;
181 float v1 = 1.0f;
182 textureCacheGL_->ForgetLastTexture();
183 Draw::Texture *tex = MakePixelTexture(src, dstBuffer->format, dstBuffer->fb_stride, dstBuffer->width, dstBuffer->height, u1, v1);
184 if (!tex)
185 return false;
186
187 draw_->BindTextures(TEX_SLOT_PSP_TEXTURE, 1, &tex);
188
189 // We must bind the program after starting the render pass, and set the color mask after clearing.
190 render_->SetScissor({ 0, 0, w, h });
191 render_->SetDepth(false, false, GL_ALWAYS);
192 render_->Clear(0, 0, 0, GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, 0x8, 0, 0, 0, 0);
193 render_->SetStencilFunc(GL_TRUE, GL_ALWAYS, 0xFF, 0xFF);
194 render_->SetRaster(false, GL_CCW, GL_FRONT, GL_FALSE, GL_FALSE);
195 render_->BindProgram(stencilUploadProgram_);
196 render_->SetNoBlendAndMask(0x8);
197
198 for (int i = 1; i < values; i += i) {
199 if (!(usedBits & i)) {
200 // It's already zero, let's skip it.
201 continue;
202 }
203 if (dstBuffer->format == GE_FORMAT_4444) {
204 render_->SetStencilOp((i << 4) | i, GL_REPLACE, GL_REPLACE, GL_REPLACE);
205 render_->SetUniformF1(&u_stencilValue, i * (16.0f / 255.0f));
206 } else if (dstBuffer->format == GE_FORMAT_5551) {
207 render_->SetStencilOp(0xFF, GL_REPLACE, GL_REPLACE, GL_REPLACE);
208 render_->SetUniformF1(&u_stencilValue, i * (128.0f / 255.0f));
209 } else {
210 render_->SetStencilOp(i, GL_REPLACE, GL_REPLACE, GL_REPLACE);
211 render_->SetUniformF1(&u_stencilValue, i * (1.0f / 255.0f));
212 }
213 DrawActiveTexture(0, 0, dstBuffer->width, dstBuffer->height, dstBuffer->bufferWidth, dstBuffer->bufferHeight, 0.0f, 0.0f, u1, v1, ROTATION_LOCKED_HORIZONTAL, DRAWTEX_NEAREST | DRAWTEX_KEEP_STENCIL_ALPHA);
214 }
215
216 if (useBlit) {
217 render_->SetScissor({ 0, 0, dstBuffer->renderWidth, dstBuffer->renderHeight });
218 draw_->BlitFramebuffer(blitFBO, 0, 0, w, h, dstBuffer->fbo, 0, 0, dstBuffer->renderWidth, dstBuffer->renderHeight, Draw::FB_STENCIL_BIT, Draw::FB_BLIT_NEAREST, "NotifyStencilUpload_Blit");
219 }
220
221 tex->Release();
222 RebindFramebuffer("RebindFramebuffer - Stencil");
223 gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
224 return true;
225 }
226