vulkan_test2
diff src/vkpipe.c @ 12:e17abe477616
pipeline madness
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 23 Jun 2018 07:47:49 +0300 |
parents | src/vksdr.c@5fbfd7f8fc24 |
children | d34f84bede17 |
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 +}