[D3D12] Change most subsystem pointers to references
This commit is contained in:
@@ -32,10 +32,10 @@ D3D12Context::D3D12Context(D3D12Provider* provider, Window* target_window)
|
||||
D3D12Context::~D3D12Context() { Shutdown(); }
|
||||
|
||||
bool D3D12Context::Initialize() {
|
||||
auto provider = GetD3D12Provider();
|
||||
auto dxgi_factory = provider->GetDXGIFactory();
|
||||
auto device = provider->GetDevice();
|
||||
auto direct_queue = provider->GetDirectQueue();
|
||||
auto& provider = GetD3D12Provider();
|
||||
auto dxgi_factory = provider.GetDXGIFactory();
|
||||
auto device = provider.GetDevice();
|
||||
auto direct_queue = provider.GetDirectQueue();
|
||||
|
||||
context_lost_ = false;
|
||||
|
||||
@@ -74,7 +74,7 @@ bool D3D12Context::Initialize() {
|
||||
swap_chain_desc.Flags = 0;
|
||||
IDXGISwapChain1* swap_chain_1;
|
||||
if (FAILED(dxgi_factory->CreateSwapChainForHwnd(
|
||||
provider->GetDirectQueue(),
|
||||
provider.GetDirectQueue(),
|
||||
static_cast<HWND>(target_window_->native_handle()),
|
||||
&swap_chain_desc, nullptr, nullptr, &swap_chain_1))) {
|
||||
XELOGE("Failed to create a DXGI swap chain");
|
||||
@@ -131,7 +131,7 @@ bool D3D12Context::Initialize() {
|
||||
swap_command_list_->Close();
|
||||
|
||||
// Initialize the immediate mode drawer if not offscreen.
|
||||
immediate_drawer_ = std::make_unique<D3D12ImmediateDrawer>(this);
|
||||
immediate_drawer_ = std::make_unique<D3D12ImmediateDrawer>(*this);
|
||||
if (!immediate_drawer_->Initialize()) {
|
||||
Shutdown();
|
||||
return false;
|
||||
@@ -155,7 +155,7 @@ bool D3D12Context::InitializeSwapChainBuffers() {
|
||||
swap_chain_back_buffer_index_ = swap_chain_->GetCurrentBackBufferIndex();
|
||||
|
||||
// Create RTV descriptors for the swap chain buffers.
|
||||
auto device = GetD3D12Provider()->GetDevice();
|
||||
auto device = GetD3D12Provider().GetDevice();
|
||||
D3D12_RENDER_TARGET_VIEW_DESC rtv_desc;
|
||||
rtv_desc.Format = kSwapChainFormat;
|
||||
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
|
||||
@@ -223,12 +223,6 @@ ImmediateDrawer* D3D12Context::immediate_drawer() {
|
||||
return immediate_drawer_.get();
|
||||
}
|
||||
|
||||
bool D3D12Context::is_current() { return false; }
|
||||
|
||||
bool D3D12Context::MakeCurrent() { return true; }
|
||||
|
||||
void D3D12Context::ClearCurrent() {}
|
||||
|
||||
void D3D12Context::BeginSwap() {
|
||||
if (!target_window_ || context_lost_) {
|
||||
return;
|
||||
@@ -320,7 +314,7 @@ void D3D12Context::EndSwap() {
|
||||
return;
|
||||
}
|
||||
|
||||
auto direct_queue = GetD3D12Provider()->GetDirectQueue();
|
||||
auto direct_queue = GetD3D12Provider().GetDirectQueue();
|
||||
|
||||
// Switch the back buffer to presentation state.
|
||||
D3D12_RESOURCE_BARRIER barrier;
|
||||
@@ -358,8 +352,8 @@ std::unique_ptr<RawImage> D3D12Context::Capture() {
|
||||
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE D3D12Context::GetSwapChainBufferRTV(
|
||||
uint32_t buffer_index) const {
|
||||
return GetD3D12Provider()->OffsetRTVDescriptor(swap_chain_rtv_heap_start_,
|
||||
buffer_index);
|
||||
return GetD3D12Provider().OffsetRTVDescriptor(swap_chain_rtv_heap_start_,
|
||||
buffer_index);
|
||||
}
|
||||
|
||||
} // namespace d3d12
|
||||
|
||||
@@ -28,10 +28,6 @@ class D3D12Context : public GraphicsContext {
|
||||
|
||||
ImmediateDrawer* immediate_drawer() override;
|
||||
|
||||
bool is_current() override;
|
||||
bool MakeCurrent() override;
|
||||
void ClearCurrent() override;
|
||||
|
||||
bool WasLost() override { return context_lost_; }
|
||||
|
||||
void BeginSwap() override;
|
||||
@@ -39,8 +35,8 @@ class D3D12Context : public GraphicsContext {
|
||||
|
||||
std::unique_ptr<RawImage> Capture() override;
|
||||
|
||||
D3D12Provider* GetD3D12Provider() const {
|
||||
return static_cast<D3D12Provider*>(provider_);
|
||||
D3D12Provider& GetD3D12Provider() const {
|
||||
return static_cast<D3D12Provider&>(*provider_);
|
||||
}
|
||||
|
||||
// The format used by DWM.
|
||||
|
||||
@@ -109,14 +109,14 @@ void D3D12ImmediateTexture::Transition(
|
||||
state_ = new_state;
|
||||
}
|
||||
|
||||
D3D12ImmediateDrawer::D3D12ImmediateDrawer(D3D12Context* graphics_context)
|
||||
: ImmediateDrawer(graphics_context), context_(graphics_context) {}
|
||||
D3D12ImmediateDrawer::D3D12ImmediateDrawer(D3D12Context& graphics_context)
|
||||
: ImmediateDrawer(&graphics_context), context_(graphics_context) {}
|
||||
|
||||
D3D12ImmediateDrawer::~D3D12ImmediateDrawer() { Shutdown(); }
|
||||
|
||||
bool D3D12ImmediateDrawer::Initialize() {
|
||||
auto provider = context_->GetD3D12Provider();
|
||||
auto device = provider->GetDevice();
|
||||
auto& provider = context_.GetD3D12Provider();
|
||||
auto device = provider.GetDevice();
|
||||
|
||||
// Create the root signature.
|
||||
D3D12_ROOT_PARAMETER root_parameters[size_t(RootParameter::kCount)];
|
||||
@@ -247,7 +247,7 @@ bool D3D12ImmediateDrawer::Initialize() {
|
||||
}
|
||||
sampler_heap_cpu_start_ = sampler_heap_->GetCPUDescriptorHandleForHeapStart();
|
||||
sampler_heap_gpu_start_ = sampler_heap_->GetGPUDescriptorHandleForHeapStart();
|
||||
uint32_t sampler_size = provider->GetSamplerDescriptorSize();
|
||||
uint32_t sampler_size = provider.GetSamplerDescriptorSize();
|
||||
// Nearest neighbor, clamp.
|
||||
D3D12_SAMPLER_DESC sampler_desc;
|
||||
sampler_desc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
|
||||
@@ -326,7 +326,7 @@ std::unique_ptr<ImmediateTexture> D3D12ImmediateDrawer::CreateTexture(
|
||||
const uint8_t* data) {
|
||||
auto texture =
|
||||
std::make_unique<D3D12ImmediateTexture>(width, height, filter, repeat);
|
||||
texture->Initialize(context_->GetD3D12Provider()->GetDevice());
|
||||
texture->Initialize(context_.GetD3D12Provider().GetDevice());
|
||||
if (data != nullptr) {
|
||||
UpdateTexture(texture.get(), data);
|
||||
}
|
||||
@@ -343,7 +343,7 @@ void D3D12ImmediateDrawer::UpdateTexture(ImmediateTexture* texture,
|
||||
}
|
||||
uint32_t width = d3d_texture->width, height = d3d_texture->height;
|
||||
|
||||
auto device = context_->GetD3D12Provider()->GetDevice();
|
||||
auto device = context_.GetD3D12Provider().GetDevice();
|
||||
|
||||
// Create and fill the upload buffer.
|
||||
D3D12_RESOURCE_DESC texture_desc = texture_resource->GetDesc();
|
||||
@@ -400,7 +400,7 @@ void D3D12ImmediateDrawer::UpdateTexture(ImmediateTexture* texture,
|
||||
&location_source, nullptr);
|
||||
SubmittedTextureUpload submitted_upload;
|
||||
submitted_upload.buffer = buffer;
|
||||
submitted_upload.fence_value = context_->GetSwapCurrentFenceValue();
|
||||
submitted_upload.fence_value = context_.GetSwapCurrentFenceValue();
|
||||
texture_uploads_submitted_.push_back(submitted_upload);
|
||||
} else {
|
||||
// Defer uploading to the next frame when there's a command list.
|
||||
@@ -413,13 +413,13 @@ void D3D12ImmediateDrawer::UpdateTexture(ImmediateTexture* texture,
|
||||
|
||||
void D3D12ImmediateDrawer::Begin(int render_target_width,
|
||||
int render_target_height) {
|
||||
auto device = context_->GetD3D12Provider()->GetDevice();
|
||||
auto device = context_.GetD3D12Provider().GetDevice();
|
||||
|
||||
// Use the compositing command list.
|
||||
current_command_list_ = context_->GetSwapCommandList();
|
||||
current_command_list_ = context_.GetSwapCommandList();
|
||||
|
||||
uint64_t completed_fence_value = context_->GetSwapCompletedFenceValue();
|
||||
uint64_t current_fence_value = context_->GetSwapCurrentFenceValue();
|
||||
uint64_t completed_fence_value = context_.GetSwapCompletedFenceValue();
|
||||
uint64_t current_fence_value = context_.GetSwapCurrentFenceValue();
|
||||
|
||||
// Remove temporary buffers for completed texture uploads.
|
||||
auto erase_uploads_end = texture_uploads_submitted_.begin();
|
||||
@@ -494,7 +494,7 @@ void D3D12ImmediateDrawer::BeginDrawBatch(const ImmediateDrawBatch& batch) {
|
||||
if (current_command_list_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
uint64_t current_fence_value = context_->GetSwapCurrentFenceValue();
|
||||
uint64_t current_fence_value = context_.GetSwapCurrentFenceValue();
|
||||
|
||||
batch_open_ = false;
|
||||
|
||||
@@ -549,8 +549,8 @@ void D3D12ImmediateDrawer::Draw(const ImmediateDraw& draw) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto provider = context_->GetD3D12Provider();
|
||||
auto device = provider->GetDevice();
|
||||
auto& provider = context_.GetD3D12Provider();
|
||||
auto device = provider.GetDevice();
|
||||
|
||||
// Bind the texture.
|
||||
auto texture = reinterpret_cast<D3D12ImmediateTexture*>(draw.texture_handle);
|
||||
@@ -565,7 +565,7 @@ void D3D12ImmediateDrawer::Draw(const ImmediateDraw& draw) {
|
||||
bool bind_texture = current_texture_ != texture;
|
||||
uint32_t texture_descriptor_index;
|
||||
uint64_t texture_heap_index = texture_descriptor_pool_->Request(
|
||||
context_->GetSwapCurrentFenceValue(), texture_descriptor_pool_heap_index_,
|
||||
context_.GetSwapCurrentFenceValue(), texture_descriptor_pool_heap_index_,
|
||||
bind_texture ? 1 : 0, 1, texture_descriptor_index);
|
||||
if (texture_heap_index == DescriptorHeapPool::kHeapIndexInvalid) {
|
||||
return;
|
||||
@@ -589,12 +589,12 @@ void D3D12ImmediateDrawer::Draw(const ImmediateDraw& draw) {
|
||||
texture_view_desc.Texture2D.ResourceMinLODClamp = 0.0f;
|
||||
device->CreateShaderResourceView(
|
||||
texture_resource, &texture_view_desc,
|
||||
provider->OffsetViewDescriptor(
|
||||
provider.OffsetViewDescriptor(
|
||||
texture_descriptor_pool_->GetLastRequestHeapCPUStart(),
|
||||
texture_descriptor_index));
|
||||
current_command_list_->SetGraphicsRootDescriptorTable(
|
||||
UINT(RootParameter::kTexture),
|
||||
provider->OffsetViewDescriptor(
|
||||
provider.OffsetViewDescriptor(
|
||||
texture_descriptor_pool_->GetLastRequestHeapGPUStart(),
|
||||
texture_descriptor_index));
|
||||
current_texture_ = texture;
|
||||
@@ -616,8 +616,8 @@ void D3D12ImmediateDrawer::Draw(const ImmediateDraw& draw) {
|
||||
if (current_sampler_index_ != sampler_index) {
|
||||
current_command_list_->SetGraphicsRootDescriptorTable(
|
||||
UINT(RootParameter::kSampler),
|
||||
provider->OffsetSamplerDescriptor(sampler_heap_gpu_start_,
|
||||
uint32_t(sampler_index)));
|
||||
provider.OffsetSamplerDescriptor(sampler_heap_gpu_start_,
|
||||
uint32_t(sampler_index)));
|
||||
current_sampler_index_ = sampler_index;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ class D3D12Context;
|
||||
|
||||
class D3D12ImmediateDrawer : public ImmediateDrawer {
|
||||
public:
|
||||
D3D12ImmediateDrawer(D3D12Context* graphics_context);
|
||||
D3D12ImmediateDrawer(D3D12Context& graphics_context);
|
||||
~D3D12ImmediateDrawer() override;
|
||||
|
||||
bool Initialize();
|
||||
@@ -46,7 +46,7 @@ class D3D12ImmediateDrawer : public ImmediateDrawer {
|
||||
void End() override;
|
||||
|
||||
private:
|
||||
D3D12Context* context_ = nullptr;
|
||||
D3D12Context& context_;
|
||||
|
||||
ID3D12RootSignature* root_signature_ = nullptr;
|
||||
enum class RootParameter {
|
||||
|
||||
@@ -23,10 +23,10 @@ const D3D12_HEAP_PROPERTIES kHeapPropertiesReadback = {
|
||||
D3D12_HEAP_TYPE_READBACK};
|
||||
|
||||
ID3D12RootSignature* CreateRootSignature(
|
||||
D3D12Provider* provider, const D3D12_ROOT_SIGNATURE_DESC& desc) {
|
||||
D3D12Provider& provider, const D3D12_ROOT_SIGNATURE_DESC& desc) {
|
||||
ID3DBlob* blob;
|
||||
ID3DBlob* error_blob = nullptr;
|
||||
if (FAILED(provider->SerializeRootSignature(
|
||||
if (FAILED(provider.SerializeRootSignature(
|
||||
&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &error_blob))) {
|
||||
XELOGE("Failed to serialize a root signature");
|
||||
if (error_blob != nullptr) {
|
||||
@@ -40,9 +40,9 @@ ID3D12RootSignature* CreateRootSignature(
|
||||
error_blob->Release();
|
||||
}
|
||||
ID3D12RootSignature* root_signature = nullptr;
|
||||
provider->GetDevice()->CreateRootSignature(0, blob->GetBufferPointer(),
|
||||
blob->GetBufferSize(),
|
||||
IID_PPV_ARGS(&root_signature));
|
||||
provider.GetDevice()->CreateRootSignature(0, blob->GetBufferPointer(),
|
||||
blob->GetBufferSize(),
|
||||
IID_PPV_ARGS(&root_signature));
|
||||
blob->Release();
|
||||
return root_signature;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ inline bool ReleaseAndNull(T& object) {
|
||||
return false;
|
||||
};
|
||||
|
||||
ID3D12RootSignature* CreateRootSignature(D3D12Provider* provider,
|
||||
ID3D12RootSignature* CreateRootSignature(D3D12Provider& provider,
|
||||
const D3D12_ROOT_SIGNATURE_DESC& desc);
|
||||
|
||||
ID3D12PipelineState* CreateComputePipelineState(
|
||||
|
||||
@@ -20,5 +20,11 @@ GraphicsContext::GraphicsContext(GraphicsProvider* provider,
|
||||
|
||||
GraphicsContext::~GraphicsContext() = default;
|
||||
|
||||
bool GraphicsContext::is_current() { return true; }
|
||||
|
||||
bool GraphicsContext::MakeCurrent() { return true; }
|
||||
|
||||
void GraphicsContext::ClearCurrent() {}
|
||||
|
||||
} // namespace ui
|
||||
} // namespace xe
|
||||
|
||||
@@ -41,15 +41,15 @@ class GraphicsContext {
|
||||
|
||||
virtual ImmediateDrawer* immediate_drawer() = 0;
|
||||
|
||||
virtual bool is_current() = 0;
|
||||
virtual bool MakeCurrent() = 0;
|
||||
virtual void ClearCurrent() = 0;
|
||||
virtual bool is_current();
|
||||
virtual bool MakeCurrent();
|
||||
virtual void ClearCurrent();
|
||||
|
||||
// Returns true if the OS took away our context because we caused a TDR or
|
||||
// some other outstanding error. When this happens, this context, as well as
|
||||
// any other shared contexts are junk.
|
||||
// This context must be made current in order for this call to work properly.
|
||||
virtual bool WasLost() { return false; }
|
||||
virtual bool WasLost() = 0;
|
||||
|
||||
virtual void BeginSwap() = 0;
|
||||
virtual void EndSwap() = 0;
|
||||
|
||||
Reference in New Issue
Block a user