//RootSignature是root Parameters的数组 // A root signature is an array of root parameters. CD3DX12_ROOT_SIGNATURE_DESC rootSigDesc(2, slotRootParameter, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
//RootSignature也需要序列化,传递到GPU和CPU的映射中 // create a root signature with a single slot which points to a descriptor range consisting of a single constant buffer ComPtr<ID3DBlob> serializedRootSig = nullptr; ComPtr<ID3DBlob> errorBlob = nullptr; HRESULT hr = D3D12SerializeRootSignature(&rootSigDesc, D3D_ROOT_SIGNATURE_VERSION_1, serializedRootSig.GetAddressOf(), errorBlob.GetAddressOf());
// Need a CBV descriptor for each object for each frame resource. for (int frameIndex = 0; frameIndex < gNumFrameResources; ++frameIndex) { auto objectCB = mFrameResources[frameIndex]->ObjectCB->Resource(); for (UINT i = 0; i < objCount; ++i) { D3D12_GPU_VIRTUAL_ADDRESS cbAddress = objectCB->GetGPUVirtualAddress();
// Offset to the ith object constant buffer in the buffer. cbAddress += i * objCBByteSize;
// Offset to the object cbv in the descriptor heap. int heapIndex = frameIndex * objCount + i; auto handle = CD3DX12_CPU_DESCRIPTOR_HANDLE(mCbvHeap->GetCPUDescriptorHandleForHeapStart()); handle.Offset(heapIndex, mCbvSrvUavDescriptorSize);
// Last three descriptors are the pass CBVs for each frame resource. for (int frameIndex = 0; frameIndex < gNumFrameResources; ++frameIndex) { auto passCB = mFrameResources[frameIndex]->PassCB->Resource(); D3D12_GPU_VIRTUAL_ADDRESS cbAddress = passCB->GetGPUVirtualAddress();
// Offset to the pass cbv in the descriptor heap. int heapIndex = mPassCbvOffset + frameIndex; auto handle = CD3DX12_CPU_DESCRIPTOR_HANDLE(mCbvHeap->GetCPUDescriptorHandleForHeapStart()); handle.Offset(heapIndex, mCbvSrvUavDescriptorSize);
voidShapesApp::UpdateObjectCBs(const GameTimer& gt) { auto currObjectCB = mCurrFrameResource->ObjectCB.get(); for (auto& e : mAllRitems) { //每个Object都有一个ObjectConstant Buffer // Only update the cbuffer data if the constants have changed. // This needs to be tracked per frame resource. //如果发生变化(标记为脏数据,则更新对应Obj的Cb) if (e->NumFramesDirty > 0) { XMMATRIX world = XMLoadFloat4x4(&e->World);
//在ObjectConstantBuffer Heap中 // 通过偏移找到当前Object的ConstantBuffer View的Index // Offset to the CBV in the descriptor heap for this object and for this frame resource. UINT cbvIndex = mCurrFrameResourceIndex * (UINT)mOpaqueRitems.size() + ri->ObjCBIndex; auto cbvHandle = CD3DX12_GPU_DESCRIPTOR_HANDLE(mCbvHeap->GetGPUDescriptorHandleForHeapStart()); cbvHandle.Offset(cbvIndex, mCbvSrvUavDescriptorSize);