diff --git a/engine/assets/shader/gen_glsl.bat b/engine/assets/shader/gen_glsl.bat new file mode 100644 index 0000000..5b97178 --- /dev/null +++ b/engine/assets/shader/gen_glsl.bat @@ -0,0 +1,20 @@ +@echo off +setlocal EnableDelayedExpansion +set /p=exec.bat +set destPath=%2cpp/ +ECHO @echo on >>exec.bat +for %%i in (*.vs.glsl) do ( + set "s=%%i" + echo !s:vs.glsl=vert.spv! + set /p="glslc -fshader-stage=vertex %%i -o !s:vs.glsl=vert.spv!"> exec.bat + ECHO.>> exec.bat +) +for %%i in (*.ps.glsl) do ( + set "s=%%i" + echo !s:ps.glsl=frag.spv! + set /p="glslc -fshader-stage=fragment %%i -o !s:ps.glsl=frag.spv!"> exec.bat + ECHO.>> exec.bat +) +ECHO @echo off>> exec.bat +call exec.bat +del exec.bat \ No newline at end of file diff --git a/engine/assets/shader/simple.frag.spv b/engine/assets/shader/simple.frag.spv index a1a14e5..d3a34da 100644 Binary files a/engine/assets/shader/simple.frag.spv and b/engine/assets/shader/simple.frag.spv differ diff --git a/engine/assets/shader/simple.ps.glsl b/engine/assets/shader/simple.ps.glsl index 1ab470c..1a85ab6 100644 --- a/engine/assets/shader/simple.ps.glsl +++ b/engine/assets/shader/simple.ps.glsl @@ -1,11 +1,8 @@ #version 450 - -layout(location = 0) in vec4 inColor; +#extension GL_ARB_separate_shader_objects : enable -layout(location = 0) out vec4 outFragColor; - +layout(location = 0) out vec4 outColor; -void main() -{ - outFragColor = inColor; +void main() { + outColor = vec4(1.0, 0.0, 0.0, 1.0); } \ No newline at end of file diff --git a/engine/assets/shader/simple.vert.spv b/engine/assets/shader/simple.vert.spv index d36c45c..b3268c2 100644 Binary files a/engine/assets/shader/simple.vert.spv and b/engine/assets/shader/simple.vert.spv differ diff --git a/engine/assets/shader/simple.vs.glsl b/engine/assets/shader/simple.vs.glsl index 82258bc..06ed755 100644 --- a/engine/assets/shader/simple.vs.glsl +++ b/engine/assets/shader/simple.vs.glsl @@ -9,14 +9,19 @@ layout (location = 3) in vec3 tangent; layout (location = 4) in vec4 weights; layout (location = 5) in uvec4 bones; -// 在这里声明输出变量 -layout(location = 0) out vec4 outColor; // 顶点位置输出变量 //out vec4 outPosition; +out gl_PerVertex { + vec4 gl_Position; +}; -void main() -{ - gl_Position = vec4(position, 1); - outColor = vec4(texture, 0, 1); +vec2 positions[3] = vec2[]( + vec2(0.0, -0.5), + vec2(0.5, 0.5), + vec2(-0.5, 0.5) +); + +void main() { + gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); } \ No newline at end of file diff --git a/engine/src/engine/render/asset/mesh.h b/engine/src/engine/render/asset/mesh.h index 1a51fa1..eeb7ced 100644 --- a/engine/src/engine/render/asset/mesh.h +++ b/engine/src/engine/render/asset/mesh.h @@ -1,5 +1,6 @@ #pragma once #include "asset_render.h" +#include "../wrapper/vertex.h" namespace engineapi { class Texture; class Material; diff --git a/engine/src/engine/render/wrapper/vertex.cpp b/engine/src/engine/render/wrapper/vertex.cpp new file mode 100644 index 0000000..e69de29 diff --git a/engine/src/engine/render/wrapper/vertex.h b/engine/src/engine/render/wrapper/vertex.h new file mode 100644 index 0000000..b7722e0 --- /dev/null +++ b/engine/src/engine/render/wrapper/vertex.h @@ -0,0 +1,8 @@ +#pragma once +#include "math/vector3.h" + +namespace engineapi { + struct VertexBase{ + virtual Vector3& GetPosition() = 0; + }; +}; \ No newline at end of file diff --git a/engine/src/engine/vulkanapi/backend.cpp b/engine/src/engine/vulkanapi/backend.cpp index 0a62a3b..15c70d7 100644 --- a/engine/src/engine/vulkanapi/backend.cpp +++ b/engine/src/engine/vulkanapi/backend.cpp @@ -1,6 +1,7 @@ #include "backend.h" #include "wrapper/queue.h" #include "thread/worker.h" +#include "wrapper/descriptorpool.h" #include #include "zlog.h" namespace vulkanapi { @@ -28,6 +29,9 @@ namespace vulkanapi { Backend::TransferWorker = GetWorker(Queue::TransferQueue); Backend::RenderWorker = GetWorker(Queue::RenderQueue); Backend::PresentWorker = GetWorker(Queue::PresentQueue); + + auto poolSizes = DescriptorPool::DefaultDescriptorPoolSize(); + mPool = new DescriptorPool(*mDevice, poolSizes, 1000); } Backend::~Backend() { diff --git a/engine/src/engine/vulkanapi/backend.h b/engine/src/engine/vulkanapi/backend.h index 56b3f37..321cb07 100644 --- a/engine/src/engine/vulkanapi/backend.h +++ b/engine/src/engine/vulkanapi/backend.h @@ -5,10 +5,12 @@ #include "wrapper/device_creator.h" namespace vulkanapi { class CommandWorker; + class DescriptorPool; class Backend{ protected: Instance* mInstance; Device* mDevice; + DescriptorPool* mPool; map mWorkerMap; public: Instance& GetInstance() { @@ -17,6 +19,9 @@ namespace vulkanapi { Device& GetDevice() { return *mDevice; } + DescriptorPool& GetPool() { + return *mPool; + } public: Backend(const string appName); ~Backend(); diff --git a/engine/src/engine/vulkanapi/vulkan_struct.h b/engine/src/engine/vulkanapi/vulkan_struct.h index 916dff6..79c1b93 100644 --- a/engine/src/engine/vulkanapi/vulkan_struct.h +++ b/engine/src/engine/vulkanapi/vulkan_struct.h @@ -176,6 +176,7 @@ namespace vulkanapi { string name; // For debug VkPipeline pipeline = VK_NULL_HANDLE; + VkDescriptorSet descriptorSet = VK_NULL_HANDLE; VkPipelineLayout pipelineLayout = VK_NULL_HANDLE; VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE; VkDescriptorSetLayout sceneDescriptorSetLayout = VK_NULL_HANDLE; // For ray tracing diff --git a/engine/src/engine/vulkanapi/vulkanapi.cpp b/engine/src/engine/vulkanapi/vulkanapi.cpp index f9f9251..6afdfe4 100644 --- a/engine/src/engine/vulkanapi/vulkanapi.cpp +++ b/engine/src/engine/vulkanapi/vulkanapi.cpp @@ -9,7 +9,9 @@ #include "pass/target.h" #include "wrapper/swapchain.h" #include "render/asset/mesh.h" +#include "wrapper/descriptorpool.h" #include "window.h" +#include "zlog.h" namespace vulkanapi { RenderVulkanAPI::RenderVulkanAPI() :backend("vulkan") @@ -135,8 +137,8 @@ namespace vulkanapi { vkCmdBindVertexBuffers(ptr, 0, 1, vertexBuffers, offsets); vkCmdBindIndexBuffer(ptr, vulkanVAO->indexBuffer, 0, VK_INDEX_TYPE_UINT32); vkCmdBindPipeline(ptr, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->pipeline); - //vkCmdBindDescriptorSets(ptr, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->pipelineLayout, 0, 1, &pipeline->descriptorSet, 0, VK_NULL_HANDLE); - vkCmdDrawIndexed(ptr, vulkanVAO->indexCount, 1, 0, 0, 0); + vkCmdBindDescriptorSets(ptr, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->pipelineLayout, 0, 1, &pipeline->descriptorSet, 0, VK_NULL_HANDLE); + vkCmdDrawIndexed(ptr, 3, 1, 0, 0, 0); context.Pass->EndPass(cmd); }); Backend::RenderWorker->Flush(); @@ -166,7 +168,8 @@ namespace vulkanapi { bindingDescription.binding = 0; bindingDescription.stride = sizeof(Vertex); bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; - array attributeDescriptions = {}; + + array attributeDescriptions = { }; attributeDescriptions[0].binding = 0; attributeDescriptions[0].location = 0; attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; @@ -296,7 +299,7 @@ namespace vulkanapi { pipelineInfo.pRasterizationState = &rasterizationInfo; pipelineInfo.pMultisampleState = &multisampleInfo; pipelineInfo.pColorBlendState = &colorBlending; - pipelineInfo.pDepthStencilState = &depthStencilInfo; + pipelineInfo.pDepthStencilState = nullptr; &depthStencilInfo; pipelineInfo.layout = pipelineLayout; pipelineInfo.renderPass = PassList[RENDER_FORWARD_RENDERING]->Ptr(); pipelineInfo.subpass = 0; @@ -316,6 +319,7 @@ namespace vulkanapi { vulkan_pipeline->inUse = true; vulkan_pipeline->pipelineLayout = pipelineLayout; vulkan_pipeline->descriptorSetLayout = descriptorSetLayout; + vulkan_pipeline->descriptorSet = backend.GetPool().Allocate(descriptorSetLayout); } VulkanVAO* RenderVulkanAPI::GetNextVAO(uint32_t& index) { diff --git a/engine/src/engine/vulkanapi/wrapper/commandpool.h b/engine/src/engine/vulkanapi/wrapper/commandpool.h index 1537bcb..e4f52df 100644 --- a/engine/src/engine/vulkanapi/wrapper/commandpool.h +++ b/engine/src/engine/vulkanapi/wrapper/commandpool.h @@ -1,7 +1,6 @@ #pragma once #include "../vulkan.h" #include "zstd/pool.h" -#include "zlog.h" #include "commandbuffer.h" namespace vulkanapi { class Device; diff --git a/engine/src/engine/vulkanapi/wrapper/descriptorpool.cpp b/engine/src/engine/vulkanapi/wrapper/descriptorpool.cpp new file mode 100644 index 0000000..fd527aa --- /dev/null +++ b/engine/src/engine/vulkanapi/wrapper/descriptorpool.cpp @@ -0,0 +1,30 @@ +#include "descriptorpool.h" +#include "device.h" +namespace vulkanapi { + DescriptorPool::DescriptorPool(Device& device, vector& PoolSizes, uint32_t maxSets) + : mPtr(nullptr) + , mDevice(device) + { + VkDescriptorPoolCreateInfo descriptorPoolInfo{}; + descriptorPoolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + descriptorPoolInfo.poolSizeCount = PoolSizes.size(); + descriptorPoolInfo.pPoolSizes = PoolSizes.data(); + descriptorPoolInfo.maxSets = maxSets; + + vkCreateDescriptorPool(mDevice.Ptr(), &descriptorPoolInfo, nullptr, &mPtr); + } + + VkDescriptorSet DescriptorPool::Allocate(VkDescriptorSetLayout& descriptorSetLayout) + { + + VkDescriptorSet descriptorSet; + VkDescriptorSetAllocateInfo allocInfo{}; + allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + allocInfo.descriptorPool = mPtr; + allocInfo.descriptorSetCount = 1; + allocInfo.pSetLayouts = &descriptorSetLayout; + vkAllocateDescriptorSets(mDevice.Ptr(), &allocInfo, &descriptorSet); + return descriptorSet; + } + +} diff --git a/engine/src/engine/vulkanapi/wrapper/descriptorpool.h b/engine/src/engine/vulkanapi/wrapper/descriptorpool.h new file mode 100644 index 0000000..95ce9cc --- /dev/null +++ b/engine/src/engine/vulkanapi/wrapper/descriptorpool.h @@ -0,0 +1,26 @@ +#pragma once +#include "../vulkan.h" +#include "zstd/pool.h" +#include "commandbuffer.h" +namespace vulkanapi { + class Device; + class Queue; + class DescriptorPool { + protected: + VkDescriptorPool mPtr; + Device& mDevice; + public: + DescriptorPool(Device& device, vector& pPoolSizes,uint32_t maxSets); + + VkDescriptorSet Allocate(VkDescriptorSetLayout& descriptorSetLayout); + public: + static vector DefaultDescriptorPoolSize() { + return vector{ + {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 10000}, + {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 10000}, + {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 10000}, + {VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 10000} + }; + } + }; +} \ No newline at end of file diff --git a/engine/xmake.lua b/engine/xmake.lua index dd66dd5..da1bbc2 100644 --- a/engine/xmake.lua +++ b/engine/xmake.lua @@ -4,7 +4,7 @@ includes("xmake/**xmake.lua") target("zengine") set_kind("binary") set_rundir(".") - add_rules("volk.env") + add_rules("volk.env", "glsl.env") add_deps("zlib","zlog") add_defines("VULKAN_API") add_packages("vulkansdk","tinyobjloader","assimp","nlohmann_json") diff --git a/engine/xmake/glsl/xmake.lua b/engine/xmake/glsl/xmake.lua new file mode 100644 index 0000000..f2d9f80 --- /dev/null +++ b/engine/xmake/glsl/xmake.lua @@ -0,0 +1,6 @@ +rule("glsl.env") + -- before load + before_build(function (target) + os.cd("$(projectdir)/engine/assets/shader") + os.execv("gen_glsl.bat") + end ) \ No newline at end of file