Implement Viewport render with material preview #56

Merged
Bogdan Nagirniak merged 22 commits from BLEN-421 into hydra-render 2023-06-30 09:03:28 +02:00
5 changed files with 103 additions and 37 deletions
Showing only changes of commit feb7befe87 - Show all commits

View File

@ -29,6 +29,7 @@ set(INC
../../blenlib
../../depsgraph
../../blenkernel
../../imbuf
../../gpu
../../gpu/intern
../../python/intern

View File

@ -16,6 +16,7 @@
#include "final_engine.h"
#include "preview_engine.h"
#include "scene_delegate/image.h"
#include "viewport_engine.h"
namespace blender::render::hydra {
@ -221,6 +222,20 @@ static PyObject *engine_set_render_setting_func(PyObject * /*self*/, PyObject *a
Py_RETURN_NONE;
}
static PyObject *cache_or_get_image_file_func(PyObject * /*self*/, PyObject *args)
{
PyObject *pycontext, *pyimage;
if (!PyArg_ParseTuple(args, "OO", &pycontext, &pyimage)) {
Py_RETURN_NONE;
}
bContext *context = (bContext *)PyLong_AsVoidPtr(pycontext);
Image *image = (Image *)PyLong_AsVoidPtr(pyimage);
std::string image_path = cache_or_get_image_file(image, context, nullptr);
return PyUnicode_FromString(image_path.c_str());
}
static PyMethodDef methods[] = {
{"register_plugins", register_plugins_func, METH_VARARGS, ""},
@ -233,6 +248,8 @@ static PyMethodDef methods[] = {
{"engine_set_sync_setting", engine_set_sync_setting_func, METH_VARARGS, ""},
{"engine_set_render_setting", engine_set_render_setting_func, METH_VARARGS, ""},
{"cache_or_get_image_file", cache_or_get_image_file_func, METH_VARARGS, ""},
{NULL, NULL, 0, NULL},
};

View File

@ -4,70 +4,83 @@
#include <pxr/imaging/hio/imageRegistry.h>
#include "BKE_appdir.h"
#include "BKE_image.h"
#include "BKE_image_format.h"
#include "BKE_image_save.h"
#include "BLI_fileops.h"
#include "BLI_path_util.h"
#include "DNA_windowmanager_types.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "blender_scene_delegate.h"
#include "image.h"
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,
bContext *context,
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)) {
return file_path;
}
Main *main = CTX_data_main(scene_delegate->context);
std::string file_path;
Main *main = CTX_data_main(context);
Scene *scene = CTX_data_scene(context);
ImageSaveOptions opts;
opts.im_format.imtype = R_IMF_IMTYPE_RADHDR;
if (BKE_image_save_options_init(&opts, main, scene, image, iuser, false, false)) {
char file_name[32];
const char *r_ext;
BKE_image_path_ext_from_imformat(&scene->r.im_format, &r_ext);
snprintf(file_name, sizeof(file_name), "img_%016llx%s", (uintptr_t)image, r_ext);
if (BKE_image_save_options_init(&opts, main, scene_delegate->scene, image, iuser, false, false))
{
STRNCPY(opts.filepath, file_path);
file_path = get_cache_file(file_name);
if (check_exist && BLI_exists(file_path.c_str())) {
return file_path;
}
opts.save_copy = true;
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;
}
std::string cache_or_get_image_file(Image *image,
BlenderSceneDelegate *scene_delegate,
ImageUser *iuser)
std::string cache_or_get_image_file(Image *image, bContext *context, 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);
file_path = cache_image_file(image, context, iuser, false);
}
else if (BKE_image_has_packedfile(image)) {
file_path = cache_image_file(image, scene_delegate, iuser, true);
file_path = cache_image_file(image, context, iuser, true);
}
else {
Main *main = CTX_data_main(scene_delegate->context);
Main *main = CTX_data_main(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)) {
file_path = cache_image_file(image, scene_delegate, iuser, true);
file_path = cache_image_file(image, context, iuser, true);
}
}
@ -75,4 +88,34 @@ std::string cache_or_get_image_file(Image *image,
return file_path;
}
std::string cache_image_color(float color[4])
{
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);
if (BLI_exists(file_path.c_str())) {
return file_path;
}
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

View File

@ -3,15 +3,12 @@
#pragma once
#include "BKE_context.h"
#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);
std::string cache_or_get_image_file(Image *image, bContext *context, ImageUser *iuser);
std::string cache_image_color(float color[4]);
} // namespace blender::render::hydra

View File

@ -93,7 +93,8 @@ void WorldData::init()
NodeTexImage *tex = static_cast<NodeTexImage *>(color_input_node->storage);
Image *image = (Image *)color_input_node->id;
if (image) {
std::string image_path = cache_or_get_image_file(image, scene_delegate_, &tex->iuser);
std::string image_path = cache_or_get_image_file(
image, scene_delegate_->context, &tex->iuser);
if (!image_path.empty()) {
data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path);
}
@ -120,6 +121,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<pxr::GfVec3f>();
float color[4] = {c[0], c[1], c[2], 1.0f};
std::string image_path = cache_image_color(color);
data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path);
}
}
void WorldData::insert()