1.3.64
 
Loading...
Searching...
No Matches
BufferIndexing.h
Go to the documentation of this file.
1
16#ifndef HELIOS_BUFFER_INDEXING_H
17#define HELIOS_BUFFER_INDEXING_H
18
19#include <cstddef>
20#include <climits>
21
48// Support both CUDA and CPU compilation
49#ifdef __CUDACC__
50#define HELIOS_HOST_DEVICE __host__ __device__
51#else
52#define HELIOS_HOST_DEVICE
53#endif
54
66private:
67 size_t dim1_size;
68
69public:
76 explicit BufferIndexer2D(size_t dim0_size, size_t dim1_size) : dim1_size(dim1_size) {
77 // dim0_size parameter exists for self-documentation and future validation
78 (void) dim0_size;
79 }
80
87 HELIOS_HOST_DEVICE inline size_t operator()(size_t i, size_t j) const {
88 return i * dim1_size + j;
89 }
90
92 HELIOS_HOST_DEVICE inline size_t getDim1Size() const {
93 return dim1_size;
94 }
95};
96
107private:
108 size_t dim1_size;
109 size_t dim2_size;
110 size_t dim1_dim2_product; // Precomputed for efficiency
111
112public:
120 explicit BufferIndexer3D(size_t dim0_size, size_t dim1_size, size_t dim2_size) : dim1_size(dim1_size), dim2_size(dim2_size), dim1_dim2_product(dim1_size * dim2_size) {
121 (void) dim0_size;
122 }
123
131 HELIOS_HOST_DEVICE inline size_t operator()(size_t i, size_t j, size_t k) const {
132 return i * dim1_dim2_product + j * dim2_size + k;
133 }
134
136 HELIOS_HOST_DEVICE inline size_t getDim1Size() const {
137 return dim1_size;
138 }
139
141 HELIOS_HOST_DEVICE inline size_t getDim2Size() const {
142 return dim2_size;
143 }
144};
145
157private:
158 size_t dim1_size;
159 size_t dim2_size;
160 size_t dim3_size;
161 size_t dim1_dim2_dim3_product; // dim1 * dim2 * dim3
162 size_t dim2_dim3_product; // dim2 * dim3
163
164public:
173 explicit BufferIndexer4D(size_t dim0_size, size_t dim1_size, size_t dim2_size, size_t dim3_size) :
174 dim1_size(dim1_size), dim2_size(dim2_size), dim3_size(dim3_size), dim1_dim2_dim3_product(dim1_size * dim2_size * dim3_size), dim2_dim3_product(dim2_size * dim3_size) {
175 (void) dim0_size;
176 }
177
186 HELIOS_HOST_DEVICE inline size_t operator()(size_t i, size_t j, size_t k, size_t l) const {
187 return i * dim1_dim2_dim3_product + j * dim2_dim3_product + k * dim3_size + l;
188 }
189
191 HELIOS_HOST_DEVICE inline size_t getDim1Size() const {
192 return dim1_size;
193 }
194
196 HELIOS_HOST_DEVICE inline size_t getDim2Size() const {
197 return dim2_size;
198 }
199
201 HELIOS_HOST_DEVICE inline size_t getDim3Size() const {
202 return dim3_size;
203 }
204};
205
206// ========== Named Type Aliases for Self-Documenting Code ==========
207
217
225
236
244
253
263
264// ========== UUID / Position Lookup Helper ==========
265
293private:
294 const uint *primitive_positions_;
295 size_t primitive_count_;
296
297public:
304 UUIDLookupHelper(const uint *prim_positions, size_t prim_count) : primitive_positions_(prim_positions), primitive_count_(prim_count) {
305 }
306
320 HELIOS_HOST_DEVICE inline bool toPosition(uint UUID, uint &position) const {
321 // Lookup in sparse table - returns UINT_MAX if UUID doesn't exist
322 uint pos = primitive_positions_[UUID];
323 if (pos == UINT_MAX || pos >= primitive_count_) {
324 return false; // Invalid UUID or out-of-bounds position
325 }
326 position = pos;
327 return true;
328 }
329};
330
331// ========== Camera Pixel Coordinate Helper ==========
332
366
369 PixelCoordinate(uint x_coord, uint y_coord) : x(x_coord), y(y_coord) {
370 }
371
377#ifdef __CUDACC__
378 // CUDA version: use int2 (built-in CUDA type)
379 __host__ __device__ inline size_t toFlatIndex(const int2 &full_resolution) const {
380 return static_cast<size_t>(y) * full_resolution.x + x;
381 }
382#else
383 // CPU version: use helios::int2
384 inline size_t toFlatIndex(const helios::int2 &full_resolution) const {
385 return static_cast<size_t>(y) * full_resolution.x + x;
386 }
387#endif
388
389// CUDA-specific factory methods (use OptiX types)
390#ifdef __CUDACC__
402 __device__ static PixelCoordinate fromTiledLaunch(const optix::uint3 &launch_idx, const optix::int2 &tile_offset, const optix::int2 &full_resolution) {
403 (void) full_resolution; // Unused but included for API clarity
404 return PixelCoordinate(tile_offset.x + launch_idx.y, // Global x from local y-index
405 tile_offset.y + launch_idx.z // Global y from local z-index
406 );
407 }
408
419 __device__ static size_t computeFlatIndex(const optix::uint3 &launch_idx, const optix::int2 &tile_offset, const optix::int2 &full_resolution) {
420 PixelCoordinate pixel = fromTiledLaunch(launch_idx, tile_offset, full_resolution);
421 return pixel.toFlatIndex(full_resolution);
422 }
423#endif // __CUDACC__
424};
425
426// ========== Subpatch UUID Calculator ==========
427
458private:
459 uint base_UUID_;
460#ifdef __CUDACC__
461 int2 subdivisions_;
462#else
463 helios::int2 subdivisions_;
464#endif
465
466public:
472#ifdef __CUDACC__
473 __host__ __device__ SubpatchUUIDCalculator(uint base_UUID, int2 subdivisions) : base_UUID_(base_UUID), subdivisions_(subdivisions) {
474 }
475#else
476 SubpatchUUIDCalculator(uint base_UUID, helios::int2 subdivisions) : base_UUID_(base_UUID), subdivisions_(subdivisions) {
477 }
478#endif
479
489 HELIOS_HOST_DEVICE inline uint getUUID(int col, int row) const {
490 return base_UUID_ + row * subdivisions_.x + col;
491 }
492
498 return subdivisions_.x * subdivisions_.y;
499 }
500
505#ifdef __CUDACC__
506 __host__ __device__ int2 getSubdivisions() const {
507 return subdivisions_;
508 }
509#else
511 return subdivisions_;
512 }
513#endif
514};
515
516#endif // HELIOS_BUFFER_INDEXING_H