| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/OGL/OGLVertexManager.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | |
| 8 | #include "Common/Align.h" |
| 9 | #include "Common/CommonTypes.h" |
| 10 | #include "Common/GL/GLExtensions/GLExtensions.h" |
| 11 | #include "Common/GL/GLUtil.h" |
| 12 | |
| 13 | #include "VideoBackends/OGL/OGLGfx.h" |
| 14 | #include "VideoBackends/OGL/OGLStreamBuffer.h" |
| 15 | #include "VideoBackends/OGL/ProgramShaderCache.h" |
| 16 | |
| 17 | #include "VideoCommon/IndexGenerator.h" |
| 18 | #include "VideoCommon/Statistics.h" |
| 19 | #include "VideoCommon/VertexLoaderManager.h" |
| 20 | #include "VideoCommon/VideoConfig.h" |
| 21 | |
| 22 | namespace OGL |
| 23 | { |
| 24 | static void CheckBufferBinding() |
| 25 | { |
| 26 | // The index buffer is part of the VAO state, therefore we need to bind it first. |
| 27 | if (!ProgramShaderCache::IsValidVertexFormatBound()) |
| 28 | { |
| 29 | ProgramShaderCache::BindVertexFormat( |
| 30 | static_cast<GLVertexFormat*>(VertexLoaderManager::GetCurrentVertexFormat())); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | VertexManager::VertexManager() = default; |
| 35 | |
| 36 | VertexManager::~VertexManager() |
| 37 | { |
| 38 | if (g_backend_info.bSupportsPaletteConversion) |
| 39 | { |
| 40 | glDeleteTextures(static_cast<GLsizei>(m_texel_buffer_views.size()), |
| 41 | m_texel_buffer_views.data()); |
| 42 | } |
| 43 | |
| 44 | // VAO must be found when destroying the index buffer. |
| 45 | CheckBufferBinding(); |
| 46 | m_texel_buffer.reset(); |
| 47 | m_index_buffer.reset(); |
| 48 | m_vertex_buffer.reset(); |
| 49 | } |
| 50 | |
| 51 | bool VertexManager::Initialize() |
| 52 | { |
| 53 | if (!VertexManagerBase::Initialize()) |
| 54 | return false; |
| 55 | |
| 56 | m_vertex_buffer = StreamBuffer::Create(GL_ARRAY_BUFFER, VERTEX_STREAM_BUFFER_SIZE); |
| 57 | m_index_buffer = StreamBuffer::Create(GL_ELEMENT_ARRAY_BUFFER, INDEX_STREAM_BUFFER_SIZE); |
| 58 | if (g_ActiveConfig.UseVSForLinePointExpand() || g_backend_info.bSupportsDynamicVertexLoader) |
| 59 | { |
| 60 | glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_vertex_buffer->GetGLBufferId()); |
| 61 | } |
| 62 | |
| 63 | if (g_backend_info.bSupportsPaletteConversion) |
| 64 | { |
| 65 | // The minimum MAX_TEXTURE_BUFFER_SIZE that the spec mandates is 65KB, we are asking for a 1MB |
| 66 | // buffer here. This buffer is also used as storage for undecoded textures when compute shader |
| 67 | // texture decoding is enabled, in which case the requested size is 32MB. |
| 68 | GLint max_buffer_size; |
| 69 | glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &max_buffer_size); |
| 70 | m_texel_buffer = StreamBuffer::Create( |
| 71 | GL_TEXTURE_BUFFER, std::min(max_buffer_size, static_cast<GLint>(TEXEL_STREAM_BUFFER_SIZE))); |
| 72 | |
| 73 | // Allocate texture views backed by buffer. |
| 74 | static constexpr std::array<std::pair<TexelBufferFormat, GLenum>, NUM_TEXEL_BUFFER_FORMATS> |
| 75 | format_mapping = {{ |
| 76 | {TEXEL_BUFFER_FORMAT_R8_UINT, GL_R8UI}, |
| 77 | {TEXEL_BUFFER_FORMAT_R16_UINT, GL_R16UI}, |
| 78 | {TEXEL_BUFFER_FORMAT_RGBA8_UINT, GL_RGBA8UI}, |
| 79 | {TEXEL_BUFFER_FORMAT_R32G32_UINT, GL_RG32UI}, |
| 80 | }}; |
| 81 | glGenTextures(static_cast<GLsizei>(m_texel_buffer_views.size()), m_texel_buffer_views.data()); |
| 82 | glActiveTexture(GL_MUTABLE_TEXTURE_INDEX); |
| 83 | for (const auto& it : format_mapping) |
| 84 | { |
| 85 | glBindTexture(GL_TEXTURE_BUFFER, m_texel_buffer_views[it.first]); |
| 86 | glTexBuffer(GL_TEXTURE_BUFFER, it.second, m_texel_buffer->GetGLBufferId()); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | void VertexManager::UploadUtilityUniforms(const void* uniforms, u32 uniforms_size) |
| 94 | { |
| 95 | InvalidateConstants(); |
| 96 | ProgramShaderCache::UploadConstants(uniforms, uniforms_size); |
| 97 | } |
| 98 | |
| 99 | bool VertexManager::UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format, |
| 100 | u32* out_offset) |
| 101 | { |
| 102 | if (data_size > m_texel_buffer->GetSize()) |
| 103 | return false; |
| 104 | |
| 105 | const u32 elem_size = GetTexelBufferElementSize(format); |
| 106 | const auto dst = m_texel_buffer->Map(data_size, elem_size); |
| 107 | std::memcpy(dst.first, data, data_size); |
| 108 | ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, data_size); |
| 109 | *out_offset = dst.second / elem_size; |
| 110 | m_texel_buffer->Unmap(data_size); |
| 111 | |
| 112 | // Bind the correct view to the texel buffer slot. |
| 113 | glActiveTexture(GL_TEXTURE0); |
| 114 | glBindTexture(GL_TEXTURE_BUFFER, m_texel_buffer_views[static_cast<u32>(format)]); |
| 115 | GetOGLGfx()->InvalidateTextureBinding(0); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | bool VertexManager::UploadTexelBuffer(const void* data, u32 data_size, TexelBufferFormat format, |
| 120 | u32* out_offset, const void* palette_data, u32 palette_size, |
| 121 | TexelBufferFormat palette_format, u32* out_palette_offset) |
| 122 | { |
| 123 | const u32 elem_size = GetTexelBufferElementSize(format); |
| 124 | const u32 palette_elem_size = GetTexelBufferElementSize(palette_format); |
| 125 | const u32 reserve_size = data_size + palette_size + palette_elem_size; |
| 126 | if (reserve_size > m_texel_buffer->GetSize()) |
| 127 | return false; |
| 128 | |
| 129 | const auto dst = m_texel_buffer->Map(reserve_size, elem_size); |
| 130 | const u32 palette_byte_offset = Common::AlignUp(data_size, palette_elem_size); |
| 131 | std::memcpy(dst.first, data, data_size); |
| 132 | std::memcpy(dst.first + palette_byte_offset, palette_data, palette_size); |
| 133 | ADDSTAT(g_stats.this_frame.bytes_uniform_streamed, palette_byte_offset + palette_size); |
| 134 | *out_offset = dst.second / elem_size; |
| 135 | *out_palette_offset = (dst.second + palette_byte_offset) / palette_elem_size; |
| 136 | m_texel_buffer->Unmap(palette_byte_offset + palette_size); |
| 137 | |
| 138 | glActiveTexture(GL_TEXTURE0); |
| 139 | glBindTexture(GL_TEXTURE_BUFFER, m_texel_buffer_views[static_cast<u32>(format)]); |
| 140 | GetOGLGfx()->InvalidateTextureBinding(0); |
| 141 | |
| 142 | glActiveTexture(GL_TEXTURE1); |
| 143 | glBindTexture(GL_TEXTURE_BUFFER, m_texel_buffer_views[static_cast<u32>(palette_format)]); |
| 144 | GetOGLGfx()->InvalidateTextureBinding(1); |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | GLuint VertexManager::GetVertexBufferHandle() const |
| 150 | { |
| 151 | return m_vertex_buffer->m_buffer; |
| 152 | } |
| 153 | |
| 154 | GLuint VertexManager::GetIndexBufferHandle() const |
| 155 | { |
| 156 | return m_index_buffer->m_buffer; |
| 157 | } |
| 158 | |
| 159 | void VertexManager::ResetBuffer(u32 vertex_stride) |
| 160 | { |
| 161 | CheckBufferBinding(); |
| 162 | |
| 163 | auto buffer = m_vertex_buffer->Map(MAXVBUFFERSIZE, vertex_stride); |
| 164 | m_cur_buffer_pointer = m_base_buffer_pointer = buffer.first; |
| 165 | m_end_buffer_pointer = buffer.first + MAXVBUFFERSIZE; |
| 166 | |
| 167 | buffer = m_index_buffer->Map(MAXIBUFFERSIZE * sizeof(u16)); |
| 168 | m_index_generator.Start(reinterpret_cast<u16*>(buffer.first)); |
| 169 | } |
| 170 | |
| 171 | void VertexManager::CommitBuffer(u32 num_vertices, u32 vertex_stride, u32 num_indices, |
| 172 | u32* out_base_vertex, u32* out_base_index) |
| 173 | { |
| 174 | u32 vertex_data_size = num_vertices * vertex_stride; |
| 175 | u32 index_data_size = num_indices * sizeof(u16); |
| 176 | |
| 177 | *out_base_vertex = vertex_stride > 0 ? (m_vertex_buffer->GetCurrentOffset() / vertex_stride) : 0; |
| 178 | *out_base_index = m_index_buffer->GetCurrentOffset() / sizeof(u16); |
| 179 | |
| 180 | CheckBufferBinding(); |
| 181 | m_vertex_buffer->Unmap(vertex_data_size); |
| 182 | m_index_buffer->Unmap(index_data_size); |
| 183 | |
| 184 | ADDSTAT(g_stats.this_frame.bytes_vertex_streamed, vertex_data_size); |
| 185 | ADDSTAT(g_stats.this_frame.bytes_index_streamed, index_data_size); |
| 186 | } |
| 187 | |
| 188 | void VertexManager::UploadUniforms() |
| 189 | { |
| 190 | ProgramShaderCache::UploadConstants(); |
| 191 | } |
| 192 | } // namespace OGL |