Huiwenshi commited on
Commit
0faa2c1
·
verified ·
1 Parent(s): 603236e

Upload ./hy3dgen/texgen/custom_rasterizer/lib/custom_rasterizer_kernel/rasterizer.cpp with huggingface_hub

Browse files
hy3dgen/texgen/custom_rasterizer/lib/custom_rasterizer_kernel/rasterizer.cpp ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "rasterizer.h"
2
+
3
+ void rasterizeTriangleCPU(int idx, float* vt0, float* vt1, float* vt2, int width, int height, INT64* zbuffer, float* d, float occlusion_truncation) {
4
+ float x_min = std::min(vt0[0], std::min(vt1[0],vt2[0]));
5
+ float x_max = std::max(vt0[0], std::max(vt1[0],vt2[0]));
6
+ float y_min = std::min(vt0[1], std::min(vt1[1],vt2[1]));
7
+ float y_max = std::max(vt0[1], std::max(vt1[1],vt2[1]));
8
+
9
+ for (int px = x_min; px < x_max + 1; ++px) {
10
+ if (px < 0 || px >= width)
11
+ continue;
12
+ for (int py = y_min; py < y_max + 1; ++py) {
13
+ if (py < 0 || py >= height)
14
+ continue;
15
+ float vt[2] = {px + 0.5, py + 0.5};
16
+ float baryCentricCoordinate[3];
17
+ calculateBarycentricCoordinate(vt0, vt1, vt2, vt, baryCentricCoordinate);
18
+ if (isBarycentricCoordInBounds(baryCentricCoordinate)) {
19
+ int pixel = py * width + px;
20
+ if (zbuffer == 0) {
21
+ zbuffer[pixel] = (INT64)(idx + 1);
22
+ continue;
23
+ }
24
+
25
+ float depth = baryCentricCoordinate[0] * vt0[2] + baryCentricCoordinate[1] * vt1[2] + baryCentricCoordinate[2] * vt2[2];
26
+ float depth_thres = 0;
27
+ if (d) {
28
+ depth_thres = d[pixel] * 0.49999f + 0.5f + occlusion_truncation;
29
+ }
30
+
31
+ int z_quantize = depth * (2<<17);
32
+ INT64 token = (INT64)z_quantize * MAXINT + (INT64)(idx + 1);
33
+ if (depth < depth_thres)
34
+ continue;
35
+ zbuffer[pixel] = std::min(zbuffer[pixel], token);
36
+ }
37
+ }
38
+ }
39
+ }
40
+
41
+ void barycentricFromImgcoordCPU(float* V, int* F, int* findices, INT64* zbuffer, int width, int height, int num_vertices, int num_faces,
42
+ float* barycentric_map, int pix)
43
+ {
44
+ INT64 f = zbuffer[pix] % MAXINT;
45
+ if (f == (MAXINT-1)) {
46
+ findices[pix] = 0;
47
+ barycentric_map[pix * 3] = 0;
48
+ barycentric_map[pix * 3 + 1] = 0;
49
+ barycentric_map[pix * 3 + 2] = 0;
50
+ return;
51
+ }
52
+ findices[pix] = f;
53
+ f -= 1;
54
+ float barycentric[3] = {0, 0, 0};
55
+ if (f >= 0) {
56
+ float vt[2] = {float(pix % width) + 0.5f, float(pix / width) + 0.5f};
57
+ float* vt0_ptr = V + (F[f * 3] * 4);
58
+ float* vt1_ptr = V + (F[f * 3 + 1] * 4);
59
+ float* vt2_ptr = V + (F[f * 3 + 2] * 4);
60
+
61
+ float vt0[2] = {(vt0_ptr[0] / vt0_ptr[3] * 0.5f + 0.5f) * (width - 1) + 0.5f, (0.5f + 0.5f * vt0_ptr[1] / vt0_ptr[3]) * (height - 1) + 0.5f};
62
+ float vt1[2] = {(vt1_ptr[0] / vt1_ptr[3] * 0.5f + 0.5f) * (width - 1) + 0.5f, (0.5f + 0.5f * vt1_ptr[1] / vt1_ptr[3]) * (height - 1) + 0.5f};
63
+ float vt2[2] = {(vt2_ptr[0] / vt2_ptr[3] * 0.5f + 0.5f) * (width - 1) + 0.5f, (0.5f + 0.5f * vt2_ptr[1] / vt2_ptr[3]) * (height - 1) + 0.5f};
64
+
65
+ calculateBarycentricCoordinate(vt0, vt1, vt2, vt, barycentric);
66
+
67
+ barycentric[0] = barycentric[0] / vt0_ptr[3];
68
+ barycentric[1] = barycentric[1] / vt1_ptr[3];
69
+ barycentric[2] = barycentric[2] / vt2_ptr[3];
70
+ float w = 1.0f / (barycentric[0] + barycentric[1] + barycentric[2]);
71
+ barycentric[0] *= w;
72
+ barycentric[1] *= w;
73
+ barycentric[2] *= w;
74
+
75
+ }
76
+ barycentric_map[pix * 3] = barycentric[0];
77
+ barycentric_map[pix * 3 + 1] = barycentric[1];
78
+ barycentric_map[pix * 3 + 2] = barycentric[2];
79
+ }
80
+
81
+ void rasterizeImagecoordsKernelCPU(float* V, int* F, float* d, INT64* zbuffer, float occlusion_trunc, int width, int height, int num_vertices, int num_faces, int f)
82
+ {
83
+ float* vt0_ptr = V + (F[f * 3] * 4);
84
+ float* vt1_ptr = V + (F[f * 3 + 1] * 4);
85
+ float* vt2_ptr = V + (F[f * 3 + 2] * 4);
86
+
87
+ float vt0[3] = {(vt0_ptr[0] / vt0_ptr[3] * 0.5f + 0.5f) * (width - 1) + 0.5f, (0.5f + 0.5f * vt0_ptr[1] / vt0_ptr[3]) * (height - 1) + 0.5f, vt0_ptr[2] / vt0_ptr[3] * 0.49999f + 0.5f};
88
+ float vt1[3] = {(vt1_ptr[0] / vt1_ptr[3] * 0.5f + 0.5f) * (width - 1) + 0.5f, (0.5f + 0.5f * vt1_ptr[1] / vt1_ptr[3]) * (height - 1) + 0.5f, vt1_ptr[2] / vt1_ptr[3] * 0.49999f + 0.5f};
89
+ float vt2[3] = {(vt2_ptr[0] / vt2_ptr[3] * 0.5f + 0.5f) * (width - 1) + 0.5f, (0.5f + 0.5f * vt2_ptr[1] / vt2_ptr[3]) * (height - 1) + 0.5f, vt2_ptr[2] / vt2_ptr[3] * 0.49999f + 0.5f};
90
+
91
+ rasterizeTriangleCPU(f, vt0, vt1, vt2, width, height, zbuffer, d, occlusion_trunc);
92
+ }
93
+
94
+ std::vector<torch::Tensor> rasterize_image_cpu(torch::Tensor V, torch::Tensor F, torch::Tensor D,
95
+ int width, int height, float occlusion_truncation, int use_depth_prior)
96
+ {
97
+ int num_faces = F.size(0);
98
+ int num_vertices = V.size(0);
99
+ auto options = torch::TensorOptions().dtype(torch::kInt32).requires_grad(false);
100
+ auto INT64_options = torch::TensorOptions().dtype(torch::kInt64).requires_grad(false);
101
+ auto findices = torch::zeros({height, width}, options);
102
+ INT64 maxint = (INT64)MAXINT * (INT64)MAXINT + (MAXINT - 1);
103
+ auto z_min = torch::ones({height, width}, INT64_options) * (long)maxint;
104
+
105
+ if (!use_depth_prior) {
106
+ for (int i = 0; i < num_faces; ++i) {
107
+ rasterizeImagecoordsKernelCPU(V.data_ptr<float>(), F.data_ptr<int>(), 0,
108
+ (INT64*)z_min.data_ptr<long>(), occlusion_truncation, width, height, num_vertices, num_faces, i);
109
+ }
110
+ } else {
111
+ for (int i = 0; i < num_faces; ++i)
112
+ rasterizeImagecoordsKernelCPU(V.data_ptr<float>(), F.data_ptr<int>(), D.data_ptr<float>(),
113
+ (INT64*)z_min.data_ptr<long>(), occlusion_truncation, width, height, num_vertices, num_faces, i);
114
+ }
115
+
116
+ auto float_options = torch::TensorOptions().dtype(torch::kFloat32).requires_grad(false);
117
+ auto barycentric = torch::zeros({height, width, 3}, float_options);
118
+ for (int i = 0; i < width * height; ++i)
119
+ barycentricFromImgcoordCPU(V.data_ptr<float>(), F.data_ptr<int>(),
120
+ findices.data_ptr<int>(), (INT64*)z_min.data_ptr<long>(), width, height, num_vertices, num_faces, barycentric.data_ptr<float>(), i);
121
+
122
+ return {findices, barycentric};
123
+ }
124
+
125
+ std::vector<torch::Tensor> rasterize_image(torch::Tensor V, torch::Tensor F, torch::Tensor D,
126
+ int width, int height, float occlusion_truncation, int use_depth_prior)
127
+ {
128
+ int device_id = V.get_device();
129
+ if (device_id == -1)
130
+ return rasterize_image_cpu(V, F, D, width, height, occlusion_truncation, use_depth_prior);
131
+ else
132
+ return rasterize_image_gpu(V, F, D, width, height, occlusion_truncation, use_depth_prior);
133
+ }
134
+
135
+ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
136
+ m.def("rasterize_image", &rasterize_image, "Custom image rasterization");
137
+ m.def("build_hierarchy", &build_hierarchy, "Custom image rasterization");
138
+ m.def("build_hierarchy_with_feat", &build_hierarchy_with_feat, "Custom image rasterization");
139
+ }