如何正确使用DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL?

时间:2018-11-07 18:47:21

标签: directx directx-11

在窗口上绘制立方体时出现问题。我在窗口上看不到任何图形。我在Visual Studio中看到以下警告消息:

D3D11 WARNING: ID3D11DeviceContext::DrawIndexed: The Pixel Shader expects a Render Target View bound to slot 0, but the Render Target View was unbound during a call to Present. A successful Present call for DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL SwapChains unbinds backbuffer 0 from all GPU writeable bind points. [ EXECUTION WARNING #3146082:

D3D11 WARNING: ID3D11DeviceContext::DrawIndexed: The Pixel Shader expects a Render Target View bound to slot 0, but none is bound. This is OK, as writes of an unbound Render Target View are discarded. It is also possible the developer knows the data will not be used anyway. This is only a problem if the developer actually intended to bind a Render Target View here. [ EXECUTION WARNING #3146081: DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET]

我使用以下API创建了swapchain

virtual IDXGISwapChain* SwapChain(HWND wnd)
{
HRESULT hr = S_OK;
IDXGISwapChain* swapchain = nullptr;

DXGI_SWAP_CHAIN_DESC desc;
ZeroMemory(&desc, sizeof(DXGI_SWAP_CHAIN_DESC));
desc.Windowed = TRUE; // Sets the initial state of full-screen mode.
desc.BufferCount = 2;
desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
desc.SampleDesc.Count = 1;      //multisampling setting
desc.SampleDesc.Quality = 0;    //vendor-specific flag
desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
desc.OutputWindow = wnd;

// Create the DXGI device object to use in other factories, such as Direct2D.
IDXGIDevice3* dxgiDevice;
hr = device_->QueryInterface(__uuidof(IDXGIDevice3), reinterpret_cast<void**>(&dxgiDevice));
if (FAILED(hr))
  return nullptr;

// Create swap chain.
IDXGIAdapter* adapter;
IDXGIFactory* factory;

hr = dxgiDevice->GetAdapter(&adapter);
dxgiDevice->Release();
if (FAILED(hr))
  return nullptr;

adapter->GetParent(IID_PPV_ARGS(&factory));
hr = factory->CreateSwapChain(device_, &desc, &swapchain);
adapter->Release();
factory->Release();
return swapchain;
}

渲染目标是通过调用绑定的:

m_d3dDevice.Context()->OMSetRenderTargets(1, &m_pRenderTarget, , _pDepthStencilView);

Present的实现方式为:

swap_chain->Present(0, 0);

着色器代码为:

cbuffer ConstantBuffer : register(b0)
{
  matrix World;
  matrix View;
  matrix Projection;
  float4 vLightDir[2];
  float4 vLightColor[2];
  float4 vOutputColor;
}

struct VS_INPUT
{
  float4 Pos : POSITION;
  float3 Norm : NORMAL;
};

struct PS_INPUT
{
  float4 Pos : SV_POSITION;
  float3 Norm : TEXCOORD0;
};


PS_INPUT VS(VS_INPUT input)
{
  PS_INPUT output = (PS_INPUT)0;
  output.Pos = mul(input.Pos, World);
  output.Pos = mul(output.Pos, View);
  output.Pos = mul(output.Pos, Projection);
  output.Norm = mul(float4(input.Norm, 1), World).xyz;

  return output;
}

float4 PS(PS_INPUT input) : SV_Target
{
    float4 finalColor = 0;

    //do NdotL lighting for 2 lights
    for (int i = 0; i < 2; i++)
    {
        finalColor += saturate(dot((float3)vLightDir[i],input.Norm) * vLightColor[i]);
    }
    finalColor.a = 1;
    return finalColor;
}


float4 PSSolid(PS_INPUT input) : SV_Target
{
    return vOutputColor;
}

1 个答案:

答案 0 :(得分:0)

在每次渲染之前调用ID3D11DeviceContext :: OMSetRenderTargets(...)

// Add this before each rendering
spImCtx->OMSetRenderTargets(1, spRTV.GetAddressOf(), spZView.Get());

// clear 
spImCtx->ClearRenderTargetView(spRTV.Get(), Colors::Black);
spImCtx->ClearDepthStencilView(spZView.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0);
// drawing...

// swap
spSwapChain->Present(1, 0);