fixing compilation issues, working on textures

This commit is contained in:
Kbz-8
2024-09-04 02:35:01 +02:00
parent 1b996af83f
commit 0a84ea6a18
15 changed files with 315 additions and 115 deletions

View File

@@ -19,7 +19,7 @@
Error("invalid image ptr (NULL)"); \
retval; \
} \
else if(!m_image_registry.IsTextureKnown(img)) \
else if(!m_image_registry.IsTextureKnown(static_cast<Texture*>(img))) \
{ \
Error("invalid image ptr"); \
retval; \

View File

@@ -29,7 +29,7 @@ namespace mlx
inline void LoadFont(const std::filesystem::path& filepath, float scale);
inline void TryEraseTextureFromRegistry(NonOwningPtr<Texture> texture) noexcept;
inline void TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept;
[[nodiscard]] MLX_FORCEINLINE bool HasWindow() const noexcept { return m_has_window; }
[[nodiscard]] MLX_FORCEINLINE Renderer& GetRenderer() { return m_renderer; }
@@ -42,6 +42,8 @@ namespace mlx
std::shared_ptr<Window> p_window;
std::unique_ptr<Scene> p_scene;
std::uint64_t m_current_depth = 0;
std::size_t m_width = 0;
std::size_t m_height = 0;

View File

@@ -7,6 +7,7 @@ namespace mlx
{
MLX_PROFILE_FUNCTION();
p_scene->ResetSprites();
m_current_depth = 0;
}
void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept
@@ -22,6 +23,15 @@ namespace mlx
void GraphicsSupport::TexturePut(NonOwningPtr<Texture> texture, int x, int y)
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Sprite> sprite = p_scene->GetSpriteFromTextureAndPosition(texture, Vec2f{ static_cast<float>(x), static_cast<float>(y) });
if(!sprite)
{
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec3f{ static_cast<float>(x), static_cast<float>(y), static_cast<float>(m_current_depth) });
}
else
sprite->SetPosition(Vec3f{ static_cast<float>(x), static_cast<float>(y), static_cast<float>(m_current_depth) });
m_current_depth++;
}
void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale)
@@ -29,8 +39,9 @@ namespace mlx
MLX_PROFILE_FUNCTION();
}
void GraphicsSupport::TryEraseTextureFromRegistry(NonOwningPtr<Texture> texture) noexcept
void GraphicsSupport::TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept
{
MLX_PROFILE_FUNCTION();
p_scene->TryEraseSpriteFromTexture(texture);
}
}

View File

@@ -17,7 +17,9 @@ namespace mlx
public:
Scene(SceneDescriptor desc);
Sprite& CreateSprite(std::shared_ptr<class Texture> texture) noexcept;
Sprite& CreateSprite(NonOwningPtr<class Texture> texture) noexcept;
NonOwningPtr<Sprite> GetSpriteFromTextureAndPosition(NonOwningPtr<Texture> texture, const Vec2f& position) const;
void TryEraseSpriteFromTexture(NonOwningPtr<Texture> texture);
inline void ResetSprites() { m_sprites.clear(); }

View File

@@ -14,7 +14,7 @@ namespace mlx
friend class Render2DPass;
public:
Sprite(std::shared_ptr<Texture> texture);
Sprite(NonOwningPtr<Texture> texture);
inline void SetColor(Vec4f color) noexcept { m_color = color; }
inline void SetPosition(Vec3f position) noexcept { m_position = position; }
@@ -22,7 +22,7 @@ namespace mlx
[[nodiscard]] inline const Vec4f& GetColor() const noexcept { return m_color; }
[[nodiscard]] inline const Vec3f& GetPosition() const noexcept { return m_position; }
[[nodiscard]] inline std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
[[nodiscard]] inline std::shared_ptr<Texture> GetTexture() const { return p_texture; }
[[nodiscard]] inline NonOwningPtr<Texture> GetTexture() const { return p_texture; }
~Sprite() = default;
@@ -43,7 +43,7 @@ namespace mlx
private:
DescriptorSet m_set;
std::shared_ptr<Texture> p_texture;
NonOwningPtr<Texture> p_texture;
std::shared_ptr<Mesh> p_mesh;
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
Vec3f m_position = Vec3f{ 0.0f, 0.0f, 0.0f };

View File

@@ -47,7 +47,7 @@ namespace mlx
virtual ~Image() = default;
private:
protected:
VmaAllocation m_allocation;
VkImage m_image = VK_NULL_HANDLE;
VkImageView m_image_view = VK_NULL_HANDLE;
@@ -84,6 +84,7 @@ namespace mlx
{
Init(std::move(pixels), width, height, format, is_multisampled);
}
inline void Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format = VK_FORMAT_R8G8B8A8_SRGB, bool is_multisampled = false)
{
Image::Init(ImageType::Color, width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, is_multisampled);
@@ -91,12 +92,12 @@ namespace mlx
Image::CreateSampler();
if(pixels)
{
TransitionLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
GPUBuffer staging_buffer;
std::size_t size = width * height * kvfFormatSize(format);
staging_buffer.Init(BufferType::Staging, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, pixels);
VkCommandBuffer cmd = kvfCreateCommandBuffer(RenderCore::Get().GetDevice());
kvfBeginCommandBuffer(cmd, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
TransitionLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, cmd);
kvfCopyBufferToImage(cmd, Image::Get(), staging_buffer.Get(), staging_buffer.GetOffset(), VK_IMAGE_ASPECT_COLOR_BIT, { width, height, 1 });
vkEndCommandBuffer(cmd);
VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice());
@@ -104,13 +105,26 @@ namespace mlx
kvfDestroyFence(RenderCore::Get().GetDevice(), fence);
staging_buffer.Destroy();
}
if(!pixels)
TransitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
else
TransitionLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
TransitionLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}
void SetPixel(int x, int y, std::uint32_t color) noexcept;
int GetPixel(int x, int y) noexcept;
void Update(VkCommandBuffer cmd) const;
~Texture() override { Destroy(); }
private:
void OpenCPUBuffer();
private:
std::vector<std::uint32_t> m_cpu_buffer;
std::optional<GPUBuffer> m_staging_buffer;
bool m_has_been_modified = false;
};
Texture* StbTextureLoad(const std::filesystem::path& file, int* w, int* h);
}
#endif