コミットを比較

2 コミット
作成者 SHA1 メッセージ 日付
routersys 75c089e82e バージョンをv1.0.1へ更新
リリース / build-and-release (workflow_dispatch) Actions がタグ v1.0.1 のコミットから作成しました
2026-07-18 22:25:00 +09:00
routersys 27550edb7b Lloyd緩和と描画のメモリアクセスを削減して高速化 2026-07-18 22:24:48 +09:00
5個のファイルの変更、143行の追加、341行の削除

検索条件に一致するファイルはありません

+25 -175
ファイルの表示
@@ -1,191 +1,41 @@
# v1.0.0 - 装飾モザイク for YMM4
# v1.0.1 - 装飾モザイク for YMM4
YukkuriMovieMaker4向けの装飾モザイクエフェクトプラグインの初回リリースです。
素材のエッジに沿う方向場を求め、Hausnerの装飾モザイク手法で四角タイルを敷き詰め、ローマ風モザイクとして描画します。
タイルの配置はシードから決定論的に決まり、敷設のパラメータでタイルが順に敷かれていく遷移を表現できます。
計算はComputeSharpの計算シェーダーがDirect3D 12で実行し、YMM4のDirect3D 11側とは共有テクスチャおよび共有フェンスで接続します。
8言語のリソース構成のUIを備えます。
装飾モザイクエフェクトプラグインの改善リリースです。
Lloyd緩和と描画のメモリアクセスとディスパッチを削減し、タイルの配置と描画結果を一切変えずに、構造の再計算とフレームの描画を高速化しました。
出力は前バージョンと全画素で一致します。
---
## 新機能
## 改善
### 1. 装飾モザイクの計算パイプライン
### 1. サイト状態のFloat4への集約
`DecorativeMosaicPipeline`は、解析、配置、描画の3段階の計算シェーダーを`ComputeContext`へ記録して実行します。計算格子のバッファーは計算領域の大きさと品質とタイルの間隔に応じて確保し、サイズが変わらないフレームでは再利用します。処理の流れは次のとおりです。
タイルの位置、方向場の角度、細部による縮小率を、4本の独立したバッファーではなく1本の`Float4`バッファーへまとめました。ボロノイのジャンプフラッドと描画の候補評価は、候補1つあたり4回のバッファー読み取りが1回のベクトル読み取りになります。角度から余弦と正弦を求める式は変更していないため、距離の値は前バージョンと同一です。
1. `AnalyzeShader`が、各格子セルに対応する画素のアルファ値と輝度を調べ、しきい値`0.05`を超えるセルを素材の範囲として記録します。
2. `EdgeSeedShader`が、素材の範囲の境界セルと、輝度の勾配がしきい値を超える内部のセルをエッジとして置き、`EdgeJumpFloodPassShader`が格子解像度のジャンプフラッドで各セルから最も近いエッジを求めます。
3. `DirectionShader`が、最近傍のエッジへの変位に直交する接線方向から方向場を作り、エッジからの距離とあわせて記録します。
4. `SpawnShader`が、セル位置とシードのハッシュによる棄却サンプリングでタイルの初期位置を置きます。
5. `SiteSeedShader`と`SiteJumpFloodPassShader`と`CentroidAccumulateShader`と`SiteMoveShader`が、Lloyd緩和の1反復を実行し、品質が定める回数だけ記録を繰り返します。
6. `RenderShader`が、画素ごとに所属するボロノイ領域のタイルを求め、タイル、目地、陰影、色むらを計算して出力します。
- 候補評価はセルあたり最大9候補、パスあたり全格子、反復あたり約10パスで実行されるため、削減される読み取り回数は構造再計算1回あたり数億回に達します。
- 併せて重心の参照点を移動の時点で1回だけ計算して`Float2`バッファーへ保持し、重心集計の全セルで行っていた参照点の再計算を取り除きました。参照点の式は変更していません。
| シェーダー | 役割 |
|---|---|
| `AnalyzeShader` | 画素のアルファ値と輝度から素材の範囲を求める |
| `MaskHashShader` | 素材の範囲と輝度のハッシュを集計する |
| `EdgeSeedShader` | 輪郭と輝度エッジのセルを置く |
| `EdgeJumpFloodPassShader` | 最近傍のエッジを求める |
| `DirectionShader` | 方向場とエッジからの距離を求める |
| `SpawnShader` | タイルの初期位置を置く |
| `SiteSeedShader` / `SiteJumpFloodPassShader` | 方向つきマンハッタン計量のボロノイ図を求める |
| `CentroidAccumulateShader` | ボロノイ領域の重心を集計する |
| `SiteMoveShader` | タイルを重心へ移動する |
| `RenderShader` | タイルと目地を描画する |
### 2. 同一候補の重複評価の省略
### 2. Hausner手法によるタイル配置
ジャンプフラッドと描画の候補走査で、現在の最良候補と同じサイトを再評価せず読み飛ばすようにしました。同じサイトの距離は同じ値になり、狭義の比較では最良を置き換えないため、読み飛ばしても選択結果は変わりません。ボロノイ領域の内部では9候補の大半が同一サイトになるため、後半のパスほど距離計算が減ります。
配置の計算は、Hausnerの論文「Simulating Decorative Mosaics」(SIGGRAPH 2001)に基づきます。方向場は、エッジからのユークリッド距離場の勾配に直交する接線方向です。タイルの位置は、主軸を方向場へ局所回転させたマンハッタン計量`|u|/γ + |v|·γ`の重心ボロノイ図をLloyd反復で解いて求めます。マンハッタン計量の重心ボロノイ図は局所的に正方格子を作るため、曲がる格子に四角タイルが密に並びます。
### 3. ディスパッチとバリアの削減
1反復の更新は次の順で行います。
- 重心バッファーの零初期化を、毎反復3回の全格子ディスパッチから、発生と移動のシェーダーが自分の添字を読み取り後に零へ戻す形へ畳み込みました。集計の開始時に全ての和と件数が0である不変条件は変わりません。
- ボロノイの最終パスがサイトマップへ直接書き込むようにし、毎反復のコピーのディスパッチを取り除きました。書き込まれる値は同一です。
- ボロノイ: 各タイルの位置を種としてジャンプフラッドを実行し、各セルが最も近いタイルを求めます。距離は方向つきマンハッタン計量で評価し、細部による縮小率で割ります。論文のz-bufferによるボロノイ計算は、同一機能のジャンプフラッド法へ置き換えています。
- 重心: 素材の範囲のセルを、所属するタイルごとに固定小数点の整数`InterlockedAdd`で集計します。反復の大部分では、エッジからの距離が`0.35×間隔`未満のセルを集計から除外し、タイルをエッジから遠ざけます。最後の3反復では除外を解いて隙間を詰めます。これは論文6節のエッジ回避と同じ手順です。
- 移動: 各タイルを自身の領域の重心へ移動し、移動先の方向場と縮小率を反映します。領域を持たないタイルは削除します。
### 4. 性能
整列のパラメータは、方向場の角度を4倍角の空間で無回転と合成する度合いです。細長さのパラメータは計量の異方性`γ`を1〜2.5へ割り当て、細部のパラメータはエッジ近傍のタイルを最小0.5倍まで縮小します。乱数は使用せず、すべての値が入力から決定論的に決まります。
1280×720の素材、タイル間隔14画素での実測値です。タイルの配置と描画画素数はすべての条件で前バージョンと一致します。
### 3. 可視範囲への出力矩形の最小化
| 項目 | v1.0.0 | v1.0.1 |
|---|---:|---:|
| 標準の構造再計算を含むフレーム | 72ms | 28ms |
| 高品質の構造再計算を含むフレーム | 192ms | 85ms |
| 最高品質の構造再計算を含むフレーム | 473ms | 195ms |
| 高品質の構造再計算の単発 | 177ms | 66ms |
| キャッシュ後のフレーム | 11.9ms | 5.5ms |
配置の完了後、タイルの位置と敷設順位の記録をCPUへ読み戻します。読み戻す量は格子3面の値だけで、画素は読み戻しません。敷設順位ごとの境界を累積した配列を1回の走査で構築するため、敷設が定める可視タイルのバウンディングボックスはフレームごとに一定時間で求まります。
### 5. 等価性の検証
- 矩形にはタイルの半径分の余白を加え、4画素境界へそろえます。
- 出力テクスチャは矩形の大きさで確保し、Direct2Dの`Crop`と`AffineTransform2D`で元の位置へ合成します。
- モザイクは素材の不透明な範囲の内側だけに描画されるため、計算領域は素材の範囲そのままで余白を持ちません。
### 4. 構造キャッシュ
タイルの配置を決める入力が変わらないフレームでは、配置の計算を再利用します。
- 素材の範囲と輝度は、解析の計算後にセル位置と量子化した輝度のハッシュの総和とXORの2値へ集約し、8個の整数の読み戻しで前フレームと比較します。
- ハッシュ、計算領域の大きさ、品質、シード、大きさ、細長さ、整列、エッジ検出、細部が一致する場合は、配置段階と記録の読み戻しを実行しません。
- 構造が同じで、敷設、目地幅、乱れ、立体感、色むら、目地色、目地濃度、出力矩形も変わらないフレームでは、描画段階も実行せず、前フレームの出力テクスチャを使用します。
### 5. Direct3D 11・Direct3D 12相互運用
`DecorativeMosaicGpuInterop`は、YMM4のDirect3D 11・Direct2D側と、ComputeSharpのDirect3D 12側を接続します。ComputeSharpの`GraphicsDevice`は、YMM4が使うDXGIアダプターのLUIDと一致するものを選びます。
入力と出力は、ComputeSharpで確保した共有テクスチャをDirect3D 11のテクスチャとして開き、Direct2Dのビットマップとして扱います。入力のテクスチャは素材の大きさで確保し、出力のテクスチャは可視範囲の矩形を収める容量で確保して拡大時だけ作り直します。両デバイスの同期は、Direct3D 12のフェンスを共有フェンスとしてDirect3D 11側で開いて行います。
`BeginCompute`は、Direct3D 11のコマンドを送出したうえでDirect3D 12側を待機させ、`EndCompute`は、Direct3D 12側の完了をDirect3D 11側で待ちます。
Direct3D 12デバイスの取得や共有リソースの作成に失敗した場合は、`TryCreate`が`null`を返し、エフェクトを適用せず入力映像を表示します。
### 6. カスタムシェーダーによる合成
`DecorativeMosaicCustomEffect`は、`[CustomEffect(2)]`の2入力エフェクトです。入力0は元映像、入力1は描画したモザイクです。ピクセルシェーダー`DecorativeMosaic.hlsl`の`main`は、`amount`が0以下のとき元映像をそのまま返し、そうでないときはモザイクのRGBをアルファでクランプし、`mosaic + source * (1 - mosaic.a)`のアルファ合成でモザイクを元映像の上へ重ねます。タイルの色は描画段階でモザイクの画素へ焼き込むため、合成は単純な重ね合わせです。
定数バッファーは`Amount`と3つの詰め物で16バイトです。`MapInputRectsToOutputRect`は2つの入力矩形の和集合を出力矩形とします。モザイクは素材の内側だけに描画されるため、出力範囲は素材より広がりません。
シェーダーリソース: `pack://application:,,,/DecorativeMosaic;component/Shaders/DecorativeMosaic.cso`(ps_5_0、`ShaderResourceUri.Get`が生成)
### 7. エフェクト定義とパラメータ
`DecorativeMosaicEffect`は、YMM4の映像エフェクトとして宣言されます。
`[VideoEffect]`属性は以下のパラメーターで宣言されます。
- 表示名: `Texts.DecorativeMosaic`(ローカライズキー、日本語では「装飾モザイク」)
- カテゴリー: `VideoEffectCategories.Decoration`・`VideoEffectCategories.Animation`
- 検索タグ: `TagMosaic`・`TagTile`・`TagRoman`
- `IsAviUtlSupported = false`によりAviUtl向けEXO出力は非対応
- `ResourceType = typeof(Texts)`でローカライズリソースを指定
公開プロパティは以下のとおりです。基本項目は「基本」グループ、タイル項目は「タイル」グループ、方向項目は「方向」グループ、描画項目は「描画」グループに属します。
| プロパティ | 型 | デフォルト | 内部範囲 | アニメーション |
|---|---|---|---|---|
| `Amount` | `Animation` | 100 | 0〜100 | あり |
| `Laying` | `Animation` | 100 | 0〜100 | あり |
| `Quality` | `DecorativeMosaicQuality` | `High` | — | なし |
| `TileSize` | `Animation` | 14 | 2〜200 | あり |
| `Grout` | `Animation` | 30 | 0〜100 | あり |
| `Aspect` | `Animation` | 0 | 0〜100 | あり |
| `Irregularity` | `Animation` | 25 | 0〜100 | あり |
| `Detail` | `Animation` | 0 | 0〜100 | あり |
| `Seed` | `int` | 0 | 0〜int.MaxValue | なし |
| `Align` | `Animation` | 100 | 0〜100 | あり |
| `EdgeDetect` | `Animation` | 50 | 0〜100 | あり |
| `Bevel` | `Animation` | 35 | 0〜100 | あり |
| `ColorVariation` | `Animation` | 20 | 0〜100 | あり |
| `GroutColor` | `Color` | #FFD1C8BA | — | なし |
| `GroutOpacity` | `Animation` | 100 | 0〜100 | あり |
`GetAnimatables`は`Amount`・`Laying`・`TileSize`・`Grout`・`Aspect`・`Irregularity`・`Detail`・`Align`・`EdgeDetect`・`Bevel`・`ColorVariation`・`GroutOpacity`を返します。`Seed`は負値を代入すると0へ丸めます。
`CreateExoVideoFilters`は空のシーケンスを返します(EXO非対応)。`CreateVideoEffect`は映像処理用のインスタンスを生成します。エフェクトを最初に生成したときに、更新確認を一度だけ開始します。
### 8. フレームごとの更新
各フレームでYMM4の`EffectDescription`からフレーム位置、アイテム長、FPSを取得し、アニメーション値を評価します。値をパイプラインが前提とする範囲へ制限してから転送します。
| パラメータ | 変換 |
|---|---|
| `Amount` | `value / 100` をカスタムシェーダーの`Amount`へ |
| `Laying` | `value / 100` を0〜1へクランプ |
| `TileSize` | 値をそのまま2〜200画素へクランプ |
| `Grout` | `value / 100` を0〜1へクランプ |
| `Aspect` | `value / 100` を0〜1へクランプし、計量の異方性1〜2.5へ |
| `Irregularity` | `value / 100` を0〜1へクランプ |
| `Detail` | `value / 100` を0〜1へクランプ |
| `Align` | `value / 100` を0〜1へクランプ |
| `EdgeDetect` | `value / 100` を0〜1へクランプし、輝度勾配のしきい値へ |
| `Bevel` | `value / 100` を0〜1へクランプ |
| `ColorVariation` | `value / 100` を0〜1へクランプ |
| `GroutColor` | RGB各成分を0〜1へ |
| `GroutOpacity` | `value / 100` を0〜1へクランプ |
| `Seed` | 0以上へクランプ |
強さが0以下のとき、または敷設が0以下のときは、モザイクを描画せず入力映像をそのまま出力します。入力の範囲が有限でない場合や、長辺が8192画素を超える場合も、入力映像を表示します。
### 9. 品質設定
品質は、計算格子の解像度とLloyd反復の回数をまとめて切り替えます。
| 品質 | 1タイルあたりのセル数 | Lloyd反復 | 格子解像度の上限 |
|---|---:|---:|---:|
| 標準 | 5 | 10回 | 512 |
| 高品質 | 6 | 18回 | 768 |
| 最高品質 | 8 | 26回 | 1024 |
格子解像度の上限は計算領域の長辺のセル数です。セルの大きさはタイルの間隔と上限の両方から決まり、短辺のセル数は計算領域の縦横比に合わせ、最小4セルとします。
### 10. ローカライズ
`Texts`クラスは`[AutoGenLocalizer]`属性を持つ`partial`クラスとして宣言されます。
`YukkuriMovieMaker.Generator`のソースジェネレーターが`Texts.csv`を処理し、各ロケールのリソースファイルを自動生成します。
対応リソース: 日本語(`ja-jp`)・英語(`en-us`)・中国語簡体字(`zh-cn`)・中国語繁体字(`zh-tw`)・韓国語(`ko-kr`)・スペイン語(`es-es`)・アラビア語(`ar-sa`)・インドネシア語(`id-id`)
主なローカライズキーは以下のとおりです。
| キー | ja-jp |
|---|---|
| `DecorativeMosaic` | 装飾モザイク |
| `BasicGroup` | 基本 |
| `TileGroup` | タイル |
| `FlowGroup` | 方向 |
| `AppearanceGroup` | 描画 |
| `Amount` | 強さ |
| `Laying` | 敷設 |
| `Quality` | 品質 |
| `TileSize` | 大きさ |
| `Grout` | 目地幅 |
| `Aspect` | 細長さ |
| `Irregularity` | 乱れ |
| `Detail` | 細部 |
| `Seed` | シード |
| `Align` | 整列 |
| `EdgeDetect` | エッジ検出 |
| `Bevel` | 立体感 |
| `ColorVariation` | 色むら |
| `GroutColor` | 目地色 |
| `GroutOpacity` | 目地濃度 |
| `QualityBalanced` | 標準 |
| `QualityHigh` | 高品質 |
| `QualityUltra` | 最高品質 |
| `TagMosaic` | モザイク |
| `TagTile` | タイル |
| `TagRoman` | ローマ風 |
| `UpdateAvailableMessage` | 新しいバージョン {0} が公開されています。 |
検証ハーネスの`--golden`引数で、品質2条件、シード、細長さ最大、整列0、目地最大、細部最大、部分敷設の8条件について、前バージョンのソースから同一環境でビルドした出力と、全画素のSHA-256が一致することを確認しました。テストは41件すべて成功し、ビルドは警告0です。ハーネスが出力する全画像も前バージョンと全画素で一致します。
+1 -1
ファイルの表示
@@ -1,5 +1,5 @@
========================================
装飾モザイク for YMM4 v1.0.0
装飾モザイク for YMM4 v1.0.1
========================================
YukkuriMovieMaker4上で動作する、素材のエッジに沿う方向場を求め、
Hausnerの装飾モザイク手法で四角タイルを敷き詰めてローマ風モザイクとして
+1 -1
ファイルの表示
@@ -5,7 +5,7 @@
<UseWPF>true</UseWPF>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Platforms>AnyCPU;x64</Platforms>
<PlatformTarget>x64</PlatformTarget>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
+72 -96
ファイルの表示
@@ -22,26 +22,6 @@ internal readonly partial struct FillIntShader(
}
}
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct CopyIntShader(
ReadWriteBuffer<int> input,
ReadWriteBuffer<int> output,
int length) : IComputeShader
{
private readonly ReadWriteBuffer<int> input = input;
private readonly ReadWriteBuffer<int> output = output;
private readonly int length = length;
public void Execute()
{
var index = ThreadIds.X;
if (index >= length)
return;
output[index] = input[index];
}
}
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct InitScratchShader(
@@ -346,11 +326,12 @@ internal readonly partial struct SpawnShader(
ReadWriteBuffer<int> mask,
ReadWriteBuffer<float> theta,
ReadWriteBuffer<float> edgeDistance,
ReadWriteBuffer<float> sitePosX,
ReadWriteBuffer<float> sitePosY,
ReadWriteBuffer<float> siteTheta,
ReadWriteBuffer<float> siteSize,
ReadWriteBuffer<Float4> site,
ReadWriteBuffer<Float2> siteReference,
ReadWriteBuffer<int> siteRank,
ReadWriteBuffer<int> sumX,
ReadWriteBuffer<int> sumY,
ReadWriteBuffer<int> count,
ReadWriteBuffer<int> scratch,
int gridWidth,
int gridHeight,
@@ -362,11 +343,12 @@ internal readonly partial struct SpawnShader(
private readonly ReadWriteBuffer<int> mask = mask;
private readonly ReadWriteBuffer<float> theta = theta;
private readonly ReadWriteBuffer<float> edgeDistance = edgeDistance;
private readonly ReadWriteBuffer<float> sitePosX = sitePosX;
private readonly ReadWriteBuffer<float> sitePosY = sitePosY;
private readonly ReadWriteBuffer<float> siteTheta = siteTheta;
private readonly ReadWriteBuffer<float> siteSize = siteSize;
private readonly ReadWriteBuffer<Float4> site = site;
private readonly ReadWriteBuffer<Float2> siteReference = siteReference;
private readonly ReadWriteBuffer<int> siteRank = siteRank;
private readonly ReadWriteBuffer<int> sumX = sumX;
private readonly ReadWriteBuffer<int> sumY = sumY;
private readonly ReadWriteBuffer<int> count = count;
private readonly ReadWriteBuffer<int> scratch = scratch;
private readonly int gridWidth = gridWidth;
private readonly int gridHeight = gridHeight;
@@ -383,6 +365,9 @@ internal readonly partial struct SpawnShader(
return;
var index = gy * gridWidth + gx;
sumX[index] = 0;
sumY[index] = 0;
count[index] = 0;
if (mask[index] != 1)
{
siteRank[index] = -1;
@@ -400,10 +385,10 @@ internal readonly partial struct SpawnShader(
}
var center = DecorativeMosaicShaderMath.CellCenter(gx, gy, cellSize);
sitePosX[index] = center.X + (DecorativeMosaicShaderMath.Hash01(basis ^ 0xC2B2AE35u) - 0.5f) * cellSize;
sitePosY[index] = center.Y + (DecorativeMosaicShaderMath.Hash01(basis * 0x85EBCA6Bu + 0x9E3779B9u) - 0.5f) * cellSize;
siteTheta[index] = theta[index];
siteSize[index] = sizeFactor;
var positionX = center.X + (DecorativeMosaicShaderMath.Hash01(basis ^ 0xC2B2AE35u) - 0.5f) * cellSize;
var positionY = center.Y + (DecorativeMosaicShaderMath.Hash01(basis * 0x85EBCA6Bu + 0x9E3779B9u) - 0.5f) * cellSize;
site[index] = new Float4(positionX, positionY, theta[index], sizeFactor);
siteReference[index] = DecorativeMosaicShaderMath.OwnerReference(positionX, positionY, cellSize, gridWidth, gridHeight);
siteRank[index] = Hlsl.Min(
(int)(DecorativeMosaicShaderMath.Hash01(basis + 0x27D4EB2Fu) * DecorativeMosaicSettings.LayingSteps),
DecorativeMosaicSettings.LayingSteps - 1);
@@ -415,16 +400,14 @@ internal readonly partial struct SpawnShader(
[GeneratedComputeShaderDescriptor]
internal readonly partial struct SiteSeedShader(
ReadWriteBuffer<int> siteRank,
ReadWriteBuffer<float> sitePosX,
ReadWriteBuffer<float> sitePosY,
ReadWriteBuffer<Float4> site,
ReadWriteBuffer<int> jumpFlood,
int gridWidth,
int gridHeight,
float cellSize) : IComputeShader
{
private readonly ReadWriteBuffer<int> siteRank = siteRank;
private readonly ReadWriteBuffer<float> sitePosX = sitePosX;
private readonly ReadWriteBuffer<float> sitePosY = sitePosY;
private readonly ReadWriteBuffer<Float4> site = site;
private readonly ReadWriteBuffer<int> jumpFlood = jumpFlood;
private readonly int gridWidth = gridWidth;
private readonly int gridHeight = gridHeight;
@@ -441,8 +424,9 @@ internal readonly partial struct SiteSeedShader(
if (siteRank[index] < 0)
return;
var cx = Hlsl.Clamp((int)(sitePosX[index] / cellSize), 0, gridWidth - 1);
var cy = Hlsl.Clamp((int)(sitePosY[index] / cellSize), 0, gridHeight - 1);
var packed = site[index];
var cx = Hlsl.Clamp((int)(packed.X / cellSize), 0, gridWidth - 1);
var cy = Hlsl.Clamp((int)(packed.Y / cellSize), 0, gridHeight - 1);
Hlsl.InterlockedMin(ref jumpFlood[cy * gridWidth + cx], index);
}
}
@@ -452,10 +436,7 @@ internal readonly partial struct SiteSeedShader(
internal readonly partial struct SiteJumpFloodPassShader(
ReadWriteBuffer<int> input,
ReadWriteBuffer<int> output,
ReadWriteBuffer<float> sitePosX,
ReadWriteBuffer<float> sitePosY,
ReadWriteBuffer<float> siteTheta,
ReadWriteBuffer<float> siteSize,
ReadWriteBuffer<Float4> site,
int gridWidth,
int gridHeight,
int stepSize,
@@ -464,26 +445,24 @@ internal readonly partial struct SiteJumpFloodPassShader(
{
private readonly ReadWriteBuffer<int> input = input;
private readonly ReadWriteBuffer<int> output = output;
private readonly ReadWriteBuffer<float> sitePosX = sitePosX;
private readonly ReadWriteBuffer<float> sitePosY = sitePosY;
private readonly ReadWriteBuffer<float> siteTheta = siteTheta;
private readonly ReadWriteBuffer<float> siteSize = siteSize;
private readonly ReadWriteBuffer<Float4> site = site;
private readonly int gridWidth = gridWidth;
private readonly int gridHeight = gridHeight;
private readonly int stepSize = stepSize;
private readonly float cellSize = cellSize;
private readonly float gamma = gamma;
private float Distance(Float2 position, int site)
private float Distance(Float2 position, int candidate)
{
var dx = position.X - sitePosX[site];
var dy = position.Y - sitePosY[site];
var angle = siteTheta[site];
var packed = site[candidate];
var dx = position.X - packed.X;
var dy = position.Y - packed.Y;
var angle = packed.Z;
var c = Hlsl.Cos(angle);
var s = Hlsl.Sin(angle);
var u = c * dx + s * dy;
var v = -s * dx + c * dy;
return (Hlsl.Abs(u) / gamma + Hlsl.Abs(v) * gamma) / Hlsl.Max(siteSize[site], 0.1f);
return (Hlsl.Abs(u) / gamma + Hlsl.Abs(v) * gamma) / Hlsl.Max(packed.W, 0.1f);
}
public void Execute()
@@ -505,7 +484,7 @@ internal readonly partial struct SiteJumpFloodPassShader(
if (sx < 0 || sx >= gridWidth || sy < 0 || sy >= gridHeight)
continue;
var candidate = input[sy * gridWidth + sx];
if (candidate == DecorativeMosaicSettings.SiteSentinel)
if (candidate == DecorativeMosaicSettings.SiteSentinel || candidate == best)
continue;
var distance = Distance(position, candidate);
if (distance < bestDistance)
@@ -525,8 +504,7 @@ internal readonly partial struct CentroidAccumulateShader(
ReadWriteBuffer<int> siteMap,
ReadWriteBuffer<int> mask,
ReadWriteBuffer<float> edgeDistance,
ReadWriteBuffer<float> sitePosX,
ReadWriteBuffer<float> sitePosY,
ReadWriteBuffer<Float2> siteReference,
ReadWriteBuffer<int> sumX,
ReadWriteBuffer<int> sumY,
ReadWriteBuffer<int> count,
@@ -539,8 +517,7 @@ internal readonly partial struct CentroidAccumulateShader(
private readonly ReadWriteBuffer<int> siteMap = siteMap;
private readonly ReadWriteBuffer<int> mask = mask;
private readonly ReadWriteBuffer<float> edgeDistance = edgeDistance;
private readonly ReadWriteBuffer<float> sitePosX = sitePosX;
private readonly ReadWriteBuffer<float> sitePosY = sitePosY;
private readonly ReadWriteBuffer<Float2> siteReference = siteReference;
private readonly ReadWriteBuffer<int> sumX = sumX;
private readonly ReadWriteBuffer<int> sumY = sumY;
private readonly ReadWriteBuffer<int> count = count;
@@ -566,11 +543,10 @@ internal readonly partial struct CentroidAccumulateShader(
if (owner == DecorativeMosaicSettings.SiteSentinel)
return;
var ownerCellX = Hlsl.Clamp((int)(sitePosX[owner] / cellSize), 0, gridWidth - 1);
var ownerCellY = Hlsl.Clamp((int)(sitePosY[owner] / cellSize), 0, gridHeight - 1);
var reference = siteReference[owner];
var center = DecorativeMosaicShaderMath.CellCenter(gx, gy, cellSize);
var offsetX = center.X - (ownerCellX + 0.5f) * cellSize;
var offsetY = center.Y - (ownerCellY + 0.5f) * cellSize;
var offsetX = center.X - reference.X;
var offsetY = center.Y - reference.Y;
if (Hlsl.Abs(offsetX) > cutoff || Hlsl.Abs(offsetY) > cutoff)
return;
@@ -584,10 +560,8 @@ internal readonly partial struct CentroidAccumulateShader(
[GeneratedComputeShaderDescriptor]
internal readonly partial struct SiteMoveShader(
ReadWriteBuffer<int> siteRank,
ReadWriteBuffer<float> sitePosX,
ReadWriteBuffer<float> sitePosY,
ReadWriteBuffer<float> siteTheta,
ReadWriteBuffer<float> siteSize,
ReadWriteBuffer<Float4> site,
ReadWriteBuffer<Float2> siteReference,
ReadWriteBuffer<int> sumX,
ReadWriteBuffer<int> sumY,
ReadWriteBuffer<int> count,
@@ -601,10 +575,8 @@ internal readonly partial struct SiteMoveShader(
float detail) : IComputeShader
{
private readonly ReadWriteBuffer<int> siteRank = siteRank;
private readonly ReadWriteBuffer<float> sitePosX = sitePosX;
private readonly ReadWriteBuffer<float> sitePosY = sitePosY;
private readonly ReadWriteBuffer<float> siteTheta = siteTheta;
private readonly ReadWriteBuffer<float> siteSize = siteSize;
private readonly ReadWriteBuffer<Float4> site = site;
private readonly ReadWriteBuffer<Float2> siteReference = siteReference;
private readonly ReadWriteBuffer<int> sumX = sumX;
private readonly ReadWriteBuffer<int> sumY = sumY;
private readonly ReadWriteBuffer<int> count = count;
@@ -625,10 +597,15 @@ internal readonly partial struct SiteMoveShader(
return;
var index = gy * gridWidth + gx;
var cells = count[index];
var accumulatedX = sumX[index];
var accumulatedY = sumY[index];
sumX[index] = 0;
sumY[index] = 0;
count[index] = 0;
if (siteRank[index] < 0)
return;
var cells = count[index];
if (cells == 0)
{
siteRank[index] = -1;
@@ -636,19 +613,16 @@ internal readonly partial struct SiteMoveShader(
return;
}
var ownerCellX = Hlsl.Clamp((int)(sitePosX[index] / cellSize), 0, gridWidth - 1);
var ownerCellY = Hlsl.Clamp((int)(sitePosY[index] / cellSize), 0, gridHeight - 1);
var reference = siteReference[index];
var scale = 1f / (DecorativeMosaicSettings.FixedPointScale * cells);
var newX = Hlsl.Clamp((ownerCellX + 0.5f) * cellSize + sumX[index] * scale, 0f, gridWidth * cellSize - 0.001f);
var newY = Hlsl.Clamp((ownerCellY + 0.5f) * cellSize + sumY[index] * scale, 0f, gridHeight * cellSize - 0.001f);
sitePosX[index] = newX;
sitePosY[index] = newY;
var newX = Hlsl.Clamp(reference.X + accumulatedX * scale, 0f, gridWidth * cellSize - 0.001f);
var newY = Hlsl.Clamp(reference.Y + accumulatedY * scale, 0f, gridHeight * cellSize - 0.001f);
var cellX = Hlsl.Clamp((int)(newX / cellSize), 0, gridWidth - 1);
var cellY = Hlsl.Clamp((int)(newY / cellSize), 0, gridHeight - 1);
var cell = cellY * gridWidth + cellX;
siteTheta[index] = theta[cell];
siteSize[index] = DecorativeMosaicShaderMath.SizeFactor(edgeDistance[cell], spacing, detail);
site[index] = new Float4(newX, newY, theta[cell], DecorativeMosaicShaderMath.SizeFactor(edgeDistance[cell], spacing, detail));
siteReference[index] = DecorativeMosaicShaderMath.OwnerReference(newX, newY, cellSize, gridWidth, gridHeight);
}
}
@@ -656,10 +630,7 @@ internal readonly partial struct SiteMoveShader(
[GeneratedComputeShaderDescriptor]
internal readonly partial struct RenderShader(
ReadWriteBuffer<int> siteMap,
ReadWriteBuffer<float> sitePosX,
ReadWriteBuffer<float> sitePosY,
ReadWriteBuffer<float> siteTheta,
ReadWriteBuffer<float> siteSize,
ReadWriteBuffer<Float4> site,
ReadWriteBuffer<int> siteRank,
ReadWriteTexture2D<Bgra32, Float4> source,
ReadWriteTexture2D<Bgra32, Float4> output,
@@ -685,10 +656,7 @@ internal readonly partial struct RenderShader(
float groutOpacity) : IComputeShader
{
private readonly ReadWriteBuffer<int> siteMap = siteMap;
private readonly ReadWriteBuffer<float> sitePosX = sitePosX;
private readonly ReadWriteBuffer<float> sitePosY = sitePosY;
private readonly ReadWriteBuffer<float> siteTheta = siteTheta;
private readonly ReadWriteBuffer<float> siteSize = siteSize;
private readonly ReadWriteBuffer<Float4> site = site;
private readonly ReadWriteBuffer<int> siteRank = siteRank;
private readonly ReadWriteTexture2D<Bgra32, Float4> source = source;
private readonly ReadWriteTexture2D<Bgra32, Float4> output = output;
@@ -744,16 +712,17 @@ internal readonly partial struct RenderShader(
if (sx < 0 || sx >= gridWidth || sy < 0 || sy >= gridHeight)
continue;
var candidate = siteMap[sy * gridWidth + sx];
if (candidate == DecorativeMosaicSettings.SiteSentinel)
if (candidate == DecorativeMosaicSettings.SiteSentinel || candidate == best)
continue;
var cdx = pxc - sitePosX[candidate];
var cdy = pyc - sitePosY[candidate];
var angle = siteTheta[candidate];
var packed = site[candidate];
var cdx = pxc - packed.X;
var cdy = pyc - packed.Y;
var angle = packed.Z;
var cc = Hlsl.Cos(angle);
var cs = Hlsl.Sin(angle);
var cu = cc * cdx + cs * cdy;
var cv = -cs * cdx + cc * cdy;
var distance = (Hlsl.Abs(cu) / gamma + Hlsl.Abs(cv) * gamma) / Hlsl.Max(siteSize[candidate], 0.1f);
var distance = (Hlsl.Abs(cu) / gamma + Hlsl.Abs(cv) * gamma) / Hlsl.Max(packed.W, 0.1f);
if (distance < bestDistance)
{
bestDistance = distance;
@@ -773,23 +742,24 @@ internal readonly partial struct RenderShader(
var rc = Hlsl.Cos(rotation);
var rs = Hlsl.Abs(Hlsl.Sin(rotation));
var shrink = 1f / (rc + rs);
var bestSite = site[best];
var sizeJitter = 1f - DecorativeMosaicSettings.SizeJitterFactor * irregularity * DecorativeMosaicShaderMath.Hash01(basis ^ 0x85EBCA6Bu);
var factor = siteSize[best] * shrink * sizeJitter;
var factor = bestSite.W * shrink * sizeJitter;
var halfU = gamma * halfBase * factor;
var halfV = halfBase / gamma * factor;
var tileAngle = siteTheta[best] + rotation;
var tileAngle = bestSite.Z + rotation;
var tc2 = Hlsl.Cos(tileAngle);
var ts2 = Hlsl.Sin(tileAngle);
var dx2 = pxc - sitePosX[best];
var dy2 = pyc - sitePosY[best];
var dx2 = pxc - bestSite.X;
var dy2 = pyc - bestSite.Y;
var u = tc2 * dx2 + ts2 * dy2;
var v = -ts2 * dx2 + tc2 * dy2;
if (Hlsl.Abs(u) <= halfU && Hlsl.Abs(v) <= halfV)
{
var tileX = Hlsl.Clamp((int)sitePosX[best], 0, sourceWidth - 1);
var tileY = Hlsl.Clamp((int)sitePosY[best], 0, sourceHeight - 1);
var tileX = Hlsl.Clamp((int)bestSite.X, 0, sourceWidth - 1);
var tileY = Hlsl.Clamp((int)bestSite.Y, 0, sourceHeight - 1);
var tileSample = source[new Int2(tileX, tileY)];
var tileAlpha = Hlsl.Max(tileSample.W, 1e-4f);
var r = tileSample.X / tileAlpha;
@@ -825,6 +795,12 @@ internal static class DecorativeMosaicShaderMath
public static Float2 CellCenter(int i, int j, float cellSize)
=> new((i + 0.5f) * cellSize, (j + 0.5f) * cellSize);
public static Float2 OwnerReference(float x, float y, float cellSize, int gridWidth, int gridHeight)
=> CellCenter(
Hlsl.Clamp((int)(x / cellSize), 0, gridWidth - 1),
Hlsl.Clamp((int)(y / cellSize), 0, gridHeight - 1),
cellSize);
public static float SizeFactor(float edgeDistance, float spacing, float detail)
=> 1f - detail * (1f - DecorativeMosaicSettings.MinimumSizeFactor)
* Hlsl.Saturate(1f - edgeDistance / (DecorativeMosaicSettings.DetailRangeFactor * spacing));
+44 -68
ファイルの表示
@@ -19,19 +19,15 @@ internal sealed class DecorativeMosaicPipeline : IDisposable
private ReadWriteBuffer<int>? _jumpFloodA;
private ReadWriteBuffer<int>? _jumpFloodB;
private ReadWriteBuffer<int>? _siteMap;
private ReadWriteBuffer<float>? _sitePosX;
private ReadWriteBuffer<float>? _sitePosY;
private ReadWriteBuffer<float>? _siteTheta;
private ReadWriteBuffer<float>? _siteSize;
private ReadWriteBuffer<Float4>? _site;
private ReadWriteBuffer<Float2>? _siteReference;
private ReadWriteBuffer<int>? _siteRank;
private ReadWriteBuffer<int>? _sumX;
private ReadWriteBuffer<int>? _sumY;
private ReadWriteBuffer<int>? _count;
private ReadBackBuffer<float>? _posXReadBack;
private ReadBackBuffer<float>? _posYReadBack;
private ReadBackBuffer<Float4>? _siteReadBack;
private ReadBackBuffer<int>? _rankReadBack;
private float[]? _cachedPosX;
private float[]? _cachedPosY;
private Float4[]? _cachedSites;
private int[]? _cachedRank;
private int _cachedSiteCount;
private StructureKey? _structureKey;
@@ -153,15 +149,12 @@ internal sealed class DecorativeMosaicPipeline : IDisposable
using (ComputeContext context = _device.CreateComputeContext())
RecordGrowthStage(in context, in derived, in parameters);
_scratchReadBack.CopyFrom(_scratch);
var posXReadBack = _posXReadBack!;
var posYReadBack = _posYReadBack!;
var siteReadBack = _siteReadBack!;
var rankReadBack = _rankReadBack!;
posXReadBack.CopyFrom(_sitePosX!);
posYReadBack.CopyFrom(_sitePosY!);
siteReadBack.CopyFrom(_site!);
rankReadBack.CopyFrom(_siteRank!);
_cachedSiteCount = _scratchReadBack.Span[0];
posXReadBack.Span.CopyTo(_cachedPosX!);
posYReadBack.Span.CopyTo(_cachedPosY!);
siteReadBack.Span.CopyTo(_cachedSites!);
rankReadBack.Span.CopyTo(_cachedRank!);
BuildBoundsPrefix();
_structureKey = key;
@@ -277,48 +270,43 @@ internal sealed class DecorativeMosaicPipeline : IDisposable
context.Barrier(_edgeDistance!);
context.For(gridWidth, gridHeight, new SpawnShader(
_mask!, _theta!, _edgeDistance!, _sitePosX!, _sitePosY!, _siteTheta!, _siteSize!, _siteRank!, _scratch,
_mask!, _theta!, _edgeDistance!, _site!, _siteReference!, _siteRank!,
_sumX!, _sumY!, _count!, _scratch,
gridWidth, gridHeight, cellSize, derived.Spacing, parameters.Detail, parameters.Seed));
context.Barrier(_sitePosX!);
context.Barrier(_sitePosY!);
context.Barrier(_siteTheta!);
context.Barrier(_siteSize!);
context.Barrier(_site!);
context.Barrier(_siteReference!);
context.Barrier(_siteRank!);
context.Barrier(_sumX!);
context.Barrier(_sumY!);
context.Barrier(_count!);
context.Barrier(_scratch);
for (var iteration = 0; iteration < derived.Iterations; iteration++)
{
RecordSiteVoronoi(in context, in derived, out reading);
context.For(gridLength, new FillIntShader(_sumX!, gridLength, 0));
context.For(gridLength, new FillIntShader(_sumY!, gridLength, 0));
context.For(gridLength, new FillIntShader(_count!, gridLength, 0));
context.Barrier(_sumX!);
context.Barrier(_sumY!);
context.Barrier(_count!);
RecordSiteVoronoi(in context, in derived);
var avoidWidth = iteration < derived.Iterations - DecorativeMosaicSettings.OpenIterations ? derived.AvoidWidth : 0f;
context.For(gridWidth, gridHeight, new CentroidAccumulateShader(
reading, _mask!, _edgeDistance!, _sitePosX!, _sitePosY!, _sumX!, _sumY!, _count!,
_siteMap!, _mask!, _edgeDistance!, _siteReference!, _sumX!, _sumY!, _count!,
gridWidth, gridHeight, cellSize, avoidWidth, derived.Cutoff));
context.Barrier(_sumX!);
context.Barrier(_sumY!);
context.Barrier(_count!);
context.For(gridWidth, gridHeight, new SiteMoveShader(
_siteRank!, _sitePosX!, _sitePosY!, _siteTheta!, _siteSize!, _sumX!, _sumY!, _count!,
_siteRank!, _site!, _siteReference!, _sumX!, _sumY!, _count!,
_theta!, _edgeDistance!, _scratch, gridWidth, gridHeight, cellSize, derived.Spacing, parameters.Detail));
context.Barrier(_sitePosX!);
context.Barrier(_sitePosY!);
context.Barrier(_siteTheta!);
context.Barrier(_siteSize!);
context.Barrier(_site!);
context.Barrier(_siteReference!);
context.Barrier(_siteRank!);
context.Barrier(_sumX!);
context.Barrier(_sumY!);
context.Barrier(_count!);
}
RecordSiteVoronoi(in context, in derived, out reading);
context.For(gridLength, new CopyIntShader(reading, _siteMap!, gridLength));
context.Barrier(_siteMap!);
RecordSiteVoronoi(in context, in derived);
context.Barrier(_scratch);
}
private void RecordSiteVoronoi(in ComputeContext context, in DerivedValues derived, out ReadWriteBuffer<int> result)
private void RecordSiteVoronoi(in ComputeContext context, in DerivedValues derived)
{
var gridWidth = _gridWidth;
var gridHeight = _gridHeight;
@@ -326,21 +314,21 @@ internal sealed class DecorativeMosaicPipeline : IDisposable
context.For(gridLength, new FillIntShader(_jumpFloodA!, gridLength, DecorativeMosaicSettings.SiteSentinel));
context.Barrier(_jumpFloodA!);
context.For(gridWidth, gridHeight, new SiteSeedShader(
_siteRank!, _sitePosX!, _sitePosY!, _jumpFloodA!, gridWidth, gridHeight, derived.CellSize));
_siteRank!, _site!, _jumpFloodA!, gridWidth, gridHeight, derived.CellSize));
context.Barrier(_jumpFloodA!);
var reading = _jumpFloodA!;
var writing = _jumpFloodB!;
var stepSize = InitialJumpFloodStep(gridWidth, gridHeight);
while (stepSize >= 1)
{
var target = stepSize == 1 ? _siteMap! : writing;
context.For(gridWidth, gridHeight, new SiteJumpFloodPassShader(
reading, writing, _sitePosX!, _sitePosY!, _siteTheta!, _siteSize!,
reading, target, _site!,
gridWidth, gridHeight, stepSize, derived.CellSize, derived.Gamma));
context.Barrier(writing);
(reading, writing) = (writing, reading);
context.Barrier(target);
(reading, writing) = (target, reading);
stepSize >>= 1;
}
result = reading;
}
private void RecordRenderStage(
@@ -354,7 +342,7 @@ internal sealed class DecorativeMosaicPipeline : IDisposable
in Parameters parameters)
{
context.For(rect.Width, rect.Height, new RenderShader(
_siteMap!, _sitePosX!, _sitePosY!, _siteTheta!, _siteSize!, _siteRank!, source, output,
_siteMap!, _site!, _siteRank!, source, output,
rect.X, rect.Y, rect.Width, rect.Height, _gridWidth, _gridHeight,
canvasWidth, canvasHeight, derived.CellSize, derived.Gamma, derived.HalfBase,
DecorativeMosaicSettings.GetVisibleCount(parameters.Laying), parameters.Seed,
@@ -385,15 +373,14 @@ internal sealed class DecorativeMosaicPipeline : IDisposable
}
var ranks = _cachedRank!;
var posX = _cachedPosX!;
var posY = _cachedPosY!;
var sites = _cachedSites!;
for (var index = 0; index < ranks.Length; index++)
{
var rank = ranks[index];
if (rank < 0 || rank >= DecorativeMosaicSettings.LayingSteps)
continue;
var x = posX[index];
var y = posY[index];
var x = sites[index].X;
var y = sites[index].Y;
if (x < _boundsMinX[rank])
_boundsMinX[rank] = x;
if (x > _boundsMaxX[rank])
@@ -451,19 +438,15 @@ internal sealed class DecorativeMosaicPipeline : IDisposable
_jumpFloodA = _device.AllocateReadWriteBuffer<int>(gridLength);
_jumpFloodB = _device.AllocateReadWriteBuffer<int>(gridLength);
_siteMap = _device.AllocateReadWriteBuffer<int>(gridLength);
_sitePosX = _device.AllocateReadWriteBuffer<float>(gridLength);
_sitePosY = _device.AllocateReadWriteBuffer<float>(gridLength);
_siteTheta = _device.AllocateReadWriteBuffer<float>(gridLength);
_siteSize = _device.AllocateReadWriteBuffer<float>(gridLength);
_site = _device.AllocateReadWriteBuffer<Float4>(gridLength);
_siteReference = _device.AllocateReadWriteBuffer<Float2>(gridLength);
_siteRank = _device.AllocateReadWriteBuffer<int>(gridLength);
_sumX = _device.AllocateReadWriteBuffer<int>(gridLength);
_sumY = _device.AllocateReadWriteBuffer<int>(gridLength);
_count = _device.AllocateReadWriteBuffer<int>(gridLength);
_posXReadBack = _device.AllocateReadBackBuffer<float>(gridLength);
_posYReadBack = _device.AllocateReadBackBuffer<float>(gridLength);
_siteReadBack = _device.AllocateReadBackBuffer<Float4>(gridLength);
_rankReadBack = _device.AllocateReadBackBuffer<int>(gridLength);
_cachedPosX = new float[gridLength];
_cachedPosY = new float[gridLength];
_cachedSites = new Float4[gridLength];
_cachedRank = new int[gridLength];
_cachedSiteCount = 0;
_gridWidth = gridWidth;
@@ -492,16 +475,13 @@ internal sealed class DecorativeMosaicPipeline : IDisposable
_jumpFloodA?.Dispose();
_jumpFloodB?.Dispose();
_siteMap?.Dispose();
_sitePosX?.Dispose();
_sitePosY?.Dispose();
_siteTheta?.Dispose();
_siteSize?.Dispose();
_site?.Dispose();
_siteReference?.Dispose();
_siteRank?.Dispose();
_sumX?.Dispose();
_sumY?.Dispose();
_count?.Dispose();
_posXReadBack?.Dispose();
_posYReadBack?.Dispose();
_siteReadBack?.Dispose();
_rankReadBack?.Dispose();
_mask = null;
_luminance = null;
@@ -510,19 +490,15 @@ internal sealed class DecorativeMosaicPipeline : IDisposable
_jumpFloodA = null;
_jumpFloodB = null;
_siteMap = null;
_sitePosX = null;
_sitePosY = null;
_siteTheta = null;
_siteSize = null;
_site = null;
_siteReference = null;
_siteRank = null;
_sumX = null;
_sumY = null;
_count = null;
_posXReadBack = null;
_posYReadBack = null;
_siteReadBack = null;
_rankReadBack = null;
_cachedPosX = null;
_cachedPosY = null;
_cachedSites = null;
_cachedRank = null;
_cachedSiteCount = 0;
_structureKey = null;