reimplement shading

This commit is contained in:
2026-02-05 15:23:53 +00:00
parent c5e6c1c5d8
commit 624dfa8cd3
8 changed files with 178 additions and 186 deletions
+12 -17
View File
@@ -3,15 +3,16 @@
layout(constant_id = 0) const bool SPEC_BOUNDING_BOXES = false;
layout(constant_id = 1) const bool SPEC_BRUTE_FORCE = false;
layout(constant_id = 2) const bool SPEC_SHOW_AO = false;
layout(constant_id = 3) const bool SPEC_SHOW_NORMALS = false;
layout(constant_id = 4) const bool SPEC_SHOW_DEPTH = false;
layout(constant_id = 2) const bool SPEC_SHOW_NORMALS = false;
layout(constant_id = 3) const bool SPEC_SHOW_DEPTH = false;
layout(constant_id = 4) const bool SPEC_SHOW_POSITION = false;
layout(constant_id = 5) const bool SPEC_SHOW_ALBEDO = false;
layout(constant_id = 6) const bool SPEC_SHOW_MATERIAL = false;
layout(constant_id = 7) const bool SPEC_SHOW_ALL = false;
layout(constant_id = 8) const bool SPEC_DISABLE_AO = false;
layout(constant_id = 9) const bool SPEC_USE_LINEAR_AO = false;
layout(constant_id = 10) const bool SPEC_USE_CPU_ENABLE_GEN = false;
layout(constant_id = 6) const bool SPEC_SHOW_MATERIAL1 = false;
layout(constant_id = 7) const bool SPEC_SHOW_MATERIAL2 = false;
layout(constant_id = 8) const bool SPEC_SHOW_MATERIAL3 = false;
layout(constant_id = 9) const bool SPEC_SHOW_ALL = false;
layout(constant_id = 10) const bool SPEC_DISABLE_AO = false;
layout(constant_id = 11) const bool SPEC_USE_LINEAR_AO = false;
//layout(constant_id = 30) const uint SPEC_MAXIMUM_SUBDIVISION = 64;
const uint SPEC_MAXIMUM_SUBDIVISION = 64;
@@ -74,18 +75,12 @@ struct TenTenTenTwo {
};
uint single_t_compress(float inp) {
uint t = uint(clamp(abs(inp), 0.0, 1.0) * float(0x1FF));
if (inp < 0.) {
t |= 0x200;
}
uint t = uint(clamp(inp, 0.0, 1.0) * float(0x3FF));
return t;
}
float single_t_uncompress(uint inp) {
float t = float(inp & 0x1FF) / float(0x1FF);
if ((inp & 0x200) > 0) {
t = -t;
}
float t = float(inp & 0x3FF) / float(0x3FF);
return t;
}
@@ -106,11 +101,11 @@ vec3 tttt_uncompress(TenTenTenTwo inp) {
}
struct RTGBuffer {
TenTenTenTwo normal;
TenTenTenTwo base_color;
TenTenTenTwo roughness_specular_specular_tint;
TenTenTenTwo sheen_tint_metallic_sheen;
TenTenTenTwo clear_coat_gloss_subsurface_clear_coat;
vec3 normal;
float t;
};
@@ -1,102 +0,0 @@
#version 460
#extension GL_GOOGLE_include_directive:require
#include "include.glsl"
#include "brdf.glsl"
layout(input_attachment_index = 0, set = 1, binding = 0) uniform subpassInput u_color;
layout(input_attachment_index = 1, set = 1, binding = 1) uniform subpassInput u_normals;
layout(input_attachment_index = 2, set = 1, binding = 2) uniform subpassInput u_ao_r_m;
layout(input_attachment_index = 3, set = 1, binding = 3) uniform subpassInput u_depth;
layout(input_attachment_index = 4, set = 1, binding = 4) uniform usubpassInput u_material;
layout(location = 0) in vec2 v_pos;
layout(location = 0) out vec4 f_color;
vec3 calculate_view_position(float depth)
{
vec4 view_position = camera_uniforms.inv_proj_view * vec4(v_pos, depth, 1.0);
return (view_position.xyz / view_position.w);
}
vec3 shading(vec3 albedo, vec3 normal, vec3 position, vec3 ao_r_m)
{
vec3 accum = vec3(0., 0., 0.);
BRDFInput inp;
inp.base_color = albedo;
inp.N = normal;
inp.V = normalize(camera_uniforms.campos_and_time.xyz - position);
inp.roughness = ao_r_m.y;
inp.specular = camera_uniforms.specular;
inp.specular_tint = camera_uniforms.specular_tint;
inp.sheen_tint = camera_uniforms.sheen_tint;
inp.metallic = ao_r_m.z;
inp.sheen = camera_uniforms.sheen;
inp.clear_coat_gloss = camera_uniforms.clear_coat_gloss;
inp.subsurface = camera_uniforms.subsurface;
inp.clear_coat = camera_uniforms.clear_coat;
BRDFResults r;
for (int i = 0; (i < light_uniforms.light_count) && (i < 32); i++)
{
inp.L = normalize(light_uniforms.pos[i].xyz - position);
r = DisneyBRDF(inp);
vec3 outp = light_uniforms.col[i].xyz * (r.diffuse + r.specular + r.clearcoat);
outp *= clamp(dot(inp.N, inp.L), -1., 1.);
accum += max(vec3(0.0), outp);
}
return accum * ao_r_m.x;
}
void main() {
if (SPEC_SHOW_AO) {
f_color = subpassLoad(u_ao_r_m);
} else if (SPEC_SHOW_NORMALS) {
f_color = subpassLoad(u_normals);
} else if (SPEC_SHOW_DEPTH) {
f_color = subpassLoad(u_depth) * 100.0;
} else if (SPEC_SHOW_ALBEDO) {
f_color = subpassLoad(u_color);
} else if (SPEC_SHOW_MATERIAL) {
f_color = subpassLoad(u_material) / 16.;
} else {
float depth = subpassLoad(u_depth).x;
if (depth == 1.0 && !SPEC_SHOW_ALL) {
discard;
}
vec3 albedo = subpassLoad(u_color).xyz;
vec3 normal = subpassLoad(u_normals).xyz;
vec3 ao_r_m = subpassLoad(u_ao_r_m).xyz;
vec3 position = calculate_view_position(depth);
uint material = subpassLoad(u_material).x;
vec3 shading = shading(albedo, normal, position, ao_r_m);
if (SPEC_SHOW_ALL) {
switch (uint(floor((v_pos.x + 1.0) * (6.0 / 2.0)))) {
default:
case 0:
f_color = vec4(shading, 1.0);
break;
case 1:
f_color = subpassLoad(u_ao_r_m);
break;
case 2:
f_color = subpassLoad(u_normals);
break;
case 3:
f_color = subpassLoad(u_depth) * 100.0;
break;
case 4:
f_color = subpassLoad(u_color);
break;
case 5:
f_color = subpassLoad(u_material) / 16.;
break;
}
} else {
f_color = vec4(shading, 1.0);
}
}
}
+9 -4
View File
@@ -3,7 +3,7 @@
#extension GL_GOOGLE_include_directive:require
#include "include.glsl"
layout(location = 0) rayPayloadInEXT vec3 hit_value;
layout(location = 0) rayPayloadInEXT RTGBuffer hit_value;
hitAttributeEXT vec2 baryCoord;
Object obj;
@@ -23,11 +23,16 @@ void main() {
// interpolate with barycentric coordinates
vec3 barys = vec3(1.0f - baryCoord.x - baryCoord.y, baryCoord.x, baryCoord.y);
vec3 localNormal = normalize(v0.normal * barys.x + v1.normal * barys.y + v2.normal * barys.z).xyz;
vec3 localPosition = (v0.pos * barys.x + v1.pos * barys.y + v2.pos * barys.z).xyz;
//vec3 localPosition = (v0.pos * barys.x + v1.pos * barys.y + v2.pos * barys.z).xyz;
// transform to world space
vec3 normal = normalize((gl_ObjectToWorldEXT * vec4(localNormal, 1.0)).xyz);
vec3 position = gl_ObjectToWorldEXT * vec4(localPosition, 1.0);
//vec3 position = gl_ObjectToWorldEXT * vec4(localPosition, 1.0);
hit_value = normal * 0.5 + 0.5;
hit_value.normal = normal;
hit_value.base_color = tttt_compress(obj.colour_and_roughness.xyz);
hit_value.roughness_specular_specular_tint = tttt_compress(vec3(obj.colour_and_roughness.w, obj.specular, obj.specular_tint));
hit_value.sheen_tint_metallic_sheen = tttt_compress(vec3(obj.sheen_tint, obj.metallic, obj.sheen));
hit_value.clear_coat_gloss_subsurface_clear_coat = tttt_compress(vec3(obj.clear_coat_gloss, obj.subsurface, obj.clear_coat));
hit_value.t = gl_HitTEXT;
}
+95 -2
View File
@@ -2,12 +2,45 @@
#extension GL_EXT_ray_tracing : require
#extension GL_GOOGLE_include_directive:require
#include "include.glsl"
#include "brdf.glsl"
layout(location = 0) rayPayloadEXT vec3 hit_value;
layout(location = 0) rayPayloadEXT RTGBuffer hit_value;
layout(set = 1, binding = 0) uniform accelerationStructureEXT top_level_as;
layout(set = 2, binding = 0, rgba32f) uniform image2D image;
vec3 shading(vec3 position, vec3 direction, vec3 base_color, vec3 normal, vec3 material1, vec3 material2, vec3 material3)
{
vec3 accum = vec3(0., 0., 0.);
BRDFInput inp;
inp.base_color = base_color;
inp.N = normal;
inp.V = direction;
inp.roughness = material1.x;
inp.specular = material1.y;
inp.specular_tint = material1.z;
inp.sheen_tint = material2.x;
inp.metallic = material2.y;
inp.sheen = material2.z;
inp.clear_coat_gloss = material3.x;
inp.subsurface = material3.y;
inp.clear_coat = material3.z;
BRDFResults r;
for (int i = 0; (i < light_uniforms.light_count) && (i < 32); i++)
{
inp.L = normalize(light_uniforms.pos[i].xyz - position);
r = DisneyBRDF(inp);
vec3 outp = light_uniforms.col[i].xyz * (r.diffuse + r.specular + r.clearcoat);
outp *= clamp(dot(inp.N, inp.L), -1., 1.);
accum += max(vec3(0.0), outp);
}
return accum; //* ao_r_m.x;
}
void main() {
const vec2 pixel_center = vec2(gl_LaunchIDEXT.xy) + vec2(0.5);
const vec2 in_uv = pixel_center / vec2(gl_LaunchSizeEXT.xy);
@@ -35,5 +68,65 @@ void main() {
t_max, // ray max range
0); // payload (location = 0)
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(hit_value, 1.0));
vec3 frag_color = vec3(0.0, 0.0, 0.2);
if (hit_value.t > 0) {
vec3 position = origin.xyz + (direction.xyz * hit_value.t);
vec3 albedo = tttt_uncompress(hit_value.base_color);
vec3 normal = hit_value.normal;
vec3 material1 = tttt_uncompress(hit_value.roughness_specular_specular_tint);
vec3 material2 = tttt_uncompress(hit_value.sheen_tint_metallic_sheen);
vec3 material3 = tttt_uncompress(hit_value.clear_coat_gloss_subsurface_clear_coat);
vec3 depth = vec3(hit_value.t) / 10.0;
vec3 shading = shading(position, direction.xyz, albedo, normal, material1, material2, material3);
if (SPEC_SHOW_NORMALS) {
frag_color = normal * 0.5 + 0.5;
} else if (SPEC_SHOW_DEPTH) {
frag_color = depth;
} else if (SPEC_SHOW_POSITION) {
frag_color = position;
} else if (SPEC_SHOW_ALBEDO) {
frag_color = albedo;
} else if (SPEC_SHOW_MATERIAL1) {
frag_color = material1;
} else if (SPEC_SHOW_MATERIAL2) {
frag_color = material2;
} else if (SPEC_SHOW_MATERIAL3) {
frag_color = material3;
} else {
if (SPEC_SHOW_ALL) {
switch (uint(floor(in_uv.x * 8.0))) {
default:
case 0:
frag_color = shading;
break;
case 1:
frag_color = normal * 0.5 + 0.5;
break;
case 2:
frag_color = depth;
break;
case 3:
frag_color = position;
break;
case 4:
frag_color = albedo;
break;
case 5:
frag_color = material1;
break;
case 6:
frag_color = material2;
break;
case 7:
frag_color = material3;
break;
}
} else {
frag_color = shading;
}
}
}
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(frag_color, 1.0));
}
+2 -2
View File
@@ -3,8 +3,8 @@
#extension GL_GOOGLE_include_directive:require
#include "include.glsl"
layout(location = 0) rayPayloadInEXT vec3 hit_value;
layout(location = 0) rayPayloadInEXT RTGBuffer hit_value;
void main() {
hit_value = vec3(0.0, 0.0, 0.2);
hit_value.t = -1.;
}
@@ -4,11 +4,9 @@
#include "include.glsl"
#include "implicit_include.glsl"
//layout(location = 0) out vec4 f_color;
//layout(location = 1) out vec4 f_normal;
//layout(location = 2) out vec4 f_ao_r_m;
layout(location = 0) rayPayloadInEXT RTGBuffer hit_value;
layout(location = 0) rayPayloadInEXT vec3 hit_value;
Object obj;
const float EPSILON = .0001;
@@ -20,22 +18,16 @@ vec3 getNormal(vec3 p) {
return normalize(n - (scene(vec4(p, camera_uniforms.campos_and_time.w))));
}
//Implicit Surface Entrypoint
void main() {
obj = objects.objects[gl_InstanceID];
vec3 position = gl_ObjectRayOriginEXT + (gl_ObjectRayDirectionEXT * gl_HitTEXT);
vec3 n = getNormal(position);
hit_value = n * 0.5 + 0.5;
vec3 normal = getNormal(position);
//f_color = vec4(object_uniforms.colour, 1.0);
//f_ao_r_m.y = object_uniforms.roughness;
//f_ao_r_m.z = object_uniforms.metallic;
//mat3 rotation = mat3(pc.world[0].xyz, pc.world[1].xyz, pc.world[2].xyz);
//f_normal = vec4(normalize(rotation * n), 1.0);
//f_ao_r_m.x = 1.0;
return;
hit_value.normal = normal;
hit_value.base_color = tttt_compress(obj.colour_and_roughness.xyz);
hit_value.roughness_specular_specular_tint = tttt_compress(vec3(obj.colour_and_roughness.w, obj.specular, obj.specular_tint));
hit_value.sheen_tint_metallic_sheen = tttt_compress(vec3(obj.sheen_tint, obj.metallic, obj.sheen));
hit_value.clear_coat_gloss_subsurface_clear_coat = tttt_compress(vec3(obj.clear_coat_gloss, obj.subsurface, obj.clear_coat));
hit_value.t = gl_HitTEXT;
}
+18 -13
View File
@@ -17,15 +17,16 @@ fn sized_text(ui: &mut egui::Ui, text: impl Into<String>, size: f32) {
pub(crate) struct PreviousDebug {
pub(crate) bounding_boxes: bool,
pub(crate) brute_force: bool,
pub(crate) show_ao: bool,
pub(crate) show_normals: bool,
pub(crate) show_depth: bool,
pub(crate) show_position: bool,
pub(crate) show_albedo: bool,
pub(crate) show_material: bool,
pub(crate) show_material1: bool,
pub(crate) show_material2: bool,
pub(crate) show_material3: bool,
pub(crate) show_all: bool,
pub(crate) disable_ao: bool,
pub(crate) use_linear_ao: bool,
pub(crate) use_cpu_enable_gen: bool,
}
#[derive(Debug)]
@@ -295,10 +296,6 @@ pub(crate) fn gui_up(gui: &mut Gui, state: &mut GState) {
&mut state.debug.brute_force,
"Brute force the render (use no new techniques)",
);
ui.toggle_value(
&mut state.debug.show_ao,
"Display the ambient occlusion render attachment",
);
ui.toggle_value(
&mut state.debug.show_normals,
"Display the normals render attachment",
@@ -307,13 +304,25 @@ pub(crate) fn gui_up(gui: &mut Gui, state: &mut GState) {
&mut state.debug.show_depth,
"Display the depth render attachment",
);
ui.toggle_value(
&mut state.debug.show_position,
"Display the position render attachment",
);
ui.toggle_value(
&mut state.debug.show_albedo,
"Display the albedo render attachment",
);
ui.toggle_value(
&mut state.debug.show_material,
"Display the material render attachment",
&mut state.debug.show_material1,
"Display the material 1 render attachment",
);
ui.toggle_value(
&mut state.debug.show_material2,
"Display the material 2 render attachment",
);
ui.toggle_value(
&mut state.debug.show_material3,
"Display the material 3 render attachment",
);
ui.toggle_value(
&mut state.debug.show_all,
@@ -327,10 +336,6 @@ pub(crate) fn gui_up(gui: &mut Gui, state: &mut GState) {
&mut state.debug.use_linear_ao,
"Use linear falloff for occlusion calculation",
);
ui.toggle_value(
&mut state.debug.use_cpu_enable_gen,
"Use CPU side enable flag generation",
);
state.recreate_pipelines = ui.button("Force recompile pipelines").clicked();
state.reset_time = ui.button("Reset time to 0").clicked();
+24 -20
View File
@@ -1276,6 +1276,7 @@ impl App {
self.descriptor_set_allocator.clone(),
rt_layout1.clone(),
[
WriteDescriptorSet::buffer(0, rcx.lights_buffers[index].clone()),
WriteDescriptorSet::buffer(1, rcx.camera_buffers[index].clone()),
WriteDescriptorSet::buffer(2, self.vertex_buffer.clone()),
WriteDescriptorSet::buffer(3, self.index_buffer.clone()),
@@ -2240,16 +2241,16 @@ fn framebuffer_generation(
fn get_spec_constants(debug: &PreviousDebug) -> HashMap<u32, SpecializationConstant> {
const SPEC_BOUNDING_BOXES: u32 = 0;
const SPEC_BRUTE_FORCE: u32 = 1;
const SPEC_SHOW_AO: u32 = 2;
const SPEC_SHOW_NORMALS: u32 = 3;
const SPEC_SHOW_DEPTH: u32 = 4;
const SPEC_SHOW_NORMALS: u32 = 2;
const SPEC_SHOW_DEPTH: u32 = 3;
const SPEC_SHOW_POSITION: u32 = 4;
const SPEC_SHOW_ALBEDO: u32 = 5;
const SPEC_SHOW_MATERIAL: u32 = 6;
const SPEC_SHOW_ALL: u32 = 7;
const SPEC_DISABLE_AO: u32 = 8;
const SPEC_USE_LINEAR_AO: u32 = 9;
const SPEC_USE_CPU_ENABLE_GEN: u32 = 10;
const SPEC_MAXIMUM_SUBDISIVION: u32 = 30;
const SPEC_SHOW_MATERIAL1: u32 = 6;
const SPEC_SHOW_MATERIAL2: u32 = 7;
const SPEC_SHOW_MATERIAL3: u32 = 8;
const SPEC_SHOW_ALL: u32 = 9;
const SPEC_DISABLE_AO: u32 = 10;
const SPEC_USE_LINEAR_AO: u32 = 11;
let mut spec_constants = HashMap::with_capacity(6);
spec_constants.insert(
@@ -2260,7 +2261,6 @@ fn get_spec_constants(debug: &PreviousDebug) -> HashMap<u32, SpecializationConst
SPEC_BRUTE_FORCE,
SpecializationConstant::Bool(debug.brute_force),
);
spec_constants.insert(SPEC_SHOW_AO, SpecializationConstant::Bool(debug.show_ao));
spec_constants.insert(
SPEC_SHOW_NORMALS,
SpecializationConstant::Bool(debug.show_normals),
@@ -2269,13 +2269,25 @@ fn get_spec_constants(debug: &PreviousDebug) -> HashMap<u32, SpecializationConst
SPEC_SHOW_DEPTH,
SpecializationConstant::Bool(debug.show_depth),
);
spec_constants.insert(
SPEC_SHOW_POSITION,
SpecializationConstant::Bool(debug.show_position),
);
spec_constants.insert(
SPEC_SHOW_ALBEDO,
SpecializationConstant::Bool(debug.show_albedo),
);
spec_constants.insert(
SPEC_SHOW_MATERIAL,
SpecializationConstant::Bool(debug.show_material),
SPEC_SHOW_MATERIAL1,
SpecializationConstant::Bool(debug.show_material1),
);
spec_constants.insert(
SPEC_SHOW_MATERIAL2,
SpecializationConstant::Bool(debug.show_material2),
);
spec_constants.insert(
SPEC_SHOW_MATERIAL3,
SpecializationConstant::Bool(debug.show_material3),
);
spec_constants.insert(SPEC_SHOW_ALL, SpecializationConstant::Bool(debug.show_all));
spec_constants.insert(
@@ -2286,14 +2298,6 @@ fn get_spec_constants(debug: &PreviousDebug) -> HashMap<u32, SpecializationConst
SPEC_USE_LINEAR_AO,
SpecializationConstant::Bool(debug.use_linear_ao),
);
spec_constants.insert(
SPEC_USE_CPU_ENABLE_GEN,
SpecializationConstant::Bool(debug.use_cpu_enable_gen),
);
spec_constants.insert(
SPEC_MAXIMUM_SUBDISIVION,
SpecializationConstant::U32(MAXIMUM_SUBDIVISION),
);
trace!("Specs: {:?}", spec_constants);