vulkan_test2

view src/vksdr.c @ 9:5fbfd7f8fc24

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