59 return cryptomatte_layers;
75 const int num_cryptomatte_passes = (num_cryptomatte_levels + 1) / 2;
76 return num_cryptomatte_passes;
124 g_data->cryptomatte_session = session;
147 (num_cryptomatte_layers == 2) ?
GPU_RG32F :
150 const int buffer_size = viewport_size[0] * viewport_size[1];
152 if (
g_data->cryptomatte_accum_buffer ==
NULL) {
159 buffer_size * num_cryptomatte_layers,
sizeof(
float), __func__);
164 memset(
g_data->cryptomatte_accum_buffer,
173 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
174 GPU_ATTACHMENT_TEXTURE(txl->cryptomatte),
200 float cryptohash[4] = {0.0f};
203 int layer_offset = 0;
206 g_data->cryptomatte_session,
"CryptoObject", ob);
208 cryptohash[layer_offset] = cryptomatte_color_value;
215 cryptohash[layer_offset] = cryptomatte_color_value;
220 g_data->cryptomatte_session,
"CryptoAsset", ob);
222 cryptohash[layer_offset] = cryptomatte_color_value;
242 vedata, sldata, ob,
material,
true);
253 vedata, sldata, ob,
material,
true);
295 memset(gpumat_array, 0,
sizeof(*gpumat_array) * materials_len);
297 ob, gpumat_array, materials_len);
299 for (
int i = 0; i < materials_len; i++) {
306 vedata, sldata, ob,
material,
false);
315 vedata, sldata, ob,
NULL,
false);
338 const int buffer_size = viewport_size[0] * viewport_size[1];
341 float *download_buffer =
g_data->cryptomatte_download_buffer;
351 num_cryptomatte_layers,
364 int download_pixel_index = 0;
365 int accum_pixel_index = 0;
367 for (
int pixel_index = 0; pixel_index < buffer_size; pixel_index++) {
368 for (
int layer = 0; layer < num_cryptomatte_layers; layer++) {
370 float download_hash = download_buffer[download_pixel_index++];
371 for (
int level = 0; level < num_levels; level++) {
373 if (
sample->hash == download_hash) {
378 if (
sample->weight == 0.0f) {
379 sample->hash = download_hash;
385 accum_pixel_index += accum_pixel_stride;
404 if (
g_data->cryptomatte_accurate_mode || current_sample < cryptomatte_levels) {
405 static float clear_color[4] = {0.0};
429 char cryptomatte_pass_name[
MAX_NAME];
432 for (
short pass = 0; pass < num_passes; pass++) {
435 engine,
scene, view_layer, cryptomatte_pass_name, 4,
"rgba",
SOCK_RGBA);
439 for (
short pass = 0; pass < num_passes; pass++) {
442 engine,
scene, view_layer, cryptomatte_pass_name, 4,
"rgba",
SOCK_RGBA);
446 for (
short pass = 0; pass < num_passes; pass++) {
449 engine,
scene, view_layer, cryptomatte_pass_name, 4,
"rgba",
SOCK_RGBA);
466 if (
a->weight <
b->weight) {
469 if (
a->weight >
b->weight) {
489 const int buffer_size = viewport_size[0] * viewport_size[1];
493 float *volumetric_transmittance_buffer =
NULL;
500 int accum_pixel_index = 0;
503 for (
int pixel_index = 0; pixel_index < buffer_size;
504 pixel_index++, accum_pixel_index += accum_pixel_stride) {
505 float coverage = 1.0f;
506 if (volumetric_transmittance_buffer !=
NULL) {
507 coverage = (volumetric_transmittance_buffer[pixel_index * 4] +
508 volumetric_transmittance_buffer[pixel_index * 4 + 1] +
509 volumetric_transmittance_buffer[pixel_index * 4 + 2]) /
512 for (
int layer = 0; layer < num_cryptomatte_layers; layer++) {
515 float total_weight = 0.0f;
516 for (
int level = 0; level < num_levels; level++) {
518 total_weight +=
sample->weight;
522 float total_weight_inv = coverage / total_weight;
523 if (total_weight_inv > 0.0f) {
524 for (
int level = 0; level < num_levels; level++) {
526 &accum_buffer[accum_pixel_index + layer_offset + level];
529 if (
sample->hash == 0.0f) {
532 sample->weight *= total_weight_inv;
536 qsort(&accum_buffer[accum_pixel_index + layer_offset],
543 for (
int level = 0; level < num_levels; level++) {
545 &accum_buffer[accum_pixel_index + layer_offset + level];
553 if (volumetric_transmittance_buffer) {
554 MEM_freeN(volumetric_transmittance_buffer);
561 const char *viewname,
562 const char *render_pass_name_format,
565 const int num_cryptomatte_passes,
566 const int num_cryptomatte_levels,
567 const int accum_pixel_stride,
568 const int layer_stride,
569 const int layer_index,
572 const int rect_offset_x,
573 const int rect_offset_y,
574 const int viewport_width)
576 char cryptomatte_pass_name[
MAX_NAME];
580 for (
int pass = 0; pass < num_cryptomatte_passes; pass++) {
582 const int pass_offset = pass * 2;
587 const int accum_buffer_offset = (rect_offset_x +
x +
588 (rect_offset_y +
y) * viewport_width) *
590 layer_index * layer_stride + pass_offset;
591 const int render_pass_offset = (
y *
rect_width +
x) * 4;
592 rp_object->
rect[render_pass_offset] = accum_buffer[accum_buffer_offset].
hash;
593 rp_object->
rect[render_pass_offset + 1] = accum_buffer[accum_buffer_offset].
weight;
594 if (levels_done + 1 < num_cryptomatte_levels) {
595 rp_object->
rect[render_pass_offset + 2] = accum_buffer[accum_buffer_offset + 1].
hash;
596 rp_object->
rect[render_pass_offset + 3] = accum_buffer[accum_buffer_offset + 1].
weight;
599 rp_object->
rect[render_pass_offset + 2] = 0.0f;
600 rp_object->
rect[render_pass_offset + 3] = 0.0f;
609 const char *viewname,
627 const int viewport_width = viewport_size[0];
641 num_cryptomatte_passes,
642 num_cryptomatte_levels,
656 "CryptoMaterial%02d",
658 num_cryptomatte_passes,
659 num_cryptomatte_levels,
675 num_cryptomatte_passes,
676 num_cryptomatte_levels,
706 if (
g_data->cryptomatte_session) {
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_eval(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])
#define CURVES_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
#define DRW_PASS_CREATE(pass, state)
#define DRW_shgroup_uniform_block(shgroup, name, ubo)
#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_curves_create_sub(struct Object *object, struct DRWShadingGroup *shgrp, struct GPUMaterial *gpu_material)
struct DRWShadingGroup * DRW_shgroup_hair_create_sub(struct Object *object, struct ParticleSystem *psys, struct ModifierData *md, struct DRWShadingGroup *shgrp, struct GPUMaterial *gpu_material)
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)
static struct @318 g_data
void EEVEE_cryptomatte_particle_hair_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata, Object *ob)
void EEVEE_cryptomatte_object_curves_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)
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)
void EEVEE_cryptomatte_free(EEVEE_Data *vedata)
BLI_INLINE int eevee_cryptomatte_layer_stride(const ViewLayer *view_layer)
static void eevee_cryptomatte_curves_cache_populate(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata, Object *ob, ParticleSystem *psys, ModifierData *md, Material *material)
void EEVEE_cryptomatte_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
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)
static DRWShadingGroup * eevee_cryptomatte_shading_group_create(EEVEE_Data *vedata, EEVEE_ViewLayerData *sldata, Object *ob, Material *material, bool is_hair)
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)
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx GPU_R32F
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)
ccl_gpu_kernel_postfix ccl_global float int int int int ccl_global const float int int int int int int int int int int int int num_samples
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 const pxr::TfToken b("b", pxr::TfToken::Immortal)
RenderPass * RE_pass_find_by_name(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
struct EEVEE_PrivateData * g_data
struct EEVEE_EffectsInfo * effects
struct GPUTexture * volume_transmittance_accum
struct GPUTexture * cryptomatte
struct GPUUniformBuf * shadow_ubo