我对 D3D12 编程非常陌生,刚刚开始设置一个小虚拟项目来掌握一般过程。
但是,我被困住了。
目前,在 PIX 中调试我的小程序时,我可以确认顶点着色器正在正常运行,但像素着色器没有被调用。
我尝试检查我的剪刀、视口和深度缓冲区,以确认顶点没有被剪切,但我还没有设法解决问题。
有人知道要在这里检查什么吗?
提前谢谢您!
一些我认为相关的代码片段:
(光栅化器状态、DepthStencil 状态、交换链创建、RTV 创建)
D3D12_RASTERIZER_DESC rasterizer{};
rasterizer.FillMode = D3D12_FILL_MODE_SOLID;
rasterizer.CullMode = D3D12_CULL_MODE_NONE;
rasterizer.FrontCounterClockwise = FALSE;
rasterizer.DepthClipEnable = TRUE;
rasterizer.MultisampleEnable = FALSE;
rasterizer.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
pipelineDesc.RasterizerState = rasterizer;
D3D12_DEPTH_STENCIL_DESC depthStencil{};
depthStencil.DepthEnable = TRUE;
depthStencil.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
depthStencil.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
depthStencil.StencilEnable = FALSE;
depthStencil.FrontFace.StencilFailOp = D3D12_STENCIL_OP_KEEP;
depthStencil.BackFace.StencilFailOp = D3D12_STENCIL_OP_KEEP;
depthStencil.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
depthStencil.BackFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
depthStencil.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
depthStencil.BackFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
DXGI_SWAP_CHAIN_DESC1 swapchainDescription{};
DXGI_SWAP_CHAIN_FULLSCREEN_DESC fullscreenDescription{};
swapchainDescription.Width = 0;
swapchainDescription.Height = 0;
swapchainDescription.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
swapchainDescription.Stereo = FALSE;
swapchainDescription.SampleDesc = { 1, 0 };
swapchainDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapchainDescription.BufferCount = 2;
swapchainDescription.Scaling = DXGI_SCALING_NONE;
swapchainDescription.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapchainDescription.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
swapchainDescription.Flags = 0;
fullscreenDescription.RefreshRate = { 1, 60 };
fullscreenDescription.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE;
fullscreenDescription.Scaling = DXGI_MODE_SCALING_CENTERED;
fullscreenDescription.Windowed = TRUE;
CheckHresult(factory->CreateSwapChainForHwnd(commandQueue, hwnd, &swapchainDescription, &fullscreenDescription, NULL, &t_swapchain));
HRESULT sres = t_swapchain->QueryInterface(IID_PPV_ARGS(&swapchain));
D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapInfoRtv{};
descriptorHeapInfoRtv.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
descriptorHeapInfoRtv.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
descriptorHeapInfoRtv.NumDescriptors = 2;
descriptorHeapInfoRtv.NodeMask = 0;
device->CreateDescriptorHeap(&descriptorHeapInfoRtv, IID_PPV_ARGS(&rtv_descriptorHeap.descriptorHeap));
rtv_descriptorHeap.incrementSize = device->GetDescriptorHandleIncrementSize(descriptorHeapInfoRtv.Type);
rtv_descriptorHeap.startHandle = rtv_descriptorHeap.descriptorHeap->GetCPUDescriptorHandleForHeapStart();
device->CreateRenderTargetView(swapchainBuffers[0], NULL, rtv_descriptorHeap.startHandle);
device->CreateRenderTargetView(swapchainBuffers[1], NULL, rtv_descriptorHeap.AddOffset(1));
着色器代码:
// Vertex Shader
struct VS_IN
{
float4 pos : POSITION;
};
struct VS_OUT
{
float4 pos : SV_Position;
};
VS_OUT main(VS_IN inp)
{
VS_OUT outp;
outp.pos = float4(inp.pos.x, inp.pos.y, inp.pos.z, 1.0f);
return outp;
}
// Pixel Shader
struct PS_OUT
{
float4 col_in : COLOR;
};
struct VS_OUT
{
float4 pos : SV_Position;
};
float4 main(VS_OUT inp) : SV_Target
{
PS_OUT outp;
outp.col_in = float4(1.0f, 1.0f, 1.0f, 1.0f);
return outp.col_in;
}
我尝试过使用默认设置(在设备->CreateRenderTargetView 中传递 NULL)并传递自定义描述。我尝试禁用深度测试,并更改管道状态样本掩码。
发现问题:我调用了commandList->ClearDepthStencilView(dsv_descriptorHeap.startHandle, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 1.0f, 0, 0, &rect); 1.0f、0、0 参数的顺序错误。我将它们设置为 0, 1, 0,从而产生将深度缓冲区清除为错误值的效果,可能导致我的像素无法通过深度测试。