VideoPlayer fixes

what_if_no_video_threads
cosmonaut 2024-02-23 15:28:34 -08:00
parent 22bcd2e471
commit b29341eca3
1 changed files with 21 additions and 30 deletions

View File

@ -22,7 +22,6 @@ namespace MoonWorks.Video
private Task ResetStreamATask; private Task ResetStreamATask;
private Task ResetStreamBTask; private Task ResetStreamBTask;
private GraphicsDevice GraphicsDevice;
private Texture yTexture = null; private Texture yTexture = null;
private Texture uTexture = null; private Texture uTexture = null;
private Texture vTexture = null; private Texture vTexture = null;
@ -38,8 +37,6 @@ namespace MoonWorks.Video
public VideoPlayer(GraphicsDevice device) : base(device) public VideoPlayer(GraphicsDevice device) : base(device)
{ {
GraphicsDevice = device;
LinearSampler = new Sampler(device, SamplerCreateInfo.LinearClamp); LinearSampler = new Sampler(device, SamplerCreateInfo.LinearClamp);
timer = new Stopwatch(); timer = new Stopwatch();
@ -55,63 +52,48 @@ namespace MoonWorks.Video
{ {
Stop(); Stop();
var needNewTransferBuffer = TransferBuffer == null;
if (RenderTexture == null) if (RenderTexture == null)
{ {
RenderTexture = CreateRenderTexture(GraphicsDevice, video.Width, video.Height); RenderTexture = CreateRenderTexture(Device, video.Width, video.Height);
} }
if (yTexture == null) if (yTexture == null)
{ {
yTexture = CreateSubTexture(GraphicsDevice, video.Width, video.Height); yTexture = CreateSubTexture(Device, video.Width, video.Height);
} }
if (uTexture == null) if (uTexture == null)
{ {
uTexture = CreateSubTexture(GraphicsDevice, video.UVWidth, video.UVHeight); uTexture = CreateSubTexture(Device, video.UVWidth, video.UVHeight);
} }
if (vTexture == null) 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) if (video.Width != RenderTexture.Width || video.Height != RenderTexture.Height)
{ {
RenderTexture.Dispose(); 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) if (video.Width != yTexture.Width || video.Height != yTexture.Height)
{ {
yTexture.Dispose(); yTexture.Dispose();
yTexture = CreateSubTexture(GraphicsDevice, video.Width, video.Height); yTexture = CreateSubTexture(Device, video.Width, video.Height);
needNewTransferBuffer = true;
} }
if (video.UVWidth != uTexture.Width || video.UVHeight != uTexture.Height) if (video.UVWidth != uTexture.Width || video.UVHeight != uTexture.Height)
{ {
uTexture.Dispose(); uTexture.Dispose();
uTexture = CreateSubTexture(GraphicsDevice, video.UVWidth, video.UVHeight); uTexture = CreateSubTexture(Device, video.UVWidth, video.UVHeight);
needNewTransferBuffer = true;
} }
if (video.UVWidth != vTexture.Width || video.UVHeight != vTexture.Height) if (video.UVWidth != vTexture.Width || video.UVHeight != vTexture.Height)
{ {
vTexture.Dispose(); vTexture.Dispose();
vTexture = CreateSubTexture(GraphicsDevice, video.UVWidth, video.UVHeight); vTexture = CreateSubTexture(Device, video.UVWidth, video.UVHeight);
needNewTransferBuffer = true;
}
if (needNewTransferBuffer)
{
if (TransferBuffer != null)
{
TransferBuffer.Dispose();
}
TransferBuffer = new TransferBuffer(Device, yTexture.Size + uTexture.Size + vTexture.Size);
} }
Video = video; Video = video;
@ -250,16 +232,23 @@ namespace MoonWorks.Video
{ {
lock (CurrentStream) lock (CurrentStream)
{ {
var commandBuffer = GraphicsDevice.AcquireCommandBuffer(); var commandBuffer = Device.AcquireCommandBuffer();
var ySpan = new Span<byte>((void*) CurrentStream.yDataHandle, (int) CurrentStream.yDataLength); var ySpan = new Span<byte>((void*) CurrentStream.yDataHandle, (int) CurrentStream.yDataLength);
var uSpan = new Span<byte>((void*) CurrentStream.uDataHandle, (int) CurrentStream.uvDataLength); var uSpan = new Span<byte>((void*) CurrentStream.uDataHandle, (int) CurrentStream.uvDataLength);
var vSpan = new Span<byte>((void*) CurrentStream.vDataHandle, (int) CurrentStream.uvDataLength); var vSpan = new Span<byte>((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(uSpan, (uint) ySpan.Length, SetDataOptions.Overwrite);
TransferBuffer.SetData(vSpan, (uint) (ySpan.Length + uSpan.Length), SetDataOptions.Overwrite); TransferBuffer.SetData(vSpan, (uint) (ySpan.Length + uSpan.Length), SetDataOptions.Overwrite);
commandBuffer.BeginCopyPass();
commandBuffer.UploadToTexture( commandBuffer.UploadToTexture(
TransferBuffer, TransferBuffer,
yTexture, yTexture,
@ -292,11 +281,13 @@ namespace MoonWorks.Video
} }
); );
commandBuffer.EndCopyPass();
commandBuffer.BeginRenderPass( commandBuffer.BeginRenderPass(
new ColorAttachmentInfo(RenderTexture, Color.Black) new ColorAttachmentInfo(RenderTexture, Color.Black)
); );
commandBuffer.BindGraphicsPipeline(GraphicsDevice.VideoPipeline); commandBuffer.BindGraphicsPipeline(Device.VideoPipeline);
commandBuffer.BindFragmentSamplers( commandBuffer.BindFragmentSamplers(
new TextureSamplerBinding(yTexture, LinearSampler), new TextureSamplerBinding(yTexture, LinearSampler),
new TextureSamplerBinding(uTexture, LinearSampler), new TextureSamplerBinding(uTexture, LinearSampler),
@ -307,7 +298,7 @@ namespace MoonWorks.Video
commandBuffer.EndRenderPass(); commandBuffer.EndRenderPass();
GraphicsDevice.Submit(commandBuffer); Device.Submit(commandBuffer);
} }
} }