Eevee: Stop depsgraph update callback form allocating unneeded memory
[blender.git] / source / blender / draw / engines / eevee / eevee_data.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file eevee_data.c
23  *  \ingroup draw_engine
24  *
25  * All specific data handler for Objects, Lights, ViewLayers, ...
26  */
27
28 #include "DRW_render.h"
29
30 #include "eevee_private.h"
31
32 static void eevee_view_layer_data_free(void *storage)
33 {
34         EEVEE_ViewLayerData *sldata = (EEVEE_ViewLayerData *)storage;
35
36         /* Lights */
37         MEM_SAFE_FREE(sldata->lamps);
38         DRW_UBO_FREE_SAFE(sldata->light_ubo);
39         DRW_UBO_FREE_SAFE(sldata->shadow_ubo);
40         DRW_UBO_FREE_SAFE(sldata->shadow_render_ubo);
41         DRW_FRAMEBUFFER_FREE_SAFE(sldata->shadow_target_fb);
42         DRW_FRAMEBUFFER_FREE_SAFE(sldata->shadow_store_fb);
43         DRW_TEXTURE_FREE_SAFE(sldata->shadow_cube_target);
44         DRW_TEXTURE_FREE_SAFE(sldata->shadow_cube_blur);
45         DRW_TEXTURE_FREE_SAFE(sldata->shadow_cascade_target);
46         DRW_TEXTURE_FREE_SAFE(sldata->shadow_cascade_blur);
47         DRW_TEXTURE_FREE_SAFE(sldata->shadow_pool);
48         BLI_freelistN(&sldata->shadow_casters);
49
50         /* Probes */
51         MEM_SAFE_FREE(sldata->probes);
52         DRW_UBO_FREE_SAFE(sldata->probe_ubo);
53         DRW_UBO_FREE_SAFE(sldata->grid_ubo);
54         DRW_UBO_FREE_SAFE(sldata->planar_ubo);
55         DRW_FRAMEBUFFER_FREE_SAFE(sldata->probe_fb);
56         DRW_FRAMEBUFFER_FREE_SAFE(sldata->probe_filter_fb);
57         DRW_TEXTURE_FREE_SAFE(sldata->probe_rt);
58         DRW_TEXTURE_FREE_SAFE(sldata->probe_pool);
59         DRW_TEXTURE_FREE_SAFE(sldata->irradiance_pool);
60         DRW_TEXTURE_FREE_SAFE(sldata->irradiance_rt);
61
62         /* Volumetrics */
63         MEM_SAFE_FREE(sldata->volumetrics);
64 }
65
66 static void eevee_lamp_data_free(void *storage)
67 {
68         EEVEE_LampEngineData *led = (EEVEE_LampEngineData *)storage;
69
70         MEM_SAFE_FREE(led->storage);
71         BLI_freelistN(&led->shadow_caster_list);
72 }
73
74 static void eevee_lightprobe_data_free(void *storage)
75 {
76         EEVEE_LightProbeEngineData *ped = (EEVEE_LightProbeEngineData *)storage;
77
78         BLI_freelistN(&ped->captured_object_list);
79 }
80
81 EEVEE_ViewLayerData *EEVEE_view_layer_data_get(void)
82 {
83         return (EEVEE_ViewLayerData *)DRW_view_layer_engine_data_get(
84                 &draw_engine_eevee_type);
85 }
86
87 EEVEE_ViewLayerData *EEVEE_view_layer_data_ensure(void)
88 {
89         EEVEE_ViewLayerData **sldata = (EEVEE_ViewLayerData **)DRW_view_layer_engine_data_ensure(
90                 &draw_engine_eevee_type, &eevee_view_layer_data_free);
91
92         if (*sldata == NULL) {
93                 *sldata = MEM_callocN(sizeof(**sldata), "EEVEE_ViewLayerData");
94         }
95
96         return *sldata;
97 }
98
99 EEVEE_ObjectEngineData *EEVEE_object_data_get(Object *ob)
100 {
101         return (EEVEE_ObjectEngineData *)DRW_object_engine_data_get(
102                 ob, &draw_engine_eevee_type);
103 }
104
105 EEVEE_ObjectEngineData *EEVEE_object_data_ensure(Object *ob)
106 {
107         EEVEE_ObjectEngineData **oedata = (EEVEE_ObjectEngineData **)DRW_object_engine_data_ensure(
108                 ob, &draw_engine_eevee_type, NULL);
109
110         if (*oedata == NULL) {
111                 *oedata = MEM_callocN(sizeof(**oedata), "EEVEE_ObjectEngineData");
112         }
113
114         return *oedata;
115 }
116
117 EEVEE_LightProbeEngineData *EEVEE_lightprobe_data_get(Object *ob)
118 {
119         return (EEVEE_LightProbeEngineData *)DRW_object_engine_data_get(
120                 ob, &draw_engine_eevee_type);
121 }
122
123 EEVEE_LightProbeEngineData *EEVEE_lightprobe_data_ensure(Object *ob)
124 {
125         EEVEE_LightProbeEngineData **pedata = (EEVEE_LightProbeEngineData **)DRW_object_engine_data_ensure(
126                 ob, &draw_engine_eevee_type, &eevee_lightprobe_data_free);
127
128         if (*pedata == NULL) {
129                 *pedata = MEM_callocN(sizeof(**pedata), "EEVEE_LightProbeEngineData");
130                 (*pedata)->need_full_update = true;
131                 (*pedata)->need_update = true;
132         }
133
134         return *pedata;
135 }
136
137 EEVEE_LampEngineData *EEVEE_lamp_data_get(Object *ob)
138 {
139         return (EEVEE_LampEngineData *)DRW_object_engine_data_get(
140                 ob, &draw_engine_eevee_type);
141 }
142
143 EEVEE_LampEngineData *EEVEE_lamp_data_ensure(Object *ob)
144 {
145         EEVEE_LampEngineData **ledata = (EEVEE_LampEngineData **)DRW_object_engine_data_ensure(
146                 ob, &draw_engine_eevee_type, &eevee_lamp_data_free);
147
148         if (*ledata == NULL) {
149                 *ledata = MEM_callocN(sizeof(**ledata), "EEVEE_LampEngineData");
150                 (*ledata)->need_update = true;
151         }
152
153         return *ledata;
154 }