|
|
@@ -18,6 +18,9 @@ bool D3DBox::Initialize()
|
|
|
if (!D3DApp::Initialize())
|
|
|
return false;
|
|
|
|
|
|
+ // Reset the command list to prep for initialization commands.
|
|
|
+ ThrowIfFailed(mCommandList->Reset(mCommandAllocator.Get(), nullptr));
|
|
|
+
|
|
|
BuildDescriptorHeaps();
|
|
|
BuildConstantBuffers();
|
|
|
BuildRootSignature();
|
|
|
@@ -25,21 +28,88 @@ bool D3DBox::Initialize()
|
|
|
BuildBoxGeometry();
|
|
|
BuildPSO();
|
|
|
|
|
|
+ // Execute the initialization commands.
|
|
|
+ ThrowIfFailed(mCommandList->Close());
|
|
|
+ ID3D12CommandList* cmdsLists[] = { mCommandList.Get() };
|
|
|
+ mCommandQueue->ExecuteCommandLists(_countof(cmdsLists), cmdsLists);
|
|
|
+
|
|
|
+ // Wait until initialization is complete.
|
|
|
+ FlushCommandQueue();
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
void D3DBox::OnResize()
|
|
|
{
|
|
|
D3DApp::OnResize();
|
|
|
+
|
|
|
+ XMMATRIX P = XMMatrixPerspectiveFovLH(0.25f * MathHelper::Pi, AspectRatio(), 1.0f, 1000.0f);
|
|
|
+ XMStoreFloat4x4(&mProj, P);
|
|
|
}
|
|
|
|
|
|
void D3DBox::Update(const GameTimer& gt)
|
|
|
-{
|
|
|
-
|
|
|
+{
|
|
|
+ float x = mRadius * sinf(mPhi) * cosf(mTheta);
|
|
|
+ float z = mRadius * sinf(mPhi) * sinf(mTheta);
|
|
|
+ float y = mRadius * cosf(mPhi);
|
|
|
+
|
|
|
+ // Build the view matrix.
|
|
|
+ XMVECTOR pos = XMVectorSet(x, y, z, 1.0f);
|
|
|
+ XMVECTOR target = XMVectorZero();
|
|
|
+ XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
|
|
|
+
|
|
|
+ XMMATRIX view = XMMatrixLookAtLH(pos, target, up);
|
|
|
+ XMStoreFloat4x4(&mView, view);
|
|
|
+
|
|
|
+ XMMATRIX world = XMLoadFloat4x4(&mWorld);
|
|
|
+ XMMATRIX proj = XMLoadFloat4x4(&mProj);
|
|
|
+ XMMATRIX worldViewProj = world * view * proj;
|
|
|
+
|
|
|
+ ObjectConstants objConstants;
|
|
|
+ XMStoreFloat4x4(&objConstants.WorldViewProj, XMMatrixTranspose(worldViewProj));
|
|
|
+ mObjectCB->CopyData(0, objConstants);
|
|
|
}
|
|
|
|
|
|
void D3DBox::Draw(const GameTimer& gt)
|
|
|
{
|
|
|
+ ThrowIfFailed(mCommandAllocator->Reset());
|
|
|
+
|
|
|
+ ThrowIfFailed(mCommandList->Reset(mCommandAllocator.Get(), mPSO.Get()));
|
|
|
+
|
|
|
+ mCommandList->RSSetViewports(1, &mScreenViewport);
|
|
|
+ mCommandList->RSSetScissorRects(1, &mScissorRect);
|
|
|
+
|
|
|
+ mCommandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(CurrentBackBuffer(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
|
|
|
+
|
|
|
+ mCommandList->ClearRenderTargetView(CurrentBackBufferView(), Colors::LightSteelBlue, 0, nullptr);
|
|
|
+ mCommandList->ClearDepthStencilView(DepthStencilView(), D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 1.0f, 0, 0, nullptr);
|
|
|
+
|
|
|
+ mCommandList->OMSetRenderTargets(1, &CurrentBackBufferView(), true, &DepthStencilView());
|
|
|
+
|
|
|
+ ID3D12DescriptorHeap* descriptorHeaps[] = { mCbvHeap.Get() };
|
|
|
+ mCommandList->SetDescriptorHeaps(_countof(descriptorHeaps), descriptorHeaps);
|
|
|
+
|
|
|
+ mCommandList->SetGraphicsRootSignature(mRootSignature.Get());
|
|
|
+
|
|
|
+ mCommandList->IASetVertexBuffers(0, 1, &mBoxGeo->VertexBufferView());
|
|
|
+ mCommandList->IASetIndexBuffer(&mBoxGeo->IndexBufferView());
|
|
|
+ mCommandList->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
|
|
+
|
|
|
+ mCommandList->SetGraphicsRootDescriptorTable(0, mCbvHeap->GetGPUDescriptorHandleForHeapStart());
|
|
|
+
|
|
|
+ mCommandList->DrawIndexedInstanced(mBoxGeo->DrawArgs["box"].IndexCount, 1, 0, 0, 0);
|
|
|
+
|
|
|
+ mCommandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(CurrentBackBuffer(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));
|
|
|
+
|
|
|
+ ThrowIfFailed(mCommandList->Close());
|
|
|
+
|
|
|
+ ID3D12CommandList* cmdsLists[] = { mCommandList.Get() };
|
|
|
+ mCommandQueue->ExecuteCommandLists(_countof(cmdsLists), cmdsLists);
|
|
|
+
|
|
|
+ ThrowIfFailed(mSwapChain->Present(0, 0));
|
|
|
+ mCurrentBackBuffer = (mCurrentBackBuffer + 1) % SwapChainBufferCount;
|
|
|
+
|
|
|
+ FlushCommandQueue();
|
|
|
}
|
|
|
|
|
|
void D3DBox::OnMouseDown(WPARAM btnState, int x, int y)
|
|
|
@@ -57,10 +127,10 @@ void D3DBox::OnMouseMove(WPARAM btnStae, int x, int y)
|
|
|
void D3DBox::BuildDescriptorHeaps()
|
|
|
{
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC cbvHeapDesc;
|
|
|
- cbvHeapDesc.NodeMask = 0;
|
|
|
cbvHeapDesc.NumDescriptors = 1;
|
|
|
- cbvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
|
|
|
cbvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
|
|
|
+ cbvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
|
|
|
+ cbvHeapDesc.NodeMask = 0;
|
|
|
md3dDevice->CreateDescriptorHeap(&cbvHeapDesc, IID_PPV_ARGS(&mCbvHeap));
|
|
|
}
|
|
|
|
|
|
@@ -124,24 +194,30 @@ void D3DBox::BuildShaderAndInputLayout()
|
|
|
void D3DBox::BuildPSO()
|
|
|
{
|
|
|
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc;
|
|
|
-
|
|
|
ZeroMemory(&psoDesc, sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC));
|
|
|
psoDesc.InputLayout = { mInputLayout.data(), (UINT)mInputLayout.size() };
|
|
|
psoDesc.pRootSignature = mRootSignature.Get();
|
|
|
- psoDesc.VS = { mVSByteCode->GetBufferPointer(), mVSByteCode->GetBufferSize() };
|
|
|
- psoDesc.PS = { mPSByteCode->GetBufferPointer(), mPSByteCode->GetBufferSize() };
|
|
|
+ psoDesc.VS =
|
|
|
+ {
|
|
|
+ reinterpret_cast<BYTE*>(mVSByteCode->GetBufferPointer()),
|
|
|
+ mVSByteCode->GetBufferSize()
|
|
|
+ };
|
|
|
+ psoDesc.PS =
|
|
|
+ {
|
|
|
+ reinterpret_cast<BYTE*>(mPSByteCode->GetBufferPointer()),
|
|
|
+ mPSByteCode->GetBufferSize()
|
|
|
+ };
|
|
|
psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
|
|
|
psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
|
|
|
psoDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
|
|
|
- psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
|
|
psoDesc.SampleMask = UINT_MAX;
|
|
|
+ psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
|
|
psoDesc.NumRenderTargets = 1;
|
|
|
psoDesc.RTVFormats[0] = mBackBufferFormat;
|
|
|
psoDesc.SampleDesc.Count = m4xMsaaState ? 4 : 1;
|
|
|
psoDesc.SampleDesc.Quality = m4xMsaaState ? (m4xMsaaQuality - 1) : 0;
|
|
|
psoDesc.DSVFormat = mDepthStencilFormat;
|
|
|
-
|
|
|
- md3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&mPSO));
|
|
|
+ ThrowIfFailed(md3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&mPSO)));
|
|
|
}
|
|
|
|
|
|
void D3DBox::BuildBoxGeometry()
|
|
|
@@ -190,6 +266,30 @@ void D3DBox::BuildBoxGeometry()
|
|
|
|
|
|
mBoxGeo = std::make_unique<MeshGeometry>();
|
|
|
mBoxGeo->Name = "boxGeo";
|
|
|
+
|
|
|
+ ThrowIfFailed(D3DCreateBlob(vbByteSize, &mBoxGeo->VertexBufferCPU));
|
|
|
+ CopyMemory(mBoxGeo->VertexBufferCPU->GetBufferPointer(), vertices.data(), vbByteSize);
|
|
|
+
|
|
|
+ ThrowIfFailed(D3DCreateBlob(ibByteSize, &mBoxGeo->IndexBufferCPU));
|
|
|
+ CopyMemory(mBoxGeo->IndexBufferCPU->GetBufferPointer(), indices.data(), ibByteSize);
|
|
|
+
|
|
|
+ mBoxGeo->VertexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(),
|
|
|
+ mCommandList.Get(), vertices.data(), vbByteSize, mBoxGeo->VertexBufferUploader);
|
|
|
+
|
|
|
+ mBoxGeo->IndexBufferGPU = d3dUtil::CreateDefaultBuffer(md3dDevice.Get(),
|
|
|
+ mCommandList.Get(), indices.data(), ibByteSize, mBoxGeo->IndexBufferUploader);
|
|
|
+
|
|
|
+ mBoxGeo->VertexByteStride = sizeof(Vertex);
|
|
|
+ mBoxGeo->VertexBufferByteSize = vbByteSize;
|
|
|
+ mBoxGeo->IndexFormat = DXGI_FORMAT_R16_UINT;
|
|
|
+ mBoxGeo->IndexBufferByteSize = ibByteSize;
|
|
|
+
|
|
|
+ SubmeshGeometry submesh;
|
|
|
+ submesh.IndexCount = (UINT)indices.size();
|
|
|
+ submesh.StartIndexLocation = 0;
|
|
|
+ submesh.BaseVertexLocation = 0;
|
|
|
+
|
|
|
+ mBoxGeo->DrawArgs["box"] = submesh;
|
|
|
}
|
|
|
|
|
|
|