完善command buffer
This commit is contained in:
parent
9ba69a3de0
commit
d4d9aa6916
@ -7,7 +7,7 @@
|
||||
#include"VKDescriptorSets.h"
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
CommandBuffer::CommandBuffer(VkDevice dev,VkCommandPool cp,VkCommandBuffer cb)
|
||||
CommandBuffer::CommandBuffer(VkDevice dev,const VkExtent2D &extent,VkCommandPool cp,VkCommandBuffer cb)
|
||||
{
|
||||
device=dev;
|
||||
pool=cp;
|
||||
@ -19,6 +19,10 @@ CommandBuffer::CommandBuffer(VkDevice dev,VkCommandPool cp,VkCommandBuffer cb)
|
||||
clear_values[0].color.float32[3] = 0.2f;
|
||||
clear_values[1].depthStencil.depth = 1.0f;
|
||||
clear_values[1].depthStencil.stencil = 0;
|
||||
|
||||
render_area.offset.x=0;
|
||||
render_area.offset.y=0;
|
||||
render_area.extent=extent;
|
||||
}
|
||||
|
||||
CommandBuffer::~CommandBuffer()
|
||||
@ -29,6 +33,15 @@ CommandBuffer::~CommandBuffer()
|
||||
|
||||
bool CommandBuffer::Begin(RenderPass *rp,Framebuffer *fb)
|
||||
{
|
||||
VkCommandBufferBeginInfo cmd_buf_info = {};
|
||||
cmd_buf_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
cmd_buf_info.pNext = nullptr;
|
||||
cmd_buf_info.flags = 0;
|
||||
cmd_buf_info.pInheritanceInfo = nullptr;
|
||||
|
||||
if(vkBeginCommandBuffer(cmd_buf, &cmd_buf_info)!=VK_SUCCESS)
|
||||
return(false);
|
||||
|
||||
VkRenderPassBeginInfo rp_begin;
|
||||
|
||||
rp_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||
@ -40,7 +53,6 @@ bool CommandBuffer::Begin(RenderPass *rp,Framebuffer *fb)
|
||||
rp_begin.pClearValues = clear_values;
|
||||
|
||||
vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
@ -55,8 +67,8 @@ bool CommandBuffer::Bind(Pipeline *p)
|
||||
bool CommandBuffer::Bind(PipelineLayout *pl)
|
||||
{
|
||||
if(!pl)return(false);
|
||||
|
||||
vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, *pl, 0, pl->GetDescriptorSetCount(),pl->GetDescriptorSets(), 0, nullptr);
|
||||
if(pl->GetDescriptorSetCount()>0)
|
||||
vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, *pl, 0, pl->GetDescriptorSetCount(),pl->GetDescriptorSets(), 0, nullptr);
|
||||
return(true);
|
||||
}
|
||||
|
||||
@ -70,9 +82,7 @@ bool CommandBuffer::Bind(VertexInput *vi,const VkDeviceSize offset)
|
||||
if(buf_list.GetCount()<=0)
|
||||
return(false);
|
||||
|
||||
VkDeviceSize zero_offsets[1]={offset};
|
||||
|
||||
vkCmdBindVertexBuffers(cmd_buf,0,buf_list.GetCount(),buf_list.GetData(),zero_offsets);
|
||||
vkCmdBindVertexBuffers(cmd_buf,0,buf_list.GetCount(),buf_list.GetData(),&offset);
|
||||
return(true);
|
||||
}
|
||||
|
||||
@ -86,8 +96,9 @@ void CommandBuffer::Draw(const uint32_t vertex_count,const uint32_t instance_cou
|
||||
vkCmdDraw(cmd_buf,vertex_count,instance_count,first_vertex,first_instance);
|
||||
}
|
||||
|
||||
void CommandBuffer::End()
|
||||
bool CommandBuffer::End()
|
||||
{
|
||||
vkCmdEndRenderPass(cmd_buf);
|
||||
return(vkEndCommandBuffer(cmd_buf)==VK_SUCCESS);
|
||||
}
|
||||
VK_NAMESPACE_END
|
||||
|
@ -20,7 +20,7 @@ class CommandBuffer
|
||||
|
||||
public:
|
||||
|
||||
CommandBuffer(VkDevice dev,VkCommandPool cp,VkCommandBuffer cb);
|
||||
CommandBuffer(VkDevice dev,const VkExtent2D &extent,VkCommandPool cp,VkCommandBuffer cb);
|
||||
~CommandBuffer();
|
||||
|
||||
operator VkCommandBuffer(){return cmd_buf;}
|
||||
@ -46,7 +46,7 @@ public:
|
||||
bool Bind(VertexInput *vi,const VkDeviceSize offset=0);
|
||||
void Draw(const uint32_t vertex_count);
|
||||
void Draw(const uint32_t vertex_count,const uint32_t instance_count,const uint32_t first_vertex=0,const uint32_t first_instance=0);
|
||||
void End();
|
||||
bool End();
|
||||
};//class CommandBuffer
|
||||
VK_NAMESPACE_END
|
||||
#endif//HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
|
||||
|
@ -126,7 +126,7 @@ CommandBuffer *Device::CreateCommandBuffer()
|
||||
if(res!=VK_SUCCESS)
|
||||
return(nullptr);
|
||||
|
||||
return(new CommandBuffer(attr->device,attr->cmd_pool,cmd_buf));
|
||||
return(new CommandBuffer(attr->device,attr->swapchain_extent,attr->cmd_pool,cmd_buf));
|
||||
}
|
||||
|
||||
RenderPass *Device::CreateRenderPass()
|
||||
|
@ -42,6 +42,11 @@ public:
|
||||
VkDescriptorPool GetDescriptorPool () {return attr->desc_pool;}
|
||||
VkPipelineCache GetPipelineCache () {return attr->pipeline_cache;}
|
||||
|
||||
public:
|
||||
|
||||
VkImageView GetColorImageView (int index=-1) {return GetObject(attr->sc_image_views,index==-1?current_framebuffer:index);}
|
||||
VkImageView GetDepthImageView () {return attr->depth.view;}
|
||||
|
||||
public:
|
||||
|
||||
Buffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE);
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include"VKFence.h"
|
||||
#include"VKSemaphore.h"
|
||||
#include"VKFormat.h"
|
||||
#include"VKFramebuffer.h"
|
||||
|
||||
#include<fstream>
|
||||
|
||||
@ -104,6 +105,16 @@ vulkan::VertexInput *CreateVertexBuffer(vulkan::Device *dev)
|
||||
return vi;
|
||||
}
|
||||
|
||||
void wait_seconds(int seconds) {
|
||||
#ifdef WIN32
|
||||
Sleep(seconds * 1000);
|
||||
#elif defined(__ANDROID__)
|
||||
sleep(seconds);
|
||||
#else
|
||||
sleep(seconds);
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int,char **)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
@ -140,15 +151,15 @@ int main(int,char **)
|
||||
|
||||
vulkan::CommandBuffer *cmd_buf=device->CreateCommandBuffer();
|
||||
|
||||
vulkan::Buffer *ubo=device->CreateUBO(1024);
|
||||
//vulkan::Buffer *ubo=device->CreateUBO(1024);
|
||||
|
||||
uint8_t *p=ubo->Map();
|
||||
//uint8_t *p=ubo->Map();
|
||||
|
||||
if(p)
|
||||
{
|
||||
memset(p,0,1024);
|
||||
ubo->Unmap();
|
||||
}
|
||||
//if(p)
|
||||
//{
|
||||
// memset(p,0,1024);
|
||||
// ubo->Unmap();
|
||||
//}
|
||||
|
||||
vulkan::Shader *shader=LoadShader(device->GetDevice());
|
||||
|
||||
@ -178,6 +189,23 @@ int main(int,char **)
|
||||
if(!pipeline)
|
||||
return(-4);
|
||||
|
||||
device->AcquireNextImage(); //这样才会得到current_framebuffer的值,下面的device->GetColorImageView()才会正确
|
||||
|
||||
vulkan::Framebuffer *fb=vulkan::CreateFramebuffer(device,rp,device->GetColorImageView(),device->GetDepthImageView());
|
||||
|
||||
cmd_buf->Begin(rp,fb);
|
||||
cmd_buf->Bind(pipeline);
|
||||
cmd_buf->Bind(pl);
|
||||
cmd_buf->Bind(vi);
|
||||
cmd_buf->Draw(3);
|
||||
cmd_buf->End();
|
||||
|
||||
device->QueueSubmit(cmd_buf,fence);
|
||||
device->Wait(fence);
|
||||
device->QueuePresent();
|
||||
|
||||
wait_seconds(3);
|
||||
|
||||
delete pipeline;
|
||||
|
||||
delete sem;
|
||||
@ -185,7 +213,7 @@ int main(int,char **)
|
||||
delete rp;
|
||||
|
||||
delete vi;
|
||||
delete ubo;
|
||||
// delete ubo;
|
||||
|
||||
delete cmd_buf;
|
||||
delete device;
|
||||
|
Loading…
x
Reference in New Issue
Block a user