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