/// <summary>
        /// Function called to build a new (or return an existing) 2D batch state.
        /// </summary>
        /// <param name="passIndex">The index of the current rendering pass.</param>
        /// <param name="statesChanged"><b>true</b> if the blend, raster, or depth/stencil state was changed. <b>false</b> if not.</param>
        /// <returns>The 2D batch state.</returns>
        protected override Gorgon2DBatchState OnGetBatchState(int passIndex, bool statesChanged)
        {
            if (statesChanged)
            {
                _batchState = BatchStateBuilder.Build();
            }

            return(_batchState);
        }
Example #2
0
        /// <summary>
        /// Function to build up the render targets.
        /// </summary>
        /// <param name="width">The width of the render targets.</param>
        /// <param name="height">The height of the render targets.</param>
        /// <param name="format">The format of the buffer.</param>
        private void BuildRenderTargets(int width, int height, BufferFormat format)
        {
            // For the lighting effect, we use a deferred rendering technique where we have 3 render targets for diffuse, specularity, and normal mapping.
            // These targets are sub resources of the same texture resource (array indices).

            // Diffuse.
            _rtvInfo.Width      = width;
            _rtvInfo.Height     = height;
            _rtvInfo.Format     = format;
            _rtvInfo.ArrayCount = 2;

            _gbufferTargets[0] = Graphics.TemporaryTargets.Rent(_rtvInfo, "Gorgon 2D GBuffer - Diffuse/Specular", false);
            _gbufferTargets[0].Clear(GorgonColor.Black);
            // Specular.
            _gbufferTargets[1] = _gbufferTargets[0].Texture.GetRenderTargetView(arrayIndex: 1, arrayCount: 1);

            _rtvInfo.Format     = BufferFormat.R8G8B8A8_UNorm;
            _rtvInfo.ArrayCount = 1;

            // Normals.
            // We'll clear it before the pass, the default color is insufficient.
            _gbufferTargets[2] = Graphics.TemporaryTargets.Rent(_rtvInfo, "Gorgon 2D Buffer - Normals", false);
            GorgonTexture2DView normalSrv = _gbufferTargets[2].GetShaderResourceView();

            if ((_pixelLitShaderState.ShaderResources[1] != normalSrv) ||
                (_diffuseFilter != DiffuseFiltering) ||
                (_normalFilter != NormalFiltering) ||
                (_specularFilter != SpecularFiltering))
            {
                _diffuseFilter  = DiffuseFiltering ?? GorgonSamplerState.Default;
                _normalFilter   = NormalFiltering ?? GorgonSamplerState.PointFiltering;
                _specularFilter = SpecularFiltering ?? GorgonSamplerState.Default;

                _pixelDeferShaderState = PixelShaderBuilder.ResetTo(_pixelDeferShaderState)
                                         .SamplerState(_diffuseFilter, 0)
                                         .SamplerState(_normalFilter, 1)
                                         .SamplerState(_specularFilter, 2)
                                         .Build();

                _pixelLitShaderState = PixelShaderBuilder
                                       .ResetTo(_pixelLitShaderState)
                                       .ShaderResource(normalSrv, 1)
                                       .SamplerState(_diffuseFilter, 0)
                                       .SamplerState(_normalFilter, 1)
                                       .SamplerState(_specularFilter, 2)
                                       .Build();

                _lightingState = BatchStateBuilder
                                 .ResetTo(_lightingState)
                                 .PixelShaderState(_pixelLitShaderState)
                                 .Build();
            }


            _gbufferTexture = _gbufferTargets[0].Texture.GetShaderResourceView();
        }
        /// <summary>
        /// Function called when the effect is being initialized.
        /// </summary>
        /// <remarks>
        /// Use this method to set up the effect upon its creation.  For example, this method could be used to create the required shaders for the effect.
        /// </remarks>
        protected override void OnInitialize()
        {
            // Compile our blur shader.
            _grayScaleShader = CompileShader <GorgonPixelShader>(Resources.BasicSprite, "GorgonPixelShaderGrayScale");
            _grayScaleState  = PixelShaderBuilder
                               .Shader(_grayScaleShader)
                               .Build();

            _batchState = BatchStateBuilder
                          .PixelShaderState(_grayScaleState)
                          .Build();
        }
Example #4
0
        /// <summary>
        /// Function called to initialize the effect.
        /// </summary>
        /// <remarks>Applications must implement this method to ensure that any required resources are created, and configured for the effect.</remarks>
        protected override void OnInitialize()
        {
            var globalData = new GlobalEffectData
            {
                CameraPosition = DX.Vector3.Zero,
                FlipYNormal    = 0
            };

            _globalData = GorgonConstantBufferView.CreateConstantBuffer(Graphics, ref globalData, "Global deferred light effect data.", ResourceUsage.Default);

            _lightData = GorgonConstantBufferView.CreateConstantBuffer(Graphics,
                                                                       new GorgonConstantBufferInfo("Deferred Lighting Light Data Buffer")
            {
                SizeInBytes = Unsafe.SizeOf <PointLightData>(),
                Usage       = ResourceUsage.Dynamic
            });

            Macros.Add(new GorgonShaderMacro("DEFERRED_LIGHTING"));
            _vertexDeferShader      = CompileShader <GorgonVertexShader>(Resources.Lighting, "GorgonVertexLightingShader");
            _vertexDeferShaderState = VertexShaderBuilder.Shader(_vertexDeferShader)
                                      .Build();

            _pixelDeferShader      = CompileShader <GorgonPixelShader>(Resources.Lighting, "GorgonPixelShaderDeferred");
            _pixelDeferShaderState = PixelShaderBuilder.Shader(_pixelDeferShader)
                                     .SamplerState(_diffuseFilter, 0)
                                     .SamplerState(_normalFilter, 1)
                                     .SamplerState(_specularFilter, 2)
                                     .Build();

            Macros.Clear();
            Macros.Add(new GorgonShaderMacro("LIGHTS"));
            _vertexLitShader      = CompileShader <GorgonVertexShader>(Resources.Lighting, "GorgonVertexLitShader");
            _vertexLitShaderState = VertexShaderBuilder.Shader(_vertexLitShader)
                                    .Build();

            _pixelLitShader      = CompileShader <GorgonPixelShader>(Resources.Lighting, "GorgonPixelShaderLighting");
            _pixelLitShaderState = PixelShaderBuilder.Shader(_pixelLitShader)
                                   .ConstantBuffer(_lightData, 1)
                                   .ConstantBuffer(_globalData, 2)
                                   .SamplerState(_diffuseFilter, 0)
                                   .SamplerState(_normalFilter, 1)
                                   .SamplerState(_specularFilter, 2)
                                   .Build();

            // Rebuild our states for the new pixel shaders.
            _lightingState = BatchStateBuilder.PixelShaderState(_pixelLitShaderState)
                             .VertexShaderState(_vertexLitShaderState)
                             .BlendState(GorgonBlendState.Additive)
                             .Build();
        }
Example #5
0
        /// <summary>
        /// Function called to build a new (or return an existing) 2D batch state.
        /// </summary>
        /// <param name="passIndex">The index of the current rendering pass.</param>
        /// <param name="statesChanged"><b>true</b> if the blend, raster, or depth/stencil state was changed. <b>false</b> if not.</param>
        /// <returns>The 2D batch state.</returns>
        protected override Gorgon2DBatchState OnGetBatchState(int passIndex, bool statesChanged)
        {
            switch (passIndex)
            {
            case 0:
                if ((statesChanged) || (_deferredState == null))
                {
                    _deferredState = BatchStateBuilder
                                     .PixelShaderState(_pixelDeferShaderState)
                                     .VertexShaderState(_vertexDeferShaderState)
                                     .BlendState(GorgonBlendState.NoBlending)
                                     .Build();
                }

                return(_deferredState);

            default:
                return(_lightingState);
            }
        }
Example #6
0
        /// <summary>
        /// Function to set up state prior to rendering.
        /// </summary>
        /// <param name="blendStateOverride">An override for the current blending state.</param>
        /// <param name="depthStencilStateOverride">An override for the current depth/stencil state.</param>
        /// <param name="rasterStateOverride">An override for the current raster state.</param>
        /// <param name="output">The target used as the output.</param>
        /// <param name="camera">The active camera.</param>
        /// <returns><b>true</b> if state was overridden, <b>false</b> if not or <b>null</b> if rendering is canceled.</returns>
        private bool SetupStates(GorgonBlendState blendStateOverride, GorgonDepthStencilState depthStencilStateOverride, GorgonRasterState rasterStateOverride, GorgonRenderTargetView output, IGorgon2DCamera camera)
        {
            if (!_isInitialized)
            {
                OnInitialize();
                _isInitialized = true;
            }

            bool outputSizeChanged = false;

            if ((_prevOutputSize.Width != output.Width) ||
                (_prevOutputSize.Height != output.Height))
            {
                _prevOutputSize   = new DX.Size2(output.Width, output.Height);
                outputSizeChanged = true;
            }

            OnBeforeRender(output, camera, outputSizeChanged);

            if ((blendStateOverride == BlendStateOverride) &&
                (depthStencilStateOverride == DepthStencilStateOverride) &&
                (rasterStateOverride == RasterStateOverride))
            {
                return(false);
            }

            BatchStateBuilder.BlendState(blendStateOverride ?? GorgonBlendState.Default)
            .DepthStencilState(depthStencilStateOverride ?? GorgonDepthStencilState.Default)
            .RasterState(rasterStateOverride ?? GorgonRasterState.Default);

            BlendStateOverride        = blendStateOverride;
            DepthStencilStateOverride = depthStencilStateOverride;
            RasterStateOverride       = rasterStateOverride;

            return(true);
        }
        /// <summary>Function called to build a new (or return an existing) 2D batch state.</summary>
        /// <param name="passIndex">The index of the current rendering pass.</param>
        /// <param name="statesChanged">
        ///   <b>true</b> if the blend, raster, or depth/stencil state was changed. <b>false</b> if not.</param>
        /// <returns>The 2D batch state.</returns>
        protected override Gorgon2DBatchState OnGetBatchState(int passIndex, bool statesChanged)
        {
            if ((_useSimple != FullScreen) ||
                ((LookupTexture == null) && (_currentLut != _defaultLut)) ||
                ((LookupTexture != null) && (LookupTexture != _currentLut)))
            {
                GorgonPixelShader current = FullScreen ? _simpleChromeAbShader : _chromeAbShader;
                _currentLut = LookupTexture ?? _defaultLut;

                _chromeAbBatchState = BatchStateBuilder.Clear()
                                      .BlendState(GorgonBlendState.NoBlending)
                                      .PixelShaderState(_shaderStateBuilder
                                                        .Clear()
                                                        .Shader(current)
                                                        .ShaderResource(!FullScreen ? _currentLut : null, 1)
                                                        .SamplerState(!FullScreen ? GorgonSamplerState.Default : null, 1)
                                                        .ConstantBuffer(_settings, 1))
                                      .Build();

                _useSimple = FullScreen;
            }

            return(_chromeAbBatchState);
        }