Video Core: initial Implementation of InstanceDraw Packaging

This commit is contained in:
Fernando Sahmkow 2019-09-15 11:48:54 -04:00 committed by FernandoS27
parent b31880dc5e
commit ba02d564f8
7 changed files with 192 additions and 11 deletions

View file

@ -405,6 +405,12 @@ bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) {
return true;
}
bool RasterizerOpenGL::AccelerateDrawMultiBatch(bool is_indexed) {
accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays;
DrawMultiArrays();
return true;
}
template <typename Map, typename Interval>
static constexpr auto RangeFromInterval(Map& map, const Interval& interval) {
return boost::make_iterator_range(map.equal_range(interval));
@ -688,7 +694,7 @@ void RasterizerOpenGL::Clear() {
}
}
void RasterizerOpenGL::DrawArrays() {
void RasterizerOpenGL::DrawPrelude() {
if (accelerate_draw == AccelDraw::Disabled)
return;
@ -743,10 +749,7 @@ void RasterizerOpenGL::DrawArrays() {
// Upload vertex and index data.
SetupVertexBuffer(vao);
SetupVertexInstances(vao);
const GLintptr index_buffer_offset = SetupIndexBuffer();
// Setup draw parameters. It will automatically choose what glDraw* method to use.
const DrawParameters params = SetupDraw(index_buffer_offset);
index_buffer_offset = SetupIndexBuffer();
// Prepare packed bindings.
bind_ubo_pushbuffer.Setup(0);
@ -754,7 +757,8 @@ void RasterizerOpenGL::DrawArrays() {
// Setup shaders and their used resources.
texture_cache.GuardSamplers(true);
SetupShaders(params.primitive_mode);
const auto primitive_mode = MaxwellToGL::PrimitiveTopology(gpu.regs.draw.topology);
SetupShaders(primitive_mode);
texture_cache.GuardSamplers(false);
ConfigureFramebuffers(state);
@ -778,11 +782,80 @@ void RasterizerOpenGL::DrawArrays() {
if (texture_cache.TextureBarrier()) {
glTextureBarrier();
}
}
params.DispatchDraw();
void RasterizerOpenGL::DrawArrays() {
DrawPrelude();
auto& maxwell3d = system.GPU().Maxwell3D();
auto& regs = maxwell3d.regs;
auto current_instance = maxwell3d.state.current_instance;
auto primitive_mode = MaxwellToGL::PrimitiveTopology(regs.draw.topology);
if (accelerate_draw == AccelDraw::Indexed) {
auto index_format = MaxwellToGL::IndexFormat(regs.index_array.format);
auto count = regs.index_array.count;
auto base_vertex = static_cast<GLint>(regs.vb_element_base);
const auto index_buffer_ptr = reinterpret_cast<const void*>(index_buffer_offset);
if (current_instance > 0) {
glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, count, index_format,
index_buffer_ptr, 1, base_vertex,
current_instance);
} else {
glDrawElementsBaseVertex(primitive_mode, count, index_format, index_buffer_ptr,
base_vertex);
}
} else {
auto count = regs.vertex_buffer.count;
auto vertex_first = regs.vertex_buffer.first;
if (current_instance > 0) {
glDrawArraysInstancedBaseInstance(primitive_mode, vertex_first, count, 1,
current_instance);
} else {
glDrawArrays(primitive_mode, vertex_first, count);
}
}
accelerate_draw = AccelDraw::Disabled;
gpu.dirty.memory_general = false;
maxwell3d.dirty.memory_general = false;
}
#pragma optimize("", off)
void RasterizerOpenGL::DrawMultiArrays() {
DrawPrelude();
auto& maxwell3d = system.GPU().Maxwell3D();
auto& regs = maxwell3d.regs;
auto& draw_setup = maxwell3d.mme_draw;
auto num_instances = draw_setup.instance_count;
auto base_instance = static_cast<GLint>(regs.vb_base_instance);
auto primitive_mode = MaxwellToGL::PrimitiveTopology(regs.draw.topology);
if (draw_setup.current_mode == Tegra::Engines::Maxwell3D::MMMEDrawMode::Indexed) {
auto index_format = MaxwellToGL::IndexFormat(regs.index_array.format);
auto count = regs.index_array.count;
auto base_vertex = static_cast<GLint>(regs.vb_element_base);
const auto index_buffer_ptr = reinterpret_cast<const void*>(index_buffer_offset);
if (num_instances > 1) {
glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, count, index_format,
index_buffer_ptr, num_instances,
base_vertex, base_instance);
} else {
glDrawElementsBaseVertex(primitive_mode, count, index_format, index_buffer_ptr,
base_vertex);
}
} else {
auto count = regs.vertex_buffer.count;
auto vertex_first = regs.vertex_buffer.first;
if (num_instances > 1) {
glDrawArraysInstancedBaseInstance(primitive_mode, vertex_first, count, num_instances,
base_instance);
} else {
glDrawArrays(primitive_mode, vertex_first, count);
}
}
accelerate_draw = AccelDraw::Disabled;
maxwell3d.dirty.memory_general = false;
}
void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) {

View file

@ -58,6 +58,7 @@ public:
~RasterizerOpenGL() override;
void DrawArrays() override;
void DrawMultiArrays() override;
void Clear() override;
void DispatchCompute(GPUVAddr code_addr) override;
void FlushAll() override;
@ -72,6 +73,7 @@ public:
bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
u32 pixel_stride) override;
bool AccelerateDrawBatch(bool is_indexed) override;
bool AccelerateDrawMultiBatch(bool is_indexed) override;
void UpdatePagesCachedCount(VAddr addr, u64 size, int delta) override;
void LoadDiskResources(const std::atomic_bool& stop_loading,
const VideoCore::DiskResourceLoadCallback& callback) override;
@ -136,6 +138,8 @@ private:
void SetupGlobalMemory(const GLShader::GlobalMemoryEntry& entry, GPUVAddr gpu_addr,
std::size_t size);
void DrawPrelude();
/// Configures the current textures to use for the draw command. Returns shaders texture buffer
/// usage.
TextureBufferUsage SetupDrawTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
@ -252,6 +256,8 @@ private:
DrawParameters SetupDraw(GLintptr index_buffer_offset);
GLintptr index_buffer_offset;
void SetupShaders(GLenum primitive_mode);
enum class AccelDraw { Disabled, Arrays, Indexed };

View file

@ -964,7 +964,7 @@ private:
switch (element) {
case 2:
// Config pack's first value is instance_id.
return {"config_pack[0]", Type::Uint};
return {"gl_InstanceID", Type::Uint};
case 3:
return {"gl_VertexID", Type::Int};
}