2014-11-13 22:26:49 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-11-13 22:26:49 +00:00
|
|
|
|
2021-04-17 17:14:52 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2016-05-26 17:42:07 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2015-09-18 16:40:00 +00:00
|
|
|
#include "Common/GL/GLUtil.h"
|
|
|
|
|
2020-09-15 12:00:24 +00:00
|
|
|
#include "VideoBackends/OGL/OGLBoundingBox.h"
|
|
|
|
#include "VideoBackends/OGL/OGLRender.h"
|
2014-11-13 22:26:49 +00:00
|
|
|
|
2016-05-11 12:19:59 +00:00
|
|
|
#include "VideoCommon/DriverDetails.h"
|
2014-11-13 22:26:49 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
|
2021-04-17 17:14:52 +00:00
|
|
|
enum : u32
|
|
|
|
{
|
|
|
|
NUM_BBOX_VALUES = 4,
|
|
|
|
};
|
|
|
|
|
2014-11-13 22:26:49 +00:00
|
|
|
static GLuint s_bbox_buffer_id;
|
2021-04-17 17:14:52 +00:00
|
|
|
static std::array<s32, NUM_BBOX_VALUES> s_bbox_values;
|
|
|
|
static std::array<bool, NUM_BBOX_VALUES> s_bbox_dirty;
|
|
|
|
static bool s_bbox_valid = false;
|
2014-11-13 22:26:49 +00:00
|
|
|
|
|
|
|
namespace OGL
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
void BoundingBox::Init()
|
OGL: implement Bounding Box on systems w/o SSBO
This commit should have zero performance effect if SSBOs are supported.
If they aren't (e.g. on all Macs), this commit alters FramebufferManager
to attach a new stencil buffer and VertexManager to draw to it when
bounding box is active. `BBoxRead` gets the pixel data from the buffer
and dumbly loops through it to find the bounding box.
This patch can run Paper Mario: The Thousand-Year Door at almost full
speed (50–60 FPS) without Dual-Core enabled for all common bounding
box-using actions I tested (going through pipes, Plane Mode, Paper
Mode, Prof. Frankly's gate, combat, walking around the overworld, etc.)
on my computer (macOS 10.12.3, 2.8 GHz Intel Core i7, 16 GB 1600 MHz
DDR3, and Intel Iris 1536 MB).
A few more demanding scenes (e.g. the self-building bridge on the way
to Petalburg) slow to ~15% of their speed without this patch (though
they don't run quite at full speed even on master). The slowdown is
caused almost solely by `glReadPixels` in `OGL::BoundingBox::Get`.
Other implementation ideas:
- Use a stencil buffer that's separate from the depth buffer. This would
require ARB_texture_stencil8 / OpenGL 4.4, which isn't available on
macOS.
- Use `glGetTexImage` instead of `glReadPixels`. This is ~5 FPS slower
on my computer, presumably because it has to transfer the entire
combined depth-stencil buffer instead of only the stencil data.
Getting only stencil data from `glGetTexImage` requires
ARB_texture_stencil8 / OpenGL 4.4, which (again) is not available on
macOS.
- Don't use a PBO, and use `glReadPixels` synchronously. This has no
visible performance effect on my computer, and is theoretically
slower.
2017-03-05 23:34:30 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsBBox)
|
OGL: implement Bounding Box on systems w/o SSBO
This commit should have zero performance effect if SSBOs are supported.
If they aren't (e.g. on all Macs), this commit alters FramebufferManager
to attach a new stencil buffer and VertexManager to draw to it when
bounding box is active. `BBoxRead` gets the pixel data from the buffer
and dumbly loops through it to find the bounding box.
This patch can run Paper Mario: The Thousand-Year Door at almost full
speed (50–60 FPS) without Dual-Core enabled for all common bounding
box-using actions I tested (going through pipes, Plane Mode, Paper
Mode, Prof. Frankly's gate, combat, walking around the overworld, etc.)
on my computer (macOS 10.12.3, 2.8 GHz Intel Core i7, 16 GB 1600 MHz
DDR3, and Intel Iris 1536 MB).
A few more demanding scenes (e.g. the self-building bridge on the way
to Petalburg) slow to ~15% of their speed without this patch (though
they don't run quite at full speed even on master). The slowdown is
caused almost solely by `glReadPixels` in `OGL::BoundingBox::Get`.
Other implementation ideas:
- Use a stencil buffer that's separate from the depth buffer. This would
require ARB_texture_stencil8 / OpenGL 4.4, which isn't available on
macOS.
- Use `glGetTexImage` instead of `glReadPixels`. This is ~5 FPS slower
on my computer, presumably because it has to transfer the entire
combined depth-stencil buffer instead of only the stencil data.
Getting only stencil data from `glGetTexImage` requires
ARB_texture_stencil8 / OpenGL 4.4, which (again) is not available on
macOS.
- Don't use a PBO, and use `glReadPixels` synchronously. This has no
visible performance effect on my computer, and is theoretically
slower.
2017-03-05 23:34:30 +00:00
|
|
|
return;
|
|
|
|
|
2021-04-17 17:14:52 +00:00
|
|
|
const s32 initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
|
|
|
|
std::memcpy(s_bbox_values.data(), initial_values, sizeof(s_bbox_values));
|
|
|
|
s_bbox_dirty = {};
|
|
|
|
s_bbox_valid = true;
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
glGenBuffers(1, &s_bbox_buffer_id);
|
|
|
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
|
2021-04-17 17:14:52 +00:00
|
|
|
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(initial_values), initial_values, GL_DYNAMIC_DRAW);
|
2019-02-15 01:59:50 +00:00
|
|
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, s_bbox_buffer_id);
|
2014-11-13 22:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BoundingBox::Shutdown()
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsBBox)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glDeleteBuffers(1, &s_bbox_buffer_id);
|
2014-11-13 22:26:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 17:14:52 +00:00
|
|
|
void BoundingBox::Flush()
|
2014-11-13 22:26:49 +00:00
|
|
|
{
|
2021-04-17 17:14:52 +00:00
|
|
|
s_bbox_valid = false;
|
|
|
|
|
|
|
|
if (std::none_of(s_bbox_dirty.begin(), s_bbox_dirty.end(), [](bool dirty) { return dirty; }))
|
2019-02-15 01:59:50 +00:00
|
|
|
return;
|
OGL: implement Bounding Box on systems w/o SSBO
This commit should have zero performance effect if SSBOs are supported.
If they aren't (e.g. on all Macs), this commit alters FramebufferManager
to attach a new stencil buffer and VertexManager to draw to it when
bounding box is active. `BBoxRead` gets the pixel data from the buffer
and dumbly loops through it to find the bounding box.
This patch can run Paper Mario: The Thousand-Year Door at almost full
speed (50–60 FPS) without Dual-Core enabled for all common bounding
box-using actions I tested (going through pipes, Plane Mode, Paper
Mode, Prof. Frankly's gate, combat, walking around the overworld, etc.)
on my computer (macOS 10.12.3, 2.8 GHz Intel Core i7, 16 GB 1600 MHz
DDR3, and Intel Iris 1536 MB).
A few more demanding scenes (e.g. the self-building bridge on the way
to Petalburg) slow to ~15% of their speed without this patch (though
they don't run quite at full speed even on master). The slowdown is
caused almost solely by `glReadPixels` in `OGL::BoundingBox::Get`.
Other implementation ideas:
- Use a stencil buffer that's separate from the depth buffer. This would
require ARB_texture_stencil8 / OpenGL 4.4, which isn't available on
macOS.
- Use `glGetTexImage` instead of `glReadPixels`. This is ~5 FPS slower
on my computer, presumably because it has to transfer the entire
combined depth-stencil buffer instead of only the stencil data.
Getting only stencil data from `glGetTexImage` requires
ARB_texture_stencil8 / OpenGL 4.4, which (again) is not available on
macOS.
- Don't use a PBO, and use `glReadPixels` synchronously. This has no
visible performance effect on my computer, and is theoretically
slower.
2017-03-05 23:34:30 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
|
2021-04-17 17:14:52 +00:00
|
|
|
|
|
|
|
for (u32 start = 0; start < NUM_BBOX_VALUES;)
|
|
|
|
{
|
|
|
|
if (!s_bbox_dirty[start])
|
|
|
|
{
|
|
|
|
start++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 end = start + 1;
|
|
|
|
s_bbox_dirty[start] = false;
|
|
|
|
for (; end < NUM_BBOX_VALUES; end++)
|
|
|
|
{
|
|
|
|
if (!s_bbox_dirty[end])
|
|
|
|
break;
|
|
|
|
|
|
|
|
s_bbox_dirty[end] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
glBufferSubData(GL_SHADER_STORAGE_BUFFER, start * sizeof(s32), (end - start) * sizeof(s32),
|
|
|
|
&s_bbox_values[start]);
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
|
2014-11-13 22:26:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 17:14:52 +00:00
|
|
|
void BoundingBox::Readback()
|
2014-11-13 22:26:49 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
|
2021-05-29 06:50:33 +00:00
|
|
|
|
|
|
|
// Using glMapBufferRange to read back the contents of the SSBO is extremely slow
|
|
|
|
// on nVidia drivers. This is more noticeable at higher internal resolutions.
|
|
|
|
// Using glGetBufferSubData instead does not seem to exhibit this slowdown.
|
2019-02-15 01:59:50 +00:00
|
|
|
if (!DriverDetails::HasBug(DriverDetails::BUG_SLOW_GETBUFFERSUBDATA) &&
|
|
|
|
!static_cast<Renderer*>(g_renderer.get())->IsGLES())
|
2016-05-11 12:19:59 +00:00
|
|
|
{
|
2021-05-29 06:50:33 +00:00
|
|
|
// We also need to ensure the the CPU does not receive stale values which have been updated by
|
|
|
|
// the GPU. Apparently the buffer here is not coherent on NVIDIA drivers. Not sure if this is a
|
|
|
|
// driver bug/spec violation or not, one would think that glGetBufferSubData() would invalidate
|
|
|
|
// any caches as needed, but this path is only used on NVIDIA anyway, so it's fine. A point to
|
|
|
|
// note is that according to ARB_debug_report, it's moved from video to host memory, which would
|
|
|
|
// explain why it needs the cache invalidate.
|
|
|
|
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
|
|
|
|
|
2021-04-17 17:14:52 +00:00
|
|
|
std::array<s32, NUM_BBOX_VALUES> gpu_values;
|
|
|
|
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(s32) * NUM_BBOX_VALUES,
|
|
|
|
gpu_values.data());
|
|
|
|
for (u32 i = 0; i < NUM_BBOX_VALUES; i++)
|
|
|
|
{
|
|
|
|
if (!s_bbox_dirty[i])
|
|
|
|
s_bbox_values[i] = gpu_values[i];
|
|
|
|
}
|
2016-05-11 12:19:59 +00:00
|
|
|
}
|
|
|
|
else
|
2014-11-13 22:26:49 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
// Using glMapBufferRange is faster on AMD cards by a measurable margin.
|
2021-04-17 17:14:52 +00:00
|
|
|
void* ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(s32) * NUM_BBOX_VALUES,
|
2019-02-15 01:59:50 +00:00
|
|
|
GL_MAP_READ_BIT);
|
|
|
|
if (ptr)
|
2016-05-11 12:19:59 +00:00
|
|
|
{
|
2021-04-17 17:14:52 +00:00
|
|
|
for (u32 i = 0; i < NUM_BBOX_VALUES; i++)
|
|
|
|
{
|
|
|
|
if (!s_bbox_dirty[i])
|
|
|
|
{
|
|
|
|
std::memcpy(&s_bbox_values[i], reinterpret_cast<const u8*>(ptr) + sizeof(s32) * i,
|
|
|
|
sizeof(s32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
|
2016-05-11 12:19:59 +00:00
|
|
|
}
|
2014-11-13 22:26:49 +00:00
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
|
2021-04-17 17:14:52 +00:00
|
|
|
s_bbox_valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoundingBox::Set(int index, int value)
|
|
|
|
{
|
|
|
|
if (s_bbox_valid && s_bbox_values[index] == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s_bbox_values[index] = value;
|
|
|
|
s_bbox_dirty[index] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BoundingBox::Get(int index)
|
|
|
|
{
|
|
|
|
if (!s_bbox_valid)
|
|
|
|
Readback();
|
|
|
|
|
|
|
|
return s_bbox_values[index];
|
OGL: implement Bounding Box on systems w/o SSBO
This commit should have zero performance effect if SSBOs are supported.
If they aren't (e.g. on all Macs), this commit alters FramebufferManager
to attach a new stencil buffer and VertexManager to draw to it when
bounding box is active. `BBoxRead` gets the pixel data from the buffer
and dumbly loops through it to find the bounding box.
This patch can run Paper Mario: The Thousand-Year Door at almost full
speed (50–60 FPS) without Dual-Core enabled for all common bounding
box-using actions I tested (going through pipes, Plane Mode, Paper
Mode, Prof. Frankly's gate, combat, walking around the overworld, etc.)
on my computer (macOS 10.12.3, 2.8 GHz Intel Core i7, 16 GB 1600 MHz
DDR3, and Intel Iris 1536 MB).
A few more demanding scenes (e.g. the self-building bridge on the way
to Petalburg) slow to ~15% of their speed without this patch (though
they don't run quite at full speed even on master). The slowdown is
caused almost solely by `glReadPixels` in `OGL::BoundingBox::Get`.
Other implementation ideas:
- Use a stencil buffer that's separate from the depth buffer. This would
require ARB_texture_stencil8 / OpenGL 4.4, which isn't available on
macOS.
- Use `glGetTexImage` instead of `glReadPixels`. This is ~5 FPS slower
on my computer, presumably because it has to transfer the entire
combined depth-stencil buffer instead of only the stencil data.
Getting only stencil data from `glGetTexImage` requires
ARB_texture_stencil8 / OpenGL 4.4, which (again) is not available on
macOS.
- Don't use a PBO, and use `glReadPixels` synchronously. This has no
visible performance effect on my computer, and is theoretically
slower.
2017-03-05 23:34:30 +00:00
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
}; // namespace OGL
|