diff --git a/Crafter.Grahpics-Mesh.cppm b/Crafter.Grahpics-Mesh.cppm deleted file mode 100644 index b5fe9cd..0000000 --- a/Crafter.Grahpics-Mesh.cppm +++ /dev/null @@ -1,15 +0,0 @@ -module; - -#include - -export module Crafter.Graphics:Mesh; - -namespace Crafter { - export class Mesh { - public: - vks::Buffer buffer; - Mesh(); - private: - CameraBufferData data; - }; -} diff --git a/Crafter.Graphics-Camera.cppm b/Crafter.Graphics-Camera.cppm index c4deb17..a9f4648 100644 --- a/Crafter.Graphics-Camera.cppm +++ b/Crafter.Graphics-Camera.cppm @@ -22,7 +22,7 @@ namespace Crafter { }; export class Camera { public: - Buffer buffer; + Buffer buffer; Camera(); }; } diff --git a/Crafter.Graphics-Mesh.cppm b/Crafter.Graphics-Mesh.cppm new file mode 100644 index 0000000..d423d0a --- /dev/null +++ b/Crafter.Graphics-Mesh.cppm @@ -0,0 +1,19 @@ +module; + +#include +#include + +export module Crafter.Graphics:Mesh; +import :VulkanBuffer; + +namespace Crafter { + export template + class Mesh { + public: + Buffer verticies; + Buffer 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) { + + } + }; +} diff --git a/Crafter.Graphics-Types.cppm b/Crafter.Graphics-Types.cppm index ef9cb71..54b115c 100644 --- a/Crafter.Graphics-Types.cppm +++ b/Crafter.Graphics-Types.cppm @@ -23,9 +23,10 @@ namespace Crafter { std::uint8_t a; }; - export struct Vertex_xf32_yf32_zf32 { + export struct __attribute__((packed)) Vertex_xf32_yf32_zf32_wf32 { float x; float y; float z; + float w; }; } diff --git a/Crafter.Graphics-VulkanBuffer.cppm b/Crafter.Graphics-VulkanBuffer.cppm index 3271a05..169c0ee 100644 --- a/Crafter.Graphics-VulkanBuffer.cppm +++ b/Crafter.Graphics-VulkanBuffer.cppm @@ -8,12 +8,12 @@ export module Crafter.Graphics:VulkanBuffer; import :VulkanDevice; namespace Crafter { - export template + export template class Buffer { public: T* value; 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); VulkanDevice::CHECK_VK_RESULT(vkCreateBuffer(VulkanDevice::device, &bufferCreateInfo, nullptr, &buffer)); diff --git a/Crafter.Graphics-VulkanPipeline.cppm b/Crafter.Graphics-VulkanPipeline.cppm index 259e2a2..c7c993e 100644 --- a/Crafter.Graphics-VulkanPipeline.cppm +++ b/Crafter.Graphics-VulkanPipeline.cppm @@ -23,7 +23,7 @@ import :WindowWaylandVulkan; namespace Crafter { struct DescriptorEntry { VkDescriptorType type; - std::uint32_t occurrences = 0; + bool occured = true; }; export template @@ -35,7 +35,7 @@ namespace Crafter { for(const DescriptorBinding& binding : MeshShader::descriptors) { for(DescriptorEntry& type : types) { if(type.type == binding.type) { - type.occurrences++; + type.occured = true; } } } @@ -43,14 +43,16 @@ namespace Crafter { for(const DescriptorBinding& binding : FragmentShader::descriptors) { for(DescriptorEntry& type : types) { if(type.type == binding.type) { - type.occurrences++; + type.occured = true; } } } std::uint32_t size = 0; for(DescriptorEntry& type : types) { - size+=type.occurrences; + if(type.occured) { + size++; + } } return size; @@ -58,30 +60,46 @@ namespace Crafter { constexpr static std::uint32_t uniqueDescriptorCount = GetUniqueDiscriptorCount(); consteval static std::array GetPoolSizes() { std::array 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) { + bool found = false; for(VkDescriptorPoolSize& type : types) { - if(type.type == binding.type) { + if(type.type == binding.type && type.descriptorCount != 12345) { 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) { + bool found = false; for(VkDescriptorPoolSize& type : types) { if(type.type == binding.type) { 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; @@ -165,6 +183,7 @@ namespace Crafter { shaderStages[0].pName = MeshShader::_entrypoint.value; shaderStages[0].flags = 0; shaderStages[0].pSpecializationInfo = nullptr; + shaderStages[0].pNext = nullptr; // Fragment stage of the pipeline 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].flags = 0; shaderStages[1].pSpecializationInfo = nullptr; + shaderStages[1].pNext = nullptr; VulkanDevice::CHECK_VK_RESULT(vkCreateGraphicsPipelines(VulkanDevice::device, VK_NULL_HANDLE, 1, &pipelineCI, nullptr, &pipeline)); } diff --git a/Crafter.Graphics-VulkanShader.cppm b/Crafter.Graphics-VulkanShader.cppm index 7aa5724..4718b30 100644 --- a/Crafter.Graphics-VulkanShader.cppm +++ b/Crafter.Graphics-VulkanShader.cppm @@ -31,7 +31,7 @@ namespace Crafter { StringLiteral entrypoint, VkShaderStageFlagBits stage, std::uint32_t DescriptorCount, - std::array Descriptors + const std::array Descriptors > class VulkanShader { public: diff --git a/Crafter.Graphics.cppm b/Crafter.Graphics.cppm index ee8d222..b302da0 100644 --- a/Crafter.Graphics.cppm +++ b/Crafter.Graphics.cppm @@ -12,4 +12,5 @@ export import :VulkanShader; export import :VulkanElement; export import :Camera; export import :VulkanElementFromPipeline; -export import :VulkanBuffer; \ No newline at end of file +export import :VulkanBuffer; +export import :Mesh; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 0063ce2..2fbdfbe 100644 --- a/main.cpp +++ b/main.cpp @@ -5,9 +5,9 @@ import Crafter.Graphics; 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 VulkanPipeline, VulkanShader<"test2.spirv", "main", VK_SHADER_STAGE_FRAGMENT_BIT, 0, {}>> Pipeline; +typedef VulkanPipeline Pipeline; int main() { // WindowWaylandWayland window("test", 128, 128); @@ -33,13 +33,28 @@ int main() { //WriteDescriptor(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor); VulkanDevice::CreateDevice(); - WindowWaylandVulkan window("Crafter.Graphics", 128, 128); MeshShader::CreateShader(); FragmentShader::CreateShader(); Pipeline::CreatePipeline(); - VulkanElement test = VulkanElementFromPipeline(3, 1, 1); + + WindowWaylandVulkan window("Crafter.Graphics", 128, 128); + + VulkanElement test = VulkanElementFromPipeline(1, 1, 1); + Camera camera; test.WriteDescriptor(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &camera.buffer.descriptor); + + Mesh 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.Start(); while(true) { diff --git a/project.json b/project.json index 2eb9d49..56c84a6 100644 --- a/project.json +++ b/project.json @@ -6,7 +6,7 @@ "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"], "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", "output_dir": "./bin", "type":"library", diff --git a/test.mesh b/test.mesh index 2e0a34c..3f7de2d 100644 --- a/test.mesh +++ b/test.mesh @@ -14,6 +14,15 @@ layout (binding = 0) uniform UBO mat4 view; } 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(triangles, max_vertices = 3, max_primitives = 1) out; @@ -22,12 +31,6 @@ layout(location = 0) out VertexOutput vec4 color; } 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 = { vec4(0.0, 1.0, 0.0, 1.0), vec4(0.0, 0.0, 1.0, 1.0), @@ -38,13 +41,11 @@ void main() { uint iid = gl_LocalInvocationID.x; - vec4 offset = vec4(0.0, 0.0, gl_GlobalInvocationID.x, 0.0); - SetMeshOutputsEXT(3, 1); mat4 mvp = ubo.projection * ubo.view * ubo.model; - gl_MeshVerticesEXT[0].gl_Position = mvp * (positions[0] + offset); - gl_MeshVerticesEXT[1].gl_Position = mvp * (positions[1] + offset); - gl_MeshVerticesEXT[2].gl_Position = mvp * (positions[2] + offset); + gl_MeshVerticesEXT[0].gl_Position = mvp * vertex.pos[0]; + gl_MeshVerticesEXT[1].gl_Position = mvp * vertex.pos[1]; + gl_MeshVerticesEXT[2].gl_Position = mvp * vertex.pos[2]; vertexOutput[0].color = colors[0]; vertexOutput[1].color = colors[1]; vertexOutput[2].color = colors[2];