Added helper function for loading shaders into buffers
This commit is contained in:
parent
fc9768f859
commit
d596dfc579
1 changed files with 57 additions and 6 deletions
63
src/main.c
63
src/main.c
|
|
@ -183,17 +183,12 @@ void createInstance(Application *app) {
|
|||
}
|
||||
}
|
||||
|
||||
// This currently leaks memory :(
|
||||
// This currently leaks memory on laptop :(
|
||||
VkResult result = vkCreateInstance(&createInfo, NULL, &app->instance);
|
||||
if (result != VK_SUCCESS) {
|
||||
fprintf(stderr, "Failed to create vulkan instance\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
//printf("Instance Extentions Available:\n");
|
||||
//for (uint i = 0; i < extensionCount; i++) {
|
||||
// printf("\t%s\n", extensions[i].extensionName);
|
||||
//}
|
||||
}
|
||||
|
||||
void initWindow(Application *app) {
|
||||
|
|
@ -615,6 +610,61 @@ void createImageViews(Application *app) {
|
|||
}
|
||||
}
|
||||
|
||||
// Buffer should be freed
|
||||
typedef struct {
|
||||
char *buffer;
|
||||
size_t bufferSize;
|
||||
} ShaderBuffer;
|
||||
|
||||
ShaderBuffer loadShaderIntoBuffer(const char *fileLocation) {
|
||||
ShaderBuffer buf = {.buffer = NULL, .bufferSize = 0};
|
||||
FILE *file = fopen(fileLocation, "r");
|
||||
|
||||
if (file == NULL) {
|
||||
fprintf(stderr, "Could not open file \"%s\"!\n", fileLocation);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (fseek(file, 0L, SEEK_END) != 0) {
|
||||
fprintf(stderr, "Could not seek to the end of file \"%s\"!\n",
|
||||
fileLocation);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
buf.bufferSize = ftell(file);
|
||||
|
||||
if (fseek(file, 0L, SEEK_SET) != 0) {
|
||||
fprintf(stderr, "Could not seek to the start of file \"%s\"!\n",
|
||||
fileLocation);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
buf.buffer = malloc(sizeof(char) * (buf.bufferSize)); // Not '\0' terminting ourselves
|
||||
|
||||
size_t tempSize = fread(buf.buffer, sizeof(char), buf.bufferSize, file);
|
||||
if (ferror(file) != 0) {
|
||||
fprintf(stderr, "Error reading file into buffer \"%s\"!\n", fileLocation);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void createGraphicsPipeline(Application *app) {
|
||||
ShaderBuffer vertShader = loadShaderIntoBuffer("shaders/shader.vert.spv");
|
||||
for (int i = 0; i < vertShader.bufferSize;i++){
|
||||
printf("%c",vertShader.buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
ShaderBuffer fragShader = loadShaderIntoBuffer("shaders/shader.frag.spv");
|
||||
for (int i = 0; i < fragShader.bufferSize;i++){
|
||||
printf("%c",fragShader.buffer[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
free(vertShader.buffer);
|
||||
free(fragShader.buffer);
|
||||
}
|
||||
|
||||
void initVulkan(Application *app) {
|
||||
createInstance(app);
|
||||
setupDebugMessenger(app);
|
||||
|
|
@ -623,6 +673,7 @@ void initVulkan(Application *app) {
|
|||
createLogicalDevice(app);
|
||||
createSwapChain(app);
|
||||
createImageViews(app);
|
||||
createGraphicsPipeline(app);
|
||||
}
|
||||
|
||||
void mainLoop(Application *app) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue