|
|
@ -2,192 +2,57 @@
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/alignment.h"
|
|
|
|
#include <glad/glad.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "core/core.h"
|
|
|
|
|
|
|
|
#include "video_core/memory_manager.h"
|
|
|
|
|
|
|
|
#include "video_core/renderer_opengl/gl_buffer_cache.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_buffer_cache.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_rasterizer.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_rasterizer.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_resource_manager.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_resource_manager.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace OpenGL {
|
|
|
|
namespace OpenGL {
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
OGLBufferCache::OGLBufferCache(RasterizerOpenGL& rasterizer, Core::System& system,
|
|
|
|
|
|
|
|
std::size_t stream_size)
|
|
|
|
constexpr GLuint EmptyBuffer = 0;
|
|
|
|
: VideoCommon::BufferCache<OGLBuffer, GLuint, OGLStreamBuffer>{
|
|
|
|
constexpr GLintptr CachedBufferOffset = 0;
|
|
|
|
rasterizer, system, std::make_unique<OGLStreamBuffer>(stream_size, true)} {}
|
|
|
|
|
|
|
|
|
|
|
|
OGLBuffer CreateBuffer(std::size_t size, GLenum usage) {
|
|
|
|
|
|
|
|
OGLBuffer buffer;
|
|
|
|
|
|
|
|
buffer.Create();
|
|
|
|
|
|
|
|
glNamedBufferData(buffer.handle, size, nullptr, usage);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CachedBufferEntry::CachedBufferEntry(VAddr cpu_addr, u8* host_ptr)
|
|
|
|
|
|
|
|
: RasterizerCacheObject{host_ptr}, host_ptr{host_ptr}, cpu_addr{cpu_addr} {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OGLBufferCache::OGLBufferCache(RasterizerOpenGL& rasterizer, Core::System& system, std::size_t size)
|
|
|
|
|
|
|
|
: RasterizerCache{rasterizer}, system{system}, stream_buffer(size, true) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OGLBufferCache::~OGLBufferCache() = default;
|
|
|
|
OGLBufferCache::~OGLBufferCache() = default;
|
|
|
|
|
|
|
|
|
|
|
|
void OGLBufferCache::Unregister(const std::shared_ptr<CachedBufferEntry>& entry) {
|
|
|
|
OGLBuffer OGLBufferCache::CreateBuffer(std::size_t size) {
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
OGLBuffer buffer;
|
|
|
|
|
|
|
|
buffer.Create();
|
|
|
|
if (entry->IsInternalized()) {
|
|
|
|
glNamedBufferData(buffer.handle, static_cast<GLsizeiptr>(size), nullptr, GL_DYNAMIC_DRAW);
|
|
|
|
internalized_entries.erase(entry->GetCacheAddr());
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReserveBuffer(entry);
|
|
|
|
|
|
|
|
RasterizerCache<std::shared_ptr<CachedBufferEntry>>::Unregister(entry);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OGLBufferCache::BufferInfo OGLBufferCache::UploadMemory(GPUVAddr gpu_addr, std::size_t size,
|
|
|
|
const GLuint* OGLBufferCache::ToHandle(const OGLBuffer& buffer) {
|
|
|
|
std::size_t alignment, bool internalize,
|
|
|
|
return &buffer.handle;
|
|
|
|
bool is_written) {
|
|
|
|
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto& memory_manager = system.GPU().MemoryManager();
|
|
|
|
|
|
|
|
const auto host_ptr{memory_manager.GetPointer(gpu_addr)};
|
|
|
|
|
|
|
|
const auto cache_addr{ToCacheAddr(host_ptr)};
|
|
|
|
|
|
|
|
if (!host_ptr) {
|
|
|
|
|
|
|
|
return {EmptyBuffer, 0};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Cache management is a big overhead, so only cache entries with a given size.
|
|
|
|
const GLuint* OGLBufferCache::GetEmptyBuffer(std::size_t) {
|
|
|
|
// TODO: Figure out which size is the best for given games.
|
|
|
|
static const GLuint null_buffer = 0;
|
|
|
|
if (!internalize && size < 0x800 &&
|
|
|
|
return &null_buffer;
|
|
|
|
internalized_entries.find(cache_addr) == internalized_entries.end()) {
|
|
|
|
|
|
|
|
return StreamBufferUpload(host_ptr, size, alignment);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto entry = TryGet(host_ptr);
|
|
|
|
void OGLBufferCache::UploadBufferData(const OGLBuffer& buffer, std::size_t offset, std::size_t size,
|
|
|
|
if (!entry) {
|
|
|
|
const u8* data) {
|
|
|
|
return FixedBufferUpload(gpu_addr, host_ptr, size, internalize, is_written);
|
|
|
|
glNamedBufferSubData(buffer.handle, static_cast<GLintptr>(offset),
|
|
|
|
|
|
|
|
static_cast<GLsizeiptr>(size), data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (entry->GetSize() < size) {
|
|
|
|
void OGLBufferCache::DownloadBufferData(const OGLBuffer& buffer, std::size_t offset,
|
|
|
|
GrowBuffer(entry, size);
|
|
|
|
std::size_t size, u8* data) {
|
|
|
|
}
|
|
|
|
glGetNamedBufferSubData(buffer.handle, static_cast<GLintptr>(offset),
|
|
|
|
if (is_written) {
|
|
|
|
static_cast<GLsizeiptr>(size), data);
|
|
|
|
entry->MarkAsModified(true, *this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return {entry->GetBuffer(), CachedBufferOffset};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OGLBufferCache::BufferInfo OGLBufferCache::UploadHostMemory(const void* raw_pointer,
|
|
|
|
void OGLBufferCache::CopyBufferData(const OGLBuffer& src, const OGLBuffer& dst,
|
|
|
|
std::size_t size,
|
|
|
|
std::size_t src_offset, std::size_t dst_offset,
|
|
|
|
std::size_t alignment) {
|
|
|
|
std::size_t size) {
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
glCopyNamedBufferSubData(src.handle, dst.handle, static_cast<GLintptr>(src_offset),
|
|
|
|
return StreamBufferUpload(raw_pointer, size, alignment);
|
|
|
|
static_cast<GLintptr>(dst_offset), static_cast<GLsizeiptr>(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool OGLBufferCache::Map(std::size_t max_size) {
|
|
|
|
|
|
|
|
const auto max_size_ = static_cast<GLsizeiptr>(max_size);
|
|
|
|
|
|
|
|
bool invalidate;
|
|
|
|
|
|
|
|
std::tie(buffer_ptr, buffer_offset_base, invalidate) = stream_buffer.Map(max_size_, 4);
|
|
|
|
|
|
|
|
buffer_offset = buffer_offset_base;
|
|
|
|
|
|
|
|
return invalidate;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OGLBufferCache::Unmap() {
|
|
|
|
|
|
|
|
stream_buffer.Unmap(buffer_offset - buffer_offset_base);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OGLBufferCache::FlushObjectInner(const std::shared_ptr<CachedBufferEntry>& entry) {
|
|
|
|
|
|
|
|
glGetNamedBufferSubData(entry->GetBuffer(), 0, entry->GetSize(), entry->GetWritableHostPtr());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OGLBufferCache::BufferInfo OGLBufferCache::StreamBufferUpload(const void* raw_pointer,
|
|
|
|
|
|
|
|
std::size_t size,
|
|
|
|
|
|
|
|
std::size_t alignment) {
|
|
|
|
|
|
|
|
AlignBuffer(alignment);
|
|
|
|
|
|
|
|
const GLintptr uploaded_offset = buffer_offset;
|
|
|
|
|
|
|
|
std::memcpy(buffer_ptr, raw_pointer, size);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
buffer_ptr += size;
|
|
|
|
|
|
|
|
buffer_offset += size;
|
|
|
|
|
|
|
|
return {stream_buffer.GetHandle(), uploaded_offset};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OGLBufferCache::BufferInfo OGLBufferCache::FixedBufferUpload(GPUVAddr gpu_addr, u8* host_ptr,
|
|
|
|
|
|
|
|
std::size_t size, bool internalize,
|
|
|
|
|
|
|
|
bool is_written) {
|
|
|
|
|
|
|
|
auto& memory_manager = system.GPU().MemoryManager();
|
|
|
|
|
|
|
|
const auto cpu_addr = *memory_manager.GpuToCpuAddress(gpu_addr);
|
|
|
|
|
|
|
|
auto entry = GetUncachedBuffer(cpu_addr, host_ptr);
|
|
|
|
|
|
|
|
entry->SetSize(size);
|
|
|
|
|
|
|
|
entry->SetInternalState(internalize);
|
|
|
|
|
|
|
|
Register(entry);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (internalize) {
|
|
|
|
|
|
|
|
internalized_entries.emplace(ToCacheAddr(host_ptr));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_written) {
|
|
|
|
|
|
|
|
entry->MarkAsModified(true, *this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (entry->GetCapacity() < size) {
|
|
|
|
|
|
|
|
entry->SetCapacity(CreateBuffer(size, GL_STATIC_DRAW), size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
glNamedBufferSubData(entry->GetBuffer(), 0, static_cast<GLintptr>(size), host_ptr);
|
|
|
|
|
|
|
|
return {entry->GetBuffer(), CachedBufferOffset};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OGLBufferCache::GrowBuffer(std::shared_ptr<CachedBufferEntry>& entry, std::size_t new_size) {
|
|
|
|
|
|
|
|
const auto old_size = static_cast<GLintptr>(entry->GetSize());
|
|
|
|
|
|
|
|
if (entry->GetCapacity() < new_size) {
|
|
|
|
|
|
|
|
const auto old_buffer = entry->GetBuffer();
|
|
|
|
|
|
|
|
OGLBuffer new_buffer = CreateBuffer(new_size, GL_STATIC_COPY);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Copy bits from the old buffer to the new buffer.
|
|
|
|
|
|
|
|
glCopyNamedBufferSubData(old_buffer, new_buffer.handle, 0, 0, old_size);
|
|
|
|
|
|
|
|
entry->SetCapacity(std::move(new_buffer), new_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload the new bits.
|
|
|
|
|
|
|
|
const auto size_diff = static_cast<GLintptr>(new_size - old_size);
|
|
|
|
|
|
|
|
glNamedBufferSubData(entry->GetBuffer(), old_size, size_diff, entry->GetHostPtr() + old_size);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update entry's size in the object and in the cache.
|
|
|
|
|
|
|
|
entry->SetSize(new_size);
|
|
|
|
|
|
|
|
Unregister(entry);
|
|
|
|
|
|
|
|
Register(entry);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<CachedBufferEntry> OGLBufferCache::GetUncachedBuffer(VAddr cpu_addr, u8* host_ptr) {
|
|
|
|
|
|
|
|
if (auto entry = TryGetReservedBuffer(host_ptr)) {
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<CachedBufferEntry>(cpu_addr, host_ptr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<CachedBufferEntry> OGLBufferCache::TryGetReservedBuffer(u8* host_ptr) {
|
|
|
|
|
|
|
|
const auto it = buffer_reserve.find(ToCacheAddr(host_ptr));
|
|
|
|
|
|
|
|
if (it == buffer_reserve.end()) {
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& reserve = it->second;
|
|
|
|
|
|
|
|
auto entry = reserve.back();
|
|
|
|
|
|
|
|
reserve.pop_back();
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OGLBufferCache::ReserveBuffer(std::shared_ptr<CachedBufferEntry> entry) {
|
|
|
|
|
|
|
|
buffer_reserve[entry->GetCacheAddr()].push_back(std::move(entry));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OGLBufferCache::AlignBuffer(std::size_t alignment) {
|
|
|
|
|
|
|
|
// Align the offset, not the mapped pointer
|
|
|
|
|
|
|
|
const GLintptr offset_aligned =
|
|
|
|
|
|
|
|
static_cast<GLintptr>(Common::AlignUp(static_cast<std::size_t>(buffer_offset), alignment));
|
|
|
|
|
|
|
|
buffer_ptr += offset_aligned - buffer_offset;
|
|
|
|
|
|
|
|
buffer_offset = offset_aligned;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace OpenGL
|
|
|
|
} // namespace OpenGL
|
|
|
|