23 "TextureLoom already initialized (static flag)");
29 "Cannot initialize TextureLoom with null backend");
35 "TextureLoom already initialized");
44 "TextureLoom initialized");
59 "Shutting down TextureLoom...");
62 if (texture && texture->is_initialized()) {
74 "TextureLoom shutdown complete");
83 ImageFormat format,
const void* data, uint32_t mip_levels)
87 "TextureLoom not initialized");
92 auto image = std::make_shared<Core::VKImage>(
101 if (!
image->is_initialized()) {
103 "Failed to initialize VKImage");
113 vk::ImageLayout::eUndefined,
114 vk::ImageLayout::eShaderReadOnlyOptimal,
115 mip_levels, 1, vk::ImageAspectFlagBits::eColor);
116 image->set_current_layout(vk::ImageLayout::eShaderReadOnlyOptimal);
121 "Created 2D texture: {}x{}, format: {}, mips: {}",
122 width,
height, vk::to_string(vk_format), mip_levels);
132 "TextureLoom not initialized");
137 auto image = std::make_shared<Core::VKImage>(
145 if (!
image->is_initialized()) {
147 "Failed to initialize 3D VKImage");
157 vk::ImageLayout::eUndefined,
158 vk::ImageLayout::eShaderReadOnlyOptimal,
159 1, 1, vk::ImageAspectFlagBits::eColor);
160 image->set_current_layout(vk::ImageLayout::eShaderReadOnlyOptimal);
165 "Created 3D texture: {}x{}x{}, format: {}",
171 uint32_t size,
ImageFormat format,
const void* data)
175 "TextureLoom not initialized");
180 auto image = std::make_shared<Core::VKImage>(
181 size, size, 1, vk_format,
188 if (!
image->is_initialized()) {
190 "Failed to initialize cubemap VKImage");
196 size_t total_size = face_size * 6;
201 vk::ImageLayout::eUndefined,
202 vk::ImageLayout::eShaderReadOnlyOptimal,
203 1, 6, vk::ImageAspectFlagBits::eColor);
204 image->set_current_layout(vk::ImageLayout::eShaderReadOnlyOptimal);
209 "Created cubemap: {}x{}, format: {}", size, size, vk::to_string(vk_format));
219 "TextureLoom not initialized");
225 "create_2d_array: layers must be > 0");
230 auto image = std::make_shared<Core::VKImage>(
239 if (!
image->is_initialized()) {
241 "create_2d_array: failed to initialize VKImage ({}x{} layers={})",
252 vk::ImageLayout::eUndefined,
253 vk::ImageLayout::eShaderReadOnlyOptimal,
254 1, layers, vk::ImageAspectFlagBits::eColor);
255 image->set_current_layout(vk::ImageLayout::eShaderReadOnlyOptimal);
260 "Created 2D array texture: {}x{} layers={} format={}",
270 "TextureLoom not initialized");
275 auto image = std::make_shared<Core::VKImage>(
283 if (!
image->is_initialized()) {
285 "Failed to initialize render target VKImage");
291 vk::ImageLayout::eUndefined,
292 vk::ImageLayout::eColorAttachmentOptimal,
293 1, 1, vk::ImageAspectFlagBits::eColor);
294 image->set_current_layout(vk::ImageLayout::eColorAttachmentOptimal);
298 "Created render target: {}x{}, format: {}",
304 uint32_t
width, uint32_t
height,
bool with_stencil)
308 "TextureLoom not initialized");
312 vk::Format vk_format = with_stencil
313 ? vk::Format::eD24UnormS8Uint
314 : vk::Format::eD32Sfloat;
316 auto image = std::make_shared<Core::VKImage>(
324 if (!
image->is_initialized()) {
326 "Failed to initialize depth buffer VKImage");
330 vk::ImageAspectFlags aspect = with_stencil
331 ? (vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil)
332 : vk::ImageAspectFlagBits::eDepth;
336 vk::ImageLayout::eUndefined,
337 vk::ImageLayout::eDepthStencilAttachmentOptimal,
339 image->set_current_layout(vk::ImageLayout::eDepthStencilAttachmentOptimal);
343 "Created depth buffer: {}x{}, format: {}, stencil: {}",
344 width,
height, vk::to_string(vk_format), with_stencil);
353 "TextureLoom not initialized");
358 auto image = std::make_shared<Core::VKImage>(
366 if (!
image->is_initialized()) {
368 "Failed to initialize storage image VKImage");
374 vk::ImageLayout::eUndefined,
375 vk::ImageLayout::eGeneral,
376 1, 1, vk::ImageAspectFlagBits::eColor);
377 image->set_current_layout(vk::ImageLayout::eGeneral);
381 "Created storage image: {}x{}, format: {}",
394 "TextureLoom not initialized");
404 if (access->byte_count != expected) {
405 error<std::invalid_argument>(
408 std::source_location::current(),
409 "create_2d: byte count mismatch — {} bytes supplied, "
410 "{}x{} format {} requires {}",
411 access->byte_count,
width,
height,
static_cast<int>(format), expected);
422 const std::shared_ptr<Core::VKImage>&
image,
const void* data,
size_t size)
426 "Invalid parameters for upload_data");
434 const std::shared_ptr<Core::VKImage>&
image,
437 const std::shared_ptr<Buffers::VKBuffer>& staging,
442 "Invalid parameters for upload_data_streaming");
450 const std::shared_ptr<Core::VKImage>&
image,
453 const std::shared_ptr<Buffers::VKBuffer>& staging,
458 "download_data(staging): invalid parameters");
466 const std::shared_ptr<Core::VKImage>&
image,
467 vk::ImageLayout old_layout,
468 vk::ImageLayout new_layout,
470 uint32_t array_layers,
471 vk::ImageAspectFlags aspect_mask)
475 "Invalid parameters for transition_vk_image_layout");
483 mip_levels, array_layers, aspect_mask);
485 image->set_current_layout(new_layout);
496 "TextureLoom not initialized");
538 hash ^= std::hash<int> {}(
static_cast<int>(config.
mag_filter)) << 0;
539 hash ^= std::hash<int> {}(
static_cast<int>(config.
min_filter)) << 4;
540 hash ^= std::hash<int> {}(
static_cast<int>(config.
address_mode_u)) << 8;
541 hash ^= std::hash<int> {}(
static_cast<int>(config.
address_mode_v)) << 12;
542 hash ^= std::hash<int> {}(
static_cast<int>(config.
address_mode_w)) << 16;
556 return vk::Format::eR8Unorm;
558 return vk::Format::eR8G8Unorm;
560 return vk::Format::eR8G8B8Unorm;
562 return vk::Format::eR8G8B8A8Unorm;
565 return vk::Format::eB8G8R8A8Unorm;
567 return vk::Format::eB8G8R8A8Srgb;
568 return vk::Format::eR8G8B8A8Srgb;
570 return vk::Format::eR16Unorm;
572 return vk::Format::eR16G16Unorm;
574 return vk::Format::eR16G16B16A16Unorm;
576 return vk::Format::eR16Sfloat;
578 return vk::Format::eR16G16Sfloat;
580 return vk::Format::eR16G16B16A16Sfloat;
582 return vk::Format::eR32Sfloat;
584 return vk::Format::eR32G32Sfloat;
586 return vk::Format::eR32G32B32A32Sfloat;
588 return vk::Format::eD16Unorm;
590 return vk::Format::eX8D24UnormPack32;
592 return vk::Format::eD32Sfloat;
594 return vk::Format::eD24UnormS8Uint;
596 return vk::Format::eR8G8B8A8Unorm;
645 case vk::Format::eR8Unorm:
647 case vk::Format::eR8G8Unorm:
649 case vk::Format::eR8G8B8Unorm:
651 case vk::Format::eR8G8B8A8Unorm:
653 case vk::Format::eR8G8B8A8Srgb:
655 case vk::Format::eB8G8R8A8Unorm:
658 case vk::Format::eR16Unorm:
660 case vk::Format::eR16G16Unorm:
662 case vk::Format::eR16G16B16A16Unorm:
665 case vk::Format::eR16Sfloat:
667 case vk::Format::eR16G16Sfloat:
669 case vk::Format::eR16G16B16A16Sfloat:
672 case vk::Format::eR32Sfloat:
674 case vk::Format::eR32G32Sfloat:
676 case vk::Format::eR32G32B32A32Sfloat:
679 case vk::Format::eD16Unorm:
681 case vk::Format::eX8D24UnormPack32:
683 case vk::Format::eD32Sfloat:
685 case vk::Format::eD24UnormS8Uint:
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
void cleanup_image(const std::shared_ptr< VKImage > &image)
Cleanup a VKImage (destroy view, image, and free memory)
void download_image_data(std::shared_ptr< VKImage > image, void *data, size_t size, const std::shared_ptr< Buffers::VKBuffer > &staging=nullptr, bool deferred=false, vk::ImageLayout restore_layout=vk::ImageLayout::eShaderReadOnlyOptimal, vk::PipelineStageFlags restore_stage=vk::PipelineStageFlagBits::eFragmentShader)
Download image data to a host pointer.
void initialize_image(const std::shared_ptr< VKImage > &image)
Initialize a VKImage (allocate VkImage, memory, and create image view)
vk::Sampler create_sampler(vk::Filter filter=vk::Filter::eLinear, vk::SamplerAddressMode address_mode=vk::SamplerAddressMode::eRepeat, float max_anisotropy=0.0F)
Create sampler.
void transition_image_layout(vk::Image image, vk::ImageLayout old_layout, vk::ImageLayout new_layout, uint32_t mip_levels=1, uint32_t array_layers=1, vk::ImageAspectFlags aspect_flags=vk::ImageAspectFlagBits::eColor)
Transition image layout using a pipeline barrier.
void upload_image_data(std::shared_ptr< VKImage > image, const void *data, size_t size)
Upload data to an image (creates staging buffer internally)
@ RENDER_TARGET
Color attachment for rendering.
@ STORAGE
Storage image (compute shader read/write)
@ TEXTURE_2D
Sampled texture (shader read)
@ DEPTH_STENCIL
Depth/stencil attachment.
std::shared_ptr< Core::VKImage > create_render_target(uint32_t width, uint32_t height, ImageFormat format=ImageFormat::RGBA8)
Create a render target (color attachment)
std::shared_ptr< Core::VKImage > create_depth_buffer(uint32_t width, uint32_t height, bool with_stencil=false)
Create a depth buffer.
static size_t get_bytes_per_pixel(ImageFormat format)
Get bytes per pixel for a format.
static uint32_t get_channel_count(ImageFormat format)
Get the number of color channels for a given format.
vk::Sampler get_nearest_sampler()
Get a default nearest sampler (for pixel-perfect sampling)
static size_t calculate_image_size(uint32_t width, uint32_t height, uint32_t depth, ImageFormat format)
Calculate image data size.
static vk::Format to_vulkan_format(ImageFormat format)
Convert Portal ImageFormat to Vulkan format.
std::shared_ptr< Core::VKImage > create_storage_image(uint32_t width, uint32_t height, ImageFormat format=ImageFormat::RGBA8)
Create a storage image (compute shader read/write)
void upload_data(const std::shared_ptr< Core::VKImage > &image, const void *data, size_t size)
Upload pixel data to an existing texture.
void download_data(const std::shared_ptr< Core::VKImage > &image, void *data, size_t size, const std::shared_ptr< Buffers::VKBuffer > &staging, bool deferred=false)
Download pixel data from a VKImage, reusing a caller-supplied persistent staging buffer.
bool initialize(const std::shared_ptr< Core::VulkanBackend > &backend)
Initialize texture manager.
vk::Sampler get_or_create_sampler(const SamplerConfig &config)
Get or create a sampler with the given configuration.
void transition_layout(const std::shared_ptr< Core::VKImage > &image, vk::ImageLayout old_layout, vk::ImageLayout new_layout, uint32_t mip_levels=1, uint32_t array_layers=1, vk::ImageAspectFlags aspect_mask=vk::ImageAspectFlagBits::eColor)
Transition a VKImage to a new Vulkan layout via an immediate submission.
std::shared_ptr< Core::VKImage > create_2d_array(uint32_t width, uint32_t height, uint32_t layers, ImageFormat format=ImageFormat::RGBA8, const void *data=nullptr)
Create a 2D texture array.
std::shared_ptr< Core::VulkanBackend > m_backend
std::shared_ptr< Core::VKImage > create_3d(uint32_t width, uint32_t height, uint32_t depth, ImageFormat format=ImageFormat::RGBA8, const void *data=nullptr)
Create a 3D texture (volumetric)
vk::Sampler create_sampler(const SamplerConfig &config)
void shutdown()
Shutdown and cleanup all textures.
static std::optional< ImageFormat > from_vulkan_format(vk::Format vk_format)
Convert Vulkan format to Portal ImageFormat.
Core::BackendResourceManager * m_resource_manager
std::shared_ptr< Core::VKImage > create_2d(uint32_t width, uint32_t height, ImageFormat format=ImageFormat::RGBA8, const void *data=nullptr, uint32_t mip_levels=1)
Create a 2D texture.
static size_t hash_sampler_config(const SamplerConfig &config)
std::vector< std::shared_ptr< Core::VKImage > > m_textures
static bool s_initialized
std::shared_ptr< Core::VKImage > create_cubemap(uint32_t size, ImageFormat format=ImageFormat::RGBA8, const void *data=nullptr)
Create a cubemap texture.
vk::Sampler get_default_sampler()
Get a default linear sampler (for convenience)
std::unordered_map< size_t, vk::Sampler > m_sampler_cache
bool is_initialized() const
Check if manager is initialized.
@ ImageProcessing
Image processing tasks (filters, transformations)
@ Portal
High-level user-facing API layer.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
@ VOLUMETRIC_3D
3D volumetric data
@ IMAGE_COLOR
2D RGB/RGBA image
@ IMAGE_2D
2D image (grayscale or single channel)
std::optional< TextureAccess > as_texture_access(const DataVariant &variant)
Extract a TextureAccess from a DataVariant.
@ CLAMP_TO_EDGE
Clamp to edge color.
@ NEAREST
Nearest neighbor (pixelated)
ImageFormat
User-friendly image format enum.
@ DEPTH24_STENCIL8
24-bit depth + 8-bit stencil
@ RGB8
Three channel 8-bit.
@ DEPTH32F
32-bit float depth
@ RG16
Two channel 16-bit unsigned integer.
@ BGRA8
8-bit BGRA unsigned normalized
@ RGBA16
Four channel 16-bit unsigned integer.
@ RGBA32F
Four channel 32-bit float.
@ R16F
Single channel 16-bit float.
@ R16
Single channel 16-bit unsigned integer.
@ RGBA16F
Four channel 16-bit float.
@ RGBA8
Four channel 8-bit.
@ RG32F
Two channel 32-bit float.
@ R32F
Single channel 32-bit float.
@ R8
Single channel 8-bit.
@ RG16F
Two channel 16-bit float.
@ RGBA8_SRGB
Four channel 8-bit sRGB.
@ BGRA8_SRGB
8-bit BGRA sRGB
AddressMode address_mode_v
AddressMode address_mode_w
AddressMode address_mode_u