Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/engines/eevee/eevee_lights.c
| Show First 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | |||||
| extern char datatoc_shadow_geom_glsl[]; | extern char datatoc_shadow_geom_glsl[]; | ||||
| extern char datatoc_shadow_frag_glsl[]; | extern char datatoc_shadow_frag_glsl[]; | ||||
| extern char datatoc_shadow_store_frag_glsl[]; | extern char datatoc_shadow_store_frag_glsl[]; | ||||
| extern char datatoc_shadow_copy_frag_glsl[]; | extern char datatoc_shadow_copy_frag_glsl[]; | ||||
| extern char datatoc_concentric_samples_lib_glsl[]; | extern char datatoc_concentric_samples_lib_glsl[]; | ||||
| /* *********** FUNCTIONS *********** */ | /* *********** FUNCTIONS *********** */ | ||||
| void EEVEE_lights_init(EEVEE_SceneLayerData *sldata) | void EEVEE_lights_init(EEVEE_ViewLayerData *sldata) | ||||
| { | { | ||||
| const unsigned int shadow_ubo_size = sizeof(EEVEE_Shadow) * MAX_SHADOW + | const unsigned int shadow_ubo_size = sizeof(EEVEE_Shadow) * MAX_SHADOW + | ||||
| sizeof(EEVEE_ShadowCube) * MAX_SHADOW_CUBE + | sizeof(EEVEE_ShadowCube) * MAX_SHADOW_CUBE + | ||||
| sizeof(EEVEE_ShadowCascade) * MAX_SHADOW_CASCADE; | sizeof(EEVEE_ShadowCascade) * MAX_SHADOW_CASCADE; | ||||
| const DRWContextState *draw_ctx = DRW_context_state_get(); | const DRWContextState *draw_ctx = DRW_context_state_get(); | ||||
| SceneLayer *scene_layer = draw_ctx->scene_layer; | ViewLayer *view_layer = draw_ctx->view_layer; | ||||
| IDProperty *props = BKE_scene_layer_engine_evaluated_get(scene_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_EEVEE); | IDProperty *props = BKE_view_layer_engine_evaluated_get(view_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_EEVEE); | ||||
| if (!e_data.shadow_sh) { | if (!e_data.shadow_sh) { | ||||
| e_data.shadow_sh = DRW_shader_create( | e_data.shadow_sh = DRW_shader_create( | ||||
| datatoc_shadow_vert_glsl, datatoc_shadow_geom_glsl, datatoc_shadow_frag_glsl, NULL); | datatoc_shadow_vert_glsl, datatoc_shadow_geom_glsl, datatoc_shadow_frag_glsl, NULL); | ||||
| DynStr *ds_frag = BLI_dynstr_new(); | DynStr *ds_frag = BLI_dynstr_new(); | ||||
| BLI_dynstr_append(ds_frag, datatoc_concentric_samples_lib_glsl); | BLI_dynstr_append(ds_frag, datatoc_concentric_samples_lib_glsl); | ||||
| BLI_dynstr_append(ds_frag, datatoc_shadow_store_frag_glsl); | BLI_dynstr_append(ds_frag, datatoc_shadow_store_frag_glsl); | ||||
| ▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | if ((linfo->shadow_size != sh_size) || | ||||
| CLAMP(new_cube_target_size, 1, 4096); | CLAMP(new_cube_target_size, 1, 4096); | ||||
| linfo->shadow_cube_target_size = new_cube_target_size; | linfo->shadow_cube_target_size = new_cube_target_size; | ||||
| linfo->shadow_render_data.cube_texel_size = 1.0 / (float)linfo->shadow_cube_target_size; | linfo->shadow_render_data.cube_texel_size = 1.0 / (float)linfo->shadow_cube_target_size; | ||||
| } | } | ||||
| } | } | ||||
| void EEVEE_lights_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl) | void EEVEE_lights_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_PassList *psl) | ||||
| { | { | ||||
| EEVEE_LampsInfo *linfo = sldata->lamps; | EEVEE_LampsInfo *linfo = sldata->lamps; | ||||
| linfo->num_light = 0; | linfo->num_light = 0; | ||||
| linfo->num_layer = 0; | linfo->num_layer = 0; | ||||
| linfo->gpu_cube_ct = linfo->gpu_cascade_ct = linfo->gpu_shadow_ct = 0; | linfo->gpu_cube_ct = linfo->gpu_cascade_ct = linfo->gpu_shadow_ct = 0; | ||||
| linfo->cpu_cube_ct = linfo->cpu_cascade_ct = 0; | linfo->cpu_cube_ct = linfo->cpu_cascade_ct = 0; | ||||
| memset(linfo->light_ref, 0, sizeof(linfo->light_ref)); | memset(linfo->light_ref, 0, sizeof(linfo->light_ref)); | ||||
| ▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | psl->shadow_cascade_pass = DRW_pass_create( | ||||
| "Shadow Cascade Pass", | "Shadow Cascade Pass", | ||||
| DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS); | DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS); | ||||
| } | } | ||||
| /* Reset shadow casters list */ | /* Reset shadow casters list */ | ||||
| BLI_freelistN(&sldata->shadow_casters); | BLI_freelistN(&sldata->shadow_casters); | ||||
| } | } | ||||
| void EEVEE_lights_cache_add(EEVEE_SceneLayerData *sldata, Object *ob) | void EEVEE_lights_cache_add(EEVEE_ViewLayerData *sldata, Object *ob) | ||||
| { | { | ||||
| EEVEE_LampsInfo *linfo = sldata->lamps; | EEVEE_LampsInfo *linfo = sldata->lamps; | ||||
| /* Step 1 find all lamps in the scene and setup them */ | /* Step 1 find all lamps in the scene and setup them */ | ||||
| if (linfo->num_light >= MAX_LIGHT) { | if (linfo->num_light >= MAX_LIGHT) { | ||||
| printf("Too much lamps in the scene !!!\n"); | printf("Too much lamps in the scene !!!\n"); | ||||
| linfo->num_light = MAX_LIGHT - 1; | linfo->num_light = MAX_LIGHT - 1; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | else { | ||||
| ((EEVEE_LightData *)led->storage)->light_id = linfo->num_light; | ((EEVEE_LightData *)led->storage)->light_id = linfo->num_light; | ||||
| linfo->light_ref[linfo->num_light] = ob; | linfo->light_ref[linfo->num_light] = ob; | ||||
| linfo->num_light++; | linfo->num_light++; | ||||
| } | } | ||||
| } | } | ||||
| /* Add a shadow caster to the shadowpasses */ | /* Add a shadow caster to the shadowpasses */ | ||||
| void EEVEE_lights_cache_shcaster_add( | void EEVEE_lights_cache_shcaster_add( | ||||
| EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, struct Gwn_Batch *geom, float (*obmat)[4]) | EEVEE_ViewLayerData *sldata, EEVEE_PassList *psl, struct Gwn_Batch *geom, float (*obmat)[4]) | ||||
| { | { | ||||
| DRWShadingGroup *grp = DRW_shgroup_instance_create(e_data.shadow_sh, psl->shadow_cube_pass, geom); | DRWShadingGroup *grp = DRW_shgroup_instance_create(e_data.shadow_sh, psl->shadow_cube_pass, geom); | ||||
| DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo); | DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo); | ||||
| DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat); | DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat); | ||||
| DRW_shgroup_set_instance_count(grp, 6); | DRW_shgroup_set_instance_count(grp, 6); | ||||
| grp = DRW_shgroup_instance_create(e_data.shadow_sh, psl->shadow_cascade_pass, geom); | grp = DRW_shgroup_instance_create(e_data.shadow_sh, psl->shadow_cascade_pass, geom); | ||||
| DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo); | DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo); | ||||
| DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat); | DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat); | ||||
| DRW_shgroup_set_instance_count(grp, MAX_CASCADE_NUM); | DRW_shgroup_set_instance_count(grp, MAX_CASCADE_NUM); | ||||
| } | } | ||||
| void EEVEE_lights_cache_shcaster_material_add( | void EEVEE_lights_cache_shcaster_material_add( | ||||
| EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl, struct GPUMaterial *gpumat, | EEVEE_ViewLayerData *sldata, EEVEE_PassList *psl, struct GPUMaterial *gpumat, | ||||
| struct Gwn_Batch *geom, struct Object *ob, float (*obmat)[4], float *alpha_threshold) | struct Gwn_Batch *geom, struct Object *ob, float (*obmat)[4], float *alpha_threshold) | ||||
| { | { | ||||
| DRWShadingGroup *grp = DRW_shgroup_material_instance_create(gpumat, psl->shadow_cube_pass, geom, ob); | DRWShadingGroup *grp = DRW_shgroup_material_instance_create(gpumat, psl->shadow_cube_pass, geom, ob); | ||||
| if (grp == NULL) return; | if (grp == NULL) return; | ||||
| DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo); | DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo); | ||||
| DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat); | DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat); | ||||
| if (alpha_threshold != NULL) | if (alpha_threshold != NULL) | ||||
| DRW_shgroup_uniform_float(grp, "alphaThreshold", alpha_threshold, 1); | DRW_shgroup_uniform_float(grp, "alphaThreshold", alpha_threshold, 1); | ||||
| DRW_shgroup_set_instance_count(grp, 6); | DRW_shgroup_set_instance_count(grp, 6); | ||||
| grp = DRW_shgroup_material_instance_create(gpumat, psl->shadow_cascade_pass, geom, ob); | grp = DRW_shgroup_material_instance_create(gpumat, psl->shadow_cascade_pass, geom, ob); | ||||
| DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo); | DRW_shgroup_uniform_block(grp, "shadow_render_block", sldata->shadow_render_ubo); | ||||
| DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat); | DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat); | ||||
| if (alpha_threshold != NULL) | if (alpha_threshold != NULL) | ||||
| DRW_shgroup_uniform_float(grp, "alphaThreshold", alpha_threshold, 1); | DRW_shgroup_uniform_float(grp, "alphaThreshold", alpha_threshold, 1); | ||||
| DRW_shgroup_set_instance_count(grp, MAX_CASCADE_NUM); | DRW_shgroup_set_instance_count(grp, MAX_CASCADE_NUM); | ||||
| } | } | ||||
| void EEVEE_lights_cache_finish(EEVEE_SceneLayerData *sldata) | void EEVEE_lights_cache_finish(EEVEE_ViewLayerData *sldata) | ||||
| { | { | ||||
| EEVEE_LampsInfo *linfo = sldata->lamps; | EEVEE_LampsInfo *linfo = sldata->lamps; | ||||
| DRWTextureFormat shadow_pool_format = DRW_TEX_R_32; | DRWTextureFormat shadow_pool_format = DRW_TEX_R_32; | ||||
| /* Setup enough layers. */ | /* Setup enough layers. */ | ||||
| /* Free textures if number mismatch. */ | /* Free textures if number mismatch. */ | ||||
| if (linfo->num_layer != linfo->cache_num_layer) { | if (linfo->num_layer != linfo->cache_num_layer) { | ||||
| DRW_TEXTURE_FREE_SAFE(sldata->shadow_pool); | DRW_TEXTURE_FREE_SAFE(sldata->shadow_pool); | ||||
| ▲ Show 20 Lines • Show All 479 Lines • ▼ Show 20 Lines | static void light_tag_shadow_update(Object *lamp, Object *ob) | ||||
| } | } | ||||
| else if (ldata != NULL) { | else if (ldata != NULL) { | ||||
| /* Object was a shadow caster previously and is not anymore. Remove it. */ | /* Object was a shadow caster previously and is not anymore. Remove it. */ | ||||
| led->need_update = true; | led->need_update = true; | ||||
| BLI_freelinkN(&led->shadow_caster_list, ldata); | BLI_freelinkN(&led->shadow_caster_list, ldata); | ||||
| } | } | ||||
| } | } | ||||
| static void eevee_lights_shcaster_updated(EEVEE_SceneLayerData *sldata, Object *ob) | static void eevee_lights_shcaster_updated(EEVEE_ViewLayerData *sldata, Object *ob) | ||||
| { | { | ||||
| Object *lamp; | Object *lamp; | ||||
| EEVEE_LampsInfo *linfo = sldata->lamps; | EEVEE_LampsInfo *linfo = sldata->lamps; | ||||
| /* Iterate over all shadow casting lamps to see if | /* Iterate over all shadow casting lamps to see if | ||||
| * each of them needs update because of this object */ | * each of them needs update because of this object */ | ||||
| for (int i = 0; (lamp = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) { | for (int i = 0; (lamp = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) { | ||||
| light_tag_shadow_update(lamp, ob); | light_tag_shadow_update(lamp, ob); | ||||
| } | } | ||||
| } | } | ||||
| void EEVEE_lights_update(EEVEE_SceneLayerData *sldata) | void EEVEE_lights_update(EEVEE_ViewLayerData *sldata) | ||||
| { | { | ||||
| EEVEE_LampsInfo *linfo = sldata->lamps; | EEVEE_LampsInfo *linfo = sldata->lamps; | ||||
| Object *ob; | Object *ob; | ||||
| int i; | int i; | ||||
| /* Prune shadow casters to remove if object does not exists anymore (unprune them if object exists) */ | /* Prune shadow casters to remove if object does not exists anymore (unprune them if object exists) */ | ||||
| Object *lamp; | Object *lamp; | ||||
| for (i = 0; (lamp = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) { | for (i = 0; (lamp = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) { | ||||
| Show All 20 Lines | void EEVEE_lights_update(EEVEE_ViewLayerData *sldata) | ||||
| for (i = 0; (ob = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) { | for (i = 0; (ob = linfo->shadow_cube_ref[i]) && (i < MAX_SHADOW_CUBE); i++) { | ||||
| EEVEE_LampEngineData *led = EEVEE_lamp_data_get(ob); | EEVEE_LampEngineData *led = EEVEE_lamp_data_get(ob); | ||||
| eevee_shadow_cube_setup(ob, linfo, led); | eevee_shadow_cube_setup(ob, linfo, led); | ||||
| delete_pruned_shadowcaster(led); | delete_pruned_shadowcaster(led); | ||||
| } | } | ||||
| } | } | ||||
| /* this refresh lamps shadow buffers */ | /* this refresh lamps shadow buffers */ | ||||
| void EEVEE_draw_shadows(EEVEE_SceneLayerData *sldata, EEVEE_PassList *psl) | void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_PassList *psl) | ||||
| { | { | ||||
| EEVEE_LampsInfo *linfo = sldata->lamps; | EEVEE_LampsInfo *linfo = sldata->lamps; | ||||
| Object *ob; | Object *ob; | ||||
| int i; | int i; | ||||
| float clear_col[4] = {FLT_MAX}; | float clear_col[4] = {FLT_MAX}; | ||||
| /* Cube Shadow Maps */ | /* Cube Shadow Maps */ | ||||
| DRW_stats_group_start("Cube Shadow Maps"); | DRW_stats_group_start("Cube Shadow Maps"); | ||||
| ▲ Show 20 Lines • Show All 170 Lines • Show Last 20 Lines | |||||