From: Eleni Maria Stea Date: Sun, 25 Mar 2018 22:56:39 +0000 (+0300) Subject: backup X-Git-Url: http://git.mutantstargoat.com?p=demo;a=commitdiff_plain;h=dbce2a319517c09fcf1a9be60ae46a0ff20c423e backup added pipeline generator class that creates a "default" pipeline at initialization and has functions to set extra details, wip --- diff --git a/src/global.h b/src/global.h index cbcbed6..1bdc469 100644 --- a/src/global.h +++ b/src/global.h @@ -9,4 +9,4 @@ extern ShaderManager *sdr_man; extern Mat4 mprojection; extern double time_sec; -#endif // GLOBAL_H_ \ No newline at end of file +#endif // GLOBAL_H_ diff --git a/src/renderer.cc b/src/renderer.cc index 18c93bb..5d57a36 100644 --- a/src/renderer.cc +++ b/src/renderer.cc @@ -46,7 +46,8 @@ bool Renderer::create() { //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(); diff --git a/src/vulkan/vk.cc b/src/vulkan/vk.cc index 8ddea09..654165b 100644 --- a/src/vulkan/vk.cc +++ b/src/vulkan/vk.cc @@ -32,8 +32,6 @@ static const VkFormat dformat = VK_FORMAT_D32_SFLOAT_S8_UINT; 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 @@ -433,8 +431,8 @@ static bool create_renderpass() 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; } @@ -450,7 +448,7 @@ static bool create_framebuffers() 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; @@ -551,7 +549,7 @@ static bool begin_rendering_command_buffers(VkCommandBuffer *bufs, int count) 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; diff --git a/src/vulkan/vkutil-pipeline.cc b/src/vulkan/vkutil-pipeline.cc new file mode 100644 index 0000000..bc542ff --- /dev/null +++ b/src/vulkan/vkutil-pipeline.cc @@ -0,0 +1,118 @@ +#include +#include + +#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 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() +{ +} diff --git a/src/vulkan/vkutil-pipeline.h b/src/vulkan/vkutil-pipeline.h new file mode 100644 index 0000000..4dac9af --- /dev/null +++ b/src/vulkan/vkutil-pipeline.h @@ -0,0 +1,39 @@ +#ifndef VKUTIL_PIPELINE_H_ +#define VKUTIL_PIPELINE_H_ + +#include + +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_ diff --git a/src/vulkan/vkutil.cc b/src/vulkan/vkutil.cc index c1ca1d5..1a33fa7 100644 --- a/src/vulkan/vkutil.cc +++ b/src/vulkan/vkutil.cc @@ -19,6 +19,7 @@ int vk_qfamily; VkCommandPool vk_pool; VkSurfaceKHR vk_surface; VkSwapchainKHR vk_swapchain; +VkRenderPass vk_renderpass; VkDescriptorPool vk_dpool; /* static functions */ @@ -725,4 +726,88 @@ void vku_free_descriptor_sets(VkDescriptorSet *sets, int num) 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 +} diff --git a/src/vulkan/vkutil.h b/src/vulkan/vkutil.h index 1c9ceac..775fd27 100644 --- a/src/vulkan/vkutil.h +++ b/src/vulkan/vkutil.h @@ -13,6 +13,7 @@ extern VkSurfaceKHR vk_surface; extern VkInstance vk_instance; extern int vk_qfamily; extern VkDescriptorPool vk_dpool; +extern VkRenderPass vk_renderpass; struct vku_buffer { VkBuffer buf; @@ -28,6 +29,37 @@ struct vku_descriptor { 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); @@ -68,7 +100,8 @@ vku_descriptor *vku_create_descriptor(VkDescriptorType type, VkFlags stage, 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_