2022-11-09 19:54:42 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using MoonWorks.Graphics;
|
|
|
|
|
using MoonWorks.Math.Float;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Test
|
|
|
|
|
{
|
|
|
|
|
class TexturedAnimatedQuadGame : Game
|
|
|
|
|
{
|
|
|
|
|
private GraphicsPipeline pipeline;
|
2024-02-23 22:37:45 +00:00
|
|
|
|
private GpuBuffer vertexBuffer;
|
|
|
|
|
private GpuBuffer indexBuffer;
|
2022-11-09 19:54:42 +00:00
|
|
|
|
private Texture texture;
|
|
|
|
|
private Sampler sampler;
|
|
|
|
|
|
|
|
|
|
private float t;
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
private struct FragmentUniforms
|
|
|
|
|
{
|
|
|
|
|
public Vector4 MultiplyColor;
|
|
|
|
|
|
|
|
|
|
public FragmentUniforms(Vector4 multiplyColor)
|
|
|
|
|
{
|
|
|
|
|
MultiplyColor = multiplyColor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 18:35:12 +00:00
|
|
|
|
public TexturedAnimatedQuadGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
|
2022-11-09 19:54:42 +00:00
|
|
|
|
{
|
|
|
|
|
// Load the shaders
|
2023-07-01 17:41:42 +00:00
|
|
|
|
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadWithMatrix.vert"));
|
|
|
|
|
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuadWithMultiplyColor.frag"));
|
2022-11-09 19:54:42 +00:00
|
|
|
|
|
|
|
|
|
// Create the graphics pipeline
|
2022-11-12 16:42:12 +00:00
|
|
|
|
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
|
2022-11-17 20:33:58 +00:00
|
|
|
|
MainWindow.SwapchainFormat,
|
2022-11-12 16:42:12 +00:00
|
|
|
|
vertShaderModule,
|
|
|
|
|
fragShaderModule
|
|
|
|
|
);
|
2023-01-04 20:44:23 +00:00
|
|
|
|
pipelineCreateInfo.AttachmentInfo.ColorAttachmentDescriptions[0].BlendState = ColorAttachmentBlendState.AlphaBlend;
|
2023-01-06 20:38:30 +00:00
|
|
|
|
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionTextureVertex>();
|
2023-01-31 03:39:13 +00:00
|
|
|
|
pipelineCreateInfo.VertexShaderInfo = GraphicsShaderInfo.Create<TransformVertexUniform>(vertShaderModule, "main", 0);
|
2022-11-09 19:54:42 +00:00
|
|
|
|
pipelineCreateInfo.FragmentShaderInfo = GraphicsShaderInfo.Create<FragmentUniforms>(fragShaderModule, "main", 1);
|
|
|
|
|
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
|
|
|
|
|
|
|
|
|
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
|
|
|
|
|
|
2024-02-23 22:37:45 +00:00
|
|
|
|
// Create and populate the GPU resources
|
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
|
|
|
|
|
|
|
|
|
vertexBuffer = resourceUploader.CreateBuffer(
|
|
|
|
|
[
|
2023-01-04 20:44:23 +00:00
|
|
|
|
new PositionTextureVertex(new Vector3(-0.5f, -0.5f, 0), new Vector2(0, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(0.5f, -0.5f, 0), new Vector2(1, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(0.5f, 0.5f, 0), new Vector2(1, 1)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(-0.5f, 0.5f, 0), new Vector2(0, 1)),
|
2024-02-23 22:37:45 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Vertex
|
2022-11-09 19:54:42 +00:00
|
|
|
|
);
|
2024-02-23 22:37:45 +00:00
|
|
|
|
|
|
|
|
|
indexBuffer = resourceUploader.CreateBuffer<ushort>(
|
|
|
|
|
[
|
2022-11-09 19:54:42 +00:00
|
|
|
|
0, 1, 2,
|
|
|
|
|
0, 2, 3,
|
2024-02-23 22:37:45 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Index
|
2022-11-09 19:54:42 +00:00
|
|
|
|
);
|
2024-02-23 22:37:45 +00:00
|
|
|
|
|
2024-02-23 22:54:01 +00:00
|
|
|
|
texture = resourceUploader.CreateTexture2DFromCompressed(TestUtils.GetTexturePath("ravioli.png"));
|
2024-02-23 22:37:45 +00:00
|
|
|
|
|
|
|
|
|
resourceUploader.Upload();
|
|
|
|
|
resourceUploader.Dispose();
|
2022-11-09 19:54:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update(System.TimeSpan delta)
|
|
|
|
|
{
|
|
|
|
|
t += (float) delta.TotalSeconds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Draw(double alpha)
|
|
|
|
|
{
|
2023-01-31 03:39:13 +00:00
|
|
|
|
TransformVertexUniform vertUniforms;
|
2023-01-04 20:44:23 +00:00
|
|
|
|
FragmentUniforms fragUniforms;
|
2022-11-09 19:54:42 +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));
|
2022-11-09 19:54:42 +00:00
|
|
|
|
cmdbuf.BindGraphicsPipeline(pipeline);
|
|
|
|
|
cmdbuf.BindVertexBuffers(vertexBuffer);
|
|
|
|
|
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
|
|
|
|
|
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(texture, sampler));
|
2023-01-04 20:44:23 +00:00
|
|
|
|
|
|
|
|
|
// Top-left
|
2023-01-31 03:39:13 +00:00
|
|
|
|
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ(t) * Matrix4x4.CreateTranslation(new Vector3(-0.5f, -0.5f, 0)));
|
2023-01-04 20:44:23 +00:00
|
|
|
|
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Sin(t) * 0.5f, 1f, 1f));
|
2024-02-23 22:37:45 +00:00
|
|
|
|
cmdbuf.PushVertexShaderUniforms(vertUniforms);
|
|
|
|
|
cmdbuf.PushFragmentShaderUniforms(fragUniforms);
|
|
|
|
|
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
2023-01-04 20:44:23 +00:00
|
|
|
|
|
|
|
|
|
// Top-right
|
2023-01-31 03:39:13 +00:00
|
|
|
|
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ((2 * System.MathF.PI) - t) * Matrix4x4.CreateTranslation(new Vector3(0.5f, -0.5f, 0)));
|
2023-01-04 20:44:23 +00:00
|
|
|
|
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Cos(t) * 0.5f, 1f, 1f));
|
2024-02-23 22:37:45 +00:00
|
|
|
|
cmdbuf.PushVertexShaderUniforms(vertUniforms);
|
|
|
|
|
cmdbuf.PushFragmentShaderUniforms(fragUniforms);
|
|
|
|
|
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
2023-01-04 20:44:23 +00:00
|
|
|
|
|
|
|
|
|
// Bottom-left
|
2023-01-31 03:39:13 +00:00
|
|
|
|
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ(t) * Matrix4x4.CreateTranslation(new Vector3(-0.5f, 0.5f, 0)));
|
2023-01-04 20:44:23 +00:00
|
|
|
|
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Sin(t) * 0.2f, 1f, 1f));
|
2024-02-23 22:37:45 +00:00
|
|
|
|
cmdbuf.PushVertexShaderUniforms(vertUniforms);
|
|
|
|
|
cmdbuf.PushFragmentShaderUniforms(fragUniforms);
|
|
|
|
|
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
2023-01-04 20:44:23 +00:00
|
|
|
|
|
|
|
|
|
// Bottom-right
|
2023-01-31 03:39:13 +00:00
|
|
|
|
vertUniforms = new TransformVertexUniform(Matrix4x4.CreateRotationZ(t) * Matrix4x4.CreateTranslation(new Vector3(0.5f, 0.5f, 0)));
|
2023-01-04 20:44:23 +00:00
|
|
|
|
fragUniforms = new FragmentUniforms(new Vector4(1f, 0.5f + System.MathF.Cos(t) * 1f, 1f, 1f));
|
2024-02-23 22:37:45 +00:00
|
|
|
|
cmdbuf.PushVertexShaderUniforms(vertUniforms);
|
|
|
|
|
cmdbuf.PushFragmentShaderUniforms(fragUniforms);
|
|
|
|
|
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
2023-01-04 20:44:23 +00:00
|
|
|
|
|
2022-11-09 19:54:42 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
}
|
|
|
|
|
GraphicsDevice.Submit(cmdbuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
TexturedAnimatedQuadGame game = new TexturedAnimatedQuadGame();
|
|
|
|
|
game.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|