diff options
Diffstat (limited to 'tools/platform')
| -rw-r--r-- | tools/platform/gui.cpp | 91 | ||||
| -rw-r--r-- | tools/platform/gui.h | 15 | ||||
| -rw-r--r-- | tools/platform/linux/x11-key-code.cpp | 380 | ||||
| -rw-r--r-- | tools/platform/linux/x11-window.cpp | 223 | ||||
| -rw-r--r-- | tools/platform/model.cpp | 231 | ||||
| -rw-r--r-- | tools/platform/model.h | 33 | ||||
| -rw-r--r-- | tools/platform/performance-counter.h | 5 | ||||
| -rw-r--r-- | tools/platform/placeholder/placeholder-window.cpp | 17 | ||||
| -rw-r--r-- | tools/platform/platform-api.h | 24 | ||||
| -rw-r--r-- | tools/platform/vector-math.h | 7 | ||||
| -rw-r--r-- | tools/platform/window.h | 75 | ||||
| -rw-r--r-- | tools/platform/windows/win-window.cpp | 42 |
12 files changed, 595 insertions, 548 deletions
diff --git a/tools/platform/gui.cpp b/tools/platform/gui.cpp index b1cb59517..15d683ec8 100644 --- a/tools/platform/gui.cpp +++ b/tools/platform/gui.cpp @@ -2,9 +2,10 @@ #include "gui.h" #ifdef _WIN32 -#include <windows.h> #include <examples/imgui_impl_win32.h> -IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); +#include <windows.h> +IMGUI_IMPL_API LRESULT +ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); #endif using namespace gfx; @@ -16,19 +17,22 @@ namespace platform LRESULT CALLBACK guiWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { LRESULT handled = ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam); - if(handled) return handled; + if (handled) + return handled; ImGuiIO& io = ImGui::GetIO(); - switch( msg ) + switch (msg) { case WM_LBUTTONDOWN: case WM_LBUTTONUP: - if(io.WantCaptureMouse) handled = 1; + if (io.WantCaptureMouse) + handled = 1; break; case WM_KEYDOWN: case WM_KEYUP: - if(io.WantCaptureKeyboard) handled = 1; + if (io.WantCaptureKeyboard) + handled = 1; break; } @@ -42,14 +46,13 @@ GUI::GUI( IDevice* inDevice, ICommandQueue* inQueue, IFramebufferLayout* framebufferLayout) - : device(inDevice) - , queue(inQueue) + : device(inDevice), queue(inQueue) { - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); #ifdef _WIN32 - ImGui_ImplWin32_Init((HWND)window->getNativeHandle().handleValues[0]); + ImGui_ImplWin32_Init((HWND)window->getNativeHandle().handleValues[0]); #endif // Let's do the initialization work required for our graphics API @@ -57,8 +60,7 @@ GUI::GUI( // through the same interface as other work. // - static const char* shaderCode = - "cbuffer U { float4x4 mvp; }; \ + static const char* shaderCode = "cbuffer U { float4x4 mvp; }; \ Texture2D t; \ SamplerState s; \ struct AssembledVertex { \ @@ -102,9 +104,9 @@ GUI::GUI( program = device->createProgram(programDesc); #endif InputElementDesc inputElements[] = { - {"U", 0, Format::R32G32_FLOAT, offsetof(ImDrawVert, pos) }, - {"U", 1, Format::R32G32_FLOAT, offsetof(ImDrawVert, uv) }, - {"U", 2, Format::R8G8B8A8_UNORM, offsetof(ImDrawVert, col) }, + {"U", 0, Format::R32G32_FLOAT, offsetof(ImDrawVert, pos)}, + {"U", 1, Format::R32G32_FLOAT, offsetof(ImDrawVert, uv)}, + {"U", 2, Format::R8G8B8A8_UNORM, offsetof(ImDrawVert, col)}, }; auto inputLayout = device->createInputLayout( sizeof(ImDrawVert), @@ -163,7 +165,7 @@ GUI::GUI( viewDesc.type = IResourceView::Type::ShaderResource; auto textureView = device->createTextureView(texture, viewDesc); - io.Fonts->TexID = (void*) textureView.detach(); + io.Fonts->TexID = (void*)textureView.detach(); } { @@ -187,7 +189,6 @@ GUI::GUI( } - void GUI::beginFrame() { #ifdef _WIN32 @@ -205,9 +206,12 @@ void GUI::endFrame(ITransientResourceHeap* transientHeap, IFramebuffer* framebuf auto indexCount = draw_data->TotalIdxCount; int commandListCount = draw_data->CmdListsCount; - if(!vertexCount) return; - if(!indexCount) return; - if(!commandListCount) return; + if (!vertexCount) + return; + if (!indexCount) + return; + if (!commandListCount) + return; // Allocate transient vertex/index buffers to hold the data for this frame. @@ -231,7 +235,7 @@ void GUI::endFrame(ITransientResourceHeap* transientHeap, IFramebuffer* framebuf auto cmdBuf = transientHeap->createCommandBuffer(); auto encoder = cmdBuf->encodeResourceCommands(); { - for(int ii = 0; ii < commandListCount; ++ii) + for (int ii = 0; ii < commandListCount; ++ii) { const ImDrawList* commandList = draw_data->CmdLists[ii]; encoder->uploadBufferData( @@ -262,12 +266,11 @@ void GUI::endFrame(ITransientResourceHeap* transientHeap, IFramebuffer* framebuf float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; float T = draw_data->DisplayPos.y; float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; - float mvp[4][4] = - { - { 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, - { 0.0f, 2.0f/(T-B), 0.0f, 0.0f }, - { 0.0f, 0.0f, 0.5f, 0.0f }, - { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, + float mvp[4][4] = { + {2.0f / (R - L), 0.0f, 0.0f, 0.0f}, + {0.0f, 2.0f / (T - B), 0.0f, 0.0f}, + {0.0f, 0.0f, 0.5f, 0.0f}, + {(R + L) / (L - R), (T + B) / (B - T), 0.5f, 1.0f}, }; encoder->uploadBufferData(constantBuffer, 0, sizeof(mvp), mvp); } @@ -290,37 +293,39 @@ void GUI::endFrame(ITransientResourceHeap* transientHeap, IFramebuffer* framebuf renderEncoder->setVertexBuffer(0, vertexBuffer); renderEncoder->setIndexBuffer( - indexBuffer, sizeof(ImDrawIdx) == 2 ? Format::R16_UINT : Format::R32_UINT); + indexBuffer, + sizeof(ImDrawIdx) == 2 ? Format::R16_UINT : Format::R32_UINT); renderEncoder->setPrimitiveTopology(PrimitiveTopology::TriangleList); uint32_t vertexOffset = 0; uint32_t indexOffset = 0; ImVec2 pos = draw_data->DisplayPos; - for(int ii = 0; ii < commandListCount; ++ii) + for (int ii = 0; ii < commandListCount; ++ii) { auto commandList = draw_data->CmdLists[ii]; auto commandCount = commandList->CmdBuffer.Size; - for(int jj = 0; jj < commandCount; jj++) + for (int jj = 0; jj < commandCount; jj++) { auto command = &commandList->CmdBuffer[jj]; - if(auto userCallback = command->UserCallback) + if (auto userCallback = command->UserCallback) { userCallback(commandList, command); } else { - ScissorRect rect = - { + ScissorRect rect = { (int32_t)(command->ClipRect.x - pos.x), (int32_t)(command->ClipRect.y - pos.y), (int32_t)(command->ClipRect.z - pos.x), - (int32_t)(command->ClipRect.w - pos.y) - }; + (int32_t)(command->ClipRect.w - pos.y)}; renderEncoder->setScissorRects(1, &rect); // TODO: set parameter into root shader object. - - renderEncoder->drawIndexed(command->ElemCount, (uint32_t)indexOffset, (uint32_t)vertexOffset); + + renderEncoder->drawIndexed( + command->ElemCount, + (uint32_t)indexOffset, + (uint32_t)vertexOffset); } indexOffset += command->ElemCount; } @@ -337,24 +342,24 @@ GUI::~GUI() { ComPtr<IResourceView> textureView; - textureView.attach((IResourceView*) io.Fonts->TexID); + textureView.attach((IResourceView*)io.Fonts->TexID); textureView = nullptr; } #ifdef _WIN32 - ImGui_ImplWin32_Shutdown(); + ImGui_ImplWin32_Shutdown(); #endif - ImGui::DestroyContext(); + ImGui::DestroyContext(); } -} // gfx +} // namespace platform #include <imgui.cpp> #include <imgui_draw.cpp> #include <imgui_widgets.cpp> #ifdef _WIN32 -// imgui_impl_win32 defines these, so make sure it doesn't error because + // imgui_impl_win32 defines these, so make sure it doesn't error because // they're already there #undef WIN32_LEAN_AND_MEAN #undef NOMINMAX diff --git a/tools/platform/gui.h b/tools/platform/gui.h index e3975f3ff..82193cb86 100644 --- a/tools/platform/gui.h +++ b/tools/platform/gui.h @@ -1,14 +1,15 @@ // gui.h #pragma once +#include "external/imgui/imgui.h" +#include "slang-com-ptr.h" #include "slang-gfx.h" +#include "source/core/slang-basic.h" #include "vector-math.h" #include "window.h" -#include "slang-com-ptr.h" -#include "external/imgui/imgui.h" -#include "source/core/slang-basic.h" -namespace platform { +namespace platform +{ struct GUI : Slang::RefObject { @@ -25,8 +26,8 @@ private: Slang::ComPtr<gfx::IDevice> device; Slang::ComPtr<gfx::ICommandQueue> queue; Slang::ComPtr<gfx::IRenderPassLayout> renderPass; - Slang::ComPtr<gfx::IPipelineState> pipelineState; - Slang::ComPtr<gfx::ISamplerState> samplerState; + Slang::ComPtr<gfx::IPipelineState> pipelineState; + Slang::ComPtr<gfx::ISamplerState> samplerState; }; -} // gfx +} // namespace platform diff --git a/tools/platform/linux/x11-key-code.cpp b/tools/platform/linux/x11-key-code.cpp index c078a6d1c..0faafae78 100644 --- a/tools/platform/linux/x11-key-code.cpp +++ b/tools/platform/linux/x11-key-code.cpp @@ -1,9 +1,10 @@ #if defined(SLANG_ENABLE_XLIB) -#include "core/slang-basic.h" #include "../window.h" -#include <X11/keysym.h> +#include "core/slang-basic.h" + #include <X11/Xlib.h> +#include <X11/keysym.h> #ifdef None #undef None @@ -15,233 +16,196 @@ using namespace Slang; namespace platform { - Dictionary<int, KeyCode> keyCodeMap; +Dictionary<int, KeyCode> keyCodeMap; - struct Win32KeyCode - { - KeyCode vKeyCode; - int keySym; - }; +struct Win32KeyCode +{ + KeyCode vKeyCode; + int keySym; +}; - Win32KeyCode keys[] = - { - {KeyCode::Left, XK_Left}, - {KeyCode::Up, XK_Up}, - {KeyCode::Down, XK_Down}, - {KeyCode::Right, XK_Right}, - {KeyCode::Escape, XK_Escape}, - {KeyCode::Return, XK_Return}, - {KeyCode::Space, XK_space}, - {KeyCode::Shift, XK_Shift_L}, - {KeyCode::Shift, XK_Shift_R}, - {KeyCode::Ctrl, XK_Control_L}, - {KeyCode::Ctrl, XK_Control_R}, - {KeyCode::Alt, XK_Alt_L}, - {KeyCode::Alt, XK_Alt_R}, - {KeyCode::Backspace, XK_BackSpace}, - {KeyCode::Delete, XK_Delete}, - {KeyCode::Home, XK_Home}, - {KeyCode::End, XK_End}, - {KeyCode::PageUp, XK_Page_Up}, - {KeyCode::PageDown, XK_Page_Down}, - {KeyCode::Insert, XK_Insert}, - {KeyCode::Tab, XK_Tab}, - {KeyCode::A, 0x41}, - {KeyCode::B, 0x42}, - {KeyCode::C, 0x43}, - {KeyCode::D, 0x44}, - {KeyCode::E, 0x45}, - {KeyCode::F, 0x46}, - {KeyCode::G, 0x47}, - {KeyCode::H, 0x48}, - {KeyCode::I, 0x49}, - {KeyCode::J, 0x4A}, - {KeyCode::K, 0x4B}, - {KeyCode::L, 0x4C}, - {KeyCode::M, 0x4D}, - {KeyCode::N, 0x4E}, - {KeyCode::O, 0x4F}, - {KeyCode::P, 0x50}, - {KeyCode::Q, 0x51}, - {KeyCode::R, 0x52}, - {KeyCode::S, 0x53}, - {KeyCode::T, 0x54}, - {KeyCode::U, 0x55}, - {KeyCode::V, 0x56}, - {KeyCode::W, 0x57}, - {KeyCode::X, 0x58}, - {KeyCode::Y, 0x59}, - {KeyCode::Z, 0x5A}, - {KeyCode::Semicolon, XK_semicolon}, - {KeyCode::Comma, XK_comma}, - {KeyCode::Dot, XK_period}, - {KeyCode::Slash, XK_slash}, - {KeyCode::Quote, XK_apostrophe}, - {KeyCode::LBracket, XK_bracketleft}, - {KeyCode::RBracket, XK_bracketright}, - {KeyCode::Backslash, XK_backslash}, - {KeyCode::Minus, XK_minus}, - {KeyCode::Plus, XK_equal}, - {KeyCode::Tilde, XK_asciitilde}, - {KeyCode::Key0, 0x30}, - {KeyCode::Key1, 0x31}, - {KeyCode::Key2, 0x32}, - {KeyCode::Key3, 0x33}, - {KeyCode::Key4, 0x34}, - {KeyCode::Key5, 0x35}, - {KeyCode::Key6, 0x36}, - {KeyCode::Key7, 0x37}, - {KeyCode::Key8, 0x38}, - {KeyCode::Key9, 0x39}, - {KeyCode::F1, XK_F1}, - {KeyCode::F2, XK_F2}, - {KeyCode::F3, XK_F3}, - {KeyCode::F4, XK_F4}, - {KeyCode::F5, XK_F5}, - {KeyCode::F6, XK_F6}, - {KeyCode::F7, XK_F7}, - {KeyCode::F8, XK_F8}, - {KeyCode::F9, XK_F9}, - {KeyCode::F10, XK_F10}, - {KeyCode::F11, XK_F11}, - {KeyCode::F12, XK_F12} - }; +Win32KeyCode keys[] = { + {KeyCode::Left, XK_Left}, + {KeyCode::Up, XK_Up}, + {KeyCode::Down, XK_Down}, + {KeyCode::Right, XK_Right}, + {KeyCode::Escape, XK_Escape}, + {KeyCode::Return, XK_Return}, + {KeyCode::Space, XK_space}, + {KeyCode::Shift, XK_Shift_L}, + {KeyCode::Shift, XK_Shift_R}, + {KeyCode::Ctrl, XK_Control_L}, + {KeyCode::Ctrl, XK_Control_R}, + {KeyCode::Alt, XK_Alt_L}, + {KeyCode::Alt, XK_Alt_R}, + {KeyCode::Backspace, XK_BackSpace}, + {KeyCode::Delete, XK_Delete}, + {KeyCode::Home, XK_Home}, + {KeyCode::End, XK_End}, + {KeyCode::PageUp, XK_Page_Up}, + {KeyCode::PageDown, XK_Page_Down}, + {KeyCode::Insert, XK_Insert}, + {KeyCode::Tab, XK_Tab}, + {KeyCode::A, 0x41}, + {KeyCode::B, 0x42}, + {KeyCode::C, 0x43}, + {KeyCode::D, 0x44}, + {KeyCode::E, 0x45}, + {KeyCode::F, 0x46}, + {KeyCode::G, 0x47}, + {KeyCode::H, 0x48}, + {KeyCode::I, 0x49}, + {KeyCode::J, 0x4A}, + {KeyCode::K, 0x4B}, + {KeyCode::L, 0x4C}, + {KeyCode::M, 0x4D}, + {KeyCode::N, 0x4E}, + {KeyCode::O, 0x4F}, + {KeyCode::P, 0x50}, + {KeyCode::Q, 0x51}, + {KeyCode::R, 0x52}, + {KeyCode::S, 0x53}, + {KeyCode::T, 0x54}, + {KeyCode::U, 0x55}, + {KeyCode::V, 0x56}, + {KeyCode::W, 0x57}, + {KeyCode::X, 0x58}, + {KeyCode::Y, 0x59}, + {KeyCode::Z, 0x5A}, + {KeyCode::Semicolon, XK_semicolon}, + {KeyCode::Comma, XK_comma}, + {KeyCode::Dot, XK_period}, + {KeyCode::Slash, XK_slash}, + {KeyCode::Quote, XK_apostrophe}, + {KeyCode::LBracket, XK_bracketleft}, + {KeyCode::RBracket, XK_bracketright}, + {KeyCode::Backslash, XK_backslash}, + {KeyCode::Minus, XK_minus}, + {KeyCode::Plus, XK_equal}, + {KeyCode::Tilde, XK_asciitilde}, + {KeyCode::Key0, 0x30}, + {KeyCode::Key1, 0x31}, + {KeyCode::Key2, 0x32}, + {KeyCode::Key3, 0x33}, + {KeyCode::Key4, 0x34}, + {KeyCode::Key5, 0x35}, + {KeyCode::Key6, 0x36}, + {KeyCode::Key7, 0x37}, + {KeyCode::Key8, 0x38}, + {KeyCode::Key9, 0x39}, + {KeyCode::F1, XK_F1}, + {KeyCode::F2, XK_F2}, + {KeyCode::F3, XK_F3}, + {KeyCode::F4, XK_F4}, + {KeyCode::F5, XK_F5}, + {KeyCode::F6, XK_F6}, + {KeyCode::F7, XK_F7}, + {KeyCode::F8, XK_F8}, + {KeyCode::F9, XK_F9}, + {KeyCode::F10, XK_F10}, + {KeyCode::F11, XK_F11}, + {KeyCode::F12, XK_F12}}; - void initKeyCodeTranslationTable(Display* display) +void initKeyCodeTranslationTable(Display* display) +{ + for (auto entry : keys) { - for (auto entry : keys) - { - auto systemKeyCode = XKeysymToKeycode(display, entry.keySym); - keyCodeMap[systemKeyCode] = entry.vKeyCode; - } + auto systemKeyCode = XKeysymToKeycode(display, entry.keySym); + keyCodeMap[systemKeyCode] = entry.vKeyCode; } +} - void freeKeyCodeTranslationTable() +void freeKeyCodeTranslationTable() +{ + keyCodeMap = decltype(keyCodeMap)(); +} + +KeyCode translateKeyCode(int keyCode) +{ + KeyCode result = KeyCode::None; + keyCodeMap.tryGetValue(keyCode, result); + return result; +} + +int getKeyChar(KeyCode keyCode, int keyState) +{ + bool shift = (keyState & ShiftMask) != 0; + if (keyCode >= KeyCode::A && keyCode <= KeyCode::Z) { - keyCodeMap = decltype(keyCodeMap)(); + bool capslock = (keyState & LockMask) != 0; + bool isCapital = capslock ^ shift; + if (isCapital) + return (int)keyCode; + else + return (int)keyCode + ('a' - 'A'); } - - KeyCode translateKeyCode(int keyCode) + else if (keyCode == KeyCode::Space) { - KeyCode result = KeyCode::None; - keyCodeMap.tryGetValue(keyCode, result); - return result; + return ' '; } - - int getKeyChar(KeyCode keyCode, int keyState) + else if (keyCode == KeyCode::Return) { - bool shift = (keyState & ShiftMask) != 0; - if (keyCode >= KeyCode::A && keyCode <= KeyCode::Z ) - { - bool capslock = (keyState & LockMask) != 0; - bool isCapital = capslock ^ shift; - if (isCapital) - return (int)keyCode; - else - return (int)keyCode + ('a'-'A'); - } - else if (keyCode == KeyCode::Space) - { - return ' '; - } - else if (keyCode == KeyCode::Return) - { + return (int)keyCode; + } + else if (keyCode >= KeyCode::Key0 && keyCode <= KeyCode::Key9) + { + if (!shift) return (int)keyCode; - } - else if (keyCode >= KeyCode::Key0 && keyCode <= KeyCode::Key9) + else { - if (!shift) - return (int)keyCode; - else + switch (keyCode) { - switch (keyCode) - { - case KeyCode::Key0: - return ')'; - case KeyCode::Key1: - return '!'; - case KeyCode::Key2: - return '@'; - case KeyCode::Key3: - return '#'; - case KeyCode::Key4: - return '$'; - case KeyCode::Key5: - return '%'; - case KeyCode::Key6: - return '^'; - case KeyCode::Key7: - return '&'; - case KeyCode::Key8: - return '*'; - case KeyCode::Key9: - return '('; - default: - return 0; - } + case KeyCode::Key0: return ')'; + case KeyCode::Key1: return '!'; + case KeyCode::Key2: return '@'; + case KeyCode::Key3: return '#'; + case KeyCode::Key4: return '$'; + case KeyCode::Key5: return '%'; + case KeyCode::Key6: return '^'; + case KeyCode::Key7: return '&'; + case KeyCode::Key8: return '*'; + case KeyCode::Key9: return '('; + default: return 0; } } - if (shift) + } + if (shift) + { + switch (keyCode) { - switch (keyCode) - { - case KeyCode::Semicolon: - return ':'; - case KeyCode::Comma: - return '<'; - case KeyCode::Dot: - return '>'; - case KeyCode::Slash: - return '?'; - case KeyCode::Quote: - return '\"'; - case KeyCode::LBracket: - return '{'; - case KeyCode::RBracket: - return '}'; - case KeyCode::Backslash: - return '|'; - case KeyCode::Minus: - return '_'; - case KeyCode::Plus: - return '+'; - case KeyCode::Tilde: - return '~'; - default: - return 0; - } + case KeyCode::Semicolon: return ':'; + case KeyCode::Comma: return '<'; + case KeyCode::Dot: return '>'; + case KeyCode::Slash: return '?'; + case KeyCode::Quote: return '\"'; + case KeyCode::LBracket: return '{'; + case KeyCode::RBracket: return '}'; + case KeyCode::Backslash: return '|'; + case KeyCode::Minus: return '_'; + case KeyCode::Plus: return '+'; + case KeyCode::Tilde: return '~'; + default: return 0; } - else + } + else + { + switch (keyCode) { - switch (keyCode) - { - case KeyCode::Semicolon: - return ';'; - case KeyCode::Comma: - return ','; - case KeyCode::Dot: - return '.'; - case KeyCode::Slash: - return '/'; - case KeyCode::Quote: - return '\''; - case KeyCode::LBracket: - return '['; - case KeyCode::RBracket: - return ']'; - case KeyCode::Backslash: - return '\\'; - case KeyCode::Minus: - return '-'; - case KeyCode::Plus: - return '='; - case KeyCode::Tilde: - return '`'; - default: - return 0; - } + case KeyCode::Semicolon: return ';'; + case KeyCode::Comma: return ','; + case KeyCode::Dot: return '.'; + case KeyCode::Slash: return '/'; + case KeyCode::Quote: return '\''; + case KeyCode::LBracket: return '['; + case KeyCode::RBracket: return ']'; + case KeyCode::Backslash: return '\\'; + case KeyCode::Minus: return '-'; + case KeyCode::Plus: return '='; + case KeyCode::Tilde: return '`'; + default: return 0; } } +} } // namespace platform #endif diff --git a/tools/platform/linux/x11-window.cpp b/tools/platform/linux/x11-window.cpp index be807ac33..155801049 100644 --- a/tools/platform/linux/x11-window.cpp +++ b/tools/platform/linux/x11-window.cpp @@ -1,9 +1,10 @@ #ifdef SLANG_ENABLE_XLIB #include "../window.h" + #include <X11/Xlib.h> -#include <X11/Xutil.h> #include <X11/Xresource.h> +#include <X11/Xutil.h> #ifdef None #undef None @@ -18,7 +19,7 @@ namespace platform typedef ::Window X11WindowHandle; class X11PlatformWindow; -void initKeyCodeTranslationTable(Display *display); +void initKeyCodeTranslationTable(Display* display); void freeKeyCodeTranslationTable(); KeyCode translateKeyCode(int keyCode); int getKeyChar(KeyCode keyCode, int keyState); @@ -34,12 +35,16 @@ enum class KeyState enum class KeyEvent { - Press, Release + Press, + Release }; enum class MouseEvent { - Move, Down, Up, Scroll + Move, + Down, + Up, + Scroll }; class X11AppContext @@ -51,7 +56,7 @@ public: static X11WindowHandle mainWindowHandle; static Display* xdisplay; static KeyState keyStates[kKeyStateTableSize]; - static X11PlatformWindow *currentMouseEventWindow; + static X11PlatformWindow* currentMouseEventWindow; }; bool X11AppContext::isTerminated = false; @@ -62,16 +67,19 @@ Display* X11AppContext::xdisplay = nullptr; KeyState X11AppContext::keyStates[kKeyStateTableSize] = {}; X11PlatformWindow* X11AppContext::currentMouseEventWindow = nullptr; -void Application::init() -{ - -} +void Application::init() {} static void doEventsImpl(bool waitForEvents); -void Application::doEvents() { doEventsImpl(false); } +void Application::doEvents() +{ + doEventsImpl(false); +} -void Application::quit() { X11AppContext::isTerminated = true; } +void Application::quit() +{ + X11AppContext::isTerminated = true; +} void Application::dispose() { @@ -85,7 +93,8 @@ void Application::run(Window* mainWindow, bool waitForEvents) if (mainWindow) { X11AppContext::mainWindow = mainWindow; - X11AppContext::mainWindowHandle = (X11WindowHandle)mainWindow->getNativeHandle().handleValues[1]; + X11AppContext::mainWindowHandle = + (X11WindowHandle)mainWindow->getNativeHandle().handleValues[1]; mainWindow->show(); while (!X11AppContext::isTerminated) { @@ -104,20 +113,33 @@ public: int currentWidth = 0; int currentHeight = 0; bool fixedSized = false; - X11PlatformWindow(const WindowDesc &desc) + X11PlatformWindow(const WindowDesc& desc) { currentWidth = desc.width; currentHeight = desc.height; - int blackColor = BlackPixel(X11AppContext::xdisplay, DefaultScreen(X11AppContext::xdisplay)); - int whiteColor = WhitePixel(X11AppContext::xdisplay, DefaultScreen(X11AppContext::xdisplay)); - handle = XCreateSimpleWindow(X11AppContext::xdisplay, DefaultRootWindow(X11AppContext::xdisplay), 0, 0, - desc.width, desc.height, 0, blackColor, blackColor); + int blackColor = + BlackPixel(X11AppContext::xdisplay, DefaultScreen(X11AppContext::xdisplay)); + int whiteColor = + WhitePixel(X11AppContext::xdisplay, DefaultScreen(X11AppContext::xdisplay)); + handle = XCreateSimpleWindow( + X11AppContext::xdisplay, + DefaultRootWindow(X11AppContext::xdisplay), + 0, + 0, + desc.width, + desc.height, + 0, + blackColor, + blackColor); X11AppContext::windows[handle] = this; Atom wmDelete = XInternAtom(X11AppContext::xdisplay, "WM_DELETE_WINDOW", True); XSetWMProtocols(X11AppContext::xdisplay, handle, &wmDelete, 1); - XSelectInput(X11AppContext::xdisplay, handle, StructureNotifyMask | KeyPressMask | KeyReleaseMask | PointerMotionMask | - ButtonPressMask | ButtonReleaseMask | ExposureMask | FocusChangeMask); + XSelectInput( + X11AppContext::xdisplay, + handle, + StructureNotifyMask | KeyPressMask | KeyReleaseMask | PointerMotionMask | + ButtonPressMask | ButtonReleaseMask | ExposureMask | FocusChangeMask); if (desc.style == WindowStyle::FixedSize) { @@ -127,10 +149,7 @@ public: setText(desc.title); } - ~X11PlatformWindow() - { - close(); - } + ~X11PlatformWindow() { close(); } void setFixedSizeHint(int w, int h) { @@ -158,12 +177,27 @@ public: X11WindowHandle winRoot = 0, winParent = 0; X11WindowHandle* winChildren = nullptr; unsigned int numChilren = 0; - XQueryTree(X11AppContext::xdisplay, handle, &winRoot, &winParent, &winChildren, &numChilren); + XQueryTree( + X11AppContext::xdisplay, + handle, + &winRoot, + &winParent, + &winChildren, + &numChilren); unsigned borderWidth, depth; - XGetGeometry(X11AppContext::xdisplay, handle, &winRoot, &rect.x, &rect.y, (uint32_t*)&rect.width, (uint32_t*)&rect.height, &borderWidth, &depth); + XGetGeometry( + X11AppContext::xdisplay, + handle, + &winRoot, + &rect.x, + &rect.y, + (uint32_t*)&rect.width, + (uint32_t*)&rect.height, + &borderWidth, + &depth); return rect; } - + virtual void centerScreen() override { auto currentRect = getClientRect(); @@ -186,7 +220,8 @@ public: } virtual bool getFocused() override { - if (!handle) return false; + if (!handle) + return false; int revertTo; X11WindowHandle focusedWindow; XGetInputFocus(X11AppContext::xdisplay, &focusedWindow, &revertTo); @@ -202,18 +237,16 @@ public: } virtual void setText(String text) override { - if (!handle) return; + if (!handle) + return; XStoreName(X11AppContext::xdisplay, handle, text.getBuffer()); XClassHint* hint = XAllocClassHint(); - hint->res_class = (char *)"Slang platform window"; - hint->res_name = (char *)"Slang platform window"; + hint->res_class = (char*)"Slang platform window"; + hint->res_name = (char*)"Slang platform window"; XSetClassHint(X11AppContext::xdisplay, handle, hint); XFree(hint); } - virtual bool getVisible() override - { - return visible; - } + virtual bool getVisible() override { return visible; } virtual void show() override { XMapWindow(X11AppContext::xdisplay, handle); @@ -221,16 +254,17 @@ public: } virtual void hide() override { - if (!handle) return; + if (!handle) + return; XUnmapWindow(X11AppContext::xdisplay, handle); visible = false; } virtual int getCurrentDpi() override { - char *resourceString = XResourceManagerString(X11AppContext::xdisplay); + char* resourceString = XResourceManagerString(X11AppContext::xdisplay); XrmDatabase db; XrmValue value; - char *type = NULL; + char* type = NULL; double dpi = 96.0; db = XrmGetStringDatabase(resourceString); if (resourceString) @@ -263,12 +297,18 @@ public: ButtonState::Enum getButtonState(int state) { ButtonState::Enum buttonState = ButtonState::Enum::None; - if (state & ShiftMask) addButtonState(buttonState, ButtonState::Enum::Shift); - if (state & ControlMask) addButtonState(buttonState, ButtonState::Enum::Control); - if (state & Mod1Mask) addButtonState(buttonState, ButtonState::Enum::Alt); - if (state & Button1Mask) addButtonState(buttonState, ButtonState::Enum::LeftButton); - if (state & Button2Mask) addButtonState(buttonState, ButtonState::Enum::MiddleButton); - if (state & Button3Mask) addButtonState(buttonState, ButtonState::Enum::RightButton); + if (state & ShiftMask) + addButtonState(buttonState, ButtonState::Enum::Shift); + if (state & ControlMask) + addButtonState(buttonState, ButtonState::Enum::Control); + if (state & Mod1Mask) + addButtonState(buttonState, ButtonState::Enum::Alt); + if (state & Button1Mask) + addButtonState(buttonState, ButtonState::Enum::LeftButton); + if (state & Button2Mask) + addButtonState(buttonState, ButtonState::Enum::MiddleButton); + if (state & Button3Mask) + addButtonState(buttonState, ButtonState::Enum::RightButton); return buttonState; } @@ -291,7 +331,14 @@ public: } } - void handleMouseEvent(MouseEvent eventType, int x, int y, int delta, int button, int state, unsigned long time) + void handleMouseEvent( + MouseEvent eventType, + int x, + int y, + int delta, + int button, + int state, + unsigned long time) { auto buttonState = getButtonState(state); if (button == Button1) @@ -308,20 +355,11 @@ public: switch (eventType) { - case MouseEvent::Down: - events.mouseDown(e); - break; - case MouseEvent::Up: - events.mouseUp(e); - break; - case MouseEvent::Move: - events.mouseMove(e); - break; - case MouseEvent::Scroll: - events.mouseWheel(e); - break; - default: - break; + case MouseEvent::Down: events.mouseDown(e); break; + case MouseEvent::Up: events.mouseUp(e); break; + case MouseEvent::Move: events.mouseMove(e); break; + case MouseEvent::Scroll: events.mouseWheel(e); break; + default: break; } } @@ -332,14 +370,9 @@ public: Application::quit(); } - void handleExposeEvent() - { - } - - void handleFocus(bool focus) - { - } + void handleExposeEvent() {} + void handleFocus(bool focus) {} }; Window* Application::createWindow(const WindowDesc& desc) @@ -361,7 +394,7 @@ void doEventsImpl(bool waitForEvents) auto xdisplay = X11AppContext::xdisplay; if (!X11AppContext::xdisplay) return; - + static bool supressInvokeTasks = false; X11PlatformWindow* sysWindow = nullptr; KeyCode vKeyCode = KeyCode::None; @@ -379,7 +412,7 @@ void doEventsImpl(bool waitForEvents) { if (X11AppContext::keyStates[iKeyCode] == KeyState::Released) X11AppContext::keyStates[iKeyCode] = KeyState::Pressed; - else if (X11AppContext::keyStates[iKeyCode] == KeyState::Pressed) + else if (X11AppContext::keyStates[iKeyCode] == KeyState::Pressed) X11AppContext::keyStates[iKeyCode] = KeyState::Hold; } if (X11AppContext::windows.tryGetValue(nextEvent.xkey.window, sysWindow)) @@ -404,8 +437,14 @@ void doEventsImpl(bool waitForEvents) if (X11AppContext::windows.tryGetValue(nextEvent.xmotion.window, sysWindow)) { X11AppContext::currentMouseEventWindow = sysWindow; - sysWindow->handleMouseEvent(MouseEvent::Move, nextEvent.xmotion.x, nextEvent.xmotion.y, 0, - 0, nextEvent.xmotion.state, nextEvent.xmotion.time); + sysWindow->handleMouseEvent( + MouseEvent::Move, + nextEvent.xmotion.x, + nextEvent.xmotion.y, + 0, + 0, + nextEvent.xmotion.state, + nextEvent.xmotion.time); } break; case ButtonPress: @@ -413,28 +452,54 @@ void doEventsImpl(bool waitForEvents) { X11AppContext::currentMouseEventWindow = sysWindow; if (nextEvent.xbutton.button <= Button3) - sysWindow->handleMouseEvent(MouseEvent::Down, nextEvent.xbutton.x, nextEvent.xbutton.y, 0, - nextEvent.xbutton.button, nextEvent.xbutton.state, nextEvent.xbutton.time); + sysWindow->handleMouseEvent( + MouseEvent::Down, + nextEvent.xbutton.x, + nextEvent.xbutton.y, + 0, + nextEvent.xbutton.button, + nextEvent.xbutton.state, + nextEvent.xbutton.time); else if (nextEvent.xbutton.button == Button4) - sysWindow->handleMouseEvent(MouseEvent::Scroll, nextEvent.xbutton.x, nextEvent.xbutton.y, 120, - nextEvent.xbutton.button, nextEvent.xbutton.state, nextEvent.xbutton.time); + sysWindow->handleMouseEvent( + MouseEvent::Scroll, + nextEvent.xbutton.x, + nextEvent.xbutton.y, + 120, + nextEvent.xbutton.button, + nextEvent.xbutton.state, + nextEvent.xbutton.time); else if (nextEvent.xbutton.button == Button5) - sysWindow->handleMouseEvent(MouseEvent::Scroll, nextEvent.xbutton.x, nextEvent.xbutton.y, -120, - nextEvent.xbutton.button, nextEvent.xbutton.state, nextEvent.xbutton.time); + sysWindow->handleMouseEvent( + MouseEvent::Scroll, + nextEvent.xbutton.x, + nextEvent.xbutton.y, + -120, + nextEvent.xbutton.button, + nextEvent.xbutton.state, + nextEvent.xbutton.time); } break; case ButtonRelease: if (X11AppContext::windows.tryGetValue(nextEvent.xbutton.window, sysWindow)) { X11AppContext::currentMouseEventWindow = sysWindow; - sysWindow->handleMouseEvent(MouseEvent::Up, nextEvent.xbutton.x, nextEvent.xbutton.y, 0, - nextEvent.xbutton.button, nextEvent.xbutton.state, nextEvent.xbutton.time); + sysWindow->handleMouseEvent( + MouseEvent::Up, + nextEvent.xbutton.x, + nextEvent.xbutton.y, + 0, + nextEvent.xbutton.button, + nextEvent.xbutton.state, + nextEvent.xbutton.time); } break; case ConfigureNotify: if (X11AppContext::windows.tryGetValue(nextEvent.xconfigure.window, sysWindow)) { - sysWindow->handleResizeEvent(nextEvent.xconfigure.width, nextEvent.xconfigure.height); + sysWindow->handleResizeEvent( + nextEvent.xconfigure.width, + nextEvent.xconfigure.height); } break; case Expose: @@ -469,6 +534,6 @@ void doEventsImpl(bool waitForEvents) } } -} +} // namespace platform #endif diff --git a/tools/platform/model.cpp b/tools/platform/model.cpp index a48d499b9..3649db9dc 100644 --- a/tools/platform/model.cpp +++ b/tools/platform/model.cpp @@ -10,17 +10,17 @@ #include "../../external/stb/stb_image.h" #define STB_IMAGE_RESIZE_IMPLEMENTATION -#include "../../external/stb/stb_image_resize.h" - #include "../../external/glm/glm/glm.hpp" -#include "../../external/glm/glm/gtc/matrix_transform.hpp" #include "../../external/glm/glm/gtc/constants.hpp" +#include "../../external/glm/glm/gtc/matrix_transform.hpp" +#include "../../external/stb/stb_image_resize.h" #include <memory> #include <unordered_map> #include <unordered_set> -namespace platform { +namespace platform +{ using namespace gfx; using namespace Slang; @@ -43,9 +43,9 @@ struct ObjIndexKey bool operator==(ObjIndexKey const& left, ObjIndexKey const& right) { - return left.index.vertex_index == right.index.vertex_index - && left.index.normal_index == right.index.normal_index - && left.index.texcoord_index == right.index.texcoord_index; + return left.index.vertex_index == right.index.vertex_index && + left.index.normal_index == right.index.normal_index && + left.index.texcoord_index == right.index.texcoord_index; } struct Hasher @@ -65,70 +65,65 @@ struct SmoothingGroupVertexID }; bool operator==(SmoothingGroupVertexID const& left, SmoothingGroupVertexID const& right) { - return left.smoothingGroup == right.smoothingGroup - && left.positionID == right.positionID; + return left.smoothingGroup == right.smoothingGroup && left.positionID == right.positionID; } -} +} // namespace platform namespace std { - template<> struct hash<platform::ObjIndexKey> +template<> +struct hash<platform::ObjIndexKey> +{ + size_t operator()(platform::ObjIndexKey const& key) const { - size_t operator()(platform::ObjIndexKey const& key) const - { - platform::Hasher hasher; - hasher.add(key.index.vertex_index); - hasher.add(key.index.normal_index); - hasher.add(key.index.texcoord_index); - return hasher.state; - } - }; + platform::Hasher hasher; + hasher.add(key.index.vertex_index); + hasher.add(key.index.normal_index); + hasher.add(key.index.texcoord_index); + return hasher.state; + } +}; - template <> struct hash<platform::SmoothingGroupVertexID> +template<> +struct hash<platform::SmoothingGroupVertexID> +{ + size_t operator()(platform::SmoothingGroupVertexID const& id) const { - size_t operator()(platform::SmoothingGroupVertexID const& id) const - { - platform::Hasher hasher; - hasher.add(id.smoothingGroup); - hasher.add(id.positionID); - return hasher.state; - } - }; -} + platform::Hasher hasher; + hasher.add(id.smoothingGroup); + hasher.add(id.positionID); + return hasher.state; + } +}; +} // namespace std namespace platform { -ComPtr<ITextureResource> loadTextureImage( - IDevice* device, - char const* path) +ComPtr<ITextureResource> loadTextureImage(IDevice* device, char const* path) { int extentX = 0; int extentY = 0; int originalChannelCount = 0; int requestedChannelCount = 4; // force to 4-component result - stbi_uc* data = stbi_load( - path, - &extentX, - &extentY, - &originalChannelCount, - requestedChannelCount); - if(!data) + stbi_uc* data = + stbi_load(path, &extentX, &extentY, &originalChannelCount, requestedChannelCount); + if (!data) return nullptr; int channelCount = requestedChannelCount ? requestedChannelCount : originalChannelCount; Format format; - switch(channelCount) + switch (channelCount) { - default: - return nullptr; + default: return nullptr; - case 4: format = Format::R8G8B8A8_UNORM; + case 4: + format = Format::R8G8B8A8_UNORM; - // TODO: handle other cases here if/when we stop forcing 4-component - // results when loading the image with stb_image. + // TODO: handle other cases here if/when we stop forcing 4-component + // results when loading the image with stb_image. } std::vector<ITextureResource::SubresourceData> subresourceInitData; @@ -144,30 +139,39 @@ ComPtr<ITextureResource> loadTextureImage( // create down-sampled images for the different mip levels bool generateMips = true; - if(generateMips) + if (generateMips) { int prevExtentX = extentX; int prevExtentY = extentY; stbi_uc* prevData = data; int prevStride = int(stride); - for(;;) + for (;;) { - if(prevExtentX == 1 && prevExtentY == 1) + if (prevExtentX == 1 && prevExtentY == 1) break; int newExtentX = prevExtentX / 2; int newExtentY = prevExtentY / 2; - if(!newExtentX) newExtentX = 1; - if(!newExtentY) newExtentY = 1; + if (!newExtentX) + newExtentX = 1; + if (!newExtentY) + newExtentY = 1; - stbi_uc* newData = (stbi_uc*) malloc(newExtentX * newExtentY * channelCount * sizeof(stbi_uc)); + stbi_uc* newData = + (stbi_uc*)malloc(newExtentX * newExtentY * channelCount * sizeof(stbi_uc)); int newStride = int(newExtentX * channelCount * sizeof(stbi_uc)); stbir_resize_uint8_srgb( - prevData, prevExtentX, prevExtentY, prevStride, - newData, newExtentX, newExtentY, newStride, + prevData, + prevExtentX, + prevExtentY, + prevStride, + newData, + newExtentX, + newExtentY, + newStride, channelCount, STBIR_ALPHA_CHANNEL_NONE, STBIR_FLAG_ALPHA_PREMULTIPLIED); @@ -187,7 +191,7 @@ ComPtr<ITextureResource> loadTextureImage( } } - int mipCount = (int) subresourceInitData.size(); + int mipCount = (int)subresourceInitData.size(); ITextureResource::Desc desc = {}; desc.type = IResource::Type::Texture2D; @@ -209,9 +213,7 @@ static std::string makeString(const char* start, const char* end) return std::string(start, size_t(end - start)); } -SlangResult ModelLoader::load( - char const* inputPath, - void** outModel) +SlangResult ModelLoader::load(char const* inputPath, void** outModel) { // TODO: need to actually allocate/load the data @@ -220,7 +222,7 @@ SlangResult ModelLoader::load( std::vector<tinyobj::material_t> objMaterials; std::string baseDir; - if( auto lastSlash = strrchr(inputPath, '/') ) + if (auto lastSlash = strrchr(inputPath, '/')) { baseDir = makeString(inputPath, lastSlash); } @@ -236,11 +238,11 @@ SlangResult ModelLoader::load( baseDir.size() ? baseDir.c_str() : nullptr, shouldTriangulate); - if(!diagnostics.empty()) + if (!diagnostics.empty()) { printf("%s", diagnostics.c_str()); } - if(!success) + if (!success) { return SLANG_FAIL; } @@ -249,28 +251,22 @@ SlangResult ModelLoader::load( // we can actually use for rendering. // std::vector<void*> materials; - for(auto& objMaterial : objMaterials) + for (auto& objMaterial : objMaterials) { MaterialData materialData; - materialData.diffuseColor = glm::vec3( - objMaterial.diffuse[0], - objMaterial.diffuse[1], - objMaterial.diffuse[2]); + materialData.diffuseColor = + glm::vec3(objMaterial.diffuse[0], objMaterial.diffuse[1], objMaterial.diffuse[2]); - materialData.specularColor = glm::vec3( - objMaterial.specular[0], - objMaterial.specular[1], - objMaterial.specular[2]); + materialData.specularColor = + glm::vec3(objMaterial.specular[0], objMaterial.specular[1], objMaterial.specular[2]); materialData.specularity = objMaterial.shininess; // load any referenced textures here - if(objMaterial.diffuse_texname.length()) + if (objMaterial.diffuse_texname.length()) { - materialData.diffuseMap = loadTextureImage( - device, - objMaterial.diffuse_texname.c_str()); + materialData.diffuseMap = loadTextureImage(device, objMaterial.diffuse_texname.c_str()); } auto material = callbacks->createMaterial(materialData); @@ -279,20 +275,20 @@ SlangResult ModelLoader::load( // Flip the winding order on all faces if we are asked to... // - if(loadFlags & LoadFlag::FlipWinding) + if (loadFlags & LoadFlag::FlipWinding) { - for(auto& objShape : objShapes) + for (auto& objShape : objShapes) { size_t objIndexCounter = 0; size_t objFaceCounter = 0; - for(auto objFaceVertexCount : objShape.mesh.num_face_vertices) + for (auto objFaceVertexCount : objShape.mesh.num_face_vertices) { size_t beginIndex = objIndexCounter; size_t endIndex = beginIndex + objFaceVertexCount; objIndexCounter = endIndex; size_t halfCount = objFaceVertexCount / 2; - for(size_t ii = 0; ii < halfCount; ++ii) + for (size_t ii = 0; ii < halfCount; ++ii) { std::swap( objShape.mesh.indices[beginIndex + ii], @@ -300,7 +296,6 @@ SlangResult ModelLoader::load( } } } - } // Identify cases where a face has a vertex without a normal, and in that @@ -311,31 +306,31 @@ SlangResult ModelLoader::load( std::unordered_map<SmoothingGroupVertexID, size_t> smoothedVertexNormals; size_t firstSmoothedNormalID = objVertexAttributes.normals.size() / 3; size_t flatFaceCounter = 0; - for(auto& objShape : objShapes) + for (auto& objShape : objShapes) { size_t objIndexCounter = 0; size_t objFaceCounter = 0; - for(auto objFaceVertexCount : objShape.mesh.num_face_vertices) + for (auto objFaceVertexCount : objShape.mesh.num_face_vertices) { const size_t flatFaceIndex = flatFaceCounter++; const size_t objFaceIndex = objFaceCounter++; size_t smoothingGroup = objShape.mesh.smoothing_group_ids[objFaceIndex]; - if(!smoothingGroup) + if (!smoothingGroup) { smoothingGroup = ~flatFaceIndex; } - for(size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) + for (size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) { tinyobj::index_t& objIndex = objShape.mesh.indices[objIndexCounter++]; - if(objIndex.normal_index < 0) + if (objIndex.normal_index < 0) { SmoothingGroupVertexID smoothVertexID; smoothVertexID.positionID = objIndex.vertex_index; smoothVertexID.smoothingGroup = smoothingGroup; - if(smoothedVertexNormals.find(smoothVertexID) == smoothedVertexNormals.end()) + if (smoothedVertexNormals.find(smoothVertexID) == smoothedVertexNormals.end()) { size_t normalID = objVertexAttributes.normals.size() / 3; objVertexAttributes.normals.push_back(0); @@ -356,28 +351,29 @@ SlangResult ModelLoader::load( // to the same smoothing group. // flatFaceCounter = 0; - for(auto& objShape : objShapes) + for (auto& objShape : objShapes) { size_t objIndexCounter = 0; size_t objFaceCounter = 0; - for(auto objFaceVertexCount : objShape.mesh.num_face_vertices) + for (auto objFaceVertexCount : objShape.mesh.num_face_vertices) { const size_t flatFaceIndex = flatFaceCounter++; const size_t objFaceIndex = objFaceCounter++; size_t smoothingGroup = objShape.mesh.smoothing_group_ids[objFaceIndex]; - if(!smoothingGroup) + if (!smoothingGroup) { smoothingGroup = ~flatFaceIndex; } glm::vec3 faceNormal; - if(objFaceVertexCount >= 3) + if (objFaceVertexCount >= 3) { glm::vec3 v[3]; - for(size_t objFaceVertex = 0; objFaceVertex < 3; ++objFaceVertex) + for (size_t objFaceVertex = 0; objFaceVertex < 3; ++objFaceVertex) { - tinyobj::index_t objIndex = objShape.mesh.indices[objIndexCounter + objFaceVertex]; - if(objIndex.vertex_index >= 0) + tinyobj::index_t objIndex = + objShape.mesh.indices[objIndexCounter + objFaceVertex]; + if (objIndex.vertex_index >= 0) { v[objFaceVertex] = glm::vec3( objVertexAttributes.vertices[3 * objIndex.vertex_index + 0], @@ -389,7 +385,7 @@ SlangResult ModelLoader::load( } // Add this face normal to any to-be-smoothed vertex on the face. - for(size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) + for (size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) { tinyobj::index_t objIndex = objShape.mesh.indices[objIndexCounter++]; @@ -398,7 +394,7 @@ SlangResult ModelLoader::load( smoothVertexID.smoothingGroup = smoothingGroup; auto ii = smoothedVertexNormals.find(smoothVertexID); - if(ii != smoothedVertexNormals.end()) + if (ii != smoothedVertexNormals.end()) { size_t normalID = ii->second; objVertexAttributes.normals[normalID * 3 + 0] += faceNormal.x; @@ -413,7 +409,7 @@ SlangResult ModelLoader::load( // we can normalize the normals to compute the area-weighted average. // size_t normalCount = objVertexAttributes.normals.size() / 3; - for(size_t ii = firstSmoothedNormalID; ii < normalCount; ++ii) + for (size_t ii = firstSmoothedNormalID; ii < normalCount; ++ii) { glm::vec3 normal = glm::vec3( objVertexAttributes.normals[3 * ii + 0], @@ -445,18 +441,18 @@ SlangResult ModelLoader::load( void* defaultMaterial = nullptr; - for(auto& objShape : objShapes) + for (auto& objShape : objShapes) { size_t objIndexCounter = 0; size_t objFaceCounter = 0; - for(auto objFaceVertexCount : objShape.mesh.num_face_vertices) + for (auto objFaceVertexCount : objShape.mesh.num_face_vertices) { size_t objFaceIndex = objFaceCounter++; int faceMaterialID = objShape.mesh.material_ids[objFaceIndex]; void* faceMaterial = nullptr; - if( faceMaterialID < 0 ) + if (faceMaterialID < 0) { - if( !defaultMaterial ) + if (!defaultMaterial) { MaterialData defaultMaterialData; defaultMaterialData.diffuseColor = glm::vec3(0.5, 0.5, 0.5); @@ -469,10 +465,10 @@ SlangResult ModelLoader::load( faceMaterial = materials[faceMaterialID]; } - if(!currentMesh || (faceMaterial != currentMesh->material)) + if (!currentMesh || (faceMaterial != currentMesh->material)) { // finish old mesh. - if(currentMesh) + if (currentMesh) { meshes.push_back(callbacks->createMesh(*currentMesh)); } @@ -484,36 +480,39 @@ SlangResult ModelLoader::load( currentMesh->indexCount = 0; } - for(size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) + for (size_t objFaceVertex = 0; objFaceVertex < objFaceVertexCount; ++objFaceVertex) { tinyobj::index_t objIndex = objShape.mesh.indices[objIndexCounter++]; - ObjIndexKey objIndexKey; objIndexKey.index = objIndex; + ObjIndexKey objIndexKey; + objIndexKey.index = objIndex; Index flatIndex = Index(-1); auto iter = mapObjIndexToFlatIndex.find(objIndexKey); - if(iter != mapObjIndexToFlatIndex.end()) + if (iter != mapObjIndexToFlatIndex.end()) { flatIndex = iter->second; } else { Vertex flatVertex; - if(objIndex.vertex_index >= 0) + if (objIndex.vertex_index >= 0) { - flatVertex.position = scale * glm::vec3( - objVertexAttributes.vertices[3 * objIndex.vertex_index + 0], - objVertexAttributes.vertices[3 * objIndex.vertex_index + 1], - objVertexAttributes.vertices[3 * objIndex.vertex_index + 2]); + flatVertex.position = + scale * + glm::vec3( + objVertexAttributes.vertices[3 * objIndex.vertex_index + 0], + objVertexAttributes.vertices[3 * objIndex.vertex_index + 1], + objVertexAttributes.vertices[3 * objIndex.vertex_index + 2]); } - if(objIndex.normal_index >= 0) + if (objIndex.normal_index >= 0) { flatVertex.normal = glm::vec3( objVertexAttributes.normals[3 * objIndex.normal_index + 0], objVertexAttributes.normals[3 * objIndex.normal_index + 1], objVertexAttributes.normals[3 * objIndex.normal_index + 2]); } - if(objIndex.texcoord_index >= 0) + if (objIndex.texcoord_index >= 0) { flatVertex.uv = glm::vec2( objVertexAttributes.texcoords[2 * objIndex.texcoord_index + 0], @@ -532,7 +531,7 @@ SlangResult ModelLoader::load( } // finish last mesh. - if(currentMesh) + if (currentMesh) { meshes.push_back(callbacks->createMesh(*currentMesh)); } @@ -553,7 +552,8 @@ SlangResult ModelLoader::load( vertexBufferDesc.defaultState = ResourceState::VertexBuffer; modelData.vertexBuffer = device->createBufferResource(vertexBufferDesc, flatVertices.data()); - if(!modelData.vertexBuffer) return SLANG_FAIL; + if (!modelData.vertexBuffer) + return SLANG_FAIL; IBufferResource::Desc indexBufferDesc; indexBufferDesc.type = IResource::Type::Buffer; @@ -563,11 +563,12 @@ SlangResult ModelLoader::load( indexBufferDesc.defaultState = ResourceState::IndexBuffer; modelData.indexBuffer = device->createBufferResource(indexBufferDesc, flatIndices.data()); - if(!modelData.indexBuffer) return SLANG_FAIL; + if (!modelData.indexBuffer) + return SLANG_FAIL; *outModel = callbacks->createModel(modelData); return SLANG_OK; } -} // gfx +} // namespace platform diff --git a/tools/platform/model.h b/tools/platform/model.h index b4aff9273..b0c625ec9 100644 --- a/tools/platform/model.h +++ b/tools/platform/model.h @@ -1,25 +1,26 @@ // model.h #pragma once +#include "platform-api.h" +#include "slang-com-ptr.h" #include "slang-gfx.h" #include "vector-math.h" -#include "slang-com-ptr.h" -#include <vector> -#include <string> -#include "platform-api.h" +#include <string> +#include <vector> -namespace platform { +namespace platform +{ struct ModelLoader { struct MaterialData { - glm::vec3 diffuseColor; - glm::vec3 specularColor; - float specularity; + glm::vec3 diffuseColor; + glm::vec3 specularColor; + float specularity; - Slang::ComPtr<gfx::ITextureResource> diffuseMap; + Slang::ComPtr<gfx::ITextureResource> diffuseMap; }; struct Vertex @@ -36,18 +37,18 @@ struct ModelLoader int firstIndex; int indexCount; - void* material; + void* material; }; struct ModelData { Slang::ComPtr<gfx::IBufferResource> vertexBuffer; Slang::ComPtr<gfx::IBufferResource> indexBuffer; - gfx::PrimitiveTopology primitiveTopology; - int vertexCount; - int indexCount; - int meshCount; - void* const* meshes; + gfx::PrimitiveTopology primitiveTopology; + int vertexCount; + int indexCount; + int meshCount; + void* const* meshes; }; struct ICallbacks @@ -76,4 +77,4 @@ struct ModelLoader }; -} // gfx +} // namespace platform diff --git a/tools/platform/performance-counter.h b/tools/platform/performance-counter.h index e9e990f45..e90c2fb99 100644 --- a/tools/platform/performance-counter.h +++ b/tools/platform/performance-counter.h @@ -11,10 +11,7 @@ typedef std::chrono::high_resolution_clock::duration Duration; class PerformanceCounter { public: - static inline TimePoint now() - { - return std::chrono::high_resolution_clock::now(); - } + static inline TimePoint now() { return std::chrono::high_resolution_clock::now(); } static inline Duration getElapsedTime(TimePoint counter) { return now() - counter; } static inline float getElapsedTimeInSeconds(TimePoint counter) { diff --git a/tools/platform/placeholder/placeholder-window.cpp b/tools/platform/placeholder/placeholder-window.cpp index ae4f413f8..43494d898 100644 --- a/tools/platform/placeholder/placeholder-window.cpp +++ b/tools/platform/placeholder/placeholder-window.cpp @@ -7,17 +7,13 @@ using namespace Slang; namespace platform { -void Application::init() -{ -} +void Application::init() {} -void Application::doEvents() { } +void Application::doEvents() {} -void Application::quit() { } +void Application::quit() {} -void Application::dispose() -{ -} +void Application::dispose() {} void Application::run(Window* mainWindow, bool waitForEvents) { @@ -25,7 +21,10 @@ void Application::run(Window* mainWindow, bool waitForEvents) SLANG_UNUSED(waitForEvents); } -Window* Application::createWindow(const WindowDesc& desc) { return nullptr; } +Window* Application::createWindow(const WindowDesc& desc) +{ + return nullptr; +} } // namespace platform diff --git a/tools/platform/platform-api.h b/tools/platform/platform-api.h index b04e5ffce..1b95e677c 100644 --- a/tools/platform/platform-api.h +++ b/tools/platform/platform-api.h @@ -2,22 +2,22 @@ #define SLANG_PLATFORM_API_H #if defined(SLANG_PLATFORM_DYNAMIC) -# if defined(_MSC_VER) -# ifdef SLANG_PLATFORM_DYNAMIC_EXPORT -# define SLANG_PLATFORM_API SLANG_DLL_EXPORT -# else -# define SLANG_PLATFORM_API __declspec(dllimport) -# endif -# else +#if defined(_MSC_VER) +#ifdef SLANG_PLATFORM_DYNAMIC_EXPORT +#define SLANG_PLATFORM_API SLANG_DLL_EXPORT +#else +#define SLANG_PLATFORM_API __declspec(dllimport) +#endif +#else // TODO: need to consider compiler capabilities -//# ifdef SLANG_DYNAMIC_EXPORT -# define SLANG_PLATFORM_API SLANG_DLL_EXPORT -//# endif -# endif +// # ifdef SLANG_DYNAMIC_EXPORT +#define SLANG_PLATFORM_API SLANG_DLL_EXPORT +// # endif +#endif #endif #ifndef SLANG_PLATFORM_API -# define SLANG_PLATFORM_API +#define SLANG_PLATFORM_API #endif #endif diff --git a/tools/platform/vector-math.h b/tools/platform/vector-math.h index e35cb46ac..2e24833eb 100644 --- a/tools/platform/vector-math.h +++ b/tools/platform/vector-math.h @@ -4,12 +4,13 @@ // We will use the GLM library for our vector math types, just for simplicity. #include "../../external/glm/glm/glm.hpp" -#include "../../external/glm/glm/gtc/matrix_transform.hpp" #include "../../external/glm/glm/gtc/constants.hpp" +#include "../../external/glm/glm/gtc/matrix_transform.hpp" #include "../../external/glm/glm/gtc/quaternion.hpp" -namespace gfx { +namespace gfx +{ using namespace glm; -} // gfx +} // namespace gfx diff --git a/tools/platform/window.h b/tools/platform/window.h index 29327def3..a419f85b9 100644 --- a/tools/platform/window.h +++ b/tools/platform/window.h @@ -1,13 +1,13 @@ // window.h #pragma once +#include "platform-api.h" #include "slang-com-ptr.h" #include "source/core/slang-basic.h" #include "source/core/slang-func-ptr.h" -#include "platform-api.h" - -namespace platform { +namespace platform +{ enum class KeyCode : uint32_t { @@ -139,8 +139,13 @@ struct ButtonState { enum Enum { - None = 0, LeftButton = 1, RightButton = 2, MiddleButton = 4, - Shift = 8, Control = 16, Alt = 32 + None = 0, + LeftButton = 1, + RightButton = 2, + MiddleButton = 4, + Shift = 8, + Control = 16, + Alt = 32 }; }; @@ -167,7 +172,8 @@ struct Rect enum class WindowStyle { - Default, FixedSize, + Default, + FixedSize, }; struct WindowDesc @@ -226,37 +232,38 @@ public: #ifdef _WIN32 -# ifdef _MSC_VER -# ifdef _DEBUG -# define GFX_DUMP_LEAK _CrtDumpMemoryLeaks(); -# endif -# endif -# ifndef GFX_DUMP_LEAK -# define GFX_DUMP_LEAK -# endif -# define PLATFORM_UI_MAIN(APPLICATION_ENTRY) \ - int __stdcall wWinMain( \ - void* /*instance*/, \ - void* /* prevInstance */, \ - void* /* commandLine */, \ - int /*showCommand*/) \ - { \ - platform::Application::init(); \ - auto result = APPLICATION_ENTRY(0, nullptr); \ - platform::Application::dispose(); \ - GFX_DUMP_LEAK \ - return result; \ - } +#ifdef _MSC_VER +#ifdef _DEBUG +#define GFX_DUMP_LEAK _CrtDumpMemoryLeaks(); +#endif +#endif +#ifndef GFX_DUMP_LEAK +#define GFX_DUMP_LEAK +#endif +#define PLATFORM_UI_MAIN(APPLICATION_ENTRY) \ + int __stdcall wWinMain( \ + void* /*instance*/, \ + void* /* prevInstance */, \ + void* /* commandLine */, \ + int /*showCommand*/ \ + ) \ + { \ + platform::Application::init(); \ + auto result = APPLICATION_ENTRY(0, nullptr); \ + platform::Application::dispose(); \ + GFX_DUMP_LEAK \ + return result; \ + } #else -#define PLATFORM_UI_MAIN(APPLICATION_ENTRY) \ - int main(int argc, char** argv) \ - { \ - platform::Application::init(); \ - auto rs = APPLICATION_ENTRY(argc, argv); \ - platform::Application::dispose(); \ - return rs; \ +#define PLATFORM_UI_MAIN(APPLICATION_ENTRY) \ + int main(int argc, char** argv) \ + { \ + platform::Application::init(); \ + auto rs = APPLICATION_ENTRY(argc, argv); \ + platform::Application::dispose(); \ + return rs; \ } #endif diff --git a/tools/platform/windows/win-window.cpp b/tools/platform/windows/win-window.cpp index 896bbd2c6..9c4b79b39 100644 --- a/tools/platform/windows/win-window.cpp +++ b/tools/platform/windows/win-window.cpp @@ -83,10 +83,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) bool processed = false; if (window) { - window->events.mouseUp(MouseEventArgs{ - mx, - my, - 0, getModifierState(wParam)}); + window->events.mouseUp(MouseEventArgs{mx, my, 0, getModifierState(wParam)}); } } break; @@ -126,8 +123,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { if (window) { - KeyEventArgs keyEventArgs = { - KeyCode::None, (wchar_t)(wParam), ButtonState::Enum::None, false}; + KeyEventArgs keyEventArgs = + {KeyCode::None, (wchar_t)(wParam), ButtonState::Enum::None, false}; window->events.keyPress(keyEventArgs); if (keyEventArgs.cancelEvent) useDefProc = false; @@ -187,8 +184,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) return DefWindowProc(hWnd, message, wParam, lParam); } break; - default: - break; + default: break; } if (message == WM_DESTROY && hWnd == Win32AppContext::mainWindowHandle) { @@ -221,9 +217,13 @@ void registerWindowClass() RegisterClassExW(&wcex); } -void unregisterWindowClass() { UnregisterClassW(kWindowClassName, GetModuleHandle(NULL)); } +void unregisterWindowClass() +{ + UnregisterClassW(kWindowClassName, GetModuleHandle(NULL)); +} -HRESULT(WINAPI* getDpiForMonitor) (void* hmonitor, int dpiType, unsigned int* dpiX, unsigned int* dpiY); +HRESULT(WINAPI* getDpiForMonitor) +(void* hmonitor, int dpiType, unsigned int* dpiX, unsigned int* dpiY); void Application::init() { @@ -272,9 +272,15 @@ void doEventsImpl(bool waitForEvents) } while (!Win32AppContext::isTerminated && hasMsg); } -void Application::doEvents() { doEventsImpl(false); } +void Application::doEvents() +{ + doEventsImpl(false); +} -void Application::quit() { Win32AppContext::isTerminated = true; } +void Application::quit() +{ + Win32AppContext::isTerminated = true; +} void Application::dispose() { @@ -407,10 +413,7 @@ public: } virtual bool getFocused() override { return GetFocus() == handle; } virtual bool getVisible() override { return visible; } - virtual WindowHandle getNativeHandle() override - { - return WindowHandle::fromHwnd(handle); - } + virtual WindowHandle getNativeHandle() override { return WindowHandle::fromHwnd(handle); } virtual void setText(Slang::String text) override { SetWindowText(handle, text.toWString().begin()); @@ -442,9 +445,12 @@ public: } }; -Window* Application::createWindow(const WindowDesc& desc) { return new Win32PlatformWindow(desc); } +Window* Application::createWindow(const WindowDesc& desc) +{ + return new Win32PlatformWindow(desc); +} -} // namespace gfx +} // namespace platform #endif |
