1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2018, Blender Foundation
17 * This is a new part of Blender
18 */
19
20 /** \file
21 * \ingroup bke
22 */
23
24 #include <stdio.h>
25
26 #include "MEM_guardedalloc.h"
27
28 #include "BLI_blenlib.h"
29 #include "BLI_math_vector.h"
30 #include "BLI_string_utils.h"
31 #include "BLI_utildefines.h"
32
33 #include "BLT_translation.h"
34
35 #include "DNA_gpencil_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_shader_fx_types.h"
40
41 #include "BKE_gpencil.h"
42 #include "BKE_lib_id.h"
43 #include "BKE_lib_query.h"
44 #include "BKE_object.h"
45 #include "BKE_shader_fx.h"
46
47 #include "DEG_depsgraph.h"
48 #include "DEG_depsgraph_query.h"
49
50 #include "FX_shader_types.h"
51
52 static ShaderFxTypeInfo *shader_fx_types[NUM_SHADER_FX_TYPES] = {NULL};
53
54 /* *************************************************** */
55 /* Methods - Evaluation Loops, etc. */
56
57 /* check if exist grease pencil effects */
BKE_shaderfx_has_gpencil(Object * ob)58 bool BKE_shaderfx_has_gpencil(Object *ob)
59 {
60 ShaderFxData *fx;
61 for (fx = ob->shader_fx.first; fx; fx = fx->next) {
62 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
63 if (fxi->type == eShaderFxType_GpencilType) {
64 return true;
65 }
66 }
67 return false;
68 }
69
BKE_shaderfx_init(void)70 void BKE_shaderfx_init(void)
71 {
72 /* Initialize shaders */
73 shaderfx_type_init(shader_fx_types); /* FX_shader_util.c */
74 }
75
BKE_shaderfx_new(int type)76 ShaderFxData *BKE_shaderfx_new(int type)
77 {
78 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(type);
79 ShaderFxData *fx = MEM_callocN(fxi->struct_size, fxi->struct_name);
80
81 /* note, this name must be made unique later */
82 BLI_strncpy(fx->name, DATA_(fxi->name), sizeof(fx->name));
83
84 fx->type = type;
85 fx->mode = eShaderFxMode_Realtime | eShaderFxMode_Render;
86 fx->flag = eShaderFxFlag_OverrideLibrary_Local;
87 fx->ui_expand_flag = 1; /* Expand only the parent panel by default. */
88
89 if (fxi->flags & eShaderFxTypeFlag_EnableInEditmode) {
90 fx->mode |= eShaderFxMode_Editmode;
91 }
92
93 if (fxi->initData) {
94 fxi->initData(fx);
95 }
96
97 return fx;
98 }
99
shaderfx_free_data_id_us_cb(void * UNUSED (userData),Object * UNUSED (ob),ID ** idpoin,int cb_flag)100 static void shaderfx_free_data_id_us_cb(void *UNUSED(userData),
101 Object *UNUSED(ob),
102 ID **idpoin,
103 int cb_flag)
104 {
105 ID *id = *idpoin;
106 if (id != NULL && (cb_flag & IDWALK_CB_USER) != 0) {
107 id_us_min(id);
108 }
109 }
110
BKE_shaderfx_free_ex(ShaderFxData * fx,const int flag)111 void BKE_shaderfx_free_ex(ShaderFxData *fx, const int flag)
112 {
113 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
114
115 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
116 if (fxi->foreachIDLink) {
117 fxi->foreachIDLink(fx, NULL, shaderfx_free_data_id_us_cb, NULL);
118 }
119 }
120
121 if (fxi->freeData) {
122 fxi->freeData(fx);
123 }
124 if (fx->error) {
125 MEM_freeN(fx->error);
126 }
127
128 MEM_freeN(fx);
129 }
130
BKE_shaderfx_free(ShaderFxData * fx)131 void BKE_shaderfx_free(ShaderFxData *fx)
132 {
133 BKE_shaderfx_free_ex(fx, 0);
134 }
135
136 /* check unique name */
BKE_shaderfx_unique_name(ListBase * shaders,ShaderFxData * fx)137 bool BKE_shaderfx_unique_name(ListBase *shaders, ShaderFxData *fx)
138 {
139 if (shaders && fx) {
140 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
141 return BLI_uniquename(
142 shaders, fx, DATA_(fxi->name), '.', offsetof(ShaderFxData, name), sizeof(fx->name));
143 }
144 return false;
145 }
146
BKE_shaderfx_depends_ontime(ShaderFxData * fx)147 bool BKE_shaderfx_depends_ontime(ShaderFxData *fx)
148 {
149 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
150
151 return fxi->dependsOnTime && fxi->dependsOnTime(fx);
152 }
153
BKE_shaderfx_get_info(ShaderFxType type)154 const ShaderFxTypeInfo *BKE_shaderfx_get_info(ShaderFxType type)
155 {
156 /* type unsigned, no need to check < 0 */
157 if (type < NUM_SHADER_FX_TYPES && type > 0 && shader_fx_types[type]->name[0] != '\0') {
158 return shader_fx_types[type];
159 }
160
161 return NULL;
162 }
163
164 /**
165 * Get an effect's panel type, which was defined in the #panelRegister callback.
166 *
167 * \note ShaderFx panel types are assumed to be named with the struct name field concatenated to
168 * the defined prefix.
169 */
BKE_shaderfxType_panel_id(ShaderFxType type,char * r_idname)170 void BKE_shaderfxType_panel_id(ShaderFxType type, char *r_idname)
171 {
172 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(type);
173
174 strcpy(r_idname, SHADERFX_TYPE_PANEL_PREFIX);
175 strcat(r_idname, fxi->name);
176 }
177
BKE_shaderfx_copydata_generic(const ShaderFxData * fx_src,ShaderFxData * fx_dst)178 void BKE_shaderfx_copydata_generic(const ShaderFxData *fx_src, ShaderFxData *fx_dst)
179 {
180 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx_src->type);
181
182 /* fx_dst may have already be fully initialized with some extra allocated data,
183 * we need to free it now to avoid memleak. */
184 if (fxi->freeData) {
185 fxi->freeData(fx_dst);
186 }
187
188 const size_t data_size = sizeof(ShaderFxData);
189 const char *fx_src_data = ((const char *)fx_src) + data_size;
190 char *fx_dst_data = ((char *)fx_dst) + data_size;
191 BLI_assert(data_size <= (size_t)fxi->struct_size);
192 memcpy(fx_dst_data, fx_src_data, (size_t)fxi->struct_size - data_size);
193 }
194
shaderfx_copy_data_id_us_cb(void * UNUSED (userData),Object * UNUSED (ob),ID ** idpoin,int cb_flag)195 static void shaderfx_copy_data_id_us_cb(void *UNUSED(userData),
196 Object *UNUSED(ob),
197 ID **idpoin,
198 int cb_flag)
199 {
200 ID *id = *idpoin;
201 if (id != NULL && (cb_flag & IDWALK_CB_USER) != 0) {
202 id_us_plus(id);
203 }
204 }
205
BKE_shaderfx_copydata_ex(ShaderFxData * fx,ShaderFxData * target,const int flag)206 void BKE_shaderfx_copydata_ex(ShaderFxData *fx, ShaderFxData *target, const int flag)
207 {
208 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
209
210 target->mode = fx->mode;
211 target->flag = fx->flag;
212 target->ui_expand_flag = fx->ui_expand_flag;
213
214 if (fxi->copyData) {
215 fxi->copyData(fx, target);
216 }
217
218 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
219 if (fxi->foreachIDLink) {
220 fxi->foreachIDLink(target, NULL, shaderfx_copy_data_id_us_cb, NULL);
221 }
222 }
223 }
224
BKE_shaderfx_copydata(ShaderFxData * fx,ShaderFxData * target)225 void BKE_shaderfx_copydata(ShaderFxData *fx, ShaderFxData *target)
226 {
227 BKE_shaderfx_copydata_ex(fx, target, 0);
228 }
229
BKE_shaderfx_copy(ListBase * dst,const ListBase * src)230 void BKE_shaderfx_copy(ListBase *dst, const ListBase *src)
231 {
232 ShaderFxData *fx;
233 ShaderFxData *srcfx;
234
235 BLI_listbase_clear(dst);
236 BLI_duplicatelist(dst, src);
237
238 for (srcfx = src->first, fx = dst->first; srcfx && fx; srcfx = srcfx->next, fx = fx->next) {
239 BKE_shaderfx_copydata(srcfx, fx);
240 }
241 }
242
BKE_shaderfx_findby_type(Object * ob,ShaderFxType type)243 ShaderFxData *BKE_shaderfx_findby_type(Object *ob, ShaderFxType type)
244 {
245 ShaderFxData *fx = ob->shader_fx.first;
246
247 for (; fx; fx = fx->next) {
248 if (fx->type == type) {
249 break;
250 }
251 }
252
253 return fx;
254 }
255
BKE_shaderfx_foreach_ID_link(Object * ob,ShaderFxIDWalkFunc walk,void * userData)256 void BKE_shaderfx_foreach_ID_link(Object *ob, ShaderFxIDWalkFunc walk, void *userData)
257 {
258 ShaderFxData *fx = ob->shader_fx.first;
259
260 for (; fx; fx = fx->next) {
261 const ShaderFxTypeInfo *fxi = BKE_shaderfx_get_info(fx->type);
262
263 if (fxi->foreachIDLink) {
264 fxi->foreachIDLink(fx, ob, walk, userData);
265 }
266 }
267 }
268
BKE_shaderfx_findby_name(Object * ob,const char * name)269 ShaderFxData *BKE_shaderfx_findby_name(Object *ob, const char *name)
270 {
271 return BLI_findstring(&(ob->shader_fx), name, offsetof(ShaderFxData, name));
272 }
273