1 //
2 // Copyright (C) 2009-2021 Intel Corporation
3 //
4 // SPDX-License-Identifier: MIT
5 //
6 //
7
8 #pragma once
9
10 #include "shared.h"
11 #include "intrinsics.h"
12 #include "AABB.h"
13 #include "AABB3f.h"
14
15 // JDB TODO: Use corresponding GRL structures!!!
16
17 struct Quad
18 {
19 unsigned int shaderIndex; // note: also mask
20 unsigned int geomIndex; // note: also geom flags in upper 2 bits
21 unsigned int primIndex0;
22 unsigned int primIndex1Delta;
23 float v[4][3];
24 };
25
Quad_getGeomIndex(global struct Quad * quad)26 GRL_INLINE unsigned int Quad_getGeomIndex(global struct Quad *quad)
27 {
28 return quad->geomIndex;
29 }
30
Quad_getPrimIndex0(global struct Quad * quad)31 GRL_INLINE unsigned int Quad_getPrimIndex0(global struct Quad *quad)
32 {
33 return quad->primIndex0;
34 }
35
Quad_getPrimIndex1(global struct Quad * quad)36 GRL_INLINE unsigned int Quad_getPrimIndex1(global struct Quad *quad)
37 {
38 return quad->primIndex0 + (quad->primIndex1Delta & 0xFFFF);
39 }
40
load_float3(float * p)41 GRL_INLINE float3 load_float3(float *p)
42 {
43 return (float3)(p[0], p[1], p[2]);
44 }
45
load_perm_float3(float * p,const uint3 perm)46 GRL_INLINE float3 load_perm_float3(float *p, const uint3 perm)
47 {
48 return (float3)(p[perm.x], p[perm.y], p[perm.z]);
49 }
50
load_perm_float2(float * p,const uint2 perm)51 GRL_INLINE float2 load_perm_float2(float *p, const uint2 perm)
52 {
53 return (float2)(p[perm.x], p[perm.y]);
54 }
55
load_perm_float(float * p,const uint perm)56 GRL_INLINE float load_perm_float(float *p, const uint perm)
57 {
58 return p[perm];
59 }
60
getAABB_Quad(struct Quad * q)61 GRL_INLINE struct AABB getAABB_Quad(struct Quad *q)
62 {
63 struct AABB aabb;
64 const float3 lower = min(min(load_float3(q->v[0]), load_float3(q->v[1])), min(load_float3(q->v[2]), load_float3(q->v[3])));
65 const float3 upper = max(max(load_float3(q->v[0]), load_float3(q->v[1])), max(load_float3(q->v[2]), load_float3(q->v[3])));
66 aabb.lower = (float4)(lower, 0.0f);
67 aabb.upper = (float4)(upper, 0.0f);
68 return aabb;
69 }
70
Quad_ExtendAABB(struct Quad * q,struct AABB * box)71 GRL_INLINE void Quad_ExtendAABB(struct Quad* q, struct AABB* box)
72 {
73 struct AABB aabb;
74 const float3 lower = min(min(load_float3(q->v[0]), load_float3(q->v[1])), min(load_float3(q->v[2]), load_float3(q->v[3])));
75 const float3 upper = max(max(load_float3(q->v[0]), load_float3(q->v[1])), max(load_float3(q->v[2]), load_float3(q->v[3])));
76 aabb.lower = (float4)(lower, 0.0f);
77 aabb.upper = (float4)(upper, 0.0f);
78 AABB_extend(box, &aabb);
79 }
80
getCentroid2_Quad(struct Quad * q)81 GRL_INLINE float4 getCentroid2_Quad(struct Quad *q)
82 {
83 struct AABB aabb = getAABB_Quad(q);
84 return aabb.lower + aabb.upper;
85 }
86
setQuad(struct Quad * quad,const float4 v0,const float4 v1,const float4 v2,const float4 v3,const uchar j0,const uchar j1,const uchar j2,const uint geomID,const uint primID0,const uint primID1,const uint geomMask,const uint geomFlags)87 GRL_INLINE void setQuad(struct Quad *quad, const float4 v0, const float4 v1, const float4 v2, const float4 v3,
88 const uchar j0, const uchar j1, const uchar j2,
89 const uint geomID, const uint primID0, const uint primID1, const uint geomMask, const uint geomFlags )
90 {
91 quad->v[0][0] = v0.x;
92 quad->v[0][1] = v0.y;
93 quad->v[0][2] = v0.z;
94 quad->v[1][0] = v1.x;
95 quad->v[1][1] = v1.y;
96 quad->v[1][2] = v1.z;
97 quad->v[2][0] = v2.x;
98 quad->v[2][1] = v2.y;
99 quad->v[2][2] = v2.z;
100 quad->v[3][0] = v3.x;
101 quad->v[3][1] = v3.y;
102 quad->v[3][2] = v3.z;
103
104 quad->shaderIndex = (geomMask << 24) | geomID;
105 quad->geomIndex = geomID | (geomFlags << 30);
106 quad->primIndex0 = primID0;
107 const uint delta = primID1 - primID0;
108 const uint j = (((j0) << 0) | ((j1) << 2) | ((j2) << 4));
109 quad->primIndex1Delta = delta | (j << 16) | (1 << 22); // single prim in leaf
110
111 }
112
setQuadVertices(struct Quad * quad,const float3 v0,const float3 v1,const float3 v2,const float3 v3)113 GRL_INLINE void setQuadVertices(struct Quad *quad, const float3 v0, const float3 v1, const float3 v2, const float3 v3)
114 {
115 quad->v[0][0] = v0.x;
116 quad->v[0][1] = v0.y;
117 quad->v[0][2] = v0.z;
118 quad->v[1][0] = v1.x;
119 quad->v[1][1] = v1.y;
120 quad->v[1][2] = v1.z;
121 quad->v[2][0] = v2.x;
122 quad->v[2][1] = v2.y;
123 quad->v[2][2] = v2.z;
124 quad->v[3][0] = v3.x;
125 quad->v[3][1] = v3.y;
126 quad->v[3][2] = v3.z;
127 }
128