Skip to content

Commit

Permalink
fimpl
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Feb 2, 2022
1 parent 8336fb8 commit a7628bc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 26 deletions.
6 changes: 4 additions & 2 deletions 09/01_texture/06/main.cu
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,13 @@ int main() {
}

std::vector<std::thread> tpool;
for (int frame = 1; frame <= 100; frame++) {
for (int frame = 1; frame <= 250; frame++) {
std::vector<float4> cpu(n * n * n);
sim.clr->copyOut(cpu.data());
tpool.push_back(std::thread([cpu = std::move(cpu), frame, n] {
writevdb<float, 1>("/tmp/a" + std::to_string(1000 + frame).substr(1) + ".vdb", cpu.data(), n, n, n, sizeof(float4));
VDBWriter writer;
writer.addGrid<float, 1>("density", cpu.data(), n, n, n, sizeof(float4));
writer.write("/tmp/a" + std::to_string(1000 + frame).substr(1) + ".vdb");
}));

printf("frame=%d, loss=%f\n", frame, sim.calc_loss());
Expand Down
85 changes: 61 additions & 24 deletions 09/01_texture/06/writevdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,65 @@

#include <string>
#include <array>
#include <vector>
#include <memory>

template <class T, size_t N>
struct _impl_writevdb {
std::string const &path;
void const *base;
uint32_t sizex, sizey, sizez;
int32_t minx, miny, minz;
uint32_t pitchx, pitchy, pitchz;

void operator()() const;
};
class VDBWriter {
struct Impl;
std::unique_ptr<Impl> const impl;

template <class T, size_t N = 1>
static void writevdb(std::string const &path, void const *base, uint32_t sizex, uint32_t sizey, uint32_t sizez, uint32_t pitchx = 0, uint32_t pitchy = 0, uint32_t pitchz = 0) {
if (pitchx == 0) pitchx = sizeof(T) * N;
if (pitchy == 0) pitchy = pitchx * sizex;
if (pitchz == 0) pitchz = pitchy * sizey;
int32_t minx = -(int32_t)sizex / 2;
int32_t miny = -(int32_t)sizey / 2;
int32_t minz = -(int32_t)sizez / 2;
_impl_writevdb<T, N>{path, base, sizex, sizey, sizez, minx, miny, minz, pitchx, pitchy, pitchz}();
}
template <class T, size_t N>
struct AddGridImpl {
VDBWriter *that;
std::string name;
void const *base;
uint32_t sizex, sizey, sizez;
int32_t minx, miny, minz;
uint32_t pitchx, pitchy, pitchz;

void operator()() const;
};

public:
VDBWriter();
~VDBWriter();

VDBWriter(VDBWriter const &) = delete;
VDBWriter &operator=(VDBWriter const &) = delete;
VDBWriter(VDBWriter &&) = delete;
VDBWriter &operator=(VDBWriter &&) = delete;

template <class T, size_t N, bool normalizedCoords = true>
void addGrid(std::string const &name, void const *base, uint32_t sizex, uint32_t sizey, uint32_t sizez, uint32_t pitchx = 0, uint32_t pitchy = 0, uint32_t pitchz = 0) {
if (pitchx == 0) pitchx = sizeof(T) * N;
if (pitchy == 0) pitchy = pitchx * sizex;
if (pitchz == 0) pitchz = pitchy * sizey;
int32_t minx = normalizedCoords ? -(int32_t)sizex / 2 : 0;
int32_t miny = normalizedCoords ? -(int32_t)sizey / 2 : 0;
int32_t minz = normalizedCoords ? -(int32_t)sizez / 2 : 0;
AddGridImpl<T, N>{this, name, base, sizex, sizey, sizez, minx, miny, minz, pitchx, pitchy, pitchz}();
}

void write(std::string const &path);
};

#ifdef WRITEVDB_IMPLEMENTATION
#include <openvdb/openvdb.h>
#include <openvdb/tools/Dense.h>

struct VDBWriter::Impl {
openvdb::GridPtrVec grids;
};

VDBWriter::VDBWriter() : impl(std::make_unique<Impl>()) {
}

VDBWriter::~VDBWriter() = default;

void VDBWriter::write(std::string const &path) {
openvdb::io::File(path).write(impl->grids);
}

namespace {

template <class T, size_t N>
Expand All @@ -54,8 +86,9 @@ VecT help_make_vec(T const *ptr, std::index_sequence<Is...>) {
}

template <class T, size_t N>
void _impl_writevdb<T, N>::operator()() const {
void VDBWriter::AddGridImpl<T, N>::operator()() const {
using GridT = typename vdbtraits<T, N>::type;

openvdb::tools::Dense<typename GridT::ValueType> dens(openvdb::Coord(sizex, sizey, sizez), openvdb::Coord(minx, miny, minz));
for (uint32_t z = 0; z < sizez; z++) {
for (uint32_t y = 0; y < sizey; y++) {
Expand All @@ -65,12 +98,16 @@ void _impl_writevdb<T, N>::operator()() const {
}
}
}

auto grid = GridT::create();
typename GridT::ValueType tolerance{0};
openvdb::tools::copyFromDense(dens, grid->tree(), tolerance);
openvdb::io::File(path).write({grid});

openvdb::MetaMap &meta = *grid;
meta.insertMeta(openvdb::Name("name"), openvdb::TypedMetadata<std::string>(name));
that->impl->grids.push_back(grid);
}

template struct _impl_writevdb<float, 1>;
template struct _impl_writevdb<float, 3>;
template struct VDBWriter::AddGridImpl<float, 1>;
template struct VDBWriter::AddGridImpl<float, 3>;
#endif
Binary file modified 09/slides.pptx
Binary file not shown.

0 comments on commit a7628bc

Please sign in to comment.