Skip to content

Commit

Permalink
add wgpu backend (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
skallweitNV committed Sep 23, 2024
1 parent 2115c78 commit 3adf0dc
Show file tree
Hide file tree
Showing 50 changed files with 6,183 additions and 8 deletions.
59 changes: 56 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(SLANG_RHI_HAS_VULKAN ON)
set(SLANG_RHI_HAS_METAL OFF)
set(SLANG_RHI_HAS_CUDA ON)
set(SLANG_RHI_HAS_WGPU ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(SLANG_RHI_HAS_D3D11 OFF)
set(SLANG_RHI_HAS_D3D12 OFF)
set(SLANG_RHI_HAS_VULKAN ON)
set(SLANG_RHI_HAS_METAL OFF)
set(SLANG_RHI_HAS_CUDA ON)
set(SLANG_RHI_HAS_WGPU ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(SLANG_RHI_HAS_D3D11 OFF)
set(SLANG_RHI_HAS_D3D12 OFF)
set(SLANG_RHI_HAS_VULKAN ON)
set(SLANG_RHI_HAS_METAL ON)
set(SLANG_RHI_HAS_CUDA OFF)
set(SLANG_RHI_HAS_WGPU ON)
endif()

# Backend options
Expand All @@ -44,12 +46,13 @@ cmake_dependent_option(SLANG_RHI_ENABLE_D3D12 "Enable D3D12 backend" ON "SLANG_R
cmake_dependent_option(SLANG_RHI_ENABLE_VULKAN "Enable Vulkan backend" ON "SLANG_RHI_HAS_VULKAN" OFF)
cmake_dependent_option(SLANG_RHI_ENABLE_METAL "Enable Metal backend" ON "SLANG_RHI_HAS_METAL" OFF)
cmake_dependent_option(SLANG_RHI_ENABLE_CUDA "Enable CUDA backend" ON "SLANG_RHI_HAS_CUDA" OFF)
cmake_dependent_option(SLANG_RHI_ENABLE_WGPU "Enable WebGPU backend" ON "SLANG_RHI_HAS_WGPU" OFF)

# If this is the master project, fetch binary dependencies.
if(SLANG_RHI_MASTER_PROJECT)
include(FetchPackage)
# Fetch slang
set(SLANG_VERSION "2024.10")
set(SLANG_VERSION "2024.11.1")
set(SLANG_URL "https://github.com/shader-slang/slang/releases/download/v${SLANG_VERSION}/slang-${SLANG_VERSION}")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(SLANG_URL "${SLANG_URL}-windows-x86_64.zip")
Expand Down Expand Up @@ -81,6 +84,23 @@ if(SLANG_RHI_MASTER_PROJECT)
# set(AGILITY_SDK_VERSION "1.611.2")
# FetchPackage(agility_sdk URL "https://www.nuget.org/api/v2/package/Microsoft.Direct3D.D3D12/${AGILITY_SDK_VERSION}")
endif()

# Fetch Google Dawn
if(SLANG_RHI_ENABLE_WGPU)
set(DAWN_VERSION "129.0.6728")
set(DAWN_URL "https://github.com/skallweitNV/webgpu-dawn-binaries/releases/download/v${DAWN_VERSION}.0/webgpu-dawn-${DAWN_VERSION}")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(DAWN_URL "${DAWN_URL}-win64.zip")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(DAWN_URL "${DAWN_URL}-Linux.zip")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(DAWN_URL "${DAWN_URL}-Darwin.zip")
endif()
FetchPackage(dawn URL ${DAWN_URL})
set(SLANG_RHI_DAWN_INCLUDE_DIR ${dawn_SOURCE_DIR}/include)
set(SLANG_RHI_DAWN_LIB_DIR ${dawn_SOURCE_DIR}/lib)
set(SLANG_RHI_DAWN_BIN_DIR ${dawn_SOURCE_DIR}/bin)
endif()
endif()

set(SLANG_RHI_SLANG_INCLUDE_DIR ${SLANG_RHI_SLANG_INCLUDE_DIR} CACHE STRING "Slang include directory")
Expand Down Expand Up @@ -137,11 +157,39 @@ if(SLANG_RHI_MASTER_PROJECT)
COMMAND ${CMAKE_COMMAND} -E copy ${SLANG_RHI_SLANG_BINARY_DIR}/bin/slang-glslang.dll ${SLANG_RHI_OUTPUT_DIRECTORY}/
COMMAND ${CMAKE_COMMAND} -E copy ${SLANG_RHI_SLANG_BINARY_DIR}/bin/slang-llvm.dll ${SLANG_RHI_OUTPUT_DIRECTORY}/
COMMAND ${CMAKE_COMMAND} -E copy ${SLANG_RHI_SLANG_BINARY_DIR}/bin/slang-rt.dll ${SLANG_RHI_OUTPUT_DIRECTORY}/
COMMENT "Copying DLLs"
COMMENT "Copy Slang DLLs"
)
add_custom_target(slang-rhi-copy-binaries ALL DEPENDS ${SLANG_RHI_OUTPUT_DIRECTORY}/slang.dll)
add_dependencies(slang slang-rhi-copy-binaries)
endif()

if(SLANG_RHI_ENABLE_WGPU)
add_library(dawn SHARED IMPORTED GLOBAL)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set_target_properties(dawn PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${SLANG_RHI_DAWN_INCLUDE_DIR}
IMPORTED_IMPLIB ${SLANG_RHI_DAWN_LIB_DIR}/dawn.lib
IMPORTED_LOCATION ${SLANG_RHI_DAWN_BIN_DIR}/dawn.dll
)
# hacky way to copy the dawn.dll to the output directory
add_custom_command(
OUTPUT ${SLANG_RHI_OUTPUT_DIRECTORY}/slang.dll
COMMAND ${CMAKE_COMMAND} -E copy ${SLANG_RHI_DAWN_BIN_DIR}/dawn.dll ${SLANG_RHI_OUTPUT_DIRECTORY}/
APPEND
)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_target_properties(dawn PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${SLANG_RHI_DAWN_INCLUDE_DIR}
IMPORTED_LOCATION ${SLANG_RHI_DAWN_LIB_DIR}64/libdawn.so
)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set_target_properties(dawn PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${SLANG_RHI_DAWN_INCLUDE_DIR}
IMPORTED_LOCATION ${SLANG_RHI_DAWN_LIB_DIR}/libdawn.dylib
)
endif()
target_link_libraries(slang-rhi PUBLIC dawn)
endif()
else()
target_include_directories(slang-rhi PUBLIC ${SLANG_RHI_SLANG_INCLUDE_DIR})
endif()
Expand Down Expand Up @@ -191,6 +239,10 @@ if(SLANG_RHI_ENABLE_CUDA)
file(GLOB CUDA_SOURCES src/cuda/*.cpp)
target_sources(slang-rhi PRIVATE ${CUDA_SOURCES})
endif()
if(SLANG_RHI_ENABLE_WGPU)
file(GLOB WGPU_SOURCES src/wgpu/*.cpp)
target_sources(slang-rhi PRIVATE ${WGPU_SOURCES})
endif()

target_include_directories(slang-rhi PUBLIC include)
target_include_directories(slang-rhi PRIVATE src)
Expand All @@ -201,6 +253,7 @@ target_compile_definitions(slang-rhi
SLANG_RHI_ENABLE_VULKAN=$<BOOL:${SLANG_RHI_ENABLE_VULKAN}>
SLANG_RHI_ENABLE_METAL=$<BOOL:${SLANG_RHI_ENABLE_METAL}>
SLANG_RHI_ENABLE_CUDA=$<BOOL:${SLANG_RHI_ENABLE_CUDA}>
SLANG_RHI_ENABLE_WGPU=$<BOOL:${SLANG_RHI_ENABLE_WGPU}>
$<$<PLATFORM_ID:Windows>:NOMINMAX> # do not define min/max macros
$<$<PLATFORM_ID:Windows>:UNICODE> # force character map to unicode
)
Expand Down
16 changes: 14 additions & 2 deletions include/slang-rhi.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ enum class DeviceType
Metal,
CPU,
CUDA,
WGPU,
};

// TODO: Is this actually a flag when there are no bit fields?
Expand Down Expand Up @@ -399,6 +400,16 @@ enum class NativeHandleType
CUdevice = 0x00050001,
CUdeviceptr = 0x00050002,
CUtexObject = 0x00050003,

WGPUDevice = 0x00060001,
WGPUBuffer = 0x00060002,
WGPUTexture = 0x00060003,
WGPUSampler = 0x00060004,
WGPURenderPipeline = 0x00060005,
WGPUComputePipeline = 0x00060006,
WGPUQueue = 0x00060007,
WGPUCommandBuffer = 0x00060008,
WGPUTextureView = 0x00060009,
};

struct NativeHandle
Expand Down Expand Up @@ -767,8 +778,8 @@ struct SamplerDesc
uint32_t maxAnisotropy = 1;
ComparisonFunc comparisonFunc = ComparisonFunc::Never;
float borderColor[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float minLOD = -FLT_MAX;
float maxLOD = FLT_MAX;
float minLOD = 0.0f;
float maxLOD = 1000.0f;

const char* label = nullptr;
};
Expand Down Expand Up @@ -1871,6 +1882,7 @@ class ICommandQueue : public ISlangUnknown
IFence* fenceToSignal,
uint64_t newFenceValue
) = 0;

inline void executeCommandBuffer(
ICommandBuffer* commandBuffer,
IFence* fenceToSignal = nullptr,
Expand Down
12 changes: 11 additions & 1 deletion src/rhi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Result SLANG_MCALL createVKDevice(const IDevice::Desc* desc, IDevice** outDevice
Result SLANG_MCALL createMetalDevice(const IDevice::Desc* desc, IDevice** outDevice);
Result SLANG_MCALL createCUDADevice(const IDevice::Desc* desc, IDevice** outDevice);
Result SLANG_MCALL createCPUDevice(const IDevice::Desc* desc, IDevice** outDevice);
Result SLANG_MCALL createWGPUDevice(const IDevice::Desc* desc, IDevice** outDevice);

Result SLANG_MCALL getD3D11Adapters(std::vector<AdapterInfo>& outAdapters);
Result SLANG_MCALL getD3D12Adapters(std::vector<AdapterInfo>& outAdapters);
Expand Down Expand Up @@ -329,7 +330,12 @@ extern "C"
{
return createCPUDevice(desc, outDevice);
}
break;
#if SLANG_RHI_ENABLE_WGPU
case DeviceType::WGPU:
{
return createWGPUDevice(desc, outDevice);
}
#endif

default:
return SLANG_FAIL;
Expand Down Expand Up @@ -390,6 +396,8 @@ extern "C"
return "CPU";
case DeviceType::CUDA:
return "CUDA";
case DeviceType::WGPU:
return "WGPU";
default:
return "?";
}
Expand All @@ -415,6 +423,8 @@ extern "C"
#else
return false;
#endif
case DeviceType::WGPU:
return SLANG_RHI_ENABLE_WGPU;
default:
return false;
}
Expand Down
31 changes: 31 additions & 0 deletions src/wgpu/wgpu-api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "wgpu-api.h"

namespace rhi::wgpu {

API::~API()
{
if (m_module)
{
unloadSharedLibrary(m_module);
}
}

Result API::init()
{
#if SLANG_WINDOWS_FAMILY
SLANG_RETURN_ON_FAIL(loadSharedLibrary("dawn.dll", m_module));
#elif SLANG_LINUX_FAMILY
SLANG_RETURN_ON_FAIL(loadSharedLibrary("libdawn.so", m_module));
#elif SLANG_APPLE_FAMILY
SLANG_RETURN_ON_FAIL(loadSharedLibrary("libdawn.dylib", m_module));
#else
return SLANG_FAIL;
#endif

#define LOAD_PROC(name) wgpu##name = (WGPUProc##name)findSymbolAddressByName(m_module, "wgpu" #name);
SLANG_RHI_WGPU_PROCS(LOAD_PROC)
#undef LOAD_PROC
return SLANG_OK;
}

} // namespace rhi::wgpu
Loading

0 comments on commit 3adf0dc

Please sign in to comment.