2013df35deb0a07efea26fa76a1bac2189343035
[blender.git] / source / blender / draw / engines / eevee / eevee.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.c
23  *  \ingroup DNA
24  */
25
26 #include "DRW_render.h"
27
28 #include "BLI_dynstr.h"
29
30 #include "eevee.h"
31 #include "eevee_private.h"
32 #include "eevee_lut.h"
33
34 #define EEVEE_ENGINE "BLENDER_EEVEE"
35
36 /* *********** STATIC *********** */
37 static struct {
38         struct GPUShader *default_lit;
39         struct GPUShader *depth_sh;
40         struct GPUShader *tonemap;
41         struct GPUShader *shadow_sh;
42         struct GPUTexture *ltc_mat;
43         struct GPUTexture *ltc_mag;
44
45         float camera_pos[3];
46 } e_data = {NULL}; /* Engine data */
47
48 extern char datatoc_ltc_lib_glsl[];
49 extern char datatoc_bsdf_common_lib_glsl[];
50 extern char datatoc_bsdf_direct_lib_glsl[];
51 extern char datatoc_lit_surface_frag_glsl[];
52 extern char datatoc_lit_surface_vert_glsl[];
53 extern char datatoc_tonemap_frag_glsl[];
54 extern char datatoc_shadow_frag_glsl[];
55 extern char datatoc_shadow_geom_glsl[];
56 extern char datatoc_shadow_vert_glsl[];
57
58 /* *********** FUNCTIONS *********** */
59
60 static void EEVEE_engine_init(void *vedata)
61 {
62         EEVEE_TextureList *txl = ((EEVEE_Data *)vedata)->txl;
63         EEVEE_FramebufferList *fbl = ((EEVEE_Data *)vedata)->fbl;
64         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
65
66         DRWFboTexture tex = {&txl->color, DRW_BUF_RGBA_16, DRW_TEX_FILTER};
67
68         float *viewport_size = DRW_viewport_size_get();
69         DRW_framebuffer_init(&fbl->main,
70                             (int)viewport_size[0], (int)viewport_size[1],
71                             &tex, 1);
72
73         if (!e_data.default_lit) {
74                 e_data.depth_sh = DRW_shader_create_3D_depth_only();
75         }
76
77         if (!e_data.default_lit) {
78                 char *lib_str = NULL;
79
80                 DynStr *ds_vert = BLI_dynstr_new();
81                 BLI_dynstr_append(ds_vert, datatoc_bsdf_common_lib_glsl);
82                 BLI_dynstr_append(ds_vert, datatoc_ltc_lib_glsl);
83                 BLI_dynstr_append(ds_vert, datatoc_bsdf_direct_lib_glsl);
84                 lib_str = BLI_dynstr_get_cstring(ds_vert);
85                 BLI_dynstr_free(ds_vert);
86
87                 e_data.default_lit = DRW_shader_create_with_lib(datatoc_lit_surface_vert_glsl, NULL, datatoc_lit_surface_frag_glsl, lib_str,
88                                                                 "#define MAX_LIGHT 128\n"
89                                                                 "#define MAX_SHADOW_CUBE 42\n"
90                                                                 "#define MAX_SHADOW_MAP 64\n"
91                                                                 "#define MAX_SHADOW_CASCADE 8\n"
92                                                                 "#define MAX_CASCADE_NUM 4\n");
93
94                 MEM_freeN(lib_str);
95         }
96
97         if (!e_data.shadow_sh) {
98                 e_data.shadow_sh = DRW_shader_create(datatoc_shadow_vert_glsl, datatoc_shadow_geom_glsl, datatoc_shadow_frag_glsl, NULL);
99         }
100
101         if (!e_data.tonemap) {
102                 e_data.tonemap = DRW_shader_create_fullscreen(datatoc_tonemap_frag_glsl, NULL);
103         }
104
105         if (!e_data.ltc_mat) {
106                 e_data.ltc_mat = DRW_texture_create_2D(64, 64, DRW_TEX_RGBA_16, DRW_TEX_FILTER, ltc_mat_ggx);
107         }
108
109         if (!e_data.ltc_mag) {
110                 e_data.ltc_mag = DRW_texture_create_2D(64, 64, DRW_TEX_R_16, DRW_TEX_FILTER, ltc_mag_ggx);
111         }
112
113         if (stl->lamps == NULL) {
114                 EEVEE_lights_init(stl);
115         }
116
117         // EEVEE_lights_update(stl);
118         {
119                 float viewinvmat[4][4];
120                 DRW_viewport_matrix_get(viewinvmat, DRW_MAT_VIEWINV);
121
122                 copy_v3_v3(e_data.camera_pos, viewinvmat[3]);
123         }
124 }
125
126 static void EEVEE_cache_init(void *vedata)
127 {
128         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
129         EEVEE_TextureList *txl = ((EEVEE_Data *)vedata)->txl;
130         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
131
132         if (!stl->g_data) {
133                 /* Alloc transient pointers */
134                 stl->g_data = MEM_mallocN(sizeof(g_data), "g_data");
135         }
136
137         {
138                 /* Shadow Pass */
139                 psl->shadow_pass = DRW_pass_create("Shadow Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
140                 stl->g_data->shadow_shgrp = DRW_shgroup_create(e_data.shadow_sh, psl->shadow_pass);
141                 DRW_shgroup_uniform_mat4(stl->g_data->shadow_shgrp, "ShadowMatrix", (float *)stl->lamps->shadowmat);
142                 DRW_shgroup_uniform_int(stl->g_data->shadow_shgrp, "Layer", &stl->lamps->layer, 1);
143         }
144
145         {
146                 psl->depth_pass = DRW_pass_create("Depth Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
147                 stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);
148
149                 psl->depth_pass_cull = DRW_pass_create("Depth Pass Cull", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_CULL_BACK);
150                 stl->g_data->depth_shgrp_cull = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass_cull);
151         }
152
153         {
154                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_EQUAL;
155                 psl->pass = DRW_pass_create("Default Light Pass", state);
156
157                 stl->g_data->default_lit_grp = DRW_shgroup_create(e_data.default_lit, psl->pass);
158                 DRW_shgroup_uniform_block(stl->g_data->default_lit_grp, "light_block", stl->light_ubo, 0);
159                 DRW_shgroup_uniform_block(stl->g_data->default_lit_grp, "shadow_block", stl->shadow_ubo, 1);
160                 DRW_shgroup_uniform_int(stl->g_data->default_lit_grp, "light_count", &stl->lamps->num_light, 1);
161                 DRW_shgroup_uniform_vec3(stl->g_data->default_lit_grp, "cameraPos", e_data.camera_pos, 1);
162                 DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "ltcMat", e_data.ltc_mat, 0);
163                 DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "ltcMag", e_data.ltc_mag, 1);
164                 /* NOTE : Adding Shadow Map textures uniform in EEVEE_cache_finish */
165         }
166
167         {
168                 /* Final pass : Map HDR color to LDR color.
169                  * Write result to the default color buffer */
170                 psl->tonemap = DRW_pass_create("Tone Mapping", DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
171
172                 DRWShadingGroup *grp = DRW_shgroup_create(e_data.tonemap, psl->tonemap);
173                 DRW_shgroup_uniform_buffer(grp, "hdrColorBuf", &txl->color, 0);
174
175                 struct Batch *geom = DRW_cache_fullscreen_quad_get();
176                 DRW_shgroup_call_add(grp, geom, NULL);
177         }
178
179         EEVEE_lights_cache_init(stl);
180 }
181
182 static void EEVEE_cache_populate(void *vedata, Object *ob)
183 {
184         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
185
186         if (ob->type == OB_MESH) {
187                 IDProperty *ces_mode_ob = BKE_object_collection_engine_get(ob, COLLECTION_MODE_OBJECT, "");
188                 bool do_cull = BKE_collection_engine_property_value_get_bool(ces_mode_ob, "show_backface_culling");
189                 struct Batch *geom = DRW_cache_surface_get(ob);
190
191                 /* Depth Prepass */
192                 DRW_shgroup_call_add((do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp, geom, ob->obmat);
193
194                 DRW_shgroup_call_add(stl->g_data->default_lit_grp, geom, ob->obmat);
195                 DRW_shgroup_call_add(stl->g_data->shadow_shgrp, geom, ob->obmat);
196         }
197         else if (ob->type == OB_LAMP) {
198                 EEVEE_lights_cache_add(stl, ob);
199         }
200 }
201
202 static void EEVEE_cache_finish(void *vedata)
203 {
204         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
205         EEVEE_TextureList *txl = ((EEVEE_Data *)vedata)->txl;
206         EEVEE_FramebufferList *fbl = ((EEVEE_Data *)vedata)->fbl;
207
208         EEVEE_lights_cache_finish(stl, txl, fbl);
209
210         /* Shadows binding */
211         DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "shadowMaps", txl->shadow_depth_map_pool, 2);
212         DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "shadowCubes", txl->shadow_depth_cube_pool, 3);
213 }
214
215 static void EEVEE_draw_scene(void *vedata)
216 {
217         EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
218         EEVEE_FramebufferList *fbl = ((EEVEE_Data *)vedata)->fbl;
219
220         /* Default framebuffer and texture */
221         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
222         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
223
224         /* Refresh shadows */
225         EEVEE_draw_shadows((EEVEE_Data *)vedata);
226
227         /* Attach depth to the hdr buffer and bind it */        
228         DRW_framebuffer_texture_detach(dtxl->depth);
229         DRW_framebuffer_texture_attach(fbl->main, dtxl->depth, 0);
230         DRW_framebuffer_bind(fbl->main);
231
232         /* Clear Depth */
233         /* TODO do background */
234         float clearcol[4] = {0.0f, 0.0f, 0.0f, 1.0f};
235         DRW_framebuffer_clear(true, true, false, clearcol, 1.0f);
236
237         DRW_draw_pass(psl->depth_pass);
238         DRW_draw_pass(psl->depth_pass_cull);
239         DRW_draw_pass(psl->pass);
240
241         /* Restore default framebuffer */
242         DRW_framebuffer_texture_detach(dtxl->depth);
243         DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0);
244         DRW_framebuffer_bind(dfbl->default_fb);
245
246         DRW_draw_pass(psl->tonemap);
247 }
248
249 static void EEVEE_engine_free(void)
250 {
251         if (e_data.default_lit)
252                 DRW_shader_free(e_data.default_lit);
253         if (e_data.shadow_sh)
254                 DRW_shader_free(e_data.shadow_sh);
255         if (e_data.tonemap)
256                 DRW_shader_free(e_data.tonemap);
257         if (e_data.ltc_mat)
258                 DRW_texture_free(e_data.ltc_mat);
259         if (e_data.ltc_mag)
260                 DRW_texture_free(e_data.ltc_mag);
261 }
262
263 static void EEVEE_collection_settings_create(RenderEngine *UNUSED(engine), IDProperty *props)
264 {
265         BLI_assert(props &&
266                    props->type == IDP_GROUP &&
267                    props->subtype == IDP_GROUP_SUB_ENGINE_RENDER);
268         // BKE_collection_engine_property_add_int(props, "high_quality_sphere_lamps", false);
269 }
270
271 DrawEngineType draw_engine_eevee_type = {
272         NULL, NULL,
273         N_("Eevee"),
274         &EEVEE_engine_init,
275         &EEVEE_engine_free,
276         &EEVEE_cache_init,
277         &EEVEE_cache_populate,
278         &EEVEE_cache_finish,
279         &EEVEE_draw_scene,
280         NULL//&EEVEE_draw_scene
281 };
282
283 RenderEngineType viewport_eevee_type = {
284         NULL, NULL,
285         EEVEE_ENGINE, N_("Eevee"), RE_INTERNAL | RE_USE_OGL_PIPELINE,
286         NULL, NULL, NULL, NULL, NULL, NULL, &EEVEE_collection_settings_create,
287         &draw_engine_eevee_type,
288         {NULL, NULL, NULL}
289 };
290
291
292 #undef EEVEE_ENGINE