Skip to content

Commit

Permalink
Fix crashes due to concurrent texture accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
nmellado committed Dec 12, 2023
1 parent fdae89f commit 81db3bf
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ class ExampleApplication : public Screen {
window->set_size(Vector2i(768,768));
window->set_layout(new GroupLayout(3));

m_textureBuffer = new uint8_t [tex_width*tex_height];
m_textureBufferPing = new uint8_t [tex_width * tex_height];
m_textureBufferPong = new uint8_t [tex_width * tex_height];
m_texture = new Texture(
Texture::PixelFormat::RGBA,
Texture::ComponentFormat::UInt8,
Expand All @@ -206,6 +207,7 @@ class ExampleApplication : public Screen {
image_view->center();
image_view->setUpdateFunction([this](){ this->renderPasses();});

renderPasses(); // render twice to fill m_textureBufferPing and m_textureBufferPong
renderPasses();

// call perform_layout
Expand All @@ -229,7 +231,7 @@ class ExampleApplication : public Screen {

void draw_contents() override {
if (m_needUpdate){
m_texture->upload(m_textureBuffer);
m_texture->upload(m_computeInPing ? m_textureBufferPong : m_textureBufferPing);
m_needUpdate = false;
}
Screen::draw_contents();
Expand Down Expand Up @@ -271,15 +273,17 @@ class ExampleApplication : public Screen {
std::cout << "[Main] Update texture" << std::endl;
const auto& points = image_view->getPointCollection();
for (auto* p : m_passes) {
p->render(points, m_textureBuffer, tex_width, tex_height);
p->render(points, m_computeInPing ? m_textureBufferPing : m_textureBufferPong, tex_width, tex_height);
}

m_computeInPing = ! m_computeInPing;
m_needUpdate = true;
}
private:
uint8_t* m_textureBuffer {nullptr};
uint8_t* m_textureBufferPing {nullptr}, * m_textureBufferPong {nullptr};
bool m_computeInPing{true};
Texture* m_texture {nullptr};
std::array<DrawingPass*,3> m_passes;
std::array<DrawingPass*,3> m_passes{nullptr, nullptr, nullptr};
bool m_needUpdate{false};
Widget* pass1Widget, *distanceFieldWidget,
*genericFitWidget, //< parameters applicable to all fitting techniques
Expand Down

0 comments on commit 81db3bf

Please sign in to comment.