Created
December 31, 2016 07:31
-
-
Save Swoorup/af44fb2f18e15fc5264229687ff0ae8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.array; | |
import std.stdio; | |
import std.exception; | |
import std.string; | |
import std.conv : to; | |
import std.algorithm.searching; | |
import std.algorithm.iteration; | |
import erupted; | |
import derelict.sdl2.sdl; | |
// should we enable vulkan validation layers | |
const bool enableValidationLayers = true; | |
string[1] validationLayers = ["VK_LAYER_LUNARG_standard_validation"]; | |
// what vulkan extension to use according to the platform | |
version(linux) { | |
string[] extensionNames = [ | |
"VK_KHR_surface", | |
"VK_EXT_debug_report", | |
"VK_KHR_xlib_surface", | |
]; | |
} | |
else version(Windows) { | |
string[] extensionNames = [ | |
"VK_KHR_surface", | |
"VK_EXT_debug_report", | |
"VK_KHR_xlib_surface", | |
]; | |
} | |
else | |
static assert(false, "Unsupported platform"); | |
extern(System) VkBool32 MyDebugReportCallback( | |
VkDebugReportFlagsEXT flags, | |
VkDebugReportObjectTypeEXT objectType, | |
uint64_t object, | |
size_t location, | |
int32_t messageCode, | |
const (char)* pLayerPrefix, | |
const (char)* pMessage, | |
void* pUserData) nothrow @nogc | |
{ | |
import std.stdio; | |
printf("ObjectTpye: %i \n", objectType); | |
printf(pMessage); | |
printf("\n"); | |
return VK_FALSE; | |
} | |
void enforceVk(VkResult res){ | |
import std.exception; | |
import std.conv; | |
enforce(res is VkResult.VK_SUCCESS, res.to!string); | |
} | |
class HelloTriangleApplication { | |
public: | |
void run() { | |
initWindow(); | |
initVulkan(); | |
mainLoop(); | |
} | |
private: | |
void initWindow() { | |
_sdlWindow = SDL_CreateWindow("vulkan", 0, 0, _width, _height, 0); | |
SDL_SysWMinfo sdlWindowInfo; | |
SDL_VERSION(&sdlWindowInfo.version_); | |
enforce(SDL_GetWindowWMInfo(_sdlWindow, &sdlWindowInfo), "sdl err"); | |
} | |
void initVulkan() { | |
bool checkValidationLayerSupport() { | |
uint layerCount; | |
vkEnumerateInstanceLayerProperties(&layerCount, null); | |
auto availableLayers = new VkLayerProperties[](layerCount); | |
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.ptr); | |
// writefln("Available layers: %s" , layerCount); | |
// writefln("Available layers: %s" , availableLayers); | |
return validationLayers[].all!((layerName){ | |
return availableLayers[].count!((layer) { | |
writefln("Matching %s == %s, %s", to!string(layer.layerName), layerName, to!string(layer.layerName) == layerName); | |
return fromStringz(layer.layerName) == layerName; | |
}) > 0; | |
}); | |
} | |
bool checkExtensionSupport(){ | |
uint extensionCount = 0; | |
vkEnumerateInstanceExtensionProperties(null, &extensionCount, null ); | |
auto extensionProps = new VkExtensionProperties[](extensionCount); | |
vkEnumerateInstanceExtensionProperties(null, &extensionCount, extensionProps.ptr ); | |
return extensionNames[].all!((extensionName){ | |
return extensionProps[].count!((extension){ | |
return to!string(extension.extensionName) == extensionName; | |
}) > 0; | |
}); | |
} | |
void createInstance() { | |
if (enableValidationLayers) | |
enforce(checkValidationLayerSupport(),"validation layers requested, but not available!"); | |
VkApplicationInfo appInfo = {}; | |
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; | |
appInfo.pApplicationName = "Hello Triangle"; | |
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); | |
appInfo.pEngineName = "No Engine"; | |
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); | |
appInfo.apiVersion = VK_API_VERSION_1_0; | |
VkInstanceCreateInfo createInfo = {}; | |
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; | |
createInfo.pApplicationInfo = &appInfo; | |
enforce(checkExtensionSupport(), "requested extensions are not available"); | |
createInfo.enabledExtensionCount = cast(uint)extensionNames.length; | |
createInfo.ppEnabledExtensionNames = extensionNames[].map!toStringz.array.ptr; | |
if (enableValidationLayers) { | |
createInfo.enabledLayerCount = cast(uint)validationLayers.length; | |
createInfo.ppEnabledLayerNames = validationLayers[].map!toStringz.array.ptr; | |
} else { | |
createInfo.enabledLayerCount = 0; | |
} | |
enforceVk(vkCreateInstance(&createInfo, null, m_instance)); | |
} | |
void setupDebugCallback() { | |
} | |
void pickPhysicalDevice() { | |
} | |
void createLogicalDevice() { | |
} | |
createInstance(); | |
setupDebugCallback(); | |
pickPhysicalDevice(); | |
createLogicalDevice(); | |
} | |
void mainLoop() { | |
} | |
@property void width(int value) { | |
enforce(value > 0, "Width of the window cannot be zero. DipShit"); | |
_width = value; | |
} | |
@property void height(int value) { | |
enforce(value > 0, "Heigth of the window cannot be zero. DipShit"); | |
_width = value; | |
} | |
SDL_Window* _sdlWindow; | |
int _width = 800; | |
int _height = 600; | |
// vulkan idiosyncraes | |
VkInstance* m_instance; | |
} | |
void main() | |
{ | |
import std.stdio; | |
// load the sdl library | |
DerelictSDL2.load(); | |
// load the vulkan lbrary | |
DerelictErupted.load(); | |
// run the application | |
HelloTriangleApplication app = new HelloTriangleApplication(); | |
app.width = 1024; | |
app.height = 768; | |
app.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment