Example #1
0
        internal void ApplyState(GraphicsDevice device)
        {
            if (_state == null)
            {
                // We're now bound to a device... no one should
                // be changing the state of this object now!
                GraphicsDevice = device;

                // Build the description.
                var desc = new SharpDX.Direct3D11.DepthStencilStateDescription();

                desc.IsDepthEnabled  = DepthBufferEnable;
                desc.DepthComparison = GetComparison(DepthBufferFunction);

                if (DepthBufferWriteEnable)
                {
                    desc.DepthWriteMask = SharpDX.Direct3D11.DepthWriteMask.All;
                }
                else
                {
                    desc.DepthWriteMask = SharpDX.Direct3D11.DepthWriteMask.Zero;
                }

                desc.IsStencilEnabled = StencilEnable;
                desc.StencilReadMask  = (byte)StencilMask; // TODO: Should this instead grab the upper 8bits?
                desc.StencilWriteMask = (byte)StencilWriteMask;

                if (TwoSidedStencilMode)
                {
                    desc.BackFace.Comparison         = GetComparison(CounterClockwiseStencilFunction);
                    desc.BackFace.DepthFailOperation = GetStencilOp(CounterClockwiseStencilDepthBufferFail);
                    desc.BackFace.FailOperation      = GetStencilOp(CounterClockwiseStencilFail);
                    desc.BackFace.PassOperation      = GetStencilOp(CounterClockwiseStencilPass);
                }
                else
                {   //use same settings as frontFace
                    desc.BackFace.Comparison         = GetComparison(StencilFunction);
                    desc.BackFace.DepthFailOperation = GetStencilOp(StencilDepthBufferFail);
                    desc.BackFace.FailOperation      = GetStencilOp(StencilFail);
                    desc.BackFace.PassOperation      = GetStencilOp(StencilPass);
                }

                desc.FrontFace.Comparison         = GetComparison(StencilFunction);
                desc.FrontFace.DepthFailOperation = GetStencilOp(StencilDepthBufferFail);
                desc.FrontFace.FailOperation      = GetStencilOp(StencilFail);
                desc.FrontFace.PassOperation      = GetStencilOp(StencilPass);

                // Create the state.
                _state = new SharpDX.Direct3D11.DepthStencilState(GraphicsDevice._d3dDevice, desc);
            }

            Debug.Assert(GraphicsDevice == device, "The state was created for a different device!");

            // NOTE: We make the assumption here that the caller has
            // locked the d3dContext for us to use.

            // Apply the state!
            device._d3dContext.OutputMerger.DepthStencilReference = ReferenceStencil;
            device._d3dContext.OutputMerger.DepthStencilState     = _state;
        }
        internal void PlatformApplyState(GraphicsDevice device)
        {
            if (_state == null)
            {
                // We're now bound to a device... no one should
                // be changing the state of this object now!
                GraphicsDevice = device;

                // Build the description.
                var desc = new SharpDX.Direct3D11.DepthStencilStateDescription();

                desc.IsDepthEnabled = DepthBufferEnable;
                desc.DepthComparison = GetComparison(DepthBufferFunction);

                if (DepthBufferWriteEnable)
                    desc.DepthWriteMask = SharpDX.Direct3D11.DepthWriteMask.All;
                else
                    desc.DepthWriteMask = SharpDX.Direct3D11.DepthWriteMask.Zero;

                desc.IsStencilEnabled = StencilEnable;
                desc.StencilReadMask = (byte)StencilMask; // TODO: Should this instead grab the upper 8bits?
                desc.StencilWriteMask = (byte)StencilWriteMask;

                if (TwoSidedStencilMode)
                {
                    desc.BackFace.Comparison = GetComparison(CounterClockwiseStencilFunction);
                    desc.BackFace.DepthFailOperation = GetStencilOp(CounterClockwiseStencilDepthBufferFail);
                    desc.BackFace.FailOperation = GetStencilOp(CounterClockwiseStencilFail);
                    desc.BackFace.PassOperation = GetStencilOp(CounterClockwiseStencilPass);
                }
                else
                {   //use same settings as frontFace 
                    desc.BackFace.Comparison = GetComparison(StencilFunction);
                    desc.BackFace.DepthFailOperation = GetStencilOp(StencilDepthBufferFail);
                    desc.BackFace.FailOperation = GetStencilOp(StencilFail);
                    desc.BackFace.PassOperation = GetStencilOp(StencilPass);
                }

                desc.FrontFace.Comparison = GetComparison(StencilFunction);
                desc.FrontFace.DepthFailOperation = GetStencilOp(StencilDepthBufferFail);
                desc.FrontFace.FailOperation = GetStencilOp(StencilFail);
                desc.FrontFace.PassOperation = GetStencilOp(StencilPass);

                // Create the state.
                _state = new SharpDX.Direct3D11.DepthStencilState(GraphicsDevice._d3dDevice, desc);
            }

            Debug.Assert(GraphicsDevice == device, "The state was created for a different device!");

            // NOTE: We make the assumption here that the caller has
            // locked the d3dContext for us to use.

            // Apply the state!
            device._d3dContext.OutputMerger.SetDepthStencilState(_state, ReferenceStencil);
        }
Example #3
0
        private void _スワップチェーンに依存するグラフィックリソースを作成する()
        {
            using (var backbufferTexture2D = this.SwapChain.GetBackBuffer <SharpDX.Direct3D11.Texture2D>(0))   // D3D 用
                using (var backbufferSurface = this.SwapChain.GetBackBuffer <SharpDX.DXGI.Surface>(0))         // D2D 用
                {
                    // ※正確には、「スワップチェーン」というより、「スワップチェーンが持つバックバッファ」に依存するリソース。

                    // D3D 関連

                    #region " バックバッファに対するD3Dレンダーターゲットビューを作成する。"
                    //----------------
                    this.D3DRenderTargetView = new SharpDX.Direct3D11.RenderTargetView(this.D3DDevice, backbufferTexture2D);
                    //----------------
                    #endregion

                    #region " バックバッファに対する深度ステンシル、深度ステンシルビュー、深度ステンシルステートを作成する。"
                    //----------------
                    var depthStencilDesc = new SharpDX.Direct3D11.Texture2DDescription()
                    {
                        Width             = backbufferTexture2D.Description.Width,
                        Height            = backbufferTexture2D.Description.Height,
                        MipLevels         = 1,
                        ArraySize         = 1,
                        Format            = SharpDX.DXGI.Format.D32_Float, // Depthのみのフォーマット
                        SampleDescription = backbufferTexture2D.Description.SampleDescription,
                        Usage             = SharpDX.Direct3D11.ResourceUsage.Default,
                        BindFlags         = SharpDX.Direct3D11.BindFlags.DepthStencil,
                        CpuAccessFlags    = SharpDX.Direct3D11.CpuAccessFlags.None, // CPUからはアクセスしない
                        OptionFlags       = SharpDX.Direct3D11.ResourceOptionFlags.None,
                    };
                    this.D3DDepthStencil = new SharpDX.Direct3D11.Texture2D(this.D3DDevice, depthStencilDesc);

                    var depthStencilViewDesc = new SharpDX.Direct3D11.DepthStencilViewDescription()
                    {
                        Format    = depthStencilDesc.Format,
                        Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D,
                        Flags     = SharpDX.Direct3D11.DepthStencilViewFlags.None,
                        Texture2D = new SharpDX.Direct3D11.DepthStencilViewDescription.Texture2DResource()
                        {
                            MipSlice = 0,
                        },
                    };
                    this.D3DDepthStencilView = new SharpDX.Direct3D11.DepthStencilView(this.D3DDevice, this.D3DDepthStencil, depthStencilViewDesc);

                    var depthSencilStateDesc = new SharpDX.Direct3D11.DepthStencilStateDescription()
                    {
                        IsDepthEnabled   = this._深度ステンシルを使う,                      // 深度テストあり?
                        DepthWriteMask   = SharpDX.Direct3D11.DepthWriteMask.All, // 書き込む
                        DepthComparison  = SharpDX.Direct3D11.Comparison.Less,    // 手前の物体を描画
                        IsStencilEnabled = false,                                 // ステンシルテストなし。
                        StencilReadMask  = 0,                                     // ステンシル読み込みマスク。
                        StencilWriteMask = 0,                                     // ステンシル書き込みマスク。
                                                                                  // 面が表を向いている場合のステンシル・テストの設定
                        FrontFace = new SharpDX.Direct3D11.DepthStencilOperationDescription()
                        {
                            FailOperation      = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
                            DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
                            PassOperation      = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
                            Comparison         = SharpDX.Direct3D11.Comparison.Never,      // 常に失敗
                        },
                        // 面が裏を向いている場合のステンシル・テストの設定
                        BackFace = new SharpDX.Direct3D11.DepthStencilOperationDescription()
                        {
                            FailOperation      = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
                            DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
                            PassOperation      = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
                            Comparison         = SharpDX.Direct3D11.Comparison.Always,     // 常に成功
                        },
                    };
                    this.D3DDepthStencilState = new SharpDX.Direct3D11.DepthStencilState(this.D3DDevice, depthSencilStateDesc);
                    //----------------
                    #endregion

                    #region " バックバッファに対するビューポートを作成する。"
                    //----------------
                    this.D3DViewPort[0] = new SharpDX.Mathematics.Interop.RawViewportF()
                    {
                        X        = 0.0f,
                        Y        = 0.0f,
                        Width    = (float)backbufferTexture2D.Description.Width,
                        Height   = (float)backbufferTexture2D.Description.Height,
                        MinDepth = 0.0f,
                        MaxDepth = 1.0f,
                    };
                    //----------------
                    #endregion

                    // D2D 関連

                    #region " バックバッファとメモリを共有する、既定のD2Dレンダーターゲットビットマップを作成する。"
                    //----------------
                    this.D2DRenderBitmap = new SharpDX.Direct2D1.Bitmap1( // このビットマップは、
                        this.D2DDeviceContext,
                        backbufferSurface,                                // このDXGIサーフェスとメモリを共有する。
                        new SharpDX.Direct2D1.BitmapProperties1()
                    {
                        PixelFormat   = new SharpDX.Direct2D1.PixelFormat(backbufferSurface.Description.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                        BitmapOptions = SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw,
                    });

                    this.D2DDeviceContext.Target = this.D2DRenderBitmap;
                    //----------------
                    #endregion
                }

            if (null == this.UIFramework)
            {
                this.UIFramework = new UI.Framework();
            }
            this.UIFramework.活性化する();
            this.UIFramework.Root.可視 = false;
        }