diff options
Diffstat (limited to 'shaders')
-rw-r--r-- | shaders/lightmap.cpp | 8 | ||||
-rw-r--r-- | shaders/lightmap.hpp | 6 | ||||
-rw-r--r-- | shaders/shader.cpp | 31 | ||||
-rw-r--r-- | shaders/shader.hpp | 25 |
4 files changed, 49 insertions, 21 deletions
diff --git a/shaders/lightmap.cpp b/shaders/lightmap.cpp index 3e3967ee..a1635fa1 100644 --- a/shaders/lightmap.cpp +++ b/shaders/lightmap.cpp @@ -170,7 +170,7 @@ std::array<Vector3, 4>& lightmap_shader::alloc_rect() } } -lightmap_shader::lightmap_shader() +lightmap_shader::lightmap_shader(texture_unit_cache& tuc) : tuc{tuc} { constexpr auto min_version = GL::Version::GL330; const auto version = GL::Context::current().version(); @@ -199,8 +199,8 @@ lightmap_shader::lightmap_shader() }}; light_mesh = make_light_mesh(GL::Buffer{blend_vertexes}, GL::Buffer{quad_indexes(0)}); - framebuffer.scratch.bind(TextureSampler); - setUniform(SamplerUniform, TextureSampler); + //framebuffer.scratch.bind(TextureSampler); + setUniform(SamplerUniform, -1); setUniform(LightColorUniform, Color3{1, 1, 1}); setUniform(SizeUniform, Vector2(1)); setUniform(CenterFragcoordUniform, Vector2(0, 0)); @@ -264,10 +264,10 @@ void lightmap_shader::add_light(Vector2 neighbor_offset, const light_s& light) mesh_view.setCount((int32_t)count*6); AbstractShaderProgram::draw(mesh_view); + setUniform(SamplerUniform, tuc.bind(framebuffer.scratch)); framebuffer.fb.mapForDraw({ { 1u, GL::Framebuffer::ColorAttachment{1} }, }); - setUniform(ModeUniform, BlendLightmapMode); AbstractShaderProgram::draw(light_mesh); } diff --git a/shaders/lightmap.hpp b/shaders/lightmap.hpp index 5bd38069..2c08cc62 100644 --- a/shaders/lightmap.hpp +++ b/shaders/lightmap.hpp @@ -1,6 +1,7 @@ #pragma once #include "light-falloff.hpp" +#include "shaders/texture-unit-cache.hpp" #include <array> #include <Corrade/Containers/Array.h> #include <Corrade/Containers/Optional.h> @@ -16,6 +17,8 @@ namespace floormat { +struct texture_unit_cache; + struct light_s final { Vector2 center; @@ -31,7 +34,7 @@ struct chunk; struct lightmap_shader final : GL::AbstractShaderProgram { - explicit lightmap_shader(); + explicit lightmap_shader(texture_unit_cache& tuc); ~lightmap_shader() override; struct Framebuffer final { @@ -85,6 +88,7 @@ private: void add_rect(Vector2 neighbor_offset, Pair<Vector2, Vector2> minmax); [[nodiscard]] std::array<Vector3, 4>& alloc_rect(); + texture_unit_cache& tuc; GL::Buffer vertex_buf{NoCreate}, index_buf{NoCreate}; Array<std::array<Vector3, 4>> vertexes; // todo make a contiguous allocation Array<std::array<UnsignedShort, 6>> indexes; diff --git a/shaders/shader.cpp b/shaders/shader.cpp index 4b184057..22222842 100644 --- a/shaders/shader.cpp +++ b/shaders/shader.cpp @@ -12,7 +12,7 @@ namespace floormat { -tile_shader::tile_shader() +tile_shader::tile_shader(texture_unit_cache& tuc) : tuc{tuc} { constexpr auto min_version = GL::Version::GL330; const auto version = GL::Context::current().version(); @@ -34,7 +34,7 @@ tile_shader::tile_shader() set_tint({1, 1, 1, 1}); setUniform(OffsetUniform, Vector3(Vector2(_camera_offset), _depth_offset)); setUniform(EnableLightmapUniform, _enable_lightmap); - setUniform(SamplerUniform, 0); + setUniform(SamplerUniform, _real_sampler = _sampler); setUniform(LightmapSamplerUniform, 1); } @@ -67,7 +67,13 @@ tile_shader& tile_shader::set_lightmap_enabled(bool value) return *this; } -void tile_shader::_draw() +tile_shader& tile_shader::set_sampler(Int sampler) +{ + _sampler = sampler; + return *this; +} + +void tile_shader::draw_pre(GL::AbstractTexture& tex) { fm_assert(std::fabs(_camera_offset[0]) < 1 << 24 && std::fabs(_camera_offset[1]) < 1 << 24); fm_assert(std::fabs(_depth_offset) < 1 << 24); @@ -75,12 +81,19 @@ void tile_shader::_draw() if (_tint != _real_tint) setUniform(TintUniform, _real_tint = _tint); - if (const auto offset = Vector3(Vector2(_camera_offset), _depth_offset); - offset != _real_camera_offset) - { - _real_camera_offset = offset; - setUniform(OffsetUniform, offset); - } + const auto offset = Vector3(Vector2(_camera_offset), _depth_offset); + if (offset != _real_camera_offset) + setUniform(OffsetUniform, _real_camera_offset = offset); + + auto id = tuc.bind(tex); + set_sampler(id); + if (_sampler != _real_sampler) + setUniform(SamplerUniform, _real_sampler = _sampler); +} + +void tile_shader::draw_post(GL::AbstractTexture& tex) // NOLINT(*-convert-member-functions-to-static) +{ + (void)tex; } float tile_shader::depth_value(const local_coords& xy, float offset) noexcept diff --git a/shaders/shader.hpp b/shaders/shader.hpp index e53fb6d3..bd0f5884 100644 --- a/shaders/shader.hpp +++ b/shaders/shader.hpp @@ -1,11 +1,14 @@ #pragma once #include "tile-defs.hpp" -#include <Corrade/Utility/Move.h> +#include "shaders/texture-unit-cache.hpp" +#include <utility> #include <Magnum/GL/AbstractShaderProgram.h> #include <Magnum/Math/Vector2.h> #include <Magnum/Math/Vector3.h> #include <Magnum/Math/Vector4.h> +namespace Magnum::GL { class AbstractTexture; } + namespace floormat { struct local_coords; @@ -17,7 +20,7 @@ struct tile_shader final : private GL::AbstractShaderProgram using LightCoord = GL::Attribute<2, Vector2>; using Depth = GL::Attribute<3, float>; - explicit tile_shader(); + explicit tile_shader(texture_unit_cache& tuc); ~tile_shader() override; Vector2 scale() const { return _scale; } @@ -34,7 +37,7 @@ struct tile_shader final : private GL::AbstractShaderProgram template<typename T = float> static constexpr Math::Vector2<T> project(const Math::Vector3<T>& pt); template<typename T = float> static constexpr Math::Vector2<T> unproject(const Math::Vector2<T>& px); - template<typename T, typename... Xs> decltype(auto) draw(T&& mesh, Xs&&... xs); + template<typename T, typename... Xs> decltype(auto) draw(GL::AbstractTexture& tex, T&& mesh, Xs&&... xs); static constexpr Vector2s max_screen_tiles = {8, 8}; static constexpr float character_depth_offset = 1 + 1./64; @@ -45,15 +48,21 @@ struct tile_shader final : private GL::AbstractShaderProgram static constexpr float depth_tile_size = 1.f/(TILE_MAX_DIM * 2 * max_screen_tiles.product()); static constexpr float foreshortening_factor = 0.578125f; + tile_shader& set_sampler(Int sampler); + Int sampler() const { return _sampler; } + private: - void _draw(); + void draw_pre(GL::AbstractTexture& tex); + void draw_post(GL::AbstractTexture& tex); + texture_unit_cache& tuc; // NOLINT(*-avoid-const-or-ref-data-members) Vector2d _camera_offset; Vector4 _tint, _real_tint; Vector2 _scale; Vector3 _real_camera_offset; float _depth_offset = 0; bool _enable_lightmap : 1 = false; + Int _sampler = 0, _real_sampler; enum { ScaleUniform = 0, OffsetUniform = 1, TintUniform = 2, @@ -63,10 +72,12 @@ private: }; template<typename T, typename... Xs> -decltype(auto) tile_shader::draw(T&& mesh, Xs&&... xs) +decltype(auto) tile_shader::draw(GL::AbstractTexture& tex, T&& mesh, Xs&&... xs) { - _draw(); - return GL::AbstractShaderProgram::draw(Utility::forward<T>(mesh), Utility::forward<Xs>(xs)...); + draw_pre(tex); + decltype(auto) ret = GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...); + draw_post(tex); + return ret; } template<typename T> |