mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 06:33:35 +00:00
fixing things
This commit is contained in:
@@ -50,6 +50,13 @@ namespace mlx
|
||||
|
||||
void Application::SetFPSCap(std::uint32_t fps) noexcept
|
||||
{
|
||||
if(fps == 0)
|
||||
{
|
||||
SDL_DisplayMode mode;
|
||||
if(!SDL_GetCurrentDisplayMode(1, &mode))
|
||||
return;
|
||||
fps = mode.refresh_rate;
|
||||
}
|
||||
m_fps.SetMaxFPS(fps);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,13 +22,13 @@ namespace mlx
|
||||
|
||||
void Render() noexcept;
|
||||
|
||||
inline void ResetRenderData(mlx_color color) noexcept;
|
||||
void ResetRenderData(mlx_color color) noexcept;
|
||||
|
||||
inline void PixelPut(int x, int y, mlx_color color) noexcept;
|
||||
inline void PixelPutArray(int x, int y, mlx_color* color, std::size_t pixels_size) noexcept;
|
||||
inline void PixelPutRegion(int x, int y, int w, int h, mlx_color* color) noexcept;
|
||||
inline void StringPut(int x, int y, mlx_color color, std::string str);
|
||||
inline void TexturePut(NonOwningPtr<class Texture> texture, int x, int y, float scale_x, float scale_y, float angle);
|
||||
void PixelPut(int x, int y, mlx_color color) noexcept;
|
||||
void PixelPutArray(int x, int y, mlx_color* color, std::size_t pixels_size) noexcept;
|
||||
void PixelPutRegion(int x, int y, int w, int h, mlx_color* color) noexcept;
|
||||
void StringPut(int x, int y, mlx_color color, std::string str);
|
||||
void TexturePut(NonOwningPtr<class Texture> texture, int x, int y, float scale_x, float scale_y, float angle);
|
||||
|
||||
inline void TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept;
|
||||
|
||||
|
||||
@@ -3,107 +3,6 @@
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void GraphicsSupport::ResetRenderData(mlx_color color) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
Vec4f vec_color = {
|
||||
static_cast<float>(color.r) / 255.0f,
|
||||
static_cast<float>(color.g) / 255.0f,
|
||||
static_cast<float>(color.b) / 255.0f,
|
||||
static_cast<float>(color.a) / 255.0f
|
||||
};
|
||||
p_scene->ResetScene(std::move(vec_color));
|
||||
m_put_pixel_manager.ResetRenderData();
|
||||
m_draw_layer = 0;
|
||||
m_pixelput_called = false;
|
||||
}
|
||||
|
||||
void GraphicsSupport::PixelPut(int x, int y, mlx_color color) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color);
|
||||
if(texture)
|
||||
{
|
||||
m_pixelput_called = true;
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
|
||||
}
|
||||
}
|
||||
|
||||
void GraphicsSupport::PixelPutArray(int x, int y, mlx_color* pixels, std::size_t pixels_size) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixelsArray(x, y, m_draw_layer, pixels, pixels_size);
|
||||
if(texture)
|
||||
{
|
||||
m_pixelput_called = true;
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
|
||||
}
|
||||
}
|
||||
|
||||
void GraphicsSupport::PixelPutRegion(int x, int y, int w, int h, mlx_color* pixels) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixelsRegion(x, y, w, h, m_draw_layer, pixels);
|
||||
if(texture)
|
||||
{
|
||||
m_pixelput_called = true;
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
|
||||
}
|
||||
}
|
||||
|
||||
void GraphicsSupport::StringPut(int x, int y, mlx_color color, std::string str)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(str.empty())
|
||||
return;
|
||||
|
||||
Vec4f vec_color = {
|
||||
static_cast<float>(color.r) / 255.0f,
|
||||
static_cast<float>(color.g) / 255.0f,
|
||||
static_cast<float>(color.b) / 255.0f,
|
||||
static_cast<float>(color.a) / 255.0f,
|
||||
};
|
||||
|
||||
NonOwningPtr<Text> text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, vec_color);
|
||||
if(!text)
|
||||
{
|
||||
if(m_pixelput_called)
|
||||
{
|
||||
m_draw_layer++;
|
||||
m_pixelput_called = false;
|
||||
}
|
||||
Text& new_text = p_scene->CreateText(str);
|
||||
new_text.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
|
||||
new_text.SetColor(std::move(vec_color));
|
||||
}
|
||||
else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer))
|
||||
p_scene->BringToDrawLayer(text.Get(), m_draw_layer);
|
||||
}
|
||||
|
||||
void GraphicsSupport::TexturePut(NonOwningPtr<Texture> texture, int x, int y, float scale_x, float scale_y, float angle)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
NonOwningPtr<Sprite> sprite = p_scene->GetSpriteFromTexturePositionScaleRotation(texture, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, scale_x, scale_y, angle);
|
||||
if(!sprite)
|
||||
{
|
||||
if(m_pixelput_called)
|
||||
{
|
||||
m_draw_layer++;
|
||||
m_pixelput_called = false;
|
||||
}
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetCenter(Vec2f{ texture->GetWidth() / 2.0f, texture->GetHeight() / 2.0f });
|
||||
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
|
||||
new_sprite.SetScale(Vec2f{ scale_x, scale_y });
|
||||
new_sprite.SetRotation(angle);
|
||||
}
|
||||
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
|
||||
p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer);
|
||||
}
|
||||
|
||||
void GraphicsSupport::TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
|
||||
@@ -29,17 +29,21 @@ namespace mlx
|
||||
|
||||
void BringToDrawLayer(NonOwningPtr<Drawable> drawable, std::uint64_t draw_layer);
|
||||
|
||||
inline void ResetScene(Vec4f clear) { m_drawables.clear(); m_clear_color = std::move(clear); }
|
||||
inline void ResetScene(Vec4f clear) { m_drawables.clear(); m_clear_color = std::move(clear); m_has_scene_changed = true; }
|
||||
inline const Vec4f& GetClearColor() const noexcept { return m_clear_color; }
|
||||
|
||||
[[nodiscard]] MLX_FORCEINLINE const std::vector<std::shared_ptr<Drawable>>& GetDrawables() const noexcept { return m_drawables; }
|
||||
|
||||
inline void ResetChangeChecker() noexcept { m_has_scene_changed = false; }
|
||||
inline bool HasSceneChanged() const noexcept { return m_has_scene_changed; }
|
||||
|
||||
~Scene() = default;
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<Drawable>> m_drawables;
|
||||
std::shared_ptr<Font> p_bound_font;
|
||||
Vec4f m_clear_color = { 0.0f, 0.0f, 0.0f, 1.0f };
|
||||
bool m_has_scene_changed = false;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -66,6 +66,7 @@ namespace mlx
|
||||
catch(...) { return nullptr; }
|
||||
m_image_registry.RegisterTexture(texture);
|
||||
image->texture = texture;
|
||||
texture->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,8 +47,6 @@ extern "C"
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
if(fps < 0)
|
||||
mlx::Error("You cannot set a negative FPS cap (nice try)");
|
||||
else if(fps == 0)
|
||||
mlx::Error("You cannot set a FPS cap to 0 (nice try)");
|
||||
else
|
||||
mlx->app->SetFPSCap(static_cast<std::uint32_t>(fps));
|
||||
}
|
||||
|
||||
@@ -51,6 +51,107 @@ namespace mlx
|
||||
#endif
|
||||
}
|
||||
|
||||
void GraphicsSupport::ResetRenderData(mlx_color color) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
Vec4f vec_color = {
|
||||
static_cast<float>(color.r) / 255.0f,
|
||||
static_cast<float>(color.g) / 255.0f,
|
||||
static_cast<float>(color.b) / 255.0f,
|
||||
static_cast<float>(color.a) / 255.0f
|
||||
};
|
||||
p_scene->ResetScene(std::move(vec_color));
|
||||
m_put_pixel_manager.ResetRenderData();
|
||||
m_draw_layer = 0;
|
||||
m_pixelput_called = false;
|
||||
}
|
||||
|
||||
void GraphicsSupport::PixelPut(int x, int y, mlx_color color) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color);
|
||||
if(texture)
|
||||
{
|
||||
m_pixelput_called = true;
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
|
||||
}
|
||||
}
|
||||
|
||||
void GraphicsSupport::PixelPutArray(int x, int y, mlx_color* pixels, std::size_t pixels_size) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixelsArray(x, y, m_draw_layer, pixels, pixels_size);
|
||||
if(texture)
|
||||
{
|
||||
m_pixelput_called = true;
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
|
||||
}
|
||||
}
|
||||
|
||||
void GraphicsSupport::PixelPutRegion(int x, int y, int w, int h, mlx_color* pixels) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixelsRegion(x, y, w, h, m_draw_layer, pixels);
|
||||
if(texture)
|
||||
{
|
||||
m_pixelput_called = true;
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
|
||||
}
|
||||
}
|
||||
|
||||
void GraphicsSupport::StringPut(int x, int y, mlx_color color, std::string str)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(str.empty())
|
||||
return;
|
||||
|
||||
Vec4f vec_color = {
|
||||
static_cast<float>(color.r) / 255.0f,
|
||||
static_cast<float>(color.g) / 255.0f,
|
||||
static_cast<float>(color.b) / 255.0f,
|
||||
static_cast<float>(color.a) / 255.0f,
|
||||
};
|
||||
|
||||
NonOwningPtr<Text> text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, vec_color);
|
||||
if(!text)
|
||||
{
|
||||
if(m_pixelput_called)
|
||||
{
|
||||
m_draw_layer++;
|
||||
m_pixelput_called = false;
|
||||
}
|
||||
Text& new_text = p_scene->CreateText(str);
|
||||
new_text.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
|
||||
new_text.SetColor(std::move(vec_color));
|
||||
}
|
||||
else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer))
|
||||
p_scene->BringToDrawLayer(text.Get(), m_draw_layer);
|
||||
}
|
||||
|
||||
void GraphicsSupport::TexturePut(NonOwningPtr<Texture> texture, int x, int y, float scale_x, float scale_y, float angle)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
NonOwningPtr<Sprite> sprite = p_scene->GetSpriteFromTexturePositionScaleRotation(texture, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, scale_x, scale_y, angle);
|
||||
if(!sprite)
|
||||
{
|
||||
if(m_pixelput_called)
|
||||
{
|
||||
m_draw_layer++;
|
||||
m_pixelput_called = false;
|
||||
}
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetCenter(Vec2f{ texture->GetWidth() / 2.0f, texture->GetHeight() / 2.0f });
|
||||
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
|
||||
new_sprite.SetScale(Vec2f{ scale_x, scale_y });
|
||||
new_sprite.SetRotation(angle);
|
||||
}
|
||||
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
|
||||
p_scene->BringToDrawLayer(sprite.Get(), m_draw_layer);
|
||||
}
|
||||
|
||||
GraphicsSupport::~GraphicsSupport()
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
|
||||
@@ -18,12 +18,14 @@ namespace mlx
|
||||
{
|
||||
std::shared_ptr<Sprite> new_sprite = std::make_shared<Sprite>(drawable->GetMesh(), texture);
|
||||
m_drawables.push_back(new_sprite);
|
||||
m_has_scene_changed = true;
|
||||
return *new_sprite;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Sprite> sprite = std::make_shared<Sprite>(texture);
|
||||
m_drawables.push_back(sprite);
|
||||
m_has_scene_changed = true;
|
||||
return *sprite;
|
||||
}
|
||||
|
||||
@@ -83,12 +85,14 @@ namespace mlx
|
||||
{
|
||||
std::shared_ptr<Text> new_text = std::make_shared<Text>(text, p_bound_font, drawable->GetMesh());
|
||||
m_drawables.push_back(new_text);
|
||||
m_has_scene_changed = true;
|
||||
return *new_text;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Text> new_text = std::make_shared<Text>(text, p_bound_font);
|
||||
m_drawables.push_back(new_text);
|
||||
m_has_scene_changed = true;
|
||||
return *new_text;
|
||||
}
|
||||
|
||||
@@ -127,5 +131,6 @@ namespace mlx
|
||||
if(it == m_drawables.end())
|
||||
return;
|
||||
std::swap(*it, *(m_drawables.begin() + draw_layer));
|
||||
m_has_scene_changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,9 +227,27 @@ namespace mlx
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
Verify(m_sets[i] != VK_NULL_HANDLE, "invalid descriptor");
|
||||
std::vector<VkWriteDescriptorSet> writes;
|
||||
std::vector<VkDescriptorBufferInfo> buffer_infos;
|
||||
std::vector<VkDescriptorImageInfo> image_infos;
|
||||
|
||||
std::size_t image_count = 0;
|
||||
std::size_t buffer_count = 0;
|
||||
|
||||
for(auto& descriptor : m_descriptors)
|
||||
{
|
||||
if(descriptor.image_ptr)
|
||||
image_count++;
|
||||
else if(descriptor.uniform_buffer_ptr || descriptor.storage_buffer_ptr)
|
||||
buffer_count++;
|
||||
else
|
||||
FatalError("unknown descriptor data");
|
||||
}
|
||||
|
||||
std::vector<VkWriteDescriptorSet> writes(m_descriptors.size());
|
||||
std::vector<VkDescriptorBufferInfo> buffer_infos(buffer_count);
|
||||
std::vector<VkDescriptorImageInfo> image_infos(image_count);
|
||||
std::size_t buffer_index = 0;
|
||||
std::size_t image_index = 0;
|
||||
std::size_t write_index = 0;
|
||||
|
||||
for(auto& descriptor : m_descriptors)
|
||||
{
|
||||
if(descriptor.image_ptr)
|
||||
@@ -239,8 +257,9 @@ namespace mlx
|
||||
info.sampler = descriptor.image_ptr->GetSampler();
|
||||
info.imageLayout = descriptor.image_ptr->GetLayout();
|
||||
info.imageView = descriptor.image_ptr->GetImageView();
|
||||
image_infos.push_back(info);
|
||||
writes.push_back(kvfWriteImageToDescriptorSet(RenderCore::Get().GetDevice(), m_sets[i], &image_infos.back(), descriptor.binding));
|
||||
image_infos[image_index] = std::move(info);
|
||||
writes[write_index] = kvfWriteImageToDescriptorSet(RenderCore::Get().GetDevice(), m_sets[i], &image_infos[image_index], descriptor.binding);
|
||||
image_index++;
|
||||
}
|
||||
else if(descriptor.uniform_buffer_ptr)
|
||||
{
|
||||
@@ -248,8 +267,9 @@ namespace mlx
|
||||
info.buffer = descriptor.uniform_buffer_ptr->Get();
|
||||
info.offset = descriptor.uniform_buffer_ptr->GetOffset();
|
||||
info.range = VK_WHOLE_SIZE;
|
||||
buffer_infos.push_back(info);
|
||||
writes.push_back(kvfWriteUniformBufferToDescriptorSet(RenderCore::Get().GetDevice(), m_sets[i], &buffer_infos.back(), descriptor.binding));
|
||||
buffer_infos[buffer_index] = std::move(info);
|
||||
writes[write_index] = kvfWriteUniformBufferToDescriptorSet(RenderCore::Get().GetDevice(), m_sets[i], &buffer_infos[buffer_index], descriptor.binding);
|
||||
buffer_index++;
|
||||
}
|
||||
else if(descriptor.storage_buffer_ptr)
|
||||
{
|
||||
@@ -257,9 +277,11 @@ namespace mlx
|
||||
info.buffer = descriptor.storage_buffer_ptr->Get();
|
||||
info.offset = descriptor.storage_buffer_ptr->GetOffset();
|
||||
info.range = VK_WHOLE_SIZE;
|
||||
buffer_infos.push_back(info);
|
||||
writes.push_back(kvfWriteStorageBufferToDescriptorSet(RenderCore::Get().GetDevice(), m_sets[i], &buffer_infos.back(), descriptor.binding));
|
||||
buffer_infos[buffer_index] = std::move(info);
|
||||
writes[write_index] = kvfWriteStorageBufferToDescriptorSet(RenderCore::Get().GetDevice(), m_sets[i], &buffer_infos[buffer_index], descriptor.binding);
|
||||
buffer_index++;
|
||||
}
|
||||
write_index++;
|
||||
}
|
||||
RenderCore::Get().vkUpdateDescriptorSets(RenderCore::Get().GetDevice(), writes.size(), writes.data(), 0, nullptr);
|
||||
}
|
||||
|
||||
@@ -100,9 +100,9 @@ namespace mlx
|
||||
for(auto& drawable : drawables)
|
||||
{
|
||||
// Check every textures and update modified ones to GPU before starting the render pass
|
||||
drawable->Update(cmd);
|
||||
if(!drawable->IsSetInit())
|
||||
drawable->UpdateDescriptorSet(p_texture_set);
|
||||
drawable->Update(cmd);
|
||||
}
|
||||
|
||||
m_pipeline.BindPipeline(cmd, 0, {});
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace mlx
|
||||
|
||||
void RenderPasses::Pass(Scene& scene, Renderer& renderer, const Vec4f& clear_color)
|
||||
{
|
||||
bool force_render = false;
|
||||
if(!m_main_render_texture.IsInit())
|
||||
{
|
||||
VkExtent2D extent;
|
||||
@@ -34,11 +35,15 @@ namespace mlx
|
||||
m_main_render_texture.Init({}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_UNORM, false, {});
|
||||
#endif
|
||||
m_main_render_texture.TransitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||
force_render = true;
|
||||
}
|
||||
if(scene.HasSceneChanged() || force_render)
|
||||
{
|
||||
m_main_render_texture.Clear(renderer.GetActiveCommandBuffer(), clear_color);
|
||||
m_2Dpass.Pass(scene, renderer, m_main_render_texture);
|
||||
}
|
||||
m_main_render_texture.Clear(renderer.GetActiveCommandBuffer(), clear_color);
|
||||
|
||||
m_2Dpass.Pass(scene, renderer, m_main_render_texture);
|
||||
m_final.Pass(scene, renderer, m_main_render_texture, p_render_target);
|
||||
scene.ResetChangeChecker();
|
||||
}
|
||||
|
||||
void RenderPasses::Destroy()
|
||||
|
||||
@@ -87,6 +87,8 @@ namespace mlx
|
||||
DebugLog("Vulkan: image available semaphore destroyed");
|
||||
kvfDestroySemaphore(RenderCore::Get().GetDevice(), m_render_finished_semaphores[i]);
|
||||
DebugLog("Vulkan: render finished semaphore destroyed");
|
||||
kvfDestroyCommandBuffer(RenderCore::Get().GetDevice(), m_cmd_buffers[i]);
|
||||
DebugLog("Vulkan: command buffer destroyed");
|
||||
kvfDestroyFence(RenderCore::Get().GetDevice(), m_cmd_fences[i]);
|
||||
DebugLog("Vulkan: fence destroyed");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user