From 74e156179fa0c552736cd962497db170db8a7a4e Mon Sep 17 00:00:00 2001 From: Shu Date: Wed, 14 Oct 2020 12:59:24 +0200 Subject: [PATCH] Indicator pass --- resource/content/shaders/Color.fs.spv | 4 +- resource/content/shaders/Color.vs.spv | 4 +- resource/content/shaders/Sky.fs.spv | 4 +- resource/content/shaders/Sky.vs.spv | 4 +- resource/content/shaders/Tris.fs.spv | 4 +- resource/content/shaders/Tris.spv | 3 + resource/content/shaders/Tris.vs.spv | 4 +- resource/content/shaders/Voxel.fs.geo.ins.spv | 3 - resource/content/shaders/Voxel.fs.geo.spv | 3 - resource/content/shaders/Voxel.fs.ins.spv | 3 - resource/content/shaders/Voxel.fs.spv | 4 +- resource/content/shaders/Voxel.geo.fs.spv | 3 + resource/content/shaders/Voxel.geo.gs.spv | 3 + resource/content/shaders/Voxel.geo.ins.fs.spv | 3 + resource/content/shaders/Voxel.geo.ins.gs.spv | 3 + resource/content/shaders/Voxel.geo.ins.vs.spv | 3 + resource/content/shaders/Voxel.geo.vs.spv | 3 + resource/content/shaders/Voxel.gs.geo.ins.spv | 3 - resource/content/shaders/Voxel.gs.geo.spv | 3 - resource/content/shaders/Voxel.ins.fs.spv | 3 + resource/content/shaders/Voxel.ins.vs.spv | 3 + resource/content/shaders/Voxel.vs.geo.ins.spv | 3 - resource/content/shaders/Voxel.vs.geo.spv | 3 - resource/content/shaders/Voxel.vs.ins.spv | 3 - resource/content/shaders/Voxel.vs.spv | 4 +- resource/shaders-src/Color.vert | 10 +- resource/shaders-src/Sky.vert | 6 +- resource/shaders-src/Tris.vert | 18 +-- resource/shaders-src/Voxel.frag | 13 +- resource/shaders-src/Voxel.vert | 35 +++-- resource/shaders-src/compile.sh | 16 +-- src/client/render/api/Models.cpp | 16 +++ src/client/render/api/Models.hpp | 10 ++ src/client/render/gl/Renderer.cpp | 28 +--- src/client/render/gl/api/Models.hpp | 2 +- src/client/render/gl/pass/SkyProgram.cpp | 45 +----- src/client/render/vk/CommandCenter.cpp | 81 ++++++----- src/client/render/vk/CommandCenter.hpp | 7 +- src/client/render/vk/Pipeline.cpp | 134 ++++++++++++++++-- src/client/render/vk/Pipeline.hpp | 4 + src/client/render/vk/Renderer.cpp | 2 +- src/client/render/vk/api/Models.cpp | 21 +++ src/client/render/vk/api/Models.hpp | 77 +++++++--- src/client/render/vk/buffer/VertexData.hpp | 24 ---- 44 files changed, 376 insertions(+), 256 deletions(-) create mode 100644 resource/content/shaders/Tris.spv delete mode 100644 resource/content/shaders/Voxel.fs.geo.ins.spv delete mode 100644 resource/content/shaders/Voxel.fs.geo.spv delete mode 100644 resource/content/shaders/Voxel.fs.ins.spv create mode 100644 resource/content/shaders/Voxel.geo.fs.spv create mode 100644 resource/content/shaders/Voxel.geo.gs.spv create mode 100644 resource/content/shaders/Voxel.geo.ins.fs.spv create mode 100644 resource/content/shaders/Voxel.geo.ins.gs.spv create mode 100644 resource/content/shaders/Voxel.geo.ins.vs.spv create mode 100644 resource/content/shaders/Voxel.geo.vs.spv delete mode 100644 resource/content/shaders/Voxel.gs.geo.ins.spv delete mode 100644 resource/content/shaders/Voxel.gs.geo.spv create mode 100644 resource/content/shaders/Voxel.ins.fs.spv create mode 100644 resource/content/shaders/Voxel.ins.vs.spv delete mode 100644 resource/content/shaders/Voxel.vs.geo.ins.spv delete mode 100644 resource/content/shaders/Voxel.vs.geo.spv delete mode 100644 resource/content/shaders/Voxel.vs.ins.spv diff --git a/resource/content/shaders/Color.fs.spv b/resource/content/shaders/Color.fs.spv index b148912..723bfdb 100644 --- a/resource/content/shaders/Color.fs.spv +++ b/resource/content/shaders/Color.fs.spv @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5311d6e728a8130264f2142f937cc4f893757bc5820bc3258c7b17b0b79f2e2f -size 632 +oid sha256:903412f9daaff692f7101131acaea1cfac4f8087cd74c9caa0581eaa513182d4 +size 560 diff --git a/resource/content/shaders/Color.vs.spv b/resource/content/shaders/Color.vs.spv index 1bcadce..8dd2e99 100644 --- a/resource/content/shaders/Color.vs.spv +++ b/resource/content/shaders/Color.vs.spv @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:de2683c8c34c4c6d1f340ae75cf561e0b36b8c87b66c08374df336d35d1ced76 -size 1316 +oid sha256:bf2765ba17634879c081b1b65a885d1c4f47bd6b67b600e511b6d9e4257f3204 +size 1672 diff --git a/resource/content/shaders/Sky.fs.spv b/resource/content/shaders/Sky.fs.spv index 72d7b55..078eab8 100644 --- a/resource/content/shaders/Sky.fs.spv +++ b/resource/content/shaders/Sky.fs.spv @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:fd7c280636d4b76039fa80bcb2a5dc96a50b0f5281bb52a8d4b4e501fb6f2eb6 -size 656 +oid sha256:b6d9789511b9333fd6e77ab2b2275b4287700a40de0ed78c193c70d3ee7a45d0 +size 584 diff --git a/resource/content/shaders/Sky.vs.spv b/resource/content/shaders/Sky.vs.spv index 9245725..a9e7abe 100644 --- a/resource/content/shaders/Sky.vs.spv +++ b/resource/content/shaders/Sky.vs.spv @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:828ef16d9df0c78afdff93f7c0873e50fe162d236caed3ca1a4606f047704df3 -size 1664 +oid sha256:535c3e8fbcef391cc18e00729c86433c0181afc8ebfd7eb31be91cc7fe15bb2f +size 1592 diff --git a/resource/content/shaders/Tris.fs.spv b/resource/content/shaders/Tris.fs.spv index a103cb7..104c328 100644 --- a/resource/content/shaders/Tris.fs.spv +++ b/resource/content/shaders/Tris.fs.spv @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4b32ce588c35a63f6b6ff88860385d1afca26ba07270ce602b0fd55db589e619 -size 764 +oid sha256:4a3a54d2d67c04901f050ad92f28b9956cbf6982f748e23846538fb67836dca3 +size 692 diff --git a/resource/content/shaders/Tris.spv b/resource/content/shaders/Tris.spv new file mode 100644 index 0000000..104c328 --- /dev/null +++ b/resource/content/shaders/Tris.spv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4a3a54d2d67c04901f050ad92f28b9956cbf6982f748e23846538fb67836dca3 +size 692 diff --git a/resource/content/shaders/Tris.vs.spv b/resource/content/shaders/Tris.vs.spv index 1185d02..4cfd8f0 100644 --- a/resource/content/shaders/Tris.vs.spv +++ b/resource/content/shaders/Tris.vs.spv @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4690f6b8744b857929110d5057255ce2fe1266275f1b0fdb581bf0beff4e08a3 -size 1964 +oid sha256:4738b854a30f192ee79b7ffb22baad0f9231f1dd9b5495ce8d09001da0182ddd +size 1900 diff --git a/resource/content/shaders/Voxel.fs.geo.ins.spv b/resource/content/shaders/Voxel.fs.geo.ins.spv deleted file mode 100644 index 762ebb0..0000000 --- a/resource/content/shaders/Voxel.fs.geo.ins.spv +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:7bd1d2f677b8714ab6c4a7d6921b60c1b047403b59a1982b5292f70a532fee2d -size 20640 diff --git a/resource/content/shaders/Voxel.fs.geo.spv b/resource/content/shaders/Voxel.fs.geo.spv deleted file mode 100644 index 762ebb0..0000000 --- a/resource/content/shaders/Voxel.fs.geo.spv +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:7bd1d2f677b8714ab6c4a7d6921b60c1b047403b59a1982b5292f70a532fee2d -size 20640 diff --git a/resource/content/shaders/Voxel.fs.ins.spv b/resource/content/shaders/Voxel.fs.ins.spv deleted file mode 100644 index e0992ae..0000000 --- a/resource/content/shaders/Voxel.fs.ins.spv +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:2f50de047dcdd53f99c20a86db1c2945c71455112e5e47d9a07aad36f803d31e -size 17528 diff --git a/resource/content/shaders/Voxel.fs.spv b/resource/content/shaders/Voxel.fs.spv index e0992ae..ec1b4b1 100644 --- a/resource/content/shaders/Voxel.fs.spv +++ b/resource/content/shaders/Voxel.fs.spv @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:2f50de047dcdd53f99c20a86db1c2945c71455112e5e47d9a07aad36f803d31e -size 17528 +oid sha256:def10c8004463218189138021e9d662bdbc3d0982bad4bbc7ba422886944a6e5 +size 17432 diff --git a/resource/content/shaders/Voxel.geo.fs.spv b/resource/content/shaders/Voxel.geo.fs.spv new file mode 100644 index 0000000..06859ba --- /dev/null +++ b/resource/content/shaders/Voxel.geo.fs.spv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7b17eaf996b876fe3e2032aa63aab17fe4ab12f531ef02f26071890a6570b397 +size 20544 diff --git a/resource/content/shaders/Voxel.geo.gs.spv b/resource/content/shaders/Voxel.geo.gs.spv new file mode 100644 index 0000000..65bc051 --- /dev/null +++ b/resource/content/shaders/Voxel.geo.gs.spv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0307afe1bf8c529e8714dd7727a51fb1e0cb5dc861991a4b9f823ad86a42c12e +size 3700 diff --git a/resource/content/shaders/Voxel.geo.ins.fs.spv b/resource/content/shaders/Voxel.geo.ins.fs.spv new file mode 100644 index 0000000..06859ba --- /dev/null +++ b/resource/content/shaders/Voxel.geo.ins.fs.spv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7b17eaf996b876fe3e2032aa63aab17fe4ab12f531ef02f26071890a6570b397 +size 20544 diff --git a/resource/content/shaders/Voxel.geo.ins.gs.spv b/resource/content/shaders/Voxel.geo.ins.gs.spv new file mode 100644 index 0000000..65bc051 --- /dev/null +++ b/resource/content/shaders/Voxel.geo.ins.gs.spv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0307afe1bf8c529e8714dd7727a51fb1e0cb5dc861991a4b9f823ad86a42c12e +size 3700 diff --git a/resource/content/shaders/Voxel.geo.ins.vs.spv b/resource/content/shaders/Voxel.geo.ins.vs.spv new file mode 100644 index 0000000..d1ed5f8 --- /dev/null +++ b/resource/content/shaders/Voxel.geo.ins.vs.spv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:969679c552d49afc4dce2b5eb39aa88795b19adc371474405983d457afa5dc5d +size 5588 diff --git a/resource/content/shaders/Voxel.geo.vs.spv b/resource/content/shaders/Voxel.geo.vs.spv new file mode 100644 index 0000000..319102c --- /dev/null +++ b/resource/content/shaders/Voxel.geo.vs.spv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e04354761dd0a887d5cc97ecb1dd8ab4bef6f66796aeedf58bf27f48b39c926d +size 5712 diff --git a/resource/content/shaders/Voxel.gs.geo.ins.spv b/resource/content/shaders/Voxel.gs.geo.ins.spv deleted file mode 100644 index b177fcb..0000000 --- a/resource/content/shaders/Voxel.gs.geo.ins.spv +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ff0fe922ae4878d89d51e9356617691561d3820a0c678530945add2da6c3d473 -size 3772 diff --git a/resource/content/shaders/Voxel.gs.geo.spv b/resource/content/shaders/Voxel.gs.geo.spv deleted file mode 100644 index b177fcb..0000000 --- a/resource/content/shaders/Voxel.gs.geo.spv +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ff0fe922ae4878d89d51e9356617691561d3820a0c678530945add2da6c3d473 -size 3772 diff --git a/resource/content/shaders/Voxel.ins.fs.spv b/resource/content/shaders/Voxel.ins.fs.spv new file mode 100644 index 0000000..ec1b4b1 --- /dev/null +++ b/resource/content/shaders/Voxel.ins.fs.spv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:def10c8004463218189138021e9d662bdbc3d0982bad4bbc7ba422886944a6e5 +size 17432 diff --git a/resource/content/shaders/Voxel.ins.vs.spv b/resource/content/shaders/Voxel.ins.vs.spv new file mode 100644 index 0000000..d1ed5f8 --- /dev/null +++ b/resource/content/shaders/Voxel.ins.vs.spv @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:969679c552d49afc4dce2b5eb39aa88795b19adc371474405983d457afa5dc5d +size 5588 diff --git a/resource/content/shaders/Voxel.vs.geo.ins.spv b/resource/content/shaders/Voxel.vs.geo.ins.spv deleted file mode 100644 index bb74c08..0000000 --- a/resource/content/shaders/Voxel.vs.geo.ins.spv +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:3cba8a7d42afcb5d943ccb89b57e5cb25876fc30e5ad879e25f91e391e8990bb -size 5716 diff --git a/resource/content/shaders/Voxel.vs.geo.spv b/resource/content/shaders/Voxel.vs.geo.spv deleted file mode 100644 index 33a7bd3..0000000 --- a/resource/content/shaders/Voxel.vs.geo.spv +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:3401b8a047bdff2ba0a2a697bf621d7c44b5ca64977a01009e8064603983c21f -size 5840 diff --git a/resource/content/shaders/Voxel.vs.ins.spv b/resource/content/shaders/Voxel.vs.ins.spv deleted file mode 100644 index bb74c08..0000000 --- a/resource/content/shaders/Voxel.vs.ins.spv +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:3cba8a7d42afcb5d943ccb89b57e5cb25876fc30e5ad879e25f91e391e8990bb -size 5716 diff --git a/resource/content/shaders/Voxel.vs.spv b/resource/content/shaders/Voxel.vs.spv index 33a7bd3..319102c 100644 --- a/resource/content/shaders/Voxel.vs.spv +++ b/resource/content/shaders/Voxel.vs.spv @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:3401b8a047bdff2ba0a2a697bf621d7c44b5ca64977a01009e8064603983c21f -size 5840 +oid sha256:e04354761dd0a887d5cc97ecb1dd8ab4bef6f66796aeedf58bf27f48b39c926d +size 5712 diff --git a/resource/shaders-src/Color.vert b/resource/shaders-src/Color.vert index 4f17928..dd6f06b 100644 --- a/resource/shaders-src/Color.vert +++ b/resource/shaders-src/Color.vert @@ -1,7 +1,11 @@ #version 450 core -layout(push_constant) uniform PushConstants { - mat4 MVP; +layout(binding = 0) uniform UniformBufferObject { + mat4 view; + mat4 proj; +} UBO; +layout(push_constant) uniform PushConst { + mat4 model; } Push; layout(location = 0) in vec3 Position_modelspace; @@ -10,7 +14,7 @@ layout(location = 1) in vec4 Color_model; layout(location = 0) out vec4 Color; void main(){ - gl_Position = Push.MVP * vec4(Position_modelspace, 1); + gl_Position = UBO.proj * UBO.view * Push.model * vec4(Position_modelspace, 1); Color = Color_model; } diff --git a/resource/shaders-src/Sky.vert b/resource/shaders-src/Sky.vert index 6b99e2a..9ddab44 100644 --- a/resource/shaders-src/Sky.vert +++ b/resource/shaders-src/Sky.vert @@ -3,7 +3,7 @@ layout(binding = 0) uniform UniformBufferObject { mat4 view; mat4 proj; -} ubo; +} UBO; layout (location = 0) in vec3 Position_modelspace; @@ -11,7 +11,7 @@ layout (location = 0) out vec3 UV; void main(){ UV = Position_modelspace; - mat4 view = ubo.view; + mat4 view = UBO.view; view[3] = vec4(0.0f, 0.0f, 0.0f, 1.0f); - gl_Position = (ubo.proj * view * vec4(Position_modelspace, 1.0)).xyww; + gl_Position = (UBO.proj * view * vec4(Position_modelspace, 1.0)).xyww; } \ No newline at end of file diff --git a/resource/shaders-src/Tris.vert b/resource/shaders-src/Tris.vert index 9e36d40..da3c3ed 100644 --- a/resource/shaders-src/Tris.vert +++ b/resource/shaders-src/Tris.vert @@ -4,20 +4,20 @@ layout(binding = 0) uniform UniformBufferObject { mat4 view; mat4 proj; -} ubo; +} UBO; layout(push_constant) uniform PushConst { mat4 model; } Push; -layout(location = 0) in vec3 inPosition; -layout(location = 1) in vec3 inColor; -layout(location = 2) in vec2 inTexCoord; +layout(location = 0) in vec3 Position_modelspace; +layout(location = 1) in vec3 Color_model; +layout(location = 2) in vec2 TexCoord_model; -layout(location = 0) out vec3 fragColor; -layout(location = 1) out vec2 fragTexCoord; +layout(location = 0) out vec3 Color; +layout(location = 1) out vec2 TexCoord; void main() { - gl_Position = ubo.proj * ubo.view * Push.model * vec4(inPosition, 1.0); - fragColor = inColor; - fragTexCoord = inTexCoord; + gl_Position = UBO.proj * UBO.view * Push.model * vec4(Position_modelspace, 1.0); + Color = Color_model; + TexCoord = TexCoord_model; } \ No newline at end of file diff --git a/resource/shaders-src/Voxel.frag b/resource/shaders-src/Voxel.frag index de62715..086eb0f 100644 --- a/resource/shaders-src/Voxel.frag +++ b/resource/shaders-src/Voxel.frag @@ -10,13 +10,12 @@ layout (constant_id = 4) const bool BLEND = true; layout (constant_id = 16) const int UNIT_SIZE = 8; layout (binding = 0) uniform UniformBufferObject { - mat4 Proj; - mat4 View; + mat4 view; + mat4 proj; - vec3 LightInvDirection_worldspace; + vec3 lightInvDirection_worldspace; - vec3 FogColor; - float FogDepth; + vec4 fog; //color.rgb depth.a } UBO; layout (binding = 1) uniform sampler2DArray TextureAtlas; layout (binding = 2) uniform sampler2DArray NormalAtlas; @@ -181,7 +180,7 @@ if(PBR) { vec3 TextureAmbientColor = vec3(.1) * TextureDiffuseColor * texHOS.y; vec3 TextureSpecularColor = vec3(.8) * texHOS.z; - vec3 Normal_cameraspace = normalize((UBO.View * vec4(worldNormal,0)).xyz); + vec3 Normal_cameraspace = normalize((UBO.view * vec4(worldNormal,0)).xyz); // Light emission properties // You probably want to put them as uniforms @@ -226,7 +225,7 @@ if(PBR) { } if(FOG) { float ratio = exp(vs.Depth * 0.69)-1; - color = mix(color, pow(UBO.FogColor, vec3(2.2)), clamp(ratio, 0, 1)); + color = mix(color, pow(UBO.fog.rgb, vec3(2.2)), clamp(ratio, 0, 1)); } color = pow(color, vec3(1.0 / 2.2)); if(color.r > 1 || color.g > 1 || color.b > 1) { diff --git a/resource/shaders-src/Voxel.vert b/resource/shaders-src/Voxel.vert index 9e9cbb0..0cdfdcf 100644 --- a/resource/shaders-src/Voxel.vert +++ b/resource/shaders-src/Voxel.vert @@ -8,22 +8,21 @@ layout (constant_id = 5) const bool DO_CURVATURE = false; layout (constant_id = 6) const bool CURV_DEPTH = true; layout (binding = 0) uniform UniformBufferObject { - mat4 Proj; - mat4 View; + mat4 view; + mat4 proj; - vec3 LightInvDirection_worldspace; + vec3 lightInvDirection_worldspace; - vec3 FogColor; - float FogDepth; + vec4 fog; //color.rgb depth.a } UBO; -layout(push_constant) uniform PushConstants { +layout(push_constant) uniform PushConst { #ifndef INSTANCED - mat4 Model; + mat4 model; #endif - vec4 SphereProj; - float Curvature; + vec4 sphereProj; + float curvature; } Push; layout (location = 0) in vec3 Position_modelspace; @@ -48,27 +47,27 @@ layout (location = 0) out VertexData { void main(){ #ifndef INSTANCED - mat4 Model = Push.Model; + mat4 Model = Push.model; #endif vs.Position_modelspace = Position_modelspace; if(DO_CURVATURE) { - if(Push.Curvature > 0) { - vec3 Position_areaspace = Position_modelspace + Push.SphereProj.xyz; + if(Push.curvature > 0) { + vec3 Position_areaspace = Position_modelspace + Push.sphereProj.xyz; vec2 sph = vec2(acos(Position_areaspace.z / length(Position_areaspace.xyz)), atan(Position_areaspace.y, Position_areaspace.x)); float radius = CURV_DEPTH ? max(max(abs(Position_areaspace.x), abs(Position_areaspace.y)), abs(Position_areaspace.z)) : - Push.SphereProj.w; - vs.Position_modelspace = mix(vs.Position_modelspace, vec3(sin(sph.x)*cos(sph.y), sin(sph.x)*sin(sph.y), cos(sph.x)) * radius - Push.SphereProj.xyz, Push.Curvature); + Push.sphereProj.w; + vs.Position_modelspace = mix(vs.Position_modelspace, vec3(sin(sph.x)*cos(sph.y), sin(sph.x)*sin(sph.y), cos(sph.x)) * radius - Push.sphereProj.xyz, Push.curvature); } } - vec4 Position_cameraspace = UBO.View * Model * vec4(vs.Position_modelspace, 1); - gl_Position = UBO.Proj * Position_cameraspace; + vec4 Position_cameraspace = UBO.view * Model * vec4(vs.Position_modelspace, 1); + gl_Position = UBO.proj * Position_cameraspace; if(FOG) { - vs.Depth = length(Position_cameraspace.xyz) / UBO.FogDepth; + vs.Depth = length(Position_cameraspace.xyz) / UBO.fog.a; } vs.Texture = Texture_model; @@ -83,6 +82,6 @@ if(PBR) { vs.EyeDirection_cameraspace = vec3(0,0,0) - Position_cameraspace.xyz; // Vector that goes from the vertex to the light, in camera space - vs.LightDirection_cameraspace = (UBO.View * vec4(UBO.LightInvDirection_worldspace,0)).xyz; + vs.LightDirection_cameraspace = (UBO.view * vec4(UBO.lightInvDirection_worldspace,0)).xyz; } } diff --git a/resource/shaders-src/compile.sh b/resource/shaders-src/compile.sh index 3fa18a6..4c62652 100755 --- a/resource/shaders-src/compile.sh +++ b/resource/shaders-src/compile.sh @@ -22,12 +22,12 @@ $GLSL $BASEDIR/Sky.frag -o $TARGETDIR/Sky.fs.spv # Voxel $GLSL $BASEDIR/Voxel.vert -o $TARGETDIR/Voxel.vs.spv $GLSL $BASEDIR/Voxel.frag -o $TARGETDIR/Voxel.fs.spv -$GLSL $BASEDIR/Voxel.vert -DINSTANCED -o $TARGETDIR/Voxel.vs.ins.spv -$GLSL $BASEDIR/Voxel.frag -DINSTANCED -o $TARGETDIR/Voxel.fs.ins.spv +$GLSL $BASEDIR/Voxel.vert -DINSTANCED -o $TARGETDIR/Voxel.ins.vs.spv +$GLSL $BASEDIR/Voxel.frag -DINSTANCED -o $TARGETDIR/Voxel.ins.fs.spv -$GLSL $BASEDIR/Voxel.vert -DGEOMETRY -o $TARGETDIR/Voxel.vs.geo.spv -$GLSL $BASEDIR/Voxel.geom -DGEOMETRY -o $TARGETDIR/Voxel.gs.geo.spv -$GLSL $BASEDIR/Voxel.frag -DGEOMETRY -o $TARGETDIR/Voxel.fs.geo.spv -$GLSL $BASEDIR/Voxel.vert -DGEOMETRY -DINSTANCED -o $TARGETDIR/Voxel.vs.geo.ins.spv -$GLSL $BASEDIR/Voxel.geom -DGEOMETRY -DINSTANCED -o $TARGETDIR/Voxel.gs.geo.ins.spv -$GLSL $BASEDIR/Voxel.frag -DGEOMETRY -DINSTANCED -o $TARGETDIR/Voxel.fs.geo.ins.spv \ No newline at end of file +$GLSL $BASEDIR/Voxel.vert -DGEOMETRY -o $TARGETDIR/Voxel.geo.vs.spv +$GLSL $BASEDIR/Voxel.geom -DGEOMETRY -o $TARGETDIR/Voxel.geo.gs.spv +$GLSL $BASEDIR/Voxel.frag -DGEOMETRY -o $TARGETDIR/Voxel.geo.fs.spv +$GLSL $BASEDIR/Voxel.vert -DGEOMETRY -DINSTANCED -o $TARGETDIR/Voxel.geo.ins.vs.spv +$GLSL $BASEDIR/Voxel.geom -DGEOMETRY -DINSTANCED -o $TARGETDIR/Voxel.geo.ins.gs.spv +$GLSL $BASEDIR/Voxel.frag -DGEOMETRY -DINSTANCED -o $TARGETDIR/Voxel.geo.ins.fs.spv \ No newline at end of file diff --git a/src/client/render/api/Models.cpp b/src/client/render/api/Models.cpp index d3ae584..6fb44bd 100644 --- a/src/client/render/api/Models.cpp +++ b/src/client/render/api/Models.cpp @@ -7,6 +7,22 @@ using namespace render; std::unique_ptr (*Model::createFunc)(const Data&) = nullptr; std::unique_ptr (*LodModel::createFunc)(const LodData&) = nullptr; +const std::vector Shape::SKY_CUBE = { + {-1.0f, 1.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, { 1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, -1.0f}, + {-1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, -1.0f}, {-1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, + { 1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, + {-1.0f, -1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, + {-1.0f, 1.0f, -1.0f}, { 1.0f, 1.0f, -1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, -1.0f}, + {-1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, 1.0f} +}; +const std::pair, std::vector> Indicator::CUBE = {{ + {0, 0, 0}, {0, 0, 1}, {0, 0, 1}, {0, 1, 1}, {0, 1, 1}, {0, 1, 0}, {0, 1, 0}, {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {1, 0, 1}, {1, 1, 1}, + {1, 1, 1}, {1, 1, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 0}, {1, 0, 0}, {0, 0, 1}, {1, 0, 1}, {0, 1, 1}, {1, 1, 1}, {0, 1, 0}, {1, 1, 0} +}, { + {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, + {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1} +}}; + void indexVBO(const std::vector &in_vertices, std::vector &out_indices, std::vector &out_vertices); Model::Data::Data(const std::vector &vs, const std::vector &indices): indices(indices), vertices(vs) { } diff --git a/src/client/render/api/Models.hpp b/src/client/render/api/Models.hpp index 8109ff4..b701c9b 100644 --- a/src/client/render/api/Models.hpp +++ b/src/client/render/api/Models.hpp @@ -39,10 +39,20 @@ struct PackedVertexData { }; }; +/// Position only buffer +class Shape { +public: + virtual ~Shape() { } + + static const std::vector SKY_CUBE; +}; + /// Color lines model class Indicator { public: virtual ~Indicator() { } + + static const std::pair, std::vector> CUBE; }; /// VertexData model with index diff --git a/src/client/render/gl/Renderer.cpp b/src/client/render/gl/Renderer.cpp index c4df42f..dcc90a9 100644 --- a/src/client/render/gl/Renderer.cpp +++ b/src/client/render/gl/Renderer.cpp @@ -15,33 +15,7 @@ constexpr auto GL_MAJOR = 4; constexpr auto GL_MINOR = 6; Renderer::Renderer(const renderOptions& options): - IndicatorCubeBuffer({ - glm::vec3(0, 0, 0), glm::vec3(0, 0, 1), - glm::vec3(0, 0, 1), glm::vec3(0, 1, 1), - glm::vec3(0, 1, 1), glm::vec3(0, 1, 0), - glm::vec3(0, 1, 0), glm::vec3(0, 0, 0), - glm::vec3(1, 0, 0), glm::vec3(1, 0, 1), - glm::vec3(1, 0, 1), glm::vec3(1, 1, 1), - glm::vec3(1, 1, 1), glm::vec3(1, 1, 0), - glm::vec3(1, 1, 0), glm::vec3(1, 0, 0), - glm::vec3(0, 0, 0), glm::vec3(1, 0, 0), - glm::vec3(0, 0, 1), glm::vec3(1, 0, 1), - glm::vec3(0, 1, 1), glm::vec3(1, 1, 1), - glm::vec3(0, 1, 0), glm::vec3(1, 1, 0), - }, { - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1), - }) { + IndicatorCubeBuffer(Indicator::CUBE.first, Indicator::CUBE.second) { glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); diff --git a/src/client/render/gl/api/Models.hpp b/src/client/render/gl/api/Models.hpp index 89d4356..7700ea3 100644 --- a/src/client/render/gl/api/Models.hpp +++ b/src/client/render/gl/api/Models.hpp @@ -8,7 +8,7 @@ namespace render::gl { /// Positions only buffer -class Shape final { +class Shape final: public render::Shape { public: Shape(const std::vector&); ~Shape(); diff --git a/src/client/render/gl/pass/SkyProgram.cpp b/src/client/render/gl/pass/SkyProgram.cpp index 792ee8b..f87784e 100644 --- a/src/client/render/gl/pass/SkyProgram.cpp +++ b/src/client/render/gl/pass/SkyProgram.cpp @@ -4,50 +4,7 @@ using namespace pass; -const std::vector g_cubemap_vertices = { - {-1.0f, 1.0f, -1.0f}, - {-1.0f, -1.0f, -1.0f}, - { 1.0f, -1.0f, -1.0f}, - { 1.0f, -1.0f, -1.0f}, - { 1.0f, 1.0f, -1.0f}, - {-1.0f, 1.0f, -1.0f}, - - {-1.0f, -1.0f, 1.0f}, - {-1.0f, -1.0f, -1.0f}, - {-1.0f, 1.0f, -1.0f}, - {-1.0f, 1.0f, -1.0f}, - {-1.0f, 1.0f, 1.0f}, - {-1.0f, -1.0f, 1.0f}, - - { 1.0f, -1.0f, -1.0f}, - { 1.0f, -1.0f, 1.0f}, - { 1.0f, 1.0f, 1.0f}, - { 1.0f, 1.0f, 1.0f}, - { 1.0f, 1.0f, -1.0f}, - { 1.0f, -1.0f, -1.0f}, - - {-1.0f, -1.0f, 1.0f}, - {-1.0f, 1.0f, 1.0f}, - { 1.0f, 1.0f, 1.0f}, - { 1.0f, 1.0f, 1.0f}, - { 1.0f, -1.0f, 1.0f}, - {-1.0f, -1.0f, 1.0f}, - - {-1.0f, 1.0f, -1.0f}, - { 1.0f, 1.0f, -1.0f}, - { 1.0f, 1.0f, 1.0f}, - { 1.0f, 1.0f, 1.0f}, - {-1.0f, 1.0f, 1.0f}, - {-1.0f, 1.0f, -1.0f}, - - {-1.0f, -1.0f, -1.0f}, - {-1.0f, -1.0f, 1.0f}, - { 1.0f, -1.0f, -1.0f}, - { 1.0f, -1.0f, -1.0f}, - {-1.0f, -1.0f, 1.0f}, - { 1.0f, -1.0f, 1.0f} -}; -SkyProgram::SkyProgram(): Program(), CubeBuffer(g_cubemap_vertices) { +SkyProgram::SkyProgram(): Program(), CubeBuffer(render::gl::Shape::SKY_CUBE) { std::vector flags; std::vector shaders; diff --git a/src/client/render/vk/CommandCenter.cpp b/src/client/render/vk/CommandCenter.cpp index fb773d5..f76e926 100644 --- a/src/client/render/vk/CommandCenter.cpp +++ b/src/client/render/vk/CommandCenter.cpp @@ -32,14 +32,12 @@ CommandCenter::CommandCenter(VkDevice device, const PhysicalDeviceInfo &info, co FATAL("Failed to create texture sampler!"); } - skyCubeBuffer = Shape::Create({ - {-1.0f, 1.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, { 1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, -1.0f}, - {-1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, -1.0f}, {-1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, - { 1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, - {-1.0f, -1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, - {-1.0f, 1.0f, -1.0f}, { 1.0f, 1.0f, -1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, -1.0f}, - {-1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, 1.0f} - }); + indicCubeBuffer = Indicator::Create(Indicator::CUBE.first, Indicator::CUBE.second); + if (!indicCubeBuffer) { + FATAL("Failed to create vertex buffer!"); + } + + skyCubeBuffer = Shape::Create(Shape::SKY_CUBE); if (!skyCubeBuffer) { FATAL("Failed to create vertex buffer!"); } @@ -99,13 +97,13 @@ void CommandCenter::allocate(const std::vector& views, const Pipeli { // Uniform buffers std::vector requirements; - requirements.resize(framebuffers.size(), Buffer::requirement(sizeof(buffer::vk::UniformBufferObject), Buffer::Usage::UNIFORM)); + requirements.resize(framebuffers.size(), Buffer::requirement(sizeof(UniformBufferObject), Buffer::Usage::UNIFORM)); uniformBuffers.allocate(requirements, true); if (!uniformBuffers) { FATAL("Failed to allocate UBO"); } } - { // Voxel Descriptor pool + { // Descriptor pools std::array poolSizes{}; poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; poolSizes[0].descriptorCount = framebuffers.size(); @@ -126,34 +124,38 @@ void CommandCenter::allocate(const std::vector& views, const Pipeli if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &skyDescriptorPool) != VK_SUCCESS) { FATAL("Failed to create descriptor pool!"); } + + poolInfo.poolSizeCount = 1; + poolInfo.pPoolSizes = &poolSizes[0]; + + if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &indicDescriptorPool) != VK_SUCCESS) { + FATAL("Failed to create descriptor pool!"); + } } { // Descriptor sets - std::vector voxLayouts(framebuffers.size(), pipe.getVoxelDescriptorSet()); VkDescriptorSetAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - allocInfo.descriptorPool = voxelDescriptorPool; allocInfo.descriptorSetCount = framebuffers.size(); - allocInfo.pSetLayouts = voxLayouts.data(); + + auto allocSets = [&](VkDescriptorPool pool, VkDescriptorSetLayout layout, std::vector& out) { + std::vector layouts(framebuffers.size(), layout); + allocInfo.descriptorPool = pool; + allocInfo.pSetLayouts = layouts.data(); - voxelDescriptorSets.resize(framebuffers.size()); - if (vkAllocateDescriptorSets(device, &allocInfo, voxelDescriptorSets.data()) != VK_SUCCESS) { - FATAL("Failed to allocate descriptor sets!"); - } - - std::vector skyLayouts(framebuffers.size(), pipe.getSkyDescriptorSet()); - allocInfo.descriptorPool = skyDescriptorPool; - allocInfo.pSetLayouts = skyLayouts.data(); - - skyDescriptorSets.resize(framebuffers.size()); - if (vkAllocateDescriptorSets(device, &allocInfo, skyDescriptorSets.data()) != VK_SUCCESS) { - FATAL("Failed to allocate descriptor sets!"); - } + out.resize(framebuffers.size()); + if (vkAllocateDescriptorSets(device, &allocInfo, out.data()) != VK_SUCCESS) { + FATAL("Failed to allocate descriptor sets!"); + } + }; + allocSets(voxelDescriptorPool, pipe.getVoxelDescriptorSet(), voxelDescriptorSets); + allocSets(indicDescriptorPool, pipe.getIndicDescriptorSet(), indicDescriptorSets); + allocSets(skyDescriptorPool, pipe.getSkyDescriptorSet(), skyDescriptorSets); for (size_t i = 0; i < voxelDescriptorSets.size(); i++) { VkDescriptorBufferInfo bufferInfo{}; bufferInfo.buffer = uniformBuffers.at(i); bufferInfo.offset = 0; - bufferInfo.range = sizeof(buffer::vk::UniformBufferObject); + bufferInfo.range = sizeof(UniformBufferObject); std::array descriptorWrites{}; descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; @@ -178,6 +180,9 @@ void CommandCenter::allocate(const std::vector& views, const Pipeli descriptorWrites[1].dstSet = skyDescriptorSets[i]; descriptorWrites[1].pImageInfo = &skyboxTexture->getDescriptor(); vkUpdateDescriptorSets(device, descriptorWrites.size(), descriptorWrites.data(), 0, nullptr); + + descriptorWrites[0].dstSet = indicDescriptorSets[i]; + vkUpdateDescriptorSets(device, 1, &descriptorWrites[0], 0, nullptr); } } { @@ -193,9 +198,6 @@ void CommandCenter::allocate(const std::vector& views, const Pipeli } } - proj = glm::perspectiveZO(glm::radians(45.0f), extent.width / (float) extent.height, 0.1f, 10.0f); - proj[1][1] *= -1; - freed = false; } void CommandCenter::free() { @@ -203,6 +205,7 @@ void CommandCenter::free() { vkFreeCommandBuffers(device, graphicsPool, static_cast(graphicsBuffers.size()), graphicsBuffers.data()); vkDestroyDescriptorPool(device, voxelDescriptorPool, ALLOC); + vkDestroyDescriptorPool(device, indicDescriptorPool, ALLOC); vkDestroyDescriptorPool(device, skyDescriptorPool, ALLOC); colorbuffer.reset(); @@ -219,7 +222,7 @@ void CommandCenter::free() { #include void CommandCenter::startRecording(uint32_t idx, VkRenderPass renderPass, VkExtent2D extent, const glm::vec4& clear_color, glm::mat4 view, glm::mat4 proj) { - buffer::vk::UniformBufferObject ubo{}; + UniformBufferObject ubo{}; ubo.view = view; ubo.proj = proj; uniformBuffers.write(idx, data_view(&ubo, sizeof(ubo))); @@ -258,7 +261,7 @@ void CommandCenter::startWorldPass(uint32_t idx, const Subpass &worldPass) { auto currentTime = std::chrono::high_resolution_clock::now(); float time = std::chrono::duration(currentTime - startTime).count(); - buffer::vk::ModelPush push{}; + ModelPush push{}; push.model = glm::translate(glm::mat4(1.0f), glm::vec3(98.0f, -2.f, -2.f)); push.model = glm::rotate(push.model, time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); vkCmdPushConstants(graphicsBuffers[idx], worldPass.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(push), &push); @@ -271,7 +274,19 @@ void CommandCenter::recordModel(uint32_t i) { vkCmdDrawIndexed(graphicsBuffers[i], static_cast(buffer::vk::indices.size()), 1, 0, 0, 0); } void CommandCenter::startEntityPass(uint32_t) { } -void CommandCenter::recordIndicator(uint32_t, glm::mat4) { } +void CommandCenter::recordIndicator(uint32_t idx, const Subpass& indicPass, glm::mat4 model) { + vkCmdNextSubpass(graphicsBuffers[idx], VK_SUBPASS_CONTENTS_INLINE); + vkCmdBindPipeline(graphicsBuffers[idx], VK_PIPELINE_BIND_POINT_GRAPHICS, indicPass.pipeline); + vkCmdBindDescriptorSets(graphicsBuffers[idx], VK_PIPELINE_BIND_POINT_GRAPHICS, indicPass.layout, 0, 1, &indicDescriptorSets[idx], 0, nullptr); + + ModelPush push{model}; + vkCmdPushConstants(graphicsBuffers[idx], indicPass.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(push), &push); + + VkBuffer vertexBuffers[] = {indicCubeBuffer->getRef()}; + VkDeviceSize offsets[] = {0}; + vkCmdBindVertexBuffers(graphicsBuffers[idx], 0, 1, vertexBuffers, offsets); + vkCmdDraw(graphicsBuffers[idx], indicCubeBuffer->size, 1, 0, 0); +} void CommandCenter::recordPostprocess(uint32_t idx, const Subpass& skyPass, bool skybox, glm::mat4, glm::mat4) { vkCmdNextSubpass(graphicsBuffers[idx], VK_SUBPASS_CONTENTS_INLINE); if (skybox) { diff --git a/src/client/render/vk/CommandCenter.hpp b/src/client/render/vk/CommandCenter.hpp index ab02c9c..fc7ccef 100644 --- a/src/client/render/vk/CommandCenter.hpp +++ b/src/client/render/vk/CommandCenter.hpp @@ -21,7 +21,7 @@ public: void startWorldPass(uint32_t idx, const Subpass&); void recordModel(uint32_t idx); void startEntityPass(uint32_t idx); - void recordIndicator(uint32_t idx, glm::mat4 model); + void recordIndicator(uint32_t idx, const Subpass&, glm::mat4 model); void recordPostprocess(uint32_t idx, const Subpass&, bool skybox, glm::mat4 view, glm::mat4 proj); void submitGraphics(uint32_t, VkSemaphore, VkSemaphore, VkFence); @@ -50,12 +50,15 @@ private: std::unique_ptr sampleTexture; std::unique_ptr modelBuffer; + VkDescriptorPool indicDescriptorPool; + std::vector indicDescriptorSets; + std::unique_ptr indicCubeBuffer; + VkDescriptorPool skyDescriptorPool; std::vector skyDescriptorSets; std::unique_ptr skyboxTexture; std::unique_ptr skyCubeBuffer; - glm::mat4 proj; bool freed = true; }; diff --git a/src/client/render/vk/Pipeline.cpp b/src/client/render/vk/Pipeline.cpp index 1642b26..b1aa513 100644 --- a/src/client/render/vk/Pipeline.cpp +++ b/src/client/render/vk/Pipeline.cpp @@ -5,6 +5,7 @@ #include "../Renderer.hpp" #include "buffer/VertexData.hpp" #include "api/Models.hpp" +#include #define CONTENT_DIR "content/" #define SHADER_DIR CONTENT_DIR "shaders/" @@ -67,9 +68,9 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render if (hasSamples) { colorDepthSubpass.pResolveAttachments = &colorAttachmentResolveRef; } - std::array subpasses = {colorDepthSubpass, colorDepthSubpass}; + std::array subpasses = {colorDepthSubpass, colorDepthSubpass, colorDepthSubpass}; - std::array dependencies{}; + std::array dependencies{}; dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL; dependencies[0].dstSubpass = 0; dependencies[0].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; @@ -93,7 +94,22 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render dependencies[2].dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; dependencies[2].dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; dependencies[2].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; - //FIXME: probably needs depth dependency and/or 1->external + + dependencies[3].srcSubpass = 1; + dependencies[3].dstSubpass = 2; + dependencies[3].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + dependencies[3].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + dependencies[3].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + dependencies[3].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + dependencies[3].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + + dependencies[4].srcSubpass = 1; + dependencies[4].dstSubpass = 2; + dependencies[4].srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; + dependencies[4].srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; + dependencies[4].dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; + dependencies[4].dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; + dependencies[4].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; VkRenderPassCreateInfo renderPassInfo{}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; @@ -112,6 +128,7 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render FATAL("Failed to create render pass!"); } } + //MAYBE: use sets (ubo, (samplers, passInfos)) { // Voxel descriptor VkDescriptorSetLayoutBinding samplerLayoutBinding{}; samplerLayoutBinding.binding = 1; @@ -120,7 +137,7 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render samplerLayoutBinding.pImmutableSamplers = nullptr; samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; - std::vector bindings = {buffer::vk::UniformBufferObject::getLayoutBinding(), samplerLayoutBinding}; + std::vector bindings = {UniformBufferObject::getLayoutBinding(), samplerLayoutBinding}; VkDescriptorSetLayoutCreateInfo layoutInfo{}; layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; layoutInfo.bindingCount = bindings.size(); @@ -130,6 +147,17 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render FATAL("Failed to create descriptor set layout!"); } } + { // Indicator descriptor + VkDescriptorSetLayoutCreateInfo layoutInfo{}; + layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + auto binding = UniformBufferObject::getLayoutBinding(); + layoutInfo.bindingCount = 1; + layoutInfo.pBindings = &binding; + + if (vkCreateDescriptorSetLayout(device, &layoutInfo, ALLOC, &indicDescriptorSet) != VK_SUCCESS) { + FATAL("Failed to create descriptor set layout!"); + } + } { // Sky descriptor VkDescriptorSetLayoutBinding samplerLayoutBinding{}; samplerLayoutBinding.binding = 1; @@ -138,7 +166,7 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render samplerLayoutBinding.pImmutableSamplers = nullptr; samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; - std::vector bindings = {buffer::vk::UniformBufferObject::getLayoutBinding(), samplerLayoutBinding}; + std::vector bindings = {UniformBufferObject::getLayoutBinding(), samplerLayoutBinding}; VkDescriptorSetLayoutCreateInfo layoutInfo{}; layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; layoutInfo.bindingCount = bindings.size(); @@ -149,7 +177,29 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render } } // Common pipeline settings - auto setShaders = [&](Subpass &pass, const std::string &shaderName) -> std::vector { + auto makeSpecialization = [](const std::map& constants, std::vector& speData, std::vector& speIndex) { + //MAYBE: create class + VkSpecializationInfo specialization; + speData.resize(0); + speIndex.resize(0); + speData.reserve(constants.size()); //NOTE: Lower bound + speIndex.reserve(constants.size()); + for(auto& constant: constants) { + VkSpecializationMapEntry entry; + entry.constantID = constant.first; + entry.size = constant.second.size; + entry.offset = speData.size(); + speData.resize(speData.size() + entry.size); + memcpy(speData.data() + entry.offset, static_cast(constant.second.ptr), entry.size); + } + specialization.dataSize = speData.size(); + specialization.pData = speData.data(); + specialization.mapEntryCount = speIndex.size(); + specialization.pMapEntries = speIndex.data(); + return specialization; + }; + auto setShaders = [&](Subpass &pass, const std::string &shaderName, bool geometry = false, const VkSpecializationInfo* specialization = nullptr) -> std::vector + { auto createShaderModule = [&](const data::file_content &code) { VkShaderModuleCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; @@ -162,13 +212,14 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render } return shaderModule; }; + VkPipelineShaderStageCreateInfo vertShaderStageInfo{}; vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; data::file_content vsFile({SHADER_DIR + shaderName + ".vs.spv"}); vertShaderStageInfo.module = pass.vsShader = createShaderModule(vsFile); vertShaderStageInfo.pName = "main"; - vertShaderStageInfo.pSpecializationInfo = nullptr; //TODO: pass constants + vertShaderStageInfo.pSpecializationInfo = specialization; VkPipelineShaderStageCreateInfo fragShaderStageInfo{}; fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; @@ -176,11 +227,21 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render data::file_content fsFile({SHADER_DIR + shaderName + ".fs.spv"}); fragShaderStageInfo.module = pass.fsShader = createShaderModule(fsFile); fragShaderStageInfo.pName = "main"; - fragShaderStageInfo.pSpecializationInfo = nullptr; //TODO: pass constants + fragShaderStageInfo.pSpecializationInfo = specialization; - //TODO: geometry + if (geometry) { + VkPipelineShaderStageCreateInfo geomShaderStageInfo{}; + geomShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; + geomShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT; + data::file_content fsFile({SHADER_DIR + shaderName + ".gs.spv"}); + geomShaderStageInfo.module = pass.fsShader = createShaderModule(fsFile); + geomShaderStageInfo.pName = "main"; + geomShaderStageInfo.pSpecializationInfo = specialization; - return {vertShaderStageInfo, fragShaderStageInfo}; + return {vertShaderStageInfo, geomShaderStageInfo, fragShaderStageInfo}; + } else { + return {vertShaderStageInfo, fragShaderStageInfo}; + } }; auto setLayout = [&](Subpass& pass, const std::vector& layout, const std::vector& push = {}) { VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; @@ -288,7 +349,7 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render };*/ VkPushConstantRange pushRange{}; pushRange.offset = 0; - pushRange.size = sizeof(buffer::vk::ModelPush); + pushRange.size = sizeof(ModelPush); pushRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; setLayout(worldPass, {voxelDescriptorSet}, {pushRange}); auto shaderStages = setShaders(worldPass, "Tris"); @@ -327,12 +388,54 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render FATAL("Failed to create graphics pipeline!"); } } - { // Sky pipeline + { // Indicator pipeline VkPushConstantRange pushRange{}; pushRange.offset = 0; - pushRange.size = sizeof(buffer::vk::SkyPush); + pushRange.size = sizeof(ModelPush); pushRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; - setLayout(skyPass, {skyDescriptorSet}, {pushRange}); + setLayout(indicPass, {indicDescriptorSet}, {pushRange}); + auto shaderStages = setShaders(indicPass, "Color"); + + VkPipelineInputAssemblyStateCreateInfo lineInputAssembly{}; + lineInputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; + lineInputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST; + lineInputAssembly.primitiveRestartEnable = VK_FALSE; + + VkPipelineVertexInputStateCreateInfo vertexInputInfo{}; + vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; + auto bindingDescription = Indicator::getBindingDescription(); + vertexInputInfo.vertexBindingDescriptionCount = 1; + vertexInputInfo.pVertexBindingDescriptions = &bindingDescription; + auto attributeDescriptions = Indicator::getAttributeDescription(); + vertexInputInfo.vertexAttributeDescriptionCount = attributeDescriptions.size(); + vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data(); + + VkGraphicsPipelineCreateInfo pipelineInfo{}; + pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; + pipelineInfo.stageCount = shaderStages.size(); + pipelineInfo.pStages = shaderStages.data(); + pipelineInfo.pVertexInputState = &vertexInputInfo; + pipelineInfo.pInputAssemblyState = &lineInputAssembly; + pipelineInfo.pViewportState = &viewportState; + pipelineInfo.pRasterizationState = &rasterizer; + pipelineInfo.pMultisampleState = &multisampling; + pipelineInfo.pDepthStencilState = &depthStencil; + pipelineInfo.pColorBlendState = &colorBlending; + pipelineInfo.pDynamicState = nullptr; + + pipelineInfo.layout = indicPass.layout; + pipelineInfo.renderPass = renderPass; + pipelineInfo.subpass = 1; + + pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; + pipelineInfo.basePipelineIndex = -1; + + if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, ALLOC, &indicPass.pipeline) != VK_SUCCESS) { + FATAL("Failed to create graphics pipeline!"); + } + } + { // Sky pipeline + setLayout(skyPass, {skyDescriptorSet}, {}); auto shaderStages = setShaders(skyPass, "Sky"); depthStencil.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; @@ -361,7 +464,7 @@ Pipeline::Pipeline(VkDevice device, const PhysicalDeviceInfo &info, const render pipelineInfo.layout = skyPass.layout; pipelineInfo.renderPass = renderPass; - pipelineInfo.subpass = 1; + pipelineInfo.subpass = 2; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; pipelineInfo.basePipelineIndex = -1; @@ -383,6 +486,7 @@ Pipeline::~Pipeline() { destroy(skyPass); vkDestroyDescriptorSetLayout(device, voxelDescriptorSet, ALLOC); + vkDestroyDescriptorSetLayout(device, indicDescriptorSet, ALLOC); vkDestroyDescriptorSetLayout(device, skyDescriptorSet, ALLOC); vkDestroyRenderPass(device, renderPass, ALLOC); } \ No newline at end of file diff --git a/src/client/render/vk/Pipeline.hpp b/src/client/render/vk/Pipeline.hpp index 2a81941..8bc05b0 100644 --- a/src/client/render/vk/Pipeline.hpp +++ b/src/client/render/vk/Pipeline.hpp @@ -22,6 +22,8 @@ public: // Voxels (world & entity) passes descriptor set constexpr VkDescriptorSetLayout getVoxelDescriptorSet() const { return voxelDescriptorSet; } constexpr const Subpass& getWorldPass() const { return worldPass; } + constexpr VkDescriptorSetLayout getIndicDescriptorSet() const { return indicDescriptorSet; } + constexpr const Subpass &getIndicPass() const { return indicPass; } constexpr VkDescriptorSetLayout getSkyDescriptorSet() const { return skyDescriptorSet; } constexpr const Subpass& getSkyPass() const { return skyPass; } @@ -31,9 +33,11 @@ private: VkRenderPass renderPass; VkDescriptorSetLayout voxelDescriptorSet; + VkDescriptorSetLayout indicDescriptorSet; VkDescriptorSetLayout skyDescriptorSet; Subpass worldPass; + Subpass indicPass; Subpass skyPass; }; } \ No newline at end of file diff --git a/src/client/render/vk/Renderer.cpp b/src/client/render/vk/Renderer.cpp index 894e02d..bba0a42 100644 --- a/src/client/render/vk/Renderer.cpp +++ b/src/client/render/vk/Renderer.cpp @@ -442,7 +442,7 @@ std::function &)> Rend size_t Renderer::drawIndicatorCube(glm::mat4 model) { assert(currentImage < swapChain->getImageViews().size()); - commandCenter->recordIndicator(currentImage, model); + commandCenter->recordIndicator(currentImage, pipeline->getIndicPass(), model); return 0; } diff --git a/src/client/render/vk/api/Models.cpp b/src/client/render/vk/api/Models.cpp index e2bd8bf..c49a6a5 100644 --- a/src/client/render/vk/api/Models.cpp +++ b/src/client/render/vk/api/Models.cpp @@ -9,6 +9,27 @@ std::unique_ptr Shape::Create(const std::vector& vertices) { return std::unique_ptr(new Shape(tmp.ref, std::move(mem), vertices.size())); } +std::unique_ptr Indicator::Create(const std::vector& vert, const std::vector& cols) { + assert(vert.size() == cols.size()); + std::vector vertices; + vertices.reserve(vert.size()); + for (size_t i = 0; i < vert.size(); i++) { + vertices.push_back(Vertex{vert.at(i), cols.at(i)}); + } + + vk::Buffer::info tmp; + data_view view(vertices); + auto mem = createBuffer(view.size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, view, tmp); + return std::unique_ptr(new Indicator(vertices.size(), tmp.ref, std::move(mem))); +} + +std::unique_ptr Model::Create(const Data& data) { + std::vector tmp; + data_view vertices(data.vertices); + data_view indices(data.indices); + auto mem = createBuffers({{vertices.size, Usage::VERTEX, vertices}, {indices.size, Usage::INDEX, indices}}, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, tmp); + return std::unique_ptr(new Model(data.indices.size(), tmp.at(0).ref, tmp.at(1).ref, std::move(mem))); +} std::unique_ptr LodModel::Create(const LodData& data) { std::vector tmp; data_view vertices(data.first.vertices); diff --git a/src/client/render/vk/api/Models.hpp b/src/client/render/vk/api/Models.hpp index 79ae695..c798f32 100644 --- a/src/client/render/vk/api/Models.hpp +++ b/src/client/render/vk/api/Models.hpp @@ -6,7 +6,7 @@ namespace render::vk { /// Positions only buffer -class Shape final: public Buffer { +class Shape final: public render::Shape, public Buffer { public: const size_t size; @@ -33,36 +33,52 @@ protected: Buffer(ref, std::move(mem)), size(size) { } }; -/*class Indicator final: public render::Indicator { +class Indicator final: public render::Indicator, public Buffer { public: - Indicator(const std::vector&, const std::vector&); - ~Indicator(); + const size_t size; - size_t draw(); - size_t drawInstanced(size_t count); + static std::unique_ptr Create(const std::vector&, const std::vector&); -private: - size_t size; - GLuint vertexBufferId; - GLuint colorBufferId; + struct Vertex { + alignas(16) glm::vec3 pos; + alignas(16) glm::vec4 color; + }; + + static VkVertexInputBindingDescription getBindingDescription() { + VkVertexInputBindingDescription description{}; + description.binding = 0; + description.stride = sizeof(Vertex); + description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; + return description; + } + static std::array getAttributeDescription() { + std::array attributeDescriptions{}; + attributeDescriptions[0].binding = 0; + attributeDescriptions[0].location = 0; + attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT; + attributeDescriptions[0].offset = offsetof(Vertex, pos); + attributeDescriptions[1].binding = 0; + attributeDescriptions[1].location = 1; + attributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT; + attributeDescriptions[1].offset = offsetof(Vertex, color); + return attributeDescriptions; + } + +protected: + Indicator(size_t size, VkBuffer ref, memory::ptr mem): Buffer(ref, std::move(mem)), size(size) {} - void enableAttribs(); - void disableAttribs(); }; -class Model final: public render::Model { +class Model final: public render::Model, public ShortIndexedVertexBuffer { public: - Model(const Data&); - ~Model(); + static std::unique_ptr Create(const Data &); - size_t draw(); - size_t drawInstanced(size_t count); +protected: + Model(size_t size, VkBuffer vertex, VkBuffer index, memory::ptr mem): + ShortIndexedVertexBuffer(vertex, index, std::move(mem)), indexSize(size) { } -private: size_t indexSize; - GLuint vertexBufferId; - GLuint indexBufferId; -};*/ +}; class LodModel final: public render::LodModel, public ShortIndexedVertexBuffer { public: static std::unique_ptr Create(const LodData&); @@ -79,4 +95,23 @@ protected: } }; +struct UniformBufferObject { + alignas(16) glm::mat4 view; + alignas(16) glm::mat4 proj; + + static VkDescriptorSetLayoutBinding getLayoutBinding() { + VkDescriptorSetLayoutBinding uboLayoutBinding{}; + uboLayoutBinding.binding = 0; + uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + uboLayoutBinding.descriptorCount = 1; + uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + uboLayoutBinding.pImmutableSamplers = nullptr; + return uboLayoutBinding; + } +}; + +struct ModelPush { + alignas(16) glm::mat4 model; +}; + } \ No newline at end of file diff --git a/src/client/render/vk/buffer/VertexData.hpp b/src/client/render/vk/buffer/VertexData.hpp index 022b218..31b7fed 100644 --- a/src/client/render/vk/buffer/VertexData.hpp +++ b/src/client/render/vk/buffer/VertexData.hpp @@ -53,28 +53,4 @@ const std::vector indices = { 4, 5, 6, 6, 7, 4 }; -struct UniformBufferObject { - alignas(16) glm::mat4 view; - alignas(16) glm::mat4 proj; - - static VkDescriptorSetLayoutBinding getLayoutBinding() { - VkDescriptorSetLayoutBinding uboLayoutBinding{}; - uboLayoutBinding.binding = 0; - uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - uboLayoutBinding.descriptorCount = 1; - uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; - uboLayoutBinding.pImmutableSamplers = nullptr; - return uboLayoutBinding; - } -}; - -struct ModelPush { - alignas(16) glm::mat4 model; -}; - -struct SkyPush { - alignas(16) glm::mat4 view; - alignas(16) glm::mat4 proj; -}; - } \ No newline at end of file