EKsumic's Blog

let today = new Beginning();

Click the left button to use the catalog.

OR

从零开始的SharpDX(二)——初始化DirectX并修改颜色

上一篇:从零开始的SharpDX(一)——创建SharpDX窗口

完整代码:

using System;
using System.Drawing;
using SharpDX.Direct3D;
using SharpDX.DXGI;
using SharpDX.Mathematics.Interop;
using SharpDX.Windows;
using D3D11 = SharpDX.Direct3D11;

namespace CreateWindow
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var temp = new CreateSharpDXWindow())
            {
                temp.Run();
            }
        }
    }

    class CreateSharpDXWindow : IDisposable
    {
        public CreateSharpDXWindow()
        {
            _renderForm = new RenderForm();
            _renderForm.ClientSize = new Size(Width, Height);
            //自己写的方法
            InitializeDeviceResources();
        }

        private const int Width = 1280;

        private const int Height = 720;

        public void Run()
        {
            RenderLoop.Run(_renderForm, RenderCallback);
        }

        private RenderForm _renderForm;

        private D3D11.Device _d3DDevice;
        private D3D11.DeviceContext _d3DDeviceContext;
        private SwapChain _swapChain;
        private D3D11.RenderTargetView _renderTargetView;

        private void RenderCallback()
        {
            Draw();
        }

        private void InitializeDeviceResources()
        {
            //模式描述,参数1,参数2,是大小,参数3是刷新率,表示60Hz,最后一个参数是像素格式
            ModeDescription backBufferDesc =
                new ModeDescription(Width, Height, new Rational(60, 1), Format.R8G8B8A8_UNorm);
            //定义交换链 - SharpDX程序必写部分
            SwapChainDescription swapChainDesc = new SwapChainDescription()
            {
                //模式描述
                ModeDescription = backBufferDesc,
                //优化图片渲染。参数1,表示指定每个像素的采样数量;参数2,表示质量级别
                SampleDescription = new SampleDescription(1, 0),
                //设置CPU访问缓冲的权限
                Usage = Usage.RenderTargetOutput,
                //BufferCount=1表示双缓冲
                BufferCount = 1,
                //获取所要渲染窗口的句柄
                OutputHandle = _renderForm.Handle,
                //窗口化
                IsWindowed = true
            };
            //创建交换链 - 参数1,表示希望使用GPU渲染;参数2,选不使用特殊的方法;参数3,输入已定义的交换链描述;参数4,参数5,是输出的交换链和设备
            D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, swapChainDesc,
                out _d3DDevice, out _swapChain);
            //描述设备如何绘制的渲染设备上下文
            _d3DDeviceContext = _d3DDevice.ImmediateContext;
            //获得纹理并加入缓冲区
            using (D3D11.Texture2D backBuffer = _swapChain.GetBackBuffer<D3D11.Texture2D>(0))
            {
                //渲染目标视图
                _renderTargetView = new D3D11.RenderTargetView(_d3DDevice, backBuffer);
            }
        }

        private void Draw()
        {
            //获得刚才得到的 - 渲染目标视图,并开始渲染
            _d3DDeviceContext.OutputMerger.SetRenderTargets(_renderTargetView);
            //清理_renderTargetView,并设置颜色
            _d3DDeviceContext.ClearRenderTargetView(_renderTargetView, ColorToRaw4(Color.AliceBlue));
            //等待垂直同步,参数1是同步间隔,参数2是演示的标识
            _swapChain.Present(1, PresentFlags.None);
            //创建原生颜色,并设置RGBA,默认255
            RawColor4 ColorToRaw4(Color color)
            {
                const float n = 255f;
                return new RawColor4(color.R / n, color.G / n, color.B / n, color.A / n);
            }
        }

        //释放资源
        public void Dispose()
        {
            _renderTargetView.Dispose();
            _swapChain.Dispose();
            _d3DDevice.Dispose();
            _d3DDeviceContext.Dispose();
            _renderForm?.Dispose();
        }
    }
}

下一篇:从零开始的SharpDX(三)——画三角形

This article was last edited at 2020-07-01 18:12:13

* *