vulkan_test2

view src/vkpipe.c @ 17:f8bd29f124a8

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 27 Jun 2018 01:57:55 +0300
parents d34f84bede17
children
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->msaa.rasterizationSamples = 1;
35 st->blend.logicOp = VK_LOGIC_OP_COPY;
36 st->blend.attachmentCount = 1;
37 st->blend.pAttachments = &st->atblend;
39 /* set some reasonable defaults (just the non-zero ones) */
40 vku_pipeline_primitive(st, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
41 vku_pipeline_polygon_mode(st, VK_POLYGON_MODE_FILL);
42 vku_pipeline_line_width(st, 1.0f);
43 vku_pipeline_front_face(st, VK_FRONT_FACE_COUNTER_CLOCKWISE);
45 return 0;
46 }
48 void vku_pipeline_shader(struct vku_pipeline *st, VkShaderModule sdr, VkShaderStageFlagBits type)
49 {
50 if(st->num_sdr_stages >= VKU_MAX_SDR_STAGES) {
51 fprintf(stderr, "vku_pipeline_shader: too many shaders\n");
52 abort();
53 }
55 st->sdrstage[st->num_sdr_stages].stage = type;
56 st->sdrstage[st->num_sdr_stages].module = sdr;
57 st->sdrstage[st->num_sdr_stages].pName = "main";
58 ++st->num_sdr_stages;
59 }
61 void vku_pipeline_primitive(struct vku_pipeline *st, VkPrimitiveTopology prim)
62 {
63 st->inpasm.topology = prim;
64 }
66 void vku_pipeline_viewport(struct vku_pipeline *st, int x, int y, int w, int h)
67 {
68 st->vport_data.x = x;
69 st->vport_data.y = y;
70 st->vport_data.width = w;
71 st->vport_data.height = h;
72 }
74 void vku_pipeline_scissor(struct vku_pipeline *st, int x, int y, int w, int h)
75 {
76 st->scissor_data.offset.x = x;
77 st->scissor_data.offset.y = y;
78 st->scissor_data.extent.width = w;
79 st->scissor_data.extent.height = h;
80 }
82 void vku_pipeline_polygon_mode(struct vku_pipeline *st, VkPolygonMode pmode)
83 {
84 st->rast.polygonMode = pmode;
85 }
87 void vku_pipeline_line_width(struct vku_pipeline *st, float w)
88 {
89 st->rast.lineWidth = w;
90 }
92 void vku_pipeline_cull_mode(struct vku_pipeline *st, VkCullModeFlagBits cull)
93 {
94 st->rast.cullMode = cull;
95 }
97 void vku_pipeline_front_face(struct vku_pipeline *st, VkFrontFace front)
98 {
99 st->rast.frontFace = front;
100 }
102 void vku_pipeline_depth_bias(struct vku_pipeline *st, float fslope, float fconst, float clamp)
103 {
104 st->rast.depthBiasEnable = VK_TRUE;
105 st->rast.depthBiasSlopeFactor = fslope;
106 st->rast.depthBiasConstantFactor = fconst;
107 st->rast.depthBiasClamp = clamp;
108 }
110 void vku_pipeline_depth_test(struct vku_pipeline *st, int enable)
111 {
112 st->zstencil.depthTestEnable = enable ? VK_TRUE : VK_FALSE;
113 }
115 void vku_pipeline_depth_func(struct vku_pipeline *st, VkCompareOp op)
116 {
117 st->zstencil.depthCompareOp = op;
118 }
120 void vku_pipeline_depth_mask(struct vku_pipeline *st, int zmask)
121 {
122 st->zstencil.depthWriteEnable = zmask ? VK_TRUE : VK_FALSE;
123 }
125 void vku_pipeline_stencil_test(struct vku_pipeline *st, int enable)
126 {
127 st->zstencil.stencilTestEnable = enable ? VK_TRUE : VK_FALSE;
128 }
130 void vku_pipeline_stencil_func(struct vku_pipeline *st, VkCompareOp op, int ref, unsigned int mask)
131 {
132 st->zstencil.front.compareOp = st->zstencil.back.compareOp = op;
133 st->zstencil.front.reference = st->zstencil.back.reference = ref;
134 st->zstencil.front.writeMask = st->zstencil.back.compareMask = mask;
135 }
137 void vku_pipeline_stencil_op(struct vku_pipeline *st, VkStencilOp sfail, VkStencilOp dfail, VkStencilOp pass)
138 {
139 st->zstencil.front.failOp = st->zstencil.back.failOp = sfail;
140 st->zstencil.front.depthFailOp = st->zstencil.back.depthFailOp = dfail;
141 st->zstencil.front.passOp = st->zstencil.back.passOp = pass;
142 }
144 void vku_pipeline_stencil_mask(struct vku_pipeline *st, unsigned int smask)
145 {
146 st->zstencil.front.writeMask = st->zstencil.back.writeMask = smask;
147 }
149 void vku_pipeline_color_mask(struct vku_pipeline *st, int rmask, int gmask, int bmask, int amask)
150 {
151 unsigned int mask = 0;
153 if(rmask) mask |= VK_COLOR_COMPONENT_R_BIT;
154 if(gmask) mask |= VK_COLOR_COMPONENT_G_BIT;
155 if(bmask) mask |= VK_COLOR_COMPONENT_B_BIT;
156 if(amask) mask |= VK_COLOR_COMPONENT_A_BIT;
158 st->atblend.colorWriteMask = mask;
159 }
161 void vku_pipeline_blend_enable(struct vku_pipeline *st, int enable)
162 {
163 st->atblend.blendEnable = enable ? VK_TRUE : VK_FALSE;
164 }
166 void vku_pipeline_blend_func(struct vku_pipeline *st, VkBlendFactor src, VkBlendFactor dst)
167 {
168 st->atblend.srcColorBlendFactor = src;
169 st->atblend.dstColorBlendFactor = dst;
170 st->atblend.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
171 st->atblend.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
172 st->atblend.colorBlendOp = VK_BLEND_OP_ADD;
173 st->atblend.alphaBlendOp = VK_BLEND_OP_ADD;
174 }
176 void vku_pipeline_renderpass(struct vku_pipeline *st, VkRenderPass rpass)
177 {
178 st->rpass = rpass;
179 }
182 VkPipeline vku_create_pipeline(struct vku_pipeline *st)
183 {
184 VkGraphicsPipelineCreateInfo pinf;
186 if(vkCreatePipelineLayout(vkdev, &st->lay, 0, &st->layout) != 0) {
187 fprintf(stderr, "vku_create_pipeline: failed to create layout\n");
188 return 0;
189 }
191 memset(&pinf, 0, sizeof pinf);
192 pinf.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
193 pinf.stageCount = st->num_sdr_stages;
194 pinf.pStages = st->sdrstage;
195 pinf.pVertexInputState = &st->vertinp;
196 pinf.pInputAssemblyState = &st->inpasm;
197 pinf.pViewportState = &st->vport;
198 pinf.pRasterizationState = &st->rast;
199 pinf.pMultisampleState = &st->msaa;
200 //pinf.pDepthStencilState = &st->zstencil;
201 pinf.pColorBlendState = &st->blend;
203 pinf.layout = st->layout;
204 pinf.renderPass = st->rpass;
206 if(vkCreateGraphicsPipelines(vkdev, 0, 1, &pinf, 0, &st->pipeline) != 0) {
207 fprintf(stderr, "vku_create_pipeline failed\n");
208 vkDestroyPipelineLayout(vkdev, st->layout, 0);
209 return 0;
210 }
211 return st->pipeline;
212 }
214 void vku_destroy_pipeline(struct vku_pipeline *p)
215 {
216 if(p->layout) {
217 vkDestroyPipelineLayout(vkdev, p->layout, 0);
218 }
219 if(p->pipeline) {
220 vkDestroyPipeline(vkdev, p->pipeline, 0);
221 }
222 }
224 VkShaderModule vku_load_shader(const char *fname)
225 {
226 int size;
227 VkShaderModuleCreateInfo inf;
228 VkShaderModule sdr;
229 FILE *fp;
230 void *buf = 0;
232 if(!(fp = fopen(fname, "rb"))) {
233 fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno));
234 return 0;
235 }
236 fseek(fp, 0, SEEK_END);
237 size = ftell(fp);
238 rewind(fp);
240 if(!(buf = malloc(size + 1))) {
241 fprintf(stderr, "vku_load_shader: failed to allocate buffer\n");
242 goto err;
243 }
244 if(fread(buf, 1, size, fp) < size) {
245 fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname);
246 goto err;
247 }
249 memset(&inf, 0, sizeof inf);
250 inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
251 inf.codeSize = size;
252 inf.pCode = buf;
254 if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) {
255 fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname);
256 goto err;
257 }
258 return sdr;
260 err:
261 fclose(fp);
262 free(buf);
263 return 0;
264 }
266 void vku_destroy_shader(VkShaderModule sdr)
267 {
268 vkDestroyShaderModule(vkdev, sdr, 0);
269 }