extern Mat4 mprojection;
extern double time_sec;
-#endif // GLOBAL_H_
\ No newline at end of file
+#endif // GLOBAL_H_
{
//debug
// if(!(nprog = sdr_man->create_shader_program("debug.v.glsl", "debug.f.glsl"))) {
- // fprintf(stderr, "Failed to load debug shaders.\n");
+ // fprin
+ // tf(stderr, "Failed to load debug shaders.\n");
// }
vbuf = gfx_create_uniform_buffer();
static VkImage *images;
static VkImageView *iviews;
static uint32_t num_images;
-//renderpass
-static VkRenderPass rpass;
static VkFramebuffer fbs[2];
//semaphores-drawing-presentation
static uint32_t curr_img; // current sc image
rinf.subpassCount = 1;
rinf.pSubpasses = &sd;
- if(vkCreateRenderPass(vk_device, &rinf, 0, &rpass) != VK_SUCCESS) {
- fprintf(stderr, "Failed to create rpass.\n");
+ if(vkCreateRenderPass(vk_device, &rinf, 0, &vk_renderpass) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create render pass.\n");
return false;
}
VkFramebufferCreateInfo fbinf;
memset(&fbinf, 0, sizeof fbinf);
fbinf.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
- fbinf.renderPass = rpass;
+ fbinf.renderPass = vk_renderpass;
fbinf.attachmentCount = 2;
fbinf.pAttachments = fb_att;
fbinf.width = win_w;
VkRenderPassBeginInfo rbinf;
memset(&rbinf, 0, sizeof rbinf);
rbinf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
- rbinf.renderPass = rpass;
+ rbinf.renderPass = vk_renderpass;
rbinf.framebuffer = fbs[i];
rbinf.renderArea.extent.width = win_w;
rbinf.renderArea.extent.height = win_h;
--- /dev/null
+#include <stdio.h>
+#include <string.h>
+
+#include "vkutil.h"
+#include "vkutil-pipeline.h"
+
+VkuPipelineGenerator::VkuPipelineGenerator()
+{
+ memset(sdri, 0, sizeof sdri);
+ sdri[0].sType = sdri[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
+ sdri[0].pName = sdri[1].pName = "main";
+ sdri[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
+ sdri[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
+
+ memset(&verti, 0, sizeof verti);
+ verti.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
+
+ memset(&asmi, 0, sizeof asmi);
+ asmi.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
+ asmi.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
+
+ VkViewport viewport = {0, 0, 0, 0};
+ VkRect2D scissor = {0, 0};
+ memset(&viewpi, 0, sizeof viewpi);
+ viewpi.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
+ viewpi.viewportCount = 1;
+ viewpi.pViewports = &viewport;
+ viewpi.scissorCount = 1;
+ viewpi.pScissors = &scissor;
+
+ memset(&rasti, 0, sizeof rasti);
+ rasti.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
+ rasti.polygonMode = VK_POLYGON_MODE_FILL;
+ rasti.cullMode = VK_CULL_MODE_BACK_BIT;
+ rasti.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
+
+ memset(&multi, 0, sizeof multi);
+ multi.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
+
+ memset(&depthi, 0, sizeof depthi);
+ depthi.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
+ depthi.depthTestEnable = VK_TRUE;
+ depthi.depthWriteEnable = VK_TRUE;
+ depthi.depthCompareOp = VK_COMPARE_OP_LESS;
+
+ memset(&cblendi, 0, sizeof cblendi);
+ cblendi.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
+}
+
+VkuPipelineGenerator::~VkuPipelineGenerator()
+{
+}
+
+VkPipeline VkuPipelineGenerator::generate(VkuDynState dyn_flags) const
+{
+ VkPipelineDynamicStateCreateInfo dyni;
+ memset(&dyni, 0, sizeof dyni);
+ dyni.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
+
+ std::vector<VkDynamicState> dyn_states;
+ if(dyn_flags & VKU_DS_VIEWPORT) {
+ dyn_states.push_back(VK_DYNAMIC_STATE_VIEWPORT);
+ }
+ if(dyn_flags & VKU_DS_SCISSOR) {
+ dyn_states.push_back(VK_DYNAMIC_STATE_SCISSOR);
+ }
+ if(dyn_flags & VKU_DS_BLEND) {
+ dyn_states.push_back(VK_DYNAMIC_STATE_BLEND_CONSTANTS);
+ }
+
+ dyni.dynamicStateCount = dyn_states.size();
+ dyni.pDynamicStates = dyn_states.data();
+
+ VkGraphicsPipelineCreateInfo gpinf;
+ memset(&gpinf, 0, sizeof gpinf);
+ gpinf.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
+ gpinf.stageCount = 2;
+ gpinf.pStages = sdri;
+ gpinf.pVertexInputState = &verti;
+ gpinf.pInputAssemblyState = &asmi;
+ gpinf.pViewportState = &viewpi;
+ gpinf.pMultisampleState = &multi;
+ gpinf.pDepthStencilState = &depthi;
+ gpinf.pColorBlendState = &cblendi;
+ gpinf.pDynamicState = &dyni;
+ gpinf.layout = layout;
+ gpinf.renderPass = vk_renderpass;
+
+ VkPipelineCacheCreateInfo pcinf;
+ memset(&pcinf, 0, sizeof pcinf);
+ pcinf.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
+
+ VkPipelineCache pcache;
+ if(vkCreatePipelineCache(vk_device, &pcinf, 0, &pcache) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create cache for pipeline.\n");
+ return 0;
+ }
+
+ VkPipeline gpipeline;
+ if(vkCreateGraphicsPipelines(vk_device, pcache, 1, &gpinf, 0, &gpipeline) !=
+ VK_SUCCESS) {
+ fprintf(stderr, "Failed to create graphics pipeline.\n");
+ return 0;
+ }
+
+ return gpipeline;
+}
+
+void VkuPipelineGenerator::set_shader_modules(VkShaderModule vs,
+ VkShaderModule fs)
+{
+ sdri[0].module = vs;
+ sdri[1].module = fs;
+}
+
+void VkuPipelineGenerator::set_vertex_attributes()
+{
+}
--- /dev/null
+#ifndef VKUTIL_PIPELINE_H_
+#define VKUTIL_PIPELINE_H_
+
+#include <vulkan/vulkan.h>
+
+enum VkuDynState {
+ VKU_DS_VIEWPORT = 1 << 0,
+ VKU_DS_SCISSOR = 1 << 1,
+ VKU_DS_BLEND = 1 << 2
+};
+
+class VkuPipelineGenerator {
+private:
+ VkPipelineShaderStageCreateInfo sdri[2];
+ VkPipelineVertexInputStateCreateInfo verti;
+ VkPipelineInputAssemblyStateCreateInfo asmi;
+ VkPipelineViewportStateCreateInfo viewpi;
+ VkPipelineRasterizationStateCreateInfo rasti;
+ VkPipelineMultisampleStateCreateInfo multi;
+ VkPipelineDepthStencilStateCreateInfo depthi;
+ VkPipelineColorBlendStateCreateInfo cblendi;
+
+ VkPipelineLayout layout;
+
+public:
+ VkuPipelineGenerator();
+ ~VkuPipelineGenerator();
+
+ VkPipeline generate(VkuDynState dyn_flags) const;
+
+ void set_shader_modules(VkShaderModule vs, VkShaderModule fs);
+
+ // TODO
+ void set_vertex_attributes();
+ void set_blend_state();
+ void set_layout();
+};
+
+#endif // VKUTIL_PIPELINE_H_
VkCommandPool vk_pool;
VkSurfaceKHR vk_surface;
VkSwapchainKHR vk_swapchain;
+VkRenderPass vk_renderpass;
VkDescriptorPool vk_dpool;
/* static functions */
vkFreeDescriptorSets(vk_device, vk_dpool, num, sets);
}
+bool vku_update_descriptor_sets(VkDescriptorSet *sets, int num_sets)
+{
+// std::vector
+ return true;
+}
+VkPipelineCache vku_create_pipeline_cache()
+{
+ VkPipelineCacheCreateInfo cinf;
+ memset(&cinf, 0, sizeof cinf);
+ cinf.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
+
+ VkPipelineCache cache;
+ if(vkCreatePipelineCache(vk_device, &cinf, 0, &cache) != VK_SUCCESS) {
+ fprintf(stderr, "Failed to create pipeline cache.\n");
+ return 0;
+ }
+ return cache;
+}
+
+void vku_destroy_pipeline_cache(VkPipelineCache cache)
+{
+ vkDestroyPipelineCache(vk_device, cache, 0);
+}
+
+void vku_pl_init_shader_stage_state_info(VkPipelineShaderStageCreateInfo *ssinf,
+ VkShaderStageFlagBits stage, VkShaderModule sm)
+{
+ memset(ssinf, 0, sizeof *ssinf);
+ ssinf->sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
+ ssinf->stage = stage;
+ ssinf->module = sm;
+ ssinf->pName = "main";
+}
+
+void vku_pl_init_input_asm_state_info(VkPipelineInputAssemblyStateCreateInfo *iasinf)
+{
+ memset(iasinf, 0, sizeof *iasinf);
+ iasinf->sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
+ iasinf->topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
+}
+
+void vku_pl_init_viewport_state_info(VkPipelineViewportStateCreateInfo *vsinf,
+ VkViewport *viewports, int num_viewports, VkRect2D *scissors,
+ int num_scissors)
+{
+ memset(vsinf, 0, sizeof *vsinf);
+ vsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
+ vsinf->viewportCount = num_viewports;
+ vsinf->scissorCount = num_scissors;
+ vsinf->pViewports = viewports;
+ vsinf->pScissors = scissors;
+}
+
+void vku_pl_init_rasterization_state_info(VkPipelineRasterizationStateCreateInfo *rsinf)
+{
+ memset(rsinf, 0, sizeof *rsinf);
+ rsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
+ rsinf->polygonMode = VK_POLYGON_MODE_FILL;
+ rsinf->cullMode = VK_CULL_MODE_FRONT_BIT;
+ rsinf->frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
+}
+
+void vku_pl_init_multisample_state_info(VkPipelineMultisampleStateCreateInfo *msinf)
+{
+ memset(msinf, 0, sizeof *msinf);
+ msinf->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
+ msinf->rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
+}
+
+void vku_pl_init_depth_stencil_state_info(VkPipelineDepthStencilStateCreateInfo *dsinf,
+ bool enable)
+{
+ memset(dsinf, 0, sizeof *dsinf);
+ dsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
+ dsinf->depthTestEnable = enable ? VK_TRUE : VK_FALSE;
+}
+
+void vku_pl_init_color_blend_state_info(VkPipelineColorBlendStateCreateInfo *cbsinf,
+ bool enable)
+{
+ memset(cbsinf, 0, sizeof *cbsinf);
+ cbsinf->sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
+ //TODO
+}
extern VkInstance vk_instance;
extern int vk_qfamily;
extern VkDescriptorPool vk_dpool;
+extern VkRenderPass vk_renderpass;
struct vku_buffer {
VkBuffer buf;
int size;
};
+struct vku_pipeline {
+};
+
+struct vku_pipeline_info {
+ /* shader stages */
+ VkShaderModule vs;
+ VkShaderModule fs;
+
+ /* states info */
+
+ VkPipelineShaderStageCreateInfo inf_shader_stage;
+
+ VkPipelineVertexInputStateCreateInfo inf_vertex_input;
+
+ VkPipelineInputAssemblyStateCreateInfo inf_input_asm;
+
+ VkPipelineTessellationStateCreateInfo inf_tessel;
+
+ VkPipelineViewportStateCreateInfo inf_viewport;
+ VkPipelineRasterizationStateCreateInfo inf_raster;
+ VkPipelineMultisampleStateCreateInfo inf_multisample;
+ VkPipelineDepthStencilStateCreateInfo inf_depth_stencil;
+ VkPipelineColorBlendStateCreateInfo inf_colorblend;
+
+ VkPipelineDynamicStateCreateInfo inf_dynamic_state;
+
+ VkPipelineLayout layout;
+ VkRenderPass renderpass;
+ uint32_t subpass;
+};
+
/* extensions */
bool vku_have_extension(const char *name);
bool vku_have_device_extension(const char *name);
int binding_point, int size);
void vku_destroy_descriptor(vku_descriptor *descriptor);
-bool vku_create_descriptor_pool(vku_descriptor **descriptors, int num_desc, VkDescriptorSet set);
+bool vku_create_descriptor_pool(vku_descriptor **descriptors, int num_desc,
+ VkDescriptorSet set);
void vku_destroy_descriptor_pool();
#endif // VKUTIL_H_