2023-01-22 01:59:48 +00:00
|
|
|
|
using MoonWorks.Math.Float;
|
|
|
|
|
using MoonWorks.Graphics;
|
|
|
|
|
using MoonWorks.Video;
|
|
|
|
|
|
|
|
|
|
namespace MoonWorks.Test
|
|
|
|
|
{
|
2024-02-07 15:27:55 +00:00
|
|
|
|
class VideoPlayerGame : Game
|
|
|
|
|
{
|
|
|
|
|
private GraphicsPipeline pipeline;
|
|
|
|
|
private Sampler sampler;
|
2024-02-23 23:29:03 +00:00
|
|
|
|
private GpuBuffer vertexBuffer;
|
|
|
|
|
private GpuBuffer indexBuffer;
|
2023-01-22 01:59:48 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
private Video.VideoAV1 video;
|
|
|
|
|
private VideoPlayer videoPlayer;
|
2023-01-22 01:59:48 +00:00
|
|
|
|
|
2024-03-07 18:35:12 +00:00
|
|
|
|
public VideoPlayerGame() : 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("TexturedQuad.vert"));
|
|
|
|
|
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("TexturedQuad.frag"));
|
2023-01-22 01:59:48 +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.SamplerBindingCount = 1;
|
|
|
|
|
pipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
|
2023-01-22 01:59:48 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Create the sampler
|
|
|
|
|
sampler = new Sampler(GraphicsDevice, SamplerCreateInfo.LinearClamp);
|
2023-01-22 01:59:48 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Create and populate the GPU resources
|
2024-02-23 23:29:03 +00:00
|
|
|
|
var resourceUploader = new ResourceUploader(GraphicsDevice);
|
2023-01-22 01:59:48 +00:00
|
|
|
|
|
2024-02-23 23:29:03 +00:00
|
|
|
|
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(1, 0)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(1, 1, 0), new Vector2(1, 1)),
|
|
|
|
|
new PositionTextureVertex(new Vector3(-1, 1, 0), new Vector2(0, 1)),
|
2024-02-23 23:29:03 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Vertex
|
2024-02-07 15:27:55 +00:00
|
|
|
|
);
|
2024-02-23 23:29:03 +00:00
|
|
|
|
|
|
|
|
|
indexBuffer = resourceUploader.CreateBuffer<ushort>(
|
|
|
|
|
[
|
2024-02-07 15:27:55 +00:00
|
|
|
|
0, 1, 2,
|
|
|
|
|
0, 2, 3,
|
2024-02-23 23:29:03 +00:00
|
|
|
|
],
|
|
|
|
|
BufferUsageFlags.Index
|
2024-02-07 15:27:55 +00:00
|
|
|
|
);
|
2024-02-23 23:29:03 +00:00
|
|
|
|
|
|
|
|
|
resourceUploader.Upload();
|
|
|
|
|
resourceUploader.Dispose();
|
2023-01-22 01:59:48 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Load the video
|
|
|
|
|
video = new VideoAV1(GraphicsDevice, TestUtils.GetVideoPath("hello.obu"), 25);
|
2023-01-22 01:59:48 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
// Play the video
|
|
|
|
|
videoPlayer = new VideoPlayer(GraphicsDevice);
|
|
|
|
|
videoPlayer.Load(video);
|
|
|
|
|
videoPlayer.Loop = true;
|
|
|
|
|
videoPlayer.Play();
|
|
|
|
|
}
|
2023-01-22 01:59:48 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
protected override void Update(System.TimeSpan delta)
|
|
|
|
|
{
|
|
|
|
|
videoPlayer.Render();
|
|
|
|
|
}
|
2023-01-22 01:59:48 +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.CornflowerBlue));
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.BindGraphicsPipeline(pipeline);
|
|
|
|
|
cmdbuf.BindVertexBuffers(vertexBuffer);
|
|
|
|
|
cmdbuf.BindIndexBuffer(indexBuffer, IndexElementSize.Sixteen);
|
|
|
|
|
cmdbuf.BindFragmentSamplers(new TextureSamplerBinding(videoPlayer.RenderTexture, sampler));
|
2024-02-23 23:29:03 +00:00
|
|
|
|
cmdbuf.DrawIndexedPrimitives(0, 0, 2);
|
2024-02-07 15:27:55 +00:00
|
|
|
|
cmdbuf.EndRenderPass();
|
|
|
|
|
}
|
|
|
|
|
GraphicsDevice.Submit(cmdbuf);
|
|
|
|
|
}
|
2023-01-22 01:59:48 +00:00
|
|
|
|
|
2024-02-07 15:27:55 +00:00
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
VideoPlayerGame game = new VideoPlayerGame();
|
|
|
|
|
game.Run();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-22 01:59:48 +00:00
|
|
|
|
}
|