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