Skip to content

Commit

Permalink
Fix: Be compatible with some devices (e.g. Mali-G76)
Browse files Browse the repository at this point in the history
  • Loading branch information
wysaid committed Feb 13, 2023
1 parent d3ab403 commit 01a4991
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
40 changes: 28 additions & 12 deletions library/src/main/jni/cge/filters/cgeWaveformFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,31 @@ static CGEConstString s_cshWaveform = "#version 310 es\n" CGE_SHADER_STRING(
imageStore(outputImage, newLoc, uvec4(255, 255, 255, 255));
});

static CGEConstString s_cshClearImage = "#version 310 es\n" CGE_SHADER_STRING(
precision highp float;
precision highp int;
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(rgba8ui, binding = 1) uniform writeonly highp uimage2D outputImage;

void main() {
ivec2 texCoord = ivec2(gl_GlobalInvocationID);
imageStore(outputImage, texCoord, uvec4(0, 0, 0, 255));
});

namespace CGE
{
CGEWaveformFilter::~CGEWaveformFilter() = default;

bool CGEWaveformFilter::init()
{
if (m_program.initWithComputeShader(s_cshWaveform))
if (m_program.initWithComputeShader(s_cshWaveform) && m_clearImageProgram.initWithComputeShader(s_cshClearImage))
{
m_program.bind();
setFormPosition(0.1f, 0.1f);
setFormSize(0.3f, 0.3f);
m_drawer.reset(TextureDrawer::create());
m_drawer->setFlipScale(1.0f, -1.0f); // flip upside down, meet the gl coord.
m_renderTarget = std::make_unique<FrameBufferWithTexture>();
m_diagramTexture = std::make_unique<TextureObject>();
return true;
}

Expand All @@ -45,21 +56,26 @@ bool CGEWaveformFilter::init()
void CGEWaveformFilter::render2Texture(CGEImageHandlerInterface* handler, GLuint srcTexture, GLuint vertexBufferID)
{
auto&& sz = handler->getOutputFBOSize();
if (sz.width != m_renderTarget->width() || m_renderTarget->texture() == 0)
if (sz.width != m_diagramTexture->width() || m_diagramTexture->texture() == 0)
{
m_renderTarget->bindTexture2D(sz.width, 256);
m_diagramTexture->resize(sz.width, 256);
}

m_renderTarget->bind();
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBindImageTexture(0, handler->getTargetTextureID(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8UI);
glBindImageTexture(1, m_diagramTexture->texture(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8UI);

glMemoryBarrier(GL_TEXTURE_FETCH_BARRIER_BIT);
// Clear diagram texture before frame.
// You can also use glClear(GL_COLOR_BUFFER_BIT) on some devices.
{
// @attention: glClear does not work on some devices. e.g. Mali-G76
// Perform clear with a compute shader.
m_clearImageProgram.bind();
glDispatchCompute(sz.width, sz.height, 1);
}

m_program.bind();
// glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

glBindImageTexture(0, handler->getTargetTextureID(), 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8UI);
glBindImageTexture(1, m_renderTarget->texture(), 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8UI);
m_program.bind();

glDispatchCompute(sz.width, sz.height, 1);

Expand All @@ -73,7 +89,7 @@ void CGEWaveformFilter::render2Texture(CGEImageHandlerInterface* handler, GLuint

handler->setAsTarget();
glViewport(m_position[0] * sz.width, m_position[1] * sz.height, m_size[0] * sz.width, m_size[1] * sz.height);
m_drawer->drawTexture(m_renderTarget->texture());
m_drawer->drawTexture(m_diagramTexture->texture());

#if USING_ALPHA
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Expand Down
4 changes: 3 additions & 1 deletion library/src/main/jni/cge/filters/cgeWaveformFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class CGEWaveformFilter : public CGEImageFilterInterface

protected:
std::unique_ptr<TextureDrawer> m_drawer;
std::unique_ptr<FrameBufferWithTexture> m_renderTarget;
std::unique_ptr<TextureObject> m_diagramTexture;
ProgramObject m_clearImageProgram;

Vec2f m_position;
Vec2f m_size;
};
Expand Down

0 comments on commit 01a4991

Please sign in to comment.