diff --git a/example/Vulkan/Geometry2D.cpp b/example/Vulkan/Geometry2D.cpp index ee03b4e7..273d76ef 100644 --- a/example/Vulkan/Geometry2D.cpp +++ b/example/Vulkan/Geometry2D.cpp @@ -4,9 +4,9 @@ // 二、试验动态合并材质渲染机制、包括普通合并与Instance #include"VulkanAppFramework.h" -#include #include -#include +#include +#include using namespace hgl; using namespace hgl::graph; @@ -22,87 +22,92 @@ struct WorldConfig Matrix4f mvp; }world; -struct RectangleCreateInfo -{ - RectScope2f scope; - vec3 normal; - Color4f color; - RectScope2f tex_coord; -}; - -vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl,const RectangleCreateInfo *rci) -{ - const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule(); - - vulkan::Renderable *render_obj=mtl->CreateRenderable(); - - const int vertex_binding=vsm->GetStageInputBinding("Vertex"); - - if(vertex_binding!=-1) - { - VB2f *vertex=new VB2f(6); - - vertex->Begin(); - vertex->WriteRect(rci->scope); - vertex->End(); - - render_obj->Set(vertex_binding,device->CreateVBO(vertex)); - delete vertex; - } - - const int normal_binding=vsm->GetStageInputBinding("Normal"); - - if(normal_binding!=-1) - { - VB3f *normal=new VB3f(6); - - normal->Begin(); - normal->Write(rci->normal,6); - normal->End(); - - render_obj->Set(normal_binding,device->CreateVBO(normal)); - delete normal; - } - - const int color_binding=vsm->GetStageInputBinding("Color"); - - if(color_binding!=-1) - { - VB4f *color=new VB4f(6); - - color->Begin(); - color->Write(rci->color,6); - color->End(); - - render_obj->Set(color_binding,device->CreateVBO(color)); - delete color; - } - - const int tex_coord_binding=vsm->GetStageInputBinding("TexCoord"); - - if(tex_coord_binding!=-1) - { - VB2f *tex_coord=new VB2f(6); - - tex_coord->Begin(); - tex_coord->WriteRect(rci->tex_coord); - tex_coord->End(); - - render_obj->Set(tex_coord_binding,device->CreateVBO(tex_coord)); - delete tex_coord; - } - - return render_obj; -} +VK_NAMESPACE_BEGIN +//using MaterialID=int; +//using PipelineID=int; +//using DescriptorSetsID=int; +//using RenderableID=int; +// +///** +// * 场景DB,用于管理场景内所需的所有数据 +// */ +//class SceneDatabase +//{ +// IDResManage rm_material; ///<材质合集 +// IDResManage rm_pipeline; ///<管线合集 +// IDResManage rm_desc_sets; ///<描述符合集 +// IDResManage rm_renderable; ///<可渲染对象合集 +// +//public: +// +// MaterialID Add(Material * mtl ){return rm_material.Add(mtl);} +// PipelineID Add(Pipeline * p ){return rm_pipeline.Add(p);} +// DescriptorSetsID Add(DescriptorSets *ds ){return rm_desc_sets.Add(ds);} +// RenderableID Add(Renderable * r ){return rm_renderable.Add(r);} +//};//class SceneDatabase /** - * 创建一个圆角矩形 - * @param r 半径 - * @param rp 半径精度 + * 可渲染对象实例 */ -//Mesh *CreateRoundrectangle(float l,float t,float w,float h,float r,uint32_t rp) -//{ -//} +class RenderableInstance +{ + Pipeline * pipeline; + DescriptorSets * desc_sets; + Renderable * render_obj; + +public: + + RenderableInstance(Pipeline *p,DescriptorSets *ds,Renderable *r):pipeline(p),desc_sets(ds),render_obj(r){} + virtual ~RenderableInstance() + { + } + + Pipeline * GetPipeline (){return pipeline;} + DescriptorSets *GetDescriptorSets (){return desc_sets;} + Renderable * GetRenderable (){return render_obj;} + + const int Comp(const RenderableInstance *ri)const + { + //绘制顺序: + + // ARM Mali GPU : 不透明、AlphaTest、半透明 + // Adreno/NV/AMD: AlphaTest、不透明、半透明 + // PowerVR: 同Adreno/NV/AMD,但不透明部分可以不排序 + + if(pipeline->IsAlphaBlend()) + { + if(!ri->pipeline->IsAlphaBlend()) + return 1; + } + else + { + if(ri->pipeline->IsAlphaBlend()) + return -1; + } + + if(pipeline->IsAlphaTest()) + { + if(!ri->pipeline->IsAlphaTest()) + return 1; + } + else + { + if(ri->pipeline->IsAlphaTest()) + return -1; + } + + if(pipeline!=ri->pipeline) + return pipeline-ri->pipeline; + + if(desc_sets!=ri->desc_sets) + return desc_sets-ri->desc_sets; + + return render_obj-ri->render_obj; + } + + CompOperator(const RenderableInstance *,Comp) +};//class RenderableInstance +VK_NAMESPACE_END class TestApp:public VulkanApplicationFramework { @@ -111,8 +116,10 @@ private: uint swap_chain_count=0; vulkan::Material * material =nullptr; - vulkan::DescriptorSets * desciptor_sets =nullptr; + vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::Renderable * render_obj =nullptr; + vulkan::RenderableInstance *render_instance =nullptr; + vulkan::Buffer * ubo_mvp =nullptr; vulkan::Pipeline * pipeline =nullptr; @@ -125,13 +132,14 @@ public: ~TestApp() { + SAFE_CLEAR(render_instance); SAFE_CLEAR(color_buffer); SAFE_CLEAR(vertex_buffer); SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count); SAFE_CLEAR(pipeline); SAFE_CLEAR(ubo_mvp); SAFE_CLEAR(render_obj); - SAFE_CLEAR(desciptor_sets); + SAFE_CLEAR(descriptor_sets); SAFE_CLEAR(material); } @@ -144,17 +152,37 @@ private: if(!material) return(false); - desciptor_sets=material->CreateDescriptorSets(); + descriptor_sets=material->CreateDescriptorSets(); return(true); } bool CreateRenderObject() { - struct RectangleCreateInfo rci; + //struct RectangleCreateInfo rci; - rci.scope.Set(10,10,10,10); + //rci.scope.Set(10,10,10,10); - render_obj=CreateRectangle(device,material,&rci); + //render_obj=CreateRectangle(device,material,&rci); + + //struct RoundRectangleCreateInfo rrci; + + //rrci.scope.Set(10,10,SCREEN_WIDTH-20,SCREEN_HEIGHT-20); + //rrci.radius=8; //半径为8 + //rrci.round_per=8; //圆角切分成8段 + + //render_obj=CreateRoundRectangle(device,material,&rrci); + + struct CircleCreateInfo cci; + + cci.center.x=SCREEN_WIDTH/2; + cci.center.y=SCREEN_HEIGHT/2; + + cci.radius.x=SCREEN_WIDTH*0.45; + cci.radius.y=SCREEN_HEIGHT*0.45; + + cci.field_count=8; + + render_obj=CreateCircle(device,material,&cci); return render_obj; } @@ -170,10 +198,10 @@ private: if(!ubo_mvp) return(false); - if(!desciptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp)) + if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp)) return(false); - desciptor_sets->Update(); + descriptor_sets->Update(); return(true); } @@ -186,7 +214,7 @@ private: pipeline_creater->SetDepthTest(false); pipeline_creater->SetDepthWrite(false); pipeline_creater->CloseCullFace(); - pipeline_creater->Set(PRIM_TRIANGLES); + pipeline_creater->Set(PRIM_TRIANGLE_FAN); pipeline=pipeline_creater->Create(); @@ -196,6 +224,27 @@ private: return pipeline; } + void Draw(vulkan::CommandBuffer *cb,vulkan::RenderableInstance *ri) + { + cb->Bind(ri->GetPipeline()); + cb->Bind(ri->GetDescriptorSets()); + + vulkan::Renderable *obj=ri->GetRenderable(); + + cb->Bind(obj); + + const vulkan::IndexBuffer *ib=obj->GetIndexBuffer(); + + if(ib) + { + cb->DrawIndexed(ib->GetCount()); + } + else + { + cb->Draw(obj->GetDrawCount()); + } + } + bool InitCommandBuffer() { cmd_buf=hgl_zero_new(swap_chain_count); @@ -209,10 +258,7 @@ private: cmd_buf[i]->Begin(); cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i)); - cmd_buf[i]->Bind(pipeline); - cmd_buf[i]->Bind(desciptor_sets); - cmd_buf[i]->Bind(render_obj); - cmd_buf[i]->Draw(6); + Draw(cmd_buf[i],render_instance); cmd_buf[i]->EndRenderPass(); cmd_buf[i]->End(); } @@ -241,6 +287,8 @@ public: if(!InitPipeline()) return(false); + render_instance=new vulkan::RenderableInstance(pipeline,descriptor_sets,render_obj); + if(!InitCommandBuffer()) return(false); diff --git a/example/Vulkan/indices_rect.cpp b/example/Vulkan/indices_rect.cpp index ca271a18..80789377 100644 --- a/example/Vulkan/indices_rect.cpp +++ b/example/Vulkan/indices_rect.cpp @@ -40,7 +40,7 @@ private: uint swap_chain_count=0; vulkan::Material * material =nullptr; - vulkan::DescriptorSets * desciptor_sets =nullptr; + vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::Renderable * render_obj =nullptr; vulkan::Buffer * ubo_mvp =nullptr; @@ -60,7 +60,7 @@ public: SAFE_CLEAR(pipeline); SAFE_CLEAR(ubo_mvp); SAFE_CLEAR(render_obj); - SAFE_CLEAR(desciptor_sets); + SAFE_CLEAR(descriptor_sets); SAFE_CLEAR(material); } @@ -73,8 +73,8 @@ private: if(!material) return(false); - render_obj=material->CreateRenderable(); - desciptor_sets=material->CreateDescriptorSets(); + render_obj=material->CreateRenderable(VERTEX_COUNT); + descriptor_sets=material->CreateDescriptorSets(); return(true); } @@ -89,10 +89,10 @@ private: if(!ubo_mvp) return(false); - if(!desciptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp)) + if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp)) return(false); - desciptor_sets->Update(); + descriptor_sets->Update(); return(true); } @@ -136,7 +136,7 @@ private: cmd_buf[i]->Begin(); cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i)); cmd_buf[i]->Bind(pipeline); - cmd_buf[i]->Bind(desciptor_sets); + cmd_buf[i]->Bind(descriptor_sets); cmd_buf[i]->Bind(render_obj); cmd_buf[i]->DrawIndexed(INDEX_COUNT); cmd_buf[i]->EndRenderPass(); diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 83686abd..1c31f1f5 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -41,7 +41,7 @@ private: uint swap_chain_count=0; vulkan::Material * material =nullptr; - vulkan::DescriptorSets * desciptor_sets =nullptr; + vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::Renderable * render_obj =nullptr; vulkan::Buffer * ubo_mvp =nullptr; @@ -61,7 +61,7 @@ public: SAFE_CLEAR(pipeline); SAFE_CLEAR(ubo_mvp); SAFE_CLEAR(render_obj); - SAFE_CLEAR(desciptor_sets); + SAFE_CLEAR(descriptor_sets); SAFE_CLEAR(material); } @@ -74,8 +74,8 @@ private: if(!material) return(false); - render_obj=material->CreateRenderable(); - desciptor_sets=material->CreateDescriptorSets(); + render_obj=material->CreateRenderable(VERTEX_COUNT); + descriptor_sets=material->CreateDescriptorSets(); return(true); } @@ -90,10 +90,10 @@ private: if(!ubo_mvp) return(false); - if(!desciptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp)) + if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp)) return(false); - desciptor_sets->Update(); + descriptor_sets->Update(); return(true); } @@ -150,7 +150,7 @@ private: cmd_buf[i]->Begin(); cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i)); cmd_buf[i]->Bind(pipeline); - cmd_buf[i]->Bind(desciptor_sets); + cmd_buf[i]->Bind(descriptor_sets); cmd_buf[i]->Bind(render_obj); cmd_buf[i]->Draw(VERTEX_COUNT); cmd_buf[i]->EndRenderPass(); diff --git a/example/Vulkan/texture_rect.cpp b/example/Vulkan/texture_rect.cpp index 94a08319..6a491743 100644 --- a/example/Vulkan/texture_rect.cpp +++ b/example/Vulkan/texture_rect.cpp @@ -93,7 +93,7 @@ private: if(!material) return(false); - render_obj=material->CreateRenderable(); + render_obj=material->CreateRenderable(VERTEX_COUNT); desciptor_sets=material->CreateDescriptorSets(); texture=vulkan::LoadTGATexture(OS_TEXT("lena.tga"),device); diff --git a/inc/hgl/filesystem/AssetManage.h b/inc/hgl/filesystem/AssetManage.h new file mode 100644 index 00000000..fa6f6269 --- /dev/null +++ b/inc/hgl/filesystem/AssetManage.h @@ -0,0 +1,9 @@ +#ifndef HGL_FILESYSTEM_ASSET_MANAGE_INCLUDE +#define HGL_FILESYSTEM_ASSET_MANAGE_INCLUDE + +#include +namespace hgl +{ + +}//namespace hgl +#endif//HGL_FILESYSTEM_ASSET_MANAGE_INCLUDE diff --git a/inc/hgl/graph/InlineGeometry.h b/inc/hgl/graph/InlineGeometry.h new file mode 100644 index 00000000..08f893f1 --- /dev/null +++ b/inc/hgl/graph/InlineGeometry.h @@ -0,0 +1,45 @@ +#ifndef HGL_GRAPH_INLINE_GEOMETRY_INCLUDE +#define HGL_GRAPH_INLINE_GEOMETRY_INCLUDE + +#include +#include +#include +namespace hgl +{ + namespace graph + { + /** + * 矩形创建信息 + */ + struct RectangleCreateInfo + { + RectScope2f scope; + }; + + vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl,const RectangleCreateInfo *rci); + + /** + * 圆角矩形创建信息 + */ + struct RoundRectangleCreateInfo:public RectangleCreateInfo + { + float radius; //圆角半径 + uint32_t round_per; //圆角精度 + }; + + vulkan::Renderable *CreateRoundRectangle(vulkan::Device *device,vulkan::Material *mtl,const RoundRectangleCreateInfo *rci); + + /** + * 圆形创建信息 + */ + struct CircleCreateInfo + { + Vector2f center; //圆心坐标 + Vector2f radius; //半径 + uint field_count; //分段次数 + }; + + vulkan::Renderable *CreateCircle(vulkan::Device *device,vulkan::Material *mtl,const CircleCreateInfo *rci); + }//namespace graph +};//namespace hgl +#endif//HGL_GRAPH_INLINE_GEOMETRY_INCLUDE diff --git a/inc/hgl/graph/SceneNode.h b/inc/hgl/graph/SceneNode.h index e8325627..6861070d 100644 --- a/inc/hgl/graph/SceneNode.h +++ b/inc/hgl/graph/SceneNode.h @@ -3,22 +3,24 @@ #include #include +#include namespace hgl { namespace graph { + using namespace vulkan; + class SceneNode; struct Camera; class Frustum; - class Renderable; - typedef List RenderList; ///<渲染列表类型重定义 + using RenderList=List; ///<渲染列表类型重定义 - typedef float (*RenderListCompFunc)(Camera *,SceneNode *,SceneNode *); ///<渲染列表排序比较函数 + using RenderListCompFunc=float (*)(Camera *,SceneNode *,SceneNode *); ///<渲染列表排序比较函数 float CameraLengthComp(Camera *,SceneNode *,SceneNode *); ///<摄像机距离比较函数 - typedef bool (*FilterSceneNodeFunc)(const SceneNode *,void *); ///<场景节点过滤函数重定义 + using FilterSceneNodeFunc=bool (*)(const SceneNode *,void *); ///<场景节点过滤函数重定义 bool FrustumClipFilter(const SceneNode *,void *); ///<平截头截减过滤函数 @@ -40,7 +42,7 @@ namespace hgl public: - List SubData; ///<可渲染数据 + List SubData; ///<可渲染数据 ObjectList SubNode; ///<子节点 public: @@ -52,7 +54,7 @@ namespace hgl ClearRenderable(); } - void Add(Renderable *r){if(r)SubData.Add(r);} ///<增加一个可渲染数据 + void Add(RenderableInstance *r){if(r)SubData.Add(r);} ///<增加一个可渲染数据 void ClearRenderable(){SubData.Clear();} ///<清除可渲染数据 void AddSubNode(SceneNode *n){if(n)SubNode.Add(n);} ///<增加一个子节点 @@ -63,7 +65,7 @@ namespace hgl return sn; } - SceneNode * AddSubNode(Renderable *r,const Matrix4f &m) + SceneNode * AddSubNode(RenderableInstance *r,const Matrix4f &m) { if(!r)return(nullptr); diff --git a/inc/hgl/graph/VertexBuffer.h b/inc/hgl/graph/VertexBuffer.h index e0bf0f33..47c97a0d 100644 --- a/inc/hgl/graph/VertexBuffer.h +++ b/inc/hgl/graph/VertexBuffer.h @@ -14,7 +14,7 @@ namespace hgl /** * 顶点属性数据实际模板 */ - template class VertexBuffer:public VertexBufferCreater + template class VertexBufferBase:public VertexBufferCreater { protected: @@ -25,7 +25,7 @@ namespace hgl public: - VertexBuffer(uint32_t _size,const T *_data=nullptr):VertexBufferCreater(_size,C,sizeof(T)) + VertexBufferBase(uint32_t _size,const T *_data=nullptr):VertexBufferCreater(_size,C,sizeof(T)) { mem_type=(T *)GetData(); access=0; @@ -35,7 +35,7 @@ namespace hgl memcpy(mem_type,_data,total_bytes); } - virtual ~VertexBuffer()=default; + virtual ~VertexBufferBase()=default; /** * 取得数据区地址 @@ -111,11 +111,11 @@ namespace hgl /** * 一元数据缓冲区 */ - template class VertexBuffer1:public VertexBuffer + template class VertexBuffer1:public VertexBufferBase { public: - using VertexBuffer::VertexBuffer; + using VertexBufferBase::VertexBufferBase; virtual ~VertexBuffer1()=default; VkFormat GetDataType()const override; @@ -154,11 +154,11 @@ namespace hgl /** * 二元数据缓冲区 */ - template class VertexBuffer2:public VertexBuffer + template class VertexBuffer2:public VertexBufferBase { public: - using VertexBuffer::VertexBuffer; + using VertexBufferBase::VertexBufferBase; virtual ~VertexBuffer2()=default; VkFormat GetDataType()const override; @@ -348,16 +348,64 @@ namespace hgl scope.GetRightBottom(), scope.GetLeftBottom()); } + + template + bool WriteRectFan(const RectScope2 &scope) + { + if(!this->access||this->access+8>this->mem_end) + { + LOG_HINT(OS_TEXT("VertexBuffer2::WriteRectFan(RectScope2 *) out")); + return(false); + } + + *this->access++=scope.GetLeft(); + *this->access++=scope.GetTop(); + + *this->access++=scope.GetRight(); + *this->access++=scope.GetTop(); + + *this->access++=scope.GetRight(); + *this->access++=scope.GetBottom(); + + *this->access++=scope.GetLeft(); + *this->access++=scope.GetBottom(); + + return(true); + } + + template + bool WriteRectTriangleStrip(const RectScope2 &scope) + { + if(!this->access||this->access+8>this->mem_end) + { + LOG_HINT(OS_TEXT("VertexBuffer2::WriteRectTriangleStrip(RectScope2 *) out")); + return(false); + } + + *this->access++=scope.GetLeft(); + *this->access++=scope.GetTop(); + + *this->access++=scope.GetLeft(); + *this->access++=scope.GetBottom(); + + *this->access++=scope.GetRight(); + *this->access++=scope.GetTop(); + + *this->access++=scope.GetRight(); + *this->access++=scope.GetBottom(); + + return(true); + } };//class VertexBuffer2 /** * 三元数据缓冲区 */ - template class VertexBuffer3:public VertexBuffer + template class VertexBuffer3:public VertexBufferBase { public: - using VertexBuffer::VertexBuffer; + using VertexBufferBase::VertexBufferBase; virtual ~VertexBuffer3()=default; VkFormat GetDataType()const override; @@ -593,11 +641,11 @@ namespace hgl /** * 四元数据缓冲区 */ - template class VertexBuffer4:public VertexBuffer + template class VertexBuffer4:public VertexBufferBase { public: - using VertexBuffer::VertexBuffer; + using VertexBufferBase::VertexBufferBase; virtual ~VertexBuffer4()=default; VkFormat GetDataType()const override; diff --git a/inc/hgl/graph/vulkan/VK.h b/inc/hgl/graph/vulkan/VK.h index 646567b1..4a45f59e 100644 --- a/inc/hgl/graph/vulkan/VK.h +++ b/inc/hgl/graph/vulkan/VK.h @@ -49,6 +49,7 @@ class DescriptorSets; class VertexAttributeBinding; class Renderable; +class RenderableInstance; using CharPointerList=hgl::List; diff --git a/inc/hgl/graph/vulkan/VKMaterial.h b/inc/hgl/graph/vulkan/VKMaterial.h index c6e7ac4d..9832b3de 100644 --- a/inc/hgl/graph/vulkan/VKMaterial.h +++ b/inc/hgl/graph/vulkan/VKMaterial.h @@ -57,7 +57,7 @@ public: void Write(VkPipelineVertexInputStateCreateInfo &vis)const; - Renderable *CreateRenderable(); + Renderable *CreateRenderable(const uint32_t draw_count); };//class Material VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_MATERIAL_INCLUDE diff --git a/inc/hgl/graph/vulkan/VKPhysicalDevice.h b/inc/hgl/graph/vulkan/VKPhysicalDevice.h index 04fe87e5..02227b5b 100644 --- a/inc/hgl/graph/vulkan/VKPhysicalDevice.h +++ b/inc/hgl/graph/vulkan/VKPhysicalDevice.h @@ -1,6 +1,7 @@ #pragma once #include +#include VK_NAMESPACE_BEGIN struct PhysicalDevice @@ -9,6 +10,7 @@ struct PhysicalDevice VkPhysicalDevice physical_device=nullptr; VkPhysicalDeviceFeatures features; VkPhysicalDeviceProperties properties; + VkPhysicalDeviceDriverPropertiesKHR driver_properties; VkPhysicalDeviceMemoryProperties memory_properties; List layer_properties; List extension_properties; @@ -18,11 +20,16 @@ public: PhysicalDevice(VkInstance,VkPhysicalDevice); ~PhysicalDevice()=default; - const bool CheckMemoryType(uint32_t,VkFlags,uint32_t *)const; + const bool CheckMemoryType(uint32_t,VkFlags,uint32_t *)const; - VkPhysicalDeviceType GetDeviceType()const{return properties.deviceType;} + VkPhysicalDeviceType GetDeviceType()const{return properties.deviceType;} + const char * GetDeviceName()const{return properties.deviceName;} - const char *GetDeviceName()const{return properties.deviceName;} + const uint32_t GetExtensionSpecVersion(const UTF8String &name)const; + + const VkDriverIdKHR GetDriverId ()const{return driver_properties.driverID;} + const char * GetDriverName ()const{return driver_properties.driverName;} + const char * GetDriverInfo ()const{return driver_properties.driverInfo;} /** * ȡ豸ǷԿ diff --git a/inc/hgl/graph/vulkan/VKPipeline.h b/inc/hgl/graph/vulkan/VKPipeline.h index 5f224e74..ecee3502 100644 --- a/inc/hgl/graph/vulkan/VKPipeline.h +++ b/inc/hgl/graph/vulkan/VKPipeline.h @@ -4,17 +4,25 @@ #include #include VK_NAMESPACE_BEGIN +using PipelineID=uint64; + class Pipeline { VkDevice device; VkPipeline pipeline; + bool alpha_test; + bool alpha_blend; + public: - Pipeline(VkDevice dev,VkPipeline p){device=dev;pipeline=p;} + Pipeline(VkDevice dev,VkPipeline p,bool at,bool ab):device(dev),pipeline(p),alpha_test(at),alpha_blend(ab){} virtual ~Pipeline(); operator VkPipeline(){return pipeline;} + + const bool IsAlphaTest()const{return alpha_test;} + const bool IsAlphaBlend()const{return alpha_blend;} };//class GraphicsPipeline constexpr size_t MAX_SAMPLE_MASK_COUNT=(VK_SAMPLE_COUNT_64_BIT+31)/32; @@ -54,6 +62,9 @@ class PipelineCreater void InitDynamicState(); + float alpha_test=0; + bool alpha_blend=false; + public: PipelineCreater(Device *dev,const Material *,RenderPass *rp,const VkExtent2D &); @@ -66,6 +77,8 @@ public: void SetDepthRange( float min_depth,float max_depth){viewport.minDepth=min_depth;viewport.maxDepth=max_depth;} void SetScissor( float l,float t,float w,float h){scissor.offset.x=l;scissor.offset.y=t;scissor.extent.width=w;scissor.extent.height=h;} + void SetAlphaTest( const float at) {alpha_test=at;} + void SetDepthTest( bool dt) {depthStencilState.depthTestEnable=dt;} void SetDepthWrite( bool dw) {depthStencilState.depthWriteEnable=dw;} @@ -115,6 +128,9 @@ public: colorBlendAttachments.Add(*cba); colorBlending.attachmentCount=colorBlendAttachments.GetCount(); + + if(cba->blendEnable) + alpha_blend=true; } bool SetBlend(uint index,bool blend) @@ -125,6 +141,22 @@ public: cba->blendEnable=blend; + if(blend) + alpha_blend=true; + else + { + cba=colorBlendAttachments.GetData(); + + for(int i=0;iblendEnable) + { + alpha_blend=true; + return(true); + } + + alpha_blend=false; + } + return(true); } diff --git a/inc/hgl/graph/vulkan/VKRenderable.h b/inc/hgl/graph/vulkan/VKRenderable.h index 61d868cb..d52233ff 100644 --- a/inc/hgl/graph/vulkan/VKRenderable.h +++ b/inc/hgl/graph/vulkan/VKRenderable.h @@ -2,6 +2,7 @@ #define HGL_GRAPH_VULKAN_RENDERABLE_INCLUDE #include +#include #include VK_NAMESPACE_BEGIN /** @@ -17,14 +18,27 @@ class Renderable VkBuffer *buf_list=nullptr; VkDeviceSize *buf_offset=nullptr; + uint32_t draw_count; + IndexBuffer *indices_buffer=nullptr; VkDeviceSize indices_offset=0; +protected: + + friend class RenderableInstance; + + uint ref_count=0; + + uint RefInc(){return ++ref_count;} + uint RefDec(){return --ref_count;} + public: - Renderable(const VertexShaderModule *); + Renderable(const VertexShaderModule *,const uint32_t dc); virtual ~Renderable(); + const uint GetRefCount()const{return ref_count;} + bool Set(const int stage_input_binding, VertexBuffer *vb,VkDeviceSize offset=0); bool Set(const UTF8String &name, VertexBuffer *vb,VkDeviceSize offset=0); @@ -39,6 +53,14 @@ public: public: + const uint32_t GetDrawCount ()const ///<取得当前对象绘制需要多少个顶点 + { + if(indices_buffer) + return indices_buffer->GetCount(); + + return draw_count; + } + const int GetBufferCount ()const{return buf_count;} const VkBuffer * GetBuffer ()const{return buf_list;} const VkDeviceSize * GetOffset ()const{return buf_offset;} diff --git a/inc/hgl/io/DataInputStream.h b/inc/hgl/io/DataInputStream.h index 64b12ec1..27084380 100644 --- a/inc/hgl/io/DataInputStream.h +++ b/inc/hgl/io/DataInputStream.h @@ -56,7 +56,7 @@ namespace hgl return(in?in->ReadFully(buf,size):-1); } - virtual int64 Seek(int64 offset,SeekOrigin so=soBegin) + virtual int64 Seek(int64 offset,SeekOrigin so=SeekOrigin::Begin) { return(in?in->Seek(offset,so):-1); } diff --git a/inc/hgl/io/DataOutputStream.h b/inc/hgl/io/DataOutputStream.h index ebccb501..e1e7d294 100644 --- a/inc/hgl/io/DataOutputStream.h +++ b/inc/hgl/io/DataOutputStream.h @@ -50,7 +50,7 @@ namespace hgl return(out?out->WriteFully(buf,size):-1); } - virtual int64 Seek(int64 offset,SeekOrigin so=soBegin) + virtual int64 Seek(int64 offset,SeekOrigin so=SeekOrigin::Begin) { return(out?out->Seek(offset,so):-1); } diff --git a/inc/hgl/io/FileAccess.h b/inc/hgl/io/FileAccess.h index 85669978..0dc09fa7 100644 --- a/inc/hgl/io/FileAccess.h +++ b/inc/hgl/io/FileAccess.h @@ -64,7 +64,7 @@ namespace hgl virtual bool CanRestart()const{return CanSeek();} ///<文件是否可复位访问 virtual bool CanSize()const{return(true);} ///<文件是否可取得长度 - virtual int64 Seek(int64,SeekOrigin=soBegin); ///<定位访问指针 + virtual int64 Seek(int64,SeekOrigin=SeekOrigin::Begin); ///<定位访问指针 virtual int64 Tell()const; ///<取得访问指针位置 virtual int64 GetSize(); ///<取得文件长度 virtual bool Restart(); ///<复位访问指针 diff --git a/inc/hgl/io/FileInputStream.h b/inc/hgl/io/FileInputStream.h index 72fe582e..83678128 100644 --- a/inc/hgl/io/FileInputStream.h +++ b/inc/hgl/io/FileInputStream.h @@ -39,7 +39,7 @@ namespace hgl virtual int64 Tell()const; ///<取当前位置 virtual int64 GetSize()const; ///<取得文件长度 virtual bool Restart(); ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin); ///<移动访问指针 + virtual int64 Seek(int64,SeekOrigin=SeekOrigin::Begin); ///<移动访问指针 virtual int64 Available()const; ///<剩下的可以不受阻塞访问的字节数 diff --git a/inc/hgl/io/FileOutputStream.h b/inc/hgl/io/FileOutputStream.h index e55d798b..04831031 100644 --- a/inc/hgl/io/FileOutputStream.h +++ b/inc/hgl/io/FileOutputStream.h @@ -41,7 +41,7 @@ namespace hgl virtual int64 Tell()const; ///<取当前位置 virtual int64 GetSize()const; ///<取得文件长度 virtual bool Restart(); ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin); ///<移动访问指针 + virtual int64 Seek(int64,SeekOrigin=SeekOrigin::Begin); ///<移动访问指针 virtual int64 Available()const{return -1;} ///<可不受影响写入的字节数 virtual int64 Write(int64,const void *,int64); ///<在指定位置写入指定长度的数据 diff --git a/inc/hgl/io/InputStream.h b/inc/hgl/io/InputStream.h index c2dd5963..ab26b419 100644 --- a/inc/hgl/io/InputStream.h +++ b/inc/hgl/io/InputStream.h @@ -29,7 +29,7 @@ namespace hgl virtual bool Restart()=0; ///<复位访问指针 virtual int64 Skip(int64)=0; ///<跳过指定字节不访问 - virtual int64 Seek(int64,SeekOrigin=soBegin)=0; ///<移动访问指针 + virtual int64 Seek(int64,SeekOrigin=SeekOrigin::Begin)=0; ///<移动访问指针 virtual int64 Tell()const=0; ///<返回当前访问位置 virtual int64 GetSize()const=0; ///<取得流长度 virtual int64 Available()const=0; ///<剩下的可以不受阻塞访问的字节数 diff --git a/inc/hgl/io/MemoryInputStream.h b/inc/hgl/io/MemoryInputStream.h index 589f7dcf..8cebe5f4 100644 --- a/inc/hgl/io/MemoryInputStream.h +++ b/inc/hgl/io/MemoryInputStream.h @@ -115,19 +115,19 @@ namespace hgl int64 Skip(int64 bytes) { - return Seek(bytes,soCurrent); + return Seek(bytes,SeekOrigin::Current); } int64 Seek(int64 off,SeekOrigin so) { if(!CanSeek())return(-1); - if(so==soCurrent) + if(so==SeekOrigin::Current) { off+=cur_pos; } else - if(so==soEnd) + if(so==SeekOrigin::End) { off+=buf_size; } diff --git a/inc/hgl/io/MemoryOutputStream.h b/inc/hgl/io/MemoryOutputStream.h index cdd528d7..04baf935 100644 --- a/inc/hgl/io/MemoryOutputStream.h +++ b/inc/hgl/io/MemoryOutputStream.h @@ -194,16 +194,16 @@ namespace hgl return(true); } - int64 Seek(int64 off,SeekOrigin so=soBegin) override + int64 Seek(int64 off,SeekOrigin so=SeekOrigin::Begin) override { if(!CanSeek())return(-1); - if(so==soCurrent) + if(so==SeekOrigin::Current) { off+=cur_pos; } else - if(so==soEnd) + if(so==SeekOrigin::End) { off+=buf_size; } diff --git a/inc/hgl/io/OutputStream.h b/inc/hgl/io/OutputStream.h index a0022836..624f6cf6 100644 --- a/inc/hgl/io/OutputStream.h +++ b/inc/hgl/io/OutputStream.h @@ -26,7 +26,7 @@ namespace hgl virtual bool CanSize()const=0; ///<是否可以取得尺寸 virtual bool Restart()=0; ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin)=0; ///<移动访问指针 + virtual int64 Seek(int64,SeekOrigin=SeekOrigin::Begin)=0; ///<移动访问指针 virtual int64 Tell()const=0; ///<返回当前访问位置 virtual int64 GetSize()const=0; ///<取得流长度 virtual int64 Available()const=0; ///<剩下的可以不受阻塞写入的字节数 diff --git a/inc/hgl/io/RandomAccessFile.h b/inc/hgl/io/RandomAccessFile.h index 01f1e4ed..ed24b74f 100644 --- a/inc/hgl/io/RandomAccessFile.h +++ b/inc/hgl/io/RandomAccessFile.h @@ -40,7 +40,7 @@ namespace hgl virtual int64 Tell()const; ///<取当前位置 virtual int64 GetSize(); ///<取得文件长度 virtual bool Restart(); ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin); ///<移动访问指针 + virtual int64 Seek(int64,SeekOrigin=SeekOrigin::Begin); ///<移动访问指针 };//class RandomAccessFile }//namespace io }//namespace hgl diff --git a/inc/hgl/io/SeekAccess.h b/inc/hgl/io/SeekAccess.h index 1db8198a..89774213 100644 --- a/inc/hgl/io/SeekAccess.h +++ b/inc/hgl/io/SeekAccess.h @@ -1,4 +1,4 @@ -#ifndef HGL_IO_SEEK_ACCESS_INCLUDE +#ifndef HGL_IO_SEEK_ACCESS_INCLUDE #define HGL_IO_SEEK_ACCESS_INCLUDE #include @@ -6,11 +6,11 @@ namespace hgl { namespace io { - enum_int(SeekOrigin) /// 资源偏移方向枚举 + enum class SeekOrigin /// 资源偏移方向枚举 { - soBegin=0, ///<从资源最开始处开始,offset必须大于0。移到资源的offset位置 - soCurrent, ///<从资源当前位置开始,移到资源的Position+offset位置 - soEnd ///<从资源的结束位置开始,offset必须小于0,表示结束前的字符数 + Begin=0, ///<从资源最开始处开始,offset必须大于0。移到资源的offset位置 + Current, ///<从资源当前位置开始,移到资源的Position+offset位置 + End ///<从资源的结束位置开始,offset必须小于0,表示结束前的字符数 };//enum SeekOrigin /** @@ -27,7 +27,7 @@ namespace hgl virtual bool CanSize()const=0; ///<是否可以取得尺寸 virtual bool Restart()=0; ///<复位访问指针 - virtual int64 Seek(int64,SeekOrigin=soBegin)=0; ///<移动访问指针 + virtual int64 Seek(int64,SeekOrigin=SeekOrigin::Begin)=0; ///<移动访问指针 virtual int64 Tell()const=0; ///<返回当前访问位置 virtual int64 GetSize()const=0; ///<取得文件长度 };//class SeekAccess diff --git a/inc/hgl/type/ResManage.cpp b/inc/hgl/type/ResManage.cpp index ee61e8d2..a72eb810 100644 --- a/inc/hgl/type/ResManage.cpp +++ b/inc/hgl/type/ResManage.cpp @@ -79,22 +79,11 @@ namespace hgl { ResItem *obj=items.GetItem(index); - //items[index]->count++; - obj->count++; + ++obj->count; -// return(items[index]->data); return obj->right; } - T *data=Create(flag); - - if(data) - { - items.Add(flag,data); - - return(data); - } - return(nullptr); } diff --git a/inc/hgl/type/ResManage.h b/inc/hgl/type/ResManage.h index 59850933..a7aa827d 100644 --- a/inc/hgl/type/ResManage.h +++ b/inc/hgl/type/ResManage.h @@ -31,7 +31,6 @@ namespace hgl protected: - virtual T *Create(const F &)=0; ///<资源创建虚拟函数无实现,请特例化实现 virtual void Clear(T *obj){delete obj;} ///<资源释放虚拟函数(缺省为直接delete对象) public: @@ -45,11 +44,33 @@ namespace hgl virtual bool Add(const F &,T *); ///<添加一个数据 virtual T * Find(const F &); ///<查找一个数据 - virtual T * Get(const F &); ///<取得一个数据,如不存在则创建 + virtual T * Get(const F &); ///<取得一个数据 virtual void Release(const F &,bool zero_clear=false); ///<释放一个数据 virtual void Release(T *,bool zero_clear=false); ///<释放一个数据 };//template class ResManage + + /** + * 使用int类做数标致的资源管理器 + */ + template class IDResManage:public ResManage + { + F id_count=0; + + public: + + using ResManage::ResManage; + virtual ~IDResManage()=default; + + virtual F Add(T *value) + { + if(!value)return(-1); + if(!ResManage::Add(id_count,value)) + return(-1); + + return id_count++; + } + };//template class IDResManage:public ResManage }//namespace hgl #include #endif//HGL_RES_MANAGE_INCLUDE diff --git a/src/Base/IO/FileAccess.cpp b/src/Base/IO/FileAccess.cpp index 08475d56..2d33cd8a 100644 --- a/src/Base/IO/FileAccess.cpp +++ b/src/Base/IO/FileAccess.cpp @@ -109,7 +109,7 @@ namespace hgl { if(!CanSeek())return(-1); - return hgl_lseek64(fp,offset,orign); + return hgl_lseek64(fp,offset,(int)orign); } int64 FileAccess::Tell()const @@ -142,7 +142,7 @@ namespace hgl { if(!CanRestart())return(false); - return(Seek(0,soBegin)==0); + return(Seek(0,SeekOrigin::Begin)==0); } int64 FileAccess::Read(void *buf,int64 size) @@ -159,7 +159,7 @@ namespace hgl int result=hgl_read64(fp,buf,size); if(result>0) - hgl_lseek64(fp,-result,soCurrent); //回移这个长度 + hgl_lseek64(fp,-result,(int)SeekOrigin::Current); //回移这个长度 return result; } diff --git a/src/Base/IO/FileInputStream.cpp b/src/Base/IO/FileInputStream.cpp index 26fda0a0..ab54e0ea 100644 --- a/src/Base/IO/FileInputStream.cpp +++ b/src/Base/IO/FileInputStream.cpp @@ -52,7 +52,7 @@ namespace hgl bool FileInputStream::CanSize ()const {return file?file->CanSize():false;} bool FileInputStream::CanPeek ()const {return file?file->CanPeek():false;} - int64 FileInputStream::Skip (int64 bytes) {return file?file->Seek(bytes,soCurrent):-1;} + int64 FileInputStream::Skip (int64 bytes) {return file?file->Seek(bytes,SeekOrigin::Current):-1;} int64 FileInputStream::Tell ()const {return file?file->Tell():-1;} int64 FileInputStream::GetSize ()const {return file?file->GetSize():-1;} bool FileInputStream::Restart () {return file?file->Restart():false;} diff --git a/src/Platform/Android/AssetManage.cpp b/src/Platform/Android/AssetManage.cpp new file mode 100644 index 00000000..bdca56af --- /dev/null +++ b/src/Platform/Android/AssetManage.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include + +namespace hgl +{ + namespace + { + static AAssetManager *asset_manager=nullptr; + + class AndroidAssetInputStream:public io::InputStream + { + AAsset *asset; + + public: + + AndroidAssetInputStream(AAsset *a) + { + asset=a; + } + + ~AndroidAssetInputStream() + { + Close(); + } + + void Close() override + { + if(!asset)return; + + AAsset_close(asset); + asset=nullptr; + } + + int64 Read(void *data,int64 size) override + { + return AAsset_read(asset,data,size); + } + + int64 Peek(void *data,int64 size) override + { + int64 left=Avaiable(); + + if(left0) + Seek(-result,SeekOrigin::Current); + + return result; + } + + int64 ReadFully(void *buf,int64 buf_size){return Read(buf,buf_size);} ///<充分读取,保证读取到指定长度的数据(不计算超时) + + bool CanRestart()const override{return(true);} + bool CanSeek()const override{return(true);} + bool CanSize()const override{return(true);} + bool CanPeek()const override{return(false);} + + bool Restart() override + { + Seek(0,soBegin); + return(true); + } + + int64 Skip(int64 size) override + { + return Seek(size,SeekOrigin::Current); + } + + int64 Seek(int64 offset,SeekOrigin so) override + { + return AAsset_seek64(asset,offset,(int)so); + } + + int64 Tell()const override + { + return Seek(0,SeekOrigin::Current); + } + + int64 GetSize()const override + { + return AAsset_getLength64(asset); + } + + int64 Available()const override + { + return AAsset_getRemainingLength64(asset); + } + };//class AndroidAssetInputStream:public io::InputStream + }//namespace + + void InitAssetManager(AAssetManager *am) + { + asset_manager=am; + } + + io::InputStream *OpenAsset(const UTF8String &filename) + { + AAsset *asset = AAssetManager_open(asset_manager, filename.c_str(), AASSET_MODE_BUFFER); + + if(!asset)return(nullptr); + + return(new AndroidAssetInputStream(asset)); + } +}//namespace hgl diff --git a/src/Platform/CMakeLists.txt b/src/Platform/CMakeLists.txt index 8cfbeeb3..62da6e3f 100644 --- a/src/Platform/CMakeLists.txt +++ b/src/Platform/CMakeLists.txt @@ -18,7 +18,8 @@ Android/NativeActivitySupport.cpp) SET(PLATFORM_FILE_SOURCE ${PLATFORM_FILE_SOURCE} - Android/ProgramPath.cpp) + Android/ProgramPath.cpp + Android/AssetManage.cpp) SET(PLATFORM_MULTI_THREAD_SOURCE ${PLATFORM_MULTI_THREAD_SOURCE} UNIX/Semaphore.cpp) diff --git a/src/Platform/Win/FileAccess.cpp b/src/Platform/Win/FileAccess.cpp index ad750388..be8bcda9 100644 --- a/src/Platform/Win/FileAccess.cpp +++ b/src/Platform/Win/FileAccess.cpp @@ -49,7 +49,7 @@ namespace hgl { if(!CanRead())return(-1); - if(_lseeki64(fp,offset,soBegin)==offset) + if(_lseeki64(fp,offset,(int)SeekOrigin::Begin)==offset) return _read(fp,buf,size); else return -1; @@ -59,7 +59,7 @@ namespace hgl { if(!CanWrite())return(-1); - if(_lseeki64(fp,offset,soBegin)==offset) + if(_lseeki64(fp,offset,(int)SeekOrigin::Begin)==offset) return _write(fp,buf,size); else return -1; diff --git a/src/RenderDevice/Vulkan/POD/VKPipelineCreateInfo.POD.cpp b/src/RenderDevice/Vulkan/POD/VKPipelineCreateInfo.POD.cpp index 3a84770b..9b1f69de 100644 --- a/src/RenderDevice/Vulkan/POD/VKPipelineCreateInfo.POD.cpp +++ b/src/RenderDevice/Vulkan/POD/VKPipelineCreateInfo.POD.cpp @@ -41,6 +41,8 @@ bool PipelineCreater::SaveToStream(io::DataOutputStream *dos) for(uint32_t i=0;iattachmentCount;i++) WRITE_AND_CHECK_SIZE(pipelineInfo.pColorBlendState->pAttachments+i,VkPipelineColorBlendAttachmentState); + if(!dos->WriteFloat(alpha_test))return(false); + return(true); } @@ -89,16 +91,29 @@ bool PipelineCreater::LoadFromMemory(uchar *data,uint size) if(sizeblendEnable) + alpha_blend=true; + + ++cba; + } } else { colorBlending.pAttachments=nullptr; + alpha_blend=false; } + CHECK_SIZE_AND_COPY(alpha_test,float); + return(true); } VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp index 3954849b..ca9baf0a 100644 --- a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp @@ -3,6 +3,10 @@ #include #include +#ifdef _DEBUG +#include +#endif//_DEBUG + VK_NAMESPACE_BEGIN namespace { @@ -300,6 +304,24 @@ namespace Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,uint width,uint height) { + #ifdef _DEBUG + { + const VkDriverIdKHR driver_id=physical_device->GetDriverId(); + + if(driver_id>=VK_DRIVER_ID_BEGIN_RANGE_KHR + &&driver_id<=VK_DRIVER_ID_END_RANGE_KHR) + { + std::cout<<"DriverID: "<GetDriverId()<GetDriverName()<GetDriverInfo()< auto_delete(attr); diff --git a/src/RenderDevice/Vulkan/VKMaterial.cpp b/src/RenderDevice/Vulkan/VKMaterial.cpp index 72de44a5..f2e83920 100644 --- a/src/RenderDevice/Vulkan/VKMaterial.cpp +++ b/src/RenderDevice/Vulkan/VKMaterial.cpp @@ -117,8 +117,8 @@ void Material::Write(VkPipelineVertexInputStateCreateInfo &vis)const return vab->Write(vis); } -Renderable *Material::CreateRenderable() +Renderable *Material::CreateRenderable(const uint32_t draw_count) { - return(new Renderable(vertex_sm)); + return(new Renderable(vertex_sm,draw_count)); } VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp b/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp index 70953e7c..f618dbc2 100644 --- a/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp +++ b/src/RenderDevice/Vulkan/VKPhysicalDevice.cpp @@ -6,10 +6,6 @@ PhysicalDevice::PhysicalDevice(VkInstance inst,VkPhysicalDevice pd) instance=inst; physical_device=pd; - vkGetPhysicalDeviceFeatures(physical_device,&features); - vkGetPhysicalDeviceProperties(physical_device,&properties); - vkGetPhysicalDeviceMemoryProperties(physical_device,&memory_properties); - { uint32_t property_count; @@ -31,6 +27,49 @@ PhysicalDevice::PhysicalDevice(VkInstance inst,VkPhysicalDevice pd) debug_out(extension_properties); } + + vkGetPhysicalDeviceFeatures(physical_device,&features); + vkGetPhysicalDeviceMemoryProperties(physical_device,&memory_properties); + + PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2=nullptr; + + if(GetExtensionSpecVersion(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) + GetPhysicalDeviceProperties2=(PFN_vkGetPhysicalDeviceProperties2KHR)vkGetInstanceProcAddr(instance,"vkGetPhysicalDeviceProperties2KHR"); + + if(!GetPhysicalDeviceProperties2) + if(GetExtensionSpecVersion(VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME)) + GetPhysicalDeviceProperties2=(PFN_vkGetPhysicalDeviceProperties2)vkGetInstanceProcAddr(instance,"vkGetPhysicalDeviceProperties2"); + + if(GetPhysicalDeviceProperties2) + { + VkPhysicalDeviceProperties2KHR properties2; + GetPhysicalDeviceProperties2(physical_device,&properties2); + + hgl_cpy(properties,properties2.properties); + + if(properties2.pNext) + memcpy(&driver_properties,properties2.pNext,sizeof(VkPhysicalDeviceDriverPropertiesKHR)); + } + else + { + vkGetPhysicalDeviceProperties(physical_device,&properties); + + hgl_zero(driver_properties); + } +} + +const uint32_t PhysicalDevice::GetExtensionSpecVersion(const UTF8String &name)const +{ + const uint count=extension_properties.GetCount(); + const VkExtensionProperties *ep=extension_properties.GetData(); + + for(uint i=0;iextensionName)==0) + return ep->specVersion; + } + + return 0; } const bool PhysicalDevice::CheckMemoryType(uint32_t typeBits,VkFlags requirements_mask,uint32_t *typeIndex)const diff --git a/src/RenderDevice/Vulkan/VKPipeline.cpp b/src/RenderDevice/Vulkan/VKPipeline.cpp index 689005ff..9730d909 100644 --- a/src/RenderDevice/Vulkan/VKPipeline.cpp +++ b/src/RenderDevice/Vulkan/VKPipeline.cpp @@ -147,6 +147,8 @@ PipelineCreater::PipelineCreater(Device *dev,const Material *material,RenderPass colorBlendAttachments.Add(cba); + alpha_blend=false; + colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; colorBlending.pNext = nullptr; colorBlending.flags = 0; @@ -231,6 +233,6 @@ Pipeline *PipelineCreater::Create() if (vkCreateGraphicsPipelines(device, cache, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) return(nullptr); - return(new Pipeline(device,graphicsPipeline)); + return(new Pipeline(device,graphicsPipeline,alpha_test>0,alpha_blend)); } VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKRenderable.cpp b/src/RenderDevice/Vulkan/VKRenderable.cpp index 1e2194d0..28d2092b 100644 --- a/src/RenderDevice/Vulkan/VKRenderable.cpp +++ b/src/RenderDevice/Vulkan/VKRenderable.cpp @@ -3,9 +3,10 @@ #include VK_NAMESPACE_BEGIN -Renderable::Renderable(const VertexShaderModule *vsm) +Renderable::Renderable(const VertexShaderModule *vsm,const uint32_t dc) { vertex_sm=vsm; + draw_count=dc; buf_count=vertex_sm->GetAttrCount(); diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 5b0413ee..1ad55edd 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -4,13 +4,15 @@ SET(SCENE_GRAPH_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/AABox.h ${ROOT_INCLUDE_PATH}/hgl/graph/SceneNode.h ${ROOT_INCLUDE_PATH}/hgl/graph/SceneOrient.h ${ROOT_INCLUDE_PATH}/hgl/graph/VertexBufferCreater.h - ${ROOT_INCLUDE_PATH}/hgl/graph/VertexBuffer.h) + ${ROOT_INCLUDE_PATH}/hgl/graph/VertexBuffer.h + ${ROOT_INCLUDE_PATH}/hgl/graph/InlineGeometry.h) SET(SCENE_GRAPH_SOURCE AABox.cpp Camera.cpp -# RenderList.cpp + RenderList.cpp SceneNode.cpp - SceneOrient.cpp) + SceneOrient.cpp + InlineGeometry.cpp) SOURCE_GROUP("Header Files" FILES ${SCENE_GRAPH_HEADER}) SOURCE_GROUP("Source Files" FILES ${SCENE_GRAPH_SOURCE}) diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp new file mode 100644 index 00000000..faff68ce --- /dev/null +++ b/src/SceneGraph/InlineGeometry.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include +#include +namespace hgl +{ + namespace graph + { + vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl,const RectangleCreateInfo *rci) + { + const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule(); + + const int vertex_binding=vsm->GetStageInputBinding("Vertex"); + + if(vertex_binding==-1) + return(nullptr); + + VB2f *vertex=new VB2f(4); + + vertex->Begin(); + vertex->WriteRectFan(rci->scope); + vertex->End(); + + vulkan::Renderable *render_obj=mtl->CreateRenderable(vertex->GetCount()); + render_obj->Set(vertex_binding,device->CreateVBO(vertex)); + + delete vertex; + return render_obj; + } + + vulkan::Renderable *CreateRoundRectangle(vulkan::Device *device,vulkan::Material *mtl,const RoundRectangleCreateInfo *rci) + { + const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule(); + + vulkan::Renderable *render_obj=nullptr; + const int vertex_binding=vsm->GetStageInputBinding("Vertex"); + + if(vertex_binding==-1) + return(nullptr); + + VB2f *vertex=nullptr; + + if(rci->radius==0||rci->round_per<=1) //这是要画矩形 + { + vertex=new VB2f(4); + + vertex->Begin(); + vertex->WriteRectFan(rci->scope); + vertex->End(); + } + else + { + float radius=rci->radius; + + if(radius>rci->scope.GetWidth()/2.0f)radius=rci->scope.GetWidth()/2.0f; + if(radius>rci->scope.GetHeight()/2.0f)radius=rci->scope.GetHeight()/2.0f; + + vertex=new VB2f(rci->round_per*4); + + vertex->Begin(); + + vec2 *coord=new vec2[rci->round_per]; + + for(uint i=0;iround_per;i++) + { + float ang=float(i)/float(rci->round_per-1)*90.0f; + + float x=sin(hgl_ang2rad(ang))*radius; + float y=cos(hgl_ang2rad(ang))*radius; + + coord[i].x=x; + coord[i].y=y; + + //右上角 + vertex->Write( rci->scope.GetRight()-radius+x, + rci->scope.GetTop()+radius-y); + } + + //右下角 + for(uint i=0;iround_per;i++) + { + vertex->Write(rci->scope.GetRight() -radius+coord[rci->round_per-1-i].x, + rci->scope.GetBottom()-radius+coord[rci->round_per-1-i].y); + } + + //左下角 + for(uint i=0;iround_per;i++) + { + vertex->Write(rci->scope.GetLeft() +radius-coord[i].x, + rci->scope.GetBottom()-radius+coord[i].y); + } + + //左上角 + for(uint i=0;iround_per;i++) + { + vertex->Write(rci->scope.GetLeft() +radius-coord[rci->round_per-1-i].x, + rci->scope.GetTop() +radius-coord[rci->round_per-1-i].y); + } + + delete[] coord; + + vertex->End(); + } + + render_obj=mtl->CreateRenderable(vertex->GetCount()); + render_obj->Set(vertex_binding,device->CreateVBO(vertex)); + + delete vertex; + return render_obj; + } + + vulkan::Renderable *CreateCircle(vulkan::Device *device,vulkan::Material *mtl,const CircleCreateInfo *cci) + { + const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule(); + + const int vertex_binding=vsm->GetStageInputBinding("Vertex"); + + if(vertex_binding==-1) + return(nullptr); + + VB2f *vertex=new VB2f(cci->field_count+2); + + vertex->Begin(); + + vertex->Write(cci->center.x,cci->center.y); + + for(uint i=0;i<=cci->field_count;i++) + { + float ang=float(i)/float(cci->field_count)*360.0f; + + float x=cci->center.x+sin(hgl_ang2rad(ang))*cci->radius.x; + float y=cci->center.y+cos(hgl_ang2rad(ang))*cci->radius.y; + + vertex->Write(x,y); + } + + vertex->End(); + + vulkan::Renderable *render_obj=mtl->CreateRenderable(vertex->GetCount()); + render_obj->Set(vertex_binding,device->CreateVBO(vertex)); + + delete vertex; + return render_obj; + } + }//namespace graph +}//namespace hgl \ No newline at end of file diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index d0f8dda3..9f0911a5 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -1,31 +1,30 @@ #include #include -#include +#include #include -#include //#include -#include +#include namespace hgl { namespace graph { -/* float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two) + float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two) { if(!cam||!obj_one||!obj_two) return(0); - return( length_squared(obj_one->GetCenter(),cam->eye)- + return( length_squared(obj_one->GetCenter(),cam->eye)- length_squared(obj_two->GetCenter(),cam->eye)); } - bool FrustumClipFilter(const SceneNode *node,void *fc) - { - if(!node||!fc)return(false); + //bool FrustumClipFilter(const SceneNode *node,void *fc) + //{ + // if(!node||!fc)return(false); - return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE); - }*/ + // return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE); + //} /** * 使用指定矩阵渲染一个渲染列表 @@ -47,7 +46,7 @@ namespace hgl const Matrix4f fin_mv=(*mv)*(*node)->GetLocalToWorldMatrix(); int sn=(*node)->SubData.GetCount(); - Renderable **p=(*node)->SubData.GetData(); + RenderableInstance **p=(*node)->SubData.GetData(); for(int j=0;j