Add IBL support #29

Merged
Bogdan Nagirniak merged 5 commits from BLEN-385 into hydra-render 2023-04-27 09:25:11 +02:00
13 changed files with 102 additions and 117 deletions
Showing only changes of commit 38135e6662 - Show all commits

View File

@ -58,8 +58,6 @@ set(SRC
viewport_engine.cc viewport_engine.cc
camera.h camera.h
camera.cc camera.cc
utils.h
utils.cc
render_task_delegate.cc render_task_delegate.cc
render_task_delegate.h render_task_delegate.h
@ -84,6 +82,8 @@ set(SRC
scene_delegate/world.cc scene_delegate/world.cc
scene_delegate/instancer.h scene_delegate/instancer.h
scene_delegate/instancer.cc scene_delegate/instancer.cc
scene_delegate/image.h
scene_delegate/image.cc
) )
set(LIB set(LIB

View File

@ -3,9 +3,10 @@
#include "DNA_camera_types.h" #include "DNA_camera_types.h"
#include "DNA_screen_types.h" #include "DNA_screen_types.h"
#include "DNA_view3d_types.h"
#include "scene_delegate/object.h"
#include "camera.h" #include "camera.h"
#include "utils.h"
namespace blender::render::hydra { namespace blender::render::hydra {

View File

@ -13,11 +13,8 @@
#include "final_engine.h" #include "final_engine.h"
#include "preview_engine.h" #include "preview_engine.h"
#include "utils.h"
#include "viewport_engine.h" #include "viewport_engine.h"
#include "BLI_timer.h"
namespace blender::render::hydra { namespace blender::render::hydra {
static PyObject *init_func(PyObject * /*self*/, PyObject *args) static PyObject *init_func(PyObject * /*self*/, PyObject *args)

View File

@ -6,7 +6,6 @@
#include "DEG_depsgraph_query.h" #include "DEG_depsgraph_query.h"
#include "DNA_scene_types.h" #include "DNA_scene_types.h"
#include "..\utils.h"
#include "blender_scene_delegate.h" #include "blender_scene_delegate.h"
namespace blender::render::hydra { namespace blender::render::hydra {

View File

@ -0,0 +1,64 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <pxr/imaging/hio/imageRegistry.h>
#include "BKE_appdir.h"
#include "BKE_image.h"
#include "BKE_image_save.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "DNA_camera_types.h"
#include "DNA_windowmanager_types.h"
#include "blender_scene_delegate.h"
#include "image.h"
namespace blender::render::hydra {
std::string cache_or_get_image_file(Image *image,
BlenderSceneDelegate *scene_delegate,
ImageUser *iuser)
{
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s", image->id.name);
std::string image_path(FILE_MAX, 0);
Main *main = CTX_data_main(scene_delegate->context);
if (pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(image->filepath)) {
BKE_image_user_file_path_ex(main, iuser, image, image_path.data(), false, true);
return image_path;
}
ImageSaveOptions opts;
opts.im_format.imtype = R_IMF_IMTYPE_RADHDR;
if (!BKE_image_save_options_init(
&opts, main, scene_delegate->scene, image, iuser, true, false)) {
BKE_image_save_options_free(&opts);
return "";
}
char image_name[32];
snprintf(image_name, 32, "img_%016llx.hdr", (uint64_t)image);
BLI_path_join(image_path.data(),
image_path.capacity(),
BKE_tempdir_session(),
"hydra_image_cache",
image_name);
STRNCPY(opts.filepath, image_path.c_str());
ReportList reports;
if (!BKE_image_save(&reports, main, image, iuser, &opts)) {
BKE_image_save_options_free(&opts);
return "";
};
BKE_image_save_options_free(&opts);
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s: cached=%s", image->id.name, image_path.c_str());
return image_path;
}
} // namespace blender::render::hydra

View File

@ -0,0 +1,17 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#pragma once
#include "BKE_image.h"
#include "BKE_image_save.h"
namespace blender::render::hydra {
class BlenderSceneDelegate;
std::string cache_or_get_image_file(Image *image,
BlenderSceneDelegate *scene_delegate,
ImageUser *iuser);
} // namespace blender::render::hydra

View File

@ -3,7 +3,6 @@
#include <pxr/base/gf/vec2f.h> #include <pxr/base/gf/vec2f.h>
#include "../utils.h"
#include "blender_scene_delegate.h" #include "blender_scene_delegate.h"
#include "instancer.h" #include "instancer.h"

View File

@ -9,7 +9,6 @@
#include "BKE_mesh_runtime.h" #include "BKE_mesh_runtime.h"
#include "BKE_object.h" #include "BKE_object.h"
#include "../utils.h"
#include "blender_scene_delegate.h" #include "blender_scene_delegate.h"
#include "mesh.h" #include "mesh.h"

View File

@ -3,7 +3,6 @@
#include "BKE_object.h" #include "BKE_object.h"
#include "../utils.h"
#include "blender_scene_delegate.h" #include "blender_scene_delegate.h"
#include "light.h" #include "light.h"
#include "mesh.h" #include "mesh.h"
@ -84,4 +83,15 @@ void ObjectData::write_transform()
transform = gf_matrix_from_transform(((Object *)id)->object_to_world); transform = gf_matrix_from_transform(((Object *)id)->object_to_world);
} }
pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4])
{
pxr::GfMatrix4d ret;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
ret[i][j] = m[i][j];
}
}
return ret;
}
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -35,4 +35,6 @@ class ObjectData : public IdData {
using ObjectDataMap = using ObjectDataMap =
pxr::TfHashMap<pxr::SdfPath, std::unique_ptr<ObjectData>, pxr::SdfPath::Hash>; pxr::TfHashMap<pxr::SdfPath, std::unique_ptr<ObjectData>, pxr::SdfPath::Hash>;
pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]);
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -9,20 +9,18 @@
#include <pxr/imaging/hd/light.h> #include <pxr/imaging/hd/light.h>
#include <pxr/imaging/hd/renderDelegate.h> #include <pxr/imaging/hd/renderDelegate.h>
#include <pxr/imaging/hd/tokens.h> #include <pxr/imaging/hd/tokens.h>
#include <pxr/imaging/hio/imageRegistry.h>
#include <pxr/usd/usdLux/tokens.h> #include <pxr/usd/usdLux/tokens.h>
#include "BKE_context.h" #include "BKE_context.h"
#include "DNA_node_types.h" #include "DNA_node_types.h"
#include "DNA_windowmanager_types.h"
#include "BKE_image.h"
#include "BKE_node.h" #include "BKE_node.h"
#include "BKE_node_runtime.hh" #include "BKE_node_runtime.hh"
#include "BLI_path_util.h"
#include "NOD_shader.h" #include "NOD_shader.h"
#include "../utils.h"
#include "blender_scene_delegate.h" #include "blender_scene_delegate.h"
#include "image.h"
#include "world.h" #include "world.h"
/* TODO : add custom tftoken "transparency"? */ /* TODO : add custom tftoken "transparency"? */
@ -97,23 +95,8 @@ void WorldData::init()
if (color_input_node->type == SH_NODE_TEX_IMAGE) { if (color_input_node->type == SH_NODE_TEX_IMAGE) {
NodeTexImage *tex = static_cast<NodeTexImage *>(color_input_node->storage); NodeTexImage *tex = static_cast<NodeTexImage *>(color_input_node->storage);
Image *image = (Image *)color_input_node->id; Image *image = (Image *)color_input_node->id;
std::string image_path = cache_or_get_image_file(image, scene_delegate_, &tex->iuser);
char image_path[1024]; if (!image_path.empty()) {
Main *main = CTX_data_main(scene_delegate_->context);
if (image) {
if (!pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(image->filepath)) {
ReportList reports;
ImageSaveOptions opts;
opts.im_format.imtype = R_IMF_IMTYPE_PNG;
strcpy(image_path,
cache_image(main, scene_delegate_->scene, image, &tex->iuser, &opts, &reports)
.c_str());
if (image_path && !image_path[0]) {
data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path);
}
}
BKE_image_user_file_path_ex(main, &tex->iuser, image, image_path, false, true);
data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path); data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path);
} }
} }

View File

@ -1,63 +0,0 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <filesystem>
#include <pxr/base/tf/stringUtils.h>
#include "BKE_appdir.h"
#include "BKE_image_save.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "DNA_camera_types.h"
#include "utils.h"
namespace blender::render::hydra {
pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4])
{
pxr::GfMatrix4d ret = pxr::GfMatrix4d();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
ret[i][j] = m[i][j];
}
}
return ret;
}
std::string cache_image(Main *bmain,
Scene *scene,
Image *image,
ImageUser *iuser,
ImageSaveOptions *opts,
ReportList *reports)
{
const char *default_format = ".png";
char tempfile[FILE_MAX];
if (!BKE_image_save_options_init(opts, bmain, scene, image, iuser, true, false)) {
BKE_image_save_options_free(opts);
return "";
}
char image_name[32];
snprintf(image_name, 32, "img_%016llx", (uint64_t)image);
strcat(image_name, default_format);
BLI_path_join(
tempfile, sizeof(tempfile), BKE_tempdir_session(), "hydra_image_cache", image_name);
STRNCPY(opts->filepath, tempfile);
if (!BKE_image_save(reports, bmain, image, iuser, opts)) {
BKE_image_save_options_free(opts);
return "";
};
BKE_image_save_options_free(opts);
return tempfile;
}
} // namespace blender::render::hydra

View File

@ -1,23 +0,0 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#pragma once
#include <string>
#include <pxr/base/gf/matrix4d.h>
#include "BKE_image.h"
#include "BKE_image_save.h"
namespace blender::render::hydra {
pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]);
std::string cache_image(Main *bmain,
Scene *scene,
Image *image,
ImageUser *iuser,
ImageSaveOptions *opts,
ReportList *reports);
} // namespace blender::render::hydra