Hi there!

I'm a bit wary of asking this question as it's a bit drawn out and might make me look stupid. But I'll do it anyway

I have recently come across a line of code in my DirectX12 stuff that got me puzzled. I'll write it as follows:

Code:
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHeapHandle(mRtvHeap->GetCPUDescriptorHandleForHeapStart());
	for (UINT i = 0; i < SwapChainBufferCount; i++)
	{
		ThrowIfFailed(mSwapChain->GetBuffer(i, IID_PPV_ARGS(&mSwapChainBuffer[i])));
		md3dDevice->CreateRenderTargetView(mSwapChainBuffer[i].Get(), nullptr, rtvHeapHandle);
		rtvHeapHandle.Offset(1, mRtvDescriptorSize);
	}
I've highlighted the part in bold. It's the mSwapChain->GetBuffer function. I know this place isn't graphics dedicated so I'll explain. This code takes an already made descriptor heap and makes the two resources in the swap chain render target views. It iterates through the two descriptors the swap chains uses and creates a render target view to them. It needs a descriptor handle to do this at the start (can't just use an array index). Fair enough I guess.

But.... what I didn't quite get is why this line was needed because it doesn't seem to really do anything:

Code:
ThrowIfFailed(mSwapChain->GetBuffer(i, IID_PPV_ARGS(&mSwapChainBuffer[i])));
If you look at the original iteration loop all it does is select a resource/buffer from the swap chain and set a render target view to it. Then it does it for the next one (maximum of 2).

This is where the ComPtr stuff comes in. If I do the following (comment out the GetBuffer() call):

Code:
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHeapHandle(mRtvHeap->GetCPUDescriptorHandleForHeapStart());
	for (UINT i = 0; i < SwapChainBufferCount; i++)
	{
		//ThrowIfFailed(mSwapChain->GetBuffer(i, IID_PPV_ARGS(&mSwapChainBuffer[i])));
		md3dDevice->CreateRenderTargetView(mSwapChainBuffer[i].Get(), nullptr, rtvHeapHandle);
		rtvHeapHandle.Offset(1, mRtvDescriptorSize);
	}
Then a little further down the lines this function fails:

Code:
ThrowIfFailed(md3dDevice->CreateCommittedResource(
        &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
		D3D12_HEAP_FLAG_NONE,
        &depthStencilDesc,
		D3D12_RESOURCE_STATE_COMMON,
        &optClear,
        IID_PPV_ARGS(mDepthStencilBuffer.GetAddressOf())));
Now to be clear where ComPtr stuff is concerned both md3dDevice and mDepthStencilBuffer are ComPtr's. So..... is the original reference to a ComPtr mSwapChain->GetBuffer (which I tried to comment out) making an increment of some kind to the ComPtr which then goes out of scope after the iteration loop, which then allows the further two ComPtr variables to behave normally? Have I screwed up this mechanism by trying to comment out mSwapChain->GetBuffer in the iteration loop?

I hope I've presented that well and good enough to warrant a reply. Please be gentle though, thanks