Cleanp: use single global for draw manager
[blender.git] / source / blender / draw / engines / eevee / eevee_shaders.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_shaders.c
23  *  \ingroup draw_engine
24  */
25
26 #include "DRW_render.h"
27
28 #include "BLI_string_utils.h"
29
30 #include "MEM_guardedalloc.h"
31
32 #include "GPU_shader.h"
33
34 #include "eevee_private.h"
35
36 static const char *filter_defines = "#define HAMMERSLEY_SIZE " STRINGIFY(HAMMERSLEY_SIZE) "\n"
37 #if defined(IRRADIANCE_SH_L2)
38                                      "#define IRRADIANCE_SH_L2\n"
39 #elif defined(IRRADIANCE_CUBEMAP)
40                                      "#define IRRADIANCE_CUBEMAP\n"
41 #elif defined(IRRADIANCE_HL2)
42                                      "#define IRRADIANCE_HL2\n"
43 #endif
44                                      "#define NOISE_SIZE 64\n";
45
46 static struct {
47         /* Probes */
48         struct GPUShader *probe_default_sh;
49         struct GPUShader *probe_default_studiolight_sh;
50         struct GPUShader *probe_grid_display_sh;
51         struct GPUShader *probe_cube_display_sh;
52         struct GPUShader *probe_planar_display_sh;
53         struct GPUShader *probe_filter_glossy_sh;
54         struct GPUShader *probe_filter_diffuse_sh;
55         struct GPUShader *probe_filter_visibility_sh;
56         struct GPUShader *probe_grid_fill_sh;
57         struct GPUShader *probe_planar_downsample_sh;
58
59         /* Velocity Resolve */
60         struct GPUShader *velocity_resolve_sh;
61
62         /* Temporal Anti Aliasing */
63         struct GPUShader *taa_resolve_sh;
64         struct GPUShader *taa_resolve_reproject_sh;
65
66 } e_data = {NULL}; /* Engine data */
67
68 extern char datatoc_bsdf_common_lib_glsl[];
69 extern char datatoc_bsdf_sampling_lib_glsl[];
70 extern char datatoc_common_uniforms_lib_glsl[];
71 extern char datatoc_common_view_lib_glsl[];
72
73 extern char datatoc_background_vert_glsl[];
74 extern char datatoc_default_world_frag_glsl[];
75 extern char datatoc_lightprobe_geom_glsl[];
76 extern char datatoc_lightprobe_vert_glsl[];
77 extern char datatoc_lightprobe_cube_display_frag_glsl[];
78 extern char datatoc_lightprobe_cube_display_vert_glsl[];
79 extern char datatoc_lightprobe_filter_diffuse_frag_glsl[];
80 extern char datatoc_lightprobe_filter_glossy_frag_glsl[];
81 extern char datatoc_lightprobe_filter_visibility_frag_glsl[];
82 extern char datatoc_lightprobe_grid_display_frag_glsl[];
83 extern char datatoc_lightprobe_grid_display_vert_glsl[];
84 extern char datatoc_lightprobe_grid_fill_frag_glsl[];
85 extern char datatoc_lightprobe_planar_display_frag_glsl[];
86 extern char datatoc_lightprobe_planar_display_vert_glsl[];
87 extern char datatoc_lightprobe_planar_downsample_frag_glsl[];
88 extern char datatoc_lightprobe_planar_downsample_geom_glsl[];
89 extern char datatoc_lightprobe_planar_downsample_vert_glsl[];
90 extern char datatoc_irradiance_lib_glsl[];
91 extern char datatoc_lightprobe_lib_glsl[];
92 extern char datatoc_octahedron_lib_glsl[];
93
94 /* Velocity Resolve */
95 extern char datatoc_effect_velocity_resolve_frag_glsl[];
96
97 /* Temporal Sampling */
98 extern char datatoc_effect_temporal_aa_glsl[];
99
100
101 /* *********** FUNCTIONS *********** */
102
103 void EEVEE_shaders_lightprobe_shaders_init(void)
104 {
105         BLI_assert(e_data.probe_filter_glossy_sh == NULL);
106         char *shader_str = NULL;
107
108         shader_str = BLI_string_joinN(
109                 datatoc_common_view_lib_glsl,
110                 datatoc_common_uniforms_lib_glsl,
111                 datatoc_bsdf_common_lib_glsl,
112                 datatoc_bsdf_sampling_lib_glsl,
113                 datatoc_lightprobe_filter_glossy_frag_glsl);
114
115         e_data.probe_filter_glossy_sh = DRW_shader_create(
116                 datatoc_lightprobe_vert_glsl, datatoc_lightprobe_geom_glsl, shader_str, filter_defines);
117
118         e_data.probe_default_sh = DRW_shader_create(
119                 datatoc_background_vert_glsl, NULL, datatoc_default_world_frag_glsl, NULL);
120
121         MEM_freeN(shader_str);
122
123         shader_str = BLI_string_joinN(
124                 datatoc_common_view_lib_glsl,
125                 datatoc_common_uniforms_lib_glsl,
126                 datatoc_bsdf_common_lib_glsl,
127                 datatoc_bsdf_sampling_lib_glsl,
128                 datatoc_lightprobe_filter_diffuse_frag_glsl);
129
130         e_data.probe_filter_diffuse_sh = DRW_shader_create_fullscreen(shader_str, filter_defines);
131
132         MEM_freeN(shader_str);
133
134         shader_str = BLI_string_joinN(
135                 datatoc_common_view_lib_glsl,
136                 datatoc_common_uniforms_lib_glsl,
137                 datatoc_bsdf_common_lib_glsl,
138                 datatoc_bsdf_sampling_lib_glsl,
139                 datatoc_lightprobe_filter_visibility_frag_glsl);
140
141         e_data.probe_filter_visibility_sh = DRW_shader_create_fullscreen(shader_str, filter_defines);
142
143         MEM_freeN(shader_str);
144
145         e_data.probe_grid_fill_sh = DRW_shader_create_fullscreen(
146                 datatoc_lightprobe_grid_fill_frag_glsl, filter_defines);
147
148         e_data.probe_planar_downsample_sh = DRW_shader_create(
149                 datatoc_lightprobe_planar_downsample_vert_glsl,
150                 datatoc_lightprobe_planar_downsample_geom_glsl,
151                 datatoc_lightprobe_planar_downsample_frag_glsl,
152                 NULL);
153 }
154
155 GPUShader *EEVEE_shaders_probe_filter_glossy_sh_get(void)
156 {
157         return e_data.probe_filter_glossy_sh;
158 }
159
160 GPUShader *EEVEE_shaders_probe_default_sh_get(void)
161 {
162         return e_data.probe_default_sh;
163 }
164
165 GPUShader *EEVEE_shaders_probe_filter_diffuse_sh_get(void)
166 {
167         return e_data.probe_filter_diffuse_sh;
168 }
169
170 GPUShader *EEVEE_shaders_probe_filter_visibility_sh_get(void)
171 {
172         return e_data.probe_filter_visibility_sh;
173 }
174
175 GPUShader *EEVEE_shaders_probe_grid_fill_sh_get(void)
176 {
177         return e_data.probe_grid_fill_sh;
178 }
179
180 GPUShader *EEVEE_shaders_probe_planar_downsample_sh_get(void)
181 {
182         return e_data.probe_planar_downsample_sh;
183 }
184
185 GPUShader *EEVEE_shaders_default_studiolight_sh_get(void)
186 {
187         if (e_data.probe_default_studiolight_sh == NULL) {
188                 e_data.probe_default_studiolight_sh = DRW_shader_create(
189                         datatoc_background_vert_glsl, NULL, datatoc_default_world_frag_glsl, "#define LOOKDEV\n");
190         }
191         return e_data.probe_default_studiolight_sh;
192 }
193
194 GPUShader *EEVEE_shaders_probe_cube_display_sh_get(void)
195 {
196         if (e_data.probe_cube_display_sh == NULL) {
197                 char *shader_str = BLI_string_joinN(
198                         datatoc_octahedron_lib_glsl,
199                         datatoc_common_view_lib_glsl,
200                         datatoc_common_uniforms_lib_glsl,
201                         datatoc_bsdf_common_lib_glsl,
202                         datatoc_lightprobe_lib_glsl,
203                         datatoc_lightprobe_cube_display_frag_glsl);
204
205                 char *vert_str = BLI_string_joinN(
206                         datatoc_common_view_lib_glsl,
207                         datatoc_lightprobe_cube_display_vert_glsl);
208
209                 e_data.probe_cube_display_sh = DRW_shader_create(vert_str, NULL, shader_str, SHADER_DEFINES);
210
211                 MEM_freeN(vert_str);
212                 MEM_freeN(shader_str);
213         }
214         return e_data.probe_cube_display_sh;
215 }
216
217  GPUShader *EEVEE_shaders_probe_grid_display_sh_get(void)
218 {
219         if (e_data.probe_grid_display_sh == NULL ) {
220                 char *shader_str = BLI_string_joinN(
221                         datatoc_octahedron_lib_glsl,
222                         datatoc_common_view_lib_glsl,
223                         datatoc_common_uniforms_lib_glsl,
224                         datatoc_bsdf_common_lib_glsl,
225                         datatoc_irradiance_lib_glsl,
226                         datatoc_lightprobe_lib_glsl,
227                         datatoc_lightprobe_grid_display_frag_glsl);
228
229                 char *vert_str = BLI_string_joinN(
230                         datatoc_common_view_lib_glsl,
231                         datatoc_lightprobe_grid_display_vert_glsl);
232
233                 e_data.probe_grid_display_sh = DRW_shader_create(vert_str, NULL, shader_str, filter_defines);
234
235                 MEM_freeN(vert_str);
236                 MEM_freeN(shader_str);
237         }
238         return e_data.probe_grid_display_sh;
239 }
240
241  GPUShader *EEVEE_shaders_probe_planar_display_sh_get(void)
242 {
243         if (e_data.probe_planar_display_sh == NULL) {
244                 char *vert_str = BLI_string_joinN(
245                         datatoc_common_view_lib_glsl,
246                         datatoc_lightprobe_planar_display_vert_glsl);
247
248                 char *shader_str = BLI_string_joinN(
249                         datatoc_common_view_lib_glsl,
250                         datatoc_lightprobe_planar_display_frag_glsl);
251
252                 e_data.probe_planar_display_sh = DRW_shader_create(vert_str, NULL, shader_str, NULL);
253
254                 MEM_freeN(vert_str);
255                 MEM_freeN(shader_str);
256         }
257         return e_data.probe_planar_display_sh;
258 }
259
260 GPUShader *EEVEE_shaders_velocity_resolve_sh_get(void)
261 {
262         if (e_data.velocity_resolve_sh == NULL) {
263                 char *frag_str = BLI_string_joinN(
264                         datatoc_common_uniforms_lib_glsl,
265                         datatoc_common_view_lib_glsl,
266                         datatoc_bsdf_common_lib_glsl,
267                         datatoc_effect_velocity_resolve_frag_glsl);
268
269                 e_data.velocity_resolve_sh = DRW_shader_create_fullscreen(frag_str, NULL);
270
271                 MEM_freeN(frag_str);
272         }
273         return e_data.velocity_resolve_sh;
274 }
275
276 GPUShader *EEVEE_shaders_taa_resolve_sh_get(EEVEE_EffectsFlag enabled_effects)
277 {
278         GPUShader **sh;
279         const char *define = NULL;
280         if (enabled_effects & EFFECT_TAA_REPROJECT) {
281                 sh = &e_data.taa_resolve_reproject_sh;
282                 define = "#define USE_REPROJECTION\n";
283
284
285
286         }
287         else {
288                 sh = &e_data.taa_resolve_sh;
289         }
290         if (*sh == NULL) {
291                 char *frag_str = BLI_string_joinN(
292                         datatoc_common_uniforms_lib_glsl,
293                         datatoc_common_view_lib_glsl,
294                         datatoc_bsdf_common_lib_glsl,
295                         datatoc_effect_temporal_aa_glsl);
296
297                 *sh = DRW_shader_create_fullscreen(frag_str, define);
298                 MEM_freeN(frag_str);
299         }
300
301         return *sh;
302 }
303
304 void EEVEE_shaders_free(void)
305 {
306         DRW_SHADER_FREE_SAFE(e_data.probe_default_sh);
307         DRW_SHADER_FREE_SAFE(e_data.probe_filter_glossy_sh);
308         DRW_SHADER_FREE_SAFE(e_data.probe_filter_diffuse_sh);
309         DRW_SHADER_FREE_SAFE(e_data.probe_filter_visibility_sh);
310         DRW_SHADER_FREE_SAFE(e_data.probe_grid_fill_sh);
311         DRW_SHADER_FREE_SAFE(e_data.probe_planar_downsample_sh);
312         DRW_SHADER_FREE_SAFE(e_data.probe_default_studiolight_sh);
313         DRW_SHADER_FREE_SAFE(e_data.probe_grid_display_sh);
314         DRW_SHADER_FREE_SAFE(e_data.probe_cube_display_sh);
315         DRW_SHADER_FREE_SAFE(e_data.probe_planar_display_sh);
316         DRW_SHADER_FREE_SAFE(e_data.velocity_resolve_sh);
317         DRW_SHADER_FREE_SAFE(e_data.taa_resolve_sh);
318         DRW_SHADER_FREE_SAFE(e_data.taa_resolve_reproject_sh);
319 }