public Direct3DRenderer()
        {
            _device = new Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_0);
            _rasterizerStateDescription = new RasterizerStateDescription()
            {
                CullMode = SlimDX.Direct3D10.CullMode.None,
                FillMode = FillMode.Solid,
                IsFrontCounterclockwise = true
            };
            var rasterizerState = RasterizerState.FromDescription(_device, _rasterizerStateDescription);

            _device.Rasterizer.State = rasterizerState;

            _depthStencilStateDescription = new DepthStencilStateDescription()
            {
                IsDepthEnabled  = true,
                DepthComparison = Comparison.Less,
                DepthWriteMask  = DepthWriteMask.All
            };
            var depthStencilState = DepthStencilState.FromDescription(_device, _depthStencilStateDescription);

            _device.OutputMerger.DepthStencilState = depthStencilState;

            _semanticsTable.Add("inPosition", "POSITION");
            _semanticsTable.Add("inNormal", "NORMAL");
            _semanticsTable.Add("inTextureCoords", "TEXCOORD");
            _semanticsTable.Add("instanceMatrix", "INSTANCE_TRANSFORM");
        }
Пример #2
0
        static public void InitializeOutputMerger()
        {
            backbuffer = Texture2D.FromSwapChain <Texture2D>(swapchain, 0);
            renderview = new RenderTargetView(device, backbuffer);
            //MainWindow.ClientSize = new Size(1200, 900);
            device.Rasterizer.SetViewports(new Viewport(0, 0, MainWindow.ClientSize.Width, MainWindow.ClientSize.Height, 0.0f, 1.0f));

            DX10.Texture2DDescription dtd = new Texture2DDescription();
            dtd.Width             = MainWindow.ClientSize.Width;
            dtd.Height            = MainWindow.ClientSize.Height;
            dtd.MipLevels         = 1;
            dtd.ArraySize         = 1;
            dtd.BindFlags         = BindFlags.DepthStencil;
            dtd.CpuAccessFlags    = CpuAccessFlags.None;
            dtd.Format            = Format.D32_Float;
            dtd.SampleDescription = new SampleDescription(1, 0);
            dtd.Usage             = ResourceUsage.Default;
            dtd.OptionFlags       = ResourceOptionFlags.None;

            depthbuffer = new Texture2D(device, dtd);


            depthview = new DepthStencilView(device, depthbuffer);

            DX10.DepthStencilStateDescription stencilStateDesc = new SlimDX.Direct3D10.DepthStencilStateDescription();
            stencilStateDesc.IsDepthEnabled   = true;
            stencilStateDesc.IsStencilEnabled = false;
            stencilStateDesc.DepthWriteMask   = DX10.DepthWriteMask.All;
            stencilStateDesc.DepthComparison  = DX10.Comparison.Less;

            device.OutputMerger.SetTargets(depthview, renderview);
            depthstate = DepthStencilState.FromDescription(device, stencilStateDesc);
        }
Пример #3
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);
        }
Пример #4
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);
        }
        public void EnableZBuffer(bool enable)
        {
            _depthStencilStateDescription.IsDepthEnabled = enable;
            var depthStencilState = DepthStencilState.FromDescription(_device, _depthStencilStateDescription);

            _device.OutputMerger.DepthStencilState = depthStencilState;
        }
Пример #6
0
        public static RenderTargetSet CreateRenderTargetSet(Device device, int width, int height, Format format, int numSurfaces, bool needsDepth)
        {
            RenderTargetSet rt = new RenderTargetSet();

            rt.m_Descriptor = new RenderTargetDescriptor()
            {
                m_Format      = format,
                m_HasDepth    = needsDepth,
                m_Height      = height,
                m_NumSurfaces = numSurfaces,
                m_Width       = width
            };

            rt.m_NumRTs = numSurfaces;
            for (int i = 0; i < numSurfaces; ++i)
            {
                rt.m_RenderTargets[i] = TextureObject.CreateTexture(device, width, height, 1, format, false, true);
            }

            if (needsDepth)
            {
                rt.m_DepthStencil = TextureObject.CreateTexture(device, width, height, 1, Format.R32_Typeless, true, false);
            }

            rt.m_Viewport = new Viewport(0, 0, width, height, 0.0f, 1.0f);

            if (m_DepthStencilState == null)
            {
                DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription()
                {
                    IsDepthEnabled   = true,
                    IsStencilEnabled = false,
                    DepthWriteMask   = DepthWriteMask.All,
                    DepthComparison  = Comparison.Less,
                };

                m_DepthStencilState = DepthStencilState.FromDescription(device, dsStateDesc);

                dsStateDesc = new DepthStencilStateDescription()
                {
                    IsDepthEnabled   = false,
                    IsStencilEnabled = false,
                    DepthWriteMask   = DepthWriteMask.Zero,
                    DepthComparison  = Comparison.Always,
                };

                m_DepthStencilStateNoDepth = DepthStencilState.FromDescription(device, dsStateDesc);
            }


            return(rt);
        }
Пример #7
0
        /// <summary>
        /// デプスステンシルステート
        /// </summary>
        static void InitializeDepthStencilState()
        {
            var device = D3D11Device;

            depthStencilState_ = new DepthStencilState[(int)RenderState.DepthState.Max];

            // None
            DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled   = false,
                IsStencilEnabled = false,
                DepthWriteMask   = DepthWriteMask.Zero,
                DepthComparison  = Comparison.Less,
            };

            depthStencilState_[(int)RenderState.DepthState.None] = DepthStencilState.FromDescription(device, dsStateDesc);

            // Normal
            dsStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled   = true,
                IsStencilEnabled = false,
                DepthWriteMask   = DepthWriteMask.All,
                DepthComparison  = Comparison.Less,
            };
            depthStencilState_[(int)RenderState.DepthState.Normal] = DepthStencilState.FromDescription(device, dsStateDesc);

            // TestOnly
            dsStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled   = true,
                IsStencilEnabled = false,
                DepthWriteMask   = DepthWriteMask.Zero,
                DepthComparison  = Comparison.Less,
            };
            depthStencilState_[(int)RenderState.DepthState.TestOnly] = DepthStencilState.FromDescription(device, dsStateDesc);

            // WriteOnly
            dsStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled   = false,
                IsStencilEnabled = false,
                DepthWriteMask   = DepthWriteMask.Zero,
                DepthComparison  = Comparison.Less,
            };
            depthStencilState_[(int)RenderState.DepthState.WriteOnly] = DepthStencilState.FromDescription(device, dsStateDesc);

            // 初期値
            SetDepthState(RenderState.DepthState.Normal);
        }
Пример #8
0
        private void Setup3dCamera(float partialStep)
        {
            Camera.Instance.Enable3d = true;
            DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled   = true,
                IsStencilEnabled = false,
                DepthWriteMask   = DepthWriteMask.All,
                DepthComparison  = Comparison.Less,
            };
            DepthStencilState depthState = DepthStencilState.FromDescription(device, dsStateDesc);

            device.ImmediateContext.OutputMerger.DepthStencilState = depthState;
            Camera.Instance.Update(partialStep);
        }
Пример #9
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;
        }
Пример #10
0
        public void CreateDepthStencilBuffer(System.Windows.Forms.Control form)
        {
            depthStencilTexture?.Dispose();
            depthStencilTexture = new Texture2D(
                device,
                new Texture2DDescription()
            {
                ArraySize         = 1,
                MipLevels         = 1,
                Format            = Format.D32_Float,
                Width             = form.ClientSize.Width,
                Height            = form.ClientSize.Height,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default
            }
                );

            depthStencil?.Dispose();
            depthStencil = new DepthStencilView(
                device,
                depthStencilTexture,
                new DepthStencilViewDescription()
            {
                ArraySize       = 0,
                FirstArraySlice = 0,
                MipSlice        = 0,
                Format          = Format.D32_Float,
                Dimension       = DepthStencilViewDimension.Texture2D
            }
                );

            depthStencilState?.Dispose();
            depthStencilState = DepthStencilState.FromDescription(
                device,
                new DepthStencilStateDescription()
            {
                DepthComparison  = Comparison.Less,
                DepthWriteMask   = DepthWriteMask.All,
                IsDepthEnabled   = true,
                IsStencilEnabled = false
            }
                );

            context.OutputMerger.DepthStencilState = depthStencilState;
            context.OutputMerger.SetTargets(depthStencil, renderTarget);
        }
Пример #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);
            }
        }
Пример #12
0
 public override void OnInitialize(DeviceContextHolder holder)
 {
     _effect     = holder.GetEffect <EffectDeferredLight>();
     _sphere     = SphereObject.Create(Radius);
     _rasterizer = RasterizerState.FromDescription(holder.Device, new RasterizerStateDescription {
         CullMode                 = CullMode.Front,
         FillMode                 = FillMode.Solid,
         IsDepthClipEnabled       = true,
         IsAntialiasedLineEnabled = false
     });
     _depth = DepthStencilState.FromDescription(holder.Device, new DepthStencilStateDescription {
         DepthComparison  = Comparison.Greater,
         IsDepthEnabled   = true,
         IsStencilEnabled = false,
         DepthWriteMask   = DepthWriteMask.Zero
     });
 }
Пример #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
            });
        }
Пример #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);
        }
Пример #15
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);
        }
Пример #16
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);
        }
Пример #17
0
        /// <summary>
        /// Creates a default <see cref="DepthStencilState"/> object.
        /// </summary>
        /// <param name="device">The current <see cref="Device"/> being used.</param>
        /// <returns>The newly created <see cref="DepthStencilState"/> with the default options.</returns>
        public static DepthStencilState CreateDepthStencilState(Device device)
        {
            DepthStencilOperationDescription dsOperation = new DepthStencilOperationDescription();

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

            DepthStencilStateDescription dsDesc = new DepthStencilStateDescription();

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

            return(DepthStencilState.FromDescription(device, dsDesc));
        }
Пример #18
0
        private static void InitializeDepthBuffer()
        {
            Format depthFormat = Format.D32_Float;

            depthBufferDesc = new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = depthFormat,
                Height            = Form.Height,
                Width             = Form.Width,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default
            };

            depthBuffer = new Texture2D(Device, depthBufferDesc);
            DepthView   = new DepthStencilView(Device, depthBuffer);

            dsStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled   = true,
                IsStencilEnabled = false,
                DepthWriteMask   = DepthWriteMask.All,
                DepthComparison  = Comparison.Less,
            };
            dsStateDescOff = new DepthStencilStateDescription()
            {
                IsDepthEnabled   = true,
                IsStencilEnabled = false,
                DepthWriteMask   = DepthWriteMask.All,
                DepthComparison  = Comparison.Less,
            };
            DepthStateOff = DepthStencilState.FromDescription(Device, dsStateDescOff);
            DepthState    = DepthStencilState.FromDescription(Device, dsStateDesc);

            DeviceContext.OutputMerger.DepthStencilState = DepthState;
        }
Пример #19
0
        /// <summary>
        /// Method called when the form is resized by the user.
        /// </summary>
        /// <param name="sender">sending form</param>
        /// <param name="eventArgs">event argument</param>
        private static void FormOnResize(object sender, EventArgs eventArgs)
        {
            RenderTarget.Dispose();
            DepthView.Dispose();
            depthBuffer.Dispose();


            DeviceContext = Device.ImmediateContext;

            DeviceContext.Rasterizer.State = RasterizerState.FromDescription(Device, Rasterizer);

            depthBufferDesc.Width  = Form.ClientSize.Width;
            depthBufferDesc.Height = Form.ClientSize.Height;


            depthBuffer = new Texture2D(Device, depthBufferDesc);
            DepthView   = new DepthStencilView(Device, depthBuffer);


            DepthState = DepthStencilState.FromDescription(Device, dsStateDesc);
            DeviceContext.OutputMerger.DepthStencilState = DepthState;



            SwapChain.ResizeBuffers(2, Form.ClientSize.Width, Form.ClientSize.Height, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
            using (var resource = Resource.FromSwapChain <Texture2D>(SwapChain, 0))
                RenderTarget = new RenderTargetView(Device, resource);

            DeviceContext.OutputMerger.SetTargets(DepthView, RenderTarget);


            Viewport = new Viewport(0.0f, 0.0f, Form.ClientSize.Width, Form.ClientSize.Height);
            DeviceContext.Rasterizer.SetViewports(Viewport);
            ProjectionMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, Viewport.Width / Viewport.Height, .1f, 1000.0f);

            UIManagerSpriteRenderer.SpriteRenderer?.RefreshViewport();
        }
Пример #20
0
        private void Initialize()
        {
            using (var code = ShaderBytecode.Compile(File.ReadAllText("..\\..\\NewModules\\Rendering\\Text\\SpriteShader.fx"), "fx_5_0"))
            {
                Fx = new Effect(device, code);
            }

            Pass                  = Fx.GetTechniqueByIndex(0).GetPassByIndex(0);
            InputLayout           = new InputLayout(device, Pass.Description.Signature, SpriteVertexLayout.Description);
            InputLayout.DebugName = "Input Layout for Sprites";

            TextureVariable = Fx.GetVariableByName("Tex").AsResource();

            VB           = new Buffer(device, BufferSize * SpriteVertexLayout.Struct.SizeInBytes, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, SpriteVertexLayout.Struct.SizeInBytes);
            VB.DebugName = "Sprites Vertexbuffer";

            var dssd = new DepthStencilStateDescription()
            {
                IsDepthEnabled = false,
                DepthWriteMask = DepthWriteMask.Zero
            };

            DSState = DepthStencilState.FromDescription(Device, dssd);
        }
Пример #21
0
        public void Initialize(DeviceContextHolder holder)
        {
            foreach (var split in Splits)
            {
                split.Buffer.Resize(holder, MapSize, MapSize, null);
            }

            _rasterizerState = RasterizerState.FromDescription(holder.Device, new RasterizerStateDescription {
                CullMode = CullMode.Front,
                FillMode = FillMode.Solid,
                IsAntialiasedLineEnabled = false,
                IsDepthClipEnabled       = true,
                DepthBias            = 100,
                DepthBiasClamp       = 0.0f,
                SlopeScaledDepthBias = 1f
            });

            _depthStencilState = DepthStencilState.FromDescription(holder.Device, new DepthStencilStateDescription {
                DepthWriteMask   = DepthWriteMask.All,
                DepthComparison  = Comparison.Greater,
                IsDepthEnabled   = true,
                IsStencilEnabled = false
            });
        }
Пример #22
0
        public HoverForm(Maze maze)
            : base("HoverRenderer")
        {
            this.ClientSize = new System.Drawing.Size(640, 480);

            var description = new SwapChainDescription()
            {
                BufferCount       = 2,
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = this.Handle,
                IsWindowed        = true,
                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.Debug, description, out device, out swapChain);

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

            // Create the depth buffer
            var depthBufferDescription = new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = Format.D32_Float,
                Height            = this.ClientSize.Height,
                Width             = this.ClientSize.Width,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default
            };

            using (var depthBuffer = new Texture2D(device, depthBufferDescription))
            {
                depthStencilView  = new DepthStencilView(device, depthBuffer);
                depthStencilState = DepthStencilState.FromDescription(device, new DepthStencilStateDescription {
                    IsDepthEnabled   = true,
                    IsStencilEnabled = false,
                    DepthWriteMask   = DepthWriteMask.All,
                    DepthComparison  = Comparison.LessEqual
                });
            }

            // Setup wireframe mode
            rasteriserState = RasterizerState.FromDescription(device, new RasterizerStateDescription
            {
                CullMode = SlimDX.Direct3D11.CullMode.None,
                FillMode = SlimDX.Direct3D11.FillMode.Wireframe
            });

            // setting a viewport is required if you want to actually see anything
            context = device.ImmediateContext;
            var viewport = new Viewport(0.0f, 0.0f, this.ClientSize.Width, this.ClientSize.Height);

            context.OutputMerger.SetTargets(depthStencilView, renderTarget);
            context.OutputMerger.DepthStencilState = depthStencilState;
            context.Rasterizer.State = rasteriserState;
            context.Rasterizer.SetViewports(viewport);

            // load and compile the vertex shader
            using (var bytecode = ShaderBytecode.CompileFromFile("shader.fx", "VShader", "vs_4_0", ShaderFlags.Debug, EffectFlags.None))
            {
                inputSignature = ShaderSignature.GetInputSignature(bytecode);
                vertexShader   = new VertexShader(device, bytecode);
            }

            // load and compile the pixel shader
            using (var bytecode = ShaderBytecode.CompileFromFile("shader.fx", "PShader", "ps_4_0", ShaderFlags.Debug, EffectFlags.None))
                pixelShader = new PixelShader(device, bytecode);

            // create test vertex data, making sure to rewind the stream afterward
            vertices        = CreateTriangleListFromMaze(maze);
            camera.Position = FindHumanStartPosition(maze);

            // create the vertex layout and buffer
            var elements = new[] {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0),
                new InputElement("COLOR", 0, Format.R32G32B32_Float, 0)
            };

            layout       = new InputLayout(device, inputSignature, elements);
            vertexBuffer = new Buffer(device, vertices, (int)vertices.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            // configure the Input Assembler portion of the pipeline with the vertex data
            context.InputAssembler.InputLayout       = layout;
            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 24, 0));

            // set the shaders
            context.VertexShader.Set(vertexShader);
            context.PixelShader.Set(pixelShader);

            // crate the constant buffer
            constantBuffer = new Buffer(device, new BufferDescription
            {
                Usage       = ResourceUsage.Default,
                SizeInBytes = Marshal.SizeOf(typeof(ConstantBuffer)),
                BindFlags   = BindFlags.ConstantBuffer
            });

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

            // handle alt+enter ourselves
            this.KeyDown += (o, e) =>
            {
                if (e.Alt && e.KeyCode == Keys.Enter)
                {
                    swapChain.IsFullScreen = !swapChain.IsFullScreen;
                }
            };

            // handle form size changes
            this.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);
            };

            this.KeyDown += new KeyEventHandler(HoverForm_KeyDown);
        }
        public Renderer3d(String title)
        {
            m_scenes = new List <Scene>();

            cameras_ = new List <Camera>();

            samplerStates_ = new Dictionary <string, SamplerState>();

            lastTime_ = DateTime.Now;

            form_        = new RenderForm(title);
            form_.Width  = 1024;
            form_.Height = 768;

            var desc = new SwapChainDescription()
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(form_.ClientSize.Width, form_.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = form_.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device_, out swapChain_);

            Factory factory = swapChain_.GetParent <Factory>();

            factory.SetWindowAssociation(form_.Handle, WindowAssociationFlags.IgnoreAll);

            backBuffer_ = Texture2D.FromSwapChain <Texture2D>(swapChain_, 0);
            renderView_ = new RenderTargetView(device_, backBuffer_);


            var depthDesc = new Texture2DDescription()
            {
                Width             = form_.Width,
                Height            = form_.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.D24_UNorm_S8_UInt,
                SampleDescription = new SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage          = ResourceUsage.Default,
                BindFlags      = BindFlags.DepthStencil,
                CpuAccessFlags = 0,
                OptionFlags    = 0
            };

            m_depthBuffer = new Texture2D(device_, depthDesc);

            device_.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, form_.ClientSize.Width, form_.ClientSize.Height, 0.1f, 1.0f));

            RasterizerStateDescription descRast = new RasterizerStateDescription()
            {
                FillMode           = FillMode.Solid,
                CullMode           = CullMode.None,
                IsDepthClipEnabled = true
            };

            m_rasterizerState = RasterizerState.FromDescription(device_, descRast);

            DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled  = true,
                DepthWriteMask  = DepthWriteMask.All,
                DepthComparison = Comparison.Less,

                IsStencilEnabled = true,
                StencilReadMask  = 0xFF,
                StencilWriteMask = 0xFF,

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

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

            DepthStencilState dsState = DepthStencilState.FromDescription(device_, dsStateDesc);

            device_.ImmediateContext.OutputMerger.DepthStencilState = dsState;
            device_.ImmediateContext.Rasterizer.State = m_rasterizerState;

            DepthStencilViewDescription DSVDesc = new DepthStencilViewDescription()
            {
                Format    = Format.D24_UNorm_S8_UInt,
                Dimension = DepthStencilViewDimension.Texture2D,
                MipSlice  = 0
            };

            m_depthView = new DepthStencilView(device_, m_depthBuffer, DSVDesc);

            dsState.Dispose();

            device_.ImmediateContext.OutputMerger.SetTargets(m_depthView, renderView_);

            CreateSamplers();
        }
Пример #24
0
        /// <summary>
        /// Sets depth stencil view.
        /// </summary>
        private void SetDepthStencilView()
        {
            if (depthTexture != null)
            {
                depthTexture.Dispose();
            }

            Texture2DDescription depthBufferDescription = new Texture2DDescription()
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = Format.D24_UNorm_S8_UInt,
                Height            = ClientSize.Height,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                Width             = ClientSize.Width
            };

            depthTexture = new Texture2D(graphicsDevice, depthBufferDescription);

            DepthStencilStateDescription stencilDescription = new DepthStencilStateDescription()
            {
                BackFace = new DepthStencilOperationDescription()
                {
                    Comparison         = Comparison.Always,
                    DepthFailOperation = StencilOperation.Decrement,
                    FailOperation      = StencilOperation.Keep,
                    PassOperation      = StencilOperation.Keep
                },
                DepthComparison = Comparison.Less,
                DepthWriteMask  = DepthWriteMask.All,
                FrontFace       = new DepthStencilOperationDescription()
                {
                    Comparison         = Comparison.Always,
                    DepthFailOperation = StencilOperation.Increment,
                    FailOperation      = StencilOperation.Keep,
                    PassOperation      = StencilOperation.Keep
                },
                IsDepthEnabled   = true,
                IsStencilEnabled = true,
                StencilReadMask  = byte.MaxValue,
                StencilWriteMask = byte.MaxValue
            };

            DepthStencilState depthStencilState = DepthStencilState.FromDescription(graphicsDevice, stencilDescription);

            graphicsDevice.ImmediateContext.OutputMerger.DepthStencilState     = depthStencilState;
            graphicsDevice.ImmediateContext.OutputMerger.DepthStencilReference = 1;

            DepthStencilViewDescription depthStencilViewDescription = new DepthStencilViewDescription()
            {
                ArraySize       = 1,
                Dimension       = DepthStencilViewDimension.Texture2D,
                FirstArraySlice = 0,
                Flags           = DepthStencilViewFlags.None,
                Format          = Format.D24_UNorm_S8_UInt,
                MipSlice        = 0
            };

            if (depthStencilView != null)
            {
                depthStencilView.Dispose();
            }

            depthStencilView = new DepthStencilView(graphicsDevice, depthTexture, depthStencilViewDescription);
        }
Пример #25
0
        public MenuEffect(Device device)
        {
            Device           = device;
            ImmediateContext = Device.ImmediateContext;

            // Compile the shader...
            string compileErrors;
            var    compiledShader = SlimDX.D3DCompiler.ShaderBytecode.CompileFromFile
                                    (
                "../../Effects/MenuEffect/MenuEffect.fx",
                null,
                "fx_5_0",
                SlimDX.D3DCompiler.ShaderFlags.None,
                SlimDX.D3DCompiler.EffectFlags.None,
                null,
                null,
                out compileErrors
                                    );

            if (compileErrors != null && compileErrors != "")
            {
                throw new EffectBuildException(compileErrors);
            }

            Effect    = new Effect(Device, compiledShader);
            Technique = Effect.GetTechniqueByName("MenuTechnique");

            var vertexDesc = new[]
            {
                new InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32_Float, 0, 0, InputClassification.PerVertexData, 0),
                new InputElement("TEXCOORD", 0, SlimDX.DXGI.Format.R32G32_Float, 8, 0, InputClassification.PerVertexData, 0)
            };

            CPO_BlendColor = Effect.GetVariableByName("gBlendColor").AsVector();
            SRV_DiffuseMap = Effect.GetVariableByName("gDiffuseMap").AsResource();

            InputLayout = new InputLayout(Device, Technique.GetPassByIndex(0).Description.Signature, vertexDesc);

            Util.ReleaseCom(ref compiledShader);

            DepthStencilStateDescription dssd = new DepthStencilStateDescription()
            {
                IsDepthEnabled   = false,
                DepthWriteMask   = DepthWriteMask.All,
                DepthComparison  = Comparison.Less,
                IsStencilEnabled = true,
                StencilReadMask  = 0xFF,
                StencilWriteMask = 0xFF,
                FrontFace        = new DepthStencilOperationDescription()
                {
                    FailOperation      = StencilOperation.Keep,
                    PassOperation      = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Increment,
                    Comparison         = Comparison.Always
                },
                BackFace = new DepthStencilOperationDescription()
                {
                    FailOperation      = StencilOperation.Keep,
                    PassOperation      = StencilOperation.Keep,
                    Comparison         = Comparison.Always,
                    DepthFailOperation = StencilOperation.Decrement
                }
            };

            DepthDisabledState = DepthStencilState.FromDescription(Device, dssd);
        }
Пример #26
0
        private void OnViewportRender(object sender, RenderEventArgs e)
        {
            foreach (IRenderBlock block in this._Model.Blocks)
            {
                using (var stateBlock = new StateBlock(e.Device, StateBlockMask.EnableAll()))
                {
                    stateBlock.Capture();

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

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

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

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

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

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

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

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

                    stateBlock.Apply();
                }
            }
        }
Пример #27
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());
            }
        }
Пример #28
0
        void DxResize()
        {
            if (_renderTarget != null)
            {
                _renderTarget.Dispose();
                _renderTargetTexture.Dispose();
                _depthState.Dispose();
                _depthStencilView.Dispose();
                _depthStencilTexture.Dispose();
            }

            _renderTargetTexture = new Texture2D(CurrentDevice, new Texture2DDescription {
                Width             = _width,
                Height            = _height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.R8G8B8A8_UNorm,
                SampleDescription = _sampleDesc,
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });
            _renderTarget         = new RenderTargetView(CurrentDevice, _renderTargetTexture);
            _renderTargetResource = new ShaderResourceView(CurrentDevice, _renderTargetTexture);

            _depthStencilTexture = new Texture2D(CurrentDevice, new Texture2DDescription {
                Width             = _width,
                Height            = _height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.R24G8_Typeless,
                SampleDescription = _sampleDesc,
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.ShaderResource | BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            })
            {
                DebugName = "DepthStencilBuffer"
            };
            _depthStencilView = new DepthStencilView(CurrentDevice, _depthStencilTexture, new DepthStencilViewDescription {
                Flags     = DepthStencilViewFlags.None,
                Format    = Format.D24_UNorm_S8_UInt,
                Dimension = DepthStencilViewDimension.Texture2DMultisampled,
                MipSlice  = 0
            });

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

            _viewport = new Viewport(0, 0, _width, _height, 0.0f, 1.0f);
            _context.Rasterizer.SetViewports(_viewport);

            _camera.SetLens(AspectRatio);
        }
        public DeferredRenderer(DX11Game game)
        {
            this.game = game;
            var device = game.Device;

            context = device.ImmediateContext;

            screenWidth  = game.Form.Form.ClientSize.Width;
            screenHeight = game.Form.Form.ClientSize.Height;
            int width  = screenWidth;
            int height = screenHeight;

            gBuffer     = new GBuffer(game.Device, width, height);
            texturePool = new TexturePool(game);

            meshesRenderer = new DeferredMeshesRenderer(game, gBuffer, TexturePool);

            directionalLightRenderer = new DirectionalLightRenderer(game, GBuffer);
            spotLightRenderer        = new SpotLightRenderer(game, GBuffer);
            pointLightRenderer       = new PointLightRenderer(game, GBuffer);

            combineFinalRenderer = new CombineFinalRenderer(game, GBuffer);

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

            hdrImage = new Texture2D(device, desc);

            hdrImageRtv = new RenderTargetView(device, hdrImage);
            hdrImageRV  = new ShaderResourceView(device, hdrImage);

            calculater = new AverageLuminanceCalculater(game, hdrImageRV);

            toneMap = new ToneMapRenderer(game);


            var tempDesc = new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.None,
                CpuAccessFlags    = CpuAccessFlags.Read,
                Format            = Format.R32_Float,
                Height            = 1,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Staging,
                Width             = 1
            };

            tempTex = new Texture2D(device, tempDesc);



            ssao = new HorizonSSAORenderer(game, screenWidth, screenHeight);



            Vector3 radius = new Vector3(500, 1000, 500);

            frustumCuller = new FrustumCuller(new BoundingBox(-radius, radius), 1);

            gbufferView           = frustumCuller.CreateView();
            meshesRenderer.Culler = frustumCuller;

            Texture2D skyColorTexture;// = Texture2D.FromFile(game.Device, TWDir.GameData.CreateSubdirectory("Core") + "\\skyColor.bmp");

            var strm = new DataStream(16 * 4, true, true);

            var multiplier = 2;

            strm.Write(new Half4(new Half(135f / 255f * multiplier), new Half(206f / 255f * multiplier), new Half(235 / 255f * multiplier), new Half(1)));
            strm.Position = 0;
            var dataRectangle = new DataRectangle(16 * 4, strm);

            skyColorTexture = new Texture2D(game.Device, new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = Format.R16G16B16A16_Float,
                Height            = 1,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                Width             = 1
            }, dataRectangle);

            skyColorRV = new ShaderResourceView(game.Device, skyColorTexture);


            postProcessRT1 = CreateBackbufferLikeRT();
            postProcessRT2 = CreateBackbufferLikeRT();

            fogRenderer = new FogEffect(game);

            backgroundDepthStencilState = DepthStencilState.FromDescription(game.Device, new DepthStencilStateDescription()
            {
                IsDepthEnabled  = true,
                DepthComparison = Comparison.LessEqual,
                DepthWriteMask  = DepthWriteMask.Zero,
            });

            lineManager = new LineManager3D(game.Device);
            updateRasterizerState();
        }
Пример #30
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);
        }