diff --git a/example/Gizmo/Gizmo3DMove.cpp b/example/Gizmo/Gizmo3DMove.cpp index 5a1864ec..7b1bbe33 100644 --- a/example/Gizmo/Gizmo3DMove.cpp +++ b/example/Gizmo/Gizmo3DMove.cpp @@ -1,4 +1,4 @@ -// Gizmo 3D Move +// Gizmo 3D Move #include"VulkanAppFramework.h" #include @@ -31,7 +31,7 @@ private: Material * mtl_vtx_color =nullptr; MaterialInstance * mi_line =nullptr; Pipeline * pipeline_vtx_color =nullptr; - Primitive * ro_line =nullptr; + Primitive * prim_line =nullptr; private: @@ -103,12 +103,12 @@ private: } /** - * дһ. + * 写入一个坐标轴的线条数据. * - * \param pos Ҫдݵָ - * \param max_line - * \param oa1 1 - * \param oa2 2 + * \param pos 要写入数据的指针 + * \param max_line 主线条方向 + * \param oa1 其它轴1方向 + * \param oa2 其它轴2方向 */ void WriteAxisPosition(Vector3f *pos,const Vector3f &max_line,const Vector3f &oa1,const Vector3f &oa2) { @@ -116,15 +116,15 @@ private: constexpr const float AXIS_MIN_STEP =1; constexpr const float AXIS_ARROW_SIZE=0.25; - const Vector3f end_pos =max_line*AXIS_LENGTH; ///<յλ - const Vector3f cross_pos=max_line*AXIS_MIN_STEP; ///<βλ - const Vector3f arrow_pos=max_line*(AXIS_LENGTH-AXIS_MIN_STEP); ///<ͷĩϵλ + const Vector3f end_pos =max_line*AXIS_LENGTH; ///<最终点位置 + const Vector3f cross_pos=max_line*AXIS_MIN_STEP; ///<坐标轴尾部交叉线位置 + const Vector3f arrow_pos=max_line*(AXIS_LENGTH-AXIS_MIN_STEP); ///<箭头末端在主线上的位置 - // + //主线 pos[0]=Vector3f(0, 0, 0); pos[1]=end_pos; - //ĸͷ + //四根箭头线 pos[2]=end_pos; pos[3]=arrow_pos-oa1*AXIS_ARROW_SIZE; @@ -137,7 +137,7 @@ private: pos[8]=end_pos; pos[9]=arrow_pos+oa2*AXIS_ARROW_SIZE; - // + //侧边连接其它轴线 pos[10]=cross_pos; pos[11]=cross_pos+oa1*AXIS_MIN_STEP; pos[12]=cross_pos; @@ -165,8 +165,8 @@ private: constexpr const uint AXIS_MAX_LINES =7; constexpr const uint AXIS_MAX_VERTICES =AXIS_MAX_LINES*2*3; - ro_line=db->CreatePrimitive("Line",AXIS_MAX_VERTICES); - if(!ro_line)return(false); + prim_line=db->CreatePrimitive("Line",AXIS_MAX_VERTICES); + if(!prim_line)return(false); Vector3f position_data[3][AXIS_MAX_LINES*2]; @@ -180,8 +180,8 @@ private: for(Color4f &c:color_data[1])c=Color4f(0,1,0,1); for(Color4f &c:color_data[2])c=Color4f(0,0,1,1); - if(!ro_line->Set(VAN::Position, db->CreateVAB(VF_V3F,AXIS_MAX_VERTICES,position_data)))return(false); - if(!ro_line->Set(VAN::Color, db->CreateVAB(VF_V4F,AXIS_MAX_VERTICES,color_data )))return(false); + if(!prim_line->Set(VAN::Position, db->CreateVAB(VF_V3F,AXIS_MAX_VERTICES,position_data)))return(false); + if(!prim_line->Set(VAN::Color, db->CreateVAB(VF_V4F,AXIS_MAX_VERTICES,color_data )))return(false); } return(true); @@ -190,7 +190,7 @@ private: bool InitScene() { Add(prim_plane_grid,mi_plane_grid,pipeline_vtx_lum); - Add(ro_line,mi_line,pipeline_vtx_color); + Add(prim_line,mi_line,pipeline_vtx_color); camera->pos=Vector3f(32,32,32); camera_control->SetTarget(Vector3f(0,0,0)); diff --git a/example/Gizmo/RayPicking.cpp b/example/Gizmo/RayPicking.cpp index 633f00ed..e52bd8a8 100644 --- a/example/Gizmo/RayPicking.cpp +++ b/example/Gizmo/RayPicking.cpp @@ -9,6 +9,7 @@ #include #include #include +#include using namespace hgl; using namespace hgl::graph; @@ -38,9 +39,12 @@ private: Pipeline * pipeline =nullptr; + VertexDataManager * vdm =nullptr; + PrimitiveCreater * prim_creater =nullptr; + Primitive * prim_plane_grid =nullptr; - Primitive * ro_line =nullptr; + Primitive * prim_line =nullptr; VAB * vab_pos =nullptr; @@ -71,6 +75,24 @@ private: return(true); } + bool InitVDMAndPC() + { + vdm=new VertexDataManager(device,material->GetDefaultVIL()); + if(!vdm->Init( 1024*1024, //VAB最大容量 + 1024*1024, //索引最大容量 + IndexType::U16)) //索引类型 + { + delete vdm; + vdm=nullptr; + + return(false); + } + + prim_creater=new PrimitiveCreater(vdm); + + return(true); + } + Renderable *Add(Primitive *r,MaterialInstance *mi) { Renderable *ri=db->CreateRenderable(r,mi,pipeline); @@ -99,15 +121,19 @@ private: pgci.lum=0.5; pgci.sub_lum=0.75; - prim_plane_grid=CreatePlaneGrid(db,material->GetDefaultVIL(),&pgci); + prim_plane_grid=CreatePlaneGrid(prim_creater,&pgci); } { - ro_line=db->CreatePrimitive("Line",2); - if(!ro_line)return(false); + if(!prim_creater->Init("Line",2)) + return(false); - if(!ro_line->Set(VAN::Position, vab_pos= db->CreateVAB(VF_V3F,2,position_data )))return(false); - if(!ro_line->Set(VAN::Luminance, db->CreateVAB(VF_V1F,2,lumiance_data )))return(false); + if(!prim_creater->WriteVAB(VAN::Position, VF_V3F,position_data))return(false); + if(!prim_creater->WriteVAB(VAN::Luminance,VF_V1F,lumiance_data))return(false); + + prim_line=prim_creater->Create(); + + prim_line->Getv } return(true); @@ -116,7 +142,7 @@ private: bool InitScene() { Add(prim_plane_grid,mi_plane_grid); - Add(ro_line,mi_line); + Add(prim_line,mi_line); camera->pos=Vector3f(32,32,32); camera_control->SetTarget(Vector3f(0,0,0)); @@ -130,6 +156,12 @@ private: public: + ~TestApp() + { + SAFE_CLEAR(prim_creater) + SAFE_CLEAR(vdm) + } + bool Init(uint w,uint h) { if(!SceneAppFramework::Init(w,h)) @@ -138,6 +170,9 @@ public: if(!InitMaterialAndPipeline()) return(false); + if(!InitVDMAndPC()) + return(false); + if(!CreateRenderObject()) return(false); diff --git a/inc/hgl/graph/PrimitiveCreater.h b/inc/hgl/graph/PrimitiveCreater.h index ce65be34..03a6313b 100644 --- a/inc/hgl/graph/PrimitiveCreater.h +++ b/inc/hgl/graph/PrimitiveCreater.h @@ -24,12 +24,18 @@ protected: uint32_t vertices_number; ///<顶点数量 - void_pointer *map_ptr_list; ///<映射指针列表 + VKBufferMap * vab_map_list; uint32_t index_number; ///<索引数量 IndexType index_type; ///<索引类型 IndexBuffer * ibo; ///<索引缓冲区 + VKBufferMap ibo_map; + +protected: + + const int GetVABIndex (const AnsiString &name,const VkFormat &format); ///<取得顶点属性索引 + public: PrimitiveCreater(GPUDevice *,const VIL *); @@ -53,10 +59,7 @@ public: //顶点缓冲区 const uint32_t GetVertexCount()const{ return vertices_number; } ///<取得顶点数量 - const int GetVABIndex (const AnsiString &name,const VkFormat &format); ///<取得顶点属性索引 - - void * MapVAB (const int vab_index); ///<映射一个顶点属性数据区 - void UnmapVAB (const int vab_index); ///<取消映射 + VKBufferMap * MapVAB (const AnsiString &name,const VkFormat &format); bool WriteVAB (const AnsiString &name,const VkFormat &format,const void *data); ///<直接写入顶点属性数据 @@ -65,8 +68,7 @@ public: //索引缓冲区 const IndexType GetIndexType()const{return index_type;} ///<取得索引类型 const uint32_t GetIndexCount()const{return index_number;} ///<取得索引数量 - void * MapIBO(); - void UnmapIBO(); + VKBufferMap * MapIBO(); bool WriteIBO(const void *data,const uint32_t count); @@ -79,89 +81,78 @@ public: //创建可渲染对象 };//class PrimitiveCreater /** -* VAB原生数据访问映射 +* 顶点属性缓冲区原生数据访问映射 */ template class VABRawMap { - PrimitiveCreater *pc; - int vab_index; + VKBufferMap *buf_map; + T *map_ptr; public: - VABRawMap(PrimitiveCreater *c,const VkFormat &format,const AnsiString &name) - { - pc=c; - vab_index=pc->GetVABIndex(name,format); - - map_ptr=(T *)(pc->MapVAB(vab_index)); + VABRawMap(PrimitiveCreater *pc,const VkFormat &format,const AnsiString &name) + { + buf_map=pc->MapVAB(name,format); + + if(buf_map) + map_ptr=(T *)(buf_map->Map()); + else + map_ptr=nullptr; } ~VABRawMap() { if(map_ptr) - pc->UnmapVAB(vab_index); + buf_map->Unmap(); } - const bool IsValid()const{ return map_ptr; } + const bool IsValid()const{ return buf_map?buf_map->IsValid():false; } operator T *(){ return map_ptr; } + T *operator->(){ return map_ptr; } };//template class VABRawMap -typedef VABRawMap VABRawMapi8, VABRawMapByte; -typedef VABRawMap VABRawMapi16,VABRawMapShort; -typedef VABRawMap VABRawMapi32,VABRawMapInt; -typedef VABRawMap VABRawMapu8, VABRawMapUByte; -typedef VABRawMap VABRawMapu16,VABRawMapUShort; -typedef VABRawMap VABRawMapu32,VABRawMapUInt; -typedef VABRawMap VABRawMapFloat; -typedef VABRawMap VABRawMapDouble; +typedef VABRawMap VABMapI8, VABMapByte; +typedef VABRawMap VABMapI16, VABMapShort; +typedef VABRawMap VABMapI32, VABMapInt; +typedef VABRawMap VABMapU8, VABMapUByte; +typedef VABRawMap VABMapU16, VABMapUShort; +typedef VABRawMap VABMapU32, VABMapUInt; +typedef VABRawMap VABMapFloat; +typedef VABRawMap VABMapDouble; /** -* VAB VertexAttribDataAccess数据访问映射 +* 顶点属性缓冲区数据访问映射 */ template class VABMap { - PrimitiveCreater *pc; - int vab_index; - T *vb; + VKBufferMap *buf_map; + + T *map_ptr; public: - VABMap(PrimitiveCreater *c,const AnsiString &name) - { - pc=c; - vab_index=pc->GetVABIndex(name,T::GetVulkanFormat()); + VABMap(PrimitiveCreater *pc,const AnsiString &name) + { + buf_map=pc->MapVAB(name,T::GetVulkanFormat()); - void *map_ptr=(T *)(pc->MapVAB(vab_index)); - - if(map_ptr) - { - vb=T::Create(pc->GetVertexCount(),map_ptr); - - vb->Begin(); - } + if(buf_map) + map_ptr=(T *)(buf_map->Map()); else - { - vb=nullptr; - } + map_ptr=nullptr; } ~VABMap() { - if(pc&&vab_index>=0) - pc->UnmapVAB(vab_index); + if(map_ptr) + buf_map->Unmap(); } - void Restart() - { - if(vb) - vb->Begin(); - } + const bool IsValid()const{ return buf_map?buf_map->IsValid():false; } - const bool IsValid()const{ return vb; } - - T *operator->(){ return vb; } + operator T *(){ return map_ptr; } + T *operator->(){ return map_ptr; } };//template class VABMap typedef VABMap VABMap1i8 ,VABMap1b; @@ -197,38 +188,39 @@ typedef VABMap VABMap4f; typedef VABMap VABMap4d; /** -* 索引缓冲区映射访问 +* 索引缓冲区数据访问映射 */ template class IBMap { - PrimitiveCreater *pc; + VKBufferMap *buf_map; + T *map_ptr; public: - IBMap(PrimitiveCreater *c) - { - pc=c; + IBMap(PrimitiveCreater *pc) + { + buf_map=pc->MapIBO(); - if(pc) - map_ptr=(T *)(pc->MapIBO()); + if(buf_map) + map_ptr=(T *)(buf_map->Map()); else map_ptr=nullptr; } ~IBMap() { - if(map_ptr&&pc) - pc->UnmapIBO(); + if(map_ptr) + buf_map->Unmap(); } const bool IsValid()const{ return map_ptr; } operator T *(){ return map_ptr; } + T *operator->(){ return map_ptr; } };//template class IBMap -using IBMapU8=IBMap; +using IBMapU8 =IBMap; using IBMapU16=IBMap; using IBMapU32=IBMap; - VK_NAMESPACE_END diff --git a/inc/hgl/graph/VKBufferMap.h b/inc/hgl/graph/VKBufferMap.h index 4fe71eca..8bf9cb0b 100644 --- a/inc/hgl/graph/VKBufferMap.h +++ b/inc/hgl/graph/VKBufferMap.h @@ -16,10 +16,14 @@ protected: public: - VKBufferMap(DeviceBuffer *buf_ptr,VkDeviceSize off,VkDeviceSize s); - virtual ~VKBufferMap(); + VKBufferMap(); + VKBufferMap(DeviceBuffer *buf,VkDeviceSize off,VkDeviceSize s); + ~VKBufferMap(); + + void Set(DeviceBuffer *buf_ptr,VkDeviceSize off,VkDeviceSize s); const bool IsValid()const{ return buffer; } + void Clear(); void *Map(); void Unmap(); diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index 21dd4430..ea0f7cc8 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -470,10 +470,10 @@ namespace hgl if(!pc->Init("Sphere",numberVertices,numberIndices)) return(nullptr); - VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); - VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); + VABMapFloat vertex (pc,VF_V3F,VAN::Position); + VABMapFloat normal (pc,VF_V3F,VAN::Normal); + VABMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; @@ -559,10 +559,10 @@ namespace hgl if(!pc->Init("Dome",numberVertices,numberIndices)) return(nullptr); - VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); - VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); + VABMapFloat vertex (pc,VF_V3F,VAN::Position); + VABMapFloat normal (pc,VF_V3F,VAN::Normal); + VABMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; @@ -707,10 +707,10 @@ namespace hgl if(!pc->Init("Torus",numberVertices,numberIndices)) return(nullptr); - VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); - VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); + VABMapFloat vertex (pc,VF_V3F,VAN::Position); + VABMapFloat normal (pc,VF_V3F,VAN::Normal); + VABMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; @@ -849,10 +849,10 @@ namespace hgl if (cci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) return nullptr; - VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); - VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); + VABMapFloat vertex (pc,VF_V3F,VAN::Position); + VABMapFloat normal (pc,VF_V3F,VAN::Normal); + VABMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; @@ -1079,10 +1079,10 @@ namespace hgl if (cci->numberSlices < 3 || cci->numberStacks < 1 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) return nullptr; - VABRawMapFloat vertex (pc,VF_V3F,VAN::Position); - VABRawMapFloat normal (pc,VF_V3F,VAN::Normal); - VABRawMapFloat tangent (pc,VF_V3F,VAN::Tangent); - VABRawMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); + VABMapFloat vertex (pc,VF_V3F,VAN::Position); + VABMapFloat normal (pc,VF_V3F,VAN::Normal); + VABMapFloat tangent (pc,VF_V3F,VAN::Tangent); + VABMapFloat tex_coord(pc,VF_V2F,VAN::TexCoord); float *vp=vertex; float *np=normal; diff --git a/src/SceneGraph/PrimitiveCreater.cpp b/src/SceneGraph/PrimitiveCreater.cpp index 6fbebf6b..08ff14bf 100644 --- a/src/SceneGraph/PrimitiveCreater.cpp +++ b/src/SceneGraph/PrimitiveCreater.cpp @@ -15,7 +15,7 @@ PrimitiveCreater::PrimitiveCreater(GPUDevice *dev,const VIL *v) prim_data =nullptr; - map_ptr_list =hgl_zero_new(v->GetVertexAttribCount()); + vab_map_list =new VKBufferMap[v->GetVertexAttribCount()]; Clear(); } @@ -30,8 +30,8 @@ PrimitiveCreater::PrimitiveCreater(VertexDataManager *_vdm) PrimitiveCreater::~PrimitiveCreater() { + delete[] vab_map_list; SAFE_CLEAR(prim_data); - SAFE_CLEAR_ARRAY(map_ptr_list) } void PrimitiveCreater::Clear() @@ -94,6 +94,8 @@ bool PrimitiveCreater::Init(const AnsiString &pname,const uint32_t vertex_count, delete prim_data; return(false); } + + ibo_map.Set(ibo,prim_data->GetFirstIndex(),index_number); #ifdef _DEBUG if(!vdm) @@ -120,11 +122,21 @@ const int PrimitiveCreater::GetVABIndex(const AnsiString &name,const VkFormat &a const int vab_index=prim_data->GetVABIndex(name); + if(vab_index<0) + return(-1); + VAB *vab=prim_data->GetVAB(vab_index); if(!vab) + { vab=prim_data->InitVAB(vab_index,acquire_format,nullptr); + if(vab) + vab_map_list[vab_index].Set(vab, + prim_data->GetVertexOffset(), + vertices_number); + } + if(!vab) return(-1); @@ -144,31 +156,13 @@ const int PrimitiveCreater::GetVABIndex(const AnsiString &name,const VkFormat &a return(vab_index); } -void *PrimitiveCreater::MapVAB(const int vab_index) +VKBufferMap *PrimitiveCreater::MapVAB(const AnsiString &name,const VkFormat &format) { - if(!prim_data) - return(nullptr); + const int vab_index=GetVABIndex(name,format); - VAB *vab=prim_data->GetVAB(vab_index); + if(vab_index<0)return nullptr; - if(!vab) - return(nullptr); - - map_ptr_list[vab_index]=vab->Map(prim_data->GetVertexOffset(),vertices_number); - - return map_ptr_list[vab_index]; -} - -void PrimitiveCreater::UnmapVAB(const int vab_index) -{ - if(!prim_data)return; - - VAB *vab=prim_data->GetVAB(vab_index); - - if(!vab)return; - - vab->Unmap(); - map_ptr_list[vab_index]=nullptr; + return vab_map_list+vab_index; } bool PrimitiveCreater::WriteVAB(const AnsiString &name,const VkFormat &format, const void *data) @@ -185,18 +179,12 @@ bool PrimitiveCreater::WriteVAB(const AnsiString &name,const VkFormat &format, c return vab->Write(data,prim_data->GetVertexOffset(),vertices_number); } -void *PrimitiveCreater::MapIBO() +VKBufferMap *PrimitiveCreater::MapIBO() { - if(!prim_data)return(nullptr); - if(!ibo)return(nullptr); + if(!ibo) + return(nullptr); - return ibo->Map(prim_data->GetFirstIndex(),index_number); -} - -void PrimitiveCreater::UnmapIBO() -{ - if(ibo) - ibo->Unmap(); + return &ibo_map; } bool PrimitiveCreater::WriteIBO(const void *data,const uint32_t count) @@ -218,11 +206,9 @@ Primitive *PrimitiveCreater::Create() return(nullptr); for(int i=0;iGetVertexAttribCount();i++) - if(map_ptr_list[i]) - { - prim_data->GetVAB(i)->Unmap(); - map_ptr_list[i]=nullptr; - } + vab_map_list[i].Clear(); + + ibo_map.Clear(); Primitive *primitive=new Primitive(prim_name,prim_data); diff --git a/src/SceneGraph/Vulkan/VKBufferMap.cpp b/src/SceneGraph/Vulkan/VKBufferMap.cpp index 6346d86c..1066077c 100644 --- a/src/SceneGraph/Vulkan/VKBufferMap.cpp +++ b/src/SceneGraph/Vulkan/VKBufferMap.cpp @@ -1,10 +1,11 @@ #include #include +#include VK_NAMESPACE_BEGIN -VKBufferMap::VKBufferMap(DeviceBuffer *buf,VkDeviceSize off,VkDeviceSize s) -{ +void VKBufferMap::Set(DeviceBuffer *buf,VkDeviceSize off,VkDeviceSize s) +{ buffer=buf; offset=off; size=s; @@ -12,10 +13,35 @@ VKBufferMap::VKBufferMap(DeviceBuffer *buf,VkDeviceSize off,VkDeviceSize s) map_ptr=nullptr; } +void VKBufferMap::Clear() +{ + if(buffer&&map_ptr) + buffer->Unmap(); + + buffer=nullptr; + offset=0; + size=0; + map_ptr=nullptr; +} + +VKBufferMap::VKBufferMap() +{ + Set(nullptr,0,0); + + std::cout<<"VKBufferMap Create"<DeviceBuffer::Unmap(); + if(buffer&&map_ptr) + buffer->Unmap(); + + std::cout<<"VKBufferMap Destory"<DeviceBuffer::Map(offset,size); + map_ptr=buffer->Map(offset,size); return map_ptr; } void VKBufferMap::Unmap() { - if(map_ptr) + if(buffer&&map_ptr) { - buffer->DeviceBuffer::Unmap(); + buffer->Unmap(); map_ptr=nullptr; } } -VK_NAMESPACE_END \ No newline at end of file +VK_NAMESPACE_END