Beispiel #1
0
        /// Creates a copy of the settings.
        internal GLWpfControlSettings Copy()
        {
            var c = new GLWpfControlSettings {
                GraphicsContextFlags   = GraphicsContextFlags,
                MajorVersion           = MajorVersion,
                MinorVersion           = MajorVersion,
                UseHardwareRender      = UseHardwareRender,
                PixelBufferObjectCount = PixelBufferObjectCount
            };

            return(c);
        }
        private static IGraphicsContext GetOrCreateSharedOpenGLContext(GLWpfControlSettings settings)
        {
            if (_sharedContext != null)
            {
                var isSameContext = GLWpfControlSettings.WouldResultInSameContext(settings, _sharedContextSettings);
                if (!isSameContext)
                {
                    throw new ArgumentException($"The provided {nameof(GLWpfControlSettings)} would result" +
                                                $"in a different context creation to one previously created. To fix this," +
                                                $" either ensure all of your context settings are identical, or provide an " +
                                                $"external context via the '{nameof(GLWpfControlSettings.ContextToUse)}' field.");
                }
            }

            else
            {
                var nws = NativeWindowSettings.Default;
                nws.StartFocused    = false;
                nws.StartVisible    = false;
                nws.NumberOfSamples = 0;
                // if we ask GLFW for 1.0, we should get the highest level context available with full compat.
                nws.APIVersion = new Version(settings.MajorVersion, settings.MinorVersion);
                nws.Flags      = ContextFlags.Offscreen | settings.GraphicsContextFlags;
                // we have to ask for any compat in this case.
                nws.Profile      = settings.GraphicsProfile;
                nws.WindowBorder = WindowBorder.Hidden;
                nws.WindowState  = WindowState.Minimized;
                var glfwWindow = new NativeWindow(nws);
                var provider   = new GLFWBindingsContext();
                Wgl.LoadBindings(provider);
                // we're already in a window context, so we can just cheat by creating a new dependency object here rather than passing any around.
                var depObject = new DependencyObject();
                // retrieve window handle/info
                var window     = Window.GetWindow(depObject);
                var baseHandle = window is null ? IntPtr.Zero : new WindowInteropHelper(window).Handle;
                var hwndSource = new HwndSource(0, 0, 0, 0, 0, "GLWpfControl", baseHandle);

                _sharedContext          = glfwWindow.Context;
                _sharedContextSettings  = settings;
                _sharedContextResources = new IDisposable[] { hwndSource, glfwWindow };
                // GL init
                // var mode = new GraphicsMode(ColorFormat.Empty, 0, 0, 0, 0, 0, false);
                // _commonContext = new GraphicsContext(mode, _windowInfo, _settings.MajorVersion, _settings.MinorVersion,
                //     _settings.GraphicsContextFlags);
                // _commonContext.LoadAll();
                _sharedContext.MakeCurrent();
            }
            Interlocked.Increment(ref _sharedContextReferenceCount);
            return(_sharedContext);
        }
Beispiel #3
0
        /// Creates a copy of the settings.
        internal GLWpfControlSettings Copy()
        {
            var c = new GLWpfControlSettings {
                ContextToUse         = ContextToUse,
                GraphicsContextFlags = GraphicsContextFlags,
                GraphicsProfile      = GraphicsProfile,
                MajorVersion         = MajorVersion,
                MinorVersion         = MinorVersion,
                RenderContinuously   = RenderContinuously,
                UseDeviceDpi         = UseDeviceDpi
            };

            return(c);
        }
        public DxGlContext([NotNull] GLWpfControlSettings settings)
        {
            DXInterop.Direct3DCreate9Ex(DXInterop.DefaultSdkVersion, out var dxContextHandle);
            DxContextHandle = dxContextHandle;

            var deviceParameters = new PresentationParameters
            {
                Windowed               = 1,
                SwapEffect             = SwapEffect.Discard,
                DeviceWindowHandle     = IntPtr.Zero,
                PresentationInterval   = 0,
                BackBufferFormat       = Format.X8R8G8B8, // this is like A8 R8 G8 B8, but avoids issues with Gamma correction being applied twice.
                BackBufferWidth        = 1,
                BackBufferHeight       = 1,
                AutoDepthStencilFormat = Format.Unknown,
                BackBufferCount        = 1,
                EnableAutoDepthStencil = 0,
                Flags = 0,
                FullScreen_RefreshRateInHz = 0,
                MultiSampleQuality         = 0,
                MultiSampleType            = MultisampleType.None
            };

            DXInterop.CreateDeviceEx(
                dxContextHandle,
                0,
                DeviceType.HAL, // use hardware rasterization
                IntPtr.Zero,
                CreateFlags.HardwareVertexProcessing |
                CreateFlags.Multithreaded |
                CreateFlags.PureDevice,
                ref deviceParameters,
                IntPtr.Zero,
                out var dxDeviceHandle);
            DxDeviceHandle = dxDeviceHandle;

            // if the graphics context is null, we use the shared context.
            if (settings.ContextToUse != null)
            {
                GraphicsContext = settings.ContextToUse;
            }
            else
            {
                GraphicsContext = GetOrCreateSharedOpenGLContext(settings);
            }

            GlDeviceHandle = Wgl.DXOpenDeviceNV(dxDeviceHandle);
        }
        /// Starts the control and rendering, using the settings provided.
        public void Start(GLWpfControlSettings settings)
        {
            _settings         = settings;
            IsVisibleChanged += (_, args) => {
                if ((bool)args.NewValue)
                {
                    CompositionTarget.Rendering += OnCompTargetRender;
                }
                else
                {
                    CompositionTarget.Rendering -= OnCompTargetRender;
                }
            };

            Loaded   += OnLoaded;
            Unloaded += OnUnloaded;
        }
Beispiel #6
0
        internal static bool WouldResultInSameContext([NotNull] GLWpfControlSettings a, [NotNull] GLWpfControlSettings b)
        {
            if (a.MajorVersion != b.MajorVersion)
            {
                return(false);
            }

            if (a.MinorVersion != b.MinorVersion)
            {
                return(false);
            }

            if (a.GraphicsProfile != b.GraphicsProfile)
            {
                return(false);
            }

            if (a.GraphicsContextFlags != b.GraphicsContextFlags)
            {
                return(false);
            }

            return(true);
        }
Beispiel #7
0
 public GLWpfControlRenderer(GLWpfControlSettings settings)
 {
     _context = new DxGlContext(settings);
 }