/// <summary> /// Registers the given resource for unloading. /// </summary> /// <param name="drawing2DResourceBase">The resource to be unloaded.</param> internal void RegisterForUnload(Drawing2DResourceBase drawing2DResourceBase) { _drawing2DResourcesToUnload.Enqueue(drawing2DResourceBase); }
/// <summary> /// Starts the engine's main loop. /// </summary> /// <param name="cancelToken"></param> /// <returns></returns> internal Task Start(CancellationToken cancelToken) { if (m_runningTask != null) { throw new SeeingSharpGraphicsException("Unable to start engine's main loop: Loop has already started!"); } m_runningTask = Task.Factory.StartNew(async() => { Stopwatch renderStopWatch = new Stopwatch(); renderStopWatch.Start(); List <RenderLoop> renderingRenderLoops = new List <RenderLoop>(16); List <Scene> scenesToRender = new List <Scene>(16); List <Camera3DBase> camerasToUpdate = new List <Camera3DBase>(16); List <EngineDevice> devicesInUse = new List <EngineDevice>(16); List <InputFrame> inputFrames = new List <InputFrame>(16); UpdateState updateState = new UpdateState(TimeSpan.Zero); while (!cancelToken.IsCancellationRequested) { bool exceptionOccurred = false; try { using (var perfToken = m_host.BeginMeasureActivityDuration(Constants.PERF_GLOBAL_PER_FRAME)) { // Wait some time before doing anything.. double lastRenderMilliseconds = renderStopWatch.GetTrueElapsedMilliseconds(); double delayTime = Constants.MINIMUM_FRAME_TIME_MS - lastRenderMilliseconds; if (delayTime < Constants.MINIMUM_DELAY_TIME_MS) { delayTime = Constants.MINIMUM_DELAY_TIME_MS; } using (var perfTokenInner = m_host.BeginMeasureActivityDuration(Constants.PERF_GLOBAL_WAIT_TIME)) { CommonTools.MaximumDelay(delayTime); } // Get all render loops renderingRenderLoops.Clear(); lock (m_registeredRenderLoopsLock) { renderingRenderLoops.AddRange(m_registeredRenderLoops); } // Queries for devices / scenes in use QueryForScenesAndCameras(renderingRenderLoops, scenesToRender, camerasToUpdate); QueryForDevicesInUse(renderingRenderLoops, devicesInUse); // Build new UpdateState object TimeSpan updateTime = renderStopWatch.Elapsed; if (updateTime.TotalMilliseconds > 100.0) { updateTime = TimeSpan.FromMilliseconds(100.0); } updateState.Reset(updateTime); // Restart the stopwatch renderStopWatch.Restart(); // Get all input frames m_host.InputGatherer.QueryForCurrentFrames(inputFrames); // First global pass: Update scene and prepare rendering await UpdateAndPrepareRendering(renderingRenderLoops, scenesToRender, devicesInUse, inputFrames, updateState) .ConfigureAwait(false); foreach (Camera3DBase actCamera in camerasToUpdate) { actCamera.AnimationHandler.Update(updateState); } // Queries for devices / scenes in use (may have changed during prepare) QueryForScenesAndCameras(renderingRenderLoops, scenesToRender, camerasToUpdate); QueryForDevicesInUse(renderingRenderLoops, devicesInUse); // Second global pass: Render scene(s) and update beside RenderAndUpdateBeside(renderingRenderLoops, scenesToRender, devicesInUse, updateState); // Raise generic input event (if registered) if (this.GenericInput != null) { this.GenericInput.Raise(this, new GenericInputEventArgs(inputFrames)); } // Clear unreferenced Scenes finally lock (m_scenesForUnloadLock) { foreach (Scene actScene in m_scenesForUnload) { actScene.UnloadResources(); actScene.Clear(true); } m_scenesForUnload.Clear(); } // Unload all Direct2D resources which are not needed anymore Drawing2DResourceBase act2DResourceToUnload = null; while (m_drawing2DResourcesToUnload.TryDequeue(out act2DResourceToUnload)) { foreach (EngineDevice actDevice in m_host.Devices) { act2DResourceToUnload.UnloadResources(actDevice); } } } } catch (Exception) { exceptionOccurred = true; } // Execute global awaiters while (m_globalLoopAwaiters.Count > 0) { Action currentAction = null; if (m_globalLoopAwaiters.TryDequeue(out currentAction)) { currentAction(); } } if (exceptionOccurred) { // Wait some time and try rendering again await Task.Delay(1000); } // Handle suspend / resume Task suspendWaiter = m_suspendWaiter; if (suspendWaiter != null) { m_suspendCallWaiterSource.TrySetResult(null); #if UNIVERSAL // Call Trim on all devices foreach (EngineDevice actDevice in m_host.Devices) { if (actDevice.DeviceDxgi != null) { actDevice.DeviceDxgi.Trim(); } } #endif // Wait for resuming await suspendWaiter; } } }); return(m_runningTask); }