Crafter.Graphics/interfaces/Crafter.Graphics-MeshShader.cppm

95 lines
4.2 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 22:16:56 +02:00
module;
#include <vulkan/vulkan.h>
export module Crafter.Graphics:MeshShader;
2025-11-16 15:32:11 +01:00
import std;
2025-04-27 22:16:56 +02:00
import :Mesh;
import :Camera;
2025-04-27 23:20:52 +02:00
import :VulkanPipeline;
2025-05-07 23:49:31 +02:00
import :DescriptorSet;
2025-05-05 05:14:47 +02:00
import Crafter.Math;
2025-04-27 22:16:56 +02:00
namespace Crafter {
2025-06-14 14:58:02 +02:00
/**
* @brief Shader for rendering indexed meshes
* @tparam VertexType The vertex type to use that is internally stored, this must match the type the glsl shader expects.
*/
2025-04-27 22:16:56 +02:00
export template <typename VertexType>
2025-05-08 01:13:59 +02:00
class MeshShader {
2025-04-27 22:16:56 +02:00
public:
2025-05-05 05:14:47 +02:00
MatrixRowMajor<float, 4, 4, 1> transform;
2025-05-08 01:13:59 +02:00
Mesh<VertexType>* mesh;
Camera* camera;
2025-05-05 05:14:47 +02:00
Buffer<MatrixRowMajor<float, 4, 4, 1>> mvp;
2025-04-27 23:20:52 +02:00
std::uint32_t threadCount;
2025-06-14 14:58:02 +02:00
private:
2025-05-05 06:00:35 +02:00
EventListener<void> cameraUpdate;
2025-06-14 14:58:02 +02:00
public:
/**
* @brief Constructs the MeshShader with a mesh.
* The Model-View-Projection (MVP) matrix and the transform matrix are initialized to the identity matrix.
*
* @param mesh Pointer to the mesh to use. The mesh must remain valid for the lifetime of this object.
*/
2025-06-13 23:59:36 +02:00
MeshShader(Mesh<VertexType>* mesh) : threadCount(std::ceil(static_cast<double>(mesh->indexCount)/64/3)), mvp(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), mesh(mesh), camera(nullptr) {
transform = MatrixRowMajor<float, 4, 4, 1>::Identity();
*mvp.value = MatrixRowMajor<float, 4, 4, 1>::Identity();
}
2025-06-14 14:58:02 +02:00
/**
* @brief Constructs the MeshShader with a mesh.
* The transform is initialized to identity, the transform is initialized to identity and the mvp to the camera's projectionView.
*
* @param mesh Pointer to the mesh to use. The mesh must remain valid for the lifetime of this object.
* @param mesh Pointer to the camera to use. The camera must remain valid for the lifetime of this object.
*/
2025-05-05 06:00:35 +02:00
MeshShader(Mesh<VertexType>* mesh, Camera* camera) : threadCount(std::ceil(static_cast<double>(mesh->indexCount)/64/3)), mvp(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), mesh(mesh), camera(camera), cameraUpdate(
&camera->onUpdate, [this](){
Update();
}
) {
2025-05-05 05:14:47 +02:00
transform = MatrixRowMajor<float, 4, 4, 1>::Identity();
2025-06-14 14:58:02 +02:00
*mvp.value = camera->projectionView;
2025-04-27 22:16:56 +02:00
}
2025-06-14 14:58:02 +02:00
/**
* @brief Writes this class's 3 descriptors to the set, this method must be called before rendering with this shader.
* Slot 0 mvp: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER.
* Slot 1 vertex: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER.
* Slot 2 index: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER.
*/
2025-05-24 00:52:06 +02:00
void WriteDescriptors(VkDescriptorSet set) {
2025-05-07 23:49:31 +02:00
VkWriteDescriptorSet write[3] = {
2025-05-24 00:52:06 +02:00
vks::initializers::writeDescriptorSet(set, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &mvp.descriptor),
vks::initializers::writeDescriptorSet(set, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, &mesh->verticies.descriptor),
vks::initializers::writeDescriptorSet(set, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2, &mesh->indicies.descriptor)
2025-05-07 23:49:31 +02:00
};
vkUpdateDescriptorSets(VulkanDevice::device, 3, &write[0], 0, nullptr);
2025-04-27 22:16:56 +02:00
}
2025-06-14 01:45:33 +02:00
/**
* @brief Must be called after every update to the camera or this transform
*/
2025-04-27 22:16:56 +02:00
void Update() {
2025-05-08 01:13:59 +02:00
*mvp.value = camera->projectionView*transform;
2025-04-27 22:16:56 +02:00
}
};
}