From eb06c9d49fddd6ffd5725bf8a33c2eab8cfd94c7 Mon Sep 17 00:00:00 2001 From: onelin Date: Wed, 3 Dec 2025 22:21:54 +0100 Subject: Add support for depth & stencil buffers --- src/include/daw/rendering.h | 77 ++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 22 deletions(-) (limited to 'src/include') diff --git a/src/include/daw/rendering.h b/src/include/daw/rendering.h index ecfb438..9f8ba7c 100644 --- a/src/include/daw/rendering.h +++ b/src/include/daw/rendering.h @@ -206,26 +206,7 @@ typedef enum { } BufferType; -// Buffer parameter: Frame buffer -// Buffer parameter: Texture buffer -#define BUFFERPARAMETER_TEXTURE_GET_DIMENSION(bufferparam) (BUFFERPARAMETER_GET_PARAMETER(bufferparam) & ((1 << 3) - 1)) -#define BUFFERPARAMETER_TEXTURE_1D 1 -#define BUFFERPARAMETER_TEXTURE_2D 2 -#define BUFFERPARAMETER_TEXTURE_3D 3 -#define BUFFERPARAMETER_TEXTURE_4D 4 - -// TODO: Change `2` once we add support for more formats -#define BUFFERPARAMETER_TEXTURE_GET_FORMAT(bufferparam) (BUFFERPARAMETER_GET_PARAMETER(bufferparam) & (((1 << 2) - 1) << 3)) -#define BUFFERPARAMETER_TEXTURE_FMT_RGBA8 (1 << 4) -#define BUFFERPARAMETER_TEXTURE_FMT_SRGB8 (2 << 4) -#define BUFFERPARAMETER_TEXTURE_FMT_SRGBA8 (3 << 4) - -//TODO: texture formats - -// Buffer parameter: Buffer buffer -#define BUFFERPARAMETER_BUFFER_DEPTH 1 -#define BUFFERPARAMETER_BUFFER_STENCIL 2 - +/* Buffer set/get macros */ // `buffer_definition` has the first 4 bits for the type, #define BUFFERPARAMETER_MASK_TYPE ((1 << 4) - 1) // next 16 bits for the type-specific parameters @@ -236,12 +217,58 @@ typedef enum { #define BUFFERPARAMETER_GET_PARAMETER(bufferparam) \ ((bufferparam & BUFFERPARAMETER_MASK_PARAMETER) >> 4) -// SET operations clear the masked bits that you're setting +// SET operations clear the masked bits that you're setting. Set multiple parameters by +// OR'ing them. #define BUFFERPARAMETER_SET_TYPE(bufferparam, type) \ (BufferType)((bufferparam & ~BUFFERPARAMETER_MASK_TYPE) | type) #define BUFFERPARAMETER_SET_PARAMETER(bufferparam, param) \ ((bufferparam & ~BUFFERPARAMETER_MASK_PARAMETER) | ((param) << 4)) +// Textures and RenderBuffers can have different formats internally. These are +// mutually exclusive and shared between texture and renderbuffer buffers. + +//TODO: More formats +#define BUFFERPARAMETER_FMT_RGBA8 (1 << 8) +#define BUFFERPARAMETER_FMT_SRGB8 (2 << 8) +#define BUFFERPARAMETER_FMT_SRGBA8 (3 << 8) + +#define BUFFERPARAMETER_FMT_DEPTH24_STENCIL8 (4 << 8) +#define BUFFERPARAMETER_FMT_DEPTH16 (5 << 8) +#define BUFFERPARAMETER_FMT_DEPTH24 (6 << 8) +#define BUFFERPARAMETER_FMT_DEPTH32 (7 << 8) +#define BUFFERPARAMETER_FMT_DEPTH32F (8 << 8) +#define BUFFERPARAMETER_FMT_STENCIL8 (9 << 8) + +// "Easy default" values +#define BUFFERPARAMETER_FMT_DEPTH_STENCIL BUFFERPARAMETER_FMT_DEPTH24_STENCIL8 +#define BUFFERPARAMETER_FMT_DEPTH BUFFERPARAMETER_FMT_DEPTH16 +#define BUFFERPARAMETER_FMT_STENCIL BUFFERPARAMETER_FMT_STENCIL8 + +// TODO: Merge Texture & RenderBuffer masks & getters/setters. They're basically +// identical, and we differentiate between them anyways wrt. dimensions, depth, +// stencil, and depth+stencil. + +// Buffer parameter: Frame buffer +// Buffer parameter: Texture buffer +#define BUFFERPARAMETER_TEXTURE_GET_DIMENSION(bufferparam) (BUFFERPARAMETER_GET_PARAMETER(bufferparam) & ((1 << 4) - 1)) +#define BUFFERPARAMETER_TEXTURE_1D 1 +#define BUFFERPARAMETER_TEXTURE_2D 2 +#define BUFFERPARAMETER_TEXTURE_3D 3 +#define BUFFERPARAMETER_TEXTURE_4D 4 + +// TODO: Change `2` once we add support for more formats +#define BUFFERPARAMETER_TEXTURE_GET_FORMAT(bufferparam) (BUFFERPARAMETER_GET_PARAMETER(bufferparam) & (((1 << 8) - 1) << 4)) + +// Buffer parameter: Buffer buffer +// A buffer can be either a depth and/or stencil buffer. Custom buffers could be +// implemented for this at some point. +// It is important that the types here are bitwise non-overlapping. +#define BUFFERPARAMETER_RENDERBUFFER_GET_TYPE(bufferparam) (BUFFERPARAMETER_GET_PARAMETER(bufferparam) & ((1 << 4) - 1)) +#define BUFFERPARAMETER_RENDERBUFFER_DEPTH 1 +#define BUFFERPARAMETER_RENDERBUFFER_STENCIL 2 + +#define BUFFERPARAMETER_RENDERBUFFER_GET_FORMAT(bufferparam) (BUFFERPARAMETER_GET_PARAMETER(bufferparam) & (((1 << 8) - 1)) << 4) + typedef struct { i32 num_attached_buffers; ivec3 dimensions; // All objects attached to a framebuffer (in OpenGL) must have same size @@ -276,7 +303,9 @@ typedef struct { //// Set to NULL to skip changing the camera size/perspective. void (**camera_reset_callback)(Camera*,ivec2); - // glTexture / glRenderBuffer / glBuffer / glWhatever + // glTexture / glRenderBuffer / glBuffer / glWhatever. + // Currently, each buffer must be either a texture or a (depth and/or stencil) + // buffer. usize buffer_len; u32 *buffer; // Stores per-buffer type, and type-specific parameters. @@ -334,6 +363,10 @@ void r_create_framebuffers(void* restrict ctx, u32* restrict framebuffer_array, void r_destroy_framebuffers(void* restrict ctx, u32* restrict framebuffer_array, usize num_targets); void r_create_textures(void *restrict ctx, u32* restrict texture_array, u32* restrict texture_types, ivec3* restrict texture_sizes, usize num_targets); void r_destroy_textures(void *restrict ctx, u32* textures, usize num_textures); +void r_create_renderbuffers(void *restrict ctx, u32* restrict buffer_array, u32* restrict buffer_types, ivec3* restrict buffer_sizes, usize num_buffers); +void r_destroy_renderbuffers(void *restrict ctx, u32* buffers, usize num_buffers); + +// Attaches `num_buffers` to the framebuffer. void r_attach_buffers(void *restrict ctx, u32 fbo, u32* buffers, u32* buffer_parameters, i32 num_buffers); void r_init_renderstack( -- cgit v1.3