国产精品免费嫩草研究院|无遮羞动漫在线观看AV|国产麻豆精品传媒AV国产在线|村在线观看|寂寞情人1正版|韩国床震韩国床震古|精品系列专区久久

DirectX 使用 Vortice 從零開始控制臺創(chuàng)建 Direct2D1 窗口修改顏色( 五 )

為了方便調試 , 這里就加上 ToList 讓所有代碼都執(zhí)行
var hardwareAdapter = GetHardwareAdapter(dxgiFactory2)// 這里 ToList 只是想列出所有的 IDXGIAdapter1 在實際代碼里 , 大部分都是獲取第一個.ToList().FirstOrDefault();if (hardwareAdapter == null){throw new InvalidOperationException("Cannot detect D3D11 adapter");}以上代碼即可獲取到顯示適配器接口用來進行后續(xù)的初始化
初始化 D3D 交換鏈在開始之前 , 按照 C# 從零開始寫 SharpDx 應用 聊聊功能等級 的方法 , 定義代碼
// 功能等級// [C# 從零開始寫 SharpDx 應用 聊聊功能等級](https://blog.lindexi.com/post/C-%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E5%86%99-SharpDx-%E5%BA%94%E7%94%A8-%E8%81%8A%E8%81%8A%E5%8A%9F%E8%83%BD%E7%AD%89%E7%BA%A7.html)D3D.FeatureLevel[] featureLevels = new[]{D3D.FeatureLevel.Level_11_1,D3D.FeatureLevel.Level_11_0,D3D.FeatureLevel.Level_10_1,D3D.FeatureLevel.Level_10_0,D3D.FeatureLevel.Level_9_3,D3D.FeatureLevel.Level_9_2,D3D.FeatureLevel.Level_9_1,};使用以上獲取的顯示適配器接口創(chuàng)建設備
DXGI.IDXGIAdapter1 adapter = hardwareAdapter;D3D11.DeviceCreationFlags creationFlags = D3D11.DeviceCreationFlags.BgraSupport;var result = D3D11.D3D11.D3D11CreateDevice(adapter,D3D.DriverType.Unknown,creationFlags,featureLevels,out D3D11.ID3D11Device d3D11Device, out D3D.FeatureLevel featureLevel,out D3D11.ID3D11DeviceContext d3D11DeviceContext);也許使用這個顯示適配器接口創(chuàng)建不出設備 , 通過判斷返回值即可了解是否成功 。創(chuàng)建失敗 , 那就不指定具體的參數 , 使用 WARP 的方法創(chuàng)建
if (result.Failure){// 如果失敗了 , 那就不指定顯卡 , 走 WARP 的方式// http://go.microsoft.com/fwlink/?LinkId=286690result = D3D11.D3D11.D3D11CreateDevice(IntPtr.Zero,D3D.DriverType.Warp,creationFlags,featureLevels,out d3D11Device, out featureLevel, out d3D11DeviceContext);// 如果失敗 , 就不能繼續(xù)result.CheckError();}以上代碼的 CheckError 方法 , 將會在失敗拋出異常
創(chuàng)建成功 , 可以獲取到 ID3D11Device 和 ID3D11DeviceContext 類型的對象和實際的功能等級 。這里的 ID3D11Device 就是 D3D 設備 , 提供給交換鏈綁定的功能 , 可以繪制到交換鏈的緩存里 , 從而被交換鏈刷新到屏幕上 。這里的 ID3D11DeviceContext 是包含了 D3D 設備的環(huán)境和配置 , 可以用來設置渲染狀態(tài)等
由于后續(xù)期望使用的是 ID3D11Device1 接口 , 按照慣例 , 從 d3D11Device 獲取
// 大部分情況下 , 用的是 ID3D11Device1 和 ID3D11DeviceContext1 類型// 從 ID3D11Device 轉換為 ID3D11Device1 類型var d3D11Device1 = d3D11Device.QueryInterface<D3D11.ID3D11Device1>();理論上當前能運行 dotnet 6 的 Windows 系統(tǒng) , 都是支持 ID3D11Device1 的
同理 , 獲取 ID3D11DeviceContext1 接口
var d3D11DeviceContext1 = d3D11DeviceContext.QueryInterface<D3D11.ID3D11DeviceContext1>();獲取到了新的兩個接口 , 就可以減少 d3D11Deviced3D11DeviceContext 的引用計數 。調用 Dispose 不會釋放掉剛才申請的 D3D 資源 , 只是減少引用計數
d3D11Device.Dispose();d3D11DeviceContext.Dispose();創(chuàng)建設備完成之后 , 接下來就是創(chuàng)建交換鏈和關聯窗口 。創(chuàng)建交換鏈需要很多參數 , 在 DX 的設計上 , 將參數放入到 SwapChainDescription 類型里面 。和 DX 的接口設計一樣 , 也有多個 SwapChainDescription 版本
創(chuàng)建 SwapChainDescription1 參數的代碼如下
// 顏色格式 , 如果后續(xù)準備接入 WPF 那推薦使用此格式DXGI.Format colorFormat = DXGI.Format.B8G8R8A8_UNorm;// 緩存的數量 , 包括前緩存 。大部分應用來說 , 至少需要兩個緩存 , 這個玩過游戲的伙伴都知道const int FrameCount = 2;DXGI.SwapChainDescription1 swapChainDescription = new(){Width = clientSize.Width,Height = clientSize.Height,Format = colorFormat,BufferCount = FrameCount,BufferUsage = DXGI.Usage.RenderTargetOutput,SampleDescription = DXGI.SampleDescription.Default,Scaling = DXGI.Scaling.Stretch,SwapEffect = DXGI.SwapEffect.FlipDiscard,AlphaMode = AlphaMode.Ignore};

經驗總結擴展閱讀