Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: started working on bidir pathtracer #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 56 additions & 11 deletions src/shaders/comp.comp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#extension GL_EXT_nonuniform_qualifier : require


#extension GL_EXT_ray_query : require
#define M_PI 3.14159265f

Expand Down Expand Up @@ -53,7 +52,8 @@ struct raytraceInfo
vec4 normal;
vec4 position;
};
layout(set =0, binding = 9, std140) writeonly buffer info {
layout(set = 0, binding = 9, std140) writeonly buffer info
{
raytraceInfo infos[];
};
// #define SHOW_DEPTH
Expand All @@ -74,14 +74,18 @@ void main()
const float window_height = (ubo.height) / int(ubo.scale);
const uint x = gl_GlobalInvocationID.x;
const uint y = gl_GlobalInvocationID.y;
const vec2 cc = vec2(x / window_width , y / window_height);
const vec2 cc = vec2(x / window_width, y / window_height);
random_init(vec2(gl_WorkGroupID.x, gl_WorkGroupID.y));
camera cam = camera_init(ubo.camera_pos.xyz, vec3(ubo.camera_target.xyz), vec3(0, 1, 0), 0, 9.3, window_width / window_height);

#if 0
vec2 rpos = vec2((x) / window_width, 1 - ((y - 1) / window_height));
ExportRecord er;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
int count = 0;



for(int cu = 0; cu < 1; cu++)
{
float u = ((float(x) + random()) / (window_width - 1));
Expand All @@ -108,12 +112,53 @@ void main()
// memoryBarrierImage();
prev = imageLoad(buf, ivec2(x, y));
}
// if(ubo.t <= 1)
// {
// int pos = int(x) + int(y * ubo.width);
// infos[pos].albedo = vec4(er.albedo, 0.0);
// infos[pos].normal = vec4(er.normal, 0.0);
// infos[pos].position = vec4(er.pos, 0.0);
// }
imageStore(buf, ivec2(x, y), ((prev * (ubo.t)) + (color)) / ((ubo.t + count)));

imageStore(buf, ivec2(x, y), vec4(color.xyz / float(count), 1) + prev);
#else

vec4 prev = vec4(0);
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);

Ray r = random_light_ray(color);
vec3 point = vec3(0);
// bool hitted = light_to_camera(color, cam, r, ubo.bounce_limit, point);
float u = ((float(x) + random()) / (window_width - 1));
float v = ((float(y) + random()) / (window_height - 1));

Ray cr = camera_ray(cam, u, v);
cr.inv_direction = vec3(1) / r.direction;

// vec4 prev = color;;
// color += (ray_color(r, u, v, ubo.bounce_limit, er));

bool hitted = bidir_raytrace(color, cam, cr, r, ubo.bounce_limit, point);
vec2 rpos = pos_to_screen_coordinate(cam, point);

if (!hitted)
{

if (ubo.t < 1)
{
imageStore(buf, ivec2(x, y), vec4(0, 0, 0, 0));
}
return;
}
// if(ubo.t <= 1)
// {
// int pos = int(x) + int(y * ubo.width);
// infos[pos].albedo = vec4(er.albedo, 0.0);
// infos[pos].normal = vec4(er.normal, 0.0);
// infos[pos].position = vec4(er.pos, 0.0);
// }
if (ubo.t > float(0))
{

// memoryBarrierImage();
prev = imageLoad(buf, ivec2(x, y));
}
int count = 1;
imageStore(buf, ivec2(x, y), vec4(color.xyz, 1) + prev);

// imageStore(buf, ivec2(x,y), vec4(1, 0, 0, 1) );
#endif
}
1 change: 1 addition & 0 deletions src/shaders/hittable/brdf.comp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct MaterialQueryRecord
vec3 H;
bool is_specular;
float pdf;
float effective_ior;
float scatter_pdf;
vec3 attenuation;
};
Expand Down
92 changes: 83 additions & 9 deletions src/shaders/hittable/material.comp
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#ifndef MATERIAL_COMP
#define MATERIAL_COMP
#include "hittable/hittable.comp"
#include "hittable/brdf.comp"
#include "hittable/hittable.comp"
#include "hittable/material.comp"


#include "hittable/sampling.comp"
#include "hittable/textures.comp"


#include "utils/ray.comp"
#include "layouts.comp"
#include "hittable/sampling.comp"


#include "utils/color.comp"
#include "utils/ray.comp"

MaterialRecord material_hit_info(HittableRecord record, Ray r)
{
Expand Down Expand Up @@ -178,6 +173,7 @@ MaterialRecord material_hit_info(HittableRecord record, Ray r)

// const float selected_wave_length = mix(390, 710, random());

#ifdef USE_RANDOM_WAVELENGTH
float selected_wave_length = 0;
float c = random();
float cc = random();
Expand All @@ -199,7 +195,10 @@ MaterialRecord material_hit_info(HittableRecord record, Ray r)

// Borosilicate glass BK
const float idx = 1.8 + transmission_refractive_coeff / (selected_wave_length * selected_wave_length * 0.001 * 0.001);
#else

const float idx = 1.8;
#endif
float refract_ratio = 1.0 / idx;

result.ior = refract_ratio;
Expand Down Expand Up @@ -258,7 +257,11 @@ MaterialRecord material_hit_info(HittableRecord record, Ray r)

if (result.is_transmissive)
{
#if USE_RANDOM_WAVELENGTH
result.albedo = result.albedo * 3 * wavelength_to_color(selected_wave_length);
#else
result.albedo = result.albedo;
#endif
// result.albedo = vec3(1);
}

Expand Down Expand Up @@ -302,6 +305,7 @@ MaterialQueryRecord material_query(MaterialRecord record, Ray r)
result.H = result.scatter.direction;
return result;
}
float effective_ior = 1;
if (record.is_specular) // specular
{
is_specular = true;
Expand Down Expand Up @@ -340,6 +344,8 @@ MaterialQueryRecord material_query(MaterialRecord record, Ray r)
{
dir = -dir;
}

effective_ior *= record.ior;
// dir = unit_direction;

// H = normalize(record.N);
Expand Down Expand Up @@ -374,10 +380,78 @@ MaterialQueryRecord material_query(MaterialRecord record, Ray r)
result.is_specular = is_specular;
result.pdf = 1;
result.H = H;
result.effective_ior = record.ior;

return result;
}
MaterialQueryRecord material_query_camera(MaterialRecord record, Ray r, PdfSampleRecord forced_record, out bool look_cam, float prob)
{
MaterialQueryRecord result;
vec3 dir;
look_cam = false;
if (record.transparent)
{
result.scatter.direction = r.direction;
result.scatter.inv_direction = 1 / r.direction;
result.scatter.origin = record.point;
result.pdf = 1;
result.scatter_pdf = 1;
result.H = result.scatter.direction;
return result;
}
if (record.do_scatter && !record.is_specular && random() > prob && !record.is_transmissive)
{

int maxv = 0;

// PdfSampleRecord pdf_sample = sample_point(record.N, record.point);
dir = -(record.point - forced_record.random_point);
vec3 ndir = normalize(dir);

if (dot(forced_record.light_normal, ndir) >= 0.00001 || dot(ndir, record.N) <= 0.00001)

{

result = material_query(record, r);
result.pdf = 1;

result.scatter_pdf = 1;

// result.emitted = vec3(0, 1, 0);
// result.do_scatter = false;
// return result;
}
else
{
look_cam = true;
float ldir = length(dir);

result.pdf = (ldir * ldir) / abs(dot(forced_record.light_normal, ndir));
// dir = normalize(dir);

result.scatter.direction = ndir;
result.scatter.origin = record.point;
result.scatter.inv_direction = 1.0 / ndir;
// result.is_specular = false;
// result.pdf = pdf_sample.pdf;
result.scatter_pdf = 1;
// result.emitted = vec3(1, 0, 0);
// result.do_scatter = false;
// return result;
}
}
else
{

result = material_query(record, r);

result.pdf = 1;

result.scatter_pdf = 1;
}

return result;
}
MaterialQueryRecord material_query_pdf(MaterialRecord record, Ray r, float prob)
{
MaterialQueryRecord result;
Expand Down
Loading