vulkan_test2
changeset 12:e17abe477616
pipeline madness
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 23 Jun 2018 07:47:49 +0300 |
parents | dc85ded6ceee |
children | d34f84bede17 |
files | src/vkpipe.c src/vkpipe.h src/vksdr.c src/vksdr.h src/vku.c src/vku.h |
diffstat | 6 files changed, 281 insertions(+), 62 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/vkpipe.c Sat Jun 23 07:47:49 2018 +0300 1.3 @@ -0,0 +1,225 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include <errno.h> 1.8 +#include "vkpipe.h" 1.9 +#include "vku.h" 1.10 + 1.11 +int vku_init_pstate(struct vku_pstate *st) 1.12 +{ 1.13 + int i; 1.14 + 1.15 + memset(st, 0, sizeof *st); 1.16 + 1.17 + for(i=0; i<VKU_MAX_SDR_STAGES; i++) { 1.18 + st->sdrstage[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; 1.19 + } 1.20 + st->vertinp.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; 1.21 + st->inpasm.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; 1.22 + st->vport.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; 1.23 + st->rast.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; 1.24 + st->msaa.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; 1.25 + st->zstencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; 1.26 + st->blend.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; 1.27 + st->layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; 1.28 + 1.29 + st->vport.pViewports = &st->vport_data; 1.30 + st->vport.pScissors = &st->scissor_data; 1.31 + st->vport.viewportCount = st->vport.scissorCount = 1; 1.32 + 1.33 + st->vport_data.minDepth = 0.0f; 1.34 + st->vport_data.maxDepth = 1.0f; 1.35 + 1.36 + st->blend.logicOp = VK_LOGIC_OP_COPY; 1.37 + st->blend.attachmentCount = 1; 1.38 + st->blend.pAttachments = &st->atblend; 1.39 + 1.40 + /* set some reasonable defaults (just the non-zero ones) */ 1.41 + vku_pstate_primitive(st, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); 1.42 + vku_pstate_polygon_mode(st, VK_POLYGON_MODE_FILL); 1.43 + vku_pstate_line_width(st, 1.0f); 1.44 + vku_pstate_front_face(st, VK_FRONT_FACE_COUNTER_CLOCKWISE); 1.45 + 1.46 + return 0; 1.47 +} 1.48 + 1.49 +void vku_pstate_shader(struct vku_pstate *st, VkShaderModule sdr, VkShaderStageFlagBits type) 1.50 +{ 1.51 + if(st->num_sdr_stages >= VKU_MAX_SDR_STAGES) { 1.52 + fprintf(stderr, "vku_pstate_shader: too many shaders\n"); 1.53 + abort(); 1.54 + } 1.55 + 1.56 + st->sdrstage[st->num_sdr_stages].stage = type; 1.57 + st->sdrstage[st->num_sdr_stages].module = sdr; 1.58 + st->sdrstage[st->num_sdr_stages].pName = "main"; 1.59 + ++st->num_sdr_stages; 1.60 +} 1.61 + 1.62 +void vku_pstate_primitive(struct vku_pstate *st, VkPrimitiveTopology prim) 1.63 +{ 1.64 + st->inpasm.topology = prim; 1.65 +} 1.66 + 1.67 +void vku_pstate_viewport(struct vku_pstate *st, int x, int y, int w, int h) 1.68 +{ 1.69 + st->vport_data.x = x; 1.70 + st->vport_data.y = y; 1.71 + st->vport_data.width = w; 1.72 + st->vport_data.height = h; 1.73 +} 1.74 + 1.75 +void vku_pstate_scissor(struct vku_pstate *st, int x, int y, int w, int h) 1.76 +{ 1.77 + st->scissor_data.offset.x = x; 1.78 + st->scissor_data.offset.y = y; 1.79 + st->scissor_data.extent.width = w; 1.80 + st->scissor_data.extent.height = h; 1.81 +} 1.82 + 1.83 +void vku_pstate_polygon_mode(struct vku_pstate *st, VkPolygonMode pmode) 1.84 +{ 1.85 + st->rast.polygonMode = pmode; 1.86 +} 1.87 + 1.88 +void vku_pstate_line_width(struct vku_pstate *st, float w) 1.89 +{ 1.90 + st->rast.lineWidth = w; 1.91 +} 1.92 + 1.93 +void vku_pstate_cull_mode(struct vku_pstate *st, VkCullModeFlagBits cull) 1.94 +{ 1.95 + st->rast.cullMode = cull; 1.96 +} 1.97 + 1.98 +void vku_pstate_front_face(struct vku_pstate *st, VkFrontFace front) 1.99 +{ 1.100 + st->rast.frontFace = front; 1.101 +} 1.102 + 1.103 +void vku_pstate_depth_bias(struct vku_pstate *st, float fslope, float fconst, float clamp) 1.104 +{ 1.105 + st->rast.depthBiasEnable = VK_TRUE; 1.106 + st->rast.depthBiasSlopeFactor = fslope; 1.107 + st->rast.depthBiasConstantFactor = fconst; 1.108 + st->rast.depthBiasClamp = clamp; 1.109 +} 1.110 + 1.111 +void vku_pstate_depth_test(struct vku_pstate *st, int enable) 1.112 +{ 1.113 + st->zstencil.depthTestEnable = enable ? VK_TRUE : VK_FALSE; 1.114 +} 1.115 + 1.116 +void vku_pstate_depth_func(struct vku_pstate *st, VkCompareOp op) 1.117 +{ 1.118 + st->zstencil.depthCompareOp = op; 1.119 +} 1.120 + 1.121 +void vku_pstate_depth_mask(struct vku_pstate *st, int zmask) 1.122 +{ 1.123 + st->zstencil.depthWriteEnable = zmask ? VK_TRUE : VK_FALSE; 1.124 +} 1.125 + 1.126 +void vku_pstate_stencil_test(struct vku_pstate *st, int enable) 1.127 +{ 1.128 + st->zstencil.stencilTestEnable = enable ? VK_TRUE : VK_FALSE; 1.129 +} 1.130 + 1.131 +void vku_pstate_stencil_func(struct vku_pstate *st, VkCompareOp op, int ref, unsigned int mask) 1.132 +{ 1.133 + st->zstencil.front.compareOp = st->zstencil.back.compareOp = op; 1.134 + st->zstencil.front.reference = st->zstencil.back.reference = ref; 1.135 + st->zstencil.front.writeMask = st->zstencil.back.compareMask = mask; 1.136 +} 1.137 + 1.138 +void vku_pstate_stencil_op(struct vku_pstate *st, VkStencilOp sfail, VkStencilOp dfail, VkStencilOp pass) 1.139 +{ 1.140 + st->zstencil.front.failOp = st->zstencil.back.failOp = sfail; 1.141 + st->zstencil.front.depthFailOp = st->zstencil.back.depthFailOp = dfail; 1.142 + st->zstencil.front.passOp = st->zstencil.back.passOp = pass; 1.143 +} 1.144 + 1.145 +void vku_pstate_stencil_mask(struct vku_pstate *st, unsigned int smask) 1.146 +{ 1.147 + st->zstencil.front.writeMask = st->zstencil.back.writeMask = smask; 1.148 +} 1.149 + 1.150 +void vku_pstate_color_mask(struct vku_pstate *st, int rmask, int gmask, int bmask, int amask) 1.151 +{ 1.152 + unsigned int mask = 0; 1.153 + 1.154 + if(rmask) mask |= VK_COLOR_COMPONENT_R_BIT; 1.155 + if(gmask) mask |= VK_COLOR_COMPONENT_G_BIT; 1.156 + if(bmask) mask |= VK_COLOR_COMPONENT_B_BIT; 1.157 + if(amask) mask |= VK_COLOR_COMPONENT_A_BIT; 1.158 + 1.159 + st->atblend.colorWriteMask = mask; 1.160 +} 1.161 + 1.162 +void vku_pstate_blend_enable(struct vku_pstate *st, int enable) 1.163 +{ 1.164 + st->atblend.blendEnable = enable ? VK_TRUE : VK_FALSE; 1.165 +} 1.166 + 1.167 +void vku_pstate_blend_func(struct vku_pstate *st, VkBlendFactor src, VkBlendFactor dst) 1.168 +{ 1.169 + st->atblend.srcColorBlendFactor = src; 1.170 + st->atblend.dstColorBlendFactor = dst; 1.171 + st->atblend.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; 1.172 + st->atblend.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; 1.173 + st->atblend.colorBlendOp = VK_BLEND_OP_ADD; 1.174 + st->atblend.alphaBlendOp = VK_BLEND_OP_ADD; 1.175 +} 1.176 + 1.177 + 1.178 +VkPipeline vku_create_pipeline(struct vku_pstate *st) 1.179 +{ 1.180 + return 0; /* TODO */ 1.181 +} 1.182 + 1.183 +VkShaderModule vku_load_shader(const char *fname) 1.184 +{ 1.185 + int size; 1.186 + VkShaderModuleCreateInfo inf; 1.187 + VkShaderModule sdr; 1.188 + FILE *fp; 1.189 + void *buf = 0; 1.190 + 1.191 + if(!(fp = fopen(fname, "rb"))) { 1.192 + fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno)); 1.193 + return 0; 1.194 + } 1.195 + fseek(fp, 0, SEEK_END); 1.196 + size = ftell(fp); 1.197 + rewind(fp); 1.198 + 1.199 + if(!(buf = malloc(size + 1))) { 1.200 + fprintf(stderr, "vku_load_shader: failed to allocate buffer\n"); 1.201 + goto err; 1.202 + } 1.203 + if(fread(buf, 1, size, fp) < size) { 1.204 + fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname); 1.205 + goto err; 1.206 + } 1.207 + 1.208 + memset(&inf, 0, sizeof inf); 1.209 + inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; 1.210 + inf.codeSize = size; 1.211 + inf.pCode = buf; 1.212 + 1.213 + if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) { 1.214 + fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname); 1.215 + goto err; 1.216 + } 1.217 + return sdr; 1.218 + 1.219 +err: 1.220 + fclose(fp); 1.221 + free(buf); 1.222 + return 0; 1.223 +} 1.224 + 1.225 +void vku_destroy_shader(VkShaderModule sdr) 1.226 +{ 1.227 + vkDestroyShaderModule(vkdev, sdr, 0); 1.228 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/vkpipe.h Sat Jun 23 07:47:49 2018 +0300 2.3 @@ -0,0 +1,53 @@ 2.4 +#ifndef VKPIPE_H_ 2.5 +#define VKPIPE_H_ 2.6 + 2.7 +#include <vulkan/vulkan.h> 2.8 + 2.9 +#define VKU_MAX_SDR_STAGES 8 2.10 + 2.11 +struct vku_pstate { 2.12 + VkPipelineShaderStageCreateInfo sdrstage[VKU_MAX_SDR_STAGES]; 2.13 + int num_sdr_stages; 2.14 + VkPipelineVertexInputStateCreateInfo vertinp; 2.15 + VkPipelineInputAssemblyStateCreateInfo inpasm; 2.16 + VkPipelineViewportStateCreateInfo vport; 2.17 + VkPipelineRasterizationStateCreateInfo rast; 2.18 + VkPipelineMultisampleStateCreateInfo msaa; 2.19 + VkPipelineDepthStencilStateCreateInfo zstencil; 2.20 + VkPipelineColorBlendStateCreateInfo blend; 2.21 + /*VkPipelineDynamicStateCreateInfo dyn;*/ 2.22 + VkPipelineLayoutCreateInfo layout; 2.23 + 2.24 + /* data needed by the structs above */ 2.25 + VkViewport vport_data; 2.26 + VkRect2D scissor_data; 2.27 + VkPipelineColorBlendAttachmentState atblend; 2.28 +}; 2.29 + 2.30 +int vku_init_pstate(struct vku_pstate *st); 2.31 +void vku_pstate_shader(struct vku_pstate *st, VkShaderModule sdr, VkShaderStageFlagBits type); 2.32 +void vku_pstate_primitive(struct vku_pstate *st, VkPrimitiveTopology prim); 2.33 +void vku_pstate_viewport(struct vku_pstate *st, int x, int y, int w, int h); 2.34 +void vku_pstate_scissor(struct vku_pstate *st, int x, int y, int w, int h); 2.35 +void vku_pstate_polygon_mode(struct vku_pstate *st, VkPolygonMode pmode); 2.36 +void vku_pstate_line_width(struct vku_pstate *st, float w); 2.37 +void vku_pstate_cull_mode(struct vku_pstate *st, VkCullModeFlagBits cull); 2.38 +void vku_pstate_front_face(struct vku_pstate *st, VkFrontFace front); 2.39 +void vku_pstate_depth_bias(struct vku_pstate *st, float fslope, float fconst, float clamp); 2.40 +void vku_pstate_depth_test(struct vku_pstate *st, int enable); 2.41 +void vku_pstate_depth_func(struct vku_pstate *st, VkCompareOp op); 2.42 +void vku_pstate_depth_mask(struct vku_pstate *st, int zmask); 2.43 +void vku_pstate_stencil_test(struct vku_pstate *st, int enable); 2.44 +void vku_pstate_stencil_func(struct vku_pstate *st, VkCompareOp op, int ref, unsigned int mask); 2.45 +void vku_pstate_stencil_op(struct vku_pstate *st, VkStencilOp sfail, VkStencilOp dfail, VkStencilOp pass); 2.46 +void vku_pstate_stencil_mask(struct vku_pstate *st, unsigned int smask); 2.47 +void vku_pstate_color_mask(struct vku_pstate *st, int rmask, int gmask, int bmask, int amask); 2.48 +void vku_pstate_blend_enable(struct vku_pstate *st, int enable); 2.49 +void vku_pstate_blend_func(struct vku_pstate *st, VkBlendFactor src, VkBlendFactor dst); 2.50 + 2.51 +VkPipeline vku_create_pipeline(struct vku_pstate *st); 2.52 + 2.53 +VkShaderModule vku_load_shader(const char *fname); 2.54 +void vku_destroy_shader(VkShaderModule sdr); 2.55 + 2.56 +#endif /* VKPIPE_H_ */
3.1 --- a/src/vksdr.c Fri Jun 22 15:29:30 2018 +0300 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,53 +0,0 @@ 3.4 -#include <stdio.h> 3.5 -#include <stdlib.h> 3.6 -#include <string.h> 3.7 -#include <errno.h> 3.8 -#include "vksdr.h" 3.9 -#include "vku.h" 3.10 - 3.11 -VkShaderModule vku_load_shader(const char *fname) 3.12 -{ 3.13 - int size; 3.14 - VkShaderModuleCreateInfo inf; 3.15 - VkShaderModule sdr; 3.16 - FILE *fp; 3.17 - void *buf = 0; 3.18 - 3.19 - if(!(fp = fopen(fname, "rb"))) { 3.20 - fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno)); 3.21 - return 0; 3.22 - } 3.23 - fseek(fp, 0, SEEK_END); 3.24 - size = ftell(fp); 3.25 - rewind(fp); 3.26 - 3.27 - if(!(buf = malloc(size + 1))) { 3.28 - fprintf(stderr, "vku_load_shader: failed to allocate buffer\n"); 3.29 - goto err; 3.30 - } 3.31 - if(fread(buf, 1, size, fp) < size) { 3.32 - fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname); 3.33 - goto err; 3.34 - } 3.35 - 3.36 - memset(&inf, 0, sizeof inf); 3.37 - inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; 3.38 - inf.codeSize = size; 3.39 - inf.pCode = buf; 3.40 - 3.41 - if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) { 3.42 - fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname); 3.43 - goto err; 3.44 - } 3.45 - return sdr; 3.46 - 3.47 -err: 3.48 - fclose(fp); 3.49 - free(buf); 3.50 - return 0; 3.51 -} 3.52 - 3.53 -void vku_destroy_shader(VkShaderModule sdr) 3.54 -{ 3.55 - vkDestroyShaderModule(vkdev, sdr, 0); 3.56 -}
4.1 --- a/src/vksdr.h Fri Jun 22 15:29:30 2018 +0300 4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 4.3 @@ -1,9 +0,0 @@ 4.4 -#ifndef VKSDR_H_ 4.5 -#define VKSDR_H_ 4.6 - 4.7 -#include <vulkan/vulkan.h> 4.8 - 4.9 -VkShaderModule vku_load_shader(const char *fname); 4.10 -void vku_destroy_shader(VkShaderModule sdr); 4.11 - 4.12 -#endif /* VKSDR_H_ */