diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b641f46..fb91940 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ -name: .NET 10 build baseline +name: .NET 10 SDK qualification on: push: @@ -10,7 +10,7 @@ permissions: contents: read concurrency: - group: build-${{ github.workflow }}-${{ github.ref }} + group: build-${{ github.workflow }}-${{ github.head_ref || github.ref_name }} cancel-in-progress: true jobs: @@ -29,7 +29,10 @@ jobs: global-json-file: global.json cache: true cache-dependency-path: '**/packages.lock.json' - - name: Restore, build, baseline tests, pack, and consume packages + - uses: ilammy/msvc-dev-cmd@v1 + with: + arch: x64 + - name: Restore, build, ABI tests, pack, and consume packages shell: pwsh run: ./build.ps1 -Configuration ${{ matrix.configuration }} - name: Upload build evidence @@ -42,3 +45,47 @@ jobs: artifacts/test-results/** artifacts/packages/** if-no-files-found: warn + + interop-aot: + name: Native COM JIT and AOT (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + timeout-minutes: 25 + strategy: + fail-fast: false + matrix: + os: [ubuntu-24.04, windows-2025] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-dotnet@v4 + with: + global-json-file: global.json + - uses: ilammy/msvc-dev-cmd@v1 + if: runner.os == 'Windows' + with: + arch: x64 + - name: Native fixture and managed/AOT ownership qualification + shell: pwsh + run: ./build/VerifyInterop.ps1 -Configuration Release -PublishAot + - name: WARP swap-chain resize qualification + if: runner.os == 'Windows' + shell: pwsh + run: dotnet run --project samples/ModernTriangle -c Debug -- --warp --resize-test --evidence=artifacts/triangle-resize + - name: WARP compute and DirectML numerical qualification + if: runner.os == 'Windows' + shell: pwsh + run: | + dotnet run --project samples/ModernCompute -c Debug -- --warp --directml + if ($LASTEXITCODE -ne 0) { throw 'JIT numerical qualification failed.' } + dotnet publish samples/ModernCompute -c Release -r win-x64 -p:PublishAot=true -o artifacts/modern-compute-aot + if ($LASTEXITCODE -ne 0) { throw 'NativeAOT compute publish failed.' } + & ./artifacts/modern-compute-aot/ModernCompute.exe --warp --directml --no-debug + if ($LASTEXITCODE -ne 0) { throw 'NativeAOT numerical qualification failed.' } + - uses: actions/upload-artifact@v4 + if: always() + with: + name: interop-${{ matrix.os }} + path: | + artifacts/qualification-aot/** + artifacts/modern-compute-aot/** + artifacts/triangle-resize/** + if-no-files-found: warn diff --git a/DXSharp.sln b/DXSharp.sln index 0f5951f..954f09b 100644 --- a/DXSharp.sln +++ b/DXSharp.sln @@ -126,6 +126,18 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "inc", "inc", "{301D517A-385 devops\pipelines\inc\global-varz.yml = devops\pipelines\inc\global-varz.yml EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DXSharp.Core", "src\DXSharp.Core\DXSharp.Core.csproj", "{95C699FE-92C8-4296-BC32-D250258EA40D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DXSharp.DirectML", "src\DXSharp.DirectML\DXSharp.DirectML.csproj", "{28A906D9-4254-467D-9238-8C60C49E2E21}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InteropQualification", "tests\InteropQualification\InteropQualification.csproj", "{6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModernTriangle", "samples\ModernTriangle\ModernTriangle.csproj", "{AA296E18-E027-4E92-A7D7-42FC5086A052}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModernCompute", "samples\ModernCompute\ModernCompute.csproj", "{2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -230,6 +242,66 @@ Global {2362EB4B-6F3D-4274-A836-C5394A9660FC}.Release|x64.Build.0 = Release|Any CPU {2362EB4B-6F3D-4274-A836-C5394A9660FC}.Release|x86.ActiveCfg = Release|Any CPU {2362EB4B-6F3D-4274-A836-C5394A9660FC}.Release|x86.Build.0 = Release|Any CPU + {95C699FE-92C8-4296-BC32-D250258EA40D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {95C699FE-92C8-4296-BC32-D250258EA40D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {95C699FE-92C8-4296-BC32-D250258EA40D}.Debug|x64.ActiveCfg = Debug|x64 + {95C699FE-92C8-4296-BC32-D250258EA40D}.Debug|x64.Build.0 = Debug|x64 + {95C699FE-92C8-4296-BC32-D250258EA40D}.Debug|x86.ActiveCfg = Debug|x86 + {95C699FE-92C8-4296-BC32-D250258EA40D}.Debug|x86.Build.0 = Debug|x86 + {95C699FE-92C8-4296-BC32-D250258EA40D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {95C699FE-92C8-4296-BC32-D250258EA40D}.Release|Any CPU.Build.0 = Release|Any CPU + {95C699FE-92C8-4296-BC32-D250258EA40D}.Release|x64.ActiveCfg = Release|x64 + {95C699FE-92C8-4296-BC32-D250258EA40D}.Release|x64.Build.0 = Release|x64 + {95C699FE-92C8-4296-BC32-D250258EA40D}.Release|x86.ActiveCfg = Release|x86 + {95C699FE-92C8-4296-BC32-D250258EA40D}.Release|x86.Build.0 = Release|x86 + {28A906D9-4254-467D-9238-8C60C49E2E21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {28A906D9-4254-467D-9238-8C60C49E2E21}.Debug|Any CPU.Build.0 = Debug|Any CPU + {28A906D9-4254-467D-9238-8C60C49E2E21}.Debug|x64.ActiveCfg = Debug|x64 + {28A906D9-4254-467D-9238-8C60C49E2E21}.Debug|x64.Build.0 = Debug|x64 + {28A906D9-4254-467D-9238-8C60C49E2E21}.Debug|x86.ActiveCfg = Debug|x86 + {28A906D9-4254-467D-9238-8C60C49E2E21}.Debug|x86.Build.0 = Debug|x86 + {28A906D9-4254-467D-9238-8C60C49E2E21}.Release|Any CPU.ActiveCfg = Release|Any CPU + {28A906D9-4254-467D-9238-8C60C49E2E21}.Release|Any CPU.Build.0 = Release|Any CPU + {28A906D9-4254-467D-9238-8C60C49E2E21}.Release|x64.ActiveCfg = Release|x64 + {28A906D9-4254-467D-9238-8C60C49E2E21}.Release|x64.Build.0 = Release|x64 + {28A906D9-4254-467D-9238-8C60C49E2E21}.Release|x86.ActiveCfg = Release|x86 + {28A906D9-4254-467D-9238-8C60C49E2E21}.Release|x86.Build.0 = Release|x86 + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Debug|x64.ActiveCfg = Debug|x64 + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Debug|x64.Build.0 = Debug|x64 + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Debug|x86.ActiveCfg = Debug|x86 + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Debug|x86.Build.0 = Debug|x86 + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Release|Any CPU.Build.0 = Release|Any CPU + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Release|x64.ActiveCfg = Release|x64 + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Release|x64.Build.0 = Release|x64 + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Release|x86.ActiveCfg = Release|x86 + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F}.Release|x86.Build.0 = Release|x86 + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Debug|x64.ActiveCfg = Debug|x64 + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Debug|x64.Build.0 = Debug|x64 + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Debug|x86.ActiveCfg = Debug|x86 + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Debug|x86.Build.0 = Debug|x86 + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Release|Any CPU.Build.0 = Release|Any CPU + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Release|x64.ActiveCfg = Release|x64 + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Release|x64.Build.0 = Release|x64 + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Release|x86.ActiveCfg = Release|x86 + {AA296E18-E027-4E92-A7D7-42FC5086A052}.Release|x86.Build.0 = Release|x86 + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Debug|x64.ActiveCfg = Debug|x64 + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Debug|x64.Build.0 = Debug|x64 + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Debug|x86.ActiveCfg = Debug|x86 + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Debug|x86.Build.0 = Debug|x86 + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Release|Any CPU.Build.0 = Release|Any CPU + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Release|x64.ActiveCfg = Release|x64 + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Release|x64.Build.0 = Release|x64 + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Release|x86.ActiveCfg = Release|x86 + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -256,6 +328,11 @@ Global {9AA76EA1-7D92-40CF-A170-B38DEEB87396} = {AF35E6EE-C244-4E1F-84A6-DED8F9BD0EC7} {3FEFFBFD-24AE-4B98-AF02-1A2EA87F9245} = {60DAF7CD-8C68-4BDF-9940-5ACBF49620D3} {301D517A-3852-4BEC-911A-EAF7F421F3EB} = {60DAF7CD-8C68-4BDF-9940-5ACBF49620D3} + {95C699FE-92C8-4296-BC32-D250258EA40D} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} + {28A906D9-4254-467D-9238-8C60C49E2E21} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} + {6D81F5A7-1D01-49DF-8D02-ECD9183FDF6F} = {4A52D278-0251-4FD5-BF3A-5C52C294480A} + {AA296E18-E027-4E92-A7D7-42FC5086A052} = {AEE84FD5-7441-42CF-AA6C-5805B759E1F8} + {2BB6521A-B6BC-4BD3-AC2E-67E8042386E4} = {AEE84FD5-7441-42CF-AA6C-5805B759E1F8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7C4990EC-248E-49D5-A254-EA8AD214C338} diff --git a/Directory.Packages.props b/Directory.Packages.props index e5055fb..5ca097c 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,6 +8,7 @@ + diff --git a/README.md b/README.md index 8ce09c7..a3d83aa 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ See [the .NET 10 build baseline](doc/NET10-Build-Baseline.md) for Release builds, locked dependencies, test scope, package checks, and current limitations. The original proof-of-concept architecture described below is being migrated incrementally; compilation does not establish production readiness. +The [generated COM foundation](doc/Generated-Com-Foundation.md) adds the modern `DXSharp.Core` and optional `DXSharp.DirectML` path, native ownership qualification, DXC compilation, and bounded graphics/compute samples. The coverage matrix distinguishes working implementations from the remaining port and records what has actually been executed. + ## Roadmap 🛤️ - [x] Initial DirectX 12 support diff --git a/build.ps1 b/build.ps1 index 7b21717..3d753ea 100644 --- a/build.ps1 +++ b/build.ps1 @@ -38,6 +38,7 @@ try { if ([int] $trx.TestRun.ResultSummary.Counters.executed -lt 1) { throw 'The baseline test filter executed no tests.' } + & (Join-Path $PSScriptRoot 'build/VerifyInterop.ps1') -Configuration $Configuration if ($RunGraphicsTests) { Invoke-DotNet -Arguments @('test', 'tests/BasicTests/BasicTests.csproj', '--configuration', $Configuration, '--no-build', '--no-restore', '--filter', 'TestCategory!=BuildBaseline', diff --git a/build/CaptureCrash.ps1 b/build/CaptureCrash.ps1 new file mode 100644 index 0000000..243bda2 --- /dev/null +++ b/build/CaptureCrash.ps1 @@ -0,0 +1,26 @@ +[CmdletBinding()] +param( + [Parameter(Mandatory)] [string] $Executable, + [Parameter(Mandatory)] [string] $ProcDumpPath, + [string[]] $Arguments = @(), + [string] $OutputDirectory = (Join-Path (Split-Path $PSScriptRoot -Parent) 'artifacts/crashes') +) +$ErrorActionPreference = 'Stop' +if (-not $IsWindows) { throw 'Crash capture requires Windows.' } +$executablePath = (Resolve-Path $Executable).Path +$procDump = (Resolve-Path $ProcDumpPath).Path +$directory = [System.IO.Path]::GetFullPath($OutputDirectory) +New-Item -ItemType Directory -Force $directory | Out-Null +@{ + Executable = $executablePath + Arguments = $Arguments + StartedUtc = [DateTime]::UtcNow.ToString('O') + OS = [System.Runtime.InteropServices.RuntimeInformation]::OSDescription + Architecture = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString() + SHA256 = (Get-FileHash $executablePath -Algorithm SHA256).Hash + DotNet = (& dotnet --info | Out-String) +} | ConvertTo-Json -Depth 4 | Set-Content (Join-Path $directory 'session.json') +# Full dump on an unhandled exception, launching this executable only. ProcDump +# handles its own first-run license prompt. No machine-wide crash settings change. +& $procDump -ma -e -x $directory $executablePath @Arguments 2>&1 | Tee-Object (Join-Path $directory 'procdump.log') +if ($LASTEXITCODE -ne 0) { throw "ProcDump exited with code $LASTEXITCODE. See $directory." } diff --git a/build/VerifyInterop.ps1 b/build/VerifyInterop.ps1 new file mode 100644 index 0000000..6defbe7 --- /dev/null +++ b/build/VerifyInterop.ps1 @@ -0,0 +1,44 @@ +[CmdletBinding()] +param( + [ValidateSet('Debug', 'Release')] [string] $Configuration = 'Debug', + [ValidateSet('x64')] [string] $Architecture = 'x64', + [switch] $PublishAot +) +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest +$repoRoot = Split-Path $PSScriptRoot -Parent +function Invoke-Checked([string] $Command, [string[]] $Arguments) { + & $Command @Arguments + if ($LASTEXITCODE -ne 0) { throw "$Command failed with exit code $LASTEXITCODE." } +} +Push-Location $repoRoot +try { + $nativeDirectory = Join-Path $repoRoot "artifacts/native/$Architecture" + New-Item -ItemType Directory -Force $nativeDirectory | Out-Null + $source = Join-Path $repoRoot 'tests/InteropQualification/native/fixture.cpp' + if ($IsWindows) { + # Run from a Developer PowerShell, with cl.exe configured for Architecture. + $fixture = Join-Path $nativeDirectory 'dxsharp_abi.dll' + Invoke-Checked 'cl.exe' @('/nologo', '/std:c++17', '/EHsc', '/LD', '/O2', $source, + "/Fo$nativeDirectory/fixture.obj", '/link', "/OUT:$fixture", "/IMPLIB:$nativeDirectory/dxsharp_abi.lib") + $rid = "win-$Architecture" + } elseif ($IsLinux -and $Architecture -eq 'x64') { + $fixture = Join-Path $nativeDirectory 'libdxsharp_abi.so' + Invoke-Checked 'g++' @('-std=c++17', '-shared', '-fPIC', '-O2', $source, '-o', $fixture) + $rid = 'linux-x64' + } else { throw 'This script qualifies Windows x64/x86/ARM64 and Linux x64.' } + $project = 'tests/InteropQualification/InteropQualification.csproj' + Invoke-Checked 'dotnet' @('restore', $project, '--locked-mode', '-m:1') + Invoke-Checked 'dotnet' @('build', $project, '-c', $Configuration, '--no-restore', '-m:1', "-p:DXSharpNativeFixture=$fixture") + $managed = (& dotnet msbuild $project "-p:Configuration=$Configuration" -getProperty:TargetPath -nologo | Out-String).Trim() + if ($LASTEXITCODE -ne 0 -or -not (Test-Path $managed)) { throw 'Unable to locate the built qualification assembly.' } + Invoke-Checked 'dotnet' @($managed) + if ($PublishAot) { + $publish = Join-Path $repoRoot "artifacts/qualification-aot/$rid" + Invoke-Checked 'dotnet' @('publish', $project, '-m:1', '-c', $Configuration, '-r', $rid, '-p:PublishAot=true', + "-p:DXSharpNativeFixture=$fixture", '-o', $publish) + Copy-Item $fixture $publish -Force + $executable = Join-Path $publish $(if ($IsWindows) { 'InteropQualification.exe' } else { 'InteropQualification' }) + Invoke-Checked $executable @() + } +} finally { Pop-Location } diff --git a/build/VerifyPackages.ps1 b/build/VerifyPackages.ps1 index 2a45414..b7ec0c7 100644 --- a/build/VerifyPackages.ps1 +++ b/build/VerifyPackages.ps1 @@ -16,7 +16,7 @@ try { if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($version)) { throw 'Unable to evaluate the package version.' } - foreach ($name in @('DXSharp', 'DXSharp.Dxc', 'DXSharp.Framework')) { + foreach ($name in @('DXSharp', 'DXSharp.Dxc', 'DXSharp.Framework', 'DXSharp.Core', 'DXSharp.DirectML')) { if (-not (Test-Path (Join-Path $packageDirectory "$name.$version.nupkg"))) { throw "Missing package: $name.$version.nupkg" } @@ -28,15 +28,21 @@ try { net10.0-windows10.0.26100.0 true true + win-x64 + false + true + "@ | Set-Content (Join-Path $consumerDirectory 'Consumer.csproj') @' System.Console.WriteLine(typeof(DXSharp.Direct3D12.IDevice).Assembly.FullName); System.Console.WriteLine(typeof(DXSharp.Dxc.ShaderBuilder).Assembly.FullName); +System.Console.WriteLine(typeof(DXSharp.Graphics.GraphicsDevice).Assembly.FullName); +System.Console.WriteLine(typeof(DXSharp.MachineLearning.DirectMlDevice).Assembly.FullName); '@ | Set-Content (Join-Path $consumerDirectory 'Program.cs') @" @@ -54,6 +60,32 @@ System.Console.WriteLine(typeof(DXSharp.Dxc.ShaderBuilder).Assembly.FullName); if ($LASTEXITCODE -ne 0) { throw 'Package consumer restore failed.' } & dotnet build Consumer.csproj --configuration $Configuration --no-restore if ($LASTEXITCODE -ne 0) { throw 'Package consumer build failed.' } + $output = (& dotnet msbuild Consumer.csproj "-p:Configuration=$Configuration" -getProperty:TargetDir -nologo | Out-String).Trim() + if ($LASTEXITCODE -ne 0 -or -not (Test-Path $output)) { throw 'Unable to locate the package consumer output.' } + $native = @{ + 'D3D12/D3D12Core.dll' = 'microsoft.direct3d.d3d12/1.620.1/build/native/bin/x64/D3D12Core.dll' + 'D3D12/D3D12StateObjectCompiler.dll' = 'microsoft.direct3d.d3d12/1.620.1/build/native/bin/x64/D3D12StateObjectCompiler.dll' + 'dxcompiler.dll' = 'microsoft.direct3d.dxc/1.9.2609.5/build/native/bin/x64/dxcompiler.dll' + 'dxil.dll' = 'microsoft.direct3d.dxc/1.9.2609.5/build/native/bin/x64/dxil.dll' + 'DirectML.dll' = 'microsoft.ai.directml/1.15.4/bin/x64-win/DirectML.dll' + } + function Test-NativeOutput([string] $Directory) { + foreach ($relative in $native.Keys) { + $actual = Join-Path $Directory $relative + $expected = Join-Path $consumerDirectory "packages/$($native[$relative])" + if (-not (Test-Path $actual) -or (Get-FileHash $actual).Hash -ne (Get-FileHash $expected).Hash) { + throw "Missing or incorrect native package asset: $actual" + } + } + } + Test-NativeOutput $output + $publish = Join-Path $consumerDirectory 'publish' + & dotnet publish Consumer.csproj --configuration $Configuration --no-restore --output $publish + if ($LASTEXITCODE -ne 0) { throw 'Package consumer single-file publish failed.' } + Test-NativeOutput $publish + foreach ($debugLayer in @('D3D12/d3d12SDKLayers.dll', 'DirectML.Debug.dll')) { + if (Test-Path (Join-Path $publish $debugLayer)) { throw "Debug layer leaked into publish: $debugLayer" } + } } finally { Pop-Location } diff --git a/build/msbuild/DXSharp.Core.targets b/build/msbuild/DXSharp.Core.targets new file mode 100644 index 0000000..84dd8b2 --- /dev/null +++ b/build/msbuild/DXSharp.Core.targets @@ -0,0 +1,8 @@ + + + + $(NuGetPackageRoot)/microsoft.direct3d.d3d12/1.620.1 + $(NuGetPackageRoot)/microsoft.direct3d.dxc/1.9.2609.5 + + + diff --git a/build/msbuild/DXSharp.DirectML.targets b/build/msbuild/DXSharp.DirectML.targets new file mode 100644 index 0000000..b028c23 --- /dev/null +++ b/build/msbuild/DXSharp.DirectML.targets @@ -0,0 +1,15 @@ + + + $(NuGetPackageRoot)/microsoft.ai.directml/1.15.4 + x64 + x86 + arm64 + + + + + + diff --git a/build/msbuild/NativeDependencies.targets b/build/msbuild/NativeDependencies.targets index 1842fac..9ffae96 100644 --- a/build/msbuild/NativeDependencies.targets +++ b/build/msbuild/NativeDependencies.targets @@ -8,13 +8,20 @@ + Link="D3D12/%(Filename)%(Extension)" Pack="false" ExcludeFromSingleFile="true" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" /> + Link="D3D12/%(Filename)%(Extension)" Pack="false" ExcludeFromSingleFile="true" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="Never" /> + Link="%(Filename)%(Extension)" Pack="false" ExcludeFromSingleFile="true" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="PreserveNewest" /> + + + + diff --git a/doc/Generated-Com-Foundation.md b/doc/Generated-Com-Foundation.md new file mode 100644 index 0000000..a6e86af --- /dev/null +++ b/doc/Generated-Com-Foundation.md @@ -0,0 +1,114 @@ +# Generated COM foundation + +This slice establishes a second, independently usable path through DXSharp: generated native projections, deterministic C# owners, explicit command recording, shader compilation, presentation, and DirectML tensor execution. It builds on the .NET 10 baseline. The original SDK and its samples remain available while their public surface is ported. + +The architectural direction follows the README's self-assessment: preserve DirectX semantics and direct access, make the managed layer idiomatic, and remove the duplicated public-interface/implementation hierarchy. The optional application framework remains separate from the graphics SDK. + +## Assemblies and dependency direction + +| Assembly | Responsibility | Depends on | +| --- | --- | --- | +| `DXSharp.Core` | Private generated DXGI/D3D12/DXC/DirectML declarations; native activation; ownership; public graphics and shader APIs | Native Agility and DXC SDK packages | +| `DXSharp.DirectML` | Public tensor shape, DirectML device, compiled dense Float32 addition, initialization/binding/execution | `DXSharp.Core`, native DirectML package | +| Existing `DXSharp`, `DXSharp.Dxc`, `DXSharp.Framework` | Existing proof-of-concept API during the port | Existing frozen projection | + +The migration target is to promote the modern implementation into the main DXSharp package after API and runtime parity gates pass, then port the framework and retire the legacy lifetime machinery. The two implementations are a transition boundary, not a permanent duplication strategy. The modern samples depend only on the new path. + +The generated declarations are implementation details. A signed friend relationship lets the DirectML component reuse the same declarations and ownership rules without a second native projection. Public graphics types are sealed owners; they do not mirror each native interface with another public interface and implementation pair. `GraphicsObject.QueryNativeInterface` supplies an independently owned `SafeHandle` for deliberate native integration. + +Core uses CsWin32 **0.3.367**, Win32Metadata **71.0.37-preview**, `GeneratedComInterface`, `GeneratedComClass`, and `LibraryImport`. Runtime marshalling is disabled in the modern assemblies. CsWin32 runs as a build task; generated output lives under `artifacts/obj`, outside source globs. The legacy projects retain their previous generator pins. These are intentional per-project overrides, not a solution-wide projection replacement. + +## Ownership contract + +`ComOwner.Adopt` consumes exactly one returned native interface reference. It creates a private `UniqueInstance` generated projection, which owns its own queried references. Disposal calls `ComObject.FinalRelease` on that unique projection and disposes the original native reference exactly once. Projection failure releases the consumed output. `Clone` obtains independent ownership. No public method returns a shared runtime RCW that the caller must manually release. + +The only handwritten vtable calls are the invariant IUnknown slots: QueryInterface, AddRef and Release. A narrow source-generated `IFactory2Raw` declaration adapts DXGI's typed swap-chain output to a raw owning output; it inherits its preceding slots from metadata-generated `IDXGIFactory1`. This avoids mixing the default shared output projection with deterministic unique ownership. No graphics call uses handwritten vtable offsets. + +Internal stack-scoped borrows use `SafeHandle.DangerousAddRef/DangerousRelease` and cannot escape to the heap. Borrow values must not be copied. The owner and native lease remain distinct; disposal of the public owner does not invalidate an active borrow or a separately retained submission reference. Callers synchronize disposal with CPU method calls on the same owner. DXC compilation and queue operations have their own locks; command-list recording and allocator management are single-threaded per object. + +### GPU lifetime + +Recording captures independent native references to resources, descriptor heaps, pipeline state, root signatures and the allocator. Submitting transfers those references to a queue completion record. The queue also retains the native command list. Fence completion releases that record and makes allocator/list reuse legal. Public wrappers can therefore be disposed after their final recording use, before submission, without releasing the native objects required by that submission. + +Pending submissions root their queue independently of application references. A nonblocking completion poll retires finished records, so dropping the last managed queue reference cannot finalize an in-flight resource. Explicit `Wait` retires immediately. The poll does not turn a timeout or signal failure into success; unresolved work stays rooted. A removed device permits retiring all records because its execution has stopped. Queue disposal drains its work; a timeout throws and leaves the owner live for another wait/disposal attempt. + +The caller still manages resource states, cross-queue synchronization, descriptor overwrites and concurrent use. A fence value from another queue is not interchangeable with this queue's completion token. Each queue owns a private fence. Objects from different device identities are rejected before recording or binding. This is a thin SDK contract, not an implicit render graph or a full D3D12 hazard validator. + +## Native activation and distribution + +Agility 1.620.1 is selected through `ID3D12SDKConfiguration1.CreateDeviceFactory(620, absoluteSdkPath)`. Debug configuration is applied to that local factory before device creation. This avoids the old module initializer, developer-mode SDK override and process-global SDK selection. `DeviceOptions.UseAgility = false` explicitly selects the system runtime. + +System D3D12/DXGI load from the Windows system directory. DXC and DirectML load from `AppContext.BaseDirectory`; Agility resides under `D3D12/`. Successful module loads are cached for process lifetime so callbacks and finalizers cannot call into an unloaded module. Missing app-local dependencies fail with the expected absolute path. + +Modern NuGet packages depend on the official native SDK packages and include `buildTransitive` copy targets. Build and publish copy the selected architecture's files to those same paths, including for a framework-dependent single-file consumer. DLLs remain outside the single-file executable. Debug layers are copied for Debug builds and excluded from publish. The separate DirectML package brings DirectML 1.15.4 only when that component is referenced. Native files are consumed from their original packages, with their original licenses. + +Set `RuntimeIdentifier` to `win-x64`, `win-x86`, or `win-arm64` for deployment. Architecture selection otherwise defaults to x64; an arbitrary AnyCPU executable is not a portable native deployment. x64 is the qualification target in this slice. x86/ARM64 copy mappings are present but their ABI execution is not yet qualified. When updating native versions, update both `Directory.Packages.props` and the pinned package paths in `build/msbuild/DXSharp.*.targets`, then rerun the isolated consumer test. + +## Implemented surface and remaining port + +| Area | Implemented in the modern path | Subsequent work | +| --- | --- | --- | +| DXGI | Adapter preference/WARP selection; flip HWND swap chain; acquire/present/resize | Adapter enumeration and selection API, HDR/color space, tearing, fullscreen policy | +| D3D12 | Device/local factory; direct/compute/copy queues; allocators/lists; completion; buffer map/copy/barriers; RTV heaps; graphics/compute roots and PSOs | General textures/views, upload footprints, descriptor allocation/tables, samplers, stream PSOs, bundles, render passes, enhanced barriers | +| Shaders | DXC Compiler3/Result; UTF-8 source; arguments; generated include/blob callbacks; status and warnings; copied object/reflection/PDB/hash/root-signature/disassembly outputs | Structured reflection API, container/linker tooling, cache, cancellation through worker isolation | +| DirectML | Immutable dense Float32 shape; device; compiled equal-shape Add; initializer; descriptors; temporary/persistent allocations; binding; dispatch | General binding/execution sessions, reusable initialization/storage, GEMM/convolution/activations, graphs, broadcasting/strides/dtypes, feature queries | +| Diagnostics | Bounded InfoQueue snapshot, device-removed HRESULT, operation-preserving exceptions, sample evidence, ProcDump launcher | DRED setup and breadcrumb/page-fault decoding; live-object regression gates; device-recovery policy | +| Samples | ModernTriangle with resize, bounded frames and GC stress; ModernCompute with numerical DXC and optional DirectML checks | Texture, async upload, multi-frame synchronization, mesh/raytracing, richer DirectML workloads | + +Declaring newer native interfaces in the generation manifest does not mean their friendly APIs or runtime paths have been qualified. General API completeness, zero-allocation hot paths and performance parity still require measured gates. `IsAotCompatible` is not yet advertised on the packages: the passing AOT console workloads qualify their exercised paths, while broader public API coverage remains part of the port. The WinForms triangle itself is not an AOT sample; ModernCompute is the console candidate. + +## Build and qualification + +Use an x64 Developer PowerShell with the C++ build tools available for the standard Windows build: + +```powershell +./build.ps1 -Configuration Debug +./build.ps1 -Configuration Release +./build/VerifyInterop.ps1 -Configuration Release -PublishAot + +dotnet run --project samples/ModernTriangle -c Debug -- --frames=600 --gc-stress +dotnet run --project samples/ModernTriangle -c Debug -- --headless --warp +dotnet run --project samples/ModernCompute -c Debug -- --directml +dotnet run --project samples/ModernCompute -c Release -- --warp --directml +``` + +ModernTriangle compiles shaders, checks failed DXC diagnostics and include callbacks, dispatches a known compute result, and renders only when headless mode is absent. It uses a fully synchronized frame path to isolate ABI/lifetime correctness before introducing multiple frames in flight. ModernCompute verifies `1234` from a compiled compute shader and, with `--directml`, `[11, 22, 33, 44]` from a native DirectML Add. Both exercise disposal of public owners before submission while native leases remain live. `--no-debug` disables validation when deliberately comparing a retail run. `--system-runtime` bypasses Agility. + +The native C++ fixture checks actual vtables, inherited methods, 16-byte structure returns, buffer lengths, failed QI/projection cleanup, independent clones, disposal and scoped borrows, forgotten-owner finalization, repeated GC stress, native-held callbacks, and the actual generated DXC include/blob callback types. It does not need a GPU or the Windows DLLs. The tensor tests verify copied shapes, invalid dimensions and checked byte counts; binding and tensor descriptor sizes are compared with the platform C++ definitions (Windows SDK types on Windows). Include resolver exceptions are checked across the native HRESULT boundary. `VerifyInterop.ps1` builds the fixture with MSVC on Windows or g++ on Linux, executes the managed suite, and optionally publishes/runs its NativeAOT counterpart. + +CI adds Windows/Linux JIT and AOT native-fixture runs, Windows WARP compute/DirectML checks, and a published AOT ModernCompute run. The isolated package consumer restores into a private cache, builds, publishes a single-file executable, compares deployed native file hashes to the selected official SDK package files, and rejects debug layers in publish output. CI configuration is an execution gate, not evidence that a workflow has already passed. + +### Evidence from this development host + +- Linux x64, SDK 10.0.403: full solution Debug and Release builds passed; new modern projects compiled without warnings. Existing legacy warnings remain visible. +- Native fixture JIT and Linux NativeAOT: **2,288 checks passed per run**, **2,004 native fixture objects destroyed per run**, **zero remaining native fixture objects**. This includes the generated DXC include/blob callback path, not actual DXC compilation on Linux. +- Linux NativeAOT publishing required a development-host workaround for MSB4216: an external, uncommitted MSBuild import ran the ILLink tasks in process instead of the failing out-of-process task host. The resulting native ELF executable passed the same suite. No workaround or warning suppression is shipped in the repository. +- Debug and Release packing and isolated consumer build/single-file publish passed, including deployed native hashes and debug-layer exclusion. +- GitHub Actions [run 41980630248](https://github.com/atcarter714/DXSharp/actions/runs/41980630248), implementation commit `222112ad1f968dbad12693f6a7e1a1972bcd1ca5`: all four jobs passed. Windows Debug/Release built the full solution, passed the three baseline tests and 2,288 native-fixture checks, packed the SDK, and verified isolated package build/publish output. Windows and Linux both passed the fixture under JIT and NativeAOT. No local task-host workaround was needed in CI. +- Windows WARP, JIT with validation enabled: DXC compute/readback returned `1234`; DirectML Add returned `[11, 22, 33, 44]`. The published Windows NativeAOT console repeated both numerical checks successfully. These runs also exercised early public-owner disposal, scoped mapping and forced GC. +- Windows interactive presentation, resize and long-duration rendering remain to be qualified on a desktop. The earlier baseline sample's Windows success was reported by the maintainer; the later CLR crash remains undiagnosed. + +The Windows gates caught two defects during this slice. Verifiers now query MSBuild for output paths rather than assuming the absence of a platform subdirectory. The include callback returns the exception's failing HRESULT directly: `Marshal.GetHRForException` had left thread-local COM error information that changed a later unsupported-interface exception. Debug builds enable validation by default; Release samples use retail configuration. The Windows JIT numerical job deliberately uses Debug so the matching packaged SDK layers are available. + +## Crash investigation + +The original BasicSample remains useful as a reproduction. Keep its exact binaries and PDBs with the dump; do not infer the failure from a managed exception alone. A CLR access violation can originate at an incorrect ABI boundary, a released native object, or unrelated native code. This foundation removes several lifetime mechanisms from the new path, but is not proof of the original crash's cause or a verified fix. + +With Sysinternals ProcDump available, launch either sample under full-dump capture: + +```powershell +./build/CaptureCrash.ps1 ` + -Executable ./artifacts/bin/BasicSample/Debug/net10.0-windows10.0.26100.0/BasicSample.exe ` + -ProcDumpPath C:/Tools/ProcDump/procdump64.exe + +./build/CaptureCrash.ps1 ` + -Executable ./artifacts/bin/ModernTriangle/Debug/net10.0-windows10.0.26100.0/ModernTriangle.exe ` + -ProcDumpPath C:/Tools/ProcDump/procdump64.exe ` + -Arguments @('--frames=100000', '--gc-stress') +``` + +If Developer PowerShell selects an `x64` platform subdirectory, use the matching executable path. MSBuild can report the assembly path with `dotnet msbuild -p:Configuration=Debug -getProperty:TargetPath`; the apphost is beside it with an `.exe` extension. + +The launcher records the executable hash, arguments, OS, architecture and SDK/runtime inventory, and asks ProcDump to launch only that executable and capture a full dump on an unhandled exception. It does not change global crash settings. ModernTriangle also writes `diagnostics.json` and managed failures under its evidence directory. Next investigation requires the crashing native instruction, native/managed stacks, loaded-module versions and device-removal evidence from that exact run. + +Primary references: [source-generated COM](https://learn.microsoft.com/dotnet/standard/native-interop/comwrappers-source-generation), [unique ComObject.FinalRelease](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.marshalling.comobject.finalrelease), [DirectML binding](https://learn.microsoft.com/windows/ai/directml/dml-binding), [Microsoft's HelloDirectML](https://github.com/microsoft/DirectML/blob/master/Samples/HelloDirectML/main.cpp), [ProcDump](https://learn.microsoft.com/sysinternals/downloads/procdump). diff --git a/samples/ModernCompute/ModernCompute.csproj b/samples/ModernCompute/ModernCompute.csproj new file mode 100644 index 0000000..6a73b40 --- /dev/null +++ b/samples/ModernCompute/ModernCompute.csproj @@ -0,0 +1,9 @@ + + + Exe + net10.0-windows10.0.26100.0 + false + true + + + diff --git a/samples/ModernCompute/Program.cs b/samples/ModernCompute/Program.cs new file mode 100644 index 0000000..cd89891 --- /dev/null +++ b/samples/ModernCompute/Program.cs @@ -0,0 +1,68 @@ +using DXSharp.Graphics; +using DXSharp.MachineLearning; +using DXSharp.Shaders; + +#if DEBUG +const bool debugLayer = true; +#else +const bool debugLayer = false; +#endif + +using var device = GraphicsDevice.Create( new( ) { + UseWarp = args.Contains( "--warp" ), UseAgility = !args.Contains( "--system-runtime" ), EnableDebugLayer = debugLayer && !args.Contains( "--no-debug" ) +} ); +Console.WriteLine( device.AdapterName ); +try { + ShaderRoundTrip( ); + if ( args.Contains( "--directml" ) ) DirectMlRoundTrip( ); + var diagnostics = device.GetDiagnostics( ); + foreach ( string message in diagnostics.Messages ) Console.WriteLine( message ); + if ( diagnostics.DeviceRemovedReason < 0 || diagnostics.Messages.Any( x => x.Contains( "SEVERITY_ERROR" ) || x.Contains( "SEVERITY_CORRUPTION" ) ) ) + throw new InvalidOperationException( "Graphics validation failed." ); + Console.WriteLine( "PASS: requested numerical qualifications completed." ); return 0; +} catch ( Exception error ) { + Console.Error.WriteLine( error ); + foreach ( string message in device.GetDiagnostics( ).Messages ) Console.Error.WriteLine( message ); + return 1; +} + +void ShaderRoundTrip( ) { + using var compiler = new ShaderCompiler( ); + var shader = compiler.Compile( "RWStructuredBuffer values : register(u0); [numthreads(1,1,1)] void Main() { values[0] = 1234; }", "Main", "cs_6_0" ).RequireSuccess( ); + using var queue = device.CreateCommandQueue( QueueKind.Compute ); using var allocator = device.CreateCommandAllocator( QueueKind.Compute ); + using var commands = device.CreateCommandList( allocator ); using var root = device.CreateRootSignature( 1 ); + using var pipeline = device.CreateComputePipeline( root, shader ); + using var output = device.CreateBuffer( 4, initialState: ResourceState.UnorderedAccess, unorderedAccess: true ); + using var readback = device.CreateBuffer( 4, BufferMemory.Readback ); + commands.SetComputePipeline( pipeline ); commands.SetComputeBuffer( 0, output ); commands.Dispatch( 1 ); + commands.UavBarrier( output ); commands.Transition( output, ResourceState.UnorderedAccess, ResourceState.CopySource ); + commands.CopyBuffer( output, readback, 4 ); commands.Close( ); + root.Dispose( ); pipeline.Dispose( ); output.Dispose( ); GC.Collect( ); GC.WaitForPendingFinalizers( ); + queue.Wait( queue.Submit( commands ) ); + Span value = stackalloc uint[1]; readback.Read( value ); + if ( value[0] != 1234 ) throw new InvalidOperationException( $"Shader readback mismatch: {value[0]}." ); + Console.WriteLine( "PASS: DXC -> compute -> readback = 1234, after early owner disposal and GC." ); +} +void DirectMlRoundTrip( ) { + using var ml = DirectMlDevice.Create( device ); using var add = ml.CompileAdd( new TensorShape( 1, 1, 1, 4 ) ); + using var queue = device.CreateCommandQueue( ); using var allocator = device.CreateCommandAllocator( ); + using var commands = device.CreateCommandList( allocator ); + using var left = device.CreateBuffer( 16, initialState: ResourceState.CopyDestination, unorderedAccess: true ); + using var right = device.CreateBuffer( 16, initialState: ResourceState.CopyDestination, unorderedAccess: true ); + using var output = device.CreateBuffer( 16, initialState: ResourceState.UnorderedAccess, unorderedAccess: true ); + using var upload = device.CreateBuffer( 32, BufferMemory.Upload ); + using var readback = device.CreateBuffer( 16, BufferMemory.Readback ); + upload.Write( [1, 2, 3, 4, 10, 20, 30, 40] ); + commands.CopyBuffer( upload, left, 16 ); commands.CopyBuffer( upload, right, 16, sourceOffset: 16 ); + commands.Transition( left, ResourceState.CopyDestination, ResourceState.UnorderedAccess ); + commands.Transition( right, ResourceState.CopyDestination, ResourceState.UnorderedAccess ); + commands.Close( ); upload.Dispose( ); GC.Collect( ); queue.Wait( queue.Submit( commands ) ); + // The operator owns its DirectML and graphics references independently of this facade. + ml.Dispose( ); add.Execute( queue, left, right, output ); + allocator.Reset( ); commands.Reset( allocator ); + commands.Transition( output, ResourceState.UnorderedAccess, ResourceState.CopySource ); + commands.CopyBuffer( output, readback, 16 ); commands.Close( ); queue.Wait( queue.Submit( commands ) ); + Span values = stackalloc float[4]; readback.Read( values ); + if ( !values.SequenceEqual( [11f, 22f, 33f, 44f] ) ) throw new InvalidOperationException( $"DirectML mismatch: {string.Join( ", ", values.ToArray( ) )}." ); + Console.WriteLine( "PASS: DirectML Add = [11, 22, 33, 44]." ); +} diff --git a/samples/ModernCompute/packages.lock.json b/samples/ModernCompute/packages.lock.json new file mode 100644 index 0000000..92072c3 --- /dev/null +++ b/samples/ModernCompute/packages.lock.json @@ -0,0 +1,39 @@ +{ + "version": 2, + "dependencies": { + "net10.0-windows10.0.26100": { + "dxsharp.core": { + "type": "Project", + "dependencies": { + "Microsoft.Direct3D.D3D12": "[1.620.1, 1.620.1]", + "Microsoft.Direct3D.DXC": "[1.9.2609.5, 1.9.2609.5]" + } + }, + "dxsharp.directml": { + "type": "Project", + "dependencies": { + "DXSharp.Core": "[0.4.0-net10-preview.1, )", + "Microsoft.AI.DirectML": "[1.15.4, 1.15.4]" + } + }, + "Microsoft.AI.DirectML": { + "type": "CentralTransitive", + "requested": "[1.15.4, )", + "resolved": "1.15.4", + "contentHash": "UblWBtQxcXcxwcn+jh0pfbq4zv2b6w7ThJ35qUV9gd8niGofrFWzVqZ6yqYDMiE+IJf8LRpG+H1cZpswceOeiA==" + }, + "Microsoft.Direct3D.D3D12": { + "type": "CentralTransitive", + "requested": "[1.620.1, )", + "resolved": "1.620.1", + "contentHash": "oQCFTys3PZ0IHtS3I/u0/1IWgpuG7aQa+84AGiraqc8bMOCM8QpDLwLBshEbJ1J7LmuctZReXhF8c0eCiu2Xcw==" + }, + "Microsoft.Direct3D.DXC": { + "type": "CentralTransitive", + "requested": "[1.9.2609.5, )", + "resolved": "1.9.2609.5", + "contentHash": "jTWr7a2bMpqV+b0Uc7oxOo8ualplaUw3fsQ+p0kp9TwlWvvmREvt3SA4+Drl2Y0M31iscqZolrL3vpdnLoPpdg==" + } + } + } +} \ No newline at end of file diff --git a/samples/ModernTriangle/ModernTriangle.csproj b/samples/ModernTriangle/ModernTriangle.csproj new file mode 100644 index 0000000..79f8dcf --- /dev/null +++ b/samples/ModernTriangle/ModernTriangle.csproj @@ -0,0 +1,9 @@ + + + Exe + net10.0-windows10.0.26100.0 + + + + + diff --git a/samples/ModernTriangle/Program.cs b/samples/ModernTriangle/Program.cs new file mode 100644 index 0000000..9f0b0f2 --- /dev/null +++ b/samples/ModernTriangle/Program.cs @@ -0,0 +1,190 @@ +using System.Text; +using System.Text.Json; +using DXSharp.Graphics; +using DXSharp.Shaders; + +namespace ModernTriangle; + +internal static class Program { + private const string Shader = """ + struct Vertex { float4 position : SV_Position; float3 color : COLOR; }; + Vertex VS(uint id : SV_VertexID) { + float2 positions[3] = { float2(0, 0.65), float2(0.65, -0.65), float2(-0.65, -0.65) }; + float3 colors[3] = { float3(1, 0.2, 0.3), float3(0.2, 1, 0.3), float3(0.2, 0.3, 1) }; + Vertex value; value.position = float4(positions[id], 0, 1); value.color = colors[id]; return value; + } + float4 PS(Vertex value) : SV_Target { return float4(value.color, 1); } + """; + + [STAThread] + public static int Main( string[] args ) { + GraphicsDevice? device = null; + string evidence = Path.GetFullPath( GetOption( args, "--evidence=" ) ?? "artifacts/modern-triangle" ); + Directory.CreateDirectory( evidence ); + try { + bool resizeTest = args.Contains( "--resize-test" ); + bool headless = args.Contains( "--headless" ); + int frameLimit = int.Parse( GetOption( args, "--frames=" ) ?? (resizeTest ? "4" : "0") ); + if ( frameLimit < 0 ) throw new ArgumentOutOfRangeException( nameof(frameLimit) ); + if ( resizeTest && (headless || frameLimit < 3) ) + throw new ArgumentException( "--resize-test requires a window and at least three frames." ); +#if DEBUG + const bool debugLayer = true; +#else + const bool debugLayer = false; +#endif + device = GraphicsDevice.Create( new( ) { + UseWarp = args.Contains( "--warp" ), UseAgility = !args.Contains( "--system-runtime" ), + EnableDebugLayer = debugLayer && !args.Contains( "--no-debug" ) + } ); + Console.WriteLine( device.AdapterName ); + WriteDiagnostics( device, evidence ); + using var compiler = new ShaderCompiler( ); + QualifyCompiler( compiler ); + ComputeRoundTrip( device, compiler ); + if ( !headless ) { + Application.SetUnhandledExceptionMode( UnhandledExceptionMode.ThrowException ); + ApplicationConfiguration.Initialize( ); + using var window = new Form { Text = "DXSharp · Generated COM · .NET 10", ClientSize = new( 1024, 720 ) }; + window.Show( ); + using var renderer = new Renderer( device, compiler, window ); + int frames = 0; + Size initialSize = window.ClientSize; + using var timer = new System.Windows.Forms.Timer { Interval = 1 }; + window.FormClosing += ( _, _ ) => timer.Stop( ); + timer.Tick += ( _, _ ) => { + if ( window.Disposing || !window.IsHandleCreated || window.WindowState == FormWindowState.Minimized ) return; + Size clientSize = window.ClientSize; + if ( clientSize.Width <= 0 || clientSize.Height <= 0 ) return; + // Native presentation can dispatch window messages. Do not render from a nested tick. + timer.Stop( ); + bool resized = renderer.Render( clientSize ); + if ( resizeTest ) { + if ( frames == 0 ) { + window.ClientSize = new Size( initialSize.Width + 64, initialSize.Height + 48 ); + } else if ( frames is 1 or 2 ) { + if ( !resized ) throw new InvalidOperationException( "The test window size change did not resize the swap chain." ); + if ( frames == 1 ) window.ClientSize = initialSize; + else Console.WriteLine( "PASS: rendered and presented after both swap-chain resizes." ); + } + } + if ( args.Contains( "--gc-stress" ) && frames % 30 == 0 ) { GC.Collect( ); GC.WaitForPendingFinalizers( ); } + ++frames; + if ( frameLimit > 0 && frames >= frameLimit ) window.Close( ); + else if ( window.IsHandleCreated && !window.Disposing ) timer.Start( ); + }; + // Propagate rendering failures to the outer evidence handler. + timer.Start( ); Application.Run( window ); timer.Stop( ); + if ( resizeTest && frames < 3 ) throw new InvalidOperationException( "The window closed before the resize test completed." ); + Console.WriteLine( $"Rendered {frames} frames." ); + } + var diagnostics = WriteDiagnostics( device, evidence ); + if ( diagnostics.DeviceRemovedReason < 0 || diagnostics.Messages.Any( x => x.Contains( "SEVERITY_ERROR" ) || x.Contains( "SEVERITY_CORRUPTION" ) ) ) + throw new InvalidOperationException( "The graphics validation layer reported errors; see diagnostics.json." ); + Console.WriteLine( "PASS: DXC, include callbacks, compute/readback, and requested rendering completed." ); + return 0; + } catch ( Exception error ) { + File.WriteAllText( Path.Combine( evidence, "failure.txt" ), error.ToString( ) ); + Console.Error.WriteLine( error ); + if ( device is not null ) { try { WriteDiagnostics( device, evidence ); } catch ( Exception diagnosticError ) { Console.Error.WriteLine( diagnosticError ); } } + return 1; + } finally { device?.Dispose( ); } + } + + private static string? GetOption( string[] args, string prefix ) => args.FirstOrDefault( x => x.StartsWith( prefix, StringComparison.Ordinal ) )?[prefix.Length..]; + private static GraphicsDiagnostics WriteDiagnostics( GraphicsDevice device, string directory ) { + var value = device.GetDiagnostics( ); + File.WriteAllText( Path.Combine( directory, "diagnostics.json" ), JsonSerializer.Serialize( value, new JsonSerializerOptions { WriteIndented = true } ) ); + return value; + } + private static void QualifyCompiler( ShaderCompiler compiler ) { + var failed = compiler.Compile( "not valid HLSL", "Main", "cs_6_0" ); + if ( failed.Succeeded || string.IsNullOrWhiteSpace( failed.Diagnostics ) ) throw new InvalidOperationException( "DXC failure diagnostics were lost." ); + int calls = 0; + var valid = compiler.Compile( "#include \"constant.hlsli\"\nRWStructuredBuffer output : register(u0); [numthreads(1,1,1)] void Main() { output[0] = VALUE; }", + "Main", "cs_6_0", includeResolver: name => { + if ( !name.EndsWith( "constant.hlsli", StringComparison.Ordinal ) ) throw new FileNotFoundException( name ); + ++calls; GC.Collect( ); return Encoding.UTF8.GetBytes( "#define VALUE 1234\n" ); + } ).RequireSuccess( ); + if ( calls == 0 || valid.Bytecode.IsEmpty ) throw new InvalidOperationException( "The managed include callback did not produce bytecode." ); + } + private static void ComputeRoundTrip( GraphicsDevice device, ShaderCompiler compiler ) { + using var queue = device.CreateCommandQueue( QueueKind.Compute ); + using var allocator = device.CreateCommandAllocator( QueueKind.Compute ); + using var commands = device.CreateCommandList( allocator ); + using var root = device.CreateRootSignature( 1 ); + var shader = compiler.Compile( "RWStructuredBuffer output : register(u0); [numthreads(1,1,1)] void Main() { output[0] = 1234; }", "Main", "cs_6_0" ).RequireSuccess( ); + using var pipeline = device.CreateComputePipeline( root, shader ); + using var output = device.CreateBuffer( 4, initialState: ResourceState.UnorderedAccess, unorderedAccess: true ); + using var readback = device.CreateBuffer( 4, BufferMemory.Readback ); + commands.SetComputePipeline( pipeline ); commands.SetComputeBuffer( 0, output ); commands.Dispatch( 1 ); + commands.UavBarrier( output ); commands.Transition( output, ResourceState.UnorderedAccess, ResourceState.CopySource ); + commands.CopyBuffer( output, readback, 4 ); commands.Close( ); + // Recording leases survive disposal of the public owners, before submission. + root.Dispose( ); pipeline.Dispose( ); output.Dispose( ); + GC.Collect( ); GC.WaitForPendingFinalizers( ); + queue.Wait( queue.Submit( commands ) ); + Span result = stackalloc uint[1]; readback.Read( result ); + if ( result[0] != 1234 ) throw new InvalidOperationException( $"Compute readback: expected 1234, got {result[0]}." ); + Console.WriteLine( "PASS: compute/readback = 1234; native dependencies survived public disposal and GC." ); + } + + private sealed class Renderer : IDisposable { + private readonly GraphicsDevice _device; + private readonly CommandQueue _queue; + private readonly CommandAllocator _allocator; + private readonly GraphicsCommandList _commands; + private readonly SwapChain _swap; + private readonly DescriptorHeap _rtvHeap; + private readonly RootSignature _root; + private readonly PipelineState _pipeline; + private readonly GpuResource?[] _buffers = new GpuResource?[2]; + private readonly RenderTargetView[] _views = new RenderTargetView[2]; + private uint _width, _height; + internal Renderer( GraphicsDevice device, ShaderCompiler compiler, Form window ) { + _device = device; + _width = (uint)window.ClientSize.Width; _height = (uint)window.ClientSize.Height; + _queue = device.CreateCommandQueue( ); _allocator = device.CreateCommandAllocator( ); + _commands = device.CreateCommandList( _allocator ); _commands.Close( ); + _swap = device.CreateSwapChain( _queue, window.Handle, _width, _height ); + _rtvHeap = device.CreateRenderTargetHeap( 2 ); _root = device.CreateRootSignature( ); + _pipeline = device.CreateGraphicsPipeline( _root, compiler.Compile( Shader, "VS", "vs_6_0" ), compiler.Compile( Shader, "PS", "ps_6_0" ) ); + AcquireBuffers( ); + } + private void AcquireBuffers( ) { + for ( uint i = 0; i < 2; ++i ) { + _buffers[i] = _swap.AcquireBackBuffer( i ); + _views[i] = _device.CreateRenderTargetView( _buffers[i]!, _rtvHeap, i ); + } + } + internal bool Render( Size clientSize ) { + uint width = (uint)clientSize.Width, height = (uint)clientSize.Height; + bool resized = width != _width || height != _height; + if ( resized ) _queue.WaitIdle( ); + _allocator.Reset( ); + _commands.Reset( _allocator ); + if ( resized ) { + foreach ( var buffer in _buffers ) buffer?.Dispose( ); + Array.Clear( _buffers ); + Array.Clear( _views ); + Console.WriteLine( $"Resizing swap chain from {_width}x{_height} to {width}x{height}." ); + _swap.Resize( width, height ); + _width = width; + _height = height; + AcquireBuffers( ); + } + uint index = _swap.CurrentBackBufferIndex; + _commands.Transition( _buffers[index]!, ResourceState.Present, ResourceState.RenderTarget ); + _commands.SetViewport( _width, _height ); _commands.Clear( _views[index], 0.025f, 0.035f, 0.065f ); + _commands.SetRenderTarget( _views[index] ); _commands.SetGraphicsPipeline( _pipeline ); _commands.Draw( 3 ); + _commands.Transition( _buffers[index]!, ResourceState.RenderTarget, ResourceState.Present ); + _commands.Close( ); _queue.Wait( _queue.Submit( _commands ) ); _swap.Present( ); + return resized; + } + public void Dispose( ) { + _queue.WaitIdle( ); _commands.Dispose( ); + foreach ( var buffer in _buffers ) buffer?.Dispose( ); Array.Clear( _views ); + _pipeline.Dispose( ); _root.Dispose( ); _rtvHeap.Dispose( ); _swap.Dispose( ); _allocator.Dispose( ); _queue.Dispose( ); + } + } +} diff --git a/samples/ModernTriangle/packages.lock.json b/samples/ModernTriangle/packages.lock.json new file mode 100644 index 0000000..d0ee3eb --- /dev/null +++ b/samples/ModernTriangle/packages.lock.json @@ -0,0 +1,26 @@ +{ + "version": 2, + "dependencies": { + "net10.0-windows10.0.26100": { + "dxsharp.core": { + "type": "Project", + "dependencies": { + "Microsoft.Direct3D.D3D12": "[1.620.1, 1.620.1]", + "Microsoft.Direct3D.DXC": "[1.9.2609.5, 1.9.2609.5]" + } + }, + "Microsoft.Direct3D.D3D12": { + "type": "CentralTransitive", + "requested": "[1.620.1, )", + "resolved": "1.620.1", + "contentHash": "oQCFTys3PZ0IHtS3I/u0/1IWgpuG7aQa+84AGiraqc8bMOCM8QpDLwLBshEbJ1J7LmuctZReXhF8c0eCiu2Xcw==" + }, + "Microsoft.Direct3D.DXC": { + "type": "CentralTransitive", + "requested": "[1.9.2609.5, )", + "resolved": "1.9.2609.5", + "contentHash": "jTWr7a2bMpqV+b0Uc7oxOo8ualplaUw3fsQ+p0kp9TwlWvvmREvt3SA4+Drl2Y0M31iscqZolrL3vpdnLoPpdg==" + } + } + } +} \ No newline at end of file diff --git a/src/DXSharp.Core/DXSharp.Core.csproj b/src/DXSharp.Core/DXSharp.Core.csproj new file mode 100644 index 0000000..89dd10e --- /dev/null +++ b/src/DXSharp.Core/DXSharp.Core.csproj @@ -0,0 +1,20 @@ + + + net10.0-windows10.0.26100.0 + true + true + true + $(BaseIntermediateOutputPath)$(Configuration)/generated + Source-generated DirectX foundation and deterministic ownership for DXSharp. + + + + + + + + + + + + diff --git a/src/DXSharp.Core/Graphics/Commands.cs b/src/DXSharp.Core/Graphics/Commands.cs new file mode 100644 index 0000000..31db923 --- /dev/null +++ b/src/DXSharp.Core/Graphics/Commands.cs @@ -0,0 +1,250 @@ +using Windows.Win32.Graphics.Direct3D; +using Windows.Win32.Graphics.Direct3D12; +using Windows.Win32.Foundation; +using DXSharp.Interop; + +namespace DXSharp.Graphics; + +/// A command-memory allocator with explicit pending-submission tracking. +public sealed class CommandAllocator : GraphicsObject { + internal readonly ComOwner Native; + internal int Pending; + /// The command-list type this allocator serves. + public QueueKind Kind { get; } + internal CommandAllocator( ComOwner owner, QueueKind kind ) : base( owner ) { Native = owner; Kind = kind; } + internal override ComReference Retain( ) => Native.Retain( ); + /// Reclaims command memory after all associated submissions complete. + public void Reset( ) { + if ( Volatile.Read( ref Pending ) != 0 ) throw new InvalidOperationException( "The allocator still has GPU work pending." ); + Results.Check( Native.Interface.Reset( ), "CommandAllocator.Reset" ); + } +} + +/// A generated-COM command list. Recording is single-threaded per list; independent lists share no scratch state. +public sealed unsafe class GraphicsCommandList : GraphicsObject { + internal readonly ComOwner Native; + internal CommandAllocator Allocator; + internal QueueKind Kind => Allocator.Kind; + private Dictionary _retained = new( ReferenceEqualityComparer.Instance ); + private readonly D3D12_RESOURCE_BARRIER[] _barrier = new D3D12_RESOURCE_BARRIER[1]; + private readonly float[] _color = new float[4]; + private int _state; // 0 recording, 1 closed, 2 submitted; reset becomes legal after retirement. + internal GraphicsCommandList( ComOwner owner, CommandAllocator allocator ) : base( owner ) { + Native = owner; Allocator = allocator; DeviceIdentity = allocator.DeviceIdentity; Track( allocator ); + } + internal override ComReference Retain( ) => Native.Retain( ); + private void Recording( ) { + ObjectDisposedException.ThrowIf( IsDisposed, this ); + if ( _state != 0 ) throw new InvalidOperationException( "The command list is not recording." ); + } + private void Track( GraphicsObject value ) { RequireSameDevice( value ); if ( !_retained.ContainsKey( value ) ) _retained.Add( value, value.Retain( ) ); } + + internal void RetainDependency( GraphicsObject value ) { Recording( ); Track( value ); } + internal void RetainNative( object key, ComReference lease ) { + Recording( ); if ( !_retained.TryAdd( key, lease ) ) lease.Dispose( ); + } + + /// Closes the current recording. + public void Close( ) { Recording( ); Results.Check( Native.Interface.Close( ), "CommandList.Close" ); _state = 1; } + /// Starts a new recording after its previous submission completes. Reset the allocator separately. + public void Reset( CommandAllocator allocator ) { + RequireSameDevice( allocator ); + if ( _state == 2 ) throw new InvalidOperationException( "The command list still has GPU work pending." ); + if ( allocator.Kind != Kind ) throw new ArgumentException( "The allocator kind does not match the command list." ); + Results.Check( Native.Interface.Reset( allocator.Native.Interface, null! ), "CommandList.Reset" ); + ReleaseRecording( ); Allocator = allocator; Track( allocator ); _state = 0; + } + /// Records a transition while retaining the native resource until submission retirement. + public void Transition( GpuResource resource, ResourceState before, ResourceState after ) { + Recording( ); Track( resource ); + using var lease = resource.Native.Borrow( ); + _barrier[0] = default; + _barrier[0].Type = D3D12_RESOURCE_BARRIER_TYPE.D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + _barrier[0].Anonymous.Transition = new( ) { + pResource = (ID3D12Resource_unmanaged*)lease.Pointer, + StateBefore = (D3D12_RESOURCE_STATES)before, StateAfter = (D3D12_RESOURCE_STATES)after, Subresource = uint.MaxValue + }; + Native.Interface.ResourceBarrier( 1, _barrier ); + } + /// Orders dependent UAV operations on a resource. + public void UavBarrier( GpuResource resource ) { + Recording( ); Track( resource ); using var lease = resource.Native.Borrow( ); + _barrier[0] = default; _barrier[0].Type = D3D12_RESOURCE_BARRIER_TYPE.D3D12_RESOURCE_BARRIER_TYPE_UAV; + _barrier[0].Anonymous.UAV.pResource = (ID3D12Resource_unmanaged*)lease.Pointer; + Native.Interface.ResourceBarrier( 1, _barrier ); + } + /// Copies a bounded buffer region. Supply appropriate source/destination states. + public void CopyBuffer( GpuResource source, GpuResource destination, ulong size, ulong sourceOffset = 0, ulong destinationOffset = 0 ) { + Recording( ); source.ValidateRange( sourceOffset, size ); destination.ValidateRange( destinationOffset, size ); + Track( source ); Track( destination ); + Native.Interface.CopyBufferRegion( destination.Native.Interface, destinationOffset, source.Native.Interface, sourceOffset, size ); + } + /// Sets a viewport and scissor rectangle. + public void SetViewport( uint width, uint height ) { + Recording( ); if ( width == 0 || height == 0 ) throw new ArgumentOutOfRangeException( nameof(width) ); + D3D12_VIEWPORT viewport = new( ) { Width = width, Height = height, MaxDepth = 1 }; + RECT scissor = new( ) { right = checked((int)width), bottom = checked((int)height) }; + Native.Interface.RSSetViewports( 1, &viewport ); Native.Interface.RSSetScissorRects( 1, &scissor ); + } + /// Clears a render target. + public void Clear( RenderTargetView target, float red, float green, float blue, float alpha = 1 ) { + Recording( ); Track( target.Heap ); Track( target.Resource ); + _color[0] = red; _color[1] = green; _color[2] = blue; _color[3] = alpha; + Native.Interface.ClearRenderTargetView( target.Handle, _color, 0, null ); + } + /// Binds a render target. + public void SetRenderTarget( RenderTargetView target ) { + Recording( ); Track( target.Heap ); Track( target.Resource ); + var handle = target.Handle; + Native.Interface.OMSetRenderTargets( 1, &handle, false, null ); + } + /// Binds a graphics root signature and pipeline. + public void SetGraphicsPipeline( PipelineState pipeline ) { + Recording( ); if ( pipeline.IsCompute ) throw new ArgumentException( "A graphics pipeline is required." ); + Track( pipeline ); + if ( !_retained.ContainsKey( pipeline.RootNative ) ) _retained.Add( pipeline.RootNative, pipeline.RootNative.Retain( ) ); + Native.Interface.SetPipelineState( pipeline.Native.Interface ); + Native.Interface.SetGraphicsRootSignature( pipeline.RootNative.Interface ); + Native.Interface.IASetPrimitiveTopology( D3D_PRIMITIVE_TOPOLOGY.D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); + } + /// Binds a compute root signature and pipeline. + public void SetComputePipeline( PipelineState pipeline ) { + Recording( ); if ( !pipeline.IsCompute ) throw new ArgumentException( "A compute pipeline is required." ); + Track( pipeline ); + if ( !_retained.ContainsKey( pipeline.RootNative ) ) _retained.Add( pipeline.RootNative, pipeline.RootNative.Retain( ) ); + Native.Interface.SetPipelineState( pipeline.Native.Interface ); + Native.Interface.SetComputeRootSignature( pipeline.RootNative.Interface ); + } + /// Binds a buffer to a compute root UAV. + public void SetComputeBuffer( uint parameterIndex, GpuResource buffer ) { + Recording( ); Track( buffer ); Native.Interface.SetComputeRootUnorderedAccessView( parameterIndex, buffer.GpuAddress ); + } + /// Draws a triangle-list vertex sequence. + public void Draw( uint vertexCount, uint instanceCount = 1 ) { Recording( ); Native.Interface.DrawInstanced( vertexCount, instanceCount, 0, 0 ); } + /// Records a compute dispatch. + public void Dispatch( uint x, uint y = 1, uint z = 1 ) { + Recording( ); if ( x == 0 || y == 0 || z == 0 ) throw new ArgumentOutOfRangeException( nameof(x) ); + Native.Interface.Dispatch( x, y, z ); + } + + internal List Submit( ) { + if ( _state != 1 ) throw new InvalidOperationException( "Close the command list before submission; retire previous submissions before reuse." ); + Track( this ); _state = 2; Interlocked.Increment( ref Allocator.Pending ); + var result = _retained.Values.ToList( ); _retained.Clear( ); return result; + } + internal void Retired( ) { _state = 1; Interlocked.Decrement( ref Allocator.Pending ); } + private void ReleaseRecording( ) { foreach ( var lease in _retained.Values ) lease.Dispose( ); _retained.Clear( ); } + /// + public override void Dispose( ) { ReleaseRecording( ); base.Dispose( ); } +} + +/// A queue whose submissions retain native resources through fence completion. Dispose to drain pending work. +public sealed class CommandQueue : GraphicsObject { + internal readonly ComOwner Native; + private readonly ComOwner _fence; + private readonly ComOwner _device; + private readonly AutoResetEvent _event = new( false ); + private readonly object _gate = new( ); + private readonly List _pending = new( ); + private readonly ID3D12CommandList[] _execute = new ID3D12CommandList[1]; + private ulong _nextValue; + /// The work type of this queue. + public QueueKind Kind { get; } + internal CommandQueue( ComOwner owner, ComOwner fence, GraphicsDevice device, QueueKind kind ) : base( owner ) { + Native = owner; _fence = fence; _device = device.Native.Clone( ); Kind = kind; + } + internal override ComReference Retain( ) => Native.Retain( ); + /// Submits a closed list and returns its monotonic completion value. + public ulong Submit( GraphicsCommandList commands ) { + RequireSameDevice( commands ); + if ( commands.Kind != Kind ) throw new ArgumentException( "The command-list kind does not match this queue." ); + lock ( _gate ) { + _ = Native.Interface; + ulong value = checked(_nextValue + 1); + var native = commands.Native.Interface; + var pending = new Pending( value, commands, commands.Submit( ) ); + _pending.Add( pending ); _nextValue = value; SubmissionRoots.Add( this ); + _execute[0] = native; + try { + Native.Interface.ExecuteCommandLists( 1, _execute ); + int result = Native.Interface.Signal( _fence.Interface, value ); + pending.SignalFailure = result; + Results.Check( result, "CommandQueue.Signal" ); + } finally { _execute[0] = null!; } + return value; + } + } + /// Waits for a submitted value. A timeout retains pending resources and prevents premature reuse. + public void Wait( ulong value, TimeSpan? timeout = null ) { + lock ( _gate ) { + if ( value > _nextValue ) throw new ArgumentOutOfRangeException( nameof(value) ); + Pending? failed = _pending.Find( x => x.Value <= value && x.SignalFailure < 0 ); + if ( failed is not null ) throw new GraphicsException( "CommandQueue.Signal", failed.SignalFailure ); + TimeSpan duration = timeout ?? TimeSpan.FromSeconds( 30 ); + if ( duration < TimeSpan.Zero || duration.TotalMilliseconds > int.MaxValue ) throw new ArgumentOutOfRangeException( nameof(timeout) ); + long started = Environment.TickCount64; + ulong completed = _fence.Interface.GetCompletedValue( ); + if ( completed < value ) Results.Check( _fence.Interface.SetEventOnCompletion( value, + new HANDLE( _event.SafeWaitHandle.DangerousGetHandle( ) ) ), "Fence.SetEventOnCompletion" ); + while ( completed < value ) { + double remaining = duration.TotalMilliseconds - (Environment.TickCount64 - started); + if ( remaining <= 0 || !_event.WaitOne( TimeSpan.FromMilliseconds( Math.Max( 0, remaining ) ) ) ) + throw new TimeoutException( $"GPU fence {value} did not complete; submission resources are still retained." ); + // A previous timed-out wait may have left an older registration on this event. + completed = _fence.Interface.GetCompletedValue( ); + } + if ( completed == ulong.MaxValue ) throw new GraphicsException( "Fence/device removed", _device.Interface.GetDeviceRemovedReason( ).Value ); + RetireThrough( completed ); + } + } + /// Signals a fresh fence and waits for earlier queue work, including swap-chain presents. + public void WaitIdle( ) { + lock ( _gate ) { + ulong value = checked(_nextValue + 1); + Results.Check( Native.Interface.Signal( _fence.Interface, value ), "CommandQueue.Signal(idle)" ); + _nextValue = value; + Wait( value ); + } + } + private void PollCompletion( ) { + if ( !Monitor.TryEnter( _gate ) ) return; + try { + if ( !IsDisposed ) RetireThrough( _fence.Interface.GetCompletedValue( ) ); + } finally { Monitor.Exit( _gate ); } + } + private void RetireThrough( ulong completed ) { + while ( _pending.Count > 0 && _pending[0].Value <= completed ) { + var item = _pending[0]; _pending.RemoveAt( 0 ); + item.Commands.Retired( ); foreach ( var lease in item.References ) lease.Dispose( ); + } + if ( _pending.Count == 0 ) SubmissionRoots.Remove( this ); + } + /// + public override void Dispose( ) { + lock ( _gate ) { + if ( IsDisposed ) return; + try { WaitIdle( ); } + catch ( GraphicsException ) when ( _device.Interface.GetDeviceRemovedReason( ).Value < 0 ) { RetireThrough( ulong.MaxValue ); } + _event.Dispose( ); _fence.Dispose( ); _device.Dispose( ); base.Dispose( ); + } + } + // Pending GPU work is an external root. Collection of a forgotten queue must not + // finalize the resources the GPU is still using. Polling never waits on a busy queue. + private static class SubmissionRoots { + private static readonly System.Collections.Concurrent.ConcurrentDictionary Queues = new( ); + private static readonly Timer Poller = new( _ => { + foreach ( var queue in Queues.Keys ) { + try { queue.PollCompletion( ); } + catch ( Exception error ) { System.Diagnostics.Debug.WriteLine( $"DXSharp retirement retained a queue after: {error}" ); } + } + }, null, 100, 100 ); + internal static void Add( CommandQueue queue ) { Queues.TryAdd( queue, 0 ); GC.KeepAlive( Poller ); } + internal static void Remove( CommandQueue queue ) => Queues.TryRemove( queue, out _ ); + } + private sealed class Pending( ulong value, GraphicsCommandList commands, List references ) { + internal readonly ulong Value = value; + internal readonly GraphicsCommandList Commands = commands; + internal readonly List References = references; + internal int SignalFailure; + } +} diff --git a/src/DXSharp.Core/Graphics/GraphicsDevice.cs b/src/DXSharp.Core/Graphics/GraphicsDevice.cs new file mode 100644 index 0000000..18a831d --- /dev/null +++ b/src/DXSharp.Core/Graphics/GraphicsDevice.cs @@ -0,0 +1,127 @@ +using System.Runtime.InteropServices; +using Windows.Win32; +using Windows.Win32.Foundation; +using Windows.Win32.Graphics.Direct3D; +using Windows.Win32.Graphics.Direct3D12; +using Windows.Win32.Graphics.Dxgi; +using DXSharp.Interop; + +namespace DXSharp.Graphics; + +/// A Direct3D 12 device backed entirely by generated COM interfaces. +public sealed unsafe partial class GraphicsDevice : GraphicsObject { + internal readonly ComOwner Native; + private readonly DeviceOptions _options; + /// The selected adapter description. + public string AdapterName { get; } + private GraphicsDevice( ComOwner owner, DeviceOptions options, string adapter ) : base( owner ) { + Native = owner; _options = options; AdapterName = adapter; + } + internal override ComReference Retain( ) => Native.Retain( ); + internal GraphicsDevice Clone( ) => new( Native.Clone( ), _options, AdapterName ) { DeviceIdentity = DeviceIdentity }; + + /// Creates a device without changing global Agility configuration. + public static GraphicsDevice Create( DeviceOptions? options = null ) { + NativeRuntime.Initialize( ); + options ??= new( ); + Guid factoryId = typeof(IDXGIFactory7).GUID; + Results.Check( PInvoke.CreateDXGIFactory2( 0, &factoryId, out nint factoryPointer ), "CreateDXGIFactory2" ); + using var factory = ComOwner.Adopt( factoryPointer ); + nint adapterPointer; + if ( options.UseWarp ) { + Guid adapterId = typeof(IDXGIAdapter4).GUID; + Results.Check( factory.Interface.EnumWarpAdapter( &adapterId, out adapterPointer ), "EnumWarpAdapter" ); + } else { + Guid adapterId = typeof(IDXGIAdapter4).GUID; + Results.Check( factory.Interface.EnumAdapterByGpuPreference( 0, DXGI_GPU_PREFERENCE.DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, + &adapterId, out adapterPointer ), "EnumAdapterByGpuPreference" ); + } + using var adapter = ComOwner.Adopt( adapterPointer ); + DXGI_ADAPTER_DESC3 description; + Results.Check( adapter.Interface.GetDesc3( &description ), "GetDesc3" ); + string name = description.Description.ToString( ); + Guid deviceId = typeof(ID3D12Device).GUID; + nint devicePointer = 0; + if ( options.UseAgility ) { + Guid configurationClass = new( "7cda6aca-a03e-49c8-9458-0334d20e07ce" ); + Guid configurationId = typeof(ID3D12SDKConfiguration1).GUID; + Results.Check( PInvoke.D3D12GetInterface( &configurationClass, &configurationId, out nint configurationPointer ), "D3D12GetInterface(SDKConfiguration1)" ); + using var configuration = ComOwner.Adopt( configurationPointer ); + Guid deviceFactoryId = typeof(ID3D12DeviceFactory).GUID; + string sdkPath = Path.Combine( AppContext.BaseDirectory, "D3D12" ) + Path.DirectorySeparatorChar; + using var path = new Utf8String( sdkPath ); + Results.Check( configuration.Interface.CreateDeviceFactory( 620, new PCSTR( path.Pointer ), &deviceFactoryId, out nint deviceFactoryPointer ), "CreateDeviceFactory(Agility 620)" ); + using var deviceFactory = ComOwner.Adopt( deviceFactoryPointer ); + if ( options.EnableDebugLayer ) { + Guid debugClass = new( "f2352aeb-dd84-49fe-b97b-a9dcfdcc1b4f" ), debugId = typeof(ID3D12Debug).GUID; + Results.Check( deviceFactory.Interface.GetConfigurationInterface( &debugClass, &debugId, out nint debugPointer ), "GetConfigurationInterface(Debug)" ); + using var debug = ComOwner.Adopt( debugPointer ); + debug.Interface.EnableDebugLayer( ); + } + Results.Check( deviceFactory.Interface.CreateDevice( adapter.Interface, D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, &deviceId, (void**)&devicePointer ), "DeviceFactory.CreateDevice" ); + } else { + if ( options.EnableDebugLayer ) { + Guid debugId = typeof(ID3D12Debug).GUID; + Results.Check( PInvoke.D3D12GetDebugInterface( &debugId, out nint debugPointer ), "D3D12GetDebugInterface" ); + using var debug = ComOwner.Adopt( debugPointer ); + debug.Interface.EnableDebugLayer( ); + } + Results.Check( PInvoke.D3D12CreateDevice( adapter.Interface, D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, &deviceId, out devicePointer ), "D3D12CreateDevice" ); + } + return new( ComOwner.Adopt( devicePointer ), options, name ); + } + + /// Creates a command allocator. Reset it only after all submissions using it complete. + public CommandAllocator CreateCommandAllocator( QueueKind kind = QueueKind.Direct ) { + Guid id = typeof(ID3D12CommandAllocator).GUID; + Results.Check( Native.Interface.CreateCommandAllocator( (D3D12_COMMAND_LIST_TYPE)kind, &id, out nint pointer ), "CreateCommandAllocator" ); + return new( ComOwner.Adopt( pointer ), kind ) { DeviceIdentity = DeviceIdentity }; + } + /// Creates an initially recording command list. + public GraphicsCommandList CreateCommandList( CommandAllocator allocator ) { + RequireSameDevice( allocator ); + Guid id = typeof(ID3D12GraphicsCommandList).GUID; + Results.Check( Native.Interface.CreateCommandList( 0, (D3D12_COMMAND_LIST_TYPE)allocator.Kind, allocator.Native.Interface, null!, &id, out nint pointer ), "CreateCommandList" ); + return new( ComOwner.Adopt( pointer ), allocator ) { DeviceIdentity = DeviceIdentity }; + } + /// Creates a queue with a dedicated completion fence. + public CommandQueue CreateCommandQueue( QueueKind kind = QueueKind.Direct ) { + D3D12_COMMAND_QUEUE_DESC description = new( ) { Type = (D3D12_COMMAND_LIST_TYPE)kind }; + Guid id = typeof(ID3D12CommandQueue).GUID; + Results.Check( Native.Interface.CreateCommandQueue( &description, &id, out nint pointer ), "CreateCommandQueue" ); + var owner = ComOwner.Adopt( pointer ); + try { + Guid fenceId = typeof(ID3D12Fence).GUID; + Results.Check( Native.Interface.CreateFence( 0, 0, &fenceId, out nint fencePointer ), "CreateFence" ); + return new( owner, ComOwner.Adopt( fencePointer ), this, kind ) { DeviceIdentity = DeviceIdentity }; + } catch { owner.Dispose( ); throw; } + } + + /// Returns a bounded snapshot of the device's validation messages and removal reason. + public GraphicsDiagnostics GetDiagnostics( ) { + var messages = new List( ); + if ( Native.TryQuery( typeof(ID3D12InfoQueue).GUID, out var owner ) ) { + using ( owner ) { + ulong count = owner!.Interface.GetNumStoredMessagesAllowedByRetrievalFilter( ); + for ( ulong i = count > 128 ? count - 128 : 0; i < count; ++i ) { + nuint length = 0; + Results.Check( owner.Interface.GetMessage( i, null, ref length ), "InfoQueue.GetMessage(size)" ); + if ( length > 1024 * 1024 ) continue; + void* buffer = NativeMemory.Alloc( length ); + try { + Results.Check( owner.Interface.GetMessage( i, (D3D12_MESSAGE*)buffer, ref length ), "InfoQueue.GetMessage" ); + D3D12_MESSAGE* message = (D3D12_MESSAGE*)buffer; + messages.Add( $"{message->Severity}: {Marshal.PtrToStringUTF8( (nint)message->pDescription, checked((int)message->DescriptionByteLength) ).TrimEnd('\0')}" ); + } finally { NativeMemory.Free( buffer ); } + } + } + } + return new( AdapterName, _options.UseAgility, Native.Interface.GetDeviceRemovedReason( ).Value, messages ); + } +} + +internal sealed unsafe class Utf8String : IDisposable { + internal byte* Pointer { get; private set; } + internal Utf8String( string value ) => Pointer = (byte*)Marshal.StringToCoTaskMemUTF8( value ); + public void Dispose( ) { Marshal.FreeCoTaskMem( (nint)Pointer ); Pointer = null; } +} diff --git a/src/DXSharp.Core/Graphics/GraphicsTypes.cs b/src/DXSharp.Core/Graphics/GraphicsTypes.cs new file mode 100644 index 0000000..32a7107 --- /dev/null +++ b/src/DXSharp.Core/Graphics/GraphicsTypes.cs @@ -0,0 +1,50 @@ +namespace DXSharp.Graphics; + +/// Formats supported by the first modern graphics surface. +public enum PixelFormat { Rgba8Unorm = 28, Bgra8Unorm = 87 } +/// The type of work a command queue accepts. +public enum QueueKind { Direct = 0, Compute = 2, Copy = 3 } +/// Explicit native resource states. +[Flags] +public enum ResourceState : uint { + Common = 0, Present = 0, VertexAndConstantBuffer = 1, RenderTarget = 4, UnorderedAccess = 8, + CopyDestination = 0x400, CopySource = 0x800, GenericRead = 0xac3 +} +/// CPU/GPU buffer placement. +public enum BufferMemory { Default = 1, Upload = 2, Readback = 3 } +/// Device selection and validation options. +public sealed record DeviceOptions { + /// Use the packaged Agility SDK through a local device factory. + public bool UseAgility { get; init; } = true; + /// Use Microsoft's software adapter for deterministic headless validation. + public bool UseWarp { get; init; } + /// Enable validation before creating the device. + public bool EnableDebugLayer { get; init; } +} +/// Diagnostics suitable for retaining alongside a crash dump. +public sealed record GraphicsDiagnostics( string Adapter, bool AgilityRequested, int DeviceRemovedReason, IReadOnlyList Messages ); + +/// Base for deterministic graphics owners. Dispose after their final CPU use; submissions retain their native references. +public abstract class GraphicsObject : IDisposable { + private IDisposable? _owner; + internal GraphicsObject( IDisposable owner ) => _owner = owner; + /// Whether this public owner has been disposed. + public bool IsDisposed => Volatile.Read( ref _owner ) is null; + internal object DeviceIdentity { get; init; } = new( ); + internal void RequireSameDevice( GraphicsObject other ) { + ObjectDisposedException.ThrowIf( IsDisposed, this ); + ArgumentNullException.ThrowIfNull( other ); + ObjectDisposedException.ThrowIf( other.IsDisposed, other ); + if ( !ReferenceEquals( DeviceIdentity, other.DeviceIdentity ) ) throw new ArgumentException( "Objects belong to different devices.", nameof(other) ); + } + internal abstract Interop.ComReference Retain( ); + /// Queries an independently owned native interface. Dispose the returned handle; do not manually Release its pointer. + public System.Runtime.InteropServices.SafeHandle QueryNativeInterface( Guid interfaceId ) { + using var owner = Retain( ); + int result = Interop.UnknownCalls.QueryInterface( owner.DangerousGetHandle( ), interfaceId, out nint pointer ); + Results.Check( result, "QueryInterface" ); + return new Interop.ComReference( pointer ); + } + /// Releases this owner's references. Native submission leases are independent. + public virtual void Dispose( ) => Interlocked.Exchange( ref _owner, null )?.Dispose( ); +} diff --git a/src/DXSharp.Core/Graphics/Pipelines.cs b/src/DXSharp.Core/Graphics/Pipelines.cs new file mode 100644 index 0000000..9e40422 --- /dev/null +++ b/src/DXSharp.Core/Graphics/Pipelines.cs @@ -0,0 +1,125 @@ +using System.Runtime.InteropServices; +using Windows.Win32.Graphics.Direct3D; +using Windows.Win32.Graphics.Direct3D12; +using Windows.Win32.Graphics.Dxgi.Common; +using DXSharp.Interop; +using DXSharp.Shaders; + +namespace DXSharp.Graphics; + +/// An owning root signature. +public sealed class RootSignature : GraphicsObject { + internal readonly ComOwner Native; + internal RootSignature( ComOwner owner ) : base( owner ) => Native = owner; + internal override ComReference Retain( ) => Native.Retain( ); +} + +/// An owning pipeline with an independent root-signature lease. +public sealed class PipelineState : GraphicsObject { + internal readonly ComOwner Native; + internal readonly ComOwner RootNative; + /// Whether the pipeline is a compute pipeline. + public bool IsCompute { get; } + internal PipelineState( ComOwner owner, ComOwner root, bool compute ) : base( owner ) { + Native = owner; RootNative = root; IsCompute = compute; + } + internal override ComReference Retain( ) => Native.Retain( ); + /// + public override void Dispose( ) { RootNative.Dispose( ); base.Dispose( ); } +} + +public sealed unsafe partial class GraphicsDevice { + [LibraryImport( "d3d12.dll", EntryPoint = "D3D12SerializeVersionedRootSignature" )] + private static partial int SerializeRoot( in D3D12_VERSIONED_ROOT_SIGNATURE_DESC description, out nint blob, out nint errors ); + + /// Creates an empty graphics signature, or a compute signature with root UAVs u0 through uN. + public RootSignature CreateRootSignature( uint computeBufferCount = 0 ) { + if ( computeBufferCount > 32 ) throw new ArgumentOutOfRangeException( nameof(computeBufferCount) ); + Span parameters = stackalloc D3D12_ROOT_PARAMETER[checked((int)computeBufferCount)]; + parameters.Clear( ); + for ( uint i = 0; i < computeBufferCount; ++i ) { + parameters[(int)i].ParameterType = D3D12_ROOT_PARAMETER_TYPE.D3D12_ROOT_PARAMETER_TYPE_UAV; + parameters[(int)i].Anonymous.Descriptor.ShaderRegister = i; + } + fixed ( D3D12_ROOT_PARAMETER* pointer = parameters ) { + D3D12_VERSIONED_ROOT_SIGNATURE_DESC description = default; + description.Version = D3D_ROOT_SIGNATURE_VERSION.D3D_ROOT_SIGNATURE_VERSION_1_0; + description.Anonymous.Desc_1_0 = new( ) { + NumParameters = computeBufferCount, pParameters = pointer, + Flags = computeBufferCount == 0 ? D3D12_ROOT_SIGNATURE_FLAGS.D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT : 0 + }; + int hr = SerializeRoot( description, out nint blobPointer, out nint errorPointer ); + try { + Results.Check( hr, "SerializeVersionedRootSignature" ); + nint owned = blobPointer; blobPointer = 0; + using var blob = ComOwner.Adopt( owned ); + Guid id = typeof(ID3D12RootSignature).GUID; + Results.Check( Native.Interface.CreateRootSignature( 0, blob.Interface.GetBufferPointer( ), blob.Interface.GetBufferSize( ), &id, out nint rootPointer ), "CreateRootSignature" ); + return new( ComOwner.Adopt( rootPointer ) ) { DeviceIdentity = DeviceIdentity }; + } finally { + if ( blobPointer != 0 ) UnknownCalls.Release( blobPointer ); + if ( errorPointer != 0 ) UnknownCalls.Release( errorPointer ); + } + } + } + + /// Creates a triangle-list graphics pipeline for SV_VertexID-generated vertices. + public PipelineState CreateGraphicsPipeline( RootSignature root, ShaderCompilation vertex, ShaderCompilation pixel, + PixelFormat targetFormat = PixelFormat.Rgba8Unorm ) { + RequireSameDevice( root ); + vertex.RequireSuccess( ); pixel.RequireSuccess( ); + if ( vertex.Bytecode.IsEmpty || pixel.Bytecode.IsEmpty ) throw new ArgumentException( "Both compiled shader objects are required." ); + using var lease = root.Native.Borrow( ); + fixed ( byte* vs = vertex.Bytecode.Span, ps = pixel.Bytecode.Span ) { + D3D12_GRAPHICS_PIPELINE_STATE_DESC description = default; + description.pRootSignature = (ID3D12RootSignature_unmanaged*)lease.Pointer; + description.VS = new( ) { pShaderBytecode = vs, BytecodeLength = (nuint)vertex.Bytecode.Length }; + description.PS = new( ) { pShaderBytecode = ps, BytecodeLength = (nuint)pixel.Bytecode.Length }; + description.SampleMask = uint.MaxValue; + description.RasterizerState.FillMode = D3D12_FILL_MODE.D3D12_FILL_MODE_SOLID; + description.RasterizerState.CullMode = D3D12_CULL_MODE.D3D12_CULL_MODE_NONE; + description.RasterizerState.DepthClipEnable = true; + description.BlendState.RenderTarget.AsSpan( )[0] = new( ) { + SrcBlend = D3D12_BLEND.D3D12_BLEND_ONE, DestBlend = D3D12_BLEND.D3D12_BLEND_ZERO, BlendOp = D3D12_BLEND_OP.D3D12_BLEND_OP_ADD, + SrcBlendAlpha = D3D12_BLEND.D3D12_BLEND_ONE, DestBlendAlpha = D3D12_BLEND.D3D12_BLEND_ZERO, BlendOpAlpha = D3D12_BLEND_OP.D3D12_BLEND_OP_ADD, + LogicOp = D3D12_LOGIC_OP.D3D12_LOGIC_OP_NOOP, RenderTargetWriteMask = 15 + }; + description.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC.D3D12_COMPARISON_FUNC_ALWAYS; + description.DepthStencilState.StencilReadMask = byte.MaxValue; + description.DepthStencilState.StencilWriteMask = byte.MaxValue; + description.DepthStencilState.FrontFace = new( ) { + StencilFailOp = D3D12_STENCIL_OP.D3D12_STENCIL_OP_KEEP, + StencilDepthFailOp = D3D12_STENCIL_OP.D3D12_STENCIL_OP_KEEP, + StencilPassOp = D3D12_STENCIL_OP.D3D12_STENCIL_OP_KEEP, + StencilFunc = D3D12_COMPARISON_FUNC.D3D12_COMPARISON_FUNC_ALWAYS + }; + description.DepthStencilState.BackFace = description.DepthStencilState.FrontFace; + description.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE.D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; + description.NumRenderTargets = 1; description.RTVFormats.AsSpan( )[0] = (DXGI_FORMAT)targetFormat; description.SampleDesc.Count = 1; + Guid id = typeof(ID3D12PipelineState).GUID; + Results.Check( Native.Interface.CreateGraphicsPipelineState( description, &id, out nint pointer ), "CreateGraphicsPipelineState" ); + return OwnPipeline( pointer, root, false ); + } + } + + /// Creates a compute pipeline with an independently owned root signature. + public PipelineState CreateComputePipeline( RootSignature root, ShaderCompilation shader ) { + RequireSameDevice( root ); + shader.RequireSuccess( ); if ( shader.Bytecode.IsEmpty ) throw new ArgumentException( "A compiled shader object is required." ); + using var lease = root.Native.Borrow( ); + fixed ( byte* bytes = shader.Bytecode.Span ) { + D3D12_COMPUTE_PIPELINE_STATE_DESC description = new( ) { + pRootSignature = (ID3D12RootSignature_unmanaged*)lease.Pointer, + CS = new( ) { pShaderBytecode = bytes, BytecodeLength = (nuint)shader.Bytecode.Length } + }; + Guid id = typeof(ID3D12PipelineState).GUID; + Results.Check( Native.Interface.CreateComputePipelineState( description, &id, out nint pointer ), "CreateComputePipelineState" ); + return OwnPipeline( pointer, root, true ); + } + } + private static PipelineState OwnPipeline( nint pointer, RootSignature root, bool compute ) { + var owner = ComOwner.Adopt( pointer ); + try { return new( owner, root.Native.Clone( ), compute ) { DeviceIdentity = root.DeviceIdentity }; } + catch { owner.Dispose( ); throw; } + } +} diff --git a/src/DXSharp.Core/Graphics/Resources.cs b/src/DXSharp.Core/Graphics/Resources.cs new file mode 100644 index 0000000..c4ab2c1 --- /dev/null +++ b/src/DXSharp.Core/Graphics/Resources.cs @@ -0,0 +1,120 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Windows.Win32.Graphics.Direct3D12; +using DXSharp.Interop; + +namespace DXSharp.Graphics; + +/// An owning GPU resource. Buffer read/write methods copy data within a scoped native mapping. +public sealed unsafe class GpuResource : GraphicsObject { + internal readonly ComOwner Native; + /// Whether this buffer was created for unordered access. + public bool SupportsUnorderedAccess { get; internal init; } + /// The size of a buffer; zero for swap-chain textures. + public ulong SizeInBytes { get; } + /// The memory placement of this resource. + public BufferMemory Memory { get; } + internal GpuResource( ComOwner owner, ulong size, BufferMemory memory ) : base( owner ) { + Native = owner; SizeInBytes = size; Memory = memory; + } + internal override ComReference Retain( ) => Native.Retain( ); + /// The buffer's GPU virtual address. The resource must remain live while the GPU uses it. + public ulong GpuAddress => Native.Interface.GetGPUVirtualAddress( ); + + /// Copies data into an upload buffer. Synchronize prior GPU reads before overwriting it. + public void Write( ReadOnlySpan source, ulong byteOffset = 0 ) where T : unmanaged { + if ( Memory != BufferMemory.Upload ) throw new InvalidOperationException( "Write requires an upload buffer." ); + ReadOnlySpan bytes = MemoryMarshal.AsBytes( source ); + ValidateRange( byteOffset, (ulong)bytes.Length ); + D3D12_RANGE readRange = default; + void* pointer = null; + Results.Check( Native.Interface.Map( 0, &readRange, &pointer ), "Resource.Map(write)" ); + try { bytes.CopyTo( new Span( (byte*)pointer + checked((nint)byteOffset), bytes.Length ) ); } + finally { + D3D12_RANGE written = new( ) { Begin = checked((nuint)byteOffset), End = checked((nuint)(byteOffset + (ulong)bytes.Length)) }; + Native.Interface.Unmap( 0, &written ); + } + } + + /// Copies data from a readback buffer after its GPU work has completed. + public void Read( Span destination, ulong byteOffset = 0 ) where T : unmanaged { + if ( Memory != BufferMemory.Readback ) throw new InvalidOperationException( "Read requires a readback buffer." ); + Span bytes = MemoryMarshal.AsBytes( destination ); + ValidateRange( byteOffset, (ulong)bytes.Length ); + D3D12_RANGE readRange = new( ) { Begin = checked((nuint)byteOffset), End = checked((nuint)(byteOffset + (ulong)bytes.Length)) }; + void* pointer = null; + Results.Check( Native.Interface.Map( 0, &readRange, &pointer ), "Resource.Map(read)" ); + try { new ReadOnlySpan( (byte*)pointer + checked((nint)byteOffset), bytes.Length ).CopyTo( bytes ); } + finally { D3D12_RANGE written = default; Native.Interface.Unmap( 0, &written ); } + } + + internal void ValidateRange( ulong offset, ulong size ) { + if ( offset > SizeInBytes || size > SizeInBytes - offset ) throw new ArgumentOutOfRangeException( nameof(size), "The buffer range exceeds its allocation." ); + } +} + +/// An owning descriptor allocation. +public sealed class DescriptorHeap : GraphicsObject { + internal readonly ComOwner Native; + internal readonly uint Stride; + /// The number of descriptors. + public uint Capacity { get; } + internal DescriptorHeap( ComOwner owner, uint capacity, uint stride ) : base( owner ) { + Native = owner; Capacity = capacity; Stride = stride; + } + internal override ComReference Retain( ) => Native.Retain( ); + internal D3D12_CPU_DESCRIPTOR_HANDLE CpuHandle( uint index ) { + if ( index >= Capacity ) throw new ArgumentOutOfRangeException( nameof(index) ); + var handle = Native.Interface.GetCPUDescriptorHandleForHeapStart( ); + handle.ptr = checked(handle.ptr + (nuint)index * Stride); + return handle; + } +} + +/// A render-target view associated with its descriptor heap and resource. +public readonly struct RenderTargetView { + internal readonly DescriptorHeap Heap; + internal readonly GpuResource Resource; + internal readonly uint Index; + internal RenderTargetView( DescriptorHeap heap, GpuResource resource, uint index ) { Heap = heap; Resource = resource; Index = index; } + internal D3D12_CPU_DESCRIPTOR_HANDLE Handle => (Heap ?? throw new InvalidOperationException( "The view is uninitialized." )).CpuHandle( Index ); +} + +public sealed unsafe partial class GraphicsDevice { + /// Allocates an explicit buffer. The caller manages resource-state transitions. + public GpuResource CreateBuffer( ulong size, BufferMemory memory = BufferMemory.Default, + ResourceState initialState = ResourceState.Common, bool unorderedAccess = false ) { + if ( size == 0 || size > (ulong)nint.MaxValue ) throw new ArgumentOutOfRangeException( nameof(size) ); + if ( !Enum.IsDefined( memory ) ) throw new ArgumentOutOfRangeException( nameof(memory) ); + if ( memory == BufferMemory.Upload ) initialState = ResourceState.GenericRead; + if ( memory == BufferMemory.Readback ) initialState = ResourceState.CopyDestination; + if ( unorderedAccess && memory != BufferMemory.Default ) throw new ArgumentException( "UAV buffers require default memory." ); + D3D12_HEAP_PROPERTIES heap = new( ) { Type = (D3D12_HEAP_TYPE)memory, CreationNodeMask = 1, VisibleNodeMask = 1 }; + D3D12_RESOURCE_DESC description = new( ) { + Dimension = D3D12_RESOURCE_DIMENSION.D3D12_RESOURCE_DIMENSION_BUFFER, Width = size, Height = 1, + DepthOrArraySize = 1, MipLevels = 1, SampleDesc = new( ) { Count = 1 }, + Layout = D3D12_TEXTURE_LAYOUT.D3D12_TEXTURE_LAYOUT_ROW_MAJOR, + Flags = unorderedAccess ? D3D12_RESOURCE_FLAGS.D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS : 0 + }; + Guid id = typeof(ID3D12Resource).GUID; + nint pointer = 0; + Results.Check( Native.Interface.CreateCommittedResource( &heap, 0, &description, (D3D12_RESOURCE_STATES)initialState, null, &id, (void**)&pointer ), "CreateCommittedResource" ); + return new( ComOwner.Adopt( pointer ), size, memory ) { DeviceIdentity = DeviceIdentity, SupportsUnorderedAccess = unorderedAccess }; + } + /// Creates a non-shader-visible render-target descriptor heap. + public DescriptorHeap CreateRenderTargetHeap( uint capacity ) { + if ( capacity == 0 ) throw new ArgumentOutOfRangeException( nameof(capacity) ); + D3D12_DESCRIPTOR_HEAP_DESC description = new( ) { Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_RTV, NumDescriptors = capacity }; + Guid id = typeof(ID3D12DescriptorHeap).GUID; + Results.Check( Native.Interface.CreateDescriptorHeap( &description, &id, out nint pointer ), "CreateDescriptorHeap" ); + return new( ComOwner.Adopt( pointer ), capacity, + Native.Interface.GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_RTV ) ) { DeviceIdentity = DeviceIdentity }; + } + /// Writes an RTV and returns a view carrying its lifetime dependencies. + public RenderTargetView CreateRenderTargetView( GpuResource resource, DescriptorHeap heap, uint index ) { + RequireSameDevice( resource ); RequireSameDevice( heap ); + Native.Interface.CreateRenderTargetView( resource.Native.Interface, null, heap.CpuHandle( index ) ); + GC.KeepAlive( resource ); + return new( heap, resource, index ); + } +} diff --git a/src/DXSharp.Core/Graphics/SwapChain.cs b/src/DXSharp.Core/Graphics/SwapChain.cs new file mode 100644 index 0000000..baf3c2f --- /dev/null +++ b/src/DXSharp.Core/Graphics/SwapChain.cs @@ -0,0 +1,68 @@ +using Windows.Win32; +using Windows.Win32.Foundation; +using Windows.Win32.Graphics.Direct3D12; +using Windows.Win32.Graphics.Dxgi; +using Windows.Win32.Graphics.Dxgi.Common; +using DXSharp.Interop; + +namespace DXSharp.Graphics; + +/// An owning flip-model HWND swap chain. +public sealed unsafe class SwapChain : GraphicsObject { + private readonly ComOwner _native; + /// The number of back buffers. + public uint BufferCount { get; } + /// The presentation format. + public PixelFormat Format { get; } + internal SwapChain( ComOwner owner, uint count, PixelFormat format ) : base( owner ) { _native = owner; BufferCount = count; Format = format; } + internal override ComReference Retain( ) => _native.Retain( ); + /// The current back-buffer index. + public uint CurrentBackBufferIndex => _native.Interface.GetCurrentBackBufferIndex( ); + /// Acquires an independently owned back-buffer resource. Dispose all such owners before resizing. + public GpuResource AcquireBackBuffer( uint index ) { + if ( index >= BufferCount ) throw new ArgumentOutOfRangeException( nameof(index) ); + Guid id = typeof(ID3D12Resource).GUID; + Results.Check( _native.Interface.GetBuffer( index, &id, out nint pointer ), "SwapChain.GetBuffer" ); + return new( ComOwner.Adopt( pointer ), 0, BufferMemory.Default ) { DeviceIdentity = DeviceIdentity }; + } + /// Presents, preserving non-failing statuses such as occlusion. + public int Present( uint syncInterval = 1 ) { + if ( syncInterval > 4 ) throw new ArgumentOutOfRangeException( nameof(syncInterval) ); + int hr = _native.Interface.Present( syncInterval, 0 ); Results.Check( hr, "SwapChain.Present" ); return hr; + } + /// Resizes after the caller drains the presentation queue with + /// and releases all back-buffer references. + public void Resize( uint width, uint height ) { + if ( width == 0 || height == 0 ) throw new ArgumentOutOfRangeException( nameof(width) ); + Results.Check( _native.Interface.ResizeBuffers( BufferCount, width, height, (DXGI_FORMAT)Format, 0 ), "SwapChain.ResizeBuffers" ); + } +} + +public sealed unsafe partial class GraphicsDevice { + /// Creates a swap chain for a direct command queue and a live HWND. + public SwapChain CreateSwapChain( CommandQueue queue, nint window, uint width, uint height, + PixelFormat format = PixelFormat.Rgba8Unorm, uint bufferCount = 2 ) { + RequireSameDevice( queue ); + if ( queue.Kind != QueueKind.Direct ) throw new ArgumentException( "Presentation requires a direct queue." ); + if ( window == 0 ) throw new ArgumentNullException( nameof(window) ); + if ( width == 0 || height == 0 || bufferCount < 2 || bufferCount > 16 ) throw new ArgumentOutOfRangeException( nameof(width) ); + Guid id = typeof(IDXGIFactory7).GUID; + Results.Check( PInvoke.CreateDXGIFactory2( 0, &id, out nint pointer ), "CreateDXGIFactory2" ); + using var factory = ComOwner.Adopt( pointer ); + DXGI_SWAP_CHAIN_DESC1 description = new( ) { + Width = width, Height = height, Format = (DXGI_FORMAT)format, + SampleDesc = new( ) { Count = 1 }, BufferUsage = DXGI_USAGE.DXGI_USAGE_RENDER_TARGET_OUTPUT, + BufferCount = bufferCount, SwapEffect = DXGI_SWAP_EFFECT.DXGI_SWAP_EFFECT_FLIP_DISCARD, + Scaling = DXGI_SCALING.DXGI_SCALING_STRETCH, AlphaMode = DXGI_ALPHA_MODE.DXGI_ALPHA_MODE_IGNORE + }; + factory.TryQuery( typeof(IFactory2Raw).GUID, out var rawFactory ); + using var raw = rawFactory ?? throw new NotSupportedException( "DXGI 1.2 is required." ); + using var queueLease = queue.Native.Borrow( ); + Results.Check( raw.Interface.CreateSwapChainForHwnd( queueLease.Pointer, new HWND( window ), description, null, 0, out nint swapPointer ), "CreateSwapChainForHwnd" ); + var swap = ComOwner.Adopt( swapPointer ); + try { + Results.Check( factory.Interface.MakeWindowAssociation( new HWND( window ), DXGI_MWA_FLAGS.DXGI_MWA_NO_ALT_ENTER ), "MakeWindowAssociation" ); + return new( swap, bufferCount, format ) { DeviceIdentity = DeviceIdentity }; + } catch { swap.Dispose( ); throw; } + } +} diff --git a/src/DXSharp.Core/Interop/ComOwner.cs b/src/DXSharp.Core/Interop/ComOwner.cs new file mode 100644 index 0000000..5e056c7 --- /dev/null +++ b/src/DXSharp.Core/Interop/ComOwner.cs @@ -0,0 +1,109 @@ +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; + +namespace DXSharp.Interop; + +// The only hand-written vtable calls in the modern core are the three invariant IUnknown slots. +internal static unsafe class UnknownCalls { + internal static int QueryInterface( nint value, in Guid iid, out nint result ) { + result = 0; + fixed ( Guid* id = &iid ) fixed ( nint* output = &result ) { + return ((delegate* unmanaged[MemberFunction])(*(nint**)value)[0])( value, id, output ); + } + } + internal static uint AddRef( nint value ) => ((delegate* unmanaged[MemberFunction])(*(nint**)value)[1])( value ); + internal static uint Release( nint value ) => ((delegate* unmanaged[MemberFunction])(*(nint**)value)[2])( value ); +} + +internal sealed class ComReference : SafeHandle { + internal ComReference( nint owned ) : base( 0, true ) => SetHandle( owned ); + public override bool IsInvalid => handle == 0; + protected override bool ReleaseHandle( ) { UnknownCalls.Release( handle ); return true; } +} + +/// Owns one native interface reference and one private generated projection. +/// Callers synchronize disposal with method calls. A borrow keeps its native reference live during disposal. +internal sealed class ComOwner : IDisposable where T : class { + private static readonly StrategyBasedComWrappers Wrappers = new( ); + private readonly ComReference _native; + private readonly ComObject _projection; + private readonly T _interface; + private int _disposed; + + private ComOwner( ComReference native, ComObject projection, T value ) { + _native = native; + _projection = projection; + _interface = value; + } + + internal T Interface { + get { ObjectDisposedException.ThrowIf( Volatile.Read( ref _disposed ) != 0, this ); return _interface; } + } + internal bool IsDisposed => Volatile.Read( ref _disposed ) != 0; + + internal static ComOwner Adopt( nint owned ) { + if ( owned == 0 ) throw new ArgumentNullException( nameof(owned) ); + var native = new ComReference( owned ); + ComObject? projection = null; + try { + projection = (ComObject)Wrappers.GetOrCreateObjectForComInstance( owned, CreateObjectFlags.UniqueInstance ); + return new( native, projection, (T)(object)projection ); + } catch { + projection?.FinalRelease( ); + native.Dispose( ); + throw; + } + } + + internal static ComOwner FromBorrowed( nint borrowed ) { + if ( borrowed == 0 ) throw new ArgumentNullException( nameof(borrowed) ); + UnknownCalls.AddRef( borrowed ); + return Adopt( borrowed ); + } + + internal NativeBorrow Borrow( ) { + ObjectDisposedException.ThrowIf( IsDisposed, this ); + return new( _native ); + } + + internal ComOwner Clone( ) { + using var lease = Borrow( ); + return FromBorrowed( lease.Pointer ); + } + + internal ComReference Retain( ) { + using var lease = Borrow( ); + UnknownCalls.AddRef( lease.Pointer ); + return new( lease.Pointer ); + } + + internal bool TryQuery( in Guid iid, out ComOwner? result ) where TInterface : class { + using var lease = Borrow( ); + int hr = UnknownCalls.QueryInterface( lease.Pointer, iid, out nint pointer ); + if ( hr < 0 ) { + result = null; + if ( hr != unchecked((int)0x80004002) ) Results.Check( hr, "QueryInterface" ); + return false; + } + result = ComOwner.Adopt( pointer ); + return true; + } + + public void Dispose( ) { + if ( Interlocked.Exchange( ref _disposed, 1 ) != 0 ) return; + try { _projection.FinalRelease( ); } + finally { _native.Dispose( ); } + } +} + +internal ref struct NativeBorrow { + private ComReference? _owner; + internal nint Pointer { get; private set; } + internal NativeBorrow( ComReference owner ) { + bool acquired = false; + owner.DangerousAddRef( ref acquired ); + _owner = owner; + Pointer = owner.DangerousGetHandle( ); + } + public void Dispose( ) { var owner = _owner; _owner = null; Pointer = 0; owner?.DangerousRelease( ); } +} diff --git a/src/DXSharp.Core/Interop/Factory2Raw.cs b/src/DXSharp.Core/Interop/Factory2Raw.cs new file mode 100644 index 0000000..b6f247a --- /dev/null +++ b/src/DXSharp.Core/Interop/Factory2Raw.cs @@ -0,0 +1,17 @@ +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Windows.Win32.Foundation; +using Windows.Win32.Graphics.Dxgi; + +namespace DXSharp.Interop; + +// CsWin32's typed IDXGISwapChain1 output uses a shared projection. This narrow +// generated declaration preserves the native output reference for unique ownership. +// All inherited slots come from the metadata-generated IDXGIFactory1 declaration. +[GeneratedComInterface( Options = ComInterfaceOptions.ComObjectWrapper )] +[Guid( "50c83a1c-e072-4c48-87b0-3630fa36a6d0" )] +internal unsafe partial interface IFactory2Raw : IDXGIFactory1 { + [PreserveSig] BOOL IsWindowedStereoEnabled( ); + [PreserveSig] int CreateSwapChainForHwnd( nint device, HWND window, in DXGI_SWAP_CHAIN_DESC1 description, + DXGI_SWAP_CHAIN_FULLSCREEN_DESC* fullscreen, nint output, out nint swapChain ); +} diff --git a/src/DXSharp.Core/Interop/GraphicsException.cs b/src/DXSharp.Core/Interop/GraphicsException.cs new file mode 100644 index 0000000..8455a04 --- /dev/null +++ b/src/DXSharp.Core/Interop/GraphicsException.cs @@ -0,0 +1,16 @@ +namespace DXSharp; + +/// A native graphics failure, preserving its HRESULT and operation. +public sealed class GraphicsException : Exception { + /// The operation which failed. + public string Operation { get; } + /// Creates an exception from a failing native result. + public GraphicsException( string operation, int result ) : base( $"{operation} failed (HRESULT 0x{result:X8})." ) { + Operation = operation; + HResult = result; + } +} + +internal static class Results { + internal static void Check( int result, string operation ) { if ( result < 0 ) throw new GraphicsException( operation, result ); } +} diff --git a/src/DXSharp.Core/Interop/NativeRuntime.cs b/src/DXSharp.Core/Interop/NativeRuntime.cs new file mode 100644 index 0000000..93a23ba --- /dev/null +++ b/src/DXSharp.Core/Interop/NativeRuntime.cs @@ -0,0 +1,31 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +namespace DXSharp.Interop; + +internal static class NativeRuntime { + private static readonly Dictionary Modules = new( StringComparer.OrdinalIgnoreCase ); + private static readonly object Gate = new( ); + static NativeRuntime( ) => NativeLibrary.SetDllImportResolver( typeof(NativeRuntime).Assembly, Resolve ); + + internal static void Initialize( ) { + if ( !OperatingSystem.IsWindows( ) ) throw new PlatformNotSupportedException( "DirectX execution requires Windows." ); + } + + private static nint Resolve( string name, Assembly assembly, DllImportSearchPath? searchPath ) { + string file = name.EndsWith( ".dll", StringComparison.OrdinalIgnoreCase ) ? name : name + ".dll"; + string? path = file.ToLowerInvariant( ) switch { + "d3d12.dll" or "dxgi.dll" => Path.Combine( Environment.SystemDirectory, file ), + "dxcompiler.dll" or "dxil.dll" or "directml.dll" => Path.Combine( AppContext.BaseDirectory, file ), + _ => null + }; + if ( path is null ) return 0; + lock ( Gate ) { + if ( Modules.TryGetValue( path, out nint module ) ) return module; + if ( !File.Exists( path ) ) throw new DllNotFoundException( $"Required native dependency was not found: {path}" ); + module = NativeLibrary.Load( path ); + Modules.Add( path, module ); + return module; // Modules remain loaded until process exit, including while finalizers run. + } + } +} diff --git a/src/DXSharp.Core/NativeMethods.json b/src/DXSharp.Core/NativeMethods.json new file mode 100644 index 0000000..4b9956f --- /dev/null +++ b/src/DXSharp.Core/NativeMethods.json @@ -0,0 +1,12 @@ +{ + "$schema": "https://aka.ms/CsWin32.schema.json", + "allowMarshaling": true, + "emitSingleFile": false, + "public": false, + "comInterop": { + "useComSourceGenerators": true, + "autoWinRTMarshalling": false, + "useIntPtrForComOutPointers": true, + "preserveSigMethods": ["*"] + } +} diff --git a/src/DXSharp.Core/NativeMethods.txt b/src/DXSharp.Core/NativeMethods.txt new file mode 100644 index 0000000..801e2b6 --- /dev/null +++ b/src/DXSharp.Core/NativeMethods.txt @@ -0,0 +1,41 @@ +CreateDXGIFactory2 +DXGIGetDebugInterface1 +D3D12CreateDevice +D3D12GetDebugInterface +D3D12GetInterface +D3D12SerializeVersionedRootSignature +IDXGIFactory7 +IDXGIAdapter4 +IDXGISwapChain4 +IDXGIDebug1 +ID3D12Device12 +ID3D12GraphicsCommandList9 +ID3D12CommandQueue +ID3D12CommandAllocator +ID3D12Resource2 +ID3D12Fence1 +ID3D12DescriptorHeap +ID3D12PipelineState +ID3D12RootSignature +ID3D12SDKConfiguration1 +ID3D12DeviceFactory +ID3D12DeviceConfiguration +ID3D12InfoQueue1 +ID3D12Debug6 +DxcCreateInstance +IDxcUtils +IDxcCompiler3 +IDxcResult +IDxcBlobUtf8 +IDxcBlobUtf16 +IDxcIncludeHandler +DMLCreateDevice1 +IDMLDevice1 +IDMLCommandRecorder +IDMLBindingTable +IDMLOperatorInitializer +IDMLCompiledOperator +DML_BUFFER_TENSOR_DESC +DML_ELEMENT_WISE_ADD_OPERATOR_DESC +DML_GEMM_OPERATOR_DESC +DML_BUFFER_BINDING diff --git a/src/DXSharp.Core/Shaders/ShaderCompiler.cs b/src/DXSharp.Core/Shaders/ShaderCompiler.cs new file mode 100644 index 0000000..78fa980 --- /dev/null +++ b/src/DXSharp.Core/Shaders/ShaderCompiler.cs @@ -0,0 +1,126 @@ +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using System.Text; +using Windows.Win32; +using Windows.Win32.Foundation; +using Windows.Win32.Graphics.Direct3D.Dxc; +using DXSharp.Interop; + +namespace DXSharp.Shaders; + +/// Copied, independently owned compilation outputs. Diagnostics include warnings on success. +public sealed record ShaderCompilation( int Status, ReadOnlyMemory Bytecode, string Diagnostics, IReadOnlyDictionary> Outputs ) { + /// Whether HLSL compilation succeeded. + public bool Succeeded => Status >= 0; + /// Throws with compiler diagnostics if the shader did not compile. + public ShaderCompilation RequireSuccess( ) { + if ( !Succeeded ) throw new InvalidOperationException( $"HLSL compilation failed (0x{Status:X8}):{Environment.NewLine}{Diagnostics}" ); + return this; + } +} + +/// A synchronous DXC compiler using generated COM, scoped source storage, and copied outputs. +public sealed unsafe class ShaderCompiler : IDisposable { + private readonly ComOwner _compiler; + private readonly object _gate = new( ); + /// Loads the packaged DXC compiler without depending on the working directory. + public ShaderCompiler( ) { + NativeRuntime.Initialize( ); + Guid compilerClass = new( "73e22d93-e6ce-47f3-b5bf-f0664f39c1b0" ), compilerId = typeof(IDxcCompiler3).GUID; + Results.Check( PInvoke.DxcCreateInstance( &compilerClass, &compilerId, out nint pointer ), "DxcCreateInstance(Compiler3)" ); + _compiler = ComOwner.Adopt( pointer ); + } + + /// Compiles UTF-8 HLSL. Includes are resolved by the supplied callback; each output owns its bytes. + public ShaderCompilation Compile( string source, string entryPoint, string profile, IEnumerable? arguments = null, + Func>? includeResolver = null ) { + ArgumentNullException.ThrowIfNull( source ); + ArgumentException.ThrowIfNullOrWhiteSpace( entryPoint ); ArgumentException.ThrowIfNullOrWhiteSpace( profile ); + var options = new List( ) { "-E", entryPoint, "-T", profile }; + if ( arguments is not null ) options.AddRange( arguments ); + if ( options.Any( value => string.IsNullOrEmpty( value ) || value.Contains( '\0' ) ) ) throw new ArgumentException( "Compiler arguments cannot be empty or contain NUL." ); + byte[] bytes = Encoding.UTF8.GetBytes( source ); + using var nativeArguments = new Arguments( options ); + using var includes = includeResolver is null ? null : new IncludeHandler( includeResolver ); + lock ( _gate ) { + fixed ( byte* input = bytes ) fixed ( nint* args = nativeArguments.Values ) { + DxcBuffer buffer = new( ) { Ptr = input, Size = (nuint)bytes.Length, Encoding = 65001 }; + Guid resultId = typeof(IDxcResult).GUID; + Results.Check( _compiler.Interface.Compile( &buffer, (PCWSTR*)args, (uint)options.Count, includes!, &resultId, out nint resultPointer ), "Compiler3.Compile" ); + using var result = ComOwner.Adopt( resultPointer ); + HRESULT status; + Results.Check( result.Interface.GetStatus( &status ), "DxcResult.GetStatus" ); + var outputs = new Dictionary>( StringComparer.Ordinal ); + foreach ( var (kind, name) in OutputKinds ) { + if ( !result.Interface.HasOutput( kind ) ) continue; + outputs.Add( name, CopyOutput( result.Interface, kind ) ); + } + outputs.TryGetValue( "Object", out var bytecode ); + outputs.TryGetValue( "Errors", out var errors ); + return new( status.Value, bytecode, Encoding.UTF8.GetString( errors.Span ).TrimEnd( '\0' ), outputs ); + } + } + } + + private static readonly (DXC_OUT_KIND Kind, string Name)[] OutputKinds = [ + (DXC_OUT_KIND.DXC_OUT_OBJECT, "Object"), (DXC_OUT_KIND.DXC_OUT_ERRORS, "Errors"), + (DXC_OUT_KIND.DXC_OUT_REFLECTION, "Reflection"), (DXC_OUT_KIND.DXC_OUT_PDB, "Pdb"), + (DXC_OUT_KIND.DXC_OUT_SHADER_HASH, "Hash"), (DXC_OUT_KIND.DXC_OUT_ROOT_SIGNATURE, "RootSignature"), + (DXC_OUT_KIND.DXC_OUT_DISASSEMBLY, "Disassembly") + ]; + private static byte[] CopyOutput( IDxcResult result, DXC_OUT_KIND kind ) { + Guid blobId = typeof(IDxcBlob).GUID; + nint pointer = 0, name = 0; + int hr = result.GetOutput( kind, &blobId, (void**)&pointer, (IDxcBlobUtf16_unmanaged**)&name ); + try { + Results.Check( hr, $"DxcResult.GetOutput({kind})" ); + nint owned = pointer; pointer = 0; + using var blob = ComOwner.Adopt( owned ); + return new ReadOnlySpan( blob.Interface.GetBufferPointer( ), checked((int)blob.Interface.GetBufferSize( )) ).ToArray( ); + } finally { + if ( pointer != 0 ) UnknownCalls.Release( pointer ); + if ( name != 0 ) UnknownCalls.Release( name ); + } + } + /// Releases compiler references after any current compilation returns. + public void Dispose( ) { lock ( _gate ) _compiler.Dispose( ); } + + private sealed class Arguments : IDisposable { + internal readonly nint[] Values; + internal Arguments( List values ) { + Values = new nint[values.Count]; + try { for ( int i = 0; i < values.Count; ++i ) Values[i] = Marshal.StringToCoTaskMemUni( values[i] ); } + catch { Dispose( ); throw; } + } + public void Dispose( ) { foreach ( nint pointer in Values ) Marshal.FreeCoTaskMem( pointer ); Array.Clear( Values ); } + } +} + +[GeneratedComClass] +internal sealed unsafe partial class IncludeHandler( Func> resolver ) : IDxcIncludeHandler, IDisposable { + private readonly List _sources = new( ); + public HRESULT LoadSource( PCWSTR filename, out IDxcBlob source ) { + source = null!; + try { + lock ( _sources ) { + var blob = new ShaderSourceBlob( resolver( filename.ToString( ) ).Span ); + _sources.Add( blob ); source = blob; return new HRESULT( 0 ); + } + } catch ( Exception error ) { return new HRESULT( error.HResult < 0 ? error.HResult : unchecked((int)0x80004005) ); } + } + public void Dispose( ) { lock ( _sources ) { foreach ( var source in _sources ) source.Dispose( ); _sources.Clear( ); } } +} + +[GeneratedComClass] +internal sealed unsafe partial class ShaderSourceBlob : IDxcBlob, IDisposable { + private nint _pointer; + private readonly nuint _length; + internal ShaderSourceBlob( ReadOnlySpan source ) { + _length = (nuint)source.Length; _pointer = (nint)NativeMemory.Alloc( checked(_length + 1) ); + source.CopyTo( new Span( (void*)_pointer, source.Length ) ); ((byte*)_pointer)[source.Length] = 0; + } + public void* GetBufferPointer( ) => (void*)_pointer; + public nuint GetBufferSize( ) => _length; + ~ShaderSourceBlob( ) => Dispose( ); + public void Dispose( ) { NativeMemory.Free( (void*)Interlocked.Exchange( ref _pointer, 0 ) ); GC.SuppressFinalize( this ); } +} diff --git a/src/DXSharp.Core/packages.lock.json b/src/DXSharp.Core/packages.lock.json new file mode 100644 index 0000000..9eda45a --- /dev/null +++ b/src/DXSharp.Core/packages.lock.json @@ -0,0 +1,49 @@ +{ + "version": 2, + "dependencies": { + "net10.0-windows10.0.26100": { + "Microsoft.Direct3D.D3D12": { + "type": "Direct", + "requested": "[1.620.1, 1.620.1]", + "resolved": "1.620.1", + "contentHash": "oQCFTys3PZ0IHtS3I/u0/1IWgpuG7aQa+84AGiraqc8bMOCM8QpDLwLBshEbJ1J7LmuctZReXhF8c0eCiu2Xcw==" + }, + "Microsoft.Direct3D.DXC": { + "type": "Direct", + "requested": "[1.9.2609.5, 1.9.2609.5]", + "resolved": "1.9.2609.5", + "contentHash": "jTWr7a2bMpqV+b0Uc7oxOo8ualplaUw3fsQ+p0kp9TwlWvvmREvt3SA4+Drl2Y0M31iscqZolrL3vpdnLoPpdg==" + }, + "Microsoft.Windows.CsWin32": { + "type": "Direct", + "requested": "[0.3.367, )", + "resolved": "0.3.367", + "contentHash": "C4JVE3wmVjCvzpdBzC+HK/boVU6g4ZRDEZ7uDmMJHCkgjFHhiMb9vhe2+wffzWzyNNObpCsIAW4G1t+1CAIEWw==", + "dependencies": { + "Microsoft.Windows.SDK.Win32Docs": "0.1.42-alpha", + "Microsoft.Windows.SDK.Win32Metadata": "71.0.37-preview", + "Microsoft.Windows.WDK.Win32Metadata": "0.13.25-experimental" + } + }, + "Microsoft.Windows.SDK.Win32Metadata": { + "type": "Direct", + "requested": "[71.0.37-preview, )", + "resolved": "71.0.37-preview", + "contentHash": "0y4uKnOzE+k2zUCxcTOJTOuRuOFiW01A+j1GpjyN11saEjkESLW9XrWkGYG/zdlAAb8oRk4oLdokkcx0X2z7yA==" + }, + "Microsoft.Windows.SDK.Win32Docs": { + "type": "Transitive", + "resolved": "0.1.42-alpha", + "contentHash": "Z/9po23gUA9aoukirh2ItMU2ZS9++Js9Gdds9fu5yuMojDrmArvY2y+tq9985tR3cxFxpZO1O35Wjfo0khj5HA==" + }, + "Microsoft.Windows.WDK.Win32Metadata": { + "type": "Transitive", + "resolved": "0.13.25-experimental", + "contentHash": "IM50tb/+UIwBr9FMr6ZKcZjCMW+Axo6NjGqKxgjUfyCY8dRnYUfrJEXxAaXoWtYP4X8EmASmC1Jtwh4XucseZg==", + "dependencies": { + "Microsoft.Windows.SDK.Win32Metadata": "63.0.31-preview" + } + } + } + } +} \ No newline at end of file diff --git a/src/DXSharp.DirectML/DXSharp.DirectML.csproj b/src/DXSharp.DirectML/DXSharp.DirectML.csproj new file mode 100644 index 0000000..22c631f --- /dev/null +++ b/src/DXSharp.DirectML/DXSharp.DirectML.csproj @@ -0,0 +1,12 @@ + + + net10.0-windows10.0.26100.0 + true + DirectML tensor operators for the DXSharp generated-COM foundation. + + + + + + + diff --git a/src/DXSharp.DirectML/DirectMlDevice.cs b/src/DXSharp.DirectML/DirectMlDevice.cs new file mode 100644 index 0000000..6a585a0 --- /dev/null +++ b/src/DXSharp.DirectML/DirectMlDevice.cs @@ -0,0 +1,49 @@ +using Windows.Win32; +using Windows.Win32.AI.MachineLearning.DirectML; +using DXSharp.Graphics; +using DXSharp.Interop; + +namespace DXSharp.MachineLearning; + +/// A DirectML device with an independently owned reference to its Direct3D device. +public sealed unsafe class DirectMlDevice : GraphicsObject { + internal readonly ComOwner Native; + internal readonly GraphicsDevice Graphics; + private DirectMlDevice( ComOwner owner, GraphicsDevice graphics ) : base( owner ) { Native = owner; Graphics = graphics; } + internal override ComReference Retain( ) => Native.Retain( ); + /// Creates a DirectML device from the same adapter as an existing graphics device. + public static DirectMlDevice Create( GraphicsDevice graphics, bool enableDebugLayer = false ) { + ArgumentNullException.ThrowIfNull( graphics ); + Guid id = typeof(IDMLDevice1).GUID; + Results.Check( PInvoke.DMLCreateDevice1( graphics.Native.Interface, + enableDebugLayer ? DML_CREATE_DEVICE_FLAGS.DML_CREATE_DEVICE_FLAG_DEBUG : 0, + DML_FEATURE_LEVEL.DML_FEATURE_LEVEL_1_0, &id, out nint pointer ), "DMLCreateDevice1" ); + var owner = ComOwner.Adopt( pointer ); + try { return new( owner, graphics.Clone( ) ) { DeviceIdentity = graphics.DeviceIdentity }; } + catch { owner.Dispose( ); throw; } + } + /// Compiles A + B for three equal, dense Float32 tensors. Broadcasting is deliberately explicit in future operator overloads. + public TensorAddOperator CompileAdd( TensorShape shape ) { + ArgumentNullException.ThrowIfNull( shape ); + fixed ( uint* sizes = shape.Dimensions ) { + DML_BUFFER_TENSOR_DESC buffer = new( ) { + DataType = DML_TENSOR_DATA_TYPE.DML_TENSOR_DATA_TYPE_FLOAT32, + DimensionCount = (uint)shape.Rank, Sizes = sizes, TotalTensorSizeInBytes = shape.SizeInBytes + }; + DML_TENSOR_DESC tensor = new( ) { Type = DML_TENSOR_TYPE.DML_TENSOR_TYPE_BUFFER, Desc = &buffer }; + DML_ELEMENT_WISE_ADD_OPERATOR_DESC addition = new( ) { ATensor = &tensor, BTensor = &tensor, OutputTensor = &tensor }; + DML_OPERATOR_DESC description = new( ) { Type = DML_OPERATOR_TYPE.DML_OPERATOR_ELEMENT_WISE_ADD, Desc = &addition }; + Guid operatorId = typeof(IDMLOperator).GUID; + nint pointer = 0; + Results.Check( Native.Interface.CreateOperator( &description, &operatorId, (void**)&pointer ), "DirectML.CreateOperator(Add)" ); + using var op = ComOwner.Adopt( pointer ); + Guid compiledId = typeof(IDMLCompiledOperator).GUID; + Results.Check( Native.Interface.CompileOperator( op.Interface, 0, &compiledId, (void**)&pointer ), "DirectML.CompileOperator(Add)" ); + var compiled = ComOwner.Adopt( pointer ); + try { return new( compiled, Native.Clone( ), Graphics.Clone( ), shape ) { DeviceIdentity = DeviceIdentity }; } + catch { compiled.Dispose( ); throw; } + } + } + /// + public override void Dispose( ) { base.Dispose( ); Graphics.Dispose( ); } +} diff --git a/src/DXSharp.DirectML/TensorAddOperator.cs b/src/DXSharp.DirectML/TensorAddOperator.cs new file mode 100644 index 0000000..d26f509 --- /dev/null +++ b/src/DXSharp.DirectML/TensorAddOperator.cs @@ -0,0 +1,115 @@ +using System.Runtime.InteropServices; +using Windows.Win32.AI.MachineLearning.DirectML; +using Windows.Win32.Graphics.Direct3D12; +using DXSharp.Graphics; +using DXSharp.Interop; + +namespace DXSharp.MachineLearning; + +/// A compiled, dense Float32 addition operator. +/// Execute is synchronous and serializes this operator. Each execution owns its descriptor, +/// temporary and persistent allocations, and initializes persistent data before dispatch. +public sealed unsafe class TensorAddOperator : GraphicsObject { + private readonly ComOwner _native; + private readonly ComOwner _device; + private readonly GraphicsDevice _graphics; + private readonly object _gate = new( ); + /// The shape shared by both inputs and the output. + public TensorShape Shape { get; } + internal TensorAddOperator( ComOwner owner, ComOwner device, + GraphicsDevice graphics, TensorShape shape ) : base( owner ) { _native = owner; _device = device; _graphics = graphics; Shape = shape; } + internal override ComReference Retain( ) => _native.Retain( ); + + /// Initializes and executes A + B. Supply distinct default-heap UAV buffers in UnorderedAccess state. + /// The queue must belong to this device and accept direct or compute commands. State transitions remain explicit. + public void Execute( CommandQueue queue, GpuResource left, GpuResource right, GpuResource output ) { + lock ( _gate ) { + RequireSameDevice( queue ); RequireSameDevice( left ); RequireSameDevice( right ); RequireSameDevice( output ); + if ( queue.Kind == QueueKind.Copy ) throw new ArgumentException( "DirectML requires a direct or compute queue.", nameof(queue) ); + foreach ( var buffer in new[] { left, right, output } ) { + buffer.ValidateRange( 0, Shape.SizeInBytes ); + if ( buffer.Memory != BufferMemory.Default || !buffer.SupportsUnorderedAccess ) throw new ArgumentException( "DirectML bindings require default-heap UAV buffers." ); + } + if ( ReferenceEquals( output, left ) || ReferenceEquals( output, right ) ) throw new ArgumentException( "The output must not alias an input.", nameof(output) ); + Guid id = typeof(IDMLOperatorInitializer).GUID; + Results.Check( _device.Interface.CreateOperatorInitializer( 1, [_native.Interface], &id, out nint pointer ), "DirectML.CreateOperatorInitializer" ); + using var initializer = ComOwner.Adopt( pointer ); + DML_BINDING_PROPERTIES init = initializer.Interface.GetBindingProperties( ), execute = _native.Interface.GetBindingProperties( ); + uint descriptors = Math.Max( 1, Math.Max( init.RequiredDescriptorCount, execute.RequiredDescriptorCount ) ); + using var heap = CreateHeap( descriptors ); + using var temporary = CreateScratch( Math.Max( init.TemporaryResourceSize, execute.TemporaryResourceSize ) ); + using var persistent = CreateScratch( execute.PersistentResourceSize ); + id = typeof(IDMLCommandRecorder).GUID; + Results.Check( _device.Interface.CreateCommandRecorder( &id, out pointer ), "DirectML.CreateCommandRecorder" ); + using var recorder = ComOwner.Adopt( pointer ); + using var allocator = _graphics.CreateCommandAllocator( queue.Kind ); + using var commands = _graphics.CreateCommandList( allocator ); + using var initLease = initializer.Borrow( ); + DML_BINDING_TABLE_DESC_unmanaged description = new( ) { + Dispatchable = (IDMLDispatchable_unmanaged*)initLease.Pointer, + CPUDescriptorHandle = heap.Native.Interface.GetCPUDescriptorHandleForHeapStart( ), + GPUDescriptorHandle = heap.Native.Interface.GetGPUDescriptorHandleForHeapStart( ), SizeInDescriptors = descriptors + }; + id = typeof(IDMLBindingTable).GUID; + Results.Check( _device.Interface.CreateBindingTable( &description, &id, out pointer ), "DirectML.CreateBindingTable" ); + using var table = ComOwner.Adopt( pointer ); + BindResource( table.Interface, temporary, init.TemporaryResourceSize, 0 ); + BindResource( table.Interface, persistent, execute.PersistentResourceSize, 1 ); + RetainAndBindHeap( commands, heap, temporary, persistent ); + commands.RetainNative( initializer, initializer.Retain( ) ); commands.RetainNative( table, table.Retain( ) ); + commands.RetainNative( recorder, recorder.Retain( ) ); commands.RetainDependency( this ); + recorder.Interface.RecordDispatch( commands.Native.Interface, initializer.Interface, table.Interface ); + if ( persistent is not null ) commands.UavBarrier( persistent ); + if ( temporary is not null ) commands.UavBarrier( temporary ); + commands.Close( ); queue.Wait( queue.Submit( commands ) ); + + allocator.Reset( ); commands.Reset( allocator ); + using var executeLease = _native.Borrow( ); + description.Dispatchable = (IDMLDispatchable_unmanaged*)executeLease.Pointer; + Results.Check( table.Interface.Reset( &description ), "DirectML.BindingTable.Reset" ); + BindResource( table.Interface, temporary, execute.TemporaryResourceSize, 0 ); + BindResource( table.Interface, persistent, execute.PersistentResourceSize, 2 ); + using var a = left.Native.Borrow( ); using var b = right.Native.Borrow( ); using var result = output.Native.Borrow( ); + DML_BUFFER_BINDING* buffers = stackalloc DML_BUFFER_BINDING[3]; + buffers[0] = Buffer( a.Pointer, Shape.SizeInBytes ); buffers[1] = Buffer( b.Pointer, Shape.SizeInBytes ); buffers[2] = Buffer( result.Pointer, Shape.SizeInBytes ); + DML_BINDING_DESC* inputs = stackalloc DML_BINDING_DESC[2]; + inputs[0] = Binding( &buffers[0] ); inputs[1] = Binding( &buffers[1] ); + DML_BINDING_DESC target = Binding( &buffers[2] ); + table.Interface.BindInputs( 2, inputs ); table.Interface.BindOutputs( 1, &target ); + RetainAndBindHeap( commands, heap, temporary, persistent ); + commands.RetainDependency( this ); commands.RetainDependency( left ); commands.RetainDependency( right ); commands.RetainDependency( output ); + commands.RetainNative( table, table.Retain( ) ); commands.RetainNative( recorder, recorder.Retain( ) ); + recorder.Interface.RecordDispatch( commands.Native.Interface, _native.Interface, table.Interface ); + commands.UavBarrier( output ); commands.Close( ); queue.Wait( queue.Submit( commands ) ); + } + } + private DescriptorHeap CreateHeap( uint capacity ) { + D3D12_DESCRIPTOR_HEAP_DESC description = new( ) { Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + Flags = D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, NumDescriptors = capacity }; + Guid id = typeof(ID3D12DescriptorHeap).GUID; + Results.Check( _graphics.Native.Interface.CreateDescriptorHeap( &description, &id, out nint pointer ), "DirectML.CreateDescriptorHeap" ); + return new( ComOwner.Adopt( pointer ), capacity, + _graphics.Native.Interface.GetDescriptorHandleIncrementSize( description.Type ) ) { DeviceIdentity = DeviceIdentity }; + } + private GpuResource? CreateScratch( ulong size ) => size == 0 ? null : _graphics.CreateBuffer( size, initialState: ResourceState.UnorderedAccess, unorderedAccess: true ); + private static void RetainAndBindHeap( GraphicsCommandList commands, DescriptorHeap heap, GpuResource? temporary, GpuResource? persistent ) { + commands.RetainDependency( heap ); + if ( temporary is not null ) commands.RetainDependency( temporary ); + if ( persistent is not null ) commands.RetainDependency( persistent ); + commands.Native.Interface.SetDescriptorHeaps( 1, [heap.Native.Interface] ); + } + private static void BindResource( IDMLBindingTable table, GpuResource? resource, ulong size, int kind ) { + if ( size == 0 ) return; + using var lease = resource!.Native.Borrow( ); + DML_BUFFER_BINDING buffer = Buffer( lease.Pointer, size ); DML_BINDING_DESC binding = Binding( &buffer ); + if ( kind == 0 ) table.BindTemporaryResource( &binding ); + else if ( kind == 1 ) table.BindOutputs( 1, &binding ); + else table.BindPersistentResource( &binding ); + } + private static DML_BINDING_DESC Binding( DML_BUFFER_BINDING* buffer ) => new( ) { Type = DML_BINDING_TYPE.DML_BINDING_TYPE_BUFFER, Desc = buffer }; + private static DML_BUFFER_BINDING Buffer( nint pointer, ulong size ) => new( ) { + Buffer = (ID3D12Resource_unmanaged*)pointer, SizeInBytes = size + }; + /// + public override void Dispose( ) { lock ( _gate ) { base.Dispose( ); _device.Dispose( ); _graphics.Dispose( ); } } +} diff --git a/src/DXSharp.DirectML/TensorShape.cs b/src/DXSharp.DirectML/TensorShape.cs new file mode 100644 index 0000000..b18d642 --- /dev/null +++ b/src/DXSharp.DirectML/TensorShape.cs @@ -0,0 +1,25 @@ +namespace DXSharp.MachineLearning; + +/// An immutable, contiguous, row-major Float32 tensor shape. +public sealed class TensorShape { + private readonly uint[] _dimensions; + /// Tensor rank, from one through eight. + public int Rank => _dimensions.Length; + /// The number of elements, with checked dimension multiplication. + public ulong ElementCount { get; } + /// The required number of buffer bytes, including DirectML's four-byte alignment. + public ulong SizeInBytes => checked(ElementCount * sizeof(float)); + /// A read-only view of the copied dimensions. + public ReadOnlySpan Dimensions => _dimensions; + /// Creates a dense Float32 shape. Dimensions must be nonzero. + public TensorShape( params ReadOnlySpan dimensions ) { + if ( dimensions.Length is < 1 or > 8 ) throw new ArgumentOutOfRangeException( nameof(dimensions) ); + ulong count = 1; + foreach ( uint dimension in dimensions ) { + if ( dimension == 0 ) throw new ArgumentOutOfRangeException( nameof(dimensions) ); + count = checked(count * dimension); + } + _dimensions = dimensions.ToArray( ); ElementCount = count; + _ = SizeInBytes; + } +} diff --git a/src/DXSharp.DirectML/packages.lock.json b/src/DXSharp.DirectML/packages.lock.json new file mode 100644 index 0000000..9563611 --- /dev/null +++ b/src/DXSharp.DirectML/packages.lock.json @@ -0,0 +1,32 @@ +{ + "version": 2, + "dependencies": { + "net10.0-windows10.0.26100": { + "Microsoft.AI.DirectML": { + "type": "Direct", + "requested": "[1.15.4, 1.15.4]", + "resolved": "1.15.4", + "contentHash": "UblWBtQxcXcxwcn+jh0pfbq4zv2b6w7ThJ35qUV9gd8niGofrFWzVqZ6yqYDMiE+IJf8LRpG+H1cZpswceOeiA==" + }, + "dxsharp.core": { + "type": "Project", + "dependencies": { + "Microsoft.Direct3D.D3D12": "[1.620.1, 1.620.1]", + "Microsoft.Direct3D.DXC": "[1.9.2609.5, 1.9.2609.5]" + } + }, + "Microsoft.Direct3D.D3D12": { + "type": "CentralTransitive", + "requested": "[1.620.1, )", + "resolved": "1.620.1", + "contentHash": "oQCFTys3PZ0IHtS3I/u0/1IWgpuG7aQa+84AGiraqc8bMOCM8QpDLwLBshEbJ1J7LmuctZReXhF8c0eCiu2Xcw==" + }, + "Microsoft.Direct3D.DXC": { + "type": "CentralTransitive", + "requested": "[1.9.2609.5, )", + "resolved": "1.9.2609.5", + "contentHash": "jTWr7a2bMpqV+b0Uc7oxOo8ualplaUw3fsQ+p0kp9TwlWvvmREvt3SA4+Drl2Y0M31iscqZolrL3vpdnLoPpdg==" + } + } + } +} \ No newline at end of file diff --git a/tests/InteropQualification/InteropQualification.csproj b/tests/InteropQualification/InteropQualification.csproj new file mode 100644 index 0000000..6cced92 --- /dev/null +++ b/tests/InteropQualification/InteropQualification.csproj @@ -0,0 +1,13 @@ + + + Exe + net10.0-windows10.0.26100.0 + false + false + true + + + + + + diff --git a/tests/InteropQualification/Program.cs b/tests/InteropQualification/Program.cs new file mode 100644 index 0000000..cee5d8f --- /dev/null +++ b/tests/InteropQualification/Program.cs @@ -0,0 +1,159 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using DXSharp.Interop; +using DXSharp.Shaders; +using DXSharp.MachineLearning; +using Windows.Win32.AI.MachineLearning.DirectML; +using Windows.Win32.Graphics.Direct3D.Dxc; + +namespace InteropQualification; + +[StructLayout( LayoutKind.Sequential )] +internal struct Pair { internal ulong First, Second; } + +[GeneratedComInterface] +[Guid( "96446a17-3c17-451c-bc9d-5bb559180b54" )] +internal unsafe partial interface IFixture { + [PreserveSig] int Echo( int value ); + [PreserveSig] void SetValue( int value ); + [PreserveSig] int GetValue( ); + [PreserveSig] unsafe int Fill( byte* output, uint count ); +} + +[GeneratedComInterface] +[Guid( "1659e483-98ab-4e6c-89ab-0f42f2702c71" )] +internal unsafe partial interface IDerived : IFixture { + [PreserveSig] Pair ReadPair( ); +} + +[GeneratedComInterface] +[Guid( "a06f1049-f293-4285-bf27-2c66b35aef32" )] +internal partial interface IUnsupported { [PreserveSig] int Missing( ); } + +[GeneratedComInterface] +[Guid( "afac9b8c-37e7-40b1-999b-6f65ada83a73" )] +internal partial interface ICallback { [PreserveSig] int Invoke( int value ); } + +[GeneratedComClass] +internal partial class Callback : ICallback { + public int Invoke( int value ) { GC.Collect( ); return value * 3; } +} + +internal static partial class Native { + [LibraryImport( "dxsharp_abi", EntryPoint = "fixture_create" )] internal static partial nint Create( ); + [LibraryImport( "dxsharp_abi", EntryPoint = "fixture_alive" )] internal static partial int Alive( ); + [LibraryImport( "dxsharp_abi", EntryPoint = "fixture_destroyed" )] internal static partial int Destroyed( ); + [LibraryImport( "dxsharp_abi", EntryPoint = "fixture_size" )] internal static partial int Size( int index ); + [LibraryImport( "dxsharp_abi", EntryPoint = "fixture_retain_callback" )] internal static partial void RetainCallback( nint value ); + [LibraryImport( "dxsharp_abi", EntryPoint = "fixture_invoke_callback" )] internal static partial int InvokeCallback( int value ); + [LibraryImport( "dxsharp_abi", EntryPoint = "fixture_include" )] internal static partial int Include( nint handler ); +} + +internal static class Program { + private static int _checks; + private static void Check( bool condition, string name ) { if ( !condition ) throw new InvalidOperationException( name ); ++_checks; } + + public static unsafe int Main( ) { + try { + uint[] dimensions = [1, 2, 3, 4]; + var shape = new TensorShape( dimensions ); dimensions[1] = 99; + Check( shape.ElementCount == 24 && shape.SizeInBytes == 96 && shape.Dimensions[1] == 2, "immutable dense tensor layout" ); + bool rejected = false; + try { _ = new TensorShape( 1, 0 ); } catch ( ArgumentOutOfRangeException ) { rejected = true; } + Check( rejected, "zero tensor extent rejected" ); + rejected = false; + try { _ = new TensorShape( uint.MaxValue, uint.MaxValue, uint.MaxValue ); } catch ( OverflowException ) { rejected = true; } + Check( rejected, "tensor extent multiplication checked" ); + using ( var includes = new IncludeHandler( name => { + Check( name == "fixture.hlsli", "UTF-16 include name" ); + GC.Collect( ); return System.Text.Encoding.UTF8.GetBytes( "generated include" ); + } ) ) { + void* pointer = ComInterfaceMarshaller.ConvertToUnmanaged( includes ); + try { Check( Native.Include( (nint)pointer ) == 1, "generated DXC include and blob callbacks" ); } + finally { ComInterfaceMarshaller.Free( pointer ); } + } + using ( var failingInclude = new IncludeHandler( _ => throw new FileNotFoundException( "Missing shader include." ) ) ) { + void* pointer = ComInterfaceMarshaller.ConvertToUnmanaged( failingInclude ); + try { Check( Native.Include( (nint)pointer ) == -1, "include failure translates to HRESULT" ); } + finally { ComInterfaceMarshaller.Free( pointer ); } + } + Check( Native.Size( 0 ) == Unsafe.SizeOf( ), "GUID layout" ); + Check( Native.Size( 1 ) == Unsafe.SizeOf( ), "structure layout" ); + Check( Native.Size( 2 ) == IntPtr.Size, "pointer width" ); + Check( Native.Size( 3 ) == Unsafe.SizeOf( ), "DirectML buffer binding ABI" ); + Check( Native.Size( 4 ) == Unsafe.SizeOf( ), "DirectML tensor descriptor ABI" ); + using ( var root = ComOwner.Adopt( Native.Create( ) ) ) { + Check( root.Interface.Echo( 42 ) == (42 ^ 0x13579), "scalar return and base slots" ); + root.Interface.SetValue( 77 ); + Check( root.Interface.GetValue( ) == 77, "void call and stored value" ); + Span bytes = stackalloc byte[257]; + fixed ( byte* pointer = bytes ) Check( root.Interface.Fill( pointer, (uint)bytes.Length ) == 0, "array call" ); + for ( int i = 0; i < bytes.Length; ++i ) Check( bytes[i] == (byte)(i ^ 0x5a), "array length and contents" ); + Check( root.Interface.Fill( null, 0 ) == 0, "empty input" ); + Check( root.TryQuery( typeof(IDerived).GUID, out var derived ), "derived QI" ); + using ( derived ) { + Pair pair = derived!.Interface.ReadPair( ); + Check( pair.First == 0x1122334455667788UL && pair.Second == 0x8877665544332211UL, "C++ structure return ABI" ); + Check( derived.Interface.Echo( 3 ) == (3 ^ 0x13579), "inherited slots" ); + } + Check( !root.TryQuery( typeof(IUnsupported).GUID, out var missing ) && missing is null, "failed QI" ); + using var clone = root.Clone( ); + root.Dispose( ); + root.Dispose( ); + Check( clone.Interface.GetValue( ) == 77, "clone survives source disposal" ); + try { _ = root.Interface; throw new InvalidOperationException( "disposed access accepted" ); } + catch ( ObjectDisposedException ) { ++_checks; } + } + Check( Native.Alive( ) == 0, "balanced deterministic ownership" ); + + var owner = ComOwner.Adopt( Native.Create( ) ); + using ( var borrow = owner.Borrow( ) ) { + owner.Dispose( ); + Check( Native.Alive( ) == 1, "borrow retains object during owner disposal" ); + Check( UnknownCalls.AddRef( borrow.Pointer ) > 0, "borrowed reference is callable" ); + UnknownCalls.Release( borrow.Pointer ); + } + Check( Native.Alive( ) == 0, "borrow released" ); + try { using var invalid = ComOwner.Adopt( Native.Create( ) ); throw new InvalidOperationException( "invalid projection accepted" ); } + catch ( InvalidCastException ) { ++_checks; } + Check( Native.Alive( ) == 0, "failed projection cleans native output" ); + + for ( int i = 0; i < 2000; ++i ) { + using var item = ComOwner.Adopt( Native.Create( ) ); + using var copy = item.Clone( ); + if ( i % 100 == 0 ) { GC.Collect( ); GC.WaitForPendingFinalizers( ); } + Check( copy.Interface.Echo( i ) == (i ^ 0x13579), "GC stress" ); + } + Check( Native.Alive( ) == 0, "stress teardown" ); + ForgetOwner( ); + for ( int i = 0; i < 4; ++i ) { GC.Collect( ); GC.WaitForPendingFinalizers( ); } + Check( Native.Alive( ) == 0, "finalizer fallback" ); + + RetainCallback( ); + GC.Collect( ); GC.WaitForPendingFinalizers( ); + Check( Native.InvokeCallback( 17 ) == 51, "native-held generated callback survives GC" ); + Native.RetainCallback( 0 ); + Check( Native.Destroyed( ) >= 2004, "native destructor accounting" ); + Console.WriteLine( $"PASS: {_checks} checks; {Native.Destroyed( )} native objects destroyed; no live objects." ); + return 0; + } catch ( Exception exception ) { Console.Error.WriteLine( exception ); return 1; } + } + + [MethodImpl( MethodImplOptions.NoInlining )] + private static void ForgetOwner( ) { _ = ComOwner.Adopt( Native.Create( ) ); } + + [MethodImpl( MethodImplOptions.NoInlining )] + private static void RetainCallback( ) { + var callback = new Callback( ); + var wrappers = new StrategyBasedComWrappers( ); + nint unknown = wrappers.GetOrCreateComInterfaceForObject( callback, CreateComInterfaceFlags.None ); + try { + ResultsCheck( UnknownCalls.QueryInterface( unknown, typeof(ICallback).GUID, out nint pointer ) ); + try { Native.RetainCallback( pointer ); } + finally { UnknownCalls.Release( pointer ); } + } finally { UnknownCalls.Release( unknown ); } + GC.KeepAlive( callback ); + } + private static void ResultsCheck( int value ) { if ( value < 0 ) throw new COMException( "Callback QI", value ); } +} diff --git a/tests/InteropQualification/native/fixture.cpp b/tests/InteropQualification/native/fixture.cpp new file mode 100644 index 0000000..fb36eda --- /dev/null +++ b/tests/InteropQualification/native/fixture.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#ifdef _WIN32 +#include +#define ABI_CALL __stdcall +#define EXPORT extern "C" __declspec(dllexport) +#else +#define ABI_CALL +#define EXPORT extern "C" __attribute__((visibility("default"))) +#endif + +struct Guid { std::uint32_t a; std::uint16_t b, c; std::uint8_t d[8]; }; +struct Pair { std::uint64_t first, second; }; +static const Guid unknown_id = { 0, 0, 0, {0xc0,0,0,0,0,0,0,0x46} }; +static const Guid fixture_id = { 0x96446a17, 0x3c17, 0x451c, {0xbc,0x9d,0x5b,0xb5,0x59,0x18,0x0b,0x54} }; +static const Guid derived_id = { 0x1659e483, 0x98ab, 0x4e6c, {0x89,0xab,0x0f,0x42,0xf2,0x70,0x2c,0x71} }; +static std::atomic alive{0}, destroyed{0}; + +struct Unknown { + virtual std::int32_t ABI_CALL QueryInterface(const Guid*, void**) = 0; + virtual std::uint32_t ABI_CALL AddRef() = 0; + virtual std::uint32_t ABI_CALL Release() = 0; +}; +struct Fixture : Unknown { + virtual std::int32_t ABI_CALL Echo(std::int32_t) = 0; + virtual void ABI_CALL SetValue(std::int32_t) = 0; + virtual std::int32_t ABI_CALL GetValue() = 0; + virtual std::int32_t ABI_CALL Fill(std::uint8_t*, std::uint32_t) = 0; +}; +struct Derived : Fixture { virtual Pair ABI_CALL ReadPair() = 0; }; +struct Callback : Unknown { virtual std::int32_t ABI_CALL Invoke(std::int32_t) = 0; }; + +class Object final : public Derived { + std::atomic refs{1}; + std::int32_t value{0}; +public: + Object() { ++alive; } + ~Object() { --alive; ++destroyed; } + std::int32_t ABI_CALL QueryInterface(const Guid* iid, void** output) override { + *output = nullptr; + if (std::memcmp(iid, &unknown_id, sizeof(Guid)) && std::memcmp(iid, &fixture_id, sizeof(Guid)) && std::memcmp(iid, &derived_id, sizeof(Guid))) return static_cast(0x80004002u); + *output = static_cast(this); + AddRef(); + return 0; + } + std::uint32_t ABI_CALL AddRef() override { return ++refs; } + std::uint32_t ABI_CALL Release() override { auto count = --refs; if (!count) delete this; return count; } + std::int32_t ABI_CALL Echo(std::int32_t input) override { return input ^ 0x13579; } + void ABI_CALL SetValue(std::int32_t input) override { value = input; } + std::int32_t ABI_CALL GetValue() override { return value; } + std::int32_t ABI_CALL Fill(std::uint8_t* output, std::uint32_t count) override { + if (!output && count) return static_cast(0x80004003u); + for (std::uint32_t i = 0; i < count; ++i) output[i] = static_cast(i ^ 0x5a); + return 0; + } + Pair ABI_CALL ReadPair() override { return {0x1122334455667788ULL, 0x8877665544332211ULL}; } +}; + +static Callback* retained = nullptr; +EXPORT void* fixture_create() { return static_cast(new Object()); } +EXPORT int fixture_alive() { return alive.load(); } +EXPORT int fixture_destroyed() { return destroyed.load(); } +#ifndef _WIN32 +struct DML_BUFFER_BINDING { void* Buffer; std::uint64_t Offset, SizeInBytes; }; +struct DML_BUFFER_TENSOR_DESC { + std::uint32_t DataType, Flags, DimensionCount; const std::uint32_t *Sizes, *Strides; + std::uint64_t TotalTensorSizeInBytes; std::uint32_t GuaranteedBaseOffsetAlignment; +}; +#endif +EXPORT int fixture_size(int index) { + switch(index) { + case 0: return sizeof(Guid); + case 1: return sizeof(Pair); + case 2: return sizeof(void*); + case 3: return sizeof(DML_BUFFER_BINDING); + case 4: return sizeof(DML_BUFFER_TENSOR_DESC); + default: return -1; + } +} +EXPORT void fixture_retain_callback(Callback* callback) { if (retained) retained->Release(); retained = callback; if (retained) retained->AddRef(); } +EXPORT int fixture_invoke_callback(int input) { return retained ? retained->Invoke(input) : -1; } + +struct Blob : Unknown { + virtual void* ABI_CALL GetBufferPointer() = 0; + virtual std::size_t ABI_CALL GetBufferSize() = 0; +}; +struct Include : Unknown { + virtual std::int32_t ABI_CALL LoadSource(const char16_t*, Blob**) = 0; +}; +EXPORT int fixture_include(void* pointer) { + auto handler = static_cast(pointer); + Blob* blob = nullptr; + auto hr = handler->LoadSource(u"fixture.hlsli", &blob); + if (hr < 0 || !blob) return -1; + const char expected[] = "generated include"; + bool valid = blob->GetBufferSize() == sizeof(expected) - 1 + && std::memcmp(blob->GetBufferPointer(), expected, sizeof(expected) - 1) == 0; + blob->Release(); + return valid ? 1 : 0; +} diff --git a/tests/InteropQualification/packages.lock.json b/tests/InteropQualification/packages.lock.json new file mode 100644 index 0000000..92072c3 --- /dev/null +++ b/tests/InteropQualification/packages.lock.json @@ -0,0 +1,39 @@ +{ + "version": 2, + "dependencies": { + "net10.0-windows10.0.26100": { + "dxsharp.core": { + "type": "Project", + "dependencies": { + "Microsoft.Direct3D.D3D12": "[1.620.1, 1.620.1]", + "Microsoft.Direct3D.DXC": "[1.9.2609.5, 1.9.2609.5]" + } + }, + "dxsharp.directml": { + "type": "Project", + "dependencies": { + "DXSharp.Core": "[0.4.0-net10-preview.1, )", + "Microsoft.AI.DirectML": "[1.15.4, 1.15.4]" + } + }, + "Microsoft.AI.DirectML": { + "type": "CentralTransitive", + "requested": "[1.15.4, )", + "resolved": "1.15.4", + "contentHash": "UblWBtQxcXcxwcn+jh0pfbq4zv2b6w7ThJ35qUV9gd8niGofrFWzVqZ6yqYDMiE+IJf8LRpG+H1cZpswceOeiA==" + }, + "Microsoft.Direct3D.D3D12": { + "type": "CentralTransitive", + "requested": "[1.620.1, )", + "resolved": "1.620.1", + "contentHash": "oQCFTys3PZ0IHtS3I/u0/1IWgpuG7aQa+84AGiraqc8bMOCM8QpDLwLBshEbJ1J7LmuctZReXhF8c0eCiu2Xcw==" + }, + "Microsoft.Direct3D.DXC": { + "type": "CentralTransitive", + "requested": "[1.9.2609.5, )", + "resolved": "1.9.2609.5", + "contentHash": "jTWr7a2bMpqV+b0Uc7oxOo8ualplaUw3fsQ+p0kp9TwlWvvmREvt3SA4+Drl2Y0M31iscqZolrL3vpdnLoPpdg==" + } + } + } +} \ No newline at end of file