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;
|
|
|
|
|
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-02-07 15:27:55 +00:00
|
|
|
|
public RenderTexture3DGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), 60, true)
|
|
|
|
|
{
|
|
|
|
|
// 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
|
2024-02-23 22:32:56 +00:00
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
2024-02-23 21:27:36 +00:00
|
|
|
|
|
2024-02-23 22:32:56 +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
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
indexBuffer = resourceUploader.CreateBuffer<ushort>(
|
2024-02-23 21:27:36 +00:00
|
|
|
|
[
|
|
|
|
|
0, 1, 2,
|
|
|
|
|
0, 2, 3,
|
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Index
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-23 22:32:56 +00:00
|
|
|
|
resourceUploader.Upload();
|
|
|
|
|
resourceUploader.Dispose();
|
2024-02-23 21:27:36 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
rt = Texture.CreateTexture3D(
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
Texture = rt,
|
|
|
|
|
ClearColor = colors[i],
|
|
|
|
|
Depth = i,
|
|
|
|
|
Layer = 0,
|
|
|
|
|
Level = 0,
|
|
|
|
|
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;
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, Color.Black));
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
RenderTexture3DGame game = new RenderTexture3DGame();
|
|
|
|
|
game.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-23 00:51:09 +00:00
|
|
|
|
}
|