1 // Copyright 2019 yuzu Emulator Project
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4
5 #include <tuple>
6 #include <unordered_map>
7 #include <utility>
8
9 #include <glad/glad.h>
10
11 #include "common/common_types.h"
12 #include "video_core/engines/maxwell_3d.h"
13 #include "video_core/renderer_opengl/gl_framebuffer_cache.h"
14
15 namespace OpenGL {
16
17 using Maxwell = Tegra::Engines::Maxwell3D::Regs;
18 using VideoCore::Surface::SurfaceType;
19
20 FramebufferCacheOpenGL::FramebufferCacheOpenGL() = default;
21
22 FramebufferCacheOpenGL::~FramebufferCacheOpenGL() = default;
23
GetFramebuffer(const FramebufferCacheKey & key)24 GLuint FramebufferCacheOpenGL::GetFramebuffer(const FramebufferCacheKey& key) {
25 const auto [entry, is_cache_miss] = cache.try_emplace(key);
26 auto& framebuffer{entry->second};
27 if (is_cache_miss) {
28 framebuffer = CreateFramebuffer(key);
29 }
30 return framebuffer.handle;
31 }
32
CreateFramebuffer(const FramebufferCacheKey & key)33 OGLFramebuffer FramebufferCacheOpenGL::CreateFramebuffer(const FramebufferCacheKey& key) {
34 OGLFramebuffer framebuffer;
35 framebuffer.Create();
36
37 // TODO(Rodrigo): Use DSA here after Nvidia fixes their framebuffer DSA bugs.
38 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer.handle);
39
40 if (key.zeta) {
41 const bool stencil = key.zeta->GetSurfaceParams().type == SurfaceType::DepthStencil;
42 const GLenum attach_target = stencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT;
43 key.zeta->Attach(attach_target, GL_DRAW_FRAMEBUFFER);
44 }
45
46 std::size_t num_buffers = 0;
47 std::array<GLenum, Maxwell::NumRenderTargets> targets;
48
49 for (std::size_t index = 0; index < Maxwell::NumRenderTargets; ++index) {
50 if (!key.colors[index]) {
51 targets[index] = GL_NONE;
52 continue;
53 }
54 const GLenum attach_target = GL_COLOR_ATTACHMENT0 + static_cast<GLenum>(index);
55 key.colors[index]->Attach(attach_target, GL_DRAW_FRAMEBUFFER);
56
57 const u32 attachment = (key.color_attachments >> (BitsPerAttachment * index)) & 0b1111;
58 targets[index] = GL_COLOR_ATTACHMENT0 + attachment;
59 num_buffers = index + 1;
60 }
61
62 if (num_buffers > 0) {
63 glDrawBuffers(static_cast<GLsizei>(num_buffers), std::data(targets));
64 } else {
65 glDrawBuffer(GL_NONE);
66 }
67
68 return framebuffer;
69 }
70
Hash() const71 std::size_t FramebufferCacheKey::Hash() const noexcept {
72 std::size_t hash = std::hash<View>{}(zeta);
73 for (const auto& color : colors) {
74 hash ^= std::hash<View>{}(color);
75 }
76 hash ^= static_cast<std::size_t>(color_attachments) << 16;
77 return hash;
78 }
79
operator ==(const FramebufferCacheKey & rhs) const80 bool FramebufferCacheKey::operator==(const FramebufferCacheKey& rhs) const noexcept {
81 return std::tie(colors, zeta, color_attachments) ==
82 std::tie(rhs.colors, rhs.zeta, rhs.color_attachments);
83 }
84
85 } // namespace OpenGL
86