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 Windows support #15

Closed
wants to merge 20 commits into from
Closed
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
71 changes: 39 additions & 32 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
OS := $(shell uname -s)
OS ?= $(shell uname -s)
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
ifeq ($(OS),Darwin)
CC := clang
SED := gsed
endif
ifeq ($(OS),Linux)
CC := gcc
endif
ifeq ($(OS),Windows_NT)
CC := cl
endif
ifndef CC
$(error Unsupported target OS '$(OS)')
endif
ifndef SED
SED := sed
endif
SOURCES := $(shell find source -name '*.d')
SOURCES := $(call rwildcard,source/,*.d)
LIBS_PATH := lib

.DEFAULT_GOAL := docs
Expand All @@ -21,45 +25,36 @@ all: docs
#################################################
# Subprojects
#################################################
# wgpu-native binaries ships with static libraries 🎉
ifeq ($(OS),Darwin)
LIB_WGPU_EXT := dylib
LIB_WGPU := libwgpu_native.a
endif
ifeq ($(OS),Linux)
LIB_WGPU_EXT := so
LIB_WGPU := libwgpu_native.a
endif
ifndef LIB_WGPU_EXT
$(error Unsupported target OS '$(OS)')
ifeq ($(OS),Windows_NT)
LIB_WGPU := wgpu_native.lib
endif
ifndef LIB_WGPU
$(error Unsupported target OS '$(OS)')
endif
LIB_WGPU_SOURCE := subprojects/wgpu/libwgpu.$(LIB_WGPU_EXT)
LIB_WGPU := lib/libwgpu.$(LIB_WGPU_EXT)
wgpu: $(LIB_WGPU)
LIB_WGPU_SOURCE := subprojects/wgpu
wgpu: lib/$(LIB_WGPU)
.PHONY: wgpu
$(LIB_WGPU_SOURCE): subprojects/wgpu.Makefile
@make -C subprojects -f wgpu.Makefile
$(LIB_WGPU): $(LIB_WGPU_SOURCE)
lib/$(LIB_WGPU): $(LIB_WGPU_SOURCE)
ifeq ($(OS),Windows_NT)
@if not exist lib mkdir lib
@xcopy $(subst /,\\,$(LIB_WGPU_SOURCE))\\$(LIB_WGPU) lib /y >NUL
else
@mkdir -p lib
@cp $(LIB_WGPU_SOURCE) lib/.
@cp $(LIB_WGPU_SOURCE)/$(LIB_WGPU) lib/.
endif

#################################################
# Examples
# Test Coverage
#################################################
example_utils_SOURCES := $(shell find examples/utils/source -name '*.d')
EXAMPLES := bin/cube bin/headless bin/triangle
examples: $(EXAMPLES)
.PHONY: examples

cube_SOURCES := $(example_utils_SOURCES) $(shell find examples/cube/source -name '*.d')
bin/cube: $(SOURCES) $(cube_SOURCES) examples/cube/dub.json
cd examples/cube && dub build

headless_SOURCES := $(shell find examples/headless/source -name '*.d')
bin/headless: $(SOURCES) $(headless_SOURCES) examples/headless/dub.json
cd examples/headless && dub build

triangle_SOURCES := $(shell find examples/triangle/source -name '*.d')
bin/triangle: $(SOURCES) $(triangle_SOURCES) examples/triangle/dub.json
cd examples/triangle && dub build

cover: $(SOURCES)
dub test --build=unittest-cov

Expand Down Expand Up @@ -88,18 +83,30 @@ docs/sitemap.xml: $(SOURCES)
@echo Done

docs: docs/sitemap.xml
ifeq ($(OS),Windows_NT)
$(error Build documentation on *nix!)
endif
.PHONY: docs

clean:
ifneq ($(OS),Windows_NT)
clean: clean-docs
rm -rf bin lib
else
$(error Build documentation on *nix!)
endif
dub clean
@rm -rf bin lib
@echo "Cleaning code coverage reports..."
rm -f -- *.lst
@rm -f -- *.lst
.PHONY: clean

DOCS_HTML := $(call rwildcard,docs/,*.html)
clean-docs:
ifeq ($(OS),Windows_NT)
$(error Build documentation on *nix!)
endif
@echo "Cleaning generated documentation..."
@rm -f docs.json
@rm -f docs/sitemap.xml docs/file_hashes.json
@rm -rf `find docs -name '*.html'`
@rm -rf $(DOCS_HTML)
.PHONY: clean-docs
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

D bindings to [wgpu-native](https://github.com/gfx-rs/wgpu-native) as an idiomatic wrapper around the library.

Targets wgpu-native [`v0.10.4.1`](https://github.com/gfx-rs/wgpu-native/releases/tag/v0.10.4.1).
Targets wgpu-native [`v0.17.0.2`](https://github.com/gfx-rs/wgpu-native/releases/tag/v0.17.0.2).

## Usage

Expand Down
34 changes: 16 additions & 18 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"license": "MIT",
"copyright": "Copyright © 2020-2023, Chance Snow",
"subPackages": [
"examples/enumerate",
"examples/headless",
"examples/triangle",
"examples/cube"
Expand All @@ -24,8 +25,8 @@
},
"systemDependencies-linux": "vulkan >= 1.1.0, patchelf",
"systemDependencies-osx": "metal >= 2",
"systemDependencies-windows": "DirectX >= 11",
"preGenerateCommands-posix": [
"systemDependencies-windows": "vulkan >= 1.1.0 or DirectX >= 11",
"preGenerateCommands": [
"make wgpu"
],
"dflags": [
Expand All @@ -34,33 +35,30 @@
],
"configurations": [
{
"name": "library",
"lflags-posix": [
"-L$PACKAGE_DIR/lib"
]
"name": "library"
},
{
"name": "unittest",
"lflags-posix": [
"-Llib"
],
"postBuildCommands-linux": [
"echo Fixing up libwgpu so path...",
"patchelf --set-rpath '$$ORIGIN' bin/wgpu-d-test-unittest"
],
"postBuildCommands-osx": [
"echo Fixing up libwgpu dylib path...",
"install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/x86_64-apple-darwin/debug/deps/libwgpu_native.dylib @executable_path/libwgpu.dylib bin/wgpu-d-test-unittest"
"install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/x86_64-apple-darwin/debug/deps/libwgpu_native.dylib @executable_path/libwgpu_native.dylib bin/wgpu-d-test-unittest"
]
}
],
"libs": [
"wgpu"
],
"copyFiles-linux": [
"lib/libwgpu.so"
],
"copyFiles-osx": [
"lib/libwgpu.dylib"
"lflags-posix": ["-L$WGPU_D_PACKAGE_DIR/lib"],
"lflags-windows": ["/LIBPATH:$WGPU_D_PACKAGE_DIR/subprojects/wgpu"],
"libs": ["wgpu_native"],
"libs-windows": [
"d3dcompiler",
"ws2_32",
"advapi32",
"user32",
"userenv",
"bcrypt",
"ntdll"
]
}
2 changes: 1 addition & 1 deletion examples/cube/dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
],
"postBuildCommands-osx": [
"echo Fixing up libwgpu dylib path...",
"install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/x86_64-apple-darwin/debug/deps/libwgpu_native.dylib @executable_path/../lib/libwgpu.dylib `find $PACKAGE_DIR/../../bin -name '*cube'`"
"install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/x86_64-apple-darwin/debug/deps/libwgpu_native.dylib @executable_path/../lib/libwgpu_native.dylib `find $PACKAGE_DIR/../../bin -name '*cube'`"
]
}
15 changes: 15 additions & 0 deletions examples/enumerate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.dub
docs.json
__dummy.html
docs/
/headless
headless.so
headless.dylib
headless.dll
headless.a
headless.lib
headless-test-*
*.exe
*.o
*.obj
*.lst
24 changes: 24 additions & 0 deletions examples/enumerate/dub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "enumerate",
"description": "Enumerate GPU adapters example",
"authors": [
"Chance Snow"
],
"license": "MIT",
"copyright": "Copyright © 2023, Chance Snow",
"targetType": "executable",
"targetPath": "../../bin",
"dependencies": {
"wgpu-d": {
"path": "../.."
}
},
"preBuildCommands-osx": [
"echo Fixing up libwgpu dylib path...",
"rm -f `find $PACKAGE_DIR/../../bin -name '*enumerate'`"
],
"postBuildCommands-osx": [
"echo Fixing up libwgpu dylib path...",
"install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/x86_64-apple-darwin/debug/deps/libwgpu_native.dylib @executable_path/../lib/libwgpu_native.dylib `find $PACKAGE_DIR/../../bin -name '*enumerate'`"
]
}
46 changes: 46 additions & 0 deletions examples/enumerate/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import std.conv : text, to;
import std.stdio;
import std.string : fromStringz;

import wgpu.api;

// Adapted from https://github.com/gfx-rs/wgpu-rs/blob/f6123e4fe89f74754093c07b476099623b76dd08/examples/capture/main.rs
void main() {
writeln("Enumerating Device Adapters Example");

auto instance = Instance.create();
assert(instance.id, "Could not create WebGPU instance.");

writefln("Preferred backend type? %s", cast(BackendType) instance.report.backendType);
auto adapters = instance.adapters;
writefln("Found %d suitable adapters:", adapters.length);
for (int i = 0; i < adapters.length; i++) {
auto adapter = adapters[i];
auto props = adapter.properties;
writefln("Adapter %d", i);
writefln("\tvendorID: %u", props.vendorID);
writefln("\tvendorName: %s", props.vendorName.fromStringz.to!string);
writefln("\tarchitecture: %s", props.architecture.fromStringz.to!string);
writefln("\tdeviceID: %u", props.deviceID);
writefln("\tname: %s", props.name.fromStringz.to!string);
writefln("\tdriverDescription: %s", props.driverDescription.fromStringz.to!string);
writefln("\tadapterType: %s", cast(AdapterType) props.adapterType);
writefln("\tbackendType: %s", cast(BackendType) props.backendType);
writeln();
adapter.destroy();
}

auto adapter = instance.requestAdapter(PowerPreference.lowPower, BackendType.d3d12 | BackendType.vulkan);
assert(adapter.ready, "Adapter instance was not initialized: Adapter status: " ~ adapter.status.text);
writefln("Adapter limits:\n%s", adapter.limits.toString);
writeln();

auto device = adapter.requestDevice(adapter.limits);
assert(device.ready, "Device is not ready: Device status: " ~ device.status.text);
writefln("Device limits:\n%s", device.limits.toString);
writeln();

device.destroy();
adapter.destroy();
instance.destroy();
}
2 changes: 1 addition & 1 deletion examples/headless/dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
],
"postBuildCommands-osx": [
"echo Fixing up libwgpu dylib path...",
"install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/x86_64-apple-darwin/debug/deps/libwgpu_native.dylib @executable_path/../lib/libwgpu.dylib `find $PACKAGE_DIR/../../bin -name '*headless'`"
"install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/x86_64-apple-darwin/debug/deps/libwgpu_native.dylib @executable_path/../lib/libwgpu_native.dylib `find $PACKAGE_DIR/../../bin -name '*headless'`"
]
}
13 changes: 7 additions & 6 deletions examples/headless/source/app.d
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import std.conv : text;
import std.stdio;

import wgpu.api;
Expand All @@ -6,14 +7,14 @@ import wgpu.api;
void main() {
writeln("Headless (windowless) Example");

auto adapter = Instance.requestAdapter(PowerPreference.lowPower);
assert(adapter.ready, "Adapter instance was not initialized");
writefln("Adapter properties: %s", adapter.properties);
writefln("Adapter limits: %s", adapter.limits);
auto instance = Instance.create();
assert(instance.id, "Could not create WebGPU instance.");

auto adapter = instance.requestAdapter(PowerPreference.lowPower);
assert(adapter.ready, "Adapter instance was not initialized: Adapter status: " ~ adapter.status.text);

auto device = adapter.requestDevice(adapter.limits);
assert(device.ready, "Device is not ready");
writefln("Device limits: %s", device.limits);
assert(device.ready, "Device is not ready: Device status: " ~ device.status.text);

const width = 400;
const height = 300;
Expand Down
8 changes: 5 additions & 3 deletions examples/triangle/dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@
"subConfigurations": {
"bindbc-glfw": "static"
},
"preGenerateCommands-posix": [
"preGenerateCommands": [
"make -C subprojects -f glfw.Makefile"
],
"preBuildCommands-osx": [
"rm -f `find $PACKAGE_DIR/../../bin -name '*triangle'`"
],
"postBuildCommands-osx": [
"echo Fixing up libwgpu dylib path...",
"install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/x86_64-apple-darwin/debug/deps/libwgpu_native.dylib @executable_path/lib/libwgpu.dylib `find $PACKAGE_DIR/../../bin -name '*triangle'`"
"install_name_tool -change /Users/runner/work/wgpu-native/wgpu-native/target/x86_64-apple-darwin/debug/deps/libwgpu_native.dylib @executable_path/lib/libwgpu_native.dylib `find $PACKAGE_DIR/../../bin -name '*triangle'`"
],
"libs": ["glfw3"],
"libs-linux": [
"X11"
],
"libs-windows": ["gdi32"],
"lflags-posix": ["-L$PACKAGE_DIR/subprojects/glfw/src"],
"lflags-osx": [
"-framework", "AppKit",
"-framework", "Metal",
"-framework", "QuartzCore"
]
],
"lflags-windows": ["/LIBPATH:$PACKAGE_DIR/subprojects/glfw/src"]
}
Loading
Loading