Spaces:
Running
on
Zero
Running
on
Zero
Upload ./hy3dgen/texgen/custom_rasterizer/lib/custom_rasterizer_kernel/rasterizer_gpu.cu with huggingface_hub
Browse files
hy3dgen/texgen/custom_rasterizer/lib/custom_rasterizer_kernel/rasterizer_gpu.cu
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include "rasterizer.h"
|
2 |
+
|
3 |
+
__device__ void rasterizeTriangleGPU(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.5f, py + 0.5f};
|
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 |
+
atomicExch(&zbuffer[pixel], (INT64)(idx + 1));
|
22 |
+
continue;
|
23 |
+
}
|
24 |
+
float depth = baryCentricCoordinate[0] * vt0[2] + baryCentricCoordinate[1] * vt1[2] + baryCentricCoordinate[2] * vt2[2];
|
25 |
+
float depth_thres = 0;
|
26 |
+
if (d) {
|
27 |
+
depth_thres = d[pixel] * 0.49999f + 0.5f + occlusion_truncation;
|
28 |
+
}
|
29 |
+
|
30 |
+
int z_quantize = depth * (2<<17);
|
31 |
+
INT64 token = (INT64)z_quantize * MAXINT + (INT64)(idx + 1);
|
32 |
+
if (depth < depth_thres)
|
33 |
+
continue;
|
34 |
+
atomicMin(&zbuffer[pixel], token);
|
35 |
+
}
|
36 |
+
}
|
37 |
+
}
|
38 |
+
}
|
39 |
+
|
40 |
+
__global__ void barycentricFromImgcoordGPU(float* V, int* F, int* findices, INT64* zbuffer, int width, int height, int num_vertices, int num_faces,
|
41 |
+
float* barycentric_map)
|
42 |
+
{
|
43 |
+
int pix = blockIdx.x * blockDim.x + threadIdx.x;
|
44 |
+
if (pix >= width * height)
|
45 |
+
return;
|
46 |
+
INT64 f = zbuffer[pix] % MAXINT;
|
47 |
+
if (f == (MAXINT-1)) {
|
48 |
+
findices[pix] = 0;
|
49 |
+
barycentric_map[pix * 3] = 0;
|
50 |
+
barycentric_map[pix * 3 + 1] = 0;
|
51 |
+
barycentric_map[pix * 3 + 2] = 0;
|
52 |
+
return;
|
53 |
+
}
|
54 |
+
findices[pix] = f;
|
55 |
+
f -= 1;
|
56 |
+
float barycentric[3] = {0, 0, 0};
|
57 |
+
if (f >= 0) {
|
58 |
+
float vt[2] = {float(pix % width) + 0.5f, float(pix / width) + 0.5f};
|
59 |
+
float* vt0_ptr = V + (F[f * 3] * 4);
|
60 |
+
float* vt1_ptr = V + (F[f * 3 + 1] * 4);
|
61 |
+
float* vt2_ptr = V + (F[f * 3 + 2] * 4);
|
62 |
+
|
63 |
+
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};
|
64 |
+
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};
|
65 |
+
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};
|
66 |
+
|
67 |
+
calculateBarycentricCoordinate(vt0, vt1, vt2, vt, barycentric);
|
68 |
+
|
69 |
+
barycentric[0] = barycentric[0] / vt0_ptr[3];
|
70 |
+
barycentric[1] = barycentric[1] / vt1_ptr[3];
|
71 |
+
barycentric[2] = barycentric[2] / vt2_ptr[3];
|
72 |
+
float w = 1.0f / (barycentric[0] + barycentric[1] + barycentric[2]);
|
73 |
+
barycentric[0] *= w;
|
74 |
+
barycentric[1] *= w;
|
75 |
+
barycentric[2] *= w;
|
76 |
+
|
77 |
+
}
|
78 |
+
barycentric_map[pix * 3] = barycentric[0];
|
79 |
+
barycentric_map[pix * 3 + 1] = barycentric[1];
|
80 |
+
barycentric_map[pix * 3 + 2] = barycentric[2];
|
81 |
+
}
|
82 |
+
|
83 |
+
__global__ void rasterizeImagecoordsKernelGPU(float* V, int* F, float* d, INT64* zbuffer, float occlusion_trunc, int width, int height, int num_vertices, int num_faces)
|
84 |
+
{
|
85 |
+
int f = blockIdx.x * blockDim.x + threadIdx.x;
|
86 |
+
if (f >= num_faces)
|
87 |
+
return;
|
88 |
+
|
89 |
+
float* vt0_ptr = V + (F[f * 3] * 4);
|
90 |
+
float* vt1_ptr = V + (F[f * 3 + 1] * 4);
|
91 |
+
float* vt2_ptr = V + (F[f * 3 + 2] * 4);
|
92 |
+
|
93 |
+
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};
|
94 |
+
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};
|
95 |
+
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};
|
96 |
+
|
97 |
+
rasterizeTriangleGPU(f, vt0, vt1, vt2, width, height, zbuffer, d, occlusion_trunc);
|
98 |
+
}
|
99 |
+
|
100 |
+
std::vector<torch::Tensor> rasterize_image_gpu(torch::Tensor V, torch::Tensor F, torch::Tensor D,
|
101 |
+
int width, int height, float occlusion_truncation, int use_depth_prior)
|
102 |
+
{
|
103 |
+
int device_id = V.get_device();
|
104 |
+
cudaSetDevice(device_id);
|
105 |
+
int num_faces = F.size(0);
|
106 |
+
int num_vertices = V.size(0);
|
107 |
+
auto options = torch::TensorOptions().dtype(torch::kInt32).device(torch::kCUDA, device_id).requires_grad(false);
|
108 |
+
auto INT64_options = torch::TensorOptions().dtype(torch::kInt64).device(torch::kCUDA, device_id).requires_grad(false);
|
109 |
+
auto findices = torch::zeros({height, width}, options);
|
110 |
+
INT64 maxint = (INT64)MAXINT * (INT64)MAXINT + (MAXINT - 1);
|
111 |
+
auto z_min = torch::ones({height, width}, INT64_options) * (long)maxint;
|
112 |
+
|
113 |
+
if (!use_depth_prior) {
|
114 |
+
rasterizeImagecoordsKernelGPU<<<(num_faces+255)/256,256,0,at::cuda::getCurrentCUDAStream()>>>(V.data_ptr<float>(), F.data_ptr<int>(), 0,
|
115 |
+
(INT64*)z_min.data_ptr<long>(), occlusion_truncation, width, height, num_vertices, num_faces);
|
116 |
+
} else {
|
117 |
+
rasterizeImagecoordsKernelGPU<<<(num_faces+255)/256,256,0,at::cuda::getCurrentCUDAStream()>>>(V.data_ptr<float>(), F.data_ptr<int>(), D.data_ptr<float>(),
|
118 |
+
(INT64*)z_min.data_ptr<long>(), occlusion_truncation, width, height, num_vertices, num_faces);
|
119 |
+
}
|
120 |
+
|
121 |
+
auto float_options = torch::TensorOptions().dtype(torch::kFloat32).device(torch::kCUDA, device_id).requires_grad(false);
|
122 |
+
auto barycentric = torch::zeros({height, width, 3}, float_options);
|
123 |
+
barycentricFromImgcoordGPU<<<(width * height + 255)/256, 256>>>(V.data_ptr<float>(), F.data_ptr<int>(),
|
124 |
+
findices.data_ptr<int>(), (INT64*)z_min.data_ptr<long>(), width, height, num_vertices, num_faces, barycentric.data_ptr<float>());
|
125 |
+
|
126 |
+
return {findices, barycentric};
|
127 |
+
}
|