-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathpls_impl_ext_native.cpp
More file actions
247 lines (221 loc) · 9.04 KB
/
Copy pathpls_impl_ext_native.cpp
File metadata and controls
247 lines (221 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Copyright 2023 Rive
*/
#include "rive/renderer/gl/render_context_gl_impl.hpp"
#include "rive/renderer/gl/load_store_actions_ext.hpp"
#include "rive/renderer/gl/gl_utils.hpp"
#include "rive/renderer/gl/render_target_gl.hpp"
#include "shaders/constants.glsl"
#include <map>
#include <sstream>
#include "generated/shaders/pls_load_store_ext.glsl.exports.h"
namespace rive::gpu
{
// Wraps an EXT_shader_pixel_local_storage load/store program, described by a
// set of LoadStoreActions.
class PLSLoadStoreProgram
{
public:
PLSLoadStoreProgram(const PLSLoadStoreProgram&) = delete;
PLSLoadStoreProgram& operator=(const PLSLoadStoreProgram&) = delete;
PLSLoadStoreProgram(LoadStoreActionsEXT actions,
GLuint vertexShader,
gpu::ShaderFeatures combinedShaderFeatures,
rcp<GLState> state) :
m_state(std::move(state))
{
m_id = glCreateProgram();
glAttachShader(m_id, vertexShader);
std::ostringstream glsl;
glsl << "#version 300 es\n";
glsl << "#define " GLSL_FRAGMENT "\n";
BuildLoadStoreEXTGLSL(glsl, actions);
GLuint fragmentShader =
glutils::CompileRawGLSL(GL_FRAGMENT_SHADER, glsl.str().c_str());
glAttachShader(m_id, fragmentShader);
glDeleteShader(fragmentShader);
glutils::LinkProgram(m_id);
if (enums::is_flag_set(actions, LoadStoreActionsEXT::clearColor))
{
m_colorClearUniLocation =
glGetUniformLocation(m_id, GLSL_clearColor);
}
}
~PLSLoadStoreProgram() { m_state->deleteProgram(m_id); }
GLuint id() const { return m_id; }
GLint clearColorUniLocation() const { return m_colorClearUniLocation; }
private:
GLuint m_id;
GLint m_colorClearUniLocation = -1;
const rcp<GLState> m_state;
};
class RenderContextGLImpl::PLSImplEXTNative
: public RenderContextGLImpl::PixelLocalStorageImpl
{
public:
PLSImplEXTNative(const GLCapabilities& capabilities) :
m_capabilities(capabilities)
{}
~PLSImplEXTNative()
{
if (m_plsLoadStoreVertexShader != 0)
{
glDeleteShader(m_plsLoadStoreVertexShader);
}
m_state->deleteVAO(m_plsLoadStoreVAO);
}
void init(rcp<GLState> state) override { m_state = std::move(state); }
void getSupportedInterlockModes(
const GLCapabilities& capabilities,
PlatformFeatures* platformFeatures) const override
{
assert(capabilities.EXT_shader_pixel_local_storage);
platformFeatures->supportsRasterOrderingMode = true;
platformFeatures->supportsClockwiseMode = true;
platformFeatures->supportsClockwiseFixedFunctionMode =
capabilities.EXT_shader_pixel_local_storage2;
}
void applyPipelineStateOverrides(
const DrawBatch&,
const FlushDescriptor& desc,
const PlatformFeatures&,
PipelineState* pipelineState) const override
{
// Vivo Y21 and Oppo Reno 3 Pro both disable writes to pixel local
// storage when the color mask is off; just leave the color mask enabled
// in EXT_shader_pixel_local_storage mode.
pipelineState->colorWriteEnabled = true;
}
void activatePixelLocalStorage(RenderContextGLImpl* impl,
const FlushDescriptor& desc) override
{
#ifndef RIVE_IOS_GLES
assert(impl->m_capabilities.EXT_shader_pixel_local_storage);
assert(impl->m_capabilities.EXT_shader_framebuffer_fetch ||
impl->m_capabilities.ARM_shader_framebuffer_fetch);
auto renderTarget = static_cast<RenderTargetGL*>(desc.renderTarget);
renderTarget->bindDestinationFramebuffer(GL_FRAMEBUFFER);
if (desc.fixedFunctionColorOutput)
{
// We need EXT_shader_pixel_local_storage2 for
// fixedFunctionColorOutput.
assert(impl->m_capabilities.EXT_shader_pixel_local_storage2);
assert(desc.interlockMode == gpu::InterlockMode::clockwise);
assert(!enums::is_flag_set(
desc.combinedShaderFeatures,
gpu::ShaderFeatures::ENABLE_ADVANCED_BLEND));
glFramebufferPixelLocalStorageSizeEXT(GL_FRAMEBUFFER,
2 * sizeof(uint32_t));
}
else if (impl->m_capabilities.usePixelLocalStorage2AsWorkaround)
{
// PowerVR Rogue GE8300, OpenGL ES 3.2 build 1.10@5187610 has severe
// pixel local storage corruption issues with our renderer. Using
// the EXT_shader_pixel_local_storage2 API to set the size is an
// apparent workaround that comes with worse performance and other,
// less severe visual artifacts.
assert(impl->m_capabilities.EXT_shader_pixel_local_storage2);
glFramebufferPixelLocalStorageSizeEXT(GL_FRAMEBUFFER,
PLS_PLANE_COUNT *
sizeof(uint32_t));
}
glEnable(GL_SHADER_PIXEL_LOCAL_STORAGE_EXT);
m_state->setPipelineState(gpu::COLOR_ONLY_PIPELINE_STATE);
if (desc.fixedFunctionColorOutput)
{
// Clear the main framebuffer.
float cc[4];
UnpackColorToRGBA32FPremul(desc.colorClearValue, cc);
glClearColor(cc[0], cc[1], cc[2], cc[3]);
glClear(GL_COLOR_BUFFER_BIT);
// Clear PLS using the EXT_shader_pixel_local_storage2 API.
assert(impl->m_capabilities.EXT_shader_pixel_local_storage2);
GLuint plsClearValues[2] = {
desc.coverageClearValue,
/*clipClearValue=*/0u,
};
glClearPixelLocalStorageuiEXT(0, 2, plsClearValues);
}
else
{
// EXT_shader_pixel_local_storage doesn't have an initialization
// API. Initialize PLS by drawing a fullscreen quad.
std::array<float, 4> clearColor4f;
LoadStoreActionsEXT actions =
BuildLoadActionsEXT(desc, &clearColor4f);
const PLSLoadStoreProgram& plsProgram =
findLoadStoreProgram(actions, desc.combinedShaderFeatures);
m_state->bindProgram(plsProgram.id());
if (plsProgram.clearColorUniLocation() >= 0)
{
glUniform4fv(plsProgram.clearColorUniLocation(),
1,
clearColor4f.data());
}
m_state->bindVAO(m_plsLoadStoreVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
#endif
}
void deactivatePixelLocalStorage(RenderContextGLImpl* impl,
const FlushDescriptor& desc) override
{
#ifndef RIVE_IOS_GLES
if (!desc.fixedFunctionColorOutput)
{
// EXT_shader_pixel_local_storage doesn't support concurrent
// rendering to PLS and the framebuffer. Now that we're done, issue
// a fullscreen draw that transfers the color information from PLS
// to the main framebuffer.
LoadStoreActionsEXT actions = LoadStoreActionsEXT::storeColor;
m_state->bindProgram(
findLoadStoreProgram(actions, desc.combinedShaderFeatures)
.id());
m_state->bindVAO(m_plsLoadStoreVAO);
m_state->setPipelineState(gpu::COLOR_ONLY_PIPELINE_STATE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
glDisable(GL_SHADER_PIXEL_LOCAL_STORAGE_EXT);
#endif
}
void pushShaderDefines(gpu::InterlockMode,
std::vector<const char*>* defines) const override
{
defines->push_back(GLSL_PLS_IMPL_EXT_NATIVE);
}
private:
const PLSLoadStoreProgram& findLoadStoreProgram(
LoadStoreActionsEXT actions,
gpu::ShaderFeatures combinedShaderFeatures)
{
if (m_plsLoadStoreVertexShader == 0)
{
std::ostringstream glsl;
glsl << "#version 300 es\n";
glsl << "#define " GLSL_VERTEX "\n";
BuildLoadStoreEXTGLSL(glsl, LoadStoreActionsEXT::none);
m_plsLoadStoreVertexShader =
glutils::CompileRawGLSL(GL_VERTEX_SHADER, glsl.str().c_str());
glGenVertexArrays(1, &m_plsLoadStoreVAO);
}
const uint32_t programKey = static_cast<uint32_t>(actions);
return m_plsLoadStorePrograms
.try_emplace(programKey,
actions,
m_plsLoadStoreVertexShader,
combinedShaderFeatures,
m_state)
.first->second;
}
const GLCapabilities m_capabilities;
std::map<uint32_t, PLSLoadStoreProgram> m_plsLoadStorePrograms;
GLuint m_plsLoadStoreVertexShader = 0;
GLuint m_plsLoadStoreVAO = 0;
rcp<GLState> m_state;
};
std::unique_ptr<RenderContextGLImpl::PixelLocalStorageImpl>
RenderContextGLImpl::MakePLSImplEXTNative(const GLCapabilities& capabilities)
{
return std::make_unique<PLSImplEXTNative>(capabilities);
}
} // namespace rive::gpu