diff --git a/example/Vulkan/VKDescriptorSetLayout.cpp b/example/Vulkan/VKDescriptorSetLayout.cpp index 2eca9b6e..539c8003 100644 --- a/example/Vulkan/VKDescriptorSetLayout.cpp +++ b/example/Vulkan/VKDescriptorSetLayout.cpp @@ -29,7 +29,7 @@ void DescriptorSetLayoutCreater::Bind(const int binding,VkDescriptorType desc_ty layout_binding_list.Add(layout_binding); } -DescriptorSetLayout *DescriptorSetLayoutCreater::Creater() +DescriptorSetLayout *DescriptorSetLayoutCreater::Create() { VkDescriptorSetLayoutCreateInfo descriptor_layout = {}; descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; diff --git a/example/Vulkan/VKDescriptorSetLayout.h b/example/Vulkan/VKDescriptorSetLayout.h index a2f895ea..ea941b4c 100644 --- a/example/Vulkan/VKDescriptorSetLayout.h +++ b/example/Vulkan/VKDescriptorSetLayout.h @@ -59,7 +59,7 @@ public: #undef DESC_SET_BIND_FUNC - DescriptorSetLayout *Creater(); + DescriptorSetLayout *Create(); };//class DescriptorSet VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SET_LAYOUT_INCLUDE diff --git a/example/Vulkan/VKDevice.h b/example/Vulkan/VKDevice.h index 191a81c9..39627005 100644 --- a/example/Vulkan/VKDevice.h +++ b/example/Vulkan/VKDevice.h @@ -34,6 +34,7 @@ public: VkSurfaceKHR GetSurface () {return rsa->surface;} VkDevice GetDevice () {return rsa->device;} const PhysicalDevice *GetPhysicalDevice ()const {return rsa->physical_device;} + const VkExtent2D & GetExtent ()const {return rsa->swapchain_extent;} public: diff --git a/example/Vulkan/VKPipeline.cpp b/example/Vulkan/VKPipeline.cpp index 303d3bc4..0d19d470 100644 --- a/example/Vulkan/VKPipeline.cpp +++ b/example/Vulkan/VKPipeline.cpp @@ -1,6 +1,7 @@ #include"VKPipeline.h" #include"VKShader.h" #include"VKVertexInput.h" +#include"VKRenderPass.h" VK_NAMESPACE_BEGIN Pipeline::~Pipeline() @@ -8,24 +9,23 @@ Pipeline::~Pipeline() vkDestroyPipeline(device,pipeline,nullptr); } -PipelineCreater::PipelineCreater(VkDevice dev,uint w,uint h) +PipelineCreater::PipelineCreater(Device *dev) { - device=dev; - width=w; - height=h; + device=dev->GetDevice(); + extent=dev->GetExtent(); + hgl_zero(pipelineInfo); pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; viewport.x = 0.0f; viewport.y = 0.0f; - viewport.width = width; - viewport.height = height; + viewport.width = extent.width; + viewport.height = extent.height; viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; scissor.offset = {0, 0}; - scissor.extent.width=width; - scissor.extent.height=height; + scissor.extent = extent; viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportState.viewportCount = 1; @@ -34,6 +34,40 @@ PipelineCreater::PipelineCreater(VkDevice dev,uint w,uint h) viewportState.pScissors = &scissor; pipelineInfo.pViewportState = &viewportState; + + rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; + rasterizer.depthClampEnable = VK_FALSE; + rasterizer.rasterizerDiscardEnable = VK_FALSE; + rasterizer.polygonMode = VK_POLYGON_MODE_FILL; + rasterizer.lineWidth = 1.0f; + rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; + rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; + rasterizer.depthBiasEnable = VK_FALSE; + + pipelineInfo.pRasterizationState = &rasterizer; + + multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; + multisampling.sampleShadingEnable = VK_FALSE; + multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + + pipelineInfo.pMultisampleState = &multisampling; + + colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; + colorBlendAttachment.blendEnable = VK_FALSE; + + colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; + colorBlending.logicOpEnable = VK_FALSE; + colorBlending.logicOp = VK_LOGIC_OP_COPY; + colorBlending.attachmentCount = 1; + colorBlending.pAttachments = &colorBlendAttachment; + colorBlending.blendConstants[0] = 0.0f; + colorBlending.blendConstants[1] = 0.0f; + colorBlending.blendConstants[2] = 0.0f; + colorBlending.blendConstants[3] = 0.0f; + + pipelineInfo.pColorBlendState = &colorBlending; + + pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; } bool PipelineCreater::Set(const Shader *s) @@ -75,16 +109,26 @@ bool PipelineCreater::Set(const VkPrimitiveTopology topology,bool restart) return(true); } +bool PipelineCreater::Set(VkPipelineLayout pl) +{ + if(!pl)return(false); + + pipelineInfo.layout = pl; + return(true); +} + +bool PipelineCreater::Set(VkRenderPass rp,uint32_t subpass) +{ + if(!rp)return(false); + + pipelineInfo.renderPass = rp; + pipelineInfo.subpass = subpass; + + return(true); +} + Pipeline *PipelineCreater::Create() { - //pipelineInfo.pRasterizationState = &rasterizer; - //pipelineInfo.pMultisampleState = &multisampling; - //pipelineInfo.pColorBlendState = &colorBlending; - //pipelineInfo.layout = pipelineLayout; - //pipelineInfo.renderPass = renderPass; - //pipelineInfo.subpass = 0; - //pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; - VkPipeline graphicsPipeline; if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) diff --git a/example/Vulkan/VKPipeline.h b/example/Vulkan/VKPipeline.h index b9b5ede1..26eb37f3 100644 --- a/example/Vulkan/VKPipeline.h +++ b/example/Vulkan/VKPipeline.h @@ -2,8 +2,11 @@ #define HGL_GRAPH_VULKAN_PIPELINE_INCLUDE #include"VK.h" +#include"VKDevice.h" VK_NAMESPACE_BEGIN +class RenderPass; + class Pipeline { VkDevice device; @@ -13,6 +16,8 @@ public: Pipeline(VkDevice dev,VkPipeline p){device=dev;pipeline=p;} virtual ~Pipeline(); + + operator VkPipeline(){return pipeline;} };//class GraphicsPipeline class Shader; @@ -21,7 +26,7 @@ class VertexInput; class PipelineCreater { VkDevice device; - uint width,height; + VkExtent2D extent; VkGraphicsPipelineCreateInfo pipelineInfo; VkPipelineVertexInputStateCreateInfo vis_create_info; @@ -31,6 +36,13 @@ class PipelineCreater VkRect2D scissor; VkPipelineViewportStateCreateInfo viewportState; + VkPipelineRasterizationStateCreateInfo rasterizer; + + VkPipelineMultisampleStateCreateInfo multisampling; + + VkPipelineColorBlendAttachmentState colorBlendAttachment; + VkPipelineColorBlendStateCreateInfo colorBlending; + private: const Shader * shader =nullptr; @@ -38,12 +50,65 @@ private: public: - PipelineCreater(VkDevice dev,uint w,uint h); - ~PipelineCreater(); + PipelineCreater(Device *dev); + ~PipelineCreater()=default; bool Set(const Shader *); bool Set(const VertexInput *); bool Set(const VkPrimitiveTopology,bool=false); + bool Set(VkPipelineLayout pl); + bool Set(VkRenderPass,uint32_t subpass=0); + + void SetViewport( float x,float y,float w,float h){viewport.x=x;viewport.y=y;viewport.width=w;viewport.height=h;} + void SetDepthRange( float min_depth,float max_depth){viewport.minDepth=min_depth;viewport.maxDepth=max_depth;} + void SetScissor( float l,float t,float w,float h){scissor.offset.x=l;scissor.offset.y=t;scissor.extent.width=w;scissor.extent.height=h;} + + void SetDepthClamp( bool dc) {rasterizer.depthClampEnable=dc;} + void SetDiscard( bool discard) {rasterizer.rasterizerDiscardEnable=discard;} + void SetPolygonMode(VkPolygonMode pm) {rasterizer.polygonMode =pm;} + void SetCullMode( VkCullModeFlagBits cm) {rasterizer.cullMode =cm;} + void SetFrontFace( VkFrontFace ff) {rasterizer.frontFace =ff;} + void SetDepthBias( float ConstantFactor, + float Clamp, + float SlopeFactor) + { + rasterizer.depthBiasEnable =VK_TRUE; + rasterizer.depthBiasConstantFactor =ConstantFactor; + rasterizer.depthBiasClamp =Clamp; + rasterizer.depthBiasSlopeFactor =SlopeFactor; + } + void DisableDepthBias() {rasterizer.depthBiasEnable=VK_FALSE;} + void SetLineWidth( float line_width) {rasterizer.lineWidth =line_width;} + + void SetSamleCount( VkSampleCountFlagBits sc) + { + multisampling.sampleShadingEnable=(sc==VK_SAMPLE_COUNT_1_BIT?VK_FALSE:VK_TRUE); + multisampling.rasterizationSamples=sc; + } + + void SetColorWriteMask(bool r,bool g,bool b,bool a) + { + colorBlendAttachment.colorWriteMask=0; + + if(r)colorBlendAttachment.colorWriteMask|=VK_COLOR_COMPONENT_R_BIT; + if(r)colorBlendAttachment.colorWriteMask|=VK_COLOR_COMPONENT_G_BIT; + if(g)colorBlendAttachment.colorWriteMask|=VK_COLOR_COMPONENT_B_BIT; + if(a)colorBlendAttachment.colorWriteMask|=VK_COLOR_COMPONENT_A_BIT; + } + + void SetBlend( bool blend) {colorBlendAttachment.blendEnable=blend;} + void SetLogicOp( VkLogicOp logic_op) {colorBlending.logicOpEnable=VK_TRUE;colorBlending.logicOp=logic_op;} + void DisableLogicOp() {colorBlending.logicOpEnable=VK_FALSE;} + + void SetBlendConstans(float r,float g,float b,float a) + { + colorBlending.blendConstants[0] = r; + colorBlending.blendConstants[1] = g; + colorBlending.blendConstants[2] = b; + colorBlending.blendConstants[3] = a; + } + + void SetBlendConstans(float *blend_constans) {hgl_typecpy(colorBlending.blendConstants,blend_constans,4);} Pipeline *Create(); };//class PipelineCreater diff --git a/example/Vulkan/VKPipelineLayout.cpp b/example/Vulkan/VKPipelineLayout.cpp index 67fdbb37..b1a333e8 100644 --- a/example/Vulkan/VKPipelineLayout.cpp +++ b/example/Vulkan/VKPipelineLayout.cpp @@ -7,15 +7,19 @@ PipelineLayout::~PipelineLayout() vkDestroyPipelineLayout(device,layout,nullptr); } -PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout &dsl) +PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl) { + if(!dsl)return(nullptr); + + if(dsl->GetCount()<=0)return(nullptr); + VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {}; pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pPipelineLayoutCreateInfo.pNext = nullptr; pPipelineLayoutCreateInfo.pushConstantRangeCount = 0; pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr; - pPipelineLayoutCreateInfo.setLayoutCount = dsl.GetCount(); - pPipelineLayoutCreateInfo.pSetLayouts = dsl.GetData(); + pPipelineLayoutCreateInfo.setLayoutCount = dsl->GetCount(); + pPipelineLayoutCreateInfo.pSetLayouts = dsl->GetData(); VkPipelineLayout pipeline_layout; diff --git a/example/Vulkan/VKPipelineLayout.h b/example/Vulkan/VKPipelineLayout.h index c046aba0..baf269eb 100644 --- a/example/Vulkan/VKPipelineLayout.h +++ b/example/Vulkan/VKPipelineLayout.h @@ -9,12 +9,19 @@ class PipelineLayout VkDevice device; VkPipelineLayout layout; -public: +private: + + friend PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl); PipelineLayout(VkDevice dev,VkPipelineLayout pl){device=dev;layout=pl;} + +public: + ~PipelineLayout(); + + operator VkPipelineLayout (){return layout;} };//class PipelineLayout -PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout &dsl); +PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl); VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_PIPELINE_LAYOUT_INCLUDE diff --git a/example/Vulkan/VKRenderPass.h b/example/Vulkan/VKRenderPass.h index b4eb06ce..52099355 100644 --- a/example/Vulkan/VKRenderPass.h +++ b/example/Vulkan/VKRenderPass.h @@ -16,6 +16,8 @@ public: render_pass=rp; } virtual ~RenderPass(); + + operator VkRenderPass(){return render_pass;} };//class RenderPass VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_RENDER_PASS_INCLUDE diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index ffed6953..98cc59d1 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -2,6 +2,10 @@ #include"VKInstance.h" #include"VKDevice.h" #include"VKShader.h" +#include"VKVertexInput.h" +#include"VKDescriptorSetLayout.h" +#include"VKPipelineLayout.h" +#include"VKPipeline.h" #include #include @@ -49,17 +53,16 @@ bool LoadShader(vulkan::Shader *sc,const char *filename,VkShaderStageFlagBits sh return(true); } -bool LoadShader(VkDevice device) +vulkan::Shader *LoadShader(VkDevice device) { - vulkan::Shader sc(device); + vulkan::Shader *sc=new vulkan::Shader(device); - if(!LoadShader(&sc,"FlatColor.vert.spv",VK_SHADER_STAGE_VERTEX_BIT)) - return(false); + if(LoadShader(sc,"FlatColor.vert.spv",VK_SHADER_STAGE_VERTEX_BIT)) + if(LoadShader(sc,"FlatColor.frag.spv",VK_SHADER_STAGE_FRAGMENT_BIT)) + return sc; - if(!LoadShader(&sc,"FlatColor.frag.spv",VK_SHADER_STAGE_FRAGMENT_BIT)) - return(false); - - return(true); + delete sc; + return(nullptr); } int main(int,char **) @@ -96,9 +99,6 @@ int main(int,char **) std::cout<<"auto select physical device: "<GetDeviceName()<GetDevice())) - return(-3); - vulkan::CommandBuffer *cmd_buf=device->CreateCommandBuffer(); vulkan::Buffer *ubo=device->CreateUBO(1024); @@ -111,8 +111,32 @@ int main(int,char **) ubo->Unmap(); } + vulkan::Shader *shader=LoadShader(device->GetDevice()); + + if(!shader) + return -3; + + vulkan::VertexInput vi; + vulkan::PipelineCreater pc(device); vulkan::RenderPass *rp=device->CreateRenderPass(); + vulkan::DescriptorSetLayoutCreater dslc(device->GetDevice()); + vulkan::DescriptorSetLayout *dsl=dslc.Create(); + vulkan::PipelineLayout *pl=CreatePipelineLayout(device->GetDevice(),dsl); + + pc.Set(shader); + pc.Set(&vi); + pc.Set(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); + pc.Set(*pl); + pc.Set(*rp); + + vulkan::Pipeline *pipeline=pc.Create(); + + if(pipeline) + { + delete pipeline; + } + delete rp; delete ubo;