using System.Diagnostics; using System.Runtime.InteropServices; namespace Brutal.ShaderCApi; /// /// Provides access to the shaderc library for compiling GLSL/HLSL shaders to SPIR-V. /// /// /// /// The native shaderc_shared library is loaded in the type initializer. If the library is not /// present in the assembly directory at the time the type is first accessed, the runtime will throw a /// , a common deployment pitfall. /// /// /// On macOS the library is loaded as shaderc_shared.1 (versioned dylib); all other platforms /// use shaderc_shared. This platform difference is managed internally. /// /// /// All handles returned by the factory methods (, /// ) wrap native memory and must be disposed when no longer needed. /// Each handle must be disposed exactly once: disposing twice or copying a handle struct and /// disposing both copies passes an invalid handle to native code and results in undefined behavior /// (heap corruption or crash). A structural dispose-guard is tracked upstream against the /// [InteropHandle] generator. /// /// public static partial class ShaderC { private const string DLL = "shaderc"; static ShaderC() { var assembly = typeof(ShaderC).Assembly; // EXCEPTION: The dll name is different on macOS, so we need to handle that case var nativeDll = NativeLibrary.Load( OperatingSystem.IsMacOS() ? "shaderc_shared.1" : "shaderc_shared", assembly, DllImportSearchPath.AssemblyDirectory ); // Set the DllImportResolver for the assembly to resolve the shaderc library NativeLibrary.SetDllImportResolver( assembly, (libraryName, _, _) => { // We should only ever be resolving the shaderc library, so we assert that the library name is correct. Debug.Assert(libraryName == "shaderc", "Unexpected library name requested for resolution."); return nativeDll; } ); } /// Creates a new shaderc compiler handle. /// /// A handle wrapping native memory. The caller is responsible for disposing /// the handle to release native resources. /// /// /// /// The returned handle wraps a native shaderc_compiler_t object. It must be disposed /// when no longer needed; failing to do so leaks native memory. Dispose the handle exactly /// once; a second Dispose call (or disposing two copies of the same struct) passes an /// invalid handle to native code and results in undefined behavior. /// /// /// A handle is not thread-safe: do not use the same /// handle concurrently across threads. Create a separate handle per thread if concurrent /// compilation is required. /// /// /// If the underlying native initialization fails, shaderc returns a null handle, which this /// method propagates as-is (an uninitialized struct). /// /// public static Compiler CreateCompiler() { return PInvoke.shaderc_compiler_initialize(); } /// Creates a new shaderc compile-options handle. /// /// A handle wrapping native memory. The caller is responsible for /// disposing the handle to release native resources. /// /// /// /// The returned handle wraps a native shaderc_compile_options_t object. It must be /// disposed when no longer needed; failing to do so leaks native memory. Dispose the handle /// exactly once; a second Dispose call (or disposing two copies of the same struct) /// passes an invalid handle to native code and results in undefined behavior. /// /// /// A handle is not thread-safe: do not mutate /// options from multiple threads simultaneously. The options instance must also outlive any /// compilation that uses it. /// /// /// If the underlying native initialization fails, shaderc returns a null handle, which this /// method propagates as-is (an uninitialized struct). /// /// public static CompileOptions CreateCompilerOptions() { return PInvoke.shaderc_compile_options_initialize(); } /// Gets the SPIR-V version and revision that shaderc was built against. /// /// Packed SPIR-V version: major version in the upper 16 bits, minor version in the lower 16 bits. /// /// SPIR-V revision number. public static unsafe void GetSpvVersion(out uint version, out uint revision) { fixed (uint* v = &version) fixed (uint* r = &revision) { PInvoke.shaderc_get_spv_version(v, r); } } /// Tries to parse a GLSL version/profile string such as "450core". /// UTF-8 encoded, null-terminated version/profile string. /// /// When this method returns true, the parsed GLSL version number (e.g. 450). /// /// /// When this method returns true, the parsed profile. Undefined when this method returns /// false. /// /// true if parsing succeeded; otherwise false. /// /// str must be null-terminated: the underlying shaderc API treats the pointer as a C string /// and will overread if no null terminator is present. A obtained /// from a string literal or a stack-allocated buffer with an explicit \0 satisfies this /// requirement. /// public static unsafe bool TryParseVersionProfile(ReadOnlySpan str, out int version, out Profile profile) { fixed (byte* s = str) fixed (int* v = &version) fixed (Profile* p = &profile) { return PInvoke.shaderc_parse_version_profile(s, v, p); } } }