Beispiel #1
0
        public static DepthStencilState CreateDepthStencilState(Device device, bool depthEnable, DepthWriteMask writeMask)
        {
            DepthStencilOperationDescription frontFace = new DepthStencilOperationDescription();
            DepthStencilOperationDescription backFace  = new DepthStencilOperationDescription();
            bool stencil = false;//depthEnable;
            byte rm      = 0;
            byte wm      = 0;

            if (stencil)
            {
                rm = 0xFF;
                wm = 0xFF;
                // Stencil operations if pixel is front-facing
                frontFace.FailOperation      = StencilOperation.Keep;      // D3D11_STENCIL_OP_KEEP;
                frontFace.DepthFailOperation = StencilOperation.Increment; // D3D11_STENCIL_OP_INCR;
                frontFace.PassOperation      = StencilOperation.Keep;      // D3D11_STENCIL_OP_KEEP;
                frontFace.Comparison         = Comparison.Always;          // D3D11_COMPARISON_ALWAYS;

                // Stencil operations if pixel is back-facing
                backFace.FailOperation      = StencilOperation.Keep;      // D3D11_STENCIL_OP_KEEP;
                backFace.DepthFailOperation = StencilOperation.Decrement; // D3D11_STENCIL_OP_DECR;
                backFace.PassOperation      = StencilOperation.Keep;      // D3D11_STENCIL_OP_KEEP;
                backFace.Comparison         = Comparison.Always;          // D3D11_COMPARISON_ALWAYS;
            }

            return(CreateDepthStencilState(device, depthEnable, writeMask, Comparison.LessEqual, stencil, rm, wm, frontFace, backFace));
        }
 public DepthStencilOperationDataContract(DepthStencilOperationDescription desc)
 {
     FailOperation      = (int)desc.FailOperation;
     DepthFailOperation = (int)desc.DepthFailOperation;
     PassOperation      = (int)desc.PassOperation;
     Comparison         = (int)desc.Comparison;
 }
Beispiel #3
0
 public DepthStencilStateDescription(bool isDepthEnabled, DepthWriteMask depthWriteMask)
 {
     IsDepthEnabled   = isDepthEnabled;
     DepthWriteMask   = depthWriteMask;
     DepthComparison  = Comparison.Less;
     IsStencilEnabled = false;
     StencilReadMask  = 0;
     StencilWriteMask = 0;
     FrontFace        = new DepthStencilOperationDescription();
     BackFace         = new DepthStencilOperationDescription();
 }
		public DepthStencilStateDescription(bool isDepthEnabled, DepthWriteMask depthWriteMask)
		{
			IsDepthEnabled = isDepthEnabled;
			DepthWriteMask = depthWriteMask;
			DepthComparison = Comparison.Less;
			IsStencilEnabled = false;
			StencilReadMask = 0;
			StencilWriteMask = 0;
			FrontFace = new DepthStencilOperationDescription();
			BackFace = new DepthStencilOperationDescription();
		}
Beispiel #5
0
        private void CreateDefaultRenderStates()
        {
            var blendStateDesc = new BlendStateDescription();

            blendStateDesc.IsAlphaToCoverageEnabled = false;
            blendStateDesc.BlendOperation           = BlendOperation.Add;
            blendStateDesc.AlphaBlendOperation      = BlendOperation.Add;
            blendStateDesc.SourceBlend           = BlendOption.One;
            blendStateDesc.DestinationBlend      = BlendOption.Zero;
            blendStateDesc.SourceAlphaBlend      = BlendOption.One;
            blendStateDesc.DestinationAlphaBlend = BlendOption.Zero;
            defaultBlendState = BlendState.FromDescription(device, blendStateDesc);

            var rasterizerStateDesc = new RasterizerStateDescription();

            rasterizerStateDesc.FillMode = FillMode.Solid;
            rasterizerStateDesc.CullMode = CullMode.Back;
            rasterizerStateDesc.IsFrontCounterclockwise = false;
            rasterizerStateDesc.DepthBias                = 0;
            rasterizerStateDesc.DepthBiasClamp           = 0;
            rasterizerStateDesc.SlopeScaledDepthBias     = 0;
            rasterizerStateDesc.IsDepthClipEnabled       = true;
            rasterizerStateDesc.IsScissorEnabled         = false;
            rasterizerStateDesc.IsMultisampleEnabled     = false;
            rasterizerStateDesc.IsAntialiasedLineEnabled = false;
            defaultRasterizerState = RasterizerState.FromDescription(device, rasterizerStateDesc);

            var depthStencilStateDesc = new DepthStencilStateDescription();

            depthStencilStateDesc.IsDepthEnabled   = true;
            depthStencilStateDesc.DepthWriteMask   = DepthWriteMask.All;
            depthStencilStateDesc.DepthComparison  = Comparison.LessEqual;
            depthStencilStateDesc.IsStencilEnabled = false;
            depthStencilStateDesc.StencilReadMask  = 0xff;
            depthStencilStateDesc.StencilWriteMask = 0xff;
            var depthStencilStateFaceDesk = new DepthStencilOperationDescription();

            depthStencilStateFaceDesk.Comparison         = Comparison.Always;
            depthStencilStateFaceDesk.DepthFailOperation = StencilOperation.Keep;
            depthStencilStateFaceDesk.FailOperation      = StencilOperation.Keep;
            depthStencilStateFaceDesk.PassOperation      = StencilOperation.Keep;
            depthStencilStateDesc.FrontFace = depthStencilStateFaceDesk;
            depthStencilStateDesc.BackFace  = depthStencilStateFaceDesk;
            defaultDepthStencilState        = DepthStencilState.FromDescription(device, depthStencilStateDesc);
        }
        /// <summary>
        /// Creates a default <see cref="DepthStencilState"/> object.
        /// </summary>
        /// <param name="device">The current <see cref="Device"/> being used.</param>
        /// <returns>The newly created <see cref="DepthStencilState"/> with the default options.</returns>
        public static DepthStencilState CreateDepthStencilState(Device device)
        {
            DepthStencilOperationDescription dsOperation = new DepthStencilOperationDescription();

            dsOperation.Comparison         = Comparison.Less;
            dsOperation.DepthFailOperation = StencilOperation.Keep;
            dsOperation.FailOperation      = StencilOperation.Keep;
            dsOperation.PassOperation      = StencilOperation.Replace;

            DepthStencilStateDescription dsDesc = new DepthStencilStateDescription();

            dsDesc.BackFace         = dsOperation;
            dsDesc.DepthComparison  = Comparison.Less;
            dsDesc.DepthWriteMask   = DepthWriteMask.All;
            dsDesc.FrontFace        = dsOperation;
            dsDesc.IsDepthEnabled   = true;
            dsDesc.IsStencilEnabled = false;

            return(DepthStencilState.FromDescription(device, dsDesc));
        }
Beispiel #7
0
        private void OnViewportRender(object sender, RenderEventArgs e)
        {
            foreach (IRenderBlock block in this._Model.Blocks)
            {
                using (var stateBlock = new StateBlock(e.Device, StateBlockMask.EnableAll()))
                {
                    stateBlock.Capture();

                    var dssd = new DepthStencilStateDescription();
                    dssd.IsDepthEnabled   = true;
                    dssd.DepthWriteMask   = DepthWriteMask.All;
                    dssd.DepthComparison  = Comparison.Less;
                    dssd.IsStencilEnabled = true;
                    dssd.StencilReadMask  = 0xFF;
                    dssd.StencilWriteMask = 0xFF;

                    var frontFace = new DepthStencilOperationDescription();
                    frontFace.FailOperation      = StencilOperation.Keep;
                    frontFace.DepthFailOperation = StencilOperation.Increment;
                    frontFace.PassOperation      = StencilOperation.Keep;
                    frontFace.Comparison         = Comparison.Always;
                    dssd.FrontFace = frontFace;

                    var backFace = new DepthStencilOperationDescription();
                    backFace.FailOperation      = StencilOperation.Keep;
                    backFace.DepthFailOperation = StencilOperation.Decrement;
                    backFace.PassOperation      = StencilOperation.Keep;
                    backFace.Comparison         = Comparison.Always;
                    dssd.BackFace = backFace;

                    e.Device.OutputMerger.DepthStencilState = DepthStencilState.FromDescription(e.Device, dssd);

                    var oldState = e.Device.Rasterizer.State.Description;
                    var state    = e.Device.Rasterizer.State.Description;
                    state.FillMode = _SelectedBlocks.Contains(block) == true
                                         ? FillMode.Wireframe
                                         : FillMode.Solid;
                    state.CullMode            = CullMode.None;
                    e.Device.Rasterizer.State = RasterizerState.FromDescription(e.Device, state);

                    if (this._BlockRenderers.ContainsKey(block) == false)
                    {
                        var renderer = RendererTypes.Instantiate(block);
                        if (renderer == null)
                        {
                            continue;
                        }

                        renderer.Setup(e.Device,
                                       block,
                                       this._ShaderBundle,
                                       this._ModelPath);
                        this._BlockRenderers.Add(block, renderer);
                        this._BlockRenderers[block].Render(e.Device, e.ViewProjectionMatrix);
                    }
                    else
                    {
                        this._BlockRenderers[block].Render(e.Device, e.ViewProjectionMatrix);
                    }

                    e.Device.Rasterizer.State = RasterizerState.FromDescription(e.Device, oldState);

                    stateBlock.Apply();
                }
            }
        }
        public void Evaluate(int SpreadMax)
        {
            if (this.FInComparison.IsChanged ||
                this.FInDepthWriteMask.IsChanged ||
                this.FInEnableDepth.IsChanged ||
                this.FInEnableStencil.IsChanged ||
                this.FInState.IsChanged ||
                this.FInBFComp.IsChanged ||
                this.FInBFDFOp.IsChanged ||
                this.FInBFFOp.IsChanged ||
                this.FInBFPOp.IsChanged ||
                this.FInFFComp.IsChanged ||
                this.FInFFDFOp.IsChanged ||
                this.FInFFFOp.IsChanged ||
                this.FInFFPOp.IsChanged ||
                this.FInStencilReadMask.IsChanged ||
                this.FInStencilWriteMask.IsChanged)
            {
                this.FOutState.SliceCount = SpreadMax;

                for (int i = 0; i < SpreadMax; i++)
                {
                    DX11RenderState rs;
                    if (this.FInState.PluginIO.IsConnected)
                    {
                        rs = this.FInState[i];
                    }
                    else
                    {
                        rs = new DX11RenderState();
                    }

                    DepthStencilStateDescription ds = rs.DepthStencil;
                    ds.DepthComparison  = this.FInComparison[i];
                    ds.DepthWriteMask   = this.FInDepthWriteMask[i];
                    ds.IsDepthEnabled   = this.FInEnableDepth[i];
                    ds.IsStencilEnabled = this.FInEnableStencil[i];
                    int srm = Math.Min(255, Math.Max(this.FInStencilReadMask[i], 0));
                    int swm = Math.Min(255, Math.Max(this.FInStencilWriteMask[i], 0));
                    ds.StencilReadMask  = Convert.ToByte(srm);
                    ds.StencilWriteMask = Convert.ToByte(swm);

                    DepthStencilOperationDescription dbf = new DepthStencilOperationDescription();
                    dbf.Comparison         = this.FInBFComp[i];
                    dbf.DepthFailOperation = this.FInBFDFOp[i];
                    dbf.FailOperation      = this.FInBFFOp[i];
                    dbf.PassOperation      = this.FInBFPOp[i];

                    ds.BackFace = dbf;


                    DepthStencilOperationDescription dff = new DepthStencilOperationDescription();
                    dff.Comparison         = this.FInFFComp[i];
                    dff.DepthFailOperation = this.FInFFDFOp[i];
                    dff.FailOperation      = this.FInFFFOp[i];
                    dff.PassOperation      = this.FInFFPOp[i];

                    ds.FrontFace      = dff;
                    rs.DepthStencil   = ds;
                    this.FOutState[i] = rs;
                }
            }
        }
        public void Evaluate(int SpreadMax)
        {
            if (this.FInComparison.IsChanged
                || this.FInDepthWriteMask.IsChanged
                || this.FInEnableDepth.IsChanged
                || this.FInEnableStencil.IsChanged
                || this.FInState.IsChanged
                || this.FInBFComp.IsChanged
                || this.FInBFDFOp.IsChanged
                || this.FInBFFOp.IsChanged
                || this.FInBFPOp.IsChanged
                || this.FInFFComp.IsChanged
                || this.FInFFDFOp.IsChanged
                || this.FInFFFOp.IsChanged
                || this.FInFFPOp.IsChanged
                || this.FInStencilReadMask.IsChanged
                || this.FInStencilWriteMask.IsChanged)
            {
                this.FOutState.SliceCount = SpreadMax;

                for (int i = 0; i < SpreadMax; i++)
                {
                    DX11RenderState rs;
                    if (this.FInState.PluginIO.IsConnected)
                    {
                        rs = this.FInState[i];
                    }
                    else
                    {
                        rs = new DX11RenderState();
                    }

                    DepthStencilStateDescription ds = rs.DepthStencil;
                    ds.DepthComparison = this.FInComparison[i];
                    ds.DepthWriteMask = this.FInDepthWriteMask[i];
                    ds.IsDepthEnabled = this.FInEnableDepth[i];
                    ds.IsStencilEnabled = this.FInEnableStencil[i];
                    int srm = Math.Min(255, Math.Max(this.FInStencilReadMask[i], 0));
                    int swm = Math.Min(255, Math.Max(this.FInStencilWriteMask[i], 0));
                    ds.StencilReadMask = Convert.ToByte(srm);
                    ds.StencilWriteMask = Convert.ToByte(swm);

                    DepthStencilOperationDescription dbf = new DepthStencilOperationDescription();
                    dbf.Comparison = this.FInBFComp[i];
                    dbf.DepthFailOperation = this.FInBFDFOp[i];
                    dbf.FailOperation = this.FInBFFOp[i];
                    dbf.PassOperation = this.FInBFPOp[i];

                    ds.BackFace = dbf;

                    DepthStencilOperationDescription dff = new DepthStencilOperationDescription();
                    dff.Comparison = this.FInFFComp[i];
                    dff.DepthFailOperation = this.FInFFDFOp[i];
                    dff.FailOperation = this.FInFFFOp[i];
                    dff.PassOperation = this.FInFFPOp[i];

                    ds.FrontFace = dff;
                    rs.DepthStencil = ds;
                    this.FOutState[i] = rs;
                }

            }
        }
Beispiel #10
0
        public override void Initialize()
        {
            try
            {
                #region Rasterizer States

                RasterizerStateDescription rStateDefault = new RasterizerStateDescription()
                {
                    FillMode = FillMode.Solid,
                    CullMode = CullMode.Back,
                    IsFrontCounterclockwise = true,
                    DepthBias      = 0,
                    DepthBiasClamp = 0,
                    //SlopeScaledDepthBias = 0.0f,
                    //IsDepthClipEnabled = true
                };

                rasterizerStateDefault = RasterizerState.FromDescription(DeviceManager.Instance.device, rStateDefault);

                RasterizerStateDescription rStateDefaultNC = rStateDefault;
                rStateDefaultNC.CullMode = CullMode.None;
                rasterizerStateDefaultNC = RasterizerState.FromDescription(DeviceManager.Instance.device, rStateDefaultNC);

                RasterizerStateDescription rStateWireframe = new RasterizerStateDescription()
                {
                    FillMode = FillMode.Wireframe,
                    CullMode = CullMode.None,
                    IsFrontCounterclockwise = true,
                    DepthBias      = 0,
                    DepthBiasClamp = 0,
                    //SlopeScaledDepthBias = 0.0f,
                    //IsDepthClipEnabled = true
                };

                rasterizerStateWireframe = RasterizerState.FromDescription(DeviceManager.Instance.device, rStateWireframe);

                #endregion

                #region Blend States

                RenderTargetBlendDescription rtBlendDefault = new RenderTargetBlendDescription()
                {
                    BlendEnable           = true,
                    BlendOperation        = BlendOperation.Add,
                    RenderTargetWriteMask = ColorWriteMaskFlags.All,
                    SourceBlend           = BlendOption.SourceAlpha,
                    DestinationBlend      = BlendOption.InverseSourceAlpha,
                    BlendOperationAlpha   = BlendOperation.Add,
                    SourceBlendAlpha      = BlendOption.One,
                    DestinationBlendAlpha = BlendOption.Zero
                };

                BlendStateDescription bBlendStateDefault = new BlendStateDescription();
                bBlendStateDefault.AlphaToCoverageEnable  = false;
                bBlendStateDefault.IndependentBlendEnable = false;
                bBlendStateDefault.RenderTargets[0]       = rtBlendDefault;

                blendStateDefault = BlendState.FromDescription(DeviceManager.Instance.device, bBlendStateDefault);

                RenderTargetBlendDescription rtBlendAddColour = new RenderTargetBlendDescription()
                {
                    BlendEnable           = true,
                    BlendOperation        = BlendOperation.Add,
                    RenderTargetWriteMask = ColorWriteMaskFlags.All,
                    SourceBlend           = BlendOption.One,
                    DestinationBlend      = BlendOption.One,
                    BlendOperationAlpha   = BlendOperation.Add,
                    SourceBlendAlpha      = BlendOption.One,
                    DestinationBlendAlpha = BlendOption.Zero
                };

                BlendStateDescription bBlendStateAddColour = new BlendStateDescription();
                bBlendStateAddColour.AlphaToCoverageEnable  = false;
                bBlendStateAddColour.IndependentBlendEnable = false;
                bBlendStateAddColour.RenderTargets[0]       = rtBlendAddColour;

                blendStateAddColour = BlendState.FromDescription(DeviceManager.Instance.device, bBlendStateAddColour);

                // UseAlphaMask = textureAttributes & 0x0010

                /*RenderTargetBlendDescription rtBlendSubstactColour = new RenderTargetBlendDescription()
                 * {
                 * BlendEnable = true,
                 * BlendOperation = BlendOperation.ReverseSubtract,
                 * RenderTargetWriteMask = ColorWriteMaskFlags.All,
                 * SourceBlend = BlendOption.SourceColor, // One works too
                 * DestinationBlend = BlendOption.InverseSourceColor, // One works too
                 * BlendOperationAlpha = BlendOperation.Add,
                 * SourceBlendAlpha = BlendOption.One,
                 * DestinationBlendAlpha = BlendOption.Zero
                 * };*/

                RenderTargetBlendDescription rtBlendDepth = new RenderTargetBlendDescription()
                {
                    BlendEnable           = false,
                    RenderTargetWriteMask = ColorWriteMaskFlags.None
                };

                BlendStateDescription bBlendStateDepth = new BlendStateDescription();
                bBlendStateDepth.AlphaToCoverageEnable  = false;
                bBlendStateDepth.IndependentBlendEnable = false;
                bBlendStateDepth.RenderTargets[0]       = rtBlendDepth;

                blendStateDepth = BlendState.FromDescription(DeviceManager.Instance.device, bBlendStateDepth);

                #endregion

                #region Depth Stencils

                DepthStencilOperationDescription frontFace = new DepthStencilOperationDescription()
                {
                    FailOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Increment,
                    PassOperation      = StencilOperation.Keep,
                    Comparison         = Comparison.Always
                };

                DepthStencilOperationDescription backFace = new DepthStencilOperationDescription()
                {
                    FailOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Decrement,
                    PassOperation      = StencilOperation.Keep,
                    Comparison         = Comparison.Always
                };

                DepthStencilStateDescription dsStateSolid = new DepthStencilStateDescription
                {
                    DepthComparison  = Comparison.LessEqual,
                    DepthWriteMask   = DepthWriteMask.All,
                    IsDepthEnabled   = true,
                    IsStencilEnabled = false,
                    StencilReadMask  = 0xFF,
                    StencilWriteMask = 0xFF,
                    FrontFace        = frontFace,
                    BackFace         = backFace
                };

                depthStencilStateSolid = DepthStencilState.FromDescription(DeviceManager.Instance.device, dsStateSolid);

                DepthStencilStateDescription dsStateTranslucent = new DepthStencilStateDescription
                {
                    DepthComparison  = Comparison.LessEqual,
                    DepthWriteMask   = DepthWriteMask.Zero,
                    IsDepthEnabled   = true,
                    IsStencilEnabled = false,
                    StencilReadMask  = 0xFF,
                    StencilWriteMask = 0xFF,
                    FrontFace        = frontFace,
                    BackFace         = backFace
                };

                depthStencilStateTranslucent = DepthStencilState.FromDescription(DeviceManager.Instance.device, dsStateTranslucent);

                DepthStencilStateDescription dsStateDepth = new DepthStencilStateDescription
                {
                    DepthComparison  = Comparison.LessEqual,
                    DepthWriteMask   = DepthWriteMask.All,
                    IsDepthEnabled   = true,
                    IsStencilEnabled = false,
                    StencilReadMask  = 0xFF,
                    StencilWriteMask = 0xFF,
                    FrontFace        = frontFace,
                    BackFace         = backFace
                };

                depthStencilStateDepth = DepthStencilState.FromDescription(DeviceManager.Instance.device, dsStateDepth);

                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Beispiel #11
0
        public static DepthStencilState CreateDepthStencilState(Device device, bool depthEnable, DepthWriteMask writeMask, Comparison func, bool stencilEnable, byte stencilReadMask, byte stencilWriteMask, DepthStencilOperationDescription frontFace, DepthStencilOperationDescription backFace)
        {
            DepthStencilStateDescription dsd;

            dsd.IsDepthEnabled   = depthEnable;
            dsd.DepthWriteMask   = writeMask;
            dsd.DepthComparison  = func;
            dsd.IsStencilEnabled = stencilEnable;
            dsd.StencilReadMask  = stencilReadMask;
            dsd.StencilWriteMask = stencilWriteMask;
            dsd.FrontFace        = frontFace;
            dsd.BackFace         = backFace;
            DepthStencilState s = new DepthStencilState(device, dsd);

            return(s);
        }
 public static DepthStencilOperation Import(ref DepthStencilOperationDescription src)
 {
     var dest = new DepthStencilOperation();
     dest.Comparison = src.Comparison;
     dest.DepthFailOperation = src.DepthFailOperation;
     dest.FailOperation = src.FailOperation;
     dest.PassOperation = src.PassOperation;
     return dest;
 }
        private void BuildPSOs()
        {
            //
            // PSO for opaque objects.
            //

            var opaquePsoDesc = new GraphicsPipelineStateDescription
            {
                InputLayout           = _inputLayout,
                RootSignature         = _rootSignature,
                VertexShader          = _shaders["standardVS"],
                PixelShader           = _shaders["opaquePS"],
                RasterizerState       = RasterizerStateDescription.Default(),
                BlendState            = BlendStateDescription.Default(),
                DepthStencilState     = DepthStencilStateDescription.Default(),
                SampleMask            = unchecked ((int)0xFFFFFFFF),
                PrimitiveTopologyType = PrimitiveTopologyType.Triangle,
                RenderTargetCount     = 1,
                SampleDescription     = new SampleDescription(MsaaCount, MsaaQuality),
                DepthStencilFormat    = DepthStencilFormat,
                StreamOutput          = new StreamOutputDescription() //find out how this should actually be done later
            };

            opaquePsoDesc.RenderTargetFormats[0] = BackBufferFormat;
            _psos["opaque"] = Device.CreateGraphicsPipelineState(opaquePsoDesc);

            //
            // PSO for transparent objects.
            //

            GraphicsPipelineStateDescription transparentPsoDesc = opaquePsoDesc.Copy();
            var transparencyBlendDesc = new RenderTargetBlendDescription
            {
                IsBlendEnabled        = true,
                LogicOpEnable         = false,
                SourceBlend           = BlendOption.SourceAlpha,
                DestinationBlend      = BlendOption.InverseSourceAlpha,
                BlendOperation        = BlendOperation.Add,
                SourceAlphaBlend      = BlendOption.One,
                DestinationAlphaBlend = BlendOption.Zero,
                AlphaBlendOperation   = BlendOperation.Add,
                LogicOp = LogicOperation.Noop,
                RenderTargetWriteMask = ColorWriteMaskFlags.All
            };

            transparentPsoDesc.BlendState.RenderTarget[0] = transparencyBlendDesc;
            _psos["transparent"] = Device.CreateGraphicsPipelineState(transparentPsoDesc);

            //
            // PSO for marking stencil mirrors.
            //

            // We are not rendering backfacing polygons, so these settings do not matter.
            var backFaceDSO = new DepthStencilOperationDescription
            {
                FailOperation      = StencilOperation.Keep,
                DepthFailOperation = StencilOperation.Keep,
                PassOperation      = StencilOperation.Replace,
                Comparison         = Comparison.Always
            };

            var mirrorBlendState = BlendStateDescription.Default();

            mirrorBlendState.RenderTarget[0].RenderTargetWriteMask = 0;
            var mirrorDSS = new DepthStencilStateDescription
            {
                IsDepthEnabled   = true,
                DepthWriteMask   = DepthWriteMask.Zero,
                DepthComparison  = Comparison.Less,
                IsStencilEnabled = true,
                StencilReadMask  = 0xff,
                StencilWriteMask = 0xff,

                FrontFace = new DepthStencilOperationDescription
                {
                    FailOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Keep,
                    PassOperation      = StencilOperation.Replace,
                    Comparison         = Comparison.Always
                },
                BackFace = backFaceDSO
            };

            GraphicsPipelineStateDescription markMirrorsPsoDesc = opaquePsoDesc.Copy();

            markMirrorsPsoDesc.BlendState        = mirrorBlendState;
            markMirrorsPsoDesc.DepthStencilState = mirrorDSS;
            _psos["markStencilMirrors"]          = Device.CreateGraphicsPipelineState(markMirrorsPsoDesc);

            //
            // PSO for stencil reflections.
            //

            var reflectionDSS = new DepthStencilStateDescription
            {
                IsDepthEnabled   = true,
                DepthWriteMask   = DepthWriteMask.All,
                DepthComparison  = Comparison.Less,
                IsStencilEnabled = true,
                StencilReadMask  = 0xff,
                StencilWriteMask = 0xff,

                FrontFace = new DepthStencilOperationDescription
                {
                    FailOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Keep,
                    PassOperation      = StencilOperation.Keep,
                    Comparison         = Comparison.Equal
                },
                BackFace = backFaceDSO
            };

            GraphicsPipelineStateDescription drawReflectionsPsoDesc = opaquePsoDesc.Copy();

            drawReflectionsPsoDesc.DepthStencilState        = reflectionDSS;
            drawReflectionsPsoDesc.RasterizerState.CullMode = CullMode.Back;
            drawReflectionsPsoDesc.RasterizerState.IsFrontCounterClockwise = true;
            _psos["drawStencilReflections"] = Device.CreateGraphicsPipelineState(drawReflectionsPsoDesc);

            //
            // PSO for shadow objects.
            //

            var shadowDSS = new DepthStencilStateDescription
            {
                IsDepthEnabled   = true,
                DepthWriteMask   = DepthWriteMask.All,
                DepthComparison  = Comparison.Less,
                IsStencilEnabled = true,
                StencilReadMask  = 0xff,
                StencilWriteMask = 0xff,

                FrontFace = new DepthStencilOperationDescription
                {
                    FailOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Keep,
                    PassOperation      = StencilOperation.Increment,
                    Comparison         = Comparison.Equal
                },
                BackFace = backFaceDSO
            };

            GraphicsPipelineStateDescription shadowPsoDesc = transparentPsoDesc.Copy();

            shadowPsoDesc.DepthStencilState = shadowDSS;
            _psos["shadow"] = Device.CreateGraphicsPipelineState(shadowPsoDesc);
        }
Beispiel #14
0
        void CreateDeviceObjects()
        {
            var vertexShaderCode =
                @"
                    cbuffer vertexBuffer : register(b0) 
                    {
                        float4x4 ProjectionMatrix; 
                    };

                    struct VS_INPUT
                    {
                        float2 pos : POSITION;
                        float4 col : COLOR0;
                        float2 uv  : TEXCOORD0;
                    };
            
                    struct PS_INPUT
                    {
                        float4 pos : SV_POSITION;
                        float4 col : COLOR0;
                        float2 uv  : TEXCOORD0;
                    };
            
                    PS_INPUT main(VS_INPUT input)
                    {
                        PS_INPUT output;
                        output.pos = mul(ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));
                        output.col = input.col;
                        output.uv  = input.uv;
                        return output;
                    }";

            Compiler.Compile(vertexShaderCode, "main", "vs", "vs_4_0", out vertexShaderBlob, out var errorBlob);
            if (vertexShaderBlob == null)
            {
                throw new Exception("error compiling vertex shader");
            }

            vertexShader = device.CreateVertexShader(vertexShaderBlob.GetBytes());

            var inputElements = new[]
            {
                new InputElementDescription("POSITION", 0, Format.R32G32_Float, 0, 0, InputClassification.PerVertexData, 0),
                new InputElementDescription("TEXCOORD", 0, Format.R32G32_Float, 8, 0, InputClassification.PerVertexData, 0),
                new InputElementDescription("COLOR", 0, Format.R8G8B8A8_UNorm, 16, 0, InputClassification.PerVertexData, 0),
            };

            inputLayout = device.CreateInputLayout(inputElements, vertexShaderBlob);

            var constBufferDesc = new BufferDescription
            {
                SizeInBytes    = VertexConstantBufferSize,
                Usage          = Vortice.Direct3D11.Usage.Dynamic,
                BindFlags      = BindFlags.ConstantBuffer,
                CpuAccessFlags = CpuAccessFlags.Write
            };

            constantBuffer = device.CreateBuffer(constBufferDesc);

            var pixelShaderCode =
                @"struct PS_INPUT
                    {
                        float4 pos : SV_POSITION;
                        float4 col : COLOR0;
                        float2 uv  : TEXCOORD0;
                    };

                    sampler sampler0;
                    Texture2D texture0;
            
                    float4 main(PS_INPUT input) : SV_Target
                    {
                        float4 out_col = input.col * texture0.Sample(sampler0, input.uv); 
                        return out_col; 
                    }";

            Compiler.Compile(pixelShaderCode, "main", "ps", "ps_4_0", out pixelShaderBlob, out errorBlob);
            if (pixelShaderBlob == null)
            {
                throw new Exception("error compiling pixel shader");
            }

            pixelShader = device.CreatePixelShader(pixelShaderBlob.GetBytes());

            var blendDesc = new BlendDescription
            {
                AlphaToCoverageEnable = false
            };

            blendDesc.RenderTarget[0] = new RenderTargetBlendDescription
            {
                IsBlendEnabled        = true,
                SourceBlend           = Blend.SourceAlpha,
                DestinationBlend      = Blend.InverseSourceAlpha,
                BlendOperation        = BlendOperation.Add,
                SourceBlendAlpha      = Blend.InverseSourceAlpha,
                DestinationBlendAlpha = Blend.Zero,
                BlendOperationAlpha   = BlendOperation.Add,
                RenderTargetWriteMask = ColorWriteEnable.All
            };

            blendState = device.CreateBlendState(blendDesc);

            var rasterDesc = new RasterizerDescription
            {
                FillMode        = FillMode.Solid,
                CullMode        = CullMode.None,
                ScissorEnable   = true,
                DepthClipEnable = true
            };

            rasterizerState = device.CreateRasterizerState(rasterDesc);

            var stencilOpDesc = new DepthStencilOperationDescription(StencilOperation.Keep, StencilOperation.Keep, StencilOperation.Keep, ComparisonFunction.Always);
            var depthDesc     = new DepthStencilDescription
            {
                DepthEnable    = false,
                DepthWriteMask = DepthWriteMask.All,
                DepthFunc      = ComparisonFunction.Always,
                StencilEnable  = false,
                FrontFace      = stencilOpDesc,
                BackFace       = stencilOpDesc
            };

            depthStencilState = device.CreateDepthStencilState(depthDesc);

            CreateFontsTexture();
        }