21 #include "png_swapchain.hpp" 25 #define STB_IMAGE_STATIC 26 #define STB_IMAGE_WRITE_IMPLEMENTATION 27 #include "framework/stb/stb_image_write.h" 35 this->swapchainImagesCount = swapchainImagesCount;
37 for (
unsigned i = 0; i < swapchainImagesCount; i++)
40 worker = thread(&PNGSwapchain::threadEntry,
this);
41 return RESULT_SUCCESS;
44 void PNGSwapchain::join()
46 if (worker.joinable())
61 void PNGSwapchain::present(
unsigned index, VkDevice device, VkDeviceMemory memory,
unsigned width,
unsigned height,
62 unsigned numFences, VkFence *fences,
bool coherent)
64 lock_guard<mutex> l{ lock };
65 ready.push({ device, memory, fences, numFences, index, width, height, coherent });
71 unique_lock<mutex> l{ lock };
72 cond.wait(l, [
this] {
return !vacant.empty(); });
73 unsigned index = vacant.front();
78 void PNGSwapchain::dump(
const Command &cmd,
unsigned sequenceCount)
81 sprintf(formatted,
".%08u.png", sequenceCount);
82 string path = basePath + formatted;
84 LOGI(
"Writing PNG file to: \"%s\".\n", path.c_str());
87 VK_CHECK(vkMapMemory(cmd.device, cmd.memory, 0, cmd.width * cmd.height * 4, 0, &pSrc));
93 VkMappedMemoryRange range = { VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE };
94 range.memory = cmd.memory;
95 range.size = VK_WHOLE_SIZE;
96 VK_CHECK(vkInvalidateMappedMemoryRanges(cmd.device, 1, &range));
99 int ret = stbi_write_png(path.c_str(), cmd.width, cmd.height, 4, pSrc, cmd.width * 4);
100 vkUnmapMemory(cmd.device, cmd.memory);
103 LOGI(
"Wrote PNG file: \"%s\".\n", path.c_str());
105 LOGE(
"Failed to write PNG file: \"%s\".\n", path.c_str());
108 void PNGSwapchain::threadEntry()
123 unsigned sequenceCount = 0;
129 unique_lock<mutex> l{ lock };
130 cond.wait(l, [
this] {
return !ready.empty() || dead; });
135 command = ready.front();
139 vkWaitForFences(command.device, command.numFences, command.fences,
true, UINT64_MAX);
140 unsigned nextVacant = displayed;
142 dump(command, sequenceCount++);
145 if (nextVacant != command.index)
147 lock_guard<mutex> l{ lock };
148 vacant.push(nextVacant);
152 displayed = command.index;
Result init(const char *pBasePath, unsigned swapchainImagesCount)
Initialize the swapchain.
~PNGSwapchain()
Destructor.
void present(unsigned index, VkDevice device, VkDeviceMemory memory, unsigned width, unsigned height, unsigned numFences, VkFence *fences, bool coherent)
Dump image for a swapchain index to disk.
unsigned acquire()
Acquire a new swapchain index. When acquire returns the image is ready to be presented into...