Blender  V2.93
eevee_cryptomatte.c
Go to the documentation of this file.
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 2020, Blender Foundation.
17  */
18 
46 #include "DRW_engine.h"
47 #include "DRW_render.h"
48 
49 #include "BKE_cryptomatte.h"
50 
51 #include "GPU_batch.h"
52 
53 #include "RE_pipeline.h"
54 
55 #include "BLI_alloca.h"
56 #include "BLI_math_bits.h"
57 #include "BLI_rect.h"
58 
59 #include "DNA_hair_types.h"
60 #include "DNA_mesh_types.h"
61 #include "DNA_modifier_types.h"
62 #include "DNA_particle_types.h"
63 
64 #include "eevee_private.h"
65 
66 /* -------------------------------------------------------------------- */
71 {
72  const eViewLayerCryptomatteFlags cryptomatte_layers = view_layer->cryptomatte_flag &
74  return cryptomatte_layers;
75 }
76 
77 /* The number of cryptomatte layers that are enabled */
79 {
81  view_layer);
82  return count_bits_i(cryptomatte_layers);
83 }
84 
85 /* The number of render result passes are needed to store a single cryptomatte layer. Per
86  * renderpass 2 cryptomatte samples can be stored. */
88 {
89  const int num_cryptomatte_levels = view_layer->cryptomatte_levels;
90  const int num_cryptomatte_passes = (num_cryptomatte_levels + 1) / 2;
91  return num_cryptomatte_passes;
92 }
93 
95 {
96  return view_layer->cryptomatte_levels;
97 }
98 
99 BLI_INLINE int eevee_cryptomatte_layer_offset(const ViewLayer *view_layer, const int layer)
100 {
101  return view_layer->cryptomatte_levels * layer;
102 }
103 
105 {
106  return eevee_cryptomatte_layer_stride(view_layer) * eevee_cryptomatte_layers_count(view_layer);
107 }
108 
111 /* -------------------------------------------------------------------- */
116 {
117  EEVEE_StorageList *stl = vedata->stl;
118  EEVEE_PrivateData *g_data = stl->g_data;
119 
120  const DRWContextState *draw_ctx = DRW_context_state_get();
121  ViewLayer *view_layer = draw_ctx->view_layer;
122 
123  /* Cryptomatte is only rendered for final image renders */
124  if (!DRW_state_is_scene_render()) {
125  return;
126  }
127  const eViewLayerCryptomatteFlags active_layers = eevee_cryptomatte_active_layers(view_layer);
128  if (active_layers) {
129  struct CryptomatteSession *session = BKE_cryptomatte_init();
130  if ((active_layers & VIEW_LAYER_CRYPTOMATTE_OBJECT) != 0) {
131  BKE_cryptomatte_add_layer(session, "CryptoObject");
132  }
133  if ((active_layers & VIEW_LAYER_CRYPTOMATTE_MATERIAL) != 0) {
134  BKE_cryptomatte_add_layer(session, "CryptoMaterial");
135  }
136  if ((active_layers & VIEW_LAYER_CRYPTOMATTE_ASSET) != 0) {
137  BKE_cryptomatte_add_layer(session, "CryptoAsset");
138  }
139  g_data->cryptomatte_session = session;
140 
142  g_data->cryptomatte_accurate_mode = (view_layer->cryptomatte_flag &
144  }
145 }
146 
148  EEVEE_Data *vedata,
149  int UNUSED(tot_samples))
150 {
151  EEVEE_FramebufferList *fbl = vedata->fbl;
152  EEVEE_TextureList *txl = vedata->txl;
153  EEVEE_StorageList *stl = vedata->stl;
154  EEVEE_PrivateData *g_data = stl->g_data;
155 
157  const DRWContextState *draw_ctx = DRW_context_state_get();
158  const ViewLayer *view_layer = draw_ctx->view_layer;
159 
160  const int num_cryptomatte_layers = eevee_cryptomatte_layers_count(view_layer);
161  eGPUTextureFormat format = (num_cryptomatte_layers == 1) ?
162  GPU_R32F :
163  (num_cryptomatte_layers == 2) ? GPU_RG32F : GPU_RGBA32F;
164  const float *viewport_size = DRW_viewport_size_get();
165  const int buffer_size = viewport_size[0] * viewport_size[1];
166 
167  if (g_data->cryptomatte_accum_buffer == NULL) {
169  sizeof(EEVEE_CryptomatteSample),
170  buffer_size * eevee_cryptomatte_pixel_stride(view_layer),
171  __func__);
172  /* Download buffer should store a float per active cryptomatte layer. */
174  sizeof(float), buffer_size * num_cryptomatte_layers, __func__);
175  }
176  else {
177  /* During multiview rendering the `cryptomatte_accum_buffer` is deallocated after all views
178  * have been rendered. Clear it here to be reused by the next view. */
179  memset(g_data->cryptomatte_accum_buffer,
180  0,
181  buffer_size * eevee_cryptomatte_pixel_stride(view_layer) *
182  sizeof(EEVEE_CryptomatteSample));
183  }
184 
186  GPU_framebuffer_ensure_config(&fbl->cryptomatte_fb,
187  {
188  GPU_ATTACHMENT_TEXTURE(dtxl->depth),
189  GPU_ATTACHMENT_TEXTURE(txl->cryptomatte),
190  });
191 }
192 
195 /* -------------------------------------------------------------------- */
200 {
201  EEVEE_PassList *psl = vedata->psl;
202  if ((vedata->stl->g_data->render_passes & EEVEE_RENDER_PASS_CRYPTOMATTE) != 0) {
204  }
205 }
206 
208  EEVEE_ViewLayerData *UNUSED(sldata),
209  Object *ob,
211  bool is_hair)
212 {
213  const DRWContextState *draw_ctx = DRW_context_state_get();
214  const ViewLayer *view_layer = draw_ctx->view_layer;
216  view_layer);
217  EEVEE_PrivateData *g_data = vedata->stl->g_data;
218  float cryptohash[4] = {0.0f};
219 
220  EEVEE_PassList *psl = vedata->psl;
221  int layer_offset = 0;
222  if ((cryptomatte_layers & VIEW_LAYER_CRYPTOMATTE_OBJECT) != 0) {
223  uint32_t cryptomatte_hash = BKE_cryptomatte_object_hash(
224  g_data->cryptomatte_session, "CryptoObject", ob);
225  float cryptomatte_color_value = BKE_cryptomatte_hash_to_float(cryptomatte_hash);
226  cryptohash[layer_offset] = cryptomatte_color_value;
227  layer_offset++;
228  }
229  if ((cryptomatte_layers & VIEW_LAYER_CRYPTOMATTE_MATERIAL) != 0) {
230  uint32_t cryptomatte_hash = BKE_cryptomatte_material_hash(
231  g_data->cryptomatte_session, "CryptoMaterial", material);
232  float cryptomatte_color_value = BKE_cryptomatte_hash_to_float(cryptomatte_hash);
233  cryptohash[layer_offset] = cryptomatte_color_value;
234  layer_offset++;
235  }
236  if ((cryptomatte_layers & VIEW_LAYER_CRYPTOMATTE_ASSET) != 0) {
237  uint32_t cryptomatte_hash = BKE_cryptomatte_asset_hash(
238  g_data->cryptomatte_session, "CryptoAsset", ob);
239  float cryptomatte_color_value = BKE_cryptomatte_hash_to_float(cryptomatte_hash);
240  cryptohash[layer_offset] = cryptomatte_color_value;
241  layer_offset++;
242  }
243 
245  psl->cryptomatte_ps);
246  DRW_shgroup_uniform_vec4_copy(grp, "cryptohash", cryptohash);
247 
248  return grp;
249 }
250 
252  EEVEE_ViewLayerData *sldata,
253  Object *ob,
254  ParticleSystem *psys,
255  ModifierData *md,
257 {
259  vedata, sldata, ob, material, true);
260  DRW_shgroup_hair_create_sub(ob, psys, md, grp);
261 }
262 
264  EEVEE_ViewLayerData *sldata,
265  Object *ob)
266 {
267  BLI_assert(ob->type == OB_HAIR);
268  Hair *hair = ob->data;
269  Material *material = hair->mat ? hair->mat[HAIR_MATERIAL_NR - 1] : NULL;
271 }
272 
274  EEVEE_ViewLayerData *sldata,
275  Object *ob)
276 {
277  const DRWContextState *draw_ctx = DRW_context_state_get();
278 
279  if (ob->type == OB_MESH) {
280  if (ob != draw_ctx->object_edit) {
281  LISTBASE_FOREACH (ModifierData *, md, &ob->modifiers) {
282  if (md->type != eModifierType_ParticleSystem) {
283  continue;
284  }
285  ParticleSystem *psys = ((ParticleSystemModifierData *)md)->psys;
287  continue;
288  }
289  ParticleSettings *part = psys->part;
290  const int draw_as = (part->draw_as == PART_DRAW_REND) ? part->ren_as : part->draw_as;
291  if (draw_as != PART_DRAW_PATH) {
292  continue;
293  }
294  Mesh *mesh = ob->data;
295  Material *material = part->omat - 1 < mesh->totcol ? NULL : mesh->mat[part->omat - 1];
296  eevee_cryptomatte_hair_cache_populate(vedata, sldata, ob, psys, md, material);
297  }
298  }
299  }
300 }
301 
303 {
304  const DRWContextState *draw_ctx = DRW_context_state_get();
305  const ViewLayer *view_layer = draw_ctx->view_layer;
307  view_layer);
308 
309  if ((cryptomatte_layers & VIEW_LAYER_CRYPTOMATTE_MATERIAL) != 0) {
310  const int materials_len = DRW_cache_object_material_count_get(ob);
311  struct GPUMaterial **gpumat_array = BLI_array_alloca(gpumat_array, materials_len);
312  memset(gpumat_array, 0, sizeof(*gpumat_array) * materials_len);
314  ob, gpumat_array, materials_len);
315  if (geoms) {
316  for (int i = 0; i < materials_len; i++) {
317  struct GPUBatch *geom = geoms[i];
318  if (geom == NULL) {
319  continue;
320  }
323  vedata, sldata, ob, material, false);
324  DRW_shgroup_call(grp, geom, ob);
325  }
326  }
327  }
328  else {
330  if (geom) {
332  vedata, sldata, ob, NULL, false);
333  DRW_shgroup_call(grp, geom, ob);
334  }
335  }
336 }
337 
340 /* -------------------------------------------------------------------- */
344 /* Downloads cryptomatte sample buffer from the GPU and integrate the samples with the accumulated
345  * cryptomatte samples. */
347 {
348  EEVEE_StorageList *stl = vedata->stl;
349  EEVEE_PrivateData *g_data = stl->g_data;
350  const DRWContextState *draw_ctx = DRW_context_state_get();
351  const ViewLayer *view_layer = draw_ctx->view_layer;
352  const int num_cryptomatte_layers = eevee_cryptomatte_layers_count(view_layer);
353  const int num_levels = view_layer->cryptomatte_levels;
354  const float *viewport_size = DRW_viewport_size_get();
355  const int buffer_size = viewport_size[0] * viewport_size[1];
356 
357  EEVEE_CryptomatteSample *accum_buffer = g_data->cryptomatte_accum_buffer;
358  float *download_buffer = g_data->cryptomatte_download_buffer;
359 
360  BLI_assert(accum_buffer);
361  BLI_assert(download_buffer);
362 
363  GPU_framebuffer_read_color(framebuffer,
364  0,
365  0,
366  viewport_size[0],
367  viewport_size[1],
368  num_cryptomatte_layers,
369  0,
371  download_buffer);
372 
373  /* Integrate download buffer into the accum buffer.
374  * The download buffer contains up to 3 floats per pixel (one float per cryptomatte layer.
375  *
376  * NOTE: here we deviate from the cryptomatte standard. During integration the standard always
377  * sort the samples by its weight to make sure that samples with the lowest weight
378  * are discarded first. In our case the weight of each sample is always 1 as we don't have
379  * subsamples and apply the coverage during the post processing. When there is no room for new
380  * samples the new samples has a weight of 1 and will always be discarded. */
381  int download_pixel_index = 0;
382  int accum_pixel_index = 0;
383  int accum_pixel_stride = eevee_cryptomatte_pixel_stride(view_layer);
384  for (int pixel_index = 0; pixel_index < buffer_size; pixel_index++) {
385  for (int layer = 0; layer < num_cryptomatte_layers; layer++) {
386  const int layer_offset = eevee_cryptomatte_layer_offset(view_layer, layer);
387  float download_hash = download_buffer[download_pixel_index++];
388  for (int level = 0; level < num_levels; level++) {
389  EEVEE_CryptomatteSample *sample = &accum_buffer[accum_pixel_index + layer_offset + level];
390  if (sample->hash == download_hash) {
391  sample->weight += 1.0f;
392  break;
393  }
394  /* We test against weight as hash 0.0f is used for samples hitting the world background. */
395  if (sample->weight == 0.0f) {
396  sample->hash = download_hash;
397  sample->weight = 1.0f;
398  break;
399  }
400  }
401  }
402  accum_pixel_index += accum_pixel_stride;
403  }
404 }
405 
407 {
408  EEVEE_FramebufferList *fbl = vedata->fbl;
409  EEVEE_StorageList *stl = vedata->stl;
410  EEVEE_PrivateData *g_data = stl->g_data;
411  EEVEE_EffectsInfo *effects = stl->effects;
412  EEVEE_PassList *psl = vedata->psl;
413  const DRWContextState *draw_ctx = DRW_context_state_get();
414  const ViewLayer *view_layer = draw_ctx->view_layer;
415  const int cryptomatte_levels = view_layer->cryptomatte_levels;
416  const int current_sample = effects->taa_current_sample;
417 
418  /* In accurate mode all render samples are evaluated. In inaccurate mode this is limited to the
419  * number of cryptomatte levels. This will reduce the overhead of downloading the GPU buffer and
420  * integrating it into the accum buffer. */
421  if (g_data->cryptomatte_accurate_mode || current_sample < cryptomatte_levels) {
422  static float clear_color[4] = {0.0};
424  GPU_framebuffer_clear_color(fbl->cryptomatte_fb, clear_color);
426 
428 
429  /* Restore */
431  }
432 }
433 
436 /* -------------------------------------------------------------------- */
440 /* Register the render passes needed for cryptomatte
441  * normally this is done in `EEVEE_render_update_passes`, but it has been placed here to keep
442  * related code side-by-side for clarity. */
444 {
445  char cryptomatte_pass_name[MAX_NAME];
446  const short num_passes = eevee_cryptomatte_passes_per_layer(view_layer);
447  if ((view_layer->cryptomatte_flag & VIEW_LAYER_CRYPTOMATTE_OBJECT) != 0) {
448  for (short pass = 0; pass < num_passes; pass++) {
449  BLI_snprintf_rlen(cryptomatte_pass_name, MAX_NAME, "CryptoObject%02d", pass);
451  engine, scene, view_layer, cryptomatte_pass_name, 4, "RGBA", SOCK_RGBA);
452  }
453  }
454  if ((view_layer->cryptomatte_flag & VIEW_LAYER_CRYPTOMATTE_MATERIAL) != 0) {
455  for (short pass = 0; pass < num_passes; pass++) {
456  BLI_snprintf_rlen(cryptomatte_pass_name, MAX_NAME, "CryptoMaterial%02d", pass);
458  engine, scene, view_layer, cryptomatte_pass_name, 4, "RGBA", SOCK_RGBA);
459  }
460  }
461  if ((view_layer->cryptomatte_flag & VIEW_LAYER_CRYPTOMATTE_ASSET) != 0) {
462  for (short pass = 0; pass < num_passes; pass++) {
463  BLI_snprintf_rlen(cryptomatte_pass_name, MAX_NAME, "CryptoAsset%02d", pass);
465  engine, scene, view_layer, cryptomatte_pass_name, 4, "RGBA", SOCK_RGBA);
466  }
467  }
468 }
469 
472 /* -------------------------------------------------------------------- */
476 /* Compare function for cryptomatte samples. Samples with the highest weight will be at the
477  * beginning of the list. */
478 static int eevee_cryptomatte_sample_cmp_reverse(const void *a_, const void *b_)
479 {
480  const EEVEE_CryptomatteSample *a = a_;
481  const EEVEE_CryptomatteSample *b = b_;
482  if (a->weight < b->weight) {
483  return 1;
484  }
485  if (a->weight > b->weight) {
486  return -1;
487  }
488 
489  return 0;
490 }
491 
492 /* Post process the weights. The accumulated weights buffer adds one to each weight per sample.
493  * During post processing ensure that the total of weights per sample is between 0 and 1. */
495 {
496  EEVEE_StorageList *stl = vedata->stl;
497  EEVEE_PrivateData *g_data = stl->g_data;
498  EEVEE_EffectsInfo *effects = stl->effects;
499  EEVEE_TextureList *txl = vedata->txl;
500  const DRWContextState *draw_ctx = DRW_context_state_get();
501  const ViewLayer *view_layer = draw_ctx->view_layer;
502  const int num_cryptomatte_layers = eevee_cryptomatte_layers_count(view_layer);
503  const int num_levels = view_layer->cryptomatte_levels;
504  const float *viewport_size = DRW_viewport_size_get();
505  const int buffer_size = viewport_size[0] * viewport_size[1];
506 
507  EEVEE_CryptomatteSample *accum_buffer = g_data->cryptomatte_accum_buffer;
508  BLI_assert(accum_buffer);
509  float *volumetric_transmittance_buffer = NULL;
510  if ((effects->enabled_effects & EFFECT_VOLUMETRIC) != 0) {
511  volumetric_transmittance_buffer = GPU_texture_read(
513  }
514  const int num_samples = effects->taa_current_sample - 1;
515 
516  int accum_pixel_index = 0;
517  int accum_pixel_stride = eevee_cryptomatte_pixel_stride(view_layer);
518 
519  for (int pixel_index = 0; pixel_index < buffer_size;
520  pixel_index++, accum_pixel_index += accum_pixel_stride) {
521  float coverage = 1.0f;
522  if (volumetric_transmittance_buffer != NULL) {
523  coverage = (volumetric_transmittance_buffer[pixel_index * 4] +
524  volumetric_transmittance_buffer[pixel_index * 4 + 1] +
525  volumetric_transmittance_buffer[pixel_index * 4 + 2]) /
526  (3.0f * num_samples);
527  }
528  for (int layer = 0; layer < num_cryptomatte_layers; layer++) {
529  const int layer_offset = eevee_cryptomatte_layer_offset(view_layer, layer);
530  /* Calculate the total weight of the sample. */
531  float total_weight = 0.0f;
532  for (int level = 0; level < num_levels; level++) {
533  EEVEE_CryptomatteSample *sample = &accum_buffer[accum_pixel_index + layer_offset + level];
534  total_weight += sample->weight;
535  }
536  BLI_assert(total_weight > 0.0f);
537 
538  float total_weight_inv = coverage / total_weight;
539  if (total_weight_inv > 0.0f) {
540  for (int level = 0; level < num_levels; level++) {
542  &accum_buffer[accum_pixel_index + layer_offset + level];
543  /* Remove background samples. These samples were used to determine the correct weight
544  * but won't be part of the final result. */
545  if (sample->hash == 0.0f) {
546  sample->weight = 0.0f;
547  }
548  sample->weight *= total_weight_inv;
549  }
550 
551  /* Sort accum buffer by coverage of each sample. */
552  qsort(&accum_buffer[accum_pixel_index + layer_offset],
553  num_levels,
554  sizeof(EEVEE_CryptomatteSample),
556  }
557  else {
558  /* This pixel doesn't have any weight, so clear it fully. */
559  for (int level = 0; level < num_levels; level++) {
561  &accum_buffer[accum_pixel_index + layer_offset + level];
562  sample->weight = 0.0f;
563  sample->hash = 0.0f;
564  }
565  }
566  }
567  }
568 
569  if (volumetric_transmittance_buffer) {
570  MEM_freeN(volumetric_transmittance_buffer);
571  }
572 }
573 
574 /* Extract cryptomatte layer from the cryptomatte_accum_buffer to render passes. */
576  RenderLayer *rl,
577  const char *viewname,
578  const char *render_pass_name_format,
579  EEVEE_CryptomatteSample *accum_buffer,
580  /* number of render passes per cryptomatte layer. */
581  const int num_cryptomatte_passes,
582  const int num_cryptomatte_levels,
583  const int accum_pixel_stride,
584  const int layer_stride,
585  const int layer_index,
586  const int rect_width,
587  const int rect_height,
588  const int rect_offset_x,
589  const int rect_offset_y,
590  const int viewport_width)
591 {
592  char cryptomatte_pass_name[MAX_NAME];
593  /* A pass can store 2 levels. Technically the last pass can have a single level if the number of
594  * levels is an odd number. This parameter counts the number of levels it has processed. */
595  int levels_done = 0;
596  for (int pass = 0; pass < num_cryptomatte_passes; pass++) {
597  /* Each pass holds 2 cryptomatte samples. */
598  const int pass_offset = pass * 2;
599  BLI_snprintf_rlen(cryptomatte_pass_name, MAX_NAME, render_pass_name_format, pass);
600  RenderPass *rp_object = RE_pass_find_by_name(rl, cryptomatte_pass_name, viewname);
601  for (int y = 0; y < rect_height; y++) {
602  for (int x = 0; x < rect_width; x++) {
603  const int accum_buffer_offset = (rect_offset_x + x +
604  (rect_offset_y + y) * viewport_width) *
605  accum_pixel_stride +
606  layer_index * layer_stride + pass_offset;
607  const int render_pass_offset = (y * rect_width + x) * 4;
608  rp_object->rect[render_pass_offset] = accum_buffer[accum_buffer_offset].hash;
609  rp_object->rect[render_pass_offset + 1] = accum_buffer[accum_buffer_offset].weight;
610  if (levels_done + 1 < num_cryptomatte_levels) {
611  rp_object->rect[render_pass_offset + 2] = accum_buffer[accum_buffer_offset + 1].hash;
612  rp_object->rect[render_pass_offset + 3] = accum_buffer[accum_buffer_offset + 1].weight;
613  }
614  else {
615  rp_object->rect[render_pass_offset + 2] = 0.0f;
616  rp_object->rect[render_pass_offset + 3] = 0.0f;
617  }
618  }
619  }
620  levels_done++;
621  }
622 }
623 
625  const char *viewname,
626  const rcti *rect,
627  EEVEE_Data *vedata,
628  EEVEE_ViewLayerData *UNUSED(sldata))
629 {
630  EEVEE_PrivateData *g_data = vedata->stl->g_data;
631  const DRWContextState *draw_ctx = DRW_context_state_get();
632  const ViewLayer *view_layer = draw_ctx->view_layer;
633  const eViewLayerCryptomatteFlags cryptomatte_layers = view_layer->cryptomatte_flag &
635 
637 
638  const int rect_width = BLI_rcti_size_x(rect);
639  const int rect_height = BLI_rcti_size_y(rect);
640  const int rect_offset_x = vedata->stl->g_data->overscan_pixels + rect->xmin;
641  const int rect_offset_y = vedata->stl->g_data->overscan_pixels + rect->ymin;
642  const float *viewport_size = DRW_viewport_size_get();
643  const int viewport_width = viewport_size[0];
644  EEVEE_CryptomatteSample *accum_buffer = g_data->cryptomatte_accum_buffer;
645  BLI_assert(accum_buffer);
646  const int num_cryptomatte_levels = view_layer->cryptomatte_levels;
647  const int num_cryptomatte_passes = eevee_cryptomatte_passes_per_layer(view_layer);
648  const int layer_stride = eevee_cryptomatte_layer_stride(view_layer);
649  const int accum_pixel_stride = eevee_cryptomatte_pixel_stride(view_layer);
650 
651  int layer_index = 0;
652  if ((cryptomatte_layers & VIEW_LAYER_CRYPTOMATTE_OBJECT) != 0) {
654  viewname,
655  "CryptoObject%02d",
656  accum_buffer,
657  num_cryptomatte_passes,
658  num_cryptomatte_levels,
659  accum_pixel_stride,
660  layer_stride,
661  layer_index,
662  rect_width,
663  rect_height,
664  rect_offset_x,
665  rect_offset_y,
666  viewport_width);
667  layer_index++;
668  }
669  if ((cryptomatte_layers & VIEW_LAYER_CRYPTOMATTE_MATERIAL) != 0) {
671  viewname,
672  "CryptoMaterial%02d",
673  accum_buffer,
674  num_cryptomatte_passes,
675  num_cryptomatte_levels,
676  accum_pixel_stride,
677  layer_stride,
678  layer_index,
679  rect_width,
680  rect_height,
681  rect_offset_x,
682  rect_offset_y,
683  viewport_width);
684  layer_index++;
685  }
686  if ((cryptomatte_layers & VIEW_LAYER_CRYPTOMATTE_ASSET) != 0) {
688  viewname,
689  "CryptoAsset%02d",
690  accum_buffer,
691  num_cryptomatte_passes,
692  num_cryptomatte_levels,
693  accum_pixel_stride,
694  layer_stride,
695  layer_index,
696  rect_width,
697  rect_height,
698  rect_offset_x,
699  rect_offset_y,
700  viewport_width);
701  layer_index++;
702  }
703 }
704 
706 {
707  EEVEE_PrivateData *g_data = vedata->stl->g_data;
708  const DRWContextState *draw_ctx = DRW_context_state_get();
709  const ViewLayer *view_layer = draw_ctx->view_layer;
711 
712  BKE_cryptomatte_store_metadata(g_data->cryptomatte_session, render_result, view_layer);
713 }
714 
718 {
719  EEVEE_PrivateData *g_data = vedata->stl->g_data;
722  if (g_data->cryptomatte_session) {
724  g_data->cryptomatte_session = NULL;
725  }
726 }
void BKE_cryptomatte_add_layer(struct CryptomatteSession *session, const char *layer_name)
Definition: cryptomatte.cc:161
uint32_t BKE_cryptomatte_asset_hash(struct CryptomatteSession *session, const char *layer_name, const struct Object *object)
void BKE_cryptomatte_store_metadata(const struct CryptomatteSession *session, struct RenderResult *render_result, const ViewLayer *view_layer)
Definition: cryptomatte.cc:325
float BKE_cryptomatte_hash_to_float(uint32_t cryptomatte_hash)
Definition: cryptomatte.cc:210
uint32_t BKE_cryptomatte_object_hash(struct CryptomatteSession *session, const char *layer_name, const struct Object *object)
uint32_t BKE_cryptomatte_material_hash(struct CryptomatteSession *session, const char *layer_name, const struct Material *material)
struct CryptomatteSession * BKE_cryptomatte_init(void)
Definition: cryptomatte.cc:142
void BKE_cryptomatte_free(struct CryptomatteSession *session)
Definition: cryptomatte.cc:166
struct Material * BKE_object_material_get(struct Object *ob, short act)
Definition: material.c:697
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:36
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_INLINE
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
MINLINE int count_bits_i(unsigned int n)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition: BLI_rect.h:157
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition: BLI_rect.h:153
size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define UNUSED(x)
int rect_width(int rect[2][2])
Definition: Basic.c:58
int rect_height(int rect[2][2])
Definition: Basic.c:62
#define MAX_NAME
Definition: DNA_defs.h:62
#define HAIR_MATERIAL_NR
eViewLayerCryptomatteFlags
@ VIEW_LAYER_CRYPTOMATTE_MATERIAL
@ VIEW_LAYER_CRYPTOMATTE_ASSET
@ VIEW_LAYER_CRYPTOMATTE_ACCURATE
@ VIEW_LAYER_CRYPTOMATTE_OBJECT
#define VIEW_LAYER_CRYPTOMATTE_ALL
@ EEVEE_RENDER_PASS_VOLUME_LIGHT
@ EEVEE_RENDER_PASS_CRYPTOMATTE
@ eModifierType_ParticleSystem
@ SOCK_RGBA
@ OB_MESH
@ OB_HAIR
#define PART_DRAW_PATH
#define PART_DRAW_REND
@ DRW_STATE_DEPTH_EQUAL
Definition: DRW_render.h:324
@ DRW_STATE_WRITE_COLOR
Definition: DRW_render.h:315
#define DRW_PASS_CREATE(pass, state)
Definition: DRW_render.h:593
#define DRW_shgroup_call(shgroup, geom, ob)
Definition: DRW_render.h:420
GPUBatch
Definition: GPU_batch.h:93
struct GPUFrameBuffer GPUFrameBuffer
void GPU_framebuffer_bind(GPUFrameBuffer *fb)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
void * GPU_texture_read(GPUTexture *tex, eGPUDataFormat data_format, int miplvl)
Definition: gpu_texture.cc:371
@ GPU_DATA_FLOAT
Definition: GPU_texture.h:172
eGPUTextureFormat
Definition: GPU_texture.h:84
@ GPU_R32F
Definition: GPU_texture.h:111
@ GPU_RGBA32F
Definition: GPU_texture.h:91
@ GPU_RG32F
Definition: GPU_texture.h:101
#define MEM_SAFE_FREE(v)
Scene scene
Material material
GPUBatch ** DRW_cache_object_surface_material_get(struct Object *ob, struct GPUMaterial **gpumat_array, uint gpumat_array_len)
Definition: draw_cache.c:969
int DRW_cache_object_material_count_get(struct Object *ob)
Definition: draw_cache.c:936
GPUBatch * DRW_cache_object_surface_get(Object *ob)
Definition: draw_cache.c:886
struct DRWShadingGroup * DRW_shgroup_hair_create_sub(struct Object *object, struct ParticleSystem *psys, struct ModifierData *md, struct DRWShadingGroup *shgrp)
Definition: draw_hair.c:222
bool DRW_object_is_visible_psys_in_active_context(const Object *object, const ParticleSystem *psys)
Definition: draw_manager.c:271
const DRWContextState * DRW_context_state_get(void)
const float * DRW_viewport_size_get(void)
Definition: draw_manager.c:439
bool DRW_state_is_scene_render(void)
DefaultTextureList * DRW_viewport_texture_list_get(void)
Definition: draw_manager.c:702
DRWShadingGroup * DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass)
void DRW_shgroup_uniform_vec4_copy(DRWShadingGroup *shgroup, const char *name, const float *value)
void DRW_draw_pass(DRWPass *pass)
void DRW_texture_ensure_fullscreen_2d(GPUTexture **tex, eGPUTextureFormat format, DRWTextureFlag flags)
void EEVEE_cryptomatte_particle_hair_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata, Object *ob)
void EEVEE_cryptomatte_output_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata, int UNUSED(tot_samples))
BLI_INLINE eViewLayerCryptomatteFlags eevee_cryptomatte_active_layers(const ViewLayer *view_layer)
static void eevee_cryptomatte_download_buffer(EEVEE_Data *vedata, GPUFrameBuffer *framebuffer)
void EEVEE_cryptomatte_store_metadata(EEVEE_Data *vedata, RenderResult *render_result)
void EEVEE_cryptomatte_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata, Object *ob)
BLI_INLINE int eevee_cryptomatte_layer_offset(const ViewLayer *view_layer, const int layer)
static DRWShadingGroup * eevee_cryptomatte_shading_group_create(EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata), Object *ob, Material *material, bool is_hair)
BLI_INLINE int eevee_cryptomatte_layers_count(const ViewLayer *view_layer)
static void eevee_cryptomatte_extract_render_passes(RenderLayer *rl, const char *viewname, const char *render_pass_name_format, EEVEE_CryptomatteSample *accum_buffer, const int num_cryptomatte_passes, const int num_cryptomatte_levels, const int accum_pixel_stride, const int layer_stride, const int layer_index, const int rect_width, const int rect_height, const int rect_offset_x, const int rect_offset_y, const int viewport_width)
static void eevee_cryptomatte_hair_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata, Object *ob, ParticleSystem *psys, ModifierData *md, Material *material)
void EEVEE_cryptomatte_free(EEVEE_Data *vedata)
BLI_INLINE int eevee_cryptomatte_layer_stride(const ViewLayer *view_layer)
void EEVEE_cryptomatte_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
void EEVEE_cryptomatte_object_hair_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata, Object *ob)
static int eevee_cryptomatte_sample_cmp_reverse(const void *a_, const void *b_)
void EEVEE_cryptomatte_output_accumulate(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
BLI_INLINE int eevee_cryptomatte_pixel_stride(const ViewLayer *view_layer)
BLI_INLINE int eevee_cryptomatte_passes_per_layer(const ViewLayer *view_layer)
void EEVEE_cryptomatte_render_result(RenderLayer *rl, const char *viewname, const rcti *rect, EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata))
void EEVEE_cryptomatte_update_passes(RenderEngine *engine, Scene *scene, ViewLayer *view_layer)
void EEVEE_cryptomatte_renderpasses_init(EEVEE_Data *vedata)
static void eevee_cryptomatte_postprocess_weights(EEVEE_Data *vedata)
struct GPUShader * EEVEE_shaders_cryptomatte_sh_get(bool is_hair)
@ EFFECT_VOLUMETRIC
void RE_engine_register_pass(struct RenderEngine *engine, struct Scene *scene, struct ViewLayer *view_layer, const char *name, int channels, const char *chanid, eNodeSocketDatatype type)
Definition: engine.c:1092
void GPU_framebuffer_read_color(GPUFrameBuffer *gpu_fb, int x, int y, int w, int h, int channels, int slot, eGPUDataFormat format, void *data)
format
Definition: logImageCore.h:47
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:48
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:46
static unsigned a[3]
Definition: RandGen.cpp:92
static void sample(SocketReader *reader, int x, int y, float color[4])
RenderPass * RE_pass_find_by_name(volatile RenderLayer *rl, const char *name, const char *viewname)
Definition: pipeline.c:2764
unsigned int uint32_t
Definition: stdint.h:83
struct ViewLayer * view_layer
Definition: DRW_render.h:746
struct Object * object_edit
Definition: DRW_render.h:769
EEVEE_TextureList * txl
EEVEE_StorageList * stl
EEVEE_PassList * psl
EEVEE_FramebufferList * fbl
EEVEE_EffectsFlag enabled_effects
struct GPUFrameBuffer * main_fb
struct GPUFrameBuffer * cryptomatte_fb
struct DRWPass * cryptomatte_ps
eViewLayerEEVEEPassType render_passes
EEVEE_CryptomatteSample * cryptomatte_accum_buffer
struct CryptomatteSession * cryptomatte_session
float * cryptomatte_download_buffer
struct EEVEE_PrivateData * g_data
struct EEVEE_EffectsInfo * effects
struct GPUTexture * volume_transmittance_accum
struct GPUTexture * cryptomatte
struct Material ** mat
struct Material ** mat
short totcol
ListBase modifiers
void * data
ParticleSettings * part
float * rect
Definition: RE_pipeline.h:82
short cryptomatte_flag
short cryptomatte_levels
int ymin
Definition: DNA_vec_types.h:80
int xmin
Definition: DNA_vec_types.h:79