2019-04-09 02:02:43 +08:00
|
|
|
|
#ifndef HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
|
|
|
|
|
#define HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
|
|
|
|
|
|
2019-05-07 12:46:25 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VK.h>
|
2019-05-28 15:07:38 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKPipeline.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKDescriptorSets.h>
|
2019-04-09 02:02:43 +08:00
|
|
|
|
VK_NAMESPACE_BEGIN
|
2019-05-28 21:26:18 +08:00
|
|
|
|
//push constant 一般只有128/256字节,仅能存在矩阵。
|
|
|
|
|
//所以我们将每个对象的独立变换矩阵存在push constant中
|
2019-04-18 22:10:24 +08:00
|
|
|
|
class CommandBuffer
|
|
|
|
|
{
|
|
|
|
|
VkDevice device;
|
|
|
|
|
VkCommandPool pool;
|
|
|
|
|
VkCommandBuffer cmd_buf;
|
2019-04-16 13:21:21 +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
|
|
|
|
|
2019-05-28 15:07:38 +08:00
|
|
|
|
VkPipelineLayout pipeline_layout;
|
|
|
|
|
|
2019-04-18 22:10:24 +08:00
|
|
|
|
public:
|
2019-04-16 13:21:21 +08:00
|
|
|
|
|
2019-07-10 21:00:36 +08:00
|
|
|
|
CommandBuffer(VkDevice dev,const VkExtent2D &extent,const uint32_t att_count,VkCommandPool cp,VkCommandBuffer cb);
|
2019-04-18 22:10:24 +08:00
|
|
|
|
~CommandBuffer();
|
2019-04-16 02:23:03 +08:00
|
|
|
|
|
2019-04-19 20:23:14 +08:00
|
|
|
|
operator VkCommandBuffer(){return cmd_buf;}
|
2019-05-05 11:54:49 +08:00
|
|
|
|
operator const VkCommandBuffer()const{return cmd_buf;}
|
2019-05-18 15:41:49 +08:00
|
|
|
|
operator const VkCommandBuffer *()const{return &cmd_buf;}
|
2019-04-19 20:23:14 +08:00
|
|
|
|
|
2019-04-18 22:10:24 +08:00
|
|
|
|
void SetRenderArea(const VkRect2D &ra){render_area=ra;}
|
2019-07-12 17:33:38 +08:00
|
|
|
|
void SetViewport(const VkViewport &vp){viewport=vp;}
|
2019-07-13 02:37:19 +08:00
|
|
|
|
void SetClearColor(uint32_t index,float r,float g,float b,float a=1.0f)
|
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->color.float32[0]=r;
|
|
|
|
|
cv->color.float32[1]=g;
|
|
|
|
|
cv->color.float32[2]=b;
|
|
|
|
|
cv->color.float32[3]=a;
|
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开始后即不可改变
|
|
|
|
|
|
2019-04-29 14:53:56 +08:00
|
|
|
|
bool Begin();
|
2019-05-18 15:41:49 +08:00
|
|
|
|
|
|
|
|
|
void PipelineBarrier(
|
|
|
|
|
VkPipelineStageFlags srcStageMask,
|
|
|
|
|
VkPipelineStageFlags dstStageMask,
|
|
|
|
|
VkDependencyFlags dependencyFlags,
|
|
|
|
|
uint32_t memoryBarrierCount,
|
2019-06-21 18:18:48 +08:00
|
|
|
|
const VkMemoryBarrier * pMemoryBarriers,
|
2019-05-18 15:41:49 +08:00
|
|
|
|
uint32_t bufferMemoryBarrierCount,
|
2019-06-21 18:18:48 +08:00
|
|
|
|
const VkBufferMemoryBarrier * pBufferMemoryBarriers,
|
2019-05-18 15:41:49 +08:00
|
|
|
|
uint32_t imageMemoryBarrierCount,
|
2019-06-21 18:18:48 +08:00
|
|
|
|
const VkImageMemoryBarrier * pImageMemoryBarriers)
|
2019-05-18 15:41:49 +08:00
|
|
|
|
{
|
|
|
|
|
vkCmdPipelineBarrier(cmd_buf,srcStageMask,dstStageMask,dependencyFlags,memoryBarrierCount,pMemoryBarriers,bufferMemoryBarrierCount,pBufferMemoryBarriers,imageMemoryBarrierCount,pImageMemoryBarriers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CopyBufferToImage(
|
|
|
|
|
VkBuffer srcBuffer,
|
|
|
|
|
VkImage dstImage,
|
|
|
|
|
VkImageLayout dstImageLayout,
|
|
|
|
|
uint32_t regionCount,
|
2019-06-21 16:44:22 +08:00
|
|
|
|
const VkBufferImageCopy * pRegions)
|
2019-05-18 15:41:49 +08:00
|
|
|
|
{
|
|
|
|
|
vkCmdCopyBufferToImage(cmd_buf,srcBuffer,dstImage,dstImageLayout,regionCount,pRegions);
|
2019-06-21 16:44:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CopyImageToBuffer(
|
|
|
|
|
VkImage srcImage,
|
|
|
|
|
VkImageLayout srcImageLayout,
|
|
|
|
|
VkBuffer dstBuffer,
|
|
|
|
|
uint32_t regionCount,
|
|
|
|
|
const VkBufferImageCopy * pRegions)
|
|
|
|
|
{
|
|
|
|
|
vkCmdCopyImageToBuffer(cmd_buf,srcImage,srcImageLayout,dstBuffer,regionCount,pRegions);
|
2019-05-18 15:41:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 20:22:29 +08:00
|
|
|
|
bool BeginRenderPass(VkRenderPass rp,VkFramebuffer fb);
|
2019-07-16 21:32:29 +08:00
|
|
|
|
bool BeginRenderPass(RenderTarget *rt);
|
2019-05-28 15:07:38 +08:00
|
|
|
|
|
|
|
|
|
bool Bind(Pipeline *p)
|
|
|
|
|
{
|
|
|
|
|
if(!p)return(false);
|
|
|
|
|
|
|
|
|
|
vkCmdBindPipeline(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,*p);
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Bind(DescriptorSets *dsl)
|
|
|
|
|
{
|
|
|
|
|
if(!dsl)return(false);
|
|
|
|
|
|
|
|
|
|
pipeline_layout=dsl->GetPipelineLayout();
|
|
|
|
|
|
|
|
|
|
vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,pipeline_layout,0,1,dsl->GetDescriptorSets(),0,nullptr);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 19:40:08 +08:00
|
|
|
|
void PushConstants(ShaderStageBit 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
|
|
|
|
|
2019-05-29 21:48:56 +08:00
|
|
|
|
void PushConstants(const PushConstant *pc){vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,0,sizeof(PushConstant),pc);}
|
2019-05-28 21:26:18 +08:00
|
|
|
|
|
2019-04-28 17:02:38 +08:00
|
|
|
|
bool Bind(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
|
|
|
|
|
2019-05-28 16:50: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,const uint32_t first_vertex, const uint32_t first_instance)
|
|
|
|
|
{
|
|
|
|
|
vkCmdDraw(cmd_buf,vertex_count,instance_count,first_vertex,first_instance);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawIndexed(const uint32_t index_count, const uint32_t instance_count,const uint32_t first_index, const uint32_t vertex_offset,const uint32_t first_instance)
|
|
|
|
|
{
|
|
|
|
|
vkCmdDrawIndexed(cmd_buf,index_count,instance_count,first_index,vertex_offset,first_instance);
|
|
|
|
|
}
|
2019-04-25 11:42:00 +08:00
|
|
|
|
|
2020-01-21 18:57:07 +08:00
|
|
|
|
void NextSubpass(){vkCmdNextSubpass(cmd_buf,VK_SUBPASS_CONTENTS_INLINE);}
|
|
|
|
|
|
2019-05-28 15:07:38 +08:00
|
|
|
|
void EndRenderPass(){vkCmdEndRenderPass(cmd_buf);}
|
|
|
|
|
bool End(){return(vkEndCommandBuffer(cmd_buf)==VK_SUCCESS);}
|
2019-04-18 22:10:24 +08:00
|
|
|
|
};//class CommandBuffer
|
2019-04-09 02:02:43 +08:00
|
|
|
|
VK_NAMESPACE_END
|
|
|
|
|
#endif//HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
|