Ejemplo n.º 1
0
 public DeviceRenderState(AlphaBlendState alphaBlendState, AlphaTestState alphaTestState, DepthColourCullState depthState, StencilTestState stencilState)
 {
     StencilTest     = stencilState;
     AlphaBlend      = alphaBlendState;
     AlphaTest       = alphaTestState;
     DepthColourCull = depthState;
 }
Ejemplo n.º 2
0
        internal void ResetState(ref StencilTestState current, DeviceContext device)
        {
            device.SetRenderState(RenderState.StencilZFail, StencilPassZFailOperation);
            device.SetRenderState(RenderState.StencilPass, StencilPassOperation);
            device.SetRenderState(RenderState.StencilFail, StencilFailOperation);
            device.SetRenderState(RenderState.CcwStencilZFail, BackfaceStencilPassZFailOperation);
            device.SetRenderState(RenderState.CcwStencilPass, BackfaceStencilPassOperation);
            device.SetRenderState(RenderState.CcwStencilFail, BackfaceStencilFailOperation);
            device.SetRenderState(RenderState.StencilFunc, StencilFunction);
            device.SetRenderState(RenderState.CcwStencilFunc, BackfaceStencilFunction);

            device.SetRenderState(RenderState.StencilEnable, Enabled);
            device.SetRenderState(RenderState.TwoSidedStencilMode, TwoSidedStencilModeEnabled);

            device.SetRenderState(RenderState.StencilRef, ReferenceValue);
            device.SetRenderState(RenderState.StencilMask, StencilReadMask);
            device.SetRenderState(RenderState.StencilWriteMask, StencilWriteMask);

            current._op   = _op;
            current._mode = _mode;
        }
Ejemplo n.º 3
0
        public void Render(DrawState state, byte clipDepth)
        {
            Element parent = _parent;
            Matrix matrix;
            DeviceContext context = state.Context;

            if (parent == null)
            {
                _clipTest = false;

                DeviceRenderState rstate = new DeviceRenderState();

                rstate.DepthColourCull.DepthWriteEnabled = false;
                rstate.DepthColourCull.DepthTestEnabled = false;

                state.PushRenderState(ref rstate);

                if (_camera == null)
                    _camera = new ElementCamera(true);

                state.PushCamera(_camera);
            }
            else
                _clipTest = parent._clipTest | parent.ClipsChildren;

            StencilTestState stencilState = new StencilTestState();

            if (_clipTest)
            {
                stencilState.Enabled = true;
                stencilState.ReferenceValue = clipDepth;
                stencilState.StencilFunction = Compare.Equal;
                stencilState.StencilPassOperation = StencilOperation.Keep;
            }

            bool clearStencil = false;

            if (ClipsChildren)
            {
                clearStencil = clipDepth == 255;
                clipDepth--;

                if (!_clipTest)
                {
                    stencilState.Enabled = true;
                    stencilState.ReferenceValue = clipDepth;
                    stencilState.StencilPassOperation = StencilOperation.Replace;
                }
                else
                    stencilState.StencilPassOperation = StencilOperation.Decrement;
            }

            Viewport viewport = context.Viewport;
            Vector2 scale = new Vector2(viewport.Width, viewport.Height);

            if ((scale.X != 0 && scale.Y != 0))
            {
                Vector2 size = ElementSize;
                GetDisplayMatrix(out matrix, scale, ref size);

                state.PushWorldMatrixMultiply(ref matrix);

                BindShader(state, false);

                state.RenderState.AlphaBlend = _blendState;
                state.RenderState.StencilTest = stencilState;

                if (!UseSize)
                    size = new Vector2(1, 1);
                else if (IsNormalised)
                {
                    size.X *= scale.X;
                    size.Y *= scale.Y;
                }

                PreDraw(state.Context, size);

                DrawElement(state);

                List<Element> children = Children;
                if (children != null)
                    foreach (Element child in children)
                        if (child.CullTest(state))
                            child.Render(state, clipDepth);

                if (clearStencil)
                {
                    BindShader(state, true);

                    stencilState = new StencilTestState();
                    stencilState.Enabled = true;
                    stencilState.StencilFunction = Compare.Never;
                    stencilState.StencilFailOperation = StencilOperation.Zero;
                    state.RenderState.StencilTest = stencilState;

                    DrawElement(state);
                }

                state.PopWorldMatrix();
            }

            if (parent == null)
            {
                state.PopRenderState();
                state.PopCamera();
            }
        }
Ejemplo n.º 4
0
        internal bool ApplyState(ref StencilTestState current, DeviceContext device)
        {
            bool changed = false;

            if (Enabled)
            {
#if DEBUG
                changed = _mode != current._mode ||
                          _op != current._op;
#endif
                if (!current.Enabled)
                {
                    device.SetRenderState(RenderState.StencilEnable, true);
                }

                if (_op != current._op)
                {
                    if (current.StencilPassZFailOperation != StencilPassZFailOperation)
                    {
                        device.SetRenderState(RenderState.StencilZFail, StencilPassZFailOperation);
                    }

                    if (current.StencilPassOperation != StencilPassOperation)
                    {
                        device.SetRenderState(RenderState.StencilPass, StencilPassOperation);
                    }

                    if (current.StencilFailOperation != StencilFailOperation)
                    {
                        device.SetRenderState(RenderState.StencilFail, StencilFailOperation);
                    }

                    if (current.BackfaceStencilPassZFailOperation != BackfaceStencilPassZFailOperation)
                    {
                        device.SetRenderState(RenderState.CcwStencilZFail, BackfaceStencilPassZFailOperation);
                    }

                    if (current.BackfaceStencilPassOperation != BackfaceStencilPassOperation)
                    {
                        device.SetRenderState(RenderState.CcwStencilPass, BackfaceStencilPassOperation);
                    }

                    if (current.BackfaceStencilFailOperation != BackfaceStencilFailOperation)
                    {
                        device.SetRenderState(RenderState.CcwStencilFail, BackfaceStencilFailOperation);
                    }

                    if (current.StencilFunction != StencilFunction)
                    {
                        device.SetRenderState(RenderState.StencilFunc, StencilFunction);
                    }

                    if (current.BackfaceStencilFunction != BackfaceStencilFunction)
                    {
                        device.SetRenderState(RenderState.CcwStencilFunc, BackfaceStencilFunction);
                    }

                    current._op = _op;
                }

                if (current._mode != _mode)
                {
                    if (current.TwoSidedStencilModeEnabled != TwoSidedStencilModeEnabled)
                    {
                        device.SetRenderState(RenderState.TwoSidedStencilMode, TwoSidedStencilModeEnabled);
                    }

                    if (current.ReferenceValue != ReferenceValue)
                    {
                        device.SetRenderState(RenderState.StencilRef, ReferenceValue);
                    }

                    if (current.StencilReadMask != StencilReadMask)
                    {
                        device.SetRenderState(RenderState.StencilMask, StencilReadMask);
                    }

                    if (current.StencilWriteMask != StencilWriteMask)
                    {
                        device.SetRenderState(RenderState.StencilWriteMask, StencilWriteMask);
                    }
                }

                current._mode = _mode;
            }
            else
            {
                if (current.Enabled)
                {
#if DEBUG
                    changed = true;
#endif
                    device.SetRenderState(RenderState.StencilEnable, false);
                    current.Enabled = false;
                }
            }
            return(changed);
        }
Ejemplo n.º 5
0
        internal void ResetState(ref StencilTestState current, DeviceContext device)
        {
            device.SetRenderState(RenderState.StencilZFail, StencilPassZFailOperation);
            device.SetRenderState(RenderState.StencilPass, StencilPassOperation);
            device.SetRenderState(RenderState.StencilFail, StencilFailOperation);
            device.SetRenderState(RenderState.CcwStencilZFail, BackfaceStencilPassZFailOperation);
            device.SetRenderState(RenderState.CcwStencilPass, BackfaceStencilPassOperation);
            device.SetRenderState(RenderState.CcwStencilFail, BackfaceStencilFailOperation);
            device.SetRenderState(RenderState.StencilFunc, StencilFunction);
            device.SetRenderState(RenderState.CcwStencilFunc, BackfaceStencilFunction);

            device.SetRenderState(RenderState.StencilEnable, Enabled);
            device.SetRenderState(RenderState.TwoSidedStencilMode, TwoSidedStencilModeEnabled);

            device.SetRenderState(RenderState.StencilRef, ReferenceValue);
            device.SetRenderState(RenderState.StencilMask, StencilReadMask);
            device.SetRenderState(RenderState.StencilWriteMask, StencilWriteMask);

            current._op = _op;
            current._mode = _mode;
        }
Ejemplo n.º 6
0
        internal bool ApplyState(ref StencilTestState current, DeviceContext device)
        {
            bool changed = false;
            if (Enabled)
            {
            #if DEBUG
                changed = _mode != current._mode ||
                    _op != current._op;
            #endif
                if (!current.Enabled)
                    device.SetRenderState(RenderState.StencilEnable, true);

                if (_op != current._op)
                {
                    if (current.StencilPassZFailOperation != StencilPassZFailOperation)
                        device.SetRenderState(RenderState.StencilZFail, StencilPassZFailOperation);

                    if (current.StencilPassOperation != StencilPassOperation)
                        device.SetRenderState(RenderState.StencilPass, StencilPassOperation);

                    if (current.StencilFailOperation != StencilFailOperation)
                        device.SetRenderState(RenderState.StencilFail, StencilFailOperation);

                    if (current.BackfaceStencilPassZFailOperation != BackfaceStencilPassZFailOperation)
                        device.SetRenderState(RenderState.CcwStencilZFail, BackfaceStencilPassZFailOperation);

                    if (current.BackfaceStencilPassOperation != BackfaceStencilPassOperation)
                        device.SetRenderState(RenderState.CcwStencilPass, BackfaceStencilPassOperation);

                    if (current.BackfaceStencilFailOperation != BackfaceStencilFailOperation)
                        device.SetRenderState(RenderState.CcwStencilFail, BackfaceStencilFailOperation);

                    if (current.StencilFunction != StencilFunction)
                        device.SetRenderState(RenderState.StencilFunc, StencilFunction);

                    if (current.BackfaceStencilFunction != BackfaceStencilFunction)
                        device.SetRenderState(RenderState.CcwStencilFunc, BackfaceStencilFunction);

                    current._op = _op;
                }

                if (current._mode != _mode)
                {
                    if (current.TwoSidedStencilModeEnabled != TwoSidedStencilModeEnabled)
                        device.SetRenderState(RenderState.TwoSidedStencilMode, TwoSidedStencilModeEnabled);

                    if (current.ReferenceValue != ReferenceValue)
                        device.SetRenderState(RenderState.StencilRef, ReferenceValue);

                    if (current.StencilReadMask != StencilReadMask)
                        device.SetRenderState(RenderState.StencilMask, StencilReadMask);

                    if (current.StencilWriteMask != StencilWriteMask)
                        device.SetRenderState(RenderState.StencilWriteMask, StencilWriteMask);
                }

                current._mode = _mode;
            }
            else
            {
                if (current.Enabled)
                {
            #if DEBUG
                    changed = true;
            #endif
                    device.SetRenderState(RenderState.StencilEnable, false);
                    current.Enabled = false;
                }
            }
            return changed;
        }
Ejemplo n.º 7
0
        public void Render(DrawState state, byte clipDepth)
        {
            Element       parent = _parent;
            Matrix        matrix;
            DeviceContext context = state.Context;

            if (parent == null)
            {
                _clipTest = false;

                DeviceRenderState rstate = new DeviceRenderState();

                rstate.DepthColourCull.DepthWriteEnabled = false;
                rstate.DepthColourCull.DepthTestEnabled  = false;

                state.PushRenderState(ref rstate);

                if (_camera == null)
                {
                    _camera = new ElementCamera(true);
                }

                state.PushCamera(_camera);
            }
            else
            {
                _clipTest = parent._clipTest | parent.ClipsChildren;
            }

            StencilTestState stencilState = new StencilTestState();

            if (_clipTest)
            {
                stencilState.Enabled              = true;
                stencilState.ReferenceValue       = clipDepth;
                stencilState.StencilFunction      = Compare.Equal;
                stencilState.StencilPassOperation = StencilOperation.Keep;
            }

            bool clearStencil = false;

            if (ClipsChildren)
            {
                clearStencil = clipDepth == 255;
                clipDepth--;

                if (!_clipTest)
                {
                    stencilState.Enabled              = true;
                    stencilState.ReferenceValue       = clipDepth;
                    stencilState.StencilPassOperation = StencilOperation.Replace;
                }
                else
                {
                    stencilState.StencilPassOperation = StencilOperation.Decrement;
                }
            }

            Viewport viewport = context.Viewport;
            Vector2  scale    = new Vector2(viewport.Width, viewport.Height);

            if ((scale.X != 0 && scale.Y != 0))
            {
                Vector2 size = ElementSize;
                GetDisplayMatrix(out matrix, scale, ref size);

                state.PushWorldMatrixMultiply(ref matrix);

                BindShader(state, false);

                state.RenderState.AlphaBlend  = _blendState;
                state.RenderState.StencilTest = stencilState;

                if (!UseSize)
                {
                    size = new Vector2(1, 1);
                }
                else if (IsNormalised)
                {
                    size.X *= scale.X;
                    size.Y *= scale.Y;
                }

                PreDraw(state.Context, size);

                DrawElement(state);

                List <Element> children = Children;
                if (children != null)
                {
                    foreach (Element child in children)
                    {
                        if (child.CullTest(state))
                        {
                            child.Render(state, clipDepth);
                        }
                    }
                }

                if (clearStencil)
                {
                    BindShader(state, true);

                    stencilState                      = new StencilTestState();
                    stencilState.Enabled              = true;
                    stencilState.StencilFunction      = Compare.Never;
                    stencilState.StencilFailOperation = StencilOperation.Zero;
                    state.RenderState.StencilTest     = stencilState;

                    DrawElement(state);
                }

                state.PopWorldMatrix();
            }

            if (parent == null)
            {
                state.PopRenderState();
                state.PopCamera();
            }
        }