diff options
Diffstat (limited to 'src/render/llr_impl.h')
| -rw-r--r-- | src/render/llr_impl.h | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/render/llr_impl.h b/src/render/llr_impl.h new file mode 100644 index 0000000..319441c --- /dev/null +++ b/src/render/llr_impl.h | |||
| @@ -0,0 +1,117 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <gfx/render/llr.h> | ||
| 4 | #include <gfx/sizes.h> | ||
| 5 | |||
| 6 | #include "scene/types.h" | ||
| 7 | |||
| 8 | #include <math/mat4.h> | ||
| 9 | #include <math/vec3.h> | ||
| 10 | |||
| 11 | #include <stdbool.h> | ||
| 12 | #include <stddef.h> | ||
| 13 | #include <stdint.h> | ||
| 14 | |||
| 15 | typedef struct Geometry Geometry; | ||
| 16 | typedef struct GfxCore GfxCore; | ||
| 17 | typedef struct IBL IBL; | ||
| 18 | typedef struct Material Material; | ||
| 19 | typedef struct ShaderProgram ShaderProgram; | ||
| 20 | typedef struct Texture Texture; | ||
| 21 | |||
| 22 | /// An environment light. | ||
| 23 | typedef struct EnvironmentLight { | ||
| 24 | const Texture* environment_map; | ||
| 25 | const Texture* irradiance_map; // Renderer implementation. | ||
| 26 | const Texture* prefiltered_environment_map; // Renderer implementation. | ||
| 27 | int max_reflection_lod; // Mandatory when prefiltered_environment_map is | ||
| 28 | // given. | ||
| 29 | } EnvironmentLight; | ||
| 30 | |||
| 31 | /// A scene light. | ||
| 32 | typedef struct Light { | ||
| 33 | LightType type; | ||
| 34 | union { | ||
| 35 | EnvironmentLight environment; | ||
| 36 | }; | ||
| 37 | node_idx parent; // Parent SceneNode. | ||
| 38 | } Light; | ||
| 39 | |||
| 40 | typedef struct Material { | ||
| 41 | ShaderUniform uniforms[GFX_MAX_UNIFORMS_PER_MATERIAL]; | ||
| 42 | int num_uniforms; | ||
| 43 | } Material; | ||
| 44 | |||
| 45 | typedef struct Mesh { | ||
| 46 | const Geometry* geometry; | ||
| 47 | const Material* material; | ||
| 48 | ShaderProgram* shader; // TODO: Move this back to Material? | ||
| 49 | } Mesh; | ||
| 50 | |||
| 51 | /// Immediate mode renderer. | ||
| 52 | /// | ||
| 53 | /// The renderer caches state changes in memory and only programs the underlying | ||
| 54 | /// shader program when a draw call is issued and if anything has changed. This | ||
| 55 | /// keeps the number of graphics API calls to a minimum, but requires tracking | ||
| 56 | /// state changes. The 'changed' booleans below fulfill this purpose, and | ||
| 57 | /// indicate whether a given state has changed since the last draw call. | ||
| 58 | /// | ||
| 59 | /// The renderer must combine state changes accordingly. For example, if only | ||
| 60 | /// the lights have changed, then it is sufficient to update light uniforms in | ||
| 61 | /// the current shader program. On the other hand, if the shader program has | ||
| 62 | /// changed, then the renderer must reconfigure it from scratch and set light | ||
| 63 | /// uniforms, camera uniforms, etc. | ||
| 64 | /// | ||
| 65 | /// Note that the shader program API has its own level of caching as well, so | ||
| 66 | /// reconfiguration at the level of the renderer does not result in the | ||
| 67 | /// worst-case set of graphics API calls. | ||
| 68 | typedef struct LLR { | ||
| 69 | GfxCore* gfxcore; | ||
| 70 | |||
| 71 | union { | ||
| 72 | struct { | ||
| 73 | bool shader_changed : 1; // Whether the shader has changed. | ||
| 74 | bool camera_changed : 1; // Whether the camera parameters have changed. | ||
| 75 | bool lights_changed : 1; // Whether the lights have changed. | ||
| 76 | bool skeleton_changed : 1; // Whether the skeleton has changed. | ||
| 77 | bool material_changed : 1; // Whether the material has changed. | ||
| 78 | bool matrix_changed : 1; // Whether the matrix stack has changed. | ||
| 79 | }; | ||
| 80 | uint8_t changed_flags; | ||
| 81 | }; | ||
| 82 | |||
| 83 | IBL* ibl; | ||
| 84 | Texture* brdf_integration_map; | ||
| 85 | |||
| 86 | ShaderProgram* shader; // Active shader. Not owned. | ||
| 87 | |||
| 88 | const Material* material; // Active material. Not owned. | ||
| 89 | |||
| 90 | vec3 camera_position; | ||
| 91 | mat4 camera_rotation; | ||
| 92 | mat4 view; // Camera view matrix. | ||
| 93 | mat4 projection; // Camera projection matrix. | ||
| 94 | R fovy; // Camera vertical field of view. | ||
| 95 | R aspect; // Aspect ratio. | ||
| 96 | |||
| 97 | // Lights are not const because environment lights store lazily-computed | ||
| 98 | // irradiance maps. | ||
| 99 | Light* lights[GFX_LLR_MAX_NUM_LIGHTS]; // Lights stack. | ||
| 100 | int num_lights; // Number of lights enabled at a given point in time. It | ||
| 101 | // points to one past the top of the stack. | ||
| 102 | |||
| 103 | size_t num_joints; | ||
| 104 | mat4 joint_matrices[GFX_MAX_NUM_JOINTS]; | ||
| 105 | |||
| 106 | // The matrix stack contains pre-multiplied matrices. | ||
| 107 | // It is also never empty. The top of the stack is an identity matrix when the | ||
| 108 | // stack is "empty" from the user's perspective. | ||
| 109 | mat4 matrix_stack[GFX_LLR_MAX_NUM_MATRICES]; | ||
| 110 | int stack_pointer; // Points to the top of the stack. | ||
| 111 | } LLR; | ||
| 112 | |||
| 113 | /// Create a new immediate mode renderer. | ||
| 114 | bool gfx_llr_make(LLR*, GfxCore*); | ||
| 115 | |||
| 116 | /// Destroy the immediate mode renderer. | ||
| 117 | void gfx_llr_destroy(LLR*); | ||
