123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Threading.Tasks;
- using Avalonia;
- using Avalonia.Platform;
- using Avalonia.Rendering.Composition;
- using Silk.NET.Core;
- using Silk.NET.Vulkan;
- using Silk.NET.Vulkan.Extensions.KHR;
- using SilkNetDemo;
- namespace GpuInterop.VulkanDemo;
- public class VulkanDemoControl : DrawingSurfaceDemoBase
- {
- private Instance _vkInstance;
- private Vk _api;
- class VulkanResources : IAsyncDisposable
- {
- public VulkanContext Context { get; }
- public VulkanSwapchain Swapchain { get; }
- public VulkanContent Content { get; }
- public VulkanResources(VulkanContext context, VulkanSwapchain swapchain, VulkanContent content)
- {
- Context = context;
- Swapchain = swapchain;
- Content = content;
- }
- public async ValueTask DisposeAsync()
- {
- Context.Pool.FreeUsedCommandBuffers();
- Content.Dispose();
- await Swapchain.DisposeAsync();
- Context.Dispose();
- }
- }
- protected override bool SupportsDisco => true;
- private VulkanResources? _resources;
- protected override (bool success, string info) InitializeGraphicsResources(Compositor compositor,
- CompositionDrawingSurface compositionDrawingSurface, ICompositionGpuInterop gpuInterop)
- {
- var (context, info) = VulkanContext.TryCreate(gpuInterop);
- if (context == null)
- return (false, info);
- try
- {
- var content = new VulkanContent(context);
- _resources = new VulkanResources(context,
- new VulkanSwapchain(context, gpuInterop, compositionDrawingSurface), content);
- return (true, info);
- }
- catch(Exception e)
- {
- return (false, e.ToString());
- }
- }
- protected override void FreeGraphicsResources()
- {
- _resources?.DisposeAsync();
- _resources = null;
- }
- protected override unsafe void RenderFrame(PixelSize pixelSize)
- {
- if (_resources == null)
- return;
- using (_resources.Swapchain.BeginDraw(pixelSize, out var image))
- {
- /*
- var commandBuffer = _resources.Context.Pool.CreateCommandBuffer();
- commandBuffer.BeginRecording();
- image.TransitionLayout(commandBuffer.InternalHandle, ImageLayout.TransferDstOptimal, AccessFlags.None);
- var range = new ImageSubresourceRange
- {
- AspectMask = ImageAspectFlags.ColorBit,
- LayerCount = 1,
- LevelCount = 1,
- BaseArrayLayer = 0,
- BaseMipLevel = 0
- };
- var color = new ClearColorValue
- {
- Float32_0 = 1, Float32_1 = 0, Float32_2 = 0, Float32_3 = 1
- };
- _resources.Context.Api.CmdClearColorImage(commandBuffer.InternalHandle, image.InternalHandle.Value, ImageLayout.TransferDstOptimal,
- &color, 1, &range);
- commandBuffer.Submit();*/
- _resources.Content.Render(image, Yaw, Pitch, Roll, Disco);
- }
- }
- }
|