-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
298 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Application xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
x:Class="RaytracingVulkan.UI.App" | ||
RequestedThemeVariant="Default"> | ||
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. --> | ||
|
||
<Application.Styles> | ||
<FluentTheme /> | ||
</Application.Styles> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Avalonia; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using Avalonia.Markup.Xaml; | ||
|
||
namespace RaytracingVulkan.UI; | ||
|
||
public partial class App : Application | ||
{ | ||
public readonly VkContext VkContext = new(); | ||
public override void Initialize() | ||
{ | ||
AvaloniaXamlLoader.Load(this); | ||
} | ||
|
||
public override void OnFrameworkInitializationCompleted() | ||
{ | ||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) | ||
{ | ||
desktop.MainWindow = new MainWindow(); | ||
} | ||
|
||
base.OnFrameworkInitializationCompleted(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Window xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:viewModels="clr-namespace:RaytracingVulkan.UI.ViewModels" | ||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" | ||
x:Class="RaytracingVulkan.UI.MainWindow" | ||
x:DataType="viewModels:MainViewModel" | ||
Title="RaytracingVulkan.UI"> | ||
<Design.DataContext> | ||
<viewModels:MainViewModel/> | ||
</Design.DataContext> | ||
<Image Source="{Binding Image}" x:Name="Image" Width="500" Height="500"/> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Avalonia.Controls; | ||
using Avalonia.Media; | ||
using Avalonia.Threading; | ||
using RaytracingVulkan.UI.ViewModels; | ||
|
||
namespace RaytracingVulkan.UI; | ||
|
||
public partial class MainWindow : Window | ||
{ | ||
private readonly MainViewModel _viewModel = new(); | ||
|
||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
DataContext = _viewModel; | ||
} | ||
|
||
public override void Render(DrawingContext context) | ||
{ | ||
base.Render(context); | ||
_viewModel.Render(); | ||
Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Render); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Avalonia; | ||
using System; | ||
|
||
namespace RaytracingVulkan.UI; | ||
|
||
class Program | ||
{ | ||
// Initialization code. Don't use any Avalonia, third-party APIs or any | ||
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized | ||
// yet and stuff might break. | ||
[STAThread] | ||
public static void Main(string[] args) => BuildAvaloniaApp() | ||
.StartWithClassicDesktopLifetime(args); | ||
|
||
// Avalonia configuration, don't remove; also used by visual designer. | ||
public static AppBuilder BuildAvaloniaApp() | ||
=> AppBuilder.Configure<App>() | ||
.UsePlatformDetect() | ||
.WithInterFont() | ||
.LogToTrace(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<BuiltInComInteropSupport>true</BuiltInComInteropSupport> | ||
<ApplicationManifest>app.manifest</ApplicationManifest> | ||
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<PackageReference Include="Avalonia" Version="11.0.2"/> | ||
<PackageReference Include="Avalonia.Desktop" Version="11.0.2"/> | ||
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.2"/> | ||
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.2"/> | ||
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.--> | ||
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.2"/> | ||
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.1.2" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\RaytracingVulkan\RaytracingVulkan.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\RaytracingVulkan\RaytracingVulkan.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using Avalonia; | ||
using Avalonia.Media.Imaging; | ||
using Avalonia.Platform; | ||
using Microsoft.Toolkit.Mvvm.ComponentModel; | ||
using Silk.NET.Vulkan; | ||
|
||
namespace RaytracingVulkan.UI.ViewModels; | ||
|
||
public unsafe partial class MainViewModel : ObservableObject, IDisposable | ||
{ | ||
[ObservableProperty] private WriteableBitmap _image; | ||
|
||
private readonly VkContext _context = (Application.Current as App)!.VkContext; | ||
|
||
private Stopwatch VulkanStopWatch = new(); | ||
private readonly DescriptorPool _descriptorPool; | ||
private readonly DescriptorSet _descriptorSet; | ||
private readonly DescriptorSetLayout _setLayout; | ||
private readonly PipelineLayout _pipelineLayout; | ||
private readonly Pipeline _pipeline; | ||
|
||
private readonly CommandBuffer _cmd; | ||
|
||
private readonly VkImage _vkImage; | ||
private readonly VkBuffer _vkBuffer; | ||
|
||
private readonly void* _mappedData; | ||
|
||
public MainViewModel() | ||
{ | ||
//pipeline creation | ||
var poolSizes = new DescriptorPoolSize[] {new() {Type = DescriptorType.StorageImage, DescriptorCount = 1000}}; | ||
_descriptorPool = _context.CreateDescriptorPool(poolSizes); | ||
var binding = new DescriptorSetLayoutBinding | ||
{ | ||
Binding = 0, | ||
DescriptorCount = 1, | ||
DescriptorType = DescriptorType.StorageImage, | ||
StageFlags = ShaderStageFlags.ComputeBit | ||
}; | ||
_setLayout = _context.CreateDescriptorSetLayout(new[] {binding}); | ||
_descriptorSet = _context.AllocateDescriptorSet(_descriptorPool, _setLayout); | ||
|
||
var shaderModule = _context.LoadShaderModule("./assets/shaders/raytracing.comp.spv"); | ||
_pipelineLayout = _context.CreatePipelineLayout(_setLayout); | ||
_pipeline = _context.CreateComputePipeline(_pipelineLayout, shaderModule); | ||
var sz = 2000u; | ||
//image creation | ||
_vkImage = new VkImage(_context, sz, sz, Format.B8G8R8A8Unorm,ImageUsageFlags.StorageBit | ImageUsageFlags.TransferDstBit | ImageUsageFlags.TransferSrcBit); | ||
_vkImage.TransitionLayoutImmediate(ImageLayout.General); | ||
_context.UpdateDescriptorSetImage(ref _descriptorSet, _vkImage.GetImageInfo(), DescriptorType.StorageImage, 0); | ||
|
||
_image = new WriteableBitmap(new PixelSize((int) sz, (int) sz), new Vector(96, 96), PixelFormat.Bgra8888); | ||
|
||
//we don't need it anymore | ||
_context.DestroyShaderModule(shaderModule); | ||
_cmd = _context.AllocateCommandBuffer(); | ||
_vkBuffer = new VkBuffer(_context, _vkImage.Width * _vkImage.Height * 4, BufferUsageFlags.TransferDstBit, | ||
MemoryPropertyFlags.HostCachedBit | MemoryPropertyFlags.HostCoherentBit | | ||
MemoryPropertyFlags.HostVisibleBit); | ||
_vkBuffer.MapMemory(ref _mappedData); | ||
} | ||
public void Dispose() | ||
{ | ||
_vkBuffer.UnmapMemory(); | ||
_context.DestroyDescriptorPool(_descriptorPool); | ||
_context.DestroyDescriptorSetLayout(_setLayout); | ||
_context.DestroyPipelineLayout(_pipelineLayout); | ||
_context.DestroyPipeline(_pipeline); | ||
_image.Dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public void Render() | ||
{ | ||
//execute compute shader | ||
_context.BeginCommandBuffer(_cmd); | ||
_vkImage.TransitionLayout(_cmd, ImageLayout.General); | ||
_context.BindComputePipeline(_cmd, _pipeline); | ||
_context.BindComputeDescriptorSet(_cmd, _descriptorSet, _pipelineLayout); | ||
_context.Dispatch(_cmd, _vkImage.Width/32, _vkImage.Height/32, 1); | ||
_vkImage.TransitionLayout(_cmd, ImageLayout.TransferSrcOptimal); | ||
_vkImage.CopyToBuffer(_cmd, _vkBuffer.Buffer); | ||
_context.EndCommandBuffer(_cmd); | ||
_context.WaitForQueue(); | ||
|
||
using var buffer = _image.Lock(); | ||
var size = _vkImage.Width * _vkImage.Height * 4; | ||
System.Buffer.MemoryCopy(_mappedData, (void*) buffer.Address, size, size); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<!-- This manifest is used on Windows only. | ||
Don't remove it as it might cause problems with window transparency and embeded controls. | ||
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests --> | ||
<assemblyIdentity version="1.0.0.0" name="RaytracingVulkan.UI.Desktop"/> | ||
|
||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> | ||
<application> | ||
<!-- A list of the Windows versions that this application has been tested on | ||
and is designed to work with. Uncomment the appropriate elements | ||
and Windows will automatically select the most compatible environment. --> | ||
|
||
<!-- Windows 10 --> | ||
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" /> | ||
</application> | ||
</compatibility> | ||
</assembly> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.