Skip to content

Instantly share code, notes, and snippets.

@aolo2
Created November 5, 2018 21:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aolo2/57d74c07440e237f7ff591241eb5f132 to your computer and use it in GitHub Desktop.
Save aolo2/57d74c07440e237f7ff591241eb5f132 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <gsl/gsl_matrix.h>
// #include <gsl/gsl_matrix.h>
// gsl_matrix_alloc(w, h)
// gsl_matrix_set(*m, x, y, val)
// gsl_matrix_get(*m, x, y)
// gsl_matrix_free(*m)
const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;
const uint32_t validation_layer_count = 1;
const char *validation_layers[1] =
{
"VK_LAYER_LUNARG_standard_validation"
};
#ifdef DEBUG
const bool enable_validation_layers = true;
#else
const bool enable_validation_layers = false;
#endif
static inline void
_validate(VkResult result, char *procedure)
{
if (result != VK_SUCCESS)
{
printf("ERROR in procedure: %s\n", procedure);
exit(1);
}
}
static bool
is_device_suitable(
/*
VkPhysicalDevice device
*/
)
{
/*
VkPhysicalDeviceProperties device_props;
VkPhysicalDeviceFeatures device_feats;
vkGetPhysicalDeviceProperties(device, &device_props);
vkGetPhysicalDeviceFeatures(device, &device_feats);
return (device_props.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
device_feats.geometryShader);
*/
return(true);
}
int32_t
main(void)
{
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow *window = glfwCreateWindow(800, 600, "SRIR-V SSA", NULL, NULL);
VkInstance instance;
VkApplicationInfo app_info =
{
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "SRIR-V SSA",
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
.pEngineName = "No Engine",
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
.apiVersion = VK_API_VERSION_1_0
};
uint32_t glfw_ext_count = 0;
VkInstanceCreateInfo create_info =
{
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &app_info,
.ppEnabledExtensionNames = glfwGetRequiredInstanceExtensions(&glfw_ext_count),
.enabledExtensionCount = glfw_ext_count,
.enabledLayerCount = (enable_validation_layers ? validation_layer_count : 0),
.ppEnabledLayerNames = (enable_validation_layers ? validation_layers : NULL)
};
_validate(
vkCreateInstance(&create_info, NULL, &instance),
"vkCreateInstance");
VkPhysicalDevice device = VK_NULL_HANDLE;
uint32_t device_count = 0;
_validate(
vkEnumeratePhysicalDevices(instance, &device_count, NULL),
"vkEnumeratePhysicalDevices_1");
_validate(
device_count > 0 ? VK_SUCCESS : -1,
"find GPUs");
VkPhysicalDevice physical_devices[device_count];
_validate(
vkEnumeratePhysicalDevices(instance, &device_count, physical_devices),
"vkEnumeratePhysicalDevices_2");
for (uint32_t i = 0; i < device_count; ++i)
{
if (is_device_suitable(physical_devices[i]))
{
device = physical_devices[i];
break;
}
}
_validate(
device != VK_NULL_HANDLE ? VK_SUCCESS : -1,
"find a suitable GPU");
uint32_t queue_family_count = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, NULL);
VkQueueFamilyProperties queue_families[queue_family_count];
vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, queue_families);
int32_t gfx_queue = -1;
for (uint32_t i = 0; i < queue_family_count; ++i)
{
if (queue_families[i].queueCount > 0 && queue_families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
gfx_queue = i;
break;
}
}
_validate(
gfx_queue != -1 ? VK_SUCCESS : -1,
"find a sutiable queue");
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
}
vkDestroyInstance(instance, NULL);
glfwDestroyWindow(window);
glfwTerminate();
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment