vulkan_test2

annotate 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
rev   line source
nuclear@8 1 #include <stdio.h>
nuclear@8 2 #include <stdlib.h>
nuclear@8 3 #include <string.h>
nuclear@8 4 #include <errno.h>
nuclear@12 5 #include "vkpipe.h"
nuclear@8 6 #include "vku.h"
nuclear@8 7
nuclear@12 8 int vku_init_pstate(struct vku_pstate *st)
nuclear@12 9 {
nuclear@12 10 int i;
nuclear@12 11
nuclear@12 12 memset(st, 0, sizeof *st);
nuclear@12 13
nuclear@12 14 for(i=0; i<VKU_MAX_SDR_STAGES; i++) {
nuclear@12 15 st->sdrstage[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
nuclear@12 16 }
nuclear@12 17 st->vertinp.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
nuclear@12 18 st->inpasm.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
nuclear@12 19 st->vport.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
nuclear@12 20 st->rast.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
nuclear@12 21 st->msaa.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
nuclear@12 22 st->zstencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
nuclear@12 23 st->blend.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
nuclear@12 24 st->layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
nuclear@12 25
nuclear@12 26 st->vport.pViewports = &st->vport_data;
nuclear@12 27 st->vport.pScissors = &st->scissor_data;
nuclear@12 28 st->vport.viewportCount = st->vport.scissorCount = 1;
nuclear@12 29
nuclear@12 30 st->vport_data.minDepth = 0.0f;
nuclear@12 31 st->vport_data.maxDepth = 1.0f;
nuclear@12 32
nuclear@12 33 st->blend.logicOp = VK_LOGIC_OP_COPY;
nuclear@12 34 st->blend.attachmentCount = 1;
nuclear@12 35 st->blend.pAttachments = &st->atblend;
nuclear@12 36
nuclear@12 37 /* set some reasonable defaults (just the non-zero ones) */
nuclear@12 38 vku_pstate_primitive(st, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
nuclear@12 39 vku_pstate_polygon_mode(st, VK_POLYGON_MODE_FILL);
nuclear@12 40 vku_pstate_line_width(st, 1.0f);
nuclear@12 41 vku_pstate_front_face(st, VK_FRONT_FACE_COUNTER_CLOCKWISE);
nuclear@12 42
nuclear@12 43 return 0;
nuclear@12 44 }
nuclear@12 45
nuclear@12 46 void vku_pstate_shader(struct vku_pstate *st, VkShaderModule sdr, VkShaderStageFlagBits type)
nuclear@12 47 {
nuclear@12 48 if(st->num_sdr_stages >= VKU_MAX_SDR_STAGES) {
nuclear@12 49 fprintf(stderr, "vku_pstate_shader: too many shaders\n");
nuclear@12 50 abort();
nuclear@12 51 }
nuclear@12 52
nuclear@12 53 st->sdrstage[st->num_sdr_stages].stage = type;
nuclear@12 54 st->sdrstage[st->num_sdr_stages].module = sdr;
nuclear@12 55 st->sdrstage[st->num_sdr_stages].pName = "main";
nuclear@12 56 ++st->num_sdr_stages;
nuclear@12 57 }
nuclear@12 58
nuclear@12 59 void vku_pstate_primitive(struct vku_pstate *st, VkPrimitiveTopology prim)
nuclear@12 60 {
nuclear@12 61 st->inpasm.topology = prim;
nuclear@12 62 }
nuclear@12 63
nuclear@12 64 void vku_pstate_viewport(struct vku_pstate *st, int x, int y, int w, int h)
nuclear@12 65 {
nuclear@12 66 st->vport_data.x = x;
nuclear@12 67 st->vport_data.y = y;
nuclear@12 68 st->vport_data.width = w;
nuclear@12 69 st->vport_data.height = h;
nuclear@12 70 }
nuclear@12 71
nuclear@12 72 void vku_pstate_scissor(struct vku_pstate *st, int x, int y, int w, int h)
nuclear@12 73 {
nuclear@12 74 st->scissor_data.offset.x = x;
nuclear@12 75 st->scissor_data.offset.y = y;
nuclear@12 76 st->scissor_data.extent.width = w;
nuclear@12 77 st->scissor_data.extent.height = h;
nuclear@12 78 }
nuclear@12 79
nuclear@12 80 void vku_pstate_polygon_mode(struct vku_pstate *st, VkPolygonMode pmode)
nuclear@12 81 {
nuclear@12 82 st->rast.polygonMode = pmode;
nuclear@12 83 }
nuclear@12 84
nuclear@12 85 void vku_pstate_line_width(struct vku_pstate *st, float w)
nuclear@12 86 {
nuclear@12 87 st->rast.lineWidth = w;
nuclear@12 88 }
nuclear@12 89
nuclear@12 90 void vku_pstate_cull_mode(struct vku_pstate *st, VkCullModeFlagBits cull)
nuclear@12 91 {
nuclear@12 92 st->rast.cullMode = cull;
nuclear@12 93 }
nuclear@12 94
nuclear@12 95 void vku_pstate_front_face(struct vku_pstate *st, VkFrontFace front)
nuclear@12 96 {
nuclear@12 97 st->rast.frontFace = front;
nuclear@12 98 }
nuclear@12 99
nuclear@12 100 void vku_pstate_depth_bias(struct vku_pstate *st, float fslope, float fconst, float clamp)
nuclear@12 101 {
nuclear@12 102 st->rast.depthBiasEnable = VK_TRUE;
nuclear@12 103 st->rast.depthBiasSlopeFactor = fslope;
nuclear@12 104 st->rast.depthBiasConstantFactor = fconst;
nuclear@12 105 st->rast.depthBiasClamp = clamp;
nuclear@12 106 }
nuclear@12 107
nuclear@12 108 void vku_pstate_depth_test(struct vku_pstate *st, int enable)
nuclear@12 109 {
nuclear@12 110 st->zstencil.depthTestEnable = enable ? VK_TRUE : VK_FALSE;
nuclear@12 111 }
nuclear@12 112
nuclear@12 113 void vku_pstate_depth_func(struct vku_pstate *st, VkCompareOp op)
nuclear@12 114 {
nuclear@12 115 st->zstencil.depthCompareOp = op;
nuclear@12 116 }
nuclear@12 117
nuclear@12 118 void vku_pstate_depth_mask(struct vku_pstate *st, int zmask)
nuclear@12 119 {
nuclear@12 120 st->zstencil.depthWriteEnable = zmask ? VK_TRUE : VK_FALSE;
nuclear@12 121 }
nuclear@12 122
nuclear@12 123 void vku_pstate_stencil_test(struct vku_pstate *st, int enable)
nuclear@12 124 {
nuclear@12 125 st->zstencil.stencilTestEnable = enable ? VK_TRUE : VK_FALSE;
nuclear@12 126 }
nuclear@12 127
nuclear@12 128 void vku_pstate_stencil_func(struct vku_pstate *st, VkCompareOp op, int ref, unsigned int mask)
nuclear@12 129 {
nuclear@12 130 st->zstencil.front.compareOp = st->zstencil.back.compareOp = op;
nuclear@12 131 st->zstencil.front.reference = st->zstencil.back.reference = ref;
nuclear@12 132 st->zstencil.front.writeMask = st->zstencil.back.compareMask = mask;
nuclear@12 133 }
nuclear@12 134
nuclear@12 135 void vku_pstate_stencil_op(struct vku_pstate *st, VkStencilOp sfail, VkStencilOp dfail, VkStencilOp pass)
nuclear@12 136 {
nuclear@12 137 st->zstencil.front.failOp = st->zstencil.back.failOp = sfail;
nuclear@12 138 st->zstencil.front.depthFailOp = st->zstencil.back.depthFailOp = dfail;
nuclear@12 139 st->zstencil.front.passOp = st->zstencil.back.passOp = pass;
nuclear@12 140 }
nuclear@12 141
nuclear@12 142 void vku_pstate_stencil_mask(struct vku_pstate *st, unsigned int smask)
nuclear@12 143 {
nuclear@12 144 st->zstencil.front.writeMask = st->zstencil.back.writeMask = smask;
nuclear@12 145 }
nuclear@12 146
nuclear@12 147 void vku_pstate_color_mask(struct vku_pstate *st, int rmask, int gmask, int bmask, int amask)
nuclear@12 148 {
nuclear@12 149 unsigned int mask = 0;
nuclear@12 150
nuclear@12 151 if(rmask) mask |= VK_COLOR_COMPONENT_R_BIT;
nuclear@12 152 if(gmask) mask |= VK_COLOR_COMPONENT_G_BIT;
nuclear@12 153 if(bmask) mask |= VK_COLOR_COMPONENT_B_BIT;
nuclear@12 154 if(amask) mask |= VK_COLOR_COMPONENT_A_BIT;
nuclear@12 155
nuclear@12 156 st->atblend.colorWriteMask = mask;
nuclear@12 157 }
nuclear@12 158
nuclear@12 159 void vku_pstate_blend_enable(struct vku_pstate *st, int enable)
nuclear@12 160 {
nuclear@12 161 st->atblend.blendEnable = enable ? VK_TRUE : VK_FALSE;
nuclear@12 162 }
nuclear@12 163
nuclear@12 164 void vku_pstate_blend_func(struct vku_pstate *st, VkBlendFactor src, VkBlendFactor dst)
nuclear@12 165 {
nuclear@12 166 st->atblend.srcColorBlendFactor = src;
nuclear@12 167 st->atblend.dstColorBlendFactor = dst;
nuclear@12 168 st->atblend.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
nuclear@12 169 st->atblend.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
nuclear@12 170 st->atblend.colorBlendOp = VK_BLEND_OP_ADD;
nuclear@12 171 st->atblend.alphaBlendOp = VK_BLEND_OP_ADD;
nuclear@12 172 }
nuclear@12 173
nuclear@12 174
nuclear@12 175 VkPipeline vku_create_pipeline(struct vku_pstate *st)
nuclear@12 176 {
nuclear@12 177 return 0; /* TODO */
nuclear@12 178 }
nuclear@12 179
nuclear@8 180 VkShaderModule vku_load_shader(const char *fname)
nuclear@8 181 {
nuclear@8 182 int size;
nuclear@8 183 VkShaderModuleCreateInfo inf;
nuclear@8 184 VkShaderModule sdr;
nuclear@8 185 FILE *fp;
nuclear@9 186 void *buf = 0;
nuclear@8 187
nuclear@8 188 if(!(fp = fopen(fname, "rb"))) {
nuclear@8 189 fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno));
nuclear@8 190 return 0;
nuclear@8 191 }
nuclear@8 192 fseek(fp, 0, SEEK_END);
nuclear@8 193 size = ftell(fp);
nuclear@8 194 rewind(fp);
nuclear@8 195
nuclear@8 196 if(!(buf = malloc(size + 1))) {
nuclear@8 197 fprintf(stderr, "vku_load_shader: failed to allocate buffer\n");
nuclear@8 198 goto err;
nuclear@8 199 }
nuclear@8 200 if(fread(buf, 1, size, fp) < size) {
nuclear@8 201 fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname);
nuclear@8 202 goto err;
nuclear@8 203 }
nuclear@8 204
nuclear@8 205 memset(&inf, 0, sizeof inf);
nuclear@8 206 inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
nuclear@8 207 inf.codeSize = size;
nuclear@9 208 inf.pCode = buf;
nuclear@8 209
nuclear@8 210 if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) {
nuclear@8 211 fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname);
nuclear@8 212 goto err;
nuclear@8 213 }
nuclear@8 214 return sdr;
nuclear@8 215
nuclear@8 216 err:
nuclear@8 217 fclose(fp);
nuclear@8 218 free(buf);
nuclear@8 219 return 0;
nuclear@8 220 }
nuclear@8 221
nuclear@8 222 void vku_destroy_shader(VkShaderModule sdr)
nuclear@8 223 {
nuclear@8 224 vkDestroyShaderModule(vkdev, sdr, 0);
nuclear@8 225 }