diff --git a/CMCore b/CMCore index 4c22ba5a..46574e0f 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 4c22ba5a6066bd60badb65b6ce745562664c1705 +Subproject commit 46574e0f49560525442b9d46575aed58678b965a diff --git a/example/Vulkan/DrawText.cpp b/example/Vulkan/DrawText.cpp index ee020293..c2da2186 100644 --- a/example/Vulkan/DrawText.cpp +++ b/example/Vulkan/DrawText.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include"VulkanAppFramework.h" #include @@ -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 diff --git a/inc/hgl/graph/RenderableCreater.h b/inc/hgl/graph/RenderableCreater.h index b1937144..b5e8e324 100644 --- a/inc/hgl/graph/RenderableCreater.h +++ b/inc/hgl/graph/RenderableCreater.h @@ -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; diff --git a/inc/hgl/graph/VertexAttribData.h b/inc/hgl/graph/VertexAttribData.h index 6a7b78e3..bc233542 100644 --- a/inc/hgl/graph/VertexAttribData.h +++ b/inc/hgl/graph/VertexAttribData.h @@ -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 + /** * 顶点属性数据 */ diff --git a/inc/hgl/graph/font/TextLayout.h b/inc/hgl/graph/font/TextLayout.h index 05eee4e0..1cdbef7e 100644 --- a/inc/hgl/graph/font/TextLayout.h +++ b/inc/hgl/graph/font/TextLayout.h @@ -5,11 +5,13 @@ #include #include #include +#include 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 int SimpleLayout(TileFont *,const BaseString &); ///<简易排版 + template int SimpleLayout(TextRenderable *,TileFont *,const BaseString &); ///<简易排版 -// template int SimpleLayout(TileFont *,const StringList> &); ///<简易排版 +// template int SimpleLayout(TileFont *,const StringList> &); ///<简易排版 protected: @@ -138,26 +139,23 @@ namespace hgl TEXT_COORD_TYPE paragraph_gap; protected: - - VB4f *vertex; - VB4f *tex_coord; + + TextRenderable *text_render_obj; + MemBlock vertex; + MemBlock 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 &); ///<简易排版 diff --git a/inc/hgl/graph/font/TextRenderable.h b/inc/hgl/graph/font/TextRenderable.h index 86738609..36c35ea7 100644 --- a/inc/hgl/graph/font/TextRenderable.h +++ b/inc/hgl/graph/font/TextRenderable.h @@ -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 diff --git a/inc/hgl/graph/vulkan/VKBuffer.h b/inc/hgl/graph/vulkan/VKBuffer.h index de6a17df..a64d03f9 100644 --- a/inc/hgl/graph/vulkan/VKBuffer.h +++ b/inc/hgl/graph/vulkan/VKBuffer.h @@ -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 diff --git a/inc/hgl/graph/vulkan/VKMemory.h b/inc/hgl/graph/vulkan/VKMemory.h index 286ca558..e291e104 100644 --- a/inc/hgl/graph/vulkan/VKMemory.h +++ b/inc/hgl/graph/vulkan/VKMemory.h @@ -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 diff --git a/inc/hgl/graph/vulkan/VKRenderable.h b/inc/hgl/graph/vulkan/VKRenderable.h index e81457ec..0e918652 100644 --- a/inc/hgl/graph/vulkan/VKRenderable.h +++ b/inc/hgl/graph/vulkan/VKRenderable.h @@ -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(); diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 1eb1597c..01fbd94e 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -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}) diff --git a/src/SceneGraph/font/TextLayout.cpp b/src/SceneGraph/font/TextLayout.cpp index f837b46e..5f0cf392 100644 --- a/src/SceneGraph/font/TextLayout.cpp +++ b/src/SceneGraph/font/TextLayout.cpp @@ -1,5 +1,6 @@ #include #include +#include #include 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;iEnd(); - vertex->End(); - return count; } @@ -229,46 +226,57 @@ namespace hgl * 简易文本排版。无任何特殊处理,不支持任何转义符,不支持\r\n */ template - int TextLayout::SimpleLayout(TileFont *tf,const BaseString &str) + int TextLayout::SimpleLayout(TextRenderable *tr,TileFont *tf,const BaseString &str) { + if(!tr) + return(-1); + if(!tf||str.IsEmpty()) return(-1); - if(!preprocess(tf,str.c_str(),str.Length())) + int max_chars=str.Length(); + + if(!preprocess(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(VAN::Vertex); - tex_coord =rc->CreateVADA(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(tf,str);} - int TextLayout::SimpleLayout(TileFont *tf,const UTF32String &str){return this->SimpleLayout(tf,str);} + int TextLayout::SimpleLayout(TextRenderable *tr,TileFont *tf,const UTF16String &str){return this->SimpleLayout(tr,tf,str);} + int TextLayout::SimpleLayout(TextRenderable *tr,TileFont *tf,const UTF32String &str){return this->SimpleLayout(tr,tf,str);} //template //int TextLayout::SimpleLayout(TileFont *tf,const StringList> &sl) diff --git a/src/SceneGraph/font/TextRenderable.cpp b/src/SceneGraph/font/TextRenderable.cpp index ee0350fc..ae3b9560 100644 --- a/src/SceneGraph/font/TextRenderable.cpp +++ b/src/SceneGraph/font/TextRenderable.cpp @@ -1,58 +1,53 @@ #include +#include +#include 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(mcdraw_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