Blender V5.0
hiprt/device_impl.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2023 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#ifdef WITH_HIPRT
8
10# include "device/hip/kernel.h"
11# include "device/hip/queue.h"
12# include "device/hiprt/queue.h"
13
14# ifdef WITH_HIP_DYNLOAD
15# include <hiprtew.h>
16# else
17# include <hiprt/hiprt_types.h>
18# endif
19
21
22class Mesh;
23class Hair;
24class PointCloud;
25class Geometry;
26class Object;
27class BVHHIPRT;
28
29class HIPRTDevice : public HIPDevice {
30
31 public:
32 BVHLayoutMask get_bvh_layout_mask(const uint kernel_features) const override;
33
34 HIPRTDevice(const DeviceInfo &info, Stats &stats, Profiler &profiler, bool headless);
35
36 ~HIPRTDevice() override;
37 unique_ptr<DeviceQueue> gpu_queue_create() override;
38 string compile_kernel_get_common_cflags(const uint kernel_features) override;
39 string compile_kernel(const uint kernel_features,
40 const char *name,
41 const char *base = "hiprt") override;
42
43 bool load_kernels(const uint kernel_features) override;
44
45 void const_copy_to(const char *name, void *host, const size_t size) override;
46
47 void build_bvh(BVH *bvh, Progress &progress, bool refit) override;
48
49 void release_bvh(BVH *bvh) override;
50
51 hiprtContext get_hiprt_context()
52 {
53 return hiprt_context;
54 }
55
56 hiprtGlobalStackBuffer global_stack_buffer;
57
58 protected:
59 enum Filter_Function { Closest = 0, Shadows, Local, Volume, Max_Intersect_Filter_Function };
60 enum Primitive_Type { Triangle = 0, Curve, Motion_Triangle, Point, Max_Primitive_Type };
61
62 hiprtGeometryBuildInput prepare_triangle_blas(BVHHIPRT *bvh, Mesh *mesh);
63 hiprtGeometryBuildInput prepare_curve_blas(BVHHIPRT *bvh, Hair *hair);
64 hiprtGeometryBuildInput prepare_point_blas(BVHHIPRT *bvh, PointCloud *pointcloud);
65 void build_blas(BVHHIPRT *bvh, Geometry *geom, hiprtBuildOptions options);
66 hiprtScene build_tlas(BVHHIPRT *bvh,
67 const vector<Object *> &objects,
68 hiprtBuildOptions options,
69 bool refit);
70 void free_bvh_memory_delayed();
71 hiprtContext hiprt_context;
72 hiprtScene scene;
73 hiprtFuncTable functions_table;
74
75 thread_mutex hiprt_mutex;
76 size_t scratch_buffer_size;
77 device_vector<char> scratch_buffer;
78
79 /* This vector tracks the hiprt_geom members of BVHRT so that device memory
80 * can be managed/released in HIPRTDevice.
81 * Even if synchronization occurs before memory release, a GPU job may still
82 * launch between synchronization and release, potentially causing the GPU
83 * to access unmapped memory. */
84 vector<hiprtGeometry> stale_bvh;
85
86 /* Is this scene using motion blur? Note there might exist motion data even if
87 * motion blur is disabled, for render passes. */
88 bool use_motion_blur = false;
89
90 /* The following vectors are to transfer scene information available on the host to the GPU
91 * visibility, instance_transform_matrix, transform_headers, and hiprt_blas_ptr are passed to
92 * hiprt to build bvh the rest are directly used in traversal functions/intersection kernels and
93 * are defined on the GPU side as members of KernelParamsHIPRT struct the host memory is copied
94 * to GPU through const_copy_to() function. */
95
96 /* Originally, visibility was only passed to HIP RT but after a bug report it was noted it was
97 * required for custom primitives (i.e., motion triangles). This buffer, however, has visibility
98 * per object not per primitive so the same buffer as the one that is passed to HIP RT can be
99 * used. */
100 device_vector<uint32_t> prim_visibility;
101
102 /* instance_transform_matrix passes transform matrix of instances converted from Cycles Transform
103 * format to instanceFrames member of hiprtSceneBuildInput. */
104 device_vector<hiprtFrameMatrix> instance_transform_matrix;
105 /* Movement over a time interval for motion blur is captured through multiple transform matrices.
106 * In this case transform matrix of an instance cannot be directly retrieved by looking up
107 * instance_transform_matrix give the instance id. transform_headers maps the instance id to the
108 * appropriate index to retrieve instance transform matrix (frameIndex member of
109 * hiprtTransformHeader). transform_headers also has the information on how many transform
110 * matrices are associated with an instance (frameCount member of hiprtTransformHeader)
111 * transform_headers is passed to hiprt through instanceTransformHeaders member of
112 * hiprtSceneBuildInput. */
113 device_vector<hiprtTransformHeader> transform_headers;
114
115 /* Instance/object ids are not explicitly passed to hiprt.
116 * HIP RT assigns the ids based on the order blas pointers are passed to it (through
117 * instanceGeometries member of hiprtSceneBuildInput). If blas is absent for a particular
118 * geometry (e.g. a plane), HIP RT removes that entry and in scenes with objects with no blas,
119 * the instance id that hiprt returns for a hit point will not necessarily match the instance id
120 * of the application. user_instance_id provides a map for retrieving original instance id from
121 * what HIP RT returns as instance id. hiprt_blas_ptr is the list of all the valid blas pointers.
122 * blas_ptr has all the valid pointers and null pointers and blas for any geometry can be
123 * directly retrieved from this array (used in subsurface scattering). */
124 device_vector<int> user_instance_id;
125 device_vector<hiprtInstance> hiprt_blas_ptr;
126 device_vector<uint64_t> blas_ptr;
127
128 /* custom_prim_info stores custom information for custom primitives for all the primitives in a
129 * scene. Primitive id that HIP RT returns is local to the geometry that was hit.
130 * custom_prim_info_offset returns the offset required to add to the primitive id to retrieve
131 * primitive info from custom_prim_info. */
132 device_vector<int2> custom_prim_info;
133 device_vector<int2> custom_prim_info_offset;
134
135 /* prims_time stores primitive time for geometries with motion blur.
136 * prim_time_offset returns the offset to add to primitive id to retrieve primitive time. */
137 device_vector<float2> prims_time;
138 device_vector<int> prim_time_offset;
139};
141
142#endif
unsigned int uint
struct Curve Curve
struct Mesh Mesh
struct PointCloud PointCloud
struct Volume Volume
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
Definition hair.h:13
CCL_NAMESPACE_BEGIN struct Options options
#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:28
int BVHLayoutMask
Definition params.h:50
const char * name
std::mutex thread_mutex
Definition thread.h:27