Skip to content

Shaders

McHorse edited this page Sep 3, 2023 · 4 revisions

IMPORTANT: this is WIP feature on bbs-studio branch.

Shader pipeline

Shader pipeline allows to configure how final picture gets assembled. Following is an example default.shader.json from BBS.

{
    /* Gbuffers is a list of buffers (textures) that can be written to during 
     * first pass in world shaders. These buffers can be accessed via their uniform 
     * name in composite shaders as well. */
    "gbuffers": [
        {
            /* Name of the uniform by which this buffer is accessible in all composite shaders */
            "name": "u_texture",
            /* Storage format. Following are availabke: rgba_u8, rgb_u8, rgba_f16 and depth_f24 */
            "format": "rgba_u8"
        },
        {
            "name": "u_position",
            "format": "rgba_f16"
        },
        {
            "name": "u_normal",
            "format": "rgba_f16"
        },
        {
            "name": "u_lighting",
            "format": "rgba_u8"
        },
        {
            "name": "u_depth",
            "format": "depth_f24"
        }
    ],
    
    /* Composite is an array of buffers that can be written to or read from.
     * The format is the same as with gbuffers section. */
    "composite": [
        {
            /* This name will be used for a uniform when it's specified in the composite's stage as an input */
            "name": "u_texture0",
            "format": "rgba_u8",
            /* Whether texture's filter is GL_NEAREST (false) or GL_LINEAR (true) */
            "linear": true
        },
        {
            "name": "u_texture1",
            "format": "rgba_u8"
        },
        {
            "name": "u_texture2",
            "format": "rgba_f16"
        },
        {
            "name": "u_texture3",
            "format": "rgba_f16",
            /* Whether texture will be cleared in the beginning of the stage where it is 
             * input to (if false, it allows to accumulate the data) */
            "clear": false
        }
    ],

    /* Shadow map settings */
    "shadow": {
        /* Whether shadow map will be rendered, and u_shadowmap available */
        "enabled": true,
        /* Resolution of the shadow map (the texture is square) */
        "resolution": 1024
    },
    
    /* Stages contains an array (in order) of composite shaders that post process 
     * gbuffer datat */
    "stages": [
        {
            /* Shader is the path to a GLSL program that will execute at that stage */
            "shader": "studio:shaders/default/deferred/vertex_2d-composite.glsl",
            /* Output is the name of a buffer specified in composite to which data can be written to */
            "output": ["u_texture1"]
        },
        {
            "shader": "studio:shaders/default/deferred/vertex_2d-composite1.glsl",
            /* Input is the name of a buffer specified in composite which can be sampled.
             * In order to sample that buffer you need to define it as uniform with the 
             * same name as the name of the buffer. */
            "input": ["u_texture3"],
            "output": ["u_texture2"]
        },
        {
            "shader": "studio:shaders/default/deferred/vertex_2d-composite2.glsl",
            "input": ["u_texture1", "u_texture2"],
            /* IMPORTANT:
             * The first output buffer in the last stage will be used to render on the screen! */
            "output": ["u_texture0", "u_texture3"]
        }
    ]
}

Shader uniforms

Following uniforms are available in composite shaders beside textures from gbuffer pass:

// Top color of the skybox gradient
uniform vec3 u_zenith;
// Middle color of the skybox gradient
uniform vec3 u_horizon;
// Bottom color of the skybox gradient
uniform vec3 u_bottom;
// Shading direction
uniform vec3 u_shading;
// Fog max distance
uniform int u_fog; 

// Texture of the lightmap
uniform sampler2D u_lightmap; 
// Current camera position
uniform vec3 u_camera; 
// Current camera projection
uniform mat4 u_projection; 
// Current camera view
uniform mat4 u_view; 
// Inverse matrix of `u_projection`
uniform mat4 u_projection_inv; 
// Inverse matrix of `u_view`
uniform mat4 u_view_inv; 
// Frame counter
uniform int u_frames; 
// Camera's near plane
uniform float u_far; 
// Camera's near plane
uniform float u_near; 
// Framebuffer's resolution (.x is width, .y is height)
uniform vec2 u_screen_size; 
// Previous frame's u_camera
uniform vec3 u_prev_camera; 
// Previous frame's u_projection
uniform mat4 u_prev_projection; 
// Previous frame's u_view
uniform mat4 u_prev_view;

// Shadow projection
uniform mat4 u_shadow_projection;
// Shadow view (near and far should be same as u_near and u_far for now)
uniform mat4 u_shadow_view;
// Shadow resolution (the texture is square)
uniform float u_shadow_resolution;
// Shadow map sampler
uniform sampler2D u_shadowmap;
Clone this wiki locally