Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

[UWP] Basic uwp sample #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions WindowsUWP.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.27130.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UWPSwapchain", "WindowsUWP\Direct3D11\Swapchain\UWPSwapchain.csproj", "{948B81A7-97FF-4C62-82B6-35A8733DDD7F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM = Debug|ARM
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|ARM = Release|ARM
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Debug|ARM.ActiveCfg = Debug|ARM
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Debug|ARM.Build.0 = Debug|ARM
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Debug|ARM.Deploy.0 = Debug|ARM
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Debug|x64.ActiveCfg = Debug|x64
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Debug|x64.Build.0 = Debug|x64
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Debug|x64.Deploy.0 = Debug|x64
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Debug|x86.ActiveCfg = Debug|x86
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Debug|x86.Build.0 = Debug|x86
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Debug|x86.Deploy.0 = Debug|x86
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Release|ARM.ActiveCfg = Release|ARM
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Release|ARM.Build.0 = Release|ARM
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Release|ARM.Deploy.0 = Release|ARM
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Release|x64.ActiveCfg = Release|x64
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Release|x64.Build.0 = Release|x64
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Release|x64.Deploy.0 = Release|x64
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Release|x86.ActiveCfg = Release|x86
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Release|x86.Build.0 = Release|x86
{948B81A7-97FF-4C62-82B6-35A8733DDD7F}.Release|x86.Deploy.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {91C21AD8-0B37-492E-B233-289135C42A0E}
EndGlobalSection
EndGlobal
8 changes: 8 additions & 0 deletions WindowsUWP/Direct3D11/Swapchain/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Application
x:Class="UWPSwapchain.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestUWPDX"
RequestedTheme="Light">

</Application>
100 changes: 100 additions & 0 deletions WindowsUWP/Direct3D11/Swapchain/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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 Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace UWPSwapchain
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed 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();
this.Suspending += OnSuspending;
}

/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;

// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();

rootFrame.NavigationFailed += OnNavigationFailed;

if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}

// Place the frame in the current Window
Window.Current.Content = rootFrame;
}

if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
}

/// <summary>
/// Invoked when Navigation to a certain page fails
/// </summary>
/// <param name="sender">The Frame which failed navigation</param>
/// <param name="e">Details about the navigation failure</param>
void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}

/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
deferral.Complete();
}
}
}
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.
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions WindowsUWP/Direct3D11/Swapchain/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Page
x:Class="UWPSwapchain.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestUWPDX"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<SwapChainPanel x:Name="panel" x:FieldModifier="private" Loaded="panel_Loaded" Unloaded="panel_Unloaded" SizeChanged="panel_SizeChanged" />
</Grid>


</Page>
151 changes: 151 additions & 0 deletions WindowsUWP/Direct3D11/Swapchain/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using SharpDX.DXGI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using SharpDX;
using SharpDX.Direct3D11;
using Windows.ApplicationModel;
using System.Text;
using System.Diagnostics;

namespace UWPSwapchain
{


public sealed partial class MainPage : Page
{
SharpDX.Direct3D11.Device device;
SharpDX.Direct3D11.DeviceContext deviceContext;

SharpDX.DXGI.SwapChain1 swapchain;
Texture2D backBufferTexture;
RenderTargetView backBufferView;

Stopwatch sw = Stopwatch.StartNew();

public MainPage()
{
this.InitializeComponent();

bool enableDebug = false;

DeviceCreationFlags flags = DeviceCreationFlags.BgraSupport;
if (enableDebug)
flags |= DeviceCreationFlags.Debug;

this.device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, flags);
this.deviceContext = this.device.ImmediateContext;
}

private void CompositionTarget_Rendering(object sender, object e)
{
SharpDX.Color4 c = Color.Red;
c.Red = (float)Math.Abs(Math.Sin(sw.Elapsed.TotalSeconds));

//This is your render call, perform all render tasks and then call present on the swap chain
this.deviceContext.ClearRenderTargetView(this.backBufferView, c);

//Attach render target and set viewport
this.deviceContext.OutputMerger.SetRenderTargets(this.backBufferView);
this.deviceContext.Rasterizer.SetViewport(new ViewportF(0.0f, 0.0f, this.backBufferTexture.Description.Width, this.backBufferTexture.Description.Height, 0.0f, 1.0f));

//perform draw calls here

this.swapchain.Present(0, PresentFlags.None);
}

private void panel_Loaded(object sender, RoutedEventArgs e)
{
//This is trigerred when panel is loaded, you can now create your swap chain.

//Please note : Do not use this.panel.Width, make sure to use render size as width property returns NaN on create
SwapChainDescription1 swapChainDescription = new SwapChainDescription1()
{
AlphaMode = AlphaMode.Ignore,
BufferCount = 2,
Format = Format.R8G8B8A8_UNorm,
Height = (int)(this.panel.RenderSize.Height),
Width = (int)(this.panel.RenderSize.Width),
SampleDescription = new SampleDescription(1, 0),
Scaling = SharpDX.DXGI.Scaling.Stretch,
Stereo = false,
SwapEffect = SwapEffect.FlipSequential,
Usage = Usage.RenderTargetOutput
};

using (SharpDX.DXGI.Device3 dxgiDevice3 = this.device.QueryInterface<SharpDX.DXGI.Device3>())
{
using (SharpDX.DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent<SharpDX.DXGI.Factory3>())
{
SharpDX.DXGI.SwapChain1 swapChain1 = new SharpDX.DXGI.SwapChain1(dxgiFactory3, this.device, ref swapChainDescription);
this.swapchain = swapChain1;
}
}

using (SharpDX.DXGI.ISwapChainPanelNative nativeObject = SharpDX.ComObject.As<SharpDX.DXGI.ISwapChainPanelNative>(this.panel))
{
nativeObject.SwapChain = this.swapchain;
}

this.backBufferTexture = this.swapchain.GetBackBuffer<Texture2D>(0);
this.backBufferView = new RenderTargetView(this.device, this.backBufferTexture);

CompositionTarget.Rendering += CompositionTarget_Rendering;
Application.Current.Suspending += Current_Suspending;
}

private void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
//This is the only part where you will receive a "close event", you should serialize any state so app can resume correctly.

//Please note that this method has a time out (e.SuspendingOperation.Deadline), if you do not return prior to this time, application will terminate instead of being suspended.

if (this.swapchain != null)
{
this.deviceContext.ClearState();
using (SharpDX.DXGI.Device3 dxgiDevice3 = this.device.QueryInterface<SharpDX.DXGI.Device3>())
dxgiDevice3.Trim();
}


}

private void panel_Unloaded(object sender, RoutedEventArgs e)
{
//On uwp windows 10 , this is never called, quitting the app does not do anything on it
}

private void panel_SizeChanged(object sender, SizeChangedEventArgs e)
{
/* Resize swap chain, to do it cleanly, we clear state (to ensure resources are not bound to pipeline, and
* dispose associated resources (eg : render view and texture object)
* Once done, we can call resizebuffers, and create direct3d11 objects again */
if (this.swapchain != null)
{
this.deviceContext.ClearState();

Size2 newSize = new Size2((int)e.NewSize.Width, (int)e.NewSize.Height);

Utilities.Dispose(ref this.backBufferView);
Utilities.Dispose(ref this.backBufferTexture);

this.swapchain.ResizeBuffers(this.swapchain.Description.BufferCount, (int)e.NewSize.Width, (int)e.NewSize.Height, swapchain.Description1.Format, swapchain.Description1.Flags);

this.backBufferTexture = this.swapchain.GetBackBuffer<Texture2D>(0);
this.backBufferView = new RenderTargetView(this.device, this.backBufferTexture);

}
}
}
}
29 changes: 29 additions & 0 deletions WindowsUWP/Direct3D11/Swapchain/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp">
<Identity Name="4c4e2067-5ada-4bb4-94ab-650cf72b6730" Publisher="CN=vux" Version="1.0.0.0" />
<mp:PhoneIdentity PhoneProductId="4c4e2067-5ada-4bb4-94ab-650cf72b6730" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>TestUWPDX</DisplayName>
<PublisherDisplayName>vux</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
</Dependencies>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="UWPSwapchain.App">
<uap:VisualElements DisplayName="SharpDX / UWP" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="Minimalistic sample for sharpDX and UWP" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png">
</uap:DefaultTile>
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClient" />
<Capability Name="internetClientServer" />
</Capabilities>
</Package>
29 changes: 29 additions & 0 deletions WindowsUWP/Direct3D11/Swapchain/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TestUWPDX")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TestUWPDX")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]
Loading