Finish slang inital implementation

This commit is contained in:
2025-02-14 20:02:34 -05:00
parent bc326e2c79
commit 59afe41ea0
19 changed files with 115 additions and 329 deletions

View File

@@ -195,6 +195,7 @@ namespace fgl::engine
m_renderer.endFrame( command_buffers );
m_transfer_manager.dump();
m_device.getCmdBufferPool().advanceInFlight();
{
ZoneScopedN( "Post frame hooks" );

View File

@@ -23,24 +23,24 @@ namespace fgl::engine
};
// PBR
data.color_texture_id = getTexID( pbr.color_tex );
data.color_factors = pbr.color_factors;
data.color.color_texture_id = getTexID( pbr.color_tex );
data.color.color_factors = pbr.color_factors;
data.metallic_texture_id = getTexID( pbr.metallic_roughness_tex );
data.roughness_factor = pbr.roughness_factor;
data.metallic_factor = pbr.metallic_factor;
data.metallic.metallic_texture_id = getTexID( pbr.metallic_roughness_tex );
data.metallic.roughness_factor = pbr.roughness_factor;
data.metallic.metallic_factor = pbr.metallic_factor;
// Normal
data.normal_texture_id = getTexID( normal.texture );
data.normal_tex_scale = normal.scale;
data.normal.normal_texture_id = getTexID( normal.texture );
data.normal.normal_tex_scale = normal.scale;
// Occlusion
data.occlusion_texture_id = getTexID( occlusion.texture );
data.occlusion_tex_strength = occlusion.strength;
data.occlusion.occlusion_texture_id = getTexID( occlusion.texture );
data.occlusion.occlusion_tex_strength = occlusion.strength;
// Emissive
data.emissive_texture_id = getTexID( emissive.texture );
data.emissive_factors = emissive.factors;
data.emissive.emissive_texture_id = getTexID( emissive.texture );
data.emissive.emissive_factors = emissive.factors;
}
DeviceMaterialData MaterialProperties::data() const

View File

@@ -81,27 +81,44 @@ namespace fgl::engine
// Alignas to prevent the struct from becoming bigger then needed
struct DeviceMaterialData
{
TextureID color_texture_id { constants::INVALID_TEXTURE_ID };
struct Albedo
{
TextureID color_texture_id { constants::INVALID_TEXTURE_ID };
PAD(1, 12);
glm::vec4 color_factors {};
} color;
PAD( 0, 12 );
struct Metallic
{
// Padding to shove metallic_texture_id to offset 32
TextureID metallic_texture_id { constants::INVALID_TEXTURE_ID };
float metallic_factor { 0.0f };
float roughness_factor { 0.0f };
PAD(1, 4);
} metallic;
glm::vec4 color_factors { 0.0f, 0.0f, 0.0f, 0.0f };
struct Normal
{
TextureID normal_texture_id { constants::INVALID_TEXTURE_ID };
float normal_tex_scale { 0.0f };
PAD(1,8);
} normal;
// Padding to shove metallic_texture_id to offset 32
TextureID metallic_texture_id { constants::INVALID_TEXTURE_ID };
float metallic_factor { 0.0f };
float roughness_factor { 0.0f };
struct Occlusion
{
TextureID occlusion_texture_id { constants::INVALID_TEXTURE_ID };
float occlusion_tex_strength { 0.0f };
PAD(1,8);
} occlusion;
TextureID normal_texture_id { constants::INVALID_TEXTURE_ID };
float normal_tex_scale { 0.0f };
TextureID occlusion_texture_id { constants::INVALID_TEXTURE_ID };
float occlusion_tex_strength { 0.0f };
TextureID emissive_texture_id { constants::INVALID_TEXTURE_ID };
glm::vec3 emissive_factors { 0.0f, 0.0f, 0.0f };
struct Emissive
{
TextureID emissive_texture_id { constants::INVALID_TEXTURE_ID };
glm::vec3 emissive_factors { 0.0f, 0.0f, 0.0f };
} emissive;
};
/*
static_assert( sizeof( DeviceMaterialData ) == 76 );
static_assert( offsetof( DeviceMaterialData, color_factors ) == 16 );
static_assert( offsetof( DeviceMaterialData, metallic_texture_id ) == 32 );
@@ -109,6 +126,7 @@ namespace fgl::engine
static_assert( offsetof( DeviceMaterialData, occlusion_texture_id ) == 52 );
static_assert( offsetof( DeviceMaterialData, emissive_texture_id ) == 60 );
static_assert( offsetof( DeviceMaterialData, emissive_factors ) == 64 );
*/
class Material
{

View File

@@ -267,7 +267,7 @@ namespace fgl::engine
{
const auto& [ pos, scale, rotation ] = m_transform;
const auto rotation_matrix { rotation.mat3() };
const auto rotation_matrix { rotation.mat() };
const glm::vec3 forward { rotation_matrix * glm::vec4( constants::WORLD_FORWARD, 0.0f ) };

View File

@@ -14,6 +14,6 @@ inline const static std::vector< const char* > DEVICE_EXTENSIONS = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, // Used for descriptor indexing
VK_EXT_MESH_SHADER_EXTENSION_NAME, // MAGICAL SHIT
// VK_EXT_MESH_SHADER_EXTENSION_NAME, // MAGICAL SHIT
VK_KHR_DYNAMIC_RENDERING_LOCAL_READ_EXTENSION_NAME // Required until vulkan 1.4
};

View File

@@ -32,19 +32,52 @@ namespace fgl::engine
using ShaderLayoutFlags = std::variant< ShaderVertexFlags, ShaderFragmentFlags >;
void dumpJson( slang::ProgramLayout* layout )
{
Slang::ComPtr< slang::IBlob > json_glob {};
layout->toJson( json_glob.writeRef() );
log::debug(
"Shader layout: {}",
std::string_view(
static_cast< const char* >( json_glob->getBufferPointer() ), json_glob->getBufferSize() ) );
if ( std::ofstream ofs( "./shaders/slang-dump.json" ); ofs )
{
ofs.write( static_cast< const char* >( json_glob->getBufferPointer() ), json_glob->getBufferSize() );
}
}
void checkMaterialDataLayout( slang::ProgramLayout* layout )
{
auto* const type_info { layout->findTypeByName( "Material" ) };
if ( type_info == nullptr ) return;
//TODO: This
}
std::vector< std::byte > compileShader( const std::filesystem::path& path, const ShaderType type )
{
using namespace slang;
Slang::ComPtr< IGlobalSession > global_session {};
SlangGlobalSessionDesc global_desc {};
static Slang::ComPtr< IGlobalSession > global_session {};
static SlangGlobalSessionDesc global_desc {};
global_desc.enableGLSL = true;
const auto setupGlobalSession = [ & ]()
{
global_desc.enableGLSL = true;
createGlobalSession( &global_desc, global_session.writeRef() );
createGlobalSession( &global_desc, global_session.writeRef() );
};
static std::once_flag once {};
std::call_once( once, setupGlobalSession );
SessionDesc session_desc {};
session_desc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR;
std::array< CompilerOptionEntry, 1 > options {
{ { CompilerOptionName::VulkanUseEntryPointName,
{ .kind = CompilerOptionValueKind::Int, .intValue0 = 1 } } }
@@ -119,12 +152,12 @@ namespace fgl::engine
Slang::ComPtr< IBlob > json_glob {};
layout->toJson( json_glob.writeRef() );
/*
log::debug(
"Shader layout: {}",
std::string_view(
static_cast< const char* >( json_glob->getBufferPointer() ), json_glob->getBufferSize() ) );
/*
if ( std::ofstream
ofs( path.parent_path()
/ std::format( "{}-{}.json", path.filename().string(), entry_point_name ) );

View File

@@ -3,7 +3,6 @@
import camera;
struct Vertex {
vec2 uv;
vec4 position : SV_Position;
};
@@ -11,8 +10,12 @@ struct Vertex {
Vertex vertexMain( uint index : SV_VertexID )
{
Vertex vertex;
vertex.uv = vec2( ( index << 1 ) & 2, index & 2 );
vertex.position = vec4( vertex.uv * 2.0 - 1.0f, 0.0f, 0.0f );
vec2 uv = vec2( ( index << 1 ) & 2, index & 2 );
vec2 mulled = uv * vec2( 2.0 );
vec2 minone = mulled - vec2( 1.0, 1.0 );
vertex.position = vec4(minone, 0.0f, 1.0f );
return vertex;
}
@@ -31,6 +34,7 @@ struct GBufferInput {
// SubpassInput< vec4 > test : TEST;
[[vk::binding(0,0)]]
ParameterBlock<GBufferInput> gbuffer : GBUFFER;
// SubpassInput<vec4> color : COLOR;
@@ -92,17 +96,6 @@ CompositeFragment fragmentMain( Vertex vertex )
// r, g, b
const vec3 metallic_comb = gbuffer.metallic.SubpassLoad().xyz;
/*
const vec3 albedo = color.SubpassLoad().xyz;
const vec3 position = position.SubpassLoad().xyz;
const vec3 normal = normal.SubpassLoad().xyz;
// metallic, roughness, occlusion
// r, g, b
const vec3 metallic_comb = metallic.SubpassLoad().xyz;
*/
const float metallic = metallic_comb.r;
const float roughness = metallic_comb.g;
const float occlusion = metallic_comb.b;

View File

@@ -1,5 +0,0 @@
layout (set = 1, binding = 0) uniform CameraInfo {
mat4 projection;
mat4 view;
mat4 inverse_view;
} ubo;

View File

@@ -1,6 +0,0 @@
layout (location = 0) out vec4 out_color;
layout (location = 1) out vec3 out_position;
layout (location = 2) out vec4 out_normal;
layout (location = 3) out vec3 out_metallic;
//layout (location = 4) out vec3 out_specular;
layout (location = 4) out vec3 out_emissive;

View File

@@ -1,47 +0,0 @@
layout (set = 3, binding = 0) uniform Material {
uint color_texture_id;
vec4 color_factors;
uint metallic_texture_id;
float metallic_factor;
float roughness_factor;
uint normal_texture_id;
float normal_scale;
uint occlusion_texture_id;
float occlusion_strength;
uint emissive_texture_id;
vec3 emissive_factors;
} materials[];
bool hasColorTexture()
{
return materials[in_material_id].color_texture_id != INVALID_TEXTURE_ID;
}
bool hasMetallicTexture()
{
return materials[in_material_id].metallic_texture_id != INVALID_TEXTURE_ID;
}
bool hasNormalTexture()
{
return materials[in_material_id].normal_texture_id != INVALID_TEXTURE_ID;
}
bool hasOcclusionTexture()
{
return materials[in_material_id].occlusion_texture_id != INVALID_TEXTURE_ID;
}
bool hasEmissiveTexture()
{
return materials[in_material_id].emissive_texture_id != INVALID_TEXTURE_ID;
}
bool isValidTex(const uint id)
{
return id != INVALID_TEXTURE_ID;
}

View File

@@ -1,15 +0,0 @@

View File

@@ -1,7 +0,0 @@
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec3 normal;
layout (location = 3) in vec2 uv;
layout (location = 4) in mat4 instance_model_matrix;// 4, 5, 6, 7
layout (location = 8) in uint in_material_id;

View File

@@ -2,10 +2,10 @@
module material;
public static const uint INVALID_TEXTURE_ID = 0;
public static const flat uint INVALID_TEXTURE_ID = 0;
public struct Albedo {
public uint texture_id;
public flat uint texture_id;
public vec4 factors;
public bool isTexture()
@@ -16,7 +16,7 @@ public struct Albedo {
public struct Metallic
{
public uint texture_id;
public flat uint texture_id;
public float metallic_factor;
public float roughness_factor;
@@ -28,7 +28,7 @@ public struct Metallic
public struct Normal
{
public uint texture_id;
public flat uint texture_id;
public float scale;
public bool isTexture()
@@ -39,7 +39,7 @@ public struct Normal
public struct Occlusion
{
public uint texture_id;
public flat uint texture_id;
public float strength;
public bool isTexture()
@@ -50,7 +50,7 @@ public struct Occlusion
public struct Emissive
{
public uint texture_id;
public flat uint texture_id;
public vec3 factors;
public bool isTexture()

View File

@@ -1,41 +0,0 @@
#version 450
#extension GL_EXT_nonuniform_qualifier: enable
#extension GL_EXT_debug_printf: enable
layout (location = 0) in vec3 in_normal;
layout (location = 1) in vec2 in_tex_coord;
layout (location = 2) in vec3 in_world_pos;
layout (location = 3) in flat uint in_tex_idx;
layout (location = 0) out vec4 out_position;
layout (location = 1) out vec4 out_normal;
layout (location = 2) out vec4 out_albedo;
layout (set = 0, binding = 0) uniform CameraInfo {
mat4 projection;
mat4 view;
mat4 inverse_view;
} ubo;
layout (set = 1, binding = 0) uniform sampler2D tex[];
#define NEAR_PLANE 0.01f
#define FAR_PLANE 1000.0f
float linearDepth(float depth)
{
float z = depth * 2.0f - 1.0f;
return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE));
}
void main()
{
out_position = vec4(in_world_pos, 1.0f);
vec3 N = normalize(in_normal);
out_normal = vec4(N, 1.0);
out_albedo = texture(tex[in_tex_idx], in_tex_coord);
}

View File

@@ -1,56 +0,0 @@
#version 450
layout(location = 0) in vec3 in_normal[];
layout(location = 1) in vec3 in_color[];
layout(location = 2) in vec2 in_uv[];
layout(location = 3) in flat uint in_tex_idx[];
layout(location = 4) in flat float in_scale_z[];
layout(vertices = 4) out;
layout(location = 0) out vec3 out_normal[4];
layout(location = 1) out vec2 out_uv[4];
layout(location = 2) out flat uint out_tex_idx[4];
layout(location = 3) out flat float out_scale_z[4];
layout (set = 0, binding = 0) uniform CameraInfo {
mat4 projection;
mat4 view;
mat4 inverse_view;
} ubo;
vec3 midpoint(vec3 point1, vec3 point2)
{
return (point1 + point2) / vec3(2, 2, 2);
}
float tesselationLevel(vec4 point1, vec4 point2)
{
vec3 mid = midpoint(vec3(point1), vec3(point2));
vec3 camera_pos = ubo.inverse_view[3].xyz;
float max_tess = 32.0f;
float dist_multip = 8.0f;
float dist = max_tess - (distance(camera_pos, mid) / dist_multip);
return clamp(dist, 1.0f, max_tess);
}
void main()
{
gl_TessLevelOuter[0] = tesselationLevel(gl_in[3].gl_Position, gl_in[0].gl_Position);
gl_TessLevelOuter[1] = tesselationLevel(gl_in[0].gl_Position, gl_in[1].gl_Position);
gl_TessLevelOuter[2] = tesselationLevel(gl_in[1].gl_Position, gl_in[2].gl_Position);
gl_TessLevelOuter[3] = tesselationLevel(gl_in[2].gl_Position, gl_in[3].gl_Position);
gl_TessLevelInner[0] = mix(gl_TessLevelOuter[1], gl_TessLevelOuter[3], 0.5);
gl_TessLevelInner[1] = mix(gl_TessLevelOuter[0], gl_TessLevelOuter[2], 0.5);
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
out_normal[gl_InvocationID] = in_normal[gl_InvocationID];
out_uv[gl_InvocationID] = in_uv[gl_InvocationID];
out_tex_idx[gl_InvocationID] = in_tex_idx[gl_InvocationID];
out_scale_z[gl_InvocationID] = in_scale_z[gl_InvocationID];
}

View File

@@ -1,54 +0,0 @@
#version 450
#extension GL_EXT_nonuniform_qualifier: enable
layout(quads, equal_spacing, cw) in;
layout(location = 0) in vec3 in_normal[];
layout(location = 1) in vec2 in_uv[];
layout(location = 2) in flat uint in_tex_idx[];
layout(location = 3) in flat float in_scale_z[];
layout(location = 0) out vec3 out_normal;
layout(location = 1) out vec2 out_uv;
layout(location = 2) out vec3 out_world_pos;
layout(location = 3) out uint out_tex_idx;
layout(set = 0, binding = 0) uniform CameraInfo
{
mat4 projection;
mat4 view;
mat4 inverse_view;
} ubo;
layout (set = 1, binding = 0) uniform sampler2D tex[];
void main()
{
// Mix new UV
vec2 uv1 = mix(in_uv[0], in_uv[1], gl_TessCoord.x);
vec2 uv2 = mix(in_uv[3], in_uv[2], gl_TessCoord.x);
out_uv = mix(uv1, uv2, gl_TessCoord.y);
// Mix new normal
vec3 n1 = mix(in_normal[0], in_normal[1], gl_TessCoord.x);
vec3 n2 = mix(in_normal[3], in_normal[2], gl_TessCoord.x);
out_normal = mix(n1, n2, gl_TessCoord.y);
// Mix new position
vec4 pos1 = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x);
vec4 pos2 = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x);
vec4 pos = mix(pos1, pos2, gl_TessCoord.y);
// Passthrough Tex ID to fragment shader
out_tex_idx = in_tex_idx[0];
vec4 texel = texture(tex[out_tex_idx], out_uv);
// Doesn't matter which in_scale_z we take since it'll be the same for the entire model.
pos.z += (texture(tex[out_tex_idx], out_uv).r - 0.5) * in_scale_z[0];
vec4 world_pos = ubo.projection * ubo.view * pos;
out_world_pos = vec3(world_pos);
gl_Position = world_pos;
}

View File

@@ -1,33 +0,0 @@
#version 450
#extension GL_EXT_debug_printf: enable
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec3 normal;
layout (location = 3) in vec2 uv;
layout (location = 4) in mat4 instance_model_matrix;// 4, 5, 6, 7
layout (location = 8) in uint in_texture_id;
layout (location = 0) out vec3 out_normal;
layout (location = 1) out vec3 out_color;
layout (location = 2) out vec2 out_tex_coord;
layout (location = 3) out flat uint out_texture_idx;
layout (location = 4) out flat float out_scale_z;
void main() {
gl_Position = instance_model_matrix * vec4(position, 1.0f);
//We want to grab the scale value
out_scale_z = length(instance_model_matrix[2]);
mat3 normal_matrix = transpose(inverse(mat3(instance_model_matrix)));
out_normal = normalize(normal_matrix * normal);
out_color = color;
out_tex_coord = uv;
out_texture_idx = in_texture_id;
}

View File

@@ -5,10 +5,10 @@ import camera;
import gbuffer;
import material;
struct Vertex {
struct CoarseVertex {
vec3 normal;
vec2 tex_coord;
vec3 world_pos;
float4 position : SV_Position;
uint material_id;
}
@@ -16,17 +16,17 @@ struct Vertex {
ParameterBlock< CameraData > camera : CAMERA;
[shader("vertex")]
Vertex vertexMain( ModelVertex in_vertex )
CoarseVertex vertexMain( ModelVertex in_vertex )
{
Vertex out_vertex;
CoarseVertex out_vertex;
vec4 world_pos = in_vertex.model_matrix * vec4(in_vertex.simple.position, 1.0);
vec4 world_pos = mul(in_vertex.model_matrix, vec4(in_vertex.simple.position, 1.0));
out_vertex.world_pos = (camera.projection * camera.view * world_pos).xyz;
out_vertex.position = mul(mul(camera.projection , camera.view), world_pos);
mat3 normal_matrix = transpose( inverse( mat3( in_vertex.model_matrix ) ) );
out_vertex.normal = normalize( normal_matrix * in_vertex.simple.normal );
out_vertex.normal = normalize( mul(normal_matrix, in_vertex.simple.normal) );
out_vertex.tex_coord = in_vertex.simple.uv;
out_vertex.material_id = in_vertex.material_id;
@@ -42,16 +42,19 @@ Sampler2D[ ] tex : TEXTURES;
static const float MIN_ROUGHNESS = 0.04;
[shader("fragment")]
GBufferFragment fragmentMain( Vertex vertex : Vertex )
GBufferFragment fragmentMain( CoarseVertex vertex )
{
GBufferFragment frag;
frag.position = vertex.world_pos;
frag.position = vertex.position.xyz;
vec3 diffuse_color;
vec4 base_color;
vec3 f0 = vec3( 0.04 );
frag.color = vec4(1.0, 1.0, 1.0, 1.0);
if ( vertex.material_id == INVALID_TEXTURE_ID )
{
discard;
@@ -59,6 +62,8 @@ GBufferFragment fragmentMain( Vertex vertex : Vertex )
Material material = materials[vertex.material_id];
frag.color = material.color.factors;
if ( material.color.isTexture() )
{
vec4 color = texture( tex[ material.color.texture_id ], vertex.tex_coord );
@@ -68,7 +73,7 @@ GBufferFragment fragmentMain( Vertex vertex : Vertex )
else
{
frag.color = material.color.factors;
}
}
float metallic_scalar = 0.0;
float roughness_scalar = 0.0;
@@ -100,7 +105,7 @@ GBufferFragment fragmentMain( Vertex vertex : Vertex )
else
{
frag.normal = vec4( vertex.normal, 1.0 );
}
}
return frag;
}