Skip to content

Commit

Permalink
shaders/lightmap: use uniform blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sthalik committed Aug 31, 2023
1 parent eda27f0 commit 10bd169
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 41 deletions.
45 changes: 33 additions & 12 deletions shaders/lightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 11 additions & 7 deletions shaders/lightmap.frag
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
19 changes: 6 additions & 13 deletions shaders/lightmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -88,8 +80,9 @@ struct lightmap_shader final : GL::AbstractShaderProgram
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;
Expand Down
19 changes: 10 additions & 9 deletions shaders/lightmap.vert
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit 10bd169

Please sign in to comment.