Skip to content

Commit a783865

Browse files
committed
output absolute depth
1 parent 9994002 commit a783865

File tree

7 files changed

+29
-12
lines changed

7 files changed

+29
-12
lines changed

cuda_rasterizer/forward.cu

+14-3
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ renderCUDA(
285285
const float* __restrict__ bg_color,
286286
float* __restrict__ out_color,
287287
const float* __restrict__ depths,
288-
float* __restrict__ invdepth)
288+
float* __restrict__ invdepth,
289+
float* __restrict__ out_depth)
289290
{
290291
// Identify current tile and associated min/max pixel range.
291292
auto block = cg::this_thread_block();
@@ -310,12 +311,14 @@ renderCUDA(
310311
__shared__ int collected_id[BLOCK_SIZE];
311312
__shared__ float2 collected_xy[BLOCK_SIZE];
312313
__shared__ float4 collected_conic_opacity[BLOCK_SIZE];
314+
__shared__ float collected_depth[BLOCK_SIZE];
313315

314316
// Initialize helper variables
315317
float T = 1.0f;
316318
uint32_t contributor = 0;
317319
uint32_t last_contributor = 0;
318320
float C[CHANNELS] = { 0 };
321+
float D = 65503.0f; // Median Depth. TODO: This is a hack setting max_depth to 65503.0
319322

320323
float expected_invdepth = 0.0f;
321324

@@ -335,6 +338,7 @@ renderCUDA(
335338
collected_id[block.thread_rank()] = coll_id;
336339
collected_xy[block.thread_rank()] = points_xy_image[coll_id];
337340
collected_conic_opacity[block.thread_rank()] = conic_opacity[coll_id];
341+
collected_depth[block.thread_rank()] = depths[coll_id];
338342
}
339343
block.sync();
340344

@@ -371,6 +375,10 @@ renderCUDA(
371375
for (int ch = 0; ch < CHANNELS; ch++)
372376
C[ch] += features[collected_id[j] * CHANNELS + ch] * alpha * T;
373377

378+
// Median depth:
379+
if (T > 0.5f && test_T < 0.5)
380+
D = collected_depth[j];
381+
374382
if(invdepth)
375383
expected_invdepth += (1 / depths[collected_id[j]]) * alpha * T;
376384

@@ -390,6 +398,7 @@ renderCUDA(
390398
n_contrib[pix_id] = last_contributor;
391399
for (int ch = 0; ch < CHANNELS; ch++)
392400
out_color[ch * H * W + pix_id] = C[ch] + T * bg_color[ch];
401+
out_depth[pix_id] = D;
393402

394403
if (invdepth)
395404
invdepth[pix_id] = expected_invdepth;// 1. / (expected_depth + T * 1e3);
@@ -409,7 +418,8 @@ void FORWARD::render(
409418
const float* bg_color,
410419
float* out_color,
411420
float* depths,
412-
float* depth)
421+
float* invdepth,
422+
float* out_depth)
413423
{
414424
renderCUDA<NUM_CHANNELS> << <grid, block >> > (
415425
ranges,
@@ -423,7 +433,8 @@ void FORWARD::render(
423433
bg_color,
424434
out_color,
425435
depths,
426-
depth);
436+
invdepth,
437+
out_depth);
427438
}
428439

429440
void FORWARD::preprocess(int P, int D, int M,

cuda_rasterizer/forward.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ namespace FORWARD
6262
const float* bg_color,
6363
float* out_color,
6464
float* depths,
65-
float* depth);
65+
float* invdepth,
66+
float* out_depth);
6667
}
6768

6869

cuda_rasterizer/rasterizer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ namespace CudaRasterizer
4949
const float tan_fovx, float tan_fovy,
5050
const bool prefiltered,
5151
float* out_color,
52-
float* depth,
52+
float* invdepth,
53+
float* out_depth,
5354
bool antialiasing,
5455
int* radii = nullptr,
5556
bool debug = false);

cuda_rasterizer/rasterizer_impl.cu

+4-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ int CudaRasterizer::Rasterizer::forward(
216216
const float tan_fovx, float tan_fovy,
217217
const bool prefiltered,
218218
float* out_color,
219-
float* depth,
219+
float* invdepth,
220+
float* out_depth,
220221
bool antialiasing,
221222
int* radii,
222223
bool debug)
@@ -335,7 +336,8 @@ int CudaRasterizer::Rasterizer::forward(
335336
background,
336337
out_color,
337338
geomState.depths,
338-
depth), debug)
339+
invdepth,
340+
out_depth), debug)
339341

340342
return num_rendered;
341343
}

diff_gaussian_rasterization/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ def forward(
8181
)
8282

8383
# Invoke C++/CUDA rasterizer
84-
num_rendered, color, radii, geomBuffer, binningBuffer, imgBuffer, invdepths = _C.rasterize_gaussians(*args)
84+
num_rendered, color, radii, geomBuffer, binningBuffer, imgBuffer, invdepths, depths = _C.rasterize_gaussians(*args)
8585

8686
# Keep relevant tensors for backward
8787
ctx.raster_settings = raster_settings
8888
ctx.num_rendered = num_rendered
8989
ctx.save_for_backward(colors_precomp, means3D, scales, rotations, cov3Ds_precomp, radii, sh, opacities, geomBuffer, binningBuffer, imgBuffer)
90-
return color, radii, invdepths
90+
return color, radii, invdepths, depths
9191

9292
@staticmethod
9393
def backward(ctx, grad_out_color, _, grad_out_depth):

rasterize_points.cu

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ std::function<char*(size_t N)> resizeFunctional(torch::Tensor& t) {
3232
return lambda;
3333
}
3434

35-
std::tuple<int, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>
35+
std::tuple<int, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>
3636
RasterizeGaussiansCUDA(
3737
const torch::Tensor& background,
3838
const torch::Tensor& means3D,
@@ -74,6 +74,7 @@ RasterizeGaussiansCUDA(
7474
out_invdepthptr = out_invdepth.data<float>();
7575

7676
torch::Tensor radii = torch::full({P}, 0, means3D.options().dtype(torch::kInt32));
77+
torch::Tensor out_depth = torch::full({1, H, W}, 0.0, float_opts);
7778

7879
torch::TensorOptions byte_opts = means3D.options().dtype(torch::kByte);
7980
torch::Tensor geomBuffer = torch::empty({0}, byte_opts);
@@ -115,11 +116,12 @@ RasterizeGaussiansCUDA(
115116
prefiltered,
116117
out_color.contiguous().data<float>(),
117118
out_invdepthptr,
119+
out_depth.contiguous().data<float>(),
118120
antialiasing,
119121
radii.contiguous().data<int>(),
120122
debug);
121123
}
122-
return std::make_tuple(rendered, out_color, radii, geomBuffer, binningBuffer, imgBuffer, out_invdepth);
124+
return std::make_tuple(rendered, out_color, radii, geomBuffer, binningBuffer, imgBuffer, out_invdepth, out_depth);
123125
}
124126

125127
std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>

rasterize_points.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <tuple>
1616
#include <string>
1717

18-
std::tuple<int, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>
18+
std::tuple<int, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>
1919
RasterizeGaussiansCUDA(
2020
const torch::Tensor& background,
2121
const torch::Tensor& means3D,

0 commit comments

Comments
 (0)