Esempio n. 1
0
        public override void Load()
        {
            base.Load();

            if (OfflineCompilation)
                return;

            // Declare post render pass
            PostPass = new RenderPass("PostPass").KeepAliveBy(ActiveObjects);
            RenderPass.AddPass(PostPass);


            var depthStencilTexture = Texture.New2D(GraphicsDevice, AtlasSize, AtlasSize, PixelFormat.D32_Float, TextureFlags.DepthStencil | TextureFlags.ShaderResource).KeepAliveBy(ActiveObjects);
            var depthStencilBuffer = depthStencilTexture.ToDepthStencilBuffer(false);
            ShadowMapDepth = depthStencilBuffer;

            //MainTargetPlugin.Parameters.Set(ShadowMapKeys.Texture0, ShadowMapDepth);

            // Setup clear of this target
            var renderTargetPlugin = new RenderTargetsPlugin
                {
                    Services = Services,
                    EnableClearDepth = true,
                    EnableSetTargets = false,
                    RenderPass = RenderPass,
                    RenderTarget = null,
                    DepthStencil = depthStencilBuffer,
                };
            renderTargetPlugin.Apply();

            // Use Default ZTest for GBuffer
            depthStencilStateZStandard = DepthStencilState.New(GraphicsDevice, new DepthStencilStateDescription().Default()).KeepAliveBy(ActiveObjects);
            depthStencilStateZStandard.Name = "ZStandard";

            Parameters.Set(EffectPlugin.DepthStencilStateKey, depthStencilStateZStandard);

            casterRasterizerState = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription(CullMode.Back)).KeepAliveBy(ActiveObjects);

            // Variance Shadow Mapping
            // Create the blur temporary texture
            var shadowMapTextureDesc = ShadowMapDepth.Description;
            var shadowMapBlurH = Texture.New2D(GraphicsDevice, shadowMapTextureDesc.Width, shadowMapTextureDesc.Height, PixelFormat.R32G32_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget).KeepAliveBy(ActiveObjects);
            var shadowMapBlurV = Texture.New2D(GraphicsDevice, shadowMapTextureDesc.Width, shadowMapTextureDesc.Height, PixelFormat.R32G32_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget).KeepAliveBy(ActiveObjects);

            Texture2D textureSourceH = ShadowMapDepth.Texture;
            Texture2D textureSourceV = shadowMapBlurH;
            RenderTarget renderTargetH = shadowMapBlurH.ToRenderTarget();
            RenderTarget renderTargetV = shadowMapBlurV.ToRenderTarget();

            var blurQuadMesh = new EffectMesh[2];
            for (int j = 0; j < BlurCount; j++)
            {
                for (int i = 0; i < 2; ++i)
                {
                    blurQuadMesh[i] = new EffectMesh(j > 0 ? blurEffects[1] : blurEffects[i]).KeepAliveBy(ActiveObjects);
                    blurQuadMesh[i].Parameters.Set(PostEffectBlurKeys.Coefficients, new[] { 0.2270270270f, 0.3162162162f, 0.3162162162f, 0.0702702703f, 0.0702702703f });
                    var unit = i == 0 ? Vector2.UnitX : Vector2.UnitY;
                    blurQuadMesh[i].Parameters.Set(PostEffectBlurKeys.Offsets, new[] { Vector2.Zero, unit * -1.3846153846f, unit * +1.3846153846f, unit * -3.2307692308f, unit * +3.2307692308f });

                    PostPass.AddPass(blurQuadMesh[i].EffectPass);

                    RenderSystem.GlobalMeshes.AddMesh(blurQuadMesh[i]);
                }

                blurQuadMesh[0].Parameters.Set(TexturingKeys.Texture0, textureSourceH);
                blurQuadMesh[1].Parameters.Set(TexturingKeys.Texture0, textureSourceV);
                blurQuadMesh[0].Parameters.Set(RenderTargetKeys.RenderTarget, renderTargetH);
                blurQuadMesh[1].Parameters.Set(RenderTargetKeys.RenderTarget, renderTargetV);

                textureSourceH = shadowMapBlurV;
                textureSourceV = shadowMapBlurH;
            }

            ShadowMapVsm = shadowMapBlurV;

            // Final texture for VSM is result of blur
            //MainTargetPlugin.Parameters.Set(ShadowMapKeys.Texture0, shadowMapBlurV);
        }
 /// <summary>
 /// Sets the depth-stencil state of the output-merger stage. See <see cref="Render+states"/> to learn how to use it.
 /// </summary>
 /// <param name="depthStencilState">a depth-stencil state</param>
 /// <param name="stencilReference">Reference value to perform against when doing a depth-stencil test.</param>
 private void SetDepthStencilStateImpl(DepthStencilState depthStencilState, int stencilReference = 0)
 {
     NativeDeviceContext.OutputMerger.SetDepthStencilState(depthStencilState != null ? (SharpDX.Direct3D11.DepthStencilState)depthStencilState.NativeDeviceChild : null, stencilReference);
 }
 /// <summary>	
 /// Sets the depth-stencil state of the output-merger stage.
 /// </summary>	
 /// <param name="depthStencilState"><dd>  <p>Pointer to a depth-stencil state interface (see <strong><see cref="SharpDX.Direct3D11.DepthStencilState"/></strong>) to bind to the device. Set this to <strong><c>null</c></strong> to use the default state listed in <strong><see cref="SharpDX.Direct3D11.DepthStencilStateDescription"/></strong>.</p> </dd></param>	
 /// <param name="stencilReference"><dd>  <p>Reference value to perform against when doing a depth-stencil test. See remarks.</p> </dd></param>	
 /// <remarks>	
 /// <p>To create a depth-stencil state interface, call <strong><see cref="SharpDX.Direct3D11.Device.CreateDepthStencilState"/></strong>.</p><p> The method will hold a reference to the interfaces passed in. This differs from the device state behavior in Direct3D 10. </p>	
 /// </remarks>	
 public void SetDepthStencilState(DepthStencilState depthStencilState, int stencilReference = 0)
 {
     throw new NotImplementedException();
 }
Esempio n. 4
0
        protected override void LoadContent()
        {
            base.LoadContent();

            // create effect and geometric primitives
            Batch = new UIBatch(GraphicsDevice);

            // create depth stencil states
            var depthStencilDescription = new DepthStencilStateDescription(true, true)
                {
                    StencilEnable = true,
                    FrontFace = new DepthStencilStencilOpDescription
                    {
                        StencilDepthBufferFail = StencilOperation.Keep,
                        StencilFail = StencilOperation.Keep,
                        StencilPass = StencilOperation.Keep,
                        StencilFunction = CompareFunction.Equal
                    },
                    BackFace = new DepthStencilStencilOpDescription
                    {
                        StencilDepthBufferFail = StencilOperation.Keep,
                        StencilFail = StencilOperation.Keep,
                        StencilPass = StencilOperation.Keep,
                        StencilFunction = CompareFunction.Equal
                    },
                };
            KeepStencilValueState = DepthStencilState.New(GraphicsDevice, depthStencilDescription);

            depthStencilDescription.FrontFace.StencilPass = StencilOperation.Increment;
            depthStencilDescription.BackFace.StencilPass = StencilOperation.Increment;
            IncreaseStencilValueState = DepthStencilState.New(GraphicsDevice, depthStencilDescription);

            depthStencilDescription.FrontFace.StencilPass = StencilOperation.Decrement;
            depthStencilDescription.BackFace.StencilPass = StencilOperation.Decrement;
            DecreaseStencilValueState = DepthStencilState.New(GraphicsDevice, depthStencilDescription);

            // set the default design of the UI elements.
            var designsTexture = TextureExtensions.FromFileData(GraphicsDevice, DefaultDesigns.Designs);
            Button.PressedImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default button pressed design", designsTexture) { Borders = 8 * Vector4.One, Region = new RectangleF(71, 3, 32, 32)});
            Button.NotPressedImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default button not pressed design", designsTexture) { Borders = 8 * Vector4.One, Region = new RectangleF(3, 3, 32, 32) });
            Button.MouseOverImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default button overred design", designsTexture) { Borders = 8 * Vector4.One, Region = new RectangleF(37, 3, 32, 32) });
            EditText.ActiveImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default edit active design", designsTexture) { Borders = 12 * Vector4.One, Region = new RectangleF(105, 3, 32, 32) });
            EditText.InactiveImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default edit inactive design", designsTexture) { Borders = 12 * Vector4.One, Region = new RectangleF(139, 3, 32, 32) });
            EditText.MouseOverImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default edit overred design", designsTexture) { Borders = 12 * Vector4.One, Region = new RectangleF(173, 3, 32, 32) });
            ToggleButton.CheckedImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default toggle button checked design", designsTexture) { Borders = 8 * Vector4.One, Region = new RectangleF(71, 3, 32, 32) });
            ToggleButton.UncheckedImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default toggle button unchecked design", designsTexture) { Borders = 8 * Vector4.One, Region = new RectangleF(3, 3, 32, 32) });
            ToggleButton.IndeterminateImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default toggle button indeterminate design", designsTexture) { Borders = 8 * Vector4.One, Region = new RectangleF(37, 3, 32, 32) });
            Slider.TrackBackgroundImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default slider track background design", designsTexture) { Borders = 14 * Vector4.One, Region = new RectangleF(207, 3, 32, 32) });
            Slider.TrackForegroundImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default slider track foreground design", designsTexture) { Borders = 0 * Vector4.One, Region = new RectangleF(3, 37, 32, 32) });
            Slider.ThumbImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default slider thumb design", designsTexture) { Borders = 4 * Vector4.One, Region = new RectangleF(37, 37, 16, 32) });
            Slider.MouseOverThumbImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default slider thumb overred design", designsTexture) { Borders = 4 * Vector4.One, Region = new RectangleF(71, 37, 16, 32) });
            Slider.TickImagePropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Sprite("Default slider track foreground design", designsTexture) { Region = new RectangleF(245, 3, 3, 6) });
            Slider.TickOffsetPropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(13f);
            Slider.TrackStartingOffsetsrPropertyKey.DefaultValueMetadata = DefaultValueMetadata.Static(new Vector2(3));
        }
 public DefaultModalElementRenderer(IServiceRegistry services)
     : base(services)
 {
     noStencilNoDepth = DepthStencilState.New(GraphicsDevice, new DepthStencilStateDescription(false, false));
 }
Esempio n. 6
0
        /// <inheritdoc/>
        public override void Initialize()
        {
            base.Initialize();
            graphicsDeviceService = Services.GetServiceAs<IGraphicsDeviceService>();
            var graphicsDevice = graphicsDeviceService.GraphicsDevice;


            Parameters.AddSources(MainPlugin.ViewParameters);

            if (OfflineCompilation)
                return;

            // Create texture used for normal packing
            var texture2D = Texture.New2D(GraphicsDevice, 1024, 1024, 1, PixelFormat.A8_UNorm);
            texture2D.Name = "Renorm";

            // Load normal packing data
            var texDataStream = VirtualFileSystem.OpenStream("/assets/effects/gbuffer/renorm.bin", VirtualFileMode.Open, VirtualFileAccess.Read);
            var texFileLength = texDataStream.Length;
            var texData = new byte[texFileLength];
            texDataStream.Read(texData, 0, (int)texFileLength);
            texture2D.SetData(graphicsDevice, texData);
            texDataStream.Dispose();

            // Force custom depth stencil state on main pass
            var mainDepthStencilState = MainTargetPlugin.Parameters.TryGet(EffectPlugin.DepthStencilStateKey) ?? graphicsDevice.DepthStencilStates.Default;
            MainTargetPlugin.Parameters.Set(EffectPlugin.DepthStencilStateKey, mainDepthStencilState);

            // Use depth stencil value from MainPlugin
            var defaultDescription = mainDepthStencilState.Description;
            ClearDepth = MainTargetPlugin.ClearDepth;

            // Use Default ZTest for GBuffer
            var depthStencilStateZStandard = DepthStencilState.New(GraphicsDevice, defaultDescription);
            depthStencilStateZStandard.Name = "ZStandard";

            Parameters.Set(EffectPlugin.DepthStencilStateKey, depthStencilStateZStandard);
            
            Parameters.Set(GBufferKeys.NormalPack, texture2D);
            Parameters.Set(TexturingKeys.PointSampler, graphicsDevice.SamplerStates.PointWrap);

            // MainPlugin is going to use the readonly depth stencil buffer
            if (DepthStencilBuffer.IsReadOnlySupported(GraphicsDevice))
            {
                MainTargetPlugin.UseDepthStencilReadOnly = true;
                MainTargetPlugin.Parameters.Set(RenderTargetKeys.DepthStencilSource, DepthStencil.Texture);
                MainTargetPlugin.DepthStencilReadOnly = DepthStencil.Texture.ToDepthStencilBuffer(true);
            }
            else
            {
                RenderPass.EndPass += (context) =>
                    {
                        //context.GraphicsDevice.Copy(DepthStencil
                        
                        //DepthStencil.SynchronizeReadonly(context.GraphicsDevice)
                    };
            }

            defaultDescription = mainDepthStencilState.Description;
            defaultDescription.DepthBufferWriteEnable = false;
            DepthStencilStateZReadOnly = DepthStencilState.New(GraphicsDevice,defaultDescription);
            DepthStencilStateZReadOnly.Name = "ZReadOnly";

            // Create normal texture (that LightPlugin will use)
            var gbufferTexture = Texture.New2D(GraphicsDevice, graphicsDevice.BackBuffer.Width, graphicsDevice.BackBuffer.Height, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource | TextureFlags.RenderTarget);
            gbufferTexture.Name = "GBufferTexture";
            Parameters.Set(GBufferBaseKeys.GBufferTexture, gbufferTexture);
            RenderTarget = gbufferTexture.ToRenderTarget();

            // Set parameters for MainPlugin
            MainTargetPlugin.Parameters.Set(GBufferBaseKeys.GBufferTexture, gbufferTexture);
        }
Esempio n. 7
0
        /// <summary>
        /// Begins a sprite batch rendering using the specified sorting mode and blend state, sampler, depth stencil, rasterizer state objects and a custom effect.
        /// Passing null for any of the state objects selects the default default state objects (BlendState.AlphaBlend, depthStencilState.None, RasterizerState.CullCounterClockwise, SamplerState.LinearClamp).
        /// Passing a null effect selects the default effect shader.
        /// </summary>
        /// <param name="effect">The effect to use for this begin/end draw session (default effect if null)</param>
        /// <param name="parameterCollectionGroup">The parameter collection group.</param>
        /// <param name="sessionSortMode">Sprite drawing order used for the Begin/End session.</param>
        /// <param name="sessionBlendState">Blending state used for the Begin/End session</param>
        /// <param name="sessionSamplerState">Texture sampling used for the Begin/End session</param>
        /// <param name="sessionDepthStencilState">Depth and stencil state used for the Begin/End session</param>
        /// <param name="sessionRasterizerState">Rasterization state used for the Begin/End session</param>
        /// <param name="stencilValue">The value of the stencil buffer to take as reference for the Begin/End session</param>
        /// <exception cref="System.InvalidOperationException">Only one SpriteBatch at a time can use SpriteSortMode.Immediate</exception>
        protected void Begin(Effect effect, EffectParameterCollectionGroup parameterCollectionGroup, SpriteSortMode sessionSortMode, BlendState sessionBlendState, SamplerState sessionSamplerState, DepthStencilState sessionDepthStencilState, RasterizerState sessionRasterizerState, int stencilValue)
        {
            CheckEndHasBeenCalled("begin");

            SortMode              = sessionSortMode;
            BlendState            = sessionBlendState;
            SamplerState          = sessionSamplerState;
            DepthStencilState     = sessionDepthStencilState;
            RasterizerState       = sessionRasterizerState;
            StencilReferenceValue = stencilValue;

            Effect = effect ?? (GraphicsDevice.ColorSpace == ColorSpace.Linear ? DefaultEffectSRgb : DefaultEffect);
            ParameterCollectionGroup = parameterCollectionGroup ?? defaultParameterCollectionGroup;
            if (ParameterCollectionGroup == defaultParameterCollectionGroup && ParameterCollectionGroup.Effect != Effect)
            {
                // If ParameterCollectionGroup is not specified (using default one), let's make sure it is updated to matches effect
                // It is quite inefficient if user is often switching effect without providing a matching ParameterCollectionGroup
                ParameterCollectionGroup = defaultParameterCollectionGroup = new EffectParameterCollectionGroup(GraphicsDevice, Effect, new[] { parameters });
            }

            textureUpdater = null;
            if (Effect.HasParameter(TexturingKeys.Texture0))
            {
                textureUpdater = Effect.GetParameterFastUpdater(TexturingKeys.Texture0);
            }
            if (Effect.HasParameter(TexturingKeys.TextureCube0))
            {
                textureUpdater = Effect.GetParameterFastUpdater(TexturingKeys.TextureCube0);
            }
            if (Effect.HasParameter(TexturingKeys.Texture3D0))
            {
                textureUpdater = Effect.GetParameterFastUpdater(TexturingKeys.Texture3D0);
            }

            // Immediate mode, then prepare for rendering here instead of End()
            if (sessionSortMode == SpriteSortMode.Immediate)
            {
                if (ResourceContext.IsInImmediateMode)
                {
                    throw new InvalidOperationException("Only one SpriteBatch at a time can use SpriteSortMode.Immediate");
                }

                PrepareForRendering();

                ResourceContext.IsInImmediateMode = true;
            }

            // Sets to true isBeginCalled
            isBeginCalled = true;
        }
Esempio n. 8
0
        /// <summary>
        /// Begins a sprite batch rendering using the specified sorting mode and blend state, sampler, depth stencil, rasterizer state objects, plus a custom effect and a 2D transformation matrix. Passing null for any of the state objects selects the default default state objects (BlendState.AlphaBlend, DepthStencilState.Default, RasterizerState.CullCounterClockwise, SamplerState.LinearClamp). Passing a null effect selects the default SpriteBatch Class shader.
        /// </summary>
        /// <param name="viewMatrix">The view matrix to use for the batch session</param>
        /// <param name="projectionMatrix">The projection matrix to use for the batch session</param>
        /// <param name="sortMode">The sprite drawing order to use for the batch session</param>
        /// <param name="blendState">The blending state to use for the batch session</param>
        /// <param name="samplerState">The sampling state to use for the batch session</param>
        /// <param name="depthStencilState">The depth stencil state to use for the batch session</param>
        /// <param name="rasterizerState">The rasterizer state to use for the batch session</param>
        /// <param name="effect">The effect to use for the batch session</param>
        /// <param name="parameterCollectionGroup">The parameter collection group.</param>
        /// <param name="stencilValue">The value of the stencil buffer to take as reference for the batch session</param>
        public void Begin(Matrix viewMatrix, Matrix projectionMatrix, SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState blendState = null, SamplerState samplerState = null, DepthStencilState depthStencilState = null, RasterizerState rasterizerState = null, Effect effect = null, EffectParameterCollectionGroup parameterCollectionGroup = null, int stencilValue = 0)
        {
            CheckEndHasBeenCalled("begin");

            userViewMatrix       = viewMatrix;
            userProjectionMatrix = projectionMatrix;

            Begin(effect, parameterCollectionGroup, sortMode, blendState, samplerState, depthStencilState, rasterizerState, stencilValue);
        }
Esempio n. 9
0
 /// <summary>
 /// Begins a sprite batch rendering using the specified sorting mode and blend state, sampler, depth stencil, rasterizer state objects, plus a custom effect and a 2D transformation matrix. Passing null for any of the state objects selects the default default state objects (BlendState.AlphaBlend, DepthStencilState.Default, RasterizerState.CullCounterClockwise, SamplerState.LinearClamp). Passing a null effect selects the default SpriteBatch Class shader.
 /// </summary>
 /// <param name="viewMatrix">The view matrix to use for the batch session</param>
 /// <param name="sortMode">The sprite drawing order to use for the batch session</param>
 /// <param name="blendState">The blending state to use for the batch session</param>
 /// <param name="samplerState">The sampling state to use for the batch session</param>
 /// <param name="depthStencilState">The depth stencil state to use for the batch session</param>
 /// <param name="rasterizerState">The rasterizer state to use for the batch session</param>
 /// <param name="effect">The effect to use for the batch session</param>
 /// <param name="parameterCollectionGroup">The parameter collection group.</param>
 /// <param name="stencilValue">The value of the stencil buffer to take as reference for the batch session</param>
 public void Begin(Matrix viewMatrix, SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState blendState = null, SamplerState samplerState = null, DepthStencilState depthStencilState = null, RasterizerState rasterizerState = null, Effect effect = null, EffectParameterCollectionGroup parameterCollectionGroup = null, int stencilValue = 0)
 {
     UpdateDefaultProjectionMatrix();
     Begin(viewMatrix, defaultProjectionMatrix, sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, parameterCollectionGroup, stencilValue);
 }
Esempio n. 10
0
 /// <summary>
 /// Sets the depth-stencil state of the output-merger stage. See <see cref="Render+states"/> to learn how to use it.
 /// </summary>
 /// <param name="depthStencilState">a depth-stencil state</param>
 /// <param name="stencilReference">Reference value to perform against when doing a depth-stencil test.</param>
 private void SetDepthStencilStateImpl(DepthStencilState depthStencilState, int stencilReference = 0)
 {
     NativeDeviceContext.OutputMerger.SetDepthStencilState(depthStencilState != null ? (SharpDX.Direct3D11.DepthStencilState)depthStencilState.NativeDeviceChild : null, stencilReference);
 }