完成Pipeline创建代码

This commit is contained in:
HuYingzhuo 2019-04-18 21:02:42 +08:00
parent ef86341146
commit 3b3bdf0846
9 changed files with 184 additions and 37 deletions

View File

@ -29,7 +29,7 @@ void DescriptorSetLayoutCreater::Bind(const int binding,VkDescriptorType desc_ty
layout_binding_list.Add(layout_binding); layout_binding_list.Add(layout_binding);
} }
DescriptorSetLayout *DescriptorSetLayoutCreater::Creater() DescriptorSetLayout *DescriptorSetLayoutCreater::Create()
{ {
VkDescriptorSetLayoutCreateInfo descriptor_layout = {}; VkDescriptorSetLayoutCreateInfo descriptor_layout = {};
descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;

View File

@ -59,7 +59,7 @@ public:
#undef DESC_SET_BIND_FUNC #undef DESC_SET_BIND_FUNC
DescriptorSetLayout *Creater(); DescriptorSetLayout *Create();
};//class DescriptorSet };//class DescriptorSet
VK_NAMESPACE_END VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SET_LAYOUT_INCLUDE #endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SET_LAYOUT_INCLUDE

View File

@ -34,6 +34,7 @@ public:
VkSurfaceKHR GetSurface () {return rsa->surface;} VkSurfaceKHR GetSurface () {return rsa->surface;}
VkDevice GetDevice () {return rsa->device;} VkDevice GetDevice () {return rsa->device;}
const PhysicalDevice *GetPhysicalDevice ()const {return rsa->physical_device;} const PhysicalDevice *GetPhysicalDevice ()const {return rsa->physical_device;}
const VkExtent2D & GetExtent ()const {return rsa->swapchain_extent;}
public: public:

View File

@ -1,6 +1,7 @@
#include"VKPipeline.h" #include"VKPipeline.h"
#include"VKShader.h" #include"VKShader.h"
#include"VKVertexInput.h" #include"VKVertexInput.h"
#include"VKRenderPass.h"
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
Pipeline::~Pipeline() Pipeline::~Pipeline()
@ -8,24 +9,23 @@ Pipeline::~Pipeline()
vkDestroyPipeline(device,pipeline,nullptr); vkDestroyPipeline(device,pipeline,nullptr);
} }
PipelineCreater::PipelineCreater(VkDevice dev,uint w,uint h) PipelineCreater::PipelineCreater(Device *dev)
{ {
device=dev; device=dev->GetDevice();
width=w; extent=dev->GetExtent();
height=h;
hgl_zero(pipelineInfo); hgl_zero(pipelineInfo);
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
viewport.x = 0.0f; viewport.x = 0.0f;
viewport.y = 0.0f; viewport.y = 0.0f;
viewport.width = width; viewport.width = extent.width;
viewport.height = height; viewport.height = extent.height;
viewport.minDepth = 0.0f; viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f; viewport.maxDepth = 1.0f;
scissor.offset = {0, 0}; scissor.offset = {0, 0};
scissor.extent.width=width; scissor.extent = extent;
scissor.extent.height=height;
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1; viewportState.viewportCount = 1;
@ -34,6 +34,40 @@ PipelineCreater::PipelineCreater(VkDevice dev,uint w,uint h)
viewportState.pScissors = &scissor; viewportState.pScissors = &scissor;
pipelineInfo.pViewportState = &viewportState; 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) bool PipelineCreater::Set(const Shader *s)
@ -75,16 +109,26 @@ bool PipelineCreater::Set(const VkPrimitiveTopology topology,bool restart)
return(true); 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() 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; VkPipeline graphicsPipeline;
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS)

View File

@ -2,8 +2,11 @@
#define HGL_GRAPH_VULKAN_PIPELINE_INCLUDE #define HGL_GRAPH_VULKAN_PIPELINE_INCLUDE
#include"VK.h" #include"VK.h"
#include"VKDevice.h"
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
class RenderPass;
class Pipeline class Pipeline
{ {
VkDevice device; VkDevice device;
@ -13,6 +16,8 @@ public:
Pipeline(VkDevice dev,VkPipeline p){device=dev;pipeline=p;} Pipeline(VkDevice dev,VkPipeline p){device=dev;pipeline=p;}
virtual ~Pipeline(); virtual ~Pipeline();
operator VkPipeline(){return pipeline;}
};//class GraphicsPipeline };//class GraphicsPipeline
class Shader; class Shader;
@ -21,7 +26,7 @@ class VertexInput;
class PipelineCreater class PipelineCreater
{ {
VkDevice device; VkDevice device;
uint width,height; VkExtent2D extent;
VkGraphicsPipelineCreateInfo pipelineInfo; VkGraphicsPipelineCreateInfo pipelineInfo;
VkPipelineVertexInputStateCreateInfo vis_create_info; VkPipelineVertexInputStateCreateInfo vis_create_info;
@ -31,6 +36,13 @@ class PipelineCreater
VkRect2D scissor; VkRect2D scissor;
VkPipelineViewportStateCreateInfo viewportState; VkPipelineViewportStateCreateInfo viewportState;
VkPipelineRasterizationStateCreateInfo rasterizer;
VkPipelineMultisampleStateCreateInfo multisampling;
VkPipelineColorBlendAttachmentState colorBlendAttachment;
VkPipelineColorBlendStateCreateInfo colorBlending;
private: private:
const Shader * shader =nullptr; const Shader * shader =nullptr;
@ -38,12 +50,65 @@ private:
public: public:
PipelineCreater(VkDevice dev,uint w,uint h); PipelineCreater(Device *dev);
~PipelineCreater(); ~PipelineCreater()=default;
bool Set(const Shader *); bool Set(const Shader *);
bool Set(const VertexInput *); bool Set(const VertexInput *);
bool Set(const VkPrimitiveTopology,bool=false); 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(); Pipeline *Create();
};//class PipelineCreater };//class PipelineCreater

View File

@ -7,15 +7,19 @@ PipelineLayout::~PipelineLayout()
vkDestroyPipelineLayout(device,layout,nullptr); 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 = {}; VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pPipelineLayoutCreateInfo.pNext = nullptr; pPipelineLayoutCreateInfo.pNext = nullptr;
pPipelineLayoutCreateInfo.pushConstantRangeCount = 0; pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;
pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr; pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
pPipelineLayoutCreateInfo.setLayoutCount = dsl.GetCount(); pPipelineLayoutCreateInfo.setLayoutCount = dsl->GetCount();
pPipelineLayoutCreateInfo.pSetLayouts = dsl.GetData(); pPipelineLayoutCreateInfo.pSetLayouts = dsl->GetData();
VkPipelineLayout pipeline_layout; VkPipelineLayout pipeline_layout;

View File

@ -9,12 +9,19 @@ class PipelineLayout
VkDevice device; VkDevice device;
VkPipelineLayout layout; VkPipelineLayout layout;
public: private:
friend PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl);
PipelineLayout(VkDevice dev,VkPipelineLayout pl){device=dev;layout=pl;} PipelineLayout(VkDevice dev,VkPipelineLayout pl){device=dev;layout=pl;}
public:
~PipelineLayout(); ~PipelineLayout();
operator VkPipelineLayout (){return layout;}
};//class PipelineLayout };//class PipelineLayout
PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout &dsl); PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl);
VK_NAMESPACE_END VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_PIPELINE_LAYOUT_INCLUDE #endif//HGL_GRAPH_VULKAN_PIPELINE_LAYOUT_INCLUDE

View File

@ -16,6 +16,8 @@ public:
render_pass=rp; render_pass=rp;
} }
virtual ~RenderPass(); virtual ~RenderPass();
operator VkRenderPass(){return render_pass;}
};//class RenderPass };//class RenderPass
VK_NAMESPACE_END VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_RENDER_PASS_INCLUDE #endif//HGL_GRAPH_VULKAN_RENDER_PASS_INCLUDE

View File

@ -2,6 +2,10 @@
#include"VKInstance.h" #include"VKInstance.h"
#include"VKDevice.h" #include"VKDevice.h"
#include"VKShader.h" #include"VKShader.h"
#include"VKVertexInput.h"
#include"VKDescriptorSetLayout.h"
#include"VKPipelineLayout.h"
#include"VKPipeline.h"
#include<io.h> #include<io.h>
#include<fcntl.h> #include<fcntl.h>
@ -49,17 +53,16 @@ bool LoadShader(vulkan::Shader *sc,const char *filename,VkShaderStageFlagBits sh
return(true); 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)) if(LoadShader(sc,"FlatColor.vert.spv",VK_SHADER_STAGE_VERTEX_BIT))
return(false); if(LoadShader(sc,"FlatColor.frag.spv",VK_SHADER_STAGE_FRAGMENT_BIT))
return sc;
if(!LoadShader(&sc,"FlatColor.frag.spv",VK_SHADER_STAGE_FRAGMENT_BIT)) delete sc;
return(false); return(nullptr);
return(true);
} }
int main(int,char **) int main(int,char **)
@ -96,9 +99,6 @@ int main(int,char **)
std::cout<<"auto select physical device: "<<render_device->GetDeviceName()<<std::endl; std::cout<<"auto select physical device: "<<render_device->GetDeviceName()<<std::endl;
} }
if(!LoadShader(device->GetDevice()))
return(-3);
vulkan::CommandBuffer *cmd_buf=device->CreateCommandBuffer(); vulkan::CommandBuffer *cmd_buf=device->CreateCommandBuffer();
vulkan::Buffer *ubo=device->CreateUBO(1024); vulkan::Buffer *ubo=device->CreateUBO(1024);
@ -111,8 +111,32 @@ int main(int,char **)
ubo->Unmap(); 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::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 rp;
delete ubo; delete ubo;