# HG changeset patch # User John Tsiombikas # Date 1529729269 -10800 # Node ID e17abe477616f5bb00dd2b45f6c563cbbf48af4d # Parent dc85ded6ceee598dbe0a52114ef56fa2bb13e138 pipeline madness diff -r dc85ded6ceee -r e17abe477616 src/vkpipe.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vkpipe.c Sat Jun 23 07:47:49 2018 +0300 @@ -0,0 +1,225 @@ +#include +#include +#include +#include +#include "vkpipe.h" +#include "vku.h" + +int vku_init_pstate(struct vku_pstate *st) +{ + int i; + + memset(st, 0, sizeof *st); + + for(i=0; isdrstage[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; + } + st->vertinp.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; + st->inpasm.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; + st->vport.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; + st->rast.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; + st->msaa.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; + st->zstencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; + st->blend.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; + st->layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + + st->vport.pViewports = &st->vport_data; + st->vport.pScissors = &st->scissor_data; + st->vport.viewportCount = st->vport.scissorCount = 1; + + st->vport_data.minDepth = 0.0f; + st->vport_data.maxDepth = 1.0f; + + st->blend.logicOp = VK_LOGIC_OP_COPY; + st->blend.attachmentCount = 1; + st->blend.pAttachments = &st->atblend; + + /* set some reasonable defaults (just the non-zero ones) */ + vku_pstate_primitive(st, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); + vku_pstate_polygon_mode(st, VK_POLYGON_MODE_FILL); + vku_pstate_line_width(st, 1.0f); + vku_pstate_front_face(st, VK_FRONT_FACE_COUNTER_CLOCKWISE); + + return 0; +} + +void vku_pstate_shader(struct vku_pstate *st, VkShaderModule sdr, VkShaderStageFlagBits type) +{ + if(st->num_sdr_stages >= VKU_MAX_SDR_STAGES) { + fprintf(stderr, "vku_pstate_shader: too many shaders\n"); + abort(); + } + + st->sdrstage[st->num_sdr_stages].stage = type; + st->sdrstage[st->num_sdr_stages].module = sdr; + st->sdrstage[st->num_sdr_stages].pName = "main"; + ++st->num_sdr_stages; +} + +void vku_pstate_primitive(struct vku_pstate *st, VkPrimitiveTopology prim) +{ + st->inpasm.topology = prim; +} + +void vku_pstate_viewport(struct vku_pstate *st, int x, int y, int w, int h) +{ + st->vport_data.x = x; + st->vport_data.y = y; + st->vport_data.width = w; + st->vport_data.height = h; +} + +void vku_pstate_scissor(struct vku_pstate *st, int x, int y, int w, int h) +{ + st->scissor_data.offset.x = x; + st->scissor_data.offset.y = y; + st->scissor_data.extent.width = w; + st->scissor_data.extent.height = h; +} + +void vku_pstate_polygon_mode(struct vku_pstate *st, VkPolygonMode pmode) +{ + st->rast.polygonMode = pmode; +} + +void vku_pstate_line_width(struct vku_pstate *st, float w) +{ + st->rast.lineWidth = w; +} + +void vku_pstate_cull_mode(struct vku_pstate *st, VkCullModeFlagBits cull) +{ + st->rast.cullMode = cull; +} + +void vku_pstate_front_face(struct vku_pstate *st, VkFrontFace front) +{ + st->rast.frontFace = front; +} + +void vku_pstate_depth_bias(struct vku_pstate *st, float fslope, float fconst, float clamp) +{ + st->rast.depthBiasEnable = VK_TRUE; + st->rast.depthBiasSlopeFactor = fslope; + st->rast.depthBiasConstantFactor = fconst; + st->rast.depthBiasClamp = clamp; +} + +void vku_pstate_depth_test(struct vku_pstate *st, int enable) +{ + st->zstencil.depthTestEnable = enable ? VK_TRUE : VK_FALSE; +} + +void vku_pstate_depth_func(struct vku_pstate *st, VkCompareOp op) +{ + st->zstencil.depthCompareOp = op; +} + +void vku_pstate_depth_mask(struct vku_pstate *st, int zmask) +{ + st->zstencil.depthWriteEnable = zmask ? VK_TRUE : VK_FALSE; +} + +void vku_pstate_stencil_test(struct vku_pstate *st, int enable) +{ + st->zstencil.stencilTestEnable = enable ? VK_TRUE : VK_FALSE; +} + +void vku_pstate_stencil_func(struct vku_pstate *st, VkCompareOp op, int ref, unsigned int mask) +{ + st->zstencil.front.compareOp = st->zstencil.back.compareOp = op; + st->zstencil.front.reference = st->zstencil.back.reference = ref; + st->zstencil.front.writeMask = st->zstencil.back.compareMask = mask; +} + +void vku_pstate_stencil_op(struct vku_pstate *st, VkStencilOp sfail, VkStencilOp dfail, VkStencilOp pass) +{ + st->zstencil.front.failOp = st->zstencil.back.failOp = sfail; + st->zstencil.front.depthFailOp = st->zstencil.back.depthFailOp = dfail; + st->zstencil.front.passOp = st->zstencil.back.passOp = pass; +} + +void vku_pstate_stencil_mask(struct vku_pstate *st, unsigned int smask) +{ + st->zstencil.front.writeMask = st->zstencil.back.writeMask = smask; +} + +void vku_pstate_color_mask(struct vku_pstate *st, int rmask, int gmask, int bmask, int amask) +{ + unsigned int mask = 0; + + if(rmask) mask |= VK_COLOR_COMPONENT_R_BIT; + if(gmask) mask |= VK_COLOR_COMPONENT_G_BIT; + if(bmask) mask |= VK_COLOR_COMPONENT_B_BIT; + if(amask) mask |= VK_COLOR_COMPONENT_A_BIT; + + st->atblend.colorWriteMask = mask; +} + +void vku_pstate_blend_enable(struct vku_pstate *st, int enable) +{ + st->atblend.blendEnable = enable ? VK_TRUE : VK_FALSE; +} + +void vku_pstate_blend_func(struct vku_pstate *st, VkBlendFactor src, VkBlendFactor dst) +{ + st->atblend.srcColorBlendFactor = src; + st->atblend.dstColorBlendFactor = dst; + st->atblend.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; + st->atblend.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; + st->atblend.colorBlendOp = VK_BLEND_OP_ADD; + st->atblend.alphaBlendOp = VK_BLEND_OP_ADD; +} + + +VkPipeline vku_create_pipeline(struct vku_pstate *st) +{ + return 0; /* TODO */ +} + +VkShaderModule vku_load_shader(const char *fname) +{ + int size; + VkShaderModuleCreateInfo inf; + VkShaderModule sdr; + FILE *fp; + void *buf = 0; + + if(!(fp = fopen(fname, "rb"))) { + fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno)); + return 0; + } + fseek(fp, 0, SEEK_END); + size = ftell(fp); + rewind(fp); + + if(!(buf = malloc(size + 1))) { + fprintf(stderr, "vku_load_shader: failed to allocate buffer\n"); + goto err; + } + if(fread(buf, 1, size, fp) < size) { + fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname); + goto err; + } + + memset(&inf, 0, sizeof inf); + inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + inf.codeSize = size; + inf.pCode = buf; + + if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) { + fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname); + goto err; + } + return sdr; + +err: + fclose(fp); + free(buf); + return 0; +} + +void vku_destroy_shader(VkShaderModule sdr) +{ + vkDestroyShaderModule(vkdev, sdr, 0); +} diff -r dc85ded6ceee -r e17abe477616 src/vkpipe.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vkpipe.h Sat Jun 23 07:47:49 2018 +0300 @@ -0,0 +1,53 @@ +#ifndef VKPIPE_H_ +#define VKPIPE_H_ + +#include + +#define VKU_MAX_SDR_STAGES 8 + +struct vku_pstate { + VkPipelineShaderStageCreateInfo sdrstage[VKU_MAX_SDR_STAGES]; + int num_sdr_stages; + VkPipelineVertexInputStateCreateInfo vertinp; + VkPipelineInputAssemblyStateCreateInfo inpasm; + VkPipelineViewportStateCreateInfo vport; + VkPipelineRasterizationStateCreateInfo rast; + VkPipelineMultisampleStateCreateInfo msaa; + VkPipelineDepthStencilStateCreateInfo zstencil; + VkPipelineColorBlendStateCreateInfo blend; + /*VkPipelineDynamicStateCreateInfo dyn;*/ + VkPipelineLayoutCreateInfo layout; + + /* data needed by the structs above */ + VkViewport vport_data; + VkRect2D scissor_data; + VkPipelineColorBlendAttachmentState atblend; +}; + +int vku_init_pstate(struct vku_pstate *st); +void vku_pstate_shader(struct vku_pstate *st, VkShaderModule sdr, VkShaderStageFlagBits type); +void vku_pstate_primitive(struct vku_pstate *st, VkPrimitiveTopology prim); +void vku_pstate_viewport(struct vku_pstate *st, int x, int y, int w, int h); +void vku_pstate_scissor(struct vku_pstate *st, int x, int y, int w, int h); +void vku_pstate_polygon_mode(struct vku_pstate *st, VkPolygonMode pmode); +void vku_pstate_line_width(struct vku_pstate *st, float w); +void vku_pstate_cull_mode(struct vku_pstate *st, VkCullModeFlagBits cull); +void vku_pstate_front_face(struct vku_pstate *st, VkFrontFace front); +void vku_pstate_depth_bias(struct vku_pstate *st, float fslope, float fconst, float clamp); +void vku_pstate_depth_test(struct vku_pstate *st, int enable); +void vku_pstate_depth_func(struct vku_pstate *st, VkCompareOp op); +void vku_pstate_depth_mask(struct vku_pstate *st, int zmask); +void vku_pstate_stencil_test(struct vku_pstate *st, int enable); +void vku_pstate_stencil_func(struct vku_pstate *st, VkCompareOp op, int ref, unsigned int mask); +void vku_pstate_stencil_op(struct vku_pstate *st, VkStencilOp sfail, VkStencilOp dfail, VkStencilOp pass); +void vku_pstate_stencil_mask(struct vku_pstate *st, unsigned int smask); +void vku_pstate_color_mask(struct vku_pstate *st, int rmask, int gmask, int bmask, int amask); +void vku_pstate_blend_enable(struct vku_pstate *st, int enable); +void vku_pstate_blend_func(struct vku_pstate *st, VkBlendFactor src, VkBlendFactor dst); + +VkPipeline vku_create_pipeline(struct vku_pstate *st); + +VkShaderModule vku_load_shader(const char *fname); +void vku_destroy_shader(VkShaderModule sdr); + +#endif /* VKPIPE_H_ */ diff -r dc85ded6ceee -r e17abe477616 src/vksdr.c --- a/src/vksdr.c Fri Jun 22 15:29:30 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -#include -#include -#include -#include -#include "vksdr.h" -#include "vku.h" - -VkShaderModule vku_load_shader(const char *fname) -{ - int size; - VkShaderModuleCreateInfo inf; - VkShaderModule sdr; - FILE *fp; - void *buf = 0; - - if(!(fp = fopen(fname, "rb"))) { - fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno)); - return 0; - } - fseek(fp, 0, SEEK_END); - size = ftell(fp); - rewind(fp); - - if(!(buf = malloc(size + 1))) { - fprintf(stderr, "vku_load_shader: failed to allocate buffer\n"); - goto err; - } - if(fread(buf, 1, size, fp) < size) { - fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname); - goto err; - } - - memset(&inf, 0, sizeof inf); - inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - inf.codeSize = size; - inf.pCode = buf; - - if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) { - fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname); - goto err; - } - return sdr; - -err: - fclose(fp); - free(buf); - return 0; -} - -void vku_destroy_shader(VkShaderModule sdr) -{ - vkDestroyShaderModule(vkdev, sdr, 0); -} diff -r dc85ded6ceee -r e17abe477616 src/vksdr.h --- a/src/vksdr.h Fri Jun 22 15:29:30 2018 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -#ifndef VKSDR_H_ -#define VKSDR_H_ - -#include - -VkShaderModule vku_load_shader(const char *fname); -void vku_destroy_shader(VkShaderModule sdr); - -#endif /* VKSDR_H_ */ diff -r dc85ded6ceee -r e17abe477616 src/vku.c --- a/src/vku.c Fri Jun 22 15:29:30 2018 +0300 +++ b/src/vku.c Sat Jun 23 07:47:49 2018 +0300 @@ -425,6 +425,7 @@ vkCmdCopyBuffer(cmdbuf, src, dest, 1, ©); } + #ifdef VK_USE_PLATFORM_XLIB_KHR int vku_xlib_usable_visual(Display *dpy, VisualID vid) diff -r dc85ded6ceee -r e17abe477616 src/vku.h --- a/src/vku.h Fri Jun 22 15:29:30 2018 +0300 +++ b/src/vku.h Sat Jun 23 07:47:49 2018 +0300 @@ -16,6 +16,8 @@ VkImage *swapchain_images; int next_swapchain_image; +VkViewport vkvport; + struct vku_buffer { VkBuffer buf;