Blender V5.0
device/metal/compat.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#define __KERNEL_GPU__
8#define __KERNEL_METAL__
9#define CCL_NAMESPACE_BEGIN
10#define CCL_NAMESPACE_END
11
12#ifndef ATTR_FALLTHROUGH
13# define ATTR_FALLTHROUGH
14#endif
15
16#include <metal_atomic>
17#include <metal_pack>
18#include <metal_stdlib>
19#include <simd/simd.h>
20
21using namespace metal;
22
23#ifdef __METALRT__
24using namespace metal::raytracing;
25#endif
26
27#pragma clang diagnostic ignored "-Wunused-variable"
28#pragma clang diagnostic ignored "-Wsign-compare"
29#pragma clang diagnostic ignored "-Wuninitialized"
30#pragma clang diagnostic ignored "-Wc++17-extensions"
31#pragma clang diagnostic ignored "-Wmacro-redefined"
32
33/* Qualifiers */
34
35#define ccl_device
36#define ccl_device_inline ccl_device __attribute__((always_inline))
37#define ccl_device_forceinline ccl_device __attribute__((always_inline))
38#if defined(__KERNEL_METAL_APPLE__)
39# define ccl_device_noinline ccl_device
40#else
41# define ccl_device_noinline ccl_device __attribute__((noinline))
42#endif
43
44#define ccl_device_extern extern "C"
45#define ccl_device_noinline_cpu ccl_device
46#define ccl_device_inline_method ccl_device
47#define ccl_device_template_spec template<> ccl_device_inline
48#define ccl_global device
49#define ccl_inline_constant static constant constexpr
50#define ccl_device_constant constant
51#define ccl_static_constexpr static constant constexpr
52#define ccl_constant constant
53#define ccl_gpu_shared threadgroup
54#define ccl_private thread
55#ifdef __METALRT__
56# define ccl_ray_data ray_data
57#else
58# define ccl_ray_data ccl_private
59#endif
60#define ccl_may_alias
61#define ccl_restrict __restrict
62#define ccl_align(n) alignas(n)
63#define ccl_optional_struct_init
64
65/* No assert supported for Metal */
66
67#define kernel_assert(cond)
68
69#define offsetof(t, d) __builtin_offsetof(t, d)
70
71#define ccl_gpu_global_id_x() metal_global_id
72#define ccl_gpu_warp_size simdgroup_size
73#define ccl_gpu_thread_idx_x simd_group_index
74#define ccl_gpu_thread_mask(thread_warp) uint64_t((1ull << thread_warp) - 1)
75
76#define ccl_gpu_ballot(predicate) ((uint64_t)((simd_vote::vote_t)simd_ballot(predicate)))
77#define ccl_gpu_syncthreads() threadgroup_barrier(mem_flags::mem_threadgroup);
78
79// clang-format off
80
81/* kernel.h adapters */
82
83#define ccl_gpu_kernel(block_num_threads, thread_num_registers)
84#define ccl_gpu_kernel_threads(block_num_threads)
85
86/* Convert a comma-separated list into a semicolon-separated list
87 * (so that we can generate a struct based on kernel entry-point parameters). */
88#define FN0()
89#define FN1(p1) p1;
90#define FN2(p1, p2) p1; p2;
91#define FN3(p1, p2, p3) p1; p2; p3;
92#define FN4(p1, p2, p3, p4) p1; p2; p3; p4;
93#define FN5(p1, p2, p3, p4, p5) p1; p2; p3; p4; p5;
94#define FN6(p1, p2, p3, p4, p5, p6) p1; p2; p3; p4; p5; p6;
95#define FN7(p1, p2, p3, p4, p5, p6, p7) p1; p2; p3; p4; p5; p6; p7;
96#define FN8(p1, p2, p3, p4, p5, p6, p7, p8) p1; p2; p3; p4; p5; p6; p7; p8;
97#define FN9(p1, p2, p3, p4, p5, p6, p7, p8, p9) p1; p2; p3; p4; p5; p6; p7; p8; p9;
98#define FN10(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10;
99#define FN11(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11;
100#define FN12(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11; p12;
101#define FN13(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11; p12; p13;
102#define FN14(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11; p12; p13; p14;
103#define FN15(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11; p12; p13; p14; p15;
104#define FN16(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11; p12; p13; p14; p15; p16;
105#define FN17(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11; p12; p13; p14; p15; p16; p17;
106#define FN18(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11; p12; p13; p14; p15; p16; p17; p18;
107#define FN19(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11; p12; p13; p14; p15; p16; p17; p18; p19;
108#define FN20(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20) p1; p2; p3; p4; p5; p6; p7; p8; p9; p10; p11; p12; p13; p14; p15; p16; p17; p18; p19; p20;
109#define GET_LAST_ARG(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, ...) p20
110#define PARAMS_MAKER(...) GET_LAST_ARG(__VA_ARGS__, FN20, FN19, FN18, FN17, FN16, FN15, FN14, FN13, FN12, FN11, FN10, FN9, FN8, FN7, FN6, FN5, FN4, FN3, FN2, FN1, FN0)
111
112/* Generate a struct containing the entry-point parameters and a "run"
113 * method which can access them implicitly via this-> */
114
115#ifdef __METAL_GLOBAL_BUILTINS__
116
117#define ccl_gpu_kernel_signature(name, ...) \
118struct kernel_gpu_##name \
119{ \
120 PARAMS_MAKER(__VA_ARGS__)(__VA_ARGS__) \
121 void run(thread MetalKernelContext& context, \
122 threadgroup atomic_int *threadgroup_array) ccl_global const; \
123}; \
124kernel void cycles_metal_##name(device const kernel_gpu_##name *params_struct, \
125 constant KernelParamsMetal &ccl_restrict _launch_params_metal, \
126 constant MetalAncillaries *_metal_ancillaries, \
127 threadgroup atomic_int *threadgroup_array[[ threadgroup(0) ]]) { \
128 MetalKernelContext context(_launch_params_metal, _metal_ancillaries); \
129 params_struct->run(context, threadgroup_array); \
130} \
131void kernel_gpu_##name::run(thread MetalKernelContext& context, \
132 threadgroup atomic_int *threadgroup_array) ccl_global const
133
134#else
135
136/* On macOS versions before 14.x, builtin constants (e.g. metal_global_id) must
137 * be accessed through attributed entry-point parameters. */
138
139#define ccl_gpu_kernel_signature(name, ...) \
140struct kernel_gpu_##name \
141{ \
142 PARAMS_MAKER(__VA_ARGS__)(__VA_ARGS__) \
143 void run(thread MetalKernelContext& context, \
144 threadgroup atomic_int *threadgroup_array, \
145 const uint metal_global_id, \
146 const ushort metal_local_id, \
147 const ushort metal_local_size, \
148 const uint metal_grid_id, \
149 uint simdgroup_size, \
150 uint simd_lane_index, \
151 uint simd_group_index, \
152 uint num_simd_groups) ccl_global const; \
153}; \
154kernel void cycles_metal_##name(device const kernel_gpu_##name *params_struct, \
155 constant KernelParamsMetal &ccl_restrict _launch_params_metal, \
156 constant MetalAncillaries *_metal_ancillaries, \
157 threadgroup atomic_int *threadgroup_array[[ threadgroup(0) ]], \
158 const uint metal_global_id [[thread_position_in_grid]], \
159 const ushort metal_local_id [[thread_position_in_threadgroup]], \
160 const ushort metal_local_size [[threads_per_threadgroup]], \
161 const uint metal_grid_id [[threadgroup_position_in_grid]], \
162 uint simdgroup_size [[threads_per_simdgroup]], \
163 uint simd_lane_index [[thread_index_in_simdgroup]], \
164 uint simd_group_index [[simdgroup_index_in_threadgroup]], \
165 uint num_simd_groups [[simdgroups_per_threadgroup]]) { \
166 MetalKernelContext context(_launch_params_metal, _metal_ancillaries); \
167 params_struct->run(context, threadgroup_array, metal_global_id, metal_local_id, metal_local_size, metal_grid_id, simdgroup_size, simd_lane_index, simd_group_index, num_simd_groups); \
168} \
169void kernel_gpu_##name::run(thread MetalKernelContext& context, \
170 threadgroup atomic_int *threadgroup_array, \
171 const uint metal_global_id, \
172 const ushort metal_local_id, \
173 const ushort metal_local_size, \
174 const uint metal_grid_id, \
175 uint simdgroup_size, \
176 uint simd_lane_index, \
177 uint simd_group_index, \
178 uint num_simd_groups) ccl_global const
179
180#endif /* __METAL_GLOBAL_BUILTINS__ */
181
182#define ccl_gpu_kernel_postfix
183#define ccl_gpu_kernel_call(x) context.x
184#define ccl_gpu_kernel_within_bounds(i,n) true
185
186/* define a function object where "func" is the lambda body, and additional parameters are used to specify captured state. */
187#define ccl_gpu_kernel_lambda(func, ...) \
188 struct KernelLambda \
189 { \
190 KernelLambda(ccl_private MetalKernelContext &_context) : context(_context) {} \
191 ccl_private MetalKernelContext &context; \
192 __VA_ARGS__; \
193 int operator()(const int state) const { return (func); } \
194 } ccl_gpu_kernel_lambda_pass(context)
195
196// clang-format on
197
198/* make_type definitions with Metal style element initializers */
199ccl_device_forceinline float2 make_float2(const float x, const float y)
200{
201 return float2(x, y);
202}
203
204ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
205{
206 return float3(x, y, z);
207}
208
210 const float y,
211 const float z,
212 const float w)
213{
214 return float4(x, y, z, w);
215}
216
218{
219 return int2(x, y);
220}
221
222ccl_device_forceinline int3 make_int3(const int x, const int y, const int z)
223{
224 return int3(x, y, z);
225}
226
227ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
228{
229 return int4(x, y, z, w);
230}
231
233{
234 return uint2(x, y);
235}
236
238{
239 return uint3(x, y, z);
240}
241
243{
244 return uint4(x, y, z, w);
245}
246
248 const uchar y,
249 const uchar z,
250 const uchar w)
251{
252 return uchar4(x, y, z, w);
253}
254
255/* Math functions */
256
257#define __uint_as_float(x) as_type<float>(x)
258#define __float_as_uint(x) as_type<uint>(x)
259#define __int_as_float(x) as_type<float>(x)
260#define __float_as_int(x) as_type<int>(x)
261#define __float2half(x) half(x)
262#define powf(x, y) pow(float(x), float(y))
263#define fabsf(x) fabs(float(x))
264#define copysignf(x, y) copysign(float(x), float(y))
265#define asinf(x) asin(float(x))
266#define acosf(x) acos(float(x))
267#define atanf(x) atan(float(x))
268#define floorf(x) floor(float(x))
269#define ceilf(x) ceil(float(x))
270#define roundf(x) round(float(x))
271#define hypotf(x, y) hypot(float(x), float(y))
272#define atan2f(x, y) atan2(float(x), float(y))
273#define fmaxf(x, y) fmax(float(x), float(y))
274#define fminf(x, y) fmin(float(x), float(y))
275#define fmodf(x, y) fmod(float(x), float(y))
276#define sinhf(x) sinh(float(x))
277#define coshf(x) cosh(float(x))
278#define tanhf(x) tanh(float(x))
279#define saturatef(x) saturate(float(x))
280#define ldexpf(x, y) ldexp(float(x), int(y))
281
282/* Use native functions with possibly lower precision for performance,
283 * no issues found so far. */
284#define trigmode fast
285#define sinf(x) trigmode::sin(float(x))
286#define cosf(x) trigmode::cos(float(x))
287#define tanf(x) trigmode::tan(float(x))
288#define expf(x) trigmode::exp(float(x))
289#define sqrtf(x) trigmode::sqrt(float(x))
290#define logf(x) trigmode::log(float(x))
291
292#define __device__
293
294#ifdef __METALRT__
295
296# if defined(__METALRT_MOTION__)
297# define METALRT_TAGS instancing, instance_motion, primitive_motion
298# define METALRT_BLAS_TAGS , primitive_motion
299# else
300# define METALRT_TAGS instancing
301# define METALRT_BLAS_TAGS
302# endif /* __METALRT_MOTION__ */
303
304# if defined(__METALRT_EXTENDED_LIMITS__)
305# define METALRT_LIMITS , extended_limits
306# else
307# define METALRT_LIMITS
308# endif /* __METALRT_MOTION__ */
309
310typedef acceleration_structure<METALRT_TAGS> metalrt_as_type;
311typedef intersection_function_table<triangle_data, curve_data, METALRT_TAGS METALRT_LIMITS>
312 metalrt_ift_type;
313typedef metal::raytracing::intersector<triangle_data, curve_data, METALRT_TAGS METALRT_LIMITS>
314 metalrt_intersector_type;
315# if defined(__METALRT_MOTION__)
316typedef acceleration_structure<primitive_motion> metalrt_blas_as_type;
317typedef intersection_function_table<triangle_data, curve_data, primitive_motion METALRT_LIMITS>
318 metalrt_blas_ift_type;
319typedef metal::raytracing::intersector<triangle_data, curve_data, primitive_motion METALRT_LIMITS>
320 metalrt_blas_intersector_type;
321# else
322typedef acceleration_structure<> metalrt_blas_as_type;
323typedef intersection_function_table<triangle_data, curve_data METALRT_LIMITS>
324 metalrt_blas_ift_type;
325typedef metal::raytracing::intersector<triangle_data, curve_data METALRT_LIMITS>
326 metalrt_blas_intersector_type;
327# endif
328
329#endif /* __METALRT__ */
330
331/* texture bindings and sampler setup */
332
333/* TextureParamsMetal is reinterpreted as Texture2DParamsMetal. */
338 texture2d<float, access::sample> tex;
339};
340
341#ifdef __METALRT__
342struct MetalRTBlasWrapper {
343 metalrt_blas_as_type blas;
344};
345#endif
346
347/* Additional Metal-specific resources which aren't encoded in KernelData.
348 * IMPORTANT: If this layout changes, ANCILLARY_SLOT_COUNT and the host-side encoding must change
349 * to match. */
352
353#ifdef __METALRT__
354 metalrt_as_type accel_struct;
355 constant MetalRTBlasWrapper *blas_accel_structs;
356 metalrt_ift_type ift_default;
357 metalrt_ift_type ift_shadow;
358 metalrt_ift_type ift_shadow_all;
359 metalrt_ift_type ift_volume;
360 metalrt_blas_ift_type ift_local;
361 metalrt_ift_type ift_local_mblur;
362 metalrt_blas_ift_type ift_local_single_hit;
363 metalrt_ift_type ift_local_single_hit_mblur;
364#endif
365};
366
367#include "util/half.h"
368#include "util/types.h"
369
383
385 sampler(address::repeat, filter::nearest),
386 sampler(address::clamp_to_edge, filter::nearest),
387 sampler(address::clamp_to_zero, filter::nearest),
388 sampler(address::mirrored_repeat, filter::nearest),
389 sampler(address::repeat, filter::linear),
390 sampler(address::clamp_to_edge, filter::linear),
391 sampler(address::clamp_to_zero, filter::linear),
392 sampler(address::mirrored_repeat, filter::linear),
393};
394
395#ifdef __METAL_GLOBAL_BUILTINS__
396const uint metal_global_id [[thread_position_in_grid]];
397const ushort metal_local_id [[thread_position_in_threadgroup]];
398const ushort metal_local_size [[threads_per_threadgroup]];
399const uint metal_grid_id [[threadgroup_position_in_grid]];
400const uint simdgroup_size [[threads_per_simdgroup]];
401const uint simd_lane_index [[thread_index_in_simdgroup]];
402const uint simd_group_index [[simdgroup_index_in_threadgroup]];
403const uint num_simd_groups [[simdgroups_per_threadgroup]];
404#endif /* __METAL_GLOBAL_BUILTINS__ */
unsigned char uchar
unsigned int uint
unsigned short ushort
unsigned long long int uint64_t
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define ccl_device_forceinline
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline uchar4 make_uchar4(const uchar x, const uchar y, const uchar z, const uchar w)
ccl_device_forceinline uint3 make_uint3(const uint x, const uint y, const uint z)
ccl_device_forceinline uint4 make_uint4(const uint x, const uint y, const uint z, const uint w)
ccl_device_forceinline int3 make_int3(const int x, const int y, const int z)
ccl_device_forceinline uint2 make_uint2(const uint x, const uint y)
ccl_device_forceinline int2 make_int2(const int x, const int y)
constexpr constant array< sampler, SamplerCount > metal_samplers
@ SamplerFilterLinear_AddressMirroredRepeat
@ SamplerFilterNearest_AddressRepeat
@ SamplerFilterLinear_AddressClampEdge
@ SamplerFilterNearest_AddressMirroredRepeat
@ SamplerFilterNearest_AddressClampZero
@ SamplerFilterLinear_AddressClampZero
@ SamplerFilterNearest_AddressClampEdge
@ SamplerFilterLinear_AddressRepeat
@ SamplerCount
ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
VecBase< float, 2 > float2
VecBase< uchar, 4 > uchar4
VecBase< int, 2 > int2
VecBase< float, 4 > float4
VecBase< int, 3 > int3
VecBase< float, 3 > float3
VecBase< uint, 2 > uint2
VecBase< int, 4 > int4
VecBase< uint, 4 > uint4
VecBase< uint, 3 > uint3
#define make_float2
#define make_float4
device TextureParamsMetal * textures
texture2d< float, access::sample > tex