Crafter.Graphics/Crafter.Graphics-VulkanTexture.cppm

160 lines
7 KiB
Text
Raw Normal View History

2025-05-07 19:21:51 +02:00
/*
Crafter®.Graphics
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2025-04-27 23:20:52 +02:00
module;
#include <cstdint>
#include <vulkan/vulkan.h>
#include <cstring>
#include <iostream>
export module Crafter.Graphics:VulkanTexture;
2025-05-03 06:51:33 +02:00
import Crafter.Component;
2025-04-27 23:20:52 +02:00
import :VulkanDevice;
2025-05-03 06:51:33 +02:00
import :VulkanBuffer;
2025-04-27 23:20:52 +02:00
namespace Crafter {
2025-05-03 06:51:33 +02:00
export template <typename PixelType>
2025-05-08 01:35:54 +02:00
class VulkanTexture {
2025-04-27 23:20:52 +02:00
public:
2025-05-03 06:51:33 +02:00
VkImage image;
VkDeviceMemory imageMemory;
Buffer<PixelType> buffer;
VkImageView imageView;
VulkanTexture(std::uint32_t width, std::uint32_t height, const PixelType* pixels, VkCommandBuffer cmd) : buffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, width*height) {
memcpy(buffer.value, pixels, width*height*sizeof(PixelType));
2025-04-27 23:20:52 +02:00
VkImageCreateInfo imageInfo{};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = width;
imageInfo.extent.height = height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
2025-05-03 06:51:33 +02:00
imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
2025-04-27 23:20:52 +02:00
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
2025-05-03 06:51:33 +02:00
imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
2025-04-27 23:20:52 +02:00
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
2025-05-03 02:34:38 +02:00
VulkanDevice::CHECK_VK_RESULT(vkCreateImage(VulkanDevice::device, &imageInfo, nullptr, &image));
2025-04-27 23:20:52 +02:00
VkMemoryRequirements memRequirements;
2025-05-03 02:34:38 +02:00
vkGetImageMemoryRequirements(VulkanDevice::device, image, &memRequirements);
2025-04-27 23:20:52 +02:00
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
2025-05-03 06:51:33 +02:00
allocInfo.memoryTypeIndex = VulkanDevice::GetMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
2025-04-27 23:20:52 +02:00
2025-05-03 02:34:38 +02:00
VulkanDevice::CHECK_VK_RESULT(vkAllocateMemory(VulkanDevice::device, &allocInfo, nullptr, &imageMemory));
2025-04-27 23:20:52 +02:00
2025-05-03 02:34:38 +02:00
vkBindImageMemory(VulkanDevice::device, image, imageMemory, 0);
2025-05-03 06:51:33 +02:00
TransitionImageLayout(cmd, buffer, image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
VkBufferImageCopy region{};
region.bufferOffset = 0;
region.bufferRowLength = 0;
region.bufferImageHeight = 0;
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.imageSubresource.mipLevel = 0;
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageOffset = {0, 0, 0};
region.imageExtent = { width, height, 1};
vkCmdCopyBufferToImage(
cmd,
buffer.buffer,
image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1,
&region
);
TransitionImageLayout(cmd, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = image;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
VulkanDevice::CHECK_VK_RESULT(vkCreateImageView(VulkanDevice::device, &viewInfo, nullptr, &imageView));
}
2025-05-08 01:35:54 +02:00
VulkanTexture(const char* asset, VkCommandBuffer cmd) : VulkanTexture(reinterpret_cast<const std::uint32_t*>(asset)[0], reinterpret_cast<const std::uint32_t*>(asset)[1], reinterpret_cast<const PixelType*>(reinterpret_cast<const std::uint32_t*>(asset)+2), cmd) {
2025-05-03 06:51:33 +02:00
}
private:
void TransitionImageLayout(VkCommandBuffer cmd, Buffer<PixelType>& buffer, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout) {
VkImageMemoryBarrier barrier{};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.oldLayout = oldLayout;
barrier.newLayout = newLayout;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image;
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
barrier.srcAccessMask = 0;
barrier.dstAccessMask = 0;
VkPipelineStageFlags sourceStage;
VkPipelineStageFlags destinationStage;
if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
barrier.srcAccessMask = 0;
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
} else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
} else {
throw std::invalid_argument("unsupported layout transition!");
}
vkCmdPipelineBarrier(
cmd,
sourceStage, destinationStage,
0,
0, nullptr,
0, nullptr,
1, &barrier
);
2025-04-27 23:20:52 +02:00
}
};
}