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

Added new build options and shader file inclusion overrides. #3

Closed
wants to merge 24 commits into from

Conversation

sinnwrig
Copy link
Contributor

@sinnwrig sinnwrig commented Apr 28, 2024

Description

Multiple new options have been added to the build system in order to build mach-dxcompiler for more various use cases, and an addition to the API has been made in order to override default DXC file inclusion behavior.

Additional Build Features

Four new build options have been added to build.zig:

  • -Dshared Build machdxcompiler as a shared library alongside the static library.
  • -Dspirv Build SPIR-V codegen support for machdxcompiler.
  • -Dskip_executables Skip building machdxcompiler executable (Implicitly skips tests)
  • -Dskip_tests Skip building machdxcompiler-tests executable

API Changes

  • machDxcCompile function signature has been changed, accepting a MachDxcCompileOptions struct.

    • MachDxcCompileOptions struct typedef has been added, containing the following fields:
      • char const* code` (UTF-8)
      • size_t code_len
      • char const* const* args (UTF-8)
      • size_t args_len
      • MachDxcIncludeCallbacks* include_callbacks (null falls back to default behavior)
  • The ability to override the default DXC file includer has been added to the C API.

    • MachDxcIncludeCallbacks struct typedef has been added, containing the following fields:

      • MachDxcIncludeFunc* include_func
      • MachDxcFreeIncludeFunc* free_func
      • void* context
    • MachDxcIncludeResult struct typedef has been added, containing the following fields:

      • const char* header_data (UTF-8)
      • size_t header_length
    • MachDxcIncludeFunc function type definition has been added.

      • Receives void* as context.
      • Receives const char* as header name in UTF-8.
      • Returns MachDxcIncludeResult*.
    • MachDxcFreeIncludeFunc function type definition has been added.

      • Receives void* as context.
      • Receives MachDxcIncludeResult* to free.
      • Returns int exit code.

Implementation details

  • Shared library building links the static library when compiling a minimal .cpp file.

  • Executable and test generation are now behind compilation flags.

  • SPIR-V codegen support builds tools/clang/lib/SPIRV sources in addition to core DXC sources, and links to an external dependency on hexops/spirv-tools.

  • DXC inclusion behavior has been routed through the provided function pointers using a minimal class inheriting from IDxcIncludeHandler.

Known Issues

  • There appears to be no issues with SPIR-V compilation or shared library support. However, certain combinations of input arguments, such as mixing '-Zi' with '-spirv' may cause segmentation faults.

@sinnwrig sinnwrig changed the title Added shared library build option. Added shared library build option and SPIR-V codegen option. May 2, 2024
sinnwrig and others added 4 commits May 1, 2024 22:49
Refactored build.zig to ensure format matches more similarly to the original build.zig.
Removed a python build file created by mistake.
@sinnwrig sinnwrig changed the title Added shared library build option and SPIR-V codegen option. Added shared library option, SPIR-V codegen option, and executable/test skipping option. May 3, 2024
@sinnwrig sinnwrig marked this pull request as draft May 6, 2024 05:12
@sinnwrig sinnwrig marked this pull request as ready for review May 6, 2024 05:32
@sinnwrig sinnwrig changed the title Added shared library option, SPIR-V codegen option, and executable/test skipping option. Added new build options and file inclusion overrides. May 21, 2024
@sinnwrig sinnwrig changed the title Added new build options and file inclusion overrides. Added new build options and shader file inclusion overrides. May 21, 2024
@slimsag
Copy link
Member

slimsag commented Jun 2, 2024

@sinnwrig can you rebase onto latest main which updates the zig version? I'll review this soon

.gitignore Outdated
@@ -18,6 +18,7 @@ zig-out/
/docgen_tmp/

libs/DirectXShaderCompiler
generated-include/spirv-tools/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for the addition of this? do we end up needing generated spirv-tools stuff?

Can we include it from https://github.com/hexops/spirv-tools instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't know that fork existed! Using that would probably work better than jamming together the SPIR-V headers with everything else. That folder just held the build-generated headers from SPIRV-Tools, so I figured it would be redundant to include them in the repo.

build.zig Outdated
@@ -138,6 +153,82 @@ pub fn build(b: *Build) !void {
lib_support_c_sources ++
lib_dxilcompression_c_sources;

// Build and link SPIRV-Tools
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely think we should be using https://github.com/hexops/spirv-tools here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll definitely be using it instead.

try std.fs.cwd().copyFile(lib_path, std.fs.cwd(), "atls.lib", .{});
try std.fs.cwd().copyFile(pdb_path, std.fs.cwd(), pdb_name, .{});
// This is probably a bug in the Zig linker.
if (!skip_executables)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you build spirv support for dxc.exe? or are you not testing that?

curious how I'll be able to test this works if dxc.exe doesn't work with it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the option to skip executables since I wanted to clean up the folder output as I was directly copying zig-out in another project. dxc.exe works fine with it.

build.zig Outdated

fn ensurePython(allocator: std.mem.Allocator) void
{
if (!ensureCommandExists(allocator, "python3", "--version"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't have a dependency on python here

This is a bit unfortunate; I am guessing we would need a fork of https://github.com/KhronosGroup/SPIRV-Headers packaged for build.zig with the headers pregenerated

Copy link
Contributor Author

@sinnwrig sinnwrig Jun 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I'm pretty sure hexops/SPIRV-Tools makes the stuff here a bit redundant, and I'll just use that repo instead.

build.zig Outdated
buildSPIRVVersion(allocator);
}

const BuildSPIRVGrammarStep = struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a goal to move away from having any custom build steps like this; so this will be a no-go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, this was only meant to generate spirv-headers, so if those are pregenerated it should be unneccesary.

@slimsag
Copy link
Member

slimsag commented Jun 2, 2024

getting this merged upstream here might be a fair bit of work - we can chat about it though

@sinnwrig
Copy link
Contributor Author

sinnwrig commented Jun 2, 2024

getting this merged upstream here might be a fair bit of work - we can chat about it though

Looking at the reviews, a lot of problems should be resolved if an external repo of SPIRV-Tools built with zig is used instead.

@sinnwrig
Copy link
Contributor Author

sinnwrig commented Jun 3, 2024

spirv compilation now depends on hexops/spirv-tools in the .zon file.

@PaperPrototype
Copy link

why is this not merged yet

@slimsag
Copy link
Member

slimsag commented Oct 9, 2024

Closing in favor of #7 which I promise I will actually merge soon.

@slimsag slimsag closed this Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants