From 0609b4bb491eae28b7aff411f54d732b458d6ae9 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 17 May 2022 15:31:37 +0200 Subject: Fix T98052: Eevee / Workbench background render crash with GPU subdivision The problem is that depsgraph evaluation happens before the OpenGL context is initialized, and so modifier evaluation happens without GPU subdivision. Later the BKE_subsurf_modifier_can_do_gpu_subdiv test in the draw code gives a different result. This just checks if the mesh has information for GPU subdivision in the draw code, and if so uses it. This is only set if the test for supported GPU subdivision passes in the modifier evaluation. Additionally it may be good to perform OpenGL context initialization earlier so background render can take advantage of GPU subdivision, but this is more complicated. Differential Revision: https://developer.blender.org/D14969 --- source/blender/blenkernel/BKE_subdiv_modifier.h | 10 ++---- source/blender/blenkernel/intern/subdiv_modifier.c | 39 +++++++--------------- source/blender/draw/intern/draw_cache_impl_mesh.c | 4 +-- source/blender/modifiers/intern/MOD_subsurf.c | 3 +- 4 files changed, 17 insertions(+), 39 deletions(-) diff --git a/source/blender/blenkernel/BKE_subdiv_modifier.h b/source/blender/blenkernel/BKE_subdiv_modifier.h index e9de7d1532e..4ad17610207 100644 --- a/source/blender/blenkernel/BKE_subdiv_modifier.h +++ b/source/blender/blenkernel/BKE_subdiv_modifier.h @@ -42,18 +42,14 @@ bool BKE_subsurf_modifier_force_disable_gpu_evaluation_for_mesh( * \param skip_check_is_last: When true, we assume that the modifier passed is the last enabled * modifier in the stack. */ -bool BKE_subsurf_modifier_can_do_gpu_subdiv_ex(const struct Scene *scene, - const struct Object *ob, - const struct Mesh *mesh, - const struct SubsurfModifierData *smd, - int required_mode, - bool skip_check_is_last); - bool BKE_subsurf_modifier_can_do_gpu_subdiv(const struct Scene *scene, const struct Object *ob, const struct Mesh *mesh, + const struct SubsurfModifierData *smd, int required_mode); +bool BKE_subsurf_modifier_has_gpu_subdiv(const struct Mesh *mesh); + extern void (*BKE_subsurf_modifier_free_gpu_cache_cb)(struct Subdiv *subdiv); /** diff --git a/source/blender/blenkernel/intern/subdiv_modifier.c b/source/blender/blenkernel/intern/subdiv_modifier.c index 3692a3cc4f7..e43da956ce5 100644 --- a/source/blender/blenkernel/intern/subdiv_modifier.c +++ b/source/blender/blenkernel/intern/subdiv_modifier.c @@ -3,6 +3,8 @@ #include "BKE_subdiv_modifier.h" +#include "BLI_session_uuid.h" + #include "MEM_guardedalloc.h" #include "DNA_mesh_types.h" @@ -105,12 +107,11 @@ bool BKE_subsurf_modifier_force_disable_gpu_evaluation_for_mesh(const SubsurfMod return subsurf_modifier_use_autosmooth_or_split_normals(smd, mesh); } -bool BKE_subsurf_modifier_can_do_gpu_subdiv_ex(const Scene *scene, - const Object *ob, - const Mesh *mesh, - const SubsurfModifierData *smd, - int required_mode, - bool skip_check_is_last) +bool BKE_subsurf_modifier_can_do_gpu_subdiv(const Scene *scene, + const Object *ob, + const Mesh *mesh, + const SubsurfModifierData *smd, + int required_mode) { if ((U.gpu_flag & USER_GPU_FLAG_SUBDIVISION_EVALUATION) == 0) { return false; @@ -122,33 +123,17 @@ bool BKE_subsurf_modifier_can_do_gpu_subdiv_ex(const Scene *scene, return false; } - if (!skip_check_is_last) { - ModifierData *md = modifier_get_last_enabled_for_mode(scene, ob, required_mode); - if (md != (const ModifierData *)smd) { - return false; - } + ModifierData *md = modifier_get_last_enabled_for_mode(scene, ob, required_mode); + if (md != (const ModifierData *)smd) { + return false; } return is_subdivision_evaluation_possible_on_gpu(); } -bool BKE_subsurf_modifier_can_do_gpu_subdiv(const Scene *scene, - const Object *ob, - const Mesh *mesh, - int required_mode) +bool BKE_subsurf_modifier_has_gpu_subdiv(const Mesh *mesh) { - ModifierData *md = modifier_get_last_enabled_for_mode(scene, ob, required_mode); - - if (!md) { - return false; - } - - if (md->type != eModifierType_Subsurf) { - return false; - } - - return BKE_subsurf_modifier_can_do_gpu_subdiv_ex( - scene, ob, mesh, (SubsurfModifierData *)md, required_mode, true); + return BLI_session_uuid_is_generated(&mesh->runtime.subsurf_session_uuid); } void (*BKE_subsurf_modifier_free_gpu_cache_cb)(Subdiv *subdiv) = NULL; diff --git a/source/blender/draw/intern/draw_cache_impl_mesh.c b/source/blender/draw/intern/draw_cache_impl_mesh.c index 7dc0244275d..dac4b7488be 100644 --- a/source/blender/draw/intern/draw_cache_impl_mesh.c +++ b/source/blender/draw/intern/draw_cache_impl_mesh.c @@ -1822,9 +1822,7 @@ void DRW_mesh_batch_cache_create_requested(struct TaskGraph *task_graph, do_uvcage = !editmesh_eval_final->runtime.is_original; } - const int required_mode = BKE_subsurf_modifier_eval_required_mode(DRW_state_is_scene_render(), - is_editmode); - const bool do_subdivision = BKE_subsurf_modifier_can_do_gpu_subdiv(scene, ob, me, required_mode); + const bool do_subdivision = BKE_subsurf_modifier_has_gpu_subdiv(me); MeshBufferList *mbuflist = &cache->final.buff; diff --git a/source/blender/modifiers/intern/MOD_subsurf.c b/source/blender/modifiers/intern/MOD_subsurf.c index 571e564f583..4201bab0326 100644 --- a/source/blender/modifiers/intern/MOD_subsurf.c +++ b/source/blender/modifiers/intern/MOD_subsurf.c @@ -236,8 +236,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * * assigned at this stage of modifier stack evaluation. */ const bool is_editmode = (mesh->edit_mesh != NULL); const int required_mode = BKE_subsurf_modifier_eval_required_mode(is_render_mode, is_editmode); - if (BKE_subsurf_modifier_can_do_gpu_subdiv_ex( - scene, ctx->object, mesh, smd, required_mode, false)) { + if (BKE_subsurf_modifier_can_do_gpu_subdiv(scene, ctx->object, mesh, smd, required_mode)) { subdiv_cache_cpu_evaluation_settings(ctx, mesh, smd); return result; } -- cgit v1.2.3 From 8fdd3aad9bd8bd463b9b792f0d9063c2330aadfa Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 17 May 2022 16:27:37 +0200 Subject: Fix T98163: Cycles OSL rendering normal maps differently Match SVM and ensure valid reflection when setting up BSDFs. --- intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp | 3 +++ intern/cycles/kernel/osl/bsdf_phong_ramp.cpp | 3 +++ intern/cycles/kernel/osl/bssrdf.cpp | 2 ++ intern/cycles/kernel/osl/closures.cpp | 23 +++++++++++++++++++++++ intern/cycles/kernel/osl/closures.h | 1 + 5 files changed, 32 insertions(+) diff --git a/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp b/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp index dbbce655ef7..39fcee1ac0d 100644 --- a/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp +++ b/intern/cycles/kernel/osl/bsdf_diffuse_ramp.cpp @@ -17,6 +17,7 @@ #include "kernel/types.h" #include "kernel/closure/alloc.h" #include "kernel/closure/bsdf_diffuse_ramp.h" +#include "kernel/closure/bsdf_util.h" // clang-format on CCL_NAMESPACE_BEGIN @@ -30,6 +31,8 @@ class DiffuseRampClosure : public CBSDFClosure { void setup(ShaderData *sd, uint32_t /* path_flag */, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + DiffuseRampBsdf *bsdf = (DiffuseRampBsdf *)bsdf_alloc_osl( sd, sizeof(DiffuseRampBsdf), weight, ¶ms); diff --git a/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp b/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp index fd602c8847e..972ed7e4a6d 100644 --- a/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp +++ b/intern/cycles/kernel/osl/bsdf_phong_ramp.cpp @@ -17,6 +17,7 @@ #include "kernel/types.h" #include "kernel/closure/alloc.h" #include "kernel/closure/bsdf_phong_ramp.h" +#include "kernel/closure/bsdf_util.h" // clang-format on CCL_NAMESPACE_BEGIN @@ -30,6 +31,8 @@ class PhongRampClosure : public CBSDFClosure { void setup(ShaderData *sd, uint32_t /* path_flag */, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + PhongRampBsdf *bsdf = (PhongRampBsdf *)bsdf_alloc_osl( sd, sizeof(PhongRampBsdf), weight, ¶ms); diff --git a/intern/cycles/kernel/osl/bssrdf.cpp b/intern/cycles/kernel/osl/bssrdf.cpp index 29d2d93ce34..4b282fddad3 100644 --- a/intern/cycles/kernel/osl/bssrdf.cpp +++ b/intern/cycles/kernel/osl/bssrdf.cpp @@ -44,6 +44,8 @@ class CBSSRDFClosure : public CClosurePrimitive { void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + if (method == u_burley) { alloc(sd, path_flag, weight, CLOSURE_BSSRDF_BURLEY_ID); } diff --git a/intern/cycles/kernel/osl/closures.cpp b/intern/cycles/kernel/osl/closures.cpp index 06b91ab9bb7..7c6b48154e4 100644 --- a/intern/cycles/kernel/osl/closures.cpp +++ b/intern/cycles/kernel/osl/closures.cpp @@ -180,6 +180,8 @@ class PrincipledSheenClosure : public CBSDFClosure { void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { if (!skip(sd, path_flag, LABEL_DIFFUSE)) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + PrincipledSheenBsdf *bsdf = (PrincipledSheenBsdf *)bsdf_alloc_osl( sd, sizeof(PrincipledSheenBsdf), weight, ¶ms); sd->flag |= (bsdf) ? bsdf_principled_sheen_setup(sd, bsdf) : 0; @@ -223,6 +225,8 @@ class PrincipledHairClosure : public CBSDFClosure { void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { if (!skip(sd, path_flag, LABEL_GLOSSY)) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + PrincipledHairBSDF *bsdf = (PrincipledHairBSDF *)alloc(sd, path_flag, weight); if (!bsdf) { return; @@ -282,6 +286,7 @@ class PrincipledClearcoatClosure : public CBSDFClosure { void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); if (!bsdf) { return; @@ -503,6 +508,8 @@ class MicrofacetClosure : public CBSDFClosure { return; } + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + MicrofacetBsdf *bsdf = (MicrofacetBsdf *)bsdf_alloc_osl( sd, sizeof(MicrofacetBsdf), weight, ¶ms); @@ -601,6 +608,8 @@ class MicrofacetGGXFresnelClosure : public MicrofacetFresnelClosure { public: void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); if (!bsdf) { return; @@ -630,6 +639,8 @@ class MicrofacetGGXAnisoFresnelClosure : public MicrofacetFresnelClosure { public: void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); if (!bsdf) { return; @@ -695,6 +706,8 @@ class MicrofacetMultiGGXClosure : public MicrofacetMultiClosure { public: void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); if (!bsdf) { return; @@ -723,6 +736,8 @@ class MicrofacetMultiGGXAnisoClosure : public MicrofacetMultiClosure { public: void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); if (!bsdf) { return; @@ -755,6 +770,8 @@ class MicrofacetMultiGGXGlassClosure : public MicrofacetMultiClosure { void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); if (!bsdf) { return; @@ -819,6 +836,8 @@ class MicrofacetMultiGGXFresnelClosure : public MicrofacetMultiFresnelClosure { public: void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); if (!bsdf) { return; @@ -849,6 +868,8 @@ class MicrofacetMultiGGXAnisoFresnelClosure : public MicrofacetMultiFresnelClosu public: void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); if (!bsdf) { return; @@ -883,6 +904,8 @@ class MicrofacetMultiGGXGlassFresnelClosure : public MicrofacetMultiFresnelClosu void setup(ShaderData *sd, uint32_t path_flag, float3 weight) { + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); + MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight); if (!bsdf) { return; diff --git a/intern/cycles/kernel/osl/closures.h b/intern/cycles/kernel/osl/closures.h index 6225644a8e6..e10a3d88a04 100644 --- a/intern/cycles/kernel/osl/closures.h +++ b/intern/cycles/kernel/osl/closures.h @@ -114,6 +114,7 @@ class CBSDFClosure : public CClosurePrimitive { void setup(ShaderData *sd, uint32_t path_flag, float3 weight) \ { \ if (!skip(sd, path_flag, TYPE)) { \ + params.N = ensure_valid_reflection(sd->Ng, sd->I, params.N); \ structname *bsdf = (structname *)bsdf_alloc_osl(sd, sizeof(structname), weight, ¶ms); \ sd->flag |= (bsdf) ? bsdf_##lower##_setup(bsdf) : 0; \ } \ -- cgit v1.2.3 From 83349294b12f904bc1295c53e0242ea92c593c93 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Tue, 17 May 2022 17:32:23 +0200 Subject: Fix T98201: Corrution of filepaths in some case during 'make relative' process. We need to ensure `Main.filepath` is consistent with the current path where we are saving the .blend file, otherwise some path processing code can produce invalid results (happens with e.g. the code syncing the two path storages in Library IDs). --- source/blender/blenloader/intern/writefile.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c index 2ae660ab1b6..22db54d7609 100644 --- a/source/blender/blenloader/intern/writefile.c +++ b/source/blender/blenloader/intern/writefile.c @@ -1349,7 +1349,6 @@ bool BLO_write_file(Main *mainvar, /* Remapping of relative paths to new file location. */ if (remap_mode != BLO_WRITE_PATH_REMAP_NONE) { - if (remap_mode == BLO_WRITE_PATH_REMAP_RELATIVE) { /* Make all relative as none of the existing paths can be relative in an unsaved document. */ if (relbase_valid == false) { @@ -1388,6 +1387,13 @@ bool BLO_write_file(Main *mainvar, } if (remap_mode != BLO_WRITE_PATH_REMAP_NONE) { + /* Some path processing (e.g. with libraries) may use the current `main->filepath`, if this + * is not matching the path currently used for saving, unexpected paths corruptions can + * happen. See T98201. */ + char mainvar_filepath_orig[FILE_MAX]; + STRNCPY(mainvar_filepath_orig, mainvar->filepath); + STRNCPY(mainvar->filepath, filepath); + /* Check if we need to backup and restore paths. */ if (UNLIKELY(use_save_as_copy)) { path_list_backup = BKE_bpath_list_backup(mainvar, path_list_flag); @@ -1412,6 +1418,8 @@ bool BLO_write_file(Main *mainvar, BLI_assert(0); /* Unreachable. */ break; } + + STRNCPY(mainvar->filepath, mainvar_filepath_orig); } } -- cgit v1.2.3