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

View File

@ -8,27 +8,6 @@ namespace hgl
{ {
namespace graph 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 struct ShaderStageBind
{ {
AnsiString name; AnsiString name;

View File

@ -6,6 +6,27 @@ namespace hgl
{ {
namespace graph 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/font/FontSource.h>
#include<hgl/graph/RenderableCreater.h> #include<hgl/graph/RenderableCreater.h>
#include<hgl/graph/TileData.h> #include<hgl/graph/TileData.h>
#include<hgl/type/MemBlock.h>
namespace hgl namespace hgl
{ {
namespace graph namespace graph
{ {
class TileFont; class TileFont;
class TextRenderable;
/** /**
* *
@ -86,7 +88,6 @@ namespace hgl
protected: protected:
FontSource *font_source; FontSource *font_source;
RenderableCreater *rc;
TextLayoutAttributes tla; TextLayoutAttributes tla;
protected: protected:
@ -121,7 +122,7 @@ namespace hgl
int sl_v_r2l(); int sl_v_r2l();
int sl_v_l2r(); 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>> &); ///<简易排版
@ -139,25 +140,22 @@ namespace hgl
protected: protected:
VB4f *vertex; TextRenderable *text_render_obj;
VB4f *tex_coord; MemBlock<float> vertex;
MemBlock<float> tex_coord;
public: public:
TextLayout() TextLayout()
{ {
rc=nullptr;
direction.text_direction=0; direction.text_direction=0;
draw_chars_count=0; draw_chars_count=0;
vertex =nullptr; text_render_obj =nullptr;
tex_coord =nullptr;
} }
virtual ~TextLayout()=default; 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 (const TextLayoutAttributes *_tla) {if(_tla)memcpy(&tla,_tla,sizeof(TextLayoutAttributes));}
void Set (FontSource *fs) {if(fs)font_source=fs;} void Set (FontSource *fs) {if(fs)font_source=fs;}
void SetTextDirection (const uint8 &td) {tla.text_direction=td;} void SetTextDirection (const uint8 &td) {tla.text_direction=td;}
@ -167,8 +165,8 @@ namespace hgl
virtual bool Init (); ///<初始化排版 virtual bool Init (); ///<初始化排版
int SimpleLayout (TileFont *,const UTF16String &); ///<简易排版 int SimpleLayout (TextRenderable *,TileFont *,const UTF16String &); ///<简易排版
int SimpleLayout (TileFont *,const UTF32String &); ///<简易排版 int SimpleLayout (TextRenderable *,TileFont *,const UTF32String &); ///<简易排版
// int SimpleLayout (TileFont *,const UTF16StringList &); ///<简易排版 // int SimpleLayout (TileFont *,const UTF16StringList &); ///<简易排版
// int SimpleLayout (TileFont *,const UTF32StringList &); ///<简易排版 // int SimpleLayout (TileFont *,const UTF32StringList &); ///<简易排版

View File

@ -11,22 +11,25 @@ namespace hgl
*/ */
class TextRenderable:public vulkan::Renderable class TextRenderable:public vulkan::Renderable
{ {
vulkan::Device *device; vulkan::Device * device;
vulkan::Material * mtl;
vulkan::Renderable *render_obj; uint max_count; ///<缓冲区最大容量
uint32 max_count; ///<缓冲区最大容量 vulkan::VAB * vab_vertex;
uint32 cur_count; ///<当前容量 vulkan::VAB * vab_tex_coord;
vulkan::VAB *vab_vertex;
vulkan::VAB *vab_tex_coord;
public: public:
TextRenderable(vulkan::Device *,int mc=1024); TextRenderable(vulkan::Device *,vulkan::Material *,uint mc=1024);
virtual ~TextRenderable(); 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 };//class TextRenderable:public vulkan::Renderable
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl

View File

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

View File

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

View File

@ -19,12 +19,14 @@ class Renderable
VkBuffer *buf_list=nullptr; VkBuffer *buf_list=nullptr;
VkDeviceSize *buf_offset=nullptr; VkDeviceSize *buf_offset=nullptr;
protected:
uint32_t draw_count; uint32_t draw_count;
IndexBuffer *indices_buffer=nullptr; IndexBuffer *indices_buffer=nullptr;
VkDeviceSize indices_offset=0; VkDeviceSize indices_offset=0;
private: protected:
AABB BoundingBox; AABB BoundingBox;
@ -61,9 +63,8 @@ public:
public: public:
void SetDrawCount (const uint32_t dc){draw_count=dc;} ///<设置当前对象绘制需要多少个顶点 void SetDrawCount(const uint32_t dc){draw_count=dc;} ///<设置当前对象绘制需要多少个顶点
virtual const uint32_t GetDrawCount()const ///<取得当前对象绘制需要多少个顶点
const uint32_t GetDrawCount ()const ///<取得当前对象绘制需要多少个顶点
{ {
if(indices_buffer) if(indices_buffer)
return indices_buffer->GetCount(); 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 SET(FONT_LAYOUT_SOURCE ${ROOT_INCLUDE_PATH}/hgl/graph/font/TextLayout.h
font/TextLayout.cpp) 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" FILES ${FONT_MANAGE_SOURCE})
SOURCE_GROUP("Font\\Source" FILES ${FONT_SOURCE}) SOURCE_GROUP("Font\\Source" FILES ${FONT_SOURCE})
SOURCE_GROUP("Font\\TileFont" FILES ${TILE_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) IF(WIN32)
SET(FONT_SOURCE_OS font/FontSourceWin.cpp SET(FONT_SOURCE_OS font/FontSourceWin.cpp
@ -102,4 +105,5 @@ add_cm_library(ULRE.SceneGraph "ULRE" ${SCENE_GRAPH_HEADER}
${FONT_SOURCE} ${FONT_SOURCE}
${FONT_SOURCE_OS} ${FONT_SOURCE_OS}
${TILE_FONT_SOURCE} ${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/TextLayout.h>
#include<hgl/graph/font/TileFont.h> #include<hgl/graph/font/TileFont.h>
#include<hgl/graph/font/TextRenderable.h>
#include<hgl/type/Extent.h> #include<hgl/type/Extent.h>
namespace hgl namespace hgl
@ -8,8 +9,7 @@ namespace hgl
{ {
bool TextLayout::Init() bool TextLayout::Init()
{ {
if(!rc if((!tla.font_source&&!font_source)
||(!tla.font_source&&!font_source)
||!tla.char_layout_attr) ||!tla.char_layout_attr)
return(false); return(false);
@ -173,8 +173,8 @@ namespace hgl
int cur_size=0; int cur_size=0;
int left=0,top=0; int left=0,top=0;
float *tp=vertex->Get(); float *tp=vertex;
float *tcp=tex_coord->Get(); float *tcp=tex_coord;
for(int i=0;i<count;i++) for(int i=0;i<count;i++)
{ {
@ -215,9 +215,6 @@ namespace hgl
++cda; ++cda;
} }
tex_coord->End();
vertex->End();
return count; return count;
} }
@ -229,46 +226,57 @@ namespace hgl
* \r\n * \r\n
*/ */
template<typename T> 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()) if(!tf||str.IsEmpty())
return(-1); 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); return(-2);
if(draw_chars_count<=0) //可绘制字符为0这是全空格 if(draw_chars_count<=0) //可绘制字符为0这是全空格
return(-3); return(-3);
if(!rc->Init(draw_chars_count)) //创建 vertex .SetLength(max_chars*4);
return(-4); tex_coord .SetLength(max_chars*4);
vertex =rc->CreateVADA<VB4f>(VAN::Vertex);
tex_coord =rc->CreateVADA<VB4f>(VAN::TexCoord);
if(!vertex||!tex_coord) if(!vertex||!tex_coord)
return(-5); return(-5);
int result;
if(direction.vertical) if(direction.vertical)
{ {
if(direction.right_to_left) if(direction.right_to_left)
return sl_v_r2l(); result=sl_v_r2l();
else else
return sl_v_l2r(); result=sl_v_l2r();
} }
else else
{ {
if(direction.right_to_left) if(direction.right_to_left)
return sl_h_r2l(); result=sl_h_r2l();
else else
return sl_h_l2r(); result=sl_h_l2r();
} }
return 0; if(result>0)
{
tr->SetCharCount(result);
tr->WriteVertex(vertex);
tr->WriteTexCoord(tex_coord);
} }
int TextLayout::SimpleLayout(TileFont *tf,const UTF16String &str){return this->SimpleLayout<u16char>(tf,str);} return result;
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> //template<typename T>
//int TextLayout::SimpleLayout(TileFont *tf,const StringList<BaseString<T>> &sl) //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/font/TextRenderable.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKMaterial.h>
namespace hgl namespace hgl
{ {
namespace graph namespace graph
{ {
namespace TextRenderable::TextRenderable(vulkan::Device *dev,vulkan::Material *m,uint mc):vulkan::Renderable(m->GetVertexShaderModule(),mc)
{
vulkan::Pipeline *text_pipeline=nullptr;
}//namespace
TextRenderable::TextRenderable(Device *dev,int mc)
{ {
device=dev; device=dev;
mtl=m;
max_count=0;
render_obj=nullptr;
vab_vertex=nullptr; vab_vertex=nullptr;
vab_tex_coord=nullptr; vab_tex_coord=nullptr;
max_count=mc;
cur_count=mc;
SetMaxCount(mc);
} }
TextRenderable::~TextRenderable() TextRenderable::~TextRenderable()
{ {
SAFE_CLEAR(vab_tex_coord); SAFE_CLEAR(vab_tex_coord);
SAFE_CLEAR(vab_vertex); 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) if(vab_vertex)
{
delete vab_vertex; delete vab_vertex;
vab_vertex =device->CreateVAB(VAF_VEC4,max_count); vab_vertex =device->CreateVAB(VAF_VEC4,max_count);
Set(VAN::Vertex,vab_vertex);
render_obj->Set(VAN::Vertex,vab_vertex);
} }
if(vab_tex_coord)
{ {
if(vab_tex_coord)
delete vab_tex_coord; delete vab_tex_coord;
vab_tex_coord =device->CreateVAB(VAF_VEC4,max_count); vab_tex_coord =device->CreateVAB(VAF_VEC4,max_count);
Set(VAN::TexCoord,vab_tex_coord);
}
}
render_obj->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 graph
}//namespace hgl }//namespace hgl