예제 #1
0
        public static void Initialize(GraphicsDevice device)
        {
            // register convar
            r_hdr_enable = ConVar.Register("r_hdr_enable", true, "Enable high dynamic range rendering.", ConVarFlags.Archived);

            // create render targets
            int width  = device.PresentationParameters.BackBufferWidth;
            int height = device.PresentationParameters.BackBufferHeight;

            _colorRT  = new RenderTarget2D(device, width, height, false, SurfaceFormat.Color, DepthFormat.Depth16);
            _depthRT  = new RenderTarget2D(device, width, height, false, SurfaceFormat.Single, DepthFormat.Depth16);
            _normalRT = new RenderTarget2D(device, width, height, false, SurfaceFormat.Color, DepthFormat.Depth16);
            _lightRT  = new RenderTarget2D(device, width, height, false, (r_hdr_enable.GetValue <bool>()) ? SurfaceFormat.Rgba1010102 : SurfaceFormat.Color, DepthFormat.Depth16);

            if (r_hdr_enable.GetValue <bool>())
            {
                _hdrRT      = new RenderTarget2D(device, width, height, false, SurfaceFormat.Rgba1010102, DepthFormat.None);
                _hdrBloomRT = new RenderTarget2D(device, width, height, false, SurfaceFormat.Rgba1010102, DepthFormat.None);

                int sampleEntries = 1;
                int startSize     = Math.Min(width / 16, height / 16);

                int size = 16;
                for (size = 16; size < startSize; size *= 4)
                {
                    sampleEntries++;
                }

                _lightDownsampleRTs = new RenderTarget2D[sampleEntries];

                size /= 4;

                for (int i = 0; i < sampleEntries; i++)
                {
                    _lightDownsampleRTs[i] = new RenderTarget2D(device, size, size, false, SurfaceFormat.Single, DepthFormat.None);
                    size /= 4;
                }

                _avgLightRT = new RenderTarget2D(device, 1, 1, false, SurfaceFormat.Single, DepthFormat.None);
            }

            // load shaders
            _baseEffect      = EffectManager.Load("BaseDraw", device);
            _compositeEffect = EffectManager.Load("ScreenComposite", device);
            _bloomEffect     = EffectManager.Load("Bloom", device);
            _lightnessEffect = EffectManager.Load("Lightness", device);

            // light renderer
            LightRenderer.Initialize(device);

            // shadow renderer
            ShadowRenderer.Initialize(device);
        }
예제 #2
0
        public static void Render3DStuff(GraphicsDevice device)
        {
            // clear render targets
            ClearRenderTarget(device, _colorRT, Color.Black);
            ClearRenderTarget(device, _depthRT, Color.White);
            ClearRenderTarget(device, _normalRT, Color.Gray);

            // set render targets
            device.SetRenderTargets(_colorRT, _depthRT, _normalRT);

            // set up the shader
            Camera.Apply(_baseEffect);

            // render the scene
            RenderScene(device, _baseEffect, Camera.MainCamera);

            // set render target to the back buffer
            device.SetRenderTargets(null);

            // render shadow stuff
            _shadowRT = ShadowRenderer.RenderShadows(device);

            // set render target to the light buffer
            device.SetRenderTarget(_lightRT);

            // clear the RT
            device.Clear(Color.Transparent);

            // render ambient/directional light
            LightRenderer.RenderDirectionalLight(device, _normalRT, _depthRT, _shadowRT);

            // render lights
            //LightRenderer.RenderLights(device, _depthRT);

            //return;

            // again go back to the back buffer
            device.SetRenderTarget(_hdrRT); // will be null if not enabled

            // draw the screen composite
            DrawScreenComposite(device);

            if (_hdrRT != null)
            {
                //DrawAvgLightness(device);

                device.SetRenderTarget(_hdrBloomRT);
                DrawHDRBloom(device, true);
                device.SetRenderTarget(null);
                DrawHDRBloom(device, false);
            }
        }