SlimDX 11 深度缓冲区问题

发布于 2024-11-16 19:25:39 字数 2933 浏览 2 评论 0原文

我遇到了 SlimDX March SDK 的问题(我相信是 DXSDK11 June 2010)。问题是,每当我将深度视图附加到输出合并状态时,我在屏幕上都看不到任何输出。我将我的代码与 DX11 示例进行了比较,它似乎是正确的。我已经尝试了深度测试的各种标志和格式(包括始终通过等),但似乎没有任何效果。如果有人能发现错误,我将不胜感激。这是代码。步骤如下:

  1. 初始化后台缓冲区:

     D3DDevice 设备;
        交换链交换链;
    
        /// 创建交换链
        SwapChainDescription desc = new SwapChainDescription()
        {
            缓冲区计数 = 1,
            模式描述 = 新模式描述 
            { 
                宽度 = ContextSettings.Width, 
                高度 = ContextSettings.Height, 
                刷新率 = 新 SlimDX.Rational(ContextSettings.RefreshRate, 1), 
                格式 = ContextSettings.BufferFormat,
            },
            IsWindowed = !ContextSettings.FullScreen,
            输出句柄=窗口句柄,
            样本描述 = 新样本描述(1, 0),
            SwapEffect = SwapEffect.Discard,
            用法 = 用法.RenderTargetOutput,
        };
    
        FeatureLevel[] featureLevels = new FeatureLevel[] { FeatureLevel.Level_11_0, FeatureLevel.Level_10_1 };
        DriverType driverType = DriverType.硬件;
    
        D3DDevice.CreateWithSwapChain(driverType, DeviceCreationFlags.Debug, featureLevels, desc, out device, out swapChain);
    
        设备=设备;
        交换链=交换链;
    
        /// 设置窗口关联
        工厂工厂 = swapChain.GetParent();
        factory.SetWindowAssociation(WindowHandle, WindowAssociationFlags.IgnoreAll);
    
        /// 设置后台缓冲区并渲染目标视图
        RenderBuffer = DXTexture2D.FromSwapChain(swapChain, 0);
        RenderView = new RenderTargetView(Device, RenderBuffer);
    
  2. 然后初始化深度缓冲区:

     格式深度Format = Format.D32_Float;
        Texture2DDescription 深度BufferDesc = 新的Texture2DDescription 
        {
            数组大小 = 1,
            BindFlags = BindFlags.DepthStencil,
            CpuAccessFlags = CpuAccessFlags.None,
            格式=深度格式,
            高度=宽度,
            宽度=高度,
            Mip 级别 = 1,
            OptionFlags = ResourceOptionFlags.None,
            样本描述 = 新样本描述( 1, 0 ),
            用法 = ResourceUsage.Default
        };
    
        DepthBuffer = new DXTexture2D(Device, heightBufferDesc);
    
        DepthStencilViewDescription dsViewDesc = 新 DepthStencilViewDescription
        {
            数组大小 = 0,
            格式=深度格式,
            尺寸= DepthStencilViewDimension.Texture2D,
            Mip 切片 = 0,
            标志= 0,
            第一个数组切片 = 0
        };
    
        DepthView = new DepthStencilView(Device, DepthBuffer, dsViewDesc);
    
        DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription()
        {
            深度启用 = true,
            IsStencilEnabled = false,
            DepthWriteMask = DepthWriteMask.All,
            深度比较 = Comparison.Less,
        };
    
        DepthState = DepthStencilState.FromDescription(Device, dsStateDesc);
    
  3. 设置渲染目标:

     DeviceContext.OutputMerger.DepthStencilState = DepthState;
        DeviceContext.OutputMerger.SetTargets(DepthView, RenderView);
        DeviceContext.Rasterizer.SetViewports(new Viewport(0, 0, ContextSettings.Width, ContextSettings.Height, 0.0f, 1.0f));
    
        清除();
    

一旦我从 OutputMerger.SetTargets 中删除 DepthView,我就开始在屏幕上看到图像(当然没有深度测试),反之亦然。

I am getting an issue with SlimDX March SDK (For DXSDK11 June 2010 I believe). The problem is that whenever I turn the attach the depth view to the output merger state I don't get any output on the screen. I have compared my code with DX11 samples and it does seem to be correct. I have tried all sorts of flags and formats for the depth test (including always passing etc.) but nothing seems to work. I'd appreciate if anyone can spot a mistake. Here is the code. Here are the steps:

  1. Initialize the back buffer:

            D3DDevice device;
        SwapChain swapChain;
    
        /// Create the swap chain
        SwapChainDescription desc = new SwapChainDescription()
        {
            BufferCount = 1,
            ModeDescription = new ModeDescription 
            { 
                Width = ContextSettings.Width, 
                Height = ContextSettings.Height, 
                RefreshRate = new SlimDX.Rational(ContextSettings.RefreshRate, 1), 
                Format = ContextSettings.BufferFormat,
            },
            IsWindowed = !ContextSettings.FullScreen,
            OutputHandle = WindowHandle,
            SampleDescription = new SampleDescription(1, 0),
            SwapEffect = SwapEffect.Discard,
            Usage = Usage.RenderTargetOutput,
        };
    
        FeatureLevel[] featureLevels = new FeatureLevel[] { FeatureLevel.Level_11_0, FeatureLevel.Level_10_1 };
        DriverType driverType = DriverType.Hardware;
    
        D3DDevice.CreateWithSwapChain(driverType, DeviceCreationFlags.Debug, featureLevels, desc, out device, out swapChain);
    
        Device = device;
        SwapChain = swapChain;
    
        /// Setup window association
        Factory factory = swapChain.GetParent<Factory>();
        factory.SetWindowAssociation(WindowHandle, WindowAssociationFlags.IgnoreAll);
    
        /// Setup back buffers and render target views
        RenderBuffer = DXTexture2D.FromSwapChain<DXTexture2D>(swapChain, 0);
        RenderView = new RenderTargetView(Device, RenderBuffer);
    
  2. Then initialize the depth buffer:

            Format depthFormat = Format.D32_Float;
        Texture2DDescription depthBufferDesc = new Texture2DDescription 
        {
            ArraySize = 1,
            BindFlags = BindFlags.DepthStencil,
            CpuAccessFlags = CpuAccessFlags.None,
            Format = depthFormat,
            Height = width,
            Width = height,
            MipLevels = 1,
            OptionFlags = ResourceOptionFlags.None,
            SampleDescription = new SampleDescription( 1, 0 ),
            Usage = ResourceUsage.Default
        };
    
        DepthBuffer = new DXTexture2D(Device, depthBufferDesc);
    
        DepthStencilViewDescription dsViewDesc = new DepthStencilViewDescription
        {
            ArraySize = 0,
            Format = depthFormat,
            Dimension = DepthStencilViewDimension.Texture2D,
            MipSlice = 0,
            Flags = 0,
            FirstArraySlice = 0
        };
    
        DepthView = new DepthStencilView(Device, DepthBuffer, dsViewDesc);
    
        DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription()
        {
            IsDepthEnabled = true,
            IsStencilEnabled = false,
            DepthWriteMask = DepthWriteMask.All,
            DepthComparison = Comparison.Less,
        };
    
        DepthState = DepthStencilState.FromDescription(Device, dsStateDesc);
    
  3. Setup the render targets:

        DeviceContext.OutputMerger.DepthStencilState = DepthState;
        DeviceContext.OutputMerger.SetTargets(DepthView, RenderView);
        DeviceContext.Rasterizer.SetViewports(new Viewport(0, 0, ContextSettings.Width, ContextSettings.Height, 0.0f, 1.0f));
    
        Clear();
    

As soon as I remove DepthView from OutputMerger.SetTargets I start seeing images on the screen (without the depth test of course) and vice-versa otherwise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2024-11-23 19:25:39

事实证明,在 heightBufferDesc 中,我将宽度传递给 Height 变量,将高度传递给 Width。这会创建两个具有不同尺寸的渲染目标,从而破坏了它。

It turned out that in the depthBufferDesc, I was passing width to the Height variable and height to the Width. This was creating the two render targets with different dimensions which was breaking it down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文