From 03cd007506c50d9734ce3d3bc80f520d228d9276 Mon Sep 17 00:00:00 2001 From: kj16609 Date: Mon, 7 Jul 2025 07:29:47 -0400 Subject: [PATCH] Fixes up shaders --- src/shaders/composition.slang | 12 +++++++----- src/shaders/material.slang | 10 +++++----- src/shaders/objects/camera.slang | 6 +++++- src/shaders/textured.slang | 16 +++++++++++----- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/shaders/composition.slang b/src/shaders/composition.slang index 0a677b6..8643c90 100644 --- a/src/shaders/composition.slang +++ b/src/shaders/composition.slang @@ -22,7 +22,7 @@ Vertex vertexMain( uint index : SV_VertexID ) struct CompositeFragment { vec4 color; -} +}; struct GBufferInput { SubpassInput color : COLOR; @@ -48,7 +48,9 @@ ParameterBlock< CameraData > camera : CAMERA; //TODO: constant flags -vec3 sun_dir = normalize(vec3(-1.0, -1.0, -1.0)); +// Direction the sun is facing +vec3 sun_dir = -normalize( vec3( 0.0, 0.0, 1.0 ) ); +vec3 anti_sun_dir = -sun_dir; // GGX/Towbridge-Reitz normal distribution function. // Uses Disney's reparametrization of alpha = roughness^2. @@ -81,17 +83,17 @@ vec3 schlick(vec3 F0, float cosTheta) return F0 + (vec3(1.0) - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0); } - [shader("fragment")] CompositeFragment fragmentMain( Vertex vertex ) { + CompositeFragment frag; + // const vec3 test_sample = test.SubpassLoad().xyz; // const vec3 albedo = gbuffer.color.SubpassLoad().xyz * test_sample; const vec3 albedo = gbuffer.color.SubpassLoad().xyz; const vec3 position = gbuffer.position.SubpassLoad().xyz; const vec3 normal = gbuffer.normal.SubpassLoad().xyz; - // metallic, roughness, occlusion // r, g, b const vec3 metallic_comb = gbuffer.metallic.SubpassLoad().xyz; @@ -117,7 +119,7 @@ CompositeFragment fragmentMain( Vertex vertex ) vec3 direct_lighting = vec3( 0.0 ); - vec3 Li = -sun_dir; + vec3 Li = anti_sun_dir; vec3 Lradiance = vec3(1.0); vec3 Lh = normalize( Li + Lo ); diff --git a/src/shaders/material.slang b/src/shaders/material.slang index f79e687..aa3eb9e 100644 --- a/src/shaders/material.slang +++ b/src/shaders/material.slang @@ -24,7 +24,7 @@ public struct Metallic { return texture_id != INVALID_TEXTURE_ID; } -} +}; public struct Normal { @@ -35,7 +35,7 @@ public struct Normal { return texture_id != INVALID_TEXTURE_ID; } -} +}; public struct Occlusion { @@ -46,7 +46,7 @@ public struct Occlusion { return texture_id != INVALID_TEXTURE_ID; } -} +}; public struct Emissive { @@ -57,7 +57,7 @@ public struct Emissive { return texture_id != INVALID_TEXTURE_ID; } -} +}; public struct Material @@ -67,5 +67,5 @@ public struct Material public Normal normal; public Occlusion occlusion; public Emissive emissive; -} +}; diff --git a/src/shaders/objects/camera.slang b/src/shaders/objects/camera.slang index c3ae2f4..d2561cc 100644 --- a/src/shaders/objects/camera.slang +++ b/src/shaders/objects/camera.slang @@ -14,4 +14,8 @@ public struct CameraData return vec3( inverse_view[ 0 ][ 3 ], inverse_view[ 1 ][ 3 ], inverse_view[ 2 ][ 3 ] ); } -} \ No newline at end of file + public mat4x4 mat() + { + return mul( projection, view ); + } +}; \ No newline at end of file diff --git a/src/shaders/textured.slang b/src/shaders/textured.slang index 8000653..ad81cee 100644 --- a/src/shaders/textured.slang +++ b/src/shaders/textured.slang @@ -11,7 +11,9 @@ struct CoarseVertex { float4 position : SV_Position; float3 world_pos; uint material_id; -} + mat4x4 matrix; + vec4 tangent; +}; [ [ vk::binding( 0, 1 ) ] ] ConstantBuffer< CameraData > camera : CAMERA; @@ -21,17 +23,19 @@ CoarseVertex vertexMain( ModelVertex in_vertex ) { CoarseVertex out_vertex; - vec4 world_pos = mul(in_vertex.model_matrix, vec4(in_vertex.simple.position, 1.0)); + vec4 world_pos = mul(in_vertex.instance.model_matrix, vec4(in_vertex.simple.position, 1.0)); - const float4 transformed_pos = mul( mul( camera.projection, camera.view ), world_pos ); + const float4 transformed_pos = mul( camera.mat(), world_pos ); out_vertex.position = transformed_pos; out_vertex.world_pos = world_pos.xyz; - mat3 normal_matrix = transpose( inverse( mat3( in_vertex.model_matrix ) ) ); + mat3 normal_matrix = transpose( inverse( mat3( in_vertex.instance.model_matrix ) ) ); out_vertex.normal = normalize( mul(normal_matrix, in_vertex.normal) ); out_vertex.tex_coord = in_vertex.uv; - out_vertex.material_id = in_vertex.material_id; + out_vertex.material_id = in_vertex.instance.material_id; + out_vertex.matrix = in_vertex.instance.model_matrix; + out_vertex.tangent = in_vertex.tangent; return out_vertex; } @@ -78,6 +82,8 @@ GBufferFragment fragmentMain( CoarseVertex vertex ) frag.color = material.color.factors; } + if ( frag.color.w <= 0.0f ) discard; + float metallic_scalar = 0.0; float roughness_scalar = 0.0;