Skip to content

Commit b91c89f

Browse files
committed
Implemented implicit backface culling of triangle meshes.
This implementation utilises the order of the vertices of each triangle to determine whether the side facing the direction of the ray is the front or back side. The front side is defined as the side where all vertices of the triangle are defined in clockwise order. The implementation makes use of one of the two integer padding fields of the ray data structure to declare whether the ray needs to cull backfaces or not. Thus to enable backface culling for a ray, the new member variable "int doBackfaceCulling;" needs to be set to true/1 either manually or by using the new method "void SetDoBackfaceCulling(bool _bDoBackfaceCulling)". If backface culling is disabled via the CMake config switch (RR_ENABLE_BACKFACE_CULL; off by default) all backface culling checks in the intersection kernels are disabled at compile time to make sure that there is no performance impact when not using the feature.
1 parent 7672d16 commit b91c89f

21 files changed

+308
-580
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ option(RR_NO_TESTS "Don't add any unit tests and remove any test functionality f
1111
option(RR_ENABLE_STATIC "Create static libraries rather than dynamic" OFF)
1212
option(RR_SHARED_CALC "Link Calc(compute abstraction layer) dynamically" OFF)
1313
option(RR_ENABLE_RAYMASK "Enable ray masking in intersection kernels" OFF)
14+
option(RR_ENABLE_BACKFACE_CULL "Enable backface culling in intersection kernels" OFF)
1415
#option(RR_TUTORIALS "Add tutorials projects" OFF)
1516
option(RR_SAFE_MATH "use safe math" OFF)
1617
mark_as_advanced(FORCE RR_USE_VULKAN)

RadeonRays/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ if (RR_ENABLE_RAYMASK)
195195
target_compile_definitions(RadeonRays PRIVATE RR_RAY_MASK)
196196
endif (RR_ENABLE_RAYMASK)
197197

198+
if (RR_ENABLE_BACKFACE_CULL)
199+
target_compile_definitions(RadeonRays PRIVATE RR_BACKFACE_CULL)
200+
endif (RR_ENABLE_BACKFACE_CULL)
201+
198202
if (RR_USE_OPENCL)
199203
target_link_libraries(RadeonRays PUBLIC OpenCL::OpenCL)
200204
target_compile_definitions(RadeonRays PUBLIC USE_OPENCL=1)

RadeonRays/include/math/ray.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace RadeonRays
4242
SetTime(time);
4343
SetMask(-1);
4444
SetActive(true);
45+
SetDoBackfaceCulling(false);
4546
}
4647

4748
float3 operator ()(float t) const
@@ -89,9 +90,20 @@ namespace RadeonRays
8990
return extra.y > 0;
9091
}
9192

93+
void SetDoBackfaceCulling(bool _bDoBackfaceCulling)
94+
{
95+
doBackfaceCulling = _bDoBackfaceCulling ? 1 : 0;
96+
}
97+
98+
bool GetDoBackfaceCulling() const
99+
{
100+
return doBackfaceCulling > 0;
101+
}
102+
92103
float4 o;
93104
float4 d;
94105
int2 extra;
95-
int2 padding;
106+
int doBackfaceCulling;
107+
int padding;
96108
};
97109
}

RadeonRays/src/intersector/intersector_2level.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ namespace RadeonRays
127127
, m_gpudata(new GpuData(device))
128128
, m_cpudata(new CpuData)
129129
{
130-
std::string buildopts =
130+
std::string buildopts;
131131
#ifdef RR_RAY_MASK
132-
"-D RR_RAY_MASK ";
133-
#else
134-
"";
132+
buildopts.append("-D RR_RAY_MASK ");
135133
#endif
136-
134+
135+
#ifdef RR_BACKFACE_CULL
136+
buildopts.append("-D RR_BACKFACE_CULL ");
137+
#endif // RR_BACKFACE_CULL
138+
137139
#ifdef USE_SAFE_MATH
138140
buildopts.append("-D USE_SAFE_MATH ");
139141
#endif

RadeonRays/src/intersector/intersector_bittrail.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ namespace RadeonRays
8888
, m_gpudata(new GpuData(device))
8989
, m_bvh(nullptr)
9090
{
91-
std::string buildopts =
91+
std::string buildopts;
9292
#ifdef RR_RAY_MASK
93-
"-D RR_RAY_MASK ";
94-
#else
95-
"";
93+
buildopts.append("-D RR_RAY_MASK ");
9694
#endif
97-
95+
96+
#ifdef RR_BACKFACE_CULL
97+
buildopts.append("-D RR_BACKFACE_CULL ");
98+
#endif // RR_BACKFACE_CULL
99+
98100
#ifdef USE_SAFE_MATH
99101
buildopts.append("-D USE_SAFE_MATH ");
100102
#endif

RadeonRays/src/intersector/intersector_hlbvh.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ namespace RadeonRays
7777
, m_gpudata(new GpuData(device))
7878
, m_bvh(nullptr)
7979
{
80-
std::string buildopts =
80+
std::string buildopts;
8181
#ifdef RR_RAY_MASK
82-
"-D RR_RAY_MASK ";
83-
#else
84-
"";
82+
buildopts.append("-D RR_RAY_MASK ");
8583
#endif
86-
84+
85+
#ifdef RR_BACKFACE_CULL
86+
buildopts.append("-D RR_BACKFACE_CULL ");
87+
#endif // RR_BACKFACE_CULL
88+
8789
#ifdef USE_SAFE_MATH
8890
buildopts.append("-D USE_SAFE_MATH ");
8991
#endif

RadeonRays/src/intersector/intersector_lds.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ namespace RadeonRays
100100
#ifdef RR_RAY_MASK
101101
buildopts.append("-D RR_RAY_MASK ");
102102
#endif
103+
104+
#ifdef RR_BACKFACE_CULL
105+
buildopts.append("-D RR_BACKFACE_CULL ");
106+
#endif // RR_BACKFACE_CULL
107+
103108
#ifdef USE_SAFE_MATH
104109
buildopts.append("-D USE_SAFE_MATH ");
105110
#endif

RadeonRays/src/intersector/intersector_short_stack.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ namespace RadeonRays
8383
, m_gpudata(new GpuData(device))
8484
, m_bvh(nullptr)
8585
{
86-
std::string buildopts =
86+
std::string buildopts;
8787
#ifdef RR_RAY_MASK
88-
"-D RR_RAY_MASK ";
89-
#else
90-
"";
88+
buildopts.append("-D RR_RAY_MASK ");
9189
#endif
92-
90+
91+
#ifdef RR_BACKFACE_CULL
92+
buildopts.append("-D RR_BACKFACE_CULL ");
93+
#endif // RR_BACKFACE_CULL
94+
9395
#ifdef USE_SAFE_MATH
9496
buildopts.append("-D USE_SAFE_MATH ");
9597
#endif

RadeonRays/src/intersector/intersector_skip_links.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@ namespace RadeonRays
8181
, m_gpudata(new GpuData(device))
8282
, m_bvh(nullptr)
8383
{
84-
std::string buildopts =
84+
std::string buildopts;
8585
#ifdef RR_RAY_MASK
86-
"-D RR_RAY_MASK ";
87-
#else
88-
"";
86+
buildopts.append("-D RR_RAY_MASK ");
8987
#endif
9088

89+
#ifdef RR_BACKFACE_CULL
90+
buildopts.append("-D RR_BACKFACE_CULL ");
91+
#endif // RR_BACKFACE_CULL
92+
9193
#ifdef USE_SAFE_MATH
9294
buildopts.append("-D USE_SAFE_MATH ");
9395
#endif

RadeonRays/src/kernels/CL/common.cl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ typedef struct
5555
float4 o;
5656
float4 d;
5757
int2 extra;
58-
int2 padding;
58+
int doBackfaceCulling;
59+
int padding;
5960
} ray;
6061

6162
// Intersection definition
@@ -96,6 +97,12 @@ float ray_get_time(ray const* r)
9697
return r->d.w;
9798
}
9899

100+
INLINE
101+
int ray_get_doBackfaceCull(ray const* r)
102+
{
103+
return r->doBackfaceCulling;
104+
}
105+
99106
/*************************************************************************
100107
FUNCTIONS
101108
**************************************************************************/
@@ -172,6 +179,14 @@ float fast_intersect_triangle(ray r, float3 v1, float3 v2, float3 v3, float t_ma
172179
{
173180
float3 const e1 = v2 - v1;
174181
float3 const e2 = v3 - v1;
182+
183+
#ifdef RR_BACKFACE_CULL
184+
if (ray_get_doBackfaceCull(&r) && dot(cross(e1, e2), r.d.xyz) > 0.f)
185+
{
186+
return t_max;
187+
}
188+
#endif // RR_BACKFACE_CULL
189+
175190
float3 const s1 = cross(r.d.xyz, e2);
176191

177192
float denom = dot(s1, e1);

0 commit comments

Comments
 (0)