MoonWorksGraphicsTests/Examples/VertexSamplerGame.cs

80 lines
2.6 KiB
C#
Raw Normal View History

2024-03-01 23:03:29 +00:00
using System;
using MoonWorks;
2023-01-22 21:49:35 +00:00
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
2024-02-07 15:27:55 +00:00
class VertexSamplerGame : Game
{
private GraphicsPipeline pipeline;
private GpuBuffer vertexBuffer;
2024-02-07 15:27:55 +00:00
private Texture texture;
private Sampler sampler;
2023-01-22 21:49:35 +00:00
2024-03-07 18:35:12 +00:00
public VertexSamplerGame() : 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("PositionSampler.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag"));
2023-01-22 21:49:35 +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.VertexShaderInfo.SamplerBindingCount = 1;
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
2023-01-22 21:49:35 +00:00
2024-02-07 15:27:55 +00:00
// Create and populate the GPU resources
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
2023-01-22 21:49:35 +00:00
var resourceUploader = new ResourceUploader(GraphicsDevice);
vertexBuffer = resourceUploader.CreateBuffer(
[
2024-02-07 15:27:55 +00:00
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 0)),
new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(0.334f, 0)),
new PositionTextureVertex(new Vector3(0, -1, 0), new Vector2(0.667f, 0)),
],
BufferUsageFlags.Vertex
2024-02-07 15:27:55 +00:00
);
2024-03-01 23:03:29 +00:00
texture = resourceUploader.CreateTexture2D(
new Span<Color>([Color.Yellow, Color.Indigo, Color.HotPink]),
3,
1
);
resourceUploader.Upload();
resourceUploader.Dispose();
2024-02-07 15:27:55 +00:00
}
2023-01-22 21:49:35 +00:00
2024-02-07 15:27:55 +00:00
protected override void Update(System.TimeSpan delta) { }
2023-01-22 21:49:35 +00:00
2024-02-07 15:27:55 +00:00
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.BindVertexSamplers(new TextureSamplerBinding(texture, sampler));
cmdbuf.DrawPrimitives(0, 1);
2024-02-07 15:27:55 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
2023-01-22 21:49:35 +00:00
2024-02-07 15:27:55 +00:00
public static void Main(string[] args)
{
VertexSamplerGame p = new VertexSamplerGame();
p.Run();
}
}
2023-01-22 21:49:35 +00:00
}