From 40b57bf7a7415d98e05e65833dbeb2e7cd77bb32 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 7 May 2019 03:18:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Linux=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E4=B8=8B=E7=BC=BA=E5=B0=91=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/indices_rect.cpp | 12 +- inc/hgl/platform/Exit.h | 10 + src/Platform/CMakeLists.txt | 3 +- src/Platform/UNIX/Exit.cpp | 24 ++ src/Platform/UNIX/ExternalModule.cpp | 2 +- src/Platform/UNIX/LogConsole.cpp | 126 +++++++++ src/Platform/UNIX/XCBVulkan.cpp | 26 ++ src/Platform/UNIX/XCBWindow.cpp | 240 ++++++++---------- src/Platform/UNIX/XCBWindow.h | 39 +++ .../{VkImageView.cpp => VKImageView.cpp} | 0 10 files changed, 337 insertions(+), 145 deletions(-) create mode 100644 inc/hgl/platform/Exit.h create mode 100644 src/Platform/UNIX/Exit.cpp create mode 100644 src/Platform/UNIX/LogConsole.cpp create mode 100644 src/Platform/UNIX/XCBVulkan.cpp create mode 100644 src/Platform/UNIX/XCBWindow.h rename src/RenderDevice/Vulkan/{VkImageView.cpp => VKImageView.cpp} (100%) diff --git a/example/Vulkan/indices_rect.cpp b/example/Vulkan/indices_rect.cpp index f8bf14e5..1c011f71 100644 --- a/example/Vulkan/indices_rect.cpp +++ b/example/Vulkan/indices_rect.cpp @@ -35,13 +35,13 @@ constexpr uint16 index_data[INDEX_COUNT]= class TestApp:public VulkanApplicationFramework { -private: +private: uint swap_chain_count=0; vulkan::Material * material =nullptr; vulkan::DescriptorSets * desciptor_sets =nullptr; - vulkan::Renderable * render_obj =nullptr; + vulkan::Renderable * render_obj =nullptr; vulkan::Buffer * ubo_mvp =nullptr; vulkan::Pipeline * pipeline =nullptr; @@ -89,11 +89,11 @@ private: if(!ubo_mvp) return(false); - return desciptor_sets->UpdateUBO(material->GetUBOBinding("world"),*ubo_mvp); + return desciptor_sets->UpdateUBO(material->GetUBO("world"),*ubo_mvp); } void InitVBO() - { + { vertex_buffer =device->CreateVBO(FMT_RG32F,VERTEX_COUNT,vertex_data); index_buffer =device->CreateIBO16(INDEX_COUNT,index_data); @@ -144,7 +144,7 @@ private: public: - bool Init() + bool Init() { if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) return(false); @@ -191,6 +191,6 @@ int main(int,char **) return(-1); while(app.Run()); - + return 0; } diff --git a/inc/hgl/platform/Exit.h b/inc/hgl/platform/Exit.h new file mode 100644 index 00000000..04f5494d --- /dev/null +++ b/inc/hgl/platform/Exit.h @@ -0,0 +1,10 @@ +#ifndef HGL_EXIT_INCLUDE +#define HGL_EXIT_INCLUDE + +namespace hgl +{ + typedef int (*SignalAppExitFunc)(); //程序退出处理事件函数 + + void SetSignalAppExit(SignalAppExitFunc); //设置程序退出处理事件函数 +}//namespace hgl +#endif//HGL_EXIT_INCLUDE diff --git a/src/Platform/CMakeLists.txt b/src/Platform/CMakeLists.txt index afff6afe..47c3f659 100644 --- a/src/Platform/CMakeLists.txt +++ b/src/Platform/CMakeLists.txt @@ -40,7 +40,8 @@ SET(PLATFORM_FILE_SOURCE ${PLATFORM_FILE_SOURCE} UNIX/ProgramPath.cpp) - SET(PLATFORM_WINDOW_SOURCE UNIX/XCBWindow.cpp) + SET(PLATFORM_WINDOW_SOURCE UNIX/XCBWindow.cpp + UNIX/XCBVulkan.cpp) ENDIF() ENDIF() diff --git a/src/Platform/UNIX/Exit.cpp b/src/Platform/UNIX/Exit.cpp new file mode 100644 index 00000000..bcf47719 --- /dev/null +++ b/src/Platform/UNIX/Exit.cpp @@ -0,0 +1,24 @@ +#include +#include + +namespace hgl +{ + static SignalAppExitFunc app_exit_func=nullptr; + + void exit_signal_proc(int n,siginfo_t *si,void *) + { + } + + void SetSignalAppExit(SignalAppExitFunc func) + { + if(!func)return; + + app_exit_func=func; + + struct sigaction act; + sigemptyset(&act.sa_mask); /** 清空阻塞信号 **/ + + act.sa_flags=SA_SIGINFO; /** 设置SA_SIGINFO 表示传递附加信息到触发函数 **/ + act.sa_sigaction=exit_signal_proc; + } +}//namespace hgl diff --git a/src/Platform/UNIX/ExternalModule.cpp b/src/Platform/UNIX/ExternalModule.cpp index 2120a103..bf1ccc4c 100644 --- a/src/Platform/UNIX/ExternalModule.cpp +++ b/src/Platform/UNIX/ExternalModule.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/src/Platform/UNIX/LogConsole.cpp b/src/Platform/UNIX/LogConsole.cpp new file mode 100644 index 00000000..ee6b2829 --- /dev/null +++ b/src/Platform/UNIX/LogConsole.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include + +#ifdef LOG_INFO_TIME +#include +#endif//LOG_INFO_TIME + +namespace hgl +{ + namespace logger + { + constexpr uint LOG_BUF_SIZE=4096; + + /** + * unix控制台日志插件接口 + */ + class LogUnixConsole:public Logger + { + char endline; + char log_buf[LOG_BUF_SIZE]; + +#ifdef LOGINFO_THREAD_MUTEX + ThreadMutex mutex; +#endif//LOGINFO_THREAD_MUTEX + + public: + + LogUnixConsole(LogLevel ll):Logger(ll) + { + endline='\n'; + } + + bool Create(const OSString &) + { + return(true); + } + + void Close(){} + +#ifdef LOG_INFO_THREAD + void WriteThreadID() + { + memcpy(log_buf,"[Thread:",8); + + htos(log_buf+8,128-9,pthread_self()); + strcat(log_buf,LOG_BUF_SIZE,']'); + + write(STDOUT_FILENO,log_buf,strlen(log_buf)); + } +#endif//LOG_INFO_THREAD + +#ifdef LOG_INFO_TIME + void WriteTime() + { + memcpy(log_buf,"[Time:",6); + + ftos(log_buf+6,128-strlen(log_buf),GetDoubleTime()); + strcat(log_buf,LOG_BUF_SIZE,']'); + + write(STDOUT_FILENO,log_buf,strlen(log_buf)); + } +#endif//LOG_INFO_TIME + + void Write(const u16char *str,int size) + { + #ifdef LOGINFO_THREAD_MUTEX + mutex.Lock(); + #endif//LOGINFO_THREAD_MUTEX + + #ifdef LOG_INFO_THREAD + WriteThreadID(); + #endif//LOG_INFO_THREAD + + #ifdef LOG_INFO_TIME + WriteTime(); + #endif//LOG_INFO_TIME + + int len; + + len=u16_to_u8(log_buf,LOG_BUF_SIZE,str,size); + + if(len>0) + { + log_buf[len++]='\n'; + + write(STDOUT_FILENO,log_buf,len); + } + #ifdef LOGINFO_THREAD_MUTEX + mutex.Unlock(); + #endif//LOGINFO_THREAD_MUTEX + } + + void Write(const char *str,int size) + { + #ifdef LOGINFO_THREAD_MUTEX + mutex.Lock(); + #endif//LOGINFO_THREAD_MUTEX + + #ifdef LOG_INFO_THREAD + WriteThreadID(); + #endif//LOG_INFO_THREAD + + #ifdef LOG_INFO_TIME + WriteTime(); + #endif//LOG_INFO_TIME + + write(STDOUT_FILENO,str,size); + write(STDOUT_FILENO,&endline,1); + #ifdef LOGINFO_THREAD_MUTEX + mutex.Unlock(); + #endif//LOGINFO_THREAD_MUTEX + } + };//class LogInterface + + Logger *CreateLoggerConsole(const OSString &,LogLevel ll) + { + if(ll +#include + +VK_NAMESPACE_BEGIN +VkSurfaceKHR CreateRenderDevice(VkInstance vk_inst,Window *win) +{ + XCBWindow *xcb_win=(XCBWindow *)win; + + VkXcbSurfaceCreateInfoKHR createInfo = {}; + createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; + createInfo.pNext = nullptr; + createInfo.connection = xcb_win->GetConnection(); + createInfo.window = xcb_win->GetWindow(); + + VkSurfaceKHR surface; + + VkResult res = vkCreateXcbSurfaceKHR(vk_inst, &createInfo, nullptr, &surface); + + if (res != VK_SUCCESS) + return(nullptr); + + return(surface); +} +VK_NAMESPACE_END + diff --git a/src/Platform/UNIX/XCBWindow.cpp b/src/Platform/UNIX/XCBWindow.cpp index 4905e46a..f1209b52 100644 --- a/src/Platform/UNIX/XCBWindow.cpp +++ b/src/Platform/UNIX/XCBWindow.cpp @@ -1,144 +1,110 @@ -#include"Window.h" -#include -#include -#include"VK.h" -#include -#include -#include +#include"XCBWindow.h" namespace hgl { - namespace graph + bool XCBWindow::InitConnection() { - class XCBWindow:public Window + int scr; + + connection=xcb_connect(nullptr,&scr); + + if(!connection||xcb_connection_has_error(connection)) + return(false); + + const xcb_setup_t *setup=xcb_get_setup(connection); + xcb_screen_iterator_t iter=xcb_setup_roots_iterator(setup); + + while(scr-->0)xcb_screen_next(&iter); + + screen=iter.data; + return(true); + } + + XCBWindow::XCBWindow(const UTF8String &wn):Window(wn) + { + connection=nullptr; + screen=nullptr; + atom_wm_delete_window=nullptr; + } + + XCBWindow::~XCBWindow() + { + } + + bool XCBWindow::Create(uint w,uint h) + { + if(w<=0||h<=0)return(false); + if(!InitConnection())return(false); + + window=xcb_generate_id(connection); + + uint32_t value_mask,value_list[32]; + + value_mask=XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + value_list[0] = screen->black_pixel; + value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE; + + width=w; + height=h; + + xcb_create_window(connection, XCB_COPY_FROM_PARENT, window, screen->root, 0,0, width, height, 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, value_mask, value_list); + + xcb_intern_atom_cookie_t cookie = xcb_intern_atom(connection, 1, 12, "WM_PROTOCOLS"); + xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection, cookie, 0); + + xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(connection, 0, 16, "WM_DELETE_WINDOW"); + atom_wm_delete_window = xcb_intern_atom_reply(connection, cookie2, 0); + + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, window, (*reply).atom, 4, 32, 1, + &(*atom_wm_delete_window).atom); + free(reply); + + xcb_change_property (connection, XCB_PROP_MODE_REPLACE, window,XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, + win_name.Length(), win_name.c_str()); + + xcb_map_window(connection, window); + + const uint32_t coords[] = { - xcb_connection_t *connection; - xcb_screen_t *screen; - xcb_window_t window; - xcb_intern_atom_reply_t *atom_wm_delete_window; + (screen->width_in_pixels-width)/2, + (screen->height_in_pixels-height)/2 + }; + xcb_configure_window(connection, window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); + xcb_flush(connection); - private: - - bool InitConnection() - { - int scr; - - connection=xcb_connect(nullptr,&scr); - - if(!connection||xcb_connection_has_error(connection)) - return(false); - - const xcb_setup_t *setup=xcb_get_setup(connection); - xcb_screen_iterator_t iter=xcb_setup_roots_iterator(setup); - - while(scr-->0)xcb_screen_next(&iter); - - screen=iter.data; - return(true); - } - - public: - - XCBWindow(const UTF8String &wn):Window(wn) - { - connection=nullptr; - screen=nullptr; - atom_wm_delete_window=nullptr; - } - - ~XCBWindow() - { - } - - bool Create(uint w,uint h) override - { - if(w<=0||h<=0)return(false); - if(!InitConnection())return(false); - - window=xcb_generate_id(connection); - - uint32_t value_mask,value_list[32]; - - value_mask=XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; - value_list[0] = screen->black_pixel; - value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE; - - width=w; - height=h; - - xcb_create_window(connection, XCB_COPY_FROM_PARENT, window, screen->root, 0,0, width, height, 0, - XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, value_mask, value_list); - - xcb_intern_atom_cookie_t cookie = xcb_intern_atom(connection, 1, 12, "WM_PROTOCOLS"); - xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection, cookie, 0); - - xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(connection, 0, 16, "WM_DELETE_WINDOW"); - atom_wm_delete_window = xcb_intern_atom_reply(connection, cookie2, 0); - - xcb_change_property(connection, XCB_PROP_MODE_REPLACE, window, (*reply).atom, 4, 32, 1, - &(*atom_wm_delete_window).atom); - free(reply); - - xcb_change_property (connection, XCB_PROP_MODE_REPLACE, window,XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, - win_name.Length(), win_name.c_str()); - - xcb_map_window(connection, window); - - const uint32_t coords[] = - { - (screen->width_in_pixels-width)/2, - (screen->height_in_pixels-height)/2 - }; - xcb_configure_window(connection, window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); - xcb_flush(connection); - - xcb_generic_event_t *e; - while ((e = xcb_wait_for_event(connection))) { - if ((e->response_type & ~0x80) == XCB_EXPOSE) break; - } - } - - bool Create(uint,uint,uint)override{} - void Close()override - { - xcb_destroy_window(connection, window); - xcb_disconnect(connection); - } - - void Show()override{} - void Hide()override{} - - public: - - xcb_connection_t *GetConnection(){return connection;} - xcb_window_t GetWindow(){return window;} - - };//class XCBWindow:public Window - - Window *CreateRenderWindow(const UTF8String &win_name) - { - return(new XCBWindow(win_name)); + xcb_generic_event_t *e; + while ((e = xcb_wait_for_event(connection))) { + if ((e->response_type & ~0x80) == XCB_EXPOSE) break; } - }//namespace graph + } + + void XCBWindow::Close() + { + xcb_destroy_window(connection,window); + xcb_disconnect(connection); + } + + void XCBWindow::SetCaption(const OSString &caption) + { + win_name=caption; + xcb_change_property (connection, XCB_PROP_MODE_REPLACE, window,XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, + win_name.Length(), win_name.c_str()); + } + + bool XCBWindow::MessageProc() + { + return(true); + } + + bool XCBWindow::WaitMessage() + { + return(true); + } + + Window *CreateRenderWindow(const UTF8String &win_name) + { + return(new XCBWindow(win_name)); + } + + void InitNativeWindowSystem(){} }//namespace hgl - -VK_NAMESPACE_BEGIN -VkSurfaceKHR CreateRenderDevice(VkInstance vk_inst,Window *win) -{ - XCBWindow *xcb_win=(XCBWindow *)win; - - VkXcbSurfaceCreateInfoKHR createInfo = {}; - createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; - createInfo.pNext = nullptr; - createInfo.connection = xcb_win->GetConnection(); - createInfo.window = xcb_win->GetWindow(); - - VkSurfaceKHR surface; - - VkResult res = vkCreateXcbSurfaceKHR(vk_inst, &createInfo, nullptr, &surface); - - if (res != VK_SUCCESS) - return(nullptr); - - return(surface); -} -VK_NAMESPACE_END diff --git a/src/Platform/UNIX/XCBWindow.h b/src/Platform/UNIX/XCBWindow.h new file mode 100644 index 00000000..7313642e --- /dev/null +++ b/src/Platform/UNIX/XCBWindow.h @@ -0,0 +1,39 @@ +#include +#include +#include +namespace hgl +{ + class XCBWindow:public Window + { + xcb_connection_t *connection; + xcb_screen_t *screen; + xcb_window_t window; + xcb_intern_atom_reply_t *atom_wm_delete_window; + + private: + + bool InitConnection(); + + public: + + XCBWindow(const UTF8String &wn); + ~XCBWindow(); + + bool Create(uint w,uint h)override; + bool Create(uint,uint,uint)override{} + void Close()override; + + void SetCaption(const OSString &caption) override; + + void Show()override{} + void Hide()override{} + bool MessageProc() override; + bool WaitMessage() override; + + public: + + xcb_connection_t *GetConnection(){return connection;} + xcb_window_t GetWindow(){return window;} + };//class XCBWindow:public Window +}//namespace hgl + diff --git a/src/RenderDevice/Vulkan/VkImageView.cpp b/src/RenderDevice/Vulkan/VKImageView.cpp similarity index 100% rename from src/RenderDevice/Vulkan/VkImageView.cpp rename to src/RenderDevice/Vulkan/VKImageView.cpp