Workbench: Rename orientation to type and Camera studio light to Studio
[blender.git] / source / blender / draw / engines / eevee / eevee_lookdev.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_lookdev.c
23  *  \ingroup draw_engine
24  */
25 #include "DRW_render.h"
26
27 #include "BKE_camera.h"
28 #include "BKE_studiolight.h"
29
30 #include "DNA_screen_types.h"
31 #include "DNA_world_types.h"
32
33 #include "DEG_depsgraph_query.h"
34
35 #include "ED_screen.h"
36
37 #include "UI_resources.h"
38
39 #include "eevee_private.h"
40 #include "eevee_lightcache.h"
41
42 #include "draw_common.h"
43
44 static void eevee_lookdev_lightcache_delete(EEVEE_Data *vedata)
45 {
46         EEVEE_StorageList *stl = vedata->stl;
47         EEVEE_TextureList *txl = vedata->txl;
48
49         MEM_SAFE_FREE(stl->lookdev_lightcache);
50         MEM_SAFE_FREE(stl->lookdev_grid_data);
51         MEM_SAFE_FREE(stl->lookdev_cube_data);
52         DRW_TEXTURE_FREE_SAFE(txl->lookdev_grid_tx);
53         DRW_TEXTURE_FREE_SAFE(txl->lookdev_cube_tx);
54 }
55
56 void EEVEE_lookdev_cache_init(
57         EEVEE_Data *vedata, DRWShadingGroup **grp, DRWPass *pass,
58         World *UNUSED(world), EEVEE_LightProbesInfo *pinfo)
59 {
60         EEVEE_StorageList *stl = vedata->stl;
61         EEVEE_TextureList *txl = vedata->txl;
62         const DRWContextState *draw_ctx = DRW_context_state_get();
63         View3D *v3d = draw_ctx->v3d;
64         if (LOOK_DEV_STUDIO_LIGHT_ENABLED(v3d)) {
65                 StudioLight *sl = BKE_studiolight_find(v3d->shading.studio_light, STUDIOLIGHT_ORIENTATIONS_MATERIAL_MODE);
66                 if (sl && (sl->flag & STUDIOLIGHT_TYPE_WORLD)) {
67                         GPUShader *shader = EEVEE_shaders_default_studiolight_sh_get();
68                         struct GPUBatch *geom = DRW_cache_fullscreen_quad_get();
69                         GPUTexture *tex = NULL;
70
71                         /* If one of the component is missing we start from scratch. */
72                         if ((stl->lookdev_grid_data == NULL) ||
73                             (stl->lookdev_cube_data == NULL) ||
74                             (txl->lookdev_grid_tx == NULL) ||
75                             (txl->lookdev_cube_tx == NULL))
76                         {
77                                 eevee_lookdev_lightcache_delete(vedata);
78                         }
79
80                         if (stl->lookdev_lightcache == NULL) {
81                                 const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
82 #if defined(IRRADIANCE_SH_L2)
83                                 int grid_res = 4;
84 #elif defined(IRRADIANCE_CUBEMAP)
85                                 int grid_res = 8;
86 #elif defined(IRRADIANCE_HL2)
87                                 int grid_res = 4;
88 #endif
89                                 int cube_res = OCTAHEDRAL_SIZE_FROM_CUBESIZE(scene_eval->eevee.gi_cubemap_resolution);
90                                 int vis_res = scene_eval->eevee.gi_visibility_resolution;
91
92                                 stl->lookdev_lightcache = EEVEE_lightcache_create(1, 1, cube_res, vis_res, (int[3]){grid_res, grid_res, 1});
93
94                                 /* XXX: Fix memleak. TODO find out why. */
95                                 MEM_SAFE_FREE(stl->lookdev_cube_mips);
96
97                                 /* We do this to use a special light cache for lookdev.
98                                  * This lightcache needs to be per viewport. But we need to
99                                  * have correct freeing when the viewport is closed. So we
100                                  * need to reference all textures to the txl and the memblocks
101                                  * to the stl. */
102                                 stl->lookdev_grid_data = stl->lookdev_lightcache->grid_data;
103                                 stl->lookdev_cube_data = stl->lookdev_lightcache->cube_data;
104                                 stl->lookdev_cube_mips = stl->lookdev_lightcache->cube_mips;
105                                 txl->lookdev_grid_tx = stl->lookdev_lightcache->grid_tx.tex;
106                                 txl->lookdev_cube_tx = stl->lookdev_lightcache->cube_tx.tex;
107                         }
108
109                         stl->g_data->light_cache = stl->lookdev_lightcache;
110
111                         static float background_color[4];
112                         UI_GetThemeColor4fv(TH_HIGH_GRAD, background_color);
113                         /* XXX: Really quick conversion to avoid washed out background.
114                          * Needs to be addressed properly (color managed using ocio). */
115                         srgb_to_linearrgb_v4(background_color, background_color);
116
117                         *grp = DRW_shgroup_create(shader, pass);
118                         axis_angle_to_mat3_single(stl->g_data->studiolight_matrix, 'Z', v3d->shading.studiolight_rot_z);
119                         DRW_shgroup_uniform_mat3(*grp, "StudioLightMatrix", stl->g_data->studiolight_matrix);
120                         DRW_shgroup_uniform_float(*grp, "backgroundAlpha", &stl->g_data->background_alpha, 1);
121                         DRW_shgroup_uniform_vec3(*grp, "color", background_color, 1);
122                         DRW_shgroup_call_add(*grp, geom, NULL);
123                         if (!pinfo) {
124                                 /* Do not fadeout when doing probe rendering, only when drawing the background */
125                                 DRW_shgroup_uniform_float(*grp, "studioLightBackground", &v3d->shading.studiolight_background, 1);
126                                 BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EQUIRECT_IRRADIANCE_GPUTEXTURE);
127                                 tex = sl->equirect_irradiance_gputexture;
128                         }
129                         else {
130                                 DRW_shgroup_uniform_float_copy(*grp, "studioLightBackground", 1.0f);
131                                 BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE);
132                                 tex = sl->equirect_radiance_gputexture;
133                         }
134                         DRW_shgroup_uniform_texture(*grp, "image", tex);
135
136                         /* Do we need to recalc the lightprobes? */
137                         if (pinfo &&
138                             ((pinfo->studiolight_index != sl->index) ||
139                              (pinfo->studiolight_rot_z != v3d->shading.studiolight_rot_z)))
140                         {
141                                 stl->lookdev_lightcache->flag |= LIGHTCACHE_UPDATE_WORLD;
142                                 pinfo->studiolight_index = sl->index;
143                                 pinfo->studiolight_rot_z = v3d->shading.studiolight_rot_z;
144                         }
145                 }
146         }
147 }
148
149 void EEVEE_lookdev_draw_background(EEVEE_Data *vedata)
150 {
151         EEVEE_PassList *psl = vedata->psl;
152         EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
153         EEVEE_EffectsInfo *effects = stl->effects;
154         EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();
155         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
156
157         const DRWContextState *draw_ctx = DRW_context_state_get();
158
159         if (psl->lookdev_pass && LOOK_DEV_OVERLAY_ENABLED(draw_ctx->v3d)) {
160                 DRW_stats_group_start("Look Dev");
161                 CameraParams params;
162                 BKE_camera_params_init(&params);
163                 View3D *v3d = draw_ctx->v3d;
164                 RegionView3D *rv3d = draw_ctx->rv3d;
165                 ARegion *ar = draw_ctx->ar;
166
167                 const float *viewport_size = DRW_viewport_size_get();
168                 rcti rect;
169                 ED_region_visible_rect(draw_ctx->ar, &rect);
170
171                 const float viewport_size_target[2] = {
172                         viewport_size[0] / 4,
173                         viewport_size[1] / 4,
174                 };
175                 const int viewport_inset[2] = {
176                         max_ii(viewport_size_target[0], 300),
177                         max_ii(viewport_size_target[0], 300) / 2,  /* intentionally use 'x' here for 'y' value. */
178                 };
179
180                 /* minimum size for preview spheres viewport */
181                 const float aspect[2] = {
182                         viewport_inset[0] / viewport_size_target[0],
183                         viewport_inset[1] / viewport_size_target[1],
184                 };
185
186                 BKE_camera_params_from_view3d(&params, draw_ctx->depsgraph, v3d, rv3d);
187                 params.is_ortho = true;
188                 params.ortho_scale = 3.0f;
189                 params.zoom = CAMERA_PARAM_ZOOM_INIT_PERSP;
190                 params.offsetx = 0.0f;
191                 params.offsety = 0.0f;
192                 params.shiftx = 0.0f;
193                 params.shifty = 0.0f;
194                 params.clipsta = 0.001f;
195                 params.clipend = 20.0f;
196                 BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, aspect[0], aspect[1]);
197                 BKE_camera_params_compute_matrix(&params);
198
199                 EEVEE_CommonUniformBuffer *common = &sldata->common_data;
200                 common->la_num_light = 0;
201                 common->prb_num_planar = 0;
202                 common->prb_num_render_cube = 1;
203                 common->prb_num_render_grid = 1;
204                 common->ao_dist = 0.0f;
205                 common->ao_factor = 0.0f;
206                 common->ao_settings = 0.0f;
207                 DRW_uniformbuffer_update(sldata->common_ubo, common);
208
209                 /* override matrices */
210                 float winmat[4][4];
211                 float winmat_inv[4][4];
212                 copy_m4_m4(winmat, params.winmat);
213                 invert_m4_m4(winmat_inv, winmat);
214                 DRW_viewport_matrix_override_set(winmat, DRW_MAT_WIN);
215                 DRW_viewport_matrix_override_set(winmat_inv, DRW_MAT_WININV);
216                 float viewmat[4][4];
217                 DRW_viewport_matrix_get(viewmat, DRW_MAT_VIEW);
218                 float persmat[4][4];
219                 float persmat_inv[4][4];
220                 mul_m4_m4m4(persmat, winmat, viewmat);
221                 invert_m4_m4(persmat_inv, persmat);
222                 DRW_viewport_matrix_override_set(persmat, DRW_MAT_PERS);
223                 DRW_viewport_matrix_override_set(persmat_inv, DRW_MAT_PERSINV);
224
225                 GPUFrameBuffer *fb = effects->final_fb;
226                 GPU_framebuffer_bind(fb);
227                 GPU_framebuffer_viewport_set(fb, rect.xmax - viewport_inset[0], rect.ymin, viewport_inset[0], viewport_inset[1]);
228                 DRW_draw_pass(psl->lookdev_pass);
229
230                 fb = dfbl->depth_only_fb;
231                 GPU_framebuffer_bind(fb);
232                 GPU_framebuffer_viewport_set(fb, rect.xmax - viewport_inset[0], rect.ymin, viewport_inset[0], viewport_inset[1]);
233                 DRW_draw_pass(psl->lookdev_pass);
234
235                 DRW_viewport_matrix_override_unset_all();
236                 DRW_stats_group_end();
237         }
238 }