ULRE/inc/hgl/graph/VKCommandBuffer.h

150 lines
5.4 KiB
C
Raw Normal View History

#ifndef HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
#define HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE
#include<hgl/graph/VK.h>
#include<hgl/graph/VKPipeline.h>
#include<hgl/graph/VKDescriptorSets.h>
VK_NAMESPACE_BEGIN
//push constant 一般只有128/256字节仅能存在矩阵。
//所以我们将每个对象的独立变换矩阵存在push constant中
2020-10-21 12:39:22 +08:00
class GPUCmdBuffer
2019-04-18 22:10:24 +08:00
{
protected:
2019-04-18 22:10:24 +08:00
VkDevice device;
VkCommandPool pool;
VkCommandBuffer cmd_buf;
2019-04-16 13:21:21 +08:00
public:
GPUCmdBuffer(VkDevice dev,VkCommandPool cp,VkCommandBuffer cb);
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);}
};//class GPUCmdBuffer
class RenderCommand:public GPUCmdBuffer
{
uint32_t cv_count;
VkClearValue *clear_values;
2019-04-18 22:10:24 +08:00
VkRect2D render_area;
VkViewport viewport;
2019-04-16 13:21:21 +08:00
2020-07-20 19:17:17 +08:00
float default_line_width;
Framebuffer *fbo;
RenderPassBeginInfo rp_begin;
VkPipelineLayout pipeline_layout;
void SetFBO(Framebuffer *);
2019-04-16 13:21:21 +08:00
public:
RenderCommand(VkDevice dev,VkCommandPool cp,VkCommandBuffer cb);
~RenderCommand();
2019-04-18 22:10:24 +08:00
void SetRenderArea(const VkRect2D &ra){render_area=ra;}
void SetRenderArea(const VkExtent2D &);
void SetViewport(const VkViewport &vp){viewport=vp;}
void SetClearColor(uint32_t index,float r,float g,float b,float a=1.0f)
{
if(index>=cv_count)return;
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;
}
void SetClearDepthStencil(uint32_t index,float d=1.0f,float s=0)
{
if(index>=cv_count)return;
VkClearValue *cv=clear_values+index;
cv->depthStencil.depth=d;
cv->depthStencil.stencil=s;
}
2019-04-25 11:42:00 +08:00
//以上设定在Begin开始后即不可改变
bool BindFramebuffer(RenderPass *rp,Framebuffer *fb);
bool BeginRenderPass();
bool BindPipeline(Pipeline *p)
{
if(!p)return(false);
vkCmdBindPipeline(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,*p);
return(true);
}
bool BindDescriptorSets(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
}
void PushConstants(const void *data,const uint32_t size){vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,0,size,data);}
bool BindVAB(RenderableInstance *);
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);}
2020-10-19 22:26:42 +08:00
public: //draw
2019-05-28 16:50:22 +08:00
2020-10-19 22:26:42 +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);}
template<typename ...ARGS> void Draw (ARGS...args) {vkCmdDraw(cmd_buf,args...);}
template<typename ...ARGS> void DrawIndexed(ARGS...args) {vkCmdDrawIndexed(cmd_buf,args...);}
2019-04-25 11:42:00 +08:00
void NextSubpass(){vkCmdNextSubpass(cmd_buf,VK_SUBPASS_CONTENTS_INLINE);}
void EndRenderPass(){vkCmdEndRenderPass(cmd_buf);}
};//class RenderCommand:public GPUCmdBuffer
class TextureCommand:public GPUCmdBuffer
{
public:
using GPUCmdBuffer::GPUCmdBuffer;
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...);}
};//class TextureCommand:public GPUCmdBuffer
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_COMMAND_BUFFER_INCLUDE