| 1 | // Copyright 2013 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/OGL/OGLStreamBuffer.h" |
| 5 | |
| 6 | #include "Common/Align.h" |
| 7 | #include "Common/MathUtil.h" |
| 8 | #include "Common/MemoryUtil.h" |
| 9 | |
| 10 | #include "VideoBackends/OGL/OGLConfig.h" |
| 11 | |
| 12 | #include "VideoCommon/DriverDetails.h" |
| 13 | |
| 14 | namespace OGL |
| 15 | { |
| 16 | // moved out of constructor, so m_buffer is allowed to be const |
| 17 | static u32 GenBuffer() |
| 18 | { |
| 19 | u32 id; |
| 20 | glGenBuffers(1, &id); |
| 21 | return id; |
| 22 | } |
| 23 | |
| 24 | StreamBuffer::StreamBuffer(u32 type, u32 size) |
| 25 | : m_buffer(GenBuffer()), m_buffertype(type), m_size(MathUtil::NextPowerOf2(size)), |
| 26 | m_bit_per_slot(MathUtil::IntLog2(MathUtil::NextPowerOf2(size) / SYNC_POINTS)) |
| 27 | { |
| 28 | m_iterator = 0; |
| 29 | m_used_iterator = 0; |
| 30 | m_free_iterator = 0; |
| 31 | } |
| 32 | |
| 33 | StreamBuffer::~StreamBuffer() |
| 34 | { |
| 35 | glDeleteBuffers(1, &m_buffer); |
| 36 | } |
| 37 | |
| 38 | /* Shared synchronization code for ring buffers |
| 39 | * |
| 40 | * The next three functions are to create/delete/use the OpenGL synchronization. |
| 41 | * ARB_sync (OpenGL 3.2) is used and required. |
| 42 | * |
| 43 | * To reduce overhead, the complete buffer is split up into SYNC_POINTS chunks. |
| 44 | * For each of this chunks, there is a fence which checks if this chunk is still in use. |
| 45 | * |
| 46 | * As our API allows to alloc more memory then it has to use, we have to catch how much is already |
| 47 | * written. |
| 48 | * |
| 49 | * m_iterator - writing position |
| 50 | * m_free_iterator - last position checked if free |
| 51 | * m_used_iterator - last position known to be written |
| 52 | * |
| 53 | * So on alloc, we have to wait for all slots between m_free_iterator and m_iterator (and set |
| 54 | * m_free_iterator to m_iterator afterwards). |
| 55 | * |
| 56 | * We also assume that this buffer is accessed by the GPU between the Unmap and Map function, |
| 57 | * so we may create the fences on the start of mapping. |
| 58 | * Some here, new fences for the chunks between m_used_iterator and m_iterator (also update |
| 59 | * m_used_iterator). |
| 60 | * |
| 61 | * As ring buffers have an ugly behavior on rollover, have fun to read this code ;) |
| 62 | */ |
| 63 | |
| 64 | void StreamBuffer::CreateFences() |
| 65 | { |
| 66 | for (int i = 0; i < SYNC_POINTS; i++) |
| 67 | { |
| 68 | m_fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 69 | } |
| 70 | } |
| 71 | void StreamBuffer::DeleteFences() |
| 72 | { |
| 73 | for (int i = Slot(m_free_iterator) + 1; i < SYNC_POINTS; i++) |
| 74 | { |
| 75 | glDeleteSync(m_fences[i]); |
| 76 | } |
| 77 | for (int i = 0; i < Slot(m_iterator); i++) |
| 78 | { |
| 79 | glDeleteSync(m_fences[i]); |
| 80 | } |
| 81 | } |
| 82 | void StreamBuffer::AllocMemory(u32 size) |
| 83 | { |
| 84 | // insert waiting slots for used memory |
| 85 | for (int i = Slot(m_used_iterator); i < Slot(m_iterator); i++) |
| 86 | { |
| 87 | m_fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 88 | } |
| 89 | m_used_iterator = m_iterator; |
| 90 | |
| 91 | // wait for new slots to end of buffer |
| 92 | for (int i = Slot(m_free_iterator) + 1; i <= Slot(m_iterator + size) && i < SYNC_POINTS; i++) |
| 93 | { |
| 94 | glClientWaitSync(m_fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); |
| 95 | glDeleteSync(m_fences[i]); |
| 96 | } |
| 97 | |
| 98 | // If we allocate a large amount of memory (A), commit a smaller amount, then allocate memory |
| 99 | // smaller than allocation A, we will have already waited for these fences in A, but not used |
| 100 | // the space. In this case, don't set m_free_iterator to a position before that which we know |
| 101 | // is safe to use, which would result in waiting on the same fence(s) next time. |
| 102 | if ((m_iterator + size) > m_free_iterator) |
| 103 | m_free_iterator = m_iterator + size; |
| 104 | |
| 105 | // if buffer is full |
| 106 | if (m_iterator + size >= m_size) |
| 107 | { |
| 108 | // insert waiting slots in unused space at the end of the buffer |
| 109 | for (int i = Slot(m_used_iterator); i < SYNC_POINTS; i++) |
| 110 | { |
| 111 | m_fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 112 | } |
| 113 | |
| 114 | // move to the start |
| 115 | m_used_iterator = m_iterator = 0; // offset 0 is always aligned |
| 116 | |
| 117 | // wait for space at the start |
| 118 | for (int i = 0; i <= Slot(m_iterator + size); i++) |
| 119 | { |
| 120 | glClientWaitSync(m_fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); |
| 121 | glDeleteSync(m_fences[i]); |
| 122 | } |
| 123 | m_free_iterator = m_iterator + size; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* The usual way to stream data to the GPU. |
| 128 | * Described here: https://www.opengl.org/wiki/Buffer_Object_Streaming#Unsynchronized_buffer_mapping |
| 129 | * Just do unsync appends until the buffer is full. |
| 130 | * When it's full, orphan (alloc a new buffer and free the old one) |
| 131 | * |
| 132 | * As reallocation is an overhead, this method isn't as fast as it is known to be. |
| 133 | */ |
| 134 | class MapAndOrphan : public StreamBuffer |
| 135 | { |
| 136 | public: |
| 137 | MapAndOrphan(u32 type, u32 size) : StreamBuffer(type, size) |
| 138 | { |
| 139 | glBindBuffer(m_buffertype, m_buffer); |
| 140 | glBufferData(m_buffertype, m_size, nullptr, GL_STREAM_DRAW); |
| 141 | } |
| 142 | |
| 143 | ~MapAndOrphan() override {} |
| 144 | std::pair<u8*, u32> Map(u32 size) override |
| 145 | { |
| 146 | if (m_iterator + size >= m_size) |
| 147 | { |
| 148 | glBufferData(m_buffertype, m_size, nullptr, GL_STREAM_DRAW); |
| 149 | m_iterator = 0; |
| 150 | } |
| 151 | u8* pointer = (u8*)glMapBufferRange(m_buffertype, m_iterator, size, |
| 152 | GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT | |
| 153 | GL_MAP_UNSYNCHRONIZED_BIT); |
| 154 | return std::make_pair(pointer, m_iterator); |
| 155 | } |
| 156 | |
| 157 | void Unmap(u32 used_size) override |
| 158 | { |
| 159 | glFlushMappedBufferRange(m_buffertype, 0, used_size); |
| 160 | glUnmapBuffer(m_buffertype); |
| 161 | m_iterator += used_size; |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | /* A modified streaming way without reallocation |
| 166 | * This one fixes the reallocation overhead of the MapAndOrphan one. |
| 167 | * So it alloc a ring buffer on initialization. |
| 168 | * But with this limited resource, we have to care about the CPU-GPU distance. |
| 169 | * Else this fifo may overflow. |
| 170 | * So we had traded orphan vs syncing. |
| 171 | */ |
| 172 | class MapAndSync : public StreamBuffer |
| 173 | { |
| 174 | public: |
| 175 | MapAndSync(u32 type, u32 size) : StreamBuffer(type, size) |
| 176 | { |
| 177 | CreateFences(); |
| 178 | glBindBuffer(m_buffertype, m_buffer); |
| 179 | glBufferData(m_buffertype, m_size, nullptr, GL_STREAM_DRAW); |
| 180 | } |
| 181 | |
| 182 | ~MapAndSync() override { DeleteFences(); } |
| 183 | std::pair<u8*, u32> Map(u32 size) override |
| 184 | { |
| 185 | AllocMemory(size); |
| 186 | u8* pointer = (u8*)glMapBufferRange(m_buffertype, m_iterator, size, |
| 187 | GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT | |
| 188 | GL_MAP_UNSYNCHRONIZED_BIT); |
| 189 | return std::make_pair(pointer, m_iterator); |
| 190 | } |
| 191 | |
| 192 | void Unmap(u32 used_size) override |
| 193 | { |
| 194 | glFlushMappedBufferRange(m_buffertype, 0, used_size); |
| 195 | glUnmapBuffer(m_buffertype); |
| 196 | m_iterator += used_size; |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | /* Streaming fifo without mapping overhead. |
| 201 | * This one usually requires ARB_buffer_storage (OpenGL 4.4). |
| 202 | * And is usually not available on OpenGL3 GPUs. |
| 203 | * |
| 204 | * ARB_buffer_storage allows us to render from a mapped buffer. |
| 205 | * So we map it persistently in the initialization. |
| 206 | * |
| 207 | * Unsync mapping sounds like an easy task, but it isn't for threaded drivers. |
| 208 | * So every mapping on current close-source driver _will_ end in |
| 209 | * at least a round trip time between two threads. |
| 210 | * |
| 211 | * As persistently mapped buffer can't use orphaning, we also have to sync. |
| 212 | */ |
| 213 | class BufferStorage : public StreamBuffer |
| 214 | { |
| 215 | public: |
| 216 | BufferStorage(u32 type, u32 size, bool _coherent = true) |
| 217 | : StreamBuffer(type, size), coherent(_coherent) |
| 218 | { |
| 219 | CreateFences(); |
| 220 | glBindBuffer(m_buffertype, m_buffer); |
| 221 | |
| 222 | // PERSISTANT_BIT to make sure that the buffer can be used while mapped |
| 223 | // COHERENT_BIT is set so we don't have to use a MemoryBarrier on write |
| 224 | // CLIENT_STORAGE_BIT is set since we access the buffer more frequently on the client side then |
| 225 | // server side |
| 226 | glBufferStorage(m_buffertype, m_size, nullptr, |
| 227 | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | |
| 228 | (coherent ? GL_MAP_COHERENT_BIT : 0)); |
| 229 | m_pointer = |
| 230 | (u8*)glMapBufferRange(m_buffertype, 0, m_size, |
| 231 | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | |
| 232 | (coherent ? GL_MAP_COHERENT_BIT : GL_MAP_FLUSH_EXPLICIT_BIT)); |
| 233 | } |
| 234 | |
| 235 | ~BufferStorage() override |
| 236 | { |
| 237 | DeleteFences(); |
| 238 | glUnmapBuffer(m_buffertype); |
| 239 | glBindBuffer(m_buffertype, 0); |
| 240 | } |
| 241 | |
| 242 | std::pair<u8*, u32> Map(u32 size) override |
| 243 | { |
| 244 | AllocMemory(size); |
| 245 | return std::make_pair(m_pointer + m_iterator, m_iterator); |
| 246 | } |
| 247 | |
| 248 | void Unmap(u32 used_size) override |
| 249 | { |
| 250 | if (!coherent) |
| 251 | glFlushMappedBufferRange(m_buffertype, m_iterator, used_size); |
| 252 | m_iterator += used_size; |
| 253 | } |
| 254 | |
| 255 | u8* m_pointer; |
| 256 | const bool coherent; |
| 257 | }; |
| 258 | |
| 259 | /* --- AMD only --- |
| 260 | * Another streaming fifo without mapping overhead. |
| 261 | * As we can't orphan without mapping, we have to sync. |
| 262 | * |
| 263 | * This one uses AMD_pinned_memory which is available on all AMD GPUs. |
| 264 | * OpenGL 4.4 drivers should use BufferStorage. |
| 265 | */ |
| 266 | class PinnedMemory : public StreamBuffer |
| 267 | { |
| 268 | public: |
| 269 | PinnedMemory(u32 type, u32 size) : StreamBuffer(type, size) |
| 270 | { |
| 271 | CreateFences(); |
| 272 | m_pointer = static_cast<u8*>(Common::AllocateAlignedMemory( |
| 273 | Common::AlignUp(m_size, ALIGN_PINNED_MEMORY), ALIGN_PINNED_MEMORY)); |
| 274 | glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, m_buffer); |
| 275 | glBufferData(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, |
| 276 | Common::AlignUp(m_size, ALIGN_PINNED_MEMORY), m_pointer, GL_STREAM_COPY); |
| 277 | glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0); |
| 278 | glBindBuffer(m_buffertype, m_buffer); |
| 279 | } |
| 280 | |
| 281 | ~PinnedMemory() override |
| 282 | { |
| 283 | DeleteFences(); |
| 284 | glBindBuffer(m_buffertype, 0); |
| 285 | glFinish(); // ogl pipeline must be flushed, else this buffer can be in use |
| 286 | Common::FreeAlignedMemory(m_pointer); |
| 287 | m_pointer = nullptr; |
| 288 | } |
| 289 | |
| 290 | std::pair<u8*, u32> Map(u32 size) override |
| 291 | { |
| 292 | AllocMemory(size); |
| 293 | return std::make_pair(m_pointer + m_iterator, m_iterator); |
| 294 | } |
| 295 | |
| 296 | void Unmap(u32 used_size) override { m_iterator += used_size; } |
| 297 | u8* m_pointer; |
| 298 | static const u32 ALIGN_PINNED_MEMORY = 4096; |
| 299 | }; |
| 300 | |
| 301 | /* Fifo based on the glBufferSubData call. |
| 302 | * As everything must be copied before glBufferSubData returns, |
| 303 | * an additional memcpy in the driver will be done. |
| 304 | * So this is a huge overhead, only use it if required. |
| 305 | */ |
| 306 | class BufferSubData : public StreamBuffer |
| 307 | { |
| 308 | public: |
| 309 | BufferSubData(u32 type, u32 size) : StreamBuffer(type, size) |
| 310 | { |
| 311 | glBindBuffer(m_buffertype, m_buffer); |
| 312 | glBufferData(m_buffertype, size, nullptr, GL_STATIC_DRAW); |
| 313 | m_pointer = new u8[m_size]; |
| 314 | } |
| 315 | |
| 316 | ~BufferSubData() override { delete[] m_pointer; } |
| 317 | std::pair<u8*, u32> Map(u32 size) override { return std::make_pair(m_pointer, 0); } |
| 318 | void Unmap(u32 used_size) override { glBufferSubData(m_buffertype, 0, used_size, m_pointer); } |
| 319 | u8* m_pointer; |
| 320 | }; |
| 321 | |
| 322 | /* Fifo based on the glBufferData call. |
| 323 | * Some trashy drivers stall in BufferSubData. |
| 324 | * So here we use glBufferData, which realloc this buffer every time. |
| 325 | * This may avoid stalls, but it is a bigger overhead than BufferSubData. |
| 326 | */ |
| 327 | class BufferData : public StreamBuffer |
| 328 | { |
| 329 | public: |
| 330 | BufferData(u32 type, u32 size) : StreamBuffer(type, size) |
| 331 | { |
| 332 | glBindBuffer(m_buffertype, m_buffer); |
| 333 | m_pointer = new u8[m_size]; |
| 334 | } |
| 335 | |
| 336 | ~BufferData() override { delete[] m_pointer; } |
| 337 | std::pair<u8*, u32> Map(u32 size) override { return std::make_pair(m_pointer, 0); } |
| 338 | void Unmap(u32 used_size) override |
| 339 | { |
| 340 | glBufferData(m_buffertype, used_size, m_pointer, GL_STREAM_DRAW); |
| 341 | } |
| 342 | |
| 343 | u8* m_pointer; |
| 344 | }; |
| 345 | |
| 346 | // Chooses the best streaming method based on the supported extensions and known issues |
| 347 | std::unique_ptr<StreamBuffer> StreamBuffer::Create(u32 type, u32 size) |
| 348 | { |
| 349 | // without basevertex support, only streaming methods whith uploads everything to zero works fine: |
| 350 | if (!g_ogl_config.bSupportsGLBaseVertex) |
| 351 | { |
| 352 | if (!DriverDetails::HasBug(DriverDetails::BUG_BROKEN_BUFFER_STREAM)) |
| 353 | return std::make_unique<BufferSubData>(type, size); |
| 354 | |
| 355 | // BufferData is by far the worst way, only use it if needed |
| 356 | return std::make_unique<BufferData>(type, size); |
| 357 | } |
| 358 | |
| 359 | // Prefer the syncing buffers over the orphaning one |
| 360 | if (g_ogl_config.bSupportsGLSync) |
| 361 | { |
| 362 | // pinned memory is much faster than buffer storage on AMD cards |
| 363 | if (g_ogl_config.bSupportsGLPinnedMemory && |
| 364 | !(DriverDetails::HasBug(DriverDetails::BUG_BROKEN_PINNED_MEMORY))) |
| 365 | return std::make_unique<PinnedMemory>(type, size); |
| 366 | |
| 367 | // buffer storage works well in most situations |
| 368 | if (g_ogl_config.bSupportsGLBufferStorage && |
| 369 | !(DriverDetails::HasBug(DriverDetails::BUG_BROKEN_BUFFER_STORAGE) && |
| 370 | type == GL_ARRAY_BUFFER) && |
| 371 | !(DriverDetails::HasBug(DriverDetails::BUG_INTEL_BROKEN_BUFFER_STORAGE) && |
| 372 | type == GL_ELEMENT_ARRAY_BUFFER)) |
| 373 | return std::make_unique<BufferStorage>(type, size); |
| 374 | |
| 375 | // don't fall back to MapAnd* for Nvidia drivers |
| 376 | if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_UNSYNC_MAPPING)) |
| 377 | { |
| 378 | if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_BUFFER_STREAM)) |
| 379 | return std::make_unique<BufferData>(type, size); |
| 380 | else |
| 381 | return std::make_unique<BufferSubData>(type, size); |
| 382 | } |
| 383 | |
| 384 | // mapping fallback |
| 385 | if (g_ogl_config.bSupportsGLSync) |
| 386 | return std::make_unique<MapAndSync>(type, size); |
| 387 | } |
| 388 | |
| 389 | // default fallback, should work everywhere, but isn't the best way to do this job |
| 390 | return std::make_unique<MapAndOrphan>(type, size); |
| 391 | } |
| 392 | } // namespace OGL |