Blender V4.5
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_int4.h"
10
12
13#ifndef __KERNEL_NATIVE_VECTOR_TYPES__
14# ifdef __KERNEL_ONEAPI__
15/* Define float3 as packed for oneAPI. */
16struct float3
17# else
18struct ccl_try_align(16) float3
19# endif
20{
21# ifdef __KERNEL_GPU__
22 /* Compact structure for GPU. */
23 float x, y, z;
24# else
25 /* SIMD aligned structure for CPU. */
26# ifdef __KERNEL_SSE__
27 union {
28 __m128 m128;
29 struct {
30 float x, y, z, w;
31 };
32 };
33# else
34 float x, y, z, w;
35# endif
36# endif
37
38# ifdef __KERNEL_SSE__
39 /* Convenient constructors and operators for SIMD, otherwise default is enough. */
40 __forceinline float3() = default;
41 __forceinline float3(const float3 &a) = default;
42 __forceinline explicit float3(const __m128 &a) : m128(a) {}
43
44 __forceinline operator const __m128 &() const
45 {
46 return m128;
47 }
48 __forceinline operator __m128 &()
49 {
50 return m128;
51 }
52
54 {
55 m128 = a.m128;
56 return *this;
57 }
58# endif
59
60# ifndef __KERNEL_GPU__
61 __forceinline float operator[](int i) const
62 {
63 util_assert(i >= 0);
64 util_assert(i < 3);
65 return *(&x + i);
66 }
67 __forceinline float &operator[](int i)
68 {
69 util_assert(i >= 0);
70 util_assert(i < 3);
71 return *(&x + i);
72 }
73# endif
74};
75
76ccl_device_inline float3 make_float3(const float x, const float y, float z)
77{
78# if defined(__KERNEL_GPU__)
79 return {x, y, z};
80# elif defined(__KERNEL_SSE__)
81 return float3(_mm_set_ps(0.0f, z, y, x));
82# else
83 return {x, y, z, 0.0f};
84# endif
85}
86
87#endif /* __KERNEL_NATIVE_VECTOR_TYPES__ */
88
90{
91#if defined(__KERNEL_GPU__)
92 return make_float3(f, f, f);
93#elif defined(__KERNEL_SSE__)
94 return float3(_mm_set1_ps(f));
95#else
96 return {f, f, f, f};
97#endif
98}
99
101{
102 return make_float3(a.x, a.y, 0.0f);
103}
104
106{
107 return make_float3(a.x, a.y, b);
108}
109
110ccl_device_inline void print_float3(const ccl_private char *label, const float3 a)
111{
112#ifdef __KERNEL_PRINTF__
113 printf("%s: %.8f %.8f %.8f\n", label, (double)a.x, (double)a.y, (double)a.z);
114#else
115 (void)label;
116 (void)a;
117#endif
118}
119
121{
122 return make_float2(a.x, a.y);
123}
124
126{
127#if defined(__KERNEL_GPU__)
128 return make_int4((int)f.x, (int)f.y, (int)f.z, 0);
129#elif defined(__KERNEL_SSE__)
130 return int4(_mm_cvtps_epi32(f.m128));
131#else
132 return make_int4((int)f.x, (int)f.y, (int)f.z, (int)f.w);
133#endif
134}
135
136/* Packed float3
137 *
138 * Smaller float3 for storage. For math operations this must be converted to float3, so that on the
139 * CPU SIMD instructions can be used. */
140
141#if defined(__KERNEL_METAL__)
142/* Metal has native packed_float3. */
143#elif defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__) || defined(__KERNEL_ONEAPI__)
144/* CUDA, HIP and oneAPI float3 are already packed. */
145using packed_float3 = float3;
146#else
149
151
153 {
154 return make_float3(x, y, z);
155 }
156
158 {
159 x = a.x;
160 y = a.y;
161 z = a.z;
162 return *this;
163 }
164
165 float x, y, z;
166};
167#endif
168
169static_assert(sizeof(packed_float3) == 12, "packed_float3 expected to be exactly 12 bytes");
170
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
VecBase< float, 3 > float3
VecBase< int, 4 > int4
#define printf(...)
float x
float y
float z
Definition sky_float3.h:27
float3()=default
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
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 float3 make_float3(const float x, const float y, float z)
ccl_device_inline float2 make_float2(const float3 a)