libs/render/src/eu/render/shader_resource.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <memory> | ||
| 4 | |||
| 5 | #include "eu/render/uniform.h" | ||
| 6 | #include "eu/render/uniform_buffer.h" | ||
| 7 | #include "eu/core/vertex_layout.h" | ||
| 8 | |||
| 9 | namespace eu::core | ||
| 10 | { | ||
| 11 | struct CompiledGeomVertexAttributes; | ||
| 12 | } | ||
| 13 | |||
| 14 | namespace eu::render | ||
| 15 | { | ||
| 16 | struct FullScreenGeom; | ||
| 17 | struct RenderSettings; | ||
| 18 | struct ShaderProgram; | ||
| 19 | struct CompiledCamera; | ||
| 20 | struct ShadowContext; | ||
| 21 | struct Assets; | ||
| 22 | |||
| 23 | /** \addtogroup render Renderer | ||
| 24 | * @{ | ||
| 25 | */ | ||
| 26 | |||
| 27 | /// Enum describing on how the model transform is provided. | ||
| 28 | enum class TransformSource | ||
| 29 | { | ||
| 30 | /// the model source is provided as a mat4 uniform. | ||
| 31 | uniform, | ||
| 32 | |||
| 33 | /// the model source is provided as a (instanced) mat4 attribute | ||
| 34 | instanced_mat4 | ||
| 35 | }; | ||
| 36 | |||
| 37 | /// "Global state" for the shaders describing the state of the camera. | ||
| 38 | struct CameraUniformBuffer | ||
| 39 | { | ||
| 40 | core::UniformBufferSetup setup; | ||
| 41 | |||
| 42 | core::CompiledUniformProp clip_from_view_uni; | ||
| 43 | core::CompiledUniformProp view_from_world_uni; | ||
| 44 | |||
| 45 | std::unique_ptr<UniformBuffer> buffer; | ||
| 46 | |||
| 47 | void set_props(const CompiledCamera& cc); | ||
| 48 | }; | ||
| 49 | |||
| 50 | |||
| 51 | /// A single color shader. | ||
| 52 | struct LoadedShader_SingleColor | ||
| 53 | { | ||
| 54 | LoadedShader_SingleColor( | ||
| 55 | std::shared_ptr<ShaderProgram> p, core::CompiledGeomVertexAttributes l, const CameraUniformBuffer& desc | ||
| 56 | ); | ||
| 57 | |||
| 58 | std::shared_ptr<ShaderProgram> program; | ||
| 59 | core::CompiledGeomVertexAttributes geom_layout; | ||
| 60 | Uniform tint_color_uni; | ||
| 61 | Uniform world_from_local_uni; | ||
| 62 | }; | ||
| 63 | |||
| 64 | /// Only writes depth. | ||
| 65 | /// Useful for rendering shadow maps. | ||
| 66 | struct LoadedShader_OnlyDepth | ||
| 67 | { | ||
| 68 | LoadedShader_OnlyDepth( | ||
| 69 | TransformSource model_source, | ||
| 70 | std::shared_ptr<ShaderProgram> p, core::CompiledGeomVertexAttributes l, const CameraUniformBuffer& desc | ||
| 71 | ); | ||
| 72 | |||
| 73 | std::shared_ptr<ShaderProgram> program; | ||
| 74 | core::CompiledGeomVertexAttributes geom_layout; | ||
| 75 | |||
| 76 | std::optional<Uniform> world_from_local_uni; | ||
| 77 | }; | ||
| 78 | |||
| 79 | /// A skybox shader. | ||
| 80 | struct LoadedShader_Skybox | ||
| 81 | { | ||
| 82 | LoadedShader_Skybox( | ||
| 83 | std::shared_ptr<ShaderProgram> p, core::CompiledGeomVertexAttributes l, const CameraUniformBuffer& desc | ||
| 84 | ); | ||
| 85 | |||
| 86 | std::shared_ptr<ShaderProgram> program; | ||
| 87 | core::CompiledGeomVertexAttributes geom_layout; | ||
| 88 | Uniform tex_skybox_uniform; | ||
| 89 | }; | ||
| 90 | |||
| 91 | /// Parts of a loaded unlit shader | ||
| 92 | /// @see \ref LoadedShader_Unlit_Container | ||
| 93 | struct LoadedShader_Unlit | ||
| 94 | { | ||
| 95 | std::shared_ptr<ShaderProgram> program; | ||
| 96 | |||
| 97 | explicit LoadedShader_Unlit(TransformSource model_source, std::shared_ptr<ShaderProgram> p, const CameraUniformBuffer& desc); | ||
| 98 | |||
| 99 | Uniform tint_color_uni; | ||
| 100 | Uniform tex_diffuse_uniform; | ||
| 101 | |||
| 102 | std::optional<Uniform> world_from_local_uni; | ||
| 103 | }; | ||
| 104 | |||
| 105 | /// Uniform for a directional light. | ||
| 106 | /// @see \ref DirectionalLight | ||
| 107 | struct DirectionalLightUniforms | ||
| 108 | { | ||
| 109 | DirectionalLightUniforms(const ShaderProgram* program, const std::string& base); | ||
| 110 | |||
| 111 | Uniform light_diffuse_color_uni; | ||
| 112 | Uniform light_specular_color_uni; | ||
| 113 | Uniform dir_uni; | ||
| 114 | }; | ||
| 115 | |||
| 116 | /// Uniforms for a point light. | ||
| 117 | /// @see \ref PointLight | ||
| 118 | struct PointLightUniforms | ||
| 119 | { | ||
| 120 | PointLightUniforms(const ShaderProgram* program, const std::string& base); | ||
| 121 | |||
| 122 | Uniform light_diffuse_color_uni; | ||
| 123 | Uniform light_specular_color_uni; | ||
| 124 | Uniform light_attenuation_uni; | ||
| 125 | Uniform light_world_uni; | ||
| 126 | }; | ||
| 127 | |||
| 128 | /// Uniforms for a frustum light. | ||
| 129 | /// @see \ref FrustumLight | ||
| 130 | struct FrustumLightUniforms | ||
| 131 | { | ||
| 132 | FrustumLightUniforms(const ShaderProgram* program, const std::string& base); | ||
| 133 | |||
| 134 | Uniform diffuse_uni; | ||
| 135 | Uniform specular_uni; | ||
| 136 | Uniform attenuation_uni; | ||
| 137 | Uniform clip_from_world_uni; | ||
| 138 | Uniform world_pos_uni; | ||
| 139 | Uniform tex_cookie_uniform; | ||
| 140 | }; | ||
| 141 | |||
| 142 | /// Bitmask for what features each postproc shader wants. | ||
| 143 | enum class PostProcSetup | ||
| 144 | { | ||
| 145 | none = 0, | ||
| 146 | factor = 1 << 1, | ||
| 147 | resolution = 1 << 2, | ||
| 148 | time = 1 << 3 | ||
| 149 | }; | ||
| 150 | PostProcSetup operator|(PostProcSetup lhs, PostProcSetup rhs); | ||
| 151 | |||
| 152 | /// The "base class" for a loaded postproc shader. | ||
| 153 | struct LoadedPostProcShader | ||
| 154 | { | ||
| 155 | std::shared_ptr<ShaderProgram> program; | ||
| 156 | Uniform tex_input_uniform; | ||
| 157 | std::optional<Uniform> factor_uni; | ||
| 158 | std::optional<Uniform> resolution_uni; | ||
| 159 | std::optional<Uniform> time_uni; | ||
| 160 | |||
| 161 | explicit LoadedPostProcShader(std::shared_ptr<ShaderProgram> s, PostProcSetup setup); | ||
| 162 | }; | ||
| 163 | |||
| 164 | /// Part of a loaded "default" shader. | ||
| 165 | /// @see \ref LoadedShader_Default_Container | ||
| 166 | struct LoadedShader_Default | ||
| 167 | { | ||
| 168 | std::shared_ptr<ShaderProgram> program; | ||
| 169 | |||
| 170 | LoadedShader_Default( | ||
| 171 | TransformSource model_source, std::shared_ptr<ShaderProgram> p, const RenderSettings& settings, const CameraUniformBuffer& desc | ||
| 172 | ); | ||
| 173 | |||
| 174 | Uniform tint_color_uni; | ||
| 175 | Uniform tex_directional_light_depth_uni; | ||
| 176 | Uniform directional_shadow_clip_from_world_uni; | ||
| 177 | Uniform tex_diffuse_uniform; | ||
| 178 | Uniform tex_specular_uniform; | ||
| 179 | Uniform tex_emissive_uniform; | ||
| 180 | Uniform ambient_tint_uni; | ||
| 181 | Uniform specular_color_uni; | ||
| 182 | Uniform shininess_uni; | ||
| 183 | Uniform emissive_factor_uni; | ||
| 184 | |||
| 185 | std::optional<Uniform> world_from_local_uni; | ||
| 186 | |||
| 187 | Uniform view_position_uni; | ||
| 188 | Uniform light_ambient_color_uni; | ||
| 189 | |||
| 190 | std::vector<DirectionalLightUniforms> directional_lights; | ||
| 191 | std::vector<PointLightUniforms> point_lights; | ||
| 192 | std::vector<FrustumLightUniforms> frustum_lights; | ||
| 193 | }; | ||
| 194 | |||
| 195 | /// A "named boolean" | ||
| 196 | enum class UseTransparency | ||
| 197 | { | ||
| 198 | yes, | ||
| 199 | no | ||
| 200 | }; | ||
| 201 | |||
| 202 | /// Stores information that is useful when selecting a shader part from a shader container. | ||
| 203 | struct RenderContext | ||
| 204 | { | ||
| 205 | TransformSource model_source; | ||
| 206 | UseTransparency use_transparency; | ||
| 207 | float gamma; ///< gamma from the rendering settings | ||
| 208 | ShadowContext const* shadow_context; | ||
| 209 | |||
| 210 | ✗ | constexpr RenderContext(TransformSource s, UseTransparency t, float g, const ShadowContext* sc) | |
| 211 | ✗ | : model_source(s) | |
| 212 | ✗ | , use_transparency(t) | |
| 213 | ✗ | , gamma(g) | |
| 214 | ✗ | , shadow_context(sc) | |
| 215 | ✗ | {} | |
| 216 | }; | ||
| 217 | |||
| 218 | /// A unlit shader. | ||
| 219 | struct LoadedShader_Unlit_Container | ||
| 220 | { | ||
| 221 | core::CompiledGeomVertexAttributes geom_layout; | ||
| 222 | |||
| 223 | LoadedShader_Unlit default_shader; | ||
| 224 | LoadedShader_Unlit transparency_shader; | ||
| 225 | |||
| 226 | [[nodiscard]] bool is_loaded() const; | ||
| 227 | }; | ||
| 228 | |||
| 229 | /// A default shader | ||
| 230 | struct LoadedShader_Default_Container | ||
| 231 | { | ||
| 232 | core::CompiledGeomVertexAttributes geom_layout; | ||
| 233 | |||
| 234 | LoadedShader_Default default_shader; | ||
| 235 | LoadedShader_Default transparency_shader; | ||
| 236 | LoadedShader_Default default_shader_instance; | ||
| 237 | |||
| 238 | [[nodiscard]] bool is_loaded() const; | ||
| 239 | }; | ||
| 240 | |||
| 241 | /// Select the correct sub shader from a container. | ||
| 242 | [[nodiscard]] const LoadedShader_Unlit& shader_from_container(const LoadedShader_Unlit_Container& container, const RenderContext& rc); | ||
| 243 | |||
| 244 | /// Select the correct sub shader from a container. | ||
| 245 | [[nodiscard]] const LoadedShader_Default& shader_from_container(const LoadedShader_Default_Container& container, const RenderContext& rc); | ||
| 246 | |||
| 247 | // todo(Gustav): Rename this to composing shader | ||
| 248 | /// The shader data for composing a rendered image. | ||
| 249 | struct RealizeShader | ||
| 250 | { | ||
| 251 | explicit RealizeShader(std::shared_ptr<ShaderProgram> s); | ||
| 252 | |||
| 253 | std::shared_ptr<ShaderProgram> program; | ||
| 254 | Uniform tex_input_uniform; | ||
| 255 | Uniform tex_blurred_bloom_uniform; | ||
| 256 | |||
| 257 | Uniform use_blur_uniform; | ||
| 258 | Uniform gamma_uniform; | ||
| 259 | Uniform exposure_uniform; | ||
| 260 | }; | ||
| 261 | |||
| 262 | /// Extracts data for a bloom | ||
| 263 | struct ExtractShader | ||
| 264 | { | ||
| 265 | explicit ExtractShader(std::shared_ptr<LoadedPostProcShader>&& sh); | ||
| 266 | |||
| 267 | std::shared_ptr<LoadedPostProcShader> shader; | ||
| 268 | Uniform cutoff_uniform; | ||
| 269 | Uniform softness_uniform; | ||
| 270 | }; | ||
| 271 | |||
| 272 | struct PingPongBlurShader | ||
| 273 | { | ||
| 274 | explicit PingPongBlurShader(std::shared_ptr<LoadedPostProcShader>&& sh); | ||
| 275 | |||
| 276 | std::shared_ptr<LoadedPostProcShader> shader; | ||
| 277 | Uniform is_horizontal_uniform; | ||
| 278 | }; | ||
| 279 | |||
| 280 | |||
| 281 | /// All loaded shaders. | ||
| 282 | struct ShaderResource | ||
| 283 | { | ||
| 284 | LoadedShader_SingleColor single_color_shader; | ||
| 285 | LoadedShader_OnlyDepth depth_transform_uniform; | ||
| 286 | LoadedShader_OnlyDepth depth_transform_instanced_mat4; | ||
| 287 | LoadedShader_Skybox skybox_shader; | ||
| 288 | |||
| 289 | LoadedShader_Unlit_Container unlit_shader_container; | ||
| 290 | LoadedShader_Default_Container default_shader_container; | ||
| 291 | |||
| 292 | /// the realization shader that is always run | ||
| 293 | RealizeShader pp_realize; | ||
| 294 | ExtractShader pp_extract; | ||
| 295 | PingPongBlurShader pp_ping; | ||
| 296 | |||
| 297 | /// verify that the shaders are loaded | ||
| 298 | [[nodiscard]] bool is_loaded() const; | ||
| 299 | }; | ||
| 300 | |||
| 301 | ShaderResource load_shaders(const Assets& assets, const CameraUniformBuffer& desc, const RenderSettings& settings, const FullScreenGeom& full_screen); | ||
| 302 | |||
| 303 | /** | ||
| 304 | * @} | ||
| 305 | */ | ||
| 306 | |||
| 307 | } // namespace eu::render | ||
| 308 |