public unsafe void Allocate_ReadBackTexture2D_Copy_Range(Device device, int x, int y, int width, int height) { int[,] source = new int[2048, 2048]; fixed(int *p = source) { new Random(42).NextBytes(new Span <int>(p, source.Length).AsBytes()); } using ReadOnlyTexture2D <int> readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(source); using ReadBackTexture2D <int> readBackTexture2D = device.Get().AllocateReadBackTexture2D <int>(2048, 2048); readOnlyTexture2D.CopyTo(readBackTexture2D, x, y, width, height); fixed(int *p = source) { for (int i = 0; i < height; i++) { Span <int> sourceRow = new Span <int>(Unsafe.AsPointer(ref source[i + y, 0]), source.GetLength(1)).Slice(x, width); Span <int> destinationRow = readBackTexture2D.View.GetRowSpan(i).Slice(0, width); Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); } } }
public unsafe void Allocate_UploadTexture2D_Copy_Range(Device device, int x, int y, int width, int height) { using UploadTexture2D <int> uploadTexture2D = device.Get().AllocateUploadTexture2D <int>(2048, 2048); for (int i = 0; i < uploadTexture2D.Height; i++) { new Random(i).NextBytes(uploadTexture2D.View.GetRowSpan(i).AsBytes()); } using ReadOnlyTexture2D <int> readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D <int>(uploadTexture2D.Width, uploadTexture2D.Height); uploadTexture2D.CopyTo(readOnlyTexture2D, x, y, width, height); int[,] result = readOnlyTexture2D.ToArray(); fixed(void *_ = result) { for (int i = 0; i < height; i++) { Span <int> sourceRow = uploadTexture2D.View.GetRowSpan(i + y).Slice(x, width); Span <int> destinationRow = new Span <int>(Unsafe.AsPointer(ref result[i, 0]), result.GetLength(1)).Slice(0, width); Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); } } }
public unsafe void Allocate_ReadBackTexture2D_Copy_Full(Device device) { int[,] source = new int[256, 256]; fixed(int *p = source) { new Random(42).NextBytes(new Span <int>(p, source.Length).AsBytes()); } using ReadOnlyTexture2D <int> readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(source); using ReadBackTexture2D <int> readBackTexture2D = device.Get().AllocateReadBackTexture2D <int>(256, 256); readOnlyTexture2D.CopyTo(readBackTexture2D); Assert.AreEqual(readBackTexture2D.Width, 256); Assert.AreEqual(readBackTexture2D.Height, 256); fixed(int *p = source) { for (int i = 0; i < readBackTexture2D.Height; i++) { Assert.IsTrue(readBackTexture2D.View.GetRowSpan(i).SequenceEqual(new Span <int>(Unsafe.AsPointer(ref source[i, 0]), source.GetLength(1)))); } } }
public unsafe void Allocate_UploadTexture2D_Copy_Full(Device device) { using UploadTexture2D <int> uploadTexture2D = device.Get().AllocateUploadTexture2D <int>(256, 256); for (int i = 0; i < uploadTexture2D.Height; i++) { new Random(i).NextBytes(uploadTexture2D.View.GetRowSpan(i).AsBytes()); } using ReadOnlyTexture2D <int> readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D <int>(uploadTexture2D.Width, uploadTexture2D.Height); uploadTexture2D.CopyTo(readOnlyTexture2D); int[,] result = readOnlyTexture2D.ToArray(); Assert.AreEqual(uploadTexture2D.Width, result.GetLength(1)); Assert.AreEqual(uploadTexture2D.Height, result.GetLength(0)); fixed(int *_ = result) { for (int i = 0; i < uploadTexture2D.Height; i++) { Assert.IsTrue(uploadTexture2D.View.GetRowSpan(i).SequenceEqual(new Span <int>(Unsafe.AsPointer(ref result[i, 0]), result.GetLength(1)))); } } }
public static ulong ValidateAndGetGpuDescriptorHandle <T>(ReadOnlyTexture2D <T> texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); return(Unsafe.As <D3D12_GPU_DESCRIPTOR_HANDLE, ulong>(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle))); }
public void Dispatch_NormalizedTexture2D(Device device, Type t, Type tPixel) { if (t == typeof(Bgra32) && tPixel == typeof(Float4)) { using ReadOnlyTexture2D <Bgra32, Float4> source = device.Get().AllocateReadOnlyTexture2D <Bgra32, Float4>(128, 128); using ReadWriteTexture2D <Bgra32, Float4> destination = device.Get().AllocateReadWriteTexture2D <Bgra32, Float4>(128, 128); device.Get().For(128, 128, new Shader_Unorm_Bgra32_Float4(source, destination)); } if (t == typeof(R16) && tPixel == typeof(float)) { using ReadOnlyTexture2D <R16, float> source = device.Get().AllocateReadOnlyTexture2D <R16, float>(128, 128); using ReadWriteTexture2D <R16, float> destination = device.Get().AllocateReadWriteTexture2D <R16, float>(128, 128); device.Get().For(128, 128, new Shader_Unorm_R16_float(source, destination)); } if (t == typeof(R8) && tPixel == typeof(float)) { using ReadOnlyTexture2D <R8, float> source = device.Get().AllocateReadOnlyTexture2D <R8, float>(128, 128); using ReadWriteTexture2D <R8, float> destination = device.Get().AllocateReadWriteTexture2D <R8, float>(128, 128); device.Get().For(128, 128, new Shader_Unorm_R8_float(source, destination)); } if (t == typeof(Rg16) && tPixel == typeof(Float2)) { using ReadOnlyTexture2D <Rg16, Float2> source = device.Get().AllocateReadOnlyTexture2D <Rg16, Float2>(128, 128); using ReadWriteTexture2D <Rg16, Float2> destination = device.Get().AllocateReadWriteTexture2D <Rg16, Float2>(128, 128); device.Get().For(128, 128, new Shader_Unorm_Rg16_Float2(source, destination)); } if (t == typeof(Rg32) && tPixel == typeof(Float2)) { using ReadOnlyTexture2D <Rg32, Float2> source = device.Get().AllocateReadOnlyTexture2D <Rg32, Float2>(128, 128); using ReadWriteTexture2D <Rg32, Float2> destination = device.Get().AllocateReadWriteTexture2D <Rg32, Float2>(128, 128); device.Get().For(128, 128, new Shader_Unorm_Rg32_Float2(source, destination)); } if (t == typeof(Rgba32) && tPixel == typeof(Float4)) { using ReadOnlyTexture2D <Rgba32, Float4> source = device.Get().AllocateReadOnlyTexture2D <Rgba32, Float4>(128, 128); using ReadWriteTexture2D <Rgba32, Float4> destination = device.Get().AllocateReadWriteTexture2D <Rgba32, Float4>(128, 128); device.Get().For(128, 128, new Shader_Unorm_Rgba32_Float4(source, destination)); } if (t == typeof(Rgba64) && tPixel == typeof(Float4)) { using ReadOnlyTexture2D <Rgba64, Float4> source = device.Get().AllocateReadOnlyTexture2D <Rgba64, Float4>(128, 128); using ReadWriteTexture2D <Rgba64, Float4> destination = device.Get().AllocateReadWriteTexture2D <Rgba64, Float4>(128, 128); device.Get().For(128, 128, new Shader_Unorm_Rgba64_Float4(source, destination)); } }
public void ContouredLayers(Device device, Type shaderType) { string filename = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) !, "Shaders", "Textures", "RustyMetal.png"); using ReadOnlyTexture2D <Rgba32, float4> background = device.Get().LoadReadOnlyTexture2D <Rgba32, float4>(filename); RunAndCompareShader( device, shaderType, texture => new SwapChain.Shaders.Compute.ContouredLayers(texture, 0, background), texture => new ContouredLayers(0, background), 0.000703f); }
public bool TryExecute(IReadWriteNormalizedTexture2D <Float4> texture, TimeSpan timespan, object?parameter) { if (this.texture is null || this.texture.GraphicsDevice != texture.GraphicsDevice) { string filename = Path.Combine(Package.Current.InstalledLocation.Path, "Assets", "Textures", "RustyMetal.png"); this.texture?.Dispose(); this.texture = texture.GraphicsDevice.LoadReadOnlyTexture2D <Rgba32, Float4>(filename); } texture.GraphicsDevice.ForEach(texture, new ContouredLayers((float)timespan.TotalSeconds, this.texture)); return(true); }
public void Allocate_ReadBackTexture2D_Copy_Range(Device device, int x, int y, int width, int height) { int[,] source = new int[2048, 2048]; for (int i = 0; i < 2048; i++) { new Random(42).NextBytes(source.GetRowSpan(i).AsBytes()); } using ReadOnlyTexture2D <int> readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(source); using ReadBackTexture2D <int> readBackTexture2D = device.Get().AllocateReadBackTexture2D <int>(2048, 2048); readOnlyTexture2D.CopyTo(readBackTexture2D, x, y, width, height); for (int i = 0; i < height; i++) { Span <int> sourceRow = source.GetRowSpan(i + y).Slice(x, width); Span <int> destinationRow = readBackTexture2D.View.GetRowSpan(i).Slice(0, width); Assert.IsTrue(sourceRow.SequenceEqual(destinationRow)); } }
public void Allocate_ReadBackTexture2D_Copy_Full(Device device) { int[,] source = new int[256, 256]; for (int i = 0; i < 256; i++) { new Random(42).NextBytes(source.GetRowSpan(i).AsBytes()); } using ReadOnlyTexture2D <int> readOnlyTexture2D = device.Get().AllocateReadOnlyTexture2D(source); using ReadBackTexture2D <int> readBackTexture2D = device.Get().AllocateReadBackTexture2D <int>(256, 256); readOnlyTexture2D.CopyTo(readBackTexture2D); Assert.AreEqual(readBackTexture2D.Width, 256); Assert.AreEqual(readBackTexture2D.Height, 256); for (int i = 0; i < readBackTexture2D.Height; i++) { Assert.IsTrue(readBackTexture2D.View.GetRowSpan(i).SequenceEqual(source.GetRowSpan(i))); } }