1 /*
2 * Copyright 2023 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #ifndef LIBAGX_H
7 #define LIBAGX_H
8
9 /* Define stdint types compatible between the CPU and GPU for shared headers */
10 #ifndef __OPENCL_VERSION__
11 #include <stdint.h>
12 #include "util/macros.h"
13 #define GLOBAL(type_) uint64_t
14 #define CONSTANT(type_) uint64_t
15 #define AGX_STATIC_ASSERT(_COND) static_assert(_COND, #_COND)
16 #else
17 #pragma OPENCL EXTENSION cl_khr_fp16 : enable
18 #define PACKED __attribute__((packed, aligned(4)))
19 #define GLOBAL(type_) global type_ *
20 #define CONSTANT(type_) constant type_ *
21
22 typedef ulong uint64_t;
23 typedef uint uint32_t;
24 typedef ushort uint16_t;
25 typedef uchar uint8_t;
26
27 typedef long int64_t;
28 typedef int int32_t;
29 typedef short int16_t;
30 typedef char int8_t;
31
32 /* Define NIR intrinsics for CL */
33 uint32_t nir_interleave_agx(uint16_t x, uint16_t y);
34 void nir_doorbell_agx(uint8_t value);
35 void nir_stack_map_agx(uint16_t index, uint32_t address);
36 uint32_t nir_stack_unmap_agx(uint16_t index);
37 uint32_t nir_load_core_id_agx(void);
38 uint32_t nir_load_helper_op_id_agx(void);
39 uint32_t nir_load_helper_arg_lo_agx(void);
40 uint32_t nir_load_helper_arg_hi_agx(void);
41 uint32_t nir_fence_helper_exit_agx(void);
42
43 uint4 nir_bindless_image_load_array(uint2 handle, int4 coord);
44 void nir_bindless_image_store_array(uint2 handle, int4 coord, uint4 datum);
45 uint4 nir_bindless_image_load_ms_array(uint2 handle, int4 coord, uint sample);
46 void nir_bindless_image_store_ms_array(uint2 handle, int4 coord, uint sample,
47 uint4 datum);
48
49 uint libagx_load_index_buffer_internal(uintptr_t index_buffer,
50 uint32_t index_buffer_range_el, uint id,
51 uint index_size);
52
53 /* I have no idea why CL doesn't have this */
54 uint ballot(bool cond);
55
56 #define _S(x) #x
57 #define AGX_PASTE_(x, y) x##y
58 #define AGX_PASTE(x, y) AGX_PASTE_(x, y)
59 #define AGX_STATIC_ASSERT(_COND) \
60 typedef char AGX_PASTE(static_assertion, __LINE__)[(_COND) ? 1 : -1]
61
62 static inline uint
align(uint x,uint y)63 align(uint x, uint y)
64 {
65 return (x + y - 1) & ~(y - 1);
66 }
67
68 static inline uint32_t
libagx_logbase2_ceil(uint32_t n)69 libagx_logbase2_ceil(uint32_t n)
70 {
71 return (n <= 1) ? 0 : 32 - clz(n - 1);
72 }
73
74 #define offsetof(x, y) __builtin_offsetof(x, y)
75
76 #endif
77
78 #endif
79