mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 14:43:34 +00:00
adding multiple mapping support on textures
This commit is contained in:
3
Makefile
3
Makefile
@@ -6,7 +6,7 @@
|
||||
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/10/04 16:43:41 by maldavid #+# #+# #
|
||||
# Updated: 2023/04/21 14:13:15 by maldavid ### ########.fr #
|
||||
# Updated: 2023/04/22 20:28:48 by maldavid ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@@ -16,7 +16,6 @@ SRCS = $(wildcard $(addsuffix /*.cpp, ./src/core))
|
||||
SRCS += $(wildcard $(addsuffix /*.cpp, ./src/platform))
|
||||
SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer))
|
||||
SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer/**))
|
||||
SRCS += $(wildcard $(addsuffix /*.cpp, ./src/utils/**))
|
||||
|
||||
OBJS = $(SRCS:.cpp=.o)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/19 13:11:27 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/22 19:56:48 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -136,6 +136,9 @@ int mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*f)(), void* p
|
||||
* @param y Y coordinate
|
||||
* @param color Color of the pixel (coded on 3 bytes in an int, 0x00RRGGBB)
|
||||
*
|
||||
* Note : If your're reading pixel colors from an image, don't forget to shift them
|
||||
* one byte to the right as image pixels are encoded as 0xRRGGBBAA and pixel put takes 0x00RRGGBB.
|
||||
*
|
||||
* @return (int) Always return 0, made this to copy the behaviour of the original MLX
|
||||
*/
|
||||
int mlx_pixel_put(void* mlx, void* win, int x, int y, int color);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/21 19:24:35 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/22 19:39:59 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -37,14 +37,12 @@ namespace mlx::core
|
||||
void* Application::newTexture(int w, int h)
|
||||
{
|
||||
_textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM);
|
||||
_textures.front().openCPUmap();
|
||||
return &_textures.front();
|
||||
}
|
||||
|
||||
void* Application::newStbTexture(char* file, int* w, int* h)
|
||||
{
|
||||
_textures.emplace_front(stbTextureLoad(file, w, h));
|
||||
_textures.front().openCPUmap();
|
||||
return &_textures.front();
|
||||
}
|
||||
|
||||
@@ -55,7 +53,7 @@ namespace mlx::core
|
||||
*bits_per_pixel = 32;
|
||||
*size_line = img->getWidth() * 4;
|
||||
*endian = endianness;
|
||||
return static_cast<char*>(img->getMap());
|
||||
return static_cast<char*>(img->openCPUmap());
|
||||
}
|
||||
|
||||
void Application::destroyTexture(void* ptr)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 17:48:06 by maldavid #+# #+# */
|
||||
/* Updated: 2022/12/18 17:47:51 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 13:07:56 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -30,11 +30,11 @@ namespace mlx::core::error
|
||||
|
||||
switch(kind)
|
||||
{
|
||||
case e_kind::message: std::cout << "[MacroLibX] Message : " << buffer << std::endl; break;
|
||||
case e_kind::warning: std::cout << "[MacroLibX] Warning : " << buffer << std::endl; break;
|
||||
case e_kind::error: std::cerr << "[MacroLibX] Error : " << buffer << std::endl; break;
|
||||
case e_kind::message: std::cout << "\033[1;34m[MacroLibX] Message : \033[1;0m" << buffer << std::endl; break;
|
||||
case e_kind::warning: std::cout << "\033[1;35m[MacroLibX] Warning : \033[1;0m" << buffer << std::endl; break;
|
||||
case e_kind::error: std::cerr << "\033[1;31m[MacroLibX] Error : \033[1;0m" << buffer << std::endl; break;
|
||||
case e_kind::fatal_error:
|
||||
std::cerr << "[MacroLibX] Fatal Error : " << buffer << std::endl;
|
||||
std::cerr << "\033[1;31m[MacroLibX] Fatal Error : \033[1;0m" << buffer << std::endl;
|
||||
std::exit(EXIT_FAILURE);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/25 15:24:40 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 12:37:32 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -81,7 +81,7 @@ namespace mlx
|
||||
VkMemoryAllocateInfo allocInfo{};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||
allocInfo.allocationSize = memRequirements.size;
|
||||
allocInfo.memoryTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, properties);
|
||||
allocInfo.memoryTypeIndex = *RCore::findMemoryType(memRequirements.memoryTypeBits, properties);
|
||||
|
||||
if(vkAllocateMemory(device, &allocInfo, nullptr, &_memory) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate buffer memory");
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */
|
||||
/* Updated: 2023/03/31 15:29:01 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/22 19:51:47 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/25 15:05:05 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/25 15:36:41 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/22 19:51:54 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:45:52 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/23 18:54:12 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/22 19:51:56 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:45:29 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/25 15:37:09 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/22 19:51:59 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:27:38 by maldavid #+# #+# */
|
||||
/* Updated: 2023/01/25 15:37:00 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/22 19:52:05 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/06 18:26:06 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/21 13:24:56 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 15:19:08 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -105,5 +105,6 @@ namespace mlx
|
||||
void CmdBuffer::destroy() noexcept
|
||||
{
|
||||
_fence.destroy();
|
||||
_cmd_buffer = VK_NULL_HANDLE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/12/17 23:33:34 by maldavid #+# #+# */
|
||||
/* Updated: 2023/03/31 12:22:34 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 19:09:21 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -23,13 +23,15 @@
|
||||
#include "render_core.h"
|
||||
#include <mutex>
|
||||
|
||||
#ifdef DEBUG
|
||||
#warning "MLX is being compiled in debug mode, this activates Vulkan's validation layers and debug messages and may impact rendering performances"
|
||||
#endif
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
std::mutex mutex;
|
||||
|
||||
namespace RCore
|
||||
{
|
||||
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
|
||||
std::optional<uint32_t> findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties, bool error)
|
||||
{
|
||||
VkPhysicalDeviceMemoryProperties memProperties;
|
||||
vkGetPhysicalDeviceMemoryProperties(Render_Core::get().getDevice().getPhysicalDevice(), &memProperties);
|
||||
@@ -37,11 +39,11 @@ namespace mlx
|
||||
for(uint32_t i = 0; i < memProperties.memoryTypeCount; i++)
|
||||
{
|
||||
if((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties)
|
||||
return i;
|
||||
return i;
|
||||
}
|
||||
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type");
|
||||
return -1; // just to avoid warning
|
||||
if(error)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type");
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,12 +62,10 @@ namespace mlx
|
||||
|
||||
void Render_Core::destroy()
|
||||
{
|
||||
std::unique_lock<std::mutex> watchdog(mutex, std::try_to_lock);
|
||||
|
||||
if(!_is_init)
|
||||
return;
|
||||
|
||||
vkDeviceWaitIdle(_device());
|
||||
vkDeviceWaitIdle(_device());
|
||||
|
||||
_device.destroy();
|
||||
_layers.destroy();
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/08 19:16:32 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/21 14:13:32 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 12:31:42 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#define __MLX_RENDER_CORE__
|
||||
|
||||
#include <volk.h>
|
||||
#include <optional>
|
||||
|
||||
#include "vk_queues.h"
|
||||
#include "vk_device.h"
|
||||
@@ -27,10 +28,10 @@ namespace mlx
|
||||
{
|
||||
namespace RCore
|
||||
{
|
||||
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
|
||||
std::optional<uint32_t> findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties, bool error = true);
|
||||
}
|
||||
|
||||
#ifndef DEBUG
|
||||
#ifdef DEBUG
|
||||
constexpr const bool enableValidationLayers = true;
|
||||
#else
|
||||
constexpr const bool enableValidationLayers = false;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */
|
||||
/* Updated: 2023/03/31 17:54:38 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/22 19:52:08 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/21 19:31:15 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 15:30:54 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -14,20 +14,39 @@
|
||||
#include <renderer/images/texture.h>
|
||||
#include <renderer/buffers/vk_buffer.h>
|
||||
#include <renderer/renderer.h>
|
||||
#include <iostream>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void Texture::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format)
|
||||
void Texture::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, bool enable_mapping)
|
||||
{
|
||||
Image::create(width, height, format,
|
||||
VK_IMAGE_TILING_OPTIMAL,
|
||||
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
||||
);
|
||||
if(enable_mapping)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Texture : creating CPU mappable texture");
|
||||
#endif
|
||||
Image::create(width, height, format, VK_IMAGE_TILING_LINEAR,
|
||||
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
{
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Texture : creating non CPU mappable texture");
|
||||
#endif
|
||||
Image::create(width, height, format, VK_IMAGE_TILING_OPTIMAL,
|
||||
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
{ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT }
|
||||
);
|
||||
}
|
||||
|
||||
Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
Image::createSampler();
|
||||
@@ -52,28 +71,49 @@ namespace mlx
|
||||
Image::copyFromBuffer(staging_buffer);
|
||||
staging_buffer.destroy();
|
||||
}
|
||||
_cpu_map = nullptr;
|
||||
_cpu_map_adress = nullptr;
|
||||
}
|
||||
|
||||
void* Texture::openCPUmap()
|
||||
{
|
||||
if(_cpu_map_adress != nullptr)
|
||||
return _cpu_map_adress;
|
||||
_cpu_map = std::make_shared<Buffer>();
|
||||
_cpu_map->create(Buffer::kind::dynamic, sizeof(uint32_t) * (getWidth() * getHeight()), VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
|
||||
Image::copyToBuffer(*_cpu_map);
|
||||
if(!_cpu_map->isMapped())
|
||||
_cpu_map->mapMem(&_cpu_map_adress);
|
||||
if(_cpu_map_adress == nullptr)
|
||||
core::error::report(e_kind::fatal_error, "Texture : CPU memory mapping failed");
|
||||
return _cpu_map_adress;
|
||||
if(_cpu_map != nullptr)
|
||||
return _cpu_map;
|
||||
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Texture : enabling CPU mapping");
|
||||
#endif
|
||||
|
||||
Buffer staging_buffer;
|
||||
std::size_t size = getWidth() * getHeight() * formatSize(getFormat());
|
||||
staging_buffer.create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, nullptr);
|
||||
Image::copyToBuffer(staging_buffer);
|
||||
|
||||
this->destroy();
|
||||
this->create(nullptr, getWidth(), getHeight(), getFormat(), true);
|
||||
|
||||
Image::copyFromBuffer(staging_buffer);
|
||||
staging_buffer.destroy();
|
||||
|
||||
if(vkMapMemory(Render_Core::get().getDevice().get(), getDeviceMemory(), 0, VK_WHOLE_SIZE, 0, &_cpu_map) != VK_SUCCESS)
|
||||
{
|
||||
_buf_map.emplace();
|
||||
_buf_map->create(Buffer::kind::dynamic, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
|
||||
Image::copyToBuffer(*_buf_map);
|
||||
_buf_map->mapMem(&_cpu_map);
|
||||
#ifdef DEBUG
|
||||
core::error::report(e_kind::message, "Texture : mapped CPU memory using staging buffer");
|
||||
#endif
|
||||
}
|
||||
#ifdef DEBUG
|
||||
else
|
||||
core::error::report(e_kind::message, "Texture : mapped CPU memory using direct memory mapping");
|
||||
#endif
|
||||
return _cpu_map;
|
||||
}
|
||||
|
||||
void Texture::render(Renderer& renderer, int x, int y)
|
||||
{
|
||||
if(_cpu_map_adress != nullptr)
|
||||
Image::copyFromBuffer(*_cpu_map);
|
||||
if(_buf_map.has_value())
|
||||
Image::copyFromBuffer(*_buf_map);
|
||||
auto cmd = renderer.getActiveCmdBuffer().get();
|
||||
_vbo.bind(renderer);
|
||||
_ibo.bind(renderer);
|
||||
@@ -85,8 +125,8 @@ namespace mlx
|
||||
void Texture::destroy() noexcept
|
||||
{
|
||||
Image::destroy();
|
||||
if(_cpu_map != nullptr)
|
||||
_cpu_map->destroy();
|
||||
if(_buf_map.has_value())
|
||||
_buf_map->destroy();
|
||||
_vbo.destroy();
|
||||
_ibo.destroy();
|
||||
}
|
||||
@@ -95,7 +135,6 @@ namespace mlx
|
||||
{
|
||||
Texture texture;
|
||||
int channels;
|
||||
VkFormat format;
|
||||
uint8_t* data = nullptr;
|
||||
std::string filename = file.string();
|
||||
|
||||
@@ -104,8 +143,7 @@ namespace mlx
|
||||
if(stbi_is_hdr(filename.c_str()))
|
||||
core::error::report(e_kind::fatal_error, "Texture : unsupported image format '%s'", filename.c_str());
|
||||
data = stbi_load(filename.c_str(), w, h, &channels, 4);
|
||||
format = VK_FORMAT_R8G8B8A8_UNORM;
|
||||
texture.create(data, *w, *h, format);
|
||||
texture.create(data, *w, *h, VK_FORMAT_R8G8B8A8_UNORM);
|
||||
stbi_image_free(data);
|
||||
return texture;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/21 19:05:34 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 15:23:44 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -29,12 +29,11 @@ namespace mlx
|
||||
public:
|
||||
Texture() = default;
|
||||
|
||||
void create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format);
|
||||
void create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, bool enable_mapping = false);
|
||||
void render(class Renderer& renderer, int x, int y);
|
||||
void destroy() noexcept override;
|
||||
|
||||
void* openCPUmap();
|
||||
inline void* getMap() const noexcept { return _cpu_map_adress; }
|
||||
|
||||
inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
|
||||
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
|
||||
@@ -48,8 +47,8 @@ namespace mlx
|
||||
C_VBO _vbo;
|
||||
C_IBO _ibo;
|
||||
DescriptorSet _set;
|
||||
std::shared_ptr<Buffer> _cpu_map;
|
||||
void* _cpu_map_adress;
|
||||
std::optional<Buffer> _buf_map = std::nullopt;
|
||||
void* _cpu_map = nullptr;
|
||||
bool _has_been_updated = false;
|
||||
};
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/11 12:30:28 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 12:55:22 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace mlx
|
||||
Image::create(width, height, format,
|
||||
VK_IMAGE_TILING_OPTIMAL,
|
||||
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
||||
{ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT }
|
||||
);
|
||||
|
||||
Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/25 11:59:07 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/21 13:23:52 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 14:59:41 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -18,11 +18,12 @@
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void Image::create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties)
|
||||
void Image::create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, std::vector<VkMemoryPropertyFlags> properties)
|
||||
{
|
||||
_width = width;
|
||||
_height = height;
|
||||
_format = format;
|
||||
_tiling = tiling;
|
||||
|
||||
VkImageCreateInfo imageInfo{};
|
||||
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||
@@ -45,10 +46,19 @@ namespace mlx
|
||||
VkMemoryRequirements memRequirements;
|
||||
vkGetImageMemoryRequirements(Render_Core::get().getDevice().get(), _image, &memRequirements);
|
||||
|
||||
std::optional<uint32_t> memTypeIndex;
|
||||
for(auto prop : properties)
|
||||
{
|
||||
memTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, prop, false);
|
||||
if(memTypeIndex.has_value())
|
||||
break;
|
||||
}
|
||||
if(!memTypeIndex.has_value())
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to find suitable memory type for an image");
|
||||
VkMemoryAllocateInfo allocInfo{};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||
allocInfo.allocationSize = memRequirements.size;
|
||||
allocInfo.memoryTypeIndex = RCore::findMemoryType(memRequirements.memoryTypeBits, properties);
|
||||
allocInfo.memoryTypeIndex = *memTypeIndex;
|
||||
|
||||
if(vkAllocateMemory(Render_Core::get().getDevice().get(), &allocInfo, nullptr, &_memory) != VK_SUCCESS)
|
||||
core::error::report(e_kind::fatal_error, "Vulkan : failed to allocate memory for an image");
|
||||
@@ -123,12 +133,8 @@ namespace mlx
|
||||
region.imageSubresource.mipLevel = 0;
|
||||
region.imageSubresource.baseArrayLayer = 0;
|
||||
region.imageSubresource.layerCount = 1;
|
||||
region.imageOffset = {0, 0, 0};
|
||||
region.imageExtent = {
|
||||
_width,
|
||||
_height,
|
||||
1
|
||||
};
|
||||
region.imageOffset = { 0, 0, 0 };
|
||||
region.imageExtent = { _width, _height, 1 };
|
||||
|
||||
vkCmdCopyBufferToImage(_transfer_cmd.get(), buffer.get(), _image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
||||
|
||||
@@ -179,12 +185,9 @@ namespace mlx
|
||||
region.imageSubresource.mipLevel = 0;
|
||||
region.imageSubresource.baseArrayLayer = 0;
|
||||
region.imageSubresource.layerCount = 1;
|
||||
region.imageOffset = {0, 0, 0};
|
||||
region.imageExtent = {
|
||||
_width,
|
||||
_height,
|
||||
1
|
||||
};
|
||||
region.imageOffset = { 0, 0, 0 };
|
||||
region.imageExtent = { _width, _height, 1 };
|
||||
|
||||
vkCmdCopyImageToBuffer(_transfer_cmd.get(), _image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, buffer.get(), 1, ®ion);
|
||||
|
||||
VkImageMemoryBarrier use_barrier = {};
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/25 11:54:21 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/21 10:58:02 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 14:17:11 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace mlx
|
||||
public:
|
||||
Image() = default;
|
||||
|
||||
void create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties);
|
||||
void create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, std::vector<VkMemoryPropertyFlags> properties);
|
||||
void createImageView(VkImageViewType type, VkImageAspectFlags aspectFlags) noexcept;
|
||||
void createSampler() noexcept;
|
||||
void copyFromBuffer(class Buffer& buffer);
|
||||
@@ -40,6 +40,7 @@ namespace mlx
|
||||
inline VkDeviceMemory getDeviceMemory() noexcept { return _memory; }
|
||||
inline VkImageView getImageView() noexcept { return _image_view; }
|
||||
inline VkFormat getFormat() noexcept { return _format; }
|
||||
inline VkImageTiling getTiling() noexcept { return _tiling; }
|
||||
inline VkSampler getSampler() noexcept { return _sampler; }
|
||||
inline uint32_t getWidth() const noexcept { return _width; }
|
||||
inline uint32_t getHeight() const noexcept { return _height; }
|
||||
@@ -54,6 +55,7 @@ namespace mlx
|
||||
VkImageView _image_view = VK_NULL_HANDLE;
|
||||
VkSampler _sampler = VK_NULL_HANDLE;
|
||||
VkFormat _format;
|
||||
VkImageTiling _tiling;
|
||||
uint32_t _width = 0;
|
||||
uint32_t _height = 0;
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/31 15:14:50 by maldavid #+# #+# */
|
||||
/* Updated: 2023/04/21 14:51:47 by maldavid ### ########.fr */
|
||||
/* Updated: 2023/04/23 15:24:37 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user