Cleanup: warnings (clang)
[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 extern GlobalsUboStorage ts;
101
102 /* *********** FUNCTIONS *********** */
103
104 void EEVEE_shaders_lightprobe_shaders_init(void)
105 {
106         BLI_assert(e_data.probe_filter_glossy_sh == NULL);
107         char *shader_str = NULL;
108
109         shader_str = BLI_string_joinN(
110                 datatoc_common_view_lib_glsl,
111                 datatoc_common_uniforms_lib_glsl,
112                 datatoc_bsdf_common_lib_glsl,
113                 datatoc_bsdf_sampling_lib_glsl,
114                 datatoc_lightprobe_filter_glossy_frag_glsl);
115
116         e_data.probe_filter_glossy_sh = DRW_shader_create(
117                 datatoc_lightprobe_vert_glsl, datatoc_lightprobe_geom_glsl, shader_str, filter_defines);
118
119         e_data.probe_default_sh = DRW_shader_create(
120                 datatoc_background_vert_glsl, NULL, datatoc_default_world_frag_glsl, NULL);
121
122         MEM_freeN(shader_str);
123
124         shader_str = BLI_string_joinN(
125                 datatoc_common_view_lib_glsl,
126                 datatoc_common_uniforms_lib_glsl,
127                 datatoc_bsdf_common_lib_glsl,
128                 datatoc_bsdf_sampling_lib_glsl,
129                 datatoc_lightprobe_filter_diffuse_frag_glsl);
130
131         e_data.probe_filter_diffuse_sh = DRW_shader_create_fullscreen(shader_str, filter_defines);
132
133         MEM_freeN(shader_str);
134
135         shader_str = BLI_string_joinN(
136                 datatoc_common_view_lib_glsl,
137                 datatoc_common_uniforms_lib_glsl,
138                 datatoc_bsdf_common_lib_glsl,
139                 datatoc_bsdf_sampling_lib_glsl,
140                 datatoc_lightprobe_filter_visibility_frag_glsl);
141
142         e_data.probe_filter_visibility_sh = DRW_shader_create_fullscreen(shader_str, filter_defines);
143
144         MEM_freeN(shader_str);
145
146         e_data.probe_grid_fill_sh = DRW_shader_create_fullscreen(
147                 datatoc_lightprobe_grid_fill_frag_glsl, filter_defines);
148
149         e_data.probe_planar_downsample_sh = DRW_shader_create(
150                 datatoc_lightprobe_planar_downsample_vert_glsl,
151                 datatoc_lightprobe_planar_downsample_geom_glsl,
152                 datatoc_lightprobe_planar_downsample_frag_glsl,
153                 NULL);
154 }
155
156 GPUShader *EEVEE_shaders_probe_filter_glossy_sh_get(void)
157 {
158         return e_data.probe_filter_glossy_sh;
159 }
160
161 GPUShader *EEVEE_shaders_probe_default_sh_get(void)
162 {
163         return e_data.probe_default_sh;
164 }
165
166 GPUShader *EEVEE_shaders_probe_filter_diffuse_sh_get(void)
167 {
168         return e_data.probe_filter_diffuse_sh;
169 }
170
171 GPUShader *EEVEE_shaders_probe_filter_visibility_sh_get(void)
172 {
173         return e_data.probe_filter_visibility_sh;
174 }
175
176 GPUShader *EEVEE_shaders_probe_grid_fill_sh_get(void)
177 {
178         return e_data.probe_grid_fill_sh;
179 }
180
181 GPUShader *EEVEE_shaders_probe_planar_downsample_sh_get(void)
182 {
183         return e_data.probe_planar_downsample_sh;
184 }
185
186 GPUShader *EEVEE_shaders_default_studiolight_sh_get(void)
187 {
188         if (e_data.probe_default_studiolight_sh == NULL) {
189                 e_data.probe_default_studiolight_sh = DRW_shader_create(
190                         datatoc_background_vert_glsl, NULL, datatoc_default_world_frag_glsl, "#define LOOKDEV\n");
191         }
192         return e_data.probe_default_studiolight_sh;
193 }
194
195 GPUShader *EEVEE_shaders_probe_cube_display_sh_get(void)
196 {
197         if (e_data.probe_cube_display_sh == NULL) {
198                 char *shader_str = BLI_string_joinN(
199                         datatoc_octahedron_lib_glsl,
200                         datatoc_common_view_lib_glsl,
201                         datatoc_common_uniforms_lib_glsl,
202                         datatoc_bsdf_common_lib_glsl,
203                         datatoc_lightprobe_lib_glsl,
204                         datatoc_lightprobe_cube_display_frag_glsl);
205
206                 char *vert_str = BLI_string_joinN(
207                         datatoc_common_view_lib_glsl,
208                         datatoc_lightprobe_cube_display_vert_glsl);
209
210                 e_data.probe_cube_display_sh = DRW_shader_create(vert_str, NULL, shader_str, SHADER_DEFINES);
211
212                 MEM_freeN(vert_str);
213                 MEM_freeN(shader_str);
214         }
215         return e_data.probe_cube_display_sh;
216 }
217
218  GPUShader *EEVEE_shaders_probe_grid_display_sh_get(void)
219 {
220         if (e_data.probe_grid_display_sh == NULL ) {
221                 char *shader_str = BLI_string_joinN(
222                         datatoc_octahedron_lib_glsl,
223                         datatoc_common_view_lib_glsl,
224                         datatoc_common_uniforms_lib_glsl,
225                         datatoc_bsdf_common_lib_glsl,
226                         datatoc_irradiance_lib_glsl,
227                         datatoc_lightprobe_lib_glsl,
228                         datatoc_lightprobe_grid_display_frag_glsl);
229
230                 char *vert_str = BLI_string_joinN(
231                         datatoc_common_view_lib_glsl,
232                         datatoc_lightprobe_grid_display_vert_glsl);
233
234                 e_data.probe_grid_display_sh = DRW_shader_create(vert_str, NULL, shader_str, filter_defines);
235
236                 MEM_freeN(vert_str);
237                 MEM_freeN(shader_str);
238         }
239         return e_data.probe_grid_display_sh;
240 }
241
242  GPUShader *EEVEE_shaders_probe_planar_display_sh_get(void)
243 {
244         if (e_data.probe_planar_display_sh == NULL) {
245                 char *vert_str = BLI_string_joinN(
246                         datatoc_common_view_lib_glsl,
247                         datatoc_lightprobe_planar_display_vert_glsl);
248
249                 char *shader_str = BLI_string_joinN(
250                         datatoc_common_view_lib_glsl,
251                         datatoc_lightprobe_planar_display_frag_glsl);
252
253                 e_data.probe_planar_display_sh = DRW_shader_create(vert_str, NULL, shader_str, NULL);
254
255                 MEM_freeN(vert_str);
256                 MEM_freeN(shader_str);
257         }
258         return e_data.probe_planar_display_sh;
259 }
260
261 GPUShader *EEVEE_shaders_velocity_resolve_sh_get(void)
262 {
263         if (e_data.velocity_resolve_sh == NULL) {
264                 char *frag_str = BLI_string_joinN(
265                         datatoc_common_uniforms_lib_glsl,
266                         datatoc_common_view_lib_glsl,
267                         datatoc_bsdf_common_lib_glsl,
268                         datatoc_effect_velocity_resolve_frag_glsl);
269
270                 e_data.velocity_resolve_sh = DRW_shader_create_fullscreen(frag_str, NULL);
271
272                 MEM_freeN(frag_str);
273         }
274         return e_data.velocity_resolve_sh;
275 }
276
277 GPUShader *EEVEE_shaders_taa_resolve_sh_get(EEVEE_EffectsFlag enabled_effects)
278 {
279         GPUShader **sh;
280         const char *define = NULL;
281         if (enabled_effects & EFFECT_TAA_REPROJECT) {
282                 sh = &e_data.taa_resolve_reproject_sh;
283                 define = "#define USE_REPROJECTION\n";
284
285
286
287         }
288         else {
289                 sh = &e_data.taa_resolve_sh;
290         }
291         if (*sh == NULL) {
292                 char *frag_str = BLI_string_joinN(
293                         datatoc_common_uniforms_lib_glsl,
294                         datatoc_common_view_lib_glsl,
295                         datatoc_bsdf_common_lib_glsl,
296                         datatoc_effect_temporal_aa_glsl);
297
298                 *sh = DRW_shader_create_fullscreen(frag_str, define);
299                 MEM_freeN(frag_str);
300         }
301
302         return *sh;
303 }
304
305 void EEVEE_shaders_free(void)
306 {
307         DRW_SHADER_FREE_SAFE(e_data.probe_default_sh);
308         DRW_SHADER_FREE_SAFE(e_data.probe_filter_glossy_sh);
309         DRW_SHADER_FREE_SAFE(e_data.probe_filter_diffuse_sh);
310         DRW_SHADER_FREE_SAFE(e_data.probe_filter_visibility_sh);
311         DRW_SHADER_FREE_SAFE(e_data.probe_grid_fill_sh);
312         DRW_SHADER_FREE_SAFE(e_data.probe_planar_downsample_sh);
313         DRW_SHADER_FREE_SAFE(e_data.probe_default_studiolight_sh);
314         DRW_SHADER_FREE_SAFE(e_data.probe_grid_display_sh);
315         DRW_SHADER_FREE_SAFE(e_data.probe_cube_display_sh);
316         DRW_SHADER_FREE_SAFE(e_data.probe_planar_display_sh);
317         DRW_SHADER_FREE_SAFE(e_data.velocity_resolve_sh);
318         DRW_SHADER_FREE_SAFE(e_data.taa_resolve_sh);
319         DRW_SHADER_FREE_SAFE(e_data.taa_resolve_reproject_sh);
320 }