Added shader loading, added glad
This commit is contained in:
@ -6,6 +6,9 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
#Include
|
||||
include(FetchContent)
|
||||
|
||||
#Vars
|
||||
set(DEPS_DIR "${PROJECT_BINARY_DIR}/_deps")
|
||||
|
||||
#################################### PROJECT ###################################
|
||||
project(Dawn VERSION 1.0)
|
||||
|
||||
@ -21,8 +24,10 @@ include_directories(${CMAKE_SOURCE_DIR}/lib/stb)
|
||||
add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES})
|
||||
|
||||
################################# STATIC LIBS ##################################
|
||||
add_subdirectory(${CMAKE_SOURCE_DIR}/lib/glad)
|
||||
target_link_libraries(${PROJECT_NAME} glad)
|
||||
|
||||
# GLFW
|
||||
# find_package(glfw3 3.3.2)
|
||||
if(NOT glfw3_FOUND)
|
||||
FetchContent_Declare(
|
||||
glfw
|
||||
@ -33,9 +38,7 @@ if(NOT glfw3_FOUND)
|
||||
endif()
|
||||
target_link_libraries(${PROJECT_NAME} glfw)
|
||||
|
||||
|
||||
# CGLM
|
||||
# find_package(cglm)
|
||||
if(NOT cglm_FOUND)
|
||||
FetchContent_Declare(
|
||||
cglm
|
||||
@ -46,6 +49,8 @@ if(NOT cglm_FOUND)
|
||||
endif()
|
||||
target_link_libraries(${PROJECT_NAME} cglm)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
target_link_libraries(${PROJECT_NAME} OpenGL::GL)
|
||||
|
||||
# OpenMP
|
||||
# find_package(OpenMP)
|
||||
@ -53,8 +58,4 @@ target_link_libraries(${PROJECT_NAME} cglm)
|
||||
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
|
||||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
|
||||
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
|
||||
# endif()
|
||||
|
||||
# Vulkan
|
||||
find_package(Vulkan REQUIRED FATAL_ERROR)
|
||||
target_link_libraries(${PROJECT_NAME} Vulkan::Vulkan)
|
||||
# endif()
|
@ -2,7 +2,7 @@
|
||||
|
||||
render_t *RENDER_CURRENT = NULL;
|
||||
|
||||
render_t * renderInit(int32_t width, int32_t height, char *name) {
|
||||
render_t * renderInit(char *name) {
|
||||
// Can't contextualize twice
|
||||
if(RENDER_CURRENT != NULL) return NULL;
|
||||
|
||||
@ -12,11 +12,13 @@ render_t * renderInit(int32_t width, int32_t height, char *name) {
|
||||
// Initialize the renderer
|
||||
render_t *render = malloc(sizeof(render_t));
|
||||
if(!render) return NULL;
|
||||
render->width = width;
|
||||
render->height = height;
|
||||
render->width = WINDOW_WIDTH_DEFAULT;
|
||||
render->height = WINDOW_HEIGHT_DEFAULT;
|
||||
|
||||
// Create the window.
|
||||
render->window = glfwCreateWindow(width, height, name, NULL, NULL);
|
||||
render->window = glfwCreateWindow(render->width, render->height,
|
||||
name, NULL, NULL
|
||||
);
|
||||
if(!render->window) {
|
||||
free(render);
|
||||
glfwTerminate();
|
||||
@ -25,6 +27,10 @@ render_t * renderInit(int32_t width, int32_t height, char *name) {
|
||||
|
||||
// Make the window the context current
|
||||
glfwMakeContextCurrent(render->window);
|
||||
|
||||
// Load GLAD
|
||||
gladLoadGL((GLADloadproc)glfwGetProcAddress);
|
||||
|
||||
RENDER_CURRENT = render;
|
||||
|
||||
// Listeners
|
@ -5,11 +5,14 @@
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#define GLFW_INCLUDE_VULKAN
|
||||
// I load GLAD and GLFW Here because they need to be included in specific orders
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <cglm/vec4.h>
|
||||
#include <cglm/mat4.h>
|
||||
#include <cglm/call.h>
|
||||
|
||||
#define WINDOW_WIDTH_DEFAULT 480
|
||||
#define WINDOW_HEIGHT_DEFAULT 270
|
||||
|
||||
/**
|
||||
* Contains information about the current render state, can be used for querying
|
||||
@ -34,7 +37,7 @@ extern render_t *RENDER_CURRENT;
|
||||
* @param name String of the windows' name.
|
||||
* @return Rendering Context information.
|
||||
*/
|
||||
render_t * renderInit(int32_t width, int32_t height, char *name);
|
||||
render_t * renderInit(char *name);
|
||||
|
||||
/**
|
||||
* Render a single frame of the render loop. The renderer is not (currently)
|
88
src/display/shader.c
Normal file
88
src/display/shader.c
Normal file
@ -0,0 +1,88 @@
|
||||
#include "shader.h"
|
||||
|
||||
shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) {
|
||||
int isSuccess, maxLength;
|
||||
char *error;
|
||||
GLuint shaderVertex, shaderFragment, shaderProgram;
|
||||
|
||||
|
||||
// Load the vertex shader first
|
||||
shaderVertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(shaderVertex, 1, &vertexShaderSource, 0);
|
||||
glCompileShader(shaderVertex);
|
||||
|
||||
// Validate
|
||||
glGetShaderiv(shaderVertex, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(shaderVertex, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
error = malloc(maxLength);
|
||||
glGetShaderInfoLog(shaderVertex, maxLength, &maxLength, error);
|
||||
printf("Failed to compile vertex shader %s\n", error);
|
||||
free(error);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Now load the Frag shader
|
||||
shaderFragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(shaderFragment, 1, &fragmentShaderSource, 0);
|
||||
glCompileShader(shaderFragment);
|
||||
glGetShaderiv(shaderFragment, GL_COMPILE_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetShaderiv(shaderFragment, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
error = malloc(maxLength);
|
||||
glGetShaderInfoLog(shaderFragment, maxLength, &maxLength, error);
|
||||
printf("Failed to compile fragment shader %s\n", error);
|
||||
free(error);
|
||||
glDeleteShader(shaderVertex);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Now create the shader program.
|
||||
shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, shaderVertex);
|
||||
glAttachShader(shaderProgram, shaderFragment);
|
||||
|
||||
//Bind, Verify & Use the shader program
|
||||
glLinkProgram(shaderProgram);
|
||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &isSuccess);
|
||||
if(!isSuccess) {
|
||||
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
|
||||
error = malloc(maxLength);
|
||||
glGetProgramInfoLog(shaderProgram, maxLength, &maxLength, error);
|
||||
printf("Failed to load shader program %s\n", error);
|
||||
free(error);
|
||||
glDeleteShader(shaderVertex);
|
||||
glDeleteShader(shaderFragment);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Everything is okay, let's create the encapsulated shader.
|
||||
shader_t *shader = malloc(sizeof(shader_t));
|
||||
if(shader == NULL) {
|
||||
glDeleteProgram(shaderProgram);
|
||||
glDeleteShader(shaderVertex);
|
||||
glDeleteShader(shaderFragment);
|
||||
return NULL;
|
||||
}
|
||||
shader->shaderVertex = shaderVertex;
|
||||
shader->shaderFrag = shaderFragment;
|
||||
shader->shaderProgram = shaderProgram;
|
||||
|
||||
// Bind the shader
|
||||
shaderUse(shader);
|
||||
|
||||
// Fetch the uniforms.
|
||||
return shader;
|
||||
}
|
||||
|
||||
bool shaderDipose(shader_t *shader) {
|
||||
glDeleteProgram(shader->shaderProgram);
|
||||
glDeleteShader(shader->shaderVertex);
|
||||
glDeleteShader(shader->shaderFrag);
|
||||
free(shader);
|
||||
return true;
|
||||
}
|
||||
|
||||
void shaderUse(shader_t *shader) {
|
||||
glUseProgram(shader->shaderProgram);
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
#pragma once
|
||||
#include <GL/gl3w.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glad/glad.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <malloc.h>
|
||||
|
||||
/**
|
||||
* Structure containing information about an OpenGL Shader. For simplicity sake
|
||||
@ -9,13 +10,13 @@
|
||||
*/
|
||||
typedef struct {
|
||||
/** Pointer to an uploaded vertex shader program */
|
||||
unsigned int shaderVertex;
|
||||
GLuint shaderVertex;
|
||||
|
||||
/** Pointer to an uploaded fragment shader program */
|
||||
unsigned int shaderFrag;
|
||||
GLuint shaderFrag;
|
||||
|
||||
/** Pointer to an uploaded shader program linked */
|
||||
unsigned int shaderProgram;
|
||||
GLuint shaderProgram;
|
||||
} shader_t;
|
||||
|
||||
/**
|
||||
@ -33,4 +34,10 @@ shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource);
|
||||
* @param shader The shader to unload
|
||||
* @return True if successfully disposed.
|
||||
*/
|
||||
bool shaderDipose(shader_t *shader);
|
||||
bool shaderDipose(shader_t *shader);
|
||||
|
||||
/**
|
||||
* Attaches the supplied shader as the current shader.
|
||||
* @param shader The shader to attach
|
||||
*/
|
||||
void shaderUse(shader_t *shader);
|
@ -1,6 +1,6 @@
|
||||
#include "asset.h"
|
||||
|
||||
char * assetLoadString(char *assetName) {
|
||||
char * assetStringLoad(char *assetName) {
|
||||
// Open a buffer.
|
||||
FILE *fptr = assetBufferOpen(assetName);
|
||||
if(fptr == NULL) return NULL;
|
||||
@ -59,4 +59,26 @@ int32_t assetBufferEnd(FILE *buffer) {
|
||||
|
||||
void assetBufferSkip(FILE *buffer, int32_t n) {
|
||||
fseek(buffer, n, SEEK_CUR);
|
||||
}
|
||||
|
||||
shader_t * assetShaderLoad(char *fileVertex, char *fileFragment) {
|
||||
// Load the vertex shader into memory
|
||||
char *vertexShader = assetStringLoad(fileVertex);
|
||||
if(vertexShader == NULL) return NULL;
|
||||
|
||||
// Load the fragment shader into memory
|
||||
char *fragmentShader = assetStringLoad(fileFragment);
|
||||
if(fragmentShader == NULL) {
|
||||
free(vertexShader);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Now attempt to load the shader
|
||||
shader_t *shader = shaderCompile(vertexShader, fragmentShader);
|
||||
|
||||
//Cleanup
|
||||
free(vertexShader);
|
||||
free(fragmentShader);
|
||||
|
||||
return shader;//shader may be NULL if loading failed, but not our problem.
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include "../display/shader.h"
|
||||
|
||||
/** Prefix of all asset load methods, may be customizable in future. */
|
||||
#define ASSET_PREFIX "../assets/"
|
||||
@ -14,7 +15,7 @@
|
||||
* @param assetName Path leading to the asset within the root asset directory.
|
||||
* @return Pointer to char array of data from asset, NULL if unsuccesful.
|
||||
*/
|
||||
char * assetLoadString(char *assetName);
|
||||
char * assetStringLoad(char *assetName);
|
||||
|
||||
/**
|
||||
* Platform-centric method to open a file buffer to an asset.
|
||||
@ -57,3 +58,11 @@ int32_t assetBufferEnd(FILE *buffer);
|
||||
* @param n Count of bytes to skip.
|
||||
*/
|
||||
void assetBufferSkip(FILE *buffer, int32_t n);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param fileVertex The file path of the vertex shader
|
||||
* @param fileFragment The file path of the fragment shader
|
||||
* @return The loaded shader_t instance (From shaderCompile)
|
||||
*/
|
||||
shader_t * assetShaderLoad(char *fileVertex, char *fileFragment);
|
@ -6,12 +6,20 @@ game_t * gameInit(char *gameName) {
|
||||
if(game == NULL) return NULL;
|
||||
|
||||
// Setup the renderer
|
||||
game->render = renderInit(640, 480, gameName);
|
||||
game->render = renderInit(gameName);
|
||||
if(game->render == NULL) {
|
||||
free(game);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Load a shader
|
||||
shader_t *shader = assetShaderLoad("shaders/test.vert", "shaders/test.frag");
|
||||
if(shader == NULL) {
|
||||
printf("Shader loading failed\n");
|
||||
} else {
|
||||
printf("Shader loaded!\n");
|
||||
}
|
||||
|
||||
return game;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include "../display/render.h"
|
||||
#include "../file/asset.h"
|
||||
|
||||
/** Information about the current game context. */
|
||||
typedef struct {
|
316
src/main.c
316
src/main.c
@ -1,319 +1,17 @@
|
||||
#include "main.h"
|
||||
|
||||
int32_t main() {
|
||||
// Init variables
|
||||
int i;
|
||||
// Create the game instance
|
||||
game_t *game = gameInit("Dawn");
|
||||
if(game == NULL) return 1;
|
||||
|
||||
//Prepare GLFW, get some info now that we pass into vulkan later.
|
||||
if(!glfwInit()) {
|
||||
printf("Failed to init glfw\n");
|
||||
return 1;
|
||||
}
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
||||
GLFWwindow *window = glfwCreateWindow(
|
||||
WINDOW_WIDTH, WINDOW_HEIGHT, "Vulkan window", NULL, NULL
|
||||
);
|
||||
uint32_t glfwExtensionCount = 0;
|
||||
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
||||
// Start running the game instance
|
||||
gameStart(game);
|
||||
|
||||
//Prepare information about the app for Vulkan
|
||||
VkApplicationInfo appInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
||||
.pNext = NULL,
|
||||
.pApplicationName = "Step 1",
|
||||
.applicationVersion = 1,
|
||||
.pEngineName = NULL,
|
||||
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
|
||||
.apiVersion = VK_API_VERSION_1_0
|
||||
};
|
||||
|
||||
// Create the Vulkan instance
|
||||
VkInstanceCreateInfo createInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
||||
.pNext = NULL,
|
||||
.flags = 0,
|
||||
.ppEnabledLayerNames = NULL,
|
||||
.pApplicationInfo = &appInfo,
|
||||
.enabledExtensionCount = glfwExtensionCount,
|
||||
.ppEnabledExtensionNames = glfwExtensions,
|
||||
.enabledLayerCount = 0
|
||||
};
|
||||
VkInstance instance;
|
||||
VkResult result = vkCreateInstance(&createInfo, NULL, &instance);
|
||||
if(result != VK_SUCCESS) {
|
||||
printf("Failed to init vulkan\n");
|
||||
// Game has finished running, cleanup.
|
||||
if(!gameDispose(game)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create the rendering surface
|
||||
VkSurfaceKHR surface;
|
||||
if(glfwCreateWindowSurface(instance, window, NULL, &surface) != VK_SUCCESS) {
|
||||
printf("Failed to create window surface!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get and print the extension support
|
||||
uint32_t extensionCount = 0;
|
||||
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, NULL);
|
||||
VkExtensionProperties *extensionProperties = malloc(sizeof(VkExtensionProperties) * extensionCount);
|
||||
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensionProperties);
|
||||
printf("Available extensions:\n");
|
||||
for(i = 0; i < extensionCount; i++) {
|
||||
printf(" %s\n", extensionProperties[i].extensionName);
|
||||
}
|
||||
|
||||
// Physical Device finding.
|
||||
uint32_t deviceCount = 0;
|
||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||
vkEnumeratePhysicalDevices(instance, &deviceCount, NULL);
|
||||
if(deviceCount == 0) {
|
||||
printf("No Vulkan GPU\n");
|
||||
return 1;
|
||||
}
|
||||
VkPhysicalDevice *devices = malloc(sizeof(VkPhysicalDevice) * deviceCount);
|
||||
|
||||
// Select a suitable device from that list of devices we just made.
|
||||
QueueFamilyIndices indices;
|
||||
SwapChainSupportDetails details;
|
||||
vkEnumeratePhysicalDevices(instance, &deviceCount, devices);
|
||||
for(i = 0; i < deviceCount; i++) {
|
||||
VkPhysicalDevice device = devices[i];
|
||||
VkPhysicalDeviceProperties deviceProperties;
|
||||
VkPhysicalDeviceFeatures deviceFeatures;
|
||||
vkGetPhysicalDeviceProperties(device, &deviceProperties);
|
||||
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
|
||||
|
||||
// For this we select a DISCRETE GPU, not an internal.
|
||||
if(deviceProperties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) continue;
|
||||
// Make sure it can run a geom shader
|
||||
if(!deviceFeatures.geometryShader) continue;
|
||||
|
||||
// This is something I don't really understand but I know we need it.
|
||||
// Follow the method for info.
|
||||
indices = findQueueFamilies(device);
|
||||
if(!indices.graphicsFamilyFound) continue;
|
||||
|
||||
// Supports Presenting?
|
||||
VkBool32 presentSupport = false;
|
||||
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
|
||||
if(!presentSupport) continue;
|
||||
indices.presentFamily = i;
|
||||
indices.presentFamilyFound = true;
|
||||
|
||||
// Now I'm trying to find out what extensions this device supports
|
||||
uint32_t extensionCount;
|
||||
vkEnumerateDeviceExtensionProperties(device, NULL, &extensionCount, NULL);
|
||||
VkExtensionProperties *availableExtensions = malloc(sizeof(VkExtensionProperties) * extensionCount);
|
||||
vkEnumerateDeviceExtensionProperties(device, NULL, &extensionCount, availableExtensions);
|
||||
|
||||
// Make sure it supports the things I need
|
||||
int countHas = 0;
|
||||
for(int j = 0; j < extensionCount; j++) {
|
||||
VkExtensionProperties prop = availableExtensions[j];
|
||||
if(strcmp(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) {
|
||||
countHas++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if(countHas == 0) continue;
|
||||
|
||||
// Find out the capabilities of the swap chain.
|
||||
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
|
||||
|
||||
uint32_t formatCount, presentModeCount;
|
||||
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, NULL);
|
||||
if(formatCount == 0) continue;
|
||||
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, NULL);
|
||||
if(presentModeCount == 0) continue;
|
||||
|
||||
details.formats = malloc(sizeof(VkSurfaceFormatKHR) * formatCount);
|
||||
details.presentModes = malloc(sizeof(VkPresentModeKHR) * presentModeCount);
|
||||
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats);
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes);
|
||||
|
||||
// Determine the surface format
|
||||
details.surfaceFormat = details.formats[0];
|
||||
for(int j = 0; j < formatCount; j++) {
|
||||
VkSurfaceFormatKHR availableFormat = details.formats[j];
|
||||
if(availableFormat.format != VK_FORMAT_B8G8R8A8_SRGB) continue;
|
||||
if(availableFormat.colorSpace != VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) continue;
|
||||
details.surfaceFormat = availableFormat;
|
||||
break;
|
||||
}
|
||||
|
||||
// Choose the surface mode, prefer triple buffered, fallback to vsync
|
||||
details.presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
for(int j = 0; j < presentModeCount; j++) {
|
||||
VkPresentModeKHR availableMode = details.presentModes[j];
|
||||
if(availableMode != VK_PRESENT_MODE_MAILBOX_KHR) continue;
|
||||
details.presentMode = VK_PRESENT_MODE_MAILBOX_KHR;
|
||||
break;
|
||||
}
|
||||
|
||||
physicalDevice = device;
|
||||
break;
|
||||
}
|
||||
|
||||
// Did we find it?
|
||||
if(physicalDevice == VK_NULL_HANDLE) {
|
||||
printf("No Vulkan Device\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Swap Extent
|
||||
if (details.capabilities.currentExtent.width != UINT32_MAX) {
|
||||
details.extent = details.capabilities.currentExtent;
|
||||
} else {
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(window, &width, &height);
|
||||
|
||||
VkExtent2D actualExtent = {
|
||||
.width = width,
|
||||
.height = height
|
||||
};
|
||||
|
||||
actualExtent.width = MAX(
|
||||
details.capabilities.minImageExtent.width,
|
||||
MIN(details.capabilities.maxImageExtent.width, actualExtent.width)
|
||||
);
|
||||
actualExtent.height = MAX(
|
||||
details.capabilities.minImageExtent.height,
|
||||
MIN(details.capabilities.maxImageExtent.height, actualExtent.height)
|
||||
);
|
||||
|
||||
details.extent = actualExtent;
|
||||
}
|
||||
|
||||
uint32_t imageCount = details.capabilities.minImageCount + 1;
|
||||
if (details.capabilities.maxImageCount > 0 && imageCount > details.capabilities.maxImageCount) {
|
||||
imageCount = details.capabilities.maxImageCount;
|
||||
}
|
||||
|
||||
VkSwapchainCreateInfoKHR swapChainCreateInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
|
||||
.surface = surface,
|
||||
.minImageCount = imageCount,
|
||||
.imageFormat = details.surfaceFormat.format,
|
||||
.imageColorSpace = details.surfaceFormat.colorSpace,
|
||||
.imageExtent = details.extent,
|
||||
.imageArrayLayers = 1,
|
||||
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
|
||||
.preTransform = details.capabilities.currentTransform,
|
||||
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
||||
.presentMode = details.presentMode,
|
||||
.clipped = VK_TRUE,
|
||||
.oldSwapchain = VK_NULL_HANDLE
|
||||
};
|
||||
|
||||
uint32_t queueFamilyIndices[2];
|
||||
queueFamilyIndices[0] = indices.graphicsFamily;
|
||||
queueFamilyIndices[1] = indices.presentFamily;
|
||||
if (indices.graphicsFamily != indices.presentFamily) {
|
||||
swapChainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
||||
swapChainCreateInfo.queueFamilyIndexCount = 2;
|
||||
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
|
||||
} else {
|
||||
swapChainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
swapChainCreateInfo.queueFamilyIndexCount = 0; // Optional
|
||||
swapChainCreateInfo.pQueueFamilyIndices = NULL; // Optional
|
||||
}
|
||||
|
||||
// Now we create a LOGICAL device. I think this is a way of reserving the
|
||||
// queue from the indices and using it for our own needs.
|
||||
float queuePriority = 1.0f;
|
||||
VkDeviceQueueCreateInfo queueCreateInfo[DEV_QUEUE_COUNT];
|
||||
for(i = 0; i < DEV_QUEUE_COUNT; i++) {
|
||||
queueCreateInfo[i].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
||||
queueCreateInfo[i].queueFamilyIndex = i == 0 ? indices.graphicsFamily : indices.presentFamily;
|
||||
queueCreateInfo[i].queueCount = 1;
|
||||
queueCreateInfo[i].pNext = NULL;
|
||||
queueCreateInfo[i].pQueuePriorities = &queuePriority;// Related to threading
|
||||
}
|
||||
|
||||
VkPhysicalDeviceFeatures deviceFeatures = {
|
||||
.alphaToOne = VK_FALSE,
|
||||
};
|
||||
|
||||
char *logicalEnabledExtensions[1];
|
||||
logicalEnabledExtensions[0] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
|
||||
VkDeviceCreateInfo logicalCreateInfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
||||
.pQueueCreateInfos = queueCreateInfo,
|
||||
.queueCreateInfoCount = DEV_QUEUE_COUNT,
|
||||
.pEnabledFeatures = &deviceFeatures,
|
||||
.enabledExtensionCount = 1,
|
||||
.ppEnabledExtensionNames = logicalEnabledExtensions,
|
||||
.enabledLayerCount = 0,
|
||||
.pNext = NULL
|
||||
};
|
||||
VkDevice device;
|
||||
if(vkCreateDevice(physicalDevice, &logicalCreateInfo, NULL, &device) != VK_SUCCESS) {
|
||||
printf("Failed to create logical device.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get back our queues we made.
|
||||
VkQueue graphicsQueue, presentQueue;
|
||||
vkGetDeviceQueue(device, indices.graphicsFamily, 0, &graphicsQueue);
|
||||
vkGetDeviceQueue(device, indices.presentFamily, 0, &presentQueue);
|
||||
|
||||
VkSwapchainKHR swapChain;
|
||||
if(vkCreateSwapchainKHR(device, &swapChainCreateInfo, NULL, &swapChain) != VK_SUCCESS) {
|
||||
printf("Failed to create a swap chain\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
//Idk
|
||||
uint32_t imageCount;
|
||||
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, NULL);
|
||||
VkImage* swapChainImages = malloc(sizeof(VkImage) * imageCount);
|
||||
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages);
|
||||
|
||||
VkImageView> swapChainImageViews;
|
||||
|
||||
// Begin render loop
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
vkDestroySwapchainKHR(device, swapChain, NULL);
|
||||
vkDestroyDevice(device, NULL);
|
||||
vkDestroySurfaceKHR(instance, surface, NULL);
|
||||
vkDestroyInstance(instance, NULL);
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
|
||||
QueueFamilyIndices indices = {
|
||||
.graphicsFamily = 0,
|
||||
.graphicsFamilyFound = false,
|
||||
.presentFamily = 0,
|
||||
.presentFamilyFound = false
|
||||
};
|
||||
|
||||
// Basically what I understand is we're trying to find a specific kind of
|
||||
// queue from the capabilities of queues that the device supports. I think
|
||||
// that I will understand queues more later but for now we're just finding...
|
||||
uint32_t queueFamilyCount = 0;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, NULL);
|
||||
|
||||
VkQueueFamilyProperties *properties = malloc(sizeof(VkQueueFamilyProperties) * queueFamilyCount);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, properties);
|
||||
|
||||
for(int i = 0; i < queueFamilyCount; i++) {
|
||||
// ... A queue that has this particular bit flag.
|
||||
VkQueueFamilyProperties queueFamily = properties[i];
|
||||
|
||||
if(queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
||||
indices.graphicsFamily = i;
|
||||
indices.graphicsFamilyFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
return indices;
|
||||
}
|
34
src/main.h
34
src/main.h
@ -1,39 +1,7 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#define VK_USE_PLATFORM_WIN32_KHR
|
||||
#define GLFW_INCLUDE_VULKAN
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cglm/call.h>
|
||||
|
||||
#define WINDOW_WIDTH 800
|
||||
#define WINDOW_HEIGHT 600
|
||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
||||
#define MAX(a,b) (((a)>(b))?(a):(b))
|
||||
#define DEV_QUEUE_COUNT 2
|
||||
|
||||
typedef struct {
|
||||
uint32_t graphicsFamily;
|
||||
bool graphicsFamilyFound;
|
||||
|
||||
uint32_t presentFamily;
|
||||
bool presentFamilyFound;
|
||||
} QueueFamilyIndices;
|
||||
|
||||
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
|
||||
|
||||
typedef struct {
|
||||
VkSurfaceCapabilitiesKHR capabilities;
|
||||
VkSurfaceFormatKHR *formats;
|
||||
VkPresentModeKHR *presentModes;
|
||||
|
||||
VkSurfaceFormatKHR surfaceFormat;
|
||||
VkPresentModeKHR presentMode;
|
||||
VkExtent2D extent;
|
||||
} SwapChainSupportDetails;
|
||||
#include "game/game.h"
|
||||
|
||||
/**
|
||||
* Entry of the program
|
||||
|
@ -1,9 +0,0 @@
|
||||
#include "shader.h"
|
||||
|
||||
shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource) {
|
||||
}
|
||||
|
||||
|
||||
bool shaderDipose(shader_t *shader) {
|
||||
return true;
|
||||
}
|
17
test/main.c
17
test/main.c
@ -1,17 +0,0 @@
|
||||
#include "main.h"
|
||||
|
||||
int32_t main() {
|
||||
// Create the game instance
|
||||
game_t *game = gameInit("Dawn");
|
||||
if(game == NULL) return 1;
|
||||
|
||||
// Start running the game instance
|
||||
gameStart(game);
|
||||
|
||||
// Game has finished running, cleanup.
|
||||
if(!gameDispose(game)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user