Stap 4: Maak de laag van de ondersteuning Windows leiden
We moeten nu Microsofts OpenGLES helper klasse toevoegen aan de gedeelde C++ brug-project. Dus voeg gewoon een klasse met de naam OpenGLES aan dat project.
De kop moet bevatten:
#pragma once#include <EGL/egl.h>
class OpenGLES { public: OpenGLES(); ~OpenGLES();
EGLSurface CreateSurface(Windows::UI::Xaml::Controls::SwapChainPanel^ panel, const Windows::Foundation::Size* renderSurfaceSize); void DestroySurface(const EGLSurface surface); void MakeCurrent(const EGLSurface surface); EGLBoolean SwapBuffers(const EGLSurface surface); void Reset();
private: void Initialize(); void Cleanup();
private: EGLDisplay mEglDisplay; EGLContext mEglContext; EGLConfig mEglConfig; };
En de cpp-bestand moet bevatten:
#include "OpenGLES.h"#include <concrt.h> #include <EGL/egl.h> #include <EGL/eglext.h> #include <EGL/eglplatform.h> #include <angle_windowsstore.h>
using namespace Platform; using namespace Windows::UI::Xaml::Controls; using namespace Windows::Foundation; using namespace Windows::Foundation::Collections;
OpenGLES::OpenGLES(): mEglConfig(nullptr), mEglDisplay(EGL_NO_DISPLAY), mEglContext(EGL_NO_CONTEXT) {Initialize(); OpenGLES::OpenGLES() : mEglConfig(nullptr), mEglDisplay(EGL_NO_DISPLAY), mEglContext(EGL_NO_CONTEXT) { Initialize(); }
OpenGLES::~OpenGLES() {Cleanup(); OpenGLES::~OpenGLES() { Cleanup(); }
void OpenGLES::Initialize() { const EGLint configAttributes[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 8, EGL_STENCIL_SIZE, 8, EGL_NONE };
const EGLint contextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
const EGLint defaultDisplayAttributes[] = { // These are the default display attributes, used to request ANGLE's D3D11 renderer. // eglInitialize will only succeed with these attributes if the hardware supports D3D11 Feature Level 10_0+. EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
// EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER is an optimization that can have large performance benefits on mobile devices. // Its syntax is subject to change, though. Please update your Visual Studio templates if you experience compilation issues with it. EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE,
// EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE is an option that enables ANGLE to automatically call // the IDXGIDevice3::Trim method on behalf of the application when it gets suspended. // Calling IDXGIDevice3::Trim when an application is suspended is a Windows Store application certification requirement. EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE, EGL_NONE, };
const EGLint fl9_3DisplayAttributes[] = { // These can be used to request ANGLE's D3D11 renderer, with D3D11 Feature Level 9_3. // These attributes are used if the call to eglInitialize fails with the default display attributes. EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9, EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3, EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE, EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE, EGL_NONE, };
const EGLint warpDisplayAttributes[] = { // These attributes can be used to request D3D11 WARP. // They are used if eglInitialize fails with both the default display attributes and the 9_3 display attributes. EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE, EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, EGL_TRUE, EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, EGL_TRUE, EGL_NONE, };
EGLConfig config = NULL;
eglGetPlatformDisplayEXT is een alternatief voor eglGetDisplay. Het toelaat ons te passeren bij display Kenmerkenop gebruikt voor het configureren van D3D11. PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast (eglGetProcAddress("eglGetPlatformDisplayEXT")); Als (! eglGetPlatformDisplayEXT) {gooien Exception::CreateException (E_FAIL, L "Failed to get functie eglGetPlatformDisplayEXT"); // eglGetPlatformDisplayEXT is an alternative to eglGetDisplay. It allows us to pass in display attributes, used to configure D3D11. PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast (eglGetProcAddress("eglGetPlatformDisplayEXT")); if (!eglGetPlatformDisplayEXT) { throw Exception::CreateException(E_FAIL, L"Failed to get function eglGetPlatformDisplayEXT"); }
// // To initialize the display, we make three sets of calls to eglGetPlatformDisplayEXT and eglInitialize, with varying // parameters passed to eglGetPlatformDisplayEXT: // 1) The first calls uses "defaultDisplayAttributes" as a parameter. This corresponds to D3D11 Feature Level 10_0+. // 2) If eglInitialize fails for step 1 (e.g. because 10_0+ isn't supported by the default GPU), then we try again // using "fl9_3DisplayAttributes". This corresponds to D3D11 Feature Level 9_3. // 3) If eglInitialize fails for step 2 (e.g. because 9_3+ isn't supported by the default GPU), then we try again // using "warpDisplayAttributes". This corresponds to D3D11 Feature Level 11_0 on WARP, a D3D11 software rasterizer. // // Note: On Windows Phone, we #ifdef out the first set of calls to eglPlatformDisplayEXT and eglInitialize. // Windows Phones devices only support D3D11 Feature Level 9_3, but the Windows Phone emulator supports 11_0+. // We use this #ifdef to limit the Phone emulator to Feature Level 9_3, making it behave more like // real Windows Phone devices. // If you wish to test Feature Level 10_0+ in the Windows Phone emulator then you should remove this #ifdef. //
#if (WINAPI_FAMILY! = WINAPI_FAMILY_PHONE_APP) / / dit probeert te initialiseren EGL naar D3D11 functie niveau 10_0 +. Zie bovenstaande opmerking voor meer informatie. mEglDisplay = eglGetPlatformDisplayEXT (EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, defaultDisplayAttributes); Als (mEglDisplay == EGL_NO_DISPLAY) {gooien Exception::CreateException (E_FAIL, L "Failed to get EGL weergave"); #if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) // This tries to initialize EGL to D3D11 Feature Level 10_0+. See above comment for details. mEglDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, defaultDisplayAttributes); if (mEglDisplay == EGL_NO_DISPLAY) { throw Exception::CreateException(E_FAIL, L"Failed to get EGL display"); }
Als (eglInitialize (mEglDisplay, NULL, NULL) == EGL_FALSE) if (eglInitialize(mEglDisplay, NULL, NULL) == EGL_FALSE) #endif { // This tries to initialize EGL to D3D11 Feature Level 9_3, if 10_0+ is unavailable (e.g. on Windows Phone, or certain Windows tablets). mEglDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, fl9_3DisplayAttributes); if (mEglDisplay == EGL_NO_DISPLAY) { throw Exception::CreateException(E_FAIL, L"Failed to get EGL display"); } {/ / Dit probeert te initialiseren EGL te D3D11 functie niveau 9_3, als 10_0 + niet beschikbaar (bijvoorbeeld op Windows Phone, of bepaalde Windows-tabletten is). mEglDisplay = eglGetPlatformDisplayEXT (EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, fl9_3DisplayAttributes); if (mEglDisplay == EGL_NO_DISPLAY) {gooien Exception::CreateException (E_FAIL, L "Failed to get EGL weergave"); if (eglInitialize(mEglDisplay, NULL, NULL) == EGL_FALSE) { // This initializes EGL to D3D11 Feature Level 11_0 on WARP, if 9_3+ is unavailable on the default GPU (e.g. on Surface RT). mEglDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, warpDisplayAttributes); if (mEglDisplay == EGL_NO_DISPLAY) { throw Exception::CreateException(E_FAIL, L"Failed to get EGL display"); }
Als (eglInitialize (mEglDisplay, NULL, NULL) == EGL_FALSE) {/ / Dit initialiseert EGL te D3D11 functie niveau 11_0 op WARP, als 9_3 + niet beschikbaar op de standaard GPU (bijvoorbeeld op de Surface RT is). mEglDisplay = eglGetPlatformDisplayEXT (EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, warpDisplayAttributes); if (mEglDisplay == EGL_NO_DISPLAY) {gooien Exception::CreateException (E_FAIL, L "Failed to get EGL weergave"); if (eglInitialize(mEglDisplay, NULL, NULL) == EGL_FALSE) { // If all of the calls to eglInitialize returned EGL_FALSE then an error has occurred. throw Exception::CreateException(E_FAIL, L"Failed to initialize EGL"); } } }
Als (eglInitialize (mEglDisplay, NULL, NULL) == EGL_FALSE) {/ / als alle van de oproepen naar eglInitialize EGL_FALSE geretourneerd dan er is een fout opgetreden. gooien Exception::CreateException (E_FAIL, L "Failed to initialize EGL"); EGLint numConfigs = 0; if ((eglChooseConfig(mEglDisplay, configAttributes, &mEglConfig, 1, &numConfigs) == EGL_FALSE) || (numConfigs == 0)) { throw Exception::CreateException(E_FAIL, L"Failed to choose first EGLConfig"); } } mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, contextAttributes); if (mEglContext == EGL_NO_CONTEXT) { throw Exception::CreateException(E_FAIL, L"Failed to create EGL context"); } }
EGLint numConfigs = 0; Als ((eglChooseConfig (mEglDisplay, configAttributes, & mEglConfig, 1, & numConfigs) == EGL_FALSE) || (numConfigs == 0)) {gooien Exception::CreateException (E_FAIL, L 'Mislukt om te kiezen van de eerste EGLConfig'); void OpenGLES::Cleanup() { if (mEglDisplay != EGL_NO_DISPLAY && mEglContext != EGL_NO_CONTEXT) { eglDestroyContext(mEglDisplay, mEglContext); mEglContext = EGL_NO_CONTEXT; }
mEglContext = eglCreateContext (mEglDisplay, mEglConfig, EGL_NO_CONTEXT, contextAttributes); Als (mEglContext == EGL_NO_CONTEXT) {gooien Exception::CreateException (E_FAIL, L "Failed to create EGL kader"); if (mEglDisplay != EGL_NO_DISPLAY) { eglTerminate(mEglDisplay); mEglDisplay = EGL_NO_DISPLAY; } }
VOID OpenGLES::Cleanup() {als (mEglDisplay! = EGL_NO_DISPLAY & & mEglContext! = EGL_NO_CONTEXT) {eglDestroyContext (mEglDisplay, mEglContext); mEglContext = EGL_NO_CONTEXT; void OpenGLES::Reset() { Cleanup(); Initialize(); }
Als (mEglDisplay! = EGL_NO_DISPLAY) {eglTerminate(mEglDisplay); mEglDisplay = EGL_NO_DISPLAY; EGLSurface OpenGLES::CreateSurface(SwapChainPanel^ panel, const Size* renderSurfaceSize) { if (!panel) { throw Exception::CreateException(E_INVALIDARG, L"SwapChainPanel parameter is invalid"); }
VOID OpenGLES::Reset() {Cleanup(); Initialize(); EGLSurface surface = EGL_NO_SURFACE;
EGLSurface OpenGLES::CreateSurface(SwapChainPanel^ panel, const Size* renderSurfaceSize) {als (! deelvenster) {Exception::CreateException gooien (E_INVALIDARG, L "SwapChainPanel parameter is ongeldig"); const EGLint surfaceAttributes[] = { // EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER is part of the same optimization as EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER (see above). // If you have compilation issues with it then please update your Visual Studio templates. EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER, EGL_TRUE, EGL_NONE };
// Create a PropertySet and initialize with the EGLNativeWindowType. PropertySet^ surfaceCreationProperties = ref new PropertySet(); surfaceCreationProperties->Insert(ref new String(EGLNativeWindowTypeProperty), panel);
// If a render surface size is specified, add it to the surface creation properties if (renderSurfaceSize != nullptr) { surfaceCreationProperties->Insert(ref new String(EGLRenderSurfaceSizeProperty), PropertyValue::CreateSize(*renderSurfaceSize)); }
surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, reinterpret_cast(surfaceCreationProperties), surfaceAttributes); if (surface == EGL_NO_SURFACE) { throw Exception::CreateException(E_FAIL, L"Failed to create EGL surface"); }
Als een render oppervlakte grootte wordt opgegeven, toevoegen aan de creatie van de oppervlakte-eigenschappen als (renderSurfaceSize! = nullptr) {surfaceCreationProperties -> invoegen (ref nieuwe String(EGLRenderSurfaceSizeProperty), PropertyValue::CreateSize(*renderSurfaceSize)); return surface; }
oppervlakte = eglCreateWindowSurface (mEglDisplay, mEglConfig, reinterpret_cast(surfaceCreationProperties), surfaceAttributes); Als (oppervlak == EGL_NO_SURFACE) {gooien Exception::CreateException (E_FAIL, L 'Failed to create surface EGL'); void OpenGLES::DestroySurface(const EGLSurface surface) { if (mEglDisplay != EGL_NO_DISPLAY && surface != EGL_NO_SURFACE) { eglDestroySurface(mEglDisplay, surface); } }
retourneren oppervlak; void OpenGLES::MakeCurrent(const EGLSurface surface) { if (eglMakeCurrent(mEglDisplay, surface, surface, mEglContext) == EGL_FALSE) { throw Exception::CreateException(E_FAIL, L"Failed to make EGLSurface current"); } }
VOID OpenGLES::DestroySurface (const EGLSurface oppervlak) {als (mEglDisplay! = EGL_NO_DISPLAY & & oppervlakte! = EGL_NO_SURFACE) {eglDestroySurface (mEglDisplay, oppervlakte); EGLBoolean OpenGLES::SwapBuffers(const EGLSurface surface) { return (eglSwapBuffers(mEglDisplay, surface)); }
VOID OpenGLES::MakeCurrent (const EGLSurface oppervlak) {als (eglMakeCurrent (mEglDisplay, oppervlak, oppervlak, mEglContext) == EGL_FALSE) {gooien Exception::CreateException (E_FAIL, L 'Failed to EGLSurface huidige maken'); #if WIN_STORE
EGLBoolean OpenGLES::SwapBuffers(const EGLSurface surface) {terugkeer (eglSwapBuffers (mEglDisplay, oppervlakte)); #define MAINNAMESPACE CppGLESBridge_Win
Nogmaals, dit is ontleend aan de hoek-sjabloon en er werken moet u waarschijnlijk niet aangaan. Opnieuw proberen om te zien als je nog steeds kunt compileren.
OK dus nu voor de CX (Microsofts C++ extensies runtimeversie van C++ of wat het ook wordt genoemd) / (de versie van C++ met de syntaxis van de erg grappig) wrapper die ons van een naadloze interface C# voorzien zal. Voor nu zullen we een gewijzigde versie van de code van de MainPage gevonden op de hoek sjabloon gebruiken. Deze pagina is nu eigenlijk een C#-object, zal er voor het routeren van de gebeurtenissen vanuit C#, c++. De rest van de code kan worden gehouden, zoals in de C++ over dingen simpel te houden.
Dus nu een klasse Maak in de gedeelde brug klasse en noem het GLESSurface. De klasse zal moeten worden in een naamruimte met dezelfde naam als uw project, want anders u fouten krijgt wanneer het proberen om te compileren de C#-toepassing. Het probleem is dat deze naamruimte verschillend voor de Windows Store en Windows Phone-projecten is. Om dit te verhelpen moeten we toevoegen sommige preprocessor richtlijnen. Voor het project winkel voegde ik "WIN_STORE" en voor de WP een "WIN_PHONE". Misschien zijn er al enkele standaard degenen die zou werken, maar ik kon ze niet vinden en ging gewoon met deze. Met deze kan ik dan het volgende in het headerbestand gebruik om de benodigde naamruimte te definiëren:
#else if WIN_PHONE
#define MAINNAMESPACE CppGLESBridge_WP
#endif
using Windows.UI.Core;
using CppGLESBridge_Win;
U zal moeten veranderen om wat uw projecten worden genoemd. Mij worden "CppGLESBridge.Win" en "CppGLESBride.WP" genoemd. U had moeten kunnen zien wat u nodig hebt in de standaardbestanden Class1 naamruimte. Ook kunt u iets in die aard overal in uw C++-code waar u wilt iets voor WP en Win winkel iets anders doen.
Nu proberen compileren om te zien of alles werkt. Dit is ook nodig omdat de Runtime-Component worden gecompileerd moet voordat Intellisense beginnen kan om het te registreren wanneer we overgaan tot C#.
Als alles ging goed tot nu toe dan u zeer zult dichtbij zien iets over nu, maar we er nog niet zijn. Alleen zal komen we bij Xamarin.Forms aan het einde van de tutorial. Voor nu willen we gewoon een gekleurde draaiende kubus zien op alle platformen. Dus ga naar je bestanden MainPage.xaml voor zowel Win als WP en voeg dat een SwapChainPanel met de naam iets als "swapPanel" zoals kan worden gezien in de screenshots.
Nu moet u de MainPage.xaml.cs voor elk project openen. Voeg de volgende instructies:
gs = new GLESSurface(swapPanel);
CoreWindow window = Window.Current.CoreWindow;
De CppGLESBridge_Win is voor Win-archief en vervolgens voor WP u CppGLESBridge_WP moet gebruiken. Als je ging met een andere naam dan gewoon passen deze op de juiste manier. Vervolgens maakt een privé GLESSurface object in beide projecten. We zullen de gebeurtenissen worden routering naar dit object. Dan moet u dit object maken en de nodige C# gebeurtenissen verbinden met de C++-handlers. U wilt uw MainPage() methode naar de volgende aanvulling op wat er al was uiteindelijk te bevatten:
window.VisibilityChanged += (CoreWindow sender, VisibilityChangedEventArgs args) => { gs.OnVisibilityChanged(sender, args); };
swapPanel.SizeChanged += (object sender, SizeChangedEventArgs e) => { gs.OnSwapChainPanelSizeChanged(sender, e); };
this.Loaded += (object sender, RoutedEventArgs e) => { gs.OnPageLoaded(sender, e); };
using System.Runtime.InteropServices;using Android.Opengl;
namespace CppGLESXamarin.Android { class MyGLRenderer : Java.Lang.Object, GLSurfaceView.IRenderer { [DllImport("libAndroidGLESBridge.so")] public static extern void on_surface_created();
Er zijn screenshots voor zowel de afgewerkte Windows Phone en Windows Store bestanden.
Als u bouwen en uitvoeren van uw projecten moet nu je zien een draaiende kubus.