summaryrefslogtreecommitdiffhomepage
path: root/shaders
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-08-30 20:36:44 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-08-31 06:43:49 +0200
commit10bd169bd4e1416c941faa2d5bc6a5be2e2fbfb8 (patch)
tree4c3897c9d4a85858cba9b99dd32ccd9326e4a28a /shaders
parenteda27f03e5115f7d9b4d2f2061e03e0bbe517370 (diff)
shaders/lightmap: use uniform blocks
Diffstat (limited to 'shaders')
-rw-r--r--shaders/lightmap.cpp45
-rw-r--r--shaders/lightmap.frag18
-rw-r--r--shaders/lightmap.hpp19
-rw-r--r--shaders/lightmap.vert19
4 files changed, 60 insertions, 41 deletions
diff --git a/shaders/lightmap.cpp b/shaders/lightmap.cpp
index 1be6080b..0585223b 100644
--- a/shaders/lightmap.cpp
+++ b/shaders/lightmap.cpp
@@ -62,6 +62,16 @@ GL::Mesh make_light_mesh(T&& vert, U&& index)
return mesh;
}
+struct Block final
+{
+ Vector4 light_color;
+ Vector2 scale;
+ Vector2 center_fragcoord;
+ Vector2 center_clip;
+ float range;
+ uint32_t falloff;
+};
+
} // namespace
auto lightmap_shader::make_framebuffer(Vector2i size) -> Framebuffer
@@ -200,13 +210,20 @@ lightmap_shader::lightmap_shader(texture_unit_cache& tuc) : tuc{tuc}
light_mesh = make_light_mesh(GL::Buffer{blend_vertexes}, GL::Buffer{quad_indexes(0)});
setUniform(SamplerUniform, 0);
- setUniform(LightColorUniform, 0xffffffff_rgbaf);
- setUniform(SizeUniform, Vector2(1));
- setUniform(CenterFragcoordUniform, Vector2(0, 0));
- setUniform(CenterClipUniform, Vector2(-1, -1));
- setUniform(RangeUniform, 1.f);
setUniform(ModeUniform, DrawLightmapMode);
- setUniform(FalloffUniform, (uint32_t)light_falloff::constant);
+
+ Block block {
+ .light_color = 0xffffffff_rgbaf,
+ .scale = Vector2(1),
+ .center_fragcoord = Vector2(0, 0),
+ .center_clip = Vector2(-1, -1),
+ .range = 1.f,
+ .falloff = (uint32_t)light_falloff::constant,
+ };
+
+ setUniformBlockBinding(uniformBlockIndex("Lightmap"_s), BlockUniform);
+ block_uniform_buf.setData({&block, 1});
+ block_uniform_buf.bind(GL::Buffer::Target::Uniform, BlockUniform);
}
std::array<UnsignedShort, 6> lightmap_shader::quad_indexes(size_t N)
@@ -246,12 +263,16 @@ void lightmap_shader::add_light(Vector2 neighbor_offset, const light_s& light)
{ 0u, GL::Framebuffer::ColorAttachment{0} },
});
- setUniform(LightColorUniform, Vector4(light.color) / 255.f);
- setUniform(SizeUniform, Vector2(1) / real_image_size);
- setUniform(CenterFragcoordUniform, center_fragcoord * image_size_ratio);
- setUniform(CenterClipUniform, center_clip);
- setUniform(RangeUniform, range * image_size_ratio.sum()/2);
- setUniform(FalloffUniform, (uint32_t)light.falloff);
+ Block block = {
+ .light_color = Vector4(light.color) / 255.f,
+ .scale = Vector2(1) / real_image_size,
+ .center_fragcoord = center_fragcoord * image_size_ratio,
+ .center_clip = center_clip,
+ .range = range * image_size_ratio.sum()/2,
+ .falloff = (uint32_t)light.falloff,
+ };
+
+ block_uniform_buf.setSubData(0, {&block, 1});
setUniform(ModeUniform, DrawLightmapMode);
AbstractShaderProgram::draw(light_mesh);
diff --git a/shaders/lightmap.frag b/shaders/lightmap.frag
index 9136a4a5..dec46f29 100644
--- a/shaders/lightmap.frag
+++ b/shaders/lightmap.frag
@@ -1,13 +1,17 @@
precision mediump float;
layout (location = 2) uniform sampler2D sampler0;
-layout (location = 3) uniform vec4 light_color;
-layout (location = 4) uniform vec2 scale;
-layout (location = 5) uniform vec2 center_fragcoord;
-layout (location = 6) uniform vec2 center_clip;
-layout (location = 7) uniform float range;
-layout (location = 8) uniform uint mode;
-layout (location = 9) uniform uint falloff;
+layout (location = 3) uniform uint mode;
+
+layout (std140)
+uniform Lightmap {
+ vec4 light_color;
+ vec2 scale;
+ vec2 center_fragcoord;
+ vec2 center_clip;
+ float range;
+ uint falloff;
+};
layout (location = 0) out vec4 color0;
layout (location = 1) out vec4 color1;
diff --git a/shaders/lightmap.hpp b/shaders/lightmap.hpp
index 2c08cc62..2dea87e9 100644
--- a/shaders/lightmap.hpp
+++ b/shaders/lightmap.hpp
@@ -58,17 +58,9 @@ struct lightmap_shader final : GL::AbstractShaderProgram
private:
enum : Int {
SamplerUniform = 2,
- LightColorUniform = 3,
- SizeUniform = 4,
- CenterFragcoordUniform = 5,
- CenterClipUniform = 6,
- RangeUniform = 7,
- ModeUniform = 8,
- FalloffUniform = 9,
- };
-
- enum : Int {
- TextureSampler = 1,
+ ModeUniform = 3,
+ // GL_MAX_UNIFORM_BUFFER_BINDINGS must be at least 36
+ BlockUniform = 35,
};
enum ShaderMode : uint32_t
@@ -88,8 +80,9 @@ 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};
+ texture_unit_cache& tuc; // NOLINT(*-avoid-const-or-ref-data-members)
+ GL::Buffer vertex_buf{NoCreate}, index_buf{NoCreate},
+ block_uniform_buf{GL::Buffer::TargetHint::Uniform, };
Array<std::array<Vector3, 4>> vertexes; // todo make a contiguous allocation
Array<std::array<UnsignedShort, 6>> indexes;
size_t count = 0, capacity = 0;
diff --git a/shaders/lightmap.vert b/shaders/lightmap.vert
index 2ed69f8b..1a8a39f1 100644
--- a/shaders/lightmap.vert
+++ b/shaders/lightmap.vert
@@ -1,16 +1,17 @@
precision mediump float;
layout (location = 2) uniform sampler2D sampler0;
-layout (location = 3) uniform vec4 light_color;
-layout (location = 4) uniform vec2 scale;
-layout (location = 5) uniform vec2 center_fragcoord;
-layout (location = 6) uniform vec2 center_clip;
-layout (location = 7) uniform float range;
-layout (location = 8) uniform uint mode;
-layout (location = 9) uniform uint falloff;
+layout (location = 3) uniform uint mode;
-//layout (location = 0) out vec2 frag_texcoords;
-//layout (location = 1) flat out vec2 frag_light_coord;
+layout (std140)
+uniform Lightmap {
+ vec4 light_color;
+ vec2 scale;
+ vec2 center_fragcoord;
+ vec2 center_clip;
+ float range;
+ uint falloff;
+};
layout (location = 0) in vec3 position;