diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | CMakeLists.txt | 16 | ||||
| -rw-r--r-- | contrib/glfw/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | dxcommon/CMakeLists.txt | 16 | ||||
| -rw-r--r-- | dxcommon/dxcommon.h | 17 | ||||
| -rw-r--r-- | dxcommon/dxcommon.ixx (renamed from dxcommon/include/dxcommon.h) | 25 | ||||
| -rw-r--r-- | dxcommon/src/dxcommon.cc | 8 | ||||
| -rw-r--r-- | dxwindow/CMakeLists.txt | 10 | ||||
| -rw-r--r-- | dxwindow/dxwindow.ixx | 113 | ||||
| -rw-r--r-- | dxwindow/include/dxwindow.h | 50 | ||||
| -rw-r--r-- | dxwindow/src/dxwindow.cc | 80 | ||||
| -rw-r--r-- | hello/CMakeLists.txt | 9 | ||||
| -rw-r--r-- | hello/main.cc | 9 |
13 files changed, 178 insertions, 178 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4c4ffc --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1 @@ | |||
| *.zip | |||
diff --git a/CMakeLists.txt b/CMakeLists.txt index c916974..631e1e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
| @@ -1,14 +1,16 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.20) | 1 | cmake_minimum_required(VERSION 3.25) |
| 2 | |||
| 3 | project(dx12) | ||
| 4 | 2 | ||
| 5 | set(CMAKE_CXX_STANDARD 20) | 3 | set(CMAKE_CXX_STANDARD 20) |
| 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) | 4 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 5 | set(CMAKE_CXX_EXTENSIONS OFF) | ||
| 6 | set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1) | ||
| 7 | set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "2182bf5c-ef0d-489a-91da-49dbc3090d2a") | ||
| 8 | set(CMAKE_CXX_SCAN_FOR_MODULES ON) | ||
| 7 | 9 | ||
| 8 | # -MT, multi-threaded statically-linked runtime library. | 10 | # Multi-threaded statically-linked runtime library (-MT) |
| 9 | #set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) | 11 | set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") |
| 10 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") | 12 | |
| 11 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd /DDEBUG") | 13 | project(dx12) |
| 12 | 14 | ||
| 13 | # External dependencies. | 15 | # External dependencies. |
| 14 | add_subdirectory(contrib/DirectX-Headers) | 16 | add_subdirectory(contrib/DirectX-Headers) |
diff --git a/contrib/glfw/CMakeLists.txt b/contrib/glfw/CMakeLists.txt index 3521151..5635cfb 100644 --- a/contrib/glfw/CMakeLists.txt +++ b/contrib/glfw/CMakeLists.txt | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.20) | 1 | cmake_minimum_required(VERSION 3.25) |
| 2 | 2 | ||
| 3 | add_library(glfw INTERFACE) | 3 | add_library(glfw INTERFACE) |
| 4 | 4 | ||
diff --git a/dxcommon/CMakeLists.txt b/dxcommon/CMakeLists.txt index 08fa178..dfb52bf 100644 --- a/dxcommon/CMakeLists.txt +++ b/dxcommon/CMakeLists.txt | |||
| @@ -1,10 +1,18 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.20) | 1 | cmake_minimum_required(VERSION 3.25) |
| 2 | 2 | ||
| 3 | add_library(dxcommon | 3 | project(dxcommon) |
| 4 | src/dxcommon.cc) | 4 | |
| 5 | add_library(dxcommon) | ||
| 6 | |||
| 7 | target_sources(dxcommon PUBLIC | ||
| 8 | dxcommon.h) | ||
| 9 | |||
| 10 | target_sources(dxcommon PUBLIC | ||
| 11 | FILE_SET cxx_modules TYPE CXX_MODULES FILES | ||
| 12 | dxcommon.ixx) | ||
| 5 | 13 | ||
| 6 | target_include_directories(dxcommon PUBLIC | 14 | target_include_directories(dxcommon PUBLIC |
| 7 | ${CMAKE_CURRENT_SOURCE_DIR}/include) | 15 | .) |
| 8 | 16 | ||
| 9 | target_link_libraries(dxcommon PUBLIC | 17 | target_link_libraries(dxcommon PUBLIC |
| 10 | DirectX-Headers | 18 | DirectX-Headers |
diff --git a/dxcommon/dxcommon.h b/dxcommon/dxcommon.h new file mode 100644 index 0000000..addb8c3 --- /dev/null +++ b/dxcommon/dxcommon.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <d3d12.h> | ||
| 4 | #include <dxgi1_4.h> | ||
| 5 | #include <directx/d3dx12.h> | ||
| 6 | |||
| 7 | #define THROW(error) throw exception(error, __FILE__, __LINE__) | ||
| 8 | |||
| 9 | #define ThrowIfFailed(result) \ | ||
| 10 | {\ | ||
| 11 | if (result != S_OK) \ | ||
| 12 | {\ | ||
| 13 | THROW(result);\ | ||
| 14 | }\ | ||
| 15 | } | ||
| 16 | |||
| 17 | //#define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), static_cast<void**>(ppType) | ||
diff --git a/dxcommon/include/dxcommon.h b/dxcommon/dxcommon.ixx index 35ec0e2..b06ae95 100644 --- a/dxcommon/include/dxcommon.h +++ b/dxcommon/dxcommon.ixx | |||
| @@ -1,14 +1,15 @@ | |||
| 1 | #pragma once | 1 | module; |
| 2 | 2 | ||
| 3 | #include <wrl.h> | 3 | #include <wrl.h> |
| 4 | |||
| 5 | #include <stdexcept> | 4 | #include <stdexcept> |
| 6 | 5 | ||
| 7 | //#define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), static_cast<void**>(ppType) | 6 | export module dxcommon; |
| 7 | |||
| 8 | using Microsoft::WRL::ComPtr; | ||
| 8 | 9 | ||
| 9 | namespace dx { | 10 | namespace dx { |
| 10 | 11 | ||
| 11 | using Microsoft::WRL::ComPtr; | 12 | export { |
| 12 | 13 | ||
| 13 | class exception : public std::exception | 14 | class exception : public std::exception |
| 14 | { | 15 | { |
| @@ -35,16 +36,6 @@ private: | |||
| 35 | static thread_local char m_error[1024]; | 36 | static thread_local char m_error[1024]; |
| 36 | }; | 37 | }; |
| 37 | 38 | ||
| 38 | #define THROW(error) throw exception(error, __FILE__, __LINE__) | ||
| 39 | |||
| 40 | #define ThrowIfFailed(result) \ | ||
| 41 | {\ | ||
| 42 | if (result != S_OK) \ | ||
| 43 | {\ | ||
| 44 | THROW(result);\ | ||
| 45 | }\ | ||
| 46 | } | ||
| 47 | |||
| 48 | template <typename T> | 39 | template <typename T> |
| 49 | void SafeRelease(ComPtr<T>& ptr) | 40 | void SafeRelease(ComPtr<T>& ptr) |
| 50 | { | 41 | { |
| @@ -55,4 +46,8 @@ void SafeRelease(ComPtr<T>& ptr) | |||
| 55 | } | 46 | } |
| 56 | } | 47 | } |
| 57 | 48 | ||
| 58 | } // dx | 49 | } // export |
| 50 | |||
| 51 | thread_local char exception::m_error[1024]; | ||
| 52 | |||
| 53 | } // dx | ||
diff --git a/dxcommon/src/dxcommon.cc b/dxcommon/src/dxcommon.cc deleted file mode 100644 index 2139136..0000000 --- a/dxcommon/src/dxcommon.cc +++ /dev/null | |||
| @@ -1,8 +0,0 @@ | |||
| 1 | #include "dxcommon.h" | ||
| 2 | |||
| 3 | namespace dx | ||
| 4 | { | ||
| 5 | |||
| 6 | thread_local char exception::m_error[1024]; | ||
| 7 | |||
| 8 | } // namespace dx | ||
diff --git a/dxwindow/CMakeLists.txt b/dxwindow/CMakeLists.txt index baf99de..16c1709 100644 --- a/dxwindow/CMakeLists.txt +++ b/dxwindow/CMakeLists.txt | |||
| @@ -1,10 +1,10 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.20) | 1 | cmake_minimum_required(VERSION 3.25) |
| 2 | 2 | ||
| 3 | add_library(dxwindow | 3 | add_library(dxwindow) |
| 4 | src/dxwindow.cc) | ||
| 5 | 4 | ||
| 6 | target_include_directories(dxwindow PUBLIC | 5 | target_sources(dxwindow PUBLIC |
| 7 | include/) | 6 | FILE_SET cxx_modules TYPE CXX_MODULES FILES |
| 7 | dxwindow.ixx) | ||
| 8 | 8 | ||
| 9 | target_link_libraries(dxwindow PUBLIC | 9 | target_link_libraries(dxwindow PUBLIC |
| 10 | glfw) | 10 | glfw) |
diff --git a/dxwindow/dxwindow.ixx b/dxwindow/dxwindow.ixx new file mode 100644 index 0000000..6efcc18 --- /dev/null +++ b/dxwindow/dxwindow.ixx | |||
| @@ -0,0 +1,113 @@ | |||
| 1 | module; | ||
| 2 | |||
| 3 | // Include Windows.h before GLFW to avoid macro redefinition warnings. | ||
| 4 | #define WIN32_LEAN_AND_MEAN | ||
| 5 | #include <Windows.h> | ||
| 6 | |||
| 7 | #define GLFW_INCLUDE_NONE // Do not include OpenGL headers. | ||
| 8 | #include <GLFW/glfw3.h> | ||
| 9 | |||
| 10 | #define GLFW_EXPOSE_NATIVE_WIN32 | ||
| 11 | #include <GLFW/glfw3native.h> | ||
| 12 | |||
| 13 | #include <cassert> | ||
| 14 | #include <cstdio> | ||
| 15 | |||
| 16 | export module dxwindow; | ||
| 17 | |||
| 18 | namespace dx { | ||
| 19 | |||
| 20 | char glfw_error[1024] = {}; | ||
| 21 | |||
| 22 | void glfw_error_callback(int error, const char* description) | ||
| 23 | { | ||
| 24 | sprintf_s(glfw_error, sizeof(glfw_error), | ||
| 25 | "GLFW error %d: %s", error, description); | ||
| 26 | } | ||
| 27 | |||
| 28 | export { | ||
| 29 | |||
| 30 | class Window | ||
| 31 | { | ||
| 32 | public: | ||
| 33 | ~Window() | ||
| 34 | { | ||
| 35 | if (m_window != nullptr) | ||
| 36 | { | ||
| 37 | glfwDestroyWindow(m_window); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | /// Creates the window. | ||
| 42 | bool Initialise(int width, int height, const char* title) | ||
| 43 | { | ||
| 44 | // GLFW by default creates an OpenGL context with the window. | ||
| 45 | // Use GLFW_NO_API to tell it not to do so. | ||
| 46 | glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); | ||
| 47 | |||
| 48 | if ((m_window = glfwCreateWindow( | ||
| 49 | width, height, title, /*monitor=*/NULL, /*share=*/NULL)) == nullptr) | ||
| 50 | { | ||
| 51 | return false; | ||
| 52 | } | ||
| 53 | |||
| 54 | return true; | ||
| 55 | } | ||
| 56 | |||
| 57 | /// Returns the native window handle. | ||
| 58 | /// If the window has not been initialized, returns an invalid handle. | ||
| 59 | HWND GetWindowHandle() | ||
| 60 | { | ||
| 61 | if (!m_window) | ||
| 62 | { | ||
| 63 | return NULL; | ||
| 64 | } | ||
| 65 | return glfwGetWin32Window(m_window); | ||
| 66 | } | ||
| 67 | |||
| 68 | /// Updates the window by polling for user input. | ||
| 69 | void Update() | ||
| 70 | { | ||
| 71 | assert(m_window); | ||
| 72 | glfwPollEvents(); | ||
| 73 | } | ||
| 74 | |||
| 75 | /// Returns true if the user tried to close the window, false otherwise. | ||
| 76 | bool ShouldClose() const | ||
| 77 | { | ||
| 78 | assert(m_window); | ||
| 79 | return glfwWindowShouldClose(m_window) == GLFW_TRUE; | ||
| 80 | } | ||
| 81 | |||
| 82 | private: | ||
| 83 | GLFWwindow* m_window = nullptr; | ||
| 84 | }; | ||
| 85 | |||
| 86 | /// Initialise the window subsystem. | ||
| 87 | /// | ||
| 88 | /// This function must be called at the start of your application before any | ||
| 89 | /// Windows are created. | ||
| 90 | bool WindowInitialise() | ||
| 91 | { | ||
| 92 | glfwSetErrorCallback(glfw_error_callback); | ||
| 93 | return glfwInit() == GLFW_TRUE; | ||
| 94 | } | ||
| 95 | |||
| 96 | /// Terminate the window subsystem. | ||
| 97 | /// | ||
| 98 | /// This function should be called at the end of your application. Any existing | ||
| 99 | /// Windows are destroyed and are invalid beyond this call. | ||
| 100 | void WindowTerminate() | ||
| 101 | { | ||
| 102 | glfwTerminate(); | ||
| 103 | } | ||
| 104 | |||
| 105 | /// Returns the last Window error. | ||
| 106 | const char* GetWindowError() | ||
| 107 | { | ||
| 108 | return glfw_error; | ||
| 109 | } | ||
| 110 | |||
| 111 | } // export | ||
| 112 | |||
| 113 | } // namespace dx | ||
diff --git a/dxwindow/include/dxwindow.h b/dxwindow/include/dxwindow.h deleted file mode 100644 index e8f551a..0000000 --- a/dxwindow/include/dxwindow.h +++ /dev/null | |||
| @@ -1,50 +0,0 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Include Windows.h before GLFW to avoid macro redefinition warnings. | ||
| 4 | #define WIN32_LEAN_AND_MEAN | ||
| 5 | #include <Windows.h> | ||
| 6 | |||
| 7 | #define GLFW_INCLUDE_NONE // Do not include OpenGL headers. | ||
| 8 | #include <GLFW/glfw3.h> | ||
| 9 | |||
| 10 | namespace dx | ||
| 11 | { | ||
| 12 | |||
| 13 | class Window | ||
| 14 | { | ||
| 15 | public: | ||
| 16 | ~Window(); | ||
| 17 | |||
| 18 | /// Creates the window. | ||
| 19 | bool Initialise(int width, int height, const char* title); | ||
| 20 | |||
| 21 | /// Returns the native window handle. | ||
| 22 | /// If the window has not been initialized, returns an invalid handle. | ||
| 23 | HWND GetWindowHandle(); | ||
| 24 | |||
| 25 | /// Updates the window by polling for user input. | ||
| 26 | void Update(); | ||
| 27 | |||
| 28 | /// Returns true if the user tried to close the window, false otherwise. | ||
| 29 | bool ShouldClose() const; | ||
| 30 | |||
| 31 | private: | ||
| 32 | GLFWwindow* m_window = nullptr; | ||
| 33 | }; | ||
| 34 | |||
| 35 | /// Initialise the window subsystem. | ||
| 36 | /// | ||
| 37 | /// This function must be called at the start of your application before any | ||
| 38 | /// Windows are created. | ||
| 39 | bool WindowInitialise(); | ||
| 40 | |||
| 41 | /// Terminate the window subsystem. | ||
| 42 | /// | ||
| 43 | /// This function should be called at the end of your application. Any existing | ||
| 44 | /// Windows are destroyed and are invalid beyond this call. | ||
| 45 | void WindowTerminate(); | ||
| 46 | |||
| 47 | /// Returns the last Window error. | ||
| 48 | const char* GetWindowError(); | ||
| 49 | |||
| 50 | } // namespace dx | ||
diff --git a/dxwindow/src/dxwindow.cc b/dxwindow/src/dxwindow.cc deleted file mode 100644 index 8848a7e..0000000 --- a/dxwindow/src/dxwindow.cc +++ /dev/null | |||
| @@ -1,80 +0,0 @@ | |||
| 1 | #include "dxwindow.h" | ||
| 2 | |||
| 3 | #define GLFW_EXPOSE_NATIVE_WIN32 | ||
| 4 | #include <GLFW/glfw3native.h> | ||
| 5 | |||
| 6 | #include <cassert> | ||
| 7 | #include <cstdio> | ||
| 8 | |||
| 9 | namespace dx | ||
| 10 | { | ||
| 11 | |||
| 12 | static char glfw_error[1024] = {}; | ||
| 13 | |||
| 14 | static void glfw_error_callback(int error, const char* description) | ||
| 15 | { | ||
| 16 | sprintf_s(glfw_error, sizeof(glfw_error), | ||
| 17 | "GLFW error %d: %s", error, description); | ||
| 18 | } | ||
| 19 | |||
| 20 | Window::~Window() | ||
| 21 | { | ||
| 22 | if (m_window != nullptr) | ||
| 23 | { | ||
| 24 | glfwDestroyWindow(m_window); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | bool Window::Initialise(int width, int height, const char* title) | ||
| 29 | { | ||
| 30 | // GLFW by default creates an OpenGL context with the window. | ||
| 31 | // Use GLFW_NO_API to tell it not to do so. | ||
| 32 | glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); | ||
| 33 | |||
| 34 | if ((m_window = glfwCreateWindow( | ||
| 35 | width, height, title, /*monitor=*/NULL, /*share=*/NULL)) == nullptr) | ||
| 36 | { | ||
| 37 | return false; | ||
| 38 | } | ||
| 39 | |||
| 40 | return true; | ||
| 41 | } | ||
| 42 | |||
| 43 | HWND Window::GetWindowHandle() | ||
| 44 | { | ||
| 45 | if (!m_window) | ||
| 46 | { | ||
| 47 | return NULL; | ||
| 48 | } | ||
| 49 | return glfwGetWin32Window(m_window); | ||
| 50 | } | ||
| 51 | |||
| 52 | void Window::Update() | ||
| 53 | { | ||
| 54 | assert(m_window); | ||
| 55 | glfwPollEvents(); | ||
| 56 | } | ||
| 57 | |||
| 58 | bool Window::ShouldClose() const | ||
| 59 | { | ||
| 60 | assert(m_window); | ||
| 61 | return glfwWindowShouldClose(m_window) == GLFW_TRUE; | ||
| 62 | } | ||
| 63 | |||
| 64 | bool WindowInitialise() | ||
| 65 | { | ||
| 66 | glfwSetErrorCallback(glfw_error_callback); | ||
| 67 | return glfwInit() == GLFW_TRUE; | ||
| 68 | } | ||
| 69 | |||
| 70 | void WindowTerminate() | ||
| 71 | { | ||
| 72 | glfwTerminate(); | ||
| 73 | } | ||
| 74 | |||
| 75 | const char* GetWindowError() | ||
| 76 | { | ||
| 77 | return glfw_error; | ||
| 78 | } | ||
| 79 | |||
| 80 | } // namespace dx | ||
diff --git a/hello/CMakeLists.txt b/hello/CMakeLists.txt index 90a0d78..ac9f285 100644 --- a/hello/CMakeLists.txt +++ b/hello/CMakeLists.txt | |||
| @@ -1,7 +1,10 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.20) | 1 | cmake_minimum_required(VERSION 3.25) |
| 2 | 2 | ||
| 3 | add_executable(hello main.cc) | 3 | project(hello) |
| 4 | 4 | ||
| 5 | target_link_libraries(hello | 5 | add_executable(hello |
| 6 | main.cc) | ||
| 7 | |||
| 8 | target_link_libraries(hello PRIVATE | ||
| 6 | dxcommon | 9 | dxcommon |
| 7 | dxwindow) | 10 | dxwindow) |
diff --git a/hello/main.cc b/hello/main.cc index 8e41d4b..47a2aba 100644 --- a/hello/main.cc +++ b/hello/main.cc | |||
| @@ -1,14 +1,13 @@ | |||
| 1 | #include <dxcommon.h> | 1 | #include <dxcommon.h> |
| 2 | #include <dxwindow.h> | ||
| 3 | |||
| 4 | #include <d3d12.h> | ||
| 5 | #include <dxgi1_4.h> | ||
| 6 | #include <directx/d3dx12.h> | ||
| 7 | 2 | ||
| 8 | #include <cassert> | 3 | #include <cassert> |
| 9 | #include <cstdio> | 4 | #include <cstdio> |
| 10 | 5 | ||
| 6 | import dxcommon; | ||
| 7 | import dxwindow; | ||
| 8 | |||
| 11 | using namespace dx; | 9 | using namespace dx; |
| 10 | using Microsoft::WRL::ComPtr; | ||
| 12 | 11 | ||
| 13 | struct D3DSettings | 12 | struct D3DSettings |
| 14 | { | 13 | { |
