MESH BUFFER

This commit is contained in:
Jorijn van der Graaf 2025-04-27 01:57:25 +02:00
commit 523852da08
11 changed files with 93 additions and 51 deletions

View file

@ -1,15 +0,0 @@
module;
#include <cstdint>
export module Crafter.Graphics:Mesh;
namespace Crafter {
export class Mesh {
public:
vks::Buffer buffer;
Mesh();
private:
CameraBufferData data;
};
}

View file

@ -22,7 +22,7 @@ namespace Crafter {
}; };
export class Camera { export class Camera {
public: public:
Buffer<CameraBufferData, 1> buffer; Buffer<CameraBufferData> buffer;
Camera(); Camera();
}; };
} }

View file

@ -0,0 +1,19 @@
module;
#include <cstdint>
#include <vulkan/vulkan.h>
export module Crafter.Graphics:Mesh;
import :VulkanBuffer;
namespace Crafter {
export template <typename VertexType>
class Mesh {
public:
Buffer<VertexType> verticies;
Buffer<std::uint32_t> indicies;
Mesh(std::uint32_t vertexCount, std::uint32_t indexCount) : verticies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, vertexCount), indicies(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, indexCount) {
}
};
}

View file

@ -23,9 +23,10 @@ namespace Crafter {
std::uint8_t a; std::uint8_t a;
}; };
export struct Vertex_xf32_yf32_zf32 { export struct __attribute__((packed)) Vertex_xf32_yf32_zf32_wf32 {
float x; float x;
float y; float y;
float z; float z;
float w;
}; };
} }

View file

@ -8,12 +8,12 @@ export module Crafter.Graphics:VulkanBuffer;
import :VulkanDevice; import :VulkanDevice;
namespace Crafter { namespace Crafter {
export template <typename T, std::uint32_t count> export template <typename T>
class Buffer { class Buffer {
public: public:
T* value; T* value;
VkDescriptorBufferInfo descriptor; VkDescriptorBufferInfo descriptor;
Buffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags) { Buffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, std::uint32_t count = 1) {
VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(usageFlags, sizeof(T)*count); VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(usageFlags, sizeof(T)*count);
VulkanDevice::CHECK_VK_RESULT(vkCreateBuffer(VulkanDevice::device, &bufferCreateInfo, nullptr, &buffer)); VulkanDevice::CHECK_VK_RESULT(vkCreateBuffer(VulkanDevice::device, &bufferCreateInfo, nullptr, &buffer));

View file

@ -23,7 +23,7 @@ import :WindowWaylandVulkan;
namespace Crafter { namespace Crafter {
struct DescriptorEntry { struct DescriptorEntry {
VkDescriptorType type; VkDescriptorType type;
std::uint32_t occurrences = 0; bool occured = true;
}; };
export template <typename MeshShader, typename FragmentShader> export template <typename MeshShader, typename FragmentShader>
@ -35,7 +35,7 @@ namespace Crafter {
for(const DescriptorBinding& binding : MeshShader::descriptors) { for(const DescriptorBinding& binding : MeshShader::descriptors) {
for(DescriptorEntry& type : types) { for(DescriptorEntry& type : types) {
if(type.type == binding.type) { if(type.type == binding.type) {
type.occurrences++; type.occured = true;
} }
} }
} }
@ -43,14 +43,16 @@ namespace Crafter {
for(const DescriptorBinding& binding : FragmentShader::descriptors) { for(const DescriptorBinding& binding : FragmentShader::descriptors) {
for(DescriptorEntry& type : types) { for(DescriptorEntry& type : types) {
if(type.type == binding.type) { if(type.type == binding.type) {
type.occurrences++; type.occured = true;
} }
} }
} }
std::uint32_t size = 0; std::uint32_t size = 0;
for(DescriptorEntry& type : types) { for(DescriptorEntry& type : types) {
size+=type.occurrences; if(type.occured) {
size++;
}
} }
return size; return size;
@ -58,30 +60,46 @@ namespace Crafter {
constexpr static std::uint32_t uniqueDescriptorCount = GetUniqueDiscriptorCount(); constexpr static std::uint32_t uniqueDescriptorCount = GetUniqueDiscriptorCount();
consteval static std::array<VkDescriptorPoolSize, uniqueDescriptorCount> GetPoolSizes() { consteval static std::array<VkDescriptorPoolSize, uniqueDescriptorCount> GetPoolSizes() {
std::array<VkDescriptorPoolSize, uniqueDescriptorCount> types = {}; std::array<VkDescriptorPoolSize, uniqueDescriptorCount> types = {};
std::uint32_t i = 0; for(std::uint32_t i = 0; i < uniqueDescriptorCount; i++){
types[i].descriptorCount = 12345;
}
for(const DescriptorBinding& binding : MeshShader::descriptors) { for(const DescriptorBinding& binding : MeshShader::descriptors) {
bool found = false;
for(VkDescriptorPoolSize& type : types) { for(VkDescriptorPoolSize& type : types) {
if(type.type == binding.type) { if(type.type == binding.type && type.descriptorCount != 12345) {
type.descriptorCount++; type.descriptorCount++;
goto next; found = true;
}
}
if(!found) {
for(std::uint32_t i = 0; i < uniqueDescriptorCount; i++){
if(types[i].descriptorCount == 12345) {
types[i].type = binding.type;
types[i].descriptorCount = 1;
break;
}
} }
} }
types[i].type = binding.type;
types[i].descriptorCount = 1;
next:;
} }
for(const DescriptorBinding& binding : FragmentShader::descriptors) { for(const DescriptorBinding& binding : FragmentShader::descriptors) {
bool found = false;
for(VkDescriptorPoolSize& type : types) { for(VkDescriptorPoolSize& type : types) {
if(type.type == binding.type) { if(type.type == binding.type) {
type.descriptorCount++; type.descriptorCount++;
goto next2; found = true;
}
}
if(!found) {
for(std::uint32_t i = 0; i < uniqueDescriptorCount; i++){
if(types[i].descriptorCount == 12345) {
types[i].type = binding.type;
types[i].descriptorCount = 1;
break;
}
} }
} }
types[i].type = binding.type;
types[i].descriptorCount = 1;
next2:;
} }
return types; return types;
@ -165,6 +183,7 @@ namespace Crafter {
shaderStages[0].pName = MeshShader::_entrypoint.value; shaderStages[0].pName = MeshShader::_entrypoint.value;
shaderStages[0].flags = 0; shaderStages[0].flags = 0;
shaderStages[0].pSpecializationInfo = nullptr; shaderStages[0].pSpecializationInfo = nullptr;
shaderStages[0].pNext = nullptr;
// Fragment stage of the pipeline // Fragment stage of the pipeline
shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
@ -173,6 +192,7 @@ namespace Crafter {
shaderStages[1].pName = FragmentShader::_entrypoint.value; shaderStages[1].pName = FragmentShader::_entrypoint.value;
shaderStages[1].flags = 0; shaderStages[1].flags = 0;
shaderStages[1].pSpecializationInfo = nullptr; shaderStages[1].pSpecializationInfo = nullptr;
shaderStages[1].pNext = nullptr;
VulkanDevice::CHECK_VK_RESULT(vkCreateGraphicsPipelines(VulkanDevice::device, VK_NULL_HANDLE, 1, &pipelineCI, nullptr, &pipeline)); VulkanDevice::CHECK_VK_RESULT(vkCreateGraphicsPipelines(VulkanDevice::device, VK_NULL_HANDLE, 1, &pipelineCI, nullptr, &pipeline));
} }

View file

@ -31,7 +31,7 @@ namespace Crafter {
StringLiteral entrypoint, StringLiteral entrypoint,
VkShaderStageFlagBits stage, VkShaderStageFlagBits stage,
std::uint32_t DescriptorCount, std::uint32_t DescriptorCount,
std::array<DescriptorBinding, DescriptorCount> Descriptors const std::array<DescriptorBinding, DescriptorCount> Descriptors
> >
class VulkanShader { class VulkanShader {
public: public:

View file

@ -12,4 +12,5 @@ export import :VulkanShader;
export import :VulkanElement; export import :VulkanElement;
export import :Camera; export import :Camera;
export import :VulkanElementFromPipeline; export import :VulkanElementFromPipeline;
export import :VulkanBuffer; export import :VulkanBuffer;
export import :Mesh;

View file

@ -5,9 +5,9 @@
import Crafter.Graphics; import Crafter.Graphics;
using namespace Crafter; using namespace Crafter;
typedef VulkanShader<"test.spirv", "main", VK_SHADER_STAGE_MESH_BIT_EXT, 1, {{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0}}> MeshShader; typedef VulkanShader<"test.spirv", "main", VK_SHADER_STAGE_MESH_BIT_EXT, 3, {{{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2}}}> MeshShader;
typedef VulkanShader<"test2.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 0, {}> FragmentShader; typedef VulkanShader<"test2.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 0, {}> FragmentShader;
typedef VulkanPipeline<VulkanShader<"test.spirv", "main", VK_SHADER_STAGE_MESH_BIT_EXT, 1, {{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0}}>, VulkanShader<"test2.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 0, {}>> Pipeline; typedef VulkanPipeline<MeshShader, FragmentShader> Pipeline;
int main() { int main() {
// WindowWaylandWayland window("test", 128, 128); // WindowWaylandWayland window("test", 128, 128);
@ -33,13 +33,28 @@ int main() {
//WriteDescriptor(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor); //WriteDescriptor(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor);
VulkanDevice::CreateDevice(); VulkanDevice::CreateDevice();
WindowWaylandVulkan window("Crafter.Graphics", 128, 128);
MeshShader::CreateShader(); MeshShader::CreateShader();
FragmentShader::CreateShader(); FragmentShader::CreateShader();
Pipeline::CreatePipeline(); Pipeline::CreatePipeline();
VulkanElement test = VulkanElementFromPipeline<Pipeline>(3, 1, 1);
WindowWaylandVulkan window("Crafter.Graphics", 128, 128);
VulkanElement test = VulkanElementFromPipeline<Pipeline>(1, 1, 1);
Camera camera; Camera camera;
test.WriteDescriptor(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &camera.buffer.descriptor); test.WriteDescriptor(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &camera.buffer.descriptor);
Mesh<Vertex_xf32_yf32_zf32_wf32> mesh(3, 3);
mesh.verticies.value[0] = {0.0f, -1.0f, 0.0f, 1.0f};
mesh.verticies.value[1] = {-1.0f, 1.0f, 0.0f, 1.0f};
mesh.verticies.value[2] = {1.0f, 1.0f, 0.0f, 1.0f};
mesh.indicies.value[0] = 0;
mesh.indicies.value[0] = 1;
mesh.indicies.value[0] = 2;
test.WriteDescriptor(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, &mesh.verticies.descriptor);
test.WriteDescriptor(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2, &mesh.indicies.descriptor);
window.vulkanElements.AddComponent(&test); window.vulkanElements.AddComponent(&test);
window.Start(); window.Start();
while(true) { while(true) {

View file

@ -6,7 +6,7 @@
"standard": "c++26", "standard": "c++26",
"source_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics-UiElement", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-WindowWaylandVulkan", "VulkanBuffer", "VulkanTools", "Crafter.Graphics-VulkanElement", "Crafter.Graphics-Camera"], "source_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics-UiElement", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-WindowWaylandVulkan", "VulkanBuffer", "VulkanTools", "Crafter.Graphics-VulkanElement", "Crafter.Graphics-Camera"],
"c_files": ["wayland-xdg-decoration-unstable-v1-client-protocol", "xdg-shell-protocol", "shm"], "c_files": ["wayland-xdg-decoration-unstable-v1-client-protocol", "xdg-shell-protocol", "shm"],
"module_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics", "Crafter.Graphics-UiElement", "Crafter.Graphics-Types", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-VulkanPipeline", "Crafter.Graphics-VulkanShader", "Crafter.Graphics-WindowWaylandVulkan", "Crafter.Graphics-VulkanElement", "Crafter.Graphics-VulkanElementFromPipeline", "Crafter.Graphics-Camera", "Crafter.Graphics-VulkanBuffer"], "module_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics", "Crafter.Graphics-UiElement", "Crafter.Graphics-Types", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-VulkanPipeline", "Crafter.Graphics-VulkanShader", "Crafter.Graphics-WindowWaylandVulkan", "Crafter.Graphics-VulkanElement", "Crafter.Graphics-VulkanElementFromPipeline", "Crafter.Graphics-Camera", "Crafter.Graphics-VulkanBuffer", "Crafter.Graphics-Mesh"],
"build_dir": "./build", "build_dir": "./build",
"output_dir": "./bin", "output_dir": "./bin",
"type":"library", "type":"library",

View file

@ -14,6 +14,15 @@ layout (binding = 0) uniform UBO
mat4 view; mat4 view;
} ubo; } ubo;
layout (binding = 1) buffer VERTEX
{
vec4 pos[];
} vertex;
layout (binding = 2) buffer INDEX {
uint index[];
} index;
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(triangles, max_vertices = 3, max_primitives = 1) out; layout(triangles, max_vertices = 3, max_primitives = 1) out;
@ -22,12 +31,6 @@ layout(location = 0) out VertexOutput
vec4 color; vec4 color;
} vertexOutput[]; } vertexOutput[];
const vec4[3] positions = {
vec4( 0.0, -1.0, 0.0, 1.0),
vec4(-1.0, 1.0, 0.0, 1.0),
vec4( 1.0, 1.0, 0.0, 1.0)
};
const vec4[3] colors = { const vec4[3] colors = {
vec4(0.0, 1.0, 0.0, 1.0), vec4(0.0, 1.0, 0.0, 1.0),
vec4(0.0, 0.0, 1.0, 1.0), vec4(0.0, 0.0, 1.0, 1.0),
@ -38,13 +41,11 @@ void main()
{ {
uint iid = gl_LocalInvocationID.x; uint iid = gl_LocalInvocationID.x;
vec4 offset = vec4(0.0, 0.0, gl_GlobalInvocationID.x, 0.0);
SetMeshOutputsEXT(3, 1); SetMeshOutputsEXT(3, 1);
mat4 mvp = ubo.projection * ubo.view * ubo.model; mat4 mvp = ubo.projection * ubo.view * ubo.model;
gl_MeshVerticesEXT[0].gl_Position = mvp * (positions[0] + offset); gl_MeshVerticesEXT[0].gl_Position = mvp * vertex.pos[0];
gl_MeshVerticesEXT[1].gl_Position = mvp * (positions[1] + offset); gl_MeshVerticesEXT[1].gl_Position = mvp * vertex.pos[1];
gl_MeshVerticesEXT[2].gl_Position = mvp * (positions[2] + offset); gl_MeshVerticesEXT[2].gl_Position = mvp * vertex.pos[2];
vertexOutput[0].color = colors[0]; vertexOutput[0].color = colors[0];
vertexOutput[1].color = colors[1]; vertexOutput[1].color = colors[1];
vertexOutput[2].color = colors[2]; vertexOutput[2].color = colors[2];