Skip to content

Commit

Permalink
adding possible new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Dec 22, 2023
1 parent bf6389f commit 064f660
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/core/application.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: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2023/12/11 19:46:49 by kbz_8 ### ########.fr */
/* Updated: 2023/12/22 21:04:48 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

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

inline void getScreenSize(int* w, int* h) noexcept;

inline void* newGraphicsSuport(std::size_t w, std::size_t h, std::string title);
inline void* newGraphicsSuport(std::size_t w, std::size_t h, const char* title);
inline void clearGraphicsSupport(void* win);
inline void destroyGraphicsSupport(void* win);

Expand Down
15 changes: 12 additions & 3 deletions src/core/application.inl
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,19 @@ namespace mlx::core
*h = DM.h;
}

void* Application::newGraphicsSuport(std::size_t w, std::size_t h, std::string title)
void* Application::newGraphicsSuport(std::size_t w, std::size_t h, const char* title)
{
_graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, title, _graphics.size()));
_in->addWindow(_graphics.back()->getWindow());
auto it = std::find_if(_textures.begin(), _textures.end(), [=](const Texture& texture)
{
return &texture == reinterpret_cast<Texture*>(const_cast<char*>(title));
});
if(it != _textures.end())
_graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, reinterpret_cast<Texture*>(const_cast<char*>(title)), _graphics.size()));
else
{
_graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, title, _graphics.size()));
_in->addWindow(_graphics.back()->getWindow());
}
return static_cast<void*>(&_graphics.back()->getID());
}

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

#include <core/graphics.h>

namespace mlx
{
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, const std::string& title, int id) :
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, Texture* render_target, int id) :
_window(nullptr),
_text_put_pipeline(std::make_unique<TextPutPipeline>()),
_renderer(std::make_unique<Renderer>()),
_width(w),
_height(h),
_id(id)
{
_renderer->setWindow(nullptr);
_renderer->init(render_target);
_pixel_put_pipeline.init(w, h, *_renderer);
_text_put_pipeline->init(_renderer.get());
}

GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id) :
_window(std::make_shared<MLX_Window>(w, h, title)),
_text_put_pipeline(std::make_unique<TextPutPipeline>()),
_renderer(std::make_unique<Renderer>()),
_width(w),
_height(h),
_id(id)
{
_renderer->setWindow(_window.get());
_renderer->init();
_renderer->init(nullptr);
_pixel_put_pipeline.init(w, h, *_renderer);
_text_put_pipeline->init(_renderer.get());
}
Expand Down Expand Up @@ -76,6 +92,7 @@ namespace mlx
_text_put_pipeline->destroy();
_pixel_put_pipeline.destroy();
_renderer->destroy();
_window->destroy();
if(_window)
_window->destroy();
}
}
7 changes: 5 additions & 2 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/11 19:47:03 by kbz_8 ### ########.fr */
/* Updated: 2023/12/22 23:10:14 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -33,7 +33,8 @@ namespace mlx
class GraphicsSupport : public non_copyable
{
public:
GraphicsSupport(std::size_t w, std::size_t h, const std::string& title, int id);
GraphicsSupport(std::size_t w, std::size_t h, Texture* render_target, int id);
GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id);

inline int& getID() noexcept;
inline std::shared_ptr<MLX_Window> getWindow();
Expand All @@ -56,6 +57,8 @@ namespace mlx
std::shared_ptr<MLX_Window> _window;
std::unique_ptr<TextPutPipeline> _text_put_pipeline; // unique_ptr because of the size of the class
std::unique_ptr<Renderer> _renderer;
std::size_t _width = 0;
std::size_t _height = 0;
int _id;
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace mlx
{
if(!_renderer->beginFrame())
return;
_proj = glm::ortho<float>(0, _window->getWidth(), 0, _window->getHeight());
_proj = glm::ortho<float>(0, _width, 0, _height);
_renderer->getUniformBuffer()->setData(sizeof(_proj), &_proj);
}

Expand Down
4 changes: 2 additions & 2 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/10 22:46:08 by kbz_8 ### ########.fr */
/* Updated: 2023/12/22 23:07:44 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,7 +31,7 @@ namespace mlx
{
void Texture::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, const char* name, bool dedicated_memory)
{
Image::create(width, height, format, TILING, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, name, dedicated_memory);
Image::create(width, height, format, TILING, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, name, dedicated_memory);
Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
Image::createSampler();

Expand Down
3 changes: 2 additions & 1 deletion src/renderer/images/vk_image.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/25 11:54:21 by maldavid #+# #+# */
/* Updated: 2023/12/15 21:44:30 by maldavid ### ########.fr */
/* Updated: 2023/12/22 23:02:54 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -53,6 +53,7 @@ namespace mlx
inline VkImageView getImageView() const noexcept { return _image_view; }
inline VkFormat getFormat() const noexcept { return _format; }
inline VkImageTiling getTiling() const noexcept { return _tiling; }
inline VkImageLayout getLayout() const noexcept { return _layout; }
inline VkSampler getSampler() const noexcept { return _sampler; }
inline uint32_t getWidth() const noexcept { return _width; }
inline uint32_t getHeight() const noexcept { return _height; }
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/pipeline/pipeline.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 21:27:38 by maldavid #+# #+# */
/* Updated: 2023/11/25 10:23:20 by maldavid ### ########.fr */
/* Updated: 2023/12/22 22:00:37 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -222,14 +222,14 @@ namespace mlx
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = (float)renderer.getSwapChain().getExtent().width;
viewport.height = (float)renderer.getSwapChain().getExtent().height;
viewport.width = (float)renderer.getFrameBuffer(0).getWidth();
viewport.height = (float)renderer.getFrameBuffer(0).getHeight();
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;

VkRect2D scissor{};
scissor.offset = { 0, 0 };
scissor.extent = renderer.getSwapChain().getExtent();
scissor.extent = { renderer.getFrameBuffer(0).getWidth(), renderer.getFrameBuffer(0).getHeight()};

VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Expand Down
94 changes: 59 additions & 35 deletions src/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,36 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 17:25:16 by maldavid #+# #+# */
/* Updated: 2023/12/10 22:21:10 by kbz_8 ### ########.fr */
/* Updated: 2023/12/22 23:16:10 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

#include <mutex>
#include <renderer/renderer.h>
#include <renderer/images/texture.h>
#include <renderer/core/render_core.h>

namespace mlx
{
void Renderer::init()
void Renderer::init(Texture* render_target)
{
_surface.create(*this);
_swapchain.init(this);
_pass.init(_swapchain.getImagesFormat());
if(render_target == nullptr)
{
_surface.create(*this);
_swapchain.init(this);
_pass.init(_swapchain.getImagesFormat(), VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
for(std::size_t i = 0; i < _swapchain.getImagesNumber(); i++)
_framebuffers.emplace_back().init(_pass, _swapchain.getImage(i));
}
else
{
_render_target = render_target;
_render_target->transitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
_pass.init(_render_target->getFormat(), _render_target->getLayout());
_framebuffers.emplace_back().init(_pass, *static_cast<Image*>(_render_target));
}
_cmd.init();

for(std::size_t i = 0; i < _swapchain.getImagesNumber(); i++)
_framebuffers.emplace_back().init(_pass, _swapchain.getImage(i));
for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
_semaphores[i].init();

Expand Down Expand Up @@ -65,15 +76,20 @@ namespace mlx
_cmd.getCmdBuffer(_current_frame_index).waitForExecution();
_cmd.getCmdBuffer(_current_frame_index).reset();

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)
if(_render_target == nullptr)
{
_swapchain.recreate();
return false;
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)
{
_swapchain.recreate();
return false;
}
else if(result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
core::error::report(e_kind::fatal_error, "Vulkan error : failed to acquire swapchain image");
}
else if(result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
core::error::report(e_kind::fatal_error, "Vulkan error : failed to acquire swapchain image");
else
_image_index = 0;

_cmd.getCmdBuffer(_current_frame_index).beginRecord();
auto& fb = _framebuffers[_image_index];
Expand Down Expand Up @@ -102,28 +118,34 @@ namespace mlx
{
_pass.end(getActiveCmdBuffer());
_cmd.getCmdBuffer(_current_frame_index).endRecord();
_cmd.getCmdBuffer(_current_frame_index).submit(_semaphores[_current_frame_index]);

VkSwapchainKHR swapchain = _swapchain();
VkSemaphore signalSemaphores[] = { _semaphores[_current_frame_index].getRenderImageSemaphore() };

VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &swapchain;
presentInfo.pImageIndices = &_image_index;

VkResult result = vkQueuePresentKHR(Render_Core::get().getQueue().getPresent(), &presentInfo);

if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || _framebufferResized)
if(_render_target == nullptr)
{
_framebufferResized = false;
_swapchain.recreate();
_cmd.getCmdBuffer(_current_frame_index).submit(_semaphores[_current_frame_index]);

VkSwapchainKHR swapchain = _swapchain();
VkSemaphore signalSemaphores[] = { _semaphores[_current_frame_index].getRenderImageSemaphore() };

VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &swapchain;
presentInfo.pImageIndices = &_image_index;

VkResult result = vkQueuePresentKHR(Render_Core::get().getQueue().getPresent(), &presentInfo);

if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || _framebufferResized)
{
_framebufferResized = false;
_swapchain.recreate();
}
else if(result != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan error : failed to present swap chain image");
}
else if(result != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan error : failed to present swap chain image");
else
_cmd.getCmdBuffer(_current_frame_index).submitIdle();

_current_frame_index = (_current_frame_index + 1) % MAX_FRAMES_IN_FLIGHT;
}
Expand All @@ -139,11 +161,13 @@ namespace mlx
_cmd.destroy();
_desc_pool.destroy();
_pass.destroy();
_swapchain.destroy();
if(_render_target == nullptr)
_swapchain.destroy();
for(auto& fb : _framebuffers)
fb.destroy();
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
_semaphores[i].destroy();
_surface.destroy();
if(_render_target == nullptr)
_surface.destroy();
}
}
6 changes: 4 additions & 2 deletions src/renderer/renderer.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: 2022/12/18 17:14:45 by maldavid #+# #+# */
/* Updated: 2023/12/10 22:19:41 by kbz_8 ### ########.fr */
/* Updated: 2023/12/22 21:59:15 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -83,7 +83,7 @@ namespace mlx
public:
Renderer() = default;

void init();
void init(class Texture* render_target);

bool beginFrame();
void endFrame();
Expand All @@ -102,6 +102,7 @@ namespace mlx
inline GraphicPipeline& getPipeline() noexcept { return _pipeline; }
inline CmdBuffer& getCmdBuffer(int i) noexcept { return _cmd.getCmdBuffer(i); }
inline CmdBuffer& getActiveCmdBuffer() noexcept { return _cmd.getCmdBuffer(_current_frame_index); }
inline FrameBuffer& getFrameBuffer(int i) noexcept { return _framebuffers[i]; }
inline DescriptorSet& getVertDescriptorSet() noexcept { return _vert_set; }
inline DescriptorSet& getFragDescriptorSet() noexcept { return _frag_set; }
inline DescriptorSetLayout& getVertDescriptorSetLayout() noexcept { return _vert_layout; }
Expand Down Expand Up @@ -133,6 +134,7 @@ namespace mlx
std::unique_ptr<UBO> _uniform_buffer;

class MLX_Window* _window = nullptr;
class Texture* _render_target = nullptr;

uint32_t _current_frame_index = 0;
uint32_t _image_index = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/renderpass/vk_render_pass.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:21:36 by maldavid #+# #+# */
/* Updated: 2023/12/10 22:32:27 by kbz_8 ### ########.fr */
/* Updated: 2023/12/22 23:05:38 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,7 +20,7 @@ namespace mlx
{
static const VkClearValue clearColor = {{{ 0.0f, 0.0f, 0.0f, 1.0f }}}; // wtf, this mess to satisfy a warning

void RenderPass::init(VkFormat attachement_format)
void RenderPass::init(VkFormat attachement_format, VkImageLayout layout)
{
VkAttachmentDescription colorAttachment{};
colorAttachment.format = attachement_format;
Expand All @@ -30,11 +30,11 @@ namespace mlx
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
colorAttachment.finalLayout = layout;

VkAttachmentReference colorAttachmentRef{};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
colorAttachmentRef.layout = (layout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR ? VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL : layout);

VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
Expand Down
Loading

0 comments on commit 064f660

Please sign in to comment.