开发者

SlimDX 11 Depth Buffer issues

开发者 https://www.devze.com 2023-03-14 20:26 出处:网络
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 s

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);
        Device开发者_如何学GoContext.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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消