MoonWorksGraphicsTests/Texture3D/Texture3DGame.cs

161 lines
4.3 KiB
C#
Raw Normal View History

2024-02-23 22:05:05 +00:00
using MoonWorks.Graphics;
2023-01-22 21:21:31 +00:00
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
2024-02-07 15:27:55 +00:00
class Texture3DGame : Game
{
private GraphicsPipeline pipeline;
2024-02-23 22:05:05 +00:00
private GpuBuffer vertexBuffer;
private GpuBuffer indexBuffer;
2024-02-07 15:27:55 +00:00
private Texture texture;
private Sampler sampler;
private int currentDepth = 0;
struct FragUniform
{
public float Depth;
public FragUniform(float depth)
{
Depth = depth;
}
}
2024-03-07 18:35:12 +00:00
public Texture3DGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.PreferredBackends, 60, true)
2024-02-07 15:27:55 +00:00
{
Logger.LogInfo("Press Left and Right to cycle between depth slices");
// Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad3D.frag"));
// 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);
// Create samplers
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.PointClamp);
// Create and populate the GPU resources
var resourceUploader = new ResourceUploader(GraphicsDevice);
2024-02-07 15:27:55 +00:00
vertexBuffer = resourceUploader.CreateBuffer(
2024-02-23 22:05:05 +00:00
[
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(1, 0)),
new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)),
2024-02-23 22:05:05 +00:00
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1))
],
BufferUsageFlags.Vertex
2024-02-07 15:27:55 +00:00
);
2024-02-23 22:05:05 +00:00
indexBuffer = resourceUploader.CreateBuffer<ushort>(
2024-02-23 22:05:05 +00:00
[
2024-02-07 15:27:55 +00:00
0, 1, 2,
0, 2, 3,
2024-02-23 22:05:05 +00:00
],
BufferUsageFlags.Index
2024-02-07 15:27:55 +00:00
);
2024-03-01 23:03:29 +00:00
texture = Texture.CreateTexture3D(
GraphicsDevice,
16,
16,
7,
TextureFormat.R8G8B8A8,
TextureUsageFlags.Sampler
);
2024-02-23 22:05:05 +00:00
2024-02-07 15:27:55 +00:00
// Load each depth subimage of the 3D texture
for (uint i = 0; i < texture.Depth; i += 1)
{
2024-03-01 23:03:29 +00:00
var region = new TextureRegion
2024-02-23 22:05:05 +00:00
{
2024-03-01 23:03:29 +00:00
TextureSlice = new TextureSlice
{
Texture = texture,
MipLevel = 0,
Layer = 0
},
2024-02-23 22:05:05 +00:00
X = 0,
Y = 0,
Z = i,
Width = texture.Width,
Height = texture.Height,
Depth = 1
};
resourceUploader.SetTextureDataFromCompressed(
2024-03-01 23:03:29 +00:00
region,
2023-04-19 06:39:45 +00:00
TestUtils.GetTexturePath($"tex3d_{i}.png")
);
2024-02-07 15:27:55 +00:00
}
resourceUploader.Upload();
resourceUploader.Dispose();
2024-02-07 15:27:55 +00:00
}
protected override void Update(System.TimeSpan delta)
{
int prevDepth = currentDepth;
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Left))
{
currentDepth -= 1;
if (currentDepth < 0)
{
currentDepth = (int) texture.Depth - 1;
}
}
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Right))
{
currentDepth += 1;
if (currentDepth >= texture.Depth)
{
currentDepth = 0;
}
}
if (prevDepth != currentDepth)
{
Logger.LogInfo("Setting depth to: " + currentDepth);
}
}
protected override void Draw(double alpha)
{
FragUniform fragUniform = new FragUniform((float)currentDepth / texture.Depth + 0.01f);
2024-02-07 15:27:55 +00:00
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
if (backbuffer != null)
{
2024-03-01 23:03:29 +00:00
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.SafeDiscard, 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(texture, sampler));
2024-02-23 22:05:05 +00:00
cmdbuf.PushFragmentShaderUniforms(fragUniform);
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
2024-02-07 15:27:55 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
public static void Main(string[] args)
{
Texture3DGame game = new Texture3DGame();
game.Run();
}
}
2023-01-22 21:21:31 +00:00
}