Blender V5.3
shader_fx.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdio>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_listbase.hh"
14#include "BLI_string_utf8.hh"
15#include "BLI_string_utils.hh"
16
17#include "BLT_translation.hh"
18
19#include "DNA_object_types.h"
20#include "DNA_screen_types.h"
21#include "DNA_shader_fx_types.h"
22
23#include "BKE_lib_id.hh"
24#include "BKE_lib_query.hh"
25#include "BKE_screen.hh"
26#include "BKE_shader_fx.hh"
27
28#include "FX_shader_types.hh"
29
30#include "BLO_read_write.hh"
31
32namespace blender {
33
35
36/* *************************************************** */
37/* Methods - Evaluation Loops, etc. */
38
40{
41 for (const ShaderFxData &fx : ob->shader_fx) {
43 if (fxi->type == eShaderFxType_GpencilType) {
44 return true;
45 }
46 }
47 return false;
48}
49
51{
52 /* Initialize shaders */
53 shaderfx_type_init(shader_fx_types); /* `FX_shader_util.cc`. */
54}
55
57{
58 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(type);
59 ShaderFxData *fx = static_cast<ShaderFxData *>(
61
62 /* NOTE: this name must be made unique later. */
63 STRNCPY_UTF8(fx->name, DATA_(fxi->name));
64
65 fx->type = type;
68 /* Expand only the parent panel by default. */
70
73 }
74
75 if (fxi->init_data) {
76 fxi->init_data(fx);
77 }
78
79 return fx;
80}
81
82static void shaderfx_free_data_id_us_cb(void * /*user_data*/,
83 Object * /*ob*/,
84 ID **idpoin,
85 const LibraryForeachIDCallbackFlag cb_flag)
86{
87 ID *id = *idpoin;
88 if (id != nullptr && (cb_flag & IDWALK_CB_USER) != 0) {
89 id_us_min(id);
90 }
91}
92
93void BKE_shaderfx_free_ex(ShaderFxData *fx, const int flag)
94{
96
97 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
98 if (fxi->foreach_ID_link) {
99 fxi->foreach_ID_link(fx, nullptr, shaderfx_free_data_id_us_cb, nullptr);
100 }
101 }
102
103 if (fxi->free_data) {
104 fxi->free_data(fx);
105 }
106 if (fx->error) {
107 MEM_delete(fx->error);
108 }
109
110 MEM_delete(fx);
111}
112
117
118void BKE_shaderfx_unique_name(ListBaseT<ShaderFxData> *shaders, ShaderFxData *fx)
119{
120 if (shaders && fx) {
123 shaders, fx, DATA_(fxi->name), '.', offsetof(ShaderFxData, name), sizeof(fx->name));
124 }
125}
126
128{
130
131 return fxi->depends_on_time && fxi->depends_on_time(fx);
132}
133
135{
136 /* type unsigned, no need to check < 0 */
137 if (type < NUM_SHADER_FX_TYPES && type > 0 && shader_fx_types[type]->name[0] != '\0') {
138 return shader_fx_types[type];
139 }
140
141 return nullptr;
142}
143
145{
146 return (ID_IS_OVERRIDE_LIBRARY(ob) &&
147 ((shaderfx == nullptr) || (shaderfx->flag & eShaderFxFlag_OverrideLibrary_Local) == 0));
148}
149
150void BKE_shaderfxType_panel_id(ShaderFxType type, char *r_idname)
151{
152 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(type);
154}
155
160
162{
164
165 /* `fx_dst` may have already be fully initialized with some extra allocated data,
166 * we need to free it now to avoid a memory leak. */
167 if (fxi->free_data) {
168 fxi->free_data(fx_dst);
169 }
170
171 const size_t data_size = sizeof(ShaderFxData);
172 const char *fx_src_data = (reinterpret_cast<const char *>(fx_src)) + data_size;
173 char *fx_dst_data = (reinterpret_cast<char *>(fx_dst)) + data_size;
174 BLI_assert(data_size <= size_t(fxi->struct_size));
175 memcpy(fx_dst_data, fx_src_data, size_t(fxi->struct_size) - data_size);
176}
177
178static void shaderfx_copy_data_id_us_cb(void * /*user_data*/,
179 Object * /*ob*/,
180 ID **idpoin,
181 const LibraryForeachIDCallbackFlag cb_flag)
182{
183 ID *id = *idpoin;
184 if (id != nullptr && (cb_flag & IDWALK_CB_USER) != 0) {
185 id_us_plus(id);
186 }
187}
188
189void BKE_shaderfx_copydata_ex(ShaderFxData *fx, ShaderFxData *target, const int flag)
190{
192
193 target->mode = fx->mode;
194 target->flag = fx->flag;
195 target->ui_expand_flag = fx->ui_expand_flag;
196
197 if (fxi->copy_data) {
198 fxi->copy_data(fx, target);
199 }
200
201 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
202 if (fxi->foreach_ID_link) {
203 fxi->foreach_ID_link(target, nullptr, shaderfx_copy_data_id_us_cb, nullptr);
204 }
205 }
206}
207
209{
210 BKE_shaderfx_copydata_ex(fx, target, 0);
211}
212
213void BKE_shaderfx_copy(ListBaseT<ShaderFxData> *dst, const ListBaseT<ShaderFxData> *src)
214{
215 ShaderFxData *fx;
216 ShaderFxData *srcfx;
217
218 dst->clear_no_delete();
219 BLI_duplicatelist(dst, src);
220
221 for (srcfx = src->first(), fx = dst->first(); srcfx && fx; srcfx = srcfx->next, fx = fx->next) {
222 BKE_shaderfx_copydata(srcfx, fx);
223 }
224}
225
227{
228 ShaderFxData *fx = ob->shader_fx.first();
229
230 for (; fx; fx = fx->next) {
231 if (fx->type == type) {
232 break;
233 }
234 }
235
236 return fx;
237}
238
240{
241 ShaderFxData *fx = ob->shader_fx.first();
242
243 for (; fx; fx = fx->next) {
245
246 if (fxi->foreach_ID_link) {
247 fxi->foreach_ID_link(fx, ob, walk, user_data);
248 }
249 }
250}
251
253{
254 return static_cast<ShaderFxData *>(
255 BLI_findstring(&(ob->shader_fx), name, offsetof(ShaderFxData, name)));
256}
257
258void BKE_shaderfx_blend_write(BlendWriter *writer, ListBaseT<ShaderFxData> *fxbase)
259{
260 if (fxbase == nullptr) {
261 return;
262 }
263
264 for (ShaderFxData &fx : *fxbase) {
266 if (fxi == nullptr) {
267 return;
268 }
269
270 writer->write_struct_by_name(fxi->struct_name, &fx);
271 }
272}
273
274void BKE_shaderfx_blend_read_data(BlendDataReader *reader, ListBaseT<ShaderFxData> *lb, Object *ob)
275{
278 return fxi ? size_t(fxi->struct_size) : sizeof(ShaderFxData);
279 });
280
281 for (ShaderFxData &fx : lb->items_mutable()) {
282 fx.error = nullptr;
283
284 /* Remove if shader disappeared, for upward compatibility. There is no type info for the None
285 * type, so this can't be used. */
286 if (BKE_shaderfx_get_info(ShaderFxType(fx.type)) == nullptr) {
287 BLI_freelinkN(lb, &fx);
288 continue;
289 }
290
291 /* If linking from a library, clear 'local' library override flag. */
292 if (ID_IS_LINKED(ob)) {
294 }
295 }
296}
297
298} // namespace blender
#define BKE_ST_MAXNAME
Definition BKE_screen.hh:91
#define SHADERFX_TYPE_PANEL_PREFIX
#define STRNCPY_UTF8(dst, src)
#define BLI_string_join(...)
#define DATA_(msgid)
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define offsetof(t, d)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ UI_PANEL_DATA_EXPAND_ROOT
void BKE_shaderfx_foreach_ID_link(Object *ob, ShaderFxIDWalkFunc walk, void *user_data)
Definition shader_fx.cc:239
static void shaderfx_free_data_id_us_cb(void *, Object *, ID **idpoin, const LibraryForeachIDCallbackFlag cb_flag)
Definition shader_fx.cc:82
ShaderFxData * BKE_shaderfx_new(ShaderFxType type)
Definition shader_fx.cc:56
void id_us_plus(ID *id)
Definition lib_id.cc:433
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
void BKE_shaderfx_blend_write(BlendWriter *writer, ListBaseT< ShaderFxData > *fxbase)
Definition shader_fx.cc:258
void BKE_shaderfx_blend_read_data(BlendDataReader *reader, ListBaseT< ShaderFxData > *lb, Object *ob)
Definition shader_fx.cc:274
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
ShaderFxData * BKE_shaderfx_findby_type(Object *ob, ShaderFxType type)
Definition shader_fx.cc:226
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void BKE_shaderfx_init()
Definition shader_fx.cc:50
@ eShaderFxFlag_OverrideLibrary_Local
bool BKE_shaderfx_depends_ontime(ShaderFxData *fx)
Definition shader_fx.cc:127
void BKE_shaderfx_copydata_ex(ShaderFxData *fx, ShaderFxData *target, int flag)
Definition shader_fx.cc:189
void BLI_uniquename(const ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
void BKE_shaderfx_unique_name(ListBaseT< ShaderFxData > *shaders, ShaderFxData *fx)
Definition shader_fx.cc:118
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool BKE_shaderfx_has_gpencil(const Object *ob)
Definition shader_fx.cc:39
void BLO_read_struct_list_with_type_size(BlendDataReader *reader, size_t base_size, ListBase *list, FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:6591
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
@ eShaderFxType_GpencilType
static void shaderfx_copy_data_id_us_cb(void *, Object *, ID **idpoin, const LibraryForeachIDCallbackFlag cb_flag)
Definition shader_fx.cc:178
void BKE_shaderfx_free_ex(ShaderFxData *fx, int flag)
Definition shader_fx.cc:93
ShaderFxData * BKE_shaderfx_findby_name(Object *ob, const char *name)
Definition shader_fx.cc:252
void BKE_shaderfx_free(ShaderFxData *fx)
Definition shader_fx.cc:113
bool BKE_shaderfx_is_nonlocal_in_liboverride(const Object *ob, const ShaderFxData *shaderfx)
Definition shader_fx.cc:144
void BKE_shaderfx_panel_expand(ShaderFxData *fx)
Definition shader_fx.cc:156
@ eShaderFxTypeFlag_EnableInEditmode
void BKE_shaderfx_copydata(ShaderFxData *fx, ShaderFxData *target)
Definition shader_fx.cc:208
void shaderfx_type_init(ShaderFxTypeInfo *types[])
static ShaderFxTypeInfo * shader_fx_types[NUM_SHADER_FX_TYPES]
Definition shader_fx.cc:34
void BKE_shaderfx_copydata_generic(const ShaderFxData *fx_src, ShaderFxData *fx_dst)
Definition shader_fx.cc:161
LibraryForeachIDCallbackFlag
void(*) ShaderFxIDWalkFunc(void *user_data, Object *ob, ID **idpoin, LibraryForeachIDCallbackFlag cb_flag)
void id_us_min(ID *id)
Definition lib_id.cc:441
void BKE_shaderfx_copy(ListBaseT< ShaderFxData > *dst, const ListBaseT< ShaderFxData > *src)
Definition shader_fx.cc:213
void BKE_shaderfxType_panel_id(ShaderFxType type, char *r_idname)
Definition shader_fx.cc:150
const ShaderFxTypeInfo * BKE_shaderfx_get_info(ShaderFxType type)
Definition shader_fx.cc:134
void write_struct_by_name(const char *struct_name, const void *data, BlendStructWriterFn fn=nullptr)
ListBaseT< ShaderFxData > shader_fx
struct ShaderFxData * next
void(*) foreach_ID_link(ShaderFxData *fx, Object *ob, ShaderFxIDWalkFunc walk, void *user_data)
void(*) init_data(ShaderFxData *fx)
bool(*) depends_on_time(ShaderFxData *fx)
void(*) free_data(ShaderFxData *fx)
void(*) copy_data(const ShaderFxData *fx, ShaderFxData *target)