DirectX - 为CoreWindow

时间:2018-02-26 14:42:36

标签: c# uwp directx sharpdx

我目前正在尝试使用最新的SharpDX作为DirectX包装器和UWP作为项目基础框架为CoreWindow创建交换链。

关于这方面的文件非常稀少,令人难以置信。尽管如此,我还是可以找到一个看起来很有希望的片段。最初,我总是收到 E_INVALIDCALL 错误消息。现在只有"只有"的 E_ACCESSDENIED

到目前为止,我已经做了这个以建立链:

var description = new SwapChainDescription1
{
    BufferCount = 2,
    Flags = SwapChainFlags.None,
    SampleDescription = new SampleDescription(1, 0),
    SwapEffect = SwapEffect.FlipSequential,
    Usage = Usage.RenderTargetOutput,
    Width = 0,
    Height = 0,
    Scaling = Scaling.None,
    Format = Format.B8G8R8A8_UNorm,
    Stereo = false
};

CoreWindow window = CoreWindow.GetForCurrentThread();
if (window == null)
{
    Logging.Error("Could not retrieve core window for swap chain.");
    throw new Exception("Invalid core window.");
}

using (var device = _device.QueryInterface<SharpDX.DXGI.Device2>())
{
    device.MaximumFrameLatency = 1;
    using (Adapter adapter = device.Adapter)
    {
        using (ComObject coreWindow = new ComObject(window))
        {
            using (Factory2 factory = adapter.GetParent<Factory2>())
                _swapChain = new SwapChain1(factory, _device, coreWindow, ref description);
        }
    }
}

SwapChain1的构造函数抛出SharpDX异常:

  

SharpDX.Result.CheckError()   SharpDX.DXGI.Factory2.CreateSwapChainForCoreWindow(ComObject deviceRef,ComObject windowRef,SwapChainDescription1&amp; descRef,Output restrictToOutputRef,SwapChain1 swapChainOut)   SharpDX.DXGI.SwapChain1..ctor(Factory2工厂,ComObject设备,ComObject coreWindow,SwapChainDescription1&amp; description,输出restrictToOutput)   RobInspect.Visualizer.Rendering.RenderingPanel.InitializeSizeDependentResources()   RobInspect.Visualizer.Rendering.RenderingPanel.InitializeDevice()

     

&#34; HRESULT:[0x80070005],模块:[常规],ApiCode:[E_ACCESSDENIED /一般访问被拒绝错误],消息:访问被拒绝。   &#34;

任何人都可以解释为什么吗? &#34;访问被拒绝&#34;是一个非常广泛的声明,我对DirectX的内部经验并不熟悉。

更多信息:代码正在主(UI)线程上执行。所以我想我可以排除CoreWindow引用无法访问。由于这是第一次初始化,我还排除了在创建交换链之前不能正确释放DirectX对象的可能性。

修改: 这是创建设备的代码。而标志设置为 DeviceCreationFlags.BgraSuuport DeviceCreationFlags.Debug 。级别设置为 FeatureLevel.Level_11_1 ,直至 FeatureLevel.Level_9_1

using (var device = new Device(DriverType.Hardware, flags, levels))
{
    _device = device.QueryInterface<Device1>();
    _context = _device.ImmediateContext1;
}

1 个答案:

答案 0 :(得分:1)

此问题的解决方案是术语 WinRT核心 WinRT XAML 相当误导。由于UWP基于CoreWindow,并且都支持和使用它们,因此不清楚在哪里使用它。

DirectX为 WinRT 公开两种方法,为桌面公开一种方法。一个是Factory2.CreateSwapChainForCoreWindow(...),一个是Factory2.CreateSwapChainForComposition(...)。不同之处在于,将CoreWindow作为参数,而不是IFrameworkView。这就是我陷入的陷阱。

核心代表设计方案,其中只使用IFrameworkViewSourceWindows.UI.Xaml.Application(有关SharpDX的示例,请参阅here)而 XAML 代表您拥有...ForCoreWindow(...)类的传统方案。

使用Core-model时,必须调用SwapChainDescription1方法才能创建交换链。在使用基于XAML的方法时,您需要一个组合交换链。我自己已经试过了,但失败了,因为我忘了启用(提示:如果还没有这样做的话)本机调试,所以DirectX调试层实际上向我展示了基本信息,如果不是试用和错误的日子,这可以节省我几个小时。

这里的问题是组合和CoreWindow交换链都需要var data_module = function(){ var data; //Module stores the json data in a variable return{ //Returns an object that contains a public method accessible to external functions get_json:function(){ if(data){ //If data already exists, then return a Promise object that immediately resolves with data return Promise.resolve(data); } else{ //Else if data does not exist, make fetch request fetch('/rest/url/endpoint', {credentials:'include'}) .then(function(response){ if(!response.ok){ throw new Error(response.statusText); } return response.json(); //Returns json of response }) .then(function(json){ data = json; //Assigns data the value of json to store the result for subsequent requests return Promise.resolve(data) //Returns a Promise that resolves with data }); } } //Public method that is supposed to provide access to data } }(); //Module will automatically execute 中的特殊设置。我会留下你MSDN documentation。此外,如果启用了本机调试和调试层,DirectX将告诉您确切的设置无效。