summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/D3D12/VertexManager.cpp
blob: 9f22cc56eb9e287cb49bb65d94b91fbb1655e609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2010 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "Common/CommonTypes.h"

#include "VideoBackends/D3D12/BoundingBox.h"
#include "VideoBackends/D3D12/D3DBase.h"
#include "VideoBackends/D3D12/D3DCommandListManager.h"
#include "VideoBackends/D3D12/D3DState.h"
#include "VideoBackends/D3D12/D3DStreamBuffer.h"
#include "VideoBackends/D3D12/FramebufferManager.h"
#include "VideoBackends/D3D12/Render.h"
#include "VideoBackends/D3D12/ShaderCache.h"
#include "VideoBackends/D3D12/VertexManager.h"

#include "VideoCommon/BoundingBox.h"
#include "VideoCommon/Debugger.h"
#include "VideoCommon/IndexGenerator.h"
#include "VideoCommon/RenderBase.h"
#include "VideoCommon/Statistics.h"
#include "VideoCommon/VertexLoaderManager.h"
#include "VideoCommon/VideoConfig.h"

namespace DX12
{

static constexpr unsigned int MAX_IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * sizeof(u16) * 16;
static constexpr unsigned int MAX_VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE * 4;

void VertexManager::SetIndexBuffer()
{
	D3D12_INDEX_BUFFER_VIEW ib_view = {
		m_index_stream_buffer->GetBaseGPUAddress(),          // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
		static_cast<UINT>(m_index_stream_buffer->GetSize()), // UINT SizeInBytes;
		DXGI_FORMAT_R16_UINT                                 // DXGI_FORMAT Format;
	};

	D3D::current_command_list->IASetIndexBuffer(&ib_view);
}

void VertexManager::CreateDeviceObjects()
{
	m_vertex_draw_offset = 0;
	m_index_draw_offset = 0;

	m_vertex_stream_buffer = std::make_unique<D3DStreamBuffer>(MAXVBUFFERSIZE * 2, MAX_VBUFFER_SIZE, &m_vertex_stream_buffer_reallocated);
	m_index_stream_buffer  = std::make_unique<D3DStreamBuffer>(MAXIBUFFERSIZE * sizeof(u16) * 2, MAXIBUFFERSIZE * sizeof(u16) * 16, &m_index_stream_buffer_reallocated);

	SetIndexBuffer();

	// Use CPU-only memory if the GPU won't be reading from the buffers,
	// since reading upload heaps on the CPU is slow..
	m_vertex_cpu_buffer.resize(MAXVBUFFERSIZE);
	m_index_cpu_buffer.resize(MAXIBUFFERSIZE);
}

void VertexManager::DestroyDeviceObjects()
{
	m_vertex_stream_buffer.reset();
	m_index_stream_buffer.reset();

	m_vertex_cpu_buffer.clear();
	m_index_cpu_buffer.clear();
}

VertexManager::VertexManager()
{
	CreateDeviceObjects();
}

VertexManager::~VertexManager()
{
	DestroyDeviceObjects();
}

void VertexManager::PrepareDrawBuffers(u32 stride)
{
	u32 vertex_data_size = IndexGenerator::GetNumVerts() * stride;
	u32 index_data_size = IndexGenerator::GetIndexLen() * sizeof(u16);

	m_vertex_stream_buffer->OverrideSizeOfPreviousAllocation(vertex_data_size);
	m_index_stream_buffer->OverrideSizeOfPreviousAllocation(index_data_size);

	ADDSTAT(stats.thisFrame.bytesVertexStreamed, vertex_data_size);
	ADDSTAT(stats.thisFrame.bytesIndexStreamed, index_data_size);
}

void VertexManager::Draw(u32 stride)
{
	static u32 s_previous_stride = UINT_MAX;

	u32 indices = IndexGenerator::GetIndexLen();

	if (D3D::command_list_mgr->GetCommandListDirtyState(COMMAND_LIST_STATE_VERTEX_BUFFER) || s_previous_stride != stride)
	{
		D3D12_VERTEX_BUFFER_VIEW vb_view = {
			m_vertex_stream_buffer->GetBaseGPUAddress(),          // D3D12_GPU_VIRTUAL_ADDRESS BufferLocation;
			static_cast<UINT>(m_vertex_stream_buffer->GetSize()), // UINT SizeInBytes;
			stride                                                // UINT StrideInBytes;
		};

		D3D::current_command_list->IASetVertexBuffers(0, 1, &vb_view);

		D3D::command_list_mgr->SetCommandListDirtyState(COMMAND_LIST_STATE_VERTEX_BUFFER, false);
		s_previous_stride = stride;
	}

	D3D_PRIMITIVE_TOPOLOGY d3d_primitive_topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;

	switch (current_primitive_type)
	{
		case PRIMITIVE_POINTS:
			d3d_primitive_topology = D3D_PRIMITIVE_TOPOLOGY_POINTLIST;
			break;
		case PRIMITIVE_LINES:
			d3d_primitive_topology = D3D_PRIMITIVE_TOPOLOGY_LINELIST;
			break;
	}

	if (D3D::command_list_mgr->GetCommandListPrimitiveTopology() != d3d_primitive_topology)
	{
		D3D::current_command_list->IASetPrimitiveTopology(d3d_primitive_topology);
		D3D::command_list_mgr->SetCommandListPrimitiveTopology(d3d_primitive_topology);
	}

	u32 base_vertex = m_vertex_draw_offset / stride;
	u32 start_index = m_index_draw_offset / sizeof(u16);

	D3D::current_command_list->DrawIndexedInstanced(indices, 1, start_index, base_vertex, 0);

	INCSTAT(stats.thisFrame.numDrawCalls);
}

void VertexManager::vFlush(bool use_dst_alpha)
{
	ShaderCache::LoadAndSetActiveShaders(use_dst_alpha ? DSTALPHA_DUAL_SOURCE_BLEND : DSTALPHA_NONE, current_primitive_type);

	if (g_ActiveConfig.backend_info.bSupportsBBox && BoundingBox::active)
		BBox::Invalidate();

	u32 stride = VertexLoaderManager::GetCurrentVertexFormat()->GetVertexStride();

	PrepareDrawBuffers(stride);

	g_renderer->ApplyState(use_dst_alpha);

	Draw(stride);

	D3D::command_list_mgr->m_draws_since_last_execution++;

	// Many Gamecube/Wii titles read from the EFB each frame to determine what new rendering work to submit, e.g. where sun rays are
	// occluded and where they aren't. When the CPU wants to read this data (done in Renderer::AccessEFB), it requires that the GPU
	// finish all oustanding work. As an optimization, when we detect that the CPU is likely to read back data this frame, we break
	// up the rendering work and submit it more frequently to the GPU (via ExecuteCommandList). Thus, when the CPU finally needs the
	// the GPU to finish all of its work, there is (hopefully) less work outstanding to wait on at that moment.

	// D3D12TODO: Decide right threshold for drawCountSinceAsyncFlush at runtime depending on
	// amount of stall measured in AccessEFB.

	// We can't do this with perf queries enabled since it can leave queries open.

	if (D3D::command_list_mgr->m_cpu_access_last_frame &&
		D3D::command_list_mgr->m_draws_since_last_execution > 100 &&
		!PerfQueryBase::ShouldEmulate())
	{
		D3D::command_list_mgr->m_draws_since_last_execution = 0;

		D3D::command_list_mgr->ExecuteQueuedWork();
	}
}

void VertexManager::ResetBuffer(u32 stride)
{
	if (s_cull_all)
	{
		s_pCurBufferPointer = m_vertex_cpu_buffer.data();
		s_pBaseBufferPointer = m_vertex_cpu_buffer.data();
		s_pEndBufferPointer = m_vertex_cpu_buffer.data() + MAXVBUFFERSIZE;

		IndexGenerator::Start(reinterpret_cast<u16*>(m_index_cpu_buffer.data()));
		return;
	}

	m_vertex_stream_buffer->AllocateSpaceInBuffer(MAXVBUFFERSIZE, stride);

	if (m_vertex_stream_buffer_reallocated)
	{
		D3D::command_list_mgr->SetCommandListDirtyState(COMMAND_LIST_STATE_VERTEX_BUFFER, true);
		m_vertex_stream_buffer_reallocated = false;
	}

	s_pBaseBufferPointer = static_cast<u8*>(m_vertex_stream_buffer->GetBaseCPUAddress());
	s_pEndBufferPointer  = s_pBaseBufferPointer + m_vertex_stream_buffer->GetSize();
	s_pCurBufferPointer  = static_cast<u8*>(m_vertex_stream_buffer->GetCPUAddressOfCurrentAllocation());
	m_vertex_draw_offset = static_cast<u32>(m_vertex_stream_buffer->GetOffsetOfCurrentAllocation());

	m_index_stream_buffer->AllocateSpaceInBuffer(MAXIBUFFERSIZE * sizeof(u16), sizeof(u16));

	if (m_index_stream_buffer_reallocated)
	{
		SetIndexBuffer();
		m_index_stream_buffer_reallocated = false;
	}

	m_index_draw_offset = static_cast<u32>(m_index_stream_buffer->GetOffsetOfCurrentAllocation());
	IndexGenerator::Start(static_cast<u16*>(m_index_stream_buffer->GetCPUAddressOfCurrentAllocation()));
}

}  // namespace