yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeFix type checking on generic extensions. (#5316)c97166aed

master
3.7 KiB131 linesraw
1import fcpw;
2
3#define UNDEFINED_BVH_TYPE 0
4#define LINE_SEGMENT_BVH 1
5#define TRIANGLE_BVH 2
6#define LINE_SEGMENT_SNCH 3
7#define TRIANGLE_SNCH 4
8
9#ifndef _BVH_TYPE
10#define _BVH_TYPE UNDEFINED_BVH_TYPE
11#endif
12
13#if _BVH_TYPE == LINE_SEGMENT_BVH
14uniform ParameterBlock<Bvh<BvhNode, LineSegment, NoSilhouette>> gBvh;
15#define _BVH_HAS_SILHOUETTE_DATA 0
16
17#elif _BVH_TYPE == TRIANGLE_BVH
18uniform ParameterBlock<Bvh<BvhNode, Triangle, NoSilhouette>> gBvh;
19#define _BVH_HAS_SILHOUETTE_DATA 0
20
21#elif _BVH_TYPE == LINE_SEGMENT_SNCH
22uniform ParameterBlock<Bvh<SnchNode, LineSegment, Vertex>> gBvh;
23#define _BVH_HAS_SILHOUETTE_DATA 1
24
25#elif _BVH_TYPE == TRIANGLE_SNCH
26uniform ParameterBlock<Bvh<SnchNode, Triangle, Edge>> gBvh;
27#define _BVH_HAS_SILHOUETTE_DATA 1
28
29#else
30// Compile time error
31#error _BVH_TYPE is not set to a supported type
32#endif
33
34[shader("compute")]
35[numthreads(256, 1, 1)]
36void rayIntersection(uint3 threadId: SV_DispatchThreadID,
37                     uniform StructuredBuffer<Ray> rays,
38                     uniform bool checkForOcclusion,
39                     uniform RWStructuredBuffer<Interaction> interactions,
40                     uniform uint nQueries)
41{
42    uint index = threadId.x;
43    if (index >= nQueries)
44    {
45        return;
46    }
47
48    Ray r = rays[index];
49    Interaction i;
50    bool didIntersect = gBvh.intersect(r, checkForOcclusion, i);
51    if (didIntersect)
52    {
53        interactions[index] = i;
54    }
55}
56
57[shader("compute")]
58[numthreads(256, 1, 1)]
59void sphereIntersection(uint3 threadId: SV_DispatchThreadID,
60                        uniform StructuredBuffer<BoundingSphere> boundingSpheres,
61                        uniform StructuredBuffer<float3> randNums,
62                        uniform RWStructuredBuffer<Interaction> interactions,
63                        uniform uint nQueries)
64{
65    uint index = threadId.x;
66    if (index >= nQueries)
67    {
68        return;
69    }
70
71    BoundingSphere s = boundingSpheres[index];
72    float3 randNum = randNums[index];
73    ConstantBranchTraversalWeight branchTraversalWeight;
74    Interaction i;
75    bool didIntersect = gBvh.intersect<ConstantBranchTraversalWeight>(s, randNum, branchTraversalWeight, i);
76    if (didIntersect)
77    {
78        interactions[index] = i;
79    }
80}
81
82[shader("compute")]
83[numthreads(256, 1, 1)]
84void closestPoint(uint3 threadId: SV_DispatchThreadID,
85                  uniform StructuredBuffer<BoundingSphere> boundingSpheres,
86                  uniform RWStructuredBuffer<Interaction> interactions,
87                  uniform bool recordNormals,
88                  uniform uint nQueries)
89{
90    uint index = threadId.x;
91    if (index >= nQueries)
92    {
93        return;
94    }
95
96    BoundingSphere s = boundingSpheres[index];
97    Interaction i;
98    bool found = gBvh.findClosestPoint(s, i, recordNormals);
99    if (found)
100    {
101        interactions[index] = i;
102    }
103}
104
105[shader("compute")]
106[numthreads(256, 1, 1)]
107void closestSilhouettePoint(uint3 threadId: SV_DispatchThreadID,
108                            uniform StructuredBuffer<BoundingSphere> boundingSpheres,
109                            uniform StructuredBuffer<uint> flipNormalOrientation,
110                            uniform float squaredMinRadius,
111                            uniform float precision,
112                            uniform RWStructuredBuffer<Interaction> interactions,
113                            uniform uint nQueries)
114{
115    uint index = threadId.x;
116    if (index >= nQueries)
117    {
118        return;
119    }
120
121    Interaction i;
122#if _BVH_HAS_SILHOUETTE_DATA
123    BoundingSphere s = boundingSpheres[index];
124    bool flipNormal = flipNormalOrientation[index] == 1 ? true : false;
125    bool found = gBvh.findClosestSilhouettePoint(s, flipNormal, squaredMinRadius, precision, i);
126    if (found)
127    {
128        interactions[index] = i;
129    }
130#endif
131}