Antkeeper  0.0.1
final-pass.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
22 #include <engine/gl/pipeline.hpp>
28 #include <engine/gl/texture.hpp>
31 #include <cmath>
32 #include <cstdint>
34 #include <engine/debug/log.hpp>
35 
36 namespace render {
37 
39  pass(pipeline, framebuffer)
40 {
41  // Construct empty vertex array
42  m_vertex_array = std::make_unique<gl::vertex_array>();
43 
44  // Load shader template and build shader program
45  auto shader_template = resource_manager->load<gl::shader_template>("final.glsl");
46  m_shader_program = shader_template->build();
47  if (!m_shader_program->linked())
48  {
49  debug::log_error("Failed to final pass shader program: {}", m_shader_program->info());
50  debug::log_warning("{}", shader_template->configure(gl::shader_stage::vertex));
51  }
52 }
53 
55 {
56  // Update resolution
57  const auto& viewport_dimensions = (m_framebuffer) ? m_framebuffer->dimensions() : m_pipeline->get_default_framebuffer_dimensions();
58  m_resolution = {static_cast<float>(viewport_dimensions[0]), static_cast<float>(viewport_dimensions[1])};
59 
60  // Update time
61  m_time = ctx.t;
62 
63  // Execute render commands
64  for (const auto& command: m_command_buffer)
65  {
66  command();
67  }
68 
69  // Increment current frame
70  ++m_frame;
71 }
72 
73 void final_pass::set_color_texture(std::shared_ptr<gl::texture_2d> texture)
74 {
75  m_color_texture = texture;
76  rebuild_command_buffer();
77 }
78 
79 void final_pass::set_bloom_texture(std::shared_ptr<gl::texture_2d> texture) noexcept
80 {
81  m_bloom_texture = texture;
82  rebuild_command_buffer();
83 }
84 
85 void final_pass::set_bloom_weight(float weight) noexcept
86 {
87  m_bloom_weight = weight;
88 }
89 
90 void final_pass::set_blue_noise_texture(std::shared_ptr<gl::texture_2d> texture)
91 {
92  m_blue_noise_texture = texture;
93  m_blue_noise_scale = 1.0f / static_cast<float>(texture->get_image_view()->get_image()->get_dimensions()[0]);
94  rebuild_command_buffer();
95 }
96 
97 void final_pass::rebuild_command_buffer()
98 {
99  m_command_buffer.clear();
100 
101  m_command_buffer.emplace_back
102  (
103  [&]()
104  {
105  const gl::viewport viewport[1] = {{0.0f, 0.0f, m_resolution.x(), m_resolution.y()}};
106 
108  m_pipeline->set_viewport(0, viewport);
113  m_pipeline->bind_shader_program(m_shader_program.get());
114  m_pipeline->bind_vertex_array(m_vertex_array.get());
115  }
116  );
117 
118  if (m_color_texture)
119  {
120  if (const auto var = m_shader_program->variable("color_texture"))
121  {
122  m_command_buffer.emplace_back([&, var](){var->update(*m_color_texture);});
123  }
124  }
125  if (m_bloom_texture)
126  {
127  if (const auto var = m_shader_program->variable("bloom_texture"))
128  {
129  m_command_buffer.emplace_back([&, var](){var->update(*m_bloom_texture);});
130  }
131  }
132  if (m_blue_noise_texture)
133  {
134  if (const auto var = m_shader_program->variable("blue_noise_texture"))
135  {
136  m_command_buffer.emplace_back([&, var](){var->update(*m_blue_noise_texture);});
137  }
138  }
139 
140  if (const auto var = m_shader_program->variable("bloom_weight"))
141  {
142  m_command_buffer.emplace_back([&, var](){var->update(m_bloom_weight);});
143  }
144  if (const auto var = m_shader_program->variable("blue_noise_scale"))
145  {
146  m_command_buffer.emplace_back([&, var](){var->update(m_blue_noise_scale);});
147  }
148  if (const auto var = m_shader_program->variable("resolution"))
149  {
150  m_command_buffer.emplace_back([&, var](){var->update(m_resolution);});
151  }
152  if (const auto var = m_shader_program->variable("time"))
153  {
154  m_command_buffer.emplace_back([&, var](){var->update(m_time);});
155  }
156  if (const auto frame_var = m_shader_program->variable("frame"))
157  {
158  m_command_buffer.emplace_back([&, frame_var](){frame_var->update(m_frame);});
159  }
160 
161  m_command_buffer.emplace_back
162  (
163  [&]()
164  {
165  // Draw fullscreen triangle
166  m_pipeline->draw(3, 1, 0, 0);
167  }
168  );
169 }
170 
171 } // namespace render
constexpr const std::array< std::uint32_t, 2 > & dimensions() const noexcept
Returns the dimensions of the framebuffer.
Definition: framebuffer.hpp:67
Graphics pipeline interface.
Definition: pipeline.hpp:48
void set_primitive_topology(primitive_topology topology)
Sets the primitive topology to use for drawing.
Definition: pipeline.cpp:356
void bind_shader_program(const gl::shader_program *shader_program)
Sets the vertex input.
Definition: pipeline.cpp:292
void bind_framebuffer(const gl::framebuffer *framebuffer)
Sets the vertex input.
Definition: pipeline.cpp:275
void set_color_blend_enabled(bool enabled)
Controls whether blending is enabled for the corresponding color attachment.
Definition: pipeline.cpp:953
void set_cull_mode(cull_mode mode)
Sets the triangle culling mode.
Definition: pipeline.cpp:503
void set_viewport(std::uint32_t first_viewport, std::span< const viewport > viewports)
Sets one or more viewports.
Definition: pipeline.cpp:381
void draw(std::uint32_t vertex_count, std::uint32_t instance_count, std::uint32_t first_vertex, std::uint32_t first_instance)
Draws primitives.
Definition: pipeline.cpp:1028
constexpr const std::array< std::uint32_t, 2 > & get_default_framebuffer_dimensions() const noexcept
Returns the dimensions of the default framebuffer.
Definition: pipeline.hpp:383
void set_depth_test_enabled(bool enabled)
Controls whether depth testing is enabled.
Definition: pipeline.cpp:649
void bind_vertex_array(const vertex_array *array)
Binds a vertex array.
Definition: pipeline.cpp:309
Template used to for generating one or more shader variants from a single source.
void set_color_texture(std::shared_ptr< gl::texture_2d > texture)
Definition: final-pass.cpp:73
void render(render::context &ctx) override
Definition: final-pass.cpp:54
void set_blue_noise_texture(std::shared_ptr< gl::texture_2d > texture)
Definition: final-pass.cpp:90
void set_bloom_weight(float weight) noexcept
Definition: final-pass.cpp:85
void set_bloom_texture(std::shared_ptr< gl::texture_2d > texture) noexcept
Definition: final-pass.cpp:79
final_pass(gl::pipeline *pipeline, const gl::framebuffer *framebuffer, resource_manager *resource_manager)
Definition: final-pass.cpp:38
Render pass.
Definition: pass.hpp:34
gl::pipeline * m_pipeline
Definition: pass.hpp:63
const gl::framebuffer * m_framebuffer
Definition: pass.hpp:64
Manages the loading, caching, and saving of resources.
std::shared_ptr< T > load(const std::filesystem::path &path)
Loads and caches a resource.
Commands which operate on entity::id components.
Definition: commands.cpp:28
log_message< log_message_severity::warning, Args... > log_warning
Formats and logs a warning message.
Definition: log.hpp:130
log_message< log_message_severity::error, Args... > log_error
Formats and logs an error message.
Definition: log.hpp:144
@ back
Back-facing triangles are discarded.
@ vertex
Vertex shader stage.
@ triangle_list
Separate triangle primitives.
High-level rendering.
Viewport position, dimensions, and depth range.
Definition: viewport.hpp:31
constexpr element_type & x() noexcept
Returns a reference to the first element.
Definition: vector.hpp:164
constexpr element_type & y() noexcept
Returns a reference to the second element.
Definition: vector.hpp:180
Context of a renderer.
Definition: context.hpp:40
float t
Current time, in seconds.
Definition: context.hpp:48