Antkeeper  0.0.1
resample-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 
32 namespace render {
33 
35  pass(pipeline, framebuffer)
36 {
37  // Construct empty vertex array
38  m_vertex_array = std::make_unique<gl::vertex_array>();
39 
40  // Load resample shader template
41  auto shader_template = resource_manager->load<gl::shader_template>("resample.glsl");
42 
43  // Build resample shader program
44  m_shader_program = shader_template->build();
45  if (!m_shader_program->linked())
46  {
47  debug::log_error("Failed to build resample shader program: {}", m_shader_program->info());
48  debug::log_warning("{}", shader_template->configure(gl::shader_stage::vertex));
49  }
50 }
51 
53 {
54  for (const auto& command: m_command_buffer)
55  {
56  command();
57  }
58 }
59 
60 void resample_pass::set_source_texture(std::shared_ptr<gl::texture_2d> texture)
61 {
62  m_source_texture = texture;
63  rebuild_command_buffer();
64 }
65 
66 void resample_pass::rebuild_command_buffer()
67 {
68  m_command_buffer.clear();
69 
70  if (!m_source_texture || !m_shader_program)
71  {
72  return;
73  }
74 
75  // Setup resample state
76  m_command_buffer.emplace_back
77  (
78  [&]()
79  {
80  const auto& viewport_dimensions = (m_framebuffer) ? m_framebuffer->dimensions() : m_pipeline->get_default_framebuffer_dimensions();
81  const gl::viewport viewport[1] =
82  {{
83  0.0f,
84  0.0f,
85  static_cast<float>(viewport_dimensions[0]),
86  static_cast<float>(viewport_dimensions[1])
87  }};
88 
90  m_pipeline->set_viewport(0, viewport);
93 
95  m_pipeline->bind_shader_program(m_shader_program.get());
96  m_pipeline->bind_vertex_array(m_vertex_array.get());
97  }
98  );
99 
100  // Update shader variables
101  if (auto source_texture_var = m_shader_program->variable("source_texture"))
102  {
103  m_command_buffer.emplace_back([&, source_texture_var](){source_texture_var->update(*m_source_texture);});
104  }
105 
106  m_command_buffer.emplace_back
107  (
108  [&]()
109  {
110  // Draw fullscreen triangle
111  m_pipeline->draw(3, 1, 0, 0);
112  }
113  );
114 }
115 
116 } // 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_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.
Render pass.
Definition: pass.hpp:34
gl::pipeline * m_pipeline
Definition: pass.hpp:63
const gl::framebuffer * m_framebuffer
Definition: pass.hpp:64
resample_pass(gl::pipeline *pipeline, const gl::framebuffer *framebuffer, resource_manager *resource_manager)
Constructs a resample pass.
void set_source_texture(std::shared_ptr< gl::texture_2d > texture)
Sets the resample source texture.
void render(render::context &ctx) override
Resamples a texture.
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
Context of a renderer.
Definition: context.hpp:40