Skip to content

Commit

Permalink
Merge pull request #27 from ambergorzynski/dawn_update
Browse files Browse the repository at this point in the history
Dawn update
  • Loading branch information
hasali19 committed Jan 12, 2024
2 parents af1b355 + 8c7d760 commit e8b44a8
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .github/dependency_versions.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dawn: autobuild-2023-01-31-12-46
dawn: autobuild-2024-01-04-21-29
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ jobs:
matrix:
target:
- win64
- macos64
# - macos64
- linux64
include:
- target: win64
os: ubuntu-latest
rust-target: x86_64-pc-windows-msvc
exe_suffix: .exe
- target: macos64
os: macos-latest
rust-target: x86_64-apple-darwin
exe_suffix: ""
# - target: macos64
# os: macos-latest
# rust-target: x86_64-apple-darwin
# exe_suffix: ""
- target: linux64
os: ubuntu-latest
rust-target: x86_64-unknown-linux-gnu
Expand Down
20 changes: 11 additions & 9 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def parse_args():
parser.add_argument("--install-prefix")
parser.add_argument("--no-reducer", action="store_true")
parser.add_argument("--no-harness", action="store_true")
parser.add_argument("--dawn-path", default="external/dawn")
return parser.parse_args()


Expand All @@ -33,6 +34,8 @@ def get_cargo_host_target():
build_target = args.target if args.target is not None else host_target
is_cross = args.target is not None and host_target != args.target

dawn_src_dir = Path(args.dawn_path)
dawn_build_dir = Path(f"build/dawn/{build_target}")

def get_commit(git_dir):
output = subprocess.check_output(["git", "--git-dir", git_dir, "rev-parse", "HEAD"])
Expand Down Expand Up @@ -84,30 +87,28 @@ def cargo_build(package, target=None, cwd=None, features=[]):
cmd += ["--target", target]
if len(features) > 0:
cmd += ["--features", ",".join(features)]

cmd += ["--config",f'env.DAWN_SRC_DIR="{dawn_src_dir}"']
print(f">> {' '.join(cmd)}")
subprocess.run(cmd, cwd=cwd).check_returncode()


def bootstrap_gclient_config():
gclient_config = Path("external/dawn/.gclient")
gclient_config_tmpl = Path("external/dawn/scripts/standalone.gclient")
gclient_config = Path(f'{dawn_src_dir}/.gclient')
gclient_config_tmpl = Path(f'{dawn_src_dir}/scripts/standalone.gclient')
if not gclient_config.exists():
shutil.copyfile(gclient_config_tmpl, gclient_config)


def gclient_sync():
dawn_commit = get_commit("external/dawn/.git")
dawn_commit = get_commit(f'{dawn_src_dir}/.git')
print(f'dawn commit is: {dawn_commit}')
gclient_sync_hash = read_gclient_sync_hash()
if gclient_sync_hash != dawn_commit:
print("> dawn commit has changed, rerunning gclient sync")
subprocess.run(["gclient", "sync"], cwd="external/dawn").check_returncode()
subprocess.run(["gclient", "sync"], cwd=dawn_src_dir).check_returncode()
write_gclient_sync_hash(dawn_commit)


dawn_src_dir = Path("external/dawn")
dawn_build_dir = Path(f"build/dawn/{build_target}")


def dawn_gen_cmake():
if is_cross and build_target != "x86_64-pc-windows-msvc":
print(f"cannot build dawn for target '{build_target}' (host={host_target})")
Expand Down Expand Up @@ -197,3 +198,4 @@ def build_harness():

for task in tasks:
task()

8 changes: 1 addition & 7 deletions crates/ast/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub struct Writer {

#[derive(Default)]
pub struct Options {
pub concise_stage_attrs: bool,
pub module_scope_constants: bool,
}

Expand Down Expand Up @@ -98,12 +97,7 @@ impl Writer {
for attr in &func.attrs {
match attr {
FnAttr::Stage(stage) => {
// TODO: Tint doesn't currently support the new stage attribute syntax - update when implemented
if self.options.concise_stage_attrs {
writeln!(f, "@{stage}")?;
} else {
writeln!(f, "@stage({stage})")?;
}
writeln!(f, "@{stage}")?;
}
_ => self.write_attr(f, attr)?,
}
Expand Down
40 changes: 12 additions & 28 deletions crates/dawn/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

Expand All @@ -25,55 +26,38 @@ fn main() -> Result<(), Box<dyn Error>> {

println!("cargo:rustc-link-search=native={}", dawn_lib_dir.display());

let common_libs = [
"absl_base",
"absl_int128",
"absl_log_severity",
"absl_raw_logging_internal",
"absl_spinlock_wait",
"absl_str_format_internal",
"absl_strings_internal",
"absl_strings",
"absl_throw_delegate",
"dawn_common",
"dawn_headers",
"dawn_native",
"dawn_platform",
"dawn_proc",
"dawncpp_headers",
"SPIRV-Tools-opt",
"SPIRV-Tools",
"tint_diagnostic_utils",
"tint",
];
let common_libs = fs::read_dir(dawn_lib_dir)?
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|e| e.is_file())
.collect::<Vec<_>>();

let target_os = env::var("CARGO_CFG_TARGET_OS")?;
let target_family = env::var("CARGO_CFG_TARGET_FAMILY")?;

for lib in common_libs {
let lib_name = lib.file_stem().unwrap().to_str().unwrap();
let lib_name = if target_family == "windows" {
format!("{lib}.lib")
lib_name
} else if target_family == "unix" {
format!("lib{lib}.a")
&lib_name[3..]
} else {
panic!("unsupported target_family '{target_family}'");
};

let path = dawn_lib_dir.join(lib_name);

if target_os == "linux"
&& !Command::new("ar")
.arg("d")
.arg(&path)
.arg(&lib)
.arg("Placeholder.cpp.o")
.status()?
.success()
{
panic!("ar command failed");
}

println!("cargo:rerun-if-changed={}", path.display());
println!("cargo:rustc-link-lib=static={lib}");
println!("cargo:rerun-if-changed={}", lib.display());
println!("cargo:rustc-link-lib=static={lib_name}");
}

// Additional platform-specific libraries we need to link
Expand Down
4 changes: 2 additions & 2 deletions crates/dawn/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Device {
sType: WGPUSType_WGPUSType_ShaderModuleWGSLDescriptor,
..zeroed()
},
source: source.as_ptr() as _,
code: source.as_ptr() as _,
};

let descriptor = WGPUShaderModuleDescriptor {
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Device {

pub fn create_buffer(
&self,
mapped: bool,
mapped: crate::webgpu::WGPUBool,
size: usize,
usage: DeviceBufferUsage,
) -> DeviceBuffer {
Expand Down
25 changes: 14 additions & 11 deletions crates/dawn/src/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,35 @@
#include <dawn/webgpu_cpp.h>
#include <dawn/native/DawnNative.h>

extern "C" dawn_native::Instance* new_instance() {
extern "C" dawn::native::Instance* new_instance() {
// Initialize WebGPU proc table
dawnProcSetProcs(&dawn_native::GetProcs());
dawnProcSetProcs(&dawn::native::GetProcs());

auto instance = new dawn_native::Instance;
auto instance = new dawn::native::Instance;

// This makes things slow
// instance->EnableBackendValidation(true);
// instance->SetBackendValidationLevel(dawn_native::BackendValidationLevel::Full);
// instance->SetBackendValidationLevel(dawn::native::BackendValidationLevel::Full);

instance->DiscoverDefaultAdapters();
WGPURequestAdapterOptions options = {};
instance->EnumerateAdapters(&options);

return instance;
}

extern "C" void delete_instance(dawn_native::Instance* instance) {
extern "C" void delete_instance(dawn::native::Instance* instance) {
delete instance;
}

extern "C" void enumerate_adapters(
const dawn_native::Instance* instance,
const dawn::native::Instance* instance,
void(*callback)(const WGPUAdapterProperties*, void*),
void* userdata
) {
if (callback == nullptr) return;

auto adapters = instance->GetAdapters();
WGPURequestAdapterOptions options = {};
auto adapters = instance->EnumerateAdapters(&options);

for (auto& adapter : adapters) {
WGPUAdapterProperties properties = {};
Expand All @@ -42,13 +44,14 @@ extern "C" void enumerate_adapters(
}

extern "C" WGPUDevice create_device(
const dawn_native::Instance* instance,
const dawn::native::Instance* instance,
WGPUBackendType backendType,
uint32_t deviceID
) {
auto adapters = instance->GetAdapters();
WGPURequestAdapterOptions options = {};
auto adapters = instance->EnumerateAdapters(&options);

dawn_native::Adapter *selectedAdapter = nullptr;
dawn::native::Adapter *selectedAdapter = nullptr;
for (auto& adapter : adapters) {
WGPUAdapterProperties properties = {};
adapter.GetProperties(&properties);
Expand Down
12 changes: 8 additions & 4 deletions crates/harness/src/dawn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use color_eyre::eyre::eyre;
use dawn::webgpu::{
WGPUBackendType_WGPUBackendType_D3D12, WGPUBackendType_WGPUBackendType_Metal,
WGPUBackendType_WGPUBackendType_Vulkan,
WGPUBackendType_WGPUBackendType_Vulkan, WGPUBool,
};
use dawn::*;
use reflection::{PipelineDescription, ResourceKind};
Expand Down Expand Up @@ -63,18 +63,20 @@ pub async fn run(

let mut buffer_sets = vec![];

let mapped: WGPUBool = 0;

for resource in &meta.resources {
let size = resource.size as usize;
match resource.kind {
ResourceKind::StorageBuffer => {
let storage = device.create_buffer(
false,
mapped,
size,
DeviceBufferUsage::STORAGE | DeviceBufferUsage::COPY_SRC,
);

let read = device.create_buffer(
false,
mapped,
size,
DeviceBufferUsage::COPY_DST | DeviceBufferUsage::MAP_READ,
);
Expand All @@ -87,7 +89,9 @@ pub async fn run(
});
}
ResourceKind::UniformBuffer => {
let mut buffer = device.create_buffer(true, size, DeviceBufferUsage::UNIFORM);
let mapped: WGPUBool = 1;

let mut buffer = device.create_buffer(mapped, size, DeviceBufferUsage::UNIFORM);

if let Some(init) = resource.init.as_deref() {
buffer.get_mapped_range(size).copy_from_slice(init);
Expand Down
1 change: 0 additions & 1 deletion crates/harness/src/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub async fn run(
let (device, queue) = adapter.request_device(&device_descriptor, None).await?;

let preprocessor_opts = preprocessor::Options {
concise_stage_attrs: true,
module_scope_constants: false,
};

Expand Down
6 changes: 1 addition & 5 deletions crates/preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
pub use ast::writer::Options;

pub fn preprocess(options: Options, mut shader: String) -> String {
if options.concise_stage_attrs {
shader = shader.replace("@stage(compute)", "@compute");
}

pub fn preprocess(options: Options, shader: String) -> String {
if options.module_scope_constants {
panic!("module scope constants are not supported yet");
}
Expand Down
20 changes: 13 additions & 7 deletions crates/tint/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};

fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -23,23 +24,28 @@ fn main() -> Result<(), Box<dyn Error>> {

println!("cargo:rustc-link-search=native={}", dawn_lib_dir.display());

let libs = ["tint_diagnostic_utils", "tint"];
let libs = fs::read_dir(dawn_lib_dir)?
.filter_map(|e| e.ok())
.map(|e| e.path())
.filter(|e| e.is_file())
.filter(|e| e.to_str().unwrap().contains("tint"))
.collect::<Vec<_>>();

let target_family = env::var("CARGO_CFG_TARGET_FAMILY")?;

for lib in libs {
let lib_name = lib.file_stem().unwrap().to_str().unwrap();

let lib_name = if target_family == "windows" {
format!("{lib}.lib")
lib_name
} else if target_family == "unix" {
format!("lib{lib}.a")
&lib_name[3..]
} else {
panic!("unsupported target_family '{target_family}'");
};

let path = dawn_lib_dir.join(lib_name);

println!("cargo:rerun-if-changed={}", path.display());
println!("cargo:rustc-link-lib=static={lib}");
println!("cargo:rerun-if-changed={}", lib.display());
println!("cargo:rustc-link-lib=static={}", lib_name);
}

let mut build = cxx_build::bridge("src/lib.rs");
Expand Down

0 comments on commit e8b44a8

Please sign in to comment.