Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/engines/eevee/eevee_effects.c
| Show First 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | |||||
| typedef struct EEVEE_LightProbeData { | typedef struct EEVEE_LightProbeData { | ||||
| short probe_id, shadow_id; | short probe_id, shadow_id; | ||||
| } EEVEE_LightProbeData; | } EEVEE_LightProbeData; | ||||
| /* TODO Option */ | /* TODO Option */ | ||||
| #define ENABLE_EFFECT_MOTION_BLUR 1 | #define ENABLE_EFFECT_MOTION_BLUR 1 | ||||
| #define ENABLE_EFFECT_BLOOM 1 | #define ENABLE_EFFECT_BLOOM 1 | ||||
| #define ENABLE_EFFECT_DOF 1 | #define ENABLE_EFFECT_DOF 1 | ||||
| #define ENABLE_EFFECT_FXAA 1 | |||||
| static struct { | static struct { | ||||
| /* Motion Blur */ | /* Motion Blur */ | ||||
| struct GPUShader *motion_blur_sh; | struct GPUShader *motion_blur_sh; | ||||
| /* Bloom */ | /* Bloom */ | ||||
| struct GPUShader *bloom_blit_sh[2]; | struct GPUShader *bloom_blit_sh[2]; | ||||
| struct GPUShader *bloom_downsample_sh[2]; | struct GPUShader *bloom_downsample_sh[2]; | ||||
| struct GPUShader *bloom_upsample_sh[2]; | struct GPUShader *bloom_upsample_sh[2]; | ||||
| struct GPUShader *bloom_resolve_sh[2]; | struct GPUShader *bloom_resolve_sh[2]; | ||||
| /* Depth Of Field */ | /* Depth Of Field */ | ||||
| struct GPUShader *dof_downsample_sh; | struct GPUShader *dof_downsample_sh; | ||||
| struct GPUShader *dof_scatter_sh; | struct GPUShader *dof_scatter_sh; | ||||
| struct GPUShader *dof_resolve_sh; | struct GPUShader *dof_resolve_sh; | ||||
| /* FXAA */ | |||||
| struct GPUShader *fxaa_sh; | |||||
| float rcp_dim[2]; | |||||
| } e_data = {NULL}; /* Engine data */ | } e_data = {NULL}; /* Engine data */ | ||||
| extern char datatoc_effect_motion_blur_frag_glsl[]; | extern char datatoc_effect_motion_blur_frag_glsl[]; | ||||
| extern char datatoc_effect_bloom_frag_glsl[]; | extern char datatoc_effect_bloom_frag_glsl[]; | ||||
| extern char datatoc_effect_dof_vert_glsl[]; | extern char datatoc_effect_dof_vert_glsl[]; | ||||
| extern char datatoc_effect_dof_geom_glsl[]; | extern char datatoc_effect_dof_geom_glsl[]; | ||||
| extern char datatoc_effect_dof_frag_glsl[]; | extern char datatoc_effect_dof_frag_glsl[]; | ||||
| extern char datatoc_tonemap_frag_glsl[]; | extern char datatoc_tonemap_frag_glsl[]; | ||||
| extern char datatoc_effect_fxaa_frag_glsl[]; | |||||
| static void eevee_motion_blur_camera_get_matrix_at_time( | static void eevee_motion_blur_camera_get_matrix_at_time( | ||||
| Scene *scene, ARegion *ar, RegionView3D *rv3d, View3D *v3d, Object *camera, float time, float r_mat[4][4]) | Scene *scene, ARegion *ar, RegionView3D *rv3d, View3D *v3d, Object *camera, float time, float r_mat[4][4]) | ||||
| { | { | ||||
| float obmat[4][4]; | float obmat[4][4]; | ||||
| /* HACK */ | /* HACK */ | ||||
| Object cam_cpy; Camera camdata_cpy; | Object cam_cpy; Camera camdata_cpy; | ||||
| ▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | void EEVEE_effects_init(EEVEE_Data *vedata) | ||||
| Scene *scene = draw_ctx->scene; | Scene *scene = draw_ctx->scene; | ||||
| View3D *v3d = draw_ctx->v3d; | View3D *v3d = draw_ctx->v3d; | ||||
| RegionView3D *rv3d = draw_ctx->rv3d; | RegionView3D *rv3d = draw_ctx->rv3d; | ||||
| ARegion *ar = draw_ctx->ar; | ARegion *ar = draw_ctx->ar; | ||||
| IDProperty *props = BKE_scene_layer_engine_evaluated_get(scene_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_EEVEE); | IDProperty *props = BKE_scene_layer_engine_evaluated_get(scene_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_EEVEE); | ||||
| const float *viewport_size = DRW_viewport_size_get(); | const float *viewport_size = DRW_viewport_size_get(); | ||||
| e_data.rcp_dim[0] = 1/viewport_size[0]; | |||||
| e_data.rcp_dim[1] = 1/viewport_size[1]; | |||||
| if (!e_data.motion_blur_sh) { | if (!e_data.motion_blur_sh) { | ||||
| e_data.motion_blur_sh = DRW_shader_create_fullscreen(datatoc_effect_motion_blur_frag_glsl, NULL); | e_data.motion_blur_sh = DRW_shader_create_fullscreen(datatoc_effect_motion_blur_frag_glsl, NULL); | ||||
| } | } | ||||
| if (!e_data.dof_downsample_sh) { | if (!e_data.dof_downsample_sh) { | ||||
| e_data.dof_downsample_sh = DRW_shader_create(datatoc_effect_dof_vert_glsl, NULL, | e_data.dof_downsample_sh = DRW_shader_create(datatoc_effect_dof_vert_glsl, NULL, | ||||
| datatoc_effect_dof_frag_glsl, "#define STEP_DOWNSAMPLE\n"); | datatoc_effect_dof_frag_glsl, "#define STEP_DOWNSAMPLE\n"); | ||||
| e_data.dof_scatter_sh = DRW_shader_create(datatoc_effect_dof_vert_glsl, NULL, | e_data.dof_scatter_sh = DRW_shader_create(datatoc_effect_dof_vert_glsl, NULL, | ||||
| Show All 15 Lines | if (!e_data.bloom_blit_sh[0]) { | ||||
| e_data.bloom_upsample_sh[1] = DRW_shader_create_fullscreen(datatoc_effect_bloom_frag_glsl, "#define STEP_UPSAMPLE\n" | e_data.bloom_upsample_sh[1] = DRW_shader_create_fullscreen(datatoc_effect_bloom_frag_glsl, "#define STEP_UPSAMPLE\n" | ||||
| "#define HIGH_QUALITY\n"); | "#define HIGH_QUALITY\n"); | ||||
| e_data.bloom_resolve_sh[0] = DRW_shader_create_fullscreen(datatoc_effect_bloom_frag_glsl, "#define STEP_RESOLVE\n"); | e_data.bloom_resolve_sh[0] = DRW_shader_create_fullscreen(datatoc_effect_bloom_frag_glsl, "#define STEP_RESOLVE\n"); | ||||
| e_data.bloom_resolve_sh[1] = DRW_shader_create_fullscreen(datatoc_effect_bloom_frag_glsl, "#define STEP_RESOLVE\n" | e_data.bloom_resolve_sh[1] = DRW_shader_create_fullscreen(datatoc_effect_bloom_frag_glsl, "#define STEP_RESOLVE\n" | ||||
| "#define HIGH_QUALITY\n"); | "#define HIGH_QUALITY\n"); | ||||
| } | } | ||||
| if (!e_data.fxaa_sh) { | |||||
| e_data.fxaa_sh = DRW_shader_create_fullscreen(datatoc_effect_fxaa_frag_glsl, | |||||
| "#define FXAA_GATHER4_ALPHA 0\n" | |||||
| "#define FXAA_QUALITY__PRESET 39\n"); | |||||
| } | |||||
| if (!stl->effects) { | if (!stl->effects) { | ||||
| stl->effects = MEM_callocN(sizeof(EEVEE_EffectsInfo), "EEVEE_EffectsInfo"); | stl->effects = MEM_callocN(sizeof(EEVEE_EffectsInfo), "EEVEE_EffectsInfo"); | ||||
| } | } | ||||
| effects = stl->effects; | effects = stl->effects; | ||||
| int enabled_effects = 0; | int enabled_effects = 0; | ||||
| ▲ Show 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | if (rv3d->persp == RV3D_CAMOB && v3d->camera) { | ||||
| effects->dof_bokeh[2] = ratio; | effects->dof_bokeh[2] = ratio; | ||||
| effects->dof_bokeh[3] = BKE_collection_engine_property_value_get_float(props, "bokeh_max_size"); | effects->dof_bokeh[3] = BKE_collection_engine_property_value_get_float(props, "bokeh_max_size"); | ||||
| enabled_effects |= EFFECT_DOF; | enabled_effects |= EFFECT_DOF; | ||||
| } | } | ||||
| } | } | ||||
| #endif /* ENABLE_EFFECT_DOF */ | #endif /* ENABLE_EFFECT_DOF */ | ||||
| #ifdef ENABLE_EFFECT_FXAA | |||||
| if (BKE_collection_engine_property_value_get_bool(props, "fxaa_enable")) { | |||||
| enabled_effects |= EFFECT_FXAA; | |||||
| DRWFboTexture tex_fxaa_input = {&txl->fxaa_input_tex, DRW_TEX_RGBA_8, DRW_TEX_FILTER}; | |||||
| DRW_framebuffer_init(&fbl->fxaa_input_fb, &draw_engine_eevee_type, viewport_size[0], viewport_size[1], &tex_fxaa_input, 1); | |||||
| } | |||||
| #endif /* ENABLE_EFFECT_FXAA */ | |||||
| effects->enabled_effects = enabled_effects; | effects->enabled_effects = enabled_effects; | ||||
| /* Only allocate if at least one effect is activated */ | /* Only allocate if at least one effect is activated */ | ||||
| if (effects->enabled_effects != 0) { | if (effects->enabled_effects != 0) { | ||||
| /* Ping Pong buffer */ | /* Ping Pong buffer */ | ||||
| DRWFboTexture tex = {&txl->color_post, DRW_TEX_RGB_11_11_10, DRW_TEX_FILTER}; | DRWFboTexture tex = {&txl->color_post, DRW_TEX_RGB_11_11_10, DRW_TEX_FILTER}; | ||||
| DRW_framebuffer_init(&fbl->effect_fb, &draw_engine_eevee_type, | DRW_framebuffer_init(&fbl->effect_fb, &draw_engine_eevee_type, | ||||
| Show All 22 Lines | |||||
| void EEVEE_effects_cache_init(EEVEE_Data *vedata) | void EEVEE_effects_cache_init(EEVEE_Data *vedata) | ||||
| { | { | ||||
| EEVEE_PassList *psl = vedata->psl; | EEVEE_PassList *psl = vedata->psl; | ||||
| EEVEE_StorageList *stl = vedata->stl; | EEVEE_StorageList *stl = vedata->stl; | ||||
| EEVEE_TextureList *txl = vedata->txl; | EEVEE_TextureList *txl = vedata->txl; | ||||
| EEVEE_EffectsInfo *effects = stl->effects; | EEVEE_EffectsInfo *effects = stl->effects; | ||||
| DefaultTextureList *dtxl = DRW_viewport_texture_list_get(); | DefaultTextureList *dtxl = DRW_viewport_texture_list_get(); | ||||
| const float *viewport_size = DRW_viewport_size_get(); | |||||
| struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get(); | struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get(); | ||||
| { | { | ||||
| psl->motion_blur = DRW_pass_create("Motion Blur", DRW_STATE_WRITE_COLOR); | psl->motion_blur = DRW_pass_create("Motion Blur", DRW_STATE_WRITE_COLOR); | ||||
| DRWShadingGroup *grp = DRW_shgroup_create(e_data.motion_blur_sh, psl->motion_blur); | DRWShadingGroup *grp = DRW_shgroup_create(e_data.motion_blur_sh, psl->motion_blur); | ||||
| DRW_shgroup_uniform_int(grp, "samples", &effects->motion_blur_samples, 1); | DRW_shgroup_uniform_int(grp, "samples", &effects->motion_blur_samples, 1); | ||||
| ▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get(); | ||||
| DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1); | DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1); | ||||
| DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1); | DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1); | ||||
| DRW_shgroup_call_add(grp, quad, NULL); | DRW_shgroup_call_add(grp, quad, NULL); | ||||
| psl->dof_scatter = DRW_pass_create("DoF Scatter", DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE); | psl->dof_scatter = DRW_pass_create("DoF Scatter", DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE); | ||||
| /* This create an empty batch of N triangles to be positioned | /* This create an empty batch of N triangles to be positioned | ||||
| * by the vertex shader 0.4ms against 6ms with instancing */ | * by the vertex shader 0.4ms against 6ms with instancing */ | ||||
| const float *viewport_size = DRW_viewport_size_get(); | |||||
| const int sprite_ct = ((int)viewport_size[0]/2) * ((int)viewport_size[1]/2); /* brackets matters */ | const int sprite_ct = ((int)viewport_size[0]/2) * ((int)viewport_size[1]/2); /* brackets matters */ | ||||
| grp = DRW_shgroup_empty_tri_batch_create(e_data.dof_scatter_sh, psl->dof_scatter, sprite_ct); | grp = DRW_shgroup_empty_tri_batch_create(e_data.dof_scatter_sh, psl->dof_scatter, sprite_ct); | ||||
| DRW_shgroup_uniform_buffer(grp, "colorBuffer", &effects->unf_source_buffer); | DRW_shgroup_uniform_buffer(grp, "colorBuffer", &effects->unf_source_buffer); | ||||
| DRW_shgroup_uniform_buffer(grp, "cocBuffer", &txl->dof_coc); | DRW_shgroup_uniform_buffer(grp, "cocBuffer", &txl->dof_coc); | ||||
| DRW_shgroup_uniform_vec2(grp, "layerSelection", effects->dof_layer_select, 1); | DRW_shgroup_uniform_vec2(grp, "layerSelection", effects->dof_layer_select, 1); | ||||
| DRW_shgroup_uniform_vec4(grp, "bokehParams", effects->dof_bokeh, 1); | DRW_shgroup_uniform_vec4(grp, "bokehParams", effects->dof_bokeh, 1); | ||||
| psl->dof_resolve = DRW_pass_create("DoF Resolve", DRW_STATE_WRITE_COLOR); | psl->dof_resolve = DRW_pass_create("DoF Resolve", DRW_STATE_WRITE_COLOR); | ||||
| grp = DRW_shgroup_create(e_data.dof_resolve_sh, psl->dof_resolve); | grp = DRW_shgroup_create(e_data.dof_resolve_sh, psl->dof_resolve); | ||||
| DRW_shgroup_uniform_buffer(grp, "colorBuffer", &effects->source_buffer); | DRW_shgroup_uniform_buffer(grp, "colorBuffer", &effects->source_buffer); | ||||
| DRW_shgroup_uniform_buffer(grp, "nearBuffer", &txl->dof_near_blur); | DRW_shgroup_uniform_buffer(grp, "nearBuffer", &txl->dof_near_blur); | ||||
| DRW_shgroup_uniform_buffer(grp, "farBuffer", &txl->dof_far_blur); | DRW_shgroup_uniform_buffer(grp, "farBuffer", &txl->dof_far_blur); | ||||
| DRW_shgroup_uniform_buffer(grp, "depthBuffer", &dtxl->depth); | DRW_shgroup_uniform_buffer(grp, "depthBuffer", &dtxl->depth); | ||||
| DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1); | DRW_shgroup_uniform_vec2(grp, "nearFar", effects->dof_near_far, 1); | ||||
| DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1); | DRW_shgroup_uniform_vec3(grp, "dofParams", effects->dof_params, 1); | ||||
| DRW_shgroup_call_add(grp, quad, NULL); | DRW_shgroup_call_add(grp, quad, NULL); | ||||
| } | } | ||||
| /* FXAA */ | |||||
| { | |||||
| psl->fxaa_pass = DRW_pass_create("FXAA", DRW_STATE_WRITE_COLOR); | |||||
| DRWShadingGroup *grp = DRW_shgroup_create(e_data.fxaa_sh, psl->fxaa_pass); | |||||
| DRW_shgroup_uniform_buffer(grp, "colorBuffer", &txl->fxaa_input_tex); | |||||
| DRW_shgroup_uniform_vec2(grp, "rcpDimensions", e_data.rcp_dim, 1); | |||||
| DRW_shgroup_call_add(grp, quad, NULL); | |||||
| } | |||||
| } | } | ||||
| #define SWAP_BUFFERS() { \ | #define SWAP_BUFFERS() { \ | ||||
| if (effects->source_buffer == txl->color) { \ | if (effects->source_buffer == txl->color) { \ | ||||
| effects->source_buffer = txl->color_post; \ | effects->source_buffer = txl->color_post; \ | ||||
| effects->target_buffer = fbl->main; \ | effects->target_buffer = fbl->main; \ | ||||
LazyDodo: What's going on with the braces here? | |||||
Not Done Inline ActionsThey're blocks. They scope the local variables (eg, grp). cmr: They're blocks. They scope the local variables (eg, `grp`). | |||||
| } \ | } \ | ||||
| else { \ | else { \ | ||||
| effects->source_buffer = txl->color; \ | effects->source_buffer = txl->color; \ | ||||
| effects->target_buffer = fbl->effect_fb; \ | effects->target_buffer = fbl->effect_fb; \ | ||||
| } \ | } \ | ||||
| } ((void)0) | } ((void)0) | ||||
| void EEVEE_draw_effects(EEVEE_Data *vedata) | void EEVEE_draw_effects(EEVEE_Data *vedata) | ||||
| ▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | if ((effects->enabled_effects & EFFECT_BLOOM) != 0) { | ||||
| effects->unf_source_buffer = last; | effects->unf_source_buffer = last; | ||||
| effects->unf_base_buffer = effects->source_buffer; | effects->unf_base_buffer = effects->source_buffer; | ||||
| DRW_framebuffer_bind(effects->target_buffer); | DRW_framebuffer_bind(effects->target_buffer); | ||||
| DRW_draw_pass(psl->bloom_resolve); | DRW_draw_pass(psl->bloom_resolve); | ||||
| SWAP_BUFFERS(); | SWAP_BUFFERS(); | ||||
| } | } | ||||
| /* Restore default framebuffer */ | /* Restore default framebuffer */ | ||||
| DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0); | DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0); | ||||
| if ((effects->enabled_effects & EFFECT_FXAA) != 0) { | |||||
| DRW_framebuffer_bind(fbl->fxaa_input_fb); | |||||
| } else { | |||||
| DRW_framebuffer_bind(dfbl->default_fb); | DRW_framebuffer_bind(dfbl->default_fb); | ||||
| } | |||||
| /* Tonemapping */ | /* Tonemapping */ | ||||
| DRW_transform_to_display(effects->source_buffer); | DRW_transform_to_display(effects->source_buffer); | ||||
| /* FXAA is explicitly after tonemapping! */ | |||||
| if ((effects->enabled_effects & EFFECT_FXAA) != 0) { | |||||
| DRW_framebuffer_bind(dfbl->default_fb); | |||||
| DRW_draw_pass(psl->fxaa_pass); | |||||
| } | |||||
| } | } | ||||
| void EEVEE_effects_free(void) | void EEVEE_effects_free(void) | ||||
| { | { | ||||
| DRW_SHADER_FREE_SAFE(e_data.motion_blur_sh); | DRW_SHADER_FREE_SAFE(e_data.motion_blur_sh); | ||||
| DRW_SHADER_FREE_SAFE(e_data.dof_downsample_sh); | DRW_SHADER_FREE_SAFE(e_data.dof_downsample_sh); | ||||
| DRW_SHADER_FREE_SAFE(e_data.dof_scatter_sh); | DRW_SHADER_FREE_SAFE(e_data.dof_scatter_sh); | ||||
| DRW_SHADER_FREE_SAFE(e_data.dof_resolve_sh); | DRW_SHADER_FREE_SAFE(e_data.dof_resolve_sh); | ||||
| DRW_SHADER_FREE_SAFE(e_data.fxaa_sh); | |||||
| DRW_SHADER_FREE_SAFE(e_data.bloom_blit_sh[0]); | DRW_SHADER_FREE_SAFE(e_data.bloom_blit_sh[0]); | ||||
| DRW_SHADER_FREE_SAFE(e_data.bloom_downsample_sh[0]); | DRW_SHADER_FREE_SAFE(e_data.bloom_downsample_sh[0]); | ||||
| DRW_SHADER_FREE_SAFE(e_data.bloom_upsample_sh[0]); | DRW_SHADER_FREE_SAFE(e_data.bloom_upsample_sh[0]); | ||||
| DRW_SHADER_FREE_SAFE(e_data.bloom_resolve_sh[0]); | DRW_SHADER_FREE_SAFE(e_data.bloom_resolve_sh[0]); | ||||
| DRW_SHADER_FREE_SAFE(e_data.bloom_blit_sh[1]); | DRW_SHADER_FREE_SAFE(e_data.bloom_blit_sh[1]); | ||||
| DRW_SHADER_FREE_SAFE(e_data.bloom_downsample_sh[1]); | DRW_SHADER_FREE_SAFE(e_data.bloom_downsample_sh[1]); | ||||
| DRW_SHADER_FREE_SAFE(e_data.bloom_upsample_sh[1]); | DRW_SHADER_FREE_SAFE(e_data.bloom_upsample_sh[1]); | ||||
| DRW_SHADER_FREE_SAFE(e_data.bloom_resolve_sh[1]); | DRW_SHADER_FREE_SAFE(e_data.bloom_resolve_sh[1]); | ||||
| } | } | ||||
| No newline at end of file | |||||
What's going on with the braces here?