From b29341eca30057d1382b9c61803e1f7bea47830f Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 23 Feb 2024 15:28:34 -0800 Subject: [PATCH] VideoPlayer fixes --- src/Video/VideoPlayer.cs | 51 +++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/Video/VideoPlayer.cs b/src/Video/VideoPlayer.cs index 381b49e..e32c018 100644 --- a/src/Video/VideoPlayer.cs +++ b/src/Video/VideoPlayer.cs @@ -22,7 +22,6 @@ namespace MoonWorks.Video private Task ResetStreamATask; private Task ResetStreamBTask; - private GraphicsDevice GraphicsDevice; private Texture yTexture = null; private Texture uTexture = null; private Texture vTexture = null; @@ -38,8 +37,6 @@ namespace MoonWorks.Video public VideoPlayer(GraphicsDevice device) : base(device) { - GraphicsDevice = device; - LinearSampler = new Sampler(device, SamplerCreateInfo.LinearClamp); timer = new Stopwatch(); @@ -55,63 +52,48 @@ namespace MoonWorks.Video { Stop(); - var needNewTransferBuffer = TransferBuffer == null; - if (RenderTexture == null) { - RenderTexture = CreateRenderTexture(GraphicsDevice, video.Width, video.Height); + RenderTexture = CreateRenderTexture(Device, video.Width, video.Height); } if (yTexture == null) { - yTexture = CreateSubTexture(GraphicsDevice, video.Width, video.Height); + yTexture = CreateSubTexture(Device, video.Width, video.Height); } if (uTexture == null) { - uTexture = CreateSubTexture(GraphicsDevice, video.UVWidth, video.UVHeight); + uTexture = CreateSubTexture(Device, video.UVWidth, video.UVHeight); } if (vTexture == null) { - vTexture = CreateSubTexture(GraphicsDevice, video.UVWidth, video.UVHeight); + vTexture = CreateSubTexture(Device, video.UVWidth, video.UVHeight); } if (video.Width != RenderTexture.Width || video.Height != RenderTexture.Height) { RenderTexture.Dispose(); - RenderTexture = CreateRenderTexture(GraphicsDevice, video.Width, video.Height); + RenderTexture = CreateRenderTexture(Device, video.Width, video.Height); } if (video.Width != yTexture.Width || video.Height != yTexture.Height) { yTexture.Dispose(); - yTexture = CreateSubTexture(GraphicsDevice, video.Width, video.Height); - needNewTransferBuffer = true; + yTexture = CreateSubTexture(Device, video.Width, video.Height); } if (video.UVWidth != uTexture.Width || video.UVHeight != uTexture.Height) { uTexture.Dispose(); - uTexture = CreateSubTexture(GraphicsDevice, video.UVWidth, video.UVHeight); - needNewTransferBuffer = true; + uTexture = CreateSubTexture(Device, video.UVWidth, video.UVHeight); } if (video.UVWidth != vTexture.Width || video.UVHeight != vTexture.Height) { vTexture.Dispose(); - vTexture = CreateSubTexture(GraphicsDevice, video.UVWidth, video.UVHeight); - needNewTransferBuffer = true; - } - - if (needNewTransferBuffer) - { - if (TransferBuffer != null) - { - TransferBuffer.Dispose(); - } - - TransferBuffer = new TransferBuffer(Device, yTexture.Size + uTexture.Size + vTexture.Size); + vTexture = CreateSubTexture(Device, video.UVWidth, video.UVHeight); } Video = video; @@ -250,16 +232,23 @@ namespace MoonWorks.Video { lock (CurrentStream) { - var commandBuffer = GraphicsDevice.AcquireCommandBuffer(); + var commandBuffer = Device.AcquireCommandBuffer(); var ySpan = new Span((void*) CurrentStream.yDataHandle, (int) CurrentStream.yDataLength); var uSpan = new Span((void*) CurrentStream.uDataHandle, (int) CurrentStream.uvDataLength); var vSpan = new Span((void*) CurrentStream.vDataHandle, (int) CurrentStream.uvDataLength); - TransferBuffer.SetData(ySpan, SetDataOptions.Discard); + if (TransferBuffer == null || TransferBuffer.Size < ySpan.Length + uSpan.Length + vSpan.Length) + { + TransferBuffer?.Dispose(); + TransferBuffer = new TransferBuffer(Device, (uint) (ySpan.Length + uSpan.Length + vSpan.Length)); + } + TransferBuffer.SetData(ySpan, 0, SetDataOptions.Discard); TransferBuffer.SetData(uSpan, (uint) ySpan.Length, SetDataOptions.Overwrite); TransferBuffer.SetData(vSpan, (uint) (ySpan.Length + uSpan.Length), SetDataOptions.Overwrite); + commandBuffer.BeginCopyPass(); + commandBuffer.UploadToTexture( TransferBuffer, yTexture, @@ -292,11 +281,13 @@ namespace MoonWorks.Video } ); + commandBuffer.EndCopyPass(); + commandBuffer.BeginRenderPass( new ColorAttachmentInfo(RenderTexture, Color.Black) ); - commandBuffer.BindGraphicsPipeline(GraphicsDevice.VideoPipeline); + commandBuffer.BindGraphicsPipeline(Device.VideoPipeline); commandBuffer.BindFragmentSamplers( new TextureSamplerBinding(yTexture, LinearSampler), new TextureSamplerBinding(uTexture, LinearSampler), @@ -307,7 +298,7 @@ namespace MoonWorks.Video commandBuffer.EndRenderPass(); - GraphicsDevice.Submit(commandBuffer); + Device.Submit(commandBuffer); } }