Skip to content

Commit

Permalink
Refactorize
Browse files Browse the repository at this point in the history
  • Loading branch information
PENGUINLIONG committed Feb 13, 2023
1 parent 1333cda commit 360bacc
Show file tree
Hide file tree
Showing 38 changed files with 1,392 additions and 1,578 deletions.
37 changes: 16 additions & 21 deletions 1_hello_world/app.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <thread>
#include <chrono>
#include <iostream>
#include "glm/glm.hpp"
#include "taichi/aot_demo/framework.hpp"
#include "taichi/aot_demo/shadow_buffer.hpp"

using namespace ti::aot_demo;

Expand All @@ -15,30 +14,18 @@ struct App1_hello_world : public App {
virtual AppConfig cfg() const override final {
AppConfig out {};
out.app_name = "1_hello_world";
out.supported_archs = {
TI_ARCH_VULKAN,
};
return out;
}
virtual void initialize(TiArch arch) override final{

if(arch != TI_ARCH_VULKAN) {
std::cout << "1_hello_world only supports vulkan backend" << std::endl;
exit(0);
}

GraphicsRuntime& runtime = F_->runtime();
virtual void initialize() override final{
Renderer& renderer = F_->renderer();
ti::Runtime &runtime = F_->runtime();

points = runtime.allocate_vertex_buffer(3, 2, true);
points = runtime.allocate_ndarray<float>({3}, {2}, true);
colors = runtime.allocate_ndarray<float>({3}, {4}, true);

draw_points = runtime.draw_points(points)
.point_size(10.0f)
.color(colors)
.build();

std::cout << "initialized!" << std::endl;
}
virtual bool update() override final {
Renderer& renderer = F_->renderer();

std::vector<glm::vec2> points_data {
{ -0.5f, -0.5f },
{ 0.0f, 0.0f },
Expand All @@ -53,6 +40,14 @@ struct App1_hello_world : public App {
};
colors.write(colors_data);

draw_points = renderer.draw_points(points)
.point_size(10.0f)
.color(colors)
.build();

std::cout << "initialized!" << std::endl;
}
virtual bool update() override final {
std::cout << "stepped! (fps=" << F_->fps() << ")" << std::endl;
return true;
}
Expand Down
40 changes: 9 additions & 31 deletions 1_hello_world_with_interop/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
#include <iostream>
#include "glm/glm.hpp"
#include "taichi/aot_demo/framework.hpp"
#include "taichi/aot_demo/interop/cross_device_copy.hpp"

using namespace ti::aot_demo;

struct App1_hello_world_with_interop : public App {
// Runtime/Ndarray to perform computations
TiArch arch_;
ti::Runtime runtime;
ti::NdArray<float> points;

Expand All @@ -22,31 +20,22 @@ struct App1_hello_world_with_interop : public App {
virtual AppConfig cfg() const override final {
AppConfig out {};
out.app_name = "1_hello_world_with_interop";
out.supported_archs = {
TI_ARCH_VULKAN,
TI_ARCH_CUDA,
TI_ARCH_X64,
TI_ARCH_OPENGL,
};
return out;
}
virtual void initialize(TiArch arch) override final{
if(arch != TI_ARCH_VULKAN && arch != TI_ARCH_X64 && arch != TI_ARCH_CUDA && arch != TI_ARCH_OPENGL) {
std::cout << "1_hello_world_with_interop only supports cuda, x64, vulkan, opengl backends" << std::endl;
exit(0);
}
arch_ = arch;
virtual void initialize() override final{
Renderer &renderer = F_->renderer();

// Prepare Ndarray to store computation results
if(arch_ == TI_ARCH_VULKAN) {
// Reuse the vulkan runtime from renderer framework
runtime = ti::Runtime(arch_, F_->runtime(), false);;
} else {
runtime = ti::Runtime(arch_);
}
points = runtime.allocate_ndarray<float>({3}, {2}, true);

// Prepare vertex buffers for the renderer
GraphicsRuntime& g_runtime = F_->runtime();
render_points = g_runtime.allocate_vertex_buffer(3, 2, true);
colors = g_runtime.allocate_ndarray<float>({3}, {4}, true);

// Renderer renders with data from "render_points" in each frame
draw_points = g_runtime.draw_points(render_points)
draw_points = renderer.draw_points(points)
.point_size(10.0f)
.color(colors)
.build();
Expand All @@ -69,17 +58,6 @@ struct App1_hello_world_with_interop : public App {
};
colors.write(colors_data);

// Copy data from "points" to "render_points"
if(arch_ == TI_ARCH_X64) {
InteropHelper<float>::copy_from_cpu(F_->runtime(), render_points, runtime, points);
} else if(arch_ == TI_ARCH_CUDA) {
InteropHelper<float>::copy_from_cuda(F_->runtime(), render_points, runtime, points);
} else if(arch_ == TI_ARCH_VULKAN) {
InteropHelper<float>::copy_from_vulkan(F_->runtime(), render_points, runtime, points);
} else if(arch_ == TI_ARCH_OPENGL) {
InteropHelper<float>::copy_from_opengl(F_->runtime(), render_points, runtime, points);
}

std::cout << "stepped! (fps=" << F_->fps() << ")" << std::endl;
return true;
}
Expand Down
89 changes: 22 additions & 67 deletions 2_mpm88/app.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <memory>
#include <thread>
#include <chrono>
#include <iostream>
#include "glm/glm.hpp"
#include "taichi/aot_demo/draws/draw_points.hpp"
#include "taichi/aot_demo/framework.hpp"
#include "taichi/aot_demo/interop/cross_device_copy.hpp"
#include "taichi/aot_demo/shadow_buffer.hpp"

using namespace ti::aot_demo;

Expand All @@ -27,42 +29,11 @@ static std::string get_aot_file_dir(TiArch arch) {
}
}

template<typename T>
static void copy_to_vulkan_ndarray(ti::NdArray<T>& dst,
GraphicsRuntime& dst_runtime,
ti::NdArray<T>& src,
ti::Runtime& src_runtime, TiArch src_arch) {

switch(src_arch) {
case TI_ARCH_VULKAN: {
InteropHelper<T>::copy_from_vulkan(dst_runtime, dst, src_runtime, src);
break;
}
case TI_ARCH_X64: {
InteropHelper<T>::copy_from_cpu(dst_runtime, dst, src_runtime, src);
break;
}
case TI_ARCH_CUDA: {
InteropHelper<T>::copy_from_cuda(dst_runtime, dst, src_runtime, src);
break;
}
case TI_ARCH_OPENGL: {
InteropHelper<T>::copy_from_opengl(dst_runtime, dst, src_runtime, src);
break;
}
default: {
throw std::runtime_error("Unable to perform NdArray memory copy");
}
}
}

struct App2_mpm88 : public App {
static const uint32_t NPARTICLE = 8192 * 2;
static const uint32_t GRID_SIZE = 128;

ti::Runtime runtime_;
ti::AotModule module_;
TiArch arch_;

ti::ComputeGraph g_init_;
ti::ComputeGraph g_update_;
Expand All @@ -74,8 +45,6 @@ struct App2_mpm88 : public App {
ti::NdArray<float> J_;
ti::NdArray<float> grid_v_;
ti::NdArray<float> grid_m_;

ti::NdArray<float> render_x_;

std::unique_ptr<GraphicsTask> draw_points;

Expand All @@ -84,49 +53,40 @@ struct App2_mpm88 : public App {
out.app_name = "2_mpm88";
out.framebuffer_width = 256;
out.framebuffer_height = 256;
out.supported_archs = {
TI_ARCH_VULKAN,
TI_ARCH_CUDA,
TI_ARCH_X64,
};
return out;
}


virtual void initialize(TiArch arch) override final{
if(arch != TI_ARCH_VULKAN && arch != TI_ARCH_X64 && arch != TI_ARCH_CUDA && arch != TI_ARCH_OPENGL) {
std::cout << "1_hello_world_with_interop only supports cuda, x64, vulkan, opengl backends" << std::endl;
exit(0);
}
arch_ = arch;

GraphicsRuntime& g_runtime = F_->runtime();
if(arch_ == TI_ARCH_VULKAN) {
// Reuse the vulkan runtime from renderer framework
runtime_ = ti::Runtime(arch_, F_->runtime(), false);;
} else {
runtime_ = ti::Runtime(arch_);
}
virtual void initialize() override final{
Renderer &renderer = F_->renderer();
ti::Runtime &runtime = F_->runtime();

// 2. Load AOT module
#ifdef TI_AOT_DEMO_WITH_ANDROID_APP
std::vector<uint8_t> tcm;
F_->asset_mgr().load_file("E2_mpm88.tcm", tcm);
module_ = runtime_.create_aot_module(tcm);
module_ = runtime.create_aot_module(tcm);
#else
auto aot_file_path = get_aot_file_dir(arch_);
module_ = runtime_.load_aot_module(aot_file_path);
auto aot_file_path = get_aot_file_dir(runtime.arch());
module_ = runtime.load_aot_module(aot_file_path);
#endif

g_init_ = module_.get_compute_graph("init");
g_update_ = module_.get_compute_graph("update");

render_x_ = g_runtime.allocate_vertex_buffer(NPARTICLE, 2, false/*host_access*/);
x_ = runtime.allocate_ndarray<float>({NPARTICLE}, {2});
v_ = runtime.allocate_ndarray<float>({NPARTICLE}, {2});
pos_ = runtime.allocate_ndarray<float>({NPARTICLE}, {3});
C_ = runtime.allocate_ndarray<float>({NPARTICLE}, {2, 2});
J_ = runtime.allocate_ndarray<float>({NPARTICLE}, {});
grid_v_ = runtime.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {2});
grid_m_ = runtime.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {});

x_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {2}, false/*host_access*/);
v_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {2});
pos_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {3});
C_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {2, 2});
J_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {});
grid_v_ = runtime_.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {2});
grid_m_ = runtime_.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {});

draw_points = g_runtime.draw_points(render_x_)
draw_points = renderer.draw_points(x_)
.point_size(3.0f)
.color(glm::vec3(0,0,1))
.build();
Expand All @@ -144,18 +104,13 @@ struct App2_mpm88 : public App {
g_update_["grid_v"] = grid_v_;
g_update_["grid_m"] = grid_m_;

Renderer& renderer = F_->renderer();
renderer.set_framebuffer_size(256, 256);

std::cout << "initialized!" << std::endl;
}
virtual bool update() override final {
g_update_.launch();

auto& g_runtime = F_->runtime();
copy_to_vulkan_ndarray<float>(render_x_, g_runtime, x_, runtime_, arch_);
runtime_.wait();

std::cout << "stepped! (fps=" << F_->fps() << ")" << std::endl;
return true;
}
Expand Down
22 changes: 11 additions & 11 deletions 3_implicit_fem/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ struct App3_implicit_fem : public App {
out.app_name = "3_implicit_fem";
out.framebuffer_width = 256;
out.framebuffer_height = 256;
out.supported_archs = {
TI_ARCH_VULKAN,
TI_ARCH_CUDA,
TI_ARCH_X64,
};
return out;
}
virtual void initialize(TiArch arch) override final{

if(arch != TI_ARCH_VULKAN) {
std::cout << "3_implicit_fem only supports vulkan backend" << std::endl;
exit(0);
}
GraphicsRuntime& runtime = F_->runtime();
Renderer& renderer = F_->renderer();
virtual void initialize() override final{
Renderer &renderer = F_->renderer();
ti::Runtime& runtime = F_->runtime();

#ifdef TI_AOT_DEMO_WITH_ANDROID_APP
std::vector<uint8_t> tcm;
Expand Down Expand Up @@ -84,15 +84,15 @@ struct App3_implicit_fem : public App {

hes_edge_ = runtime.allocate_ndarray<float>({nedge});
hes_vert_ = runtime.allocate_ndarray<float>({ncell});
x_ = runtime.allocate_vertex_buffer(nvert, 3, true);
x_ = runtime.allocate_ndarray<float>({nvert}, {3}, true);
v_ = runtime.allocate_ndarray<float>({nvert}, {3});
f_ = runtime.allocate_ndarray<float>({nvert}, {3});
mul_ans_ = runtime.allocate_ndarray<float>({nvert}, {3});
c2e_ = runtime.allocate_ndarray<int>({ncell}, {6}, true);
b_ = runtime.allocate_ndarray<float>({nvert}, {3});
r0_ = runtime.allocate_ndarray<float>({nvert}, {3});
p0_ = runtime.allocate_ndarray<float>({nvert}, {3});
indices_ = runtime.allocate_index_buffer(nface, 3, true);
indices_ = runtime.allocate_ndarray<uint32_t>({nface}, {3}, true);
vertices_ = runtime.allocate_ndarray<int>({ncell}, {4}, true);
edges_ = runtime.allocate_ndarray<int>({nedge}, {2}, true);
ox_ = runtime.allocate_ndarray<float>({nvert}, {3}, true);
Expand All @@ -116,7 +116,7 @@ struct App3_implicit_fem : public App {
glm::mat4 world2camera = glm::lookAt(glm::vec3(0, 0, 10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
glm::mat4 world2view = camera2view * world2camera;

draw_mesh = runtime.draw_mesh(x_, indices_)
draw_mesh = renderer.draw_mesh(x_, indices_)
.model2world(model2world)
.world2view(world2view)
.color(glm::vec3(0,0,1))
Expand Down
16 changes: 7 additions & 9 deletions 4_texture_fractal/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,25 @@ struct App4_texture_fractal : public App {
out.app_name = "4_texture_fractal";
out.framebuffer_width = 640;
out.framebuffer_height = 320;
out.supported_archs = {
TI_ARCH_VULKAN,
};
return out;
}
virtual void initialize(TiArch arch) override final{

if(arch != TI_ARCH_VULKAN && arch != TI_ARCH_OPENGL) {
std::cout << "4_texture_fractal only supports vulkan, opengl backend" << std::endl;
exit(0);
}
GraphicsRuntime& runtime = F_->runtime();
virtual void initialize() override final{
ti::aot_demo::Renderer& renderer = F_->renderer();
ti::Runtime& runtime = F_->runtime();

module_ = runtime.load_aot_module("4_texture_fractal/assets/fractal");
graph_ = module_.get_compute_graph("fractal");

canvas_ = runtime.allocate_texture2d(640, 320, TI_FORMAT_R32F, TI_NULL_HANDLE);

draw_points = runtime.draw_texture(canvas_)
draw_points = renderer.draw_texture(canvas_)
.build();

graph_["canvas"] = canvas_;

Renderer& renderer = F_->renderer();
renderer.set_framebuffer_size(640, 320);

std::cout << "initialized!" << std::endl;
Expand Down
1 change: 1 addition & 0 deletions 5_sph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ add_demo(5_sph ${CMAKE_CURRENT_SOURCE_DIR}/app.cpp)
generate_aot_files(5_sph "assets/sph.py" "vulkan")
generate_aot_files(5_sph "assets/sph.py" "x64")
generate_aot_files(5_sph "assets/sph.py" "cuda")
generate_aot_files(5_sph "assets/sph.py" "android-vulkan")
Loading

0 comments on commit 360bacc

Please sign in to comment.