2018-03-19 22:10:59 +01:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <vector>
|
|
|
|
#include "common/alignment.h"
|
|
|
|
#include "common/assert.h"
|
2018-11-06 10:42:33 +01:00
|
|
|
#include "common/microprofile.h"
|
2018-03-19 22:10:59 +01:00
|
|
|
#include "video_core/renderer_opengl/gl_state.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_stream_buffer.h"
|
|
|
|
|
2018-11-06 10:42:33 +01:00
|
|
|
MICROPROFILE_DEFINE(OpenGL_StreamBuffer, "OpenGL", "Stream Buffer Orphaning",
|
|
|
|
MP_RGB(128, 128, 192));
|
|
|
|
|
2018-08-21 10:18:27 +02:00
|
|
|
namespace OpenGL {
|
|
|
|
|
2019-04-28 23:12:28 +02:00
|
|
|
OGLStreamBuffer::OGLStreamBuffer(GLsizeiptr size, bool vertex_data_usage, bool prefer_coherent,
|
|
|
|
bool use_persistent)
|
2019-01-06 06:07:20 +01:00
|
|
|
: buffer_size(size) {
|
2018-08-09 21:31:46 +02:00
|
|
|
gl_buffer.Create();
|
2018-03-19 22:10:59 +01:00
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
GLsizeiptr allocate_size = size;
|
2019-01-06 06:07:20 +01:00
|
|
|
if (vertex_data_usage) {
|
2018-08-09 21:31:46 +02:00
|
|
|
// On AMD GPU there is a strange crash in indexed drawing. The crash happens when the buffer
|
|
|
|
// read position is near the end and is an out-of-bound access to the vertex buffer. This is
|
|
|
|
// probably a bug in the driver and is related to the usage of vec3<byte> attributes in the
|
|
|
|
// vertex array. Doubling the allocation size for the vertex buffer seems to avoid the
|
|
|
|
// crash.
|
|
|
|
allocate_size *= 2;
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2019-04-28 23:12:28 +02:00
|
|
|
if (use_persistent) {
|
2018-08-09 21:31:46 +02:00
|
|
|
persistent = true;
|
|
|
|
coherent = prefer_coherent;
|
2018-09-09 17:51:43 +02:00
|
|
|
const GLbitfield flags =
|
2018-08-09 21:31:46 +02:00
|
|
|
GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | (coherent ? GL_MAP_COHERENT_BIT : 0);
|
2019-01-06 06:07:20 +01:00
|
|
|
glNamedBufferStorage(gl_buffer.handle, allocate_size, nullptr, flags);
|
|
|
|
mapped_ptr = static_cast<u8*>(glMapNamedBufferRange(
|
|
|
|
gl_buffer.handle, 0, buffer_size, flags | (coherent ? 0 : GL_MAP_FLUSH_EXPLICIT_BIT)));
|
2018-08-09 21:31:46 +02:00
|
|
|
} else {
|
2019-01-06 06:07:20 +01:00
|
|
|
glNamedBufferData(gl_buffer.handle, allocate_size, nullptr, GL_STREAM_DRAW);
|
2018-08-09 21:31:46 +02:00
|
|
|
}
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
OGLStreamBuffer::~OGLStreamBuffer() {
|
|
|
|
if (persistent) {
|
2019-01-06 06:07:20 +01:00
|
|
|
glUnmapNamedBuffer(gl_buffer.handle);
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
|
|
|
gl_buffer.Release();
|
|
|
|
}
|
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
GLuint OGLStreamBuffer::GetHandle() const {
|
|
|
|
return gl_buffer.handle;
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
GLsizeiptr OGLStreamBuffer::GetSize() const {
|
|
|
|
return buffer_size;
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
std::tuple<u8*, GLintptr, bool> OGLStreamBuffer::Map(GLsizeiptr size, GLintptr alignment) {
|
2018-03-19 22:10:59 +01:00
|
|
|
ASSERT(size <= buffer_size);
|
2018-08-09 21:31:46 +02:00
|
|
|
ASSERT(alignment <= buffer_size);
|
|
|
|
mapped_size = size;
|
2018-03-19 22:10:59 +01:00
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
if (alignment > 0) {
|
2018-09-15 15:21:06 +02:00
|
|
|
buffer_pos = Common::AlignUp<std::size_t>(buffer_pos, alignment);
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
bool invalidate = false;
|
2018-03-19 22:10:59 +01:00
|
|
|
if (buffer_pos + size > buffer_size) {
|
|
|
|
buffer_pos = 0;
|
2018-08-09 21:31:46 +02:00
|
|
|
invalidate = true;
|
2018-03-19 22:10:59 +01:00
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
if (persistent) {
|
2019-01-06 06:07:20 +01:00
|
|
|
glUnmapNamedBuffer(gl_buffer.handle);
|
2018-08-09 21:31:46 +02:00
|
|
|
}
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2018-09-21 13:59:00 +02:00
|
|
|
if (invalidate || !persistent) {
|
2018-11-06 10:42:33 +01:00
|
|
|
MICROPROFILE_SCOPE(OpenGL_StreamBuffer);
|
2018-08-09 21:31:46 +02:00
|
|
|
GLbitfield flags = GL_MAP_WRITE_BIT | (persistent ? GL_MAP_PERSISTENT_BIT : 0) |
|
|
|
|
(coherent ? GL_MAP_COHERENT_BIT : GL_MAP_FLUSH_EXPLICIT_BIT) |
|
|
|
|
(invalidate ? GL_MAP_INVALIDATE_BUFFER_BIT : GL_MAP_UNSYNCHRONIZED_BIT);
|
|
|
|
mapped_ptr = static_cast<u8*>(
|
2019-01-06 06:07:20 +01:00
|
|
|
glMapNamedBufferRange(gl_buffer.handle, buffer_pos, buffer_size - buffer_pos, flags));
|
2018-08-09 21:31:46 +02:00
|
|
|
mapped_offset = buffer_pos;
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
return std::make_tuple(mapped_ptr + buffer_pos - mapped_offset, buffer_pos, invalidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OGLStreamBuffer::Unmap(GLsizeiptr size) {
|
|
|
|
ASSERT(size <= mapped_size);
|
|
|
|
|
|
|
|
if (!coherent && size > 0) {
|
2019-01-06 06:07:20 +01:00
|
|
|
glFlushMappedNamedBufferRange(gl_buffer.handle, buffer_pos - mapped_offset, size);
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
if (!persistent) {
|
2019-01-06 06:07:20 +01:00
|
|
|
glUnmapNamedBuffer(gl_buffer.handle);
|
2018-08-09 21:31:46 +02:00
|
|
|
}
|
2018-03-19 22:10:59 +01:00
|
|
|
|
2018-08-09 21:31:46 +02:00
|
|
|
buffer_pos += size;
|
2018-03-19 22:10:59 +01:00
|
|
|
}
|
2018-08-21 10:18:27 +02:00
|
|
|
|
|
|
|
} // namespace OpenGL
|