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