使用D3D12加载FBX模型时,总是获得一个空的窗口,在PIX分析大致确定问题出在光栅化阶段(顶点着色器输出正常,像素着色器只是返回CB中的颜色,应该没问题),PSO创建与核心渲染代码如下:
vertexShader = D3DTools::compileShaderFromFile(L"shaders.hlsl", "VS", "vs_5_0");
pixelShader = D3DTools::compileShaderFromFile(L"shaders.hlsl", "PS", "ps_5_0");
D3D12_GRAPHICS_PIPELINE_STATE_DESC desc = { 0 };
desc.pRootSignature = rootSignature.Get();
desc.VS = {
reinterpret_cast<BYTE*>(vertexShader->GetBufferPointer()),
vertexShader->GetBufferSize()
};
desc.PS = {
reinterpret_cast<BYTE*>(pixelShader->GetBufferPointer()),
pixelShader->GetBufferSize()
};
std::vector<D3D12_INPUT_ELEMENT_DESC> inputLayout = {
{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,
D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,0},
};
desc.InputLayout = { inputLayout.data(), static_cast<UINT>(inputLayout.size()) };
desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
desc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
desc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
desc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
desc.DepthStencilState.DepthEnable = FALSE;
desc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
desc.SampleMask = UINT_MAX;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.NumRenderTargets = 1;
desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
HRESULT hr = device->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&PSO));
if (FAILED(hr))
{
MessageBox(0, L"ERROR: Cannot Create Pipeline State!", L"DXLP", MB_ICONERROR);
exit(-1);
}
commandList->SetPipelineState(PSO.Get());
void MainGameWindow::Update()
{
// Update the game state
// Update the camera position and target
D3DTools::ConstantPerPass cp;
XMMATRIX view, proj;
view = XMMatrixLookAtLH(XMLoadFloat3(&cameraPos), XMLoadFloat3(&cameraTarget), XMLoadFloat3(&cameraUp));
proj = XMMatrixPerspectiveFovLH(XMConvertToRadians(45.0f), aspectRatio, 1.0f, 1000.0f);
XMMATRIX viewProj = XMMatrixMultiply(proj, view);
XMStoreFloat4x4(&cp.viewPorj, viewProj);
/*std::cout << "view: " << view.r[0].m128_f32[0] << " ..." << std::endl;
std::cout << "proj: " << proj.r[0].m128_f32[0] << " ..." << std::endl;*/
frameResources[CurrFrameResourceIndex]->PassCB->CopyData(0, cp);
// Update the constant buffer for each render item
for (int i = 0; i < renderItems.size(); i++)
{
if (renderItems[i]->NumFramesDirty) {
D3DTools::ConstantPerObject co;
XMStoreFloat4(&co.color, XMVectorSet(1.0, 1.0, 1.0, 1));
co.world = XMMatrixIdentity();
frameResources[CurrFrameResourceIndex]->ObjectCB->CopyData(i, co);
renderItems[i]->NumFramesDirty--;
}
}
}
void MainGameWindow::Render()
{
//使用Direct3D 12渲染游戏内容
CurrFrameResourceIndex = (CurrFrameResourceIndex + 1) % numFrameResources;
if (frameResources[CurrFrameResourceIndex]->Fence != 0
&& fence->GetCompletedValue() < currFR->Fence)
{
HANDLE eventHandle = CreateEventEx(nullptr, nullptr, 0, EVENT_ALL_ACCESS);
fence->SetEventOnCompletion(currFR->Fence, eventHandle);
WaitForSingleObject(eventHandle, INFINITE);
CloseHandle(eventHandle);
}
auto commandAllocator = frameResources[CurrFrameResourceIndex]->commandAllocator;
commandAllocator->Reset();
commandList->Reset(commandAllocator.Get(), PSO.Get());
//for (int i = 0; i < renderItems.size(); i++)
//{
// if (renderItems[i]->NumFramesDirty) {
// D3DTools::ConstantPerObject co;
// XMStoreFloat4(&co.color, XMVectorSet(0, 1, 0, 0));
// co.world = XMMatrixIdentity();
// frameResources[CurrFrameResourceIndex]->ObjectCB->CopyData(i, co);
// renderItems[i]->NumFramesDirty--;
// }
//}
Update();
w = h = 800;
CreateViewportAndScissorRect();
commandList->RSSetViewports(1, &vp);
commandList->RSSetScissorRects(1, &sciRect);
target = CD3DX12_RESOURCE_BARRIER::Transition(
CurrentBackBuffer(),
D3D12_RESOURCE_STATE_PRESENT,
D3D12_RESOURCE_STATE_RENDER_TARGET);
commandList->ResourceBarrier(1, &target);
FLOAT clear[] = { 0.0,0.0,0.0,1 };
commandList->ClearRenderTargetView(
CurrentBackBufferView(),
clear,
0,
nullptr);
commandList->ClearDepthStencilView(
DSVHeap->GetCPUDescriptorHandleForHeapStart(),
D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL,
1.0f, 0, 0, nullptr);
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = CurrentBackBufferView();
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = DSVHeap->GetCPUDescriptorHandleForHeapStart();
commandList->OMSetRenderTargets(1, &rtvHandle,
true, &dsvHandle);
ID3D12DescriptorHeap* heaps[] = { CBVHeap.Get() };
commandList->SetDescriptorHeaps(_countof(heaps), heaps);
commandList->SetGraphicsRootSignature(rootSignature.Get());
//commandList->SetGraphicsRootDescriptorTable(0,
// CBVHeap->GetGPUDescriptorHandleForHeapStart());
//D3D12_VERTEX_BUFFER_VIEW vbv = geo->GetVertexBufferView();
//D3D12_INDEX_BUFFER_VIEW ibv = geo->GetIndexBufferView();
//commandList->IASetVertexBuffers(0, 1, &vbv);
//commandList->IASetIndexBuffer(&ibv);
//commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//commandList->DrawIndexedInstanced(3, 1, 0, 0, 0);
auto pcbHandle = CD3DX12_GPU_DESCRIPTOR_HANDLE(
CBVHeap->GetGPUDescriptorHandleForHeapStart());
UINT passCBHeapIndex = numFrameResources * renderItems.size() + CurrFrameResourceIndex;
pcbHandle.Offset(passCBHeapIndex, cbvUavSize);
commandList->SetGraphicsRootDescriptorTable(1,
pcbHandle);
DrawRenderItems();
present = CD3DX12_RESOURCE_BARRIER::Transition(
CurrentBackBuffer(),
D3D12_RESOURCE_STATE_RENDER_TARGET,
D3D12_RESOURCE_STATE_PRESENT);
commandList->ResourceBarrier(1, &present);
commandList->Close();
ID3D12CommandList* cmdLists[] = { commandList.Get() };
commandQueue->ExecuteCommandLists(_countof(cmdLists), cmdLists);
swapChain->Present(0, 0);
CurrBackBuffer = (CurrBackBuffer + 1) % SwapChainBufferCount;
// FlushCommandQueue();
currFR->Fence = ++currFence;
commandQueue->Signal(fence.Get(), currFR->Fence);
}
void MainGameWindow::DrawRenderItems()
{
UINT objCBVSize = D3DTools::calcConstantBufferSize(sizeof(D3DTools::ConstantPerObject));
for(int i = 0; i < renderItems.size(); i++)
{
RenderItem* item = renderItems[i].get();
UINT cbvIndex = item->objCBIndex + renderItems.size() * CurrFrameResourceIndex;
auto objCBVHandle = CD3DX12_GPU_DESCRIPTOR_HANDLE(
CBVHeap->GetGPUDescriptorHandleForHeapStart());
objCBVHandle.Offset(cbvIndex, cbvUavSize);
commandList->SetGraphicsRootDescriptorTable(0, objCBVHandle);
D3D12_VERTEX_BUFFER_VIEW vbv = item->geometry->GetVertexBufferView();
D3D12_INDEX_BUFFER_VIEW ibv = item->geometry->GetIndexBufferView();
commandList->IASetVertexBuffers(0, 1, &vbv);
commandList->IASetIndexBuffer(&ibv);
commandList->IASetPrimitiveTopology(item->primitiveType);
commandList->DrawIndexedInstanced(item->indexCount, 1, item->startIndexLocation, item->baseVertexLocation, 0);
}
}
PIX几处关键图像:



然后获得一个全黑的窗口
希望好心人们帮忙解决一下!