-
Notifications
You must be signed in to change notification settings - Fork 5
/
RayCast.comp
67 lines (55 loc) · 2.8 KB
/
RayCast.comp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#version 460
#extension GL_GOOGLE_include_directive : require
#extension GL_EXT_ray_query : require
#include "Platform.glsl"
#include "common/Const_Func.glsl"
#include "common/Material.glsl"
#include "common/UniformBufferObject.glsl"
layout(binding = 0, set = 0) uniform accelerationStructureEXT Scene;
layout(binding = 1) buffer RayCastInArray { RayCastContext[] RayIO; };
//layout(binding = 3) readonly uniform UniformBufferObjectStruct { UniformBufferObject Camera; };
layout(binding = 4) readonly buffer VertexArray { float Vertices[]; };
layout(binding = 5) readonly buffer IndexArray { uint Indices[]; };
layout(binding = 6) readonly buffer MaterialArray { Material[] Materials; };
layout(binding = 7) readonly buffer OffsetArray { uvec2[] Offsets; };
layout(binding = 8) readonly buffer NodeProxyArray { NodeProxy[] NodeProxies; };
layout(set = 1, binding = 0) uniform sampler2D TextureSamplers[];
#include "common/RTSimple.glsl"
layout(local_size_x = 8, local_size_y = 4, local_size_z = 1) in;
void main() {
RayCastContext context = RayIO[gl_GlobalInvocationID.x];
rayQueryEXT rayQuery;
// gl_RayFlagsTerminateOnFirstHitEXT for fast, but hit not the closet
rayQueryInitializeEXT(rayQuery, Scene, gl_RayFlagsNoneEXT, 0xFF, context.Origin.xyz, EPS, context.Direction.xyz, INF);
while( rayQueryProceedEXT(rayQuery) )
{
}
if (rayQueryGetIntersectionTypeEXT(rayQuery, true) == gl_RayQueryCommittedIntersectionTriangleEXT ) {
const bool IsCommitted = true;
const int InstCustIndex = rayQueryGetIntersectionInstanceCustomIndexEXT(rayQuery, IsCommitted);
const vec3 RayOrigin = context.Origin.xyz;
const vec3 RayDirection = context.Direction.xyz;
const float RayDist = rayQueryGetIntersectionTEXT(rayQuery, IsCommitted);
const mat4x3 WorldToObject = rayQueryGetIntersectionWorldToObjectEXT(rayQuery, IsCommitted);
const vec2 TwoBaryCoords = rayQueryGetIntersectionBarycentricsEXT(rayQuery, IsCommitted);
const vec3 HitPos = RayOrigin + RayDirection * RayDist;
const int PrimitiveIndex = rayQueryGetIntersectionPrimitiveIndexEXT(rayQuery, IsCommitted);
const int InstanceID = rayQueryGetIntersectionInstanceIdEXT(rayQuery, IsCommitted);
vec3 OutNormal;
vec2 OutTexcoord;
uint OutMaterialId;
uint OutInstanceId;
SimpleHit(InstCustIndex, WorldToObject, TwoBaryCoords, PrimitiveIndex, InstanceID, OutNormal, OutTexcoord, OutMaterialId, OutInstanceId);
context.HitPoint = vec4(HitPos, 1.0);
context.Normal = vec4(OutNormal, 0.0);
context.Hitted = 1;
context.T = RayDist;
context.InstanceId = OutInstanceId;
context.MaterialId = OutMaterialId;
}
else
{
context.Hitted = 0;
}
RayIO[gl_GlobalInvocationID.x] = context;
}