2019-04-09 02:02:43 +08:00
|
|
|
|
#ifndef HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
|
|
|
|
|
#define HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
|
|
|
|
|
|
2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VK.h>
|
|
|
|
|
#include<hgl/graph/VKPipeline.h>
|
2023-02-13 11:50:55 +08:00
|
|
|
|
#include<hgl/graph/VKDescriptorSet.h>
|
2023-02-19 19:28:47 +08:00
|
|
|
|
#include<hgl/color/Color4f.h>
|
2019-04-09 02:02:43 +08:00
|
|
|
|
VK_NAMESPACE_BEGIN
|
2020-10-21 12:39:22 +08:00
|
|
|
|
class GPUCmdBuffer
|
2019-04-18 22:10:24 +08:00
|
|
|
|
{
|
2020-10-28 12:30:44 +08:00
|
|
|
|
protected:
|
|
|
|
|
|
2020-11-16 16:42:20 +08:00
|
|
|
|
const GPUDeviceAttribute *dev_attr;
|
|
|
|
|
|
2019-04-18 22:10:24 +08:00
|
|
|
|
VkCommandBuffer cmd_buf;
|
2019-04-16 13:21:21 +08:00
|
|
|
|
|
2020-10-28 12:30:44 +08:00
|
|
|
|
public:
|
|
|
|
|
|
2020-11-16 16:42:20 +08:00
|
|
|
|
GPUCmdBuffer(const GPUDeviceAttribute *attr,VkCommandBuffer cb);
|
2020-10-28 12:30:44 +08:00
|
|
|
|
virtual ~GPUCmdBuffer();
|
|
|
|
|
|
|
|
|
|
operator VkCommandBuffer(){return cmd_buf;}
|
|
|
|
|
operator const VkCommandBuffer()const{return cmd_buf;}
|
|
|
|
|
operator const VkCommandBuffer *()const{return &cmd_buf;}
|
|
|
|
|
|
|
|
|
|
bool Begin();
|
|
|
|
|
bool End(){return(vkEndCommandBuffer(cmd_buf)==VK_SUCCESS);}
|
2021-11-23 11:54:54 +08:00
|
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2022-09-27 19:41:28 +08:00
|
|
|
|
void SetDebugName(const UTF8String &);
|
|
|
|
|
void BeginRegion(const UTF8String &,const Color4f &);
|
2021-11-23 11:54:54 +08:00
|
|
|
|
void EndRegion();
|
|
|
|
|
#else
|
2022-09-27 19:41:28 +08:00
|
|
|
|
void BeginRegion(const UTF8String &,const Color4f &){}
|
2021-11-23 11:54:54 +08:00
|
|
|
|
void EndRegion(){}
|
|
|
|
|
#endif//_DEBUG
|
2020-10-28 12:30:44 +08:00
|
|
|
|
};//class GPUCmdBuffer
|
|
|
|
|
|
2020-11-09 15:37:00 +08:00
|
|
|
|
class RenderCmdBuffer:public GPUCmdBuffer
|
2020-10-28 12:30:44 +08:00
|
|
|
|
{
|
2019-07-10 21:00:36 +08:00
|
|
|
|
uint32_t cv_count;
|
|
|
|
|
VkClearValue *clear_values;
|
2019-04-18 22:10:24 +08:00
|
|
|
|
VkRect2D render_area;
|
2019-04-20 16:11:39 +08:00
|
|
|
|
VkViewport viewport;
|
2019-04-16 13:21:21 +08:00
|
|
|
|
|
2020-07-20 19:17:17 +08:00
|
|
|
|
float default_line_width;
|
2020-10-28 12:30:44 +08:00
|
|
|
|
|
|
|
|
|
Framebuffer *fbo;
|
|
|
|
|
RenderPassBeginInfo rp_begin;
|
2019-05-28 15:07:38 +08:00
|
|
|
|
VkPipelineLayout pipeline_layout;
|
|
|
|
|
|
2020-10-28 12:30:44 +08:00
|
|
|
|
void SetFBO(Framebuffer *);
|
2019-04-16 13:21:21 +08:00
|
|
|
|
|
2020-10-28 12:30:44 +08:00
|
|
|
|
public:
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
2020-11-16 16:42:20 +08:00
|
|
|
|
RenderCmdBuffer(const GPUDeviceAttribute *attr,VkCommandBuffer cb);
|
2020-11-09 15:37:00 +08:00
|
|
|
|
~RenderCmdBuffer();
|
2019-04-19 20:23:14 +08:00
|
|
|
|
|
2019-04-18 22:10:24 +08:00
|
|
|
|
void SetRenderArea(const VkRect2D &ra){render_area=ra;}
|
2020-06-11 19:23:51 +08:00
|
|
|
|
void SetRenderArea(const VkExtent2D &);
|
2019-07-12 17:33:38 +08:00
|
|
|
|
void SetViewport(const VkViewport &vp){viewport=vp;}
|
2020-06-11 19:23:51 +08:00
|
|
|
|
|
2022-03-11 17:43:07 +08:00
|
|
|
|
void SetClearColor(uint32_t index,const Color4f &cc)
|
2019-04-19 00:46:49 +08:00
|
|
|
|
{
|
2019-07-13 02:37:19 +08:00
|
|
|
|
if(index>=cv_count)return;
|
2019-07-10 21:00:36 +08:00
|
|
|
|
|
2022-03-11 17:49:47 +08:00
|
|
|
|
hgl_cpy(clear_values[index].color.float32,cc.rgba,4);
|
2019-04-19 00:46:49 +08:00
|
|
|
|
}
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
2019-07-13 02:37:19 +08:00
|
|
|
|
void SetClearDepthStencil(uint32_t index,float d=1.0f,float s=0)
|
2019-04-19 00:46:49 +08:00
|
|
|
|
{
|
2019-07-13 02:37:19 +08:00
|
|
|
|
if(index>=cv_count)return;
|
2019-07-10 21:00:36 +08:00
|
|
|
|
|
|
|
|
|
VkClearValue *cv=clear_values+index;
|
|
|
|
|
|
|
|
|
|
cv->depthStencil.depth=d;
|
|
|
|
|
cv->depthStencil.stencil=s;
|
2019-04-19 00:46:49 +08:00
|
|
|
|
}
|
2019-04-25 11:42:00 +08:00
|
|
|
|
|
2019-04-25 11:44:22 +08:00
|
|
|
|
//以上设定在Begin开始后即不可改变
|
|
|
|
|
|
2020-10-27 22:43:24 +08:00
|
|
|
|
bool BindFramebuffer(RenderPass *rp,Framebuffer *fb);
|
2019-05-28 15:07:38 +08:00
|
|
|
|
|
2020-10-30 18:59:58 +08:00
|
|
|
|
bool BeginRenderPass();
|
2022-08-18 19:01:08 +08:00
|
|
|
|
void NextSubpass(){vkCmdNextSubpass(cmd_buf,VK_SUBPASS_CONTENTS_INLINE);}
|
|
|
|
|
void EndRenderPass(){vkCmdEndRenderPass(cmd_buf);}
|
|
|
|
|
|
|
|
|
|
void BeginRendering(const VkRenderingInfoKHR *ri)
|
|
|
|
|
{
|
|
|
|
|
if(!ri)return;
|
|
|
|
|
|
|
|
|
|
vkCmdBeginRenderingKHR(cmd_buf,ri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EndRendering()
|
|
|
|
|
{
|
|
|
|
|
vkCmdEndRenderingKHR(cmd_buf);
|
|
|
|
|
}
|
2020-10-28 12:30:44 +08:00
|
|
|
|
|
2020-09-21 19:05:25 +08:00
|
|
|
|
bool BindPipeline(Pipeline *p)
|
2019-05-28 15:07:38 +08:00
|
|
|
|
{
|
|
|
|
|
if(!p)return(false);
|
|
|
|
|
|
|
|
|
|
vkCmdBindPipeline(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,*p);
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 11:48:53 +08:00
|
|
|
|
bool BindDescriptorSets(DescriptorSet *dsl)
|
2019-05-28 15:07:38 +08:00
|
|
|
|
{
|
|
|
|
|
if(!dsl)return(false);
|
|
|
|
|
|
|
|
|
|
pipeline_layout=dsl->GetPipelineLayout();
|
|
|
|
|
|
2021-06-16 20:29:25 +08:00
|
|
|
|
const VkDescriptorSet ds=dsl->GetDescriptorSet();
|
|
|
|
|
|
|
|
|
|
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,pipeline_layout,0,1,&ds,0,nullptr);
|
2019-05-28 15:07:38 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 11:48:53 +08:00
|
|
|
|
bool BindDescriptorSets(DescriptorSet *dsl,const uint32_t offset)
|
2020-11-30 16:52:38 +08:00
|
|
|
|
{
|
|
|
|
|
if(!dsl)return(false);
|
|
|
|
|
|
|
|
|
|
pipeline_layout=dsl->GetPipelineLayout();
|
|
|
|
|
|
2021-06-16 20:29:25 +08:00
|
|
|
|
const VkDescriptorSet ds=dsl->GetDescriptorSet();
|
|
|
|
|
|
|
|
|
|
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,pipeline_layout,0,1,&ds,1,&offset);
|
2020-11-30 16:52:38 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 10:47:42 +08:00
|
|
|
|
bool BindDescriptorSets(VkPipelineLayout pipeline_layout,const uint32_t first_set,const VkDescriptorSet *ds_list,const uint32_t ds_count,const uint32_t *offset,const uint32_t offset_count)
|
2021-06-16 20:29:25 +08:00
|
|
|
|
{
|
|
|
|
|
if(!ds_list||ds_count<=0)return(false);
|
|
|
|
|
|
2021-06-24 10:47:42 +08:00
|
|
|
|
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,pipeline_layout,first_set,ds_count,ds_list,offset_count,offset);
|
2021-06-16 20:29:25 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 21:06:38 +08:00
|
|
|
|
bool BindDescriptorSets(Renderable *ri);
|
2021-06-16 20:29:25 +08:00
|
|
|
|
|
2020-10-31 20:13:51 +08:00
|
|
|
|
bool PushDescriptorSet(VkPipelineLayout pipeline_layout,uint32_t set,uint32_t count,const VkWriteDescriptorSet *write_desc_set)
|
|
|
|
|
{
|
|
|
|
|
vkCmdPushDescriptorSetKHR(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,pipeline_layout,set,count,write_desc_set);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 20:19:25 +08:00
|
|
|
|
void PushConstants(VkShaderStageFlagBits shader_stage_bit,uint32_t offset,uint32_t size,const void *pValues)
|
2019-05-28 20:12:55 +08:00
|
|
|
|
{
|
2020-06-09 19:40:08 +08:00
|
|
|
|
vkCmdPushConstants(cmd_buf,pipeline_layout,(VkShaderStageFlagBits)shader_stage_bit,offset,size,pValues);
|
2019-05-28 20:12:55 +08:00
|
|
|
|
}
|
2019-05-28 15:07:38 +08:00
|
|
|
|
|
2020-10-31 20:13:51 +08:00
|
|
|
|
void PushConstants(const void *data,const uint32_t size) {vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,0, size,data);}
|
|
|
|
|
void PushConstants(const void *data,const uint32_t offset,const uint32_t size) {vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,offset, size,data);}
|
2019-05-28 21:26:18 +08:00
|
|
|
|
|
2023-05-06 19:29:38 +08:00
|
|
|
|
void BindVBO(const uint32_t first,const uint32_t count,const VkBuffer *vbo,const VkDeviceSize *offsets)
|
|
|
|
|
{
|
|
|
|
|
vkCmdBindVertexBuffers(cmd_buf,first,count,vbo,offsets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BindIBO(const IndexBufferData *);
|
|
|
|
|
|
2022-06-24 21:06:38 +08:00
|
|
|
|
bool BindVBO(Renderable *);
|
2019-04-25 11:42:00 +08:00
|
|
|
|
|
2019-05-28 16:50:22 +08:00
|
|
|
|
void SetViewport (uint32_t first,uint32_t count,const VkViewport *vp) {vkCmdSetViewport(cmd_buf,first,count,vp);}
|
|
|
|
|
void SetScissor (uint32_t first,uint32_t count,const VkRect2D *sci) {vkCmdSetScissor(cmd_buf,first,count,sci);}
|
2019-04-25 11:42:00 +08:00
|
|
|
|
|
2019-05-28 16:50:22 +08:00
|
|
|
|
void SetLineWidth (float line_width) {vkCmdSetLineWidth(cmd_buf,line_width);}
|
2019-04-25 11:38:57 +08:00
|
|
|
|
|
2019-05-28 16:50:22 +08:00
|
|
|
|
void SetDepthBias (float constant_factor,float clamp,float slope_factor) {vkCmdSetDepthBias(cmd_buf,constant_factor,clamp,slope_factor);}
|
|
|
|
|
void SetDepthBounds (float min_db,float max_db) {vkCmdSetDepthBounds(cmd_buf,min_db,max_db);}
|
|
|
|
|
void SetBlendConstants (const float constants[4]) {vkCmdSetBlendConstants(cmd_buf,constants);}
|
2019-04-25 11:38:57 +08:00
|
|
|
|
|
2019-05-28 16:50:22 +08:00
|
|
|
|
void SetStencilCompareMask (VkStencilFaceFlags faceMask,uint32_t compareMask) {vkCmdSetStencilCompareMask(cmd_buf,faceMask,compareMask);}
|
|
|
|
|
void SetStencilWriteMask (VkStencilFaceFlags faceMask,uint32_t compareMask) {vkCmdSetStencilWriteMask(cmd_buf,faceMask,compareMask);}
|
|
|
|
|
void SetStencilReference (VkStencilFaceFlags faceMask,uint32_t compareMask) {vkCmdSetStencilReference(cmd_buf,faceMask,compareMask);}
|
2019-04-19 00:46:49 +08:00
|
|
|
|
|
2020-10-19 22:26:42 +08:00
|
|
|
|
public: //draw
|
2019-05-28 16:50:22 +08:00
|
|
|
|
|
2022-06-24 16:28:22 +08:00
|
|
|
|
void Draw (const uint32_t vertex_count) {vkCmdDraw(cmd_buf,vertex_count,1,0,0);}
|
|
|
|
|
void DrawIndexed (const uint32_t index_count ) {vkCmdDrawIndexed(cmd_buf,index_count,1,0,0,0);}
|
|
|
|
|
void Draw (const uint32_t vertex_count,const uint32_t instance_count) {vkCmdDraw(cmd_buf,vertex_count,instance_count,0,0);}
|
|
|
|
|
void DrawIndexed (const uint32_t index_count ,const uint32_t instance_count) {vkCmdDrawIndexed(cmd_buf,index_count,instance_count,0,0,0);}
|
2020-10-19 22:26:42 +08:00
|
|
|
|
|
2020-11-16 16:42:20 +08:00
|
|
|
|
template<typename ...ARGS> void Draw (ARGS...args) {vkCmdDraw(cmd_buf,args...);}
|
|
|
|
|
template<typename ...ARGS> void DrawIndexed (ARGS...args) {vkCmdDrawIndexed(cmd_buf,args...);}
|
|
|
|
|
|
|
|
|
|
void DrawIndirect (VkBuffer,VkDeviceSize, uint32_t drawCount,uint32_t stride=sizeof(VkDrawIndirectCommand ));
|
|
|
|
|
void DrawIndexedIndirect(VkBuffer,VkDeviceSize, uint32_t drawCount,uint32_t stride=sizeof(VkDrawIndexedIndirectCommand ));
|
|
|
|
|
void DrawIndirect (VkBuffer buf, uint32_t drawCount,uint32_t stride=sizeof(VkDrawIndirectCommand )){return DrawIndirect( buf,0,drawCount,stride);}
|
|
|
|
|
void DrawIndexedIndirect(VkBuffer buf, uint32_t drawCount,uint32_t stride=sizeof(VkDrawIndexedIndirectCommand )){return DrawIndexedIndirect( buf,0,drawCount,stride);}
|
2019-04-25 11:42:00 +08:00
|
|
|
|
|
2022-01-24 19:48:54 +08:00
|
|
|
|
public: //dynamic state
|
2020-11-09 15:37:00 +08:00
|
|
|
|
};//class RenderCmdBuffer:public GPUCmdBuffer
|
2020-10-28 12:30:44 +08:00
|
|
|
|
|
2020-11-09 15:37:00 +08:00
|
|
|
|
class TextureCmdBuffer:public GPUCmdBuffer
|
2020-10-28 12:30:44 +08:00
|
|
|
|
{
|
2021-12-15 14:24:35 +08:00
|
|
|
|
VkImageMemoryBarrier imageMemoryBarrier;
|
|
|
|
|
|
2020-10-28 12:30:44 +08:00
|
|
|
|
public:
|
|
|
|
|
|
2021-12-15 14:24:35 +08:00
|
|
|
|
TextureCmdBuffer(const GPUDeviceAttribute *attr,VkCommandBuffer cb):GPUCmdBuffer(attr,cb)
|
|
|
|
|
{
|
|
|
|
|
imageMemoryBarrier.sType=VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
|
|
|
imageMemoryBarrier.pNext=nullptr;
|
|
|
|
|
imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
|
|
|
imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
|
|
|
}
|
2020-10-28 12:30:44 +08:00
|
|
|
|
|
|
|
|
|
template<typename ...ARGS> void PipelineBarrier (ARGS...args){vkCmdPipelineBarrier (cmd_buf,args...);}
|
|
|
|
|
template<typename ...ARGS> void CopyBufferToImage (ARGS...args){vkCmdCopyBufferToImage(cmd_buf,args...);}
|
|
|
|
|
template<typename ...ARGS> void CopyImageToBuffer (ARGS...args){vkCmdCopyImageToBuffer(cmd_buf,args...);}
|
|
|
|
|
template<typename ...ARGS> void BlitImage (ARGS...args){vkCmdBlitImage (cmd_buf,args...);}
|
2021-12-15 14:24:35 +08:00
|
|
|
|
|
|
|
|
|
void ImageMemoryBarrier(VkImage image,
|
|
|
|
|
VkPipelineStageFlags srcStageMask,
|
|
|
|
|
VkPipelineStageFlags dstStageMask,
|
|
|
|
|
VkAccessFlags srcAccessMask,
|
|
|
|
|
VkAccessFlags dstAccessMask,
|
|
|
|
|
VkImageLayout oldImageLayout,
|
|
|
|
|
VkImageLayout newImageLayout,
|
|
|
|
|
VkImageSubresourceRange subresourceRange)
|
|
|
|
|
{
|
|
|
|
|
imageMemoryBarrier.srcAccessMask = srcAccessMask;
|
|
|
|
|
imageMemoryBarrier.dstAccessMask = dstAccessMask;
|
|
|
|
|
imageMemoryBarrier.oldLayout = oldImageLayout;
|
|
|
|
|
imageMemoryBarrier.newLayout = newImageLayout;
|
|
|
|
|
imageMemoryBarrier.image = image;
|
|
|
|
|
imageMemoryBarrier.subresourceRange = subresourceRange;
|
|
|
|
|
|
|
|
|
|
vkCmdPipelineBarrier( cmd_buf,
|
|
|
|
|
srcStageMask,
|
|
|
|
|
dstStageMask,
|
|
|
|
|
0,
|
|
|
|
|
0, nullptr,
|
|
|
|
|
0, nullptr,
|
|
|
|
|
1, &imageMemoryBarrier);
|
|
|
|
|
}
|
2020-11-09 15:37:00 +08:00
|
|
|
|
};//class TextureCmdBuffer:public GPUCmdBuffer
|
2019-04-09 02:02:43 +08:00
|
|
|
|
VK_NAMESPACE_END
|
|
|
|
|
#endif//HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
|