summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2019-11-28 22:59:09 -0300
committerGravatar ReinUsesLisp2019-11-28 23:02:20 -0300
commitfb6cf12a17daf1f452d9c542e22c9252bbfb5c76 (patch)
tree44b01462f402e7e8bf2c8a7e37edf1fb16ea1526 /src
parentgl_rasterizer: Re-enable framebuffer cache for clear buffers (diff)
downloadyuzu-fb6cf12a17daf1f452d9c542e22c9252bbfb5c76.tar.gz
yuzu-fb6cf12a17daf1f452d9c542e22c9252bbfb5c76.tar.xz
yuzu-fb6cf12a17daf1f452d9c542e22c9252bbfb5c76.zip
gl_framebuffer_cache: Optimize framebuffer key
Pack color attachment enumerations into a single u32. To determine the number of buffers, the highest color attachment with a shared pointer that doesn't point to null is used.
Diffstat (limited to 'src')
-rw-r--r--src/video_core/renderer_opengl/gl_framebuffer_cache.cpp57
-rw-r--r--src/video_core/renderer_opengl/gl_framebuffer_cache.h19
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp30
3 files changed, 60 insertions, 46 deletions
diff --git a/src/video_core/renderer_opengl/gl_framebuffer_cache.cpp b/src/video_core/renderer_opengl/gl_framebuffer_cache.cpp
index a5d69d78d..874ed3c6e 100644
--- a/src/video_core/renderer_opengl/gl_framebuffer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_framebuffer_cache.cpp
@@ -3,9 +3,12 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <tuple> 5#include <tuple>
6#include <unordered_map>
7#include <utility>
6 8
7#include "common/cityhash.h" 9#include <glad/glad.h>
8#include "common/scope_exit.h" 10
11#include "common/common_types.h"
9#include "video_core/engines/maxwell_3d.h" 12#include "video_core/engines/maxwell_3d.h"
10#include "video_core/renderer_opengl/gl_framebuffer_cache.h" 13#include "video_core/renderer_opengl/gl_framebuffer_cache.h"
11#include "video_core/renderer_opengl/gl_state.h" 14#include "video_core/renderer_opengl/gl_state.h"
@@ -13,6 +16,7 @@
13namespace OpenGL { 16namespace OpenGL {
14 17
15using Maxwell = Tegra::Engines::Maxwell3D::Regs; 18using Maxwell = Tegra::Engines::Maxwell3D::Regs;
19using VideoCore::Surface::SurfaceType;
16 20
17FramebufferCacheOpenGL::FramebufferCacheOpenGL() = default; 21FramebufferCacheOpenGL::FramebufferCacheOpenGL() = default;
18 22
@@ -35,36 +39,49 @@ OGLFramebuffer FramebufferCacheOpenGL::CreateFramebuffer(const FramebufferCacheK
35 local_state.draw.draw_framebuffer = framebuffer.handle; 39 local_state.draw.draw_framebuffer = framebuffer.handle;
36 local_state.ApplyFramebufferState(); 40 local_state.ApplyFramebufferState();
37 41
42 if (key.zeta) {
43 const bool stencil = key.zeta->GetSurfaceParams().type == SurfaceType::DepthStencil;
44 const GLenum attach_target = stencil ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT;
45 key.zeta->Attach(attach_target, GL_DRAW_FRAMEBUFFER);
46 }
47
48 std::size_t num_buffers = 0;
49 std::array<GLenum, Maxwell::NumRenderTargets> targets;
50
38 for (std::size_t index = 0; index < Maxwell::NumRenderTargets; ++index) { 51 for (std::size_t index = 0; index < Maxwell::NumRenderTargets; ++index) {
39 if (key.colors[index]) { 52 if (!key.colors[index]) {
40 key.colors[index]->Attach(GL_COLOR_ATTACHMENT0 + static_cast<GLenum>(index), 53 targets[index] = GL_NONE;
41 GL_DRAW_FRAMEBUFFER); 54 continue;
42 } 55 }
56 const GLenum attach_target = GL_COLOR_ATTACHMENT0 + static_cast<GLenum>(index);
57 key.colors[index]->Attach(attach_target, GL_DRAW_FRAMEBUFFER);
58
59 const u32 attachment = (key.color_attachments >> (BitsPerAttachment * index)) & 0b1111;
60 targets[index] = GL_COLOR_ATTACHMENT0 + attachment;
61 num_buffers = index + 1;
43 } 62 }
44 if (key.colors_count) { 63
45 glDrawBuffers(key.colors_count, key.color_attachments.data()); 64 if (num_buffers > 0) {
65 glDrawBuffers(static_cast<GLsizei>(num_buffers), std::data(targets));
46 } else { 66 } else {
47 glDrawBuffer(GL_NONE); 67 glDrawBuffer(GL_NONE);
48 } 68 }
49 69
50 if (key.zeta) {
51 key.zeta->Attach(key.stencil_enable ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT,
52 GL_DRAW_FRAMEBUFFER);
53 }
54
55 return framebuffer; 70 return framebuffer;
56} 71}
57 72
58std::size_t FramebufferCacheKey::Hash() const { 73std::size_t FramebufferCacheKey::Hash() const noexcept {
59 static_assert(sizeof(*this) % sizeof(u64) == 0, "Unaligned struct"); 74 std::size_t hash = std::hash<View>{}(zeta);
60 return static_cast<std::size_t>( 75 for (const auto& color : colors) {
61 Common::CityHash64(reinterpret_cast<const char*>(this), sizeof(*this))); 76 hash ^= std::hash<View>{}(color);
77 }
78 hash ^= static_cast<std::size_t>(color_attachments) << 16;
79 return hash;
62} 80}
63 81
64bool FramebufferCacheKey::operator==(const FramebufferCacheKey& rhs) const { 82bool FramebufferCacheKey::operator==(const FramebufferCacheKey& rhs) const noexcept {
65 return std::tie(stencil_enable, colors_count, color_attachments, colors, zeta) == 83 return std::tie(colors, zeta, color_attachments) ==
66 std::tie(rhs.stencil_enable, rhs.colors_count, rhs.color_attachments, rhs.colors, 84 std::tie(rhs.colors, rhs.zeta, rhs.color_attachments);
67 rhs.zeta);
68} 85}
69 86
70} // namespace OpenGL 87} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_framebuffer_cache.h b/src/video_core/renderer_opengl/gl_framebuffer_cache.h
index 3b5ff2149..02ec80ae9 100644
--- a/src/video_core/renderer_opengl/gl_framebuffer_cache.h
+++ b/src/video_core/renderer_opengl/gl_framebuffer_cache.h
@@ -18,21 +18,24 @@
18 18
19namespace OpenGL { 19namespace OpenGL {
20 20
21struct alignas(sizeof(u64)) FramebufferCacheKey { 21constexpr std::size_t BitsPerAttachment = 4;
22 bool stencil_enable;
23 u16 colors_count;
24 22
25 std::array<GLenum, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> color_attachments{}; 23struct FramebufferCacheKey {
26 std::array<View, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> colors;
27 View zeta; 24 View zeta;
25 std::array<View, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> colors;
26 u32 color_attachments = 0;
28 27
29 std::size_t Hash() const; 28 std::size_t Hash() const noexcept;
30 29
31 bool operator==(const FramebufferCacheKey& rhs) const; 30 bool operator==(const FramebufferCacheKey& rhs) const noexcept;
32 31
33 bool operator!=(const FramebufferCacheKey& rhs) const { 32 bool operator!=(const FramebufferCacheKey& rhs) const noexcept {
34 return !operator==(rhs); 33 return !operator==(rhs);
35 } 34 }
35
36 void SetAttachment(std::size_t index, u32 attachment) {
37 color_attachments |= attachment << (BitsPerAttachment * index);
38 }
36}; 39};
37 40
38} // namespace OpenGL 41} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index 18c122228..9eef7fcd2 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -372,33 +372,31 @@ void RasterizerOpenGL::ConfigureFramebuffers() {
372 UNIMPLEMENTED_IF(regs.rt_separate_frag_data == 0); 372 UNIMPLEMENTED_IF(regs.rt_separate_frag_data == 0);
373 373
374 // Bind the framebuffer surfaces 374 // Bind the framebuffer surfaces
375 FramebufferCacheKey fbkey; 375 FramebufferCacheKey key;
376 for (std::size_t index = 0; index < Maxwell::NumRenderTargets; ++index) { 376 const auto colors_count = static_cast<std::size_t>(regs.rt_control.count);
377 for (std::size_t index = 0; index < colors_count; ++index) {
377 View color_surface{texture_cache.GetColorBufferSurface(index, true)}; 378 View color_surface{texture_cache.GetColorBufferSurface(index, true)};
378 379 if (!color_surface) {
379 if (color_surface) { 380 continue;
380 // Assume that a surface will be written to if it is used as a framebuffer, even
381 // if the shader doesn't actually write to it.
382 texture_cache.MarkColorBufferInUse(index);
383 } 381 }
382 // Assume that a surface will be written to if it is used as a framebuffer, even
383 // if the shader doesn't actually write to it.
384 texture_cache.MarkColorBufferInUse(index);
384 385
385 fbkey.color_attachments[index] = GL_COLOR_ATTACHMENT0 + regs.rt_control.GetMap(index); 386 key.SetAttachment(index, regs.rt_control.GetMap(index));
386 fbkey.colors[index] = std::move(color_surface); 387 key.colors[index] = std::move(color_surface);
387 } 388 }
388 fbkey.colors_count = static_cast<u16>(regs.rt_control.count);
389 389
390 if (depth_surface) { 390 if (depth_surface) {
391 // Assume that a surface will be written to if it is used as a framebuffer, even if 391 // Assume that a surface will be written to if it is used as a framebuffer, even if
392 // the shader doesn't actually write to it. 392 // the shader doesn't actually write to it.
393 texture_cache.MarkDepthBufferInUse(); 393 texture_cache.MarkDepthBufferInUse();
394 394 key.zeta = std::move(depth_surface);
395 fbkey.stencil_enable = depth_surface->GetSurfaceParams().type == SurfaceType::DepthStencil;
396 fbkey.zeta = std::move(depth_surface);
397 } 395 }
398 396
399 texture_cache.GuardRenderTargets(false); 397 texture_cache.GuardRenderTargets(false);
400 398
401 state.draw.draw_framebuffer = framebuffer_cache.GetFramebuffer(fbkey); 399 state.draw.draw_framebuffer = framebuffer_cache.GetFramebuffer(key);
402 SyncViewport(state); 400 SyncViewport(state);
403} 401}
404 402
@@ -421,12 +419,8 @@ void RasterizerOpenGL::ConfigureClearFramebuffer(OpenGLState& current_state, boo
421 texture_cache.GuardRenderTargets(false); 419 texture_cache.GuardRenderTargets(false);
422 420
423 FramebufferCacheKey key; 421 FramebufferCacheKey key;
424 key.colors_count = color_surface ? 1 : 0;
425 key.colors[0] = color_surface; 422 key.colors[0] = color_surface;
426 key.color_attachments[0] = GL_COLOR_ATTACHMENT0;
427 key.zeta = depth_surface; 423 key.zeta = depth_surface;
428 key.stencil_enable = depth_surface && depth_surface->GetSurfaceParams().type ==
429 VideoCore::Surface::SurfaceType::DepthStencil;
430 424
431 current_state.draw.draw_framebuffer = framebuffer_cache.GetFramebuffer(key); 425 current_state.draw.draw_framebuffer = framebuffer_cache.GetFramebuffer(key);
432 current_state.ApplyFramebufferState(); 426 current_state.ApplyFramebufferState();