vulkan_test2

view 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
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_pstate(struct vku_pstate *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->layout.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_pstate_primitive(st, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
39 vku_pstate_polygon_mode(st, VK_POLYGON_MODE_FILL);
40 vku_pstate_line_width(st, 1.0f);
41 vku_pstate_front_face(st, VK_FRONT_FACE_COUNTER_CLOCKWISE);
43 return 0;
44 }
46 void vku_pstate_shader(struct vku_pstate *st, VkShaderModule sdr, VkShaderStageFlagBits type)
47 {
48 if(st->num_sdr_stages >= VKU_MAX_SDR_STAGES) {
49 fprintf(stderr, "vku_pstate_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_pstate_primitive(struct vku_pstate *st, VkPrimitiveTopology prim)
60 {
61 st->inpasm.topology = prim;
62 }
64 void vku_pstate_viewport(struct vku_pstate *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_pstate_scissor(struct vku_pstate *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_pstate_polygon_mode(struct vku_pstate *st, VkPolygonMode pmode)
81 {
82 st->rast.polygonMode = pmode;
83 }
85 void vku_pstate_line_width(struct vku_pstate *st, float w)
86 {
87 st->rast.lineWidth = w;
88 }
90 void vku_pstate_cull_mode(struct vku_pstate *st, VkCullModeFlagBits cull)
91 {
92 st->rast.cullMode = cull;
93 }
95 void vku_pstate_front_face(struct vku_pstate *st, VkFrontFace front)
96 {
97 st->rast.frontFace = front;
98 }
100 void vku_pstate_depth_bias(struct vku_pstate *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_pstate_depth_test(struct vku_pstate *st, int enable)
109 {
110 st->zstencil.depthTestEnable = enable ? VK_TRUE : VK_FALSE;
111 }
113 void vku_pstate_depth_func(struct vku_pstate *st, VkCompareOp op)
114 {
115 st->zstencil.depthCompareOp = op;
116 }
118 void vku_pstate_depth_mask(struct vku_pstate *st, int zmask)
119 {
120 st->zstencil.depthWriteEnable = zmask ? VK_TRUE : VK_FALSE;
121 }
123 void vku_pstate_stencil_test(struct vku_pstate *st, int enable)
124 {
125 st->zstencil.stencilTestEnable = enable ? VK_TRUE : VK_FALSE;
126 }
128 void vku_pstate_stencil_func(struct vku_pstate *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_pstate_stencil_op(struct vku_pstate *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_pstate_stencil_mask(struct vku_pstate *st, unsigned int smask)
143 {
144 st->zstencil.front.writeMask = st->zstencil.back.writeMask = smask;
145 }
147 void vku_pstate_color_mask(struct vku_pstate *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_pstate_blend_enable(struct vku_pstate *st, int enable)
160 {
161 st->atblend.blendEnable = enable ? VK_TRUE : VK_FALSE;
162 }
164 void vku_pstate_blend_func(struct vku_pstate *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 }
175 VkPipeline vku_create_pipeline(struct vku_pstate *st)
176 {
177 return 0; /* TODO */
178 }
180 VkShaderModule vku_load_shader(const char *fname)
181 {
182 int size;
183 VkShaderModuleCreateInfo inf;
184 VkShaderModule sdr;
185 FILE *fp;
186 void *buf = 0;
188 if(!(fp = fopen(fname, "rb"))) {
189 fprintf(stderr, "vku_load_shader: failed to load %s: %s\n", fname, strerror(errno));
190 return 0;
191 }
192 fseek(fp, 0, SEEK_END);
193 size = ftell(fp);
194 rewind(fp);
196 if(!(buf = malloc(size + 1))) {
197 fprintf(stderr, "vku_load_shader: failed to allocate buffer\n");
198 goto err;
199 }
200 if(fread(buf, 1, size, fp) < size) {
201 fprintf(stderr, "vku_load_shader: unexpected end of file while reading: %s\n", fname);
202 goto err;
203 }
205 memset(&inf, 0, sizeof inf);
206 inf.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
207 inf.codeSize = size;
208 inf.pCode = buf;
210 if(vkCreateShaderModule(vkdev, &inf, 0, &sdr) != 0) {
211 fprintf(stderr, "vku_load_shader: failed to create shader: %s\n", fname);
212 goto err;
213 }
214 return sdr;
216 err:
217 fclose(fp);
218 free(buf);
219 return 0;
220 }
222 void vku_destroy_shader(VkShaderModule sdr)
223 {
224 vkDestroyShaderModule(vkdev, sdr, 0);
225 }