add VKPrimitive and TextPrimitive

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2022-06-24 17:51:39 +08:00
parent 0fca6b8fc4
commit 6e7144ffab
4 changed files with 254 additions and 0 deletions

View File

@ -0,0 +1,87 @@
#ifndef HGL_GRAPH_VULKAN_PRIMITIVE_INCLUDE
#define HGL_GRAPH_VULKAN_PRIMITIVE_INCLUDE
#include<hgl/graph/VKIndexBuffer.h>
#include<hgl/type/Map.h>
#include<hgl/type/String.h>
#include<hgl/math/Math.h>
#include<hgl/graph/AABB.h>
VK_NAMESPACE_BEGIN
/**
*
*/
class Primitive
{
struct VBOData
{
VBO *buf;
VkDeviceSize offset;
public:
CompOperatorMemcmp(const VBOData &);
};
Map<AnsiString,VBOData> buffer_list;
protected:
uint32_t draw_count;
IndexBuffer *indices_buffer=nullptr;
VkDeviceSize indices_offset=0;
protected:
AABB BoundingBox;
protected:
friend class RenderableNode;
uint ref_count=0;
uint RefInc(){return ++ref_count;}
uint RefDec(){return --ref_count;}
public:
Primitive(const uint32_t dc=0):draw_count(dc){}
virtual ~Primitive()=default;
const uint GetRefCount()const{return ref_count;}
void SetBoundingBox(const AABB &aabb){BoundingBox=aabb;}
const AABB & GetBoundingBox()const {return BoundingBox;}
bool Set(const AnsiString &name,VBO *vb,VkDeviceSize offset=0);
bool Set(IndexBuffer *ib,VkDeviceSize offset=0)
{
if(!ib)return(false);
indices_buffer=ib;
indices_offset=offset;
return(true);
}
public:
void SetDrawCount(const uint32_t dc){draw_count=dc;} ///<设置当前对象绘制需要多少个顶点
virtual const uint32_t GetDrawCount()const ///<取得当前对象绘制需要多少个顶点
{
if(indices_buffer)
return indices_buffer->GetCount();
return draw_count;
}
VBO * GetVBO (const AnsiString &,VkDeviceSize *);
VkBuffer GetBuffer (const AnsiString &,VkDeviceSize *);
const int GetBufferCount ()const {return buffer_list.GetCount();}
IndexBuffer * GetIndexBuffer () {return indices_buffer;}
const VkDeviceSize GetIndexBufferOffset()const {return indices_offset;}
};//class Primitive
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_PRIMITIVE_INCLUDE

View File

@ -0,0 +1,50 @@
#ifndef HGL_GRAPH_FONT_PRIMITIVE_INCLUDE
#define HGL_GRAPH_FONT_PRIMITIVE_INCLUDE
#include<hgl/graph/VKPrimitive.h>
namespace hgl
{
namespace graph
{
/**
*
*/
class TextPrimitive:public Primitive
{
GPUDevice * device;
Material * mtl;
uint max_count; ///<缓冲区最大容量
VBO * vbo_position;
VBO * vbo_tex_coord;
protected:
friend class TextLayout;
friend class TextRender;
SortedSets<u32char> chars_sets;
const SortedSets<u32char> &GetCharsSets()const{return chars_sets;}
void SetCharsSets(const SortedSets<u32char> &sl){chars_sets=sl;}
void ClearCharsSets(){chars_sets.Clear();}
private:
virtual ~TextPrimitive();
public:
TextPrimitive(GPUDevice *,Material *,uint mc=1024);
public:
void SetCharCount (const uint);
bool WriteVertex (const int16 *fp);
bool WriteTexCoord (const float *fp);
};//class TextPrimitive:public Renderable
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_FONT_PRIMITIVE_INCLUDE

View File

@ -0,0 +1,63 @@
#include<hgl/graph/VKPrimitive.h>
#include<hgl/graph/VKBuffer.h>
#include<hgl/graph/VKShaderModule.h>
#include<hgl/graph/VKVertexAttribBuffer.h>
VK_NAMESPACE_BEGIN
//bool Renderable::Set(const int stage_input_binding,VAB *vab,VkDeviceSize offset)
//{
// if(stage_input_binding<0||stage_input_binding>=buf_count||!vab)return(false);
//
// const VkVertexInputBindingDescription *desc=vertex_sm->GetDesc(stage_input_binding);
// const VkVertexInputAttributeDescription *attr=vertex_sm->GetAttr(stage_input_binding);
//
// if(vab->GetFormat()!=attr->format)return(false);
// if(vab->GetStride()!=desc->stride)return(false);
//
// //format信息来自于shader实际中可以不一样。但那样需要为每一个格式产生一个同样shader的material instance不同的格式又需要不同的pipeline我们不支持这种行为
//
// buf_list[stage_input_binding]=vab->GetBuffer();
// buf_offset[stage_input_binding]=offset;
//
// return(true);
//}
bool Primitive::Set(const AnsiString &name,VBO *vbo,VkDeviceSize offset)
{
if(!vbo)return(false);
if(buffer_list.KeyExist(name))return(false);
VBOData bd;
bd.buf=vbo;
bd.offset=offset;
buffer_list.Add(name,bd);
return(true);
}
VBO *Primitive::GetVBO(const AnsiString &name,VkDeviceSize *offset)
{
if(!offset)return(nullptr);
if(name.IsEmpty())return(nullptr);
VBOData bd;
if(buffer_list.Get(name,bd))
{
*offset=bd.offset;
return bd.buf;
}
return(nullptr);
}
VkBuffer Primitive::GetBuffer(const AnsiString &name,VkDeviceSize *offset)
{
VBO *vbo=GetVBO(name,offset);
if(vbo)return vbo->GetBuffer();
return(VK_NULL_HANDLE);
}
VK_NAMESPACE_END

View File

@ -0,0 +1,54 @@
#include<hgl/graph/font/TextPrimitive.h>
#include<hgl/graph/VKDevice.h>
#include<hgl/graph/VKMaterial.h>
#include<hgl/graph/VKVertexAttribBuffer.h>
namespace hgl
{
namespace graph
{
TextPrimitive::TextPrimitive(GPUDevice *dev,Material *m,uint mc):Primitive(mc)
{
device=dev;
mtl=m;
max_count=0;
vbo_position=nullptr;
vbo_tex_coord=nullptr;
}
TextPrimitive::~TextPrimitive()
{
SAFE_CLEAR(vbo_tex_coord);
SAFE_CLEAR(vbo_position);
}
void TextPrimitive::SetCharCount(const uint cc)
{
this->draw_count=cc;
if(cc<=max_count)return;
max_count=power_to_2(cc);
{
if(vbo_position)
delete vbo_position;
vbo_position =device->CreateVBO(VF_V4I16,max_count);
Set(VAN::Position,vbo_position);
}
{
if(vbo_tex_coord)
delete vbo_tex_coord;
vbo_tex_coord =device->CreateVBO(VF_V4F,max_count);
Set(VAN::TexCoord,vbo_tex_coord);
}
}
bool TextPrimitive::WriteVertex (const int16 *fp){if(!fp)return(false);if(!vbo_position )return(false);return vbo_position ->Write(fp,draw_count*4*sizeof(int16));}
bool TextPrimitive::WriteTexCoord (const float *fp){if(!fp)return(false);if(!vbo_tex_coord)return(false);return vbo_tex_coord ->Write(fp,draw_count*4*sizeof(float));}
}//namespace graph
}//namespace hgl