vulkan_test2

changeset 8:633b522539a1

shaders
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Jun 2018 02:08:06 +0300
parents 20eb42197ab8
children 5fbfd7f8fc24
files src/vksdr.c src/vksdr.h
diffstat 2 files changed, 62 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/vksdr.c	Fri Jun 22 02:08:06 2018 +0300
     1.3 @@ -0,0 +1,53 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <errno.h>
     1.8 +#include "vksdr.h"
     1.9 +#include "vku.h"
    1.10 +
    1.11 +VkShaderModule vku_load_shader(const char *fname)
    1.12 +{
    1.13 +	int size;
    1.14 +	VkShaderModuleCreateInfo inf;
    1.15 +	VkShaderModule sdr;
    1.16 +	FILE *fp;
    1.17 +	char *buf = 0;
    1.18 +
    1.19 +	if(!(fp = fopen(fname, "rb"))) {
    1.20 +		fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno));
    1.21 +		return 0;
    1.22 +	}
    1.23 +	fseek(fp, 0, SEEK_END);
    1.24 +	size = ftell(fp);
    1.25 +	rewind(fp);
    1.26 +
    1.27 +	if(!(buf = malloc(size + 1))) {
    1.28 +		fprintf(stderr, "vku_load_shader: failed to allocate buffer\n");
    1.29 +		goto err;
    1.30 +	}
    1.31 +	if(fread(buf, 1, size, fp) < size) {
    1.32 +		fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname);
    1.33 +		goto err;
    1.34 +	}
    1.35 +
    1.36 +	memset(&inf, 0, sizeof inf);
    1.37 +	inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
    1.38 +	inf.codeSize = size;
    1.39 +	inf.pCode = (const uint32_t*)buf;
    1.40 +
    1.41 +	if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) {
    1.42 +		fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname);
    1.43 +		goto err;
    1.44 +	}
    1.45 +	return sdr;
    1.46 +
    1.47 +err:
    1.48 +	fclose(fp);
    1.49 +	free(buf);
    1.50 +	return 0;
    1.51 +}
    1.52 +
    1.53 +void vku_destroy_shader(VkShaderModule sdr)
    1.54 +{
    1.55 +	vkDestroyShaderModule(vkdev, sdr, 0);
    1.56 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/vksdr.h	Fri Jun 22 02:08:06 2018 +0300
     2.3 @@ -0,0 +1,9 @@
     2.4 +#ifndef VKSDR_H_
     2.5 +#define VKSDR_H_
     2.6 +
     2.7 +#include <vulkan/vulkan.h>
     2.8 +
     2.9 +VkShaderModule vku_load_shader(const char *fname);
    2.10 +void vku_destroy_shader(VkShaderModule sdr);
    2.11 +
    2.12 +#endif	/* VKSDR_H_ */