vulkan_test2

view src/vkpipe.c @ 14:9fb6c24691ea

this shit never ends...
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Jun 2018 07:20:03 +0300
parents e17abe477616
children f8bd29f124a8
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "vkpipe.h"
6 #include "vku.h"
8 int vku_init_pipeline(struct vku_pipeline *st)
9 {
10 int i;
12 memset(st, 0, sizeof *st);
14 for(i=0; i<VKU_MAX_SDR_STAGES; i++) {
15 st->sdrstage[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
16 }
17 st->vertinp.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
18 st->inpasm.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
19 st->vport.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
20 st->rast.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
21 st->msaa.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
22 st->zstencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
23 st->blend.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
24 st->lay.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
26 st->vport.pViewports = &st->vport_data;
27 st->vport.pScissors = &st->scissor_data;
28 st->vport.viewportCount = st->vport.scissorCount = 1;
30 st->vport_data.minDepth = 0.0f;
31 st->vport_data.maxDepth = 1.0f;
33 st->blend.logicOp = VK_LOGIC_OP_COPY;
34 st->blend.attachmentCount = 1;
35 st->blend.pAttachments = &st->atblend;
37 /* set some reasonable defaults (just the non-zero ones) */
38 vku_pipeline_primitive(st, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
39 vku_pipeline_polygon_mode(st, VK_POLYGON_MODE_FILL);
40 vku_pipeline_line_width(st, 1.0f);
41 vku_pipeline_front_face(st, VK_FRONT_FACE_COUNTER_CLOCKWISE);
43 return 0;
44 }
46 void vku_pipeline_shader(struct vku_pipeline *st, VkShaderModule sdr, VkShaderStageFlagBits type)
47 {
48 if(st->num_sdr_stages >= VKU_MAX_SDR_STAGES) {
49 fprintf(stderr, "vku_pipeline_shader: too many shaders\n");
50 abort();
51 }
53 st->sdrstage[st->num_sdr_stages].stage = type;
54 st->sdrstage[st->num_sdr_stages].module = sdr;
55 st->sdrstage[st->num_sdr_stages].pName = "main";
56 ++st->num_sdr_stages;
57 }
59 void vku_pipeline_primitive(struct vku_pipeline *st, VkPrimitiveTopology prim)
60 {
61 st->inpasm.topology = prim;
62 }
64 void vku_pipeline_viewport(struct vku_pipeline *st, int x, int y, int w, int h)
65 {
66 st->vport_data.x = x;
67 st->vport_data.y = y;
68 st->vport_data.width = w;
69 st->vport_data.height = h;
70 }
72 void vku_pipeline_scissor(struct vku_pipeline *st, int x, int y, int w, int h)
73 {
74 st->scissor_data.offset.x = x;
75 st->scissor_data.offset.y = y;
76 st->scissor_data.extent.width = w;
77 st->scissor_data.extent.height = h;
78 }
80 void vku_pipeline_polygon_mode(struct vku_pipeline *st, VkPolygonMode pmode)
81 {
82 st->rast.polygonMode = pmode;
83 }
85 void vku_pipeline_line_width(struct vku_pipeline *st, float w)
86 {
87 st->rast.lineWidth = w;
88 }
90 void vku_pipeline_cull_mode(struct vku_pipeline *st, VkCullModeFlagBits cull)
91 {
92 st->rast.cullMode = cull;
93 }
95 void vku_pipeline_front_face(struct vku_pipeline *st, VkFrontFace front)
96 {
97 st->rast.frontFace = front;
98 }
100 void vku_pipeline_depth_bias(struct vku_pipeline *st, float fslope, float fconst, float clamp)
101 {
102 st->rast.depthBiasEnable = VK_TRUE;
103 st->rast.depthBiasSlopeFactor = fslope;
104 st->rast.depthBiasConstantFactor = fconst;
105 st->rast.depthBiasClamp = clamp;
106 }
108 void vku_pipeline_depth_test(struct vku_pipeline *st, int enable)
109 {
110 st->zstencil.depthTestEnable = enable ? VK_TRUE : VK_FALSE;
111 }
113 void vku_pipeline_depth_func(struct vku_pipeline *st, VkCompareOp op)
114 {
115 st->zstencil.depthCompareOp = op;
116 }
118 void vku_pipeline_depth_mask(struct vku_pipeline *st, int zmask)
119 {
120 st->zstencil.depthWriteEnable = zmask ? VK_TRUE : VK_FALSE;
121 }
123 void vku_pipeline_stencil_test(struct vku_pipeline *st, int enable)
124 {
125 st->zstencil.stencilTestEnable = enable ? VK_TRUE : VK_FALSE;
126 }
128 void vku_pipeline_stencil_func(struct vku_pipeline *st, VkCompareOp op, int ref, unsigned int mask)
129 {
130 st->zstencil.front.compareOp = st->zstencil.back.compareOp = op;
131 st->zstencil.front.reference = st->zstencil.back.reference = ref;
132 st->zstencil.front.writeMask = st->zstencil.back.compareMask = mask;
133 }
135 void vku_pipeline_stencil_op(struct vku_pipeline *st, VkStencilOp sfail, VkStencilOp dfail, VkStencilOp pass)
136 {
137 st->zstencil.front.failOp = st->zstencil.back.failOp = sfail;
138 st->zstencil.front.depthFailOp = st->zstencil.back.depthFailOp = dfail;
139 st->zstencil.front.passOp = st->zstencil.back.passOp = pass;
140 }
142 void vku_pipeline_stencil_mask(struct vku_pipeline *st, unsigned int smask)
143 {
144 st->zstencil.front.writeMask = st->zstencil.back.writeMask = smask;
145 }
147 void vku_pipeline_color_mask(struct vku_pipeline *st, int rmask, int gmask, int bmask, int amask)
148 {
149 unsigned int mask = 0;
151 if(rmask) mask |= VK_COLOR_COMPONENT_R_BIT;
152 if(gmask) mask |= VK_COLOR_COMPONENT_G_BIT;
153 if(bmask) mask |= VK_COLOR_COMPONENT_B_BIT;
154 if(amask) mask |= VK_COLOR_COMPONENT_A_BIT;
156 st->atblend.colorWriteMask = mask;
157 }
159 void vku_pipeline_blend_enable(struct vku_pipeline *st, int enable)
160 {
161 st->atblend.blendEnable = enable ? VK_TRUE : VK_FALSE;
162 }
164 void vku_pipeline_blend_func(struct vku_pipeline *st, VkBlendFactor src, VkBlendFactor dst)
165 {
166 st->atblend.srcColorBlendFactor = src;
167 st->atblend.dstColorBlendFactor = dst;
168 st->atblend.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
169 st->atblend.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
170 st->atblend.colorBlendOp = VK_BLEND_OP_ADD;
171 st->atblend.alphaBlendOp = VK_BLEND_OP_ADD;
172 }
174 void vku_pipeline_renderpass(struct vku_pipeline *st, VkRenderPass rpass)
175 {
176 st->rpass = rpass;
177 }
180 VkPipeline vku_create_pipeline(struct vku_pipeline *st)
181 {
182 VkGraphicsPipelineCreateInfo pinf;
184 if(vkCreatePipelineLayout(vkdev, &st->lay, 0, &st->layout) != 0) {
185 fprintf(stderr, "vku_create_pipeline: failed to create layout\n");
186 return 0;
187 }
189 memset(&pinf, 0, sizeof pinf);
190 pinf.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
191 pinf.stageCount = st->num_sdr_stages;
192 pinf.pStages = st->sdrstage;
193 pinf.pVertexInputState = &st->vertinp;
194 pinf.pInputAssemblyState = &st->inpasm;
195 pinf.pViewportState = &st->vport;
196 pinf.pRasterizationState = &st->rast;
197 pinf.pMultisampleState = &st->msaa;
198 //pinf.pDepthStencilState = &st->zstencil;
199 pinf.pColorBlendState = &st->blend;
201 pinf.layout = st->layout;
202 pinf.renderPass = st->rpass;
204 if(vkCreateGraphicsPipelines(vkdev, 0, 1, &pinf, 0, &st->pipeline) != 0) {
205 fprintf(stderr, "vku_create_pipeline failed\n");
206 vkDestroyPipelineLayout(vkdev, st->layout, 0);
207 return 0;
208 }
209 return st->pipeline;
210 }
212 void vku_destroy_pipeline(struct vku_pipeline *p)
213 {
214 if(p->layout) {
215 vkDestroyPipelineLayout(vkdev, p->layout, 0);
216 }
217 if(p->pipeline) {
218 vkDestroyPipeline(vkdev, p->pipeline, 0);
219 }
220 }
222 VkShaderModule vku_load_shader(const char *fname)
223 {
224 int size;
225 VkShaderModuleCreateInfo inf;
226 VkShaderModule sdr;
227 FILE *fp;
228 void *buf = 0;
230 if(!(fp = fopen(fname, "rb"))) {
231 fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno));
232 return 0;
233 }
234 fseek(fp, 0, SEEK_END);
235 size = ftell(fp);
236 rewind(fp);
238 if(!(buf = malloc(size + 1))) {
239 fprintf(stderr, "vku_load_shader: failed to allocate buffer\n");
240 goto err;
241 }
242 if(fread(buf, 1, size, fp) < size) {
243 fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname);
244 goto err;
245 }
247 memset(&inf, 0, sizeof inf);
248 inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
249 inf.codeSize = size;
250 inf.pCode = buf;
252 if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) {
253 fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname);
254 goto err;
255 }
256 return sdr;
258 err:
259 fclose(fp);
260 free(buf);
261 return 0;
262 }
264 void vku_destroy_shader(VkShaderModule sdr)
265 {
266 vkDestroyShaderModule(vkdev, sdr, 0);
267 }