Factorized Push Data

This commit is contained in:
2026-02-22 15:54:09 -05:00
parent 1e1ec84fa1
commit a781facd48
13 changed files with 85 additions and 36 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -11,6 +11,11 @@ cbuffer RootConstants : register(b0, space0)
uint _Padding; // Padding for alignment uint _Padding; // Padding for alignment
float2 Scale; // 2D scale factor float2 Scale; // 2D scale factor
float2 Translate; // 2D translation float2 Translate; // 2D translation
float3 LightDirection; // Normalized, world-space
float LightPad;
float3 LightColor;
float AmbientIntensity;
}; };

View File

@@ -1,4 +1,10 @@
float4 main(float4 Color : TEXCOORD0) : SV_Target0 #include "RootConstants.hlsl"
float4 main(float4 Color : TEXCOORD0, float3 WorldNormal : TEXCOORD1) : SV_Target0
{ {
return Color; float3 N = normalize(WorldNormal);
float NdotL = saturate(dot(N, -LightDirection));
float3 diffuse = Color.rgb * LightColor * NdotL;
float3 ambient = Color.rgb * AmbientIntensity;
return float4(diffuse + ambient, Color.a);
} }

View File

@@ -1,7 +1,7 @@
struct Output struct Output
{ {
float4 Color : TEXCOORD0; float4 Color : TEXCOORD0;
float3 Normal : TEXCOORD1; float3 WorldNormal : TEXCOORD1;
float4 Position : SV_Position; float4 Position : SV_Position;
}; };
@@ -20,9 +20,10 @@ Output main(uint vertexIndex : SV_VertexID)
float3 normal = asfloat(buffer.Load3(offset + 12)); float3 normal = asfloat(buffer.Load3(offset + 12));
float4 col = asfloat(buffer.Load4(offset + 24)); float4 col = asfloat(buffer.Load4(offset + 24));
//output.Position = float4(pos, 1.0f);
output.Position = mul(ViewProjection, mul(Model, float4(pos, 1.0f))); output.Position = mul(ViewProjection, mul(Model, float4(pos, 1.0f)));
output.Color = col; output.Color = col;
output.Normal = normal;
float3 worldNormal = mul((float3x3)Model, normal);
output.WorldNormal = worldNormal;
return output; return output;
} }

View File

@@ -3,6 +3,7 @@
#include <Core/Container/Vector.h> #include <Core/Container/Vector.h>
#include <Core/Math/Matrix.h> #include <Core/Math/Matrix.h>
#include <Graphics/VertexData.h> #include <Graphics/VertexData.h>
#include <Graphics/PushConstants.h>
#include <Juliet.h> #include <Juliet.h>
namespace Juliet namespace Juliet
@@ -40,13 +41,6 @@ namespace Juliet
GraphicsPipeline* Pipeline; GraphicsPipeline* Pipeline;
}; };
struct PushData
{
Matrix ViewProjection;
Matrix Model;
uint32 BufferIndex;
};
[[nodiscard]] JULIET_API bool InitializeMeshRenderer(NonNullPtr<Arena> arena, NonNullPtr<GraphicsDevice> device, [[nodiscard]] JULIET_API bool InitializeMeshRenderer(NonNullPtr<Arena> arena, NonNullPtr<GraphicsDevice> device,
NonNullPtr<Window> window); NonNullPtr<Window> window);
JULIET_API void ShutdownMeshRenderer(); JULIET_API void ShutdownMeshRenderer();

View File

@@ -0,0 +1,25 @@
#pragma once
#include <Core/Math/Matrix.h>
#include <Core/Math/Vector.h>
#include <Juliet.h>
namespace Juliet
{
struct PushData
{
Matrix ViewProjection;
Matrix Model;
uint32 BufferIndex;
uint32 TextureIndex;
uint32 VertexOffset;
uint32 Padding;
float Scale[2];
float Translate[2];
Vector3 LightDirection;
float LightPad;
Vector3 LightColor;
float AmbientIntensity;
};
} // namespace Juliet

View File

@@ -5,6 +5,7 @@
#include <Core/HAL/Filesystem/Filesystem.h> #include <Core/HAL/Filesystem/Filesystem.h>
#include <Core/Memory/Allocator.h> #include <Core/Memory/Allocator.h>
#include <Graphics/GraphicsPipeline.h> #include <Graphics/GraphicsPipeline.h>
#include <Graphics/PushConstants.h>
namespace Juliet namespace Juliet
{ {
@@ -302,18 +303,22 @@ namespace Juliet
BindGraphicsPipeline(renderPass, g_DebugState.DepthTestedPipeline); BindGraphicsPipeline(renderPass, g_DebugState.DepthTestedPipeline);
// Pack VP matrix + Model (identity for debug) + buffer index into push constants // Pack VP matrix + Model (identity for debug) + buffer index into push constants
struct PushData pushData = {};
{ pushData.ViewProjection = Camera_GetViewProjectionMatrix(camera);
Matrix vp; pushData.Model = MatrixIdentity();
Matrix model; pushData.BufferIndex = bufferIndex;
uint32 bufferIndex; pushData.TextureIndex = 0;
uint32 vertexOffset; // Offset in vertices (not bytes) pushData.VertexOffset = 0; // Depth-tested vertices start at 0
uint32 padding[2]; pushData.Padding = 0;
} pushData; pushData.Scale[0] = 1.0f; pushData.Scale[1] = 1.0f;
pushData.vp = Camera_GetViewProjectionMatrix(camera); pushData.Translate[0] = 0.0f; pushData.Translate[1] = 0.0f;
pushData.model = MatrixIdentity();
pushData.bufferIndex = bufferIndex; // Dummy light data as we don't light debug primitives
pushData.vertexOffset = 0; // Depth-tested vertices start at 0 pushData.LightDirection = {0,0,-1};
pushData.LightPad = 0;
pushData.LightColor = {1,1,1};
pushData.AmbientIntensity = 1.0f;
SetPushConstants(cmdList, ShaderStage::Vertex, 0, sizeof(pushData) / sizeof(uint32), &pushData); SetPushConstants(cmdList, ShaderStage::Vertex, 0, sizeof(pushData) / sizeof(uint32), &pushData);
DrawPrimitives(renderPass, g_DebugState.DepthTestedVertexCount, 1, 0, 0); DrawPrimitives(renderPass, g_DebugState.DepthTestedVertexCount, 1, 0, 0);
@@ -325,18 +330,22 @@ namespace Juliet
BindGraphicsPipeline(renderPass, g_DebugState.OverlayPipeline); BindGraphicsPipeline(renderPass, g_DebugState.OverlayPipeline);
// Pack VP matrix + Model (identity for debug) + buffer index into push constants // Pack VP matrix + Model (identity for debug) + buffer index into push constants
struct PushData pushData = {};
{ pushData.ViewProjection = Camera_GetViewProjectionMatrix(camera);
Matrix vp; pushData.Model = MatrixIdentity();
Matrix model; pushData.BufferIndex = bufferIndex;
uint32 bufferIndex; pushData.TextureIndex = 0;
uint32 vertexOffset; // Offset in vertices (not bytes) pushData.VertexOffset = kMaxDebugVertices / 2; // Overlay vertices start at half
uint32 padding[2]; pushData.Padding = 0;
} pushData; pushData.Scale[0] = 1.0f; pushData.Scale[1] = 1.0f;
pushData.vp = Camera_GetViewProjectionMatrix(camera); pushData.Translate[0] = 0.0f; pushData.Translate[1] = 0.0f;
pushData.model = MatrixIdentity();
pushData.bufferIndex = bufferIndex; // Dummy light data as we don't light debug primitives
pushData.vertexOffset = kMaxDebugVertices / 2; // Overlay vertices start at half pushData.LightDirection = {0,0,-1};
pushData.LightPad = 0;
pushData.LightColor = {1,1,1};
pushData.AmbientIntensity = 1.0f;
SetPushConstants(cmdList, ShaderStage::Vertex, 0, sizeof(pushData) / sizeof(uint32), &pushData); SetPushConstants(cmdList, ShaderStage::Vertex, 0, sizeof(pushData) / sizeof(uint32), &pushData);
DrawPrimitives(renderPass, g_DebugState.OverlayVertexCount, 1, 0, 0); DrawPrimitives(renderPass, g_DebugState.OverlayVertexCount, 1, 0, 0);

View File

@@ -178,6 +178,11 @@ namespace Juliet
for (Mesh& mesh : g_MeshRenderer.Meshes) for (Mesh& mesh : g_MeshRenderer.Meshes)
{ {
pushData.Model = mesh.Transform; pushData.Model = mesh.Transform;
pushData.TextureIndex = 0;
pushData.VertexOffset = static_cast<uint32>(mesh.VertexOffset);
pushData.Padding = 0;
pushData.Scale[0] = 1.0f; pushData.Scale[1] = 1.0f;
pushData.Translate[0] = 0.0f; pushData.Translate[1] = 0.0f;
SetPushConstants(cmdList, ShaderStage::Vertex, 0, sizeof(pushData) / 4, &pushData); SetPushConstants(cmdList, ShaderStage::Vertex, 0, sizeof(pushData) / 4, &pushData);
DrawIndexedPrimitives(pass, static_cast<uint32>(mesh.IndexCount), 1, static_cast<uint32>(mesh.IndexOffset), DrawIndexedPrimitives(pass, static_cast<uint32>(mesh.IndexCount), 1, static_cast<uint32>(mesh.IndexOffset),
static_cast<uint32>(mesh.VertexOffset), 0); static_cast<uint32>(mesh.VertexOffset), 0);

View File

@@ -320,6 +320,10 @@ void JulietApplication::OnRender(RenderPass* pass, CommandList* cmd)
PushData pushData = {}; PushData pushData = {};
pushData.ViewProjection = Camera_GetViewProjectionMatrix(GetDebugCamera()); pushData.ViewProjection = Camera_GetViewProjectionMatrix(GetDebugCamera());
pushData.LightDirection = Normalize({ 0.5f, -1.0f, -0.5f });
pushData.LightColor = { 1.0f, 0.95f, 0.8f };
pushData.AmbientIntensity = 0.2f;
RenderMeshes(pass, cmd, pushData); RenderMeshes(pass, cmd, pushData);
} }