From 45b2ee6fb3d3f7c9c27625dd6622676c0c8aced7 Mon Sep 17 00:00:00 2001 From: iaomw Date: Wed, 28 Aug 2024 18:59:59 +0800 Subject: [PATCH] transform --- zeno/src/nodes/prim/SimpleGeometry.cpp | 8 ++-- zeno/src/nodes/prim/TransformPrimitive.cpp | 13 +++++ zenovis/src/optx/RenderEngineOptx.cpp | 27 +++++++++-- zenovis/xinxinoptix/hair/Hair.cpp | 2 + zenovis/xinxinoptix/hair/Hair.h | 7 ++- zenovis/xinxinoptix/hair/optixHair.cpp | 2 + zenovis/xinxinoptix/hair/optixHair.h | 55 +++++++++++++++------- zenovis/xinxinoptix/optixPathTracer.cpp | 2 +- 8 files changed, 87 insertions(+), 29 deletions(-) diff --git a/zeno/src/nodes/prim/SimpleGeometry.cpp b/zeno/src/nodes/prim/SimpleGeometry.cpp index 52c09cde8f..fc2c51c2ec 100644 --- a/zeno/src/nodes/prim/SimpleGeometry.cpp +++ b/zeno/src/nodes/prim/SimpleGeometry.cpp @@ -1283,10 +1283,10 @@ struct CreateSphere : zeno::INode { memcpy(row2.data(), transform_ptr+8, sizeof(float)*4); memcpy(row3.data(), transform_ptr+12, sizeof(float)*4); - prim->userData().set2("sphere_transform_row0", row0); - prim->userData().set2("sphere_transform_row1", row1); - prim->userData().set2("sphere_transform_row2", row2); - prim->userData().set2("sphere_transform_row3", row3); + prim->userData().set2("_transform_row0", row0); + prim->userData().set2("_transform_row1", row1); + prim->userData().set2("_transform_row2", row2); + prim->userData().set2("_transform_row3", row3); } set_output("prim",std::move(prim)); diff --git a/zeno/src/nodes/prim/TransformPrimitive.cpp b/zeno/src/nodes/prim/TransformPrimitive.cpp index 937a5140a9..ec7777f5c7 100644 --- a/zeno/src/nodes/prim/TransformPrimitive.cpp +++ b/zeno/src/nodes/prim/TransformPrimitive.cpp @@ -377,6 +377,19 @@ struct PrimitiveTransform : zeno::INode { } } + auto transform_ptr = glm::value_ptr(matrix); + + zeno::vec4f row0, row1, row2, row3; + memcpy(row0.data(), transform_ptr, sizeof(float)*4); + memcpy(row1.data(), transform_ptr+4, sizeof(float)*4); + memcpy(row2.data(), transform_ptr+8, sizeof(float)*4); + memcpy(row3.data(), transform_ptr+12, sizeof(float)*4); + + iObject->userData().set2("_transform_row0", row0); + iObject->userData().set2("_transform_row1", row1); + iObject->userData().set2("_transform_row2", row2); + iObject->userData().set2("_transform_row3", row3); + set_output("outPrim", std::move(iObject)); } }; diff --git a/zenovis/src/optx/RenderEngineOptx.cpp b/zenovis/src/optx/RenderEngineOptx.cpp index 229b2c85c1..27fb426cbb 100644 --- a/zenovis/src/optx/RenderEngineOptx.cpp +++ b/zenovis/src/optx/RenderEngineOptx.cpp @@ -251,8 +251,24 @@ struct GraphicsManager { auto type_index = ud.get2("curve", 0u); auto path_string = ud.get2("path", ""); - auto yup = ud.get2("yup", true); + glm::mat4 transform(1.0f); + auto transform_ptr = glm::value_ptr(transform); + + if (ud.has("_transform_row0") && ud.has("_transform_row1") && ud.has("_transform_row2") && ud.has("_transform_row3")) { + + auto row0 = ud.get2("_transform_row0"); + auto row1 = ud.get2("_transform_row1"); + auto row2 = ud.get2("_transform_row2"); + auto row3 = ud.get2("_transform_row3"); + + memcpy(transform_ptr, row0.data(), sizeof(float)*4); + memcpy(transform_ptr+4, row1.data(), sizeof(float)*4); + memcpy(transform_ptr+8, row2.data(), sizeof(float)*4); + memcpy(transform_ptr+12, row3.data(), sizeof(float)*4); + } + + auto yup = ud.get2("yup", true); auto trans = yup? glm::mat4 { 0, 0, 1, 0, 1, 0, 0, 0, @@ -260,6 +276,7 @@ struct GraphicsManager { 0, 0, 0, 1 } : glm::mat4(1.0); + trans = transform * trans; loadHair( path_string, mtlid, type_index, trans); return; } @@ -287,10 +304,10 @@ struct GraphicsManager { } else { //zeno::vec4f row0, row1, row2, row3; - auto row0 = ud.get2("sphere_transform_row0"); - auto row1 = ud.get2("sphere_transform_row1"); - auto row2 = ud.get2("sphere_transform_row2"); - auto row3 = ud.get2("sphere_transform_row3"); + auto row0 = ud.get2("_transform_row0"); + auto row1 = ud.get2("_transform_row1"); + auto row2 = ud.get2("_transform_row2"); + auto row3 = ud.get2("_transform_row3"); glm::mat4 sphere_transform; auto transform_ptr = glm::value_ptr(sphere_transform); diff --git a/zenovis/xinxinoptix/hair/Hair.cpp b/zenovis/xinxinoptix/hair/Hair.cpp index f6a8d921df..0bc94c4fb8 100644 --- a/zenovis/xinxinoptix/hair/Hair.cpp +++ b/zenovis/xinxinoptix/hair/Hair.cpp @@ -35,6 +35,8 @@ Hair::Hair( const std::string& fileName ) { + last_write_time = std::filesystem::last_write_time(fileName); + std::ifstream input( fileName.c_str(), std::ios::binary ); SUTIL_ASSERT_MSG( input.is_open(), "Unable to open " + fileName + "." ); diff --git a/zenovis/xinxinoptix/hair/Hair.h b/zenovis/xinxinoptix/hair/Hair.h index f1c59207a6..4164626118 100644 --- a/zenovis/xinxinoptix/hair/Hair.h +++ b/zenovis/xinxinoptix/hair/Hair.h @@ -37,6 +37,7 @@ #include #include #include +#include static const std::map CURVE_TYPE_MAP { @@ -86,6 +87,8 @@ class Hair std::vector segments(zeno::CurveType mode) const; sutil::Aabb aabb() const { return m_aabb; } + + std::filesystem::file_time_type time() const { return last_write_time; } protected: bool hasSegments() const; @@ -94,8 +97,6 @@ class Hair bool hasAlpha() const; bool hasColor() const; - OptixTraversableHandle gas() const; - uint32_t defaultNumberOfSegments() const; float defaultThickness() const; float defaultAlpha() const; @@ -146,6 +147,8 @@ class Hair mutable sutil::Aabb m_aabb; + std::filesystem::file_time_type last_write_time; + friend std::ostream& operator<<( std::ostream& o, const Hair& hair ); }; diff --git a/zenovis/xinxinoptix/hair/optixHair.cpp b/zenovis/xinxinoptix/hair/optixHair.cpp index ffef6b0bf0..d8783f99ba 100644 --- a/zenovis/xinxinoptix/hair/optixHair.cpp +++ b/zenovis/xinxinoptix/hair/optixHair.cpp @@ -169,6 +169,8 @@ void HairState::makeHairGAS(OptixDeviceContext context) { auto pState = this; const Hair* pHair = pState->pHair.get(); + + if (pState->gasHandle) return; pState->gasHandle = 0; pState->gasBuffer.reset(); diff --git a/zenovis/xinxinoptix/hair/optixHair.h b/zenovis/xinxinoptix/hair/optixHair.h index 27a8629340..bd843e04b1 100644 --- a/zenovis/xinxinoptix/hair/optixHair.h +++ b/zenovis/xinxinoptix/hair/optixHair.h @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -91,7 +92,7 @@ std::vector strandIndices(zeno::CurveType curveType, const std::vector > hair_cache; -inline std::map< std::tuple, std::shared_ptr > geo_hair_map; +inline std::map< std::tuple, std::shared_ptr > geo_hair_cache; using hair_state_key = std::tuple; @@ -100,21 +101,37 @@ inline std::map> hair_yyy_cache; inline void loadHair(const std::string& filePath, const std::string& mtlid, uint mode, glm::mat4 transform=glm::mat4(1.0f)) { + auto lwt = std::filesystem::last_write_time(filePath); + bool neo = false; + auto hair = [&]() -> std::shared_ptr { - if (hair_cache.count(filePath) == 0) + + if (hair_cache.count(filePath) == 0 || lwt != hair_cache[filePath]->time()) { + neo = true; auto tmp = std::make_shared( filePath ); tmp->prepareWidths(); hair_cache[filePath] = tmp; + return tmp; } return hair_cache[filePath]; } (); - auto state = std::make_shared(); - state->curveType = (zeno::CurveType)mode; - state->pHair = hair; + auto hairState = [&]() { + auto key = std::tuple {filePath, mode}; + + if (geo_hair_cache.count( key ) == 0 || neo) { - geo_hair_map[ std::tuple{filePath, mode} ] = state; + auto tmp = std::make_shared(); + tmp->curveType = (zeno::CurveType)mode; + tmp->pHair = hair; + + geo_hair_cache[ key ] = tmp; + return tmp; + } + + return geo_hair_cache[key]; + } (); auto key = std::tuple{ filePath, mode, mtlid}; if (hair_xxx_cache.count(key)) { @@ -126,24 +143,28 @@ inline void loadHair(const std::string& filePath, const std::string& mtlid, uint inline void prepareHairs(OptixDeviceContext context) { - std::vector< std::tuple > garbage; - for (auto& [key, _] : geo_hair_map) { - auto& [filePath, mode] = key; + decltype(hair_cache) hair_cache_tmp; + decltype(geo_hair_cache) geo_hair_cache_tmp; + + for (auto& [key, val] : hair_xxx_cache) { + auto& [filePath, mode, mtlid] = key; + if (hair_cache.count(filePath)) { - continue; + hair_cache_tmp[filePath] = hair_cache[filePath]; } - garbage.push_back(key); - } - for (auto& key : garbage) { - geo_hair_map.erase(key); + if (geo_hair_cache.count( {filePath, mode} )) { + geo_hair_cache_tmp[ {filePath, mode} ] = geo_hair_cache[ {filePath, mode} ]; + } } + hair_cache = std::move(hair_cache_tmp); + geo_hair_cache = std::move(geo_hair_cache_tmp); + hair_yyy_cache = hair_xxx_cache; hair_xxx_cache.clear(); - hair_cache.clear(); - for (auto& [key, state] : geo_hair_map) { + for (auto& [key, state] : geo_hair_cache) { state->makeHairGAS(context); } } @@ -189,7 +210,7 @@ inline void prepareCurveGroup(OptixDeviceContext context) { inline void cleanupHairs() { hair_cache.clear(); - geo_hair_map.clear(); + geo_hair_cache.clear(); hair_xxx_cache.clear(); hair_yyy_cache.clear(); diff --git a/zenovis/xinxinoptix/optixPathTracer.cpp b/zenovis/xinxinoptix/optixPathTracer.cpp index 7548b7ed82..08a473e8cb 100644 --- a/zenovis/xinxinoptix/optixPathTracer.cpp +++ b/zenovis/xinxinoptix/optixPathTracer.cpp @@ -1294,7 +1294,7 @@ void updateRootIAS() auto combinedID = mtlid + ":" + std::to_string(shader_mark); auto shader_index = OptixUtil::matIDtoShaderIndex[combinedID]; - auto& hair_state = geo_hair_map[ std::tuple(filePath, mode) ]; + auto& hair_state = geo_hair_cache[ std::tuple(filePath, mode) ]; opinstance.flags = OPTIX_INSTANCE_FLAG_NONE; //opinstance.instanceId = op_index++;