コミットを比較
| 作成者 | SHA1 | 日付 | |
|---|---|---|---|
|
|
15e0cccdcc
|
||
|
|
b07d5b8937
|
||
|
|
bfc35b3931
|
||
|
|
a2531d96e8
|
||
|
|
4f54851fc5
|
||
|
|
fcb5c87637
|
||
|
|
82ae3e3acd
|
||
|
|
b9e57ffd66
|
||
|
|
c7415baf1b
|
||
|
|
c85592277e
|
||
|
|
0d8e5124a0
|
||
|
|
086557db7f
|
検索条件に一致するファイルはありません
+2
-2
@@ -1,4 +1,4 @@
|
||||
# v1.0.1 - 浮世絵 for YMM4
|
||||
# v1.0.1 - 浮世絵 for YMM4
|
||||
|
||||
浮世絵エフェクトプラグインの改善リリースです。
|
||||
L1平坦化の反復で近傍セルの読み取りをグループ共有メモリへ移し、地色の計算を高速化しました。
|
||||
@@ -15,7 +15,7 @@ L1平坦化の各反復は、1セルにつき近傍24セルの元の色と勾配
|
||||
|
||||
- 1セルあたりの大域メモリの読み取りは、色と勾配強度と反復値の3種類で最大75回から、グループ内で分担した約7回へ減りました。
|
||||
- 近傍の加算の順序と各項の値は変更していません。変わるのは値を読み出す記憶域の種類だけです。
|
||||
- グループ共有メモリを使うシェーダーは、グループ内のすべてのスレッドが同期に到達する必要があります。ComputeSharpが生成するシェーダーの入口は`For`へ渡した反復数を上限としてスレッドを間引くため、処理格子の寸法のままディスパッチすると最終グループの一部のスレッドが同期に到達しません。平坦化のディスパッチ範囲は8の倍数へ切り上げています。切り上げで増えたスレッドは格子の範囲外として何も書き込みません。
|
||||
- グループ共有メモリを使うシェーダーは、グループ内のすべてのスレッドが同期に到達する必要があります。ComputeWeaveが生成するシェーダーの入口は`For`へ渡した反復数を上限としてスレッドを間引くため、処理格子の寸法のままディスパッチすると最終グループの一部のスレッドが同期に到達しません。平坦化のディスパッチ範囲は8の倍数へ切り上げています。切り上げで増えたスレッドは格子の範囲外として何も書き込みません。
|
||||
|
||||
### 2. 性能
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
name: リリース
|
||||
name: リリース
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -126,9 +126,7 @@ jobs:
|
||||
New-Item -ItemType Directory -Path $stageRoot -Force | Out-Null
|
||||
New-Item -ItemType Directory -Path "$stageRoot/LICENSE" -Force | Out-Null
|
||||
|
||||
Copy-Item "$outDir/Ukiyoe.dll" "$stageRoot/"
|
||||
Copy-Item "$outDir/Ukiyoe.ComputeSharp.dll" "$stageRoot/"
|
||||
Copy-Item "$outDir/Ukiyoe.ComputeSharp.Core.dll" "$stageRoot/"
|
||||
Copy-Item "$outDir/plugin/Ukiyoe.dll" "$stageRoot/"
|
||||
|
||||
foreach ($lang in @("ar-sa", "en-us", "es-es", "id-id", "ko-kr", "zh-cn", "zh-tw")) {
|
||||
$src = "$outDir/$lang"
|
||||
|
||||
+1
-4
@@ -1,9 +1,6 @@
|
||||
[submodule "YukkuriMovieMaker.Generator"]
|
||||
[submodule "YukkuriMovieMaker.Generator"]
|
||||
path = YukkuriMovieMaker.Generator
|
||||
url = https://github.com/manju-summoner/YukkuriMovieMaker.Generator.git
|
||||
[submodule "YMM4-GitHubUpdateNotifier"]
|
||||
path = YMM4-GitHubUpdateNotifier
|
||||
url = https://github.com/routersys/YMM4-GitHubUpdateNotifier.git
|
||||
[submodule "ComputeSharp"]
|
||||
path = ComputeSharp
|
||||
url = https://github.com/routersys/ComputeSharp.git
|
||||
-1
サブモジュール ComputeSharp が 7c4145d5f2 から削除されました。
@@ -1,4 +1,4 @@
|
||||
# 浮世絵 for YMM4
|
||||
# 浮世絵 for YMM4
|
||||
|
||||
[](#)
|
||||
[](#)
|
||||
@@ -46,7 +46,7 @@ YukkuriMovieMaker4(YMM4)上で動作する、素材を多色木版画の原
|
||||
|
||||
入力された現在のフレームの色とアルファ値から素材の絵柄を求め、多色木版画の工程になぞらえて3つの層を刷り重ねます。主線の層は、Kang・Lee・Chuiの論文「Coherent Line Drawing」(NPAR 2007)に基づき、エッジ接線流の非線形ベクトル平滑化で絵柄の流れを求め、流れに直交する方向の差分ガウスを流線に沿って集計して、つながりの良い輪郭線を取り出します。色面の層は、Bi・Han・Yuの論文「An L1 Image Transform for Edge-Preserving Smoothing and Scene-Level Intrinsic Decomposition」(SIGGRAPH 2015)のL1局所平坦化を前向きの反復重み付き最小二乗として解き、細部を消しながら際を保った均質な色面を作り、Winnemöllerらの論文「Real-Time Video Abstraction」(SIGGRAPH 2006)の軟量子化で限定パレットの階調へ寄せます。意匠の層は、階調ごとの刷り位置のずれ、ばれんの擦り跡、和紙の繊維のかすれを決定論的なハッシュ模様として重ねます。
|
||||
|
||||
計算はComputeSharpの計算シェーダーがDirect3D 12で実行します。YMM4のDirect3D 11側とComputeSharpのDirect3D 12側は、共有テクスチャと共有フェンスで接続します。通常のフレーム処理では、CPUへの画素の読み戻しを行わず、8個の整数の読み戻しだけで素材の変化を判定します。素材が存在する最小の矩形の範囲だけを描画し、絵柄と構造のパラメータが変わらないフレームでは輪郭と平坦化の計算を再利用します。
|
||||
計算はComputeWeaveの計算シェーダーがDirect3D 12で実行します。YMM4のDirect3D 11側とComputeWeaveのDirect3D 12側は、共有テクスチャと共有フェンスで接続します。通常のフレーム処理では、CPUへの画素の読み戻しを行わず、8個の整数の読み戻しだけで素材の変化を判定します。素材が存在する最小の矩形の範囲だけを描画し、絵柄と構造のパラメータが変わらないフレームでは輪郭と平坦化の計算を再利用します。
|
||||
|
||||
このエフェクトはAviUtl向けのEXO出力には対応していません。
|
||||
|
||||
@@ -187,11 +187,11 @@ YukkuriMovieMaker4(YMM4)上で動作する、素材を多色木版画の原
|
||||
|
||||
## サードパーティライセンス
|
||||
|
||||
本プラグインは以下のサードパーティソフトウェアを同梱・使用しています。ライセンスの全文は、リポジトリの [`.github/LICENSE/ComputeSharp.txt`](.github/LICENSE/ComputeSharp.txt) と、配布パッケージの `LICENSE` フォルダーに収録しています。
|
||||
本プラグインは以下のサードパーティソフトウェアを同梱・使用しています。ライセンスの全文は、リポジトリの [`.github/LICENSE/ComputeWeave.txt`](.github/LICENSE/ComputeWeave.txt) と、配布パッケージの `LICENSE` フォルダーに収録しています。
|
||||
|
||||
| ソフトウェア | 用途 | ライセンス | 著作権表示 |
|
||||
|---|---|---|---|
|
||||
| [ComputeSharp](https://github.com/routersys/ComputeSharp) | Direct3D 12計算シェーダーとDirect3D 11・12共有処理 | MIT License | Copyright (c) 2024 Sergio Pedri |
|
||||
| [ComputeWeave](https://github.com/routersys/ComputeWeave) | Direct3D 12計算シェーダーとDirect3D 11・12共有処理 | MIT License | Copyright (c) 2024 Sergio Pedri |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using ComputeSharp;
|
||||
using System.Diagnostics;
|
||||
using ComputeWeave;
|
||||
using Ukiyoe;
|
||||
|
||||
var width = 1280;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
@@ -26,6 +26,5 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="..\Ukiyoe\ComputeSharp.Reference.props" />
|
||||
|
||||
</Project>
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
|
||||
@@ -63,6 +63,5 @@
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="..\Ukiyoe\ComputeSharp.Reference.props" />
|
||||
|
||||
</Project>
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp;
|
||||
using ComputeSharp.Interop;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeWeave;
|
||||
using ComputeWeave.Interop;
|
||||
using Vortice;
|
||||
using Vortice.Direct2D1;
|
||||
using Vortice.DXGI;
|
||||
@@ -537,18 +537,20 @@ public sealed class UkiyoeEffectTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Direct2DInteropProducesPrintFromOpaqueCore()
|
||||
public void Direct2DInteropProducesPrintAfterGrowingFullHdOutput()
|
||||
{
|
||||
using var devices = new GraphicsDevices();
|
||||
using var graphicsContext = devices.CreateContext();
|
||||
using var interop = UkiyoeGpuInterop.TryCreate(graphicsContext);
|
||||
if (interop is null)
|
||||
using var provider = UkiyoeInteropProvider.TryCreate(graphicsContext, out var interopDevice);
|
||||
if (provider is null || interopDevice is null)
|
||||
{
|
||||
Assert.Skip("Direct3D 11 and Direct3D 12 sharing is unavailable.");
|
||||
return;
|
||||
}
|
||||
|
||||
using var pipeline = UkiyoePipeline.TryCreate(interop.Device);
|
||||
using var domain = interopDevice.RegisterExternalDomain(provider);
|
||||
using var resourceSet = UkiyoeResourceSet.Create(interopDevice, domain);
|
||||
using var pipeline = UkiyoePipeline.TryCreate(interopDevice);
|
||||
Assert.NotNull(pipeline);
|
||||
|
||||
const int width = 96;
|
||||
@@ -571,24 +573,46 @@ public sealed class UkiyoeEffectTests
|
||||
handle.Free();
|
||||
}
|
||||
|
||||
Assert.True(interop.EnsureResources(width, height));
|
||||
var bounds = new RawRectF(0f, 0f, width, height);
|
||||
Assert.True(resourceSet.TryEnsureSource(width, height, out _));
|
||||
var parameters = CreateParameters();
|
||||
var renderContext = provider.RenderContext;
|
||||
for (var iteration = 0; iteration < 2; iteration++)
|
||||
{
|
||||
interop.RenderInput(inputBitmap, bounds);
|
||||
interop.BeginCompute();
|
||||
try
|
||||
using (var borrow = resourceSet.BeginSourceExternalOperation())
|
||||
{
|
||||
pipeline!.Process(interop.SourceTexture, interop.OutputTexture, width, height, in parameters);
|
||||
var previousTarget = renderContext.Target;
|
||||
renderContext.Target = borrow.DangerousGetView().Bitmap;
|
||||
renderContext.BeginDraw();
|
||||
renderContext.Clear(null);
|
||||
renderContext.DrawImage(
|
||||
inputBitmap,
|
||||
new System.Numerics.Vector2(0f, 0f),
|
||||
null,
|
||||
InterpolationMode.NearestNeighbor,
|
||||
CompositeMode.SourceCopy);
|
||||
renderContext.EndDraw();
|
||||
renderContext.Target = previousTarget;
|
||||
}
|
||||
finally
|
||||
|
||||
pipeline!.Simulate(
|
||||
resourceSet.GetSourceComputeBinding(), width, height, 0, 0, width, height, in parameters);
|
||||
Assert.True(pipeline.TryGetVisibleBounds(width, height, in parameters, out var visible));
|
||||
Assert.True(resourceSet.TryEnsureOutput(
|
||||
iteration == 0 ? width : 1920,
|
||||
iteration == 0 ? height : 1080,
|
||||
out _));
|
||||
pipeline.RenderVisible(
|
||||
resourceSet.GetOutputComputeBinding(), width, height, visible, in parameters);
|
||||
|
||||
if (iteration == 0)
|
||||
{
|
||||
interop.EndCompute();
|
||||
using var retiredLease = resourceSet.AcquireOutputExternalViewLease();
|
||||
Assert.Equal(width, retiredLease.Width);
|
||||
Assert.Equal(height, retiredLease.Height);
|
||||
}
|
||||
}
|
||||
interop.WaitForIdle();
|
||||
|
||||
using var outputLease = resourceSet.AcquireOutputExternalViewLease();
|
||||
using var staging = graphicsContext.DeviceContext.CreateBitmap(
|
||||
new SizeI(width, height),
|
||||
new BitmapProperties1(
|
||||
@@ -596,7 +620,7 @@ public sealed class UkiyoeEffectTests
|
||||
96f,
|
||||
96f,
|
||||
BitmapOptions.CpuRead | BitmapOptions.CannotDraw));
|
||||
staging.CopyFromBitmap(interop.OutputBitmap);
|
||||
staging.CopyFromBitmap(outputLease.DangerousGetView().Bitmap);
|
||||
var mapped = staging.Map(MapOptions.Read);
|
||||
try
|
||||
{
|
||||
|
||||
+1
-9
@@ -1,16 +1,8 @@
|
||||
<Solution>
|
||||
<Solution>
|
||||
<Configurations>
|
||||
<Platform Name="Any CPU" />
|
||||
<Platform Name="x64" />
|
||||
</Configurations>
|
||||
<Project Path="ComputeSharp/src/ComputeSharp.Core/ComputeSharp.Core.csproj">
|
||||
<Platform Solution="*|x64" Project="Any CPU" />
|
||||
</Project>
|
||||
<Project Path="ComputeSharp/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj" />
|
||||
<Project Path="ComputeSharp/src/ComputeSharp/ComputeSharp.csproj">
|
||||
<Platform Solution="*|x64" Project="Any CPU" />
|
||||
</Project>
|
||||
<Project Path="ComputeSharp/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj" />
|
||||
<Project Path="Ukiyoe/Ukiyoe.csproj">
|
||||
<Platform Solution="*|x64" Project="x64" />
|
||||
</Project>
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
<Project>
|
||||
<PropertyGroup Condition="'$(MSBuildProjectName)' == 'ComputeSharp' or '$(MSBuildProjectName)' == 'ComputeSharp.Core'">
|
||||
<IsPackagedProject>false</IsPackagedProject>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition="'$(MSBuildProjectName)' == 'ComputeSharp.Core'">
|
||||
<InternalsVisibleTo Include="Ukiyoe.ComputeSharp, PublicKey=$(AssemblySignPublicKey)" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,14 +0,0 @@
|
||||
<Project>
|
||||
<PropertyGroup Condition="'$(MSBuildProjectName)' == 'ComputeSharp' or '$(MSBuildProjectName)' == 'ComputeSharp.Core'">
|
||||
<BaseOutputPath>$(MSBuildProjectDirectory)\bin\Ukiyoe\</BaseOutputPath>
|
||||
<BaseIntermediateOutputPath>$(MSBuildProjectDirectory)\obj\Ukiyoe\</BaseIntermediateOutputPath>
|
||||
<MSBuildProjectExtensionsPath>$(MSBuildProjectDirectory)\obj\</MSBuildProjectExtensionsPath>
|
||||
<DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)\obj\**</DefaultItemExcludes>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(MSBuildProjectName)' == 'ComputeSharp'">
|
||||
<AssemblyName>Ukiyoe.ComputeSharp</AssemblyName>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(MSBuildProjectName)' == 'ComputeSharp.Core'">
|
||||
<AssemblyName>Ukiyoe.ComputeSharp.Core</AssemblyName>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,9 +0,0 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<PrivateComputeSharpBuildProperties Condition="'$(BuildingSolutionFile)' != 'true'">CustomBeforeDirectoryBuildProps=$(MSBuildThisFileDirectory)ComputeSharp.Build.props;CustomAfterDirectoryBuildProps=$(MSBuildThisFileDirectory)ComputeSharp.Build.after.props</PrivateComputeSharpBuildProperties>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="$(MSBuildThisFileDirectory)..\ComputeSharp\src\ComputeSharp.Core\ComputeSharp.Core.csproj" SetConfiguration="Configuration=$(Configuration)" SetPlatform="Platform=AnyCPU" AdditionalProperties="$(PrivateComputeSharpBuildProperties)" GlobalPropertiesToRemove="SkipPluginDeploy" />
|
||||
<ProjectReference Include="$(MSBuildThisFileDirectory)..\ComputeSharp\src\ComputeSharp\ComputeSharp.csproj" SetConfiguration="Configuration=$(Configuration)" SetPlatform="Platform=AnyCPU" AdditionalProperties="$(PrivateComputeSharpBuildProperties)" GlobalPropertiesToRemove="SkipPluginDeploy" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
+10
-7
@@ -5,7 +5,7 @@
|
||||
<UseWPF>true</UseWPF>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>1.0.1</Version>
|
||||
<Version>1.0.2</Version>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
||||
@@ -83,6 +83,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ComputeWeave" Version="1.4.1" />
|
||||
<PackageReference Include="ILRepack" Version="2.0.46" PrivateAssets="all" ExcludeAssets="all" GeneratePathProperty="true" />
|
||||
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.142" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -96,20 +98,21 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ComputeSharp\src\ComputeSharp.Core.SourceGenerators\ComputeSharp.Core.SourceGenerators.csproj" SetConfiguration="Configuration=$(Configuration)" SetPlatform="Platform=AnyCPU" GlobalPropertiesToRemove="SkipPluginDeploy" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
<ProjectReference Include="..\ComputeSharp\src\ComputeSharp.SourceGenerators\ComputeSharp.SourceGenerators.csproj" SetConfiguration="Configuration=$(Configuration)" SetPlatform="Platform=AnyCPU" GlobalPropertiesToRemove="SkipPluginDeploy" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
<ProjectReference Include="..\YukkuriMovieMaker.Generator\YukkuriMovieMaker.Generator\YukkuriMovieMaker.Generator.csproj" SetConfiguration="Configuration=$(Configuration)" SetPlatform="Platform=AnyCPU" GlobalPropertiesToRemove="SkipPluginDeploy" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
<AdditionalFiles Include="**/*.csv" />
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="ComputeSharp.Reference.props" />
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Shaders\" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(SkipPluginDeploy)' != 'true' AND '$(BuildingSolutionFile)' != 'true'">
|
||||
<Exec Command="mkdir "$(YMM4DirPath)user\plugin\$(ProjectName)" > NUL 2>&1
copy "$(TargetPath)" "$(YMM4DirPath)user\plugin\$(ProjectName)\"
copy "$(TargetDir)Ukiyoe.ComputeSharp.dll" "$(YMM4DirPath)user\plugin\$(ProjectName)\"
copy "$(TargetDir)Ukiyoe.ComputeSharp.Core.dll" "$(YMM4DirPath)user\plugin\$(ProjectName)\"" />
|
||||
<Target Name="MergePluginAssembly" AfterTargets="PostBuildEvent" Inputs="$(TargetPath);$(TargetDir)ComputeWeave.dll;$(TargetDir)ComputeWeave.Core.dll" Outputs="$(TargetDir)plugin\$(TargetFileName)">
|
||||
<MakeDir Directories="$(TargetDir)plugin" />
|
||||
<Exec Command=""$(PkgILRepack)\tools\ILRepack.exe" /out:"$(TargetDir)plugin\$(TargetFileName)" /lib:"$([System.IO.Path]::GetDirectoryName('$(TargetPath)'))" /lib:"$([System.IO.Path]::GetDirectoryName('$(YMM4DirPath)'))" /internalize "$(TargetPath)" "$(TargetDir)ComputeWeave.dll" "$(TargetDir)ComputeWeave.Core.dll"" />
|
||||
</Target>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="MergePluginAssembly" Condition="'$(SkipPluginDeploy)' != 'true' AND '$(BuildingSolutionFile)' != 'true'">
|
||||
<Exec Command="mkdir "$(YMM4DirPath)user\plugin\$(ProjectName)" > NUL 2>&1
copy "$(TargetDir)plugin\$(TargetFileName)" "$(YMM4DirPath)user\plugin\$(ProjectName)\"
del "$(YMM4DirPath)user\plugin\$(ProjectName)\ComputeWeave.dll" > NUL 2>&1
del "$(YMM4DirPath)user\plugin\$(ProjectName)\ComputeWeave.Core.dll" > NUL 2>&1" />
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
+131
-55
@@ -1,4 +1,5 @@
|
||||
using System.Numerics;
|
||||
using ComputeWeave;
|
||||
using Vortice.Direct2D1;
|
||||
using Vortice.Direct2D1.Effects;
|
||||
using YukkuriMovieMaker.Commons;
|
||||
@@ -11,7 +12,12 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
{
|
||||
private readonly IGraphicsDevicesAndContext _devices;
|
||||
private readonly UkiyoeEffect _item;
|
||||
private UkiyoeGpuInterop? _interop;
|
||||
private UkiyoeInteropProvider? _interopProvider;
|
||||
private ComputeInteropDomain? _interopDomain;
|
||||
private UkiyoeResourceSet? _resourceSet;
|
||||
private ExternalTextureLease<UkiyoeExternalView>? _outputLease;
|
||||
private int _sourceWidth;
|
||||
private int _sourceHeight;
|
||||
private UkiyoePipeline? _pipeline;
|
||||
private UkiyoeCustomEffect? _effect;
|
||||
private Crop? _outputCrop;
|
||||
@@ -37,7 +43,7 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
|
||||
public override DrawDescription Update(EffectDescription effectDescription)
|
||||
{
|
||||
if (IsPassThroughEffect || _effect is null || _outputCrop is null || _outputTransform is null || _outputTransformOutput is null || _interop is null || _pipeline is null || input is null)
|
||||
if (IsPassThroughEffect || _effect is null || _outputCrop is null || _outputTransform is null || _outputTransformOutput is null || _resourceSet is null || _interopProvider is null || _pipeline is null || input is null)
|
||||
return effectDescription.DrawDescription;
|
||||
|
||||
var frame = effectDescription.ItemPosition.Frame;
|
||||
@@ -109,8 +115,14 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
var itemWidth = (int)widthValue;
|
||||
var itemHeight = (int)heightValue;
|
||||
|
||||
_interop.EnsureSource(itemWidth, itemHeight);
|
||||
_interop.RenderInput(input, new Vortice.RawRectF(bounds.Left, bounds.Top, bounds.Left + itemWidth, bounds.Top + itemHeight));
|
||||
if (!EnsureSource(itemWidth, itemHeight))
|
||||
{
|
||||
_effect.Amount = 0f;
|
||||
_isFirst = true;
|
||||
return effectDescription.DrawDescription;
|
||||
}
|
||||
|
||||
RenderInput(new Vortice.RawRectF(bounds.Left, bounds.Top, bounds.Left + itemWidth, bounds.Top + itemHeight));
|
||||
|
||||
var pipelineParameters = new UkiyoePipeline.Parameters(
|
||||
parameters.Quality,
|
||||
@@ -128,24 +140,15 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
parameters.LineColor.B / 255f,
|
||||
Math.Max(parameters.Seed, 0));
|
||||
|
||||
bool structureChanged;
|
||||
_interop.BeginCompute();
|
||||
try
|
||||
{
|
||||
structureChanged = _pipeline.Simulate(
|
||||
_interop.SourceTexture,
|
||||
canvasWidth,
|
||||
canvasHeight,
|
||||
margin,
|
||||
margin,
|
||||
itemWidth,
|
||||
itemHeight,
|
||||
in pipelineParameters);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_interop.EndCompute();
|
||||
}
|
||||
var structureChanged = _pipeline.Simulate(
|
||||
_resourceSet.GetSourceComputeBinding(),
|
||||
canvasWidth,
|
||||
canvasHeight,
|
||||
margin,
|
||||
margin,
|
||||
itemWidth,
|
||||
itemHeight,
|
||||
in pipelineParameters);
|
||||
|
||||
if (!_pipeline.TryGetVisibleBounds(canvasWidth, canvasHeight, in pipelineParameters, out var rect))
|
||||
{
|
||||
@@ -156,9 +159,16 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
return effectDescription.DrawDescription;
|
||||
}
|
||||
|
||||
if (!_interop.OutputCovers(rect.Width, rect.Height))
|
||||
if (!OutputCovers(rect.Width, rect.Height))
|
||||
_outputCrop.SetInput(0, null, true);
|
||||
var outputChanged = _interop.EnsureOutput(rect.Width, rect.Height);
|
||||
if (!EnsureOutput(rect.Width, rect.Height, out var outputChanged))
|
||||
{
|
||||
_effect.Amount = 0f;
|
||||
_parameters = parameters;
|
||||
_isFirst = true;
|
||||
_hasRenderState = false;
|
||||
return effectDescription.DrawDescription;
|
||||
}
|
||||
var renderState = new RenderState(
|
||||
pipelineParameters.LineDetail,
|
||||
pipelineParameters.LineStrength,
|
||||
@@ -173,27 +183,21 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
rect);
|
||||
if (structureChanged || outputChanged || !_hasOutput || !_hasRenderState || _renderState != renderState)
|
||||
{
|
||||
_interop.BeginCompute();
|
||||
try
|
||||
{
|
||||
_pipeline.RenderVisible(
|
||||
_interop.OutputTexture,
|
||||
canvasWidth,
|
||||
canvasHeight,
|
||||
rect,
|
||||
in pipelineParameters);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_interop.EndCompute();
|
||||
}
|
||||
_pipeline.RenderVisible(
|
||||
_resourceSet.GetOutputComputeBinding(),
|
||||
canvasWidth,
|
||||
canvasHeight,
|
||||
rect,
|
||||
in pipelineParameters);
|
||||
_renderState = renderState;
|
||||
_hasRenderState = true;
|
||||
}
|
||||
|
||||
_outputLease ??= _resourceSet.AcquireOutputExternalViewLease();
|
||||
|
||||
if (outputChanged || !_hasOutput)
|
||||
{
|
||||
_outputCrop.SetInput(0, _interop.OutputBitmap, true);
|
||||
_outputCrop.SetInput(0, _outputLease.DangerousGetView().Bitmap, true);
|
||||
_effect.SetInput(1, _outputTransformOutput, true);
|
||||
}
|
||||
var cropRect = new Vector4(0f, 0f, rect.Width, rect.Height);
|
||||
@@ -216,15 +220,95 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
return effectDescription.DrawDescription;
|
||||
}
|
||||
|
||||
private bool EnsureSource(int width, int height)
|
||||
{
|
||||
if (_sourceWidth == width && _sourceHeight == height)
|
||||
return true;
|
||||
|
||||
_sourceWidth = 0;
|
||||
_sourceHeight = 0;
|
||||
if (!_resourceSet!.TryEnsureSource(width, height, out _))
|
||||
return false;
|
||||
|
||||
_sourceWidth = width;
|
||||
_sourceHeight = height;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool OutputCovers(int width, int height)
|
||||
=> _outputLease is { IsDisposed: false } lease &&
|
||||
lease.Width >= width &&
|
||||
lease.Height >= height;
|
||||
|
||||
private bool EnsureOutput(int width, int height, out bool changed)
|
||||
{
|
||||
changed = false;
|
||||
if (OutputCovers(width, height))
|
||||
return true;
|
||||
|
||||
_outputLease?.Dispose();
|
||||
_outputLease = null;
|
||||
return _resourceSet!.TryEnsureOutput(width, height, out changed);
|
||||
}
|
||||
|
||||
private void RenderInput(Vortice.RawRectF bounds)
|
||||
{
|
||||
var renderContext = _interopProvider!.RenderContext;
|
||||
using var borrow = _resourceSet!.BeginSourceExternalOperation();
|
||||
var previousTarget = renderContext.Target;
|
||||
renderContext.Target = borrow.DangerousGetView().Bitmap;
|
||||
renderContext.BeginDraw();
|
||||
renderContext.Clear(null);
|
||||
renderContext.DrawImage(
|
||||
input,
|
||||
new Vector2(-bounds.Left, -bounds.Top),
|
||||
null,
|
||||
InterpolationMode.NearestNeighbor,
|
||||
CompositeMode.SourceCopy);
|
||||
renderContext.EndDraw();
|
||||
renderContext.Target = previousTarget;
|
||||
}
|
||||
|
||||
private void ReleaseInterop()
|
||||
{
|
||||
_outputLease?.Dispose();
|
||||
_outputLease = null;
|
||||
_pipeline?.Dispose();
|
||||
_pipeline = null;
|
||||
_resourceSet?.Dispose();
|
||||
_resourceSet?.WaitForDisposal();
|
||||
_resourceSet = null;
|
||||
_interopDomain?.Dispose();
|
||||
_interopDomain?.WaitForDisposal();
|
||||
_interopDomain = null;
|
||||
_interopProvider?.Dispose();
|
||||
_interopProvider = null;
|
||||
_sourceWidth = 0;
|
||||
_sourceHeight = 0;
|
||||
}
|
||||
|
||||
protected override ID2D1Image? CreateEffect(IGraphicsDevicesAndContext devices)
|
||||
{
|
||||
var interop = UkiyoeGpuInterop.TryCreate(devices);
|
||||
if (interop is null)
|
||||
var interopProvider = UkiyoeInteropProvider.TryCreate(devices, out var interopDevice);
|
||||
if (interopProvider is null || interopDevice is null)
|
||||
return null;
|
||||
var pipeline = UkiyoePipeline.TryCreate(interop.Device);
|
||||
if (pipeline is null)
|
||||
|
||||
try
|
||||
{
|
||||
interop.Dispose();
|
||||
_interopProvider = interopProvider;
|
||||
_interopDomain = interopDevice.RegisterExternalDomain(interopProvider);
|
||||
_resourceSet = UkiyoeResourceSet.Create(interopDevice, _interopDomain);
|
||||
_pipeline = UkiyoePipeline.TryCreate(interopDevice);
|
||||
}
|
||||
catch
|
||||
{
|
||||
ReleaseInterop();
|
||||
throw;
|
||||
}
|
||||
|
||||
if (_pipeline is null)
|
||||
{
|
||||
ReleaseInterop();
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -240,8 +324,7 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
if (!effect.IsEnabled)
|
||||
{
|
||||
effect.Dispose();
|
||||
pipeline.Dispose();
|
||||
interop.Dispose();
|
||||
ReleaseInterop();
|
||||
return null;
|
||||
}
|
||||
outputCrop = new Crop(devices.DeviceContext);
|
||||
@@ -253,8 +336,6 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
outputTransform.SetInput(0, outputCropOutput, true);
|
||||
outputTransformOutput = outputTransform.Output;
|
||||
output = effect.Output;
|
||||
_interop = interop;
|
||||
_pipeline = pipeline;
|
||||
_effect = effect;
|
||||
_outputCrop = outputCrop;
|
||||
_outputCropOutput = outputCropOutput;
|
||||
@@ -276,8 +357,7 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
outputCropOutput?.Dispose();
|
||||
outputCrop?.Dispose();
|
||||
effect?.Dispose();
|
||||
pipeline.Dispose();
|
||||
interop.Dispose();
|
||||
ReleaseInterop();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -308,11 +388,7 @@ internal sealed class UkiyoeEffectProcessor : VideoEffectProcessorBase
|
||||
if (disposing)
|
||||
{
|
||||
ClearEffectChain();
|
||||
_interop?.WaitForIdle();
|
||||
_pipeline?.Dispose();
|
||||
_pipeline = null;
|
||||
_interop?.Dispose();
|
||||
_interop = null;
|
||||
ReleaseInterop();
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
||||
+157
-267
@@ -1,7 +1,5 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp;
|
||||
using ComputeSharp.Interop;
|
||||
using Vortice;
|
||||
using System.Threading;
|
||||
using ComputeWeave;
|
||||
using Vortice.Direct2D1;
|
||||
using Vortice.Direct3D11;
|
||||
using Vortice.DXGI;
|
||||
@@ -10,314 +8,206 @@ using PixelFormat = Vortice.DCommon.PixelFormat;
|
||||
|
||||
namespace Ukiyoe;
|
||||
|
||||
internal sealed class UkiyoeGpuInterop : IDisposable
|
||||
internal sealed class UkiyoeQueueScheduler : ComputeExternalQueueScheduler
|
||||
{
|
||||
private static readonly Guid D3D12FenceGuid = new("0a753dcf-c4d8-4b91-adf6-be5a60d95a76");
|
||||
private int _entered;
|
||||
|
||||
private readonly GraphicsDevice _device;
|
||||
private readonly ID3D11Device1 _d3dDevice;
|
||||
private readonly ID3D11DeviceContext4 _d3dContext;
|
||||
private readonly ID3D11Fence _fence;
|
||||
private readonly ID2D1DeviceContext6 _renderContext;
|
||||
private nint _d3d12Fence;
|
||||
private ReadWriteTexture2D<Bgra32, Float4>? _sourceTexture;
|
||||
private ReadWriteTexture2D<Bgra32, Float4>? _outputTexture;
|
||||
private ID3D11Texture2D? _sourceD3D11Texture;
|
||||
private ID3D11Texture2D? _outputD3D11Texture;
|
||||
private ID2D1Bitmap1? _sourceBitmap;
|
||||
private ID2D1Bitmap1? _outputBitmap;
|
||||
private int _sourceWidth;
|
||||
private int _sourceHeight;
|
||||
private int _outputWidth;
|
||||
private int _outputHeight;
|
||||
private ulong _fenceValue;
|
||||
private bool _computeActive;
|
||||
private bool _disposed;
|
||||
|
||||
private UkiyoeGpuInterop(
|
||||
GraphicsDevice device,
|
||||
ID3D11Device1 d3dDevice,
|
||||
ID3D11DeviceContext4 d3dContext,
|
||||
ID3D11Fence fence,
|
||||
ID2D1DeviceContext6 renderContext,
|
||||
nint d3d12Fence)
|
||||
protected override void EnterCore()
|
||||
{
|
||||
_device = device;
|
||||
_d3dDevice = d3dDevice;
|
||||
_d3dContext = d3dContext;
|
||||
_fence = fence;
|
||||
_renderContext = renderContext;
|
||||
_d3d12Fence = d3d12Fence;
|
||||
if (Interlocked.CompareExchange(ref _entered, 1, 0) != 0)
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
public GraphicsDevice Device => _device;
|
||||
|
||||
public ReadWriteTexture2D<Bgra32, Float4> SourceTexture => _sourceTexture!;
|
||||
|
||||
public ReadWriteTexture2D<Bgra32, Float4> OutputTexture => _outputTexture!;
|
||||
|
||||
public ID2D1Bitmap1 OutputBitmap => _outputBitmap!;
|
||||
|
||||
public bool SourceMatches(int width, int height)
|
||||
=> _sourceTexture is not null && _sourceWidth == width && _sourceHeight == height;
|
||||
|
||||
public bool OutputCovers(int width, int height)
|
||||
=> _outputTexture is not null && _outputWidth >= width && _outputHeight >= height;
|
||||
|
||||
public static unsafe UkiyoeGpuInterop? TryCreate(IGraphicsDevicesAndContext devices)
|
||||
protected override void ExitCore()
|
||||
{
|
||||
ID3D11Device1? d3dDevice = null;
|
||||
ID3D11DeviceContext4? d3dContext = null;
|
||||
ID3D11Fence? fence = null;
|
||||
if (Interlocked.Exchange(ref _entered, 0) != 1)
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
protected override void DisposeCore()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class UkiyoeExternalView(ID3D11Texture2D texture, ID2D1Bitmap1 bitmap) : IDisposable
|
||||
{
|
||||
private readonly ID3D11Texture2D _texture = texture;
|
||||
private readonly ID2D1Bitmap1 _bitmap = bitmap;
|
||||
|
||||
public ID2D1Bitmap1 Bitmap => _bitmap;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_bitmap.Dispose();
|
||||
_texture.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class UkiyoeInteropProvider : IComputeExternalInteropProvider<UkiyoeExternalView>
|
||||
{
|
||||
private readonly ID3D11Device1 _device;
|
||||
private readonly ID3D11Device5 _device5;
|
||||
private readonly ID3D11DeviceContext4 _context;
|
||||
private readonly ID2D1DeviceContext6 _renderContext;
|
||||
private readonly UkiyoeQueueScheduler _scheduler;
|
||||
private readonly long _adapterLuid;
|
||||
private ID3D11Fence? _fence;
|
||||
private bool _disposed;
|
||||
|
||||
private UkiyoeInteropProvider(
|
||||
ID3D11Device1 device,
|
||||
ID3D11Device5 device5,
|
||||
ID3D11DeviceContext4 context,
|
||||
ID2D1DeviceContext6 renderContext,
|
||||
UkiyoeQueueScheduler scheduler,
|
||||
long adapterLuid)
|
||||
{
|
||||
_device = device;
|
||||
_device5 = device5;
|
||||
_context = context;
|
||||
_renderContext = renderContext;
|
||||
_scheduler = scheduler;
|
||||
_adapterLuid = adapterLuid;
|
||||
}
|
||||
|
||||
public ExternalAdapterIdentity AdapterIdentity => new(_adapterLuid);
|
||||
|
||||
public ComputeExternalQueueScheduler Scheduler => _scheduler;
|
||||
|
||||
public ExternalInteropCapabilities Capabilities =>
|
||||
ExternalInteropCapabilities.SharedFence |
|
||||
ExternalInteropCapabilities.SharedTexture2D |
|
||||
ExternalInteropCapabilities.SingleImmediateContextOrdering |
|
||||
ExternalInteropCapabilities.PersistentExternalViewOrdering;
|
||||
|
||||
public ID2D1DeviceContext6 RenderContext => _renderContext;
|
||||
|
||||
public static UkiyoeInteropProvider? TryCreate(IGraphicsDevicesAndContext devices, out GraphicsDevice? graphicsDevice)
|
||||
{
|
||||
graphicsDevice = null;
|
||||
|
||||
ID3D11Device1? device = null;
|
||||
ID3D11Device5? device5 = null;
|
||||
ID3D11DeviceContext4? context = null;
|
||||
ID2D1DeviceContext6? renderContext = null;
|
||||
nint d3d12Fence = 0;
|
||||
nint fenceHandle = 0;
|
||||
UkiyoeQueueScheduler? scheduler = null;
|
||||
try
|
||||
{
|
||||
var device = GetMatchingDevice(devices);
|
||||
d3dDevice = devices.D3D.Device.QueryInterface<ID3D11Device1>();
|
||||
d3dContext = devices.D3D.DeviceContext.QueryInterface<ID3D11DeviceContext4>();
|
||||
using var d3dDevice5 = devices.D3D.Device.QueryInterface<ID3D11Device5>();
|
||||
var fenceGuid = D3D12FenceGuid;
|
||||
InteropServices.CreateSharedFence(device, &fenceGuid, (void**)&d3d12Fence, &fenceHandle);
|
||||
fence = d3dDevice5.OpenSharedFence(fenceHandle);
|
||||
renderContext = devices.D2D.Device.CreateDeviceContext(DeviceContextOptions.EnableMultithreadedOptimizations);
|
||||
return new UkiyoeGpuInterop(device, d3dDevice, d3dContext, fence, renderContext, d3d12Fence);
|
||||
var adapterLuidText = devices.DXGI.Adapter.Description.Luid.ToString();
|
||||
using var enumerator = GraphicsDevice
|
||||
.QueryDevices(candidate => string.Equals(candidate.Luid.ToString(), adapterLuidText, StringComparison.Ordinal))
|
||||
.GetEnumerator();
|
||||
if (!enumerator.MoveNext())
|
||||
return null;
|
||||
|
||||
graphicsDevice = enumerator.Current;
|
||||
device = devices.D3D.Device.QueryInterface<ID3D11Device1>();
|
||||
device5 = devices.D3D.Device.QueryInterface<ID3D11Device5>();
|
||||
context = devices.D3D.DeviceContext.QueryInterface<ID3D11DeviceContext4>();
|
||||
renderContext = devices.D2D.Device
|
||||
.CreateDeviceContext(DeviceContextOptions.EnableMultithreadedOptimizations)
|
||||
.QueryInterface<ID2D1DeviceContext6>();
|
||||
scheduler = new UkiyoeQueueScheduler();
|
||||
return new UkiyoeInteropProvider(device, device5, context, renderContext, scheduler, graphicsDevice.Luid.ToInt64());
|
||||
}
|
||||
catch
|
||||
{
|
||||
scheduler?.Dispose();
|
||||
renderContext?.Dispose();
|
||||
fence?.Dispose();
|
||||
d3dContext?.Dispose();
|
||||
d3dDevice?.Dispose();
|
||||
if (d3d12Fence != 0)
|
||||
Marshal.Release(d3d12Fence);
|
||||
context?.Dispose();
|
||||
device5?.Dispose();
|
||||
device?.Dispose();
|
||||
graphicsDevice = null;
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (fenceHandle != 0)
|
||||
CloseHandle(fenceHandle);
|
||||
}
|
||||
}
|
||||
|
||||
public bool EnsureResources(int width, int height)
|
||||
public void Initialize(in ExternalTimelineInitialization initialization)
|
||||
{
|
||||
var sourceChanged = EnsureSource(width, height);
|
||||
var outputChanged = EnsureOutput(width, height);
|
||||
return sourceChanged || outputChanged;
|
||||
_fence = _device5.OpenSharedFence<ID3D11Fence>(initialization.SharedFenceHandle.DangerousGetHandle());
|
||||
}
|
||||
|
||||
public bool EnsureSource(int width, int height)
|
||||
public void EnqueueSignal(ulong value)
|
||||
{
|
||||
if (SourceMatches(width, height))
|
||||
return false;
|
||||
|
||||
if (_sourceTexture is not null)
|
||||
WaitForIdle();
|
||||
ReleaseSourceResources();
|
||||
CreateSourceResources(width, height);
|
||||
return true;
|
||||
_context.Signal(_fence!, value);
|
||||
}
|
||||
|
||||
public bool EnsureOutput(int width, int height)
|
||||
public void FlushAfterSignal()
|
||||
{
|
||||
if (OutputCovers(width, height))
|
||||
return false;
|
||||
|
||||
var capacityWidth = Math.Max(width, _outputWidth);
|
||||
var capacityHeight = Math.Max(height, _outputHeight);
|
||||
if (_outputTexture is not null)
|
||||
WaitForIdle();
|
||||
ReleaseOutputResources();
|
||||
CreateOutputResources(capacityWidth, capacityHeight);
|
||||
return true;
|
||||
_context.Flush();
|
||||
}
|
||||
|
||||
public void RenderInput(ID2D1Image source, RawRectF bounds)
|
||||
public void EnqueueWait(ulong value)
|
||||
{
|
||||
_renderContext.BeginDraw();
|
||||
_renderContext.Clear(null);
|
||||
_renderContext.DrawImage(
|
||||
source,
|
||||
new System.Numerics.Vector2(-bounds.Left, -bounds.Top),
|
||||
null,
|
||||
InterpolationMode.NearestNeighbor,
|
||||
CompositeMode.SourceCopy);
|
||||
_renderContext.EndDraw();
|
||||
_context.Wait(_fence!, value);
|
||||
}
|
||||
|
||||
public unsafe void BeginCompute()
|
||||
public UkiyoeExternalView OpenSharedTexture(BorrowedSharedHandle resourceHandle, in ExternalTextureDescriptor descriptor)
|
||||
{
|
||||
if (_computeActive)
|
||||
throw new InvalidOperationException();
|
||||
var value = ++_fenceValue;
|
||||
_d3dContext.Signal(_fence, value);
|
||||
_d3dContext.Flush();
|
||||
InteropServices.WaitForSharedFence(_device, (void*)_d3d12Fence, value);
|
||||
_computeActive = true;
|
||||
}
|
||||
|
||||
public unsafe void EndCompute()
|
||||
{
|
||||
if (!_computeActive)
|
||||
throw new InvalidOperationException();
|
||||
ID3D11Texture2D? texture = null;
|
||||
ID2D1Bitmap1? bitmap = null;
|
||||
try
|
||||
{
|
||||
var value = ++_fenceValue;
|
||||
InteropServices.SignalSharedFence(_device, (void*)_d3d12Fence, value);
|
||||
_d3dContext.Wait(_fence, value);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_computeActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void WaitForIdle()
|
||||
{
|
||||
if (_computeActive)
|
||||
throw new InvalidOperationException();
|
||||
var value = ++_fenceValue;
|
||||
_d3dContext.Signal(_fence, value);
|
||||
_d3dContext.Flush();
|
||||
_fence.SetEventOnCompletion(value, 0);
|
||||
}
|
||||
|
||||
private static GraphicsDevice GetMatchingDevice(IGraphicsDevicesAndContext devices)
|
||||
{
|
||||
var adapterLuid = devices.DXGI.Adapter.Description.Luid.ToString();
|
||||
using var enumerator = GraphicsDevice.QueryDevices(
|
||||
device => string.Equals(device.Luid.ToString(), adapterLuid, StringComparison.Ordinal)).GetEnumerator();
|
||||
if (!enumerator.MoveNext())
|
||||
throw new NotSupportedException();
|
||||
return enumerator.Current;
|
||||
}
|
||||
|
||||
private void CreateSourceResources(int width, int height)
|
||||
{
|
||||
ReadWriteTexture2D<Bgra32, Float4>? sourceTexture = null;
|
||||
ID3D11Texture2D? sourceD3D11Texture = null;
|
||||
ID2D1Bitmap1? sourceBitmap = null;
|
||||
nint sourceHandle = 0;
|
||||
try
|
||||
{
|
||||
sourceTexture = InteropServices.AllocateSharedReadWriteTexture2D<Bgra32, Float4>(_device, width, height);
|
||||
sourceHandle = InteropServices.CreateSharedHandle(sourceTexture);
|
||||
sourceD3D11Texture = _d3dDevice.OpenSharedResource1<ID3D11Texture2D>(sourceHandle);
|
||||
using var sourceSurface = sourceD3D11Texture.QueryInterface<IDXGISurface>();
|
||||
texture = _device.OpenSharedResource1<ID3D11Texture2D>(resourceHandle.DangerousGetHandle());
|
||||
using var surface = texture.QueryInterface<IDXGISurface>();
|
||||
var pixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied);
|
||||
sourceBitmap = _renderContext.CreateBitmapFromDxgiSurface(
|
||||
sourceSurface,
|
||||
new BitmapProperties1(pixelFormat, 96f, 96f, BitmapOptions.Target));
|
||||
_renderContext.Target = sourceBitmap;
|
||||
|
||||
_sourceTexture = sourceTexture;
|
||||
_sourceD3D11Texture = sourceD3D11Texture;
|
||||
_sourceBitmap = sourceBitmap;
|
||||
_sourceWidth = width;
|
||||
_sourceHeight = height;
|
||||
sourceTexture = null;
|
||||
sourceD3D11Texture = null;
|
||||
sourceBitmap = null;
|
||||
var options = descriptor.ExternalUsage is ExternalTextureUsage.RenderTarget
|
||||
? BitmapOptions.Target
|
||||
: BitmapOptions.None;
|
||||
bitmap = _renderContext.CreateBitmapFromDxgiSurface(
|
||||
surface,
|
||||
new BitmapProperties1(pixelFormat, 96f, 96f, options));
|
||||
var view = new UkiyoeExternalView(texture, bitmap);
|
||||
texture = null;
|
||||
bitmap = null;
|
||||
return view;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (sourceHandle != 0)
|
||||
CloseHandle(sourceHandle);
|
||||
sourceBitmap?.Dispose();
|
||||
sourceD3D11Texture?.Dispose();
|
||||
sourceTexture?.Dispose();
|
||||
bitmap?.Dispose();
|
||||
texture?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateOutputResources(int width, int height)
|
||||
public void OnDeviceTerminal(Exception reason)
|
||||
{
|
||||
ReadWriteTexture2D<Bgra32, Float4>? outputTexture = null;
|
||||
ID3D11Texture2D? outputD3D11Texture = null;
|
||||
ID2D1Bitmap1? outputBitmap = null;
|
||||
nint outputHandle = 0;
|
||||
try
|
||||
{
|
||||
outputTexture = InteropServices.AllocateSharedReadWriteTexture2D<Bgra32, Float4>(_device, width, height);
|
||||
outputHandle = InteropServices.CreateSharedHandle(outputTexture);
|
||||
outputD3D11Texture = _d3dDevice.OpenSharedResource1<ID3D11Texture2D>(outputHandle);
|
||||
using var outputSurface = outputD3D11Texture.QueryInterface<IDXGISurface>();
|
||||
var pixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied);
|
||||
outputBitmap = _renderContext.CreateBitmapFromDxgiSurface(
|
||||
outputSurface,
|
||||
new BitmapProperties1(pixelFormat, 96f, 96f, BitmapOptions.None));
|
||||
|
||||
_outputTexture = outputTexture;
|
||||
_outputD3D11Texture = outputD3D11Texture;
|
||||
_outputBitmap = outputBitmap;
|
||||
_outputWidth = width;
|
||||
_outputHeight = height;
|
||||
outputTexture = null;
|
||||
outputD3D11Texture = null;
|
||||
outputBitmap = null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (outputHandle != 0)
|
||||
CloseHandle(outputHandle);
|
||||
outputBitmap?.Dispose();
|
||||
outputD3D11Texture?.Dispose();
|
||||
outputTexture?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleaseSourceResources()
|
||||
{
|
||||
_renderContext.Target = null;
|
||||
_sourceBitmap?.Dispose();
|
||||
_sourceD3D11Texture?.Dispose();
|
||||
_sourceTexture?.Dispose();
|
||||
_sourceBitmap = null;
|
||||
_sourceD3D11Texture = null;
|
||||
_sourceTexture = null;
|
||||
_sourceWidth = 0;
|
||||
_sourceHeight = 0;
|
||||
}
|
||||
|
||||
private void ReleaseOutputResources()
|
||||
{
|
||||
_outputBitmap?.Dispose();
|
||||
_outputD3D11Texture?.Dispose();
|
||||
_outputTexture?.Dispose();
|
||||
_outputBitmap = null;
|
||||
_outputD3D11Texture = null;
|
||||
_outputTexture = null;
|
||||
_outputWidth = 0;
|
||||
_outputHeight = 0;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
try
|
||||
{
|
||||
WaitForIdle();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_disposed = true;
|
||||
ReleaseOutputResources();
|
||||
ReleaseSourceResources();
|
||||
_renderContext.Dispose();
|
||||
_fence.Dispose();
|
||||
_d3dContext.Dispose();
|
||||
_d3dDevice.Dispose();
|
||||
if (_d3d12Fence != 0)
|
||||
{
|
||||
Marshal.Release(_d3d12Fence);
|
||||
_d3d12Fence = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool CloseHandle(nint handle);
|
||||
_disposed = true;
|
||||
_fence?.Dispose();
|
||||
_renderContext.Dispose();
|
||||
_context.Dispose();
|
||||
_device5.Dispose();
|
||||
_device.Dispose();
|
||||
_scheduler.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[ComputeInteropResourceSet]
|
||||
internal sealed partial class UkiyoeResourceSet
|
||||
{
|
||||
[ComputeSharedTexture(
|
||||
ComputeResourceResizePolicy.Exact,
|
||||
ComputeResourceAccess.ReadWrite,
|
||||
ExternalResourceAccess.Write,
|
||||
ExternalTextureUsage.RenderTarget,
|
||||
ComputeAlphaMode.Premultiplied,
|
||||
ComputeSharedTextureInitialOwner.External,
|
||||
ComputeResourceRecovery.RecreateFromHost)]
|
||||
private readonly SharedTextureSlot<Bgra32, Float4, UkiyoeExternalView> _source;
|
||||
|
||||
[ComputeSharedTexture(
|
||||
ComputeResourceResizePolicy.GrowOnly,
|
||||
ComputeResourceAccess.ReadWrite,
|
||||
ExternalResourceAccess.Read,
|
||||
ExternalTextureUsage.Sampled,
|
||||
ComputeAlphaMode.Premultiplied,
|
||||
ComputeSharedTextureInitialOwner.Compute,
|
||||
ComputeResourceRecovery.Recompute)]
|
||||
private readonly SharedTextureSlot<Bgra32, Float4, UkiyoeExternalView> _output;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using ComputeSharp;
|
||||
using ComputeWeave;
|
||||
|
||||
namespace Ukiyoe;
|
||||
|
||||
|
||||
+70
-175
@@ -1,22 +1,14 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp;
|
||||
using ComputeWeave;
|
||||
|
||||
namespace Ukiyoe;
|
||||
|
||||
internal sealed class UkiyoePipeline : IDisposable
|
||||
{
|
||||
private readonly GraphicsDevice _device;
|
||||
private readonly UkiyoePipelineHost _host;
|
||||
private readonly ReadWriteBuffer<int> _scratch;
|
||||
private readonly ReadBackBuffer<int> _scratchReadBack;
|
||||
private ReadWriteBuffer<Float4>? _colorIn;
|
||||
private ReadWriteBuffer<Float4>? _colorA;
|
||||
private ReadWriteBuffer<Float4>? _colorB;
|
||||
private ReadWriteBuffer<float>? _gray;
|
||||
private ReadWriteBuffer<float>? _gradientMagnitude;
|
||||
private ReadWriteBuffer<float>? _response;
|
||||
private ReadWriteBuffer<float>? _line;
|
||||
private ReadWriteBuffer<Float2>? _tangentA;
|
||||
private ReadWriteBuffer<Float2>? _tangentB;
|
||||
private StructureKey? _structureKey;
|
||||
private int _cachedLitCount;
|
||||
private int _cachedBoundsMinX;
|
||||
@@ -30,9 +22,10 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
private int _packedWidth;
|
||||
private int _packedHeight;
|
||||
|
||||
private UkiyoePipeline(GraphicsDevice device)
|
||||
private UkiyoePipeline(GraphicsDevice device, UkiyoePipelineHost host)
|
||||
{
|
||||
_device = device;
|
||||
_host = host;
|
||||
_scratch = device.AllocateReadWriteBuffer<int>(UkiyoeSettings.ScratchLength);
|
||||
_scratchReadBack = device.AllocateReadBackBuffer<int>(UkiyoeSettings.ScratchLength);
|
||||
}
|
||||
@@ -41,7 +34,7 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
{
|
||||
try
|
||||
{
|
||||
return new UkiyoePipeline(GraphicsDevice.GetDefault());
|
||||
return TryCreate(GraphicsDevice.GetDefault());
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -51,12 +44,16 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
|
||||
public static UkiyoePipeline? TryCreate(GraphicsDevice device)
|
||||
{
|
||||
UkiyoePipelineHost? host = null;
|
||||
try
|
||||
{
|
||||
return new UkiyoePipeline(device);
|
||||
host = UkiyoePipelineHost.Create(device, UkiyoeSettings.MaximumPendingSubmissions);
|
||||
return new UkiyoePipeline(device, host);
|
||||
}
|
||||
catch
|
||||
{
|
||||
host?.Dispose();
|
||||
host?.WaitForDisposal();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -74,8 +71,7 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
var sourceTexture = _packedSource!;
|
||||
var outputTexture = _packedOutput!;
|
||||
sourceTexture.CopyFrom(MemoryMarshal.Cast<int, Bgra32>(source[..pixelCount]));
|
||||
using (ComputeContext context = _device.CreateComputeContext())
|
||||
RecordFullPipeline(in context, sourceTexture, outputTexture, width, height, in parameters);
|
||||
SubmitFullPipeline(sourceTexture, outputTexture, width, height, in parameters).Wait();
|
||||
outputTexture.CopyTo(MemoryMarshal.Cast<int, Bgra32>(destination[..pixelCount]));
|
||||
}
|
||||
|
||||
@@ -87,9 +83,7 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
in Parameters parameters)
|
||||
{
|
||||
EnsureGridFor(width, height, parameters.Quality);
|
||||
using ComputeContext context = _device.CreateComputeContext();
|
||||
RecordFullPipeline(in context, source, destination, width, height, in parameters);
|
||||
context.Submit();
|
||||
_ = SubmitFullPipeline(source, destination, width, height, in parameters);
|
||||
}
|
||||
|
||||
internal void ProcessSharedAndWait(
|
||||
@@ -100,8 +94,7 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
in Parameters parameters)
|
||||
{
|
||||
EnsureGridFor(width, height, parameters.Quality);
|
||||
using ComputeContext context = _device.CreateComputeContext();
|
||||
RecordFullPipeline(in context, source, destination, width, height, in parameters);
|
||||
SubmitFullPipeline(source, destination, width, height, in parameters).Wait();
|
||||
}
|
||||
|
||||
internal bool Simulate(
|
||||
@@ -113,14 +106,37 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
int sourceWidth,
|
||||
int sourceHeight,
|
||||
in Parameters parameters)
|
||||
{
|
||||
var derived = BeginSimulate(canvasWidth, canvasHeight, in parameters);
|
||||
_host.RecordSilhouetteAndMaskHash(
|
||||
source, _scratch, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, _gridWidth, _gridHeight, in derived).Wait();
|
||||
return CompleteSimulate(canvasWidth, canvasHeight, in parameters, in derived);
|
||||
}
|
||||
|
||||
internal bool Simulate(
|
||||
ComputeResourceBinding<ReadWriteTexture2D<Bgra32, Float4>> source,
|
||||
int canvasWidth,
|
||||
int canvasHeight,
|
||||
int sourceOffsetX,
|
||||
int sourceOffsetY,
|
||||
int sourceWidth,
|
||||
int sourceHeight,
|
||||
in Parameters parameters)
|
||||
{
|
||||
var derived = BeginSimulate(canvasWidth, canvasHeight, in parameters);
|
||||
_host.RecordSharedSilhouetteAndMaskHash(
|
||||
source, _scratch, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, _gridWidth, _gridHeight, in derived).Wait();
|
||||
return CompleteSimulate(canvasWidth, canvasHeight, in parameters, in derived);
|
||||
}
|
||||
|
||||
private DerivedValues BeginSimulate(int canvasWidth, int canvasHeight, in Parameters parameters)
|
||||
{
|
||||
EnsureGridFor(canvasWidth, canvasHeight, parameters.Quality);
|
||||
var derived = Derive(canvasWidth, canvasHeight, in parameters);
|
||||
using (ComputeContext context = _device.CreateComputeContext())
|
||||
{
|
||||
RecordSilhouetteStage(in context, source, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, in derived);
|
||||
RecordMaskHashStage(in context);
|
||||
}
|
||||
return Derive(canvasWidth, canvasHeight, in parameters);
|
||||
}
|
||||
|
||||
private bool CompleteSimulate(int canvasWidth, int canvasHeight, in Parameters parameters, in DerivedValues derived)
|
||||
{
|
||||
_scratchReadBack.CopyFrom(_scratch);
|
||||
var hashed = _scratchReadBack.Span;
|
||||
_cachedLitCount = hashed[UkiyoeSettings.ScratchLitCount];
|
||||
@@ -141,8 +157,7 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
if (_structureKey == key)
|
||||
return false;
|
||||
|
||||
using (ComputeContext context = _device.CreateComputeContext())
|
||||
RecordStructureStage(in context, in derived);
|
||||
_host.RecordStructure(_scratch, _gridWidth, _gridHeight, in derived).Wait();
|
||||
_structureKey = key;
|
||||
return true;
|
||||
}
|
||||
@@ -177,12 +192,21 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
in Parameters parameters)
|
||||
{
|
||||
var derived = Derive(canvasWidth, canvasHeight, in parameters);
|
||||
using ComputeContext context = _device.CreateComputeContext();
|
||||
RecordRenderStage(in context, output, rect, in derived, in parameters);
|
||||
_host.RecordRender(output, in rect, _gridWidth, _gridHeight, in derived, in parameters).Wait();
|
||||
}
|
||||
|
||||
private void RecordFullPipeline(
|
||||
in ComputeContext context,
|
||||
internal void RenderVisible(
|
||||
ComputeResourceBinding<ReadWriteTexture2D<Bgra32, Float4>> output,
|
||||
int canvasWidth,
|
||||
int canvasHeight,
|
||||
PixelRect rect,
|
||||
in Parameters parameters)
|
||||
{
|
||||
var derived = Derive(canvasWidth, canvasHeight, in parameters);
|
||||
_host.RecordSharedRender(output, in rect, _gridWidth, _gridHeight, in derived, in parameters).Wait();
|
||||
}
|
||||
|
||||
private ComputeSubmission SubmitFullPipeline(
|
||||
ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
int width,
|
||||
@@ -191,107 +215,7 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
{
|
||||
_structureKey = null;
|
||||
var derived = Derive(width, height, in parameters);
|
||||
RecordSilhouetteStage(in context, source, 0, 0, width, height, in derived);
|
||||
RecordStructureStage(in context, in derived);
|
||||
RecordRenderStage(in context, output, new PixelRect(0, 0, width, height), in derived, in parameters);
|
||||
}
|
||||
|
||||
private void RecordSilhouetteStage(
|
||||
in ComputeContext context,
|
||||
ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
int sourceOffsetX,
|
||||
int sourceOffsetY,
|
||||
int sourceWidth,
|
||||
int sourceHeight,
|
||||
in DerivedValues derived)
|
||||
{
|
||||
context.For(_gridWidth, _gridHeight, new SilhouetteShader(
|
||||
source, _colorIn!, _gray!, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, _gridWidth, _gridHeight, derived.CellSize));
|
||||
context.Barrier(_colorIn!);
|
||||
context.Barrier(_gray!);
|
||||
}
|
||||
|
||||
private void RecordMaskHashStage(in ComputeContext context)
|
||||
{
|
||||
context.For(1, new MaskHashResetShader(_scratch));
|
||||
context.Barrier(_scratch);
|
||||
context.For(_gridWidth, _gridHeight, new MaskHashShader(_colorIn!, _scratch, _gridWidth, _gridHeight));
|
||||
context.Barrier(_scratch);
|
||||
}
|
||||
|
||||
private void RecordStructureStage(in ComputeContext context, in DerivedValues derived)
|
||||
{
|
||||
var gridWidth = _gridWidth;
|
||||
var gridHeight = _gridHeight;
|
||||
|
||||
context.For(1, new InitScratchShader(_scratch));
|
||||
context.Barrier(_scratch);
|
||||
context.For(gridWidth, gridHeight, new GradientShader(_gray!, _tangentA!, _gradientMagnitude!, _scratch, gridWidth, gridHeight));
|
||||
context.Barrier(_tangentA!);
|
||||
context.Barrier(_gradientMagnitude!);
|
||||
context.Barrier(_scratch);
|
||||
|
||||
var tangentIn = _tangentA!;
|
||||
var tangentOut = _tangentB!;
|
||||
for (var iteration = 0; iteration < derived.EtfIterations; iteration++)
|
||||
{
|
||||
context.For(gridWidth, gridHeight, new EtfShader(tangentIn, tangentOut, _gradientMagnitude!, _scratch, gridWidth, gridHeight));
|
||||
context.Barrier(tangentOut);
|
||||
(tangentIn, tangentOut) = (tangentOut, tangentIn);
|
||||
}
|
||||
var tangent = tangentIn;
|
||||
|
||||
context.For(gridWidth, gridHeight, new CopyColorShader(_colorIn!, _colorA!, gridWidth, gridHeight));
|
||||
context.Barrier(_colorA!);
|
||||
var flattenDispatchWidth = (gridWidth + UkiyoeSettings.FlattenGroupDim - 1) & ~(UkiyoeSettings.FlattenGroupDim - 1);
|
||||
var flattenDispatchHeight = (gridHeight + UkiyoeSettings.FlattenGroupDim - 1) & ~(UkiyoeSettings.FlattenGroupDim - 1);
|
||||
var iterateIn = _colorA!;
|
||||
var iterateOut = _colorB!;
|
||||
for (var iteration = 0; iteration < derived.FlattenIterations; iteration++)
|
||||
{
|
||||
context.For(flattenDispatchWidth, flattenDispatchHeight, new FlattenShader(
|
||||
_colorIn!, iterateIn, iterateOut, _gradientMagnitude!, _scratch, gridWidth, gridHeight, derived.FlattenBeta));
|
||||
context.Barrier(iterateOut);
|
||||
(iterateIn, iterateOut) = (iterateOut, iterateIn);
|
||||
}
|
||||
|
||||
for (var iteration = 0; iteration < derived.FdogIterations; iteration++)
|
||||
{
|
||||
context.For(gridWidth, gridHeight, new DogShader(
|
||||
_gray!, tangent, _response!, gridWidth, gridHeight, derived.LineSigmaCells, derived.DogExtent));
|
||||
context.Barrier(_response!);
|
||||
context.For(gridWidth, gridHeight, new FlowAccumulateShader(
|
||||
_response!, tangent, _line!, gridWidth, gridHeight, derived.FlowSigma, derived.FlowExtent));
|
||||
context.Barrier(_line!);
|
||||
if (iteration < derived.FdogIterations - 1)
|
||||
{
|
||||
context.For(gridWidth, gridHeight, new SuperimposeShader(_line!, _gray!, gridWidth, gridHeight, derived.LineThreshold));
|
||||
context.Barrier(_gray!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RecordRenderStage(
|
||||
in ComputeContext context,
|
||||
ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
PixelRect rect,
|
||||
in DerivedValues derived,
|
||||
in Parameters parameters)
|
||||
{
|
||||
context.For(rect.Width, rect.Height, new RenderShader(
|
||||
_colorA!, _line!, output,
|
||||
rect.X, rect.Y, rect.Width, rect.Height, _gridWidth, _gridHeight,
|
||||
derived.CellSize,
|
||||
UkiyoeSettings.ClampPaletteLevels(parameters.PaletteLevels),
|
||||
derived.ShiftPixels,
|
||||
Math.Clamp(parameters.Baren, 0f, 1f),
|
||||
Math.Clamp(parameters.Paper, 0f, 1f),
|
||||
derived.LineThreshold,
|
||||
Math.Clamp(parameters.LineStrength, 0f, 1f),
|
||||
parameters.LineColorR,
|
||||
parameters.LineColorG,
|
||||
parameters.LineColorB,
|
||||
parameters.Seed));
|
||||
return _host.RecordFullPipeline(source, output, _scratch, width, height, _gridWidth, _gridHeight, in derived, in parameters);
|
||||
}
|
||||
|
||||
private DerivedValues Derive(int width, int height, in Parameters parameters)
|
||||
@@ -328,22 +252,19 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
if (_gridWidth == gridWidth && _gridHeight == gridHeight)
|
||||
return;
|
||||
|
||||
DisposeGridBuffers();
|
||||
var gridLength = gridWidth * gridHeight;
|
||||
_colorIn = _device.AllocateReadWriteBuffer<Float4>(gridLength);
|
||||
_colorA = _device.AllocateReadWriteBuffer<Float4>(gridLength);
|
||||
_colorB = _device.AllocateReadWriteBuffer<Float4>(gridLength);
|
||||
_gray = _device.AllocateReadWriteBuffer<float>(gridLength);
|
||||
_gradientMagnitude = _device.AllocateReadWriteBuffer<float>(gridLength);
|
||||
_response = _device.AllocateReadWriteBuffer<float>(gridLength);
|
||||
_line = _device.AllocateReadWriteBuffer<float>(gridLength);
|
||||
_tangentA = _device.AllocateReadWriteBuffer<Float2>(gridLength);
|
||||
_tangentB = _device.AllocateReadWriteBuffer<Float2>(gridLength);
|
||||
if (!_host.TryEnsureGrid(
|
||||
new UkiyoeGridResources.Plan(
|
||||
gridLength, gridLength, gridLength, gridLength, gridLength, gridLength, gridLength, gridLength, gridLength),
|
||||
out _))
|
||||
throw new InvalidOperationException();
|
||||
|
||||
_cachedLitCount = 0;
|
||||
_cachedBoundsMinX = int.MaxValue;
|
||||
_cachedBoundsMinY = int.MaxValue;
|
||||
_cachedBoundsMaxX = int.MinValue;
|
||||
_cachedBoundsMaxY = int.MinValue;
|
||||
_structureKey = null;
|
||||
_gridWidth = gridWidth;
|
||||
_gridHeight = gridHeight;
|
||||
}
|
||||
@@ -361,45 +282,19 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
_packedHeight = height;
|
||||
}
|
||||
|
||||
private void DisposeGridBuffers()
|
||||
{
|
||||
_colorIn?.Dispose();
|
||||
_colorA?.Dispose();
|
||||
_colorB?.Dispose();
|
||||
_gray?.Dispose();
|
||||
_gradientMagnitude?.Dispose();
|
||||
_response?.Dispose();
|
||||
_line?.Dispose();
|
||||
_tangentA?.Dispose();
|
||||
_tangentB?.Dispose();
|
||||
_colorIn = null;
|
||||
_colorA = null;
|
||||
_colorB = null;
|
||||
_gray = null;
|
||||
_gradientMagnitude = null;
|
||||
_response = null;
|
||||
_line = null;
|
||||
_tangentA = null;
|
||||
_tangentB = null;
|
||||
_cachedLitCount = 0;
|
||||
_cachedBoundsMinX = int.MaxValue;
|
||||
_cachedBoundsMinY = int.MaxValue;
|
||||
_cachedBoundsMaxX = int.MinValue;
|
||||
_cachedBoundsMaxY = int.MinValue;
|
||||
_structureKey = null;
|
||||
_gridWidth = 0;
|
||||
_gridHeight = 0;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
DisposeGridBuffers();
|
||||
_host.Dispose();
|
||||
_host.WaitForDisposal();
|
||||
_packedSource?.Dispose();
|
||||
_packedOutput?.Dispose();
|
||||
_packedSource = null;
|
||||
_packedOutput = null;
|
||||
_packedWidth = 0;
|
||||
_packedHeight = 0;
|
||||
_structureKey = null;
|
||||
_gridWidth = 0;
|
||||
_gridHeight = 0;
|
||||
_scratchReadBack.Dispose();
|
||||
_scratch.Dispose();
|
||||
}
|
||||
@@ -417,7 +312,7 @@ internal sealed class UkiyoePipeline : IDisposable
|
||||
float LineDetail,
|
||||
float Flatten);
|
||||
|
||||
private readonly record struct DerivedValues(
|
||||
internal readonly record struct DerivedValues(
|
||||
float CellSize,
|
||||
int EtfIterations,
|
||||
int FdogIterations,
|
||||
|
||||
@@ -0,0 +1,264 @@
|
||||
using ComputeWeave;
|
||||
|
||||
namespace Ukiyoe;
|
||||
|
||||
[ComputeResourceGroup]
|
||||
internal sealed partial class UkiyoeGridResources
|
||||
{
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite)]
|
||||
internal ReadWriteBuffer<Float4> ColorIn { get; }
|
||||
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite)]
|
||||
internal ReadWriteBuffer<Float4> ColorA { get; }
|
||||
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite)]
|
||||
internal ReadWriteBuffer<Float4> ColorB { get; }
|
||||
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite)]
|
||||
internal ReadWriteBuffer<float> Gray { get; }
|
||||
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite)]
|
||||
internal ReadWriteBuffer<float> GradientMagnitude { get; }
|
||||
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite)]
|
||||
internal ReadWriteBuffer<float> Response { get; }
|
||||
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite)]
|
||||
internal ReadWriteBuffer<float> Line { get; }
|
||||
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite)]
|
||||
internal ReadWriteBuffer<Float2> TangentA { get; }
|
||||
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite)]
|
||||
internal ReadWriteBuffer<Float2> TangentB { get; }
|
||||
}
|
||||
|
||||
[ComputePipelineHost("_device", 1)]
|
||||
internal sealed partial class UkiyoePipelineHost
|
||||
{
|
||||
private readonly GraphicsDevice _device;
|
||||
|
||||
[ComputePipelineResource(ComputeResourceAccess.ReadWrite, ComputeResourceRecovery.Recompute)]
|
||||
private readonly ComputeResourceGroupSlot<UkiyoeGridResources> _grid = new();
|
||||
|
||||
[ComputePipeline]
|
||||
private void RecordFullPipeline(
|
||||
in ComputeContext context,
|
||||
[ComputeOwnedResource(nameof(_grid))] UkiyoeGridResources grid,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite)] ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite)] ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite)] ReadWriteBuffer<int> scratch,
|
||||
int width,
|
||||
int height,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
in UkiyoePipeline.DerivedValues derived,
|
||||
in UkiyoePipeline.Parameters parameters)
|
||||
{
|
||||
_ = _device;
|
||||
|
||||
RecordSilhouetteStage(in context, grid, source, 0, 0, width, height, gridWidth, gridHeight, in derived);
|
||||
RecordStructureStage(in context, grid, scratch, gridWidth, gridHeight, in derived);
|
||||
RecordRenderStage(in context, grid, output, new UkiyoePipeline.PixelRect(0, 0, width, height), gridWidth, gridHeight, in derived, in parameters);
|
||||
}
|
||||
|
||||
[ComputePipeline]
|
||||
private void RecordSilhouetteAndMaskHash(
|
||||
in ComputeContext context,
|
||||
[ComputeOwnedResource(nameof(_grid))] UkiyoeGridResources grid,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite)] ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite)] ReadWriteBuffer<int> scratch,
|
||||
int sourceOffsetX,
|
||||
int sourceOffsetY,
|
||||
int sourceWidth,
|
||||
int sourceHeight,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
in UkiyoePipeline.DerivedValues derived)
|
||||
{
|
||||
_ = _device;
|
||||
|
||||
RecordSilhouetteStage(in context, grid, source, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, gridWidth, gridHeight, in derived);
|
||||
RecordMaskHashStage(in context, grid, scratch, gridWidth, gridHeight);
|
||||
}
|
||||
|
||||
[ComputePipeline]
|
||||
[ComputeInterop]
|
||||
private void RecordSharedSilhouetteAndMaskHash(
|
||||
in ComputeContext context,
|
||||
[ComputeOwnedResource(nameof(_grid))] UkiyoeGridResources grid,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite, Sharing = ComputeResourceSharing.External)] ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite)] ReadWriteBuffer<int> scratch,
|
||||
int sourceOffsetX,
|
||||
int sourceOffsetY,
|
||||
int sourceWidth,
|
||||
int sourceHeight,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
in UkiyoePipeline.DerivedValues derived)
|
||||
{
|
||||
_ = _device;
|
||||
|
||||
RecordSilhouetteStage(in context, grid, source, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, gridWidth, gridHeight, in derived);
|
||||
RecordMaskHashStage(in context, grid, scratch, gridWidth, gridHeight);
|
||||
}
|
||||
|
||||
[ComputePipeline]
|
||||
[ComputeInterop]
|
||||
private void RecordSharedRender(
|
||||
in ComputeContext context,
|
||||
[ComputeOwnedResource(nameof(_grid))] UkiyoeGridResources grid,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite, Sharing = ComputeResourceSharing.External)] ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
in UkiyoePipeline.PixelRect rect,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
in UkiyoePipeline.DerivedValues derived,
|
||||
in UkiyoePipeline.Parameters parameters)
|
||||
{
|
||||
_ = _device;
|
||||
|
||||
RecordRenderStage(in context, grid, output, rect, gridWidth, gridHeight, in derived, in parameters);
|
||||
}
|
||||
|
||||
[ComputePipeline]
|
||||
private void RecordStructure(
|
||||
in ComputeContext context,
|
||||
[ComputeOwnedResource(nameof(_grid))] UkiyoeGridResources grid,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite)] ReadWriteBuffer<int> scratch,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
in UkiyoePipeline.DerivedValues derived)
|
||||
{
|
||||
_ = _device;
|
||||
|
||||
RecordStructureStage(in context, grid, scratch, gridWidth, gridHeight, in derived);
|
||||
}
|
||||
|
||||
[ComputePipeline]
|
||||
private void RecordRender(
|
||||
in ComputeContext context,
|
||||
[ComputeOwnedResource(nameof(_grid))] UkiyoeGridResources grid,
|
||||
[ComputeResource(ComputeResourceAccess.ReadWrite)] ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
in UkiyoePipeline.PixelRect rect,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
in UkiyoePipeline.DerivedValues derived,
|
||||
in UkiyoePipeline.Parameters parameters)
|
||||
{
|
||||
_ = _device;
|
||||
|
||||
RecordRenderStage(in context, grid, output, rect, gridWidth, gridHeight, in derived, in parameters);
|
||||
}
|
||||
|
||||
private static void RecordSilhouetteStage(
|
||||
in ComputeContext context,
|
||||
UkiyoeGridResources grid,
|
||||
ReadWriteTexture2D<Bgra32, Float4> source,
|
||||
int sourceOffsetX,
|
||||
int sourceOffsetY,
|
||||
int sourceWidth,
|
||||
int sourceHeight,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
in UkiyoePipeline.DerivedValues derived)
|
||||
{
|
||||
context.For(gridWidth, gridHeight, new SilhouetteShader(
|
||||
source, grid.ColorIn, grid.Gray, sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, gridWidth, gridHeight, derived.CellSize));
|
||||
context.Barrier(grid.ColorIn);
|
||||
context.Barrier(grid.Gray);
|
||||
}
|
||||
|
||||
private static void RecordMaskHashStage(
|
||||
in ComputeContext context,
|
||||
UkiyoeGridResources grid,
|
||||
ReadWriteBuffer<int> scratch,
|
||||
int gridWidth,
|
||||
int gridHeight)
|
||||
{
|
||||
context.For(1, new MaskHashResetShader(scratch));
|
||||
context.Barrier(scratch);
|
||||
context.For(gridWidth, gridHeight, new MaskHashShader(grid.ColorIn, scratch, gridWidth, gridHeight));
|
||||
context.Barrier(scratch);
|
||||
}
|
||||
|
||||
private static void RecordStructureStage(
|
||||
in ComputeContext context,
|
||||
UkiyoeGridResources grid,
|
||||
ReadWriteBuffer<int> scratch,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
in UkiyoePipeline.DerivedValues derived)
|
||||
{
|
||||
context.For(1, new InitScratchShader(scratch));
|
||||
context.Barrier(scratch);
|
||||
context.For(gridWidth, gridHeight, new GradientShader(grid.Gray, grid.TangentA, grid.GradientMagnitude, scratch, gridWidth, gridHeight));
|
||||
context.Barrier(grid.TangentA);
|
||||
context.Barrier(grid.GradientMagnitude);
|
||||
context.Barrier(scratch);
|
||||
|
||||
var tangentIn = grid.TangentA;
|
||||
var tangentOut = grid.TangentB;
|
||||
for (var iteration = 0; iteration < derived.EtfIterations; iteration++)
|
||||
{
|
||||
context.For(gridWidth, gridHeight, new EtfShader(tangentIn, tangentOut, grid.GradientMagnitude, scratch, gridWidth, gridHeight));
|
||||
context.Barrier(tangentOut);
|
||||
(tangentIn, tangentOut) = (tangentOut, tangentIn);
|
||||
}
|
||||
var tangent = tangentIn;
|
||||
|
||||
context.For(gridWidth, gridHeight, new CopyColorShader(grid.ColorIn, grid.ColorA, gridWidth, gridHeight));
|
||||
context.Barrier(grid.ColorA);
|
||||
var flattenDispatchWidth = (gridWidth + UkiyoeSettings.FlattenGroupDim - 1) & ~(UkiyoeSettings.FlattenGroupDim - 1);
|
||||
var flattenDispatchHeight = (gridHeight + UkiyoeSettings.FlattenGroupDim - 1) & ~(UkiyoeSettings.FlattenGroupDim - 1);
|
||||
var iterateIn = grid.ColorA;
|
||||
var iterateOut = grid.ColorB;
|
||||
for (var iteration = 0; iteration < derived.FlattenIterations; iteration++)
|
||||
{
|
||||
context.For(flattenDispatchWidth, flattenDispatchHeight, new FlattenShader(
|
||||
grid.ColorIn, iterateIn, iterateOut, grid.GradientMagnitude, scratch, gridWidth, gridHeight, derived.FlattenBeta));
|
||||
context.Barrier(iterateOut);
|
||||
(iterateIn, iterateOut) = (iterateOut, iterateIn);
|
||||
}
|
||||
|
||||
for (var iteration = 0; iteration < derived.FdogIterations; iteration++)
|
||||
{
|
||||
context.For(gridWidth, gridHeight, new DogShader(
|
||||
grid.Gray, tangent, grid.Response, gridWidth, gridHeight, derived.LineSigmaCells, derived.DogExtent));
|
||||
context.Barrier(grid.Response);
|
||||
context.For(gridWidth, gridHeight, new FlowAccumulateShader(
|
||||
grid.Response, tangent, grid.Line, gridWidth, gridHeight, derived.FlowSigma, derived.FlowExtent));
|
||||
context.Barrier(grid.Line);
|
||||
if (iteration < derived.FdogIterations - 1)
|
||||
{
|
||||
context.For(gridWidth, gridHeight, new SuperimposeShader(grid.Line, grid.Gray, gridWidth, gridHeight, derived.LineThreshold));
|
||||
context.Barrier(grid.Gray);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void RecordRenderStage(
|
||||
in ComputeContext context,
|
||||
UkiyoeGridResources grid,
|
||||
ReadWriteTexture2D<Bgra32, Float4> output,
|
||||
in UkiyoePipeline.PixelRect rect,
|
||||
int gridWidth,
|
||||
int gridHeight,
|
||||
in UkiyoePipeline.DerivedValues derived,
|
||||
in UkiyoePipeline.Parameters parameters)
|
||||
{
|
||||
context.For(rect.Width, rect.Height, new RenderShader(
|
||||
grid.ColorA, grid.Line, output,
|
||||
rect.X, rect.Y, rect.Width, rect.Height, gridWidth, gridHeight,
|
||||
derived.CellSize,
|
||||
UkiyoeSettings.ClampPaletteLevels(parameters.PaletteLevels),
|
||||
derived.ShiftPixels,
|
||||
Math.Clamp(parameters.Baren, 0f, 1f),
|
||||
Math.Clamp(parameters.Paper, 0f, 1f),
|
||||
derived.LineThreshold,
|
||||
Math.Clamp(parameters.LineStrength, 0f, 1f),
|
||||
parameters.LineColorR,
|
||||
parameters.LineColorG,
|
||||
parameters.LineColorB,
|
||||
parameters.Seed));
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,7 @@ internal static class UkiyoeSettings
|
||||
public const int ScratchBoundsMaxY = 5;
|
||||
public const int ScratchMaskHashSum = 6;
|
||||
public const int ScratchMaskHashMix = 7;
|
||||
public const int MaximumPendingSubmissions = 32;
|
||||
|
||||
public static QualitySettings GetQuality(UkiyoeQuality quality)
|
||||
=> quality switch
|
||||
|
||||
バイナリファイルは表示されません。
|
変更後 幅: | 高さ: | サイズ: 400 KiB |
新しいイシューから参照
ユーザーをブロックする