Skip to content

Commit

Permalink
shader-slang: init at 2024.14.6
Browse files Browse the repository at this point in the history
  • Loading branch information
niklaskorz committed Dec 1, 2024
1 parent e16d20f commit 2d17782
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
77 changes: 77 additions & 0 deletions pkgs/by-name/sh/shader-slang/find-packages.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index dc281211..c36b9bcb 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -154,6 +154,8 @@ advanced_option(
"Build using system unordered dense"
OFF
)
+advanced_option(SLANG_USE_SYSTEM_SPIRV_TOOLS "Build using system SPIR-V tools library" OFF)
+advanced_option(SLANG_USE_SYSTEM_GLSLANG "Build using system glslang library" OFF)

option(
SLANG_SPIRV_HEADERS_INCLUDE_DIR
@@ -289,6 +291,34 @@ if(${SLANG_USE_SYSTEM_UNORDERED_DENSE})
find_package(unordered_dense CONFIG QUIET)
endif()

+if(${SLANG_USE_SYSTEM_MINIZ})
+ find_package(miniz REQUIRED)
+ add_library(miniz ALIAS miniz::miniz)
+endif()
+
+if(${SLANG_USE_SYSTEM_LZ4})
+ find_package(lz4 REQUIRED)
+ add_library(lz4_static ALIAS LZ4::lz4)
+endif()
+
+if(${SLANG_USE_SYSTEM_VULKAN_HEADERS})
+ find_package(VulkanHeaders REQUIRED)
+endif()
+
+if(${SLANG_USE_SYSTEM_SPIRV_HEADERS})
+ find_package(SPIRV-Headers REQUIRED)
+ add_library(SPIRV-Headers ALIAS SPIRV-Headers::SPIRV-Headers)
+endif()
+
+if(${SLANG_USE_SYSTEM_SPIRV_TOOLS})
+ find_package(SPIRV-Tools REQUIRED)
+endif()
+
+if(${SLANG_USE_SYSTEM_GLSLANG})
+ find_package(glslang REQUIRED)
+ add_library(glslang ALIAS glslang::glslang)
+endif()
+
add_subdirectory(external)

# webgpu_dawn is only available as a fetched shared library, since Dawn's nested source
diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt
index 43105a5f..8b9c0f14 100644
--- a/external/CMakeLists.txt
+++ b/external/CMakeLists.txt
@@ -73,19 +73,24 @@ if(NOT ${SLANG_USE_SYSTEM_SPIRV_HEADERS})
endif()

if(SLANG_ENABLE_SLANG_GLSLANG)
+if(NOT ${SLANG_USE_SYSTEM_SPIRV_TOOLS})
# SPIRV-Tools
set(SPIRV_TOOLS_BUILD_STATIC ON)
set(SPIRV_WERROR OFF)
set(SPIRV_HEADER_DIR "${CMAKE_CURRENT_LIST_DIR}/spirv-headers/")
set(SPIRV_SKIP_TESTS ON)
add_subdirectory(spirv-tools EXCLUDE_FROM_ALL ${system})
+endif()

+if(NOT ${SLANG_USE_SYSTEM_GLSLANG})
# glslang
set(SKIP_GLSLANG_INSTALL ON)
set(ENABLE_OPT ON)
set(ENABLE_PCH OFF)
+ set(ALLOW_EXTERNAL_SPIRV_TOOLS ${SLANG_USE_SYSTEM_SPIRV_TOOLS})
add_subdirectory(glslang EXCLUDE_FROM_ALL ${system})
endif()
+endif()

# imgui
add_library(imgui INTERFACE)
113 changes: 113 additions & 0 deletions pkgs/by-name/sh/shader-slang/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
lib,
stdenv,
fetchFromGitHub,
cmake,
ninja,
python3,
miniz,
lz4,
libxml2,
libX11,
vulkan-headers,
spirv-headers,
spirv-tools,
llvmPackages_13,

# Mayb be used for compiling shaders to CPU targets, see:
# https://github.com/shader-slang/slang/blob/master/docs/cpu-target.md
# If `withLLVM` is disabled, slang will fall back to the C++ compiler found
# in the environment, if one exists.
# Enabling this increases the package's size from ~20mb to ~100mb.
withLLVM ? false,
# May be enabled by default once we can use nixpkgs' glslang version.
# Effect on package size is minimal, even with the vendored glslang version.
withGlslang ? false,
}:

stdenv.mkDerivation (finalAttrs: {
pname = "shader-slang";
version = "2024.14.6";

src = fetchFromGitHub {
owner = "shader-slang";
repo = "slang";
rev = "v${finalAttrs.version}";
hash = "sha256-q/FR7CA3FddbHBmINOqQqfmOhlusswv3femKFax2AnM=";
fetchSubmodules = true;
};

patches = [ ./find-packages.patch ];

outputs = [
"out"
"dev"
"doc"
];

nativeBuildInputs = [
cmake
ninja
python3
];

buildInputs =
[
miniz
lz4
libxml2
vulkan-headers
spirv-headers
]
++ (lib.optionals stdenv.hostPlatform.isLinux [
libX11
])
++ (lib.optionals withLLVM [
llvmPackages_13.llvm
llvmPackages_13.libclang
])
++ (lib.optionals withGlslang [
# SPIRV-tools is included in glslang.
# Replace with glslang once Slang is compatible with version 15.
# Until then, we use the vendored version of glslang and make it an optional
# feature behind the `withGlslang` parameter.
spirv-tools
]);

# Required for spaces in cmakeFlags, see https://github.com/NixOS/nixpkgs/issues/114044
__structuredAttrs = true;

cmakeFlags =
[
"-GNinja Multi-Config"
# The cmake setup hook only specifies `-DCMAKE_BUILD_TYPE=Release`,
# which does nothing for "Ninja Multi-Config".
"-DCMAKE_CONFIGURATION_TYPES=Release"
"-DSLANG_VERSION_FULL=v${finalAttrs.version}-nixpkgs"
"-DSLANG_USE_SYSTEM_MINIZ=ON"
"-DSLANG_USE_SYSTEM_LZ4=ON"
#"-DSLANG_USE_SYSTEM_VULKAN_HEADERS=ON"
"-DSLANG_USE_SYSTEM_SPIRV_HEADERS=ON"
"-DSLANG_SPIRV_HEADERS_INCLUDE_DIR=${spirv-headers}/include"
"-DSLANG_SLANG_LLVM_FLAVOR=${if withLLVM then "USE_SYSTEM_LLVM" else "DISABLE"}"
]
++ (lib.optionals withGlslang [
"-DSLANG_USE_SYSTEM_SPIRV_TOOLS=ON"
# Slang is not compatible with glslang 15 yet.
#"-DSLANG_USE_SYSTEM_GLSLANG=ON"
])
++ lib.optional (!withGlslang) "-DSLANG_ENABLE_SLANG_GLSLANG=OFF";

preFixup = ''
rm $out/include/.clang-format
'';

meta = {
description = "A shading language that makes it easier to build and maintain large shader codebases in a modular and extensible fashion";
homepage = "https://github.com/shader-slang/slang";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [ niklaskorz ];
mainProgram = "slang";
platforms = lib.platforms.all;
};
})

0 comments on commit 2d17782

Please sign in to comment.