Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        private void EndD3D(bool dispose)
        {
            DetachRenderables();
            if (surfaceD3D == null)
            {
                return;
            }

            surfaceD3D.IsFrontBufferAvailableChanged -= OnIsFrontBufferAvailableChanged;
            Source = null;
            RenderContext?.Dispose();
            Disposer.RemoveAndDispose(ref deferredRenderer);
            Disposer.RemoveAndDispose(ref surfaceD3D);
            Disposer.RemoveAndDispose(ref colorBufferView);
            Disposer.RemoveAndDispose(ref depthStencilBufferView);
            Disposer.RemoveAndDispose(ref colorBuffer);
            Disposer.RemoveAndDispose(ref depthStencilBuffer);
#if MSAA
            Disposer.RemoveAndDispose(ref renderTargetNMS);
#endif
            if (dispose && defaultEffectsManager != null)
            {
                (defaultEffectsManager as IDisposable)?.Dispose();
                if (effectsManager == defaultEffectsManager)
                {
                    effectsManager = null;
                }
                defaultEffectsManager = null;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        private void EndD3D(bool dispose)
        {
            DetachRenderables();
            RenderContext?.Dispose();
            Disposer.RemoveAndDispose(ref deferredContext);
            Disposer.RemoveAndDispose(ref deferredRenderer);
            // Disposer.RemoveAndDispose(ref surfaceD3D);
            Disposer.RemoveAndDispose(ref colorBufferView);
            Disposer.RemoveAndDispose(ref depthStencilBufferView);
            Disposer.RemoveAndDispose(ref colorBuffer);
            Disposer.RemoveAndDispose(ref depthStencilBuffer);
#if MSAA
            Disposer.RemoveAndDispose(ref renderTargetNMS);
#endif
            if (dispose && defaultEffectsManager != null)
            {
                (defaultEffectsManager as IDisposable)?.Dispose();
                if (effectsManager == defaultEffectsManager)
                {
                    effectsManager = null;
                }
                defaultEffectsManager = null;
            }
        }
        private void Render(TimeSpan timeStamp)
        {
            var device = this.device;

            if (device == null)
            {
                return;
            }
            if (swapChain == null)
            {
                return;
            }
            if (renderRenderable != null)
            {
                // ---------------------------------------------------------------------------
                // this part is done only if the scene is not attached
                // it is an attach and init pass for all elements in the scene-graph
                if (!sceneAttached)
                {
                    try
                    {
                        Light3DSceneShared.Reset();
                        sceneAttached      = true;
                        ClearColor         = renderRenderable.BackgroundColor;
                        IsShadowMapEnabled = renderRenderable.IsShadowMappingEnabled;

                        RenderTechnique = renderRenderable.RenderTechnique == null ? RenderTechniquesManager?.RenderTechniques[DefaultRenderTechniqueNames.Blinn] : renderRenderable.RenderTechnique;

                        if (renderContext != null)
                        {
                            renderContext.Dispose();
                        }
                        renderContext = new RenderContext(this, EffectsManager.GetEffect(RenderTechnique), device.ImmediateContext);
                        renderContext.EnableBoundingFrustum = EnableRenderFrustum;
                        if (EnableSharingModelMode && SharedModelContainer != null)
                        {
                            SharedModelContainer.CurrentRenderHost = this;
                            renderRenderable.Attach(SharedModelContainer);
                        }
                        else
                        {
                            renderRenderable.Attach(this);
                        }

                        RenderTechniquesManager.RenderTechniques.TryGetValue(DeferredRenderTechniqueNames.GBuffer, out gbuffer);
                        RenderTechniquesManager.RenderTechniques.TryGetValue(DeferredRenderTechniqueNames.Deferred, out deferred);

                        if (RenderTechnique == deferred)
                        {
                            deferredRenderer.InitBuffers(this, Format.R32G32B32A32_Float);
                        }
                        else if (RenderTechnique == gbuffer)
                        {
                            deferredRenderer.InitBuffers(this, Format.B8G8R8A8_UNorm);
                        }
                        SetDefaultRenderTargets(false);
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show("DPFCanvas: Error attaching element: " + string.Format(ex.Message), "Error");
                        Debug.WriteLine("DPFCanvas: Error attaching element: " + string.Format(ex.Message), "Error");
                        throw;
                    }
                }
                renderContext.TimeStamp = timeStamp;
                // ---------------------------------------------------------------------------
                // this part is per frame
                // ---------------------------------------------------------------------------
                if (EnableSharingModelMode && SharedModelContainer != null)
                {
                    SharedModelContainer.CurrentRenderHost = this;
                }
                ClearRenderTarget();
                if (RenderTechnique == deferred)
                {
                    // set G-Buffer
                    deferredRenderer.SetGBufferTargets(renderContext);

                    // render G-Buffer pass
                    renderRenderable.Render(renderContext);

                    // call deferred render
                    deferredRenderer.RenderDeferred(renderContext, renderRenderable);
                }
                else if (RenderTechnique == gbuffer)
                {
                    // set G-Buffer
                    deferredRenderer.SetGBufferTargets(targetWidth / 2, targetHeight / 2, renderContext);

                    // render G-Buffer pass
                    renderRenderable.Render(renderContext);

                    // reset render targets and run lighting pass
#if DoubleBuffer
                    deferredRenderer.RenderGBufferOutput(renderContext, ref backBuffer);
#else
                    this.deferredRenderer.RenderGBufferOutput(renderContext, ref this.backBuffer);
#endif
                }
                else
                {
                    renderRenderable.Render(renderContext);
                }
#if DoubleBuffer
                device.ImmediateContext.ResolveSubresource(colorBuffer, 0, backBuffer, 0, Format.B8G8R8A8_UNorm);
#endif
            }
        }
Ejemplo n.º 4
0
        private void Render()
        {
            var device = this.device;

            if (device == null)
            {
                return;
            }

            var renderTarget = colorBuffer;

            if (renderTarget == null)
            {
                return;
            }

            if (renderRenderable != null)
            {
                /// ---------------------------------------------------------------------------
                /// this part is done only if the scene is not attached
                /// it is an attach and init pass for all elements in the scene-graph
                if (!sceneAttached)
                {
                    try
                    {
                        Light3D.LightCount = 0;
                        sceneAttached      = true;
                        ClearColor         = renderRenderable.BackgroundColor;
                        IsShadowMapEnabled = renderRenderable.IsShadowMappingEnabled;

                        var blinn = RenderTechniquesManager.RenderTechniques[DefaultRenderTechniqueNames.Blinn];
                        RenderTechnique = renderRenderable.RenderTechnique == null ? blinn : renderRenderable.RenderTechnique;

                        if (renderContext != null)
                        {
                            renderContext.Dispose();
                        }
                        renderContext = new RenderContext(this, EffectsManager.GetEffect(RenderTechnique));
                        renderRenderable.Attach(this);

                        RenderTechniquesManager.RenderTechniques.TryGetValue(DeferredRenderTechniqueNames.GBuffer, out gbuffer);
                        RenderTechniquesManager.RenderTechniques.TryGetValue(DeferredRenderTechniqueNames.Deferred, out deferred);

                        if (RenderTechnique == deferred)
                        {
                            deferredRenderer.InitBuffers(this, Format.R32G32B32A32_Float);
                        }
                        else if (RenderTechnique == gbuffer)
                        {
                            deferredRenderer.InitBuffers(this, Format.B8G8R8A8_UNorm);
                        }
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show("DPFCanvas: Error attaching element: " + string.Format(ex.Message), "Error");
                        Debug.WriteLine("DPFCanvas: Error attaching element: " + string.Format(ex.Message), "Error");
                        throw;
                    }
                }

                // ---------------------------------------------------------------------------
                // this part is per frame
                // ---------------------------------------------------------------------------

                SetDefaultRenderTargets();

                if (RenderTechnique == deferred)
                {
                    /// set G-Buffer
                    deferredRenderer.SetGBufferTargets();

                    /// render G-Buffer pass
                    renderRenderable.Render(renderContext);

                    /// call deferred render
                    deferredRenderer.RenderDeferred(renderContext, renderRenderable);
                }
                else if (RenderTechnique == gbuffer)
                {
                    /// set G-Buffer
                    deferredRenderer.SetGBufferTargets(targetWidth / 2, targetHeight / 2);

                    /// render G-Buffer pass
                    renderRenderable.Render(renderContext);

                    /// reset render targets and run lighting pass
#if MSAA
                    deferredRenderer.RenderGBufferOutput(ref renderTargetNMS);
#else
                    this.deferredRenderer.RenderGBufferOutput(ref this.colorBuffer);
#endif
                }
                else
                {
                    this.device.ImmediateContext.ClearRenderTargetView(colorBufferView, ClearColor);
                    this.device.ImmediateContext.ClearDepthStencilView(depthStencilBufferView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);

                    renderRenderable.Render(renderContext);
                }
            }

            this.device.ImmediateContext.Flush();
        }