Skip to content
This repository was archived by the owner on Dec 23, 2019. It is now read-only.

Snippets

Alex Parrill edited this page Mar 19, 2016 · 1 revision

Various snippets for making life with Vulkan easier.

Exception class + enforce

Used in several snippets.

/++
 + Vulkan exception class.
++/
final class VulkanException : Exception {
	/// VkResult that this error represents
	immutable VkResult result;
	
	/// Constructor
	pure this(VkResult res, string file =__FILE__, size_t line = __LINE__, Throwable next=null) {
		super(res.to!string, file, line, next);
		this.result = res;
	}
}

/++
 + If the argument is not VK_SUCCESS, throws a VulkanException.
++/
void enforce(VkResult res) {
	if(res != VkResult.VK_SUCCESS)
		throw new VulkanException(res);
}

Array getter

Wrapper for array retrieving functions that deals with getting the length, allocating the array, and the actual data.

/++
 + Gets a D array from a Vulkan array getter function.
 +
 + func must take the form `function(Args..., uint* length, T* output)` and return
 + either void or VkResult. Throws `VulkanException` on a non-success status.
++/
template getArray(alias func) {
	alias Params = Parameters!func;
	static assert(Params.length >= 2, "Invalid getArray func: needs >= 2 parameters");
	static assert(is(Params[$-2] == uint*), "Invalid getArray func: needs second-to-last parameters to be uint*");
	static if(is(Params[$-1] == AT*, AT))
		alias ArrType = AT;
	else
		static assert(false, "Invalid getArray func: needs last parameter to be a pointer");
	alias Rt = ReturnType!func;
	static assert(is(Rt == void) || is(Rt == VkResult), "Invalid getArray func: must return void or VkResult");
	
	ArrType[] getArray(Params[0..$-2] args) {
		uint len;
		static if(is(Rt == void))
			func(args, &len, null);
		else
			enforce(func(args, &len, null));
		ArrType[] arr = new ArrType[](len);
		static if(is(Rt == void))
			func(args, &len, arr.ptr);
		else
			enforce(func(args, &len, arr.ptr));
		return arr;
	}
}
Clone this wiki locally