2018-08-29 00:27:03 +02:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-09-06 20:57:40 +02:00
|
|
|
#include <cstddef>
|
2019-05-28 01:50:11 +02:00
|
|
|
#include <map>
|
2018-08-29 00:27:03 +02:00
|
|
|
#include <memory>
|
2018-10-02 19:47:26 +02:00
|
|
|
#include <tuple>
|
2019-05-28 01:50:11 +02:00
|
|
|
#include <unordered_set>
|
2019-05-28 00:55:44 +02:00
|
|
|
#include <utility>
|
2019-05-28 01:50:11 +02:00
|
|
|
#include <vector>
|
2018-08-29 00:27:03 +02:00
|
|
|
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "video_core/rasterizer_cache.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_resource_manager.h"
|
|
|
|
#include "video_core/renderer_opengl/gl_stream_buffer.h"
|
|
|
|
|
|
|
|
namespace OpenGL {
|
|
|
|
|
2018-11-08 12:08:00 +01:00
|
|
|
class RasterizerOpenGL;
|
|
|
|
|
2019-02-19 02:58:32 +01:00
|
|
|
class CachedBufferEntry final : public RasterizerCacheObject {
|
|
|
|
public:
|
2019-05-28 01:50:11 +02:00
|
|
|
explicit CachedBufferEntry(VAddr cpu_addr, u8* host_ptr);
|
2019-02-19 02:58:32 +01:00
|
|
|
|
|
|
|
VAddr GetCpuAddr() const override {
|
|
|
|
return cpu_addr;
|
2018-08-29 00:27:03 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:51:53 +02:00
|
|
|
std::size_t GetSizeInBytes() const override {
|
2018-08-29 00:27:03 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-05-30 02:08:33 +02:00
|
|
|
u8* GetWritableHostPtr() const {
|
|
|
|
return host_ptr;
|
|
|
|
}
|
|
|
|
|
2019-02-19 02:58:32 +01:00
|
|
|
std::size_t GetSize() const {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:50:11 +02:00
|
|
|
std::size_t GetCapacity() const {
|
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsInternalized() const {
|
|
|
|
return is_internal;
|
2019-02-19 02:58:32 +01:00
|
|
|
}
|
|
|
|
|
2019-05-28 01:11:46 +02:00
|
|
|
GLuint GetBuffer() const {
|
2019-05-28 01:50:11 +02:00
|
|
|
return buffer.handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSize(std::size_t new_size) {
|
|
|
|
size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetInternalState(bool is_internal_) {
|
|
|
|
is_internal = is_internal_;
|
2019-05-28 01:11:46 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 01:50:11 +02:00
|
|
|
void SetCapacity(OGLBuffer&& new_buffer, std::size_t new_capacity) {
|
|
|
|
capacity = new_capacity;
|
|
|
|
buffer = std::move(new_buffer);
|
2019-05-28 01:11:46 +02:00
|
|
|
}
|
|
|
|
|
2019-02-19 02:58:32 +01:00
|
|
|
private:
|
2019-05-30 02:08:33 +02:00
|
|
|
u8* host_ptr{};
|
2019-02-19 02:58:32 +01:00
|
|
|
VAddr cpu_addr{};
|
|
|
|
std::size_t size{};
|
2019-05-28 01:50:11 +02:00
|
|
|
std::size_t capacity{};
|
|
|
|
bool is_internal{};
|
|
|
|
OGLBuffer buffer;
|
2018-08-29 00:27:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class OGLBufferCache final : public RasterizerCache<std::shared_ptr<CachedBufferEntry>> {
|
2019-05-28 01:50:11 +02:00
|
|
|
using BufferInfo = std::pair<GLuint, GLintptr>;
|
|
|
|
|
2018-08-29 00:27:03 +02:00
|
|
|
public:
|
2018-11-08 12:08:00 +01:00
|
|
|
explicit OGLBufferCache(RasterizerOpenGL& rasterizer, std::size_t size);
|
2019-05-28 01:50:11 +02:00
|
|
|
~OGLBufferCache();
|
|
|
|
|
|
|
|
void Unregister(const std::shared_ptr<CachedBufferEntry>& entry) override;
|
2018-08-29 00:27:03 +02:00
|
|
|
|
2019-05-28 00:55:44 +02:00
|
|
|
/// Uploads data from a guest GPU address. Returns the OpenGL buffer where it's located and its
|
|
|
|
/// offset.
|
2019-05-28 01:50:11 +02:00
|
|
|
BufferInfo UploadMemory(GPUVAddr gpu_addr, std::size_t size, std::size_t alignment = 4,
|
2019-05-29 23:15:28 +02:00
|
|
|
bool internalize = false, bool is_written = false);
|
2018-08-29 00:27:03 +02:00
|
|
|
|
2019-05-28 00:55:44 +02:00
|
|
|
/// Uploads from a host memory. Returns the OpenGL buffer where it's located and its offset.
|
2019-05-28 01:50:11 +02:00
|
|
|
BufferInfo UploadHostMemory(const void* raw_pointer, std::size_t size,
|
|
|
|
std::size_t alignment = 4);
|
2018-08-29 00:27:03 +02:00
|
|
|
|
2018-11-06 21:26:27 +01:00
|
|
|
bool Map(std::size_t max_size);
|
2018-08-29 00:27:03 +02:00
|
|
|
void Unmap();
|
|
|
|
|
|
|
|
protected:
|
2019-04-16 22:39:27 +02:00
|
|
|
// We do not have to flush this cache as things in it are never modified by us.
|
2019-05-30 02:08:33 +02:00
|
|
|
void FlushObjectInner(const std::shared_ptr<CachedBufferEntry>& entry) override;
|
2019-04-16 22:39:27 +02:00
|
|
|
|
2018-08-29 00:27:03 +02:00
|
|
|
private:
|
2019-05-28 01:50:11 +02:00
|
|
|
BufferInfo StreamBufferUpload(const void* raw_pointer, std::size_t size, std::size_t alignment);
|
|
|
|
|
|
|
|
BufferInfo FixedBufferUpload(GPUVAddr gpu_addr, u8* host_ptr, std::size_t size,
|
2019-05-29 23:15:28 +02:00
|
|
|
bool internalize, bool is_written);
|
2019-05-28 01:50:11 +02:00
|
|
|
|
|
|
|
void GrowBuffer(std::shared_ptr<CachedBufferEntry>& entry, std::size_t new_size);
|
|
|
|
|
|
|
|
std::shared_ptr<CachedBufferEntry> GetUncachedBuffer(VAddr cpu_addr, u8* host_ptr);
|
|
|
|
|
|
|
|
std::shared_ptr<CachedBufferEntry> TryGetReservedBuffer(u8* host_ptr);
|
|
|
|
|
|
|
|
void ReserveBuffer(std::shared_ptr<CachedBufferEntry> entry);
|
|
|
|
|
|
|
|
void AlignBuffer(std::size_t alignment);
|
2018-08-29 00:27:03 +02:00
|
|
|
|
2018-09-06 21:03:01 +02:00
|
|
|
u8* buffer_ptr = nullptr;
|
|
|
|
GLintptr buffer_offset = 0;
|
|
|
|
GLintptr buffer_offset_base = 0;
|
2019-05-28 01:50:11 +02:00
|
|
|
|
|
|
|
OGLStreamBuffer stream_buffer;
|
|
|
|
std::unordered_set<CacheAddr> internalized_entries;
|
|
|
|
std::unordered_map<CacheAddr, std::vector<std::shared_ptr<CachedBufferEntry>>> buffer_reserve;
|
2018-08-29 00:27:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OpenGL
|