From c6e986bb695d1be7591cd558004f53804c4569fd Mon Sep 17 00:00:00 2001 From: ouczbs Date: Tue, 26 Mar 2024 21:23:40 +0800 Subject: [PATCH] update glsl --- engine/assets/shader/gen_glsl.bat | 20 ++++++++++++ engine/assets/shader/simple.frag.spv | Bin 452 -> 460 bytes engine/assets/shader/simple.ps.glsl | 11 +++---- engine/assets/shader/simple.vert.spv | Bin 1408 -> 1388 bytes engine/assets/shader/simple.vs.glsl | 17 ++++++---- engine/src/engine/render/asset/mesh.h | 1 + engine/src/engine/render/wrapper/vertex.cpp | 0 engine/src/engine/render/wrapper/vertex.h | 8 +++++ engine/src/engine/vulkanapi/backend.cpp | 4 +++ engine/src/engine/vulkanapi/backend.h | 5 +++ engine/src/engine/vulkanapi/vulkan_struct.h | 1 + engine/src/engine/vulkanapi/vulkanapi.cpp | 12 ++++--- .../engine/vulkanapi/wrapper/commandpool.h | 1 - .../vulkanapi/wrapper/descriptorpool.cpp | 30 ++++++++++++++++++ .../engine/vulkanapi/wrapper/descriptorpool.h | 26 +++++++++++++++ engine/xmake.lua | 2 +- engine/xmake/glsl/xmake.lua | 6 ++++ 17 files changed, 125 insertions(+), 19 deletions(-) create mode 100644 engine/assets/shader/gen_glsl.bat create mode 100644 engine/src/engine/render/wrapper/vertex.cpp create mode 100644 engine/src/engine/render/wrapper/vertex.h create mode 100644 engine/src/engine/vulkanapi/wrapper/descriptorpool.cpp create mode 100644 engine/src/engine/vulkanapi/wrapper/descriptorpool.h create mode 100644 engine/xmake/glsl/xmake.lua 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 a1a14e51e7982bb704a49f55c4a7daea39b2b844..d3a34da739fba3451e4363825ef5dd2ceb1d6feb 100644 GIT binary patch delta 159 zcmX@Ye1>^~lQ0_t3j+ff=O$+60htV(6C-66I2qi1;vIvW;)_!Y5{nW`Qsav=5>rx( z;`5WTQj<%HClM0 delta 193 zcmX@Ze1v&|lL0#e3j+ff=O$+60htV(K+Fxq0u0Pxadse^83>tx^dZKHlVYsdz-sbK zOWcYQ)1C8k@{52{46H!aAbpv65FvLUA7qvsND>G@eD2AtjM1_RKo%EJgb|2Ardvb# iAl;0UXEMsG*aAgBGCV*G614-`4>AkH{|nS-0mJ|rs}z&~ 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 d36c45c25941dcf92a1743ba9500382dd6d5a4dd..b3268c239576f9da290e5381651265414ccb169b 100644 GIT binary patch literal 1388 zcmYk5-A~j&6vc<#c12JS5by)BhzbaGzvNkERo3{Az=6`15 zU*(I5=QnM2XLEDsoO|x{-kH1Y*7gNsX3V^~Y@V2EEt!@WBd+6p-22e$7Ui(}^w}Yb z1=IFIG>gh(1+ue#>e#Q#ZpwCL4`lnY16f!0Sk~6BCI3qY1ZK{}y(B(4iFt)2y7U?Gh~JXu+w|G=7HMzHk0auDwc}gVL#_Aa(Zdnnk>@OU&IdCy&G~!vp7V)= znIYnho*A0P%utj6u-@~1^k8O+IQ2Y|XC}Bi>N%7r29F-h%)vd^doo98S@iH*472!J zh`Hc>UP-}yZOFK*H&Sp<%q<#tH1u>^2LE0f?q#dyf(j$FSd(gj{pDw literal 1408 zcmYk4TWb_S5QW>lBr(y%7?XI3abwh|f#8FRASx1Nd{{^%;OnsNHfzIVW|`^r_T+E# zSNS4%zMkoIdnl^v)ak13)6;J4FB)^fteDH@(9~<)w1gO83&AJ-v;I-#M@LVeJ%O=m z+JOjXP5W3K+4;cb#2{B?w`9Ard$Rko1KDF)TVG55w;FWJvPt@Ba&nUN->1XLB(409 z%%+*kZ8~zL9eVfG!dsf-xqO&SNArZP81rZ7>g+3;G0xIcTYj>ox8Ictt#M*%MdiFJ za-oZX&z%D9a)0K2YBGE38fRARICGPCuJVI?XlFGPp*GF3S*@#k*6JA3)rTgNI8ZSd z8WmH2Tx3P5P)H|ersMi)X+w^Fc~PDZvbv6znm)F<4|R0?X5ILUuR@N-pNrg9@*jkt z11tK7)o;w}|L1W>c-$xQh{4b|QH2;@B7{5f#w_yq<}s&+F@rjHA&>jH3pq2Wa~JY@ zRww)~Gxc($vNS&xWgYIyzwicXtE(gkB>sR*Jgvs&9JQoqi5u8HyHg- zr55MCE@MA@>gf7bebMlyjCbU%Uh5>}@Hq$C-I1YN?7!Dwax?61gK>^Hb6+uBYCRb< O>Cc-{`zs}0$o>JX5^5m; 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