summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-14 17:38:49 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-14 17:45:12 +0100
commit10a0022f35e6bfccbbbf6fbd1538b9164cd569ff (patch)
tree1a0ee542bb2d18f9074f1e0f0c9a213dae540bd5
parent0c7cf38b33534db1ea8da855f1ca02b3ca5ccea5 (diff)
allow configuring msaa samples at runtime
-rw-r--r--editor/app.cpp33
-rw-r--r--floormat/settings.hpp13
-rw-r--r--main/draw.cpp61
3 files changed, 70 insertions, 37 deletions
diff --git a/editor/app.cpp b/editor/app.cpp
index 986f6102..b031d6f3 100644
--- a/editor/app.cpp
+++ b/editor/app.cpp
@@ -52,16 +52,36 @@ static bool parse_bool(StringView name, const Corrade::Utility::Arguments& args,
return def;
}
+static int atoi_(const char* str)
+{
+ bool negative = false;
+
+ switch (*str)
+ {
+ case '+': ++str; break;
+ case '-': ++str; negative = true; break;
+ }
+
+ int result = 0;
+ for (; *str >= '0' && *str <= '9'; ++str)
+ {
+ int digit = *str - '0';
+ result *= 10;
+ result -= digit; // calculate in negatives to support INT_MIN, LONG_MIN,..
+ }
+
+ return negative ? result : -result;
+}
+
int app::run_from_argv(const int argc, const char* const* const argv)
{
fm_settings opts;
Corrade::Utility::Arguments args{};
args.addOption("vsync", "1")
.addOption("gpu-debug", "1")
- .addOption("msaa", "1")
+ .addOption("msaa", "")
.parse(argc, argv);
opts.vsync = parse_bool("vsync", args, opts.vsync);
- opts.msaa = parse_bool("msaa", args, opts.msaa);
if (auto str = args.value<StringView>("gpu-debug"); str == "no-error" || str == "none")
opts.gpu_debug = fm_gpu_debug::no_error;
else if (str == "robust" || str == "full")
@@ -71,12 +91,21 @@ int app::run_from_argv(const int argc, const char* const* const argv)
? fm_gpu_debug::on
: fm_gpu_debug::off;
+ if (auto str = args.value<StringView>("msaa"); !str.isEmpty())
+ {
+ if (int n = atoi_(str.data()); (unsigned)n > 32 || (n & (n - 1)) != 0)
+ fm_warn("invalid '--msaa' argument '%s': must be a power of two between 0 and 128", str.data());
+ else
+ opts.msaa_samples = (std::uint8_t)n;
+ }
+
int ret;
Pointer<floormat_main> ptr;
{
app application{std::move(opts)};
ret = application.exec();
ptr = std::move(application.M);
+ (void)ptr;
}
loader_::destroy();
return ret;
diff --git a/floormat/settings.hpp b/floormat/settings.hpp
index 39b5b137..01ded6c4 100644
--- a/floormat/settings.hpp
+++ b/floormat/settings.hpp
@@ -20,16 +20,15 @@ struct fm_settings
Magnum::Math::Vector2<int> resolution{1024, 768};
Corrade::Containers::String title{"Test"};
Corrade::Containers::String disabled_extensions; // TODO
- std::uint8_t msaa_samples = 16;
bool vsync = true;
fm_gpu_debug gpu_debug = fm_gpu_debug::on;
fm_log_level log_level = fm_log_level::normal;
- std::uint8_t resizable : 1 = true,
- fullscreen : 1 = false,
- fullscreen_desktop : 1 = false,
- borderless : 1 = false,
- maximized : 1 = false,
- msaa : 1 = false;
+ unsigned resizable : 1 = true,
+ fullscreen : 1 = false,
+ fullscreen_desktop : 1 = false,
+ borderless : 1 = false,
+ maximized : 1 = false;
+ std::uint8_t msaa_samples = 16;
};
} // namespace floormat
diff --git a/main/draw.cpp b/main/draw.cpp
index 45113958..98f5da85 100644
--- a/main/draw.cpp
+++ b/main/draw.cpp
@@ -7,8 +7,6 @@
#include <algorithm>
#include <thread>
-//#define FM_SKIP_MSAA
-
namespace floormat {
void main_impl::recalc_viewport(Vector2i size) noexcept
@@ -25,16 +23,19 @@ void main_impl::recalc_viewport(Vector2i size) noexcept
else
_msaa_framebuffer = GL::Framebuffer{{{}, size}};
- // --- color ---
- _msaa_color = Magnum::GL::Renderbuffer{};
const int samples = std::min(_msaa_color.maxSamples(), (int)s.msaa_samples);
- _msaa_color.setStorageMultisample(samples, GL::RenderbufferFormat::RGBA8, size);
- _msaa_framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, _msaa_color);
-
- // --- depth ---
- _msaa_depth = Magnum::GL::Renderbuffer{};
- _msaa_depth.setStorageMultisample(samples, GL::RenderbufferFormat::DepthStencil, size);
- _msaa_framebuffer.attachRenderbuffer(GL::Framebuffer::BufferAttachment::DepthStencil, _msaa_depth);
+ if (samples != s.msaa_samples)
+ fm_warn_once("using only %d MSAA samples (instead of %hhu)", samples, s.msaa_samples);
+ if (s.msaa_samples > 0) {
+ // color
+ _msaa_color = Magnum::GL::Renderbuffer{};
+ _msaa_color.setStorageMultisample(samples, GL::RenderbufferFormat::RGBA8, size);
+ _msaa_framebuffer.attachRenderbuffer(GL::Framebuffer::ColorAttachment{0}, _msaa_color);
+ // depth
+ _msaa_depth = Magnum::GL::Renderbuffer{};
+ _msaa_depth.setStorageMultisample(samples, GL::RenderbufferFormat::DepthStencil, size);
+ _msaa_framebuffer.attachRenderbuffer(GL::Framebuffer::BufferAttachment::DepthStencil, _msaa_depth);
+ }
// -- state ---
using R = GL::Renderer;
@@ -98,11 +99,10 @@ void main_impl::draw_world() noexcept
GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
constexpr float clear_depth = 0;
-#ifdef FM_SKIP_MSAA
- GL::defaultFramebuffer.clearDepthStencil(clear_depth, 0);
-#else
- _msaa_framebuffer.clearDepthStencil(clear_depth, 0);
-#endif
+ if (s.msaa_samples == 0)
+ GL::defaultFramebuffer.clearDepthStencil(clear_depth, 0);
+ else
+ _msaa_framebuffer.clearDepthStencil(clear_depth, 0);
for (std::int16_t y = miny; y <= maxy; y++)
for (std::int16_t x = minx; x <= maxx; x++)
{
@@ -165,20 +165,25 @@ void main_impl::drawEvent()
{
const auto clear_color = 0x222222ff_rgbaf;
-#ifdef FM_SKIP_MSAA
- GL::defaultFramebuffer.clearColor(clear_color);
-#else
- _msaa_framebuffer.clearColor(0, clear_color);
- _msaa_framebuffer.bind();
-#endif
+
+ if (s.msaa_samples == 0)
+ GL::defaultFramebuffer.clearColor(clear_color);
+ else
+ {
+ _msaa_framebuffer.clearColor(0, clear_color);
+ _msaa_framebuffer.bind();
+ }
+
draw_world();
app.draw_msaa();
-#ifndef FM_SKIP_MSAA
- GL::defaultFramebuffer.bind();
- using Blit = GL::FramebufferBlit;
- constexpr auto blit_mask = Blit::Color /* | Blit::Depth | Blit::Stencil */;
- GL::Framebuffer::blit(_msaa_framebuffer, GL::defaultFramebuffer, {{}, windowSize()}, blit_mask);
-#endif
+
+ if (s.msaa_samples > 0)
+ {
+ GL::defaultFramebuffer.bind();
+ using Blit = GL::FramebufferBlit;
+ constexpr auto blit_mask = Blit::Color /* | Blit::Depth | Blit::Stencil */;
+ GL::Framebuffer::blit(_msaa_framebuffer, GL::defaultFramebuffer, {{}, windowSize()}, blit_mask);
+ }
}
app.draw();