Skip to content

Commit

Permalink
[D3D12] RHI_CommandList::SetBufferVertex and RHI_CommandList::SetBuff…
Browse files Browse the repository at this point in the history
…erIndex
  • Loading branch information
PanosK92 committed Jul 9, 2022
1 parent 53898f7 commit 1e13e23
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 91 deletions.
18 changes: 6 additions & 12 deletions Runtime/RHI/D3D11/D3D11_CommandList.cpp
Expand Up @@ -78,12 +78,6 @@ namespace Spartan
return true;
}

bool RHI_CommandList::Reset()
{
m_state = RHI_CommandListState::Idle;
return true;
}

void RHI_CommandList::SetPipelineState(RHI_PipelineState& pso)
{
SP_ASSERT(pso.IsValid() && "Pipeline state is invalid");
Expand Down Expand Up @@ -543,14 +537,14 @@ namespace Spartan
m_rhi_device->GetContextRhi()->device_context->RSSetScissorRects(1, &d3d11_rectangle);
}

void RHI_CommandList::SetBufferVertex(const RHI_VertexBuffer* buffer, const uint64_t offset /*= 0*/)
void RHI_CommandList::SetBufferVertex(const RHI_VertexBuffer* buffer)
{
SP_ASSERT(buffer != nullptr);
SP_ASSERT(buffer->GetResource() != nullptr);

ID3D11Buffer* vertex_buffer = static_cast<ID3D11Buffer*>(buffer->GetResource());
UINT stride = buffer->GetStride();
UINT offsets[] = { static_cast<UINT>(offset) };
UINT offsets[] = { 0 };
ID3D11DeviceContext* device_context = m_rhi_device->GetContextRhi()->device_context;

// Get currently set buffer
Expand All @@ -560,7 +554,7 @@ namespace Spartan
device_context->IAGetVertexBuffers(0, 1, &set_buffer, &set_stride, &set_offset);

// Skip if already set
if (set_buffer == vertex_buffer && set_offset == offset)
if (set_buffer == vertex_buffer)
return;

// Set
Expand All @@ -572,7 +566,7 @@ namespace Spartan
}
}

void RHI_CommandList::SetBufferIndex(const RHI_IndexBuffer* buffer, const uint64_t offset /*= 0*/)
void RHI_CommandList::SetBufferIndex(const RHI_IndexBuffer* buffer)
{
SP_ASSERT(buffer != nullptr);
SP_ASSERT(buffer->GetResource() != nullptr);
Expand All @@ -588,11 +582,11 @@ namespace Spartan
device_context->IAGetIndexBuffer(&set_buffer, &set_format, &set_offset);

// Skip if already set
if (set_buffer == index_buffer && set_offset == offset)
if (set_buffer == index_buffer)
return;

// Set
device_context->IASetIndexBuffer(index_buffer, format, static_cast<UINT>(offset));
device_context->IASetIndexBuffer(index_buffer, format, 0);

if (m_profiler)
{
Expand Down
126 changes: 81 additions & 45 deletions Runtime/RHI/D3D12/D3D12_CommandList.cpp
Expand Up @@ -50,14 +50,18 @@ namespace Spartan
{
RHI_CommandList::RHI_CommandList(Context* context, void* cmd_pool, const char* name)
{
m_renderer = context->GetSubsystem<Renderer>();
m_profiler = context->GetSubsystem<Profiler>();
m_rhi_device = m_renderer->GetRhiDevice().get();
m_object_name = name;
SP_ASSERT(cmd_pool != nullptr);

m_renderer = context->GetSubsystem<Renderer>();
m_profiler = context->GetSubsystem<Profiler>();
m_rhi_device = m_renderer->GetRhiDevice().get();
m_object_name = name;
m_cmd_pool_resource = cmd_pool;
m_timestamps.fill(0);

//ID3D12CommandAllocator* allocator = static_cast<ID3D12CommandAllocator*>(m_rhi_device->GetCommandPoolGraphics());
//d3d12_utility::error::check(m_rhi_device->GetContextRhi()->device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator, nullptr, IID_PPV_ARGS(reinterpret_cast<ID3D12GraphicsCommandList**>(&m_resource))));
// Created command list
ID3D12CommandAllocator* allocator = static_cast<ID3D12CommandAllocator*>(cmd_pool);
d3d12_utility::error::check(m_rhi_device->GetContextRhi()->device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator, nullptr, IID_PPV_ARGS(reinterpret_cast<ID3D12GraphicsCommandList**>(&m_resource))));
}

RHI_CommandList::~RHI_CommandList()
Expand All @@ -83,7 +87,9 @@ namespace Spartan
SP_ASSERT(m_state == RHI_CommandListState::Idle);

// Unlike Vulkan, D3D12 wraps both begin and reset under Reset().
//SP_ASSERT(d3d12_utility::error::check(static_cast<ID3D12GraphicsCommandList*>(m_resource)->Reset(static_cast<ID3D12CommandAllocator*>(m_rhi_device->GetCommandPoolGraphics()), nullptr)) && "Failed to reset command list");
SP_ASSERT_MSG(d3d12_utility::error::check(static_cast<ID3D12GraphicsCommandList*>(m_resource)->Reset(
static_cast<ID3D12CommandAllocator*>(m_cmd_pool_resource), nullptr)),
"Failed to reset command list");

m_state = RHI_CommandListState::Recording;
}
Expand All @@ -106,41 +112,27 @@ namespace Spartan
return true;
}

bool RHI_CommandList::Reset()
{
// Verify a few things
SP_ASSERT(m_resource != nullptr);
SP_ASSERT(m_rhi_device != nullptr);
SP_ASSERT(m_state == RHI_CommandListState::Recording);

lock_guard<mutex> guard(m_mutex_reset);

// if (!d3d12_utility::error::check(static_cast<ID3D12GraphicsCommandList*>(m_resource)->Reset(static_cast<ID3D12CommandAllocator*>(m_rhi_device->GetCommandPoolGraphics()), nullptr)))
//return false;

m_state = RHI_CommandListState::Idle;
return true;
}

void RHI_CommandList::SetPipelineState(RHI_PipelineState& pso)
{
SP_ASSERT(pso.IsValid() && "Pipeline state is invalid");
SP_ASSERT(m_state == RHI_CommandListState::Recording);

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::BeginRenderPass()
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::EndRenderPass()
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::ClearPipelineStateRenderTargets(RHI_PipelineState& pipeline_state)
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::ClearRenderTarget(RHI_Texture* texture,
Expand All @@ -152,7 +144,7 @@ namespace Spartan
const float clear_stencil /*= rhi_stencil_load*/
)
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::Draw(const uint32_t vertex_count, uint32_t vertex_start_index /*= 0*/)
Expand Down Expand Up @@ -213,7 +205,7 @@ namespace Spartan

void RHI_CommandList::Blit(RHI_Texture* source, RHI_Texture* destination)
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::SetViewport(const RHI_Viewport& viewport) const
Expand Down Expand Up @@ -248,44 +240,88 @@ namespace Spartan
static_cast<ID3D12GraphicsCommandList*>(m_resource)->RSSetScissorRects(1, &d3d12_rectangle);
}

void RHI_CommandList::SetBufferVertex(const RHI_VertexBuffer* buffer, const uint64_t offset /*= 0*/)
void RHI_CommandList::SetBufferVertex(const RHI_VertexBuffer* buffer)
{

// Validate command list state
SP_ASSERT(m_state == RHI_CommandListState::Recording);

// Skip if already set
if (m_vertex_buffer_id == buffer->GetObjectId())
return;

D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view = {};
vertex_buffer_view.BufferLocation = 0;
vertex_buffer_view.StrideInBytes = buffer->GetStride();
vertex_buffer_view.SizeInBytes = buffer->GetObjectSizeGpu();

static_cast<ID3D12GraphicsCommandList*>(m_resource)->IASetVertexBuffers(
0, // StartSlot
1, // NumViews
&vertex_buffer_view // pViews
);

m_vertex_buffer_id = buffer->GetObjectId();

if (m_profiler)
{
m_profiler->m_rhi_bindings_buffer_vertex++;
}
}

void RHI_CommandList::SetBufferIndex(const RHI_IndexBuffer* buffer, const uint64_t offset /*= 0*/)
void RHI_CommandList::SetBufferIndex(const RHI_IndexBuffer* buffer)
{

// Validate command list state
SP_ASSERT(m_state == RHI_CommandListState::Recording);

// Skip if already set
if (m_index_buffer_id == buffer->GetObjectId())
return;

D3D12_INDEX_BUFFER_VIEW index_buffer_view = {};
index_buffer_view.BufferLocation = 0;
index_buffer_view.SizeInBytes = buffer->GetObjectSizeGpu();
index_buffer_view.Format = buffer->Is16Bit() ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;

static_cast<ID3D12GraphicsCommandList*>(m_resource)->IASetIndexBuffer(
&index_buffer_view // pView
);

m_index_buffer_id = buffer->GetObjectId();

if (m_profiler)
{
m_profiler->m_rhi_bindings_buffer_index++;
}
}

void RHI_CommandList::SetConstantBuffer(const uint32_t slot, const uint8_t scope, RHI_ConstantBuffer* constant_buffer) const
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::SetStructuredBuffer(const uint32_t slot, RHI_StructuredBuffer* structured_buffer) const
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::SetSampler(const uint32_t slot, RHI_Sampler* sampler) const
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::SetTexture(const uint32_t slot, RHI_Texture* texture, const uint32_t mip_index /*= rhi_all_mips*/, uint32_t mip_range /*= 0*/, const bool uav /*= false*/)
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::BeginTimestamp(void* query)
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::EndTimestamp(void* query)
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

float RHI_CommandList::GetTimestampDuration(void* query_start, void* query_end, const uint32_t pass_index)
Expand All @@ -300,36 +336,36 @@ namespace Spartan

void RHI_CommandList::BeginTimeblock(const char* name, const bool gpu_marker, const bool gpu_timing)
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::EndTimeblock()
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::BeginMarker(const char* name)
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::EndMarker()
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::OnDraw()
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::UnbindOutputTextures()
{

SP_ASSERT_MSG(false, "Function is not implemented");
}

void RHI_CommandList::GetDescriptorSetLayoutFromPipelineState(RHI_PipelineState& pipeline_state)
{

SP_ASSERT_MSG(false, "Function is not implemented");
}
}
12 changes: 5 additions & 7 deletions Runtime/RHI/RHI_CommandList.h
Expand Up @@ -50,7 +50,6 @@ namespace Spartan
void Begin();
bool End();
bool Submit();
bool Reset();
// Waits for the command list to finish being processed.
void Wait();
// Causes the command list to ignore one submission call (useful when the command list refers to resources which have been destroyed).
Expand Down Expand Up @@ -90,10 +89,10 @@ namespace Spartan
void SetScissorRectangle(const Math::Rectangle& scissor_rectangle) const;

// Vertex buffer
void SetBufferVertex(const RHI_VertexBuffer* buffer, const uint64_t offset = 0);
void SetBufferVertex(const RHI_VertexBuffer* buffer);

// Index buffer
void SetBufferIndex(const RHI_IndexBuffer* buffer, const uint64_t offset = 0);
void SetBufferIndex(const RHI_IndexBuffer* buffer);

// Constant buffer
void SetConstantBuffer(const uint32_t slot, const uint8_t scope, RHI_ConstantBuffer* constant_buffer) const;
Expand Down Expand Up @@ -154,6 +153,7 @@ namespace Spartan
RHI_Device* m_rhi_device = nullptr;
Profiler* m_profiler = nullptr;
void* m_resource = nullptr;
void* m_cmd_pool_resource = nullptr;
std::atomic<bool> m_discard = false;
bool m_is_rendering = false;
bool m_pipeline_dirty = false;
Expand Down Expand Up @@ -195,9 +195,7 @@ namespace Spartan
std::array<uint64_t, m_max_timestamps> m_timestamps;

// Variables to minimise state changes
uint64_t m_vertex_buffer_id = 0;
uint64_t m_vertex_buffer_offset = 0;
uint64_t m_index_buffer_id = 0;
uint64_t m_index_buffer_offset = 0;
uint64_t m_vertex_buffer_id = 0;
uint64_t m_index_buffer_id = 0;
};
}

0 comments on commit 1e13e23

Please sign in to comment.