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