deduplicate rayquery code
This commit is contained in:
parent
eab97f826d
commit
f0efface7b
3 changed files with 8 additions and 169 deletions
|
|
@ -6,7 +6,7 @@ struct TraceResult
|
|||
int primitiveIndex;
|
||||
};
|
||||
|
||||
#if defined(USE_RAYQUERY)
|
||||
#if defined(USE_RAYQUERY) || defined(SUPPORTS_RAYQUERY)
|
||||
|
||||
TraceResult TraceFirstHit(vec3 origin, float tmin, vec3 dir, float tmax)
|
||||
{
|
||||
|
|
@ -43,7 +43,6 @@ TraceResult TraceFirstHit(vec3 origin, float tmin, vec3 dir, float tmax)
|
|||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
bool TraceAnyHit(vec3 origin, float tmin, vec3 dir, float tmax)
|
||||
{
|
||||
rayQueryEXT rayQuery;
|
||||
|
|
@ -51,7 +50,6 @@ bool TraceAnyHit(vec3 origin, float tmin, vec3 dir, float tmax)
|
|||
while(rayQueryProceedEXT(rayQuery)) { }
|
||||
return rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT;
|
||||
}
|
||||
*/
|
||||
|
||||
#else
|
||||
|
||||
|
|
@ -164,7 +162,6 @@ bool is_leaf(int node_index)
|
|||
return nodes[node_index].element_index != -1;
|
||||
}
|
||||
|
||||
/*
|
||||
bool TraceAnyHit(vec3 origin, float tmin, vec3 dir, float tmax)
|
||||
{
|
||||
if (tmax <= 0.0f)
|
||||
|
|
@ -199,7 +196,6 @@ bool TraceAnyHit(vec3 origin, float tmin, vec3 dir, float tmax)
|
|||
} while (stackIndex > 0);
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
struct TraceHit
|
||||
{
|
||||
|
|
@ -281,3 +277,8 @@ TraceResult TraceFirstHit(vec3 origin, float tmin, vec3 dir, float tmax)
|
|||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool traceHit(vec3 origin, vec3 direction, float dist)
|
||||
{
|
||||
return TraceAnyHit(origin, 0.01f, direction, dist);
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ layout(set = 0, binding = 4) uniform samplerCubeArray PrefilterMap;
|
|||
|
||||
#if defined(USE_RAYTRACE)
|
||||
#if defined(SUPPORTS_RAYQUERY)
|
||||
layout(set = 0, binding = 5) uniform accelerationStructureEXT TopLevelAS;
|
||||
layout(set = 0, binding = 5) uniform accelerationStructureEXT acc;
|
||||
#else
|
||||
struct CollisionNode
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,169 +3,7 @@
|
|||
|
||||
#if defined(USE_RAYTRACE)
|
||||
|
||||
#if defined(SUPPORTS_RAYQUERY)
|
||||
|
||||
bool traceHit(vec3 origin, vec3 direction, float dist)
|
||||
{
|
||||
rayQueryEXT rayQuery;
|
||||
rayQueryInitializeEXT(rayQuery, TopLevelAS, gl_RayFlagsTerminateOnFirstHitEXT, 0xFF, origin, 0.01f, direction, dist);
|
||||
while(rayQueryProceedEXT(rayQuery)) { }
|
||||
return rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
struct RayBBox
|
||||
{
|
||||
vec3 start, end;
|
||||
vec3 c, w, v;
|
||||
};
|
||||
|
||||
RayBBox create_ray(vec3 ray_start, vec3 ray_end)
|
||||
{
|
||||
RayBBox ray;
|
||||
ray.start = ray_start;
|
||||
ray.end = ray_end;
|
||||
ray.c = (ray_start + ray_end) * 0.5;
|
||||
ray.w = ray_end - ray.c;
|
||||
ray.v = abs(ray.w);
|
||||
return ray;
|
||||
}
|
||||
|
||||
bool overlap_bv_ray(RayBBox ray, int a)
|
||||
{
|
||||
vec3 v = ray.v;
|
||||
vec3 w = ray.w;
|
||||
vec3 h = nodes[a].extents;
|
||||
vec3 c = ray.c - nodes[a].center;
|
||||
|
||||
if (abs(c.x) > v.x + h.x ||
|
||||
abs(c.y) > v.y + h.y ||
|
||||
abs(c.z) > v.z + h.z)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (abs(c.y * w.z - c.z * w.y) > h.y * v.z + h.z * v.y ||
|
||||
abs(c.x * w.z - c.z * w.x) > h.x * v.z + h.z * v.x ||
|
||||
abs(c.x * w.y - c.y * w.x) > h.x * v.y + h.y * v.x)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#define FLT_EPSILON 1.192092896e-07F // smallest such that 1.0+FLT_EPSILON != 1.0
|
||||
|
||||
float intersect_triangle_ray(RayBBox ray, int a, out float barycentricB, out float barycentricC)
|
||||
{
|
||||
int start_element = nodes[a].element_index;
|
||||
|
||||
vec3 p[3];
|
||||
p[0] = vertices[elements[start_element]].pos.xyz;
|
||||
p[1] = vertices[elements[start_element + 1]].pos.xyz;
|
||||
p[2] = vertices[elements[start_element + 2]].pos.xyz;
|
||||
|
||||
// Moeller-Trumbore ray-triangle intersection algorithm:
|
||||
|
||||
vec3 D = ray.end - ray.start;
|
||||
|
||||
// Find vectors for two edges sharing p[0]
|
||||
vec3 e1 = p[1] - p[0];
|
||||
vec3 e2 = p[2] - p[0];
|
||||
|
||||
// Begin calculating determinant - also used to calculate u parameter
|
||||
vec3 P = cross(D, e2);
|
||||
float det = dot(e1, P);
|
||||
|
||||
// Backface check
|
||||
//if (det < 0.0f)
|
||||
// return 1.0f;
|
||||
|
||||
// If determinant is near zero, ray lies in plane of triangle
|
||||
if (det > -FLT_EPSILON && det < FLT_EPSILON)
|
||||
return 1.0f;
|
||||
|
||||
float inv_det = 1.0f / det;
|
||||
|
||||
// Calculate distance from p[0] to ray origin
|
||||
vec3 T = ray.start - p[0];
|
||||
|
||||
// Calculate u parameter and test bound
|
||||
float u = dot(T, P) * inv_det;
|
||||
|
||||
// Check if the intersection lies outside of the triangle
|
||||
if (u < 0.f || u > 1.f)
|
||||
return 1.0f;
|
||||
|
||||
// Prepare to test v parameter
|
||||
vec3 Q = cross(T, e1);
|
||||
|
||||
// Calculate V parameter and test bound
|
||||
float v = dot(D, Q) * inv_det;
|
||||
|
||||
// The intersection lies outside of the triangle
|
||||
if (v < 0.f || u + v > 1.f)
|
||||
return 1.0f;
|
||||
|
||||
float t = dot(e2, Q) * inv_det;
|
||||
if (t <= FLT_EPSILON)
|
||||
return 1.0f;
|
||||
|
||||
// Return hit location on triangle in barycentric coordinates
|
||||
barycentricB = u;
|
||||
barycentricC = v;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
bool is_leaf(int node_index)
|
||||
{
|
||||
return nodes[node_index].element_index != -1;
|
||||
}
|
||||
|
||||
bool TraceAnyHit(vec3 origin, float tmin, vec3 dir, float tmax)
|
||||
{
|
||||
if (tmax <= 0.0f)
|
||||
return false;
|
||||
|
||||
RayBBox ray = create_ray(origin, origin + dir * tmax);
|
||||
tmin /= tmax;
|
||||
|
||||
int stack[64];
|
||||
int stackIndex = 0;
|
||||
stack[stackIndex++] = nodesRoot;
|
||||
do
|
||||
{
|
||||
int a = stack[--stackIndex];
|
||||
if (overlap_bv_ray(ray, a))
|
||||
{
|
||||
if (is_leaf(a))
|
||||
{
|
||||
float baryB, baryC;
|
||||
float t = intersect_triangle_ray(ray, a, baryB, baryC);
|
||||
if (t >= tmin && t < 1.0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stack[stackIndex++] = nodes[a].right;
|
||||
stack[stackIndex++] = nodes[a].left;
|
||||
}
|
||||
}
|
||||
} while (stackIndex > 0);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool traceHit(vec3 origin, vec3 direction, float dist)
|
||||
{
|
||||
return TraceAnyHit(origin, 0.01f, direction, dist);
|
||||
}
|
||||
|
||||
#endif
|
||||
#include <shaders/lightmap/polyfill_rayquery.glsl>
|
||||
|
||||
vec2 getVogelDiskSample(int sampleIndex, int sampleCount, float phi)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue