예제 #1
0
        /// <summary>
        /// Draws the scene from the perspective of the given camera to its <see cref="Camera.BackBuffer" />
        /// </summary>
        internal static void Render(IWorld world, PassType passes)
        {
            if (!world.IsRenderable)
            {
                return;
            }

            var camera = world.CurrentCamera;

            if (camera == null)
            {
                return;
            }

            lock (Locker)
            {
                if (camera.NeedsResize && (camera.SwapChain != null))
                {
                    camera.Resize(Device);
                    return;
                }

                if (!camera.CanRender)
                {
                    return;
                }

                if (passes.HasFlag(PassType.Shadow))
                {
                    Shadows.RenderShadowMap(ref Context, ref world, ref camera);
                }

                if (passes.HasFlag(PassType.Scene))
                {
                    PixHelper.BeginEvent(Color.Zero, "Geometry");
                    {
                        Context.Rasterizer.SetViewport(camera.Viewport);
                        Context.ClearDepthStencilView(camera.DepthStencilBuffer,
                                                      DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1, 0);
                        Context.ClearRenderTargetView(camera.Buffer0, Color.Zero);
                        Context.ClearRenderTargetView(camera.Buffer1, Color.Zero);
                        Context.ClearRenderTargetView(camera.Buffer2, Color.Zero);
                        Context.ClearRenderTargetView(camera.Buffer3, Color.Zero);

                        camera.UpdateConstants(ref Context);

                        Context.OutputMerger.SetRenderTargets(camera.DepthStencilBuffer,
                                                              camera.Buffer0,
                                                              camera.Buffer1,
                                                              camera.Buffer2,
                                                              camera.Buffer3);

                        CurrentPassType = PassType.Scene;

#pragma warning disable CS0728 // Possibly incorrect assignment to local which is the argument to a using or lock statement
                        world.RenderObjectProvider.Draw(ref Context, ref camera, false);

                        if (ReferenceEquals(world, Game.Workspace))
                        {
                            Game.Workspace.Terrain.Draw(ref Context);
                        }
#pragma warning restore CS0728 // Possibly incorrect assignment to local which is the argument to a using or lock statement
                    }
                    PixHelper.EndEvent();

                    if (passes.HasFlag(PassType.PhysicsDebug))
                    {
                        PixHelper.BeginEvent(Color.Zero, "Physics Debug");
                        world.Physics?.DrawWorld(ref Context, ref camera);
                        PixHelper.EndEvent();
                    }
                }
                else if (passes.HasFlag(PassType.Reflection))
                {
                }

                if (passes.HasFlag(PassType.Lighting))
                {
                    Atmosphere.Atmosphere.Update();
                    Lighting(ref camera); // rt is set to camera's backbuffer here.
                }

                if (passes.HasFlag(PassType.PostProcess))
                {
                    CurrentPassType = PassType.PostProcess;
                    PostProcess(ref camera); // uses camera's back buffer
                }

                if (passes.HasFlag(PassType.GUI))
                {
                    CurrentPassType = PassType.GUI;
                    // uses camera's back buffer
                    Draw3DGuis(camera);
                    DrawSurfaceGuis();
                    DrawBillboardGuis(camera);
                    DrawScreenGuis(camera);
                }

                if (camera.RenderHandle == IntPtr.Zero)
                {
                    Context.Flush();
                }
                else
                {
                    camera.SwapChain.Present(1, 0);
                }
            }
        }
예제 #2
0
        internal static void Init()
        {
            Logger.Info("GraphicsManager initializing.");

            // Direct3D11 Init ---
            ResizeNextFrame = true;

            Factory        = new FactoryDXGI();
            ImagingFactory = new FactoryWIC();
            Factory2D      = new FactoryD2D(FactoryType.MultiThreaded,
                                            Engine.IsDebug ? DebugLevel.Error : DebugLevel.None);
            DirectWriteFactory = new FactoryDW(SharpDX.DirectWrite.FactoryType.Shared);

            CreateDevices(RenderSettings.GraphicsAdapter);

            Brushes =
                new MemoizingMRUCache <Colour, SolidColorBrush>(
                    (colour, _) => new SolidColorBrush(Context2D, (Color4)colour)
            {
                Opacity = colour.A
            }, int.MaxValue,
                    brush => brush.Dispose());
            // ------------------------------------

            Engine.HandleSet += hwnd =>
            {
                var camera = Game.Workspace.CurrentCamera;
                camera.RenderHandle = hwnd;
            };

            if (Engine.Handle != IntPtr.Zero)
            {
                var camera = Game.Workspace.CurrentCamera;
                camera.RenderHandle = Engine.Handle;
            }

            SamplerStates.Load();
            Shaders.Init();
            BlendStates.Load();
            DepthStencilStates.Load();
            RasterizerStates.Load();

            //StandardConstants = new ConstantBuffer<StandardConstantData>();

            StandardShader = Shaders.Get("Standard");
            MainPass       = StandardShader.GetPass();
            LightingShader = Shaders.Get("Lighting");
            LightingPass   = LightingShader.GetPass();

            PostProcessShader = Shaders.Get("PostProcess");

            SkyboxShader = Shaders.Get("Skybox");
            SkyboxPass   = SkyboxShader.GetPass();

            AdornShader      = Shaders.Get("Adorn");
            AALinePass       = AdornShader.GetPass("AALine");
            AdornSelfLitPass = AdornShader.GetPass("AdornSelfLit");

            Shadows.Init();

            // ------------------

            IsInitialized = true;
            Initialized?.Invoke();
            Initialized = null;

            //PixHelper.AllowProfiling(Engine.IsDebug);

            Logger.Info("Renderer initialized.");
        }