74 return cryptomatte_layers;
90 const int num_cryptomatte_passes = (num_cryptomatte_levels + 1) / 2;
91 return num_cryptomatte_passes;
165 const int buffer_size = viewport_size[0] * viewport_size[1];
174 sizeof(
float), buffer_size * num_cryptomatte_layers, __func__);
188 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
189 GPU_ATTACHMENT_TEXTURE(txl->cryptomatte),
218 float cryptohash[4] = {0.0f};
221 int layer_offset = 0;
226 cryptohash[layer_offset] = cryptomatte_color_value;
233 cryptohash[layer_offset] = cryptomatte_color_value;
240 cryptohash[layer_offset] = cryptomatte_color_value;
259 vedata, sldata, ob,
material,
true);
312 memset(gpumat_array, 0,
sizeof(*gpumat_array) * materials_len);
314 ob, gpumat_array, materials_len);
316 for (
int i = 0; i < materials_len; i++) {
323 vedata, sldata, ob,
material,
false);
332 vedata, sldata, ob,
NULL,
false);
355 const int buffer_size = viewport_size[0] * viewport_size[1];
368 num_cryptomatte_layers,
381 int download_pixel_index = 0;
382 int accum_pixel_index = 0;
384 for (
int pixel_index = 0; pixel_index < buffer_size; pixel_index++) {
385 for (
int layer = 0; layer < num_cryptomatte_layers; layer++) {
387 float download_hash = download_buffer[download_pixel_index++];
388 for (
int level = 0; level < num_levels; level++) {
390 if (
sample->hash == download_hash) {
395 if (
sample->weight == 0.0f) {
396 sample->hash = download_hash;
402 accum_pixel_index += accum_pixel_stride;
422 static float clear_color[4] = {0.0};
445 char cryptomatte_pass_name[
MAX_NAME];
448 for (
short pass = 0; pass < num_passes; pass++) {
451 engine,
scene, view_layer, cryptomatte_pass_name, 4,
"RGBA",
SOCK_RGBA);
455 for (
short pass = 0; pass < num_passes; pass++) {
458 engine,
scene, view_layer, cryptomatte_pass_name, 4,
"RGBA",
SOCK_RGBA);
462 for (
short pass = 0; pass < num_passes; pass++) {
465 engine,
scene, view_layer, cryptomatte_pass_name, 4,
"RGBA",
SOCK_RGBA);
505 const int buffer_size = viewport_size[0] * viewport_size[1];
509 float *volumetric_transmittance_buffer =
NULL;
516 int accum_pixel_index = 0;
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);
528 for (
int layer = 0; layer < num_cryptomatte_layers; layer++) {
531 float total_weight = 0.0f;
532 for (
int level = 0; level < num_levels; level++) {
534 total_weight +=
sample->weight;
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];
545 if (
sample->hash == 0.0f) {
548 sample->weight *= total_weight_inv;
552 qsort(&accum_buffer[accum_pixel_index + layer_offset],
559 for (
int level = 0; level < num_levels; level++) {
561 &accum_buffer[accum_pixel_index + layer_offset + level];
569 if (volumetric_transmittance_buffer) {
570 MEM_freeN(volumetric_transmittance_buffer);
577 const char *viewname,
578 const char *render_pass_name_format,
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,
588 const int rect_offset_x,
589 const int rect_offset_y,
590 const int viewport_width)
592 char cryptomatte_pass_name[
MAX_NAME];
596 for (
int pass = 0; pass < num_cryptomatte_passes; pass++) {
598 const int pass_offset = pass * 2;
603 const int accum_buffer_offset = (rect_offset_x +
x +
604 (rect_offset_y +
y) * viewport_width) *
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;
615 rp_object->
rect[render_pass_offset + 2] = 0.0f;
616 rp_object->
rect[render_pass_offset + 3] = 0.0f;
625 const char *viewname,
643 const int viewport_width = viewport_size[0];
657 num_cryptomatte_passes,
658 num_cryptomatte_levels,
672 "CryptoMaterial%02d",
674 num_cryptomatte_passes,
675 num_cryptomatte_levels,
691 num_cryptomatte_passes,
692 num_cryptomatte_levels,
void BKE_cryptomatte_add_layer(struct CryptomatteSession *session, const char *layer_name)
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)
float BKE_cryptomatte_hash_to_float(uint32_t cryptomatte_hash)
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)
void BKE_cryptomatte_free(struct CryptomatteSession *session)
struct Material * BKE_object_material_get(struct Object *ob, short act)
#define BLI_array_alloca(arr, realsize)
#define LISTBASE_FOREACH(type, var, list)
MINLINE int count_bits_i(unsigned int n)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
int rect_width(int rect[2][2])
int rect_height(int rect[2][2])
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
#define DRW_PASS_CREATE(pass, state)
#define DRW_shgroup_call(shgroup, geom, ob)
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)
GPUBatch ** DRW_cache_object_surface_material_get(struct Object *ob, struct GPUMaterial **gpumat_array, uint gpumat_array_len)
int DRW_cache_object_material_count_get(struct Object *ob)
GPUBatch * DRW_cache_object_surface_get(Object *ob)
struct DRWShadingGroup * DRW_shgroup_hair_create_sub(struct Object *object, struct ParticleSystem *psys, struct ModifierData *md, struct DRWShadingGroup *shgrp)
bool DRW_object_is_visible_psys_in_active_context(const Object *object, const ParticleSystem *psys)
const DRWContextState * DRW_context_state_get(void)
const float * DRW_viewport_size_get(void)
bool DRW_state_is_scene_render(void)
DefaultTextureList * DRW_viewport_texture_list_get(void)
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)
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)
void GPU_framebuffer_read_color(GPUFrameBuffer *gpu_fb, int x, int y, int w, int h, int channels, int slot, eGPUDataFormat format, void *data)
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
void(* MEM_freeN)(void *vmemh)
void *(* MEM_calloc_arrayN)(size_t len, size_t size, const char *str)
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)
struct ViewLayer * view_layer
struct Object * object_edit
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
bool cryptomatte_accurate_mode
float * cryptomatte_download_buffer
struct EEVEE_PrivateData * g_data
struct EEVEE_EffectsInfo * effects
struct GPUTexture * volume_transmittance_accum
struct GPUTexture * cryptomatte