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

workbench_engine.c « workbench « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ac0a4e6221a5d21e93481d93c00d5d76612acb48 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 * 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
 *
 * Optimized engine to draw the working viewport with solid and transparent geometry.
 */

#include "DRW_render.h"

#include "BLI_alloca.h"

#include "BKE_modifier.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_particle.h"

#include "DNA_image_types.h"
#include "DNA_fluid_types.h"
#include "DNA_mesh_types.h"
#include "DNA_modifier_types.h"
#include "DNA_node_types.h"

#include "workbench_engine.h"
#include "workbench_private.h"

#define WORKBENCH_ENGINE "BLENDER_WORKBENCH"

static void workbench_engine_init(void *ved)
{
  WORKBENCH_Data *vedata = ved;
  WORKBENCH_StorageList *stl = vedata->stl;
  WORKBENCH_TextureList *txl = vedata->txl;

  workbench_shader_library_ensure();

  if (!stl->wpd) {
    stl->wpd = MEM_callocN(sizeof(*stl->wpd), __func__);
  }

  if (!stl->effects) {
    stl->effects = MEM_callocN(sizeof(*stl->effects), __func__);
    workbench_effect_info_init(stl->effects);
  }

  WORKBENCH_PrivateData *wpd = stl->wpd;
  workbench_private_data_init(wpd);

  if (txl->dummy_image_tx == NULL) {
    float fpixel[4] = {1.0f, 0.0f, 1.0f, 1.0f};
    txl->dummy_image_tx = DRW_texture_create_2d(1, 1, GPU_RGBA8, 0, fpixel);
  }
  wpd->dummy_image_tx = txl->dummy_image_tx;

  workbench_opaque_engine_init(vedata);
  workbench_transparent_engine_init(vedata);
  //   workbench_volume_engine_init();
  //   workbench_fxaa_engine_init();
  //   workbench_taa_engine_init(vedata);
  //   workbench_dof_engine_init(vedata, camera);
}

static void workbench_cache_init(void *ved)
{
  WORKBENCH_Data *vedata = ved;

  workbench_opaque_cache_init(vedata);
  workbench_transparent_cache_init(vedata);
  workbench_shadow_cache_init(vedata);

  //   workbench_aa_create_pass(vedata);
  //   workbench_dof_create_pass(vedata);
}

/* TODO(fclem) DRW_cache_object_surface_material_get needs a refactor to allow passing NULL
 * instead of gpumat_array. Avoiding all this boilerplate code. */
static struct GPUBatch **workbench_object_surface_material_get(Object *ob)
{
  const int materials_len = DRW_cache_object_material_count_get(ob);
  struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
  memset(gpumat_array, 0, sizeof(*gpumat_array) * materials_len);

  return DRW_cache_object_surface_material_get(ob, gpumat_array, materials_len);
}

static void workbench_cache_sculpt_populate(WORKBENCH_PrivateData *wpd, Object *ob, int color_type)
{
  const bool use_vcol = ELEM(color_type, V3D_SHADING_VERTEX_COLOR);
  const bool use_single_drawcall = !ELEM(color_type, V3D_SHADING_MATERIAL_COLOR);
  BLI_assert(wpd->shading.color_type != V3D_SHADING_TEXTURE_COLOR);

  if (use_single_drawcall) {
    DRWShadingGroup *grp = workbench_material_setup(wpd, ob, 0, color_type, NULL);
    DRW_shgroup_call_sculpt(grp, ob, false, false, use_vcol);
  }
  else {
    const int materials_len = DRW_cache_object_material_count_get(ob);
    struct DRWShadingGroup **shgrps = BLI_array_alloca(shgrps, materials_len);
    for (int i = 0; i < materials_len; i++) {
      shgrps[i] = workbench_material_setup(wpd, ob, i + 1, color_type, NULL);
    }
    DRW_shgroup_call_sculpt_with_materials(shgrps, ob, false);
  }
}

static void workbench_cache_texpaint_populate(WORKBENCH_PrivateData *wpd, Object *ob)
{
  const DRWContextState *draw_ctx = DRW_context_state_get();
  const Scene *scene = draw_ctx->scene;
  const ImagePaintSettings *imapaint = &scene->toolsettings->imapaint;
  const bool use_single_drawcall = imapaint->mode == IMAGEPAINT_MODE_IMAGE;

  if (use_single_drawcall) {
    struct GPUBatch *geom = DRW_cache_mesh_surface_texpaint_single_get(ob);
    if (geom) {
      Image *ima = imapaint->canvas;
      int interp = (imapaint->interp == IMAGEPAINT_INTERP_LINEAR) ? SHD_INTERP_LINEAR :
                                                                    SHD_INTERP_CLOSEST;

      DRWShadingGroup *grp = workbench_image_setup(wpd, ob, 0, ima, NULL, interp);
      DRW_shgroup_call(grp, geom, ob);
    }
  }
  else {
    struct GPUBatch **geoms = DRW_cache_mesh_surface_texpaint_get(ob);
    if (geoms) {
      const int materials_len = DRW_cache_object_material_count_get(ob);
      for (int i = 0; i < materials_len; i++) {
        DRWShadingGroup *grp = workbench_image_setup(wpd, ob, i + 1, NULL, NULL, 0);
        DRW_shgroup_call(grp, geoms[i], ob);
      }
    }
  }
}

static void workbench_cache_common_populate(WORKBENCH_PrivateData *wpd,
                                            Object *ob,
                                            int color_type,
                                            bool *r_transp)
{
  const bool use_tex = ELEM(color_type, V3D_SHADING_TEXTURE_COLOR);
  const bool use_vcol = ELEM(color_type, V3D_SHADING_VERTEX_COLOR);
  const bool use_single_drawcall = !ELEM(
      color_type, V3D_SHADING_MATERIAL_COLOR, V3D_SHADING_TEXTURE_COLOR);

  if (use_single_drawcall) {
    struct GPUBatch *geom = (use_vcol) ? DRW_cache_mesh_surface_vertpaint_get(ob) :
                                         DRW_cache_object_surface_get(ob);
    if (geom) {
      DRWShadingGroup *grp = workbench_material_setup(wpd, ob, 0, color_type, r_transp);
      DRW_shgroup_call(grp, geom, ob);
    }
  }
  else {
    struct GPUBatch **geoms = (use_tex) ? DRW_cache_mesh_surface_texpaint_get(ob) :
                                          workbench_object_surface_material_get(ob);
    if (geoms) {
      const int materials_len = DRW_cache_object_material_count_get(ob);
      for (int i = 0; i < materials_len; i++) {
        DRWShadingGroup *grp = workbench_material_setup(wpd, ob, i + 1, color_type, r_transp);
        DRW_shgroup_call(grp, geoms[i], ob);
      }
    }
  }
}

static void workbench_cache_hair_populate(WORKBENCH_PrivateData *wpd,
                                          Object *ob,
                                          ModifierData *md,
                                          int color_type,
                                          bool use_texpaint_mode)
{
  ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
  ParticleSettings *part = psys->part;
  const DRWContextState *draw_ctx = DRW_context_state_get();
  const Scene *scene = draw_ctx->scene;

  const ImagePaintSettings *imapaint = use_texpaint_mode ? &scene->toolsettings->imapaint : NULL;
  Image *ima = (imapaint && imapaint->mode == IMAGEPAINT_MODE_IMAGE) ? imapaint->canvas : NULL;
  int interp = (imapaint && imapaint->interp == IMAGEPAINT_INTERP_LINEAR) ? SHD_INTERP_LINEAR :
                                                                            SHD_INTERP_CLOSEST;
  DRWShadingGroup *grp = (use_texpaint_mode) ?
                             workbench_image_hair_setup(wpd, ob, part->omat, ima, NULL, interp) :
                             workbench_material_hair_setup(wpd, ob, part->omat, color_type);

  DRW_shgroup_hair_create_sub(ob, psys, md, grp);
}

/* Decide what colortype to draw the object with.
 * In some cases it can be overwritten by workbench_material_setup(). */
static int workbench_color_type_get(WORKBENCH_PrivateData *wpd,
                                    Object *ob,
                                    bool *r_sculpt_pbvh,
                                    bool *r_texpaint_mode,
                                    bool *r_draw_shadow)
{
  int color_type = wpd->shading.color_type;
  const Mesh *me = (ob->type == OB_MESH) ? ob->data : NULL;

  const DRWContextState *draw_ctx = DRW_context_state_get();
  const bool is_active = (ob == draw_ctx->obact);
  const bool is_sculpt_pbvh = BKE_sculptsession_use_pbvh_draw(ob, draw_ctx->v3d) &&
                              !DRW_state_is_image_render();
  const bool is_render = DRW_state_is_image_render() && (draw_ctx->v3d == NULL);
  const bool is_texpaint_mode = is_active && (wpd->ctx_mode == CTX_MODE_PAINT_TEXTURE);
  const bool is_vertpaint_mode = is_active && (wpd->ctx_mode == CTX_MODE_PAINT_VERTEX);

  if ((color_type == V3D_SHADING_TEXTURE_COLOR) && (ob->dt < OB_TEXTURE)) {
    color_type = V3D_SHADING_MATERIAL_COLOR;
  }
  /* Disable color mode if data layer is unavailable. */
  if ((color_type == V3D_SHADING_TEXTURE_COLOR) && (me == NULL || me->mloopuv == NULL)) {
    color_type = V3D_SHADING_MATERIAL_COLOR;
  }
  if ((color_type == V3D_SHADING_VERTEX_COLOR) && (me == NULL || me->mloopcol == NULL)) {
    color_type = V3D_SHADING_OBJECT_COLOR;
  }

  *r_sculpt_pbvh = is_sculpt_pbvh;
  *r_texpaint_mode = false;

  if (!is_sculpt_pbvh && !is_render) {
    /* Force texture or vertex mode if object is in paint mode. */
    if (is_texpaint_mode && me && me->mloopuv) {
      color_type = V3D_SHADING_TEXTURE_COLOR;
      *r_texpaint_mode = true;
    }
    else if (is_vertpaint_mode && me && me->mloopcol) {
      color_type = V3D_SHADING_VERTEX_COLOR;
    }
  }

  if (r_draw_shadow) {
    *r_draw_shadow = (ob->dtx & OB_DRAW_NO_SHADOW_CAST) == 0 && SHADOW_ENABLED(wpd);
    /* Currently unsupported in sculpt mode. We could revert to the slow
     * method in this case but I'm not sure if it's a good idea given that
     * sculpted meshes are heavy to begin with. */
    if (is_sculpt_pbvh) {
      *r_draw_shadow = false;
    }

    if (is_active && DRW_object_use_hide_faces(ob)) {
      *r_draw_shadow = false;
    }
  }

  return color_type;
}

static void workbench_cache_populate(void *ved, Object *ob)
{
  WORKBENCH_Data *vedata = ved;
  WORKBENCH_StorageList *stl = vedata->stl;
  WORKBENCH_PrivateData *wpd = stl->wpd;

  if (!DRW_object_is_renderable(ob)) {
    return;
  }

  if (ob->type == OB_MESH && ob->modifiers.first != NULL) {
    bool use_sculpt_pbvh, use_texpaint_mode;
    int color_type = workbench_color_type_get(wpd, ob, &use_sculpt_pbvh, &use_texpaint_mode, NULL);

    LISTBASE_FOREACH (ModifierData *, md, &ob->modifiers) {
      if (md->type != eModifierType_ParticleSystem) {
        continue;
      }
      ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
      if (!DRW_object_is_visible_psys_in_active_context(ob, psys)) {
        continue;
      }
      ParticleSettings *part = psys->part;
      const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;

      if (draw_as == PART_DRAW_PATH) {
        workbench_cache_hair_populate(wpd, ob, md, color_type, use_texpaint_mode);
      }
    }
  }

  /* TODO volume */

  //   if (!(DRW_object_visibility_in_active_context(ob) & OB_VISIBLE_SELF)) {
  //     return;
  //   }

  if ((ob->dt < OB_SOLID) && !DRW_state_is_scene_render()) {
    return;
  }

  if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL)) {
    bool use_sculpt_pbvh, use_texpaint_mode, draw_shadow, has_transp_mat = false;
    int color_type = workbench_color_type_get(
        wpd, ob, &use_sculpt_pbvh, &use_texpaint_mode, &draw_shadow);

    if (use_sculpt_pbvh) {
      workbench_cache_sculpt_populate(wpd, ob, color_type);
    }
    else if (use_texpaint_mode) {
      workbench_cache_texpaint_populate(wpd, ob);
    }
    else {
      workbench_cache_common_populate(wpd, ob, color_type, &has_transp_mat);
    }

    if (draw_shadow) {
      workbench_shadow_cache_populate(vedata, ob, has_transp_mat);
    }
  }
}

static void workbench_cache_finish(void *ved)
{
  WORKBENCH_Data *vedata = ved;
  WORKBENCH_StorageList *stl = vedata->stl;
  WORKBENCH_FramebufferList *fbl = vedata->fbl;
  WORKBENCH_PrivateData *wpd = stl->wpd;

  /* TODO(fclem) Only do this when really needed. */
  {
    /* HACK we allocate the infront depth here to avoid the overhead when if is not needed. */
    DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
    DefaultTextureList *dtxl = DRW_viewport_texture_list_get();

    DRW_texture_ensure_fullscreen_2d(&dtxl->depth_in_front, GPU_DEPTH24_STENCIL8, 0);

    GPU_framebuffer_ensure_config(&dfbl->in_front_fb,
                                  {
                                      GPU_ATTACHMENT_TEXTURE(dtxl->depth_in_front),
                                      GPU_ATTACHMENT_TEXTURE(dtxl->color),
                                  });

    GPU_framebuffer_ensure_config(&fbl->opaque_infront_fb,
                                  {
                                      GPU_ATTACHMENT_TEXTURE(dtxl->depth_in_front),
                                      GPU_ATTACHMENT_TEXTURE(wpd->material_buffer_tx),
                                      GPU_ATTACHMENT_TEXTURE(wpd->normal_buffer_tx),
                                      GPU_ATTACHMENT_TEXTURE(wpd->object_id_tx),
                                  });

    GPU_framebuffer_ensure_config(&fbl->transp_accum_infront_fb,
                                  {
                                      GPU_ATTACHMENT_TEXTURE(dtxl->depth_in_front),
                                      GPU_ATTACHMENT_TEXTURE(wpd->accum_buffer_tx),
                                      GPU_ATTACHMENT_TEXTURE(wpd->reveal_buffer_tx),
                                  });
  }

  workbench_update_material_ubos(wpd);

  /* TODO don't free reuse next redraw. */
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) {
        if (wpd->prepass[i][j][k].material_hash) {
          BLI_ghash_free(wpd->prepass[i][j][k].material_hash, NULL, NULL);
          wpd->prepass[i][j][k].material_hash = NULL;
        }
      }
    }
  }
}

static void workbench_draw_scene(void *ved)
{
  WORKBENCH_Data *vedata = ved;
  WORKBENCH_FramebufferList *fbl = vedata->fbl;
  WORKBENCH_PassList *psl = vedata->psl;
  DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
  float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
  float clear_col_with_alpha[4] = {0.0f, 0.0f, 0.0f, 1.0f};

  {
    GPU_framebuffer_bind(dfbl->in_front_fb);
    GPU_framebuffer_clear_depth(dfbl->in_front_fb, 1.0f);
  }

  GPU_framebuffer_bind(dfbl->color_only_fb);
  GPU_framebuffer_clear_color(dfbl->color_only_fb, clear_col);

  {
    GPU_framebuffer_bind(fbl->opaque_fb);
    GPU_framebuffer_clear_stencil(fbl->opaque_fb, 0x00);
    DRW_draw_pass(psl->opaque_pass);

    if (psl->shadow_pass[0]) {
      DRW_draw_pass(psl->shadow_pass[0]);
      DRW_draw_pass(psl->shadow_pass[1]);
    }

    {
      GPU_framebuffer_bind(fbl->opaque_infront_fb);
      DRW_draw_pass(psl->opaque_infront_pass);

      GPU_framebuffer_bind(fbl->opaque_fb);
      DRW_draw_pass(psl->merge_infront_pass);
    }

    GPU_framebuffer_bind(dfbl->default_fb);
    DRW_draw_pass(psl->composite_pass);

    /* TODO(fclem) ambient occlusion */
    // GPU_framebuffer_bind(dfbl->color_only_fb);
    // DRW_draw_pass(psl->ambient_occlusion_pass);
  }

  {
    GPU_framebuffer_bind(fbl->transp_accum_fb);
    GPU_framebuffer_clear_color(fbl->transp_accum_fb, clear_col_with_alpha);

    DRW_draw_pass(psl->transp_accum_pass);

    GPU_framebuffer_bind(dfbl->color_only_fb);
    DRW_draw_pass(psl->transp_resolve_pass);

    {
      GPU_framebuffer_bind(fbl->transp_accum_infront_fb);
      GPU_framebuffer_clear_color(fbl->transp_accum_infront_fb, clear_col_with_alpha);

      DRW_draw_pass(psl->transp_accum_infront_pass);

      GPU_framebuffer_bind(dfbl->color_only_fb);
      DRW_draw_pass(psl->transp_resolve_pass);
    }
  }

  /* TODO(fclem) outline */
  // DRW_draw_pass(psl->outline_pass);

  /* TODO(fclem) dof */

  /* TODO(fclem) antialias */
}

static void workbench_engine_free(void)
{
  workbench_shader_free();
}

static void workbench_view_update(void *UNUSED(ved))
{
}

static void workbench_id_update(void *UNUSED(vedata), struct ID *id)
{
  if (GS(id->name) == ID_OB) {
    WORKBENCH_ObjectData *oed = (WORKBENCH_ObjectData *)DRW_drawdata_get(
        id, &draw_engine_workbench_solid);
    if (oed != NULL && oed->dd.recalc != 0) {
      oed->shadow_bbox_dirty = (oed->dd.recalc & ID_RECALC_ALL) != 0;
      oed->dd.recalc = 0;
    }
  }
}

static const DrawEngineDataSize workbench_data_size = DRW_VIEWPORT_DATA_SIZE(WORKBENCH_Data);

DrawEngineType draw_engine_workbench = {
    NULL,
    NULL,
    N_("Workbench"),
    &workbench_data_size,
    &workbench_engine_init,
    &workbench_engine_free,
    &workbench_cache_init,
    &workbench_cache_populate,
    &workbench_cache_finish,
    &workbench_draw_scene,
    &workbench_view_update,
    &workbench_id_update,
    NULL,
};

RenderEngineType DRW_engine_viewport_workbench_type = {
    NULL,
    NULL,
    WORKBENCH_ENGINE,
    N_("Workbench"),
    RE_INTERNAL,
    NULL,
    &DRW_render_to_image,
    NULL,
    NULL,
    NULL,
    NULL,
    &workbench_render_update_passes,
    &draw_engine_workbench,
    {NULL, NULL, NULL},
};

#undef WORKBENCH_ENGINE