From 8ae44f9aaa1b15059f03a063495a79f790bef2c7 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Sat, 10 Jun 2023 20:50:52 +0300 Subject: [PATCH 1/3] Fix DomeLight warning in console for Storm delegate. Implemented cache_image_color() to image.cc. Added pxr::HdLightTokens->textureFile to WorldData with colored texture if only color is available. --- source/blender/render/hydra/CMakeLists.txt | 1 + .../render/hydra/scene_delegate/image.cc | 52 +++++++++++++++---- .../render/hydra/scene_delegate/image.h | 1 + .../render/hydra/scene_delegate/world.cc | 7 +++ 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/source/blender/render/hydra/CMakeLists.txt b/source/blender/render/hydra/CMakeLists.txt index 4b3a11b94659..7f53eb6f14e5 100644 --- a/source/blender/render/hydra/CMakeLists.txt +++ b/source/blender/render/hydra/CMakeLists.txt @@ -29,6 +29,7 @@ set(INC ../../blenlib ../../depsgraph ../../blenkernel + ../../imbuf ../../gpu ../../gpu/intern ../../python/intern diff --git a/source/blender/render/hydra/scene_delegate/image.cc b/source/blender/render/hydra/scene_delegate/image.cc index 61447978ceaf..f35f692094aa 100644 --- a/source/blender/render/hydra/scene_delegate/image.cc +++ b/source/blender/render/hydra/scene_delegate/image.cc @@ -8,6 +8,8 @@ #include "BKE_image_save.h" #include "BLI_fileops.h" #include "BLI_path_util.h" +#include "IMB_imbuf.h" +#include "IMB_imbuf_types.h" #include "DNA_windowmanager_types.h" @@ -16,18 +18,28 @@ namespace blender::render::hydra { +static std::string get_cache_file(const std::string &file_name, bool mkdir=true) { + char dir_path[FILE_MAX]; + BLI_path_join(dir_path, sizeof(dir_path), BKE_tempdir_session(), "hydra_image_cache"); + if (mkdir) { + BLI_dir_create_recursive(dir_path); + } + + char file_path[FILE_MAX]; + BLI_path_join(file_path, sizeof(file_path), dir_path, file_name.c_str()); + return file_path; +} + static std::string cache_image_file(Image *image, BlenderSceneDelegate *scene_delegate, ImageUser *iuser, bool check_exist) { - char file_path[FILE_MAX]; char file_name[32]; snprintf(file_name, sizeof(file_name), "img_%016llx.hdr", (uintptr_t)image); - BLI_path_join( - file_path, sizeof(file_path), BKE_tempdir_session(), "hydra_image_cache", file_name); - if (check_exist && BLI_exists(file_path)) { + std::string file_path = get_cache_file(file_name); + if (check_exist && BLI_exists(file_path.c_str())) { return file_path; } @@ -37,17 +49,18 @@ static std::string cache_image_file(Image *image, if (BKE_image_save_options_init(&opts, main, scene_delegate->scene, image, iuser, false, false)) { - STRNCPY(opts.filepath, file_path); + STRNCPY(opts.filepath, file_path.c_str()); if (BKE_image_save(nullptr, main, image, iuser, &opts)) { - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s -> %s", image->id.name, file_path); + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s -> %s", image->id.name, file_path.c_str()); } else { - memset(file_path, 0, sizeof(file_path)); + CLOG_ERROR(LOG_RENDER_HYDRA_SCENE, "Can't save %s", file_path.c_str()); + file_path = ""; } } BKE_image_save_options_free(&opts); - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s -> %s", image->id.name, file_path); + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s -> %s", image->id.name, file_path.c_str()); return file_path; } @@ -55,7 +68,7 @@ std::string cache_or_get_image_file(Image *image, BlenderSceneDelegate *scene_delegate, ImageUser *iuser) { - std::string file_path(FILE_MAX, 0); + std::string file_path; if (image->source == IMA_SRC_GENERATED) { file_path = cache_image_file(image, scene_delegate, iuser, false); } @@ -64,6 +77,7 @@ std::string cache_or_get_image_file(Image *image, } else { Main *main = CTX_data_main(scene_delegate->context); + file_path.reserve(FILE_MAX); BKE_image_user_file_path_ex(main, iuser, image, file_path.data(), false, true); if (!pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(file_path)) { @@ -75,4 +89,24 @@ std::string cache_or_get_image_file(Image *image, return file_path; } +std::string cache_image_color(float color[4], const std::string &name) +{ + std::string file_path = get_cache_file(name + ".hdr"); + + ImBuf *ibuf = IMB_allocImBuf(4, 4, 32, IB_rectfloat); + IMB_rectfill(ibuf, color); + ibuf->ftype = IMB_FTYPE_RADHDR; + + if (IMB_saveiff(ibuf, file_path.c_str(), IB_rectfloat)) { + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", file_path.c_str()); + } + else { + CLOG_ERROR(LOG_RENDER_HYDRA_SCENE, "Can't save %s", file_path.c_str()); + file_path = ""; + } + IMB_freeImBuf(ibuf); + + return file_path; +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/image.h b/source/blender/render/hydra/scene_delegate/image.h index 886385963051..9967e45cd3fe 100644 --- a/source/blender/render/hydra/scene_delegate/image.h +++ b/source/blender/render/hydra/scene_delegate/image.h @@ -13,5 +13,6 @@ class BlenderSceneDelegate; std::string cache_or_get_image_file(Image *image, BlenderSceneDelegate *scene_delegate, ImageUser *iuser); +std::string cache_image_color(float color[4], const std::string &name); } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 8df6dbb04a27..ab20d0d17c96 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -100,6 +100,13 @@ void WorldData::init() data_[pxr::HdLightTokens->exposure] = world->exposure; data_[pxr::HdLightTokens->color] = pxr::GfVec3f(world->horr, world->horg, world->horb); } + + if (data_.find(pxr::HdLightTokens->textureFile) == data_.end()) { + pxr::GfVec3f c = data_[pxr::HdLightTokens->color].Get(); + float color[4] = {c[0], c[1], c[2], 1.0f}; + std::string image_path = cache_image_color(color, "world"); + data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path); + } } void WorldData::insert() -- 2.30.2 From a64d2dcf61afd54162be609a8476bd1bc53f1c4b Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 13 Jun 2023 05:39:20 +0300 Subject: [PATCH 2/3] Fixed world light update for Storm delegate --- .../blender/render/hydra/scene_delegate/image.cc | 14 +++++++++++--- source/blender/render/hydra/scene_delegate/image.h | 2 +- .../blender/render/hydra/scene_delegate/world.cc | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/image.cc b/source/blender/render/hydra/scene_delegate/image.cc index f35f692094aa..b01a5f4e6650 100644 --- a/source/blender/render/hydra/scene_delegate/image.cc +++ b/source/blender/render/hydra/scene_delegate/image.cc @@ -18,7 +18,8 @@ namespace blender::render::hydra { -static std::string get_cache_file(const std::string &file_name, bool mkdir=true) { +static std::string get_cache_file(const std::string &file_name, bool mkdir = true) +{ char dir_path[FILE_MAX]; BLI_path_join(dir_path, sizeof(dir_path), BKE_tempdir_session(), "hydra_image_cache"); if (mkdir) { @@ -89,9 +90,16 @@ std::string cache_or_get_image_file(Image *image, return file_path; } -std::string cache_image_color(float color[4], const std::string &name) +std::string cache_image_color(float color[4]) { - std::string file_path = get_cache_file(name + ".hdr"); + char name[128]; + snprintf(name, + sizeof(name), + "color_%02x%02x%02x.hdr", + int(color[0] * 255), + int(color[1] * 255), + int(color[2] * 255)); + std::string file_path = get_cache_file(name); ImBuf *ibuf = IMB_allocImBuf(4, 4, 32, IB_rectfloat); IMB_rectfill(ibuf, color); diff --git a/source/blender/render/hydra/scene_delegate/image.h b/source/blender/render/hydra/scene_delegate/image.h index 9967e45cd3fe..16fc255d95cb 100644 --- a/source/blender/render/hydra/scene_delegate/image.h +++ b/source/blender/render/hydra/scene_delegate/image.h @@ -13,6 +13,6 @@ class BlenderSceneDelegate; std::string cache_or_get_image_file(Image *image, BlenderSceneDelegate *scene_delegate, ImageUser *iuser); -std::string cache_image_color(float color[4], const std::string &name); +std::string cache_image_color(float color[4]); } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index ab20d0d17c96..9d203448bd51 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -104,7 +104,7 @@ void WorldData::init() if (data_.find(pxr::HdLightTokens->textureFile) == data_.end()) { pxr::GfVec3f c = data_[pxr::HdLightTokens->color].Get(); float color[4] = {c[0], c[1], c[2], 1.0f}; - std::string image_path = cache_image_color(color, "world"); + std::string image_path = cache_image_color(color); data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path); } } -- 2.30.2 From ce8c3c546c24eda9d9885cc548e5410844fb7c86 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 13 Jun 2023 20:11:37 +0300 Subject: [PATCH 3/3] Added file exist check to cache_image_color() --- source/blender/render/hydra/scene_delegate/image.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/blender/render/hydra/scene_delegate/image.cc b/source/blender/render/hydra/scene_delegate/image.cc index 8c19af9c2058..c82731eb788f 100644 --- a/source/blender/render/hydra/scene_delegate/image.cc +++ b/source/blender/render/hydra/scene_delegate/image.cc @@ -93,11 +93,14 @@ std::string cache_image_color(float color[4]) char name[128]; snprintf(name, sizeof(name), - "color_%02x%02x%02x.hdr", + "color_%02x-%02x-%02x.hdr", int(color[0] * 255), int(color[1] * 255), int(color[2] * 255)); std::string file_path = get_cache_file(name); + if (BLI_exists(file_path.c_str())) { + return file_path; + } ImBuf *ibuf = IMB_allocImBuf(4, 4, 32, IB_rectfloat); IMB_rectfill(ibuf, color); -- 2.30.2