MoonWorksGraphicsTests/Examples/RenderTexture2DArrayGame.cs

140 lines
3.9 KiB
C#
Raw Normal View History

2024-03-01 23:03:29 +00:00
using System;
using MoonWorks.Graphics;
2023-01-23 00:51:09 +00:00
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
2024-03-01 23:03:29 +00:00
class RenderTexture2DArrayGame : Game
2024-02-07 15:27:55 +00:00
{
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;
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 RenderTexture2DArrayGame() : 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"));
2024-03-01 23:03:29 +00:00
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad2DArray.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
2024-03-01 23:03:29 +00:00
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointWrap);
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-02-07 15:27:55 +00:00
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
2023-01-23 00:51:09 +00:00
2024-02-07 15:27:55 +00:00
// Clear each depth slice of the RT to a different color
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-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;
2024-03-01 23:03:29 +00:00
t %= 3;
FragUniform fragUniform = new FragUniform(MathF.Floor(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);
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(rt, 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)
{
2024-03-01 23:03:29 +00:00
RenderTexture2DArrayGame game = new RenderTexture2DArrayGame();
2024-02-07 15:27:55 +00:00
game.Run();
}
}
2023-01-23 00:51:09 +00:00
}