Add proper defines for backend enablement

This commit is contained in:
Corentin Wallez 2017-07-12 12:43:24 -04:00 committed by Corentin Wallez
parent a7bfc9d2ac
commit 275817a93a
6 changed files with 232 additions and 145 deletions

View File

@ -21,6 +21,19 @@ project(nxt C CXX)
option(NXT_USE_WERROR "Treat warnings as error (useful for CI)" 0) 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 # 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") list(APPEND NXT_DEFS "NXT_ENABLE_ASSERTS")
endif() 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) if (WIN32)
# Define NOMINMAX to prevent conflicts between std::min/max and the min/max macros in WinDef.h # Define NOMINMAX to prevent conflicts between std::min/max and the min/max macros in WinDef.h
list(APPEND NXT_DEFS "NOMINMAX") list(APPEND NXT_DEFS "NOMINMAX")

View File

@ -39,12 +39,18 @@ enum class CmdBufType {
//TODO(cwallez@chromium.org) double terrible cmdbuf //TODO(cwallez@chromium.org) double terrible cmdbuf
}; };
#if defined(__APPLE__) // Default to D3D12, Metal, Vulkan, OpenGL in that order as D3D12 and Metal are the preferred on
static utils::BackendType backendType = utils::BackendType::Metal; // their respective platforms, and Vulkan is preferred to OpenGL
#elif defined(_WIN32) #if defined(NXT_ENABLE_BACKEND_D3D12)
static utils::BackendType backendType = utils::BackendType::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 #else
static utils::BackendType backendType = utils::BackendType::OpenGL; #error
#endif #endif
static CmdBufType cmdBufType = CmdBufType::Terrible; static CmdBufType cmdBufType = CmdBufType::Terrible;

View File

@ -19,109 +19,75 @@ set(OPENGL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/opengl)
set(D3D12_DIR ${CMAKE_CURRENT_SOURCE_DIR}/d3d12) set(D3D12_DIR ${CMAKE_CURRENT_SOURCE_DIR}/d3d12)
set(TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests) 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 # OpenGL Backend
################################################################################
Generate( if (NXT_ENABLE_OPENGL)
LIB_NAME opengl_autogen Generate(
LIB_TYPE STATIC LIB_NAME opengl_autogen
FOLDER "backend" LIB_TYPE STATIC
PRINT_NAME "OpenGL backend autogenerated files" FOLDER "backend"
COMMAND_LINE_ARGS PRINT_NAME "OpenGL backend autogenerated files"
${GENERATOR_COMMON_ARGS} COMMAND_LINE_ARGS
-T opengl ${GENERATOR_COMMON_ARGS}
) -T opengl
target_link_libraries(opengl_autogen glfw glad nxtcpp) )
target_include_directories(opengl_autogen PRIVATE ${SRC_DIR}) target_link_libraries(opengl_autogen glfw glad nxtcpp)
target_include_directories(opengl_autogen PUBLIC ${GENERATED_DIR}) target_include_directories(opengl_autogen PRIVATE ${SRC_DIR})
target_include_directories(opengl_autogen PUBLIC ${GENERATED_DIR})
list(APPEND BACKEND_SOURCES list(APPEND BACKEND_SOURCES
${OPENGL_DIR}/CommandBufferGL.cpp ${OPENGL_DIR}/CommandBufferGL.cpp
${OPENGL_DIR}/CommandBufferGL.h ${OPENGL_DIR}/CommandBufferGL.h
${OPENGL_DIR}/DepthStencilStateGL.cpp ${OPENGL_DIR}/DepthStencilStateGL.cpp
${OPENGL_DIR}/DepthStencilStateGL.h ${OPENGL_DIR}/DepthStencilStateGL.h
${OPENGL_DIR}/OpenGLBackend.cpp ${OPENGL_DIR}/OpenGLBackend.cpp
${OPENGL_DIR}/OpenGLBackend.h ${OPENGL_DIR}/OpenGLBackend.h
${OPENGL_DIR}/PersistentPipelineStateGL.cpp ${OPENGL_DIR}/PersistentPipelineStateGL.cpp
${OPENGL_DIR}/PersistentPipelineStateGL.h ${OPENGL_DIR}/PersistentPipelineStateGL.h
${OPENGL_DIR}/PipelineGL.cpp ${OPENGL_DIR}/PipelineGL.cpp
${OPENGL_DIR}/PipelineGL.h ${OPENGL_DIR}/PipelineGL.h
${OPENGL_DIR}/PipelineLayoutGL.cpp ${OPENGL_DIR}/PipelineLayoutGL.cpp
${OPENGL_DIR}/PipelineLayoutGL.h ${OPENGL_DIR}/PipelineLayoutGL.h
${OPENGL_DIR}/SamplerGL.cpp ${OPENGL_DIR}/SamplerGL.cpp
${OPENGL_DIR}/SamplerGL.h ${OPENGL_DIR}/SamplerGL.h
${OPENGL_DIR}/ShaderModuleGL.cpp ${OPENGL_DIR}/ShaderModuleGL.cpp
${OPENGL_DIR}/ShaderModuleGL.h ${OPENGL_DIR}/ShaderModuleGL.h
${OPENGL_DIR}/TextureGL.cpp ${OPENGL_DIR}/TextureGL.cpp
${OPENGL_DIR}/TextureGL.h ${OPENGL_DIR}/TextureGL.h
) )
endif()
# Null backend ################################################################################
Generate( # Null Backend
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 if (NXT_ENABLE_NULL)
${NULL_DIR}/NullBackend.cpp Generate(
${NULL_DIR}/NullBackend.h 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 # Metal Backend
################################################################################
if (APPLE) if (NXT_ENABLE_METAL)
Generate( Generate(
LIB_NAME metal_autogen LIB_NAME metal_autogen
LIB_TYPE STATIC LIB_TYPE STATIC
@ -161,9 +127,11 @@ if (APPLE)
) )
endif() endif()
################################################################################
# D3D12 Backend # D3D12 Backend
################################################################################
if (WIN32) if (NXT_ENABLE_D3D12)
Generate( Generate(
LIB_NAME d3d12_autogen LIB_NAME d3d12_autogen
LIB_TYPE STATIC LIB_TYPE STATIC
@ -266,13 +234,68 @@ if (WIN32)
) )
endif() 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}) add_library(nxt_backend STATIC ${BACKEND_SOURCES})
NXTInternalTarget("backend" nxt_backend) 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 (NXT_ENABLE_D3D12)
if (APPLE)
target_link_libraries(nxt_backend metal_autogen)
endif()
if (WIN32)
target_link_libraries(nxt_backend d3d12_autogen) target_link_libraries(nxt_backend d3d12_autogen)
endif() 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()

View File

@ -276,17 +276,24 @@ std::ostream& operator<< (std::ostream& stream, const RGBA8& color) {
namespace detail { namespace detail {
bool IsBackendAvailable(BackendType type) { bool IsBackendAvailable(BackendType type) {
#if defined(__APPLE__) switch (type) {
return type == MetalBackend; #if defined(NXT_ENABLE_BACKEND_D3D12)
#elif defined(_WIN32) case D3D12Backend:
return type == D3D12Backend; #endif
#elif __linux__ #if defined(NXT_ENABLE_BACKEND_METAL)
// Temporarily silence a warning while Linux doesn't have a backend that can be tested. case MetalBackend:
(void) type; #endif
return false; #if defined(NXT_ENABLE_BACKEND_OPENGL)
#else case OpenGLBackend:
return false; #endif
#endif #if defined(NXT_ENABLE_BACKEND_VULKAN)
case VulkanBackend:
#endif
return true;
default:
return false;
}
} }
std::vector<BackendType> FilterBackends(const BackendType* types, size_t numParams) { std::vector<BackendType> FilterBackends(const BackendType* types, size_t numParams) {

View File

@ -18,11 +18,21 @@
namespace utils { namespace utils {
BackendBinding* CreateD3D12Binding(); #if defined(NXT_ENABLE_BACKEND_D3D12)
BackendBinding* CreateMetalBinding(); BackendBinding* CreateD3D12Binding();
BackendBinding* CreateOpenGLBinding(); #endif
BackendBinding* CreateNullBinding(); #if defined(NXT_ENABLE_BACKEND_METAL)
BackendBinding* CreateVulkanBinding(); 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) { void BackendBinding::SetWindow(GLFWwindow* window) {
this->window = window; this->window = window;
@ -30,31 +40,33 @@ namespace utils {
BackendBinding* CreateBinding(BackendType type) { BackendBinding* CreateBinding(BackendType type) {
switch (type) { switch (type) {
case BackendType::D3D12: #if defined(NXT_ENABLE_BACKEND_D3D12)
#if defined(_WIN32) case BackendType::D3D12:
return CreateD3D12Binding(); return CreateD3D12Binding();
#else #endif
return nullptr;
#endif
case BackendType::OpenGL: #if defined(NXT_ENABLE_BACKEND_METAL)
return CreateOpenGLBinding(); case BackendType::Metal:
case BackendType::Metal:
#if defined(__APPLE__)
return CreateMetalBinding(); return CreateMetalBinding();
#else #endif
return nullptr;
#endif
case BackendType::Null: #if defined(NXT_ENABLE_BACKEND_NULL)
return CreateNullBinding(); case BackendType::Null:
return CreateNullBinding();
#endif
case BackendType::Vulkan: #if defined(NXT_ENABLE_BACKEND_OPENGL)
return nullptr; // TODO(cwallez@chromium.org) change it to CreateVulkanBinding(); case BackendType::OpenGL:
return CreateOpenGLBinding();
#endif
#if defined(NXT_ENABLE_BACKEND_VULKAN)
case BackendType::Vulkan:
return CreateVulkanBinding();
#endif
default: default:
UNREACHABLE(); return nullptr;
} }
} }

View File

@ -17,21 +17,31 @@ set(UTILS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
list(APPEND UTILS_SOURCES list(APPEND UTILS_SOURCES
${UTILS_DIR}/BackendBinding.cpp ${UTILS_DIR}/BackendBinding.cpp
${UTILS_DIR}/BackendBinding.h ${UTILS_DIR}/BackendBinding.h
${UTILS_DIR}/NullBinding.cpp
${UTILS_DIR}/OpenGLBinding.cpp
${UTILS_DIR}/NXTHelpers.cpp ${UTILS_DIR}/NXTHelpers.cpp
${UTILS_DIR}/NXTHelpers.h ${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 list(APPEND UTILS_SOURCES
${UTILS_DIR}/MetalBinding.mm ${UTILS_DIR}/MetalBinding.mm
) )
endif() endif()
if (WIN32) if (NXT_ENABLE_NULL)
list(APPEND UTILS_SOURCES 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() endif()