From a988f36b920115a4c3033c74cb8dec40efcae9d8 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 23 May 2019 13:24:21 +0800 Subject: [PATCH 01/10] =?UTF-8?q?PhysicalDevice=E5=A2=9E=E5=8A=A0=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8ID=E8=8E=B7=E5=8F=96=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/graph/vulkan/VKPhysicalDevice.h | 13 ++++-- src/RenderDevice/Vulkan/VKDeviceCreater.cpp | 22 +++++++++ src/RenderDevice/Vulkan/VKPhysicalDevice.cpp | 47 ++++++++++++++++++-- 3 files changed, 75 insertions(+), 7 deletions(-) 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/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/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 From 96d9132b29178402d485840ed36e14790087b815 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 23 May 2019 13:29:23 +0800 Subject: [PATCH 02/10] =?UTF-8?q?Pipeline=E5=A2=9E=E5=8A=A0alpha=5Ftest/al?= =?UTF-8?q?pha=5Fblend=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/graph/vulkan/VKPipeline.h | 32 ++++++++++++++++++- .../Vulkan/POD/VKPipelineCreateInfo.POD.cpp | 15 +++++++++ src/RenderDevice/Vulkan/VKPipeline.cpp | 4 ++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/inc/hgl/graph/vulkan/VKPipeline.h b/inc/hgl/graph/vulkan/VKPipeline.h index 5f224e74..83e4a01e 100644 --- a/inc/hgl/graph/vulkan/VKPipeline.h +++ b/inc/hgl/graph/vulkan/VKPipeline.h @@ -9,12 +9,18 @@ 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 +60,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 +75,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 +126,9 @@ public: colorBlendAttachments.Add(*cba); colorBlending.attachmentCount=colorBlendAttachments.GetCount(); + + if(cba->blendEnable) + alpha_blend=true; } bool SetBlend(uint index,bool blend) @@ -125,6 +139,22 @@ public: cba->blendEnable=blend; + if(blend) + alpha_blend=true; + else + { + cba=colorBlendAttachments.GetData(); + + for(uint i=0;iblendEnable) + { + alpha_blend=true; + return(true); + } + + alpha_blend=false; + } + return(true); } 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/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 From 5ec1dee6b5338cf1322331d098106f90447a8f08 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 23 May 2019 14:55:07 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E7=94=BB=E7=9F=A9=E5=BD=A2=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E4=BD=BF=E7=94=A84=E4=B8=AA=E9=A1=B6=E7=82=B9?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84FAN=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/Geometry2D.cpp | 95 +++++++++++++++++++++++++++++++---- inc/hgl/graph/VertexBuffer.h | 48 ++++++++++++++++++ 2 files changed, 133 insertions(+), 10 deletions(-) diff --git a/example/Vulkan/Geometry2D.cpp b/example/Vulkan/Geometry2D.cpp index ee03b4e7..6e21e28c 100644 --- a/example/Vulkan/Geometry2D.cpp +++ b/example/Vulkan/Geometry2D.cpp @@ -30,6 +30,81 @@ struct RectangleCreateInfo RectScope2f tex_coord; }; +VK_NAMESPACE_BEGIN +/** + * 鍦烘櫙DB锛岀敤浜庣鐞嗗満鏅唴鎵闇鐨勬墍鏈夋暟鎹 + */ +class SceneDatabase +{ + Set material_sets; ///<鏉愯川鍚堥泦 + Set pipeline_sets; ///<绠$嚎鍚堥泦 + Set desc_sets_sets; ///<鎻忚堪绗﹀悎闆 + Set renderable_sets; ///<鍙覆鏌撳璞″悎闆 +};//class SceneDatabase + +/** + * 鍙覆鏌撳璞″疄渚 + */ +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()=default; + + Pipeline * GetPipeline (){return pipeline;} + DescriptorSets *GetDescriptorSets (){return desc_sets;} + Renderable * GetRenderable (){return render_obj;} + + const int Comp(const RenderableInstance *ri)const + { + //缁樺埗椤哄簭锛 + + // ARM Mali GPU : 涓嶉忔槑銆丄lphaTest銆佸崐閫忔槑 + // Adreno/NV/AMD: AlphaTest銆佷笉閫忔槑銆佸崐閫忔槑 + // PowerVR: 鍚孉dreno/NV/AMD锛屼絾涓嶉忔槑閮ㄥ垎鍙互涓嶆帓搴 + + if(pipeline->IsAlphaTest()) + { + if(!ri->pipeline->IsAlphaTest()) + return -1; + } + else + { + if(ri->pipeline->IsAlphaTest()) + return 1; + } + + if(pipeline->IsAlphaBlend()) + { + if(!ri->pipeline->IsAlphaBlend()) + return 1; + } + else + { + if(ri->pipeline->IsAlphaBlend()) + 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 + +constexpr uint32_t VERTEX_COUNT=4; + vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl,const RectangleCreateInfo *rci) { const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule(); @@ -40,10 +115,10 @@ vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl if(vertex_binding!=-1) { - VB2f *vertex=new VB2f(6); + VB2f *vertex=new VB2f(VERTEX_COUNT); vertex->Begin(); - vertex->WriteRect(rci->scope); + vertex->WriteRectFan(rci->scope); vertex->End(); render_obj->Set(vertex_binding,device->CreateVBO(vertex)); @@ -54,10 +129,10 @@ vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl if(normal_binding!=-1) { - VB3f *normal=new VB3f(6); + VB3f *normal=new VB3f(VERTEX_COUNT); normal->Begin(); - normal->Write(rci->normal,6); + normal->Write(rci->normal,VERTEX_COUNT); normal->End(); render_obj->Set(normal_binding,device->CreateVBO(normal)); @@ -68,10 +143,10 @@ vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl if(color_binding!=-1) { - VB4f *color=new VB4f(6); + VB4f *color=new VB4f(VERTEX_COUNT); color->Begin(); - color->Write(rci->color,6); + color->Write(rci->color,VERTEX_COUNT); color->End(); render_obj->Set(color_binding,device->CreateVBO(color)); @@ -82,10 +157,10 @@ vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl if(tex_coord_binding!=-1) { - VB2f *tex_coord=new VB2f(6); + VB2f *tex_coord=new VB2f(VERTEX_COUNT); tex_coord->Begin(); - tex_coord->WriteRect(rci->tex_coord); + tex_coord->WriteRectFan(rci->tex_coord); tex_coord->End(); render_obj->Set(tex_coord_binding,device->CreateVBO(tex_coord)); @@ -186,7 +261,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(); @@ -212,7 +287,7 @@ private: cmd_buf[i]->Bind(pipeline); cmd_buf[i]->Bind(desciptor_sets); cmd_buf[i]->Bind(render_obj); - cmd_buf[i]->Draw(6); + cmd_buf[i]->Draw(VERTEX_COUNT); cmd_buf[i]->EndRenderPass(); cmd_buf[i]->End(); } diff --git a/inc/hgl/graph/VertexBuffer.h b/inc/hgl/graph/VertexBuffer.h index e0bf0f33..a7defe09 100644 --- a/inc/hgl/graph/VertexBuffer.h +++ b/inc/hgl/graph/VertexBuffer.h @@ -348,6 +348,54 @@ 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 /** From 593b0b4a03c712a6de38b3fdf5bdc3fa4df3ef3c Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 23 May 2019 19:23:49 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=BB=E5=9C=86?= =?UTF-8?q?=E8=A7=92=E7=9F=A9=E5=BD=A2=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/Geometry2D.cpp | 201 +++++++++++++++-------- example/Vulkan/indices_rect.cpp | 12 +- example/Vulkan/main.cpp | 12 +- inc/hgl/graph/vulkan/VKMaterial.h | 2 +- inc/hgl/graph/vulkan/VKPipeline.h | 2 +- inc/hgl/graph/vulkan/VKRenderable.h | 24 ++- src/RenderDevice/Vulkan/VKMaterial.cpp | 4 +- src/RenderDevice/Vulkan/VKRenderable.cpp | 3 +- 8 files changed, 178 insertions(+), 82 deletions(-) diff --git a/example/Vulkan/Geometry2D.cpp b/example/Vulkan/Geometry2D.cpp index 6e21e28c..33ef1c32 100644 --- a/example/Vulkan/Geometry2D.cpp +++ b/example/Vulkan/Geometry2D.cpp @@ -25,9 +25,6 @@ struct WorldConfig struct RectangleCreateInfo { RectScope2f scope; - vec3 normal; - Color4f color; - RectScope2f tex_coord; }; VK_NAMESPACE_BEGIN @@ -109,7 +106,7 @@ vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl { const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule(); - vulkan::Renderable *render_obj=mtl->CreateRenderable(); + vulkan::Renderable *render_obj=mtl->CreateRenderable(VERTEX_COUNT); const int vertex_binding=vsm->GetStageInputBinding("Vertex"); @@ -124,60 +121,105 @@ vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl render_obj->Set(vertex_binding,device->CreateVBO(vertex)); delete vertex; } - - const int normal_binding=vsm->GetStageInputBinding("Normal"); - - if(normal_binding!=-1) + else { - VB3f *normal=new VB3f(VERTEX_COUNT); - - normal->Begin(); - normal->Write(rci->normal,VERTEX_COUNT); - 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(VERTEX_COUNT); - - color->Begin(); - color->Write(rci->color,VERTEX_COUNT); - 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(VERTEX_COUNT); - - tex_coord->Begin(); - tex_coord->WriteRectFan(rci->tex_coord); - tex_coord->End(); - - render_obj->Set(tex_coord_binding,device->CreateVBO(tex_coord)); - delete tex_coord; + delete render_obj; + return nullptr; } return render_obj; } -/** - * 鍒涘缓涓涓渾瑙掔煩褰 - * @param r 鍗婂緞 - * @param rp 鍗婂緞绮惧害 - */ -//Mesh *CreateRoundrectangle(float l,float t,float w,float h,float r,uint32_t rp) -//{ -//} +struct RoundRectangleCreateInfo:public RectangleCreateInfo +{ + float radius; //鍦嗚鍗婂緞 + uint32_t round_per; //鍦嗚绮惧害 +}; + +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(1+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); + } + + vertex->Write(rci->scope.GetRight() -radius+coord[0].x, + rci->scope.GetTop() +radius-coord[0].y); + + delete[] coord; + + vertex->End(); + } + + render_obj=mtl->CreateRenderable(vertex->GetCount()); + render_obj->Set(vertex_binding,device->CreateVBO(vertex)); + + delete vertex; + + return render_obj; +} class TestApp:public VulkanApplicationFramework { @@ -186,8 +228,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; @@ -200,13 +244,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); } @@ -219,17 +264,25 @@ 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); return render_obj; } @@ -245,10 +298,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); } @@ -271,6 +324,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); @@ -284,10 +358,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(VERTEX_COUNT); + Draw(cmd_buf[i],render_instance); cmd_buf[i]->EndRenderPass(); cmd_buf[i]->End(); } @@ -316,6 +387,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..7ec0fd0e 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); } @@ -74,7 +74,7 @@ private: return(false); render_obj=material->CreateRenderable(); - desciptor_sets=material->CreateDescriptorSets(); + 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..f4659205 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); } @@ -75,7 +75,7 @@ private: return(false); render_obj=material->CreateRenderable(); - desciptor_sets=material->CreateDescriptorSets(); + 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/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/VKPipeline.h b/inc/hgl/graph/vulkan/VKPipeline.h index 83e4a01e..2f5f44b5 100644 --- a/inc/hgl/graph/vulkan/VKPipeline.h +++ b/inc/hgl/graph/vulkan/VKPipeline.h @@ -145,7 +145,7 @@ public: { cba=colorBlendAttachments.GetData(); - for(uint i=0;iblendEnable) { alpha_blend=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/src/RenderDevice/Vulkan/VKMaterial.cpp b/src/RenderDevice/Vulkan/VKMaterial.cpp index 7c7cf1cf..0ba62d6f 100644 --- a/src/RenderDevice/Vulkan/VKMaterial.cpp +++ b/src/RenderDevice/Vulkan/VKMaterial.cpp @@ -114,8 +114,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/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(); From 5b55c34334db4eb631acc7239d724782a026527e Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 23 May 2019 21:50:58 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E6=94=B9=E8=BF=9BResManage=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0IDResManage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/Geometry2D.cpp | 29 ++++++++++++++++++++++++----- inc/hgl/graph/vulkan/VKPipeline.h | 2 ++ inc/hgl/type/ResManage.cpp | 13 +------------ inc/hgl/type/ResManage.h | 24 ++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/example/Vulkan/Geometry2D.cpp b/example/Vulkan/Geometry2D.cpp index 33ef1c32..d37aa4b4 100644 --- a/example/Vulkan/Geometry2D.cpp +++ b/example/Vulkan/Geometry2D.cpp @@ -7,6 +7,7 @@ #include #include #include +#include using namespace hgl; using namespace hgl::graph; @@ -28,15 +29,31 @@ struct RectangleCreateInfo }; VK_NAMESPACE_BEGIN +using MaterialID=int; +using PipelineID=int; +using DescriptorSetsID=int; +using RenderableID=int; + /** * 鍦烘櫙DB锛岀敤浜庣鐞嗗満鏅唴鎵闇鐨勬墍鏈夋暟鎹 */ class SceneDatabase { - Set material_sets; ///<鏉愯川鍚堥泦 - Set pipeline_sets; ///<绠$嚎鍚堥泦 - Set desc_sets_sets; ///<鎻忚堪绗﹀悎闆 - Set renderable_sets; ///<鍙覆鏌撳璞″悎闆 + IDResManage rm_material; ///<鏉愯川鍚堥泦 + IDResManage rm_pipeline; ///<绠$嚎鍚堥泦 + IDResManage rm_desc_sets; ///<鎻忚堪绗﹀悎闆 + IDResManage rm_renderable; ///<鍙覆鏌撳璞″悎闆 + +public: + + MaterialID Add(Material *mtl) + { + if(!mtl)return(-1); + + rm_material.Add(mtl); + } + + PipelineID };//class SceneDatabase /** @@ -51,7 +68,9 @@ class RenderableInstance public: RenderableInstance(Pipeline *p,DescriptorSets *ds,Renderable *r):pipeline(p),desc_sets(ds),render_obj(r){} - virtual ~RenderableInstance()=default; + virtual ~RenderableInstance() + { + } Pipeline * GetPipeline (){return pipeline;} DescriptorSets *GetDescriptorSets (){return desc_sets;} diff --git a/inc/hgl/graph/vulkan/VKPipeline.h b/inc/hgl/graph/vulkan/VKPipeline.h index 2f5f44b5..ecee3502 100644 --- a/inc/hgl/graph/vulkan/VKPipeline.h +++ b/inc/hgl/graph/vulkan/VKPipeline.h @@ -4,6 +4,8 @@ #include #include VK_NAMESPACE_BEGIN +using PipelineID=uint64; + class Pipeline { VkDevice device; 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..a48351c8 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;} ///<璧勬簮閲婃斁铏氭嫙鍑芥暟(缂虹渷涓虹洿鎺elete瀵硅薄) public: @@ -45,11 +44,32 @@ 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(!ResManage::Add(id_count,value)) + return(-1); + + return id_count++; + } + };//template class IDResManage:public ResManage }//namespace hgl #include #endif//HGL_RES_MANAGE_INCLUDE From 557bd3d7342985adbe534d6b1ab58409e1c55065 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 24 May 2019 15:34:59 +0800 Subject: [PATCH 06/10] =?UTF-8?q?SeekOrigin=E7=94=B1enum=E6=94=B9=E4=B8=BA?= =?UTF-8?q?enum=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/io/DataInputStream.h | 2 +- inc/hgl/io/DataOutputStream.h | 2 +- inc/hgl/io/FileAccess.h | 2 +- inc/hgl/io/FileInputStream.h | 2 +- inc/hgl/io/FileOutputStream.h | 2 +- inc/hgl/io/InputStream.h | 2 +- inc/hgl/io/MemoryInputStream.h | 6 +++--- inc/hgl/io/MemoryOutputStream.h | 6 +++--- inc/hgl/io/OutputStream.h | 2 +- inc/hgl/io/RandomAccessFile.h | 2 +- inc/hgl/io/SeekAccess.h | 12 ++++++------ inc/hgl/type/ResManage.h | 1 + src/Base/IO/FileAccess.cpp | 6 +++--- src/Base/IO/FileInputStream.cpp | 2 +- src/Platform/Win/FileAccess.cpp | 4 ++-- 15 files changed, 27 insertions(+), 26 deletions(-) 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, ///<浠庤祫婧愬綋鍓嶄綅缃紑濮嬶紝绉诲埌璧勬簮鐨凱osition+offset浣嶇疆 - soEnd ///<浠庤祫婧愮殑缁撴潫浣嶇疆寮濮嬶紝offset蹇呴』灏忎簬0锛岃〃绀虹粨鏉熷墠鐨勫瓧绗︽暟 + Begin=0, ///<浠庤祫婧愭渶寮濮嬪寮濮嬶紝offset蹇呴』澶т簬0銆傜Щ鍒拌祫婧愮殑offset浣嶇疆 + Current, ///<浠庤祫婧愬綋鍓嶄綅缃紑濮嬶紝绉诲埌璧勬簮鐨凱osition+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.h b/inc/hgl/type/ResManage.h index a48351c8..a7aa827d 100644 --- a/inc/hgl/type/ResManage.h +++ b/inc/hgl/type/ResManage.h @@ -64,6 +64,7 @@ namespace hgl virtual F Add(T *value) { + if(!value)return(-1); if(!ResManage::Add(id_count,value)) return(-1); 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/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; From d97daabbc8616ef68f9eb7f423121f03d34cd00a Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 24 May 2019 15:36:34 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Android=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E7=9A=84=E8=B5=84=E4=BA=A7=E7=AE=A1=E7=90=86=E5=99=A8?= =?UTF-8?q?AssetManager=E4=BB=A5=E5=8F=8A=E6=96=87=E4=BB=B6=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/filesystem/AssetManage.h | 9 +++ src/Platform/Android/AssetManage.cpp | 109 +++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 inc/hgl/filesystem/AssetManage.h create mode 100644 src/Platform/Android/AssetManage.cpp 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/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 From c912377dc3a9df0873a7591b8d3f5b1e35c5ceba Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 24 May 2019 19:28:27 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0InlineGeometry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/Geometry2D.cpp | 196 ++++++------------------------ example/Vulkan/indices_rect.cpp | 2 +- example/Vulkan/main.cpp | 2 +- example/Vulkan/texture_rect.cpp | 2 +- inc/hgl/graph/InlineGeometry.h | 42 +++++++ src/Platform/CMakeLists.txt | 3 +- src/SceneGraph/CMakeLists.txt | 6 +- src/SceneGraph/InlineGeometry.cpp | 121 ++++++++++++++++++ 8 files changed, 206 insertions(+), 168 deletions(-) create mode 100644 inc/hgl/graph/InlineGeometry.h create mode 100644 src/SceneGraph/InlineGeometry.cpp diff --git a/example/Vulkan/Geometry2D.cpp b/example/Vulkan/Geometry2D.cpp index d37aa4b4..744300a8 100644 --- a/example/Vulkan/Geometry2D.cpp +++ b/example/Vulkan/Geometry2D.cpp @@ -4,9 +4,8 @@ // 浜屻佽瘯楠屽姩鎬佸悎骞舵潗璐ㄦ覆鏌撴満鍒躲佸寘鎷櫘閫氬悎骞朵笌Instance #include"VulkanAppFramework.h" -#include #include -#include +#include #include using namespace hgl; @@ -23,38 +22,29 @@ struct WorldConfig Matrix4f mvp; }world; -struct RectangleCreateInfo -{ - RectScope2f scope; -}; - 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) - { - if(!mtl)return(-1); - - rm_material.Add(mtl); - } - - PipelineID -};//class SceneDatabase +//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 /** * 鍙覆鏌撳璞″疄渚 @@ -84,17 +74,6 @@ public: // Adreno/NV/AMD: AlphaTest銆佷笉閫忔槑銆佸崐閫忔槑 // PowerVR: 鍚孉dreno/NV/AMD锛屼絾涓嶉忔槑閮ㄥ垎鍙互涓嶆帓搴 - if(pipeline->IsAlphaTest()) - { - if(!ri->pipeline->IsAlphaTest()) - return -1; - } - else - { - if(ri->pipeline->IsAlphaTest()) - return 1; - } - if(pipeline->IsAlphaBlend()) { if(!ri->pipeline->IsAlphaBlend()) @@ -106,6 +85,17 @@ public: 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; @@ -121,124 +111,6 @@ VK_NAMESPACE_END constexpr uint32_t VERTEX_COUNT=4; -vulkan::Renderable *CreateRectangle(vulkan::Device *device,vulkan::Material *mtl,const RectangleCreateInfo *rci) -{ - const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule(); - - vulkan::Renderable *render_obj=mtl->CreateRenderable(VERTEX_COUNT); - - const int vertex_binding=vsm->GetStageInputBinding("Vertex"); - - if(vertex_binding!=-1) - { - VB2f *vertex=new VB2f(VERTEX_COUNT); - - vertex->Begin(); - vertex->WriteRectFan(rci->scope); - vertex->End(); - - render_obj->Set(vertex_binding,device->CreateVBO(vertex)); - delete vertex; - } - else - { - delete render_obj; - return nullptr; - } - - return render_obj; -} - -struct RoundRectangleCreateInfo:public RectangleCreateInfo -{ - float radius; //鍦嗚鍗婂緞 - uint32_t round_per; //鍦嗚绮惧害 -}; - -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(1+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); - } - - vertex->Write(rci->scope.GetRight() -radius+coord[0].x, - rci->scope.GetTop() +radius-coord[0].y); - - delete[] coord; - - vertex->End(); - } - - render_obj=mtl->CreateRenderable(vertex->GetCount()); - render_obj->Set(vertex_binding,device->CreateVBO(vertex)); - - delete vertex; - - return render_obj; -} class TestApp:public VulkanApplicationFramework { diff --git a/example/Vulkan/indices_rect.cpp b/example/Vulkan/indices_rect.cpp index 7ec0fd0e..80789377 100644 --- a/example/Vulkan/indices_rect.cpp +++ b/example/Vulkan/indices_rect.cpp @@ -73,7 +73,7 @@ private: if(!material) return(false); - render_obj=material->CreateRenderable(); + render_obj=material->CreateRenderable(VERTEX_COUNT); descriptor_sets=material->CreateDescriptorSets(); return(true); } diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index f4659205..1c31f1f5 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -74,7 +74,7 @@ private: if(!material) return(false); - render_obj=material->CreateRenderable(); + render_obj=material->CreateRenderable(VERTEX_COUNT); descriptor_sets=material->CreateDescriptorSets(); return(true); } 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/graph/InlineGeometry.h b/inc/hgl/graph/InlineGeometry.h new file mode 100644 index 00000000..016a8ec1 --- /dev/null +++ b/inc/hgl/graph/InlineGeometry.h @@ -0,0 +1,42 @@ +锘#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; + uint field_count; //鍒嗘娆℃暟 + }; + }//namespace graph +};//namespace hgl +#endif//HGL_GRAPH_INLINE_GEOMETRY_INCLUDE 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/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 5b0413ee..47f83bbc 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 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..c1ba4524 --- /dev/null +++ b/src/SceneGraph/InlineGeometry.cpp @@ -0,0 +1,121 @@ +锘#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(); + + vulkan::Renderable *render_obj=mtl->CreateRenderable(4); + + const int vertex_binding=vsm->GetStageInputBinding("Vertex"); + + if(vertex_binding!=-1) + { + VB2f *vertex=new VB2f(4); + + vertex->Begin(); + vertex->WriteRectFan(rci->scope); + vertex->End(); + + render_obj->Set(vertex_binding,device->CreateVBO(vertex)); + delete vertex; + } + else + { + delete render_obj; + return nullptr; + } + + 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; + } + }//namespace graph +}//namespace hgl \ No newline at end of file From b5bed8c67701afd2cd307193ef1e8d0b8173bc57 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 24 May 2019 19:46:46 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9C=86=E5=BD=A2?= =?UTF-8?q?=E5=87=A0=E4=BD=95=E4=BD=93=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/Geometry2D.cpp | 25 ++++++++----- inc/hgl/graph/InlineGeometry.h | 5 ++- src/SceneGraph/InlineGeometry.cpp | 59 ++++++++++++++++++++++--------- 3 files changed, 64 insertions(+), 25 deletions(-) diff --git a/example/Vulkan/Geometry2D.cpp b/example/Vulkan/Geometry2D.cpp index 744300a8..273d76ef 100644 --- a/example/Vulkan/Geometry2D.cpp +++ b/example/Vulkan/Geometry2D.cpp @@ -109,9 +109,6 @@ public: };//class RenderableInstance VK_NAMESPACE_END -constexpr uint32_t VERTEX_COUNT=4; - - class TestApp:public VulkanApplicationFramework { private: @@ -167,13 +164,25 @@ private: //render_obj=CreateRectangle(device,material,&rci); - struct RoundRectangleCreateInfo rrci; + //struct RoundRectangleCreateInfo rrci; - rrci.scope.Set(10,10,SCREEN_WIDTH-20,SCREEN_HEIGHT-20); - rrci.radius=8; //鍗婂緞涓8 - rrci.round_per=8; //鍦嗚鍒囧垎鎴8娈 + //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); + //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; } diff --git a/inc/hgl/graph/InlineGeometry.h b/inc/hgl/graph/InlineGeometry.h index 016a8ec1..08f893f1 100644 --- a/inc/hgl/graph/InlineGeometry.h +++ b/inc/hgl/graph/InlineGeometry.h @@ -34,9 +34,12 @@ namespace hgl */ struct CircleCreateInfo { - Vector2f center; + 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/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index c1ba4524..faff68ce 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -12,27 +12,21 @@ namespace hgl { const vulkan::VertexShaderModule *vsm=mtl->GetVertexShaderModule(); - vulkan::Renderable *render_obj=mtl->CreateRenderable(4); - const int vertex_binding=vsm->GetStageInputBinding("Vertex"); - if(vertex_binding!=-1) - { - VB2f *vertex=new VB2f(4); + if(vertex_binding==-1) + return(nullptr); + + VB2f *vertex=new VB2f(4); - vertex->Begin(); - vertex->WriteRectFan(rci->scope); - vertex->End(); + vertex->Begin(); + vertex->WriteRectFan(rci->scope); + vertex->End(); - render_obj->Set(vertex_binding,device->CreateVBO(vertex)); - delete vertex; - } - else - { - delete render_obj; - return nullptr; - } + vulkan::Renderable *render_obj=mtl->CreateRenderable(vertex->GetCount()); + render_obj->Set(vertex_binding,device->CreateVBO(vertex)); + delete vertex; return render_obj; } @@ -114,7 +108,40 @@ namespace hgl 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 From 194adddb49a8f2a5bbb34a98120e7974cb285378 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 24 May 2019 21:43:59 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0RenderList=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/graph/SceneNode.h | 16 +++++++++------- inc/hgl/graph/VertexBuffer.h | 22 +++++++++++----------- inc/hgl/graph/vulkan/VK.h | 1 + src/SceneGraph/CMakeLists.txt | 2 +- src/SceneGraph/RenderList.cpp | 21 ++++++++++----------- 5 files changed, 32 insertions(+), 30 deletions(-) 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 a7defe09..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; @@ -401,11 +401,11 @@ namespace hgl /** * 涓夊厓鏁版嵁缂撳啿鍖 */ - template class VertexBuffer3:public VertexBuffer + template class VertexBuffer3:public VertexBufferBase { public: - using VertexBuffer::VertexBuffer; + using VertexBufferBase::VertexBufferBase; virtual ~VertexBuffer3()=default; VkFormat GetDataType()const override; @@ -641,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/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 47f83bbc..1ad55edd 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -9,7 +9,7 @@ SET(SCENE_GRAPH_HEADER ${ROOT_INCLUDE_PATH}/hgl/graph/AABox.h SET(SCENE_GRAPH_SOURCE AABox.cpp Camera.cpp -# RenderList.cpp + RenderList.cpp SceneNode.cpp SceneOrient.cpp InlineGeometry.cpp) 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