Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/kernels/common/device.h
9905 views
1
// Copyright 2009-2021 Intel Corporation
2
// SPDX-License-Identifier: Apache-2.0
3
4
#pragma once
5
6
#include "default.h"
7
#include "state.h"
8
#include "accel.h"
9
10
namespace embree
11
{
12
class BVH4Factory;
13
class BVH8Factory;
14
struct TaskArena;
15
16
class Device : public State, public MemoryMonitorInterface
17
{
18
ALIGNED_CLASS_(16);
19
20
public:
21
22
/*! allocator that performs unified shared memory allocations */
23
template<typename T, size_t alignment>
24
struct allocator
25
{
26
typedef T value_type;
27
typedef T* pointer;
28
typedef const T* const_pointer;
29
typedef T& reference;
30
typedef const T& const_reference;
31
typedef std::size_t size_type;
32
typedef std::ptrdiff_t difference_type;
33
34
allocator() {}
35
36
allocator(Device* device)
37
: device(device) {}
38
39
__forceinline pointer allocate( size_type n ) {
40
assert(device);
41
return (pointer) device->malloc(n*sizeof(T),alignment,EmbreeMemoryType::MALLOC);
42
}
43
44
__forceinline void deallocate( pointer p, size_type n ) {
45
if (device) device->free(p);
46
}
47
48
__forceinline void construct( pointer p, const_reference val ) {
49
new (p) T(val);
50
}
51
52
__forceinline void destroy( pointer p ) {
53
p->~T();
54
}
55
56
Device* device = nullptr;
57
};
58
59
/*! vector class that performs aligned allocations from Device object */
60
template<typename T>
61
using vector = vector_t<T,allocator<T,std::alignment_of<T>::value>>;
62
63
template<typename T, size_t alignment>
64
using avector = vector_t<T,allocator<T,alignment>>;
65
66
public:
67
68
/*! Device construction */
69
Device (const char* cfg);
70
71
/*! Device destruction */
72
virtual ~Device ();
73
74
/*! prints info about the device */
75
void print();
76
77
/*! sets the error code */
78
void setDeviceErrorCode(RTCError error, std::string const& msg = "");
79
80
/*! returns and clears the error code */
81
RTCError getDeviceErrorCode();
82
83
/*! Returns the string representation for the error code. For example, for RTC_ERROR_UNKNOWN the string "RTC_ERROR_UNKNOWN" will be returned. */
84
static char* getDeviceErrorString();
85
86
/*! returns the last error message */
87
const char* getDeviceLastErrorMessage();
88
89
/*! sets the error code */
90
static void setThreadErrorCode(RTCError error, std::string const& msg = "");
91
92
/*! returns and clears the error code */
93
static RTCError getThreadErrorCode();
94
95
96
/*! returns the last error message */
97
static const char* getThreadLastErrorMessage();
98
99
/*! processes error codes, do not call directly */
100
static void process_error(Device* device, RTCError error, const char* str);
101
102
/*! invokes the memory monitor callback */
103
void memoryMonitor(ssize_t bytes, bool post);
104
105
/*! sets the size of the software cache. */
106
void setCacheSize(size_t bytes);
107
108
/*! sets a property */
109
void setProperty(const RTCDeviceProperty prop, ssize_t val);
110
111
/*! gets a property */
112
ssize_t getProperty(const RTCDeviceProperty prop);
113
114
/*! enter device by setting up some global state */
115
virtual void enter() {}
116
117
/*! leave device by setting up some global state */
118
virtual void leave() {}
119
120
/*! buffer allocation - using USM shared */
121
virtual void* malloc(size_t size, size_t align);
122
123
/*! buffer allocation */
124
virtual void* malloc(size_t size, size_t align, EmbreeMemoryType type);
125
126
/*! buffer deallocation */
127
virtual void free(void* ptr);
128
129
/*! returns true if device is of type DeviceGPU */
130
virtual bool is_gpu() const { return false; }
131
132
/*! returns true if device and host have shared memory system (e.g., integrated GPU) */
133
virtual bool has_unified_memory() const { return true; }
134
135
virtual EmbreeMemoryType get_memory_type(void* ptr) const { return EmbreeMemoryType::MALLOC; }
136
137
private:
138
139
/*! initializes the tasking system */
140
void initTaskingSystem(size_t numThreads);
141
142
/*! shuts down the tasking system */
143
void exitTaskingSystem();
144
145
std::unique_ptr<TaskArena> arena;
146
147
public:
148
149
// use tasking system arena to execute func
150
void execute(bool join, const std::function<void()>& func);
151
152
/*! some variables that can be set via rtcSetParameter1i for debugging purposes */
153
public:
154
static ssize_t debug_int0;
155
static ssize_t debug_int1;
156
static ssize_t debug_int2;
157
static ssize_t debug_int3;
158
159
public:
160
std::unique_ptr<BVH4Factory> bvh4_factory;
161
#if defined(EMBREE_TARGET_SIMD8)
162
std::unique_ptr<BVH8Factory> bvh8_factory;
163
#endif
164
165
private:
166
static const std::vector<std::string> error_strings;
167
168
public:
169
static const char* getErrorString(RTCError error);
170
171
};
172
173
#if defined(EMBREE_SYCL_SUPPORT)
174
175
class DeviceGPU : public Device
176
{
177
public:
178
179
DeviceGPU(sycl::context sycl_context, const char* cfg);
180
~DeviceGPU();
181
182
virtual void enter() override;
183
virtual void leave() override;
184
virtual void* malloc(size_t size, size_t align) override;
185
virtual void* malloc(size_t size, size_t align, EmbreeMemoryType type) override;
186
virtual void free(void* ptr) override;
187
188
/* set SYCL device */
189
void setSYCLDevice(const sycl::device sycl_device);
190
191
/*! returns true if device is of type DeviceGPU */
192
virtual bool is_gpu() const override { return true; }
193
194
/*! returns true if device and host have shared memory system (e.g., integrated GPU) */
195
virtual bool has_unified_memory() const override;
196
197
virtual EmbreeMemoryType get_memory_type(void* ptr) const override {
198
switch(sycl::get_pointer_type(ptr, gpu_context)) {
199
case sycl::usm::alloc::host: return EmbreeMemoryType::USM_HOST;
200
case sycl::usm::alloc::device: return EmbreeMemoryType::USM_DEVICE;
201
case sycl::usm::alloc::shared: return EmbreeMemoryType::USM_SHARED;
202
default: return EmbreeMemoryType::MALLOC;
203
}
204
}
205
206
private:
207
sycl::context gpu_context;
208
sycl::device gpu_device;
209
210
unsigned int gpu_maxWorkGroupSize;
211
unsigned int gpu_maxComputeUnits;
212
213
public:
214
void* dispatchGlobalsPtr = nullptr;
215
216
public:
217
inline sycl::device &getGPUDevice() { return gpu_device; }
218
inline sycl::context &getGPUContext() { return gpu_context; }
219
220
inline unsigned int getGPUMaxWorkGroupSize() { return gpu_maxWorkGroupSize; }
221
222
void init_rthw_level_zero();
223
void init_rthw_opencl();
224
};
225
226
#endif
227
228
struct DeviceEnterLeave
229
{
230
DeviceEnterLeave (RTCDevice hdevice);
231
DeviceEnterLeave (RTCScene hscene);
232
DeviceEnterLeave (RTCGeometry hgeometry);
233
DeviceEnterLeave (RTCBuffer hbuffer);
234
~DeviceEnterLeave();
235
private:
236
Device* device;
237
};
238
}
239
240