update CopyTexture

refresh2
cosmonaut 2024-02-23 12:40:12 -08:00
parent b9337fdbe6
commit 3299ff578f
1 changed files with 58 additions and 61 deletions

View File

@ -1,5 +1,4 @@
using System.Runtime.InteropServices;
using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
@ -8,8 +7,8 @@ namespace MoonWorks.Test
class CopyTextureGame : Game
{
private GraphicsPipeline pipeline;
private Buffer vertexBuffer;
private Buffer indexBuffer;
private GpuBuffer vertexBuffer;
private GpuBuffer indexBuffer;
private Texture originalTexture;
private Texture textureCopy;
private Texture textureSmallCopy;
@ -36,15 +35,10 @@ namespace MoonWorks.Test
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
// Create and populate the GPU resources
vertexBuffer = Buffer.Create<PositionTextureVertex>(GraphicsDevice, BufferUsageFlags.Vertex, 12);
indexBuffer = Buffer.Create<ushort>(GraphicsDevice, BufferUsageFlags.Index, 12);
var resourceInitializer = new ResourceInitializer(GraphicsDevice);
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
cmdbuf.SetBufferData(
vertexBuffer,
new PositionTextureVertex[]
{
vertexBuffer = resourceInitializer.CreateBuffer(
[
new PositionTextureVertex(new Vector3(-1f, 0f, 0), new Vector2(0, 0)),
new PositionTextureVertex(new Vector3( 0f, 0f, 0), new Vector2(1, 0)),
new PositionTextureVertex(new Vector3( 0f, 1f, 0), new Vector2(1, 1)),
@ -59,84 +53,87 @@ namespace MoonWorks.Test
new PositionTextureVertex(new Vector3( 0.5f, -1f, 0), new Vector2(1, 0)),
new PositionTextureVertex(new Vector3( 0.5f, 0f, 0), new Vector2(1, 1)),
new PositionTextureVertex(new Vector3(-0.5f, 0f, 0), new Vector2(0, 1))
}
],
BufferUsageFlags.Vertex
);
cmdbuf.SetBufferData(
indexBuffer,
new ushort[]
{
indexBuffer = resourceInitializer.CreateBuffer<ushort>(
[
0, 1, 2,
0, 2, 3,
}
],
BufferUsageFlags.Index
);
// Load the texture. Storing the texture bytes so we can compare them.
var fileStream = new System.IO.FileStream(TestUtils.GetTexturePath("ravioli.png"), System.IO.FileMode.Open, System.IO.FileAccess.Read);
var fileLength = fileStream.Length;
var fileBuffer = NativeMemory.Alloc((nuint) fileLength);
var fileSpan = new System.Span<byte>(fileBuffer, (int) fileLength);
fileStream.ReadExactly(fileSpan);
originalTexture = resourceInitializer.CreateTexture2D(
TestUtils.GetTexturePath("ravioli.png")
);
var pixels = RefreshCS.Refresh.Refresh_Image_Load(
(nint) fileBuffer,
(int) fileLength,
resourceInitializer.Upload();
resourceInitializer.Dispose();
// Load the texture bytes so we can compare them.
var pixels = ImageUtils.GetPixelDataFromFile(
TestUtils.GetTexturePath("ravioli.png"),
out var width,
out var height,
out var byteCount
);
NativeMemory.Free(fileBuffer);
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
TextureCreateInfo textureCreateInfo = new TextureCreateInfo();
textureCreateInfo.Width = (uint) width;
textureCreateInfo.Height = (uint) height;
textureCreateInfo.Depth = 1;
textureCreateInfo.Format = TextureFormat.R8G8B8A8;
textureCreateInfo.IsCube = false;
textureCreateInfo.LevelCount = 1;
textureCreateInfo.UsageFlags = TextureUsageFlags.Sampler;
originalTexture = new Texture(GraphicsDevice, textureCreateInfo);
cmdbuf.SetTextureData(originalTexture, pixels, (uint) byteCount);
var textureCreateInfo = new TextureCreateInfo
{
Width = originalTexture.Width,
Height = originalTexture.Height,
Depth = originalTexture.Depth,
IsCube = originalTexture.IsCube,
LevelCount = originalTexture.LevelCount,
SampleCount = originalTexture.SampleCount,
Format = originalTexture.Format,
UsageFlags = originalTexture.UsageFlags
};
// Create a 1:1 copy of the texture
textureCopy = new Texture(GraphicsDevice, textureCreateInfo);
cmdbuf.BeginCopyPass();
cmdbuf.CopyTextureToTexture(
new TextureSlice(originalTexture),
new TextureSlice(textureCopy),
Filter.Linear
originalTexture,
textureCopy
);
cmdbuf.EndCopyPass();
// Create a half-sized copy of this texture
textureCreateInfo.Width /= 2;
textureCreateInfo.Height /= 2;
textureCreateInfo.UsageFlags |= TextureUsageFlags.ColorTarget;
textureSmallCopy = new Texture(GraphicsDevice, textureCreateInfo);
cmdbuf.CopyTextureToTexture(
new TextureSlice(originalTexture),
new TextureSlice(
textureSmallCopy,
new Rect(
(int) textureCreateInfo.Width,
(int) textureCreateInfo.Height
)
),
Filter.Linear
);
// Copy the texture to a buffer
Buffer compareBuffer = Buffer.Create<byte>(GraphicsDevice, 0, (uint) byteCount);
cmdbuf.CopyTextureToBuffer(new TextureSlice(originalTexture), compareBuffer);
// Render the half-size copy
cmdbuf.Blit(originalTexture, textureSmallCopy, Filter.Linear);
// Copy the texture to a transfer buffer
TransferBuffer compareBuffer = new TransferBuffer(GraphicsDevice, byteCount);
cmdbuf.BeginCopyPass();
cmdbuf.DownloadFromTexture(
new TextureSlice(originalTexture),
compareBuffer,
new BufferImageCopy(0, 0, 0)
);
cmdbuf.EndCopyPass();
var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
GraphicsDevice.WaitForFences(fence);
GraphicsDevice.ReleaseFence(fence);
// Compare the original bytes to the copied bytes.
var copiedBytes = NativeMemory.Alloc((nuint) byteCount);
var copiedSpan = new System.Span<byte>(copiedBytes, byteCount);
var copiedBytes = NativeMemory.Alloc(byteCount);
var copiedSpan = new System.Span<byte>(copiedBytes, (int) byteCount);
compareBuffer.GetData(copiedSpan);
var originalSpan = new System.Span<byte>((void*) pixels, byteCount);
var originalSpan = new System.Span<byte>((void*) pixels, (int)byteCount);
if (System.MemoryExtensions.SequenceEqual(originalSpan, copiedSpan))
{
@ -164,11 +161,11 @@ namespace MoonWorks.Test
cmdbuf.BindVertexBuffers(vertexBuffer);
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(originalTexture, sampler));
cmdbuf.DrawIndexedPrimitives(0, 0, 2, 0, 0);
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textureCopy, sampler));
cmdbuf.DrawIndexedPrimitives(4, 0, 2, 0, 0);
cmdbuf.DrawIndexedPrimitives(4, 0, 2);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textureSmallCopy, sampler));
cmdbuf.DrawIndexedPrimitives(8, 0, 2, 0, 0);
cmdbuf.DrawIndexedPrimitives(8, 0, 2);
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);