Debian Codegraph @

Source/Core/VideoBackends/OGL/OGLBoundingBox.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/OGL/OGLBoundingBox.h"
5
6 #include <cstring>
7
8 #include "VideoBackends/OGL/OGLGfx.h"
9 #include "VideoCommon/DriverDetails.h"
10
11 namespace OGL
12 {
13 OGLBoundingBox::~OGLBoundingBox()
14 {
15 if (m_buffer_id)
16 glDeleteBuffers(1, &m_buffer_id);
17 }
18
19 bool OGLBoundingBox::Initialize()
20 {
21 const BBoxType initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
22
23 glGenBuffers(1, &m_buffer_id);
24 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer_id);
25 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(initial_values), initial_values, GL_DYNAMIC_DRAW);
26 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_buffer_id);
27
28 return true;
29 }
30
31 std::vector<BBoxType> OGLBoundingBox::Read(u32 index, u32 length)
32 {
33 std::vector<BBoxType> values(length);
34 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer_id);
35
36 // Using glMapBufferRange to read back the contents of the SSBO is extremely slow
37 // on nVidia drivers. This is more noticeable at higher internal resolutions.
38 // Using glGetBufferSubData instead does not seem to exhibit this slowdown.
39 if (!DriverDetails::HasBug(DriverDetails::BUG_SLOW_GETBUFFERSUBDATA) &&
40 !static_cast<OGLGfx*>(g_gfx.get())->IsGLES())
41 {
42 // We also need to ensure the the CPU does not receive stale values which have been updated by
43 // the GPU. Apparently the buffer here is not coherent on NVIDIA drivers. Not sure if this is a
44 // driver bug/spec violation or not, one would think that glGetBufferSubData() would invalidate
45 // any caches as needed, but this path is only used on NVIDIA anyway, so it's fine. A point to
46 // note is that according to ARB_debug_report, it's moved from video to host memory, which would
47 // explain why it needs the cache invalidate.
48 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
49
50 glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(BBoxType) * index,
51 sizeof(BBoxType) * length, values.data());
52 }
53 else
54 {
55 // Using glMapBufferRange is faster on AMD cards by a measurable margin.
56 void* ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(BBoxType) * NUM_BBOX_VALUES,
57 GL_MAP_READ_BIT);
58 if (ptr)
59 {
60 std::memcpy(values.data(), static_cast<const u8*>(ptr) + sizeof(BBoxType) * index,
61 sizeof(BBoxType) * length);
62
63 glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
64 }
65 }
66
67 glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
68 return values;
69 }
70
71 void OGLBoundingBox::Write(u32 index, std::span<const BBoxType> values)
72 {
73 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer_id);
74 glBufferSubData(GL_SHADER_STORAGE_BUFFER, sizeof(BBoxType) * index,
75 sizeof(BBoxType) * values.size(), values.data());
76 glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
77 }
78
79 } // namespace OGL