Skip to content

Commit

Permalink
VK: Use thread safe queue for export signaling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamievlin committed Jan 1, 2024
1 parent 5b5488c commit db0d07e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
11 changes: 10 additions & 1 deletion include/vkrender.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "triple.h"
#include "seconds.h"
#include "statistics.h"
#include "ThreadSafeQueue.h"
#include "vkRenderMessages.h"

namespace camp
{
Expand Down Expand Up @@ -379,6 +381,11 @@ class AsyVkRender
DrawMode mode = DRAWMODE_NORMAL;
std::string title = "";

/**
* @remark Main thread is the consumer, other thread is the sender of messages;
*/
ThreadSafeQueue<VulkanRendererMessage> messageQueue;

#ifdef HAVE_PTHREAD
pthread_t mainthread;

Expand Down Expand Up @@ -928,16 +935,18 @@ class AsyVkRender

void nextFrame();
void display();
void poll();
optional<VulkanRendererMessage> poll();
void mainLoop();
void cleanup();
void processMessages(VulkanRendererMessage const& msg);

void idleFunc(std::function<void()> f);
void idle();

// user controls
static void exportHandler(int=0);
void Export(int imageIndex);
bool readyForExport=false;
void quit();

double spinStep();
Expand Down
47 changes: 41 additions & 6 deletions src/vkrender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <chrono>
#include <thread>
#include "ThreadSafeQueue.h"

#define SHADER_DIRECTORY "shaders/"
#define VALIDATION_LAYER "VK_LAYER_KHRONOS_validation"
Expand Down Expand Up @@ -3860,7 +3861,7 @@ void AsyVkRender::display()
}
}

void AsyVkRender::poll()
optional<VulkanRendererMessage> AsyVkRender::poll()
{
if (View) {
if (glfwWindowShouldClose(window)) {
Expand All @@ -3876,14 +3877,38 @@ void AsyVkRender::poll()
if (View) {
glfwPollEvents();
}

return messageQueue.dequeue();
}

void AsyVkRender::processMessages(VulkanRendererMessage const& msg)
{
switch (msg)
{
case exportRender: {
if (readyForExport)
{
readyForExport= false;
exportHandler(0);
}
}
break;
default:
break;
}
}

void AsyVkRender::mainLoop()
{
int nFrames = 0;

while (poll(), true) {

while (true) {
auto const message = poll();
if (message.has_value())
{
processMessages(*message);
}

if (redraw || queueExport) {
redraw = false;
display();
Expand All @@ -3899,18 +3924,28 @@ void AsyVkRender::mainLoop()
}

vkDeviceWaitIdle(*device);

if(!View) {
if(vkthread) {
if(havewindow) {
// from where can this thread be called?
// signals to the main thread to start exporting
readyAfterExport=true;
#ifdef HAVE_PTHREAD
pthread_kill(mainthread,SIGUSR1);
if (pthread_equal(pthread_self(), this->mainthread))
{
exportHandler();
}
else
{
messageQueue.enqueue(exportRender);
}
#endif
} else {
// from main thread
initialized=true;
readyForExport=true;
readyAfterExport=true;
Signal(SIGUSR1,exportHandler);
exportHandler();
}
} else {
Expand Down

0 comments on commit db0d07e

Please sign in to comment.