Skip to content

Commit

Permalink
fixing frame capture
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Dec 24, 2023
1 parent df12d08 commit 9aaa26d
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 58 deletions.
6 changes: 2 additions & 4 deletions src/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2023/12/21 00:17:54 by maldavid ### ########.fr */
/* Updated: 2023/12/24 08:56:33 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,14 +37,12 @@ namespace mlx::core
while(_in->is_running())
{
_in->update();
for(auto& gs : _graphics)
gs->beginRender();

if(_loop_hook)
_loop_hook(_param);

for(auto& gs : _graphics)
gs->endRender();
gs->render();
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/core/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
/* Updated: 2023/12/22 23:10:51 by kbz_8 ### ########.fr */
/* Updated: 2023/12/24 09:39:45 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -42,8 +42,12 @@ namespace mlx
_text_put_pipeline->init(_renderer.get());
}

void GraphicsSupport::endRender() noexcept
void GraphicsSupport::render() noexcept
{
if(!_renderer->beginFrame())
return;
_proj = glm::ortho<float>(0, _width, 0, _height);
_renderer->getUniformBuffer()->setData(sizeof(_proj), &_proj);
auto cmd_buff = _renderer->getActiveCmdBuffer().get();

static std::array<VkDescriptorSet, 2> sets = {
Expand All @@ -57,6 +61,8 @@ namespace mlx
continue;
if(data.texture->getSet() == VK_NULL_HANDLE)
data.texture->setDescriptor(_renderer->getFragDescriptorSet().duplicate());
if(data.texture->getLayout() != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
data.texture->transitionLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
if(!data.texture->hasBeenUpdated())
data.texture->updateSet(0);
sets[1] = data.texture->getSet();
Expand Down
5 changes: 2 additions & 3 deletions src/core/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */
/* Updated: 2023/12/22 23:10:14 by kbz_8 ### ########.fr */
/* Updated: 2023/12/24 08:56:14 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,8 +39,7 @@ namespace mlx
inline int& getID() noexcept;
inline std::shared_ptr<MLX_Window> getWindow();

inline void beginRender() noexcept;
void endRender() noexcept;
void render() noexcept;

inline void clearRenderData() noexcept;
inline void pixelPut(int x, int y, uint32_t color) noexcept;
Expand Down
8 changes: 0 additions & 8 deletions src/core/graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ namespace mlx
int& GraphicsSupport::getID() noexcept { return _id; }
std::shared_ptr<MLX_Window> GraphicsSupport::getWindow() { return _window; }

void GraphicsSupport::beginRender() noexcept
{
if(!_renderer->beginFrame())
return;
_proj = glm::ortho<float>(0, _width, 0, _height);
_renderer->getUniformBuffer()->setData(sizeof(_proj), &_proj);
}

void GraphicsSupport::clearRenderData() noexcept
{
_textures_to_render.clear();
Expand Down
10 changes: 8 additions & 2 deletions src/renderer/command/vk_cmd_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:26:06 by maldavid #+# #+# */
/* Updated: 2023/12/23 01:32:02 by kbz_8 ### ########.fr */
/* Updated: 2023/12/24 12:58:36 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -85,7 +85,8 @@ namespace mlx
VkFence fence;
vkCreateFence(device, &fenceCreateInfo, nullptr, &fence);
vkResetFences(device, 1, &fence);
vkQueueSubmit(Render_Core::get().getQueue().getGraphic(), 1, &submitInfo, fence);
if(vkQueueSubmit(Render_Core::get().getQueue().getGraphic(), 1, &submitInfo, fence) != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan error : failed to submit a single time command buffer");
_state = state::submitted;
vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX);
vkDestroyFence(device, fence, nullptr);
Expand All @@ -102,6 +103,11 @@ namespace mlx
signalSemaphores[0] = semaphores->getRenderImageSemaphore();
waitSemaphores[0] = semaphores->getImageSemaphore();
}
else
{
signalSemaphores[0] = nullptr;
waitSemaphores[0] = nullptr;
}
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };

VkSubmitInfo submitInfo{};
Expand Down
11 changes: 6 additions & 5 deletions src/renderer/descriptors/vk_descriptor_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */
/* Updated: 2023/12/14 16:45:11 by maldavid ### ########.fr */
/* Updated: 2023/12/24 09:37:55 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,6 +15,7 @@
#include "vk_descriptor_set_layout.h"
#include <renderer/buffers/vk_ubo.h>
#include <renderer/renderer.h>
#include <renderer/images/vk_image.h>

namespace mlx
{
Expand Down Expand Up @@ -66,14 +67,14 @@ namespace mlx
}
}

void DescriptorSet::writeDescriptor(int binding, VkImageView view, VkSampler sampler) const noexcept
void DescriptorSet::writeDescriptor(int binding, const Image& image) const noexcept
{
auto device = Render_Core::get().getDevice().get();

VkDescriptorImageInfo imageInfo{};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = view;
imageInfo.sampler = sampler;
imageInfo.imageLayout = image.getLayout();
imageInfo.imageView = image.getImageView();
imageInfo.sampler = image.getSampler();

VkWriteDescriptorSet descriptorWrite{};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/descriptors/vk_descriptor_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */
/* Updated: 2023/12/14 17:12:49 by maldavid ### ########.fr */
/* Updated: 2023/12/23 18:47:49 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,7 +26,7 @@ namespace mlx
void init(class Renderer* renderer, class DescriptorPool* pool, class DescriptorSetLayout* layout);

void writeDescriptor(int binding, class UBO* ubo) const noexcept;
void writeDescriptor(int binding, VkImageView view, VkSampler sampler) const noexcept;
void writeDescriptor(int binding, const class Image& image) const noexcept;

inline bool isInit() const noexcept { return _pool != nullptr && _renderer != nullptr; }

Expand Down
7 changes: 1 addition & 6 deletions src/renderer/images/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */
/* Updated: 2023/12/23 01:52:49 by kbz_8 ### ########.fr */
/* Updated: 2023/12/23 18:49:53 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -121,16 +121,11 @@ namespace mlx
_has_been_modified = false;
}
auto cmd = renderer.getActiveCmdBuffer();
VkImageLayout layout_save = getLayout();
if(getLayout() != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
transitionLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, &cmd);
_vbo.bind(renderer);
_ibo.bind(renderer);
glm::vec2 translate(x, y);
vkCmdPushConstants(cmd.get(), renderer.getPipeline().getPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(translate), &translate);
vkCmdDrawIndexed(cmd.get(), static_cast<uint32_t>(_ibo.getSize() / sizeof(uint16_t)), 1, 0, 0, 0);
if(getLayout() != layout_save)
transitionLayout(layout_save, &cmd);
}

void Texture::destroy() noexcept
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/images/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
/* Updated: 2023/12/14 14:37:08 by maldavid ### ########.fr */
/* Updated: 2023/12/23 18:49:12 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -40,7 +40,7 @@ namespace mlx

inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); _has_been_updated = true; }
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, *this); _has_been_updated = true; }
inline bool hasBeenUpdated() const noexcept { return _has_been_updated; }
inline constexpr void resetUpdate() noexcept { _has_been_updated = false; }

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/images/texture_atlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */
/* Updated: 2023/12/14 17:12:54 by maldavid ### ########.fr */
/* Updated: 2023/12/23 18:49:25 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,7 +31,7 @@ namespace mlx

inline void setDescriptor(DescriptorSet&& set) noexcept { _set = set; }
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
inline void updateSet(int binding) const noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); }
inline void updateSet(int binding) const noexcept { _set.writeDescriptor(binding, *this); }

~TextureAtlas() = default;

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pixel_put.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 15:14:50 by maldavid #+# #+# */
/* Updated: 2023/12/14 18:26:03 by maldavid ### ########.fr */
/* Updated: 2023/12/23 19:34:30 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -56,7 +56,7 @@ namespace mlx
{
_texture.render(renderer, 0, 0);
}

void PixelPutPipeline::destroy() noexcept
{
_buffer.destroy();
Expand Down
24 changes: 15 additions & 9 deletions src/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 17:25:16 by maldavid #+# #+# */
/* Updated: 2023/12/23 01:28:03 by kbz_8 ### ########.fr */
/* Updated: 2023/12/24 16:04:04 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,7 +38,7 @@ namespace mlx

for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
_semaphores[i].init();

_uniform_buffer.reset(new UBO);
#ifdef DEBUG
_uniform_buffer->create(this, sizeof(glm::mat4), "__mlx_matrices_uniform_buffer_");
Expand All @@ -51,14 +51,14 @@ namespace mlx
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4096 }
};
_desc_pool.init(2, pool_sizes);

_vert_layout.init({
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER}
}, VK_SHADER_STAGE_VERTEX_BIT);
_frag_layout.init({
{0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER}
}, VK_SHADER_STAGE_FRAGMENT_BIT);

_vert_set.init(this, &_desc_pool, &_vert_layout);
_frag_set.init(this, &_desc_pool, &_frag_layout);

Expand All @@ -73,9 +73,9 @@ namespace mlx
{
auto device = Render_Core::get().getDevice().get();

_cmd.getCmdBuffer(_current_frame_index).waitForExecution();
if(_render_target == nullptr)
{
_cmd.getCmdBuffer(_current_frame_index).waitForExecution();
VkResult result = vkAcquireNextImageKHR(device, _swapchain(), UINT64_MAX, _semaphores[_current_frame_index].getImageSemaphore(), VK_NULL_HANDLE, &_image_index);

if(result == VK_ERROR_OUT_OF_DATE_KHR)
Expand All @@ -87,7 +87,11 @@ namespace mlx
core::error::report(e_kind::fatal_error, "Vulkan error : failed to acquire swapchain image");
}
else
{
_image_index = 0;
if(_render_target->getLayout() != VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)
_render_target->transitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
}

_cmd.getCmdBuffer(_current_frame_index).reset();
_cmd.getCmdBuffer(_current_frame_index).beginRecord();
Expand All @@ -107,7 +111,7 @@ namespace mlx

VkRect2D scissor{};
scissor.offset = { 0, 0 };
scissor.extent = _swapchain.getExtent();
scissor.extent = { fb.getWidth(), fb.getHeight()};
vkCmdSetScissor(_cmd.getCmdBuffer(_current_frame_index).get(), 0, 1, &scissor);

return true;
Expand Down Expand Up @@ -142,11 +146,13 @@ namespace mlx
}
else if(result != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan error : failed to present swap chain image");
_current_frame_index = (_current_frame_index + 1) % MAX_FRAMES_IN_FLIGHT;
}
else
_cmd.getCmdBuffer(_current_frame_index).submit(nullptr);

_current_frame_index = (_current_frame_index + 1) % MAX_FRAMES_IN_FLIGHT;
{
_cmd.getCmdBuffer(_current_frame_index).submitIdle();
_current_frame_index = 0;
}
}

void Renderer::destroy()
Expand Down
Loading

0 comments on commit 9aaa26d

Please sign in to comment.