Skip to content

Commit 360bacc

Browse files
committed
Refactorize
1 parent 1333cda commit 360bacc

38 files changed

+1392
-1578
lines changed

1_hello_world/app.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#include <thread>
2-
#include <chrono>
31
#include <iostream>
42
#include "glm/glm.hpp"
53
#include "taichi/aot_demo/framework.hpp"
4+
#include "taichi/aot_demo/shadow_buffer.hpp"
65

76
using namespace ti::aot_demo;
87

@@ -15,30 +14,18 @@ struct App1_hello_world : public App {
1514
virtual AppConfig cfg() const override final {
1615
AppConfig out {};
1716
out.app_name = "1_hello_world";
17+
out.supported_archs = {
18+
TI_ARCH_VULKAN,
19+
};
1820
return out;
1921
}
20-
virtual void initialize(TiArch arch) override final{
21-
22-
if(arch != TI_ARCH_VULKAN) {
23-
std::cout << "1_hello_world only supports vulkan backend" << std::endl;
24-
exit(0);
25-
}
26-
27-
GraphicsRuntime& runtime = F_->runtime();
22+
virtual void initialize() override final{
23+
Renderer& renderer = F_->renderer();
24+
ti::Runtime &runtime = F_->runtime();
2825

29-
points = runtime.allocate_vertex_buffer(3, 2, true);
26+
points = runtime.allocate_ndarray<float>({3}, {2}, true);
3027
colors = runtime.allocate_ndarray<float>({3}, {4}, true);
3128

32-
draw_points = runtime.draw_points(points)
33-
.point_size(10.0f)
34-
.color(colors)
35-
.build();
36-
37-
std::cout << "initialized!" << std::endl;
38-
}
39-
virtual bool update() override final {
40-
Renderer& renderer = F_->renderer();
41-
4229
std::vector<glm::vec2> points_data {
4330
{ -0.5f, -0.5f },
4431
{ 0.0f, 0.0f },
@@ -53,6 +40,14 @@ struct App1_hello_world : public App {
5340
};
5441
colors.write(colors_data);
5542

43+
draw_points = renderer.draw_points(points)
44+
.point_size(10.0f)
45+
.color(colors)
46+
.build();
47+
48+
std::cout << "initialized!" << std::endl;
49+
}
50+
virtual bool update() override final {
5651
std::cout << "stepped! (fps=" << F_->fps() << ")" << std::endl;
5752
return true;
5853
}

1_hello_world_with_interop/app.cpp

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
#include <iostream>
44
#include "glm/glm.hpp"
55
#include "taichi/aot_demo/framework.hpp"
6-
#include "taichi/aot_demo/interop/cross_device_copy.hpp"
76

87
using namespace ti::aot_demo;
98

109
struct App1_hello_world_with_interop : public App {
1110
// Runtime/Ndarray to perform computations
12-
TiArch arch_;
1311
ti::Runtime runtime;
1412
ti::NdArray<float> points;
1513

@@ -22,31 +20,22 @@ struct App1_hello_world_with_interop : public App {
2220
virtual AppConfig cfg() const override final {
2321
AppConfig out {};
2422
out.app_name = "1_hello_world_with_interop";
23+
out.supported_archs = {
24+
TI_ARCH_VULKAN,
25+
TI_ARCH_CUDA,
26+
TI_ARCH_X64,
27+
TI_ARCH_OPENGL,
28+
};
2529
return out;
2630
}
27-
virtual void initialize(TiArch arch) override final{
28-
if(arch != TI_ARCH_VULKAN && arch != TI_ARCH_X64 && arch != TI_ARCH_CUDA && arch != TI_ARCH_OPENGL) {
29-
std::cout << "1_hello_world_with_interop only supports cuda, x64, vulkan, opengl backends" << std::endl;
30-
exit(0);
31-
}
32-
arch_ = arch;
31+
virtual void initialize() override final{
32+
Renderer &renderer = F_->renderer();
3333

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

43-
// Prepare vertex buffers for the renderer
44-
GraphicsRuntime& g_runtime = F_->runtime();
45-
render_points = g_runtime.allocate_vertex_buffer(3, 2, true);
46-
colors = g_runtime.allocate_ndarray<float>({3}, {4}, true);
47-
4837
// Renderer renders with data from "render_points" in each frame
49-
draw_points = g_runtime.draw_points(render_points)
38+
draw_points = renderer.draw_points(points)
5039
.point_size(10.0f)
5140
.color(colors)
5241
.build();
@@ -69,17 +58,6 @@ struct App1_hello_world_with_interop : public App {
6958
};
7059
colors.write(colors_data);
7160

72-
// Copy data from "points" to "render_points"
73-
if(arch_ == TI_ARCH_X64) {
74-
InteropHelper<float>::copy_from_cpu(F_->runtime(), render_points, runtime, points);
75-
} else if(arch_ == TI_ARCH_CUDA) {
76-
InteropHelper<float>::copy_from_cuda(F_->runtime(), render_points, runtime, points);
77-
} else if(arch_ == TI_ARCH_VULKAN) {
78-
InteropHelper<float>::copy_from_vulkan(F_->runtime(), render_points, runtime, points);
79-
} else if(arch_ == TI_ARCH_OPENGL) {
80-
InteropHelper<float>::copy_from_opengl(F_->runtime(), render_points, runtime, points);
81-
}
82-
8361
std::cout << "stepped! (fps=" << F_->fps() << ")" << std::endl;
8462
return true;
8563
}

2_mpm88/app.cpp

Lines changed: 22 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#include <memory>
12
#include <thread>
23
#include <chrono>
34
#include <iostream>
45
#include "glm/glm.hpp"
6+
#include "taichi/aot_demo/draws/draw_points.hpp"
57
#include "taichi/aot_demo/framework.hpp"
6-
#include "taichi/aot_demo/interop/cross_device_copy.hpp"
8+
#include "taichi/aot_demo/shadow_buffer.hpp"
79

810
using namespace ti::aot_demo;
911

@@ -27,42 +29,11 @@ static std::string get_aot_file_dir(TiArch arch) {
2729
}
2830
}
2931

30-
template<typename T>
31-
static void copy_to_vulkan_ndarray(ti::NdArray<T>& dst,
32-
GraphicsRuntime& dst_runtime,
33-
ti::NdArray<T>& src,
34-
ti::Runtime& src_runtime, TiArch src_arch) {
35-
36-
switch(src_arch) {
37-
case TI_ARCH_VULKAN: {
38-
InteropHelper<T>::copy_from_vulkan(dst_runtime, dst, src_runtime, src);
39-
break;
40-
}
41-
case TI_ARCH_X64: {
42-
InteropHelper<T>::copy_from_cpu(dst_runtime, dst, src_runtime, src);
43-
break;
44-
}
45-
case TI_ARCH_CUDA: {
46-
InteropHelper<T>::copy_from_cuda(dst_runtime, dst, src_runtime, src);
47-
break;
48-
}
49-
case TI_ARCH_OPENGL: {
50-
InteropHelper<T>::copy_from_opengl(dst_runtime, dst, src_runtime, src);
51-
break;
52-
}
53-
default: {
54-
throw std::runtime_error("Unable to perform NdArray memory copy");
55-
}
56-
}
57-
}
58-
5932
struct App2_mpm88 : public App {
6033
static const uint32_t NPARTICLE = 8192 * 2;
6134
static const uint32_t GRID_SIZE = 128;
6235

63-
ti::Runtime runtime_;
6436
ti::AotModule module_;
65-
TiArch arch_;
6637

6738
ti::ComputeGraph g_init_;
6839
ti::ComputeGraph g_update_;
@@ -74,8 +45,6 @@ struct App2_mpm88 : public App {
7445
ti::NdArray<float> J_;
7546
ti::NdArray<float> grid_v_;
7647
ti::NdArray<float> grid_m_;
77-
78-
ti::NdArray<float> render_x_;
7948

8049
std::unique_ptr<GraphicsTask> draw_points;
8150

@@ -84,49 +53,40 @@ struct App2_mpm88 : public App {
8453
out.app_name = "2_mpm88";
8554
out.framebuffer_width = 256;
8655
out.framebuffer_height = 256;
56+
out.supported_archs = {
57+
TI_ARCH_VULKAN,
58+
TI_ARCH_CUDA,
59+
TI_ARCH_X64,
60+
};
8761
return out;
8862
}
8963

90-
91-
virtual void initialize(TiArch arch) override final{
92-
if(arch != TI_ARCH_VULKAN && arch != TI_ARCH_X64 && arch != TI_ARCH_CUDA && arch != TI_ARCH_OPENGL) {
93-
std::cout << "1_hello_world_with_interop only supports cuda, x64, vulkan, opengl backends" << std::endl;
94-
exit(0);
95-
}
96-
arch_ = arch;
97-
98-
GraphicsRuntime& g_runtime = F_->runtime();
99-
if(arch_ == TI_ARCH_VULKAN) {
100-
// Reuse the vulkan runtime from renderer framework
101-
runtime_ = ti::Runtime(arch_, F_->runtime(), false);;
102-
} else {
103-
runtime_ = ti::Runtime(arch_);
104-
}
64+
virtual void initialize() override final{
65+
Renderer &renderer = F_->renderer();
66+
ti::Runtime &runtime = F_->runtime();
10567

10668
// 2. Load AOT module
10769
#ifdef TI_AOT_DEMO_WITH_ANDROID_APP
10870
std::vector<uint8_t> tcm;
10971
F_->asset_mgr().load_file("E2_mpm88.tcm", tcm);
110-
module_ = runtime_.create_aot_module(tcm);
72+
module_ = runtime.create_aot_module(tcm);
11173
#else
112-
auto aot_file_path = get_aot_file_dir(arch_);
113-
module_ = runtime_.load_aot_module(aot_file_path);
74+
auto aot_file_path = get_aot_file_dir(runtime.arch());
75+
module_ = runtime.load_aot_module(aot_file_path);
11476
#endif
11577

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

119-
render_x_ = g_runtime.allocate_vertex_buffer(NPARTICLE, 2, false/*host_access*/);
81+
x_ = runtime.allocate_ndarray<float>({NPARTICLE}, {2});
82+
v_ = runtime.allocate_ndarray<float>({NPARTICLE}, {2});
83+
pos_ = runtime.allocate_ndarray<float>({NPARTICLE}, {3});
84+
C_ = runtime.allocate_ndarray<float>({NPARTICLE}, {2, 2});
85+
J_ = runtime.allocate_ndarray<float>({NPARTICLE}, {});
86+
grid_v_ = runtime.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {2});
87+
grid_m_ = runtime.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {});
12088

121-
x_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {2}, false/*host_access*/);
122-
v_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {2});
123-
pos_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {3});
124-
C_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {2, 2});
125-
J_ = runtime_.allocate_ndarray<float>({NPARTICLE}, {});
126-
grid_v_ = runtime_.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {2});
127-
grid_m_ = runtime_.allocate_ndarray<float>({GRID_SIZE, GRID_SIZE}, {});
128-
129-
draw_points = g_runtime.draw_points(render_x_)
89+
draw_points = renderer.draw_points(x_)
13090
.point_size(3.0f)
13191
.color(glm::vec3(0,0,1))
13292
.build();
@@ -144,18 +104,13 @@ struct App2_mpm88 : public App {
144104
g_update_["grid_v"] = grid_v_;
145105
g_update_["grid_m"] = grid_m_;
146106

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

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

155-
auto& g_runtime = F_->runtime();
156-
copy_to_vulkan_ndarray<float>(render_x_, g_runtime, x_, runtime_, arch_);
157-
runtime_.wait();
158-
159114
std::cout << "stepped! (fps=" << F_->fps() << ")" << std::endl;
160115
return true;
161116
}

3_implicit_fem/app.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ struct App3_implicit_fem : public App {
4444
out.app_name = "3_implicit_fem";
4545
out.framebuffer_width = 256;
4646
out.framebuffer_height = 256;
47+
out.supported_archs = {
48+
TI_ARCH_VULKAN,
49+
TI_ARCH_CUDA,
50+
TI_ARCH_X64,
51+
};
4752
return out;
4853
}
49-
virtual void initialize(TiArch arch) override final{
50-
51-
if(arch != TI_ARCH_VULKAN) {
52-
std::cout << "3_implicit_fem only supports vulkan backend" << std::endl;
53-
exit(0);
54-
}
55-
GraphicsRuntime& runtime = F_->runtime();
56-
Renderer& renderer = F_->renderer();
54+
virtual void initialize() override final{
55+
Renderer &renderer = F_->renderer();
56+
ti::Runtime& runtime = F_->runtime();
5757

5858
#ifdef TI_AOT_DEMO_WITH_ANDROID_APP
5959
std::vector<uint8_t> tcm;
@@ -84,15 +84,15 @@ struct App3_implicit_fem : public App {
8484

8585
hes_edge_ = runtime.allocate_ndarray<float>({nedge});
8686
hes_vert_ = runtime.allocate_ndarray<float>({ncell});
87-
x_ = runtime.allocate_vertex_buffer(nvert, 3, true);
87+
x_ = runtime.allocate_ndarray<float>({nvert}, {3}, true);
8888
v_ = runtime.allocate_ndarray<float>({nvert}, {3});
8989
f_ = runtime.allocate_ndarray<float>({nvert}, {3});
9090
mul_ans_ = runtime.allocate_ndarray<float>({nvert}, {3});
9191
c2e_ = runtime.allocate_ndarray<int>({ncell}, {6}, true);
9292
b_ = runtime.allocate_ndarray<float>({nvert}, {3});
9393
r0_ = runtime.allocate_ndarray<float>({nvert}, {3});
9494
p0_ = runtime.allocate_ndarray<float>({nvert}, {3});
95-
indices_ = runtime.allocate_index_buffer(nface, 3, true);
95+
indices_ = runtime.allocate_ndarray<uint32_t>({nface}, {3}, true);
9696
vertices_ = runtime.allocate_ndarray<int>({ncell}, {4}, true);
9797
edges_ = runtime.allocate_ndarray<int>({nedge}, {2}, true);
9898
ox_ = runtime.allocate_ndarray<float>({nvert}, {3}, true);
@@ -116,7 +116,7 @@ struct App3_implicit_fem : public App {
116116
glm::mat4 world2camera = glm::lookAt(glm::vec3(0, 0, 10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
117117
glm::mat4 world2view = camera2view * world2camera;
118118

119-
draw_mesh = runtime.draw_mesh(x_, indices_)
119+
draw_mesh = renderer.draw_mesh(x_, indices_)
120120
.model2world(model2world)
121121
.world2view(world2view)
122122
.color(glm::vec3(0,0,1))

4_texture_fractal/app.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,25 @@ struct App4_texture_fractal : public App {
2222
out.app_name = "4_texture_fractal";
2323
out.framebuffer_width = 640;
2424
out.framebuffer_height = 320;
25+
out.supported_archs = {
26+
TI_ARCH_VULKAN,
27+
};
2528
return out;
2629
}
27-
virtual void initialize(TiArch arch) override final{
28-
29-
if(arch != TI_ARCH_VULKAN && arch != TI_ARCH_OPENGL) {
30-
std::cout << "4_texture_fractal only supports vulkan, opengl backend" << std::endl;
31-
exit(0);
32-
}
33-
GraphicsRuntime& runtime = F_->runtime();
30+
virtual void initialize() override final{
31+
ti::aot_demo::Renderer& renderer = F_->renderer();
32+
ti::Runtime& runtime = F_->runtime();
3433

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

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

40-
draw_points = runtime.draw_texture(canvas_)
39+
draw_points = renderer.draw_texture(canvas_)
4140
.build();
4241

4342
graph_["canvas"] = canvas_;
4443

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

4846
std::cout << "initialized!" << std::endl;

5_sph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ add_demo(5_sph ${CMAKE_CURRENT_SOURCE_DIR}/app.cpp)
22
generate_aot_files(5_sph "assets/sph.py" "vulkan")
33
generate_aot_files(5_sph "assets/sph.py" "x64")
44
generate_aot_files(5_sph "assets/sph.py" "cuda")
5+
generate_aot_files(5_sph "assets/sph.py" "android-vulkan")

0 commit comments

Comments
 (0)