Skip to content

Commit

Permalink
sokol: Add d3d11 backend
Browse files Browse the repository at this point in the history
  • Loading branch information
IonAgorria committed Feb 26, 2024
1 parent 9a36938 commit 84de2a3
Show file tree
Hide file tree
Showing 14 changed files with 5,840 additions and 1,241 deletions.
179 changes: 107 additions & 72 deletions Source/Render/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,9 @@ target_include_directories(Render PRIVATE

SET(Render_LINK_LIBS PluginMAX)

IF (OPTION_SOKOL)
INCLUDE("sokol/CMakeLists.txt")
SET(Render_LINK_LIBS ${Render_LINK_LIBS} RenderSokol)
ENDIF()

IF(OPTION_D3D9)
if(OPTION_D3D9)
#D3D9 Renderer enabled
IF(PERIMETER_WINDOWS)
if(PERIMETER_WINDOWS)
#DirectX SDK libs
target_include_directories(Render PUBLIC "${PROJECT_SOURCE_DIR}/MSDXSDK_02_06/Include")

Expand All @@ -111,79 +106,119 @@ IF(OPTION_D3D9)
${LIBS_MSDXSDK}/d3dx9.lib
${LIBS_MSDXSDK}/DxErr.lib
)
ELSE()
#For non windows we use DXVK to provide headers and impl
MESSAGE("Using DXVK for D3D9 API")

set(DXVK_BUILD_OPTIONS --buildtype=release -Denable_d3d9=true -Denable_d3d11=false -Denable_d3d10=false -Denable_dxgi=false)
#DXVK wrapper lib retrieval and building
IF (MACOS)
IF (CMAKE_OSX_ARCHITECTURES)
#Cross compile script, this assumes CMAKE_OSX_ARCHITECTURES only contains 1 arch
set(DXVK_BUILD_COMMAND
${PROJECT_SOURCE_DIR}/macos/dxvk-native.sh
${CMAKE_OSX_ARCHITECTURES}
${PROJECT_SOURCE_DIR}/macos/
)
ENDIF()
#TODO change this to upstream if DXVK2 works with MoltenVK and MacOS
set(OPTION_DXVK_1 ON)
else()
set(NEED_DXVK ON)
endif()
endif()

if(OPTION_SOKOL AND NOT PERIMETER_WINDOWS AND OPTION_SOKOL_API MATCHES "D3D11")
MESSAGE(WARNING "Using DXVK for Sokol D3D backend, this is only to check compilation! is NOT supported")
set(NEED_DXVK ON)
endif()

if(NEED_DXVK)
#For non windows we use DXVK to provide headers and impl
MESSAGE("Using DXVK for D3D API")

set(DXVK_BUILD_OPTIONS -Denable_d3d10=false)

if(PERIMETER_DEBUG AND OFF)
#Set ON in case one needs to go down a rabbit hole
MESSAGE(WARNING "Building debug DXVK!")
set(DXVK_BUILD_OPTIONS ${DXVK_BUILD_OPTIONS} --buildtype=debug)
else()
set(DXVK_BUILD_OPTIONS ${DXVK_BUILD_OPTIONS} --buildtype=release)
endif ()
if(OPTION_D3D9)
set(DXVK_BUILD_OPTIONS ${DXVK_BUILD_OPTIONS} -Denable_d3d9=true)
else()
set(DXVK_BUILD_OPTIONS ${DXVK_BUILD_OPTIONS} -Denable_d3d9=false)
endif()
if(OPTION_SOKOL_API MATCHES "D3D11")
set(DXVK_BUILD_OPTIONS ${DXVK_BUILD_OPTIONS} -Denable_d3d11=true -Denable_dxgi=true)
else()
set(DXVK_BUILD_OPTIONS ${DXVK_BUILD_OPTIONS} -Denable_d3d11=false -Denable_dxgi=false)
endif()

#DXVK wrapper lib retrieval and building
IF (MACOS)
IF (CMAKE_OSX_ARCHITECTURES)
#Cross compile script, this assumes CMAKE_OSX_ARCHITECTURES only contains 1 arch
set(DXVK_BUILD_COMMAND
${PROJECT_SOURCE_DIR}/macos/dxvk-native.sh
${CMAKE_OSX_ARCHITECTURES}
${PROJECT_SOURCE_DIR}/macos/
)
ENDIF()
IF(OPTION_DXVK_1)
#TODO change this to upstream if DXVK2 works with MoltenVK and MacOS
set(OPTION_DXVK_1 ON)
ENDIF()
IF(OPTION_DXVK_1)
if (NOT DXVK_GIT_URL)
set(DXVK_GIT_URL https://github.com/IonAgorria/dxvk-native)
endif ()
if (NOT DXVK_GIT_TAG)
set(DXVK_GIT_TAG "43aedc756cbd620b9ee8b1cf2c17b17cc49b3781")
ELSE()
endif ()
ELSE()
if (NOT DXVK_GIT_URL)
set(DXVK_GIT_URL https://github.com/doitsujin/dxvk)
endif ()
if (NOT DXVK_GIT_TAG)
set(DXVK_GIT_TAG "80e075406b1b7d9d2508c9dc05e9745b3d3cf7e2") # v2.3
set(DXVK_BUILD_OPTIONS ${DXVK_BUILD_OPTIONS} -Ddxvk_native_wsi=sdl2)
ENDIF()
IF("${DXVK_BUILD_COMMAND}" STREQUAL "")
set(DXVK_BUILD_COMMAND meson setup)
ENDIF()
IF(OPTION_DXVK_SOURCE_DIR)
MESSAGE("DXVK Source path provided: ${OPTION_DXVK_SOURCE_DIR}")
ExternalProject_Add(dxvk
SOURCE_DIR ${OPTION_DXVK_SOURCE_DIR}
BUILD_ALWAYS OFF
CONFIGURE_HANDLED_BY_BUILD ON
CONFIGURE_COMMAND ${DXVK_BUILD_COMMAND} ${OPTION_DXVK_SOURCE_DIR} ${DXVK_BUILD_OPTIONS}
BUILD_COMMAND ninja
INSTALL_COMMAND ""
BUILD_BYPRODUCTS <BINARY_DIR>/src/d3d9/libdxvk_d3d9${CMAKE_SHARED_LIBRARY_SUFFIX}
)
ELSE()
ExternalProject_Add(dxvk
GIT_REPOSITORY ${DXVK_GIT_URL}
GIT_TAG ${DXVK_GIT_TAG}
GIT_SHALLOW OFF
BUILD_ALWAYS OFF
CONFIGURE_HANDLED_BY_BUILD ON
CONFIGURE_COMMAND ${DXVK_BUILD_COMMAND} ../dxvk ${DXVK_BUILD_OPTIONS}
BUILD_COMMAND ninja
INSTALL_COMMAND ""
BUILD_BYPRODUCTS <BINARY_DIR>/src/d3d9/libdxvk_d3d9${CMAKE_SHARED_LIBRARY_SUFFIX}
)
ENDIF()
ExternalProject_Get_property(dxvk SOURCE_DIR BINARY_DIR)
set(DXVK_INCLUDE_DIRS
"${SOURCE_DIR}/include/native/directx"
"${SOURCE_DIR}/include/native/windows"
)

endif ()
set(DXVK_BUILD_OPTIONS ${DXVK_BUILD_OPTIONS} -Ddxvk_native_wsi=sdl2)
ENDIF()
IF("${DXVK_BUILD_COMMAND}" STREQUAL "")
set(DXVK_BUILD_COMMAND meson setup)
ENDIF()
IF(OPTION_DXVK_SOURCE_DIR)
MESSAGE("DXVK Source path provided: ${OPTION_DXVK_SOURCE_DIR}")
UNSET(DXVK_GIT_URL)
UNSET(DXVK_GIT_TAG)
SET(DXVK_BUILD_COMMAND ${DXVK_BUILD_COMMAND} ${OPTION_DXVK_SOURCE_DIR})
ELSE()
SET(DXVK_BUILD_COMMAND ${DXVK_BUILD_COMMAND} ../dxvk)
ENDIF()
ExternalProject_Add(dxvk
SOURCE_DIR ${OPTION_DXVK_SOURCE_DIR}
GIT_REPOSITORY ${DXVK_GIT_URL}
GIT_TAG ${DXVK_GIT_TAG}
GIT_SHALLOW OFF
BUILD_ALWAYS OFF
CONFIGURE_HANDLED_BY_BUILD ON
CONFIGURE_COMMAND ${DXVK_BUILD_COMMAND} ${DXVK_BUILD_OPTIONS}
BUILD_COMMAND ninja
INSTALL_COMMAND ""
BUILD_BYPRODUCTS
<BINARY_DIR>/src/d3d9/libdxvk_d3d9${CMAKE_SHARED_LIBRARY_SUFFIX}
<BINARY_DIR>/src/d3d11/libdxvk_d3d11${CMAKE_SHARED_LIBRARY_SUFFIX}
)
ExternalProject_Get_property(dxvk SOURCE_DIR BINARY_DIR)
set(DXVK_BINARY_DIR ${BINARY_DIR})
set(DXVK_INCLUDE_DIRS
"${SOURCE_DIR}/include/native/directx"
"${SOURCE_DIR}/include/native/windows"
)

if(OPTION_D3D9)
#Make sure our d3dx9 is before dxvk includes!
target_include_directories(Render PUBLIC "d3dx9")

#Handle output lib file and also export it during install
set(DXVK_D3D9_LIB ${BINARY_DIR}/src/d3d9/libdxvk_d3d9${CMAKE_SHARED_LIBRARY_SUFFIX})
set(DXVK_D3D9_LIB ${DXVK_BINARY_DIR}/src/d3d9/libdxvk_d3d9${CMAKE_SHARED_LIBRARY_SUFFIX})
set(Render_LINK_LIBS ${Render_LINK_LIBS} ${DXVK_D3D9_LIB})
install (FILES ${DXVK_D3D9_LIB} TYPE LIB)
#This way dxvk will be fetched and compiled before Render
add_dependencies(Render dxvk)

target_include_directories(Render PUBLIC
#Make sure our d3dx9 is before dxvk!
"d3dx9"
"${DXVK_INCLUDE_DIRS}"
)
ENDIF()
endif()

#This way dxvk will be fetched and compiled before Render
add_dependencies(Render dxvk)
target_include_directories(Render PUBLIC "${DXVK_INCLUDE_DIRS}")
endif()

IF (OPTION_SOKOL)
INCLUDE("sokol/CMakeLists.txt")
SET(Render_LINK_LIBS ${Render_LINK_LIBS} RenderSokol)
ENDIF()

target_link_libraries(Render PRIVATE ${Render_LINK_LIBS})
Expand Down
44 changes: 37 additions & 7 deletions Source/Render/sokol/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ IF (OPTION_PROCESS_SHADERS)
FetchContent_MakeAvailable(sokol-tools-bin)

if(NOT DEFINED ENV{PERIMETER_SHADER_LANG})
#Use all GLSL dialects
set(PERIMETER_SHADER_LANG "glsl330:glsl100:glsl300es:metal_macos")
#Use all GLSL dialects, HLSL for D3d and WebGPU
set(PERIMETER_SHADER_LANG "glsl330:glsl300es:hlsl5:wgsl")
#Metal for Apple platforms
set(PERIMETER_SHADER_LANG "${PERIMETER_SHADER_LANG}:metal_macos:metal_ios:metal_sim")
endif()

add_custom_target(sokol_shaders)
Expand Down Expand Up @@ -60,6 +62,9 @@ ENDIF()

#Select Sokol API per platform and options
set(RenderSokol_SokolImpl sokol/SokolImpl.cpp)
if(OPTION_SOKOL_API MATCHES "GL")
set(OPTION_SOKOL_API "GLCORE33")
endif()
if (MACOS)
if(NOT OPTION_SOKOL_API)
set(OPTION_SOKOL_API "METAL")
Expand All @@ -69,6 +74,7 @@ if (MACOS)
if(OPTION_SOKOL_API MATCHES "GLCORE33")
message("Sokol API: OpenGL Core 3.3")
add_definitions(-DSOKOL_GLCORE33)
set(SOKOL_GL ON)
set(RenderSokol_LINK_LIBS "-framework OpenGL")
elseif(OPTION_SOKOL_API MATCHES "METAL")
message("Sokol API: Metal")
Expand All @@ -87,9 +93,11 @@ elseif(PERIMETER_WINDOWS)
if(OPTION_SOKOL_API MATCHES "D3D11")
message("Sokol API: DirectX 11")
add_definitions(-DSOKOL_D3D11)
set(RenderSokol_LINK_LIBS d3d11)
elseif(OPTION_SOKOL_API MATCHES "GLCORE33")
message("Sokol API: OpenGL Core 3.3")
add_definitions(-DSOKOL_GLCORE33)
set(SOKOL_GL ON)
set(RenderSokol_LINK_LIBS opengl32)
else()
message(SEND_ERROR "Unknown Sokol API selected: ${OPTION_SOKOL_API}")
Expand All @@ -101,22 +109,31 @@ else()
if(OPTION_SOKOL_API MATCHES "GLES2")
message("Sokol API: OpenGLES 2.0")
add_definitions(-DSOKOL_GLES2)
set(RenderSokol_LINK_LIBS GL)
set(SOKOL_GL ON)
elseif(OPTION_SOKOL_API MATCHES "GLES3")
message("Sokol API: OpenGLES 3.0")
add_definitions(-DSOKOL_GLES3)
set(RenderSokol_LINK_LIBS GL)
set(SOKOL_GL ON)
elseif(OPTION_SOKOL_API MATCHES "GLCORE33")
message("Sokol API: OpenGL Core 3.3")
add_definitions(-DSOKOL_GLCORE33)
set(RenderSokol_LINK_LIBS GL)
set(SOKOL_GL ON)
elseif(OPTION_SOKOL_API MATCHES "D3D11")
message("Sokol API: D3D11")
add_definitions(-DSOKOL_D3D11)
#The rest done after add_library()
else()
message(SEND_ERROR "Unknown Sokol API selected: ${OPTION_SOKOL_API}")
endif()
if (SOKOL_GL)
set(RenderSokol_LINK_LIBS GL)
endif ()
endif()

#Required for SDL_GL_* funcs called during render init
set(RenderSokol_LINK_LIBS ${RenderSokol_LINK_LIBS} ${SDL2_LIBRARY} XTool)
if (SOKOL_GL)
#Required for SDL_GL_* funcs called during render init
set(RenderSokol_LINK_LIBS ${RenderSokol_LINK_LIBS} ${SDL2_LIBRARY} XTool)
endif ()

add_library(RenderSokol STATIC
${RenderSokol_SokolImpl}
Expand All @@ -129,6 +146,19 @@ add_library(RenderSokol STATIC
sokol/SokolShaders.cpp
)

if(NOT PERIMETER_WINDOWS AND OPTION_SOKOL_API MATCHES "D3D11")
#This way dxvk will be fetched and compiled before RenderSokol
add_dependencies(RenderSokol dxvk)
target_include_directories(RenderSokol PRIVATE
"${DXVK_INCLUDE_DIRS}"
)

#Handle output lib file and also export it during install
set(DXVK_D3D11_LIB ${DXVK_BINARY_DIR}/src/d3d11/libdxvk_d3d11${CMAKE_SHARED_LIBRARY_SUFFIX})
set(RenderSokol_LINK_LIBS ${RenderSokol_LINK_LIBS} ${DXVK_D3D11_LIB})
install (FILES ${DXVK_D3D11_LIB} TYPE LIB)
endif()

IF (OPTION_PROCESS_SHADERS)
add_dependencies(RenderSokol sokol_shaders)
ENDIF()
Expand Down
19 changes: 19 additions & 0 deletions Source/Render/sokol/SokolD3D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef PERIMETER_SOKOLD3D_H
#define PERIMETER_SOKOLD3D_H

#include <d3d11.h>
#include <dxgi.h>

struct sokol_d3d_context {
HWND hwnd = nullptr;
DXGI_SWAP_CHAIN_DESC swap_chain_desc = {};
ID3D11Device* device = nullptr;
ID3D11DeviceContext* device_context = nullptr;
IDXGISwapChain* swap_chain = nullptr;
ID3D11Texture2D* render_target = nullptr;
ID3D11RenderTargetView* render_target_view = nullptr;
ID3D11Texture2D* depth_stencil_buffer = nullptr;
ID3D11DepthStencilView* depth_stencil_view = nullptr;
};

#endif //PERIMETER_SOKOLD3D_H
5 changes: 5 additions & 0 deletions Source/Render/sokol/SokolImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// This file imports sokol header with implementation def set
#include <string>
#include "xerrhand.h"
#if !defined(_WIN32) && defined(SOKOL_D3D11)
//These func's are not available in non windows with DXVK, make them macro
#define LoadLibraryA(a) (nullptr)
#define GetProcAddress(a,b) (nullptr)
#endif
#define SOKOL_IMPL
#define SOKOL_ASSERT xassert
#pragma GCC diagnostic push

Check warning on line 11 in Source/Render/sokol/SokolImpl.cpp

View workflow job for this annotation

GitHub Actions / Debug x86 64 bits

unknown pragma 'GCC'

Check warning on line 11 in Source/Render/sokol/SokolImpl.cpp

View workflow job for this annotation

GitHub Actions / Release x86 32 bits

unknown pragma 'GCC'

Check warning on line 11 in Source/Render/sokol/SokolImpl.cpp

View workflow job for this annotation

GitHub Actions / Release x86 64 bits

unknown pragma 'GCC'
Expand Down
Loading

0 comments on commit 84de2a3

Please sign in to comment.