Blender V5.0
types_float3.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#include "util/types_base.h"
8#include "util/types_float2.h"
9#include "util/types_int3.h"
10#include "util/types_int4.h"
11
13
14#ifndef __KERNEL_NATIVE_VECTOR_TYPES__
15# ifdef __KERNEL_ONEAPI__
16/* Define float3 as packed for oneAPI. */
17struct float3
18# else
19struct ccl_try_align(16) float3
20# endif
21{
22# ifdef __KERNEL_GPU__
23 /* Compact structure for GPU. */
24 float x, y, z;
25# else
26 /* SIMD aligned structure for CPU. */
27# ifdef __KERNEL_SSE__
28 union {
29 __m128 m128;
30 struct {
31 float x, y, z, w;
32 };
33 };
34# else
35 float x, y, z, w;
36# endif
37# endif
38
39# ifdef __KERNEL_SSE__
40 /* Convenient constructors and operators for SIMD, otherwise default is enough. */
41 __forceinline float3() = default;
42 __forceinline float3(const float3 &a) = default;
43 __forceinline explicit float3(const __m128 &a) : m128(a) {}
44
45 __forceinline operator const __m128 &() const
46 {
47 return m128;
48 }
49 __forceinline operator __m128 &()
50 {
51 return m128;
52 }
53
55 {
56 m128 = a.m128;
57 return *this;
58 }
59# endif
60
61# ifndef __KERNEL_GPU__
62 __forceinline float operator[](int i) const
63 {
64 util_assert(i >= 0);
65 util_assert(i < 3);
66 return *(&x + i);
67 }
68 __forceinline float &operator[](int i)
69 {
70 util_assert(i >= 0);
71 util_assert(i < 3);
72 return *(&x + i);
73 }
74# endif
75};
76
77ccl_device_inline float3 make_float3(const float x, const float y, float z)
78{
79# if defined(__KERNEL_GPU__)
80 return {x, y, z};
81# elif defined(__KERNEL_SSE__)
82 return float3(_mm_set_ps(0.0f, z, y, x));
83# else
84 return {x, y, z, 0.0f};
85# endif
86}
87
88#endif /* __KERNEL_NATIVE_VECTOR_TYPES__ */
89
91{
92#if defined(__KERNEL_GPU__)
93 return make_float3(f, f, f);
94#elif defined(__KERNEL_SSE__)
95 return float3(_mm_set1_ps(f));
96#else
97 return {f, f, f, f};
98#endif
99}
100
102{
103 return make_float3(a.x, a.y, 0.0f);
104}
105
107{
108 return make_float3(a.x, a.y, b);
109}
110
112{
113#ifdef __KERNEL_SSE__
114 return float3(_mm_cvtepi32_ps(i.m128));
115#else
116 return make_float3((float)i.x, (float)i.y, (float)i.z);
117#endif
118}
119
121{
122 return a;
123}
124
125#if defined __METAL_PRINTF__
126# define print_float3(label, a) \
127 metal::os_log_default.log_debug(label ": %.8f %.8f %.8f", a.x, a.y, a.z)
128#else
129ccl_device_inline void print_float3(const ccl_private char *label, const float3 a)
130{
131# ifdef __KERNEL_PRINTF__
132 printf("%s: %.8f %.8f %.8f\n", label, (double)a.x, (double)a.y, (double)a.z);
133# else
134 (void)label;
135 (void)a;
136# endif
137}
138#endif
139
141{
142 return make_float2(a.x, a.y);
143}
144
146{
147#if defined(__KERNEL_GPU__)
148 return make_int4((int)f.x, (int)f.y, (int)f.z, 0);
149#elif defined(__KERNEL_SSE__)
150 return int4(_mm_cvtps_epi32(f.m128));
151#else
152 return make_int4((int)f.x, (int)f.y, (int)f.z, (int)f.w);
153#endif
154}
155
157{
158#ifdef __KERNEL_SSE__
159 return int3(_mm_cvtps_epi32(f.m128));
160#else
161 return make_int3((int)f.x, (int)f.y, (int)f.z);
162#endif
163}
164
165/* Packed float3
166 *
167 * Smaller float3 for storage. For math operations this must be converted to float3, so that on the
168 * CPU SIMD instructions can be used. */
169
170#if defined(__KERNEL_METAL__)
171/* Metal has native packed_float3. */
172#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__) || defined(__KERNEL_ONEAPI__)
173/* CUDA, HIP and oneAPI float3 are already packed. */
174using packed_float3 = float3;
175#else
178
180
182 {
183 return make_float3(x, y, z);
184 }
185
187 {
188 x = a.x;
189 y = a.y;
190 z = a.z;
191 return *this;
192 }
193
194 float x, y, z;
195};
196#endif
197
198static_assert(sizeof(packed_float3) == 12, "packed_float3 expected to be exactly 12 bytes");
199
btGeneric6DofConstraint & operator=(btGeneric6DofConstraint &other)
SIMD_FORCE_INLINE btVector3 & operator[](int i)
Get a mutable reference to a row of the matrix as a vector.
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 util_assert(statement)
#define ccl_private
#define ccl_device_inline
#define ccl_try_align(...)
#define ccl_device_inline_method
#define __forceinline
#define CCL_NAMESPACE_END
#define printf(...)
VecBase< int, 3 > int3
VecBase< float, 3 > float3
VecBase< int, 4 > int4
#define make_float2
float x
float y
float z
Definition sky_math.h:136
float3()=default
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
ccl_device_inline_method packed_float3(const float3 &a)
ccl_device_inline_method packed_float3 & operator=(const float3 &a)
ccl_device_inline_method packed_float3()=default
i
Definition text_draw.cc:230
ccl_device_inline int4 make_int4(const float3 f)
ccl_device_inline void print_float3(const ccl_private char *label, const float3 a)
ccl_device_inline int3 make_int3(const float3 f)
ccl_device_inline float3 make_float3(const float x, const float y, float z)