Crafter.Graphics/src/module/Crafter.Graphics-TextureShader.cppm

83 lines
3.4 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-05-03 06:51:33 +02:00
module;
#include <cstdint>
#include <vulkan/vulkan.h>
#include <cstring>
#include <iostream>
2025-06-13 23:59:36 +02:00
#include "../../lib/VulkanInitializers.hpp"
2025-05-03 06:51:33 +02:00
export module Crafter.Graphics:TextureShader;
import :VulkanTexture;
import :VulkanPipeline;
2025-05-07 23:49:31 +02:00
import :DescriptorSet;
2025-05-03 06:51:33 +02:00
namespace Crafter {
2025-06-14 14:58:02 +02:00
/**
* @brief Creates a sampler for a provided VulkanTexture<PixelType>.
* @tparam PixelType The pixeltype to use.
*/
2025-05-03 06:51:33 +02:00
export template <typename PixelType>
class TextureShader {
2025-06-14 14:58:02 +02:00
private:
2025-05-03 06:51:33 +02:00
VkSampler textureSampler;
VkDescriptorImageInfo imageInfo;
2025-05-08 01:13:59 +02:00
VulkanTexture<PixelType>* texture;
2025-06-14 14:58:02 +02:00
public:
/**
* @brief Creates a sampler for a texture.
* @param texture A pointer to the texture to create the sampler for, the texture must remain valid for the lifetime of this object.
*/
2025-05-03 06:51:33 +02:00
TextureShader(VulkanTexture<PixelType>* texture) : texture(texture) {
VkSamplerCreateInfo samplerInfo{};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VK_FILTER_LINEAR;
samplerInfo.minFilter = VK_FILTER_LINEAR;
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.anisotropyEnable = VK_FALSE;
samplerInfo.maxAnisotropy = 1;
samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
samplerInfo.unnormalizedCoordinates = VK_FALSE;
samplerInfo.compareEnable = VK_FALSE;
samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerInfo.mipLodBias = 0.0f;
samplerInfo.minLod = 0.0f;
samplerInfo.maxLod = 0.0f;
VulkanDevice::CHECK_VK_RESULT(vkCreateSampler(VulkanDevice::device, &samplerInfo, nullptr, &textureSampler));
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = texture->imageView;
imageInfo.sampler = textureSampler;
}
2025-06-14 14:58:02 +02:00
/**
* @brief Writes this class's 1 descriptor to the set, this method must be called before rendering with this shader.
* Slot 0: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER.
*/
2025-05-24 00:52:06 +02:00
void WriteDescriptors(VkDescriptorSet set) {
VkWriteDescriptorSet write = vks::initializers::writeDescriptorSet(set, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &imageInfo);
2025-05-07 23:49:31 +02:00
vkUpdateDescriptorSets(VulkanDevice::device, 1, &write, 0, nullptr);
2025-05-03 06:51:33 +02:00
}
};
}