Gizmo api now working with a simple quad in the world

This commit is contained in:
antopilo
2025-03-22 23:17:25 -04:00
parent 0243ef83cb
commit 85f0eb2c02
13 changed files with 350 additions and 82 deletions

View File

@@ -0,0 +1,114 @@
// Transforms
struct ModelData
{
float4x4 model;
};
[[vk::binding(0, 0)]]
StructuredBuffer<ModelData> model : register(t1);
// Vertex
struct Vertex
{
float3 position;
float uv_x;
float3 normal;
float uv_y;
float3 tangent;
float3 bitangent;
};
[[vk::binding(0, 1)]]
StructuredBuffer<Vertex> vertexBuffer : register(t2);
// Samplers
[[vk::binding(0, 2)]]
SamplerState mySampler : register(s0);
// Materials
struct Material
{
bool hasAlbedo;
float3 albedo;
bool hasNormal;
bool hasMetalness;
bool hasRoughness;
bool hasAO;
float metalnessValue;
float roughnessValue;
float aoValue;
int albedoTextureId;
int normalTextureId;
int metalnessTextureId;
int roughnessTextureId;
int aoTextureId;
};
[[vk::binding(0, 3)]]
StructuredBuffer<Material> material;
// Textures
[[vk::binding(0, 4)]]
Texture2D textures[];
// Lights
struct Light
{
float3 position;
int type;
float4 color;
float3 direction;
float outerConeAngle;
float innerConeAngle;
bool castShadow;
int shadowMapTextureId[4];
int transformId[4];
};
[[vk::binding(0, 5)]]
StructuredBuffer<Light> lights;
// Cameras
struct CameraView {
float4x4 View;
float4x4 Projection;
float4x4 ViewProjection;
float4x4 InverseView;
float4x4 InverseProjection;
float3 Position;
float Near;
float Far;
};
[[vk::binding(0, 6)]]
StructuredBuffer<CameraView> cameras;
struct PSInput {
float4 Position : SV_Position;
float2 UV : TEXCOORD0;
};
struct PSOutput {
float4 oColor0 : SV_TARGET;
};
struct CopyPushConstant
{
int SourceTextureID;
int Source2TextureID;
};
[[vk::push_constant]]
CopyPushConstant pushConstants;
PSOutput main(PSInput input)
{
PSOutput output;
int sourceTextureID = pushConstants.SourceTextureID;
int source2TextureID = pushConstants.Source2TextureID;
float2 uv = input.UV;
float4 sampleValue = textures[sourceTextureID].Sample(mySampler, input.UV);
float4 sampleValue2 = textures[source2TextureID].Sample(mySampler, input.UV);
output.oColor0 = sampleValue + sampleValue2;
return output;
}

View File

@@ -0,0 +1,108 @@
// Transforms
struct ModelData
{
float4x4 model;
};
[[vk::binding(0, 0)]]
StructuredBuffer<ModelData> model : register(t1);
// Vertex
struct Vertex
{
float3 position;
float uv_x;
float3 normal;
float uv_y;
float3 tangent;
float3 bitangent;
};
[[vk::binding(0, 1)]]
StructuredBuffer<Vertex> vertexBuffer : register(t2);
// Samplers
[[vk::binding(0, 2)]]
SamplerState mySampler : register(s0);
// Materials
struct Material
{
bool hasAlbedo;
float3 albedo;
bool hasNormal;
bool hasMetalness;
bool hasRoughness;
bool hasAO;
float metalnessValue;
float roughnessValue;
float aoValue;
int albedoTextureId;
int normalTextureId;
int metalnessTextureId;
int roughnessTextureId;
int aoTextureId;
};
[[vk::binding(0, 3)]]
StructuredBuffer<Material> material;
// Textures
[[vk::binding(0, 4)]]
Texture2D textures[];
// Lights
struct Light
{
float3 position;
int type;
float4 color;
float3 direction;
float outerConeAngle;
float innerConeAngle;
bool castShadow;
int shadowMapTextureId[4];
int transformId[4];
};
[[vk::binding(0, 5)]]
StructuredBuffer<Light> lights;
// Cameras
struct CameraView {
float4x4 View;
float4x4 Projection;
float4x4 ViewProjection;
float4x4 InverseView;
float4x4 InverseProjection;
float3 Position;
float Near;
float Far;
};
[[vk::binding(0, 6)]]
StructuredBuffer<CameraView> cameras;
struct CopyPushConstant
{
int SourceTextureID;
int Source2TextureID;
};
[[vk::push_constant]]
CopyPushConstant pushConstants;
// Outputs
struct VSOutput {
float4 Position : SV_Position;
float2 UV : TEXCOORD0;
};
// Main vertex shader
VSOutput main(uint vertexIndex : SV_VertexID)
{
VSOutput output;
Vertex v = vertexBuffer[vertexIndex];
output.UV = float2(v.uv_x, v.uv_y);
output.Position = float4(v.position, 1.0f);
return output;
}

View File

@@ -89,73 +89,18 @@ struct PSOutput {
float4 oColor0 : SV_TARGET;
};
struct OutlinePushConstant
struct DebugConstant
{
float4 Color;
float Thickness;
int SourceTextureID;
int EntityIDTextureID;
int DepthTextureID;
float SelectedEntity;
float4x4 Transform;
};
[[vk::push_constant]]
OutlinePushConstant pushConstants;
DebugConstant pushConstants;
PSOutput main(PSInput input)
{
PSOutput output;
float4 outlineColor = pushConstants.Color;
float target = pushConstants.SelectedEntity;
float radius = pushConstants.Thickness;
float2 uv = input.UV;
int entityIDTextureID = pushConstants.EntityIDTextureID;
float hasHit = 0.0f;
float sampleValue = textures[entityIDTextureID].Sample(mySampler, uv).r;
if(abs(sampleValue - target) < 0.0001)
{
output.oColor0 = outlineColor;
output.oColor0.a = 1.0;
return output;
}
float4 fragColor = float4(0, 0, 0, 0);
const float TAU = 6.28318530;
const float steps = 32.0;
for(float i = 0.0f; i < TAU; i += TAU / steps)
{
float2 uvOffset = float2(sin(i), cos(i)) * radius;
float2 sampleUV = uv + uvOffset;
sampleUV.x = clamp(sampleUV.x, 0.0, 0.999);
sampleUV.y = clamp(sampleUV.y, 0.0, 0.999);
int sampleValue = (int)textures[entityIDTextureID].Sample(mySampler, sampleUV).r;
if(sampleValue == target)
{
hasHit = 1.0f;
}
float alpha = smoothstep(0.5, 0.9, int(sampleValue != target) * hasHit * 10.0f);
float4 outputColor = float4(
lerp(fragColor.r, outlineColor.r, alpha),
lerp(fragColor.g, outlineColor.g, alpha),
lerp(fragColor.b, outlineColor.b, alpha),
lerp(fragColor.a, outlineColor.a, alpha)
);
fragColor = outputColor;
}
if(fragColor.a > 0.1)
{
fragColor.a = 1.0f;
}
output.oColor0 = fragColor;
output.oColor0 = float4(input.UV.x, input.UV.y, 0, 1);
return output;
}

View File

@@ -80,18 +80,13 @@ struct CameraView {
[[vk::binding(0, 6)]]
StructuredBuffer<CameraView> cameras;
struct OutlinePushConstant
struct DebugConstant
{
float4 Color;
float Thickness;
int SourceTextureID;
int EntityIDTextureID;
int DepthTextureID;
float SelectedEntity;
float4x4 Transform;
};
[[vk::push_constant]]
OutlinePushConstant pushConstants;
DebugConstant pushConstants;
// Outputs
struct VSOutput {
@@ -106,7 +101,7 @@ VSOutput main(uint vertexIndex : SV_VertexID)
Vertex v = vertexBuffer[vertexIndex];
output.UV = float2(v.uv_x, v.uv_y);
output.Position = float4(v.position, 1.0f);
output.Position = mul(pushConstants.Transform, float4(v.position, 1.0f));
return output;
}

View File

@@ -7,9 +7,11 @@
#include "Nuake/Rendering/Vulkan/VulkanRenderer.h"
#include "Nuake/Rendering/Vulkan/SceneViewport.h"
#include "Nuake/Rendering/Vulkan/DebugCmd.h"
#include <glm/gtc/type_ptr.hpp>
using namespace Nuake;
ViewportWidget::ViewportWidget(EditorContext& context) : IEditorWidget(context)
@@ -204,4 +206,17 @@ void ViewportWidget::OnSceneChanged(Ref<Nuake::Scene> scene)
vkRenderer.RegisterSceneViewport(scene, viewport->GetID());
sceneViewport = viewport;
}
sceneViewport->GetOnDebugDraw().AddRaw(this, &ViewportWidget::OnDebugDraw);
}
void ViewportWidget::OnDebugDraw(DebugCmd& debugCmd)
{
auto cam = debugCmd.GetScene()->GetCurrentCamera();
auto view = cam->GetTransform();
auto proj = cam->GetPerspective();
Matrix4 model = glm::translate(Matrix4(1.0f), Vector3(0, 3, 0));
debugCmd.DrawQuad(proj * view * model);
}

View File

@@ -9,6 +9,7 @@ class EditorContext;
namespace Nuake
{
class Viewport;
class DebugCmd;
}
class ViewportWidget : public IEditorWidget
@@ -30,4 +31,6 @@ public:
void Update(float ts) override;
void Draw() override;
void OnSceneChanged(Ref<Nuake::Scene> scene) override;
void OnDebugDraw(Nuake::DebugCmd& debugCmd);
};

View File

@@ -0,0 +1,10 @@
#pragma once
#include "Nuake/Core/Maths.h"
namespace Nuake
{
struct DebugConstant
{
Matrix4 Transform;
};
}

View File

@@ -1,11 +1,28 @@
#include "DebugCmd.h"
#include "VulkanRenderer.h"
using namespace Nuake;
DebugCmd::DebugCmd(Cmd& inCmd) :
cmd(inCmd)
Ref<Scene> DebugCmd::GetScene() const
{
return ctx.scene;
}
DebugCmd::DebugCmd(Cmd& inCmd, PassRenderContext& inCtx) :
cmd(inCmd), ctx(inCtx), debugConstant({})
{
}
void DebugCmd::DrawQuad(const Matrix4& transform)
{
debugConstant.Transform = transform;
cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(DebugConstant), &debugConstant);
auto& quadMesh = VkSceneRenderer::QuadMesh;
cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, quadMesh->GetDescriptorSet(), 1);
cmd.BindIndexBuffer(quadMesh->GetIndexBuffer()->GetBuffer());
cmd.DrawIndexed(6);
}
void DebugCmd::DrawLine(const Vector3& start, const Vector3& end, const Color& color) const

View File

@@ -1,20 +1,30 @@
#pragma once
#include "Cmd.h"
#include "Nuake/Rendering/Vulkan/Constant/DebugConstant.h"
namespace Nuake
{
class Scene;
class PassRenderContext;
// This is the API to render anything debugging related
// Like lines, shapes, gizmos, etc.
class DebugCmd
{
private:
Cmd& cmd;
PassRenderContext& ctx;
DebugConstant debugConstant;
public:
DebugCmd(Cmd& inCmd);
DebugCmd(Cmd& inCmd, PassRenderContext& inCtx);
~DebugCmd() = default;
public:
Ref<Scene> GetScene() const;
void DrawQuad(const Matrix4& transform);
void DrawLine(const Vector3& start, const Vector3& end, const Color& color) const;
void DrawSphere(const Vector2& position, float radius, const Color& color) const;
void DrawCube(const Vector3& position, const Vector3& size, const Color& color) const;

View File

@@ -225,6 +225,8 @@ void RenderPass::Build()
PipelineBuilder pipelineBuilder;
pipelineBuilder.PipelineLayout = PipelineLayout;
assert(VertShader != nullptr && FragShader != nullptr && "No shader set for renderpass!");
pipelineBuilder.SetShaders(VertShader->GetModule(), FragShader->GetModule());
pipelineBuilder.SetInputTopology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
pipelineBuilder.SetPolygonMode(VK_POLYGON_MODE_FILL);

View File

@@ -112,6 +112,9 @@ SceneRenderPipeline::SceneRenderPipeline()
GizmoOutput = CreateRef<VulkanImage>(ImageFormat::RGBA8, defaultSize);
GizmoOutput->SetDebugName("GizmoOutput");
GizmoCombineOutput = CreateRef<VulkanImage>(ImageFormat::RGBA8, defaultSize);
GizmoCombineOutput->SetDebugName("GizmoCombineOutput");
// Setup bloom targets
BloomOutput = CreateRef<VulkanImage>(ImageFormat::RGBA16F, defaultSize);
BloomThreshold = CreateRef<VulkanImage>(ImageFormat::RGBA16F, defaultSize);
@@ -241,7 +244,7 @@ SceneRenderPipeline::SceneRenderPipeline()
auto& tonemapPass = GBufferPipeline.AddPass("Tonemap");
tonemapPass.SetShaders(shaderMgr.GetShader("tonemap_vert"), shaderMgr.GetShader("tonemap_frag"));
tonemapPass.SetPushConstant<TonemapConstant>(tonemapConstant);
tonemapPass.SetPushConstant(tonemapConstant);
tonemapPass.AddAttachment("TonemapOutput", TonemappedOutput->GetFormat());
tonemapPass.SetDepthTest(false);
tonemapPass.AddInput("ShadingOutput");
@@ -279,6 +282,7 @@ SceneRenderPipeline::SceneRenderPipeline()
auto& gizmoPass = GBufferPipeline.AddPass("Gizmo");
gizmoPass.SetShaders(shaderMgr.GetShader("gizmo_vert"), shaderMgr.GetShader("gizmo_frag"));
gizmoPass.SetPushConstant(debugConstant);
gizmoPass.AddAttachment("GizmoOutput", GizmoOutput->GetFormat());
gizmoPass.AddInput("Depth");
gizmoPass.SetPreRender([&](PassRenderContext& ctx) {
@@ -297,10 +301,44 @@ SceneRenderPipeline::SceneRenderPipeline()
gizmoPass.SetRender([&](PassRenderContext& ctx)
{
auto& cmd = ctx.commandBuffer;
DebugCmd debugCmd = DebugCmd(cmd);
DebugCmd debugCmd = DebugCmd(cmd, ctx);
OnDebugDraw().Broadcast(debugCmd);
});
auto& gizmoCombinePass = GBufferPipeline.AddPass("GizmoCombine");
gizmoCombinePass.SetPushConstant(copyConstant);
gizmoCombinePass.SetShaders(shaderMgr.GetShader("copy_vert"), shaderMgr.GetShader("copy_frag"));
gizmoCombinePass.AddAttachment("GizmoCombineOutput", GizmoCombineOutput->GetFormat());
gizmoCombinePass.AddInput("GizmoCombineOutput");
gizmoCombinePass.SetPreRender([&](PassRenderContext& ctx)
{
Cmd& cmd = ctx.commandBuffer;
auto& layout = ctx.renderPass->PipelineLayout;
auto& res = GPUResources::Get();
// Bindless
cmd.BindDescriptorSet(layout, res.ModelDescriptor, 0);
cmd.BindDescriptorSet(layout, res.SamplerDescriptor, 2);
cmd.BindDescriptorSet(layout, res.MaterialDescriptor, 3);
cmd.BindDescriptorSet(layout, res.TexturesDescriptor, 4);
cmd.BindDescriptorSet(layout, res.LightsDescriptor, 5);
cmd.BindDescriptorSet(layout, res.CamerasDescriptor, 6);
});
gizmoCombinePass.SetRender([&](PassRenderContext& ctx)
{
auto& cmd = ctx.commandBuffer;
copyConstant.SourceTextureID = GPUResources::Get().GetBindlessTextureID(GizmoOutput->GetID());
copyConstant.Source2TextureID = GPUResources::Get().GetBindlessTextureID(TonemappedOutput->GetID());
cmd.PushConstants(ctx.renderPass->PipelineLayout, sizeof(copyConstant), &copyConstant);
// Draw full screen quad
auto& quadMesh = VkSceneRenderer::QuadMesh;
cmd.BindDescriptorSet(ctx.renderPass->PipelineLayout, quadMesh->GetDescriptorSet(), 1);
cmd.BindIndexBuffer(quadMesh->GetIndexBuffer()->GetBuffer());
cmd.DrawIndexed(6);
});
auto& outlinePass = GBufferPipeline.AddPass("Outline");
outlinePass.SetShaders(shaderMgr.GetShader("outline_vert"), shaderMgr.GetShader("outline_frag"));
outlinePass.SetPushConstant<OutlineConstant>(outlineConstant);
@@ -360,6 +398,9 @@ void SceneRenderPipeline::Render(PassRenderContext& ctx)
ShadingOutput = ResizeImage(ctx, ShadingOutput, ctx.resolution);
TonemappedOutput = ResizeImage(ctx, TonemappedOutput, ctx.resolution);
GizmoOutput = ResizeImage(ctx, GizmoOutput, ctx.resolution);
GizmoCombineOutput = ResizeImage(ctx, GizmoCombineOutput, ctx.resolution);
OutlineOutput = ResizeImage(ctx, OutlineOutput, ctx.resolution);
Color clearColor = ctx.scene->GetEnvironment()->AmbientColor;
@@ -370,15 +411,12 @@ void SceneRenderPipeline::Render(PassRenderContext& ctx)
{ GBufferAlbedo, GBufferDepth, GBufferNormal, GBufferMaterial, GBufferEntityID }, // GBuffer
{ ShadingOutput }, // Shading
{ TonemappedOutput }, // Tonemap
{ OutlineOutput },
{ GizmoOutput },
{ GizmoCombineOutput },
{ OutlineOutput }
};
GBufferPipeline.Execute(ctx, pipelineInputs);
// Debug drawing
DebugCmd debugCmd = DebugCmd(ctx.commandBuffer);
OnDebugDraw().Broadcast(debugCmd);
}
Ref<VulkanImage> SceneRenderPipeline::ResizeImage(PassRenderContext& ctx, Ref<VulkanImage> image, const Vector2& size)

View File

@@ -2,6 +2,7 @@
#include "Nuake/Core/Core.h"
#include "Nuake/Resource/UUID.h"
#include "Nuake/Rendering/Vulkan/Constant/DebugConstant.h"
#include "Nuake/Rendering/Vulkan/Pipeline/RenderPipeline.h"
#include "Nuake/Rendering/Vulkan/VkResources.h"
@@ -50,6 +51,12 @@ namespace Nuake
int SourceTextureID;
};
struct CopyConstant
{
int SourceTextureID;
int Source2TextureID;
};
struct OutlineConstant
{
Vector4 Color;
@@ -91,6 +98,7 @@ namespace Nuake
Ref<VulkanImage> ShadingOutput;
Ref<VulkanImage> GizmoOutput;
Ref<VulkanImage> GizmoCombineOutput;
Ref<VulkanImage> TonemappedOutput;
@@ -110,6 +118,8 @@ namespace Nuake
GBufferConstant gbufferConstant;
ShadingConstant shadingConstant;
TonemapConstant tonemapConstant;
DebugConstant debugConstant;
CopyConstant copyConstant;
OutlineConstant outlineConstant;
BloomConstant bloomConstant;
@@ -124,7 +134,7 @@ namespace Nuake
void SetCamera(UUID camera);
void Render(PassRenderContext& ctx);
Ref<VulkanImage> GetOutput() { return TonemappedOutput; }
Ref<VulkanImage> GetOutput() { return GizmoCombineOutput; }
MulticastDelegate<DebugCmd&>& OnDebugDraw() { return DebugDrawDelegate; }

View File

@@ -25,7 +25,6 @@ using namespace Nuake;
Ref<VkMesh> VkSceneRenderer::QuadMesh;
void VkSceneRenderer::Init()
{
LoadShaders();
@@ -67,6 +66,8 @@ void VkSceneRenderer::LoadShaders()
shaderMgr.AddShader("outline_vert", shaderCompiler.CompileShader("Resources/Shaders/Vulkan/outline.vert"));
shaderMgr.AddShader("gizmo_frag", shaderCompiler.CompileShader("Resources/Shaders/Vulkan/gizmo.frag"));
shaderMgr.AddShader("gizmo_vert", shaderCompiler.CompileShader("Resources/Shaders/Vulkan/gizmo.vert"));
shaderMgr.AddShader("copy_frag", shaderCompiler.CompileShader("Resources/Shaders/Vulkan/copy.frag"));
shaderMgr.AddShader("copy_vert", shaderCompiler.CompileShader("Resources/Shaders/Vulkan/copy.vert"));
}
void VkSceneRenderer::PrepareScenes(const std::vector<Ref<Scene>>& scenes, RenderContext inContext)