Use a custom mapper for pixelate effect
このコミットが含まれているのは:
1 親
8eed74214a
コミット
ea354eade6
2ファイル変更
+73
-39
検索条件に一致するファイルはありません
@@ -1,4 +1,6 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Tests.Helpers;
|
||||
@@ -14,13 +16,17 @@ public class EndToEndTests
|
||||
[TestMethod]
|
||||
public unsafe void InvertWithCustomThreshold()
|
||||
{
|
||||
RunAndCompareShader(new InvertShader(1), "Landscape.png", "Landscape_Inverted.png");
|
||||
RunAndCompareShader(new InvertShader(1), null, "Landscape.png", "Landscape_Inverted.png");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void Pixelate()
|
||||
{
|
||||
RunAndCompareShader(new PixelateShader(new PixelateShader.Constants(1280, 720, 16)), "Landscape.png", "Landscape_Pixelate.png");
|
||||
RunAndCompareShader(
|
||||
new PixelateEffect.Shader(new PixelateEffect.Shader.Constants(1280, 720, 16)),
|
||||
static () => new PixelateEffect(),
|
||||
"Landscape.png",
|
||||
"Landscape_Pixelate.png");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -28,11 +34,13 @@ public class EndToEndTests
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of pixel shader to run.</typeparam>
|
||||
/// <param name="originalFileName">The name of the source image.</param>
|
||||
/// <param name="transformMapperFactory">A custom <see cref="ID2D1TransformMapper{T}"/> factory for the effect.</param>
|
||||
/// <param name="expectedFileName">The name of the expected result image.</param>
|
||||
/// <param name="destinationFileName">The name of the destination image to save results to.</param>
|
||||
/// <param name="shader">The shader to run.</param>
|
||||
private static void RunAndCompareShader<T>(
|
||||
in T shader,
|
||||
Func<ID2D1TransformMapper<T>>? transformMapperFactory,
|
||||
string originalFileName,
|
||||
string expectedFileName,
|
||||
[CallerMemberName] string destinationFileName = "")
|
||||
@@ -49,9 +57,10 @@ public class EndToEndTests
|
||||
|
||||
// Run the shader
|
||||
D2D1ShaderTestHelper.ExecutePixelShaderAndCompareResults(
|
||||
in shader,
|
||||
transformMapperFactory,
|
||||
originalPath,
|
||||
destinationPath,
|
||||
in shader);
|
||||
destinationPath);
|
||||
|
||||
// Compare the results
|
||||
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f);
|
||||
@@ -75,45 +84,67 @@ public partial struct InvertShader : ID2D1PixelShader
|
||||
}
|
||||
}
|
||||
|
||||
[D2DInputCount(1)]
|
||||
[D2DInputComplex(0)]
|
||||
[D2DRequiresScenePosition]
|
||||
[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader40)]
|
||||
[AutoConstructor]
|
||||
public partial struct PixelateShader : ID2D1PixelShader
|
||||
public sealed partial class PixelateEffect : ID2D1TransformMapper<PixelateEffect.Shader>
|
||||
{
|
||||
[AutoConstructor]
|
||||
public partial struct Constants
|
||||
/// <inheritdoc/>
|
||||
void ID2D1TransformMapper<Shader>.MapInputsToOutput(in Shader shader, ReadOnlySpan<Rectangle> inputs, ReadOnlySpan<Rectangle> opaqueInputs, out Rectangle output, out Rectangle opaqueOutput)
|
||||
{
|
||||
public readonly int inputWidth;
|
||||
public readonly int inputHeight;
|
||||
public readonly int cellSize;
|
||||
output = inputs[0];
|
||||
opaqueOutput = Rectangle.Empty;
|
||||
}
|
||||
|
||||
private readonly Constants constants;
|
||||
|
||||
public float4 Execute()
|
||||
/// <inheritdoc/>
|
||||
void ID2D1TransformMapper<Shader>.MapInvalidOutput(in Shader shader, int inputIndex, Rectangle invalidInput, out Rectangle invalidOutput)
|
||||
{
|
||||
float2 scenePos = D2D.GetScenePosition().XY;
|
||||
uint x = (uint)Hlsl.Floor(scenePos.X);
|
||||
uint y = (uint)Hlsl.Floor(scenePos.Y);
|
||||
invalidOutput = invalidInput;
|
||||
}
|
||||
|
||||
int cellX = (int)Hlsl.Floor(x / this.constants.cellSize);
|
||||
int cellY = (int)Hlsl.Floor(y / this.constants.cellSize);
|
||||
/// <inheritdoc/>
|
||||
void ID2D1TransformMapper<Shader>.MapOutputToInputs(in Shader shader, in Rectangle output, Span<Rectangle> inputs)
|
||||
{
|
||||
inputs.Fill(output);
|
||||
}
|
||||
|
||||
int x0 = cellX * this.constants.cellSize;
|
||||
int y0 = cellY * this.constants.cellSize;
|
||||
[D2DInputCount(1)]
|
||||
[D2DInputComplex(0)]
|
||||
[D2DRequiresScenePosition]
|
||||
[D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader40)]
|
||||
[AutoConstructor]
|
||||
public partial struct Shader : ID2D1PixelShader
|
||||
{
|
||||
[AutoConstructor]
|
||||
public partial struct Constants
|
||||
{
|
||||
public readonly int inputWidth;
|
||||
public readonly int inputHeight;
|
||||
public readonly int cellSize;
|
||||
}
|
||||
|
||||
int x1 = Hlsl.Min(this.constants.inputWidth, x0 + this.constants.cellSize) - 1;
|
||||
int y1 = Hlsl.Min(this.constants.inputHeight, y0 + this.constants.cellSize) - 1;
|
||||
private readonly Constants constants;
|
||||
|
||||
float4 sample0 = D2D.SampleInputAtPosition(0, new int2(x0, y0));
|
||||
float4 sample1 = D2D.SampleInputAtPosition(0, new int2(x1, y0));
|
||||
float4 sample2 = D2D.SampleInputAtPosition(0, new int2(x0, y1));
|
||||
float4 sample3 = D2D.SampleInputAtPosition(0, new int2(x1, y1));
|
||||
public float4 Execute()
|
||||
{
|
||||
float2 scenePos = D2D.GetScenePosition().XY;
|
||||
uint x = (uint)Hlsl.Floor(scenePos.X);
|
||||
uint y = (uint)Hlsl.Floor(scenePos.Y);
|
||||
|
||||
float4 color = (sample0 + sample1 + sample2 + sample3) / 4;
|
||||
int cellX = (int)Hlsl.Floor(x / this.constants.cellSize);
|
||||
int cellY = (int)Hlsl.Floor(y / this.constants.cellSize);
|
||||
|
||||
return color;
|
||||
int x0 = cellX * this.constants.cellSize;
|
||||
int y0 = cellY * this.constants.cellSize;
|
||||
|
||||
int x1 = Hlsl.Min(this.constants.inputWidth, x0 + this.constants.cellSize) - 1;
|
||||
int y1 = Hlsl.Min(this.constants.inputHeight, y0 + this.constants.cellSize) - 1;
|
||||
|
||||
float4 sample0 = D2D.SampleInputAtPosition(0, new int2(x0, y0));
|
||||
float4 sample1 = D2D.SampleInputAtPosition(0, new int2(x1, y0));
|
||||
float4 sample2 = D2D.SampleInputAtPosition(0, new int2(x0, y1));
|
||||
float4 sample3 = D2D.SampleInputAtPosition(0, new int2(x1, y1));
|
||||
|
||||
float4 color = (sample0 + sample1 + sample2 + sample3) / 4;
|
||||
|
||||
return color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using System;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Tests.Extensions;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
@@ -14,13 +15,15 @@ internal static class D2D1ShaderTestHelper
|
||||
/// Executes a pixel shader to produce an image.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The shader type to execute.</typeparam>
|
||||
/// <param name="shader">The shader to run.</param>
|
||||
/// <param name="transformMapperFactory">A custom <see cref="ID2D1TransformMapper{T}"/> factory for the effect.</param>
|
||||
/// <param name="sourcePath">The source path for the image to run the shader on.</param>
|
||||
/// <param name="destinationPath">The destination path for the result.</param>
|
||||
/// <param name="shader">The shader to run.</param>
|
||||
public static unsafe void ExecutePixelShaderAndCompareResults<T>(
|
||||
in T shader,
|
||||
Func<ID2D1TransformMapper<T>>? transformMapperFactory,
|
||||
string sourcePath,
|
||||
string destinationPath,
|
||||
in T shader)
|
||||
string destinationPath)
|
||||
where T : unmanaged, ID2D1PixelShader
|
||||
{
|
||||
using ComPtr<ID2D1Factory2> d2D1Factory2 = default;
|
||||
@@ -113,7 +116,7 @@ internal static class D2D1ShaderTestHelper
|
||||
wicBitmapSource: (IWICBitmapSource*)wicBitmap.Get(),
|
||||
bitmap: d2D1BitmapSource.GetAddressOf()).Assert();
|
||||
|
||||
D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1<T>(d2D1Factory2.Get(), out _);
|
||||
D2D1InteropServices.RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory2.Get(), transformMapperFactory, out _);
|
||||
|
||||
using ComPtr<ID2D1Effect> d2D1Effect = default;
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする