Added shader loading, added glad
This commit is contained in:
81
src/display/render.c
Normal file
81
src/display/render.c
Normal file
@ -0,0 +1,81 @@
|
||||
#include "render.h"
|
||||
|
||||
render_t *RENDER_CURRENT = NULL;
|
||||
|
||||
render_t * renderInit(char *name) {
|
||||
// Can't contextualize twice
|
||||
if(RENDER_CURRENT != NULL) return NULL;
|
||||
|
||||
// Attempt to init GLFW
|
||||
if(!glfwInit()) return NULL;
|
||||
|
||||
// Initialize the renderer
|
||||
render_t *render = malloc(sizeof(render_t));
|
||||
if(!render) return NULL;
|
||||
render->width = WINDOW_WIDTH_DEFAULT;
|
||||
render->height = WINDOW_HEIGHT_DEFAULT;
|
||||
|
||||
// Create the window.
|
||||
render->window = glfwCreateWindow(render->width, render->height,
|
||||
name, NULL, NULL
|
||||
);
|
||||
if(!render->window) {
|
||||
free(render);
|
||||
glfwTerminate();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Make the window the context current
|
||||
glfwMakeContextCurrent(render->window);
|
||||
|
||||
// Load GLAD
|
||||
gladLoadGL((GLADloadproc)glfwGetProcAddress);
|
||||
|
||||
RENDER_CURRENT = render;
|
||||
|
||||
// Listeners
|
||||
glfwSetWindowSizeCallback(render->window, &renderOnResize);
|
||||
|
||||
//GLEnable Things
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthMask(GL_TRUE);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
|
||||
return render;
|
||||
}
|
||||
|
||||
void renderFrame(render_t *render) {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
glColor3f(1, 0, 0);
|
||||
glVertex3f(-1, -1, 0);
|
||||
|
||||
glColor3f(0, 1, 0);
|
||||
glVertex3f(0, 1, 0);
|
||||
|
||||
glColor3f(0, 0, 1);
|
||||
glVertex3f(1, -1, 0);
|
||||
glEnd();
|
||||
}
|
||||
|
||||
bool renderDispose(render_t *render) {
|
||||
// Cleanup the callback
|
||||
glfwSetWindowSizeCallback(render->window, NULL);
|
||||
|
||||
// Free up the renderer
|
||||
free(render);
|
||||
RENDER_CURRENT = NULL;
|
||||
|
||||
// Terminate the GLFW context.
|
||||
glfwTerminate();
|
||||
return true;
|
||||
}
|
||||
|
||||
void renderOnResize(GLFWwindow *window, int32_t width, int32_t height) {
|
||||
RENDER_CURRENT->width = width;
|
||||
RENDER_CURRENT->height = height;
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
65
src/display/render.h
Normal file
65
src/display/render.h
Normal file
@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
|
||||
// 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/call.h>
|
||||
|
||||
#define WINDOW_WIDTH_DEFAULT 480
|
||||
#define WINDOW_HEIGHT_DEFAULT 270
|
||||
|
||||
/**
|
||||
* Contains information about the current render state, can be used for querying
|
||||
* how the renderer is currently set up.
|
||||
*/
|
||||
typedef struct {
|
||||
/** The GLFW Window Context Pointer */
|
||||
GLFWwindow *window;
|
||||
|
||||
/** Resolution (in pixels) */
|
||||
int32_t width, height;
|
||||
} render_t;
|
||||
|
||||
/** Current active renderer. */
|
||||
extern render_t *RENDER_CURRENT;
|
||||
|
||||
/**
|
||||
* Initialize the renderer.
|
||||
*
|
||||
* @param width Width (in pixels) of the window.
|
||||
* @param height Height (in pixels) of the window.
|
||||
* @param name String of the windows' name.
|
||||
* @return Rendering Context information.
|
||||
*/
|
||||
render_t * renderInit(char *name);
|
||||
|
||||
/**
|
||||
* Render a single frame of the render loop. The renderer is not (currently)
|
||||
* responsible for render looping.
|
||||
*
|
||||
* @param render The renderer to loop for.
|
||||
*/
|
||||
void renderFrame(render_t *render);
|
||||
|
||||
/**
|
||||
* Cleanup a render context.
|
||||
*
|
||||
* @param render Renderer to cleanup.
|
||||
* @return True or False if Successful/Not.
|
||||
*/
|
||||
bool renderDispose(render_t *render);
|
||||
|
||||
/**
|
||||
* Resize callbacks.
|
||||
*
|
||||
* @param window Window that was resized.
|
||||
* @param width New window width.
|
||||
* @param height New window height.
|
||||
*/
|
||||
void renderOnResize(GLFWwindow *window, int32_t width, int32_t height);
|
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);
|
||||
}
|
43
src/display/shader.h
Normal file
43
src/display/shader.h
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#include <glad/glad.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <malloc.h>
|
||||
|
||||
/**
|
||||
* Structure containing information about an OpenGL Shader. For simplicity sake
|
||||
* we demand certain uninforms to be present on the shader target.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Pointer to an uploaded vertex shader program */
|
||||
GLuint shaderVertex;
|
||||
|
||||
/** Pointer to an uploaded fragment shader program */
|
||||
GLuint shaderFrag;
|
||||
|
||||
/** Pointer to an uploaded shader program linked */
|
||||
GLuint shaderProgram;
|
||||
} shader_t;
|
||||
|
||||
/**
|
||||
* Create a shader from vertex and fragment shader code.
|
||||
*
|
||||
* @param vertexShaderSource The raw vertex shader code.
|
||||
* @param fragmentShaderSource The raw fragment shader code.
|
||||
* @return Pointer to the loaded shader.
|
||||
*/
|
||||
shader_t * shaderCompile(char *vertexShaderSource, char* fragmentShaderSource);
|
||||
|
||||
/**
|
||||
* Cleanup and unload a previously loaded shader.
|
||||
*
|
||||
* @param shader The shader to unload
|
||||
* @return True if successfully disposed.
|
||||
*/
|
||||
bool shaderDipose(shader_t *shader);
|
||||
|
||||
/**
|
||||
* Attaches the supplied shader as the current shader.
|
||||
* @param shader The shader to attach
|
||||
*/
|
||||
void shaderUse(shader_t *shader);
|
84
src/file/asset.c
Normal file
84
src/file/asset.c
Normal file
@ -0,0 +1,84 @@
|
||||
#include "asset.h"
|
||||
|
||||
char * assetStringLoad(char *assetName) {
|
||||
// Open a buffer.
|
||||
FILE *fptr = assetBufferOpen(assetName);
|
||||
if(fptr == NULL) return NULL;
|
||||
|
||||
// Read the count of bytes in the file
|
||||
fseek(fptr, 0, SEEK_END);// Seek to the end
|
||||
size_t length = ftell(fptr);// Get our current position (the end)
|
||||
fseek(fptr, 0, SEEK_SET);// Reset the seek
|
||||
|
||||
// Create the string buffer
|
||||
char *str = malloc(length + 1);// Add 1 for the null terminator.
|
||||
if(str == NULL) {
|
||||
assetBufferClose(fptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Read and seal the string.
|
||||
fread(str, 1, length, fptr);// Read all the bytes
|
||||
str[length] = '\0';// Null terminate.
|
||||
assetBufferClose(fptr); // Close the buffer.
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
FILE * assetBufferOpen(char *assetName) {
|
||||
// Get the directory based on the raw input by creating a new string.
|
||||
size_t lenAsset = strlen(assetName);// Get the length of asset
|
||||
size_t lenPrefix = strlen(ASSET_PREFIX);// Get the length of the prefix
|
||||
|
||||
// Create str to house both the prefix and asset, and null terminator
|
||||
char *joined = malloc(lenAsset + lenPrefix + 1);
|
||||
if(joined == NULL) return NULL;// Mem okay?
|
||||
|
||||
joined[0] = '\0';//Start at null
|
||||
strcat(joined, ASSET_PREFIX);//Add prefix
|
||||
strcat(joined, assetName);//Add body
|
||||
|
||||
// Open the file pointer now.
|
||||
FILE *fptr = fopen(joined, "rb");
|
||||
free(joined);// Free the string we just created
|
||||
if(!fptr) return NULL;// File available?
|
||||
return fptr;
|
||||
}
|
||||
|
||||
bool assetBufferClose(FILE *buffer) {
|
||||
return fclose(buffer);
|
||||
}
|
||||
|
||||
int32_t assetBufferRead(FILE *buffer, char *data, int32_t size) {
|
||||
return (int32_t)fread(data, 1, size, buffer);
|
||||
}
|
||||
|
||||
int32_t assetBufferEnd(FILE *buffer) {
|
||||
return feof(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.
|
||||
}
|
68
src/file/asset.h
Normal file
68
src/file/asset.h
Normal file
@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#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/"
|
||||
|
||||
/**
|
||||
* Method to load an asset into memory as a raw string.
|
||||
*
|
||||
* @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 * assetStringLoad(char *assetName);
|
||||
|
||||
/**
|
||||
* Platform-centric method to open a file buffer to an asset.
|
||||
*
|
||||
* @param assetName The asset name to open a buffer for.
|
||||
* @return Pointer to a buffer, NULL if unsuccessfuil.
|
||||
*/
|
||||
FILE * assetBufferOpen(char *assetName);
|
||||
|
||||
/**
|
||||
* Closes a previously opened asset buffer.
|
||||
*
|
||||
* @param buffer Buffer to close.
|
||||
* @return True if successful, otherwise false.
|
||||
*/
|
||||
bool assetBufferClose(FILE *buffer);
|
||||
|
||||
/**
|
||||
* Read bytes from buffer.
|
||||
*
|
||||
* @param buffer The buffer pointing to an asset.
|
||||
* @param data Pointer to a ubyte array to buffer data into.
|
||||
* @param size Length of the data buffer. Represents how many bytes can be read.
|
||||
* @return The count of bytes read. Complete when less than data array size.
|
||||
*/
|
||||
int32_t assetBufferRead(FILE *buffer, char *data, int32_t size);
|
||||
|
||||
/**
|
||||
* Skip to the end of the buffer, useful to find the length of the buffer.
|
||||
*
|
||||
* @param Buffer The buffer pointing to an asset.
|
||||
* @return How many bytes were skipped
|
||||
*/
|
||||
int32_t assetBufferEnd(FILE *buffer);
|
||||
|
||||
/**
|
||||
* Method to skip n bytes in the buffer
|
||||
*
|
||||
* @param buffer The buffer pointing to an asset.
|
||||
* @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);
|
40
src/game/game.c
Normal file
40
src/game/game.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include "game.h"
|
||||
|
||||
game_t * gameInit(char *gameName) {
|
||||
// Create the game instance
|
||||
game_t *game = malloc(sizeof(game_t));
|
||||
if(game == NULL) return NULL;
|
||||
|
||||
// Setup the renderer
|
||||
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;
|
||||
}
|
||||
|
||||
void gameStart(game_t *game) {
|
||||
while(!glfwWindowShouldClose(game->render->window)) {
|
||||
renderFrame(game->render);
|
||||
|
||||
glfwSwapBuffers(game->render->window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
}
|
||||
|
||||
bool gameDispose(game_t *game) {
|
||||
if(!renderDispose(game->render)) return false;
|
||||
free(game);
|
||||
|
||||
return true;
|
||||
}
|
34
src/game/game.h
Normal file
34
src/game/game.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include "../display/render.h"
|
||||
#include "../file/asset.h"
|
||||
|
||||
/** Information about the current game context. */
|
||||
typedef struct {
|
||||
/** Renderer for the game */
|
||||
render_t *render;
|
||||
} game_t;
|
||||
|
||||
/**
|
||||
* Initialize the game context.
|
||||
*
|
||||
* @param gameName Name of the game being initialized.
|
||||
* @return The game instance context.
|
||||
*/
|
||||
game_t * gameInit(char *gameName);
|
||||
|
||||
/**
|
||||
* Start the main game loop.
|
||||
*
|
||||
* @param game The game to start the loop for.
|
||||
*/
|
||||
void gameStart(game_t *game);
|
||||
|
||||
/**
|
||||
* Cleanup a previously constructed.
|
||||
*
|
||||
* @param game The game to cleanup.
|
||||
* @return True if successful or not.
|
||||
*/
|
||||
bool gameDispose(game_t *game);
|
||||
|
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
|
||||
|
Reference in New Issue
Block a user