Skip to content

Commit

Permalink
Adding in the vulkan create info and vulkan debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
Maploop committed Jan 6, 2024
1 parent 8c637b7 commit a08b7b0
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files.associations": {
"cmemory.h": "c",
"darray.h": "c",
"asserts.h": "c"
},
"editor.cursorStyle": "block",
}
Binary file modified bin/engine.dll
Binary file not shown.
Binary file modified bin/engine.exp
Binary file not shown.
Binary file modified bin/engine.ilk
Binary file not shown.
Binary file modified bin/engine.lib
Binary file not shown.
Binary file modified bin/engine.pdb
Binary file not shown.
Binary file modified bin/testbed.exe
Binary file not shown.
Binary file modified bin/testbed.ilk
Binary file not shown.
Binary file modified bin/testbed.pdb
Binary file not shown.
20 changes: 20 additions & 0 deletions engine/src/core/cstring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "core/cstring.h"
#include "core/cmemory.h"

#include <string.h>

u64 string_length(const char* str) {
return strlen(str);
}

char* string_duplicate(const char* str) {
u64 length = string_length(str);
char* copy = callocate(length + 1, MEMORY_TAG_STRING);
ccopy_memory(copy, str, length + 1);
return copy;
}

// Case-sensitive string comparison. True if the same, otherwise false.
b8 strings_equal(const char* str0, const char* str1) {
return strcmp(str0, str1) == 0;
}
11 changes: 11 additions & 0 deletions engine/src/core/cstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "defines.h"

// Returns the length of the given string.
KAPI u64 string_length(const char* str);

KAPI char* string_duplicate(const char* str);

// Case-sensitive string comparison. True if the same, otherwise false.
KAPI b8 strings_equal(const char* str0, const char* str1);
12 changes: 10 additions & 2 deletions engine/src/platform/platform_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
#include "core/logger.h"
#include "core/input.h"

#include "containers/darray.h"

#include "renderer/vulkan/vulkan_platform.h"

#if KPLATFORM_WINDOWS

#include <windows.h>
#include <windowsx.h> // param input extraction
#include <windowsx.h>
#include <stdlib.h>

typedef struct internal_state {
Expand Down Expand Up @@ -154,7 +158,7 @@ void *platform_set_memory(void *dest, i32 value, u64 size) {
void platform_console_write(const char *message, u8 colour) {
HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
// FATAL,ERROR,WARN,INFO,DEBUG,TRACE
static u8 levels[6] = {64, 4, 6, 8, 8, 8};
static u8 levels[6] = {64, 4, 6, 2, 1, 8};
SetConsoleTextAttribute(console_handle, levels[colour]);
OutputDebugStringA(message);
u64 length = strlen(message);
Expand Down Expand Up @@ -183,6 +187,10 @@ void platform_sleep(u64 ms) {
Sleep(ms);
}

void platform_get_required_extension_names(const char*** names_darray) {
darray_push(*names_darray, &"VK_KHR_win32_surface");
}

LRESULT CALLBACK win32_process_message(HWND hwnd, u32 msg, WPARAM w_param, LPARAM l_param) {
switch (msg) {
case WM_ERASEBKGND:
Expand Down
138 changes: 129 additions & 9 deletions engine/src/renderer/vulkan/vulkan_backend.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
#include "vulkan_backend.h"

#include "vulkan_types.inl"
#include "vulkan_platform.h"

#include "core/cstring.h"
#include "core/logger.h"

#include "containers/darray.h"

#include "platform/platform.h"

// static Vulkan context
static vulkan_context context;

// NOTE: this is a forward declaration
VKAPI_ATTR VkBool32 VKAPI_CALL vk_debug_callback(
VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
VkDebugUtilsMessageTypeFlagsEXT message_types,
const VkDebugUtilsMessengerCallbackDataEXT* callback_data,
void* user_data);

b8 vulkan_renderer_backend_initialize(renderer_backend* backend, const char* application_name, struct platform_state* plat_state) {
// TODO: custom allocator.
context.allocator = 0;
Expand All @@ -21,22 +34,106 @@ b8 vulkan_renderer_backend_initialize(renderer_backend* backend, const char* app

VkInstanceCreateInfo create_info = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO};
create_info.pApplicationInfo = &app_info;
create_info.enabledExtensionCount = 0;
create_info.ppEnabledExtensionNames = 0;
create_info.enabledLayerCount = 0;
create_info.ppEnabledLayerNames = 0;

VkResult result = vkCreateInstance(&create_info, context.allocator, &context.instance);
if(result != VK_SUCCESS) {
CERROR("vkCreateInstance failed with result: %u", result);
return FALSE;

const char** required_extensions = darray_create(const char**);
darray_push(required_extensions, &VK_KHR_SURFACE_EXTENSION_NAME);
platform_get_required_extension_names(&required_extensions);

#if defined(_DEBUG)
darray_push(required_extensions, &VK_EXT_DEBUG_UTILS_EXTENSION_NAME);

CDEBUG("Required extensions:");
u32 length = darray_length(required_extensions);
for (int i = 0; i < length; ++i) {
CDEBUG(required_extensions[i]);
}
#endif

create_info.enabledExtensionCount = darray_length(required_extensions);
create_info.ppEnabledExtensionNames = required_extensions;

const char** required_validation_layer_names = 0;
u32 required_validation_layer_count = 0;

#if defined(_DEBUG)
CINFO("Validation layers enabled. Enumerating...");

// The list of validation layers required.
required_validation_layer_names = darray_create(const char*);
darray_push(required_validation_layer_names, &"VK_LAYER_KHRONOS_validation");
required_validation_layer_count = darray_length(required_validation_layer_names);

// Obtain a list of available validation layers
u32 available_layer_count = 0;
VK_CHECK(vkEnumerateInstanceLayerProperties(&available_layer_count, 0));
VkLayerProperties* available_layers = darray_reserve(VkLayerProperties, available_layer_count);
VK_CHECK(vkEnumerateInstanceLayerProperties(&available_layer_count, available_layers));

// Verify all required layers are available.
for (u32 i = 0; i < required_validation_layer_count; ++i) {
CINFO("Searching for layer: %s...", required_validation_layer_names[i]);
b8 found = FALSE;
for (u32 j = 0; j < available_layer_count; ++j) {
if (strings_equal(required_validation_layer_names[i], available_layers[j].layerName)) {
found = TRUE;
CINFO("Found.");
break;
}
}

if (!found) {
CFATAL("Required validation layer is missing: %s", required_validation_layer_names[i]);
return FALSE;
}
}
CINFO("All required validation layers are present.");
#endif

create_info.enabledLayerCount = required_validation_layer_count;
create_info.ppEnabledLayerNames = required_validation_layer_names;

VK_CHECK(vkCreateInstance(&create_info, context.allocator, &context.instance));
CINFO("Vulkan Instance created.");

// Debugger
#if defined(_DEBUG)
CDEBUG("Creating Vulkan debugger...");
u32 log_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT; //|
// VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;

VkDebugUtilsMessengerCreateInfoEXT debug_create_info = {VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT};
debug_create_info.messageSeverity = log_severity;
debug_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
debug_create_info.pfnUserCallback = vk_debug_callback;

PFN_vkCreateDebugUtilsMessengerEXT func =
(PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(context.instance, "vkCreateDebugUtilsMessengerEXT");
CASSERT_MSG(func, "Failed to create debug messenger!");
VK_CHECK(func(context.instance, &debug_create_info, context.allocator, &context.debug_messenger));
CDEBUG("Vulkan debugger created.");
#endif

create_info.enabledLayerCount = required_validation_layer_count;
create_info.ppEnabledLayerNames = required_validation_layer_names;

VK_CHECK(vkCreateInstance(&create_info, context.allocator, &context.instance));

CINFO("Vulkan renderer initialized successfully.");
return TRUE;
}

void vulkan_renderer_backend_shutdown(renderer_backend* backend) {
CDEBUG("Destroying Vulkan debugger...");
if (context.debug_messenger) {
PFN_vkDestroyDebugUtilsMessengerEXT func =
(PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(context.instance, "vkDestroyDebugUtilsMessengerEXT");
func(context.instance, context.debug_messenger, context.allocator);
}

CDEBUG("Destroying Vulkan instance...");
vkDestroyInstance(context.instance, context.allocator);
}

void vulkan_renderer_backend_on_resized(renderer_backend* backend, u16 width, u16 height) {
Expand All @@ -48,4 +145,27 @@ b8 vulkan_renderer_backend_begin_frame(renderer_backend* backend, f32 delta_time

b8 vulkan_renderer_backend_end_frame(renderer_backend* backend, f32 delta_time) {
return TRUE;
}

VKAPI_ATTR VkBool32 VKAPI_CALL vk_debug_callback(
VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
VkDebugUtilsMessageTypeFlagsEXT message_types,
const VkDebugUtilsMessengerCallbackDataEXT* callback_data,
void* user_data) {
switch (message_severity) {
default:
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:
CERROR(callback_data->pMessage);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT:
CWARN(callback_data->pMessage);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:
CINFO(callback_data->pMessage);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT:
CTRACE(callback_data->pMessage);
break;
}
return VK_FALSE;
}
5 changes: 5 additions & 0 deletions engine/src/renderer/vulkan/vulkan_platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "defines.h"

void platform_get_required_extension_names(const char*** names_darray);
9 changes: 9 additions & 0 deletions engine/src/renderer/vulkan/vulkan_types.inl
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#pragma once

#include "defines.h"
#include "core/asserts.h"

#include <vulkan/vulkan.h>

#define VK_CHECK(expr) \
{ \
CASSERT(expr == VK_SUCCESS); \
}

typedef struct vulkan_context {
VkInstance instance;
VkAllocationCallbacks* allocator;

#if defined(_DEBUG)
VkDebugUtilsMessengerEXT debug_messenger;
#endif
} vulkan_context;
2 changes: 1 addition & 1 deletion testbed/src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <core/logger.h>

b8 game_initialize(game* game_inst) {
CDEBUG("game_init called");
CDEBUG("game_initialize was called");
return TRUE;
}

Expand Down

0 comments on commit a08b7b0

Please sign in to comment.