Welcome to mirror list, hosted at ThFree Co, Russian Federation.

eevee_lookdev.c « eevee « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f79d90500bdd2e0793faa89ab800cae55b638509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2016, Blender Foundation.
 */

/** \file
 * \ingroup draw_engine
 */
#include "DRW_render.h"

#include "BKE_camera.h"
#include "BKE_studiolight.h"

#include "BLI_rand.h"
#include "BLI_rect.h"

#include "DNA_screen_types.h"
#include "DNA_world_types.h"

#include "DEG_depsgraph_query.h"

#include "ED_screen.h"

#include "GPU_material.h"

#include "UI_resources.h"

#include "eevee_lightcache.h"
#include "eevee_private.h"

#include "draw_common.h"

static void eevee_lookdev_lightcache_delete(EEVEE_Data *vedata)
{
  EEVEE_StorageList *stl = vedata->stl;
  EEVEE_PrivateData *g_data = stl->g_data;
  EEVEE_TextureList *txl = vedata->txl;

  MEM_SAFE_FREE(stl->lookdev_lightcache);
  MEM_SAFE_FREE(stl->lookdev_grid_data);
  MEM_SAFE_FREE(stl->lookdev_cube_data);
  DRW_TEXTURE_FREE_SAFE(txl->lookdev_grid_tx);
  DRW_TEXTURE_FREE_SAFE(txl->lookdev_cube_tx);
  g_data->studiolight_index = -1;
  g_data->studiolight_rot_z = 0.0f;
}

static void eevee_lookdev_hdri_preview_init(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata)
{
  EEVEE_PassList *psl = vedata->psl;
  const DRWContextState *draw_ctx = DRW_context_state_get();
  Scene *scene = draw_ctx->scene;
  DRWShadingGroup *grp;

  struct GPUBatch *sphere = DRW_cache_sphere_get();
  int mat_options = VAR_MAT_MESH | VAR_MAT_LOOKDEV;

  DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS |
                   DRW_STATE_CULL_BACK;

  {
    Material *ma = EEVEE_material_default_diffuse_get();
    GPUMaterial *gpumat = EEVEE_material_get(vedata, scene, ma, NULL, mat_options);
    struct GPUShader *sh = GPU_material_get_shader(gpumat);

    DRW_PASS_CREATE(psl->lookdev_diffuse_pass, state);
    grp = DRW_shgroup_create(sh, psl->lookdev_diffuse_pass);
    EEVEE_material_bind_resources(grp, gpumat, sldata, vedata, NULL, NULL, false, false);
    DRW_shgroup_add_material_resources(grp, gpumat);
    DRW_shgroup_call(grp, sphere, NULL);
  }
  {
    Material *ma = EEVEE_material_default_glossy_get();
    GPUMaterial *gpumat = EEVEE_material_get(vedata, scene, ma, NULL, mat_options);
    struct GPUShader *sh = GPU_material_get_shader(gpumat);

    DRW_PASS_CREATE(psl->lookdev_glossy_pass, state);
    grp = DRW_shgroup_create(sh, psl->lookdev_glossy_pass);
    EEVEE_material_bind_resources(grp, gpumat, sldata, vedata, NULL, NULL, false, false);
    DRW_shgroup_add_material_resources(grp, gpumat);
    DRW_shgroup_call(grp, sphere, NULL);
  }
}

void EEVEE_lookdev_cache_init(EEVEE_Data *vedata,
                              EEVEE_ViewLayerData *sldata,
                              DRWPass *pass,
                              EEVEE_LightProbesInfo *pinfo,
                              DRWShadingGroup **r_shgrp)
{
  EEVEE_StorageList *stl = vedata->stl;
  EEVEE_TextureList *txl = vedata->txl;
  EEVEE_EffectsInfo *effects = stl->effects;
  EEVEE_PrivateData *g_data = stl->g_data;
  const DRWContextState *draw_ctx = DRW_context_state_get();
  /* The view will be NULL when rendering previews. */
  const View3D *v3d = draw_ctx->v3d;
  const Scene *scene = draw_ctx->scene;

  const bool probe_render = pinfo != NULL;

  effects->lookdev_view = NULL;

  if (eevee_hdri_preview_overlay_enabled(v3d)) {
    /* Viewport / Spheres size. */
    const rcti *rect;
    rcti fallback_rect;
    if (DRW_state_is_opengl_render()) {
      const float *vp_size = DRW_viewport_size_get();
      fallback_rect.xmax = vp_size[0];
      fallback_rect.ymax = vp_size[1];
      fallback_rect.xmin = fallback_rect.ymin = 0;
      rect = &fallback_rect;
    }
    else {
      rect = ED_region_visible_rect(draw_ctx->region);
    }

    /* Make the viewport width scale the lookdev spheres a bit.
     * Scale between 1000px and 2000px. */
    const float viewport_scale = clamp_f(
        BLI_rcti_size_x(rect) / (2000.0f * U.dpi_fac), 0.5f, 1.0f);
    const int sphere_size = U.lookdev_sphere_size * U.dpi_fac * viewport_scale;

    if (sphere_size != effects->sphere_size || rect->xmax != effects->anchor[0] ||
        rect->ymin != effects->anchor[1]) {
      /* If sphere size or anchor point moves, reset TAA to avoid ghosting issue.
       * This needs to happen early because we are changing taa_current_sample. */
      effects->sphere_size = sphere_size;
      effects->anchor[0] = rect->xmax;
      effects->anchor[1] = rect->ymin;
      EEVEE_temporal_sampling_reset(vedata);
    }

    eevee_lookdev_hdri_preview_init(vedata, sldata);
  }

  if (LOOK_DEV_STUDIO_LIGHT_ENABLED(v3d)) {
    const View3DShading *shading = &v3d->shading;
    StudioLight *sl = BKE_studiolight_find(shading->lookdev_light,
                                           STUDIOLIGHT_ORIENTATIONS_MATERIAL_MODE);
    if (sl == NULL || (sl->flag & STUDIOLIGHT_TYPE_WORLD) == 0) {
      return;
    }

    GPUShader *shader = probe_render ? EEVEE_shaders_studiolight_probe_sh_get() :
                                       EEVEE_shaders_studiolight_background_sh_get();

    const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
    int cube_res = scene_eval->eevee.gi_cubemap_resolution;

    /* If one of the component is missing we start from scratch. */
    if ((stl->lookdev_grid_data == NULL) || (stl->lookdev_cube_data == NULL) ||
        (txl->lookdev_grid_tx == NULL) || (txl->lookdev_cube_tx == NULL) ||
        (g_data->light_cache && g_data->light_cache->ref_res != cube_res)) {
      eevee_lookdev_lightcache_delete(vedata);
    }

    if (stl->lookdev_lightcache == NULL) {
#if defined(IRRADIANCE_SH_L2)
      int grid_res = 4;
#elif defined(IRRADIANCE_HL2)
      int grid_res = 4;
#endif

      stl->lookdev_lightcache = EEVEE_lightcache_create(
          1, 1, cube_res, 8, (int[3]){grid_res, grid_res, 1});

      /* XXX: Fix memleak. TODO find out why. */
      MEM_SAFE_FREE(stl->lookdev_cube_mips);

      /* We do this to use a special light cache for lookdev.
       * This light-cache needs to be per viewport. But we need to
       * have correct freeing when the viewport is closed. So we
       * need to reference all textures to the txl and the memblocks
       * to the stl. */
      stl->lookdev_grid_data = stl->lookdev_lightcache->grid_data;
      stl->lookdev_cube_data = stl->lookdev_lightcache->cube_data;
      stl->lookdev_cube_mips = stl->lookdev_lightcache->cube_mips;
      txl->lookdev_grid_tx = stl->lookdev_lightcache->grid_tx.tex;
      txl->lookdev_cube_tx = stl->lookdev_lightcache->cube_tx.tex;
    }

    g_data->light_cache = stl->lookdev_lightcache;

    DRWShadingGroup *grp = DRW_shgroup_create(shader, pass);
    axis_angle_to_mat3_single(g_data->studiolight_matrix, 'Z', shading->studiolight_rot_z);
    DRW_shgroup_uniform_mat3(grp, "StudioLightMatrix", g_data->studiolight_matrix);

    if (probe_render) {
      /* Avoid artifact with equirectangular mapping. */
      eGPUSamplerState state = (GPU_SAMPLER_FILTER | GPU_SAMPLER_REPEAT_S);
      DRW_shgroup_uniform_float_copy(grp, "studioLightIntensity", shading->studiolight_intensity);
      BKE_studiolight_ensure_flag(sl, STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE);
      DRW_shgroup_uniform_texture_ex(grp, "studioLight", sl->equirect_radiance_gputexture, state);
      /* Do not fadeout when doing probe rendering, only when drawing the background */
      DRW_shgroup_uniform_float_copy(grp, "backgroundAlpha", 1.0f);
    }
    else {
      float background_alpha = g_data->background_alpha * shading->studiolight_background;
      float studiolight_blur = powf(shading->studiolight_blur, 2.5f);
      DRW_shgroup_uniform_float_copy(grp, "backgroundAlpha", background_alpha);
      DRW_shgroup_uniform_float_copy(grp, "studioLightBlur", studiolight_blur);
      DRW_shgroup_uniform_texture(grp, "probeCubes", txl->lookdev_cube_tx);
    }

    /* Common UBOs are setup latter. */
    *r_shgrp = grp;

    /* Do we need to recalc the lightprobes? */
    if (g_data->studiolight_index != sl->index ||
        g_data->studiolight_rot_z != shading->studiolight_rot_z ||
        g_data->studiolight_intensity != shading->studiolight_intensity ||
        g_data->studiolight_cubemap_res != scene->eevee.gi_cubemap_resolution ||
        g_data->studiolight_glossy_clamp != scene->eevee.gi_glossy_clamp ||
        g_data->studiolight_filter_quality != scene->eevee.gi_filter_quality) {
      stl->lookdev_lightcache->flag |= LIGHTCACHE_UPDATE_WORLD;
      g_data->studiolight_index = sl->index;
      g_data->studiolight_rot_z = shading->studiolight_rot_z;
      g_data->studiolight_intensity = shading->studiolight_intensity;
      g_data->studiolight_cubemap_res = scene->eevee.gi_cubemap_resolution;
      g_data->studiolight_glossy_clamp = scene->eevee.gi_glossy_clamp;
      g_data->studiolight_filter_quality = scene->eevee.gi_filter_quality;
    }
  }
}

static void eevee_lookdev_apply_taa(const EEVEE_EffectsInfo *effects,
                                    int sphere_size,
                                    float winmat[4][4])
{
  if (DRW_state_is_image_render() || ((effects->enabled_effects & EFFECT_TAA) != 0)) {
    double ht_point[2];
    double ht_offset[2] = {0.0, 0.0};
    const uint ht_primes[2] = {2, 3};
    float ofs[2];

    BLI_halton_2d(ht_primes, ht_offset, effects->taa_current_sample, ht_point);
    EEVEE_temporal_sampling_offset_calc(ht_point, 1.5f, ofs);
    winmat[3][0] += ofs[0] / sphere_size;
    winmat[3][1] += ofs[1] / sphere_size;
  }
}

void EEVEE_lookdev_draw(EEVEE_Data *vedata)
{
  EEVEE_PassList *psl = vedata->psl;
  EEVEE_FramebufferList *fbl = vedata->fbl;
  EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
  EEVEE_EffectsInfo *effects = stl->effects;
  EEVEE_ViewLayerData *sldata = EEVEE_view_layer_data_ensure();

  const DRWContextState *draw_ctx = DRW_context_state_get();

  if (psl->lookdev_diffuse_pass && eevee_hdri_preview_overlay_enabled(draw_ctx->v3d)) {
    /* Config renderer. */
    EEVEE_CommonUniformBuffer *common = &sldata->common_data;
    common->la_num_light = 0;
    common->prb_num_planar = 0;
    common->prb_num_render_cube = 1;
    common->prb_num_render_grid = 1;
    common->ao_dist = 0.0f;
    common->ao_factor = 0.0f;
    common->ao_settings = 0.0f;
    DRW_uniformbuffer_update(sldata->common_ubo, common);

    /* override matrices */
    float winmat[4][4], viewmat[4][4];
    unit_m4(winmat);
    /* Look through the negative Z. */
    negate_v3(winmat[2]);

    eevee_lookdev_apply_taa(effects, effects->sphere_size, winmat);

    /* "Remove" view matrix location. Leaving only rotation. */
    DRW_view_viewmat_get(NULL, viewmat, false);
    zero_v3(viewmat[3]);

    if (effects->lookdev_view) {
      /* When rendering just update the view. This avoids recomputing the culling. */
      DRW_view_update_sub(effects->lookdev_view, viewmat, winmat);
    }
    else {
      /* Using default view bypasses the culling. */
      const DRWView *default_view = DRW_view_default_get();
      effects->lookdev_view = DRW_view_create_sub(default_view, viewmat, winmat);
    }

    DRW_view_set_active(effects->lookdev_view);

    /* Find the right framebuffers to render to. */
    GPUFrameBuffer *fb = (effects->target_buffer == fbl->effect_color_fb) ? fbl->main_fb :
                                                                            fbl->effect_fb;

    DRW_stats_group_start("Look Dev");

    GPU_framebuffer_bind(fb);

    const int sphere_margin = effects->sphere_size / 6.0f;
    float offset[2] = {0.0f, sphere_margin};

    offset[0] = effects->sphere_size + sphere_margin;
    GPU_framebuffer_viewport_set(fb,
                                 effects->anchor[0] - offset[0],
                                 effects->anchor[1] + offset[1],
                                 effects->sphere_size,
                                 effects->sphere_size);

    DRW_draw_pass(psl->lookdev_diffuse_pass);

    offset[0] = (effects->sphere_size + sphere_margin) +
                (sphere_margin + effects->sphere_size + sphere_margin);
    GPU_framebuffer_viewport_set(fb,
                                 effects->anchor[0] - offset[0],
                                 effects->anchor[1] + offset[1],
                                 effects->sphere_size,
                                 effects->sphere_size);

    DRW_draw_pass(psl->lookdev_glossy_pass);

    DRW_stats_group_end();

    DRW_view_set_active(NULL);
  }
}