Coherent UI for .NET  2.5.3
A modern user interface library for games
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
SharpDX Sample

For instructions on deploying the sample, please read .NET Sample guides.

The SharpDX sample demonstrates usage of Coherent UI with the SharpDX framework.

The sample is composed of the following files:

Game.cs - contains the main loop of the application, initialization and uninitialization routines.
Rendering.cs - contains an interface for the two renderers using DX9 and DX11 **RendererD3D11.cs - DirectX11 initialization, texture creation and various utility properties.
**RendererD3D9.cs - DirectX9 (or DirectX9Ex, if suppported) initialization, texture creation and various utility properties.
InputHandling.cs - utilities for converting SharpDX supplied input events to Coherent events.
Listeners.cs - classes providing handlers for Coherent UI callbacks

Warning
You should use SharpDX, v.2.5 in order to use SharpDX with DirectX9Ex, due to a bug in the SharpDX 2.4.2.

A detailed description of each file follows.

Game.cs

The Game class creates and instance of SharpDX and initializes and runs it.

The RendererFactory is a very simple class with one method - Create. The method takes one argument and uses it to determine whether a D3D11 renderer should be created or a D3D9 one.

Warning
You'll notice that the ContextListener class passed as parameter to the view context is saved to a member variable, because the if it wasn't it would be garbage collected and the system would throw an exception. This is valid for the ViewListener as well.

The game subscribes to the Idle event of Application and processes frames in the DoFrame method. Frame processing consists of updating the view context, fetching the composed surfaces and drawing the texture received from Coherent UI as a fullscreen triangle. This is also where the system listener is polled for UI readiness so we know when can we create a view.

Rendering.cs

This file provides the interface for the renderers used in the sample - RendererD3D9 and RendererD3D11.

RendererD3D11.cs

SharpDX initialization, using D3D11, and utility routines, unrelated to Coherent UI integration.

The InitializeDirectX method creates a device and swap chain, compiles a simple shader for rendering a textured fullscreen triangle and sets the appropriate device states.
The RecreateTextureFromHost method provides means for recreating the texture that is drawn on the fullscreen triangle. This is used when resizing the game window.

RendererD3D9.cs

SharpDX initialization, using D3D11, and utility routines, unrelated to Coherent UI integration.

The CheckSupportedDirectXVersion checks if DirectX9Ex is supported or not. If it is, then the renderer uses DX9Ex throughout the sample. The InitializeDirectX method creates a device (or deviceEx, depending on the supported DirectX version), compiles a simple shader for rendering a textured fullscreen triangle and setsup the used effect. The RecreateTextureFromHost method provides means for recreating the texture that is drawn on the fullscreen triangle. This is used when resizing the game window.

InputHandling.cs

Here we have the conversion of the SharpDX events to Coherent UI events. Since the SharpDX form inherits System.Windows.Forms.Form, it receives the standard KeyEventArgs / MouseEventArgs, making the conversion very easy. We've also made use of the Windows function GetKeyState (since SharpDX is available on Windows only) to get the key state for the events. You'll notice that IsNumPad and IsAutoRepeat properties of the key events are always false, because we don't have that information in .NET. We could have used more native Windows APIs to determine the properties, but they are non-essential and draw the focus away from the sample's purpose, integrating Coherent UI.

Listeners.cs

You'll find the ContextListener and MyViewListener classes here.

The ContextListener class is the system listener, which serves the purpose to notify the user when the view context is ready to operate.

The MyViewListener class is what handles the important events for drawing. Since SharpDX supports shared textures, we can use this image transport mechanism for our view. To do that we must override the ViewListener.CreateSurface and ViewListener.DestroySurface methods, and also subscribe for the Draw event.

The CreateSurface method asks SharpDX to create a shared surface with the specified width and height using a 32-bit BGRA format and returns the shared handle for the texture.
The DestroySurface method just disposes the texture associated with the handle.
The OnDraw handler receives a shared handle, obtains a Texture2D from it and invokes the DirectX11 context's CopySubresourceRegion method. This way the surface received from Coherent UI is copied to the texture that is drawn in the DoFrame method of the application.

There is another overload of OnDraw, receiving an array of pixels as input. It creates a DataStream from the pixels and calls the context's UpdateSubresource to upload the stream to video memory. This overload is used when the view is created with the shared memory flag (The ViewInfo.UsesSharedMemory property). The shared texture transport is preferred, but shared memory is supported for SharpDX as well.