Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional path/directory argument for DXC/DXIL library #12

Merged
merged 3 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/file-ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn main() {

let args = vec![];

let dxc = Dxc::new().unwrap();
let dxc = Dxc::new(None).unwrap();

let intellisense = dxc.create_intellisense().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion examples/intellisense-tu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {

let args = vec![];

let dxc = Dxc::new().unwrap();
let dxc = Dxc::new(None).unwrap();

let intellisense = dxc.create_intellisense().unwrap();

Expand Down
15 changes: 10 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use crate::os::{SysFreeString, BSTR, HRESULT, LPSTR, LPWSTR, WCHAR};
use crate::wrapper::*;
use thiserror::Error;
Expand Down Expand Up @@ -80,7 +82,7 @@ pub enum HassleError {
ValidationError(String),
#[error("Failed to load library {filename:?}: {inner:?}")]
LoadLibraryError {
filename: String,
filename: PathBuf,
#[source]
inner: libloading::Error,
},
Expand All @@ -95,6 +97,8 @@ pub enum HassleError {
/// executable environment.
///
/// Specify -spirv as one of the `args` to compile to SPIR-V
/// `dxc_path` can point to a library directly or the directory containing the library,
/// in which case the appended filename depends on the platform.
pub fn compile_hlsl(
source_name: &str,
shader_text: &str,
Expand All @@ -103,7 +107,7 @@ pub fn compile_hlsl(
args: &[&str],
defines: &[(&str, Option<&str>)],
) -> Result<Vec<u8>, HassleError> {
let dxc = Dxc::new()?;
let dxc = Dxc::new(None)?;

let compiler = dxc.create_compiler()?;
let library = dxc.create_library()?;
Expand Down Expand Up @@ -143,10 +147,11 @@ pub fn compile_hlsl(
/// Helper function to validate a DXIL binary independant from the compilation process,
/// this function expects `dxcompiler.dll` and `dxil.dll` to be available in the current
/// execution environment.
/// `dxil.dll` is currently not available on Linux.
///
/// `dxil.dll` is only available on Windows.
pub fn validate_dxil(data: &[u8]) -> Result<Vec<u8>, HassleError> {
let dxc = Dxc::new()?;
let dxil = Dxil::new()?;
let dxc = Dxc::new(None)?;
let dxil = Dxil::new(None)?;

let validator = dxil.create_validator()?;
let library = dxc.create_library()?;
Expand Down
71 changes: 46 additions & 25 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use com_rs::ComPtr;
use libloading::{Library, Symbol};
use std::convert::Into;
use std::ffi::c_void;
use std::path::{Path, PathBuf};
use std::rc::Rc;

#[macro_export]
Expand Down Expand Up @@ -492,25 +493,35 @@ pub struct Dxc {
}

#[cfg(target_os = "windows")]
fn dxcompiler_lib_name() -> &'static str {
"dxcompiler.dll"
fn dxcompiler_lib_name() -> &'static Path {
Path::new("dxcompiler.dll")
}

#[cfg(target_os = "linux")]
fn dxcompiler_lib_name() -> &'static str {
"./libdxcompiler.so"
fn dxcompiler_lib_name() -> &'static Path {
Path::new("./libdxcompiler.so")
}

#[cfg(target_os = "macos")]
fn dxcompiler_lib_name() -> &'static str {
"./libdxcompiler.dynlib"
fn dxcompiler_lib_name() -> &'static Path {
Path::new("./libdxcompiler.dynlib")
}

impl Dxc {
pub fn new() -> Result<Self, HassleError> {
let lib_name = dxcompiler_lib_name();
let dxc_lib = Library::new(lib_name).map_err(|e| HassleError::LoadLibraryError {
filename: lib_name.to_string(),
/// `dxc_path` can point to a library directly or the directory containing the library,
/// in which case the appended filename depends on the platform.
pub fn new(lib_path: Option<PathBuf>) -> Result<Self, HassleError> {
let lib_path = if let Some(lib_path) = lib_path {
if lib_path.is_file() {
lib_path
} else {
lib_path.join(&dxcompiler_lib_name())
}
} else {
dxcompiler_lib_name().to_owned()
};
let dxc_lib = Library::new(&lib_path).map_err(|e| HassleError::LoadLibraryError {
filename: lib_path,
inner: e,
})?;

Expand Down Expand Up @@ -608,23 +619,33 @@ pub struct Dxil {
}

impl Dxil {
pub fn new() -> Result<Self, HassleError> {
#[cfg(not(windows))]
{
Err(HassleError::WindowsOnly(
"DXIL Signing is only supported on windows at the moment".to_string(),
))
}
#[cfg(not(windows))]
pub fn new(_: Option<PathBuf>) -> Result<Self, HassleError> {
Err(HassleError::WindowsOnly(
"DXIL Signing is only supported on Windows".to_string(),
))
}

/// `dxil_path` can point to a library directly or the directory containing the library,
/// in which case `dxil.dll` is appended.
#[cfg(windows)]
pub fn new(lib_path: Option<PathBuf>) -> Result<Self, HassleError> {
let lib_path = if let Some(lib_path) = lib_path {
if lib_path.is_file() {
lib_path
} else {
lib_path.join("dxil.dll")
}
} else {
PathBuf::from("dxil.dll")
};

#[cfg(windows)]
{
let dxil_lib = Library::new("dxil.dll").map_err(|e| HassleError::LoadLibraryError {
filename: "dxil".to_string(),
inner: e,
})?;
let dxil_lib = Library::new(&lib_path).map_err(|e| HassleError::LoadLibraryError {
filename: lib_path.to_owned(),
inner: e,
})?;

Ok(Self { dxil_lib })
}
Ok(Self { dxil_lib })
}

fn get_dxc_create_instance(&self) -> Result<Symbol<DxcCreateInstanceProc>, HassleError> {
Expand Down