diff --git a/CMakeLists.txt b/CMakeLists.txt index 4fe844ebb5..d45c05ab91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,19 @@ project(nxt C CXX) option(NXT_USE_WERROR "Treat warnings as error (useful for CI)" 0) +# Default values for the backend-enabling options +if (WIN32) + set(NXT_ENABLE_D3D12 ON) +elseif(APPLE) + set(NXT_ENABLE_METAL ON) +endif() + +option(NXT_ENABLE_D3D12 "Enable compilation of the D3D12 backend" OFF) +option(NXT_ENABLE_METAL "Enable compilation of the Metal backend" OFF) +option(NXT_ENABLE_NULL "Enable compilation of the Null backend" ON) +option(NXT_ENABLE_OPENGL "Enable compilation of the OpenGL backend" ON) +option(NXT_ENABLE_VULKAN "Enable compilation of the Vulkan backend" OFF) + ################################################################################ # Precompute compile flags and defines, functions to set them ################################################################################ @@ -35,6 +48,22 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInf list(APPEND NXT_DEFS "NXT_ENABLE_ASSERTS") endif() +if (NXT_ENABLE_D3D12) + list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_D3D12") +endif() +if (NXT_ENABLE_METAL) + list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_METAL") +endif() +if (NXT_ENABLE_NULL) + list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_NULL") +endif() +if (NXT_ENABLE_OPENGL) + list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_OPENGL") +endif() +if (NXT_ENABLE_VULKAN) + list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_VULKAN") +endif() + if (WIN32) # Define NOMINMAX to prevent conflicts between std::min/max and the min/max macros in WinDef.h list(APPEND NXT_DEFS "NOMINMAX") diff --git a/examples/SampleUtils.cpp b/examples/SampleUtils.cpp index 6a8ce80154..d89472817b 100644 --- a/examples/SampleUtils.cpp +++ b/examples/SampleUtils.cpp @@ -39,12 +39,18 @@ enum class CmdBufType { //TODO(cwallez@chromium.org) double terrible cmdbuf }; -#if defined(__APPLE__) -static utils::BackendType backendType = utils::BackendType::Metal; -#elif defined(_WIN32) -static utils::BackendType backendType = utils::BackendType::D3D12; +// Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on +// their respective platforms, and Vulkan is preferred to OpenGL +#if defined(NXT_ENABLE_BACKEND_D3D12) + static utils::BackendType backendType = utils::BackendType::D3D12; +#elif defined(NXT_ENABLE_BACKEND_METAL) + static utils::BackendType backendType = utils::BackendType::Metal; +#elif defined(NXT_ENABLE_BACKEND_VULKAN) + static utils::BackendType backendType = utils::BackendType::OpenGL; +#elif defined(NXT_ENABLE_BACKEND_OPENGL) + static utils::BackendType backendType = utils::BackendType::Vulkan; #else -static utils::BackendType backendType = utils::BackendType::OpenGL; + #error #endif static CmdBufType cmdBufType = CmdBufType::Terrible; diff --git a/src/backend/CMakeLists.txt b/src/backend/CMakeLists.txt index b7e0dc93d8..19aeacd673 100644 --- a/src/backend/CMakeLists.txt +++ b/src/backend/CMakeLists.txt @@ -19,109 +19,75 @@ set(OPENGL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/opengl) set(D3D12_DIR ${CMAKE_CURRENT_SOURCE_DIR}/d3d12) set(TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests) -list(APPEND BACKEND_SOURCES - ${BACKEND_DIR}/BindGroup.cpp - ${BACKEND_DIR}/BindGroup.h - ${BACKEND_DIR}/BindGroupLayout.cpp - ${BACKEND_DIR}/BindGroupLayout.h - ${BACKEND_DIR}/Builder.cpp - ${BACKEND_DIR}/Builder.h - ${BACKEND_DIR}/Buffer.cpp - ${BACKEND_DIR}/Buffer.h - ${BACKEND_DIR}/CommandAllocator.cpp - ${BACKEND_DIR}/CommandAllocator.h - ${BACKEND_DIR}/CommandBuffer.cpp - ${BACKEND_DIR}/CommandBuffer.h - ${BACKEND_DIR}/DepthStencilState.cpp - ${BACKEND_DIR}/DepthStencilState.h - ${BACKEND_DIR}/CommandBufferStateTracker.cpp - ${BACKEND_DIR}/CommandBufferStateTracker.h - ${BACKEND_DIR}/Device.cpp - ${BACKEND_DIR}/Device.h - ${BACKEND_DIR}/Forward.h - ${BACKEND_DIR}/Framebuffer.cpp - ${BACKEND_DIR}/Framebuffer.h - ${BACKEND_DIR}/InputState.cpp - ${BACKEND_DIR}/InputState.h - ${BACKEND_DIR}/PerStage.cpp - ${BACKEND_DIR}/PerStage.h - ${BACKEND_DIR}/Pipeline.cpp - ${BACKEND_DIR}/Pipeline.h - ${BACKEND_DIR}/PipelineLayout.cpp - ${BACKEND_DIR}/PipelineLayout.h - ${BACKEND_DIR}/Queue.cpp - ${BACKEND_DIR}/Queue.h - ${BACKEND_DIR}/RenderPass.cpp - ${BACKEND_DIR}/RenderPass.h - ${BACKEND_DIR}/RefCounted.cpp - ${BACKEND_DIR}/RefCounted.h - ${BACKEND_DIR}/Sampler.cpp - ${BACKEND_DIR}/Sampler.h - ${BACKEND_DIR}/ShaderModule.cpp - ${BACKEND_DIR}/ShaderModule.h - ${BACKEND_DIR}/Texture.cpp - ${BACKEND_DIR}/Texture.h - ${BACKEND_DIR}/ToBackend.h -) - +################################################################################ # OpenGL Backend +################################################################################ -Generate( - LIB_NAME opengl_autogen - LIB_TYPE STATIC - FOLDER "backend" - PRINT_NAME "OpenGL backend autogenerated files" - COMMAND_LINE_ARGS - ${GENERATOR_COMMON_ARGS} - -T opengl -) -target_link_libraries(opengl_autogen glfw glad nxtcpp) -target_include_directories(opengl_autogen PRIVATE ${SRC_DIR}) -target_include_directories(opengl_autogen PUBLIC ${GENERATED_DIR}) +if (NXT_ENABLE_OPENGL) + Generate( + LIB_NAME opengl_autogen + LIB_TYPE STATIC + FOLDER "backend" + PRINT_NAME "OpenGL backend autogenerated files" + COMMAND_LINE_ARGS + ${GENERATOR_COMMON_ARGS} + -T opengl + ) + target_link_libraries(opengl_autogen glfw glad nxtcpp) + target_include_directories(opengl_autogen PRIVATE ${SRC_DIR}) + target_include_directories(opengl_autogen PUBLIC ${GENERATED_DIR}) -list(APPEND BACKEND_SOURCES - ${OPENGL_DIR}/CommandBufferGL.cpp - ${OPENGL_DIR}/CommandBufferGL.h - ${OPENGL_DIR}/DepthStencilStateGL.cpp - ${OPENGL_DIR}/DepthStencilStateGL.h - ${OPENGL_DIR}/OpenGLBackend.cpp - ${OPENGL_DIR}/OpenGLBackend.h - ${OPENGL_DIR}/PersistentPipelineStateGL.cpp - ${OPENGL_DIR}/PersistentPipelineStateGL.h - ${OPENGL_DIR}/PipelineGL.cpp - ${OPENGL_DIR}/PipelineGL.h - ${OPENGL_DIR}/PipelineLayoutGL.cpp - ${OPENGL_DIR}/PipelineLayoutGL.h - ${OPENGL_DIR}/SamplerGL.cpp - ${OPENGL_DIR}/SamplerGL.h - ${OPENGL_DIR}/ShaderModuleGL.cpp - ${OPENGL_DIR}/ShaderModuleGL.h - ${OPENGL_DIR}/TextureGL.cpp - ${OPENGL_DIR}/TextureGL.h -) + list(APPEND BACKEND_SOURCES + ${OPENGL_DIR}/CommandBufferGL.cpp + ${OPENGL_DIR}/CommandBufferGL.h + ${OPENGL_DIR}/DepthStencilStateGL.cpp + ${OPENGL_DIR}/DepthStencilStateGL.h + ${OPENGL_DIR}/OpenGLBackend.cpp + ${OPENGL_DIR}/OpenGLBackend.h + ${OPENGL_DIR}/PersistentPipelineStateGL.cpp + ${OPENGL_DIR}/PersistentPipelineStateGL.h + ${OPENGL_DIR}/PipelineGL.cpp + ${OPENGL_DIR}/PipelineGL.h + ${OPENGL_DIR}/PipelineLayoutGL.cpp + ${OPENGL_DIR}/PipelineLayoutGL.h + ${OPENGL_DIR}/SamplerGL.cpp + ${OPENGL_DIR}/SamplerGL.h + ${OPENGL_DIR}/ShaderModuleGL.cpp + ${OPENGL_DIR}/ShaderModuleGL.h + ${OPENGL_DIR}/TextureGL.cpp + ${OPENGL_DIR}/TextureGL.h + ) +endif() -# Null backend -Generate( - LIB_NAME null_autogen - LIB_TYPE STATIC - FOLDER "backend" - PRINT_NAME "Null backend autogenerated files" - COMMAND_LINE_ARGS - ${GENERATOR_COMMON_ARGS} - -T null -) -target_link_libraries(null_autogen nxtcpp) -target_include_directories(null_autogen PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) -target_include_directories(null_autogen PUBLIC ${SRC_DIR}) +################################################################################ +# Null Backend +################################################################################ -list(APPEND BACKEND_SOURCES - ${NULL_DIR}/NullBackend.cpp - ${NULL_DIR}/NullBackend.h -) +if (NXT_ENABLE_NULL) + Generate( + LIB_NAME null_autogen + LIB_TYPE STATIC + FOLDER "backend" + PRINT_NAME "Null backend autogenerated files" + COMMAND_LINE_ARGS + ${GENERATOR_COMMON_ARGS} + -T null + ) + target_link_libraries(null_autogen nxtcpp) + target_include_directories(null_autogen PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_include_directories(null_autogen PUBLIC ${SRC_DIR}) + list(APPEND BACKEND_SOURCES + ${NULL_DIR}/NullBackend.cpp + ${NULL_DIR}/NullBackend.h + ) +endif() + +################################################################################ # Metal Backend +################################################################################ -if (APPLE) +if (NXT_ENABLE_METAL) Generate( LIB_NAME metal_autogen LIB_TYPE STATIC @@ -161,9 +127,11 @@ if (APPLE) ) endif() +################################################################################ # D3D12 Backend +################################################################################ -if (WIN32) +if (NXT_ENABLE_D3D12) Generate( LIB_NAME d3d12_autogen LIB_TYPE STATIC @@ -266,13 +234,68 @@ if (WIN32) ) endif() +################################################################################ +# Common sources definition of the library +################################################################################ + +list(APPEND BACKEND_SOURCES + ${BACKEND_DIR}/BindGroup.cpp + ${BACKEND_DIR}/BindGroup.h + ${BACKEND_DIR}/BindGroupLayout.cpp + ${BACKEND_DIR}/BindGroupLayout.h + ${BACKEND_DIR}/Builder.cpp + ${BACKEND_DIR}/Builder.h + ${BACKEND_DIR}/Buffer.cpp + ${BACKEND_DIR}/Buffer.h + ${BACKEND_DIR}/CommandAllocator.cpp + ${BACKEND_DIR}/CommandAllocator.h + ${BACKEND_DIR}/CommandBuffer.cpp + ${BACKEND_DIR}/CommandBuffer.h + ${BACKEND_DIR}/DepthStencilState.cpp + ${BACKEND_DIR}/DepthStencilState.h + ${BACKEND_DIR}/CommandBufferStateTracker.cpp + ${BACKEND_DIR}/CommandBufferStateTracker.h + ${BACKEND_DIR}/Device.cpp + ${BACKEND_DIR}/Device.h + ${BACKEND_DIR}/Forward.h + ${BACKEND_DIR}/Framebuffer.cpp + ${BACKEND_DIR}/Framebuffer.h + ${BACKEND_DIR}/InputState.cpp + ${BACKEND_DIR}/InputState.h + ${BACKEND_DIR}/PerStage.cpp + ${BACKEND_DIR}/PerStage.h + ${BACKEND_DIR}/Pipeline.cpp + ${BACKEND_DIR}/Pipeline.h + ${BACKEND_DIR}/PipelineLayout.cpp + ${BACKEND_DIR}/PipelineLayout.h + ${BACKEND_DIR}/Queue.cpp + ${BACKEND_DIR}/Queue.h + ${BACKEND_DIR}/RenderPass.cpp + ${BACKEND_DIR}/RenderPass.h + ${BACKEND_DIR}/RefCounted.cpp + ${BACKEND_DIR}/RefCounted.h + ${BACKEND_DIR}/Sampler.cpp + ${BACKEND_DIR}/Sampler.h + ${BACKEND_DIR}/ShaderModule.cpp + ${BACKEND_DIR}/ShaderModule.h + ${BACKEND_DIR}/Texture.cpp + ${BACKEND_DIR}/Texture.h + ${BACKEND_DIR}/ToBackend.h +) + add_library(nxt_backend STATIC ${BACKEND_SOURCES}) NXTInternalTarget("backend" nxt_backend) +target_link_libraries(nxt_backend nxt_common glfw glad spirv_cross) -target_link_libraries(nxt_backend nxt_common opengl_autogen null_autogen glfw glad spirv_cross) -if (APPLE) - target_link_libraries(nxt_backend metal_autogen) -endif() -if (WIN32) +if (NXT_ENABLE_D3D12) target_link_libraries(nxt_backend d3d12_autogen) endif() +if (NXT_ENABLE_METAL) + target_link_libraries(nxt_backend metal_autogen) +endif() +if (NXT_ENABLE_NULL) + target_link_libraries(nxt_backend null_autogen) +endif() +if (NXT_ENABLE_OPENGL) + target_link_libraries(nxt_backend opengl_autogen) +endif() diff --git a/src/tests/NXTTest.cpp b/src/tests/NXTTest.cpp index c59ebaaa81..de0c6161f7 100644 --- a/src/tests/NXTTest.cpp +++ b/src/tests/NXTTest.cpp @@ -276,17 +276,24 @@ std::ostream& operator<< (std::ostream& stream, const RGBA8& color) { namespace detail { bool IsBackendAvailable(BackendType type) { - #if defined(__APPLE__) - return type == MetalBackend; - #elif defined(_WIN32) - return type == D3D12Backend; - #elif __linux__ - // Temporarily silence a warning while Linux doesn't have a backend that can be tested. - (void) type; - return false; - #else - return false; - #endif + switch (type) { + #if defined(NXT_ENABLE_BACKEND_D3D12) + case D3D12Backend: + #endif + #if defined(NXT_ENABLE_BACKEND_METAL) + case MetalBackend: + #endif + #if defined(NXT_ENABLE_BACKEND_OPENGL) + case OpenGLBackend: + #endif + #if defined(NXT_ENABLE_BACKEND_VULKAN) + case VulkanBackend: + #endif + return true; + + default: + return false; + } } std::vector FilterBackends(const BackendType* types, size_t numParams) { diff --git a/src/utils/BackendBinding.cpp b/src/utils/BackendBinding.cpp index db19eb35f4..d3e65742c9 100644 --- a/src/utils/BackendBinding.cpp +++ b/src/utils/BackendBinding.cpp @@ -18,11 +18,21 @@ namespace utils { - BackendBinding* CreateD3D12Binding(); - BackendBinding* CreateMetalBinding(); - BackendBinding* CreateOpenGLBinding(); - BackendBinding* CreateNullBinding(); - BackendBinding* CreateVulkanBinding(); + #if defined(NXT_ENABLE_BACKEND_D3D12) + BackendBinding* CreateD3D12Binding(); + #endif + #if defined(NXT_ENABLE_BACKEND_METAL) + BackendBinding* CreateMetalBinding(); + #endif + #if defined(NXT_ENABLE_BACKEND_NULL) + BackendBinding* CreateNullBinding(); + #endif + #if defined(NXT_ENABLE_BACKEND_OPENGL) + BackendBinding* CreateOpenGLBinding(); + #endif + #if defined(NXT_ENABLE_BACKEND_VULKAN) + BackendBinding* CreateVulkanBinding(); + #endif void BackendBinding::SetWindow(GLFWwindow* window) { this->window = window; @@ -30,31 +40,33 @@ namespace utils { BackendBinding* CreateBinding(BackendType type) { switch (type) { - case BackendType::D3D12: - #if defined(_WIN32) + #if defined(NXT_ENABLE_BACKEND_D3D12) + case BackendType::D3D12: return CreateD3D12Binding(); - #else - return nullptr; - #endif + #endif - case BackendType::OpenGL: - return CreateOpenGLBinding(); - - case BackendType::Metal: - #if defined(__APPLE__) + #if defined(NXT_ENABLE_BACKEND_METAL) + case BackendType::Metal: return CreateMetalBinding(); - #else - return nullptr; - #endif + #endif - case BackendType::Null: - return CreateNullBinding(); + #if defined(NXT_ENABLE_BACKEND_NULL) + case BackendType::Null: + return CreateNullBinding(); + #endif - case BackendType::Vulkan: - return nullptr; // TODO(cwallez@chromium.org) change it to CreateVulkanBinding(); + #if defined(NXT_ENABLE_BACKEND_OPENGL) + case BackendType::OpenGL: + return CreateOpenGLBinding(); + #endif + + #if defined(NXT_ENABLE_BACKEND_VULKAN) + case BackendType::Vulkan: + return CreateVulkanBinding(); + #endif default: - UNREACHABLE(); + return nullptr; } } diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 1202d0e523..2bc4782bab 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -17,21 +17,31 @@ set(UTILS_DIR ${CMAKE_CURRENT_SOURCE_DIR}) list(APPEND UTILS_SOURCES ${UTILS_DIR}/BackendBinding.cpp ${UTILS_DIR}/BackendBinding.h - ${UTILS_DIR}/NullBinding.cpp - ${UTILS_DIR}/OpenGLBinding.cpp ${UTILS_DIR}/NXTHelpers.cpp ${UTILS_DIR}/NXTHelpers.h ) -if (APPLE) +if (NXT_ENABLE_D3D12) + list(APPEND UTILS_SOURCES + ${UTILS_DIR}/D3D12Binding.cpp + ) +endif() + +if (NXT_ENABLE_METAL) list(APPEND UTILS_SOURCES ${UTILS_DIR}/MetalBinding.mm ) endif() -if (WIN32) +if (NXT_ENABLE_NULL) list(APPEND UTILS_SOURCES - ${UTILS_DIR}/D3D12Binding.cpp + ${UTILS_DIR}/NullBinding.cpp + ) +endif() + +if (NXT_ENABLE_OPENGL) + list(APPEND UTILS_SOURCES + ${UTILS_DIR}/OpenGLBinding.cpp ) endif()