vulkan_test2

diff src/vkpipe.c @ 13:d34f84bede17

pipeline madness
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 25 Jun 2018 08:00:57 +0300
parents e17abe477616
children f8bd29f124a8
line diff
     1.1 --- a/src/vkpipe.c	Sat Jun 23 07:47:49 2018 +0300
     1.2 +++ b/src/vkpipe.c	Mon Jun 25 08:00:57 2018 +0300
     1.3 @@ -5,7 +5,7 @@
     1.4  #include "vkpipe.h"
     1.5  #include "vku.h"
     1.6  
     1.7 -int vku_init_pstate(struct vku_pstate *st)
     1.8 +int vku_init_pipeline(struct vku_pipeline *st)
     1.9  {
    1.10  	int i;
    1.11  
    1.12 @@ -21,7 +21,7 @@
    1.13  	st->msaa.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
    1.14  	st->zstencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
    1.15  	st->blend.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
    1.16 -	st->layout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
    1.17 +	st->lay.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
    1.18  
    1.19  	st->vport.pViewports = &st->vport_data;
    1.20  	st->vport.pScissors = &st->scissor_data;
    1.21 @@ -35,18 +35,18 @@
    1.22  	st->blend.pAttachments = &st->atblend;
    1.23  
    1.24  	/* set some reasonable defaults (just the non-zero ones) */
    1.25 -	vku_pstate_primitive(st, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
    1.26 -	vku_pstate_polygon_mode(st, VK_POLYGON_MODE_FILL);
    1.27 -	vku_pstate_line_width(st, 1.0f);
    1.28 -	vku_pstate_front_face(st, VK_FRONT_FACE_COUNTER_CLOCKWISE);
    1.29 +	vku_pipeline_primitive(st, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
    1.30 +	vku_pipeline_polygon_mode(st, VK_POLYGON_MODE_FILL);
    1.31 +	vku_pipeline_line_width(st, 1.0f);
    1.32 +	vku_pipeline_front_face(st, VK_FRONT_FACE_COUNTER_CLOCKWISE);
    1.33  
    1.34  	return 0;
    1.35  }
    1.36  
    1.37 -void vku_pstate_shader(struct vku_pstate *st, VkShaderModule sdr, VkShaderStageFlagBits type)
    1.38 +void vku_pipeline_shader(struct vku_pipeline *st, VkShaderModule sdr, VkShaderStageFlagBits type)
    1.39  {
    1.40  	if(st->num_sdr_stages >= VKU_MAX_SDR_STAGES) {
    1.41 -		fprintf(stderr, "vku_pstate_shader: too many shaders\n");
    1.42 +		fprintf(stderr, "vku_pipeline_shader: too many shaders\n");
    1.43  		abort();
    1.44  	}
    1.45  
    1.46 @@ -56,12 +56,12 @@
    1.47  	++st->num_sdr_stages;
    1.48  }
    1.49  
    1.50 -void vku_pstate_primitive(struct vku_pstate *st, VkPrimitiveTopology prim)
    1.51 +void vku_pipeline_primitive(struct vku_pipeline *st, VkPrimitiveTopology prim)
    1.52  {
    1.53  	st->inpasm.topology = prim;
    1.54  }
    1.55  
    1.56 -void vku_pstate_viewport(struct vku_pstate *st, int x, int y, int w, int h)
    1.57 +void vku_pipeline_viewport(struct vku_pipeline *st, int x, int y, int w, int h)
    1.58  {
    1.59  	st->vport_data.x = x;
    1.60  	st->vport_data.y = y;
    1.61 @@ -69,7 +69,7 @@
    1.62  	st->vport_data.height = h;
    1.63  }
    1.64  
    1.65 -void vku_pstate_scissor(struct vku_pstate *st, int x, int y, int w, int h)
    1.66 +void vku_pipeline_scissor(struct vku_pipeline *st, int x, int y, int w, int h)
    1.67  {
    1.68  	st->scissor_data.offset.x = x;
    1.69  	st->scissor_data.offset.y = y;
    1.70 @@ -77,27 +77,27 @@
    1.71  	st->scissor_data.extent.height = h;
    1.72  }
    1.73  
    1.74 -void vku_pstate_polygon_mode(struct vku_pstate *st, VkPolygonMode pmode)
    1.75 +void vku_pipeline_polygon_mode(struct vku_pipeline *st, VkPolygonMode pmode)
    1.76  {
    1.77  	st->rast.polygonMode = pmode;
    1.78  }
    1.79  
    1.80 -void vku_pstate_line_width(struct vku_pstate *st, float w)
    1.81 +void vku_pipeline_line_width(struct vku_pipeline *st, float w)
    1.82  {
    1.83  	st->rast.lineWidth = w;
    1.84  }
    1.85  
    1.86 -void vku_pstate_cull_mode(struct vku_pstate *st, VkCullModeFlagBits cull)
    1.87 +void vku_pipeline_cull_mode(struct vku_pipeline *st, VkCullModeFlagBits cull)
    1.88  {
    1.89  	st->rast.cullMode = cull;
    1.90  }
    1.91  
    1.92 -void vku_pstate_front_face(struct vku_pstate *st, VkFrontFace front)
    1.93 +void vku_pipeline_front_face(struct vku_pipeline *st, VkFrontFace front)
    1.94  {
    1.95  	st->rast.frontFace = front;
    1.96  }
    1.97  
    1.98 -void vku_pstate_depth_bias(struct vku_pstate *st, float fslope, float fconst, float clamp)
    1.99 +void vku_pipeline_depth_bias(struct vku_pipeline *st, float fslope, float fconst, float clamp)
   1.100  {
   1.101  	st->rast.depthBiasEnable = VK_TRUE;
   1.102  	st->rast.depthBiasSlopeFactor = fslope;
   1.103 @@ -105,46 +105,46 @@
   1.104  	st->rast.depthBiasClamp = clamp;
   1.105  }
   1.106  
   1.107 -void vku_pstate_depth_test(struct vku_pstate *st, int enable)
   1.108 +void vku_pipeline_depth_test(struct vku_pipeline *st, int enable)
   1.109  {
   1.110  	st->zstencil.depthTestEnable = enable ? VK_TRUE : VK_FALSE;
   1.111  }
   1.112  
   1.113 -void vku_pstate_depth_func(struct vku_pstate *st, VkCompareOp op)
   1.114 +void vku_pipeline_depth_func(struct vku_pipeline *st, VkCompareOp op)
   1.115  {
   1.116  	st->zstencil.depthCompareOp = op;
   1.117  }
   1.118  
   1.119 -void vku_pstate_depth_mask(struct vku_pstate *st, int zmask)
   1.120 +void vku_pipeline_depth_mask(struct vku_pipeline *st, int zmask)
   1.121  {
   1.122  	st->zstencil.depthWriteEnable = zmask ? VK_TRUE : VK_FALSE;
   1.123  }
   1.124  
   1.125 -void vku_pstate_stencil_test(struct vku_pstate *st, int enable)
   1.126 +void vku_pipeline_stencil_test(struct vku_pipeline *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 +void vku_pipeline_stencil_func(struct vku_pipeline *st, VkCompareOp op, int ref, unsigned int mask)
   1.133  {
   1.134  	st->zstencil.front.compareOp = st->zstencil.back.compareOp = op;
   1.135  	st->zstencil.front.reference = st->zstencil.back.reference = ref;
   1.136  	st->zstencil.front.writeMask = st->zstencil.back.compareMask = mask;
   1.137  }
   1.138  
   1.139 -void vku_pstate_stencil_op(struct vku_pstate *st, VkStencilOp sfail, VkStencilOp dfail, VkStencilOp pass)
   1.140 +void vku_pipeline_stencil_op(struct vku_pipeline *st, VkStencilOp sfail, VkStencilOp dfail, VkStencilOp pass)
   1.141  {
   1.142  	st->zstencil.front.failOp = st->zstencil.back.failOp = sfail;
   1.143  	st->zstencil.front.depthFailOp = st->zstencil.back.depthFailOp = dfail;
   1.144  	st->zstencil.front.passOp = st->zstencil.back.passOp = pass;
   1.145  }
   1.146  
   1.147 -void vku_pstate_stencil_mask(struct vku_pstate *st, unsigned int smask)
   1.148 +void vku_pipeline_stencil_mask(struct vku_pipeline *st, unsigned int smask)
   1.149  {
   1.150  	st->zstencil.front.writeMask = st->zstencil.back.writeMask = smask;
   1.151  }
   1.152  
   1.153 -void vku_pstate_color_mask(struct vku_pstate *st, int rmask, int gmask, int bmask, int amask)
   1.154 +void vku_pipeline_color_mask(struct vku_pipeline *st, int rmask, int gmask, int bmask, int amask)
   1.155  {
   1.156  	unsigned int mask = 0;
   1.157  
   1.158 @@ -156,12 +156,12 @@
   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 +void vku_pipeline_blend_enable(struct vku_pipeline *st, int enable)
   1.164  {
   1.165  	st->atblend.blendEnable = enable ? VK_TRUE : VK_FALSE;
   1.166  }
   1.167  
   1.168 -void vku_pstate_blend_func(struct vku_pstate *st, VkBlendFactor src, VkBlendFactor dst)
   1.169 +void vku_pipeline_blend_func(struct vku_pipeline *st, VkBlendFactor src, VkBlendFactor dst)
   1.170  {
   1.171  	st->atblend.srcColorBlendFactor = src;
   1.172  	st->atblend.dstColorBlendFactor = dst;
   1.173 @@ -171,10 +171,52 @@
   1.174  	st->atblend.alphaBlendOp = VK_BLEND_OP_ADD;
   1.175  }
   1.176  
   1.177 +void vku_pipeline_renderpass(struct vku_pipeline *st, VkRenderPass rpass)
   1.178 +{
   1.179 +	st->rpass = rpass;
   1.180 +}
   1.181  
   1.182 -VkPipeline vku_create_pipeline(struct vku_pstate *st)
   1.183 +
   1.184 +VkPipeline vku_create_pipeline(struct vku_pipeline *st)
   1.185  {
   1.186 -	return 0;	/* TODO */
   1.187 +	VkGraphicsPipelineCreateInfo pinf;
   1.188 +
   1.189 +	if(vkCreatePipelineLayout(vkdev, &st->lay, 0, &st->layout) != 0) {
   1.190 +		fprintf(stderr, "vku_create_pipeline: failed to create layout\n");
   1.191 +		return 0;
   1.192 +	}
   1.193 +
   1.194 +	memset(&pinf, 0, sizeof pinf);
   1.195 +	pinf.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
   1.196 +	pinf.stageCount = st->num_sdr_stages;
   1.197 +	pinf.pStages = st->sdrstage;
   1.198 +	pinf.pVertexInputState = &st->vertinp;
   1.199 +	pinf.pInputAssemblyState = &st->inpasm;
   1.200 +	pinf.pViewportState = &st->vport;
   1.201 +	pinf.pRasterizationState = &st->rast;
   1.202 +	pinf.pMultisampleState = &st->msaa;
   1.203 +	//pinf.pDepthStencilState = &st->zstencil;
   1.204 +	pinf.pColorBlendState = &st->blend;
   1.205 +
   1.206 +	pinf.layout = st->layout;
   1.207 +	pinf.renderPass = st->rpass;
   1.208 +
   1.209 +	if(vkCreateGraphicsPipelines(vkdev, 0, 1, &pinf, 0, &st->pipeline) != 0) {
   1.210 +		fprintf(stderr, "vku_create_pipeline failed\n");
   1.211 +		vkDestroyPipelineLayout(vkdev, st->layout, 0);
   1.212 +		return 0;
   1.213 +	}
   1.214 +	return st->pipeline;
   1.215 +}
   1.216 +
   1.217 +void vku_destroy_pipeline(struct vku_pipeline *p)
   1.218 +{
   1.219 +	if(p->layout) {
   1.220 +		vkDestroyPipelineLayout(vkdev, p->layout, 0);
   1.221 +	}
   1.222 +	if(p->pipeline) {
   1.223 +		vkDestroyPipeline(vkdev, p->pipeline, 0);
   1.224 +	}
   1.225  }
   1.226  
   1.227  VkShaderModule vku_load_shader(const char *fname)