vulkan_test2

annotate src/vksdr.c @ 8:633b522539a1

shaders
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Jun 2018 02:08:06 +0300
parents
children 5fbfd7f8fc24
rev   line source
nuclear@8 1 #include <stdio.h>
nuclear@8 2 #include <stdlib.h>
nuclear@8 3 #include <string.h>
nuclear@8 4 #include <errno.h>
nuclear@8 5 #include "vksdr.h"
nuclear@8 6 #include "vku.h"
nuclear@8 7
nuclear@8 8 VkShaderModule vku_load_shader(const char *fname)
nuclear@8 9 {
nuclear@8 10 int size;
nuclear@8 11 VkShaderModuleCreateInfo inf;
nuclear@8 12 VkShaderModule sdr;
nuclear@8 13 FILE *fp;
nuclear@8 14 char *buf = 0;
nuclear@8 15
nuclear@8 16 if(!(fp = fopen(fname, "rb"))) {
nuclear@8 17 fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno));
nuclear@8 18 return 0;
nuclear@8 19 }
nuclear@8 20 fseek(fp, 0, SEEK_END);
nuclear@8 21 size = ftell(fp);
nuclear@8 22 rewind(fp);
nuclear@8 23
nuclear@8 24 if(!(buf = malloc(size + 1))) {
nuclear@8 25 fprintf(stderr, "vku_load_shader: failed to allocate buffer\n");
nuclear@8 26 goto err;
nuclear@8 27 }
nuclear@8 28 if(fread(buf, 1, size, fp) < size) {
nuclear@8 29 fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname);
nuclear@8 30 goto err;
nuclear@8 31 }
nuclear@8 32
nuclear@8 33 memset(&inf, 0, sizeof inf);
nuclear@8 34 inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
nuclear@8 35 inf.codeSize = size;
nuclear@8 36 inf.pCode = (const uint32_t*)buf;
nuclear@8 37
nuclear@8 38 if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) {
nuclear@8 39 fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname);
nuclear@8 40 goto err;
nuclear@8 41 }
nuclear@8 42 return sdr;
nuclear@8 43
nuclear@8 44 err:
nuclear@8 45 fclose(fp);
nuclear@8 46 free(buf);
nuclear@8 47 return 0;
nuclear@8 48 }
nuclear@8 49
nuclear@8 50 void vku_destroy_shader(VkShaderModule sdr)
nuclear@8 51 {
nuclear@8 52 vkDestroyShaderModule(vkdev, sdr, 0);
nuclear@8 53 }