2023-04-19 06:39:45 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2023-01-07 23:03:12 +00:00
|
|
|
|
using MoonWorks.Graphics;
|
|
|
|
|
using MoonWorks.Math.Float;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Test
|
|
|
|
|
{
|
2024-02-07 15:27:55 +00:00
|
|
|
|
class CopyTextureGame : Game
|
|
|
|
|
{
|
|
|
|
|
private GraphicsPipeline pipeline;
|
2024-02-23 20:40:12 +00:00
|
|
|
|
private GpuBuffer vertexBuffer;
|
|
|
|
|
private GpuBuffer indexBuffer;
|
2024-02-07 15:27:55 +00:00
|
|
|
|
private Texture originalTexture;
|
|
|
|
|
private Texture textureCopy;
|
|
|
|
|
private Texture textureSmallCopy;
|
|
|
|
|
private Sampler sampler;
|
|
|
|
|
|
2024-03-07 18:35:12 +00:00
|
|
|
|
public unsafe CopyTextureGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
|
2024-02-07 15:27:55 +00:00
|
|
|
|
{
|
|
|
|
|
// Load the shaders
|
|
|
|
|
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.vert"));
|
|
|
|
|
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.frag"));
|
|
|
|
|
|
|
|
|
|
// Create the graphics pipeline
|
|
|
|
|
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
|
|
|
|
|
MainWindow.SwapchainFormat,
|
|
|
|
|
vertShaderModule,
|
|
|
|
|
fragShaderModule
|
|
|
|
|
);
|
|
|
|
|
pipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions[0].BlendState = ColorAttachmentBlendState.AlphaBlend;
|
|
|
|
|
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
|
|
|
|
|
pipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1;
|
|
|
|
|
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
|
|
|
|
|
|
|
|
|
// Create sampler
|
|
|
|
|
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
|
|
|
|
|
|
|
|
|
|
// Create and populate the GPU resources
|
2024-02-23 22:32:56 +00:00
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
vertexBuffer = resourceUploader.CreateBuffer(
|
2024-02-23 20:40:12 +00:00
|
|
|
|
[
|
2024-02-07 15:27:55 +00:00
|
|
|
|
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)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(-1f, 1f, 0), new Vector2(0, 1)),
|
|
|
|
|
|
|
|
|
|
new PositionTextureVertex(new Vector3(0f, 0f, 0), new Vector2(0, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(1f, 0f, 0), new Vector2(1, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(1f, 1f, 0), new Vector2(1, 1)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(0f, 1f, 0), new Vector2(0, 1)),
|
|
|
|
|
|
|
|
|
|
new PositionTextureVertex(new Vector3(-0.5f, -1f, 0), new Vector2(0, 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(0, 1))
|
2024-02-23 20:40:12 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Vertex
|
2024-02-07 15:27:55 +00:00
|
|
|
|
);
|
2024-02-23 20:40:12 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
indexBuffer = resourceUploader.CreateBuffer<ushort>(
|
2024-02-23 20:40:12 +00:00
|
|
|
|
[
|
2024-02-07 15:27:55 +00:00
|
|
|
|
0, 1, 2,
|
|
|
|
|
0, 2, 3,
|
2024-02-23 20:40:12 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Index
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-23 22:54:01 +00:00
|
|
|
|
originalTexture = resourceUploader.CreateTexture2DFromCompressed(
|
2024-02-23 20:40:12 +00:00
|
|
|
|
TestUtils.GetTexturePath("ravioli.png")
|
2024-02-07 15:27:55 +00:00
|
|
|
|
);
|
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
resourceUploader.Upload();
|
|
|
|
|
resourceUploader.Dispose();
|
2023-04-19 06:39:45 +00:00
|
|
|
|
|
2024-02-23 20:40:12 +00:00
|
|
|
|
// Load the texture bytes so we can compare them.
|
|
|
|
|
var pixels = ImageUtils.GetPixelDataFromFile(
|
|
|
|
|
TestUtils.GetTexturePath("ravioli.png"),
|
2023-04-19 06:39:45 +00:00
|
|
|
|
out var width,
|
|
|
|
|
out var height,
|
|
|
|
|
out var byteCount
|
|
|
|
|
);
|
2023-01-07 23:03:12 +00:00
|
|
|
|
|
2024-02-23 20:40:12 +00:00
|
|
|
|
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
2024-02-07 15:27:55 +00:00
|
|
|
|
|
2024-02-23 20:40:12 +00:00
|
|
|
|
var textureCreateInfo = new TextureCreateInfo
|
|
|
|
|
{
|
|
|
|
|
Width = originalTexture.Width,
|
|
|
|
|
Height = originalTexture.Height,
|
|
|
|
|
Depth = originalTexture.Depth,
|
|
|
|
|
IsCube = originalTexture.IsCube,
|
2024-03-01 23:03:29 +00:00
|
|
|
|
LayerCount = originalTexture.LayerCount,
|
2024-02-23 20:40:12 +00:00
|
|
|
|
LevelCount = originalTexture.LevelCount,
|
|
|
|
|
SampleCount = originalTexture.SampleCount,
|
|
|
|
|
Format = originalTexture.Format,
|
|
|
|
|
UsageFlags = originalTexture.UsageFlags
|
|
|
|
|
};
|
2024-02-07 15:27:55 +00:00
|
|
|
|
|
|
|
|
|
// Create a 1:1 copy of the texture
|
|
|
|
|
textureCopy = new Texture(GraphicsDevice, textureCreateInfo);
|
2024-02-23 20:40:12 +00:00
|
|
|
|
|
|
|
|
|
cmdbuf.BeginCopyPass();
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.CopyTextureToTexture(
|
2024-02-23 20:40:12 +00:00
|
|
|
|
originalTexture,
|
2024-03-01 23:03:29 +00:00
|
|
|
|
textureCopy,
|
2024-03-11 17:20:54 +00:00
|
|
|
|
WriteOptions.Unsafe
|
2024-02-07 15:27:55 +00:00
|
|
|
|
);
|
2024-02-23 20:40:12 +00:00
|
|
|
|
cmdbuf.EndCopyPass();
|
2024-02-07 15:27:55 +00:00
|
|
|
|
|
|
|
|
|
// Create a half-sized copy of this texture
|
|
|
|
|
textureCreateInfo.Width /= 2;
|
|
|
|
|
textureCreateInfo.Height /= 2;
|
2024-02-23 20:40:12 +00:00
|
|
|
|
textureCreateInfo.UsageFlags |= TextureUsageFlags.ColorTarget;
|
2024-02-07 15:27:55 +00:00
|
|
|
|
textureSmallCopy = new Texture(GraphicsDevice, textureCreateInfo);
|
2024-02-23 20:40:12 +00:00
|
|
|
|
|
|
|
|
|
// Render the half-size copy
|
2024-03-11 17:20:54 +00:00
|
|
|
|
cmdbuf.Blit(originalTexture, textureSmallCopy, Filter.Linear, WriteOptions.Unsafe);
|
2024-02-23 20:40:12 +00:00
|
|
|
|
|
2024-03-06 08:15:44 +00:00
|
|
|
|
var fence = GraphicsDevice.SubmitAndAcquireFence(cmdbuf);
|
|
|
|
|
GraphicsDevice.WaitForFences(fence);
|
|
|
|
|
GraphicsDevice.ReleaseFence(fence);
|
|
|
|
|
|
2024-02-23 20:40:12 +00:00
|
|
|
|
// Copy the texture to a transfer buffer
|
2024-03-11 17:20:54 +00:00
|
|
|
|
TransferBuffer compareBuffer = new TransferBuffer(GraphicsDevice, TransferUsage.Texture, byteCount);
|
2024-02-23 20:40:12 +00:00
|
|
|
|
|
2024-03-06 08:15:44 +00:00
|
|
|
|
GraphicsDevice.DownloadFromTexture(
|
|
|
|
|
textureCopy,
|
2024-02-23 20:40:12 +00:00
|
|
|
|
compareBuffer,
|
2024-03-11 17:20:54 +00:00
|
|
|
|
TransferOptions.Unsafe
|
2024-02-07 15:27:55 +00:00
|
|
|
|
);
|
2023-01-07 23:03:12 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Compare the original bytes to the copied bytes.
|
2024-02-23 20:40:12 +00:00
|
|
|
|
var copiedBytes = NativeMemory.Alloc(byteCount);
|
|
|
|
|
var copiedSpan = new System.Span<byte>(copiedBytes, (int) byteCount);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
compareBuffer.GetData(copiedSpan);
|
2023-01-07 23:03:12 +00:00
|
|
|
|
|
2024-02-23 20:40:12 +00:00
|
|
|
|
var originalSpan = new System.Span<byte>((void*) pixels, (int)byteCount);
|
2023-04-04 06:13:37 +00:00
|
|
|
|
|
|
|
|
|
if (System.MemoryExtensions.SequenceEqual(originalSpan, copiedSpan))
|
|
|
|
|
{
|
2024-03-07 07:59:34 +00:00
|
|
|
|
Logger.LogInfo("SUCCESS! Original texture bytes and the downloaded bytes match!");
|
2023-04-04 06:13:37 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-06 08:15:44 +00:00
|
|
|
|
Logger.LogError("FAIL! Original texture bytes do not match downloaded bytes!");
|
2023-04-04 06:13:37 +00:00
|
|
|
|
}
|
2023-04-19 06:39:45 +00:00
|
|
|
|
|
|
|
|
|
RefreshCS.Refresh.Refresh_Image_Free(pixels);
|
2024-03-06 22:23:06 +00:00
|
|
|
|
NativeMemory.Free(copiedBytes);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update(System.TimeSpan delta) { }
|
|
|
|
|
|
|
|
|
|
protected override void Draw(double alpha)
|
|
|
|
|
{
|
|
|
|
|
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
|
|
|
|
|
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
|
|
|
|
|
if (backbuffer != null)
|
|
|
|
|
{
|
2024-03-07 22:24:54 +00:00
|
|
|
|
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.Black));
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.BindGraphicsPipeline(pipeline);
|
|
|
|
|
cmdbuf.BindVertexBuffers(vertexBuffer);
|
|
|
|
|
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
|
|
|
|
|
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(originalTexture, sampler));
|
2024-02-23 20:40:12 +00:00
|
|
|
|
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textureCopy, sampler));
|
2024-02-23 20:40:12 +00:00
|
|
|
|
cmdbuf.DrawIndexedPrimitives(4, 0, 2);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textureSmallCopy, sampler));
|
2024-02-23 20:40:12 +00:00
|
|
|
|
cmdbuf.DrawIndexedPrimitives(8, 0, 2);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
}
|
|
|
|
|
GraphicsDevice.Submit(cmdbuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
CopyTextureGame game = new CopyTextureGame();
|
|
|
|
|
game.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-07 23:03:12 +00:00
|
|
|
|
}
|