Skip to content

Commit

Permalink
Request shader barycentric extension, use for nicer debug-drawing (#77)
Browse files Browse the repository at this point in the history
* Request shader barycentric extension, use for nicer debug-drawing

* set metallic to zero for debug-colering
  • Loading branch information
crocdialer authored Oct 12, 2024
1 parent f715239 commit 128750f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 47 deletions.
9 changes: 3 additions & 6 deletions shaders/pbr/g_buffer.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_scalar_block_layout : enable

#include "g_buffer_vertex_data.glsl"
#include "../renderer/types.glsl"
#include "../utils/packed_vertex.glsl"
#include "../utils/camera.glsl"
Expand All @@ -31,12 +32,8 @@ layout(push_constant) uniform PushConstants
layout(location = LOCATION_INDEX_BUNDLE) flat out index_bundle_t indices;
layout(location = LOCATION_VERTEX_BUNDLE) out VertexData
{
vec2 tex_coord;
vec3 normal;
vec3 tangent;
vec4 current_position;
vec4 last_position;
} vertex_out;
g_buffer_vertex_data_t vertex_out;
};

void main()
{
Expand Down
9 changes: 3 additions & 6 deletions shaders/pbr/g_buffer_tangent.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_shader_explicit_arithmetic_types: require

#include "g_buffer_vertex_data.glsl"
#include "../renderer/types.glsl"
#include "../utils/packed_vertex.glsl"
#include "../utils/camera.glsl"
Expand All @@ -32,12 +33,8 @@ layout(push_constant) uniform PushConstants
layout(location = LOCATION_INDEX_BUNDLE) flat out index_bundle_t indices;
layout(location = LOCATION_VERTEX_BUNDLE) out VertexData
{
vec2 tex_coord;
vec3 normal;
vec3 tangent;
vec4 current_position;
vec4 last_position;
} vertex_out;
g_buffer_vertex_data_t vertex_out;
};

void main()
{
Expand Down
9 changes: 3 additions & 6 deletions shaders/pbr/g_buffer_tangent_morph.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_scalar_block_layout : enable

#include "g_buffer_vertex_data.glsl"
#include "../renderer/types.glsl"
#include "../utils/packed_vertex.glsl"
#include "../utils/camera.glsl"
Expand Down Expand Up @@ -61,12 +62,8 @@ layout(push_constant) uniform PushConstants
layout(location = LOCATION_INDEX_BUNDLE) flat out index_bundle_t indices;
layout(location = LOCATION_VERTEX_BUNDLE) out VertexData
{
vec2 tex_coord;
vec3 normal;
vec3 tangent;
vec4 current_position;
vec4 last_position;
} vertex_out;
g_buffer_vertex_data_t vertex_out;
};

void main()
{
Expand Down
9 changes: 3 additions & 6 deletions shaders/pbr/g_buffer_tangent_skin.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_shader_explicit_arithmetic_types: require

#include "g_buffer_vertex_data.glsl"
#include "../renderer/types.glsl"
#include "../utils/packed_vertex.glsl"
#include "../utils/camera.glsl"
Expand Down Expand Up @@ -51,12 +52,8 @@ layout(std140, binding = BINDING_JITTER_OFFSET) uniform UBOJitter
layout(location = LOCATION_INDEX_BUNDLE) flat out index_bundle_t indices;
layout(location = LOCATION_VERTEX_BUNDLE) out VertexData
{
vec2 tex_coord;
vec3 normal;
vec3 tangent;
vec4 current_position;
vec4 last_position;
} vertex_out;
g_buffer_vertex_data_t vertex_out;
};

void main()
{
Expand Down
31 changes: 18 additions & 13 deletions shaders/pbr/g_buffer_uber.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#extension GL_ARB_separate_shader_objects : enable
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_fragment_shader_barycentric : require

#include "g_buffer_vertex_data.glsl"
#include "../renderer/types.glsl"

// rnd(state)
Expand All @@ -28,12 +30,8 @@ layout(set = 1, binding = BINDING_TEXTURES) uniform sampler2D u_sampler_2D[];
layout(location = LOCATION_INDEX_BUNDLE) flat in index_bundle_t indices;
layout(location = LOCATION_VERTEX_BUNDLE) in VertexData
{
vec2 tex_coord;
vec3 normal;
vec3 tangent;
vec4 current_position;
vec4 last_position;
} vertex_in;
g_buffer_vertex_data_t vertex_in;
};

layout(location = 0) out vec4 out_color;
layout(location = 1) out vec4 out_normal;
Expand Down Expand Up @@ -94,13 +92,6 @@ void main()
out_emission.rgb *= out_emission.a;
}

// debug object-ids
if(context.debug_draw_ids)
{
uint obj_hash = tea(indices.mesh_draw_index, indices.meshlet_index);// gl_PrimitiveID
out_color.rgb *= vec3(float(obj_hash & 255), float((obj_hash >> 8) & 255), float((obj_hash >> 16) & 255)) / 255.0;
}

// invert normals for two-sided/backface surfels
vec3 normal = (material.two_sided && !gl_FrontFacing) ? -vertex_in.normal : vertex_in.normal;

Expand Down Expand Up @@ -130,4 +121,18 @@ void main()

// motion
out_motion = 0.5 * (vertex_in.current_position.xy / vertex_in.current_position.w - vertex_in.last_position.xy / vertex_in.last_position.w);

// debug object-ids
if(context.debug_draw_ids)
{
uint obj_hash = tea(indices.mesh_draw_index, indices.meshlet_index);// gl_PrimitiveID
out_color.rgb = vec3(float(obj_hash & 255), float((obj_hash >> 8) & 255), float((obj_hash >> 16) & 255)) / 255.0;

// no metallic
out_ao_rough_metal.b = 0.0;

// black triangle edges
float min_bary = min(min(gl_BaryCoordEXT.x, gl_BaryCoordEXT.y), gl_BaryCoordEXT.z) < 0.05 ? 0.0 : 1.0;
out_color.rgb *= min_bary;
}
}
13 changes: 13 additions & 0 deletions shaders/pbr/g_buffer_vertex_data.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef PBR_G_BUFFER_VERTEX_DATA_GLSL
#define PBR_G_BUFFER_VERTEX_DATA_GLSL

struct g_buffer_vertex_data_t
{
vec2 tex_coord;
vec3 normal;
vec3 tangent;
vec4 current_position;
vec4 last_position;
};

#endif// PBR_G_BUFFER_VERTEX_DATA_GLSL
26 changes: 16 additions & 10 deletions src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,22 @@ Device::Device(const create_info_t &create_info) : m_physical_device(create_info
auto extensions = create_info.extensions;
if(create_info.surface) { extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); }

// check if a portability-extension is available/required
if(vierkant::check_device_extension_support(create_info.physical_device, {g_portability_ext_name}))
{
// if this extension is available, it's also mandatory
extensions.push_back(g_portability_ext_name);
}
auto check_extension = [&extensions, &physical_device = create_info.physical_device](const char *ext_name) {
if(vierkant::check_device_extension_support(physical_device, {ext_name}))
{
// if this extension is available, it's also mandatory
extensions.push_back(ext_name);
}
};
check_extension(g_portability_ext_name);

// check if mesh-shading was requested and if so, enable fragment-rate-shading as well.
// this is for some weird reason required by primitive-culling in mesh-shaders
if(crocore::contains(extensions, VK_EXT_MESH_SHADER_EXTENSION_NAME) &&
vierkant::check_device_extension_support(create_info.physical_device,
{VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME}))
if(crocore::contains(extensions, VK_EXT_MESH_SHADER_EXTENSION_NAME))
{
extensions.push_back(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME);
check_extension(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME);
}
check_extension(VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME);

if(!vierkant::check_device_extension_support(create_info.physical_device, extensions))
{
Expand Down Expand Up @@ -305,6 +306,11 @@ Device::Device(const create_info_t &create_info) : m_physical_device(create_info
descriptor_buffer_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_BUFFER_FEATURES_EXT;
update_pnext(descriptor_buffer_features, VK_EXT_DESCRIPTOR_BUFFER_EXTENSION_NAME);

//------------------------------------ VK_KHR_fragment_shader_barycentric ----------------------------------------------------
VkPhysicalDeviceFragmentShaderBarycentricFeaturesKHR barycentric_features = {};
barycentric_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_BARYCENTRIC_FEATURES_KHR;
update_pnext(barycentric_features, VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME);

//------------------------------------------------------------------------------------------------------------------
*pNext = create_info.create_device_pNext;

Expand Down

0 comments on commit 128750f

Please sign in to comment.