Blender  V3.3
optix/device_impl.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2019, NVIDIA Corporation.
3  * Copyright 2019-2022 Blender Foundation. */
4 
5 #pragma once
6 
7 #ifdef WITH_OPTIX
8 
10 # include "device/optix/queue.h"
11 # include "device/optix/util.h"
12 # include "kernel/types.h"
13 # include "util/unique_ptr.h"
14 
16 
17 class BVHOptiX;
18 struct KernelParamsOptiX;
19 
20 /* List of OptiX program groups. */
21 enum {
22  PG_RGEN_INTERSECT_CLOSEST,
23  PG_RGEN_INTERSECT_SHADOW,
24  PG_RGEN_INTERSECT_SUBSURFACE,
25  PG_RGEN_INTERSECT_VOLUME_STACK,
26  PG_RGEN_SHADE_SURFACE_RAYTRACE,
27  PG_RGEN_SHADE_SURFACE_MNEE,
28  PG_MISS,
29  PG_HITD, /* Default hit group. */
30  PG_HITS, /* __SHADOW_RECORD_ALL__ hit group. */
31  PG_HITL, /* __BVH_LOCAL__ hit group (only used for triangles). */
32  PG_HITV, /* __VOLUME__ hit group. */
33  PG_HITD_MOTION,
34  PG_HITS_MOTION,
35  PG_HITD_POINTCLOUD,
36  PG_HITS_POINTCLOUD,
37  PG_CALL_SVM_AO,
38  PG_CALL_SVM_BEVEL,
39  NUM_PROGRAM_GROUPS
40 };
41 
42 static const int MISS_PROGRAM_GROUP_OFFSET = PG_MISS;
43 static const int NUM_MIS_PROGRAM_GROUPS = 1;
44 static const int HIT_PROGAM_GROUP_OFFSET = PG_HITD;
45 static const int NUM_HIT_PROGRAM_GROUPS = 8;
46 static const int CALLABLE_PROGRAM_GROUPS_BASE = PG_CALL_SVM_AO;
47 static const int NUM_CALLABLE_PROGRAM_GROUPS = 2;
48 
49 /* List of OptiX pipelines. */
50 enum { PIP_SHADE_RAYTRACE, PIP_SHADE_MNEE, PIP_INTERSECT, NUM_PIPELINES };
51 
52 /* A single shader binding table entry. */
53 struct SbtRecord {
54  char header[OPTIX_SBT_RECORD_HEADER_SIZE];
55 };
56 
57 class OptiXDevice : public CUDADevice {
58  public:
59  OptixDeviceContext context = NULL;
60 
61  OptixModule optix_module = NULL; /* All necessary OptiX kernels are in one module. */
62  OptixModule builtin_modules[2] = {};
63  OptixPipeline pipelines[NUM_PIPELINES] = {};
64 
65  bool motion_blur = false;
66  device_vector<SbtRecord> sbt_data;
68  OptixTraversableHandle tlas_handle = 0;
69 
70  vector<unique_ptr<device_only_memory<char>>> delayed_free_bvh_memory;
71  thread_mutex delayed_free_bvh_mutex;
72 
73  class Denoiser {
74  public:
75  explicit Denoiser(OptiXDevice *device);
76 
77  OptiXDevice *device;
78  OptiXDeviceQueue queue;
79 
80  OptixDenoiser optix_denoiser = nullptr;
81 
82  /* Configuration size, as provided to `optixDenoiserSetup`.
83  * If the `optixDenoiserSetup()` was never used on the current `optix_denoiser` the
84  * `is_configured` will be false. */
85  bool is_configured = false;
86  int2 configured_size = make_int2(0, 0);
87 
88  /* OptiX denoiser state and scratch buffers, stored in a single memory buffer.
89  * The memory layout goes as following: [denoiser state][scratch buffer]. */
91  OptixDenoiserSizes sizes = {};
92 
93  bool use_pass_albedo = false;
94  bool use_pass_normal = false;
95  bool use_pass_flow = false;
96  };
97  Denoiser denoiser_;
98 
99  public:
100  OptiXDevice(const DeviceInfo &info, Stats &stats, Profiler &profiler);
101  ~OptiXDevice();
102 
103  private:
104  BVHLayoutMask get_bvh_layout_mask() const override;
105 
106  string compile_kernel_get_common_cflags(const uint kernel_features) override;
107 
108  bool load_kernels(const uint kernel_features) override;
109 
110  bool build_optix_bvh(BVHOptiX *bvh,
111  OptixBuildOperation operation,
112  const OptixBuildInput &build_input,
113  uint16_t num_motion_steps);
114 
115  void build_bvh(BVH *bvh, Progress &progress, bool refit) override;
116 
117  void release_optix_bvh(BVH *bvh) override;
118  void free_bvh_memory_delayed();
119 
120  void const_copy_to(const char *name, void *host, size_t size) override;
121 
122  void update_launch_params(size_t offset, void *data, size_t data_size);
123 
124  virtual unique_ptr<DeviceQueue> gpu_queue_create() override;
125 
126  /* --------------------------------------------------------------------
127  * Denoising.
128  */
129 
130  class DenoiseContext;
131  class DenoisePass;
132 
133  virtual bool denoise_buffer(const DeviceDenoiseTask &task) override;
134  virtual DeviceQueue *get_denoise_queue() override;
135 
136  /* Read guiding passes from the render buffers, preprocess them in a way which is expected by
137  * OptiX and store in the guiding passes memory within the given context.
138  *
139  * Pre=-processing of the guiding passes is to only happen once per context lifetime. DO not
140  * preprocess them for every pass which is being denoised. */
141  bool denoise_filter_guiding_preprocess(DenoiseContext &context);
142 
143  /* Set fake albedo pixels in the albedo guiding pass storage.
144  * After this point only passes which do not need albedo for denoising can be processed. */
145  bool denoise_filter_guiding_set_fake_albedo(DenoiseContext &context);
146 
147  void denoise_pass(DenoiseContext &context, PassType pass_type);
148 
149  /* Read input color pass from the render buffer into the memory which corresponds to the noisy
150  * input within the given context. Pixels are scaled to the number of samples, but are not
151  * preprocessed yet. */
152  void denoise_color_read(DenoiseContext &context, const DenoisePass &pass);
153 
154  /* Run corresponding filter kernels, preparing data for the denoiser or copying data from the
155  * denoiser result to the render buffer. */
156  bool denoise_filter_color_preprocess(DenoiseContext &context, const DenoisePass &pass);
157  bool denoise_filter_color_postprocess(DenoiseContext &context, const DenoisePass &pass);
158 
159  /* Make sure the OptiX denoiser is created and configured. */
160  bool denoise_ensure(DenoiseContext &context);
161 
162  /* Create OptiX denoiser descriptor if needed.
163  * Will do nothing if the current OptiX descriptor is usable for the given parameters.
164  * If the OptiX denoiser descriptor did re-allocate here it is left unconfigured. */
165  bool denoise_create_if_needed(DenoiseContext &context);
166 
167  /* Configure existing OptiX denoiser descriptor for the use for the given task. */
168  bool denoise_configure_if_needed(DenoiseContext &context);
169 
170  /* Run configured denoiser. */
171  bool denoise_run(DenoiseContext &context, const DenoisePass &pass);
172 };
173 
175 
176 #endif /* WITH_OPTIX */
unsigned int uint
Definition: BLI_sys_types.h:67
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
Definition: bvh/bvh.h:63
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
SyclQueue * queue
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
const int state
PassType
Definition: kernel/types.h:334
#define make_int2(x, y)
Definition: metal/compat.h:206
struct blender::compositor::@179::@181 task
int BVHLayoutMask
Definition: params.h:47
unsigned short uint16_t
Definition: stdint.h:79
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: thread.h:27