Antkeeper  0.0.1
material-pass.hpp
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 
20 #ifndef ANTKEEPER_RENDER_MATERIAL_PASS_HPP
21 #define ANTKEEPER_RENDER_MATERIAL_PASS_HPP
22 
23 #include <engine/render/pass.hpp>
26 #include <engine/math/vector.hpp>
29 #include <engine/gl/texture.hpp>
30 #include <functional>
31 #include <unordered_map>
32 #include <span>
33 
34 class resource_manager;
35 
36 namespace render {
37 
41 class material_pass: public pass
42 {
43 public:
45 
46  void render(render::context& ctx) override;
47 
49  void set_fallback_material(std::shared_ptr<render::material> fallback);
50 
52  {
53  mouse_position = position;
54  }
55 
56 private:
57  struct shader_cache_entry
58  {
59  std::unique_ptr<gl::shader_program> shader_program;
60 
62  std::vector<std::function<void()>> shader_command_buffer;
63 
65  std::vector<std::function<void()>> geometry_command_buffer;
66 
68  std::unordered_map<const material*, std::vector<std::function<void()>>> material_command_buffers;
69  };
70 
72  std::unordered_map<std::size_t, shader_cache_entry> shader_cache;
73 
77  void evaluate_camera(const render::context& ctx);
78 
82  void evaluate_lighting(const render::context& ctx, std::uint32_t layer_mask);
83 
84  void evaluate_misc(const render::context& ctx);
85 
86  [[nodiscard]] std::unique_ptr<gl::shader_program> generate_shader_program(const gl::shader_template& shader_template, material_blend_mode blend_mode) const;
87 
88  void build_shader_command_buffer(std::vector<std::function<void()>>& command_buffer, const gl::shader_program& shader_program) const;
89  void build_geometry_command_buffer(std::vector<std::function<void()>>& command_buffer, const gl::shader_program& shader_program) const;
90  void build_material_command_buffer(std::vector<std::function<void()>>& command_buffer, const gl::shader_program& shader_program, const material& material) const;
91 
92  // Camera
93  const math::fmat4* view;
94  const math::fmat4* inv_view;
95  const math::fmat4* projection;
96  const math::fmat4* view_projection;
97  math::fvec4 view_translation;
98  math::fmat4 view_rotation;
99  math::fmat4 model_view;
100  const math::fvec3* camera_position;
101  float camera_exposure;
102 
103  // Light probes
104  const gl::texture_cube* light_probe_luminance_texture{};
105  const gl::texture_1d* light_probe_illuminance_texture{};
106  std::size_t light_probe_count;
107 
108  // Point lights
109  std::vector<math::fvec3> point_light_colors;
110  std::vector<math::fvec3> point_light_positions;
111  std::size_t point_light_count;
112 
113  // Directional lights
114  std::vector<math::fvec3> directional_light_colors;
115  std::vector<math::fvec3> directional_light_directions;
116  std::size_t directional_light_count;
117 
118  // Directional shadows
119  std::vector<const gl::texture_2d*> directional_shadow_maps;
120  std::vector<math::fvec4> directional_shadow_splits;
121  std::vector<float> directional_shadow_fade_ranges;
122  std::vector<std::span<const math::fmat4>> directional_shadow_matrices;
123  std::size_t directional_shadow_count;
124 
125  // Spot lights
126  std::vector<math::fvec3> spot_light_colors;
127  std::vector<math::fvec3> spot_light_positions;
128  std::vector<math::fvec3> spot_light_directions;
129  std::vector<math::fvec2> spot_light_cutoffs;
130  std::size_t spot_light_count;
131 
132  // Rectangle lights
133  std::vector<math::fvec3> rectangle_light_colors;
134  std::vector<math::fvec3> rectangle_light_corners;
135  std::size_t rectangle_light_count;
136 
137  // LTC
138  std::shared_ptr<gl::texture_2d> ltc_lut_1;
139  std::shared_ptr<gl::texture_2d> ltc_lut_2;
140 
141  // IBL
142  std::shared_ptr<gl::texture_2d> brdf_lut;
143 
144  // Misc
145  float time;
146  float timestep;
147  unsigned int frame{0};
148  float subframe;
149  math::fvec2 resolution;
150  math::fvec2 mouse_position;
151 
152  // Geometry
153  const math::fmat4* model;
154  std::span<const math::fmat4> matrix_palette;
155 
157  std::size_t lighting_state_hash;
158 
159  std::shared_ptr<render::material> fallback_material;
160 };
161 
162 } // namespace render
163 
164 #endif // ANTKEEPER_RENDER_MATERIAL_PASS_HPP
Graphics pipeline interface.
Definition: pipeline.hpp:48
Shader program which can be linked to shader objects and executed.
Template used to for generating one or more shader variants from a single source.
1D texture.
Definition: texture.hpp:73
Cube texture.
Definition: texture.hpp:243
Renders scene objects using their material-specified shaders and properties.
void set_fallback_material(std::shared_ptr< render::material > fallback)
Sets the material to be used when a render operation is missing a material. If no fallback material i...
void set_mouse_position(const math::fvec2 &position)
void render(render::context &ctx) override
material_pass(gl::pipeline *pipeline, const gl::framebuffer *framebuffer, resource_manager *resource_manager)
A material is associated with exactly one shader program and contains a set of material properties wh...
Definition: material.hpp:37
Render pass.
Definition: pass.hpp:34
Manages the loading, caching, and saving of resources.
High-level rendering.
material_blend_mode
Material blend modes.
n by m column-major matrix.
Definition: math/matrix.hpp:44
n-dimensional vector.
Definition: vector.hpp:44
Context of a renderer.
Definition: context.hpp:40