summaryrefslogtreecommitdiffstats
path: root/src/video_core/renderer_opengl/gl_global_cache.cpp
blob: c8dbcacbd64158f5289c093b0df75a739e579b73 (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
// Copyright 2018 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <glad/glad.h>

#include "common/assert.h"
#include "common/logging/log.h"
#include "core/core.h"
#include "video_core/renderer_opengl/gl_global_cache.h"
#include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/gl_shader_decompiler.h"
#include "video_core/renderer_opengl/utils.h"

namespace OpenGL {

CachedGlobalRegion::CachedGlobalRegion(VAddr cpu_addr, u32 size, u8* host_ptr)
    : cpu_addr{cpu_addr}, size{size}, RasterizerCacheObject{host_ptr} {
    buffer.Create();
    // Bind and unbind the buffer so it gets allocated by the driver
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer.handle);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
    LabelGLObject(GL_BUFFER, buffer.handle, cpu_addr, "GlobalMemory");
}

void CachedGlobalRegion::Reload(u32 size_) {
    constexpr auto max_size = static_cast<u32>(RasterizerOpenGL::MaxGlobalMemorySize);

    size = size_;
    if (size > max_size) {
        size = max_size;
        LOG_CRITICAL(HW_GPU, "Global region size {} exceeded the expected size {}!", size_,
                     max_size);
    }

    // TODO(Rodrigo): Get rid of Memory::GetPointer with a staging buffer
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer.handle);
    glBufferData(GL_SHADER_STORAGE_BUFFER, size, GetHostPtr(), GL_DYNAMIC_DRAW);
}

GlobalRegion GlobalRegionCacheOpenGL::TryGetReservedGlobalRegion(CacheAddr addr, u32 size) const {
    const auto search{reserve.find(addr)};
    if (search == reserve.end()) {
        return {};
    }
    return search->second;
}

GlobalRegion GlobalRegionCacheOpenGL::GetUncachedGlobalRegion(Tegra::GPUVAddr addr, u32 size,
                                                              u8* host_ptr) {
    GlobalRegion region{TryGetReservedGlobalRegion(ToCacheAddr(host_ptr), size)};
    if (!region) {
        // No reserved surface available, create a new one and reserve it
        auto& memory_manager{Core::System::GetInstance().GPU().MemoryManager()};
        const auto cpu_addr = *memory_manager.GpuToCpuAddress(addr);
        region = std::make_shared<CachedGlobalRegion>(cpu_addr, size, host_ptr);
        ReserveGlobalRegion(region);
    }
    region->Reload(size);
    return region;
}

void GlobalRegionCacheOpenGL::ReserveGlobalRegion(GlobalRegion region) {
    reserve.insert_or_assign(region->GetCacheAddr(), std::move(region));
}

GlobalRegionCacheOpenGL::GlobalRegionCacheOpenGL(RasterizerOpenGL& rasterizer)
    : RasterizerCache{rasterizer} {}

GlobalRegion GlobalRegionCacheOpenGL::GetGlobalRegion(
    const GLShader::GlobalMemoryEntry& global_region,
    Tegra::Engines::Maxwell3D::Regs::ShaderStage stage) {

    auto& gpu{Core::System::GetInstance().GPU()};
    auto& memory_manager{gpu.MemoryManager()};
    const auto cbufs{gpu.Maxwell3D().state.shader_stages[static_cast<u64>(stage)]};
    const auto addr{cbufs.const_buffers[global_region.GetCbufIndex()].address +
                    global_region.GetCbufOffset()};
    const auto actual_addr{memory_manager.Read64(addr)};
    const auto size{memory_manager.Read32(addr + 8)};

    // Look up global region in the cache based on address
    const auto& host_ptr{memory_manager.GetPointer(actual_addr)};
    GlobalRegion region{TryGet(host_ptr)};

    if (!region) {
        // No global region found - create a new one
        region = GetUncachedGlobalRegion(actual_addr, size, host_ptr);
        Register(region);
    }

    return region;
}

} // namespace OpenGL