Esempio n. 1
0
        protected void CreateDepthStencilAndBlendState()
        {
            var dssd = new DepthStencilStateDescription()
            {
                IsDepthEnabled = false,
                DepthWriteMask = DepthWriteMask.Zero
            };

            DepthStencilState = DepthStencilState.FromDescription(Device, dssd);

            var blendDesc = new BlendStateDescription {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false
            };

            blendDesc.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            blendDesc.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            blendDesc.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
            blendDesc.RenderTargets[0].BlendEnable           = true;
            blendDesc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            blendDesc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            blendDesc.RenderTargets[0].SourceBlendAlpha      = BlendOption.SourceAlpha;
            blendDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.InverseSourceAlpha;
            BlendState = BlendState.FromDescription(Device, blendDesc);
        }
Esempio n. 2
0
        public static void Initialize()
        {
            spriteDrawer = new Sprite(Game.Device, 0);
            instances    = new Dictionary <string, CustomSpriteInstance>();
            coordiantes  = new Vector2(0.0f, 0.0f);
            dimensions   = new Vector2(1.0f, 1.0f);

            RasterizerStateDescription rsd = new RasterizerStateDescription();

            rsd.CullMode = CullMode.None;
            rsd.FillMode = FillMode.Solid;
            rs           = RasterizerState.FromDescription(Game.Device, rsd);

            BlendStateDescription bsd = new BlendStateDescription();

            bsd.SetBlendEnable(0, true);
            bsd.BlendOperation        = BlendOperation.Add;
            bsd.SourceBlend           = BlendOption.SourceAlpha;
            bsd.DestinationBlend      = BlendOption.InverseSourceAlpha;
            bsd.SourceAlphaBlend      = BlendOption.SourceAlpha;
            bsd.DestinationAlphaBlend = BlendOption.InverseSourceAlpha;
            bsd.AlphaBlendOperation   = BlendOperation.Add;
            bsAlphaBlend = BlendState.FromDescription(Game.Device, bsd);

            bsd = new BlendStateDescription();
            bsd.SetBlendEnable(0, true);
            bsd.BlendOperation        = BlendOperation.Add;
            bsd.SourceBlend           = BlendOption.One;
            bsd.DestinationBlend      = BlendOption.One;
            bsd.SourceAlphaBlend      = BlendOption.One;
            bsd.DestinationAlphaBlend = BlendOption.One;
            bsd.AlphaBlendOperation   = BlendOperation.Add;
            bsAdditive = BlendState.FromDescription(Game.Device, bsd);
        }
Esempio n. 3
0
        private void CreateStates()
        {
            var blendStateDesc = new BlendStateDescription();

            blendStateDesc.BlendOperation           = BlendOperation.Add;
            blendStateDesc.AlphaBlendOperation      = BlendOperation.Add;
            blendStateDesc.SourceBlend              = BlendOption.One;
            blendStateDesc.DestinationBlend         = BlendOption.One;
            blendStateDesc.SourceAlphaBlend         = BlendOption.One;
            blendStateDesc.DestinationAlphaBlend    = BlendOption.Zero;
            blendStateDesc.IsAlphaToCoverageEnabled = false;
            blendStateDesc.SetBlendEnable(0, true);
            visualEffectsBlendState         = BlendState.FromDescription(renderer.Device, blendStateDesc);
            blendStateDesc.DestinationBlend = BlendOption.Zero;
            blenderBlendState = BlendState.FromDescription(renderer.Device, blendStateDesc);

            var rasterizerStateDesc = new RasterizerStateDescription();

            rasterizerStateDesc.CullMode = CullMode.None;
            rasterizerStateDesc.FillMode = FillMode.Solid;
            rasterizerStateDesc.IsAntialiasedLineEnabled = false;
            rasterizerStateDesc.IsMultisampleEnabled     = false;
            // TODO: probably use scissor test
            rasterizerStateDesc.IsScissorEnabled = false;
            rasterizerState = RasterizerState.FromDescription(renderer.Device, rasterizerStateDesc);

            var depthStencilStateDesc = new DepthStencilStateDescription();

            depthStencilStateDesc.IsDepthEnabled   = false;
            depthStencilStateDesc.IsStencilEnabled = false;
            depthStencilState = DepthStencilState.FromDescription(renderer.Device, depthStencilStateDesc);
        }
Esempio n. 4
0
        void DxInitStuff()
        {
            _effectTest   = new EffectTest(CurrentDevice);
            _effectScreen = new EffectScreen(CurrentDevice);

            _squareBuffers = new SquareBuffers(CurrentDevice);

            _wireframeRs = RasterizerState.FromDescription(CurrentDevice, new RasterizerStateDescription {
                FillMode = FillMode.Wireframe,
                CullMode = CullMode.Back,
                IsFrontCounterclockwise = false,
                IsDepthClipEnabled      = true
            });

            var transDesc = new BlendStateDescription {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false
            };

            transDesc.RenderTargets[0].BlendEnable           = true;
            transDesc.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
            transDesc.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            transDesc.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            transDesc.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            transDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.One;
            transDesc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            transDesc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

            _transparentBlendState = BlendState.FromDescription(CurrentDevice, transDesc);
        }
Esempio n. 5
0
        private static void InitializeBlending()
        {
            BlendStateDescription bs = new BlendStateDescription()
            {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false,
            };

            for (int i = 0; i < bs.RenderTargets.Length; i++)
            {
                bs.RenderTargets[i].BlendEnable      = true;
                bs.RenderTargets[i].BlendOperation   = BlendOperation.Add;
                bs.RenderTargets[i].SourceBlend      = BlendOption.SourceAlpha;
                bs.RenderTargets[i].DestinationBlend = BlendOption.InverseSourceAlpha;


                bs.RenderTargets[i].BlendOperationAlpha   = BlendOperation.Add;
                bs.RenderTargets[i].SourceBlendAlpha      = BlendOption.Zero;
                bs.RenderTargets[i].DestinationBlendAlpha = BlendOption.Zero;



                bs.RenderTargets[i].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            }
            BlendState = BlendState.FromDescription(Device, bs);
        }
Esempio n. 6
0
        MeshObject CreateTexturedPlace(Matrix transform, ShaderResourceView texture, bool shadowObj = false)
        {
            var obj = shadowObj ? new MeshObjectShadow() : new MeshObject();

            obj.Transform    = transform;
            obj.InvTransform = Matrix.Invert(Matrix.Transpose(obj.Transform));

            var vertices = new VerticePT[4];

            for (var i = 0; i < vertices.Length; i++)
            {
                vertices[i] = new VerticePT(
                    new Vector3(i < 2 ? 1 : -1, 0, i % 2 == 0 ? -1 : 1),
                    new Vector2(i < 2 ? 1 : 0, i % 2)
                    );
            }

            var vbd = new BufferDescription(VerticePT.Stride * vertices.Length, ResourceUsage.Immutable,
                                            BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            obj.Vb = new Buffer(CurrentDevice, new DataStream(vertices, false, false), vbd);

            var indices = new ushort[] { 0, 2, 1, 3, 1, 2 };

            obj.IndCount = indices.Length;
            var ibd = new BufferDescription(sizeof(ushort) * obj.IndCount, ResourceUsage.Immutable,
                                            BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            obj.Ib = new Buffer(CurrentDevice, new DataStream(indices, false, false), ibd);

            obj.Mat  = new ShaderMaterial();
            obj.Rast = null;

            obj.Tex = texture;
            _objs.Add(obj);

            if (shadowObj)
            {
                var transDesc = new BlendStateDescription {
                    AlphaToCoverageEnable  = false,
                    IndependentBlendEnable = false
                };

                transDesc.RenderTargets[0].BlendEnable           = true;
                transDesc.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
                transDesc.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
                transDesc.RenderTargets[0].BlendOperation        = BlendOperation.ReverseSubtract;
                transDesc.RenderTargets[0].SourceBlendAlpha      = BlendOption.Zero;
                transDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.One;
                transDesc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
                transDesc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

                obj.Blen = BlendState.FromDescription(CurrentDevice, transDesc);
            }

            return(obj);
        }
Esempio n. 7
0
        public static BlendState CreateBlendState(this Device device, RenderTargetBlendDescription description)
        {
            var desc = new BlendStateDescription {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false
            };

            desc.RenderTargets[0] = description;
            return(BlendState.FromDescription(device, desc));
        }
Esempio n. 8
0
        /// <summary>
        /// Dessine cette cellule.
        /// </summary>
        public void Draw()
        {
            // Dessine la cellule.
            var device = Scene.GetGraphicsDevice().ImmediateContext;

            device.InputAssembler.SetIndexBuffer(Generation.ModelGenerator.GetIndexBuffer(GridResolution), Format.R32_UInt, 0);
            device.InputAssembler.PrimitiveTopology = (PrimitiveTopology.TriangleList);
            device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_vBuffer, Graphics.VertexPositionTextureNormal.Vertex.Stride, 0));
            RasterizerStateDescription rsd = new RasterizerStateDescription()
            {
                CullMode                 = CullMode.Front,
                DepthBias                = 0,
                DepthBiasClamp           = 0.0f,
                FillMode                 = FillMode.Solid,
                IsAntialiasedLineEnabled = true,
                IsDepthClipEnabled       = true,
                IsFrontCounterclockwise  = false,
                IsMultisampleEnabled     = false,
                IsScissorEnabled         = false,
                SlopeScaledDepthBias     = 0.0f
            };

            // Alpha blending
            var transDesc = new BlendStateDescription
            {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false
            };

            transDesc.RenderTargets[0].BlendEnable           = true;
            transDesc.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
            transDesc.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            transDesc.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            transDesc.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            transDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
            transDesc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            transDesc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            var bs = BlendState.FromDescription(device.Device, transDesc);

            device.OutputMerger.BlendState = bs;

            RasterizerState rs = RasterizerState.FromDescription(Scene.GetGraphicsDevice(), rsd);

            device.Rasterizer.State = rs;

            // Variables
            Graphics.AtmosphereEffect effect = Scene.GetGraphicsEngine().AtmosphereEffect;
            effect.Apply(Matrix.Identity, Scene.Instance.Camera.View, Scene.Instance.Camera.Projection, m_material);
            device.DrawIndexed(Generation.ModelGenerator.GetIndexBuffer(GridResolution).Description.SizeInBytes / sizeof(int), 0, 0);

            device.OutputMerger.BlendState = null;
        }
Esempio n. 9
0
        public CombineFinalRenderer(DX11Game game, GBuffer gBuffer)
        {
            this.game    = game;
            this.gBuffer = gBuffer;
            var device = game.Device;

            context = device.ImmediateContext;

            shader = BasicShader.LoadAutoreload(game,
                                                new System.IO.FileInfo(
                                                    CompiledShaderCache.Current.RootShaderPath + "Deferred\\CombineFinal.fx"));

            shader.SetTechnique("Technique0");

            quad = new FullScreenQuad(device);

            layout = FullScreenQuad.CreateInputLayout(device, shader.GetCurrentPass(0));

            var desc = new Texture2DDescription
            {
                BindFlags =
                    BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.R16G16B16A16_Float,
                Width             = gBuffer.Width,
                Height            = gBuffer.Height,
                ArraySize         = 1,
                SampleDescription = new SampleDescription(1, 0),
                MipLevels         = 1
            };

            lightAccumulationMap = new Texture2D(device, desc);

            lightAccumulationRTV = new RenderTargetView(device, lightAccumulationMap);
            LightAccumulationRV  = new ShaderResourceView(device, lightAccumulationMap);

            var bsDesc = new BlendStateDescription();
            var b      = new RenderTargetBlendDescription();

            b.BlendEnable           = true;
            b.BlendOperation        = BlendOperation.Add;
            b.BlendOperationAlpha   = BlendOperation.Add;
            b.DestinationBlend      = BlendOption.One;
            b.DestinationBlendAlpha = BlendOption.One;
            b.SourceBlend           = BlendOption.One;
            b.SourceBlendAlpha      = BlendOption.One;
            b.RenderTargetWriteMask = ColorWriteMaskFlags.All;
            bsDesc.RenderTargets[0] = b;


            blendState = BlendState.FromDescription(device, bsDesc);
        }
Esempio n. 10
0
        public void Apply(DX11RenderContext context)
        {
            DeviceContext ctx = context.CurrentDeviceContext;

            RasterizerState   rs = RasterizerState.FromDescription(context.Device, this.Rasterizer);
            DepthStencilState ds = DepthStencilState.FromDescription(context.Device, this.DepthStencil);
            BlendState        bs = BlendState.FromDescription(context.Device, this.Blend);

            ctx.Rasterizer.State = rs;
            ctx.OutputMerger.DepthStencilState     = ds;
            ctx.OutputMerger.DepthStencilReference = this.DepthStencilReference;
            ctx.OutputMerger.BlendState            = bs;
            ctx.OutputMerger.BlendFactor           = this.BlendFactor;
            ctx.OutputMerger.BlendSampleMask       = int.MaxValue;
        }
Esempio n. 11
0
        public static void Initialize(Device device)
        {
            {
                var blendStateDescription = new BlendStateDescription();
                blendStateDescription.RenderTargets[0].BlendEnable           = false;
                blendStateDescription.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
                blendStateDescription.RenderTargets[1].BlendEnable           = false;
                blendStateDescription.RenderTargets[1].RenderTargetWriteMask = ColorWriteMaskFlags.None;
                blendStateDescription.RenderTargets[2].BlendEnable           = false;
                blendStateDescription.RenderTargets[2].RenderTargetWriteMask = ColorWriteMaskFlags.None;
                blendStateDescription.RenderTargets[3].BlendEnable           = false;
                blendStateDescription.RenderTargets[3].RenderTargetWriteMask = ColorWriteMaskFlags.None;

                m_BlendStates[(int)BlendType.None] = BlendState.FromDescription(device, blendStateDescription);

                blendStateDescription.RenderTargets[0].BlendEnable           = true;
                blendStateDescription.RenderTargets[0].BlendOperation        = BlendOperation.Add;
                blendStateDescription.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
                blendStateDescription.RenderTargets[0].DestinationBlend      = BlendOption.One;
                blendStateDescription.RenderTargets[0].DestinationBlendAlpha = BlendOption.One;
                blendStateDescription.RenderTargets[0].SourceBlend           = BlendOption.One;
                blendStateDescription.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
                blendStateDescription.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

                m_BlendStates[(int)BlendType.Additive] = BlendState.FromDescription(device, blendStateDescription);
            }

            {
                var depthStencilStateDescription = new DepthStencilStateDescription();
                depthStencilStateDescription.DepthComparison  = Comparison.Always;
                depthStencilStateDescription.DepthWriteMask   = DepthWriteMask.Zero;
                depthStencilStateDescription.IsDepthEnabled   = false;
                depthStencilStateDescription.IsStencilEnabled = false;

                m_DepthStencilStates[(int)DepthConfigurationType.NoDepth] = DepthStencilState.FromDescription(device, depthStencilStateDescription);

                depthStencilStateDescription.DepthComparison = Comparison.LessEqual;
                depthStencilStateDescription.DepthWriteMask  = DepthWriteMask.All;
                depthStencilStateDescription.IsDepthEnabled  = true;

                m_DepthStencilStates[(int)DepthConfigurationType.DepthWriteCompare] = DepthStencilState.FromDescription(device, depthStencilStateDescription);

                depthStencilStateDescription.DepthWriteMask = DepthWriteMask.Zero;

                m_DepthStencilStates[(int)DepthConfigurationType.DepthCompare] = DepthStencilState.FromDescription(device, depthStencilStateDescription);
            }
        }
Esempio n. 12
0
        public override bool Init()
        {
            if (!base.Init())
            {
                return(false);
            }

            Effects.InitAll(Device);
            InputLayouts.InitAll(Device);
            RenderStates.InitAll(Device);

            _texMgr = new TextureManager();
            _texMgr.Init(Device);

            _bunnyModel = BasicModel.LoadSdkMesh(Device, _texMgr, "Models/bunny.sdkmesh", "Textures");

            _bunnyInstance = new BasicModelInstance(_bunnyModel);

            _effect = new FowardLightingEffect(Device, "FX/ForwardLight.fxo");

            var blendDesc = new BlendStateDescription {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false
            };

            blendDesc.RenderTargets[0].BlendEnable           = true;
            blendDesc.RenderTargets[0].SourceBlend           = BlendOption.One;
            blendDesc.RenderTargets[0].DestinationBlend      = BlendOption.One;
            blendDesc.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            blendDesc.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            blendDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.One;
            blendDesc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            blendDesc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

            _additiveBlend = BlendState.FromDescription(Device, blendDesc);


            var passDesc = _effect.Ambient.GetPassByIndex(0).Description;

            _layout = new InputLayout(Device, passDesc.Signature, InputLayoutDescriptions.PosNormalTexTan);

            AddUIElements();

            return(true);
        }
Esempio n. 13
0
        public Emitter(TexturePool texturePool, DX11Game game, EmitterParameters parameters, int width, int height)
        {
            this.texturePool = texturePool;

            //this.declarationPool = declarationPool;
            this.game = game;
            context   = game.Device.ImmediateContext;

            this.parameters = parameters;
            this.width      = width;
            this.height     = height;
            simulater       = new ParticleSimulater(game, parameters.size, parameters.EffectName);



            var blendStateDescription = new BlendStateDescription();

            blendStateDescription.RenderTargets[0].BlendEnable           = true;
            blendStateDescription.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            blendStateDescription.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            blendStateDescription.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            blendStateDescription.RenderTargets[0].SourceBlend           = BlendOption.One;
            blendStateDescription.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            blendStateDescription.RenderTargets[0].DestinationBlend      = BlendOption.One;
            blendStateDescription.RenderTargets[0].DestinationBlendAlpha = BlendOption.InverseSourceAlpha;

            additiveBlendState = BlendState.FromDescription(game.Device, blendStateDescription);
            blendStateDescription.RenderTargets[0].BlendEnable           = true;
            blendStateDescription.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            blendStateDescription.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            blendStateDescription.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            blendStateDescription.RenderTargets[0].SourceBlend           = BlendOption.One;
            blendStateDescription.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            blendStateDescription.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            blendStateDescription.RenderTargets[0].DestinationBlendAlpha = BlendOption.InverseSourceAlpha;

            normalBlendState = BlendState.FromDescription(game.Device, blendStateDescription);

            depthStencilState = DepthStencilState.FromDescription(game.Device, new DepthStencilStateDescription
            {
                IsDepthEnabled  = true,
                DepthWriteMask  = DepthWriteMask.Zero,
                DepthComparison = Comparison.LessEqual
            });
        }
Esempio n. 14
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);
        }
Esempio n. 15
0
        public D2DSpriteBatch(RenderContext context)
        {
            this.context           = context;
            this.DeviceManager     = context.DeviceManager;
            this.SpriteEffect      = CGHelper.CreateEffectFx5FromResource("MMF.Resource.Shader.SpriteShader.fx", this.DeviceManager.Device);
            this.renderPass        = this.SpriteEffect.GetTechniqueByIndex(0).GetPassByIndex(1);
            this.VertexInputLayout = new InputLayout(this.DeviceManager.Device, this.SpriteEffect.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature, SpriteVertexLayout.InputElements);
            SamplerDescription desc = new SamplerDescription();

            desc.Filter   = Filter.MinMagMipLinear;
            desc.AddressU = TextureAddressMode.Wrap;
            desc.AddressV = TextureAddressMode.Wrap;
            desc.AddressW = TextureAddressMode.Wrap;
            this.sampler  = SamplerState.FromDescription(this.DeviceManager.Device, desc);
            BlendStateDescription blendDesc = new BlendStateDescription();

            blendDesc.AlphaToCoverageEnable  = false;
            blendDesc.IndependentBlendEnable = false;
            for (int i = 0; i < blendDesc.RenderTargets.Length; i++)
            {
                blendDesc.RenderTargets[i].BlendEnable           = true;
                blendDesc.RenderTargets[i].SourceBlend           = BlendOption.SourceAlpha;
                blendDesc.RenderTargets[i].DestinationBlend      = BlendOption.InverseSourceAlpha;
                blendDesc.RenderTargets[i].BlendOperation        = BlendOperation.Add;
                blendDesc.RenderTargets[i].SourceBlendAlpha      = BlendOption.One;
                blendDesc.RenderTargets[i].DestinationBlendAlpha = BlendOption.Zero;
                blendDesc.RenderTargets[i].BlendOperationAlpha   = BlendOperation.Add;
                blendDesc.RenderTargets[i].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            }
            this.state = BlendState.FromDescription(this.DeviceManager.Device, blendDesc);

            BlendStateDescription bsd = new BlendStateDescription();

            bsd.RenderTargets[0].BlendEnable           = true;
            bsd.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
            bsd.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            bsd.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            bsd.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            bsd.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
            bsd.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            bsd.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            this.TransParentBlendState = BlendState.FromDescription(this.DeviceManager.Device, bsd);
            Resize();
        }
Esempio n. 16
0
            public Cursor(Device device, InputLayout lineLayout, EffectTechnique _tech)
            {
                this.device     = device;
                this.lineLayout = lineLayout;
                this._pass      = _tech.GetPassByIndex(0);

                var transDesc = new BlendStateDescription
                {
                    AlphaToCoverageEnable  = false,
                    IndependentBlendEnable = false
                };

                transDesc.RenderTargets[0].BlendEnable           = true;
                transDesc.RenderTargets[0].SourceBlend           = BlendOption.InverseDestinationColor;
                transDesc.RenderTargets[0].DestinationBlend      = BlendOption.DestinationColor;
                transDesc.RenderTargets[0].BlendOperation        = BlendOperation.Add;
                transDesc.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
                transDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
                transDesc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
                transDesc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
                cursorBS = BlendState.FromDescription(device, transDesc);

                PositionColored[] cursorGeometry = new PositionColored[]
                {
                    new PositionColored(new Vector3(-1, 0, 0), Color.SkyBlue),
                    new PositionColored(new Vector3(1, 0, 0), Color.SkyBlue),

                    new PositionColored(new Vector3(0, -1, 0), Color.SkyBlue),
                    new PositionColored(new Vector3(0, 1, 0), Color.SkyBlue),

                    new PositionColored(new Vector3(0, 0, -1), Color.SkyBlue),
                    new PositionColored(new Vector3(0, 0, 1), Color.SkyBlue)
                };
                device.ImmediateContext.InputAssembler.InputLayout       = lineLayout;
                device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
                BufferDescription vbd = new BufferDescription(PositionColored.Stride * cursorGeometry.Length, ResourceUsage.Immutable, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

                uint[]            indices            = new uint[] { 0, 1, 2, 3, 4, 5 };
                BufferDescription ibd                = new BufferDescription(sizeof(uint) * indices.Length, ResourceUsage.Immutable, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
                Buffer            cursorVertexBuffer = new Buffer(device, new DataStream(cursorGeometry, true, false), vbd);

                vertexBufferBinding = new VertexBufferBinding(cursorVertexBuffer, PositionColored.Stride, 0);
                IndexBuffer         = new Buffer(device, new DataStream(indices, false, false), ibd);
            }
Esempio n. 17
0
        public void SetDefaultstate()
        {
            // shader pipeline
            m_D3dDevice.ImmediateContext.InputAssembler.InputLayout       = null;
            m_D3dDevice.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            m_D3dDevice.ImmediateContext.VertexShader.Set(null);
            m_D3dDevice.ImmediateContext.HullShader.Set(null);
            m_D3dDevice.ImmediateContext.DomainShader.Set(null);
            m_D3dDevice.ImmediateContext.GeometryShader.Set(null);
            m_D3dDevice.ImmediateContext.PixelShader.Set(null);

            // render state
            m_D3dDevice.ImmediateContext.Rasterizer.State = RasterizerState.FromDescription(m_D3dDevice, new RasterizerStateDescription()
            {
                CullMode = CullMode.Back,
                FillMode = FillMode.Solid,
            });

            m_D3dDevice.ImmediateContext.OutputMerger.DepthStencilState = DepthStencilState.FromDescription(m_D3dDevice, new DepthStencilStateDescription()
            {
                DepthComparison  = Comparison.Less,
                DepthWriteMask   = DepthWriteMask.All,
                IsDepthEnabled   = true,
                IsStencilEnabled = false
            });

            var blendState = new RenderTargetBlendDescription()
            {
                BlendEnable           = false,
                BlendOperation        = BlendOperation.Add,
                DestinationBlend      = BlendOption.InverseSourceAlpha,
                SourceBlend           = BlendOption.SourceAlpha,
                RenderTargetWriteMask = ColorWriteMaskFlags.All
            };

            var blendStateDesc = new BlendStateDescription();

            blendStateDesc.AlphaToCoverageEnable  = false;
            blendStateDesc.IndependentBlendEnable = false;
            blendStateDesc.RenderTargets[0]       = blendState;

            m_D3dDevice.ImmediateContext.OutputMerger.BlendState = BlendState.FromDescription(m_D3dDevice, blendStateDesc);
        }
Esempio n. 18
0
        protected override void OnResourceLoad()
        {
            CreatePrimaryRenderTarget();
            CreateDepthBuffer();

            var dssd = new DepthStencilStateDescription {
                IsDepthEnabled   = true,
                IsStencilEnabled = false,
                DepthWriteMask   = DepthWriteMask.All,
                DepthComparison  = Comparison.Less
            };

            var solidParentOp = new BlendStateDescription();

            solidParentOp.SetBlendEnable(0, false);
            solidParentOp.SetWriteMask(0, ColorWriteMaskFlags.All);

            var transParentOp = new BlendStateDescription {
                AlphaBlendOperation      = BlendOperation.Add,
                BlendOperation           = BlendOperation.Add,
                DestinationAlphaBlend    = BlendOption.Zero,
                DestinationBlend         = BlendOption.One,
                IsAlphaToCoverageEnabled = false,
                SourceAlphaBlend         = BlendOption.Zero,
                SourceBlend = BlendOption.One,
            };

            transParentOp.SetBlendEnable(0, true);
            transParentOp.SetWriteMask(0, ColorWriteMaskFlags.All);

            transBlendState = BlendState.FromDescription(Context10.Device, transParentOp);
            solidBlendState = BlendState.FromDescription(Context10.Device, solidParentOp);

            depthStencilState = DepthStencilState.FromDescription(Context10.Device, dssd);

            jupiterMesh = new SimpleModel(Context10.Device, "SimpleModel10.fx", "jupiter.SMD", "jupiter.jpg");

            view = Matrix.LookAtLH(new Vector3(0, 160, 0), new Vector3(0, -128.0f, 0), -Vector3.UnitZ);
            jupiterMesh.Effect.GetVariableByName("view").AsMatrix().SetMatrix(view);

            proj = Matrix.PerspectiveFovLH(45.0f, WindowWidth / (float)WindowHeight, 1.0f, 1000.0f);
            jupiterMesh.Effect.GetVariableByName("proj").AsMatrix().SetMatrix(proj);
        }
Esempio n. 19
0
            public HelperStatesContainer(Device device)
            {
                this.device = device;

                RasterizerShowAll = RasterizerState.FromDescription(device, new RasterizerStateDescription
                {
                    CullMode = CullMode.None,
                    FillMode = FillMode.Solid
                });
                RasterizerWireframe = RasterizerState.FromDescription(device, new RasterizerStateDescription
                {
                    CullMode = CullMode.None,
                    FillMode = FillMode.Wireframe
                });


                var blendStateDescription = new BlendStateDescription();

                blendStateDescription.RenderTargets[0].BlendEnable           = true;
                blendStateDescription.RenderTargets[0].BlendOperation        = BlendOperation.Add;
                blendStateDescription.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
                blendStateDescription.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
                blendStateDescription.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
                blendStateDescription.RenderTargets[0].SourceBlendAlpha      = BlendOption.SourceAlpha;
                blendStateDescription.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
                blendStateDescription.RenderTargets[0].DestinationBlendAlpha = BlendOption.InverseSourceAlpha;

                AlphaBlend = BlendState.FromDescription(device, blendStateDescription);



                blendStateDescription.RenderTargets[0].BlendEnable           = true;
                blendStateDescription.RenderTargets[0].BlendOperation        = BlendOperation.Add;
                blendStateDescription.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
                blendStateDescription.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
                blendStateDescription.RenderTargets[0].SourceBlend           = BlendOption.One;
                blendStateDescription.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
                blendStateDescription.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
                blendStateDescription.RenderTargets[0].DestinationBlendAlpha = BlendOption.InverseSourceAlpha;

                AlphaBlendPremultiplied = BlendState.FromDescription(device, blendStateDescription);
            }
Esempio n. 20
0
        private void InitBlending()
        {
            BlendStateDescription statedescr = new BlendStateDescription();


            statedescr.SetBlendEnable(0, true);
            statedescr.SetWriteMask(0, SlimDX.Direct3D10.ColorWriteMaskFlags.All);

            statedescr.BlendOperation   = BlendOperation.Add;
            statedescr.DestinationBlend = SlimDX.Direct3D10.BlendOption.InverseSourceAlpha;
            statedescr.SourceBlend      = SlimDX.Direct3D10.BlendOption.SourceAlpha;

            statedescr.AlphaBlendOperation   = BlendOperation.Add;
            statedescr.DestinationAlphaBlend = SlimDX.Direct3D10.BlendOption.Zero;
            statedescr.SourceAlphaBlend      = SlimDX.Direct3D10.BlendOption.Zero;

            BlendState newstate = BlendState.FromDescription(device, statedescr);

            device.OutputMerger.BlendState = newstate;
        }
Esempio n. 21
0
        public BlendStateManager(Device device)
        {
            var blendStateDesc = new BlendStateDescription
            {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false
            };

            blendStateDesc.RenderTargets[0] = new RenderTargetBlendDescription
            {
                BlendEnable           = true,
                BlendOperation        = BlendOperation.Add,
                BlendOperationAlpha   = BlendOperation.Add,
                DestinationBlend      = BlendOption.InverseSourceAlpha,
                DestinationBlendAlpha = BlendOption.Zero,
                RenderTargetWriteMask = ColorWriteMaskFlags.All,
                SourceBlend           = BlendOption.SourceAlpha,
                SourceBlendAlpha      = BlendOption.One
            };
            BlendState = BlendState.FromDescription(device, blendStateDesc);
        }
Esempio n. 22
0
        protected virtual void generateBlendingStates()
        {
            BlendStateDescription defaultDesc = new BlendStateDescription()
            {
                AlphaToCoverageEnable = true, IndependentBlendEnable = true
            };

            defaultDesc.RenderTargets[0] = new RenderTargetBlendDescription()
            {
                BlendEnable           = true, BlendOperation = BlendOperation.Add,
                BlendOperationAlpha   = BlendOperation.Add,
                DestinationBlend      = BlendOption.Zero,
                DestinationBlendAlpha = BlendOption.Zero,
                RenderTargetWriteMask = ColorWriteMaskFlags.All, SourceBlend = BlendOption.One, SourceBlendAlpha = BlendOption.One
            };
            BlendState disableState = BlendState.FromDescription(_context.DeviceManager.Device, defaultDesc);

            blendingStates.Add(BlendStates.Disable, disableState);
            defaultDesc.RenderTargets[0] = new RenderTargetBlendDescription()
            {
                BlendEnable           = true,
                BlendOperation        = BlendOperation.Add,
                BlendOperationAlpha   = BlendOperation.Add,
                DestinationBlend      = BlendOption.InverseDestinationAlpha,
                DestinationBlendAlpha = BlendOption.InverseDestinationAlpha,
                RenderTargetWriteMask = ColorWriteMaskFlags.All,
                SourceBlend           = BlendOption.SourceAlpha,
                SourceBlendAlpha      = BlendOption.SourceAlpha
            };
            BlendState alignment = BlendState.FromDescription(_context.DeviceManager.Device, defaultDesc);

            blendingStates.Add(BlendStates.Alignment, alignment);
            defaultDesc.RenderTargets[0] = new RenderTargetBlendDescription()
            {
                BlendEnable           = true,
                BlendOperation        = BlendOperation.Add,
                BlendOperationAlpha   = BlendOperation.Add,
                DestinationBlend      = BlendOption.One,
                DestinationBlendAlpha = BlendOption.One,
                RenderTargetWriteMask = ColorWriteMaskFlags.All,
                SourceBlend           = BlendOption.SourceAlpha,
                SourceBlendAlpha      = BlendOption.SourceAlpha
            };
            BlendState add = BlendState.FromDescription(_context.DeviceManager.Device, defaultDesc);

            blendingStates.Add(BlendStates.Add, add);
            defaultDesc.RenderTargets[0] = new RenderTargetBlendDescription()
            {
                BlendEnable           = true,
                BlendOperation        = BlendOperation.ReverseSubtract,
                BlendOperationAlpha   = BlendOperation.ReverseSubtract,
                DestinationBlend      = BlendOption.One,
                DestinationBlendAlpha = BlendOption.One,
                RenderTargetWriteMask = ColorWriteMaskFlags.All,
                SourceBlend           = BlendOption.SourceAlpha,
                SourceBlendAlpha      = BlendOption.SourceAlpha
            };
            BlendState rsubtract = BlendState.FromDescription(_context.DeviceManager.Device, defaultDesc);

            blendingStates.Add(BlendStates.ReverseSubtruct, rsubtract);
            defaultDesc.RenderTargets[0] = new RenderTargetBlendDescription()
            {
                BlendEnable           = true,
                BlendOperation        = BlendOperation.Subtract,
                BlendOperationAlpha   = BlendOperation.Subtract,
                DestinationBlend      = BlendOption.One,
                DestinationBlendAlpha = BlendOption.One,
                RenderTargetWriteMask = ColorWriteMaskFlags.All,
                SourceBlend           = BlendOption.SourceAlpha,
                SourceBlendAlpha      = BlendOption.SourceAlpha
            };
            BlendState subtruct = BlendState.FromDescription(_context.DeviceManager.Device, defaultDesc);

            blendingStates.Add(BlendStates.Subtruct, subtruct);
            defaultDesc.RenderTargets[0] = new RenderTargetBlendDescription()
            {
                BlendEnable           = true,
                BlendOperation        = BlendOperation.Add,
                BlendOperationAlpha   = BlendOperation.Add,
                DestinationBlend      = BlendOption.SourceColor,
                DestinationBlendAlpha = BlendOption.SourceAlpha,
                RenderTargetWriteMask = ColorWriteMaskFlags.All,
                SourceBlend           = BlendOption.Zero,
                SourceBlendAlpha      = BlendOption.Zero
            };
            BlendState multiply = BlendState.FromDescription(_context.DeviceManager.Device, defaultDesc);

            blendingStates.Add(BlendStates.Multiply, multiply);
        }
Esempio n. 23
0
        public States()
        {
            var device = RenderFrame.Instance.device;
            DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription
            {
                IsDepthEnabled   = false,
                IsStencilEnabled = false,
                DepthWriteMask   = DepthWriteMask.Zero,
                DepthComparison  = Comparison.Less,
            };

            depthDisabledStencilDisabledWriteDisabled = DepthStencilState.FromDescription(device, dsStateDesc);
            dsStateDesc.DepthWriteMask = DepthWriteMask.All;
            depthDisabledStencilDisabledWriteEnabled = DepthStencilState.FromDescription(device, dsStateDesc);
            dsStateDesc.IsDepthEnabled = true;
            depthEnabledStencilDisabledWriteEnabled = DepthStencilState.FromDescription(device, dsStateDesc);
            dsStateDesc.DepthWriteMask = DepthWriteMask.Zero;
            depthEnabledStencilDisabledWriteDisabled = DepthStencilState.FromDescription(device, dsStateDesc);

            RasterizerStateDescription rasStateDesc = new RasterizerStateDescription
            {
                CullMode                 = CullMode.None,
                DepthBias                = 0,
                DepthBiasClamp           = 0.0f,
                FillMode                 = FillMode.Solid,
                IsAntialiasedLineEnabled = false,
                IsDepthClipEnabled       = false,
                IsFrontCounterclockwise  = true,
                IsMultisampleEnabled     = false,
                IsScissorEnabled         = false
            };

            cullNoneFillSolid      = RasterizerState.FromDescription(device, rasStateDesc);
            rasStateDesc.FillMode  = FillMode.Wireframe;
            cullNoneFillWireframe  = RasterizerState.FromDescription(device, rasStateDesc);
            rasStateDesc.CullMode  = CullMode.Back;
            cullBackFillWireframe  = RasterizerState.FromDescription(device, rasStateDesc);
            rasStateDesc.FillMode  = FillMode.Solid;
            cullBackFillSolid      = RasterizerState.FromDescription(device, rasStateDesc);
            rasStateDesc.CullMode  = CullMode.Front;
            cullFrontFillSolid     = RasterizerState.FromDescription(device, rasStateDesc);
            rasStateDesc.FillMode  = FillMode.Wireframe;
            cullFrontFillWireframe = RasterizerState.FromDescription(device, rasStateDesc);

            BlendStateDescription blendStateDesc = new BlendStateDescription
            {
                IndependentBlendEnable = false,
                AlphaToCoverageEnable  = false,
            };

            blendStateDesc.RenderTargets[0] = new RenderTargetBlendDescription
            {
                RenderTargetWriteMask = ColorWriteMaskFlags.All
            };

            blendDisabled = BlendState.FromDescription(device, blendStateDesc);
            blendStateDesc.RenderTargets[0].BlendEnable           = true;
            blendStateDesc.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            blendStateDesc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            blendStateDesc.RenderTargets[0].DestinationBlend      = BlendOption.DestinationAlpha;
            blendStateDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.DestinationAlpha;
            blendStateDesc.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
            blendStateDesc.RenderTargets[0].SourceBlendAlpha      = BlendOption.SourceAlpha;
            blendEnabledSourceAlphaDestinationAlpha = BlendState.FromDescription(device, blendStateDesc);

            blendStateDesc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Maximum;
            blendStateDesc.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            blendStateDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.InverseSourceAlpha;
            blendEnabledSourceAlphaInverseSourceAlpha             = BlendState.FromDescription(device, blendStateDesc);

            blendStateDesc.RenderTargets[0].DestinationBlend      = BlendOption.One;
            blendStateDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.One;
            blendStateDesc.RenderTargets[0].SourceBlend           = BlendOption.One;
            blendStateDesc.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            blendEnabledOneOne = BlendState.FromDescription(device, blendStateDesc);

            var desc = new BlendStateDescription
            {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false
            };

            desc.RenderTargets[0].BlendEnable           = true;
            desc.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            desc.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
            desc.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            desc.RenderTargets[0].SourceBlendAlpha      = BlendOption.Zero;
            desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
            desc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            blendEnabledAlphaBlending = BlendState.FromDescription(device, desc);
        }
Esempio n. 24
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());
            }
        }
Esempio n. 25
0
        private void DoRenderLoop(object pmMain = null)
        {
            RasterizerStateDescription rsd = new RasterizerStateDescription();

            rsd.CullMode = CullMode.None;
            rsd.FillMode = FillMode.Solid;
            mainD11.ImmediateContext.Rasterizer.State = RasterizerState.FromDescription(mainD11, rsd);
            SamplerDescription sd = new SamplerDescription();

            sd.Filter      = Filter.MinLinearMagPointMipLinear;
            sd.AddressU    = TextureAddressMode.Wrap;
            sd.AddressV    = TextureAddressMode.Wrap;
            sd.AddressW    = TextureAddressMode.Wrap;
            sd.BorderColor = new Color4(Color.White);
            SamplerState ssMain     = SamplerState.FromDescription(mainD11, sd);
            Texture2D    backBuffer = Texture2D.FromSwapChain <Texture2D>(mainSC, 0);
            var          renderView = new RenderTargetView(mainD11, backBuffer);

            BlendStateDescription bsd = new BlendStateDescription();

            bsd.RenderTargets[0]                             = new RenderTargetBlendDescription();
            bsd.RenderTargets[0].BlendEnable                 = true;
            bsd.RenderTargets[0].RenderTargetWriteMask       = ColorWriteMaskFlags.All;
            bsd.RenderTargets[0].BlendOperation              = BlendOperation.Add;
            bsd.RenderTargets[0].SourceBlend                 = BlendOption.SourceColor;
            bsd.RenderTargets[0].DestinationBlend            = BlendOption.DestinationAlpha;
            bsd.RenderTargets[0].SourceBlendAlpha            = BlendOption.One;
            bsd.RenderTargets[0].BlendOperationAlpha         = BlendOperation.Add;
            bsd.RenderTargets[0].DestinationBlendAlpha       = BlendOption.One;
            mainD11.ImmediateContext.OutputMerger.BlendState = BlendState.FromDescription(mainD11, bsd);

            Dictionary <int, MmapUnit> mmapIndexDictionaryFloor   = null;
            Dictionary <int, MmapUnit> mmapIndexDictionarySurface = null;
            ShaderResourceView         resourceViewFloor          = GenerateMmapSRV(mainD11, 0, 512, out mmapIndexDictionaryFloor);
            ShaderResourceView         resourceViewSurface        = GenerateMmapSRV(mainD11, 512, 1024, out mmapIndexDictionarySurface);
            MapRange mmapRangeFloor   = GenerateMmapRange(mainD11, 1, 0, 80, 0, 210, mmapIndexDictionaryFloor, screenPixelRateX, screenPixelRateY);
            MapRange mmapRangeSurface = GenerateMmapRange(mainD11, 2, 0, 80, 0, 210, mmapIndexDictionarySurface, screenPixelRateX, screenPixelRateY);

            var          colorBytecodeV = ShaderBytecode.CompileFromFile("Color.hlsl", "VS", "vs_5_0", ShaderFlags.None, EffectFlags.None);
            VertexShader vsColor        = new VertexShader(mainD11, colorBytecodeV);
            var          colorBytecodeP = ShaderBytecode.CompileFromFile("Color.hlsl", "PS", "ps_5_0", ShaderFlags.None, EffectFlags.None);
            PixelShader  psColor        = new PixelShader(mainD11, colorBytecodeP);
            var          signatureColor = ShaderSignature.GetInputSignature(colorBytecodeV);
            var          layoutColor    = new InputLayout(mainD11, signatureColor, new[] {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
            });

            var          textureBytecodeV = ShaderBytecode.CompileFromFile("Texture.hlsl", "VS", "vs_5_0", ShaderFlags.None, EffectFlags.None);
            VertexShader vsTexture        = new VertexShader(mainD11, textureBytecodeV);
            var          textureBytecodeP = ShaderBytecode.CompileFromFile("Texture.hlsl", "PS", "ps_5_0", ShaderFlags.None, EffectFlags.None);
            PixelShader  psTexture        = new PixelShader(mainD11, textureBytecodeP);
            var          signatureTexture = ShaderSignature.GetInputSignature(textureBytecodeV);
            var          layoutTexture    = new InputLayout(mainD11, signatureTexture, new[] {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32B32A32_Float, 16, 0)
            });

            mainD11.ImmediateContext.OutputMerger.SetTargets(renderView);
            mainD11.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, this.Width, this.Height, 0.0f, 1.0f));
            mainD11.ImmediateContext.PixelShader.SetSampler(ssMain, 0);
            mainD11.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            mainD11.ImmediateContext.InputAssembler.InputLayout       = layoutTexture;
            mainD11.ImmediateContext.VertexShader.Set(vsTexture);
            mainD11.ImmediateContext.PixelShader.Set(psTexture);

            Matrix   projection       = Matrix.OrthoLH(2f, 2f, 0, 2f);
            Matrix   view             = Matrix.LookAtLH(new Vector3(0, 0, -1f), Vector3.Zero, Vector3.UnitY);
            Matrix   world            = Matrix.Translation(Vector3.Zero);
            Matrix   WVP              = world * view * projection;
            Buffer11 constantBufferCB = new Buffer11(mainD11, new BufferDescription
            {
                Usage               = ResourceUsage.Default,
                SizeInBytes         = Marshal.SizeOf(WVP),
                BindFlags           = BindFlags.ConstantBuffer,
                CpuAccessFlags      = CpuAccessFlags.None,
                OptionFlags         = ResourceOptionFlags.None,
                StructureByteStride = 0,
            });

            mainD11.ImmediateContext.VertexShader.SetConstantBuffer(constantBufferCB, 0);

            while (running)
            {
                mainD11.ImmediateContext.ClearRenderTargetView(renderView, Color.Black);

                world = Matrix.Translation(new Vector3(moveX, moveY, 0));

                WVP = world * view * projection;
                DataStream dsCamera = new DataStream(Marshal.SizeOf(WVP), true, true);
                dsCamera.Write(WVP);
                dsCamera.Position = 0;
                mainD11.ImmediateContext.UpdateSubresource(new DataBox(0, 0, dsCamera), constantBufferCB, 0);

                mainD11.ImmediateContext.PixelShader.SetShaderResource(resourceViewFloor, 0);
                mainD11.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(mmapRangeFloor.mapVertexBuffer, 32, 0));
                mainD11.ImmediateContext.Draw(mmapRangeFloor.vector4Count, 0);

                mainD11.ImmediateContext.PixelShader.SetShaderResource(resourceViewSurface, 0);
                if (mmapRangeSurface != null)
                {
                    mainD11.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(mmapRangeSurface.mapVertexBuffer, 32, 0));
                    mainD11.ImmediateContext.Draw(mmapRangeSurface.vector4Count, 0);
                }

                mainSC.Present(0, PresentFlags.None);
                Thread.Sleep(10);
            }

            colorBytecodeP.Dispose();
            colorBytecodeV.Dispose();
            textureBytecodeP.Dispose();
            textureBytecodeV.Dispose();
            mmapRangeFloor.mapVertexBuffer.Dispose();
            if (mmapRangeSurface != null)
            {
                mmapRangeSurface.mapVertexBuffer.Dispose();
            }
            layoutColor.Dispose();
            layoutTexture.Dispose();
            constantBufferCB.Dispose();
            renderView.Dispose();
            backBuffer.Dispose();
            mainD11.Dispose();
            mainSC.Dispose();
        }
        public override void Render(Device device,
                                    SlimDX.Matrix viewMatrix)
        {
            var globals = new VertexShaderGlobals();

            globals.WorldViewProj = viewMatrix;
            globals.World         = SlimDX.Matrix.Identity;
            this._VertexShaderConstantBuffer1.Update(globals);

            device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);

            device.VertexShader.Set(this._ShaderLoader.VertexShader);
            device.VertexShader.SetConstantBuffer(this._VertexShaderConstantBuffer1.Buffer, 1);

            var globalConsts = new PixelShaderGlobalConstants();

            globalConsts.Globals     = new ShaderNatives.float4[15];
            globalConsts.Globals[0]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[1]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[2]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[3]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[4]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[5]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[6]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[7]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[8]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[9]  = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[10] = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[11] = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[12] = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[13] = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);
            globalConsts.Globals[14] = new ShaderNatives.float4(1.0f, 1.0f, 1.0f, 1.0f);

            this._PixelShaderConstantBuffer0.Update(globalConsts);

            var instanceConsts = new PixelShaderInstanceConstants();

            instanceConsts.InstanceConstants    = new ShaderNatives.float4[3];
            instanceConsts.InstanceConstants[0] = new ShaderNatives.float4(0.0f, 1.0f, 1.0f, 1.0f);
            instanceConsts.InstanceConstants[1] = new ShaderNatives.float4(1.0f, 1.0f, 0.0f, 0.8f);
            this._PixelShaderConstantBuffer1.Update(instanceConsts);

            var materialConsts = new PixelShaderMaterialConstants();

            materialConsts.MaterialConstants = new ShaderNatives.float4[3];
            for (int i = 0; i < 3; i++)
            {
                materialConsts.MaterialConstants[i] = new ShaderNatives.float4(1.0f, 1.0f, 0.0f, 1.0f);
            }
            this._PixelShaderConstantBuffer2.Update(materialConsts);

            var bs = new PixelShaderBooleans();

            bs.Bools      = new ShaderNatives.bool4[4];
            bs.Bools[3].X = true;
            bs.Bools[3].Y = true;
            bs.Bools[3].Z = false;
            this._PixelShaderConstantBuffer4.Update(bs);

            device.PixelShader.Set(this._ShaderLoader.PixelShader);
            device.PixelShader.SetConstantBuffer(this._PixelShaderConstantBuffer0.Buffer, 0);
            device.PixelShader.SetConstantBuffer(this._PixelShaderConstantBuffer1.Buffer, 1);
            device.PixelShader.SetConstantBuffer(this._PixelShaderConstantBuffer2.Buffer, 2);
            device.PixelShader.SetConstantBuffer(this._PixelShaderConstantBuffer4.Buffer, 4);

            this._MaterialLoader.SetShaderResource(device);

            device.InputAssembler.SetInputLayout(this._ShaderLoader.InputLayout);
            device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(this._VertexData0Buffer, 48, 0));
            device.InputAssembler.SetIndexBuffer(this._IndexBuffer, DXGI.Format.R16_UInt, 0);

            var bsd = new BlendStateDescription()
            {
                IsAlphaToCoverageEnabled = true,

                SourceAlphaBlend      = BlendOption.One,
                DestinationAlphaBlend = BlendOption.Zero,
                AlphaBlendOperation   = BlendOperation.Add,

                SourceBlend      = BlendOption.SourceAlpha,
                DestinationBlend = BlendOption.Zero,
                BlendOperation   = BlendOperation.Add,
            };

            bsd.SetBlendEnable(0, true);
            bsd.SetWriteMask(0, ColorWriteMaskFlags.All);

            device.OutputMerger.BlendState = BlendState.FromDescription(device, bsd);

            device.DrawIndexed(this.Block.Faces.Count, 0, 0);
        }
Esempio n. 27
0
        private void InitalizeGraphics()
        {
            if (Window.RenderCanvasHandle == IntPtr.Zero)
            {
                throw new InvalidOperationException();
            }

            SwapChainDescription swapChainDesc = new SwapChainDescription()
            {
                BufferCount     = 1,
                Flags           = SwapChainFlags.None,
                IsWindowed      = true,
                OutputHandle    = Window.RenderCanvasHandle,
                SwapEffect      = SwapEffect.Discard,
                Usage           = Usage.RenderTargetOutput,
                ModeDescription = new ModeDescription()
                {
                    Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
                    //Format = SlimDX.DXGI.Format.B8G8R8A8_UNorm,
                    Width            = (int)Window.RenderCanvasSize.Width,
                    Height           = (int)Window.RenderCanvasSize.Height,
                    RefreshRate      = new Rational(60, 1),
                    Scaling          = DisplayModeScaling.Unspecified,
                    ScanlineOrdering = DisplayModeScanlineOrdering.Unspecified
                },
                SampleDescription = new SampleDescription(1, 0)
            };

            var giFactory = new SlimDX.DXGI.Factory();
            var adapter   = giFactory.GetAdapter(0);

            Device    device;
            SwapChain swapChain;

            Device.CreateWithSwapChain(adapter, DriverType.Hardware, DeviceCreationFlags.Debug, swapChainDesc, out device, out swapChain);
            _swapChain     = swapChain;
            GraphicsDevice = device;

            // create a view of our render target, which is the backbuffer of the swap chain we just created
            using (var resource = SlimDX.Direct3D10.Resource.FromSwapChain <Texture2D>(swapChain, 0))
            {
                _backBuffer = new RenderTargetView(device, resource);
            }

            // setting a viewport is required if you want to actually see anything
            var viewport = new Viewport(0, 0, (int)Window.RenderCanvasSize.Width, (int)Window.RenderCanvasSize.Height);

            device.OutputMerger.SetTargets(_backBuffer);
            device.Rasterizer.SetViewports(viewport);

            CreateDepthStencil();
            LoadVisualizationEffect();

            // Allocate a large buffer to write the PhysX visualization vertices into
            // There's more optimized ways of doing this, but for this sample a large buffer will do
            _userPrimitivesBuffer = new SlimDX.Direct3D10.Buffer(GraphicsDevice, VertexPositionColor.SizeInBytes * 50000, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None);

            var elements = new[]
            {
                new InputElement("Position", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("Color", 0, Format.R32G32B32A32_Float, 16, 0)
            };

            _inputLayout = new InputLayout(GraphicsDevice, _visualizationEffect.RenderScenePass0.Description.Signature, elements);

            // States
            var blendDesc = new BlendStateDescription()
            {
                SourceBlend           = BlendOption.One,
                DestinationBlend      = BlendOption.Zero,
                BlendOperation        = BlendOperation.Add,
                SourceAlphaBlend      = BlendOption.One,
                DestinationAlphaBlend = BlendOption.Zero,
                AlphaBlendOperation   = BlendOperation.Add
            };

            blendDesc.SetBlendEnable(0, true);
            _blendState = BlendState.FromDescription(device, blendDesc);

            var rasterDesc = new RasterizerStateDescription()
            {
                IsAntialiasedLineEnabled = false,
                IsMultisampleEnabled     = false,
                FillMode = FillMode.Solid,
                CullMode = CullMode.None
            };

            _rasterizerState = RasterizerState.FromDescription(device, rasterDesc);
        }
Esempio n. 28
0
        public static void Initialize3D(IntPtr handle, int viewPortWidth, int viewPortHeight)
        {
            #region Set Swapchain and Viewport
            var swapDiscription = new SwapChainDescription()
            {
                BufferCount       = 1,
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = handle,
                ModeDescription   = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags             = SwapChainFlags.AllowModeSwitch,
                SwapEffect        = SwapEffect.Discard
            };
            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapDiscription, out device, out swapChain);
            swapChain.IsFullScreen = false;
            context = device.ImmediateContext;
            using (var factory = swapChain.GetParent <FactoryDXGI>())
                factory.SetWindowAssociation(handle, WindowAssociationFlags.IgnoreAltEnter);
            viewport = new Viewport(0, 0, viewPortWidth, viewPortHeight);
            context.Rasterizer.SetViewports(viewport);
            #endregion
            #region Set RenderTarget and DepthStencil
            SetTargetAndDepthSView(viewPortWidth, viewPortHeight);
            #endregion
            #region Set Blend&RasterizerStates
            RasterizerStateDescription backCull = new RasterizerStateDescription()
            {
                FillMode = SlimDX.Direct3D11.FillMode.Solid,
                CullMode = CullMode.Back,
                IsFrontCounterclockwise = true,
                IsDepthClipEnabled      = true
            };

            backCullState = RasterizerState.FromDescription(device, backCull);

            BlendStateDescription transDesc = new BlendStateDescription()
            {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false
            };
            transDesc.RenderTargets[0].BlendEnable           = true;
            transDesc.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
            transDesc.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            transDesc.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            transDesc.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            transDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
            transDesc.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            transDesc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

            transBlendState = BlendState.FromDescription(device, transDesc);

            context.Rasterizer.State        = backCullState;
            context.OutputMerger.BlendState = transBlendState;
            #endregion

            LoadShaders();
            LoadInputLayouts();

            worldMatrix = Matrix.Translation(-8, 0, -8);
            projMatrix  = Matrix.PerspectiveFovRH(FOV, (float)viewPortWidth / viewPortHeight, 0.01f, 1000.0f);
            SetupCamera();
            grid = new CoordinateGrid(new Color4(0.95f, 0.95f, 0.95f));
        }
Esempio n. 29
0
        /// <summary>
        /// TODO doc
        /// </summary>
        public void Init(string windowName, int top = 10, int left = 10, int width = 1280, int height = 1024)
        {
            InitAwesomium();

            _form = new RenderForm
            {
                Text            = windowName,
                Icon            = new System.Drawing.Icon("res/x.ico"),
                FormBorderStyle = FormBorderStyle.Sizable, // set to NONE for pseudo full screen
                StartPosition   = FormStartPosition.Manual,

                Top    = top,
                Left   = left,
                Width  = width,
                Height = height
            };


            // The characteristics of our swap chain
            var swapChainDescription = new SwapChainDescription
            {
                BufferCount       = 2, // front & back buffer
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = _form.Handle,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1 /*samples per pixel*/, 0 /*anti-aliasing level*/),
                Flags             = SwapChainFlags.AllowModeSwitch,
                SwapEffect        = SwapEffect.Discard
            };


            Device.CreateWithSwapChain(DriverType.Hardware,
                                       DeviceCreationFlags.None,
                                       swapChainDescription,
                                       out _device,
                                       out _swapChain);



            // create a view of our render target, which is the backbuffer of the swap chain
            using (var res = Resource.FromSwapChain <Texture2D>(_swapChain, 0))
                _renderTarget = new RenderTargetView(_device, res);



            // create a viewport
            var context  = _device.ImmediateContext;
            var viewport = new Viewport(0.0f, 0.0f, _form.ClientSize.Width, _form.ClientSize.Height);

            context.OutputMerger.SetTargets(_renderTarget);
            context.Rasterizer.SetViewports(viewport);


            // load and compile the vertex shader & get its input signature
            using (var bytecode = ShaderBytecode.CompileFromFile("res/simple.fx", "vertex_shader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
            {
                _vsInputSignature = ShaderSignature.GetInputSignature(bytecode);
                _vertexShader     = new VertexShader(_device, bytecode);
            }



            // enable alpha blending
            var blendStateDescription = new BlendStateDescription
            {
                AlphaToCoverageEnable  = false,
                IndependentBlendEnable = false,
            };

            blendStateDescription.RenderTargets[0].BlendEnable           = true;
            blendStateDescription.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
            blendStateDescription.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            blendStateDescription.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            blendStateDescription.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            blendStateDescription.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
            blendStateDescription.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            blendStateDescription.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;


            BlendState TransparentBS = BlendState.FromDescription(_device, blendStateDescription);

            context.OutputMerger.BlendState = TransparentBS;


            // initialize all pixel shaders
            InitPixelShaders();


            #region Input Handling

            // prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
            using (var factory = _swapChain.GetParent <Factory>())
                factory.SetWindowAssociation(_form.Handle, WindowAssociationFlags.IgnoreAltEnter);


            // add input handler
            _form.KeyDown += HandleInput;


            // handle form size changes

            /* TODO do as u wish
             * _form.UserResized += (o, e) =>
             * {
             *  _renderTarget.Dispose();
             *
             *  _swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
             *  using (var resource = Resource.FromSwapChain<Texture2D>(_swapChain, 0))
             *      _renderTarget = new RenderTargetView(_device, resource);
             *
             *  context.OutputMerger.SetTargets(_renderTarget);
             * };
             */

            #endregion
        }
Esempio n. 30
0
        public void Reset()
        {
            Destroy();

            using (Factory factory = new Factory())
            {
                SlimDX.Direct3D10.Device.CreateWithSwapChain(factory.GetAdapter(0), DriverType.Hardware, DeviceCreationFlags.None, new SwapChainDescription
                {
                    BufferCount       = 2,
                    IsWindowed        = true,
                    OutputHandle      = renderTarget.Handle,
                    Usage             = Usage.RenderTargetOutput,
                    SampleDescription = new SampleDescription(1, 0),
                    ModeDescription   = new ModeDescription(renderTarget.Width, renderTarget.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm)
                }, out device, out swapChain);
            }

            using (Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0))
            {
                renderTargetView = new RenderTargetView(device, backBuffer);
            }
            device.OutputMerger.SetTargets(renderTargetView);

            bsd = new BlendStateDescription();
            bsd.AlphaBlendOperation   = BlendOperation.Add;
            bsd.BlendOperation        = BlendOperation.Add;
            bsd.SourceBlend           = BlendOption.SourceAlpha;
            bsd.DestinationBlend      = BlendOption.InverseSourceAlpha;
            bsd.SourceAlphaBlend      = BlendOption.SourceAlpha;
            bsd.DestinationAlphaBlend = BlendOption.InverseSourceAlpha;
            bsd.SetWriteMask(0, ColorWriteMaskFlags.All);
            bsd.IsAlphaToCoverageEnabled = true;
            bsd.SetBlendEnable(0, true);

            device.OutputMerger.BlendSampleMask = -1;
            device.OutputMerger.BlendFactor     = new Color4(0, 0, 0, 0);
            device.OutputMerger.BlendState      = BlendState.FromDescription(device, bsd);

            viewport = new Viewport(0, 0, renderTarget.Width, renderTarget.Height);
            device.Rasterizer.SetViewports(viewport);

            BufferDescription bd = new BufferDescription(4 * Marshal.SizeOf(typeof(Vertex)), ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None);

            vertexBuffer = new SlimDX.Direct3D10.Buffer(device, bd);

            Vertex[] vertexData = new Vertex[4];
            vertexData[0].pos      = new Vector3(-1, -1, 0);
            vertexData[0].texCoord = new Vector2(0, 1);
            vertexData[1].pos      = new Vector3(-1, 1, 0);
            vertexData[1].texCoord = new Vector2(0, 0);
            vertexData[2].pos      = new Vector3(1, -1, 0);
            vertexData[2].texCoord = new Vector2(1, 1);
            vertexData[3].pos      = new Vector3(1, 1, 0);
            vertexData[3].texCoord = new Vector2(1, 0);

            DataStream vbStream = vertexBuffer.Map(MapMode.WriteDiscard, SlimDX.Direct3D10.MapFlags.None);

            vbStream.WriteRange(vertexData);
            vertexBuffer.Unmap();

            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            Stream file = thisExe.GetManifestResourceStream("DirectXEmu.Video.Shaders.SimpleRender.fx");

            effect = Effect.FromStream(device, file, "fx_4_0");
            file.Close();

            EffectTechnique technique;

            if (smoothOutput)
            {
                technique = effect.GetTechniqueByName("RenderSmooth");
            }
            else
            {
                technique = effect.GetTechniqueByName("Render");
            }

            pass = technique.GetPassByIndex(0);

            ShaderSignature signature = pass.Description.Signature;

            inputLayout = new InputLayout(device, signature, new[] {
                new InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0, 0),
                new InputElement("TEXCOORD", 0, SlimDX.DXGI.Format.R32G32_Float, 12, 0)
            });

            texture = new Texture2D(device, new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.Write,
                Format            = Format.R8G8B8A8_UNorm,
                Height            = imageScaler.ResizedY,
                Width             = imageScaler.ResizedX,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Dynamic
            });

            textureView = new ShaderResourceView(device, texture);
            effect.GetVariableByName("tex2d").AsResource().SetResource(textureView);


            LoadCharSheet();
        }