Fix GL buffer race condition We were mapping GL buffers with GL_MAP_UNSYNCHRONIZED_BIT, but were then just pretending like eglSwapBuffers() had a ring 3 buffers deep, and that it would magically do the fencing for us. Furthermore, there isn't always a 1:1 correspondence between Rive frames and calls to eglSwapBuffers() anyway. (As is the case for the mysterious texture_target_gl_preserve GM, which has never fully passed on all devices ... duh!) It's invalid to use GL_MAP_UNSYNCHRONIZED_BIT without explicit fencing. And since (1) there are many OpenGL environments where we could be integrated that are agnostic to fencing, and (2) Rive, by design, should never wait for a fence on the client's behalf, it's time to quit using GL_MAP_UNSYNCHRONIZED_BIT. The GL driver can do its own implicit synchronization. This also alleviates our need to manage GL buffers in rings. We can just map them and let the driver build rings internally. At this point, the ANGLE Metal backend is mature enough that there is no measurable perf reduction from dropping the buffer rings. # Description <!-- Short, digestible description of what this adds or fixes --> # Details <!-- Rationale, history, Slack discussions, and technical details --> <!-- Questions before merging: - If this is a feature, is it sufficiently unit tested? - If this fixes a bug, is there a unit test to ensure no regression? - Does this concern Marketing, and could we make an announcement about it? - If so, now is the time to notify them - Are the commits in [Conventional Commit](https://www.conventionalcommits.org/en/v1.0.0/) format? - This will help with automated release notes --> Diffs= 93d4fda5a2 Fix GL buffer race condition (#9387) Co-authored-by: Chris Dalton <99840794+csmartdalton@users.noreply.github.com>
diff --git a/.rive_head b/.rive_head index d2b352f..20057e5 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -373238831d0e05b08a816684e2d94686c495c22e +93d4fda5a233facc3c136269939225f7d4558e1f
diff --git a/renderer/include/rive/renderer/gl/gl_state.hpp b/renderer/include/rive/renderer/gl/gl_state.hpp index 5b28cce..71fc3e1 100644 --- a/renderer/include/rive/renderer/gl/gl_state.hpp +++ b/renderer/include/rive/renderer/gl/gl_state.hpp
@@ -19,6 +19,8 @@ invalidate(); } + const GLCapabilities& capabilities() const { return m_capabilities; } + void invalidate(); enum class GLBlendMode
diff --git a/renderer/include/rive/renderer/gl/gles3.hpp b/renderer/include/rive/renderer/gl/gles3.hpp index e903551..5492a6e 100644 --- a/renderer/include/rive/renderer/gl/gles3.hpp +++ b/renderer/include/rive/renderer/gl/gles3.hpp
@@ -125,7 +125,7 @@ // Driver info. bool isGLES : 1; - bool isAndroidANGLE : 1; + bool isANGLEOrWebGL : 1; bool isAdreno : 1; bool isMali : 1; bool isPowerVR : 1;
diff --git a/renderer/include/rive/renderer/gl/render_buffer_gl_impl.hpp b/renderer/include/rive/renderer/gl/render_buffer_gl_impl.hpp index 64b6174..46576f5 100644 --- a/renderer/include/rive/renderer/gl/render_buffer_gl_impl.hpp +++ b/renderer/include/rive/renderer/gl/render_buffer_gl_impl.hpp
@@ -25,7 +25,7 @@ ~RenderBufferGLImpl(); // Returns the buffer to submit to GL draw calls, updating it if dirty. - GLuint frontBufferID() { return m_bufferIDs[frontBufferIdx()]; } + GLuint bufferID() { return m_bufferID; } protected: RenderBufferGLImpl(RenderBufferType type, @@ -34,9 +34,9 @@ void init(rcp<GLState>); - // Used by the android runtime to marshal buffers off to the GL thread for - // deletion. - std::array<GLuint, gpu::kBufferRingSize> detachBuffers(); + // Used by the android runtime to marshal the buffer off to the GL thread + // for deletion. + GLuint detachBuffer(); void* onMap() override; void onUnmap() override; @@ -49,9 +49,9 @@ bool canMapBuffer() const; const GLenum m_target; - std::array<GLuint, gpu::kBufferRingSize> m_bufferIDs{}; - std::unique_ptr<uint8_t[]> - m_fallbackMappedMemory; // Used when canMapBuffer() is false. + GLuint m_bufferID = 0; + // Used when canMapBuffer() is false. + std::unique_ptr<uint8_t[]> m_fallbackMappedMemory; rcp<GLState> m_state; }; } // namespace rive::gpu
diff --git a/renderer/src/gl/render_buffer_gl_impl.cpp b/renderer/src/gl/render_buffer_gl_impl.cpp index 2bbe616..8cd7955 100644 --- a/renderer/src/gl/render_buffer_gl_impl.cpp +++ b/renderer/src/gl/render_buffer_gl_impl.cpp
@@ -27,42 +27,31 @@ RenderBufferGLImpl::~RenderBufferGLImpl() { - for (GLuint bufferID : m_bufferIDs) + if (m_bufferID != 0) { - if (bufferID != 0) - { - m_state->deleteBuffer(bufferID); - } + m_state->deleteBuffer(m_bufferID); } } void RenderBufferGLImpl::init(rcp<GLState> state) { assert(!m_state); - assert(!m_bufferIDs[0]); + assert(m_bufferID == 0); m_state = std::move(state); - int bufferCount = (flags() & RenderBufferFlags::mappedOnceAtInitialization) - ? 1 - : gpu::kBufferRingSize; - glGenBuffers(bufferCount, m_bufferIDs.data()); + glGenBuffers(1, &m_bufferID); m_state->bindVAO(0); - for (int i = 0; i < bufferCount; ++i) - { - m_state->bindBuffer(m_target, m_bufferIDs[i]); - glBufferData(m_target, - sizeInBytes(), - nullptr, - (flags() & RenderBufferFlags::mappedOnceAtInitialization) - ? GL_STATIC_DRAW - : GL_DYNAMIC_DRAW); - } + m_state->bindBuffer(m_target, m_bufferID); + glBufferData(m_target, + sizeInBytes(), + nullptr, + (flags() & RenderBufferFlags::mappedOnceAtInitialization) + ? GL_STATIC_DRAW + : GL_DYNAMIC_DRAW); } -std::array<GLuint, gpu::kBufferRingSize> RenderBufferGLImpl::detachBuffers() +GLuint RenderBufferGLImpl::detachBuffer() { - auto detachedBuffers = m_bufferIDs; - m_bufferIDs.fill(0); - return detachedBuffers; + return std::exchange(m_bufferID, 0); } void* RenderBufferGLImpl::onMap() @@ -79,15 +68,14 @@ { #ifndef RIVE_WEBGL m_state->bindVAO(0); - m_state->bindBuffer(m_target, m_bufferIDs[backBufferIdx()]); + m_state->bindBuffer(m_target, m_bufferID); return glMapBufferRange(m_target, 0, sizeInBytes(), GL_MAP_WRITE_BIT | - GL_MAP_INVALIDATE_BUFFER_BIT | - GL_MAP_UNSYNCHRONIZED_BIT); + GL_MAP_INVALIDATE_BUFFER_BIT); #else - // WebGL doesn't support buffer mapping. + // WebGL doesn't declare glMapBufferRange(). RIVE_UNREACHABLE(); #endif } @@ -96,7 +84,7 @@ void RenderBufferGLImpl::onUnmap() { m_state->bindVAO(0); - m_state->bindBuffer(m_target, m_bufferIDs[backBufferIdx()]); + m_state->bindBuffer(m_target, m_bufferID); if (!canMapBuffer()) { glBufferSubData(m_target, @@ -114,7 +102,7 @@ #ifndef RIVE_WEBGL glUnmapBuffer(m_target); #else - // WebGL doesn't support buffer mapping. + // WebGL doesn't declare glUnmapBuffer(). RIVE_UNREACHABLE(); #endif } @@ -122,12 +110,10 @@ bool RenderBufferGLImpl::canMapBuffer() const { -#ifdef RIVE_WEBGL // WebGL doesn't support buffer mapping. - return false; -#else - // NVIDIA gives performance warnings when mapping GL_STATIC_DRAW buffers. - return !(flags() & RenderBufferFlags::mappedOnceAtInitialization); -#endif + return !m_state->capabilities().isANGLEOrWebGL && + // NVIDIA gives performance warnings when mapping GL_STATIC_DRAW + // buffers. + !(flags() & RenderBufferFlags::mappedOnceAtInitialization); } } // namespace rive::gpu
diff --git a/renderer/src/gl/render_context_gl_impl.cpp b/renderer/src/gl/render_context_gl_impl.cpp index ae4d11f..2cd2e6f 100644 --- a/renderer/src/gl/render_context_gl_impl.cpp +++ b/renderer/src/gl/render_context_gl_impl.cpp
@@ -381,15 +381,9 @@ : nullptr; } - ~BufferRingGLImpl() - { - for (int i = 0; i < kBufferRingSize; ++i) - { - m_state->deleteBuffer(m_ids[i]); - } - } + ~BufferRingGLImpl() { m_state->deleteBuffer(m_bufferID); } - GLuint submittedBufferID() const { return m_ids[submittedBufferIdx()]; } + GLuint bufferID() const { return m_bufferID; } protected: BufferRingGLImpl(GLenum target, @@ -397,43 +391,55 @@ rcp<GLState> state) : BufferRing(capacityInBytes), m_target(target), m_state(std::move(state)) { - glGenBuffers(kBufferRingSize, m_ids); - for (int i = 0; i < kBufferRingSize; ++i) - { - m_state->bindBuffer(m_target, m_ids[i]); - glBufferData(m_target, capacityInBytes, nullptr, GL_DYNAMIC_DRAW); - } + glGenBuffers(1, &m_bufferID); + m_state->bindBuffer(m_target, m_bufferID); + glBufferData(m_target, capacityInBytes, nullptr, GL_DYNAMIC_DRAW); } void* onMapBuffer(int bufferIdx, size_t mapSizeInBytes) override { -#ifdef RIVE_WEBGL - // WebGL doesn't support buffer mapping. - return shadowBuffer(); + if (m_state->capabilities().isANGLEOrWebGL) + { + // WebGL doesn't support buffer mapping. + return shadowBuffer(); + } + else + { +#ifndef RIVE_WEBGL + m_state->bindBuffer(m_target, m_bufferID); + return glMapBufferRange(m_target, + 0, + mapSizeInBytes, + GL_MAP_WRITE_BIT | + GL_MAP_INVALIDATE_BUFFER_BIT); #else - m_state->bindBuffer(m_target, m_ids[bufferIdx]); - return glMapBufferRange(m_target, - 0, - mapSizeInBytes, - GL_MAP_WRITE_BIT | - GL_MAP_INVALIDATE_BUFFER_BIT | - GL_MAP_UNSYNCHRONIZED_BIT); + // WebGL doesn't declare glMapBufferRange(). + RIVE_UNREACHABLE(); #endif + } } void onUnmapAndSubmitBuffer(int bufferIdx, size_t mapSizeInBytes) override { - m_state->bindBuffer(m_target, m_ids[bufferIdx]); -#ifdef RIVE_WEBGL - // WebGL doesn't support buffer mapping. - glBufferSubData(m_target, 0, mapSizeInBytes, shadowBuffer()); + m_state->bindBuffer(m_target, m_bufferID); + if (m_state->capabilities().isANGLEOrWebGL) + { + // WebGL doesn't support buffer mapping. + glBufferSubData(m_target, 0, mapSizeInBytes, shadowBuffer()); + } + else + { +#ifndef RIVE_WEBGL + glUnmapBuffer(m_target); #else - glUnmapBuffer(m_target); + // WebGL doesn't declare glUnmapBuffer(). + RIVE_UNREACHABLE(); #endif + } } const GLenum m_target; - GLuint m_ids[kBufferRingSize]; + GLuint m_bufferID; const rcp<GLState> m_state; }; @@ -505,7 +511,7 @@ { glBindBufferRange(GL_SHADER_STORAGE_BUFFER, bindingIdx, - submittedBufferID(), + bufferID(), offsetSizeInBytes, bindingSizeInBytes); } @@ -529,21 +535,15 @@ gpu::StorageTextureSize(capacityInBytes, m_bufferStructure); GLenum internalformat = storage_texture_internalformat(m_bufferStructure); - glGenTextures(gpu::kBufferRingSize, m_textures); + glGenTextures(1, &m_textureID); glActiveTexture(GL_TEXTURE0); - for (size_t i = 0; i < gpu::kBufferRingSize; ++i) - { - glBindTexture(GL_TEXTURE_2D, m_textures[i]); - glTexStorage2D(GL_TEXTURE_2D, 1, internalformat, width, height); - glutils::SetTexture2DSamplingParams(GL_NEAREST, GL_NEAREST); - } + glBindTexture(GL_TEXTURE_2D, m_textureID); + glTexStorage2D(GL_TEXTURE_2D, 1, internalformat, width, height); + glutils::SetTexture2DSamplingParams(GL_NEAREST, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); } - ~TexelBufferRingWebGL() - { - glDeleteTextures(gpu::kBufferRingSize, m_textures); - } + ~TexelBufferRingWebGL() { glDeleteTextures(1, &m_textureID); } void* onMapBuffer(int bufferIdx, size_t mapSizeInBytes) override { @@ -559,7 +559,7 @@ auto [updateWidth, updateHeight] = gpu::StorageTextureSize(bindingSizeInBytes, m_bufferStructure); glActiveTexture(GL_TEXTURE0 + bindingIdx); - glBindTexture(GL_TEXTURE_2D, m_textures[submittedBufferIdx()]); + glBindTexture(GL_TEXTURE_2D, m_textureID); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, @@ -574,7 +574,7 @@ protected: const gpu::StorageBufferStructure m_bufferStructure; const rcp<GLState> m_state; - GLuint m_textures[gpu::kBufferRingSize]; + GLuint m_textureID; }; std::unique_ptr<BufferRing> RenderContextGLImpl::makeUniformBufferRing( @@ -1068,8 +1068,7 @@ static GLuint gl_buffer_id(const BufferRing* bufferRing) { - return static_cast<const BufferRingGLImpl*>(bufferRing) - ->submittedBufferID(); + return static_cast<const BufferRingGLImpl*>(bufferRing)->bufferID(); } static void bind_storage_buffer(const GLCapabilities& capabilities, @@ -1848,13 +1847,12 @@ RenderBufferGLImpl*, batch.indexBuffer); m_state->bindVAO(m_imageMeshVAO); - m_state->bindBuffer(GL_ARRAY_BUFFER, - vertexBuffer->frontBufferID()); + m_state->bindBuffer(GL_ARRAY_BUFFER, vertexBuffer->bufferID()); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr); - m_state->bindBuffer(GL_ARRAY_BUFFER, uvBuffer->frontBufferID()); + m_state->bindBuffer(GL_ARRAY_BUFFER, uvBuffer->bufferID()); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr); m_state->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, - indexBuffer->frontBufferID()); + indexBuffer->bufferID()); glBindBufferRange(GL_UNIFORM_BUFFER, IMAGE_DRAW_UNIFORM_BUFFER_IDX, gl_buffer_id(imageDrawUniformBufferRing()), @@ -2029,8 +2027,10 @@ #endif if (capabilities.isGLES) { -#ifdef RIVE_ANDROID - capabilities.isAndroidANGLE = strstr(glVersionStr, "ANGLE") != NULL; +#ifdef RIVE_WEBGL + capabilities.isANGLEOrWebGL = true; +#else + capabilities.isANGLEOrWebGL = strstr(glVersionStr, "ANGLE") != nullptr; #endif #ifdef _MSC_VER sscanf_s( @@ -2195,6 +2195,7 @@ { capabilities.EXT_base_instance = true; } +#ifdef RIVE_ANDROID // Don't use EXT_clip_cull_distance if we're on ANGLE. Galaxy S22 // (OpenGL Samsung Electronics Co., Ltd.; // ANGLE (Samsung Xclipse 920) on Vulkan 1.1.179; @@ -2202,11 +2203,12 @@ // this extension but then doesn't support gl_ClipDistance in the // shader. Only use clip planes on ANGLE if ANGLE_clip_cull_distance is // supported. - else if (!capabilities.isAndroidANGLE && + else if (!capabilities.isANGLEOrWebGL && strcmp(ext, "GL_EXT_clip_cull_distance") == 0) { capabilities.EXT_clip_cull_distance = true; } +#endif else if (strcmp(ext, "GL_EXT_multisampled_render_to_texture") == 0) { capabilities.EXT_multisampled_render_to_texture = true;
diff --git a/tests/gm/texture_target_gl.cpp b/tests/gm/texture_target_gl.cpp index 00d1d2e..3bae8ba 100644 --- a/tests/gm/texture_target_gl.cpp +++ b/tests/gm/texture_target_gl.cpp
@@ -83,17 +83,6 @@ copyFrameDescriptor.clearColor = 0xffff0000; renderContext->beginFrame(std::move(copyFrameDescriptor)); originalRenderTarget->bindDestinationFramebuffer(GL_FRAMEBUFFER); - if (plsImplGL->capabilities().isAndroidANGLE) - { - // Some android devices shipping with ANGLE have a - // syncronization issue drawing this texture into the main - // framebuffer. glFinish is a very heavy handed workaround, but - // it's the only one so far to work. - // Sadly, we can't incorporate a workaround this heavy into the - // main GL runtime. Apps that run into it will have to come up - // with the best workaround for them. - glFinish(); - } plsImplGL->blitTextureToFramebufferAsDraw(m_offscreenTex, {0, 0, 256, 256}, 256);