1 module radeonrays.radeonrays;
2 
3 import core.stdc.stdint;
4 
5 import radeonrays.math.float3;
6 import radeonrays.math.matrix;
7 import radeonrays.math.quaternion;
8 
9 enum RADEONRAYS_API_VERSION = 2.0;
10 
11 extern(C++,"RadeonRays")
12 {
13 	//struct Intersection;
14 	
15 	/// Represents a device, which can be used by API for intersection purposes.
16 	/// API is distributing the work across multiple devices itsels, so
17 	/// this structure is only used to query devices configuration and
18 	/// limit the number of devices available for the API.
19 	struct DeviceInfo
20 	{
21 		// Device type
22 		enum Type
23 		{
24 			kCpu,
25 			kGpu,
26 			kAccelerator
27 		}
28 		
29 		enum Platform
30 		{
31 			kOpenCL = 0x1,
32 			kVulkan = 0x2,
33 			kEmbree = 0x4,
34 			
35 			kAny = 0xFF
36 		}
37 		
38 		// Device name
39 		const char* name;
40 		// Device vendor
41 		const char* vendor;
42 		// Device type
43 		Type type;
44 		Platform platform;
45 	}
46 	
47 	// Forward declaration of entities
48 	//typedef int Id;
49 	alias int Id;
50 	const __gshared Id kNullId = -1;
51 	
52 	// Shape interface to repesent intersectable entities
53 	// The shape is assigned a particular ID which
54 	// is put by an intersection engine into Intersection structure
55 	abstract class Shape
56 	{
57 	public:
58 		~this(){}
59 	
60 		// World space transform
61 		void SetTransform(const ref matrix m, const ref matrix minv);
62 		void GetTransform(ref matrix m, ref matrix minv);
63 
64 		// Motion blur
65 		void SetLinearVelocity(const ref float3 v);
66 		float3 GetLinearVelocity();
67 
68 		void SetAngularVelocity(const ref quaternion q);
69 		quaternion GetAngularVelocity();
70 		
71 		// ID of a shape
72 		void SetId(Id id);
73 		Id GetId();
74 		
75 		// Geometry mask to mask out intersections
76 		void SetMask(int mask);
77 		int  GetMask();
78 	}
79 
80 	// Buffer represents a chunk of memory hosted inside the API
81 	// (or extrenally in case of interop)
82 	abstract class Buffer
83 	{
84 	public:
85 		~this(){}
86 	}
87 	
88 	// Synchronization object returned by some API routines.
89 	abstract class Event
90 	{
91 	public:
92 		~this(){}
93 		// Indicates whether the related action has been completed
94 		bool Complete() const;
95 		// Blocks execution until the event is completed
96 		void Wait();
97 	}
98 	
99 	// Exception class
100 	abstract class RRException
101 	{
102 	public:
103 		~this(){}
104 		const char* what();
105 	}
106 	
107 	// must match Intersection struct on the GPU side exactly!
108 	struct Intersection
109 	{
110 		// Shape ID
111 		Id shapeid = kNullId;
112 		// Primitve ID
113 		Id primid = kNullId;
114 		
115 		int padding0;
116 		int padding1;
117 		
118 		// UV parametrization
119 		float4 uvwt;
120 	}
121 	
122 	enum MapType
123 	{
124 		kMapRead = 0x1,
125 		kMapWrite = 0x2
126 	}
127 
128 	// IntersectionApi is designed to provide fast means for ray-scene intersection
129 	// for AMD architectures. It effectively absracts underlying AMD hardware and
130 	// software stack and allows user to issue low-latency batched ray queries.
131 	// The API has 2 major workflows:
132 	//    - Fast path: the data is expected to be in host memory as well as the returned result is put there
133 	//    - Complete path: the data can be put into remote memory allocated with API and can be accessed.
134 	//      by the app directly in remote memory space.
135 	//
136 	abstract class IntersectionApi
137 	{
138 	public:
139 
140 		/******************************************
141         Backend management
142         *******************************************/
143 		// By default RadeonRays will any platform with potential GPU accelerations,
144 		// if you prefer to specify which platform call SetPlatform before 
145 		// GetDeviceInfo/GetDeviceCount for each specific platform
146 		// By default will choose OpenCL if available, and if not Vulkan
147 		// Note: this may be sub optimal in some case. to avoid enum all devices
148 		// across all platforms explicitly before deciding on platform and 
149 		// device(s) to use
150 		final static void SetPlatform(const DeviceInfo.Platform platform);
151 		
152 
153 		/******************************************
154         Device management
155         ******************************************/
156 		// Use this part of API to query for available devices and
157 		// limit the set of devices which API is going to use
158 		// Get the number of devices available in the system
159 		final static uint32_t GetDeviceCount();
160 		// Get the information for the specified device
161 		final static void GetDeviceInfo(uint32_t devidx, ref DeviceInfo devinfo);
162 		
163 		/******************************************
164         API lifetime management
165         ******************************************/
166 		final static IntersectionApi Create(uint32_t devidx);
167 		
168 		// Deallocation
169 		final static void Delete(IntersectionApi api);
170 
171 		/******************************************
172         Geometry manipulation
173         ******************************************/
174 		// Fast path functions to create entities from host memory
175 		
176 		// The mesh might be mixed quad\triangle mesh which is determined
177 		// by numfacevertices array containing numfaces entries describing
178 		// the number of vertices for current face (3 or 4)
179 		// The call is blocking, so the returned value is ready upon return.
180 		Shape CreateMesh(
181 			// Position data
182 			const float * vertices, int vnum, int vstride,
183 			// Index data for vertices
184 			const int * indices, 
185 			int istride,
186 			// Numbers of vertices per face
187 			const int * numfacevertices,
188 			// Number of faces
189 			int  numfaces
190 			);
191 		
192 		// Create an instance of a shape with its own transform (set via Shape interface).
193 		// The call is blocking, so the returned value is ready upon return.
194 		Shape CreateInstance(const Shape shape) const;
195 		// Delete the shape (to simplify DLL boundary crossing
196 		void DeleteShape(const Shape shape);
197 		// Attach shape to participate in intersection process
198 		void AttachShape(const Shape shape);
199 		// Detach shape, i.e. it is not going to be considered part of the scene anymore
200 		void DetachShape(const Shape shape);
201 		// Detach all objects
202 		void DetachAll();
203 		// Commit all geometry creations/changes
204 		void Commit();
205 		//Sets the shape id allocator to its default value (1)
206 		void ResetIdCounter();
207 		//Returns true if no shapes are in the world
208 		bool IsWorldEmpty();
209 		
210 		/******************************************
211         Memory management
212         ******************************************/
213 		// Create a buffer to use the most efficient acceleration possible
214 		Buffer CreateBuffer(size_t size, void* initdata) const;
215 		// Delete the buffer
216 		void DeleteBuffer(Buffer buffer) const;
217 		// Map buffer. Event pointer might be nullptr.
218 		// The call is asynchronous.
219 		void MapBuffer(Buffer buffer, MapType type, size_t offset, size_t size, void** data, Event* event) const;
220 		// Unmap buffer
221 		void UnmapBuffer(Buffer buffer, void* ptr, Event* event) const;
222 		
223 		/******************************************
224           Events handling
225         *******************************************/
226 		void DeleteEvent(Event event) const;
227 		
228 		/******************************************
229           Ray casting
230         ******************************************/
231 		// Complete path:
232 		// Find closest intersection
233 		// The call is asynchronous. Event pointers might be nullptrs.
234 		void QueryIntersection(const Buffer rays, int numrays, Buffer hitinfos, const Event waitevent, Event* event) const;
235 		// Find any intersection.
236 		// The call is asynchronous. Event pointer mights be nullptrs.
237 		void QueryOcclusion(const Buffer rays, int numrays, Buffer hitresults, const Event waitevent, Event* event) const;
238 		
239 		// Find closest intersection, number of rays is in remote memory
240 		// The call is asynchronous. Event pointers might be nullptrs.
241 		void QueryIntersection(const Buffer rays, const Buffer numrays, int maxrays, Buffer hitinfos, const Event waitevent, Event* event) const;
242 		// Find any intersection.
243 		// The call is asynchronous. Event pointer mights be nullptrs.
244 		void QueryOcclusion(const Buffer rays, const Buffer numrays, int maxrays, Buffer hitresults, const Event waitevent, Event* event) const;
245 		
246 		/******************************************
247         Utility
248         ******************************************/
249 		// Supported options:
250 		// option "bvh.type" values {"bvh" (regular bvh, default), "qbvh" (4 branching factor), "hlbvh" (fast builds)}
251 		// option "bvh.force2level" values {0(default), 1}
252 		//         by default 2-level BVH is used only if there is instancing in the scene or
253 		//         motion blur is enabled. 1 forces 2-level BVH for all cases.
254 		// option "bvh.builder" values {"sah" (use surface area heuristic), "median" (use spatial median, faster to build, default)}
255 		// option "bvh.sah.use_splits" values {0(default),1} (allow spatial splits for BVH)
256 		// option "bvh.sah.traversal_cost" values {float, default = 10.f for GPU } (cost of node traversal vs triangle intersection)
257 		// option "bvh.sah.min_overlap" values { float < 1.f, default = 0.005f } 
258 		//         (overlap area which is considered for a spatial splits, fraction of parent bbox)
259 		// option "bvh.sah.max_split_depth" values {int, default = 10} (max depth in the tree where spatial split can happen)
260 		// option "bvh.sah.extra_node_budget" values {float, default = 1.f} (maximum node memory budget compared to normal bvh (2*num_tris - 1), for ex. 0.3 = 30% more nodes allowed
261 		// Set API global option: string
262 		void SetOption(const char* name, const char* value);
263 		// Set API global option: float
264 		void SetOption(const char* name, float value);
265 
266 	protected:
267 		
268 		~this(){}
269 	}
270 }