MoonWorksGraphicsTests/Examples/Texture3DCopy.cs

192 lines
4.9 KiB
C#
Raw Normal View History

2024-02-23 21:27:36 +00:00
using MoonWorks.Graphics;
2023-01-23 00:51:09 +00:00
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
2024-02-07 15:27:55 +00:00
class RenderTexture3DGame : Game
{
private GraphicsPipeline pipeline;
2024-02-23 21:27:36 +00:00
private GpuBuffer vertexBuffer;
private GpuBuffer indexBuffer;
2024-02-07 15:27:55 +00:00
private Texture rt;
2024-03-01 23:03:29 +00:00
private Texture texture3D;
2024-02-07 15:27:55 +00:00
private Sampler sampler;
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
private float t;
private Color[] colors = new Color[]
{
Color.Red,
Color.Green,
Color.Blue,
};
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
struct FragUniform
{
public float Depth;
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
public FragUniform(float depth)
{
Depth = depth;
}
}
2023-01-23 00:51:09 +00:00
2024-03-07 18:35:12 +00:00
public RenderTexture3DGame() : 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("TexturedQuad3D.frag"));
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
// Create the graphics pipeline
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
MainWindow.SwapchainFormat,
vertShaderModule,
fragShaderModule
);
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
pipelineCreateInfo.FragmentShaderInfo = GraphicsShaderInfo.Create<FragUniform>(fragShaderModule, "main", 1);
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
// Create samplers
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.LinearWrap);
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
// Create and populate the GPU resources
var resourceUploader = new ResourceUploader(GraphicsDevice);
2024-02-23 21:27:36 +00:00
vertexBuffer = resourceUploader.CreateBuffer(
2024-02-23 21:27:36 +00:00
[
new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)),
new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(1, 0)),
new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)),
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)),
],
BufferUsageFlags.Vertex
);
indexBuffer = resourceUploader.CreateBuffer<ushort>(
2024-02-23 21:27:36 +00:00
[
0, 1, 2,
0, 2, 3,
],
BufferUsageFlags.Index
);
resourceUploader.Upload();
resourceUploader.Dispose();
2024-02-23 21:27:36 +00:00
2024-03-01 23:03:29 +00:00
rt = Texture.CreateTexture2DArray(
2024-02-07 15:27:55 +00:00
GraphicsDevice,
16,
16,
(uint) colors.Length,
TextureFormat.R8G8B8A8,
TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler
);
2023-01-23 00:51:09 +00:00
2024-03-01 23:03:29 +00:00
texture3D = new Texture(GraphicsDevice, new TextureCreateInfo
{
Width = 16,
Height = 16,
Depth = 3,
IsCube = false,
LayerCount = 1,
LevelCount = 1,
SampleCount = SampleCount.One,
Format = TextureFormat.R8G8B8A8,
UsageFlags = TextureUsageFlags.Sampler
});
2024-02-07 15:27:55 +00:00
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
2023-01-23 00:51:09 +00:00
2024-03-01 23:03:29 +00:00
// Clear each layer slice of the RT to a different color
2024-02-07 15:27:55 +00:00
for (uint i = 0; i < colors.Length; i += 1)
{
ColorAttachmentInfo attachmentInfo = new ColorAttachmentInfo
{
2024-03-01 23:03:29 +00:00
TextureSlice = new TextureSlice
{
Texture = rt,
Layer = i,
MipLevel = 0
},
2024-02-07 15:27:55 +00:00
ClearColor = colors[i],
LoadOp = LoadOp.Clear,
StoreOp = StoreOp.Store
};
cmdbuf.BeginRenderPass(attachmentInfo);
cmdbuf.EndRenderPass();
}
2023-01-23 00:51:09 +00:00
2024-03-01 23:03:29 +00:00
// Copy each layer slice to a different 3D depth
cmdbuf.BeginCopyPass();
for (var i = 0; i < 3; i += 1)
{
cmdbuf.CopyTextureToTexture(
new TextureRegion
{
TextureSlice = new TextureSlice
{
Texture = rt,
Layer = (uint) i,
MipLevel = 0
},
X = 0,
Y = 0,
Z = 0,
Width = 16,
Height = 16,
Depth = 1
},
new TextureRegion
{
TextureSlice = new TextureSlice
{
Texture = texture3D,
Layer = 0,
MipLevel = 0
},
X = 0,
Y = 0,
Z = (uint) i,
Width = 16,
Height = 16,
Depth = 1
},
2024-03-11 17:20:54 +00:00
WriteOptions.Unsafe
2024-03-01 23:03:29 +00:00
);
}
cmdbuf.EndCopyPass();
2024-02-07 15:27:55 +00:00
GraphicsDevice.Submit(cmdbuf);
}
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
protected override void Update(System.TimeSpan delta) { }
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
protected override void Draw(double alpha)
{
t += 0.01f;
FragUniform fragUniform = new FragUniform(t);
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
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);
2024-03-01 23:03:29 +00:00
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture3D, sampler));
2024-02-23 21:27:36 +00:00
cmdbuf.PushFragmentShaderUniforms(fragUniform);
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
2024-02-07 15:27:55 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
public static void Main(string[] args)
{
RenderTexture3DGame game = new RenderTexture3DGame();
game.Run();
}
}
2023-01-23 00:51:09 +00:00
}