Blender  V3.3
shader_fx.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2018 Blender Foundation. */
3 
8 #include <stdio.h>
9 
10 #include "MEM_guardedalloc.h"
11 
12 #include "BLI_blenlib.h"
13 #include "BLI_math_vector.h"
14 #include "BLI_string_utils.h"
15 #include "BLI_utildefines.h"
16 
17 #include "BLT_translation.h"
18 
19 #include "DNA_gpencil_types.h"
20 #include "DNA_meshdata_types.h"
21 #include "DNA_object_types.h"
22 #include "DNA_scene_types.h"
23 #include "DNA_screen_types.h"
24 #include "DNA_shader_fx_types.h"
25 
26 #include "BKE_gpencil.h"
27 #include "BKE_lib_id.h"
28 #include "BKE_lib_query.h"
29 #include "BKE_object.h"
30 #include "BKE_shader_fx.h"
31 
32 #include "DEG_depsgraph.h"
33 #include "DEG_depsgraph_query.h"
34 
35 #include "FX_shader_types.h"
36 
37 #include "BLO_read_write.h"
38 
40 
41 /* *************************************************** */
42 /* Methods - Evaluation Loops, etc. */
43 
45 {
46  const ShaderFxData *fx;
47  for (fx = ob->shader_fx.first; fx; fx = fx->next) {
49  if (fxi->type == eShaderFxType_GpencilType) {
50  return true;
51  }
52  }
53  return false;
54 }
55 
57 {
58  /* Initialize shaders */
59  shaderfx_type_init(shader_fx_types); /* FX_shader_util.c */
60 }
61 
63 {
66 
67  /* NOTE: this name must be made unique later. */
68  BLI_strncpy(fx->name, DATA_(fxi->name), sizeof(fx->name));
69 
70  fx->type = type;
73  fx->ui_expand_flag = 1; /* Expand only the parent panel by default. */
74 
77  }
78 
79  if (fxi->initData) {
80  fxi->initData(fx);
81  }
82 
83  return fx;
84 }
85 
86 static void shaderfx_free_data_id_us_cb(void *UNUSED(userData),
87  Object *UNUSED(ob),
88  ID **idpoin,
89  int cb_flag)
90 {
91  ID *id = *idpoin;
92  if (id != NULL && (cb_flag & IDWALK_CB_USER) != 0) {
93  id_us_min(id);
94  }
95 }
96 
97 void BKE_shaderfx_free_ex(ShaderFxData *fx, const int flag)
98 {
100 
101  if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
102  if (fxi->foreachIDLink) {
104  }
105  }
106 
107  if (fxi->freeData) {
108  fxi->freeData(fx);
109  }
110  if (fx->error) {
111  MEM_freeN(fx->error);
112  }
113 
114  MEM_freeN(fx);
115 }
116 
118 {
119  BKE_shaderfx_free_ex(fx, 0);
120 }
121 
123 {
124  if (shaders && fx) {
125  const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
126  return BLI_uniquename(
127  shaders, fx, DATA_(fxi->name), '.', offsetof(ShaderFxData, name), sizeof(fx->name));
128  }
129  return false;
130 }
131 
133 {
134  const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
135 
136  return fxi->dependsOnTime && fxi->dependsOnTime(fx);
137 }
138 
140 {
141  /* type unsigned, no need to check < 0 */
142  if (type < NUM_SHADER_FX_TYPES && type > 0 && shader_fx_types[type]->name[0] != '\0') {
143  return shader_fx_types[type];
144  }
145 
146  return NULL;
147 }
148 
150 {
151  return (ID_IS_OVERRIDE_LIBRARY(ob) &&
152  ((shaderfx == NULL) || (shaderfx->flag & eShaderFxFlag_OverrideLibrary_Local) == 0));
153 }
154 
156 {
158 
159  strcpy(r_idname, SHADERFX_TYPE_PANEL_PREFIX);
160  strcat(r_idname, fxi->name);
161 }
162 
164 {
166 }
167 
169 {
170  const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx_src->type);
171 
172  /* fx_dst may have already be fully initialized with some extra allocated data,
173  * we need to free it now to avoid memleak. */
174  if (fxi->freeData) {
175  fxi->freeData(fx_dst);
176  }
177 
178  const size_t data_size = sizeof(ShaderFxData);
179  const char *fx_src_data = ((const char *)fx_src) + data_size;
180  char *fx_dst_data = ((char *)fx_dst) + data_size;
181  BLI_assert(data_size <= (size_t)fxi->struct_size);
182  memcpy(fx_dst_data, fx_src_data, (size_t)fxi->struct_size - data_size);
183 }
184 
185 static void shaderfx_copy_data_id_us_cb(void *UNUSED(userData),
186  Object *UNUSED(ob),
187  ID **idpoin,
188  int cb_flag)
189 {
190  ID *id = *idpoin;
191  if (id != NULL && (cb_flag & IDWALK_CB_USER) != 0) {
192  id_us_plus(id);
193  }
194 }
195 
196 void BKE_shaderfx_copydata_ex(ShaderFxData *fx, ShaderFxData *target, const int flag)
197 {
198  const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
199 
200  target->mode = fx->mode;
201  target->flag = fx->flag;
202  target->ui_expand_flag = fx->ui_expand_flag;
203 
204  if (fxi->copyData) {
205  fxi->copyData(fx, target);
206  }
207 
208  if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
209  if (fxi->foreachIDLink) {
211  }
212  }
213 }
214 
216 {
217  BKE_shaderfx_copydata_ex(fx, target, 0);
218 }
219 
221 {
222  ShaderFxData *fx;
223  ShaderFxData *srcfx;
224 
225  BLI_listbase_clear(dst);
226  BLI_duplicatelist(dst, src);
227 
228  for (srcfx = src->first, fx = dst->first; srcfx && fx; srcfx = srcfx->next, fx = fx->next) {
229  BKE_shaderfx_copydata(srcfx, fx);
230  }
231 }
232 
234 {
235  ShaderFxData *fx = ob->shader_fx.first;
236 
237  for (; fx; fx = fx->next) {
238  if (fx->type == type) {
239  break;
240  }
241  }
242 
243  return fx;
244 }
245 
247 {
248  ShaderFxData *fx = ob->shader_fx.first;
249 
250  for (; fx; fx = fx->next) {
251  const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
252 
253  if (fxi->foreachIDLink) {
254  fxi->foreachIDLink(fx, ob, walk, userData);
255  }
256  }
257 }
258 
260 {
261  return BLI_findstring(&(ob->shader_fx), name, offsetof(ShaderFxData, name));
262 }
263 
265 {
266  if (fxbase == NULL) {
267  return;
268  }
269 
270  LISTBASE_FOREACH (ShaderFxData *, fx, fxbase) {
271  const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
272  if (fxi == NULL) {
273  return;
274  }
275 
276  BLO_write_struct_by_name(writer, fxi->struct_name, fx);
277  }
278 }
279 
281 {
282  BLO_read_list(reader, lb);
283 
284  LISTBASE_FOREACH (ShaderFxData *, fx, lb) {
285  fx->error = NULL;
286 
287  /* if shader disappear, or for upward compatibility */
288  if (NULL == BKE_shaderfx_get_info(fx->type)) {
289  fx->type = eShaderFxType_None;
290  }
291  }
292 }
293 
295 {
297 
298  /* If linking from a library, clear 'local' library override flag. */
299  if (ID_IS_LINKED(ob)) {
300  LISTBASE_FOREACH (ShaderFxData *, fx, &ob->shader_fx) {
302  }
303  }
304 }
@ LIB_ID_CREATE_NO_USER_REFCOUNT
Definition: BKE_lib_id.h:126
void id_us_min(struct ID *id)
Definition: lib_id.c:313
void id_us_plus(struct ID *id)
Definition: lib_id.c:305
@ IDWALK_CB_USER
Definition: BKE_lib_query.h:73
General operations, lookup, etc. for blender objects.
void BKE_object_modifiers_lib_link_common(void *userData, struct Object *ob, struct ID **idpoin, int cb_flag)
Definition: object.cc:5562
@ eShaderFxType_GpencilType
Definition: BKE_shader_fx.h:35
void(* ShaderFxIDWalkFunc)(void *userData, struct Object *ob, struct ID **idpoin, int cb_flag)
Definition: BKE_shader_fx.h:53
@ eShaderFxTypeFlag_EnableInEditmode
Definition: BKE_shader_fx.h:44
#define SHADERFX_TYPE_PANEL_PREFIX
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
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)
Definition: BLI_listbase.h:273
void * BLI_findstring(const struct ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
bool BLI_uniquename(struct ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_len)
Definition: string_utils.c:309
#define UNUSED(x)
void BLO_write_struct_by_name(BlendWriter *writer, const char *struct_name, const void *data_ptr)
Definition: writefile.c:1494
void BLO_read_list(BlendDataReader *reader, struct ListBase *list)
Definition: readfile.c:5172
#define DATA_(msgid)
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:566
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition: DNA_ID.h:588
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
ShaderFxType
@ eShaderFxType_None
@ NUM_SHADER_FX_TYPES
void shaderfx_type_init(ShaderFxTypeInfo *types[])
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
SyclQueue void void * src
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void BKE_shaderfx_copydata(ShaderFxData *fx, ShaderFxData *target)
Definition: shader_fx.c:215
void BKE_shaderfx_copydata_generic(const ShaderFxData *fx_src, ShaderFxData *fx_dst)
Definition: shader_fx.c:168
void BKE_shaderfx_init(void)
Definition: shader_fx.c:56
void BKE_shaderfx_foreach_ID_link(Object *ob, ShaderFxIDWalkFunc walk, void *userData)
Definition: shader_fx.c:246
void BKE_shaderfxType_panel_id(ShaderFxType type, char *r_idname)
Definition: shader_fx.c:155
void BKE_shaderfx_blend_write(BlendWriter *writer, ListBase *fxbase)
Definition: shader_fx.c:264
ShaderFxData * BKE_shaderfx_findby_name(Object *ob, const char *name)
Definition: shader_fx.c:259
const ShaderFxTypeInfo * BKE_shaderfx_get_info(ShaderFxType type)
Definition: shader_fx.c:139
void BKE_shaderfx_panel_expand(ShaderFxData *fx)
Definition: shader_fx.c:163
void BKE_shaderfx_blend_read_data(BlendDataReader *reader, ListBase *lb)
Definition: shader_fx.c:280
static void shaderfx_copy_data_id_us_cb(void *UNUSED(userData), Object *UNUSED(ob), ID **idpoin, int cb_flag)
Definition: shader_fx.c:185
void BKE_shaderfx_copy(ListBase *dst, const ListBase *src)
Definition: shader_fx.c:220
bool BKE_shaderfx_unique_name(ListBase *shaders, ShaderFxData *fx)
Definition: shader_fx.c:122
static void shaderfx_free_data_id_us_cb(void *UNUSED(userData), Object *UNUSED(ob), ID **idpoin, int cb_flag)
Definition: shader_fx.c:86
void BKE_shaderfx_free_ex(ShaderFxData *fx, const int flag)
Definition: shader_fx.c:97
bool BKE_shaderfx_has_gpencil(const Object *ob)
Definition: shader_fx.c:44
void BKE_shaderfx_blend_read_lib(BlendLibReader *reader, Object *ob)
Definition: shader_fx.c:294
bool BKE_shaderfx_depends_ontime(ShaderFxData *fx)
Definition: shader_fx.c:132
bool BKE_shaderfx_is_nonlocal_in_liboverride(const Object *ob, const ShaderFxData *shaderfx)
Definition: shader_fx.c:149
static ShaderFxTypeInfo * shader_fx_types[NUM_SHADER_FX_TYPES]
Definition: shader_fx.c:39
ShaderFxData * BKE_shaderfx_findby_type(Object *ob, ShaderFxType type)
Definition: shader_fx.c:233
ShaderFxData * BKE_shaderfx_new(int type)
Definition: shader_fx.c:62
void BKE_shaderfx_copydata_ex(ShaderFxData *fx, ShaderFxData *target, const int flag)
Definition: shader_fx.c:196
void BKE_shaderfx_free(ShaderFxData *fx)
Definition: shader_fx.c:117
Definition: DNA_ID.h:368
void * first
Definition: DNA_listBase.h:31
ListBase shader_fx
struct ShaderFxData * next
void(* freeData)(struct ShaderFxData *fx)
Definition: BKE_shader_fx.h:94
ShaderFxTypeFlag flags
Definition: BKE_shader_fx.h:75
void(* copyData)(const struct ShaderFxData *fx, struct ShaderFxData *target)
Definition: BKE_shader_fx.h:80
ShaderFxTypeType type
Definition: BKE_shader_fx.h:74
void(* foreachIDLink)(struct ShaderFxData *fx, struct Object *ob, ShaderFxIDWalkFunc walk, void *userData)
bool(* dependsOnTime)(struct ShaderFxData *fx)
char struct_name[32]
Definition: BKE_shader_fx.h:69
void(* initData)(struct ShaderFxData *fx)
Definition: BKE_shader_fx.h:87