Blender V5.3
optix/device_impl.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2019-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0 */
5
6#pragma once
7
8#ifdef WITH_OPTIX
9
11# include "device/optix/util.h" // IWYU pragma: keep
12# include "kernel/osl/globals.h"
13
14# include "util/task.h"
15
17
18class BVHOptiX;
20
21/* List of OptiX program groups. */
22enum {
23 /* Ray generation */
24 PG_RGEN_INTERSECT_CLOSEST,
25 PG_RGEN_INTERSECT_SHADOW,
26 PG_RGEN_INTERSECT_SUBSURFACE,
27 PG_RGEN_INTERSECT_VOLUME_STACK,
28 PG_RGEN_INTERSECT_DEDICATED_LIGHT,
29 PG_RGEN_INTERSECT_MNEE,
30 PG_RGEN_SHADE_BACKGROUND,
31 PG_RGEN_SHADE_LIGHT_NEE,
32 PG_RGEN_SHADE_LIGHT_FORWARD,
33 PG_RGEN_SHADE_SURFACE,
34 PG_RGEN_SHADE_SURFACE_RAYTRACE,
35 PG_RGEN_SHADE_VOLUME,
36 PG_RGEN_SHADE_VOLUME_RAY_MARCHING,
37 PG_RGEN_SHADE_SHADOW,
38 PG_RGEN_SHADE_DEDICATED_LIGHT,
39 PG_RGEN_EVAL_DISPLACE,
40 PG_RGEN_EVAL_BACKGROUND,
41 PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY,
42 PG_RGEN_INIT_FROM_CAMERA,
43 PG_RGEN_EVAL_VOLUME_DENSITY,
44
45 /* Miss */
46 PG_MISS,
47
48 /* Hit */
49 PG_HITD, /* Default hit group. */
50 PG_HITS, /* __SHADOW_RECORD_ALL__ hit group. */
51 PG_HITL, /* __BVH_LOCAL__ hit group (only used for triangles). */
52 PG_HITV, /* __VOLUME__ hit group. */
53 PG_HITD_MOTION,
54 PG_HITS_MOTION,
55 PG_HITL_MOTION,
56 PG_HITV_MOTION,
57 PG_HITD_CURVE_LINEAR,
58 PG_HITS_CURVE_LINEAR,
59 PG_HITV_CURVE_LINEAR,
60 PG_HITL_CURVE_LINEAR,
61 PG_HITD_CURVE_LINEAR_MOTION,
62 PG_HITS_CURVE_LINEAR_MOTION,
63 PG_HITV_CURVE_LINEAR_MOTION,
64 PG_HITL_CURVE_LINEAR_MOTION,
65 PG_HITD_CURVE_RIBBON,
66 PG_HITS_CURVE_RIBBON,
67 PG_HITV_CURVE_RIBBON,
68 PG_HITL_CURVE_RIBBON,
69 PG_HITD_POINTCLOUD,
70 PG_HITS_POINTCLOUD,
71 PG_HITV_POINTCLOUD,
72 PG_HITL_POINTCLOUD,
73
74 /* Callable */
75 PG_CALL_SVM_AO,
76 PG_CALL_SVM_BEVEL,
77
78 NUM_PROGRAM_GROUPS
79};
80
81static const int MISS_PROGRAM_GROUP_OFFSET = PG_MISS;
82static const int NUM_MISS_PROGRAM_GROUPS = 1;
83static const int HIT_PROGAM_GROUP_OFFSET = PG_HITD;
84static const int NUM_HIT_PROGRAM_GROUPS = 24;
85static const int CALLABLE_PROGRAM_GROUPS_BASE = PG_CALL_SVM_AO;
86static const int NUM_CALLABLE_PROGRAM_GROUPS = 2;
87
88/* List of OptiX pipelines. */
89enum { PIP_SHADE, PIP_INTERSECT, NUM_PIPELINES };
90
91/* A single shader binding table entry. */
92struct SbtRecord {
93 char header[OPTIX_SBT_RECORD_HEADER_SIZE];
94};
95
96class OptiXDevice : public CUDADevice {
97 public:
98 OptixDeviceContext context = nullptr;
99
100 OptixModule optix_module = nullptr;
101 OptixModule mnee_module = nullptr;
102 OptixModule shader_raytrace_module = nullptr;
103 OptixModule builtin_modules[4] = {};
104 OptixPipeline pipelines[NUM_PIPELINES] = {};
105 OptixProgramGroup groups[NUM_PROGRAM_GROUPS] = {};
106 OptixPipelineCompileOptions pipeline_options = {};
107
108# ifdef WITH_OSL
109 OSLGlobals osl_globals;
110 vector<OptixModule> osl_modules;
111 vector<OptixProgramGroup> osl_groups;
112 OptixModule osl_camera_module = nullptr;
113 OptixModule osl_shadow_module = nullptr;
114 OptixModule osl_shadow_curve_module = nullptr;
115 OptixModule osl_volume_module = nullptr;
116 device_vector<uint8_t> osl_colorsystem;
117# endif
118
119 device_vector<SbtRecord> sbt_data;
120 device_only_memory<KernelParamsOptiX> launch_params;
121
122 private:
123 OptixTraversableHandle tlas_handle = 0;
124 vector<unique_ptr<device_only_memory<char>>> delayed_free_bvh_memory;
125 thread_mutex delayed_free_bvh_mutex;
126
127 public:
128 OptiXDevice(const DeviceInfo &info, Stats &stats, Profiler &profiler, bool headless);
129 ~OptiXDevice() override;
130
131 BVHLayoutMask get_bvh_layout_mask(uint64_t kernel_features) const override;
132
133 string compile_kernel_get_common_cflags(uint64_t kernel_features);
134
135 void create_optix_module(TaskPool &pool,
136 OptixModuleCompileOptions &module_options,
137 string &ptx_data,
138 OptixModule &module,
139 OptixResult &failure_reason);
140
141 bool load_kernels(uint64_t kernel_features) override;
142
143 bool load_osl_kernels() override;
144
145 bool build_optix_bvh(BVHOptiX *bvh,
146 OptixBuildOperation operation,
147 const OptixBuildInput &build_input,
148 uint16_t num_motion_steps);
149
150 void build_bvh(BVH *bvh, Progress &progress, bool refit) override;
151
152 void release_bvh(BVH *bvh) override;
153 void free_bvh_memory_delayed();
154
155 void const_copy_to(const char *name, void *host, const size_t size) override;
156
157 void update_launch_params(const size_t offset, void *data, const size_t data_size);
158
159 unique_ptr<DeviceQueue> gpu_queue_create() override;
160
161 OSLGlobals *get_cpu_osl_memory() override;
162};
163
165
166#endif /* WITH_OPTIX */
unsigned long long int uint64_t
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
#define CCL_NAMESPACE_END
ccl_device bool BVH_FUNCTION_FULL_NAME BVH(KernelGlobals kg, const ccl_private Ray *ray, ccl_private LocalIntersection *local_isect, const int local_object, ccl_private uint *lcg_state, const int max_hits)
Definition local.h:27
int context(const bContext *C, const char *member, bContextDataResult *result)
int BVHLayoutMask
Definition params.h:50
std::mutex thread_mutex
Definition thread.h:27