Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Enigmatisms/AdaPT
Browse files Browse the repository at this point in the history
  • Loading branch information
Enigmatisms committed Apr 7, 2024
2 parents 8157ca3 + ff715ff commit 43f1078
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,8 @@
"valarray": "cpp",
"variant": "cpp",
"bit": "cpp"
},
"python.analysis.diagnosticSeverityOverrides": {
"reportInvalidTypeForm": "none"
}
}
93 changes: 93 additions & 0 deletions renderer/ssao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
""" Screen-Space Ambient Occlusion (SSAO)
@author: Qianyue He
@date: 2024-4-7
"""
import taichi as ti
from taichi.math import vec3

from typing import List
from la.cam_transform import *
from tracer.path_tracer import PathTracer
from emitters.abtract_source import LightSource
from renderer.constants import ZERO_V3

from parsers.obj_desc import ObjDescriptor
from sampler.general_sampling import uniform_hemisphere

from rich.console import Console
CONSOLE = Console(width = 128)

@ti.data_oriented
class SSAORenderer(PathTracer):
"""
Renderer Final Class
"""
def __init__(self,
emitters: List[LightSource], array_info: dict,
objects: List[ObjDescriptor], prop: dict
):
super().__init__(emitters, array_info, objects, prop)
self.smp_hemisphere = prop.get('smp_hemisphere', 32)
self.depth_samples = prop.get('depth_samples', 64)
self.sample_extent = prop.get('sample_extent', 0.1) # float
CONSOLE.log(f"Rendering depth map: {self.depth_samples} sample(s) per pixel.")
self.get_depth_map()
CONSOLE.log(f"Depth map rendering completed.")

@ti.kernel
def get_depth_map(self):
ti.loop_config(parallelize = 8, block_dim = 512)
# first get
for i, j in self.pixels:
in_crop_range = i >= self.start_x and i < self.end_x and j >= self.start_y and j < self.end_y
if not self.do_crop or in_crop_range:
num_valid_hits = 0
for _ in range(self.depth_samples):
ray_d = self.pix2ray(i, j)
ray_o = self.cam_t
it = self.ray_intersect(ray_d, ray_o)
if it.is_ray_not_hit(): continue
num_valid_hits += 1
self.color[i, j][2] += it.min_depth

if num_valid_hits:
self.color[i, j][2] /= num_valid_hits

@ti.func
def get_sample_depth(self, it: ti.template(), pos: vec3):
""" Get samples around normal
and return the screen space depth
"""
local_dir = uniform_hemisphere()
normal_sample, _ = delocalize_rotate(it.n_s, local_dir)
position = pos + normal_sample * self.sample_extent
depth = (position - self.cam_t).norm()
return depth

@ti.kernel
def render(self, _t_start: int, _t_end: int, _s_start: int, _s_end: int, _a: int, _b: int):
self.cnt[None] += 1
ti.loop_config(parallelize = 8, block_dim = 512)

for i, j in self.pixels:
in_crop_range = i >= self.start_x and i < self.end_x and j >= self.start_y and j < self.end_y
if not self.do_crop or in_crop_range:
min_depth = self.color[i, j][2] + 1e-5
ray_d = self.pix2ray(i, j)
ray_o = self.cam_t
it = self.ray_intersect(ray_d, ray_o)
if it.is_ray_not_hit(): break
# AO sampling: the hemisphere
pos = ray_o + ray_d * it.min_depth
num_un_occluded = 0.0
for _ in range(self.smp_hemisphere):
depth = self.get_sample_depth(it, pos)
num_un_occluded += float(depth < min_depth) # depth
self.color[i, j][0] += num_un_occluded / self.smp_hemisphere
color_vec = ZERO_V3
color_vec.fill(self.color[i, j][2] / self.cnt[None])
self.pixels[i, j] = color_vec

def summary(self):
super().summary()
CONSOLE.print(f"SSAO SPP = {self.cnt[None]}. Rendering time: {self.clock.toc():.3f} s", justify="center")
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
taichi>=1.4.0
taichi==1.6.0
numpy>=1.23.0
scipy>=1.10.0
matplotlib>=3.6.0
Expand Down
5 changes: 2 additions & 3 deletions tracer/bvh/bvh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void create_bvh_info(const std::vector<Eigen::Matrix3f>& meshes, const std::vect

int recursive_bvh_SAH(BVHNode* const cur_node, std::vector<BVHInfo>& bvh_infos) {
AABB fwd_bound, bwd_bound;
int seg_idx = 0, child_prim_cnt = 0; // this index is used for indexing variable `bins`
int child_prim_cnt = 0; // this index is used for indexing variable `bins`
const int prim_num = cur_node->prim_num, base = cur_node->base, max_pos = base + prim_num;
float min_cost = 5e9, node_prim_cnt = float(prim_num), node_inv_area = 1. / cur_node->bound.area();

Expand Down Expand Up @@ -129,7 +129,6 @@ int recursive_bvh_SAH(BVHNode* const cur_node, std::vector<BVHInfo>& bvh_infos)
return bvh.centroid[dim] < pivot;
});
child_prim_cnt = prim_cnts[seg_bin_idx];
seg_idx = base + child_prim_cnt; // bvh[seg_idx] will be in rchild
}
fwd_bound.clear();
bwd_bound.clear();
Expand All @@ -138,7 +137,7 @@ int recursive_bvh_SAH(BVHNode* const cur_node, std::vector<BVHInfo>& bvh_infos)
for (int i = num_bins - 1; i > seg_bin_idx; i--)
bwd_bound += idx_bins[i].bound;
} else { // equal primitive number
seg_idx = (base + max_pos) >> 1;
int seg_idx = (base + max_pos) >> 1;
// Step 5: reordering the BVH info in the vector to make the segment contiguous (keep around half of the bvh in lchild)
std::nth_element(bvh_infos.begin() + base, bvh_infos.begin() + seg_idx, bvh_infos.begin() + max_pos,
[dim = max_axis] (const BVHInfo& bvh1, const BVHInfo& bvh2) {
Expand Down
2 changes: 1 addition & 1 deletion tracer/bvh/bvh_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct AABB {
}
/** Review needed: is this good to implement two overload? */
AABB(const Eigen::Vector3f& mini, const Eigen::Vector3f& maxi): mini(mini), maxi(maxi) {}
AABB(Eigen::Vector3f&& mini, Eigen::Vector3f&& maxi): mini(mini), maxi(maxi) {}
AABB(Eigen::Vector3f&& mini, Eigen::Vector3f&& maxi): mini(std::move(mini)), maxi(std::move(maxi)) {}
AABB(const Eigen::Matrix3f& primitive, bool is_sphere = false) {
if (is_sphere) {
mini = primitive.col(0) - primitive.col(1);
Expand Down

0 comments on commit 43f1078

Please sign in to comment.