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