Blender V4.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
9#include <cstdio>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_blenlib.h"
14#include "BLI_string_utils.hh"
15#include "BLI_utildefines.h"
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.h"
27
28#include "FX_shader_types.h"
29
30#include "BLO_read_write.hh"
31
33
34/* *************************************************** */
35/* Methods - Evaluation Loops, etc. */
36
38{
39 LISTBASE_FOREACH (const ShaderFxData *, fx, &ob->shader_fx) {
41 if (fxi->type == eShaderFxType_GpencilType) {
42 return true;
43 }
44 }
45 return false;
46}
47
49{
50 /* Initialize shaders */
51 shaderfx_type_init(shader_fx_types); /* `FX_shader_util.cc`. */
52}
53
55{
57 ShaderFxData *fx = static_cast<ShaderFxData *>(MEM_callocN(fxi->struct_size, fxi->struct_name));
58
59 /* NOTE: this name must be made unique later. */
60 STRNCPY_UTF8(fx->name, DATA_(fxi->name));
61
62 fx->type = type;
65 /* Expand only the parent panel by default. */
67
70 }
71
72 if (fxi->init_data) {
73 fxi->init_data(fx);
74 }
75
76 return fx;
77}
78
79static void shaderfx_free_data_id_us_cb(void * /*user_data*/,
80 Object * /*ob*/,
81 ID **idpoin,
82 int cb_flag)
83{
84 ID *id = *idpoin;
85 if (id != nullptr && (cb_flag & IDWALK_CB_USER) != 0) {
86 id_us_min(id);
87 }
88}
89
91{
93
95 if (fxi->foreach_ID_link) {
96 fxi->foreach_ID_link(fx, nullptr, shaderfx_free_data_id_us_cb, nullptr);
97 }
98 }
99
100 if (fxi->free_data) {
101 fxi->free_data(fx);
102 }
103 if (fx->error) {
104 MEM_freeN(fx->error);
105 }
106
107 MEM_freeN(fx);
108}
109
114
116{
117 if (shaders && fx) {
120 shaders, fx, DATA_(fxi->name), '.', offsetof(ShaderFxData, name), sizeof(fx->name));
121 }
122}
123
125{
127
128 return fxi->depends_on_time && fxi->depends_on_time(fx);
129}
130
132{
133 /* type unsigned, no need to check < 0 */
134 if (type < NUM_SHADER_FX_TYPES && type > 0 && shader_fx_types[type]->name[0] != '\0') {
135 return shader_fx_types[type];
136 }
137
138 return nullptr;
139}
140
142{
143 return (ID_IS_OVERRIDE_LIBRARY(ob) &&
144 ((shaderfx == nullptr) || (shaderfx->flag & eShaderFxFlag_OverrideLibrary_Local) == 0));
145}
146
147void BKE_shaderfxType_panel_id(ShaderFxType type, char *r_idname)
148{
149 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(type);
151}
152
157
159{
161
162 /* `fx_dst` may have already be fully initialized with some extra allocated data,
163 * we need to free it now to avoid a memory leak. */
164 if (fxi->free_data) {
165 fxi->free_data(fx_dst);
166 }
167
168 const size_t data_size = sizeof(ShaderFxData);
169 const char *fx_src_data = ((const char *)fx_src) + data_size;
170 char *fx_dst_data = ((char *)fx_dst) + data_size;
171 BLI_assert(data_size <= size_t(fxi->struct_size));
172 memcpy(fx_dst_data, fx_src_data, size_t(fxi->struct_size) - data_size);
173}
174
175static void shaderfx_copy_data_id_us_cb(void * /*user_data*/,
176 Object * /*ob*/,
177 ID **idpoin,
178 int cb_flag)
179{
180 ID *id = *idpoin;
181 if (id != nullptr && (cb_flag & IDWALK_CB_USER) != 0) {
182 id_us_plus(id);
183 }
184}
185
187{
189
190 target->mode = fx->mode;
191 target->flag = fx->flag;
192 target->ui_expand_flag = fx->ui_expand_flag;
193
194 if (fxi->copy_data) {
195 fxi->copy_data(fx, target);
196 }
197
199 if (fxi->foreach_ID_link) {
200 fxi->foreach_ID_link(target, nullptr, shaderfx_copy_data_id_us_cb, nullptr);
201 }
202 }
203}
204
206{
207 BKE_shaderfx_copydata_ex(fx, target, 0);
208}
209
210void BKE_shaderfx_copy(ListBase *dst, const ListBase *src)
211{
212 ShaderFxData *fx;
213 ShaderFxData *srcfx;
214
216 BLI_duplicatelist(dst, src);
217
218 for (srcfx = static_cast<ShaderFxData *>(src->first),
219 fx = static_cast<ShaderFxData *>(dst->first);
220 srcfx && fx;
221 srcfx = srcfx->next, fx = fx->next)
222 {
223 BKE_shaderfx_copydata(srcfx, fx);
224 }
225}
226
228{
229 ShaderFxData *fx = static_cast<ShaderFxData *>(ob->shader_fx.first);
230
231 for (; fx; fx = fx->next) {
232 if (fx->type == type) {
233 break;
234 }
235 }
236
237 return fx;
238}
239
241{
242 ShaderFxData *fx = static_cast<ShaderFxData *>(ob->shader_fx.first);
243
244 for (; fx; fx = fx->next) {
246
247 if (fxi->foreach_ID_link) {
248 fxi->foreach_ID_link(fx, ob, walk, user_data);
249 }
250 }
251}
252
254{
255 return static_cast<ShaderFxData *>(
256 BLI_findstring(&(ob->shader_fx), name, offsetof(ShaderFxData, name)));
257}
258
260{
261 if (fxbase == nullptr) {
262 return;
263 }
264
265 LISTBASE_FOREACH (ShaderFxData *, fx, fxbase) {
267 if (fxi == nullptr) {
268 return;
269 }
270
271 BLO_write_struct_by_name(writer, fxi->struct_name, fx);
272 }
273}
274
276{
278
279 LISTBASE_FOREACH (ShaderFxData *, fx, lb) {
280 fx->error = nullptr;
281
282 /* If linking from a library, clear 'local' library override flag. */
283 if (ID_IS_LINKED(ob)) {
284 fx->flag &= ~eShaderFxFlag_OverrideLibrary_Local;
285 }
286
287 /* if shader disappear, or for upward compatibility */
288 if (nullptr == BKE_shaderfx_get_info(ShaderFxType(fx->type))) {
289 fx->type = eShaderFxType_None;
290 }
291 }
292}
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void id_us_plus(ID *id)
Definition lib_id.cc:351
void id_us_min(ID *id)
Definition lib_id.cc:359
@ IDWALK_CB_USER
#define BKE_ST_MAXNAME
Definition BKE_screen.hh:66
@ eShaderFxType_GpencilType
@ eShaderFxTypeFlag_EnableInEditmode
#define SHADERFX_TYPE_PANEL_PREFIX
void(* ShaderFxIDWalkFunc)(void *user_data, struct Object *ob, struct ID **idpoin, int cb_flag)
#define BLI_assert(a)
Definition BLI_assert.h:50
void * BLI_findstring(const struct ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define LISTBASE_FOREACH(type, var, list)
void void void void void void BLI_duplicatelist(struct ListBase *dst, const struct ListBase *src) ATTR_NONNULL(1
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
#define STRNCPY_UTF8(dst, src)
void BLI_uniquename(const struct ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
#define BLI_string_join(...)
void BLO_write_struct_by_name(BlendWriter *writer, const char *struct_name, const void *data_ptr)
#define BLO_read_struct_list(reader, struct_name, list)
#define DATA_(msgid)
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:654
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:683
Object is a sort of wrapper for general info.
@ UI_PANEL_DATA_EXPAND_ROOT
@ eShaderFxFlag_OverrideLibrary_Local
@ eShaderFxMode_Realtime
@ eShaderFxMode_Editmode
@ eShaderFxMode_Render
struct ShaderFxData ShaderFxData
@ eShaderFxType_None
@ NUM_SHADER_FX_TYPES
void shaderfx_type_init(ShaderFxTypeInfo *types[])
Read Guarded memory(de)allocation.
#define offsetof(t, d)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
void BKE_shaderfx_copydata(ShaderFxData *fx, ShaderFxData *target)
Definition shader_fx.cc:205
ShaderFxData * BKE_shaderfx_new(int type)
Definition shader_fx.cc:54
void BKE_shaderfx_copydata_generic(const ShaderFxData *fx_src, ShaderFxData *fx_dst)
Definition shader_fx.cc:158
static void shaderfx_copy_data_id_us_cb(void *, Object *, ID **idpoin, int cb_flag)
Definition shader_fx.cc:175
ShaderFxData * BKE_shaderfx_findby_type(Object *ob, ShaderFxType type)
Definition shader_fx.cc:227
void BKE_shaderfxType_panel_id(ShaderFxType type, char *r_idname)
Definition shader_fx.cc:147
void BKE_shaderfx_blend_write(BlendWriter *writer, ListBase *fxbase)
Definition shader_fx.cc:259
void BKE_shaderfx_panel_expand(ShaderFxData *fx)
Definition shader_fx.cc:153
void BKE_shaderfx_init()
Definition shader_fx.cc:48
void BKE_shaderfx_unique_name(ListBase *shaders, ShaderFxData *fx)
Definition shader_fx.cc:115
void BKE_shaderfx_copy(ListBase *dst, const ListBase *src)
Definition shader_fx.cc:210
void BKE_shaderfx_free_ex(ShaderFxData *fx, const int flag)
Definition shader_fx.cc:90
void BKE_shaderfx_blend_read_data(BlendDataReader *reader, ListBase *lb, Object *ob)
Definition shader_fx.cc:275
void BKE_shaderfx_foreach_ID_link(Object *ob, ShaderFxIDWalkFunc walk, void *user_data)
Definition shader_fx.cc:240
const ShaderFxTypeInfo * BKE_shaderfx_get_info(ShaderFxType type)
Definition shader_fx.cc:131
bool BKE_shaderfx_has_gpencil(const Object *ob)
Definition shader_fx.cc:37
bool BKE_shaderfx_depends_ontime(ShaderFxData *fx)
Definition shader_fx.cc:124
bool BKE_shaderfx_is_nonlocal_in_liboverride(const Object *ob, const ShaderFxData *shaderfx)
Definition shader_fx.cc:141
static ShaderFxTypeInfo * shader_fx_types[NUM_SHADER_FX_TYPES]
Definition shader_fx.cc:32
static void shaderfx_free_data_id_us_cb(void *, Object *, ID **idpoin, int cb_flag)
Definition shader_fx.cc:79
void BKE_shaderfx_copydata_ex(ShaderFxData *fx, ShaderFxData *target, const int flag)
Definition shader_fx.cc:186
void BKE_shaderfx_free(ShaderFxData *fx)
Definition shader_fx.cc:110
ShaderFxData * BKE_shaderfx_findby_name(Object *ob, const char *name)
Definition shader_fx.cc:253
Definition DNA_ID.h:413
void * first
ListBase shader_fx
struct ShaderFxData * next
void(* copy_data)(const struct ShaderFxData *fx, struct ShaderFxData *target)
ShaderFxTypeFlag flags
ShaderFxTypeType type
void(* free_data)(struct ShaderFxData *fx)
void(* init_data)(struct ShaderFxData *fx)
void(* foreach_ID_link)(struct ShaderFxData *fx, struct Object *ob, ShaderFxIDWalkFunc walk, void *user_data)
bool(* depends_on_time)(struct ShaderFxData *fx)
uint8_t flag
Definition wm_window.cc:138