2024-02-09 17:47:55 +00:00
|
|
|
|
using MoonWorks;
|
|
|
|
|
using MoonWorks.Graphics;
|
|
|
|
|
using MoonWorks.Math.Float;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Test
|
|
|
|
|
{
|
|
|
|
|
class RenderTexture2DGame : Game
|
|
|
|
|
{
|
|
|
|
|
private GraphicsPipeline pipeline;
|
2024-02-23 21:25:21 +00:00
|
|
|
|
private GpuBuffer vertexBuffer;
|
|
|
|
|
private GpuBuffer indexBuffer;
|
2024-02-09 17:47:55 +00:00
|
|
|
|
private Texture[] textures = new Texture[4];
|
|
|
|
|
private Sampler sampler;
|
|
|
|
|
|
2024-03-07 18:35:12 +00:00
|
|
|
|
public RenderTexture2DGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
|
2024-02-09 17:47: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.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
|
|
|
|
|
pipelineCreateInfo.VertexShaderInfo = GraphicsShaderInfo.Create(vertShaderModule, "main", 0);
|
|
|
|
|
pipelineCreateInfo.FragmentShaderInfo.SamplerBindingCount = 1;
|
|
|
|
|
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
|
|
|
|
|
|
|
|
|
// Create sampler
|
|
|
|
|
SamplerCreateInfo samplerCreateInfo = SamplerCreateInfo.PointClamp;
|
|
|
|
|
sampler = new Sampler(GraphicsDevice, samplerCreateInfo);
|
|
|
|
|
|
|
|
|
|
// Create and populate the GPU resources
|
2024-02-23 22:32:56 +00:00
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
2024-02-09 17:47:55 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
vertexBuffer = resourceUploader.CreateBuffer(
|
2024-02-23 21:25:21 +00:00
|
|
|
|
[
|
2024-02-09 17:47:55 +00:00
|
|
|
|
new PositionTextureVertex(new Vector3(-1, -1, 0), new Vector2(0, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(0, -1, 0), new Vector2(1, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(1, 1)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(-1, 0, 0), new Vector2(0, 1)),
|
|
|
|
|
|
|
|
|
|
new PositionTextureVertex(new Vector3(0, -1, 0), new Vector2(0, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(1, -1, 0), new Vector2(1, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(1, 0, 0), new Vector2(1, 1)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(0, 1)),
|
|
|
|
|
|
|
|
|
|
new PositionTextureVertex(new Vector3(-1, 0, 0), new Vector2(0, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(1, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(0, 1, 0), new Vector2(1, 1)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)),
|
|
|
|
|
|
|
|
|
|
new PositionTextureVertex(new Vector3(0, 0, 0), new Vector2(0, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(1, 0, 0), new Vector2(1, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(0, 1, 0), new Vector2(0, 1)),
|
2024-02-23 21:25:21 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Vertex
|
2024-02-09 17:47:55 +00:00
|
|
|
|
);
|
2024-02-23 21:25:21 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
indexBuffer = resourceUploader.CreateBuffer<ushort>(
|
2024-02-23 21:25:21 +00:00
|
|
|
|
[
|
2024-02-09 17:47:55 +00:00
|
|
|
|
0, 1, 2,
|
|
|
|
|
0, 2, 3,
|
2024-02-23 21:25:21 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Index
|
2024-02-09 17:47:55 +00:00
|
|
|
|
);
|
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
resourceUploader.Upload();
|
|
|
|
|
resourceUploader.Dispose();
|
2024-02-23 21:25:21 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < textures.Length; i += 1)
|
|
|
|
|
{
|
|
|
|
|
textures[i] = Texture.CreateTexture2D(
|
|
|
|
|
GraphicsDevice,
|
|
|
|
|
MainWindow.Width / 4,
|
|
|
|
|
MainWindow.Height / 4,
|
|
|
|
|
TextureFormat.R8G8B8A8,
|
|
|
|
|
TextureUsageFlags.ColorTarget | TextureUsageFlags.Sampler
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-02-09 17:47: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(textures[0], WriteOptions.Cycle, Color.Red));
|
2024-02-09 17:47:55 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
|
2024-03-07 22:24:54 +00:00
|
|
|
|
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[1], WriteOptions.Cycle, Color.Blue));
|
2024-02-09 17:47:55 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
|
2024-03-07 22:24:54 +00:00
|
|
|
|
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[2], WriteOptions.Cycle, Color.Green));
|
2024-02-09 17:47:55 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
|
2024-03-07 22:24:54 +00:00
|
|
|
|
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(textures[3], WriteOptions.Cycle, Color.Yellow));
|
2024-02-09 17:47:55 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
|
2024-03-07 22:24:54 +00:00
|
|
|
|
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.Cycle, Color.Black));
|
2024-02-09 17:47:55 +00:00
|
|
|
|
cmdbuf.BindGraphicsPipeline(pipeline);
|
|
|
|
|
cmdbuf.BindVertexBuffers(vertexBuffer);
|
|
|
|
|
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
|
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < textures.Length; i += 1)
|
|
|
|
|
{
|
|
|
|
|
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(textures[i], sampler));
|
2024-02-23 21:25:21 +00:00
|
|
|
|
cmdbuf.DrawIndexedPrimitives(4 * i, 0, 2);
|
2024-02-09 17:47:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
}
|
|
|
|
|
GraphicsDevice.Submit(cmdbuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
RenderTexture2DGame game = new RenderTexture2DGame();
|
|
|
|
|
game.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|