Blender V5.3
attribute_convert.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "scene/attribute.h"
8
9#include "util/color.h"
10#include "util/param.h"
11#include "util/types.h"
12
13#include "BKE_attribute.hh"
14
15#include "BLI_color_types.hh"
18
20
21template<typename BlenderT> struct AttributeConverter {
22 using CyclesT = void;
23};
24
25template<> struct AttributeConverter<float> {
26 using CyclesT = float;
27 static constexpr auto type_desc = TypeFloat;
28 static constexpr bool layout_compatible = true;
29 static CyclesT convert(const float &value)
30 {
31 return value;
32 }
33};
34template<> struct AttributeConverter<int> {
35 using CyclesT = float;
36 static constexpr auto type_desc = TypeFloat;
37 static constexpr bool layout_compatible = false;
38 static CyclesT convert(const int &value)
39 {
40 return float(value);
41 }
42};
43template<> struct AttributeConverter<blender::float2> {
44 using CyclesT = float2;
45 static constexpr auto type_desc = TypeFloat2;
46 static constexpr bool layout_compatible = true;
47 static CyclesT convert(const blender::float2 &value)
48 {
49 return make_float2(value[0], value[1]);
50 }
51};
52template<> struct AttributeConverter<blender::float3> {
54 static constexpr auto type_desc = TypeVector;
55 static constexpr bool layout_compatible = true;
56 static CyclesT convert(const blender::float3 &value)
57 {
58 return packed_float3(make_float3(value[0], value[1], value[2]));
59 }
60};
61template<> struct AttributeConverter<blender::float4> {
62 using CyclesT = float4;
63 static constexpr auto type_desc = TypeFloat4;
64 /* Allocation alignment is not compatible with Cycles */
65 static constexpr bool layout_compatible = false;
66 static CyclesT convert(const blender::float4 &value)
67 {
68 return make_float4(value[0], value[1], value[2], value[3]);
69 }
70};
71template<> struct AttributeConverter<blender::ColorGeometry4f> {
72 using CyclesT = float4;
73 static constexpr auto type_desc = TypeRGBA;
74 /* Allocation alignment is not compatible with Cycles */
75 static constexpr bool layout_compatible = false;
77 {
78 return make_float4(value[0], value[1], value[2], value[3]);
79 }
80};
81template<> struct AttributeConverter<blender::ColorGeometry4b> {
82 using CyclesT = float4;
83 static constexpr auto type_desc = TypeRGBA;
84 static constexpr bool layout_compatible = false;
86 {
88 byte_to_float(value[1]),
89 byte_to_float(value[2]),
90 byte_to_float(value[3])));
91 }
92};
93template<> struct AttributeConverter<bool> {
94 using CyclesT = float;
95 static constexpr auto type_desc = TypeFloat;
96 static constexpr bool layout_compatible = false;
97 static CyclesT convert(const bool &value)
98 {
99 return float(value);
100 }
101};
102template<> struct AttributeConverter<int8_t> {
103 using CyclesT = float;
104 static constexpr auto type_desc = TypeFloat;
105 static constexpr bool layout_compatible = false;
106 static CyclesT convert(const int8_t &value)
107 {
108 return float(value);
109 }
110};
111template<> struct AttributeConverter<blender::math::Quaternion> {
113 static constexpr auto type_desc = TypeQuaternion;
114 static constexpr bool layout_compatible = true;
116 {
117 return make_quaternion(value.w, value.x, value.y, value.z);
118 }
119};
120
121/* Add a standard attribute from a Blender attribute reader, sharing the buffer
122 * with Blender when possible. */
123template<typename BlenderT>
125 const AttributeStandard std,
127 const int size)
128{
129 if (!b_reader) {
130 return false;
131 }
132 using Converter = AttributeConverter<BlenderT>;
133 using CyclesT = typename Converter::CyclesT;
134
135 /* Try implicit sharing. */
136 if constexpr (Converter::layout_compatible) {
137 const blender::CommonVArrayInfo info = b_reader.varray.common_info();
139 attributes.add_shared(std, ustring(), info.data, size, b_reader.sharing_info);
140 return true;
141 }
142 }
143
144 /* Otherwise allocate and copy. */
145 Attribute *attr = attributes.add(std);
146 CyclesT *data = attr->data_for_write<CyclesT>();
147 const blender::VArraySpan<BlenderT> src = *b_reader;
148 for (const int i : src.index_range()) {
149 data[i] = Converter::convert(src[i]);
150 }
151 return true;
152}
153
154/* Same as sync_attribute_from_blender, but for a single motion step of an
155 * existing attribute. */
156template<typename BlenderT>
158 Attribute &attr,
159 const int motion_step,
161{
162 if (!b_reader) {
163 return false;
164 }
165 using Converter = AttributeConverter<BlenderT>;
166 using CyclesT = typename Converter::CyclesT;
167
168 /* Try implicit sharing. */
169 if constexpr (Converter::layout_compatible) {
170 const blender::CommonVArrayInfo info = b_reader.varray.common_info();
173 motion_step, info.data, b_reader.varray.size(), b_reader.sharing_info);
174 return true;
175 }
176 }
177
178 /* Otherwise allocate and copy. */
179 CyclesT *data = attr.data_for_write<CyclesT>(motion_step);
180 const blender::VArraySpan<BlenderT> src = *b_reader;
181 for (const int i : src.index_range()) {
182 data[i] = Converter::convert(src[i]);
183 }
184 return true;
185}
186
bool sync_attribute_from_blender(AttributeSet &attributes, const AttributeStandard std, const blender::bke::AttributeReader< BlenderT > &b_reader, const int size)
bool sync_attribute_motion_step_from_blender(Attribute &attr, const int motion_step, const blender::bke::AttributeReader< BlenderT > &b_reader)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
Attribute * add_shared(ustring name, const TypeDesc type, AttributeElement element, const void *data, int size, ImplicitSharingInfo sharing_info)
T * data_for_write(const int step=0)
void set_motion_step_shared(int step, const void *data, int new_size, ImplicitSharingInfo sharing_info)
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
Definition color.h:335
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
AttributeStandard
ccl_device_inline float byte_to_float(const uchar val)
Definition math_base.h:1008
QuaternionBase< float > Quaternion
BMesh Iterators.
VecBase< float, 4 > float4
VecBase< float, 2 > float2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
#define make_float2
#define make_float4
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static CyclesT convert(const blender::ColorGeometry4b &value)
static CyclesT convert(const blender::ColorGeometry4f &value)
static CyclesT convert(const blender::float2 &value)
static CyclesT convert(const blender::float3 &value)
static CyclesT convert(const blender::float4 &value)
static CyclesT convert(const blender::math::Quaternion &value)
static constexpr auto type_desc
static constexpr bool layout_compatible
static CyclesT convert(const bool &value)
static constexpr bool layout_compatible
static CyclesT convert(const float &value)
static constexpr auto type_desc
static CyclesT convert(const int8_t &value)
static constexpr auto type_desc
static constexpr bool layout_compatible
static constexpr bool layout_compatible
static constexpr auto type_desc
static CyclesT convert(const int &value)
const ImplicitSharingInfo * sharing_info
ccl_device_inline Quaternion make_quaternion(const float w, const float x, const float y, const float z)