finished first edition of the TextRenderable class.

This commit is contained in:
hyzboy 2020-08-08 20:12:37 +08:00
parent a6f1657cac
commit 90314c4b97
12 changed files with 129 additions and 124 deletions

2
CMCore

@ -1 +1 @@
Subproject commit 4c22ba5a6066bd60badb65b6ce745562664c1705
Subproject commit 46574e0f49560525442b9d46575aed58678b965a

View File

@ -6,6 +6,7 @@
#include<hgl/graph/TileData.h>
#include<hgl/graph/font/TileFont.h>
#include<hgl/graph/font/TextLayout.h>
#include<hgl/graph/font/TextRenderable.h>
#include"VulkanAppFramework.h"
#include<hgl/graph/vulkan/VKTexture.h>
@ -31,7 +32,6 @@ private:
vulkan::Material * material =nullptr;
vulkan::Sampler * sampler =nullptr;
vulkan::MaterialInstance * material_instance =nullptr;
vulkan::Renderable * render_obj =nullptr;
vulkan::Buffer * ubo_world_matrix =nullptr;
vulkan::Buffer * ubo_color =nullptr;
@ -41,16 +41,15 @@ private:
FontSource * font_source =nullptr;
TileFont * tile_font;
TileFont * tile_font =nullptr;
TextLayout tl_engine; ///<文本排版引擎
RenderableCreater * text_rc =nullptr;
TextRenderable * text_render_obj =nullptr;
public:
~TestApp()
{
SAFE_CLEAR(text_rc);
SAFE_CLEAR(tile_font);
}
@ -136,10 +135,11 @@ private:
tla.char_layout_attr=&cla;
tla.line_gap=0.2f;
text_rc=new RenderableCreater(db,material);
text_render_obj=new TextRenderable(device,material);
db->Add(text_render_obj);
tl_engine.Set(tile_font->GetFontSource());
tl_engine.Set(text_rc);
tl_engine.Set(&tla);
tl_engine.SetTextDirection(0);
tl_engine.Set(TextAlign::Left);
@ -152,13 +152,7 @@ private:
UTF16String str;
LoadStringFromTextFile(str,OS_TEXT("res/text/DaoDeBible.txt"));
if(tl_engine.SimpleLayout(tile_font,str)>0)
{
render_obj=text_rc->Finish();
return(true);
}
return(false);
return(tl_engine.SimpleLayout(text_render_obj,tile_font,str)>0);
}
public:
@ -183,7 +177,7 @@ public:
if(!InitTextRenderable())
return(false);
BuildCommandBuffer(pipeline,material_instance,render_obj);
BuildCommandBuffer(pipeline,material_instance,text_render_obj);
return(true);
}
@ -197,7 +191,7 @@ public:
ubo_world_matrix->Write(&cam.matrix);
BuildCommandBuffer(pipeline,material_instance,render_obj);
BuildCommandBuffer(pipeline,material_instance,text_render_obj);
}
};//class TestApp:public VulkanApplicationFramework

View File

@ -8,27 +8,6 @@ namespace hgl
{
namespace graph
{
/**
* shader会使用这些名称
*/
namespace VertexAttribName
{
#define VAN_DEFINE(name) constexpr char name[]=#name;
VAN_DEFINE(Vertex)
VAN_DEFINE(Normal)
VAN_DEFINE(Color)
VAN_DEFINE(Tangent)
VAN_DEFINE(Bitangent)
VAN_DEFINE(TexCoord)
VAN_DEFINE(Metallic)
VAN_DEFINE(Specular)
VAN_DEFINE(Roughness)
VAN_DEFINE(Emission)
#undef VAN_DEFINE
}//namespace VertexAttribName
#define VAN VertexAttribName
struct ShaderStageBind
{
AnsiString name;

View File

@ -6,6 +6,27 @@ namespace hgl
{
namespace graph
{
/**
* shader会使用这些名称
*/
namespace VertexAttribName
{
#define VAN_DEFINE(name) constexpr char name[]=#name;
VAN_DEFINE(Vertex)
VAN_DEFINE(Normal)
VAN_DEFINE(Color)
VAN_DEFINE(Tangent)
VAN_DEFINE(Bitangent)
VAN_DEFINE(TexCoord)
VAN_DEFINE(Metallic)
VAN_DEFINE(Specular)
VAN_DEFINE(Roughness)
VAN_DEFINE(Emission)
#undef VAN_DEFINE
}//namespace VertexAttribName
#define VAN VertexAttribName
/**
*
*/

View File

@ -5,11 +5,13 @@
#include<hgl/graph/font/FontSource.h>
#include<hgl/graph/RenderableCreater.h>
#include<hgl/graph/TileData.h>
#include<hgl/type/MemBlock.h>
namespace hgl
{
namespace graph
{
class TileFont;
class TextRenderable;
/**
*
@ -86,7 +88,6 @@ namespace hgl
protected:
FontSource *font_source;
RenderableCreater *rc;
TextLayoutAttributes tla;
protected:
@ -121,9 +122,9 @@ namespace hgl
int sl_v_r2l();
int sl_v_l2r();
template<typename T> int SimpleLayout(TileFont *,const BaseString<T> &); ///<简易排版
template<typename T> int SimpleLayout(TextRenderable *,TileFont *,const BaseString<T> &); ///<简易排版
// template<typename T> int SimpleLayout(TileFont *,const StringList<BaseString<T>> &); ///<简易排版
// template<typename T> int SimpleLayout(TileFont *,const StringList<BaseString<T>> &); ///<简易排版
protected:
@ -139,25 +140,22 @@ namespace hgl
protected:
VB4f *vertex;
VB4f *tex_coord;
TextRenderable *text_render_obj;
MemBlock<float> vertex;
MemBlock<float> tex_coord;
public:
TextLayout()
{
rc=nullptr;
direction.text_direction=0;
draw_chars_count=0;
vertex =nullptr;
tex_coord =nullptr;
text_render_obj =nullptr;
}
virtual ~TextLayout()=default;
void Set (RenderableCreater *_rc) {if(_rc)rc=_rc;}
void Set (const TextLayoutAttributes *_tla) {if(_tla)memcpy(&tla,_tla,sizeof(TextLayoutAttributes));}
void Set (FontSource *fs) {if(fs)font_source=fs;}
void SetTextDirection (const uint8 &td) {tla.text_direction=td;}
@ -165,10 +163,10 @@ namespace hgl
void SetMaxWidth (const float mw) {tla.max_width=mw;}
void SetMaxHeight (const float mh) {tla.max_height=mh;}
virtual bool Init (); ///<初始化排版
virtual bool Init (); ///<初始化排版
int SimpleLayout (TileFont *,const UTF16String &); ///<简易排版
int SimpleLayout (TileFont *,const UTF32String &); ///<简易排版
int SimpleLayout (TextRenderable *,TileFont *,const UTF16String &); ///<简易排版
int SimpleLayout (TextRenderable *,TileFont *,const UTF32String &); ///<简易排版
// int SimpleLayout (TileFont *,const UTF16StringList &); ///<简易排版
// int SimpleLayout (TileFont *,const UTF32StringList &); ///<简易排版

View File

@ -11,22 +11,25 @@ namespace hgl
*/
class TextRenderable:public vulkan::Renderable
{
vulkan::Device *device;
vulkan::Device * device;
vulkan::Material * mtl;
vulkan::Renderable *render_obj;
uint max_count; ///<缓冲区最大容量
uint32 max_count; ///<缓冲区最大容量
uint32 cur_count; ///<当前容量
vulkan::VAB *vab_vertex;
vulkan::VAB *vab_tex_coord;
vulkan::VAB * vab_vertex;
vulkan::VAB * vab_tex_coord;
public:
TextRenderable(vulkan::Device *,int mc=1024);
TextRenderable(vulkan::Device *,vulkan::Material *,uint mc=1024);
virtual ~TextRenderable();
void SetMaxCount(int);
public:
void SetCharCount (const uint);
bool WriteVertex (const float *fp);
bool WriteTexCoord (const float *fp);
};//class TextRenderable:public vulkan::Renderable
}//namespace graph
}//namespace hgl

View File

@ -38,12 +38,13 @@ public:
Memory * GetMemory (){return buf.memory;}
const VkDescriptorBufferInfo * GetBufferInfo()const{return &buf.info;}
void * Map() {return buf.memory->Map();}
virtual void * Map(VkDeviceSize start,VkDeviceSize size) {return buf.memory->Map(start,size);}
void Unmap() {return buf.memory->Unmap();}
void * Map () {return buf.memory->Map();}
virtual void * Map (VkDeviceSize start,VkDeviceSize size) {return buf.memory->Map(start,size);}
void Unmap () {return buf.memory->Unmap();}
bool Write(const void *ptr,uint32_t start,uint32_t size) {return buf.memory->Write(ptr,start,size);}
bool Write(const void *ptr) {return buf.memory->Write(ptr);}
bool Write (const void *ptr,uint32_t start,uint32_t size) {return buf.memory->Write(ptr,start,size);}
bool Write (const void *ptr,uint32_t size) {return buf.memory->Write(ptr,0,size);}
bool Write (const void *ptr) {return buf.memory->Write(ptr);}
};//class Buffer
class VertexAttribBuffer:public Buffer

View File

@ -38,15 +38,16 @@ public:
const uint32_t GetTypeIndex ()const{return index;}
const uint32_t GetProperties ()const{return properties;}
void *Map();
void *Map(VkDeviceSize offset,VkDeviceSize size);
void Unmap();
void * Map ();
void * Map (VkDeviceSize offset,VkDeviceSize size);
void Unmap ();
bool Write(const void *ptr,VkDeviceSize start,VkDeviceSize size);
bool Write(const void *ptr){return Write(ptr,0,req.size);}
bool Write (const void *ptr,VkDeviceSize start, VkDeviceSize size);
bool Write (const void *ptr, VkDeviceSize size) {return Write(ptr,0,size);}
bool Write (const void *ptr) {return Write(ptr,0,req.size);}
bool BindBuffer(VkBuffer buffer);
bool BindImage(VkImage image);
bool BindBuffer (VkBuffer buffer);
bool BindImage (VkImage image);
};//class Memory
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_MEMORY_INCLUDE

View File

@ -19,12 +19,14 @@ class Renderable
VkBuffer *buf_list=nullptr;
VkDeviceSize *buf_offset=nullptr;
protected:
uint32_t draw_count;
IndexBuffer *indices_buffer=nullptr;
VkDeviceSize indices_offset=0;
private:
protected:
AABB BoundingBox;
@ -61,9 +63,8 @@ public:
public:
void SetDrawCount (const uint32_t dc){draw_count=dc;} ///<设置当前对象绘制需要多少个顶点
const uint32_t GetDrawCount ()const ///<取得当前对象绘制需要多少个顶点
void SetDrawCount(const uint32_t dc){draw_count=dc;} ///<设置当前对象绘制需要多少个顶点
virtual const uint32_t GetDrawCount()const ///<取得当前对象绘制需要多少个顶点
{
if(indices_buffer)
return indices_buffer->GetCount();

View File

@ -70,10 +70,13 @@ SET(TILE_FONT_SOURCE ${ROOT_INCLUDE_PATH}/hgl/graph/font/TileFont.h
SET(FONT_LAYOUT_SOURCE ${ROOT_INCLUDE_PATH}/hgl/graph/font/TextLayout.h
font/TextLayout.cpp)
SET(TEXT_RENDERABLE_SOURCE ${ROOT_INCLUDE_PATH}/hgl/graph/font/TextRenderable.h
font/TextRenderable.cpp)
SOURCE_GROUP("Font" FILES ${FONT_MANAGE_SOURCE})
SOURCE_GROUP("Font\\Source" FILES ${FONT_SOURCE})
SOURCE_GROUP("Font\\TileFont" FILES ${TILE_FONT_SOURCE})
SOURCE_GROUP("Font\\Layout" FILES ${FONT_LAYOUT_SOURCE})
SOURCE_GROUP("Font\\Layout" FILES ${FONT_LAYOUT_SOURCE} ${TEXT_RENDERABLE_SOURCE})
IF(WIN32)
SET(FONT_SOURCE_OS font/FontSourceWin.cpp
@ -102,4 +105,5 @@ add_cm_library(ULRE.SceneGraph "ULRE" ${SCENE_GRAPH_HEADER}
${FONT_SOURCE}
${FONT_SOURCE_OS}
${TILE_FONT_SOURCE}
${FONT_LAYOUT_SOURCE})
${FONT_LAYOUT_SOURCE}
${TEXT_RENDERABLE_SOURCE})

View File

@ -1,5 +1,6 @@
#include<hgl/graph/font/TextLayout.h>
#include<hgl/graph/font/TileFont.h>
#include<hgl/graph/font/TextRenderable.h>
#include<hgl/type/Extent.h>
namespace hgl
@ -8,9 +9,8 @@ namespace hgl
{
bool TextLayout::Init()
{
if(!rc
||(!tla.font_source&&!font_source)
||!tla.char_layout_attr)
if((!tla.font_source&&!font_source)
||!tla.char_layout_attr)
return(false);
direction.text_direction=tla.text_direction;
@ -173,8 +173,8 @@ namespace hgl
int cur_size=0;
int left=0,top=0;
float *tp=vertex->Get();
float *tcp=tex_coord->Get();
float *tp=vertex;
float *tcp=tex_coord;
for(int i=0;i<count;i++)
{
@ -215,9 +215,6 @@ namespace hgl
++cda;
}
tex_coord->End();
vertex->End();
return count;
}
@ -229,46 +226,57 @@ namespace hgl
* \r\n
*/
template<typename T>
int TextLayout::SimpleLayout(TileFont *tf,const BaseString<T> &str)
int TextLayout::SimpleLayout(TextRenderable *tr,TileFont *tf,const BaseString<T> &str)
{
if(!tr)
return(-1);
if(!tf||str.IsEmpty())
return(-1);
if(!preprocess<T>(tf,str.c_str(),str.Length()))
int max_chars=str.Length();
if(!preprocess<T>(tf,str.c_str(),max_chars))
return(-2);
if(draw_chars_count<=0) //可绘制字符为0这是全空格
return(-3);
if(!rc->Init(draw_chars_count)) //创建
return(-4);
vertex =rc->CreateVADA<VB4f>(VAN::Vertex);
tex_coord =rc->CreateVADA<VB4f>(VAN::TexCoord);
vertex .SetLength(max_chars*4);
tex_coord .SetLength(max_chars*4);
if(!vertex||!tex_coord)
return(-5);
int result;
if(direction.vertical)
{
if(direction.right_to_left)
return sl_v_r2l();
result=sl_v_r2l();
else
return sl_v_l2r();
result=sl_v_l2r();
}
else
{
if(direction.right_to_left)
return sl_h_r2l();
result=sl_h_r2l();
else
return sl_h_l2r();
result=sl_h_l2r();
}
return 0;
if(result>0)
{
tr->SetCharCount(result);
tr->WriteVertex(vertex);
tr->WriteTexCoord(tex_coord);
}
return result;
}
int TextLayout::SimpleLayout(TileFont *tf,const UTF16String &str){return this->SimpleLayout<u16char>(tf,str);}
int TextLayout::SimpleLayout(TileFont *tf,const UTF32String &str){return this->SimpleLayout<u32char>(tf,str);}
int TextLayout::SimpleLayout(TextRenderable *tr,TileFont *tf,const UTF16String &str){return this->SimpleLayout<u16char>(tr,tf,str);}
int TextLayout::SimpleLayout(TextRenderable *tr,TileFont *tf,const UTF32String &str){return this->SimpleLayout<u32char>(tr,tf,str);}
//template<typename T>
//int TextLayout::SimpleLayout(TileFont *tf,const StringList<BaseString<T>> &sl)

View File

@ -1,58 +1,53 @@
#include<hgl/graph/font/TextRenderable.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKMaterial.h>
namespace hgl
{
namespace graph
{
namespace
{
vulkan::Pipeline *text_pipeline=nullptr;
}//namespace
TextRenderable::TextRenderable(Device *dev,int mc)
TextRenderable::TextRenderable(vulkan::Device *dev,vulkan::Material *m,uint mc):vulkan::Renderable(m->GetVertexShaderModule(),mc)
{
device=dev;
mtl=m;
max_count=0;
render_obj=nullptr;
vab_vertex=nullptr;
vab_tex_coord=nullptr;
max_count=mc;
cur_count=mc;
SetMaxCount(mc);
}
TextRenderable::~TextRenderable()
{
SAFE_CLEAR(vab_tex_coord);
SAFE_CLEAR(vab_vertex);
SAFE_CLEAR(render_obj);
}
void TextRenderable::SetMaxCount(int mc)
void TextRenderable::SetCharCount(const uint cc)
{
if(mc<max_count)return;
this->draw_count=cc;
if(cc<=max_count)return;
max_count=mc;
max_count=power_to_2(cc);
if(vab_vertex)
{
delete vab_vertex;
if(vab_vertex)
delete vab_vertex;
vab_vertex =device->CreateVAB(VAF_VEC4,max_count);
render_obj->Set(VAN::Vertex,vab_vertex);
Set(VAN::Vertex,vab_vertex);
}
if(vab_tex_coord)
{
delete vab_tex_coord;
if(vab_tex_coord)
delete vab_tex_coord;
vab_tex_coord =device->CreateVAB(VAF_VEC4,max_count);
render_obj->Set(VAN::TexCoord,vab_tex_coord);
Set(VAN::TexCoord,vab_tex_coord);
}
}
bool TextRenderable::WriteVertex (const float *fp){if(!fp)return(false);if(!vab_vertex )return(false);return vab_vertex ->Write(fp,draw_count*4*sizeof(float));}
bool TextRenderable::WriteTexCoord (const float *fp){if(!fp)return(false);if(!vab_tex_coord)return(false);return vab_tex_coord ->Write(fp,draw_count*4*sizeof(float));}
}//namespace graph
}//namespace hgl