star-3d

Surface structuring for efficient 3D geometric queries
git clone git://git.meso-star.com/star-3d.git
Log | Files | Refs | README | LICENSE

s3d_c.h (5802B)


      1 /* Copyright (C) 2015-2023, 2026 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This file is part of Star-3D.
      4  *
      5  * Star-3D is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * Star-3D is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with Star-3D. If not, see <http://www.gnu.org/licenses/>. */
     17 
     18 #ifndef S3D_C_H
     19 #define S3D_C_H
     20 
     21 #include "s3d.h"
     22 #include "s3d_backend.h"
     23 
     24 #include <rsys/rsys.h>
     25 
     26 /* Filter function and its associated user defined data */
     27 struct hit_filter {
     28   s3d_hit_filter_function_T func;
     29   void* data;
     30 };
     31 
     32 
     33 static FINLINE res_T
     34 rtc_error_to_res_T(const enum RTCError err)
     35 {
     36   switch(err) {
     37     case RTC_ERROR_NONE: return RES_OK;
     38     case RTC_ERROR_UNKNOWN: return RES_UNKNOWN_ERR;
     39     case RTC_ERROR_INVALID_ARGUMENT: return RES_BAD_ARG;
     40     case RTC_ERROR_INVALID_OPERATION: return RES_BAD_ARG;
     41     case RTC_ERROR_OUT_OF_MEMORY: return RES_MEM_ERR;
     42     case RTC_ERROR_UNSUPPORTED_CPU: return RES_BAD_ARG;
     43     case RTC_ERROR_CANCELLED: return RES_UNKNOWN_ERR;
     44     default: FATAL("Unreachable code\n"); break;
     45   }
     46 }
     47 
     48 static INLINE const char*
     49 rtc_error_string(const enum RTCError err)
     50 {
     51   const char* str = NULL;
     52   switch(err) {
     53     case RTC_ERROR_NONE: str = "No error"; break;
     54     case RTC_ERROR_UNKNOWN: str = "Unknown error"; break;
     55     case RTC_ERROR_INVALID_ARGUMENT: str = "Invalid argument"; break;
     56     case RTC_ERROR_INVALID_OPERATION: str = "Invalid operation"; break;
     57     case RTC_ERROR_OUT_OF_MEMORY: str = "Out of memory"; break;
     58     case RTC_ERROR_UNSUPPORTED_CPU: str = "Unsupported CPU"; break;
     59     case RTC_ERROR_CANCELLED: str = "Cancelled operation"; break;
     60     default: FATAL("Unreachable code\n"); break;
     61   }
     62   return str;
     63 }
     64 
     65 static INLINE unsigned
     66 s3d_type_get_dimension(const enum s3d_type type)
     67 {
     68   switch(type) {
     69     case S3D_FLOAT: return 1;
     70     case S3D_FLOAT2: return 2;
     71     case S3D_FLOAT3: return 3;
     72     case S3D_FLOAT4: return 4;
     73     default: FATAL("Unreachable code\n"); break;
     74   }
     75 }
     76 
     77 #define RAYN_GRAB(RayN, N, i, Type, Attr) \
     78   (((Type*)((char*)(RayN)+(offsetof(struct RTCRay, Attr)*N)))[i])
     79 #define HITN_GRAB(HitN, N, i, Type, Attr) \
     80   (((Type*)((char*)(HitN)+(offsetof(struct RTCHit, Attr)*N)))[i])
     81 #define RAYHITN_GET_RAYN(RayHitN, N) \
     82   ((struct RTCRayN*)((char*)RayHitN+offsetof(struct RTCRayHit, ray)*N))
     83 #define RAYHITN_GET_HITN(RayHitN, N) \
     84   ((struct RTCHitN*)((char*)RayHitN+offsetof(struct RTCRayHit, hit)*N))
     85 
     86 static FINLINE void
     87 rtc_rayN_get_ray
     88   (const struct RTCRayN* rayN, /* SoA layout */
     89    const size_t N, /* SoA width */
     90    const size_t i, /* Id of the ray */
     91    struct RTCRay* ray)
     92 {
     93   ASSERT(rayN && ray && i < N);
     94   ray->org_x = RAYN_GRAB(rayN, N, i, float, org_x);
     95   ray->org_y = RAYN_GRAB(rayN, N, i, float, org_y);
     96   ray->org_z = RAYN_GRAB(rayN, N, i, float, org_z);
     97   ray->tnear = RAYN_GRAB(rayN, N, i, float, tnear);
     98   ray->dir_x = RAYN_GRAB(rayN, N, i, float, dir_x);
     99   ray->dir_y = RAYN_GRAB(rayN, N, i, float, dir_y);
    100   ray->dir_z = RAYN_GRAB(rayN, N, i, float, dir_z);
    101   ray->time  = RAYN_GRAB(rayN, N, i, float, time);
    102   ray->tfar  = RAYN_GRAB(rayN, N, i, float, tfar);
    103   ray->mask  = RAYN_GRAB(rayN, N, i, unsigned, mask);
    104   ray->id    = RAYN_GRAB(rayN, N, i, unsigned, id);
    105   ray->flags = RAYN_GRAB(rayN, N, i, unsigned, flags);
    106 }
    107 
    108 static FINLINE void
    109 rtc_hitN_get_hit
    110   (const struct RTCHitN* hitN,
    111    const size_t N,
    112    const size_t i,
    113    struct RTCHit* hit)
    114 {
    115   size_t id;
    116   ASSERT(hitN && hit && i < N);
    117   hit->Ng_x   = HITN_GRAB(hitN, N, i, float, Ng_x);
    118   hit->Ng_y   = HITN_GRAB(hitN, N, i, float, Ng_y);
    119   hit->Ng_z   = HITN_GRAB(hitN, N, i, float, Ng_z);
    120   hit->u      = HITN_GRAB(hitN, N, i, float, u);
    121   hit->v      = HITN_GRAB(hitN, N, i, float, v);
    122   hit->primID = HITN_GRAB(hitN, N, i, unsigned, primID);
    123   hit->geomID = HITN_GRAB(hitN, N, i, unsigned, geomID);
    124   FOR_EACH(id, 0, RTC_MAX_INSTANCE_LEVEL_COUNT) {
    125     hit->instID[id] = HITN_GRAB(hitN, N, i, unsigned, instID[id]);
    126   }
    127 }
    128 
    129 static FINLINE void
    130 rtc_rayN_set_ray
    131   (struct RTCRayN* rayN,
    132    const size_t N,
    133    const size_t i,
    134    const struct RTCRay* ray)
    135 {
    136   ASSERT(rayN && ray && i < N);
    137   RAYN_GRAB(rayN, N, i, float, org_x) = ray->org_x;
    138   RAYN_GRAB(rayN, N, i, float, org_y) = ray->org_y;
    139   RAYN_GRAB(rayN, N, i, float, org_z) = ray->org_z;
    140   RAYN_GRAB(rayN, N, i, float, tnear) = ray->tnear;
    141   RAYN_GRAB(rayN, N, i, float, dir_x) = ray->dir_x;
    142   RAYN_GRAB(rayN, N, i, float, dir_y) = ray->dir_y;
    143   RAYN_GRAB(rayN, N, i, float, dir_z) = ray->dir_z;
    144   RAYN_GRAB(rayN, N, i, float, time) = ray->time;
    145   RAYN_GRAB(rayN, N, i, float, tfar) = ray->tfar;
    146   RAYN_GRAB(rayN, N, i, unsigned, mask) = ray->mask;
    147   RAYN_GRAB(rayN, N, i, unsigned, id) = ray->id;
    148   RAYN_GRAB(rayN, N, i, unsigned, flags) = ray->flags;
    149 }
    150 
    151 static FINLINE void
    152 rtc_hitN_set_hit
    153   (const struct RTCHitN* hitN,
    154    const size_t N,
    155    const size_t i,
    156    struct RTCHit* hit)
    157 {
    158   size_t id;
    159   ASSERT(hitN && hit && i < N);
    160   HITN_GRAB(hitN, N, i, float, Ng_x) = hit->Ng_x;
    161   HITN_GRAB(hitN, N, i, float, Ng_y) = hit->Ng_y;
    162   HITN_GRAB(hitN, N, i, float, Ng_z) = hit->Ng_z;
    163   HITN_GRAB(hitN, N, i, float, u) = hit->u;
    164   HITN_GRAB(hitN, N, i, float, v) = hit->v;
    165   HITN_GRAB(hitN, N, i, unsigned, primID) = hit->primID;
    166   HITN_GRAB(hitN, N, i, unsigned, geomID) = hit->geomID;
    167   FOR_EACH(id, 0, RTC_MAX_INSTANCE_LEVEL_COUNT) {
    168     HITN_GRAB(hitN, N, i, unsigned, instID[id]) = hit->instID[id];
    169   }
    170 }
    171 
    172 #endif /* S3D_C_H */
    173