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

Commit

Permalink
[Samples] Fix issue #307. Add XAML SurfaceImageSource DirectX interop…
Browse files Browse the repository at this point in the history
… sample (WIP)
  • Loading branch information
xoofx committed Mar 18, 2014
1 parent ad80586 commit 1c09a7c
Show file tree
Hide file tree
Showing 47 changed files with 8,963 additions and 0 deletions.
@@ -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("Scenario1Component")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Scenario1Component")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[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)]
@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A98E21E7-F972-4284-B151-D35E9F3A2D9E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Scenario1Component</RootNamespace>
<AssemblyName>Scenario1Component</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformVersion>8.1</TargetPlatformVersion>
<MinimumVisualStudioVersion>12</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{BC8A1FFA-BEE3-4634-8014-F334798102B3};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
<OutputPath>bin\ARM\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Compile Include="Scenario1ImageSource.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="SharpDX">
<HintPath>..\..\..\..\..\Bin\DirectX11_2-winrt\SharpDX.dll</HintPath>
</Reference>
<Reference Include="SharpDX.Direct2D1">
<HintPath>..\..\..\..\..\Bin\DirectX11_2-winrt\SharpDX.Direct2D1.dll</HintPath>
</Reference>
<Reference Include="SharpDX.Direct3D11">
<HintPath>..\..\..\..\..\Bin\DirectX11_2-winrt\SharpDX.Direct3D11.dll</HintPath>
</Reference>
<Reference Include="SharpDX.DXGI">
<HintPath>..\..\..\..\..\Bin\DirectX11_2-winrt\SharpDX.DXGI.dll</HintPath>
</Reference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '12.0' ">
<VisualStudioVersion>12.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
@@ -0,0 +1,205 @@
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using Windows.UI.Xaml;
using SharpDX;
using SharpDX.Direct2D1;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using Device = SharpDX.Direct3D11.Device;
using FeatureLevel = SharpDX.Direct3D.FeatureLevel;

namespace Scenario1Component
{
public sealed class Scenario1ImageSource : Windows.UI.Xaml.Media.Imaging.SurfaceImageSource
{
private Device d3dDevice;
private SharpDX.Direct2D1.Device d2dDevice;
private SharpDX.Direct2D1.DeviceContext d2dContext;
private readonly int width;
private readonly int height;

public Scenario1ImageSource(int pixelWidth, int pixelHeight, bool isOpaque)
: base(pixelWidth, pixelHeight, isOpaque)
{
width = pixelWidth;
height = pixelHeight;

CreateDeviceResources();

Application.Current.Suspending += OnSuspending;
}

public void BeginDraw(Windows.Foundation.Rect updateRect)
{
// Express target area as a native RECT type.
var updateRectNative = new Rectangle
{
Left = (int) updateRect.Left,
Top = (int) updateRect.Top,
Right = (int) updateRect.Right,
Bottom = (int) updateRect.Bottom
};

// Query for ISurfaceImageSourceNative interface.
using (var sisNative = ComObject.QueryInterface<ISurfaceImageSourceNative>(this))
{
// Begin drawing - returns a target surface and an offset to use as the top left origin when drawing.
try
{
Point offset;
using (var surface = sisNative.BeginDraw(updateRectNative, out offset))
{

// Create render target.
using (var bitmap = new Bitmap1(d2dContext, surface))
{
// Set context's render target.
d2dContext.Target = bitmap;
}

// Begin drawing using D2D context.
d2dContext.BeginDraw();

// Apply a clip and transform to constrain updates to the target update area.
// This is required to ensure coordinates within the target surface remain
// consistent by taking into account the offset returned by BeginDraw, and
// can also improve performance by optimizing the area that is drawn by D2D.
// Apps should always account for the offset output parameter returned by
// BeginDraw, since it may not match the passed updateRect input parameter's location.
d2dContext.PushAxisAlignedClip(
new RectangleF(
(offset.X),
(offset.Y),
(offset.X + (float) updateRect.Width),
(offset.Y + (float) updateRect.Height)
),
AntialiasMode.Aliased
);

d2dContext.Transform = Matrix3x2.Translation(offset.X, offset.Y);
}
}
catch (SharpDXException ex)
{
if (ex.ResultCode == SharpDX.DXGI.ResultCode.DeviceRemoved ||
ex.ResultCode == SharpDX.DXGI.ResultCode.DeviceReset)
{
// If the device has been removed or reset, attempt to recreate it and continue drawing.
CreateDeviceResources();
BeginDraw(updateRect);
}
else
{
throw;
}
}
}
}

public void BeginDraw()
{
BeginDraw(new Windows.Foundation.Rect(0, 0, width, height));
}

public void EndDraw()
{
// Remove the transform and clip applied in BeginDraw since
// the target area can change on every update.
d2dContext.Transform = SharpDX.Matrix3x2.Identity;
d2dContext.PopAxisAlignedClip();

// Remove the render target and end drawing.
d2dContext.EndDraw();

d2dContext.Target = null;

// Query for ISurfaceImageSourceNative interface.
using (var sisNative = ComObject.QueryInterface<ISurfaceImageSourceNative>(this))
sisNative.EndDraw();
}

public void Clear(Windows.UI.Color color)
{
d2dContext.Clear(ConvertToColorF(color));
}

public void FillSolidRect(Windows.UI.Color color, Windows.Foundation.Rect rect)
{
// Create a solid color D2D brush.
using (var brush = new SolidColorBrush(d2dContext, ConvertToColorF(color)))
{
// Draw a filled rectangle.
d2dContext.FillRectangle(ConvertToRectF(rect), brush);
}
}

private void OnSuspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
// Hints to the driver that the app is entering an idle state and that its memory can be used temporarily for other apps.
using (var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device3>())
dxgiDevice.Trim();
}

// Initialize hardware-dependent resources.
private void CreateDeviceResources()
{
// This flag adds support for surfaces with a different color channel ordering
// than the API default. It is required for compatibility with Direct2D.
var creationFlags = DeviceCreationFlags.BgraSupport;

#if DEBUG
// If the project is in a debug build, enable debugging via SDK Layers.
creationFlags |= DeviceCreationFlags.Debug;
#endif

// This array defines the set of DirectX hardware feature levels this app will support.
// Note the ordering should be preserved.
// Don't forget to declare your application's minimum required feature level in its
// description. All applications are assumed to support 9.1 unless otherwise stated.
FeatureLevel[] featureLevels =
{
FeatureLevel.Level_11_1,
FeatureLevel.Level_11_0,
FeatureLevel.Level_10_1,
FeatureLevel.Level_10_0,
FeatureLevel.Level_9_3,
FeatureLevel.Level_9_2,
FeatureLevel.Level_9_1,
};

// Create the Direct3D 11 API device object.
d3dDevice = new Device(DriverType.Hardware, creationFlags, featureLevels);

// Get the Direct3D 11.1 API device.
using (var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>())
{
// Create the Direct2D device object and a corresponding context.
d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice);

d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, DeviceContextOptions.None);

// Query for ISurfaceImageSourceNative interface.
var sisNative = ComObject.QueryInterface<ISurfaceImageSourceNative>(this);
sisNative.Device = dxgiDevice;
}
}

private static SharpDX.Color ConvertToColorF(Windows.UI.Color color)
{
return new Color(color.R, color.G, color.B, color.A);
}

private static SharpDX.RectangleF ConvertToRectF(Windows.Foundation.Rect rect)
{
return new RectangleF((float) rect.X, (float) rect.Y, (float) rect.Width, (float) rect.Height);
}
}
}
@@ -0,0 +1,2 @@
call "%~dp0..\..\..\fxc.bat" /Tvs_4_0_level_9_1 /Emain /FoSimpleVertexShader.cso SimpleVertexShader.hlsl
call "%~dp0..\..\..\fxc.bat" /Tps_4_0_level_9_1 /Emain /FoSimplePixelShader.cso SimplePixelShader.hlsl

0 comments on commit 1c09a7c

Please sign in to comment.