Use DirectX 12 APIs to decode D2D texture, remove extra assets

このコミットが含まれているのは:
Sergio Pedri committed 2023-01-21 23:11:03 +01:00
1 親 9ef556e33b
コミット a1f78409a3
3ファイル変更
+23 -42

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

+23 -27
ファイルの表示
@@ -1,6 +1,6 @@
#if WINDOWS_UWP
using System;
using System.IO;
using CommunityToolkit.Diagnostics;
using ComputeSharp.D2D1;
using ComputeSharp.D2D1.Interop;
using ComputeSharp.D2D1.Uwp;
@@ -32,36 +32,32 @@ public sealed class D2D1ContouredLayersRunner : ID2D1ShaderRunner
{
if (this.pixelShaderEffect is null)
{
D2D1ResourceTextureManager resourceTextureManager;
string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.png");
string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.1024x1024rgba32");
// As a temporary workaround for the lack of image decoding helpers for D2D1ResourceTextureManager, and in order to
// keep the code here synchronous and simple, we can just leverage the DirectX 12 APIs to load textures. That is, we
// first load a texture (which will use WIC behind the scenes) to get the decoded image data in GPU memory, and then
// copy the data in a readback texture we can read from on the CPU. We can't just load an upload texture and read
// from it, as that type of texture can only be written to from the CPU. From there, we create a D2D resource texture.
using ReadOnlyTexture2D<Rgba32, float4> readOnlyTexture = GraphicsDevice.GetDefault().LoadReadOnlyTexture2D<Rgba32, float4>(filename);
using ReadBackTexture2D<Rgba32> readBackTexture = GraphicsDevice.GetDefault().AllocateReadBackTexture2D<Rgba32>(readOnlyTexture.Width, readOnlyTexture.Height);
// Open a file to read from the file (we're doing synchronous IO here by design, doesn't really matter)
using (FileStream fileStream = new(filename, FileMode.Open, FileAccess.Read))
{
byte[] pixelData = new byte[(int)fileStream.Length];
int totalBytesRead = 0;
readOnlyTexture.CopyTo(readBackTexture);
// Read the raw image data
while (totalBytesRead < pixelData.Length)
{
int bytesRead = fileStream.Read(pixelData, 0, pixelData.Length);
// Get the buffer pointer, the stride, and calculate the buffer size without the trailing padding.
// That is, the area between the end of the data in the last row and the stride is not included.
Rgba32* dataBuffer = readBackTexture.View.DangerousGetAddressAndByteStride(out int strideInBytes);
int bufferSize = ((readBackTexture.Height - 1) * strideInBytes) + (readBackTexture.View.Width * sizeof(Rgba32));
Guard.IsGreaterThan(bytesRead, 0);
totalBytesRead += bytesRead;
}
// Create the resource texture manager to use in the shader
resourceTextureManager = new D2D1ResourceTextureManager(
extents: stackalloc uint[] { 1024, 1024 },
bufferPrecision: D2D1BufferPrecision.UInt8Normalized,
channelDepth: D2D1ChannelDepth.Four,
filter: D2D1Filter.MinMagMipLinear,
extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror },
data: pixelData,
strides: stackalloc uint[] { (uint)(1024 * sizeof(Bgra32)) });
}
// Create the resource texture manager to use in the shader
D2D1ResourceTextureManager resourceTextureManager = new(
extents: stackalloc uint[] { (uint)readBackTexture.Height, (uint)readBackTexture.Width },
bufferPrecision: D2D1BufferPrecision.UInt8Normalized,
channelDepth: D2D1ChannelDepth.Four,
filter: D2D1Filter.MinMagMipLinear,
extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror },
data: new ReadOnlySpan<byte>(dataBuffer, bufferSize),
strides: stackalloc uint[] { (uint)strideInBytes });
// Create the new pixel shader effect
this.pixelShaderEffect = new PixelShaderEffect<ContouredLayers>() { ResourceTextureManagers = { [0] = resourceTextureManager } };