1 // Copyright 2018 Citra Emulator Project
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4
5 #include <deque>
6 #include <vector>
7 #include "common/alignment.h"
8 #include "common/assert.h"
9 #include "common/microprofile.h"
10 #include "video_core/renderer_opengl/gl_state.h"
11 #include "video_core/renderer_opengl/gl_stream_buffer.h"
12
13 MICROPROFILE_DEFINE(OpenGL_StreamBuffer, "OpenGL", "Stream Buffer Orphaning",
14 MP_RGB(128, 128, 192));
15
16 namespace OpenGL {
17
OGLStreamBuffer(GLenum target,GLsizeiptr size,bool array_buffer_for_amd,bool prefer_coherent)18 OGLStreamBuffer::OGLStreamBuffer(GLenum target, GLsizeiptr size, bool array_buffer_for_amd,
19 bool prefer_coherent)
20 : gl_target(target), buffer_size(size) {
21 gl_buffer.Create();
22 glBindBuffer(gl_target, gl_buffer.handle);
23
24 GLsizeiptr allocate_size = size;
25 if (array_buffer_for_amd) {
26 // On AMD GPU there is a strange crash in indexed drawing. The crash happens when the buffer
27 // read position is near the end and is an out-of-bound access to the vertex buffer. This is
28 // probably a bug in the driver and is related to the usage of vec3<byte> attributes in the
29 // vertex array. Doubling the allocation size for the vertex buffer seems to avoid the
30 // crash.
31 allocate_size *= 2;
32 }
33
34 if (GLAD_GL_ARB_buffer_storage) {
35 persistent = true;
36 coherent = prefer_coherent;
37 GLbitfield flags =
38 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | (coherent ? GL_MAP_COHERENT_BIT : 0);
39 glBufferStorage(gl_target, allocate_size, nullptr, flags);
40 mapped_ptr = static_cast<u8*>(glMapBufferRange(
41 gl_target, 0, buffer_size, flags | (coherent ? 0 : GL_MAP_FLUSH_EXPLICIT_BIT)));
42 } else {
43 glBufferData(gl_target, allocate_size, nullptr, GL_STREAM_DRAW);
44 }
45 }
46
~OGLStreamBuffer()47 OGLStreamBuffer::~OGLStreamBuffer() {
48 if (persistent) {
49 glBindBuffer(gl_target, gl_buffer.handle);
50 glUnmapBuffer(gl_target);
51 }
52 gl_buffer.Release();
53 }
54
GetHandle() const55 GLuint OGLStreamBuffer::GetHandle() const {
56 return gl_buffer.handle;
57 }
58
GetSize() const59 GLsizeiptr OGLStreamBuffer::GetSize() const {
60 return buffer_size;
61 }
62
Map(GLsizeiptr size,GLintptr alignment)63 std::tuple<u8*, GLintptr, bool> OGLStreamBuffer::Map(GLsizeiptr size, GLintptr alignment) {
64 ASSERT(size <= buffer_size);
65 ASSERT(alignment <= buffer_size);
66 mapped_size = size;
67
68 if (alignment > 0) {
69 buffer_pos = Common::AlignUp<std::size_t>(buffer_pos, alignment);
70 }
71
72 bool invalidate = false;
73 if (buffer_pos + size > buffer_size) {
74 buffer_pos = 0;
75 invalidate = true;
76
77 if (persistent) {
78 glUnmapBuffer(gl_target);
79 }
80 }
81
82 if (invalidate || !persistent) {
83 MICROPROFILE_SCOPE(OpenGL_StreamBuffer);
84 GLbitfield flags = GL_MAP_WRITE_BIT | (persistent ? GL_MAP_PERSISTENT_BIT : 0) |
85 (coherent ? GL_MAP_COHERENT_BIT : GL_MAP_FLUSH_EXPLICIT_BIT) |
86 (invalidate ? GL_MAP_INVALIDATE_BUFFER_BIT : GL_MAP_UNSYNCHRONIZED_BIT);
87 mapped_ptr = static_cast<u8*>(
88 glMapBufferRange(gl_target, buffer_pos, buffer_size - buffer_pos, flags));
89 mapped_offset = buffer_pos;
90 }
91
92 return std::make_tuple(mapped_ptr + buffer_pos - mapped_offset, buffer_pos, invalidate);
93 }
94
Unmap(GLsizeiptr size)95 void OGLStreamBuffer::Unmap(GLsizeiptr size) {
96 ASSERT(size <= mapped_size);
97
98 if (!coherent && size > 0) {
99 glFlushMappedBufferRange(gl_target, buffer_pos - mapped_offset, size);
100 }
101
102 if (!persistent) {
103 glUnmapBuffer(gl_target);
104 }
105
106 buffer_pos += size;
107 }
108
109 } // namespace OpenGL
110