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

70 lines
2.9 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 <cstdint>
#include <vulkan/vulkan.h>
#include <cstring>
#include <iostream>
2025-05-04 05:15:31 +02:00
#include <cmath>
2025-06-13 23:59:36 +02:00
#include "../../lib/VulkanInitializers.hpp"
2025-04-27 22:16:56 +02:00
export module Crafter.Graphics:MeshShader;
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 {
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-05-05 06:00:35 +02:00
EventListener<void> cameraUpdate;
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-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-04-27 22:16:56 +02:00
}
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
}
void Update() {
2025-05-08 01:13:59 +02:00
*mvp.value = camera->projectionView*transform;
2025-04-27 22:16:56 +02:00
}
};
}