How to render graphics from byte buffer in WinUI3? - c#

I am receiving real time graphics data via:
void Data(ref UInt8[] buffer, UInt64 length);
How can I display this data in a Window in WinUI3 via C#?
The WritableBitmap class in UWP looks promising, but I haven't found a WinUI equivalent.
CanvasRenderTarget from the Win2D for WinUI3 library also sounds promising, but that library doesn't look stable yet.
Any ideas?

If Win2d is good enough for you that's the easy way. If you want lower level control at some point, you can use SharpDX or perhaps better, it's spiritual successor Vortice. They are DirectX wrappers that can output to the Xaml SwapChainPanel in WinUI. There is also TerraFX, another DirectX wrapper which uses non-standard, lower level C# without trying to match any C# conventions. It's the most difficult option, but it's available if you need a little bit more performance.
SharpDX is retired, but it's very stable, and has been in use for many years.
https://github.com/amerkoleci/Vortice.Windows
http://sharpdx.org/
https://github.com/terrafx/terrafx.interop.windows
All these are available as nuget packages as well.

Related

How do I render a WPF window into a DirectX buffer?

I would like to render a WPF window into my own buffer and add that to my render loop, render into my own scene, manipulate it, etc.
Is there a built-in way to capture a WPF window's underlying DirectX buffer/swapchain?
Possible but not easy.
There is two great articles I found when trying to achieve this both by Jeremiah Morrill.
A Critical Deep Dive into the WPF Rendering System
How to get access to WPF’s internal Direct3D guts
Jeremiah uses a Dynamic Link Library (DLL) written in C++ which hooks DirectX functions and exports initialise function in which C# calls.
This still does not stop you using Easyhook and doing it in C# instead of Jeremiahs way.
When doing this myself this was my flow:
Hook DirectX 9/10/11 (EndScene, Present)..
Callback in .NET domain
and marshal buffer data using PInvoke
Manipulate buffer return
manipulated buffer back to C++ library
Display the final image buffer to
screen i.e WPF...
My experience: there is a compromise on speed but I believe with optimisation could be viable solution.
Another Solution
You could EAT hook D3D11CreateDeviceAndSwapChain which is an export from d3d11.dll. This won't work since WPF uses DirectX 9 under the hood.
Update:
After reversing WPF over the weekend I found unmanaged wpfgfx.dll which contains milcore which is a wrapper around DirectX 9 still to this date. You can see this in the below diagram of WPF Architecture.
Inside the module wpfgfx.dll it contains MILCore DirectX wrapper. More about WPF Architecture

UWP support for opengl

guys.
I have a opengl library written in c++. I know that I can use Angle but because of this I will need to code in C++ my whole app. Is there a way to use c++ opengl in UWP and still use C# as a main language?
Is there a way to use c++ opengl in UWP and still use C# as a main language?
ANGLE is currently the only way to get the OpenGL API to run in UWP. For more details please reference this thread.
You can write your own via interop if you want to use ANGLE from c#. You can also write your low level OpenGL stuff in C++ and wrapper it. Then invoke it by C# in your logic level.
Fortunately, people who have the same requirements as you created an issue on the GitHub a few days ago, and got samples from #mattleibow. #mattleibow shared his code in this thread. For more details, please reference OpenGL surface from SwapChainPanel declared in XAML in C#.
Additionally, ANGLE is actually for translating OpenGL ES to DirectX. So I recommend you to use Win2D instead. Win2D is a new, immediate mode 2D drawing framework for XAML applications on Windows 8.1 and Windows 10. It is built on the high performance foundation of DirectX, but is designed to provide the convenience and ease of use expected by C# and .NET developers. More details you can reference this video.
I realize it's been a long time since you posed this question, but I needed to do this also, so put together a small C# project that shows how to use ANGLE from C#. It includes minimal bindings to GL from C# (since OpenTK does not yet target UWP), and a part of the template application from ANGLE itself, which has all the SwapChainPanel, etc, setup. You can check it out here.
I had this issue, so I created my own library.
You could use my new library (https://github.com/hamarb123/hamarb123.SharpGLES) which runs OpenGL ES on UWP (and .NET Framework), it even has a GLESSwapChainPanel class for ease of use.

Program architecture for Windows 8 C# & XAML app featuring graphics

If I'm looking to create a game that doesn't necessarily run full screen, but simply needs to feature 2D/3D graphics somewhere in a portion of the screen, what's my best approach?
Some specific questions could be:
What component would the rendered area use?
Are there any game libraries I could leverage for the rendered area?
What would be the most "pure" or "canonical" stack according to Microsoft to use here?
Omega --
Visual Studio 2010 and 2012 are Both WPF apps. WinRT is for Tablets / Mobile. WPF is certainly NOT outdated.
If I were you I wouldn't render everything out the way canvas forces you to, it might be a better approach to have the center item be a UI element named Frame, which is the base element for all UI related content in WPF.
In this way you would be able to leverage all of the possible types of controls in the Frame whether you decided that An ImageSourceType or Canvas is more applicable to a particular features of the game.
Depending on how you want to draw graphics, you could use (but are by no means limited to):
Canvas - which would be totally appropriate for slow moving games. This way you get the benefit of the various WPF layout routines and can define objects inside the scene in XAML/vectors as well.
WPF supports 3D graphics (using Direct3D on the backend) so you could probably set up an orthogonal projection matrix and treat it like a Direct3D context (with the WPF API). I don't have enough experience to know how slow this is compared to D3D, but it's certainly easier (built-in "scene graph" like support from the XAML architecture, for instance).
If you want to go whole-hog with Direct3D you could use SlimDX, which has a WPF shim that I've used in the past, as well as another third party control. There may be other libraries available as well.
Direct blitting to/from a Bitmap using WriteableBitmap (see WriteableBitmapEx for a third-party version with a much friendlier API) or similar.
There are probably a lot of other options too. My preference would be for using Canvas initially if it's a slow-paced game that doesn't need super-fast frame rates (the layout work does incur a fairly substantial overhead, but it's less work and may be easier to get looking exactly the way you want).
If you want absolute control and speed, use D3D through SlimDX, but this is a pretty hefty learning curve if you're new to it.

How would one go about creating one’s own graphics effect in WPF?

I have an Image object in my application which the user can drag around. The object displays an image which is partly transparent, so the window background (which is itself a bitmap) can be seen through it.
I want to add a graphics effect to this object. Assume that I already have an algorithm for this effect — that’s not the issue. The issue is how to get this algorithm into WPF.
So I tried to look at how DropShadowEffect works, but the implementation displayed in Reflector is empty. I also tried to look at what methods from the abstract classes Effect and ShaderEffect I should override and there doesn’t seem to be anything related to actually rendering an effect.
So how do I create my own effect?
The best and fastest way is to use pixel shaders (supported starting with WPF 3.5 SP1 I think) . It will require some shader language (HLSL) knowledge, though :-)
Here is a tutorial: How Do I: Create Custom Pixel Shader Effects for WPF
a library on codeplex: Windows Presentation Foundation Pixel Shader Effects Library
an article with .NET 4 information (including Sliverlight support which has it too): SilverShader – Introduction to Silverlight and WPF Pixel Shaders
A very cool tool (and resource) is Shazzam it will help you to create the effects and it contains a nice tutorial.

Rendering graphics in C#

Is there another way to render graphics in C# beyond GDI+ and XNA?
(For the development of a tile map editor.)
SDL.NET is the solution I've come to love. If you need 3D on top of it, you can use Tao.OpenGL to render inside it. It's fast, industry standard (SDL, that is), and cross-platform.
Yes, I have written a Windows Forms control that wraps DirectX 9.0 and provides direct pixel level manipulation of the video surface.
I actually wrote another post on Stack Overflow asking if there are other better approaches: Unsafe C# and pointers for 2D rendering, good or bad?
While it is relatively high performance, it requires the unsafe compiler option as it uses pointers to access the memory efficiently. Hence the reason for this earlier post.
This is a high level of the required steps:
Download the DirectX SDK.
Create a new C# Windows Forms project and reference the installed
Microsoft DirectX assembly.
Initialize a new DirectX Device object with Presentation Parameters
(windowed, back buffering, etc.) you require.
Create the Device, taking care to record the surface "Pitch" and
current display mode (bits per pixel).
When you need to display something, Lock the backbuffer
surface and store the returned pointer to the start of surface
memory.
Use pointer arithmetic, calculate the actual pixel position in the
data based on the surface pitch,
bits per pixel and the actual x/y pixel coordinate.
In my case for simplicity I am sticking to 32 bpp, meaning setting a pixel is as simple as: *(surfacePointer + (y * pitch + x))=Color.FromARGB(255,0,0);
When finished drawing, Unlock the back buffer surface. Present the surface.
Repeat from step 5 as required.
Be aware that taking this approach you need to be very careful about checking the current display mode (pitch and bits per pxiel) of the target surface. Also you will need to have a strategy in place to deal with window resizing or changes of screen format while your program is running.
Managed DirectX (Microsoft.DirectX namespace) for faster 3D graphics. It's a solid .NET wrapper over DirectX API, which comes with a bit of performance hit for creating .NET objects and marshalling. Unless you are writing a full featured modern 3D engine, it will work fine.
Window Presentation Foundation (WPF) (Windows.Media namespace) - best choice for 2D graphics. Also has limited 3D abilities. Aimed to replace Windows Forms with vector, hardware accelerated resolution-independent framework. Very convenient, supports several flavours of custom controls, resources, data binding, events and commands... also has a few WTFs. Speed is usually faster than GDI and slower than DirectX, and depends greatly on how you do things (seen something to work 60 times faster after rewriting in a sensible way). We had a success implementing 3 1280x1024 screens full of real-time indicators, graphs and plots on a single (and not the best) PC.
You could try looking into WPF, using Visual Studio and/or Expression Blend. I'm not sure how sophisticated you're trying to get, but it should be able to handle a simple editor. Check out this MSDN Article for more info.
You might look into the Cairo graphics library. The Mono project has bindings for C#.
Cairo is an option. I'm currently rewriting my mapping software using both GDI+ and Cairo. It has a tile map generator, among other features.

Categories