wtf is hapenning

This commit is contained in:
2024-12-28 15:42:38 +01:00
parent d1d7f7ed98
commit ce530c765e
10 changed files with 68 additions and 24 deletions

View File

@@ -130,6 +130,8 @@ void window_hook(int event, void* param)
mlx_loop_end(((mlx_t*)param)->mlx); mlx_loop_end(((mlx_t*)param)->mlx);
} }
#include <stdlib.h>
int main(void) int main(void)
{ {
mlx_t mlx; mlx_t mlx;
@@ -166,7 +168,12 @@ int main(void)
mlx.logo_png = mlx_new_image_from_file(mlx.mlx, "42_logo.png", &dummy, &dummy); mlx.logo_png = mlx_new_image_from_file(mlx.mlx, "42_logo.png", &dummy, &dummy);
mlx.logo_bmp = mlx_new_image_from_file(mlx.mlx, "42_logo.bmp", &dummy, &dummy); mlx.logo_bmp = mlx_new_image_from_file(mlx.mlx, "42_logo.bmp", &dummy, &dummy);
mlx.logo_jpg = mlx_new_image_from_file(mlx.mlx, "42_logo.jpg", &dummy, &dummy); //mlx.logo_jpg = mlx_new_image_from_file(mlx.mlx, "42_logo.jpg", &dummy, &dummy);
mlx.logo_jpg = mlx_new_image(mlx.mlx, dummy, dummy);
mlx_color* data = (mlx_color*)malloc(dummy * dummy * sizeof(mlx_color));
mlx_get_image_region(mlx.mlx, mlx.logo_png, 0, 0, dummy, dummy, data);
mlx_set_image_region(mlx.mlx, mlx.logo_jpg, 0, 0, dummy, dummy, data);
mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, (mlx_color){ .rgba = 0xFF00FFFF }); mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, (mlx_color){ .rgba = 0xFF00FFFF });
mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo_png, 0, 0); mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo_png, 0, 0);

View File

@@ -87,7 +87,6 @@ namespace mlx
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
NonOwningPtr<Sprite> sprite = p_scene->GetSpriteFromTexturePositionScaleRotation(texture, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, scale_x, scale_y, angle); NonOwningPtr<Sprite> sprite = p_scene->GetSpriteFromTexturePositionScaleRotation(texture, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, scale_x, scale_y, angle);
std::cout << sprite.Get() << std::endl;
if(!sprite) if(!sprite)
{ {
if(m_pixelput_called) if(m_pixelput_called)

View File

@@ -19,7 +19,8 @@ namespace mlx
void Swap(GPUBuffer& buffer) noexcept; void Swap(GPUBuffer& buffer) noexcept;
[[nodiscard]] MLX_FORCEINLINE void* GetMap() const noexcept { return p_map; } template<typename T = void*>
[[nodiscard]] MLX_FORCEINLINE T GetMap() const noexcept { return reinterpret_cast<T>(p_map); }
[[nodiscard]] MLX_FORCEINLINE VkBuffer Get() const noexcept { return m_buffer; } [[nodiscard]] MLX_FORCEINLINE VkBuffer Get() const noexcept { return m_buffer; }
[[nodiscard]] MLX_FORCEINLINE VmaAllocation GetAllocation() const noexcept { return m_allocation; } [[nodiscard]] MLX_FORCEINLINE VmaAllocation GetAllocation() const noexcept { return m_allocation; }
[[nodiscard]] MLX_FORCEINLINE VkDeviceSize GetSize() const noexcept { return m_size; } [[nodiscard]] MLX_FORCEINLINE VkDeviceSize GetSize() const noexcept { return m_size; }

View File

@@ -98,7 +98,6 @@ namespace mlx
void OpenCPUBuffer(); void OpenCPUBuffer();
private: private:
std::vector<mlx_color> m_cpu_buffer;
std::optional<GPUBuffer> m_staging_buffer; std::optional<GPUBuffer> m_staging_buffer;
bool m_has_been_modified = false; bool m_has_been_modified = false;
}; };

View File

@@ -8,7 +8,7 @@ namespace mlx
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
VmaAllocationCreateInfo alloc_info{}; VmaAllocationCreateInfo alloc_info{};
alloc_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT; alloc_info.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT;
alloc_info.usage = VMA_MEMORY_USAGE_AUTO; alloc_info.usage = VMA_MEMORY_USAGE_AUTO;
if(type == BufferType::Constant) if(type == BufferType::Constant)
@@ -86,6 +86,7 @@ namespace mlx
VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice()); VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice());
kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence); kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence);
kvfDestroyFence(RenderCore::Get().GetDevice(), fence); kvfDestroyFence(RenderCore::Get().GetDevice(), fence);
kvfDestroyCommandBuffer(RenderCore::Get().GetDevice(), cmd);
return true; return true;
} }

View File

@@ -108,6 +108,8 @@ namespace mlx
if(is_single_time_cmd_buffer) if(is_single_time_cmd_buffer)
cmd = kvfCreateCommandBuffer(RenderCore::Get().GetDevice()); cmd = kvfCreateCommandBuffer(RenderCore::Get().GetDevice());
kvfTransitionImageLayout(RenderCore::Get().GetDevice(), m_image, KVF_IMAGE_COLOR, cmd, m_format, m_layout, new_layout, is_single_time_cmd_buffer); kvfTransitionImageLayout(RenderCore::Get().GetDevice(), m_image, KVF_IMAGE_COLOR, cmd, m_format, m_layout, new_layout, is_single_time_cmd_buffer);
if(is_single_time_cmd_buffer)
kvfDestroyCommandBuffer(RenderCore::Get().GetDevice(), cmd);
m_layout = new_layout; m_layout = new_layout;
} }
@@ -140,6 +142,7 @@ namespace mlx
VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice()); VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice());
kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence); kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence);
kvfDestroyFence(RenderCore::Get().GetDevice(), fence); kvfDestroyFence(RenderCore::Get().GetDevice(), fence);
kvfDestroyCommandBuffer(RenderCore::Get().GetDevice(), cmd);
} }
} }
@@ -199,6 +202,7 @@ namespace mlx
VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice()); VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice());
kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence); kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence);
kvfDestroyFence(RenderCore::Get().GetDevice(), fence); kvfDestroyFence(RenderCore::Get().GetDevice(), fence);
kvfDestroyCommandBuffer(RenderCore::Get().GetDevice(), cmd);
staging_buffer.Destroy(); staging_buffer.Destroy();
} }
TransitionLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); TransitionLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
@@ -219,9 +223,9 @@ namespace mlx
if(!m_staging_buffer.has_value()) if(!m_staging_buffer.has_value())
OpenCPUBuffer(); OpenCPUBuffer();
if constexpr(std::endian::native == std::endian::little) if constexpr(std::endian::native == std::endian::little)
m_cpu_buffer[(y * m_width) + x] = ReverseColor(color); m_staging_buffer->GetMap<mlx_color*>()[(y * m_width) + x] = ReverseColor(color);
else else
m_cpu_buffer[(y * m_width) + x] = color; m_staging_buffer->GetMap<mlx_color*>()[(y * m_width) + x] = color;
m_has_been_modified = true; m_has_been_modified = true;
} }
@@ -244,9 +248,9 @@ namespace mlx
moving_y++; moving_y++;
} }
if constexpr(std::endian::native == std::endian::little) if constexpr(std::endian::native == std::endian::little)
m_cpu_buffer[(moving_y * m_width) + moving_x] = ReverseColor(pixels[i]); m_staging_buffer->GetMap<mlx_color*>()[(moving_y * m_width) + moving_x] = ReverseColor(pixels[i]);
else else
m_cpu_buffer[(moving_y * m_width) + moving_x] = pixels[i]; m_staging_buffer->GetMap<mlx_color*>()[(moving_y * m_width) + moving_x] = pixels[i];
} }
m_has_been_modified = true; m_has_been_modified = true;
} }
@@ -259,11 +263,13 @@ namespace mlx
if(!m_staging_buffer.has_value()) if(!m_staging_buffer.has_value())
OpenCPUBuffer(); OpenCPUBuffer();
if constexpr(std::endian::native == std::endian::little) if constexpr(std::endian::native == std::endian::little)
{
for(std::size_t i = 0; i < len; i++) for(std::size_t i = 0; i < len; i++)
m_cpu_buffer[(y * m_width) + x + i] = ReverseColor(pixels[i]); m_staging_buffer->GetMap<mlx_color*>()[(y * m_width) + x + i] = ReverseColor(pixels[i]);
}
else else
{ {
std::memcpy(&m_cpu_buffer[(y * m_width) + x], pixels, len); std::memcpy(&m_staging_buffer->GetMap<mlx_color*>()[(y * m_width) + x], pixels, len);
} }
m_has_been_modified = true; m_has_been_modified = true;
} }
@@ -276,9 +282,9 @@ namespace mlx
if(!m_staging_buffer.has_value()) if(!m_staging_buffer.has_value())
OpenCPUBuffer(); OpenCPUBuffer();
if constexpr(std::endian::native == std::endian::little) if constexpr(std::endian::native == std::endian::little)
return ReverseColor(m_cpu_buffer[(y * m_width) + x]); return ReverseColor(m_staging_buffer->GetMap<mlx_color*>()[(y * m_width) + x]);
else else
return m_cpu_buffer[(y * m_width) + x]; return m_staging_buffer->GetMap<mlx_color*>()[(y * m_width) + x];
} }
void Texture::GetRegion(int x, int y, int w, int h, mlx_color* dst) noexcept void Texture::GetRegion(int x, int y, int w, int h, mlx_color* dst) noexcept
@@ -298,9 +304,9 @@ namespace mlx
moving_y++; moving_y++;
} }
if constexpr(std::endian::native == std::endian::little) if constexpr(std::endian::native == std::endian::little)
dst[i] = ReverseColor(m_cpu_buffer[(moving_y * m_width) + moving_x]); dst[i] = ReverseColor(m_staging_buffer->GetMap<mlx_color*>()[(moving_y * m_width) + moving_x]);
else else
dst[i] = m_cpu_buffer[(moving_y * m_width) + moving_x]; dst[i] = m_staging_buffer->GetMap<mlx_color*>()[(moving_y * m_width) + moving_x];
} }
} }
@@ -315,7 +321,16 @@ namespace mlx
processed_color.g = static_cast<std::uint8_t>(color.g * 255.f); processed_color.g = static_cast<std::uint8_t>(color.g * 255.f);
processed_color.b = static_cast<std::uint8_t>(color.b * 255.f); processed_color.b = static_cast<std::uint8_t>(color.b * 255.f);
processed_color.a = static_cast<std::uint8_t>(color.a * 255.f); processed_color.a = static_cast<std::uint8_t>(color.a * 255.f);
std::fill(m_cpu_buffer.begin(), m_cpu_buffer.end(), processed_color); if(processed_color.r == 0 && processed_color.g == 0 && processed_color.b == 0)
std::memset(m_staging_buffer->GetMap(), processed_color.a, m_staging_buffer->GetSize());
else
{
for(std::size_t y = 0; y < m_height; y++)
{
for(std::size_t x = 0; x < m_width; x++)
m_staging_buffer->GetMap<mlx_color*>()[y * m_width + x] = processed_color;
}
}
} }
} }
@@ -324,8 +339,6 @@ namespace mlx
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
if(!m_has_been_modified) if(!m_has_been_modified)
return; return;
std::memcpy(m_staging_buffer->GetMap(), m_cpu_buffer.data(), m_cpu_buffer.size() * sizeof(mlx_color));
VkImageLayout old_layout = m_layout; VkImageLayout old_layout = m_layout;
TransitionLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, cmd); TransitionLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, cmd);
kvfCopyBufferToImage(cmd, Image::Get(), m_staging_buffer->Get(), m_staging_buffer->GetOffset(), VK_IMAGE_ASPECT_COLOR_BIT, { m_width, m_height, 1 }); kvfCopyBufferToImage(cmd, Image::Get(), m_staging_buffer->Get(), m_staging_buffer->GetOffset(), VK_IMAGE_ASPECT_COLOR_BIT, { m_width, m_height, 1 });
@@ -360,9 +373,7 @@ namespace mlx
VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice()); VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice());
kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence); kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence);
kvfDestroyFence(RenderCore::Get().GetDevice(), fence); kvfDestroyFence(RenderCore::Get().GetDevice(), fence);
kvfDestroyCommandBuffer(RenderCore::Get().GetDevice(), cmd);
m_cpu_buffer.resize(m_width * m_height);
std::memcpy(m_cpu_buffer.data(), m_staging_buffer->GetMap(), m_cpu_buffer.size() * sizeof(mlx_color));
} }
Texture* StbTextureLoad(const std::filesystem::path& file, int* w, int* h) Texture* StbTextureLoad(const std::filesystem::path& file, int* w, int* h)

View File

@@ -1,4 +1,3 @@
#include "vulkan/vulkan_core.h"
#include <PreCompiled.h> #include <PreCompiled.h>
#define VMA_IMPLEMENTATION #define VMA_IMPLEMENTATION
#ifdef MLX_COMPILER_CLANG #ifdef MLX_COMPILER_CLANG

View File

@@ -214,6 +214,7 @@ namespace mlx
MLX_LOAD_FUNCTION(vkDestroyShaderModule); MLX_LOAD_FUNCTION(vkDestroyShaderModule);
MLX_LOAD_FUNCTION(vkDeviceWaitIdle); MLX_LOAD_FUNCTION(vkDeviceWaitIdle);
MLX_LOAD_FUNCTION(vkEndCommandBuffer); MLX_LOAD_FUNCTION(vkEndCommandBuffer);
MLX_LOAD_FUNCTION(vkFreeCommandBuffers);
MLX_LOAD_FUNCTION(vkGetDeviceQueue); MLX_LOAD_FUNCTION(vkGetDeviceQueue);
MLX_LOAD_FUNCTION(vkGetImageSubresourceLayout); MLX_LOAD_FUNCTION(vkGetImageSubresourceLayout);
MLX_LOAD_FUNCTION(vkQueueSubmit); MLX_LOAD_FUNCTION(vkQueueSubmit);

View File

@@ -102,6 +102,7 @@ namespace mlx
VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice()); VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice());
kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence); kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence);
kvfDestroyFence(RenderCore::Get().GetDevice(), fence); kvfDestroyFence(RenderCore::Get().GetDevice(), fence);
kvfDestroyCommandBuffer(RenderCore::Get().GetDevice(), cmd);
m_resize = false; m_resize = false;
DebugLog("Vulkan: swapchain created"); DebugLog("Vulkan: swapchain created");
} }

29
third_party/kvf.h vendored
View File

@@ -167,12 +167,13 @@ VkFramebuffer kvfCreateFramebuffer(VkDevice device, VkRenderPass renderpass, VkI
VkExtent2D kvfGetFramebufferSize(VkFramebuffer buffer); VkExtent2D kvfGetFramebufferSize(VkFramebuffer buffer);
void kvfDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer); void kvfDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer);
VkCommandBuffer kvfCreateCommandBuffer(VkDevice device); VkCommandBuffer kvfCreateCommandBuffer(VkDevice device); // Uses internal command pool, not thread safe
VkCommandBuffer kvfCreateCommandBufferLeveled(VkDevice device, VkCommandBufferLevel level); VkCommandBuffer kvfCreateCommandBufferLeveled(VkDevice device, VkCommandBufferLevel level); // Same
void kvfBeginCommandBuffer(VkCommandBuffer buffer, VkCommandBufferUsageFlags flags); void kvfBeginCommandBuffer(VkCommandBuffer buffer, VkCommandBufferUsageFlags flags);
void kvfEndCommandBuffer(VkCommandBuffer buffer); void kvfEndCommandBuffer(VkCommandBuffer buffer);
void kvfSubmitCommandBuffer(VkDevice device, VkCommandBuffer buffer, KvfQueueType queue, VkSemaphore signal, VkSemaphore wait, VkFence fence, VkPipelineStageFlags* stages); void kvfSubmitCommandBuffer(VkDevice device, VkCommandBuffer buffer, KvfQueueType queue, VkSemaphore signal, VkSemaphore wait, VkFence fence, VkPipelineStageFlags* stages);
void kvfSubmitSingleTimeCommandBuffer(VkDevice device, VkCommandBuffer buffer, KvfQueueType queue, VkFence fence); void kvfSubmitSingleTimeCommandBuffer(VkDevice device, VkCommandBuffer buffer, KvfQueueType queue, VkFence fence);
void kvfDestroyCommandBuffer(VkDevice device, VkCommandBuffer buffer);
VkAttachmentDescription kvfBuildAttachmentDescription(KvfImageType type, VkFormat format, VkImageLayout initial, VkImageLayout final, bool clear, VkSampleCountFlagBits samples); VkAttachmentDescription kvfBuildAttachmentDescription(KvfImageType type, VkFormat format, VkImageLayout initial, VkImageLayout final, bool clear, VkSampleCountFlagBits samples);
#ifndef KVF_NO_KHR #ifndef KVF_NO_KHR
@@ -313,6 +314,7 @@ void kvfCheckVk(VkResult result);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyShaderModule); KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDestroyShaderModule);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDeviceWaitIdle); KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkDeviceWaitIdle);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkEndCommandBuffer); KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkEndCommandBuffer);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkFreeCommandBuffers);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetDeviceQueue); KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetDeviceQueue);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetImageSubresourceLayout); KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkGetImageSubresourceLayout);
KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkQueueSubmit); KVF_DEFINE_VULKAN_FUNCTION_PROTOTYPE(vkQueueSubmit);
@@ -2416,6 +2418,29 @@ void kvfSubmitSingleTimeCommandBuffer(VkDevice device, VkCommandBuffer buffer, K
kvfWaitForFence(device, fence); kvfWaitForFence(device, fence);
} }
void kvfDestroyCommandBuffer(VkDevice device, VkCommandBuffer buffer)
{
if(buffer == VK_NULL_HANDLE)
return;
KVF_ASSERT(device != VK_NULL_HANDLE);
__KvfDevice* kvf_device = __kvfGetKvfDeviceFromVkDevice(device);
KVF_ASSERT(kvf_device != NULL);
for(size_t i = 0; i < kvf_device->cmd_buffers_size; i++)
{
if(kvf_device->cmd_buffers[i] == buffer)
{
KVF_GET_DEVICE_FUNCTION(vkFreeCommandBuffers)(kvf_device->device, kvf_device->cmd_pool, 1, &buffer);
// Shift the elements to fill the gap
for(size_t j = i; j < kvf_device->cmd_buffers_size - 1; j++)
kvf_device->cmd_buffers[j] = kvf_device->cmd_buffers[j + 1];
kvf_device->cmd_buffers--;
return;
}
}
KVF_ASSERT(false && "could not find command buffer in internal device");
}
VkAttachmentDescription kvfBuildAttachmentDescription(KvfImageType type, VkFormat format, VkImageLayout initial, VkImageLayout final, bool clear, VkSampleCountFlagBits samples) VkAttachmentDescription kvfBuildAttachmentDescription(KvfImageType type, VkFormat format, VkImageLayout initial, VkImageLayout final, bool clear, VkSampleCountFlagBits samples)
{ {
VkAttachmentDescription attachment = {}; VkAttachmentDescription attachment = {};