Skip to content

Commit

Permalink
transform
Browse files Browse the repository at this point in the history
  • Loading branch information
iaomw committed Aug 28, 2024
1 parent 36ee752 commit 45b2ee6
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 29 deletions.
8 changes: 4 additions & 4 deletions zeno/src/nodes/prim/SimpleGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
13 changes: 13 additions & 0 deletions zeno/src/nodes/prim/TransformPrimitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
Expand Down
27 changes: 22 additions & 5 deletions zenovis/src/optx/RenderEngineOptx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,32 @@ struct GraphicsManager {

auto type_index = ud.get2<uint>("curve", 0u);
auto path_string = ud.get2<std::string>("path", "");
auto yup = ud.get2<bool>("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<zeno::vec4f>("_transform_row0");
auto row1 = ud.get2<zeno::vec4f>("_transform_row1");
auto row2 = ud.get2<zeno::vec4f>("_transform_row2");
auto row3 = ud.get2<zeno::vec4f>("_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<bool>("yup", true);
auto trans = yup? glm::mat4 {
0, 0, 1, 0,
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 1
} : glm::mat4(1.0);

trans = transform * trans;
loadHair( path_string, mtlid, type_index, trans);
return;
}
Expand Down Expand Up @@ -287,10 +304,10 @@ struct GraphicsManager {
} else {

//zeno::vec4f row0, row1, row2, row3;
auto row0 = ud.get2<zeno::vec4f>("sphere_transform_row0");
auto row1 = ud.get2<zeno::vec4f>("sphere_transform_row1");
auto row2 = ud.get2<zeno::vec4f>("sphere_transform_row2");
auto row3 = ud.get2<zeno::vec4f>("sphere_transform_row3");
auto row0 = ud.get2<zeno::vec4f>("_transform_row0");
auto row1 = ud.get2<zeno::vec4f>("_transform_row1");
auto row2 = ud.get2<zeno::vec4f>("_transform_row2");
auto row3 = ud.get2<zeno::vec4f>("_transform_row3");

glm::mat4 sphere_transform;
auto transform_ptr = glm::value_ptr(sphere_transform);
Expand Down
2 changes: 2 additions & 0 deletions zenovis/xinxinoptix/hair/Hair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "." );

Expand Down
7 changes: 5 additions & 2 deletions zenovis/xinxinoptix/hair/Hair.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <vector>
#include <string>
#include <ostream>
#include <filesystem>

static const std::map<zeno::CurveType, OptixPrimitiveType> CURVE_TYPE_MAP {

Expand Down Expand Up @@ -86,6 +87,8 @@ class Hair
std::vector<uint> 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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 );
};

Expand Down
2 changes: 2 additions & 0 deletions zenovis/xinxinoptix/hair/optixHair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
55 changes: 38 additions & 17 deletions zenovis/xinxinoptix/hair/optixHair.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <set>
#include <map>
#include <memory>
#include <filesystem>

#include <glm/common.hpp>
#include <glm/mat4x4.hpp>
Expand Down Expand Up @@ -91,7 +92,7 @@ std::vector<uint> strandIndices(zeno::CurveType curveType, const std::vector<uin
};

inline std::map< std::string, std::shared_ptr<Hair> > hair_cache;
inline std::map< std::tuple<std::string, uint>, std::shared_ptr<HairState> > geo_hair_map;
inline std::map< std::tuple<std::string, uint>, std::shared_ptr<HairState> > geo_hair_cache;

using hair_state_key = std::tuple<std::string, uint, std::string>;

Expand All @@ -100,21 +101,37 @@ inline std::map<hair_state_key, std::vector<glm::mat4>> 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<Hair> {
if (hair_cache.count(filePath) == 0)

if (hair_cache.count(filePath) == 0 || lwt != hair_cache[filePath]->time())
{
neo = true;
auto tmp = std::make_shared<Hair>( filePath );
tmp->prepareWidths();
hair_cache[filePath] = tmp;
return tmp;
}
return hair_cache[filePath];
} ();

auto state = std::make_shared<HairState>();
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<HairState>();
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)) {
Expand All @@ -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<std::string, uint> > 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);
}
}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion zenovis/xinxinoptix/optixPathTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down

0 comments on commit 45b2ee6

Please sign in to comment.