2011-06-23 78 views
2

我遇到SlimDX March SDK问题(对于2010年6月的DXSDK11我相信)。问题是,只要我把深度视图附加到输出合并状态,我就不会在屏幕上得到任何输出。我将我的代码与DX11样本进行了比较,它似乎是正确的。我已经尝试了深度测试的各种标志和格式(包括总是通过等),但似乎没有任何工作。如果有人能发现一个错误,我会很感激。这是代码。以下是具体步骤:SlimDX 11深度缓冲区问题

  1. 初始化后缓冲区:

     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. 然后初始化深度缓存:

     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. 设置渲染目标:

    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(); 
    

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

+0

你看过DirectX提供的调试输出,看看是否列出了任何错误或警告?还可以尝试使用PIX来查看几何图形的确切情况。 – MikeP

+0

您正在初始化深度模板视图,完全像我一样。唯一不同的是我使用的格式是Format.D24_UNorm_S8_UInt。你可以试试看看它是否有效? 编辑:哦,我根本没有使用DepthStencilState。你能否尝试评论一下,看看问题出在哪里? –

回答

0

原来,在depthBufferDesc中,我将宽度传递给Height变量,将高度传递给Width。这是创建了两个不同维度的渲染目标,并将其分解。