Skip to content

Commit af378fb

Browse files
committed
Stratified sampling. Fix type casts and embree namespace
1 parent d208d25 commit af378fb

File tree

7 files changed

+27
-13
lines changed

7 files changed

+27
-13
lines changed

src/bvh/bvh_embree.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
# include "util/util_logging.h"
5454
# include "util/util_progress.h"
5555
# include "util/util_stats.h"
56+
# include "util/util_hash.h"
5657

5758
CCL_NAMESPACE_BEGIN
5859

@@ -256,10 +257,23 @@ static void rtc_filter_occluded_func_thick_curve(const RTC_NAMESPACE::RTCFilterF
256257
rtc_filter_occluded_func(args);
257258
}
258259

260+
/* Generate a pseudo random bit sequence trying to keep disjoint spans uncorrelated.
261+
https://research.nvidia.com/publication/stratified-sampling-stochastic-transparency
262+
*/
263+
inline float get_opacity_stratified(uint rng_pixel_sample, uint rng_surface) {
264+
// pixel_id + sample * num_samples
265+
uint x = reverse_bits_32(rng_pixel_sample) + rng_surface;
266+
x = x ^ (x * 0x6C50B47Cu);
267+
x = x ^ (x * 0xB82F1E52u);
268+
x = x ^ (x * 0xC7AFE638u);
269+
x = x ^ (x * 0x8D22F6E6u);
270+
return (float)(x * 0x1p-32);
271+
}
272+
259273
static void rtc_filter_func_transparent_points(const RTC_NAMESPACE::RTCFilterFunctionNArguments *args)
260274
{
261-
const RTC_NAMESPACE::RTCRay *ray = (RTCRay *)args->ray;
262-
RTC_NAMESPACE::RTCHit *hit = (RTCHit *)args->hit;
275+
const RTC_NAMESPACE::RTCRay *ray = (RTC_NAMESPACE::RTCRay *)args->ray;
276+
RTC_NAMESPACE::RTCHit *hit = (RTC_NAMESPACE::RTCHit *)args->hit;
263277
CCLIntersectContext *ctx = ((IntersectContext *)args->context)->userRayExt;
264278
KernelGlobals *kg = ctx->kg;
265279

@@ -275,8 +289,7 @@ static void rtc_filter_func_transparent_points(const RTC_NAMESPACE::RTCFilterFun
275289
const float opacity = kernel_tex_fetch(__points_opacity, opacity_offset);
276290

277291
/* Unique random number per ray per geometry */
278-
uint hash = (uint&)ctx->ps_rng_transparent;
279-
const float rand_opacity = cmj_randfloat(hash, cmj_hash(object, prim));
292+
const float rand_opacity = cmj_randfloat(ctx->rng_transparent, cmj_hash(object, prim));
280293

281294
if (rand_opacity > opacity) {
282295
*args->valid = 0;

src/kernel/bvh/bvh.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ ccl_device_intersect bool scene_intersect(KernelGlobals *kg,
155155
const Ray *ray,
156156
const uint visibility,
157157
Intersection *isect,
158-
float ps_rng = 0.0f)
158+
uint rng_transparent)
159159
{
160160
PROFILING_INIT(kg, PROFILING_INTERSECT);
161161

@@ -203,7 +203,7 @@ ccl_device_intersect bool scene_intersect(KernelGlobals *kg,
203203
isect->t = ray->t;
204204
CCLIntersectContext ctx(kg, CCLIntersectContext::RAY_REGULAR);
205205
IntersectContext rtc_ctx(&ctx);
206-
ctx.ps_rng_transparent = ps_rng;
206+
ctx.rng_transparent = rng_transparent;
207207
RTC_NAMESPACE::RTCRayHit ray_hit;
208208
kernel_embree_setup_rayhit(*ray, ray_hit, visibility);
209209
rtcIntersect1(kernel_data.bvh.scene, &rtc_ctx.context, &ray_hit);

src/kernel/bvh/bvh_embree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct CCLIntersectContext {
5050
uint *lcg_state;
5151

5252
/* for transparent rays */
53-
float ps_rng_transparent;
53+
uint rng_transparent;
5454

5555
CCLIntersectContext(KernelGlobals *kg_, RayType type_)
5656
{
@@ -62,7 +62,7 @@ struct CCLIntersectContext {
6262
local_isect = NULL;
6363
local_object_id = -1;
6464
lcg_state = NULL;
65-
ps_rng_transparent = 0;
65+
rng_transparent = 0;
6666
}
6767
};
6868

src/kernel/geom/geom_point_intersect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ccl_device_forceinline bool point_intersect_test_disc(const float4 point,
7878
}
7979

8080
/* Self-intersection */
81-
const float avoidance_factor = 2.0 * radius * rd;
81+
const float avoidance_factor = 2.0f * radius * rd;
8282
if (projC0 < avoidance_factor) {
8383
return false;
8484
}

src/kernel/kernel_path.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ ccl_device_forceinline bool kernel_path_scene_intersect(KernelGlobals *kg,
7070
}
7171

7272
// const float rand_transparency = path_state_rng_1D_hash(kg, state, state->rng_hash);
73-
const float rand_transparency = path_state_rng_1D(kg, state, PRNG_UNUSED_0);
74-
bool hit = scene_intersect(kg, ray, visibility, isect, rand_transparency);
73+
const float rng_transparent = path_state_rng_1D(kg, state, PRNG_UNUSED_0);
74+
// const uint rng_transparent = state->sample + state->rng_hash * kernel_data.integrator.aa_samples;
75+
bool hit = scene_intersect(kg, ray, visibility, isect, (uint&)rng_transparent);
7576

7677
#ifdef __KERNEL_DEBUG__
7778
if (state->flag & PATH_RAY_CAMERA) {

src/kernel/kernel_shadow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ ccl_device bool shadow_blocked_opaque(KernelGlobals *kg,
9595
Intersection *isect,
9696
float3 *shadow)
9797
{
98-
const bool blocked = scene_intersect(kg, ray, visibility & PATH_RAY_SHADOW_OPAQUE, isect);
98+
const bool blocked = scene_intersect(kg, ray, visibility & PATH_RAY_SHADOW_OPAQUE, isect, 0);
9999
#ifdef __VOLUME__
100100
if (!blocked && state->volume_stack[0].shader != SHADER_NONE) {
101101
/* Apply attenuation from current volume shader. */

src/kernel/svm/svm_ao.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ ccl_device_noinline float svm_ao(KernelGlobals *kg,
7272
}
7373
else {
7474
Intersection isect;
75-
if (!scene_intersect(kg, &ray, PATH_RAY_SHADOW_OPAQUE, &isect)) {
75+
if (!scene_intersect(kg, &ray, PATH_RAY_SHADOW_OPAQUE, &isect, 0)) {
7676
unoccluded++;
7777
}
7878
}

0 commit comments

Comments
 (0)