Esempio n. 1
0
        public void Init()
        {
            #region Device Setup
            CreateDeviceAndSwapChain();
            deviceContext = device.ImmediateContext;

            swapChain.GetParent <Factory>().MakeWindowAssociation(renderForm.Handle, WindowAssociationFlags.IgnoreAll);

            this.backBuffer  = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
            renderTargetView = new RenderTargetView(device, backBuffer);

            deviceContext.OutputMerger.SetRenderTargets(renderTargetView);

            viewport = new Viewport(0, 0, width, height);
            deviceContext.Rasterizer.SetViewport(viewport);
            deviceContext.Rasterizer.SetScissorRectangle(0, 0, renderForm.ClientSize.Width, renderForm.ClientSize.Height);
            renderState = new RasterizerState(device, new RasterizerStateDescription()
            {
                CullMode                 = CullMode.Back,
                DepthBias                = 0,
                DepthBiasClamp           = 0,
                FillMode                 = FillMode.Solid,
                IsAntialiasedLineEnabled = false,
                IsDepthClipEnabled       = true,
                IsFrontCounterClockwise  = false,
                IsMultisampleEnabled     = true,
                IsScissorEnabled         = false,
                SlopeScaledDepthBias     = 0
            });
            deviceContext.Rasterizer.State = renderState;

            depthBuffer = new Texture2D(device, new Texture2DDescription()
            {
                Format            = Format.D32_Float_S8X24_UInt,
                ArraySize         = 1,
                MipLevels         = 1,
                Width             = renderForm.ClientSize.Width,
                Height            = renderForm.ClientSize.Height,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.DepthStencil,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.None
            });
            depthStencilView = new DepthStencilView(device, depthBuffer);
            deviceContext.OutputMerger.SetTargets(depthStencilView, renderTargetView);
            deviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            #endregion

            RegisterServices();

            this.renderService = engine.Services.GetService <RenderService>();
            this.lightService  = engine.Services.GetService <LightService>();

            renderService.Init();

            //Constant Buffers
            perFrameBuffer         = engine.Services.GetService <BufferFactory>().CreateConstantBuffer <PerFrameCB>();
            perObjectBuffer        = engine.Services.GetService <BufferFactory>().CreateConstantBuffer <PerObjectCB>();
            PointLightBuffer       = engine.Services.GetService <BufferFactory>().CreateConstantBuffer(SharpDX.Utilities.SizeOf <PointLightStructure>() * Constants.MaxLightCounts.POINT);
            DirectionalLightBuffer = engine.Services.GetService <BufferFactory>().CreateConstantBuffer(SharpDX.Utilities.SizeOf <DirectionalLightStructure>() * Constants.MaxLightCounts.DIRECTIONAL);

            renderService.RegisterConstantBuffer(Constants.ConstantBufferNames.PER_FRAME_CB, perFrameBuffer, ConstantBufferType.PixelShader);
            renderService.RegisterConstantBuffer(Constants.ConstantBufferNames.POINT_LIGHT_CB, PointLightBuffer, ConstantBufferType.PixelShader);
            renderService.RegisterConstantBuffer(Constants.ConstantBufferNames.DIRECTIONAL_LIGHT_CB, DirectionalLightBuffer, ConstantBufferType.PixelShader);
            renderService.RegisterConstantBuffer(Constants.ConstantBufferNames.PER_OBJECT_CB, perObjectBuffer, ConstantBufferType.VertexShader);

            renderForm.Show();
        }