This commit is contained in:
Jorijn van der Graaf 2025-04-27 22:16:56 +02:00
commit cfc9ca8349
10 changed files with 124 additions and 81 deletions

View file

@ -15,14 +15,12 @@ module;
module Crafter.Graphics; module Crafter.Graphics;
using namespace Crafter; using namespace Crafter;
Camera::Camera() : buffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) { Camera::Camera() {
old::Camera camera; old::Camera camera;
camera.type = old::Camera::CameraType::lookat; camera.type = old::Camera::CameraType::lookat;
camera.setPerspective(60.0f, 128 / 128, 0.1f, 512.0f); camera.setPerspective(60.0f, 16 / 9, 0.1f, 512.0f);
camera.setRotation(glm::vec3(0.0f, 15.0f, 0.0f)); camera.setRotation(glm::vec3(180.0f, 0.0f, 0.0f));
camera.setTranslation(glm::vec3(0.0f, 0.0f, -5.0f)); camera.setTranslation(glm::vec3(0.0f, 0.0f, -10.0f));
buffer.value->projection = camera.matrices.perspective; projectionView = camera.matrices.perspective*camera.matrices.view;
buffer.value->view = camera.matrices.view;
buffer.value->model = glm::mat4(1.0f);
} }

View file

@ -13,16 +13,14 @@ module;
export module Crafter.Graphics:Camera; export module Crafter.Graphics:Camera;
import :VulkanBuffer; import :VulkanBuffer;
import Crafter.Component;
namespace Crafter { namespace Crafter {
struct CameraBufferData { export class Camera : public Component {
glm::mat4 projection;
glm::mat4 model;
glm::mat4 view;
};
export class Camera {
public: public:
Buffer<CameraBufferData> buffer; glm::mat4 projection;
glm::mat4 view;
glm::mat4 projectionView;
Camera(); Camera();
}; };
} }

View file

@ -2,18 +2,42 @@ module;
#include <cstdint> #include <cstdint>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <cstring>
#include <iostream>
export module Crafter.Graphics:Mesh; export module Crafter.Graphics:Mesh;
import Crafter.Component;
import :VulkanBuffer; import :VulkanBuffer;
namespace Crafter { namespace Crafter {
export template <typename VertexType> export template <typename VertexType>
class Mesh { class Mesh : public Component {
public: public:
std::uint32_t vertexCount;
std::uint32_t indexCount;
Buffer<VertexType> verticies; Buffer<VertexType> verticies;
Buffer<std::uint32_t> indicies; 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) { Mesh(std::uint32_t vertexCount, std::uint32_t indexCount) : vertexCount(vertexCount), indexCount(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) {
} }
static Mesh* FromAsset(const char* asset) {
std::uint32_t vertexCount = reinterpret_cast<const std::uint32_t*>(asset)[0];
std::uint32_t indexCount = reinterpret_cast<const std::uint32_t*>(asset)[1];
Mesh* mesh = new Mesh(vertexCount, (indexCount+ 63) & ~63);
const float* verticies = reinterpret_cast<const float*>(asset+sizeof(std::uint32_t)*2);
std::uint32_t counter = 0;
for(std::uint32_t i = 0; i < vertexCount*3; i+=3) {
mesh->verticies.value[counter].x = verticies[i];
mesh->verticies.value[counter].y = verticies[i+1];
mesh->verticies.value[counter].z = verticies[i+2];
mesh->verticies.value[counter].w = 1.0f;
counter++;
}
memcpy(mesh->indicies.value, asset+(sizeof(std::uint32_t)*2)+(vertexCount*sizeof(float)*3), indexCount*sizeof(std::uint32_t));
for(std::uint32_t i = indexCount; i < indexCount+(indexCount%64); i++) {
mesh->indicies.value[i] = 0;//pad indicies to nearest 64
}
return mesh;
}
}; };
} }

View file

@ -0,0 +1,47 @@
module;
#include <cstdint>
#include <vulkan/vulkan.h>
#include <cstring>
#include <iostream>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp>
export module Crafter.Graphics:MeshShader;
import Crafter.Component;
import :Mesh;
import :Camera;
import :VulkanElement;
namespace Crafter {
export template <typename VertexType>
class MeshShader {
public:
glm::mat4 transform;
ComponentRefOwning<Mesh<VertexType>> mesh;
ComponentRefOwning<Camera> camera;
Buffer<glm::mat4> mvp;
MeshShader(Mesh<VertexType>* mesh, Camera* camera) : mvp(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT), mesh(mesh), camera(camera) {
}
void SetGroupSize(VulkanElement* element) {
element->sizeX = (mesh.component->indexCount/3)/64;
element->sizeY = 1;
element->sizeZ = 1;
}
void WriteDescriptors(VulkanElement* element) {
element->WriteDescriptor(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &mvp.descriptor);
element->WriteDescriptor(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, &mesh.component->verticies.descriptor);
element->WriteDescriptor(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2, &mesh.component->indicies.descriptor);
}
void Update() {
mvp.value[0] = camera.component->projectionView*transform;
}
};
}

View file

@ -142,7 +142,7 @@ namespace Crafter {
// Pipeline // Pipeline
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE); VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0); VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_CLOCKWISE, 0);
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE); VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState); VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL); VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);

View file

@ -13,4 +13,5 @@ export import :VulkanElement;
export import :Camera; export import :Camera;
export import :VulkanElementFromPipeline; export import :VulkanElementFromPipeline;
export import :VulkanBuffer; export import :VulkanBuffer;
export import :Mesh; export import :Mesh;
export import :MeshShader;

View file

@ -2,36 +2,41 @@
#include <exception> #include <exception>
#include <thread> #include <thread>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp>
import Crafter.Graphics; import Crafter.Graphics;
import Crafter.Asset;
using namespace Crafter; using namespace Crafter;
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<"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}}}> MeshVulkanShader;
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<MeshShader, FragmentShader> Pipeline; typedef VulkanPipeline<MeshVulkanShader, FragmentShader> Pipeline;
int main() { int main() {
VulkanDevice::CreateDevice(); VulkanDevice::CreateDevice();
MeshShader::CreateShader(); MeshVulkanShader::CreateShader();
FragmentShader::CreateShader(); FragmentShader::CreateShader();
Pipeline::CreatePipeline(); Pipeline::CreatePipeline();
WindowWaylandVulkan window("Crafter.Graphics", 128, 128); WindowWaylandVulkan window("Crafter.Graphics", 1280, 720);
VulkanElement test = VulkanElementFromPipeline<Pipeline>(1, 1, 1); Asset asset;
asset.LoadFull("core.cras");
Camera camera; Camera camera;
test.WriteDescriptor(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &camera.buffer.descriptor); VulkanElement test = VulkanElementFromPipeline<Pipeline>();
Mesh<Vertex_xf32_yf32_zf32_wf32>* mesh = Mesh<Vertex_xf32_yf32_zf32_wf32>::FromAsset(asset.entries[0].data.data());
Mesh<Vertex_xf32_yf32_zf32_wf32> mesh(3, 3); MeshShader<Vertex_xf32_yf32_zf32_wf32> meshShader(mesh, &camera);
mesh.verticies.value[0] = {0.0f, -1.0f, 0.0f, 1.0f}; meshShader.SetGroupSize(&test);
mesh.verticies.value[1] = {-1.0f, 1.0f, 0.0f, 1.0f}; meshShader.WriteDescriptors(&test);
mesh.verticies.value[2] = {1.0f, 1.0f, 0.0f, 1.0f}; meshShader.transform = glm::mat4(1.0f);
meshShader.Update();
mesh.indicies.value[0] = 0;
mesh.indicies.value[1] = 1;
mesh.indicies.value[2] = 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();

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", "Crafter.Graphics-Mesh"], "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", "Crafter.Graphics-MeshShader"],
"build_dir": "./build", "build_dir": "./build",
"output_dir": "./bin", "output_dir": "./bin",
"type":"library", "type":"library",
@ -41,6 +41,10 @@
"path":"../Crafter.Event/project.json", "path":"../Crafter.Event/project.json",
"configuration":"debug" "configuration":"debug"
}, },
{
"path":"../Crafter.Asset/project.json",
"configuration":"lib-debug"
},
{ {
"path":"/home/jorijn/repos/Crafter/Crafter.Component/project.json", "path":"/home/jorijn/repos/Crafter/Crafter.Component/project.json",
"configuration":"debug" "configuration":"debug"

View file

@ -1,53 +1,30 @@
/* Copyright (c) 2021, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
#version 450 #version 450
#extension GL_EXT_mesh_shader : require #extension GL_EXT_mesh_shader : require
layout (binding = 0) uniform UBO layout (binding = 0) uniform UBO
{ {
mat4 projection; mat4 modelProjectionView;
mat4 model;
mat4 view;
} ubo; } ubo;
layout (binding = 1) buffer VERTEX layout (binding = 1) buffer VERTEX
{ {
vec4 pos[]; vec4 pos[];
} vertex; } vertex;
layout (binding = 2) buffer INDEX { layout (binding = 2) buffer INDEX {
uint index[]; uint index[];
} index; } index;
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(triangles, max_vertices = 3, max_primitives = 1) out; layout(triangles, max_vertices = 192, max_primitives = 64) out;
layout(location = 0) out VertexOutput
{
vec4 color;
} vertexOutput[];
const vec4[3] colors = {
vec4(0.0, 1.0, 0.0, 1.0),
vec4(0.0, 0.0, 1.0, 1.0),
vec4(1.0, 0.0, 0.0, 1.0)
};
void main() void main()
{ {
uint iid = gl_LocalInvocationID.x; SetMeshOutputsEXT(192, 64);
SetMeshOutputsEXT(3, 1); uint triangleID = ((gl_WorkGroupID.x * gl_WorkGroupSize.x) + gl_LocalInvocationIndex.x)*3;
mat4 mvp = ubo.projection * ubo.view * ubo.model; uint localID = gl_LocalInvocationIndex.x*3;
uint triangleID = gl_LocalInvocationIndex.x * 3; gl_MeshVerticesEXT[localID].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID]];
gl_MeshVerticesEXT[triangleID].gl_Position = mvp * vertex.pos[index.index[triangleID]]; gl_MeshVerticesEXT[localID+1].gl_Position = ubo.modelProjectionView * vertex.pos[index.index[triangleID+1]];
gl_MeshVerticesEXT[triangleID+1].gl_Position = mvp * vertex.pos[index.index[triangleID+1]]; gl_MeshVerticesEXT[localID+2].gl_Position =ubo.modelProjectionView * vertex.pos[index.index[triangleID+2]];
gl_MeshVerticesEXT[triangleID+2].gl_Position = mvp * vertex.pos[index.index[triangleID+2]]; gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex.x] = uvec3(localID, localID+1, localID+2);
vertexOutput[triangleID].color = colors[triangleID];
vertexOutput[triangleID+1].color = colors[triangleID+1];
vertexOutput[triangleID+2].color = colors[triangleID+2];
gl_PrimitiveTriangleIndicesEXT[gl_LocalInvocationIndex.x] = uvec3(index.index[triangleID], index.index[triangleID+1], index.index[triangleID+2]);
} }

View file

@ -1,19 +1,8 @@
/* Copyright (c) 2021, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
#version 450 #version 450
layout (location = 0) in VertexInput {
vec4 color;
} vertexInput;
layout(location = 0) out vec4 outFragColor; layout(location = 0) out vec4 outFragColor;
void main() void main()
{ {
outFragColor = vertexInput.color; outFragColor = vec4(1, 1, 1, 1);
} }