Add tests to update 2D resource textures

このコミットが含まれているのは:
Sergio Pedri committed 2022-08-15 15:01:20 +02:00
1 親 edfed9359b
コミット 0ab4e331c1
1ファイル変更
+114 -2
+114 -2
ファイルの表示
@@ -372,7 +372,7 @@ public partial class D2D1ResourceTextureManagerTests
[DataRow(128, 32, 64)]
[DataRow(1024, 311, 489)]
[DataRow(1024, 907, 117)]
public unsafe void UpdateResourceTexture1D_RCW(int width, int startOffset, int updateLength)
public unsafe void UpdateResourceTexture1D(int width, int startOffset, int updateLength)
{
using ComPtr<ID2D1Factory2> d2D1Factory2 = D2D1Helper.CreateD2D1Factory2();
using ComPtr<ID2D1Device> d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get());
@@ -442,7 +442,119 @@ public partial class D2D1ResourceTextureManagerTests
{
int2 xy = (int2)D2D.GetScenePosition().XY;
return this.source[(int)((uint)xy.X % (uint)this.width)];
float4 result = default;
if (xy.X < this.width &&
xy.Y == 0)
{
result = this.source[xy.X];
}
return result;
}
}
[TestMethod]
[DataRow(64, 64, 0, 0, 64, 64)]
[DataRow(64, 64, 32, 32, 32, 32)]
[DataRow(64, 64, 12, 33, 43, 22)]
[DataRow(512, 768, 245, 111, 255, 461)]
[DataRow(1024, 2048, 11, 899, 512, 342)]
public unsafe void UpdateResourceTexture2D(
int width,
int height,
int startOffsetX,
int startOffsetY,
int updateLengthX,
int updateLengthY)
{
using ComPtr<ID2D1Factory2> d2D1Factory2 = D2D1Helper.CreateD2D1Factory2();
using ComPtr<ID2D1Device> d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get());
using ComPtr<ID2D1DeviceContext> d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get());
D2D1PixelShaderEffect.RegisterForD2D1Factory1<CopyFromResourceTexture2DShader>(d2D1Factory2.Get(), null, out _);
using ComPtr<ID2D1Effect> d2D1Effect = default;
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<CopyFromResourceTexture2DShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
CopyFromResourceTexture2DShader shader = new(width, height);
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
byte[] texture = new byte[width * height];
D2D1ResourceTextureManager resourceTextureManager = new(
extents: stackalloc[] { (uint)width, (uint)height },
bufferPrecision: D2D1BufferPrecision.UInt8Normalized,
channelDepth: D2D1ChannelDepth.One,
filter: D2D1Filter.MinMagMipPoint,
extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp },
data: texture,
strides: stackalloc uint[] { (uint)width });
byte[] data = RandomNumberGenerator.GetBytes(updateLengthX * updateLengthY);
resourceTextureManager.Update(
minimumExtents: stackalloc uint[] { (uint)startOffsetX, (uint)startOffsetY },
maximimumExtents: stackalloc uint[] { (uint)(startOffsetX + updateLengthX), (uint)(startOffsetY + updateLengthY) },
strides: stackalloc uint[] { (uint)updateLengthX },
data: data);
for (int y = 0; y < updateLengthY; y++)
{
ReadOnlySpan<byte> source = data.AsSpan(y * updateLengthX, updateLengthX);
Span<byte> destination = texture.AsSpan(startOffsetY * width + y * width + startOffsetX, updateLengthX);
source.CopyTo(destination);
}
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager, 0);
using ComPtr<ID2D1Bitmap> d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)height);
D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get());
using ComPtr<ID2D1Bitmap1> d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
byte[] resultingBytes = new byte[width * height];
int i = 0;
for (int y = 0; y < height; y++)
{
foreach (Bgra32 pixel in new ReadOnlySpan<Bgra32>(d2D1MappedRect.bits + d2D1MappedRect.pitch * y, width))
{
resultingBytes[i++] = pixel.B;
}
}
Assert.IsTrue(texture.AsSpan().SequenceEqual(resultingBytes));
}
[D2DInputCount(0)]
[D2DRequiresScenePosition]
[AutoConstructor]
private partial struct CopyFromResourceTexture2DShader : ID2D1PixelShader
{
private int width;
private int height;
[D2DResourceTextureIndex(0)]
private D2D1ResourceTexture2D<float> source;
public float4 Execute()
{
int2 xy = (int2)D2D.GetScenePosition().XY;
float4 result = default;
if (xy.X < this.width &&
xy.Y < this.height)
{
result = this.source[xy];
}
return result;
}
}
}