Exemplo n.º 1
0
        /// <summary>
        /// Function to build the D3D11 blend state.
        /// </summary>
        /// <param name="device">The device used to create the blend state.</param>
        internal void BuildD3D11BlendState(D3D11.Device5 device)
        {
            Debug.Assert(D3DBlendState == null, "D3D Blend state already assigned to this pipeline state.");

            (int start, int count) = RwBlendStates.GetDirtyItems();
            var desc = new D3D11.BlendStateDescription1
            {
                AlphaToCoverageEnable  = IsAlphaToCoverageEnabled,
                IndependentBlendEnable = IsIndependentBlendingEnabled
            };

            for (int i = 0; i < count; ++i)
            {
                GorgonBlendState state = RwBlendStates[start + i];

                if (state == null)
                {
                    continue;
                }

                desc.RenderTarget[i] = new D3D11.RenderTargetBlendDescription1
                {
                    AlphaBlendOperation     = (D3D11.BlendOperation)state.AlphaBlendOperation,
                    BlendOperation          = (D3D11.BlendOperation)state.ColorBlendOperation,
                    IsLogicOperationEnabled = state.LogicOperation != LogicOperation.Noop,
                    IsBlendEnabled          = state.IsBlendingEnabled,
                    RenderTargetWriteMask   = (D3D11.ColorWriteMaskFlags)state.WriteMask,
                    LogicOperation          = (D3D11.LogicOperation)state.LogicOperation,
                    SourceAlphaBlend        = (D3D11.BlendOption)state.SourceAlphaBlend,
                    SourceBlend             = (D3D11.BlendOption)state.SourceColorBlend,
                    DestinationAlphaBlend   = (D3D11.BlendOption)state.DestinationAlphaBlend,
                    DestinationBlend        = (D3D11.BlendOption)state.DestinationColorBlend
                };
            }

            for (int i = count; i < D3D11.OutputMergerStage.SimultaneousRenderTargetCount; ++i)
            {
                desc.RenderTarget[i] = new D3D11.RenderTargetBlendDescription1
                {
                    AlphaBlendOperation     = D3D11.BlendOperation.Add,
                    BlendOperation          = D3D11.BlendOperation.Add,
                    IsLogicOperationEnabled = false,
                    IsBlendEnabled          = false,
                    RenderTargetWriteMask   = D3D11.ColorWriteMaskFlags.All,
                    LogicOperation          = D3D11.LogicOperation.Noop,
                    SourceAlphaBlend        = D3D11.BlendOption.One,
                    SourceBlend             = D3D11.BlendOption.One,
                    DestinationAlphaBlend   = D3D11.BlendOption.Zero,
                    DestinationBlend        = D3D11.BlendOption.Zero
                };
            }

            D3DBlendState = new D3D11.BlendState1(device, desc)
            {
                DebugName = nameof(GorgonBlendState)
            };
        }
Exemplo n.º 2
0
            public void Unload()
            {

                PixelShader.Dispose();
                VertexShader.Dispose();
                TextureView.Dispose();
                Sampler.Dispose();
                BlendState.Dispose();
                Texture2D.Dispose();
                VertexBufferBinding.Buffer.Dispose();
                VertexBuffer.Dispose();
                Layout.Dispose();
                ConstantBuffer.Dispose();



                this.PixelShader = null;
                this.VertexShader = null;
                this.TextureView = null;
                this.Sampler = null;
                this.BlendState = null;
                this.Texture2D = null;
                this.VertexBufferBinding.Buffer = null;
                this.VertexBuffer = null;
                this.Layout = null;
                this.ConstantBuffer = null;

            }
        public void Initialize(Device1 d3dDevice, DeviceContext1 d3dContext, int capacity = 1024)
        {

            m_d3dDevice = d3dDevice;

            m_d3dContext = d3dContext;



            m_capacity = capacity;





            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;



            var vertexShaderByteCode = NativeFile.ReadAllBytes(path + "\\Assets\\SpriteBatch.vs.cso");

            m_vertexShader = new VertexShader(m_d3dDevice, vertexShaderByteCode);

            m_pixelShader = new PixelShader(d3dDevice, NativeFile.ReadAllBytes(path + "\\Assets\\SpriteBatch.ps.cso"));



            // Layout from VertexShader input signature
            m_layout = new InputLayout(d3dDevice, vertexShaderByteCode, new[]
                    {

                        new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32A32_Float,0,0), 
                        new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, 16, 0),
                        new InputElement("COLOR", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 24, 0),

                    });



            SamplerStateDescription samplerDesc = SharpDX.Direct3D11.SamplerStateDescription.Default();
            m_sampler = new SamplerState(d3dDevice, samplerDesc);





            //BlendStateDescription1 blendDesc = new BlendStateDescription1();
            //blendDesc.AlphaToCoverageEnable = true;  //set to true to get nice blending betweent sprites
            //blendDesc.IndependentBlendEnable = false;
            //blendDesc.RenderTarget[0].IsBlendEnabled = true;
            //blendDesc.RenderTarget[0].IsLogicOperationEnabled = false;
            //blendDesc.RenderTarget[0].SourceBlend = BlendOption.SourceColor;
            //blendDesc.RenderTarget[0].DestinationBlend = BlendOption.SourceAlphaSaturate;
            //blendDesc.RenderTarget[0].BlendOperation = BlendOperation.Add;
            //blendDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
            //blendDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.One
            //blendDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Maximum; // set to maximum to blend 2 sprites nicely over each other
            //blendDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            //m_blendStateAlpha = new BlendState1(d3dDevice, blendDesc);




            var description = BlendStateDescription1.Default();
            description.RenderTarget[0].IsBlendEnabled = true;
            description.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
            description.RenderTarget[0].DestinationBlend = BlendOption.One;
            description.RenderTarget[0].SourceAlphaBlend = BlendOption.SourceAlpha;
            description.RenderTarget[0].DestinationAlphaBlend = BlendOption.One;
            description.RenderTarget[0].BlendOperation = BlendOperation.Add;
            description.RenderTarget[0].IsLogicOperationEnabled = false;
            description.RenderTarget[0].AlphaBlendOperation = BlendOperation.Maximum;
            description.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            description.AlphaToCoverageEnable = true;  //<==RT DOES NOT WORK
            description.IndependentBlendEnable = false;
            m_blendStateAlpha = new BlendState1(d3dDevice, description);




            //[BELOW] Windows RT this does not work
            //var description = BlendStateDescription1.Default();
            //description.RenderTarget[0].IsBlendEnabled = true;
            //description.RenderTarget[0].SourceBlend = BlendOption.SourceColor;
            //description.RenderTarget[0].DestinationBlend = BlendOption.SourceAlphaSaturate;
            //description.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
            //description.RenderTarget[0].DestinationAlphaBlend = BlendOption.One;
            //description.RenderTarget[0].BlendOperation = BlendOperation.Add;
            //description.RenderTarget[0].IsLogicOperationEnabled = false;
            //description.RenderTarget[0].AlphaBlendOperation = BlendOperation.Maximum;
            //description.AlphaToCoverageEnable = true; 
            //description.IndependentBlendEnable = false;
            //description.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

            //m_blendStateAlpha = new BlendState1(d3dDevice, description);









            m_constantBufferVS = ToDispose(new SharpDX.Direct3D11.Buffer(d3dDevice, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));
            m_constantBufferPS = ToDispose(new SharpDX.Direct3D11.Buffer(d3dDevice, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));





            //=======================

            // Setup the pipeline

            //=======================

            m_vertices = ToDispose(BuildVerticesBuffer(d3dDevice, 1.0f, new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1)));

            m_vertexBufferBinding = new VertexBufferBinding(m_vertices, sizeof(float) * 10, 0);

            d3dContext.InputAssembler.SetVertexBuffers(0, m_vertexBufferBinding);



            d3dContext.InputAssembler.InputLayout = m_layout;

            d3dContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;



            d3dContext.VertexShader.SetConstantBuffer(0, m_constantBufferVS);

            d3dContext.VertexShader.Set(m_vertexShader);



            d3dContext.PixelShader.SetConstantBuffer(0, m_constantBufferPS);

            d3dContext.PixelShader.SetSampler(0, m_sampler);

            d3dContext.PixelShader.Set(m_pixelShader);



            d3dContext.OutputMerger.BlendState = m_blendStateAlpha; // m_blendStateAlpha, m_blendStateAdditive;



        }