Skip to content

Commit

Permalink
WinUI 3 App (#13)
Browse files Browse the repository at this point in the history
* Trying win ui

* trying more plumbing with visibility overrides for PoC

* WGU crimes

* Windows working with new wgpu pr

* Remove changes to main in cappy

* Remove render view

* More changes

* Gen fixes
  • Loading branch information
DDRBoxman authored Oct 29, 2023
1 parent c3ccbbb commit fea61cc
Show file tree
Hide file tree
Showing 28 changed files with 1,048 additions and 220 deletions.
728 changes: 527 additions & 201 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions build_win_dll.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cargo build
xcopy .\target\debug\cappy3ds_render.dll .\win\ /Y
5 changes: 0 additions & 5 deletions cappy3ds/src/main.rs

This file was deleted.

8 changes: 6 additions & 2 deletions cappy3ds_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ name = "cappy3ds_render"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
build = "build.rs"

[build-dependencies]
cbindgen = "0.1.29"
csbindgen = "1.8.0"

[dependencies]
bytemuck = { version = "1.14", features = [ "derive" ] }
env_logger = "0.10"
log = "0.4"
wgpu = "0.17"
wgpu = "0.18"
raw-window-handle = "0.5.2"
image = "0.24.7"
futures = "0.3.28"
Expand Down
7 changes: 7 additions & 0 deletions cappy3ds_render/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
csbindgen::Builder::default()
.input_extern_file("src/lib.rs")
.csharp_dll_name("cappy3ds_render")
.generate_csharp_file("../win/NativeMethods.g.cs")
.unwrap();
}
4 changes: 3 additions & 1 deletion cappy3ds_render/src/dsscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,12 @@ impl DSScreen {
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: true,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
occlusion_query_set: None,
timestamp_writes: None,
});

render_pass.set_pipeline(&self.render_pipeline);
Expand Down
18 changes: 15 additions & 3 deletions cappy3ds_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use bytes::BytesMut;
use futures::executor;
use raw_window_handle::{
AppKitDisplayHandle, AppKitWindowHandle, HasRawDisplayHandle, HasRawWindowHandle,
RawDisplayHandle, RawWindowHandle,
RawDisplayHandle, RawWindowHandle, WindowsDisplayHandle,
};
use std::ffi;


mod dsscreen;
mod primitive;
mod render;
Expand All @@ -15,6 +16,16 @@ use std::thread;
pub use render::State;

#[no_mangle]
#[cfg(target_os = "windows")]
pub extern "C" fn send_swap_chain_panel(swap_chain_panel: *mut ffi::c_void) {
let mut res = State::new_from_swap_chain_panel(swap_chain_panel);
let mut v = executor::block_on(res);

v.render();
}

#[no_mangle]
#[cfg(target_os = "macos")]
pub extern "C" fn send_window(app_kit_nsview: *mut ffi::c_void) {
let window = Window {
ns_view: app_kit_nsview,
Expand Down Expand Up @@ -61,18 +72,19 @@ fn trash_code(v: &mut State) {
cappy3ds.do_capture();
}

#[cfg(target_os = "macos")]
pub struct Window {
//id: usize,
// ns_window: *mut ffi::c_void,
ns_view: *mut ffi::c_void,
}

#[cfg(target_os = "macos")]
unsafe impl HasRawDisplayHandle for Window {
fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::AppKit(AppKitDisplayHandle::empty())
}
}

#[cfg(target_os = "macos")]
unsafe impl HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> RawWindowHandle {
let mut handle = AppKitWindowHandle::empty();
Expand Down
50 changes: 42 additions & 8 deletions cappy3ds_render/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use crate::dsscreen::DSScreen;
const SCENE_WIDTH: u32 = 1270;
const SCENE_HEIGHT: u32 = 720;

use wgpu::{Instance, Surface};

pub struct State {
surface: wgpu::Surface,
pub surface: wgpu::Surface,
device: wgpu::Device,
queue: wgpu::Queue,
//config: wgpu::SurfaceConfiguration,
Expand All @@ -21,27 +23,56 @@ pub struct State {
}

impl State {
pub async fn new_from_swap_chain_panel(swap_chain_panel: *mut std::ffi::c_void) -> Self {
let instance = Self::create_instance();

// # Safety
//
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface_from_swap_chain_panel(swap_chain_panel) };

Self::new_with_surface(instance, surface).await
}

// Creating some of the wgpu types requires async code
pub async fn new<
W: raw_window_handle::HasRawWindowHandle + raw_window_handle::HasRawDisplayHandle,
>(
window: &W,
) -> Self {
// let size = window.inner_size();
let instance = Self::create_instance();

// # Safety
//
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface(&window) }.unwrap();

Self::new_with_surface(instance, surface).await
}

fn create_instance() -> wgpu::Instance{
// The instance is a handle to our GPU
// Backends::all => Vulkan + Metal + DX12 + Browser WebGPU
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::all(),
dx12_shader_compiler: Default::default(),
gles_minor_version: wgpu::Gles3MinorVersion::default(),
flags: wgpu::InstanceFlags::default(),
});

// # Safety
//
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface(&window) }.unwrap();
return instance;
}

async fn new_with_surface(instance: Instance, surface: Surface) -> Self {
// let size = window.inner_size();

let width = 400;
let height = 400;



let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
Expand Down Expand Up @@ -83,6 +114,7 @@ impl State {
alpha_mode: surface_caps.alpha_modes[0],
view_formats: vec![],
};

surface.configure(&device, &config);

let diffuse_bytes = include_bytes!("../resources/test/upper_5.png");
Expand Down Expand Up @@ -146,10 +178,12 @@ impl State {
b: 0.3,
a: 1.0,
}),
store: true,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
occlusion_query_set: None,
timestamp_writes: None,
});
}

Expand Down
16 changes: 16 additions & 0 deletions win/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Application
x:Class="Cappy3ds.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Cappy3ds">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
</ResourceDictionary>
</Application.Resources>
</Application>
53 changes: 53 additions & 0 deletions win/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using System.Runtime.InteropServices;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Cappy3ds
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{


/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}

/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window.Activate();
}

private Window m_window;
}
}
Binary file added win/Assets/LockScreenLogo.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added win/Assets/SplashScreen.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added win/Assets/Square150x150Logo.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added win/Assets/Square44x44Logo.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added win/Assets/StoreLogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added win/Assets/Wide310x150Logo.scale-200.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions win/Cappy3ds.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>Cappy3ds</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<Content Include="Assets\SplashScreen.scale-200.png" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\Square150x150Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
<Content Include="Assets\StoreLogo.png" />
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.230518007-experimental1" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.25936-preview" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

<!--
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
Tools extension to be activated for this project even if the Windows App SDK Nuget
package has not yet been restored.
-->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<None Update="cappy3ds_render.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="libcappy3ds.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<!--
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
Explorer "Package and Publish" context menu entry to be enabled for this project even if
the Windows App SDK Nuget package has not yet been restored.
-->
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup>
</Project>
43 changes: 43 additions & 0 deletions win/Cappy3ds.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.2092
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cappy3ds", "Cappy3ds.csproj", "{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|ARM64 = Release|ARM64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Debug|ARM64.ActiveCfg = Debug|ARM64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Debug|ARM64.Build.0 = Debug|ARM64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Debug|ARM64.Deploy.0 = Debug|ARM64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Debug|x64.ActiveCfg = Debug|x64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Debug|x64.Build.0 = Debug|x64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Debug|x64.Deploy.0 = Debug|x64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Debug|x86.ActiveCfg = Debug|x86
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Debug|x86.Build.0 = Debug|x86
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Debug|x86.Deploy.0 = Debug|x86
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Release|ARM64.ActiveCfg = Release|ARM64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Release|ARM64.Build.0 = Release|ARM64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Release|ARM64.Deploy.0 = Release|ARM64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Release|x64.ActiveCfg = Release|x64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Release|x64.Build.0 = Release|x64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Release|x64.Deploy.0 = Release|x64
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Release|x86.ActiveCfg = Release|x86
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Release|x86.Build.0 = Release|x86
{7197E3C4-DB4C-4CBD-A517-206C6E0D081F}.Release|x86.Deploy.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {009ED858-724A-44A4-B74E-E16AECEAAF7A}
EndGlobalSection
EndGlobal
15 changes: 15 additions & 0 deletions win/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="Cappy3ds.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Cappy3ds"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
<SwapChainPanel x:Name="swapChainPanel1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="400" Width="400" ></SwapChainPanel>
</StackPanel>
</Window>
Loading

0 comments on commit fea61cc

Please sign in to comment.