diff options
| -rw-r--r-- | dxcommon/include/dxcommon.h | 13 | ||||
| -rw-r--r-- | hello/main.cc | 19 |
2 files changed, 15 insertions, 17 deletions
diff --git a/dxcommon/include/dxcommon.h b/dxcommon/include/dxcommon.h index 0270629..e04f8e9 100644 --- a/dxcommon/include/dxcommon.h +++ b/dxcommon/include/dxcommon.h | |||
| @@ -10,9 +10,10 @@ namespace dx { | |||
| 10 | 10 | ||
| 11 | using Microsoft::WRL::ComPtr; | 11 | using Microsoft::WRL::ComPtr; |
| 12 | 12 | ||
| 13 | class exception : public std::exception { | 13 | class exception : public std::exception |
| 14 | { | ||
| 14 | public: | 15 | public: |
| 15 | exception() noexcept {} | 16 | exception() noexcept = default; |
| 16 | 17 | ||
| 17 | exception(HRESULT result, const char* file, int line) noexcept | 18 | exception(HRESULT result, const char* file, int line) noexcept |
| 18 | { | 19 | { |
| @@ -25,7 +26,7 @@ public: | |||
| 25 | sprintf_s(m_error, sizeof(m_error), "%s:%d %s", file, line, error); | 26 | sprintf_s(m_error, sizeof(m_error), "%s:%d %s", file, line, error); |
| 26 | } | 27 | } |
| 27 | 28 | ||
| 28 | const char* what() const noexcept | 29 | [[nodiscard]] const char* what() const noexcept final |
| 29 | { | 30 | { |
| 30 | return m_error; | 31 | return m_error; |
| 31 | } | 32 | } |
| @@ -36,8 +37,10 @@ private: | |||
| 36 | 37 | ||
| 37 | #define THROW(error) throw exception(error, __FILE__, __LINE__) | 38 | #define THROW(error) throw exception(error, __FILE__, __LINE__) |
| 38 | 39 | ||
| 39 | #define ThrowIfFailed(result) {\ | 40 | #define ThrowIfFailed(result) \ |
| 40 | if (result != S_OK) {\ | 41 | {\ |
| 42 | if (result != S_OK) \ | ||
| 43 | {\ | ||
| 41 | THROW(result);\ | 44 | THROW(result);\ |
| 42 | }\ | 45 | }\ |
| 43 | } | 46 | } |
diff --git a/hello/main.cc b/hello/main.cc index 1b94cf6..8e41d4b 100644 --- a/hello/main.cc +++ b/hello/main.cc | |||
| @@ -111,7 +111,6 @@ public: | |||
| 111 | _countof(command_lists), command_lists); | 111 | _countof(command_lists), command_lists); |
| 112 | 112 | ||
| 113 | ThrowIfFailed(m_swap_chain->Present(/*SyncInterval=*/1, /*Flags=*/0)); | 113 | ThrowIfFailed(m_swap_chain->Present(/*SyncInterval=*/1, /*Flags=*/0)); |
| 114 | m_current_back_buffer = m_swap_chain->GetCurrentBackBufferIndex(); | ||
| 115 | 114 | ||
| 116 | // It is not efficient to wait for the frame to complete here, but it | 115 | // It is not efficient to wait for the frame to complete here, but it |
| 117 | // is simple and sufficient for this application. | 116 | // is simple and sufficient for this application. |
| @@ -138,7 +137,7 @@ private: | |||
| 138 | 137 | ||
| 139 | // A command list can be reset as soon as it is executed with | 138 | // A command list can be reset as soon as it is executed with |
| 140 | // ExecuteCommandList(). Reset() does require that the command list is | 139 | // ExecuteCommandList(). Reset() does require that the command list is |
| 141 | // in a "closed" state, however, which is why we close it right away | 140 | // in a "closed" state, however, which is why we Close() it right away |
| 142 | // after creation. | 141 | // after creation. |
| 143 | ThrowIfFailed(m_command_list->Reset( | 142 | ThrowIfFailed(m_command_list->Reset( |
| 144 | m_command_allocator.Get(), | 143 | m_command_allocator.Get(), |
| @@ -152,7 +151,7 @@ private: | |||
| 152 | m_command_list->ResourceBarrier(1, &render_barrier); | 151 | m_command_list->ResourceBarrier(1, &render_barrier); |
| 153 | 152 | ||
| 154 | // Record commands. | 153 | // Record commands. |
| 155 | const float clear_colour[] = { 0.0f, 0.0f, 0.0f, 0.0f }; | 154 | const float clear_colour[] = { 0.0f, 0.502f, 0.494f, 0.0f }; |
| 156 | m_command_list->ClearRenderTargetView( | 155 | m_command_list->ClearRenderTargetView( |
| 157 | GetCurrentBackBufferView(), | 156 | GetCurrentBackBufferView(), |
| 158 | clear_colour, | 157 | clear_colour, |
| @@ -174,6 +173,7 @@ private: | |||
| 174 | D3D12_RESOURCE_STATE_PRESENT); | 173 | D3D12_RESOURCE_STATE_PRESENT); |
| 175 | m_command_list->ResourceBarrier(1, &present_barrier); | 174 | m_command_list->ResourceBarrier(1, &present_barrier); |
| 176 | 175 | ||
| 176 | // A command list must be closed before it can be executed. | ||
| 177 | ThrowIfFailed(m_command_list->Close()); | 177 | ThrowIfFailed(m_command_list->Close()); |
| 178 | } | 178 | } |
| 179 | 179 | ||
| @@ -196,10 +196,11 @@ private: | |||
| 196 | // execution. | 196 | // execution. |
| 197 | 197 | ||
| 198 | // Indicate that |m_fence_event| is to be fired when |m_fence| | 198 | // Indicate that |m_fence_event| is to be fired when |m_fence| |
| 199 | // reaches the |fence| value. | 199 | // reaches the new fence value. |
| 200 | ThrowIfFailed(m_fence->SetEventOnCompletion( | 200 | ThrowIfFailed(m_fence->SetEventOnCompletion( |
| 201 | m_fence_value, m_fence_event)); | 201 | m_fence_value, m_fence_event)); |
| 202 | 202 | ||
| 203 | // Will wake up when the fence takes on the new fence value. | ||
| 203 | WaitForSingleObject(m_fence_event, INFINITE); | 204 | WaitForSingleObject(m_fence_event, INFINITE); |
| 204 | } | 205 | } |
| 205 | } | 206 | } |
| @@ -266,8 +267,6 @@ private: | |||
| 266 | /*pRestrictToOutput=*/nullptr, | 267 | /*pRestrictToOutput=*/nullptr, |
| 267 | &swap_chain)); | 268 | &swap_chain)); |
| 268 | ThrowIfFailed(swap_chain.As(&m_swap_chain)); | 269 | ThrowIfFailed(swap_chain.As(&m_swap_chain)); |
| 269 | |||
| 270 | m_current_back_buffer = m_swap_chain->GetCurrentBackBufferIndex(); | ||
| 271 | } | 270 | } |
| 272 | 271 | ||
| 273 | /// Creates RTVs for all of the swap chain's buffers. | 272 | /// Creates RTVs for all of the swap chain's buffers. |
| @@ -341,7 +340,7 @@ private: | |||
| 341 | 340 | ||
| 342 | ID3D12Resource* GetCurrentBackBuffer() const | 341 | ID3D12Resource* GetCurrentBackBuffer() const |
| 343 | { | 342 | { |
| 344 | return m_swap_chain_buffer[m_current_back_buffer].Get(); | 343 | return m_swap_chain_buffer[m_swap_chain->GetCurrentBackBufferIndex()].Get(); |
| 345 | } | 344 | } |
| 346 | 345 | ||
| 347 | D3D12_CPU_DESCRIPTOR_HANDLE GetCurrentBackBufferView() const | 346 | D3D12_CPU_DESCRIPTOR_HANDLE GetCurrentBackBufferView() const |
| @@ -350,7 +349,7 @@ private: | |||
| 350 | assert(m_rtv_descriptor_size > 0); | 349 | assert(m_rtv_descriptor_size > 0); |
| 351 | return CD3DX12_CPU_DESCRIPTOR_HANDLE( | 350 | return CD3DX12_CPU_DESCRIPTOR_HANDLE( |
| 352 | m_rtv_heap->GetCPUDescriptorHandleForHeapStart(), | 351 | m_rtv_heap->GetCPUDescriptorHandleForHeapStart(), |
| 353 | m_current_back_buffer, | 352 | m_swap_chain->GetCurrentBackBufferIndex(), |
| 354 | m_rtv_descriptor_size); | 353 | m_rtv_descriptor_size); |
| 355 | } | 354 | } |
| 356 | 355 | ||
| @@ -385,10 +384,6 @@ private: | |||
| 385 | HANDLE m_fence_event = 0; | 384 | HANDLE m_fence_event = 0; |
| 386 | UINT64 m_fence_value = 0; | 385 | UINT64 m_fence_value = 0; |
| 387 | 386 | ||
| 388 | // Index to the buffer in the RTV descriptor heap that represents the | ||
| 389 | // current back buffer. | ||
| 390 | int m_current_back_buffer = 0; | ||
| 391 | |||
| 392 | UINT m_rtv_descriptor_size = 0; | 387 | UINT m_rtv_descriptor_size = 0; |
| 393 | UINT m_dsv_descriptor_size = 0; | 388 | UINT m_dsv_descriptor_size = 0; |
| 394 | UINT m_cbv_descriptor_size = 0; | 389 | UINT m_cbv_descriptor_size = 0; |
