Skip to content

Commit

Permalink
misc: more advancements with vulkan (doesn't work rn tho)
Browse files Browse the repository at this point in the history
  • Loading branch information
Supercip971 committed Apr 27, 2022
1 parent 380caae commit d2d05c9
Show file tree
Hide file tree
Showing 23 changed files with 609 additions and 65 deletions.
25 changes: 25 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---

BasedOnStyle: LLVM
UseTab: Never
IndentWidth: 4
TabWidth: 4
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 0
AccessModifierOffset: -4
FixNamespaceComments: true
SpaceBeforeInheritanceColon: true
BreakInheritanceList: AfterColon
IndentPPDirectives: AfterHash
ForEachMacros: ["__dummy_foreach"]
IncludeBlocks: Merge
IncludeCategories:
- Regex: '<([A-Za-z0-9.\/-_])+>'
Priority: 0
SortPriority: 0
- Regex: '"([A-Za-z0-9.\/-_])+"'
Priority: 10
SortPriority: 10
...
3 changes: 3 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@

#define WINDOW_WIDTH (1920)
#define WINDOW_HEIGHT (1080)

#define USE_GLFW
#define USE_VULKAN
13 changes: 12 additions & 1 deletion src/log/log.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
#pragma once
#pragma once

// Regular bold text
#define BBLK "\e[1;30m"
#define BRED "\e[1;31m"
#define BGRN "\e[1;32m"
#define BYEL "\e[1;33m"
#define BBLU "\e[1;34m"
#define BMAG "\e[1;35m"
#define BCYN "\e[1;36m"
#define BWHT "\e[1;37m"

#define CRESET "\e[0m"
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <cglm/cglm.h>
#include <render/render.h>
#include <stdio.h>
#include <utils.h>
#include <render/render.h>
#include <window/window.h>

int main(MAYBE_UNUSED int argc, MAYBE_UNUSED char **argv)
Expand All @@ -11,7 +11,7 @@ int main(MAYBE_UNUSED int argc, MAYBE_UNUSED char **argv)
window_engine_init();
window_init(&curr_window);

render_engine_init();
render_engine_init(&curr_window);
render_init(&render);
while (!window_should_close(&curr_window))
{
Expand Down
14 changes: 10 additions & 4 deletions src/render/render.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#pragma once
#pragma once

#include <stdint.h>
#include <window/window.h>

typedef struct
{
int id;
} Render;

int render_engine_init(void);
int render_engine_init(Window *window);

int render_engine_deinit(void);

int render_init(Render* self);
int render_init(Render *self);

int render_deinit(Render* self);
int render_deinit(Render *self);

int render_surface_init(Render *self, uintptr_t handle);

int render_surface_deinit(Render *self, uintptr_t handle);
18 changes: 10 additions & 8 deletions src/render/vulkan/debug.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <log/log.h>
#include <render/vulkan/debug.h>
#include <stdio.h>

Expand All @@ -9,44 +10,45 @@ VKAPI_ATTR VkBool32 VKAPI_CALL vulkan_debug_callback(
{
(void)pUserData;

printf("[VULKAN] ");
switch (messageSeverity)
{
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT:
{
printf("[VULKAN] verbose: ");
printf(BWHT "verbose: ");
break;
}
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:
{
printf("[VULKAN] info: ");
printf(BGRN "info: ");
break;
}
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT:
{
printf("[VULKAN] *warn*: ");
printf(BYEL "*warn*: ");
break;
}
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:
{
printf("[VULKAN] **error**: ");
printf(BRED "**error**: ");
break;
}
default:
{
printf("[VULKAN] **unknown** %i: ", messageSeverity);
printf("**unknown** %i: ", messageSeverity);
}
}

switch (messageType)
{
case VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT:
{
printf("validation: ");
printf(BCYN "validation: ");
break;
}
case VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT:
{
printf("performance:");
printf(BMAG "performance:");
break;
}
default:
Expand All @@ -55,7 +57,7 @@ VKAPI_ATTR VkBool32 VKAPI_CALL vulkan_debug_callback(
}
}

printf("%s\n", pCallbackData->pMessage);
printf(CRESET "%s\n", pCallbackData->pMessage);

return VK_FALSE;
}
Expand Down
74 changes: 61 additions & 13 deletions src/render/vulkan/device.c
Original file line number Diff line number Diff line change
@@ -1,53 +1,95 @@
#include <ds/vec.h>
#include <render/vulkan/device.h>
#include <render/vulkan/logical.h>
#include <render/vulkan/swapchain.h>
#include <render/vulkan/vulkan.h>
#include <stdbool.h>
#include <stdio.h>
#include <vulkan/vulkan.h>

static QueueFamilyIndices vulkan_find_queue_family(VkPhysicalDevice dev)
static QueueFamilyIndices vulkan_find_queue_family(VulkanCtx *self, VkPhysicalDevice dev)
{
QueueFamilyIndices idx = (QueueFamilyIndices){.index = 0, ._present = false};

QueueFamilyIndices idx = {};
vec_t(VkQueueFamilyProperties) queue_famiy_properties = {};
vec_init(&queue_famiy_properties);

uint32_t queue_family_count = 0;
vkGetPhysicalDeviceQueueFamilyProperties(dev, &queue_family_count, NULL);

if (queue_family_count == 0)
{
return idx;
}

vec_reserve(&queue_famiy_properties, queue_family_count);
vkGetPhysicalDeviceQueueFamilyProperties(dev, &queue_family_count, queue_famiy_properties.data);
vkGetPhysicalDeviceQueueFamilyProperties(dev, &queue_family_count,
queue_famiy_properties.data);

for (size_t i = 0; i < queue_family_count; i++)
{
VkQueueFamilyProperties curr = queue_famiy_properties.data[i];
if (curr.queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
idx.index = i;
idx.family_idx = i;
idx._present = true;
VkBool32 present_support;

vkGetPhysicalDeviceSurfaceSupportKHR(dev, idx.family_idx, self->surface, &present_support);

if (present_support)
{
idx._has_present_family = true;
idx.present_family = i;

vec_deinit(&queue_famiy_properties);
return idx;
}
}
else
{
idx._present = false;
}
}

vec_deinit(&queue_famiy_properties);
return idx;
}

static QueueFamilyIndices vulkan_pick_queue_family(VulkanCtx *self)
QueueFamilyIndices vulkan_pick_queue_family(VulkanCtx *self)
{
return vulkan_find_queue_family(self->device);

QueueFamilyIndices idx = vulkan_find_queue_family(self, self->physical_device);
return idx;
}

static bool vulkan_is_device_suitable(VkPhysicalDevice device)
static bool vulkan_is_device_suitable(VulkanCtx *self, VkPhysicalDevice device)
{
VkPhysicalDeviceProperties properties;
vkGetPhysicalDeviceProperties(device, &properties);
VkPhysicalDeviceFeatures features;
vkGetPhysicalDeviceFeatures(device, &features);

QueueFamilyIndices idx = vulkan_find_queue_family(device);
QueueFamilyIndices idx = vulkan_find_queue_family(self, device);

return properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
features.geometryShader && idx._present;
if (properties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU ||
!features.geometryShader ||
!idx._present ||
!idx._has_present_family ||
!vulkan_check_device_extensions(device))
{
return false;
}

SwapChainSupportDetails swapchain = swap_chain_support_query(self, device);

if (swapchain.formats.length == 0 || swapchain.modes.length == 0)
{
swap_chain_support_deinit(&swapchain);
return false;
}

swap_chain_support_deinit(&swapchain);
return true;
}

void vulkan_pick_physical_device(VulkanCtx *self)
Expand All @@ -72,7 +114,7 @@ void vulkan_pick_physical_device(VulkanCtx *self)
VkPhysicalDeviceProperties properties;
vkGetPhysicalDeviceProperties(devices.data[i], &properties);
printf("device[%u]: %s \n", i, properties.deviceName);
if (vulkan_is_device_suitable(devices.data[i]))
if (vulkan_is_device_suitable(self, devices.data[i]))
{
device = devices.data[i];
break;
Expand All @@ -81,7 +123,13 @@ void vulkan_pick_physical_device(VulkanCtx *self)

vec_deinit(&devices);

self->device = device;
if (device == VK_NULL_HANDLE)
{
printf("no device founded\n!");
exit(-1);
return;
}
self->physical_device = device;

vulkan_pick_queue_family(self);
}
7 changes: 6 additions & 1 deletion src/render/vulkan/device.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#pragma once

#include <ds/vec.h>
#include <render/vulkan/vulkan.h>
#include <stdbool.h>

typedef struct
{
uint32_t index;
uint32_t family_idx;
uint32_t present_family;
bool _present;
bool _has_present_family;
} QueueFamilyIndices;

void vulkan_pick_physical_device(VulkanCtx *self);

QueueFamilyIndices vulkan_pick_queue_family(VulkanCtx *self);
38 changes: 38 additions & 0 deletions src/render/vulkan/glfw_vulkan_handle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#define VK_USE_PLATFORM_XLIB_KHR
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_X11
#include <GLFW/glfw3native.h>
#include <render/vulkan/vulkan.h>
#include <stdint.h>

VkXlibSurfaceCreateInfoKHR glfw_vulkan_handle(GLFWwindow *window)
{
VkXlibSurfaceCreateInfoKHR sinfo = {
.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
.dpy = glfwGetX11Display(),
.window = glfwGetX11Window(window),
.pNext = NULL,
};
return sinfo;
}
int vulkan_render_surface_init(VulkanCtx *self, uintptr_t handle)
{
VkXlibSurfaceCreateInfoKHR sinfo = glfw_vulkan_handle((GLFWwindow *)handle);
vulkan_assert_success$(vkCreateXlibSurfaceKHR(self->instance, &sinfo, NULL, &self->surface));

// vulkan_assert_success$(glfwCreateWindowSurface(self->instance, (GLFWwindow *)handle, NULL, &self->surface));
return 0;
}
int vulkan_render_surface_deinit(VulkanCtx *self)
{

vkDestroySurfaceKHR(self->instance, self->surface, NULL);
return 0;
}

void vulkan_render_surface_target_size(VulkanCtx *self, uintptr_t handle, int *width, int *height)
{
(void)self;
glfwGetFramebufferSize((GLFWwindow *)handle, width, height);
}
6 changes: 6 additions & 0 deletions src/render/vulkan/layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ bool vulkan_load_validation_layer(VkInstanceCreateInfo *create)

return true;
}

void vulkan_load_validation_layer_device(VkDeviceCreateInfo *info)
{
info->enabledLayerCount = sizeof(enabled_layers) / sizeof(enabled_layers[0]);
info->ppEnabledLayerNames = enabled_layers;
}
6 changes: 4 additions & 2 deletions src/render/vulkan/layer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include <vulkan/vulkan.h>
#include <stdbool.h>
#include <vulkan/vulkan.h>

bool vulkan_load_validation_layer(VkInstanceCreateInfo *create);

bool vulkan_load_validation_layer(VkInstanceCreateInfo* create);
void vulkan_load_validation_layer_device(VkDeviceCreateInfo *info);
Loading

0 comments on commit d2d05c9

Please sign in to comment.