Skip to content

Commit

Permalink
fixing bug in texture clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Dec 16, 2024
1 parent 335c797 commit 30328b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
3 changes: 2 additions & 1 deletion runtime/Includes/Renderer/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace mlx
void CreateImageView(VkImageViewType type, VkImageAspectFlags aspectFlags, int layer_count = 1) noexcept;
void CreateSampler() noexcept;
void TransitionLayout(VkImageLayout new_layout, VkCommandBuffer cmd = VK_NULL_HANDLE);
void Clear(VkCommandBuffer cmd, Vec4f color);
virtual void Clear(VkCommandBuffer cmd, Vec4f color);

void DestroySampler() noexcept;
void DestroyImageView() noexcept;
Expand Down Expand Up @@ -88,6 +88,7 @@ namespace mlx
void SetLinearRegion(int x, int y, std::size_t len, int* pixels) noexcept;
int GetPixel(int x, int y) noexcept;
void GetRegion(int x, int y, int w, int h, int* dst) noexcept;
void Clear(VkCommandBuffer cmd, Vec4f color) override;

void Update(VkCommandBuffer cmd);

Expand Down
17 changes: 7 additions & 10 deletions runtime/Sources/Graphics/PutPixelManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,32 @@ namespace mlx
}
is_newlayer = true;

bool adjusment = false;
if(m_current_texture_index >= m_textures.size())
{
#ifdef DEBUG
m_textures.push_back(std::make_unique<Texture>(CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(draw_layer)));
m_textures.push_back(std::make_unique<Texture>(CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(m_current_texture_index)));
#else
m_textures.push_back(std::make_unique<Texture>(CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{}));
#endif
m_current_texture_index++;
adjusment = true;
}
try
{
m_placements[draw_layer] = m_textures.at(m_current_texture_index - adjusment).get();
m_textures.at(m_current_texture_index - adjusment)->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
return m_textures.at(m_current_texture_index - adjusment).get();
m_placements[draw_layer] = m_textures.at(m_current_texture_index).get();
m_textures.at(m_current_texture_index)->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
NonOwningPtr<Texture> texture = m_textures.at(m_current_texture_index).get();
m_current_texture_index++;
return texture;
}
catch(...)
{
Error("PutPixelManager: invalid texture index; % is not in range of 0-% (internal mlx issue, please report to devs)", m_current_texture_index - adjusment, m_textures.size());
Error("PutPixelManager: invalid texture index; % is not in range of 0-% (internal mlx issue, please report to devs)", m_current_texture_index, m_textures.size());
return nullptr;
}
}

void PutPixelManager::ResetRenderData()
{
m_placements.clear();
for(auto& texture : m_textures)
texture->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
m_current_texture_index = 0;
}
}
23 changes: 15 additions & 8 deletions runtime/Sources/Renderer/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@

namespace mlx
{
unsigned char reverse(unsigned char b)
{
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}

void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, bool is_multisampled, [[maybe_unused]] std::string_view debug_name)
{
MLX_PROFILE_FUNCTION();
Expand Down Expand Up @@ -293,6 +285,21 @@ namespace mlx
}
}

void Texture::Clear(VkCommandBuffer cmd, Vec4f color)
{
MLX_PROFILE_FUNCTION();
Image::Clear(cmd, std::move(color));
if(m_staging_buffer.has_value())
{
std::uint8_t color_bytes[4];
color_bytes[0] = static_cast<std::uint8_t>(color.r * 255.f);
color_bytes[1] = static_cast<std::uint8_t>(color.g * 255.f);
color_bytes[2] = static_cast<std::uint8_t>(color.b * 255.f);
color_bytes[3] = static_cast<std::uint8_t>(color.a * 255.f);
std::fill(m_cpu_buffer.begin(), m_cpu_buffer.end(), *reinterpret_cast<int*>(color_bytes));
}
}

void Texture::Update(VkCommandBuffer cmd)
{
MLX_PROFILE_FUNCTION();
Expand Down

0 comments on commit 30328b1

Please sign in to comment.