コード例 #1
0
        protected override void OnProcess(RenderContext context)
        {
            context.ThrowIfCameraMissing();

            var graphicsDevice   = GraphicsService.GraphicsDevice;
            var renderTargetPool = GraphicsService.RenderTargetPool;
            var source           = context.SourceTexture;
            var target           = context.RenderTarget;
            var viewport         = context.Viewport;

            RenderTarget2D temp128x128 = renderTargetPool.Obtain2D(
                new RenderTargetFormat(128, 128, false, SurfaceFormat.HalfVector4, DepthFormat.None));
            RenderTarget2D temp64x64 = renderTargetPool.Obtain2D(
                new RenderTargetFormat(64, 64, false, SurfaceFormat.HalfVector4, DepthFormat.None));
            RenderTarget2D luminance = renderTargetPool.Obtain2D(
                new RenderTargetFormat(1, 1, false, SurfaceFormat.HalfVector4, DepthFormat.None));

            // ----- Downsample scene into temp128x128.
            context.RenderTarget = temp128x128;
            context.Viewport     = new Viewport(0, 0, temp128x128.Width, temp128x128.Height);
            _downsampleFilter.Process(context);

            _useGeometricMeanParameter.SetValue(UseGeometricMean);

            // Get view-dependent information stored in camera node.
            var    cameraNode = context.CameraNode;
            object dummy;

            cameraNode.ViewDependentData.TryGetValue(this, out dummy);
            var data = dummy as ViewDependentData;

            if (data == null)
            {
                data = new ViewDependentData(GraphicsService);
                cameraNode.ViewDependentData[this] = data;
            }

            if (UseAdaption)
            {
                // Use adaption if required by user and if we already have luminance info.
                _useAdaptionParameter.SetValue(data.LastLuminance != null);
                _deltaTimeParameter.SetValue((float)context.DeltaTime.TotalSeconds);
                _adaptionSpeedParameter.SetValue(AdaptionSpeed);
                _lastLuminanceTextureParameter.SetValue(data.LastLuminance);
            }
            else
            {
                _useAdaptionParameter.SetValue(false);
                _lastLuminanceTextureParameter.SetValue((Texture2D)null);

                // Reset old luminance info.
                data.Dispose();
            }

            // ----- First downsample temp128x128 into temp64x64 and create luminance info.
            graphicsDevice.SetRenderTarget(temp64x64);
            _textureParameter.SetValue(temp128x128);
            _sourceSizeParameter.SetValue(new Vector2(temp128x128.Width, temp128x128.Height));
            _targetSizeParameter.SetValue(new Vector2(temp64x64.Width, temp64x64.Height));
            _createPass.Apply();
            graphicsDevice.DrawFullScreenQuad();

            // temp128x128 is not needed anymore.
            renderTargetPool.Recycle(temp128x128);

            // ----- Downsample luminance info.
            RenderTarget2D last = temp64x64;

            while (last.Width > 2)
            {
                Debug.Assert(last.Width == last.Height, "The render target must be quadratic");

                RenderTarget2D temp = renderTargetPool.Obtain2D(
                    new RenderTargetFormat(last.Width / 2, last.Height / 2, false, last.Format, DepthFormat.None));

                graphicsDevice.SetRenderTarget(temp);
                _textureParameter.SetValue(last);
                _sourceSizeParameter.SetValue(new Vector2(last.Width, last.Height));
                _targetSizeParameter.SetValue(new Vector2(temp.Width, temp.Height));
                _downsamplePass.Apply();
                graphicsDevice.DrawFullScreenQuad();

                renderTargetPool.Recycle(last);
                last = temp;
            }

            // ----- Sample 'last' and store final info in 'luminance'.
            graphicsDevice.SetRenderTarget(luminance);
            _textureParameter.SetValue(last);
            _sourceSizeParameter.SetValue(new Vector2(last.Width, last.Height));
            _targetSizeParameter.SetValue(new Vector2(luminance.Width, luminance.Height));
            _finalPass.Apply();
            graphicsDevice.DrawFullScreenQuad();

            renderTargetPool.Recycle(last);

            // ----- Copy luminance to original context.RenderTarget.
            context.SourceTexture = luminance;
            context.RenderTarget  = target;
            context.Viewport      = viewport;
            _copyFilter.Process(context);

            // ----- Store luminance for next frame.
            renderTargetPool.Recycle(data.LastLuminance);
            data.LastLuminance = luminance;

            // Restore original context.
            context.SourceTexture = source;

            _textureParameter.SetValue((Texture2D)null);
        }
コード例 #2
0
        protected override void OnProcess(RenderContext context)
        {
            context.ThrowIfCameraMissing();

            var graphicsDevice = GraphicsService.GraphicsDevice;
            var source         = context.SourceTexture;
            var target         = context.RenderTarget;
            var viewport       = context.Viewport;

            var            tempFormat   = new RenderTargetFormat(source.Width, source.Height, false, source.Format, DepthFormat.None);
            RenderTarget2D blurredScene = GraphicsService.RenderTargetPool.Obtain2D(tempFormat);

            if (TextureHelper.IsFloatingPointFormat(source.Format))
            {
                graphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
                graphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
            }
            else
            {
                graphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
                graphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;
            }

            context.RenderTarget = blurredScene;
            context.Viewport     = new Viewport(0, 0, blurredScene.Width, blurredScene.Height);

            // Get view-dependent information stored in camera node.
            var    cameraNode = context.CameraNode;
            object dummy;

            cameraNode.ViewDependentData.TryGetValue(this, out dummy);
            var data = dummy as ViewDependentData;

            if (data == null)
            {
                data = new ViewDependentData(GraphicsService);
                cameraNode.ViewDependentData[this] = data;
            }

            if (data.LastBlurredScene == null)
            {
                // This is the first frame. Simply remember the current source for the next frame.
                _copyFilter.Process(context);
            }
            else
            {
                // Create new blurred scene.
                graphicsDevice.SetRenderTarget(blurredScene);

                _viewportSizeParameter.SetValue(new Vector2(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height));
                _strengthParameter.SetValue(Strength);
                _sourceTextureParameter.SetValue(source);
                _lastSourceTextureParameter.SetValue(data.LastBlurredScene);
                _effect.CurrentTechnique.Passes[0].Apply();
                graphicsDevice.DrawFullScreenQuad();
            }

            // Copy blurredScene to target.
            context.SourceTexture = blurredScene;
            context.RenderTarget  = target;
            context.Viewport      = viewport;
            _copyFilter.Process(context);

            // Recycle old blurred scene and store new scene (switch render targets).
            GraphicsService.RenderTargetPool.Recycle(data.LastBlurredScene);
            data.LastBlurredScene = blurredScene;

            _sourceTextureParameter.SetValue((Texture2D)null);
            _lastSourceTextureParameter.SetValue((Texture2D)null);

            // Restore original context.
            context.SourceTexture = source;
        }
コード例 #3
0
        protected override void OnProcess(RenderContext context)
        {
            context.ThrowIfCameraMissing();

              var graphicsDevice = GraphicsService.GraphicsDevice;
              var source = context.SourceTexture;
              var target = context.RenderTarget;
              var viewport = context.Viewport;

              var tempFormat = new RenderTargetFormat(source.Width, source.Height, false, source.Format, DepthFormat.None);
              RenderTarget2D blurredScene = GraphicsService.RenderTargetPool.Obtain2D(tempFormat);

              if (TextureHelper.IsFloatingPointFormat(source.Format))
              {
            graphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
            graphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
              }
              else
              {
            graphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
            graphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;
              }

              context.RenderTarget = blurredScene;
              context.Viewport = new Viewport(0, 0, blurredScene.Width, blurredScene.Height);

              // Get view-dependent information stored in camera node.
              var cameraNode = context.CameraNode;
              object dummy;
              cameraNode.ViewDependentData.TryGetValue(this, out dummy);
              var data = dummy as ViewDependentData;
              if (data == null)
              {
            data = new ViewDependentData(GraphicsService);
            cameraNode.ViewDependentData[this] = data;
              }

              if (data.LastBlurredScene == null)
              {
            // This is the first frame. Simply remember the current source for the next frame.
            _copyFilter.Process(context);
              }
              else
              {
            // Create new blurred scene.
            graphicsDevice.SetRenderTarget(blurredScene);

            _viewportSizeParameter.SetValue(new Vector2(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height));
            _strengthParameter.SetValue(Strength);
            _sourceTextureParameter.SetValue(source);
            _lastSourceTextureParameter.SetValue(data.LastBlurredScene);
            _effect.CurrentTechnique.Passes[0].Apply();
            graphicsDevice.DrawFullScreenQuad();
              }

              // Copy blurredScene to target.
              context.SourceTexture = blurredScene;
              context.RenderTarget = target;
              context.Viewport = viewport;
              _copyFilter.Process(context);

              // Recycle old blurred scene and store new scene (switch render targets).
              GraphicsService.RenderTargetPool.Recycle(data.LastBlurredScene);
              data.LastBlurredScene = blurredScene;

              _sourceTextureParameter.SetValue((Texture2D)null);
              _lastSourceTextureParameter.SetValue((Texture2D)null);

              // Restore original context.
              context.SourceTexture = source;
        }
コード例 #4
0
ファイル: LuminanceFilter.cs プロジェクト: Zolniu/DigitalRune
        protected override void OnProcess(RenderContext context)
        {
            context.ThrowIfCameraMissing();

              var graphicsDevice = GraphicsService.GraphicsDevice;
              var renderTargetPool = GraphicsService.RenderTargetPool;
              var source = context.SourceTexture;
              var target = context.RenderTarget;
              var viewport = context.Viewport;

              RenderTarget2D temp128x128 = renderTargetPool.Obtain2D(
            new RenderTargetFormat(128, 128, false, SurfaceFormat.HalfVector4, DepthFormat.None));
              RenderTarget2D temp64x64 = renderTargetPool.Obtain2D(
            new RenderTargetFormat(64, 64, false, SurfaceFormat.HalfVector4, DepthFormat.None));
              RenderTarget2D luminance = renderTargetPool.Obtain2D(
            new RenderTargetFormat(1, 1, false, SurfaceFormat.HalfVector4, DepthFormat.None));

              // ----- Downsample scene into temp128x128.
              context.RenderTarget = temp128x128;
              context.Viewport = new Viewport(0, 0, temp128x128.Width, temp128x128.Height);
              _downsampleFilter.Process(context);

              _useGeometricMeanParameter.SetValue(UseGeometricMean);

              // Get view-dependent information stored in camera node.
              var cameraNode = context.CameraNode;
              object dummy;
              cameraNode.ViewDependentData.TryGetValue(this, out dummy);
              var data = dummy as ViewDependentData;
              if (data == null)
              {
            data = new ViewDependentData(GraphicsService);
            cameraNode.ViewDependentData[this] = data;
              }

              if (UseAdaption)
              {
            // Use adaption if required by user and if we already have luminance info.
            _useAdaptionParameter.SetValue(data.LastLuminance != null);
            _deltaTimeParameter.SetValue((float)context.DeltaTime.TotalSeconds);
            _adaptionSpeedParameter.SetValue(AdaptionSpeed);
            _lastLuminanceTextureParameter.SetValue(data.LastLuminance);
              }
              else
              {
            _useAdaptionParameter.SetValue(false);
            _lastLuminanceTextureParameter.SetValue((Texture2D)null);

            // Reset old luminance info.
            data.Dispose();
              }

              // ----- First downsample temp128x128 into temp64x64 and create luminance info.
              graphicsDevice.SetRenderTarget(temp64x64);
              _textureParameter.SetValue(temp128x128);
              _sourceSizeParameter.SetValue(new Vector2(temp128x128.Width, temp128x128.Height));
              _targetSizeParameter.SetValue(new Vector2(temp64x64.Width, temp64x64.Height));
              _createPass.Apply();
              graphicsDevice.DrawFullScreenQuad();

              // temp128x128 is not needed anymore.
              renderTargetPool.Recycle(temp128x128);

              // ----- Downsample luminance info.
              RenderTarget2D last = temp64x64;
              while (last.Width > 2)
              {
            Debug.Assert(last.Width == last.Height, "The render target must be quadratic");

            RenderTarget2D temp = renderTargetPool.Obtain2D(
              new RenderTargetFormat(last.Width / 2, last.Height / 2, false, last.Format, DepthFormat.None));

            graphicsDevice.SetRenderTarget(temp);
            _textureParameter.SetValue(last);
            _sourceSizeParameter.SetValue(new Vector2(last.Width, last.Height));
            _targetSizeParameter.SetValue(new Vector2(temp.Width, temp.Height));
            _downsamplePass.Apply();
            graphicsDevice.DrawFullScreenQuad();

            renderTargetPool.Recycle(last);
            last = temp;
              }

              // ----- Sample 'last' and store final info in 'luminance'.
              graphicsDevice.SetRenderTarget(luminance);
              _textureParameter.SetValue(last);
              _sourceSizeParameter.SetValue(new Vector2(last.Width, last.Height));
              _targetSizeParameter.SetValue(new Vector2(luminance.Width, luminance.Height));
              _finalPass.Apply();
              graphicsDevice.DrawFullScreenQuad();

              renderTargetPool.Recycle(last);

              // ----- Copy luminance to original context.RenderTarget.
              context.SourceTexture = luminance;
              context.RenderTarget = target;
              context.Viewport = viewport;
              _copyFilter.Process(context);

              // ----- Store luminance for next frame.
              renderTargetPool.Recycle(data.LastLuminance);
              data.LastLuminance = luminance;

              // Restore original context.
              context.SourceTexture = source;

              _textureParameter.SetValue((Texture2D)null);
        }