public ReadOnlySpan <byte> GetData() { BackgroundResource resources = _gd.BackgroundResources.Get(); if (_gd.CommandBufferPool.OwnedByCurrentThread) { _gd.FlushAllCommands(); return(GetData(_gd.CommandBufferPool, resources.GetFlushBuffer())); } else { return(GetData(resources.GetPool(), resources.GetFlushBuffer())); } }
public void Clear( VulkanRenderer gd, Auto <DisposableImageView> dst, ReadOnlySpan <float> clearColor, uint componentMask, int dstWidth, int dstHeight, VkFormat dstFormat, Rectangle <int> scissor) { const int ClearColorBufferSize = 16; gd.FlushAllCommands(); using var cbs = gd.CommandBufferPool.Rent(); _pipeline.SetCommandBuffer(cbs); var bufferHandle = gd.BufferManager.CreateWithHandle(gd, ClearColorBufferSize, false); gd.BufferManager.SetData <float>(bufferHandle, 0, clearColor); Span <BufferRange> bufferRanges = stackalloc BufferRange[1]; bufferRanges[0] = new BufferRange(bufferHandle, 0, ClearColorBufferSize); _pipeline.SetUniformBuffers(1, bufferRanges); Span <GAL.Viewport> viewports = stackalloc GAL.Viewport[1]; viewports[0] = new GAL.Viewport( new Rectangle <float>(0, 0, dstWidth, dstHeight), ViewportSwizzle.PositiveX, ViewportSwizzle.PositiveY, ViewportSwizzle.PositiveZ, ViewportSwizzle.PositiveW, 0f, 1f); Span <Rectangle <int> > scissors = stackalloc Rectangle <int> [1]; scissors[0] = scissor; _pipeline.SetProgram(_programColorClear); _pipeline.SetRenderTarget(dst, (uint)dstWidth, (uint)dstHeight, false, dstFormat); _pipeline.SetRenderTargetColorMasks(new uint[] { componentMask }); _pipeline.SetViewports(viewports, false); _pipeline.SetScissors(scissors); _pipeline.SetPrimitiveTopology(GAL.PrimitiveTopology.TriangleStrip); _pipeline.Draw(4, 1, 0, 0); _pipeline.Finish(); gd.BufferManager.Delete(bufferHandle); }
public unsafe void PushData(CommandBufferPool cbp, CommandBufferScoped?cbs, Action endRenderPass, BufferHolder dst, int dstOffset, ReadOnlySpan <byte> data) { bool isRender = cbs != null; CommandBufferScoped scoped = cbs ?? cbp.Rent(); // Must push all data to the buffer. If it can't fit, split it up. endRenderPass?.Invoke(); while (data.Length > 0) { if (_freeSize < data.Length) { FreeCompleted(); } while (_freeSize == 0) { if (!WaitFreeCompleted(cbp)) { if (isRender) { _gd.FlushAllCommands(); scoped = cbp.Rent(); isRender = false; } else { scoped = cbp.ReturnAndRent(scoped); } } } int chunkSize = Math.Min(_freeSize, data.Length); PushDataImpl(scoped, dst, dstOffset, data.Slice(0, chunkSize)); dstOffset += chunkSize; data = data.Slice(chunkSize); } if (!isRender) { scoped.Dispose(); } }
public void Blit( VulkanRenderer gd, TextureView src, Auto <DisposableImageView> dst, int dstWidth, int dstHeight, VkFormat dstFormat, Extents2D srcRegion, Extents2D dstRegion, bool linearFilter, bool clearAlpha = false) { gd.FlushAllCommands(); using var cbs = gd.CommandBufferPool.Rent(); Blit(gd, cbs, src, dst, dstWidth, dstHeight, dstFormat, srcRegion, dstRegion, linearFilter, clearAlpha); }
public void Create(ulong id) { MultiFenceHolder waitable = new MultiFenceHolder(); _gd.FlushAllCommands(); _gd.CommandBufferPool.AddWaitable(waitable); SyncHandle handle = new SyncHandle { ID = id, Waitable = waitable }; lock (_handles) { _handles.Add(handle); } }
public unsafe ReadOnlySpan <byte> GetData(int offset, int size) { if (_map != IntPtr.Zero) { return(GetDataStorage(offset, size)); } else { BackgroundResource resource = _gd.BackgroundResources.Get(); if (_gd.CommandBufferPool.OwnedByCurrentThread) { _gd.FlushAllCommands(); return(resource.GetFlushBuffer().GetBufferData(_gd.CommandBufferPool, this, offset, size)); } else { return(resource.GetFlushBuffer().GetBufferData(resource.GetPool(), this, offset, size)); } } }
public override unsafe void Present(ITexture texture, ImageCrop crop, Action <object> swapBuffersCallback) { if (_recreateImages) { RecreateImages(); _recreateImages = false; } var image = _images[_nextImage]; _gd.FlushAllCommands(); var cbs = _gd.CommandBufferPool.Rent(); Transition( cbs.CommandBuffer, image.GetUnsafe().Value, 0, AccessFlags.AccessTransferWriteBit, ImageLayout.TransferSrcOptimal, ImageLayout.General); var view = (TextureView)texture; int srcX0, srcX1, srcY0, srcY1; float scale = view.ScaleFactor; if (crop.Left == 0 && crop.Right == 0) { srcX0 = 0; srcX1 = (int)(view.Width / scale); } else { srcX0 = crop.Left; srcX1 = crop.Right; } if (crop.Top == 0 && crop.Bottom == 0) { srcY0 = 0; srcY1 = (int)(view.Height / scale); } else { srcY0 = crop.Top; srcY1 = crop.Bottom; } if (scale != 1f) { srcX0 = (int)(srcX0 * scale); srcY0 = (int)(srcY0 * scale); srcX1 = (int)Math.Ceiling(srcX1 * scale); srcY1 = (int)Math.Ceiling(srcY1 * scale); } if (ScreenCaptureRequested) { CaptureFrame(view, srcX0, srcY0, srcX1 - srcX0, srcY1 - srcY0, view.Info.Format.IsBgr(), crop.FlipX, crop.FlipY); ScreenCaptureRequested = false; } float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY)); float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX)); int dstWidth = (int)(_width * ratioX); int dstHeight = (int)(_height * ratioY); int dstPaddingX = (_width - dstWidth) / 2; int dstPaddingY = (_height - dstHeight) / 2; int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX; int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX; int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY; int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY; _gd.HelperShader.Blit( _gd, cbs, view, _imageViews[_nextImage], _width, _height, Format, new Extents2D(srcX0, srcY0, srcX1, srcY1), new Extents2D(dstX0, dstY1, dstX1, dstY0), true, true); Transition( cbs.CommandBuffer, image.GetUnsafe().Value, 0, 0, ImageLayout.General, ImageLayout.TransferSrcOptimal); _gd.CommandBufferPool.Return( cbs, null, stackalloc[] { PipelineStageFlags.PipelineStageColorAttachmentOutputBit },
public unsafe override void Present(ITexture texture, ImageCrop crop, Action <object> swapBuffersCallback) { uint nextImage = 0; while (true) { var acquireResult = _gd.SwapchainApi.AcquireNextImage( _device, _swapchain, ulong.MaxValue, _imageAvailableSemaphore, new Fence(), ref nextImage); if (acquireResult == Result.ErrorOutOfDateKhr || acquireResult == Result.SuboptimalKhr || _vsyncModeChanged) { RecreateSwapchain(); } else { acquireResult.ThrowOnError(); break; } } var swapchainImage = _swapchainImages[nextImage]; _gd.FlushAllCommands(); var cbs = _gd.CommandBufferPool.Rent(); Transition( cbs.CommandBuffer, swapchainImage, 0, AccessFlags.AccessTransferWriteBit, ImageLayout.Undefined, ImageLayout.General); var view = (TextureView)texture; int srcX0, srcX1, srcY0, srcY1; float scale = view.ScaleFactor; if (crop.Left == 0 && crop.Right == 0) { srcX0 = 0; srcX1 = (int)(view.Width / scale); } else { srcX0 = crop.Left; srcX1 = crop.Right; } if (crop.Top == 0 && crop.Bottom == 0) { srcY0 = 0; srcY1 = (int)(view.Height / scale); } else { srcY0 = crop.Top; srcY1 = crop.Bottom; } if (scale != 1f) { srcX0 = (int)(srcX0 * scale); srcY0 = (int)(srcY0 * scale); srcX1 = (int)Math.Ceiling(srcX1 * scale); srcY1 = (int)Math.Ceiling(srcY1 * scale); } if (ScreenCaptureRequested) { CaptureFrame(view, srcX0, srcY0, srcX1 - srcX0, srcY1 - srcY0, view.Info.Format.IsBgr(), crop.FlipX, crop.FlipY); ScreenCaptureRequested = false; } float ratioX = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _height * crop.AspectRatioX / (_width * crop.AspectRatioY)); float ratioY = crop.IsStretched ? 1.0f : MathF.Min(1.0f, _width * crop.AspectRatioY / (_height * crop.AspectRatioX)); int dstWidth = (int)(_width * ratioX); int dstHeight = (int)(_height * ratioY); int dstPaddingX = (_width - dstWidth) / 2; int dstPaddingY = (_height - dstHeight) / 2; int dstX0 = crop.FlipX ? _width - dstPaddingX : dstPaddingX; int dstX1 = crop.FlipX ? dstPaddingX : _width - dstPaddingX; int dstY0 = crop.FlipY ? dstPaddingY : _height - dstPaddingY; int dstY1 = crop.FlipY ? _height - dstPaddingY : dstPaddingY; _gd.HelperShader.Blit( _gd, cbs, view, _swapchainImageViews[nextImage], _width, _height, _format, new Extents2D(srcX0, srcY0, srcX1, srcY1), new Extents2D(dstX0, dstY1, dstX1, dstY0), true, true); Transition( cbs.CommandBuffer, swapchainImage, 0, 0, ImageLayout.General, ImageLayout.PresentSrcKhr); _gd.CommandBufferPool.Return( cbs, stackalloc[] { _imageAvailableSemaphore },