2020-10-28 19:49:57 +08:00
|
|
|
|
#include<hgl/graph/VKCommandBuffer.h>
|
|
|
|
|
#include<hgl/graph/VKRenderPass.h>
|
|
|
|
|
#include<hgl/graph/VKFramebuffer.h>
|
2022-06-24 21:06:38 +08:00
|
|
|
|
#include<hgl/graph/VKRenderable.h>
|
2020-11-16 16:42:20 +08:00
|
|
|
|
#include<hgl/graph/VKDeviceAttribute.h>
|
|
|
|
|
#include<hgl/graph/VKPhysicalDevice.h>
|
2024-04-27 03:09:16 +08:00
|
|
|
|
#include<hgl/graph/VKIndexBuffer.h>
|
2025-01-26 12:13:03 +08:00
|
|
|
|
#include<hgl/graph/VKRenderTarget.h>
|
2020-10-28 19:49:57 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2020-11-16 16:42:20 +08:00
|
|
|
|
RenderCmdBuffer::RenderCmdBuffer(const GPUDeviceAttribute *attr,VkCommandBuffer cb):GPUCmdBuffer(attr,cb)
|
2020-10-28 19:49:57 +08:00
|
|
|
|
{
|
|
|
|
|
cv_count=0;
|
|
|
|
|
clear_values=nullptr;
|
|
|
|
|
|
|
|
|
|
hgl_zero(render_area);
|
|
|
|
|
hgl_zero(viewport);
|
|
|
|
|
|
|
|
|
|
pipeline_layout=VK_NULL_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 15:37:00 +08:00
|
|
|
|
RenderCmdBuffer::~RenderCmdBuffer()
|
2020-10-28 19:49:57 +08:00
|
|
|
|
{
|
|
|
|
|
if(clear_values)
|
|
|
|
|
hgl_free(clear_values);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-26 12:13:03 +08:00
|
|
|
|
void RenderCmdBuffer::SetClear()
|
2020-10-28 19:49:57 +08:00
|
|
|
|
{
|
|
|
|
|
if(cv_count>0)
|
|
|
|
|
{
|
|
|
|
|
clear_values=hgl_align_realloc<VkClearValue>(clear_values,cv_count);
|
|
|
|
|
|
|
|
|
|
clear_values[cv_count-1].depthStencil.depth = 1.0f;
|
|
|
|
|
clear_values[cv_count-1].depthStencil.stencil = 0;
|
|
|
|
|
}
|
2024-03-16 00:41:20 +08:00
|
|
|
|
else if(clear_values)
|
2020-10-28 19:49:57 +08:00
|
|
|
|
{
|
2024-03-16 00:41:20 +08:00
|
|
|
|
hgl_free(clear_values);
|
|
|
|
|
clear_values=nullptr;
|
2020-10-28 19:49:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 15:37:00 +08:00
|
|
|
|
void RenderCmdBuffer::SetRenderArea(const VkExtent2D &ext2d)
|
2020-10-28 19:49:57 +08:00
|
|
|
|
{
|
|
|
|
|
render_area.offset.x=0;
|
|
|
|
|
render_area.offset.y=0;
|
|
|
|
|
render_area.extent=ext2d;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-26 12:13:03 +08:00
|
|
|
|
bool RenderCmdBuffer::BindFramebuffer(Framebuffer *fbo)
|
2020-10-28 19:49:57 +08:00
|
|
|
|
{
|
2025-01-26 12:13:03 +08:00
|
|
|
|
if(!fbo)return(false);
|
2020-10-28 19:49:57 +08:00
|
|
|
|
|
2025-01-26 12:13:03 +08:00
|
|
|
|
cv_count=fbo->GetAttachmentCount();
|
|
|
|
|
SetClear();
|
2020-10-28 19:49:57 +08:00
|
|
|
|
|
|
|
|
|
render_area.offset.x=0;
|
|
|
|
|
render_area.offset.y=0;
|
2025-01-26 12:13:03 +08:00
|
|
|
|
render_area.extent=fbo->GetExtent();
|
2020-10-28 19:49:57 +08:00
|
|
|
|
|
2025-01-26 12:36:23 +08:00
|
|
|
|
rp_begin.renderPass = *fbo->GetRenderPass();
|
2025-01-26 12:13:03 +08:00
|
|
|
|
rp_begin.framebuffer = *fbo;
|
2020-10-28 19:49:57 +08:00
|
|
|
|
rp_begin.renderArea = render_area;
|
|
|
|
|
rp_begin.clearValueCount = cv_count;
|
|
|
|
|
rp_begin.pClearValues = clear_values;
|
|
|
|
|
|
|
|
|
|
viewport.x = 0;
|
|
|
|
|
viewport.y = 0;
|
|
|
|
|
viewport.minDepth = 0.0f;
|
|
|
|
|
viewport.maxDepth = 1.0f;
|
|
|
|
|
viewport.width = render_area.extent.width;
|
|
|
|
|
viewport.height = render_area.extent.height;
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-09 15:37:00 +08:00
|
|
|
|
bool RenderCmdBuffer::BeginRenderPass()
|
2020-10-28 19:49:57 +08:00
|
|
|
|
{
|
|
|
|
|
vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
|
|
|
|
vkCmdSetViewport(cmd_buf,0,1,&viewport);
|
|
|
|
|
vkCmdSetScissor(cmd_buf,0,1,&render_area);
|
|
|
|
|
|
|
|
|
|
pipeline_layout=VK_NULL_HANDLE;
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 20:19:53 +08:00
|
|
|
|
bool RenderCmdBuffer::BindDescriptorSets(Material *mtl)
|
2021-06-16 20:29:25 +08:00
|
|
|
|
{
|
2023-09-05 20:19:53 +08:00
|
|
|
|
if(!mtl)return(false);
|
2021-06-16 20:29:25 +08:00
|
|
|
|
|
2025-03-05 01:01:48 +08:00
|
|
|
|
if(desc_binding)
|
|
|
|
|
desc_binding->Bind(mtl);
|
|
|
|
|
|
2021-06-16 20:29:25 +08:00
|
|
|
|
{
|
|
|
|
|
uint32_t count=0;
|
|
|
|
|
|
|
|
|
|
MaterialParameters *mp;
|
2023-03-22 19:37:48 +08:00
|
|
|
|
VkDescriptorSet ds[DESCRIPTOR_SET_TYPE_COUNT];
|
2021-06-16 20:29:25 +08:00
|
|
|
|
|
2023-02-22 21:53:51 +08:00
|
|
|
|
ENUM_CLASS_FOR(DescriptorSetType,int,i)
|
2021-06-16 20:29:25 +08:00
|
|
|
|
{
|
2023-06-05 21:39:12 +08:00
|
|
|
|
mp=mtl->GetMP((DescriptorSetType)i);
|
2021-06-16 20:29:25 +08:00
|
|
|
|
|
|
|
|
|
if(mp)
|
|
|
|
|
{
|
2023-10-12 01:40:59 +08:00
|
|
|
|
mp->Update();
|
|
|
|
|
|
2021-06-16 20:29:25 +08:00
|
|
|
|
ds[count]=mp->GetVkDescriptorSet();
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(count>0)
|
|
|
|
|
{
|
2023-09-05 20:19:53 +08:00
|
|
|
|
pipeline_layout=mtl->GetPipelineLayout();
|
2021-06-16 20:29:25 +08:00
|
|
|
|
|
2023-04-21 20:27:28 +08:00
|
|
|
|
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,pipeline_layout,0,count,ds,0,0);
|
2021-06-16 20:29:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-14 21:11:01 +08:00
|
|
|
|
return(true);
|
2021-06-16 20:29:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
void RenderCmdBuffer::BindIBO(IndexBuffer *ibo,const VkDeviceSize byte_offset)
|
2024-05-25 22:08:01 +08:00
|
|
|
|
{
|
2024-05-28 02:21:33 +08:00
|
|
|
|
if(!ibo)return;
|
2024-05-28 23:10:50 +08:00
|
|
|
|
|
2024-05-25 22:08:01 +08:00
|
|
|
|
vkCmdBindIndexBuffer(cmd_buf,
|
|
|
|
|
ibo->GetBuffer(),
|
2024-05-28 23:10:50 +08:00
|
|
|
|
byte_offset,
|
2024-05-25 22:08:01 +08:00
|
|
|
|
VkIndexType(ibo->GetType()));
|
|
|
|
|
}
|
2023-05-06 19:29:38 +08:00
|
|
|
|
|
2024-05-31 22:04:02 +08:00
|
|
|
|
bool RenderCmdBuffer::BindDataBuffer(const PrimitiveDataBuffer *pdb)
|
2020-10-28 19:49:57 +08:00
|
|
|
|
{
|
2024-05-28 23:10:50 +08:00
|
|
|
|
if(!pdb)
|
2020-10-28 19:49:57 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
if(pdb->vab_count<=0)
|
2024-05-25 22:08:01 +08:00
|
|
|
|
return(false);
|
2023-05-06 19:29:38 +08:00
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
vkCmdBindVertexBuffers(cmd_buf,
|
|
|
|
|
0, //first binding
|
|
|
|
|
pdb->vab_count,
|
|
|
|
|
pdb->vab_list,
|
2024-05-31 22:04:02 +08:00
|
|
|
|
pdb->vab_offset); //vab byte offsets
|
2020-10-28 19:49:57 +08:00
|
|
|
|
|
2024-05-28 23:10:50 +08:00
|
|
|
|
if(pdb->ibo)
|
|
|
|
|
BindIBO(pdb->ibo);
|
2020-10-28 19:49:57 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2020-11-16 16:42:20 +08:00
|
|
|
|
|
|
|
|
|
void RenderCmdBuffer::DrawIndirect( VkBuffer buffer,
|
|
|
|
|
VkDeviceSize offset,
|
|
|
|
|
uint32_t drawCount,
|
|
|
|
|
uint32_t stride)
|
|
|
|
|
{
|
2021-12-16 17:27:33 +08:00
|
|
|
|
if(this->dev_attr->physical_device->SupportMDI())
|
2020-11-16 16:42:20 +08:00
|
|
|
|
vkCmdDrawIndirect(cmd_buf,buffer,offset,drawCount,stride);
|
|
|
|
|
else
|
|
|
|
|
for(uint32_t i=0;i<drawCount;i++)
|
|
|
|
|
vkCmdDrawIndirect(cmd_buf,buffer,offset+i*stride,1,stride);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 14:33:10 +08:00
|
|
|
|
void RenderCmdBuffer::DrawIndexedIndirect( VkBuffer buffer,
|
|
|
|
|
VkDeviceSize offset,
|
|
|
|
|
uint32_t drawCount,
|
|
|
|
|
uint32_t stride)
|
2020-11-16 16:42:20 +08:00
|
|
|
|
{
|
2021-12-16 17:27:33 +08:00
|
|
|
|
if(this->dev_attr->physical_device->SupportMDI())
|
2020-11-16 16:42:20 +08:00
|
|
|
|
vkCmdDrawIndexedIndirect(cmd_buf,buffer,offset,drawCount,stride);
|
|
|
|
|
else
|
|
|
|
|
for(uint32_t i=0;i<drawCount;i++)
|
|
|
|
|
vkCmdDrawIndexedIndirect(cmd_buf,buffer,offset+i*stride,1,stride);
|
|
|
|
|
}
|
2024-04-26 00:41:53 +08:00
|
|
|
|
|
2024-05-30 13:14:13 +08:00
|
|
|
|
void RenderCmdBuffer::Draw(const PrimitiveDataBuffer *pdb,const PrimitiveRenderData *prd,const uint32_t instance_count,const uint32_t first_instance)
|
2024-04-26 00:41:53 +08:00
|
|
|
|
{
|
2024-05-30 13:14:13 +08:00
|
|
|
|
if(!pdb||!prd)
|
2024-05-25 22:08:01 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2024-05-30 13:14:13 +08:00
|
|
|
|
if (pdb->ibo)
|
2024-05-28 23:10:50 +08:00
|
|
|
|
vkCmdDrawIndexed( cmd_buf,
|
|
|
|
|
prd->index_count,
|
|
|
|
|
instance_count,
|
|
|
|
|
prd->first_index,
|
|
|
|
|
prd->vertex_offset, //这里的vertexOffset是针对所有VAB的
|
|
|
|
|
first_instance); //这里的first_instance针对的是instance Rate更新的VAB的起始实例数,不是指instance批量渲染
|
2024-04-26 00:41:53 +08:00
|
|
|
|
else
|
2024-05-28 23:10:50 +08:00
|
|
|
|
vkCmdDraw( cmd_buf,
|
|
|
|
|
prd->vertex_count,
|
|
|
|
|
instance_count,
|
|
|
|
|
prd->vertex_offset,
|
|
|
|
|
first_instance);
|
2024-04-26 00:41:53 +08:00
|
|
|
|
}
|
2024-04-27 01:07:44 +08:00
|
|
|
|
|
2024-05-25 22:08:01 +08:00
|
|
|
|
//void RenderCmdBuffer::DrawIndexed(const IBAccess *iba,const uint32_t instance_count)
|
|
|
|
|
//{
|
|
|
|
|
// if(!iba||instance_count<=0)return;
|
|
|
|
|
//
|
|
|
|
|
// vkCmdBindIndexBuffer(cmd_buf,
|
|
|
|
|
// iba->buffer->GetBuffer(),
|
|
|
|
|
// iba->start*iba->buffer->GetStride(),
|
|
|
|
|
// VkIndexType(iba->buffer->GetType()));
|
|
|
|
|
//
|
|
|
|
|
// vkCmdDrawIndexed(cmd_buf,
|
|
|
|
|
// iba->count,
|
|
|
|
|
// instance_count,
|
|
|
|
|
// 0, //first index
|
|
|
|
|
// 0, //vertex offset
|
|
|
|
|
// 0); //first instance
|
|
|
|
|
//}
|
2020-10-28 19:49:57 +08:00
|
|
|
|
VK_NAMESPACE_END
|