summaryrefslogtreecommitdiffstats
path: root/src/video_core/renderer_opengl/gl_buffer_cache.h
blob: 4267a5067b8049186160fffb65835a16aad11f7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2018 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <cstddef>
#include <map>
#include <memory>
#include <tuple>
#include <unordered_set>
#include <utility>
#include <vector>

#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 {

class RasterizerOpenGL;

class CachedBufferEntry final : public RasterizerCacheObject {
public:
    explicit CachedBufferEntry(VAddr cpu_addr, u8* host_ptr);

    VAddr GetCpuAddr() const override {
        return cpu_addr;
    }

    std::size_t GetSizeInBytes() const override {
        return size;
    }

    u8* GetWritableHostPtr() const {
        return host_ptr;
    }

    std::size_t GetSize() const {
        return size;
    }

    std::size_t GetCapacity() const {
        return capacity;
    }

    bool IsInternalized() const {
        return is_internal;
    }

    GLuint GetBuffer() const {
        return buffer.handle;
    }

    void SetSize(std::size_t new_size) {
        size = new_size;
    }

    void SetInternalState(bool is_internal_) {
        is_internal = is_internal_;
    }

    void SetCapacity(OGLBuffer&& new_buffer, std::size_t new_capacity) {
        capacity = new_capacity;
        buffer = std::move(new_buffer);
    }

private:
    u8* host_ptr{};
    VAddr cpu_addr{};
    std::size_t size{};
    std::size_t capacity{};
    bool is_internal{};
    OGLBuffer buffer;
};

class OGLBufferCache final : public RasterizerCache<std::shared_ptr<CachedBufferEntry>> {
    using BufferInfo = std::pair<GLuint, GLintptr>;

public:
    explicit OGLBufferCache(RasterizerOpenGL& rasterizer, std::size_t size);
    ~OGLBufferCache();

    void Unregister(const std::shared_ptr<CachedBufferEntry>& entry) override;

    /// Uploads data from a guest GPU address. Returns the OpenGL buffer where it's located and its
    /// offset.
    BufferInfo UploadMemory(GPUVAddr gpu_addr, std::size_t size, std::size_t alignment = 4,
                            bool internalize = false, bool is_written = false);

    /// Uploads from a host memory. Returns the OpenGL buffer where it's located and its offset.
    BufferInfo UploadHostMemory(const void* raw_pointer, std::size_t size,
                                std::size_t alignment = 4);

    bool Map(std::size_t max_size);
    void Unmap();

protected:
    // We do not have to flush this cache as things in it are never modified by us.
    void FlushObjectInner(const std::shared_ptr<CachedBufferEntry>& entry) override;

private:
    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,
                                 bool internalize, bool is_written);

    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);

    u8* buffer_ptr = nullptr;
    GLintptr buffer_offset = 0;
    GLintptr buffer_offset_base = 0;

    OGLStreamBuffer stream_buffer;
    std::unordered_set<CacheAddr> internalized_entries;
    std::unordered_map<CacheAddr, std::vector<std::shared_ptr<CachedBufferEntry>>> buffer_reserve;
};

} // namespace OpenGL