34#include "VulkanComputeDevice.h"
35#include "VulkanComputeBuffer.h"
36#include "VulkanComputePipeline.h"
37#include <NDEVR/Buffer.h>
38#include <NDEVR/Pointer.h>
39#include <NDEVR/String.h>
40#include <NDEVR/File.h>
42#if !defined(__EMSCRIPTEN__) && NDEVR_SUPPORTS_THREADING
78 static_assert(
sizeof(
PatchMatchCamera) == 128,
"PatchMatchCamera must be 128 bytes for std430 array stride");
192 bool allocated =
false;
195 class VULKAN_COMPUTE_API VulkanPatchMatch
199 static bool IsAvailable(
LogPtr log);
205 bool isInitialized()
const {
return m_initialized; }
224 bool estimateDepthMap(
226 const Buffer<uint04>& neighbor_indices,
227 const Buffer<PatchMatchCamera>& cameras,
228 const Buffer<Buffer<float>>& images,
229 const Buffer<int>& image_widths,
230 const Buffer<int>& image_heights,
233 uint04 n_estimation_iters,
235 float threshold_keep_cost,
236 const Buffer<float>& initial_planes,
237 DepthEstimateResult& result,
238 const Buffer<float>& geometric_prior = Buffer<float>(),
239 PatchMatchGpuCache* cache =
nullptr,
240 float confidence_skip = 0.0f);
253 static void FilterDepthMap(DepthEstimateResult& result,
254 uint04 min_component_size = 16u,
255 float depth_rel_tolerance = 0.02f);
271 static bool WriteDmap(File file_path,
272 const String& image_file_name,
273 const Buffer<uint32_t>& view_ids,
274 uint32_t image_width, uint32_t image_height,
275 const double K[9],
const double R[9],
const double C[3],
276 float depth_min,
float depth_max,
277 float threshold_keep_cost,
278 const DepthEstimateResult& result);
290 bool filterGeometricConsistency(
291 const PatchMatchCamera& ref_camera,
292 const Buffer<PatchMatchCamera>& neighbor_cameras,
293 DepthEstimateResult& ref_result,
294 const Buffer<DepthEstimateResult>& neighbor_results,
295 float depth_diff_threshold,
296 float normal_diff_threshold,
297 uint04 min_consistent_views);
304 void filterDepthMapGpu(DepthEstimateResult& result,
305 uint04 min_consistent = 2u,
306 float depth_rel_tolerance = 0.02f);
321 bool refineDepthsInPlace(
322 const Buffer<PatchMatchCamera>& cameras,
323 const Buffer<Buffer<float>>& images,
324 const Buffer<int>& image_widths,
325 const Buffer<int>& image_heights,
326 float depth_min,
float depth_max,
327 uint04 n_refinement_iters,
328 float threshold_keep_cost,
329 DepthEstimateResult& result,
330 PatchMatchGpuCache* cache =
nullptr);
335 bool refineGeometric(
337 const Buffer<uint04>& neighbor_indices,
338 const Buffer<PatchMatchCamera>& cameras,
339 const Buffer<Buffer<float>>& images,
340 const Buffer<int>& image_widths,
341 const Buffer<int>& image_heights,
342 float depth_min,
float depth_max,
343 uint04 n_refinement_iters,
345 float threshold_keep_cost,
346 DepthEstimateResult& result,
347 const Buffer<PatchMatchCamera>& neighbor_cameras_for_geo,
348 const Buffer<DepthEstimateResult>& neighbor_depth_maps,
349 float depth_diff_threshold,
350 float normal_diff_threshold,
351 uint04 min_consistent_views,
352 PatchMatchGpuCache* cache =
nullptr);
359 void createPipelines();
362 ConstPointer<VulkanComputeDevice> m_compute_device;
365 DynamicPointer<VulkanComputePipeline> m_initialize_pipeline;
366 DynamicPointer<VulkanComputePipeline> m_propagate_pipeline;
367 DynamicPointer<VulkanComputePipeline> m_filter_pipeline;
368 DynamicPointer<VulkanComputePipeline> m_refine_pipeline;
369 DynamicPointer<VulkanComputePipeline> m_geo_consistency_pipeline;
370 DynamicPointer<VulkanComputePipeline> m_depth_consistency_pipeline;
371 DynamicPointer<VulkanComputePipeline> m_median_pipeline;
372 DynamicPointer<VulkanComputePipeline> m_sgm_pipeline;
375 DynamicPointer<VulkanShader> m_initialize_shader;
376 DynamicPointer<VulkanShader> m_propagate_shader;
377 DynamicPointer<VulkanShader> m_filter_shader;
378 DynamicPointer<VulkanShader> m_refine_shader;
379 DynamicPointer<VulkanShader> m_geo_consistency_shader;
380 DynamicPointer<VulkanShader> m_depth_consistency_shader;
381 DynamicPointer<VulkanShader> m_median_shader;
382 DynamicPointer<VulkanShader> m_sgm_shader;
384 bool m_initialized =
false;
389 void setLog(LogPtr log) { m_log = log; }
398 class VULKAN_COMPUTE_API VulkanPatchMatch
401 static bool IsAvailable() {
return false; }
The equivelent of std::vector but with a bit more control.
Provides a modifiable pointer that has shared ownership of a dynamically allocated object.
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Push constants for depth consistency shader.
float _pad3
Padding for std430 alignment.
int32_t width
Image width in pixels.
int32_t height
Image height in pixels.
float _pad1
Padding for std430 alignment.
int32_t _pad0
Padding for std430 alignment.
int32_t _pad6
Padding for std430 alignment.
float fDepthRelTolerance
Relative depth tolerance for neighbor consistency.
int32_t nMinConsistent
Minimum consistent 8-neighbors to keep a pixel.
float _pad2
Padding for std430 alignment.
int32_t _pad4
Padding for std430 alignment.
int32_t _pad5
Padding for std430 alignment.
int32_t yOffset
Row offset for tiled dispatches.
Result from a single depth estimation pass.
Buffer< float > depth_map
W*H depth values.
int width
Image width in pixels.
Buffer< float > normal_map
W*H*3 normal components.
int height
Image height in pixels.
Buffer< float > confidence_map
W*H confidence values.
Push constants for geometric consistency shader.
int32_t nMinConsistent
Minimum consistent neighbors required to keep a pixel.
float fNormalDiffThreshold
Cosine angle threshold for normal consistency.
int32_t height
Image height in pixels.
float fDepthDiffThreshold
Relative depth tolerance for consistency.
int32_t yOffset
Row offset for tiled dispatches.
int32_t _pad0
Padding for std430 alignment.
int32_t width
Image width in pixels.
int32_t nNumNeighbors
Number of neighbor depth maps to check.
GPU-accelerated PatchMatch depth estimation using Vulkan compute shaders.
float R[12]
3x3 rotation (column-major, 3 columns × [3 floats + 1 pad])
int32_t camHeight
Image height.
uint32_t _pad0
Padding to match GLSL std430 array stride (128 bytes).
uint32_t pixelOffset
Offset into packed image SSBO.
float K[12]
3x3 intrinsic (column-major, 3 columns × [3 floats + 1 pad])
float C[3]
Camera center (vec3).
int32_t camWidth
Image width (packs into vec3 C's trailing 4 bytes).
uint32_t _pad1
(std430 rounds struct size to 16-byte alignment for arrays)
GPU buffer cache for reuse across multiple estimateDepthMap calls.
Push constant layout (must match GLSL layout exactly).
int32_t yOffset
Row offset for tiled dispatches.
int32_t nInitTopK
Number of top views to select during initialization.
float fThresholdKeepCost
Cost threshold for filtering bad matches.
int32_t nNumViews
Number of neighbor views used for matching.
int32_t iteration
Current PatchMatch iteration index.
float fConfidenceSkipThreshold
Skip pixels with cost below this (0 = disabled).
int32_t width
Image width in pixels.
int32_t _pcPad1
Padding for std430 alignment.
int32_t isRedPass
Whether this is the red (1) or black (0) checkerboard pass.
int32_t nEstimationIters
Total number of PatchMatch iterations to perform.
int32_t bLowResProcessed
Whether a low-resolution prior has been applied.
float fDepthMin
Minimum depth bound.
float fDepthMax
Maximum depth bound.
int32_t _pcPad0
Padding for std430 alignment.
int32_t _pcPad2
Padding for std430 alignment.
int32_t height
Image height in pixels.
Push constants for SGM directional cost aggregation shader.
int32_t maxRange
Max scanline walk distance (default 64).
float threshSmall
Relative depth diff threshold for small step (default 0.01).
float P2
Large depth step penalty (default 0.08).
float sgmWeight
Blend weight for aggregated cost (default 0.3).
float decay
Per-step penalty decay (default 0.92).
float P1
Small depth step penalty (default 0.02).