Skip to content

Commit

Permalink
PathTracer: enabled simple AO in shader (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadult authored Oct 19, 2024
1 parent 7e86ec1 commit f8f8d3b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
55 changes: 33 additions & 22 deletions data/shaders/trace.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ layout(binding = 1) uniform config_ { PathTracerConfig u_config; };

layout(binding = 2, rgba8) uniform image2D g_raster_image;

layout(binding = 4) buffer buf04_ { uint g_indices[]; };
layout(binding = 5) buffer buf05_ { float g_vertices[]; };
layout(binding = 6) uniform accelerationStructureEXT g_accelStruct;

Expand All @@ -44,6 +45,14 @@ vec3 getVertex(uint idx) {
return vec3(g_vertices[idx * 3 + 0], g_vertices[idx * 3 + 1], g_vertices[idx * 3 + 2]);
}

void getTriangleVertices(uint tri_id, out vec3 tri0, out vec3 tri1, out vec3 tri2) {
uint idx0 = g_indices[tri_id * 3 + 0], idx1 = g_indices[tri_id * 3 + 1],
idx2 = g_indices[tri_id * 3 + 2];
tri0 = getVertex(idx0);
tri1 = getVertex(idx1);
tri2 = getVertex(idx2);
}

void getTriangleVectors(in vec3 tri0, in vec3 tri1, in vec3 tri2, out vec3 tangent, out vec3 normal,
out vec3 binormal) {
tangent = normalize(tri1 - tri0);
Expand Down Expand Up @@ -72,6 +81,7 @@ TraceResult rayTraceAS(vec3 origin, vec3 dir) {
if(rayQueryGetIntersectionTypeEXT(rq, true) != 0) {
result.dist = rayQueryGetIntersectionTEXT(rq, true);
result.tri_id = rayQueryGetIntersectionPrimitiveIndexEXT(rq, true);
// rayQueryGetIntersectionBarycentricsEXT
} else {
result.dist = MAX_ISECT_DIST;
result.tri_id = INVALID_TRI_ID;
Expand All @@ -94,7 +104,28 @@ void getScreenRay(ivec2 pixel_pos, out vec3 origin, out vec3 dir) {
dir += vec3(0.0000001); // avoiding division by 0
}

//#define COMPUTE_AO
float computeAO(uint tri_id, vec3 hit_point) {
vec3 tri[3];
getTriangleVertices(tri_id, tri[0], tri[1], tri[2]);
vec3 tri_vecs[3];
getTriangleVectors(tri[0], tri[1], tri[2], tri_vecs[0], tri_vecs[1], tri_vecs[2]);

const int dim_size = 4;
int hits = 0, total = (dim_size + 1) * (dim_size + 1);

for(int x = 0; x <= dim_size; x++)
for(int y = 0; y <= dim_size; y++) {
vec2 pos = vec2(x, y) * (1.0 / dim_size);
vec3 hemi = uniformSampleHemisphere(pos);
vec3 dir = tri_vecs[0] * hemi[0] + tri_vecs[1] * hemi[2] + tri_vecs[2] * hemi[1];
vec3 origin = hit_point + dir * 0.001;
TraceResult ao_hit = rayTraceAS(origin, dir);
if(ao_hit.dist > 0.05 && ao_hit.dist < 5.0 && ao_hit.tri_id != tri_id)
hits++;
}

return max(0.0, (total - hits) / float(total) - 0.1) * (1.0 / 0.9);
}

void traceBin() {
ivec2 pixel_pos = ivec2(LIX & 31, LIX >> 5) + s_bin_pos;
Expand All @@ -103,30 +134,10 @@ void traceBin() {

TraceResult result = rayTraceAS(ray_origin, ray_dir);
float ao_value = 1.0;

#ifdef COMPUTE_AO
if(result.dist < MAX_ISECT_DIST) {
vec3 tri_vecs[3];
getTriangleVectors(result.tri_id, tri_vecs[0], tri_vecs[1], tri_vecs[2]);
vec3 hit_point = ray_origin + ray_dir * result.dist;

const int dim_size = 10;
int hits = 0, total = (dim_size + 1) * (dim_size + 1);

for(int x = 0; x <= dim_size; x++)
for(int y = 0; y <= dim_size; y++) {
vec2 pos = vec2(x, y) * (1.0 / dim_size);
vec3 hemi = uniformSampleHemisphere(pos);
vec3 dir = tri_vecs[0] * hemi[0] + tri_vecs[1] * hemi[2] + tri_vecs[2] * hemi[1];
vec3 origin = hit_point + dir * 0.00001;
TraceResult ao_hit = rayTraceAS(origin, dir);
if(ao_hit.dist > 0.0001 && ao_hit.dist < 0.5 && ao_hit.tri_id != result.tri_id)
hits++;
}

ao_value = (total - hits) / float(total);
ao_value = computeAO(result.tri_id, hit_point);
}
#endif

vec3 vcolor = vec3(0.0);
if(result.dist < MAX_ISECT_DIST) {
Expand Down
2 changes: 2 additions & 0 deletions src/lucid_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ Ex<void> LucidApp::updateRenderer() {
insertBack(used_shaders, m_pbr_renderer->shaderDefIds());
if(m_lucid_renderer)
insertBack(used_shaders, m_lucid_renderer->shaderDefIds());
if(m_path_tracer)
insertBack(used_shaders, m_path_tracer->shaderDefIds());
makeSortedUnique(used_shaders);
if(setIntersection(used_shaders, update_list))
do_update = true;
Expand Down
3 changes: 2 additions & 1 deletion src/path_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Ex<> PathTracer::updateScene(VulkanDevice &device, Scene &scene) {
EX_PASS(VulkanAccelStruct::buildBottom(device, scene.verts.positions, scene.tris_ib));
VAccelStructInstance instance{blas, Matrix4::identity()};
m_accel_struct = EX_PASS(VulkanAccelStruct::buildTop(device, {instance}));
m_indices = scene.tris_ib;
m_vertices = scene.verts.positions;
// TODO: wait until AS is built?

Expand Down Expand Up @@ -151,7 +152,7 @@ void PathTracer::render(const Context &ctx) {
auto raster_image = swap_chain->acquiredImage();
ds.setStorageImage(2, raster_image, VImageLayout::general);

ds.set(5, m_vertices);
ds.set(4, m_indices, m_vertices);
ds.set(6, m_accel_struct);

auto sampler = ctx.device.getSampler(ctx.config.sampler_setup);
Expand Down
1 change: 1 addition & 0 deletions src/path_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class PathTracer {
VBufferSpan<shader::PathTracerConfig> m_frame_config[num_frames];
VBufferSpan<u32> m_debug_buffer;

VBufferSpan<u32> m_indices;
VBufferSpan<float3> m_vertices;
PVAccelStruct m_accel_struct;

Expand Down

0 comments on commit f8f8d3b

Please sign in to comment.