|
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
|
#include "render_utils.h"#include "gl_common.h"#include "log.h"#include "utils.h"#include <ctype.h>#include <stddef.h>Enumerations | |
| enum | { GPU_IDENTIFIER_RAW_BUF_SIZE = 512 } |
Functions | |
| GLuint | render_utils_create_color_texture (float red, float green, float blue, float alpha) |
| Creates a 1x1 floating-point texture of a specific color. | |
| void | render_utils_bind_texture_safe (GLenum unit, GLuint texture, GLuint fallback_tex) |
| Safely binds a texture to a specific unit, using a fallback if needed. | |
| void | render_utils_reset_texture_units (int start_unit, int end_unit, GLuint fallback_tex) |
| Resets a range of texture units to a safe state. | |
| void | render_utils_create_empty_vao (GLuint *vao) |
| Creates an empty Vertex Array Object (VAO). | |
| void | render_utils_create_quad_vbo (GLuint *vbo) |
| Creates a standard centered Quad VBO. | |
| void | render_utils_create_wire_cube_vbo (GLuint *vbo) |
| Creates a wireframe Unit Cube VBO (GL_LINES). | |
| void | render_utils_create_wire_quad_vbo (GLuint *vbo) |
| Creates a wireframe Quad VBO (GL_LINE_LOOP). | |
| void | render_utils_create_fullscreen_quad (GLuint *vao, GLuint *vbo) |
| Creates a Full-Screen Quad VAO and VBO. | |
| int | render_utils_check_framebuffer (const char *label) |
| Checks the completeness of the currently bound framebuffer. | |
| GPUInfo | render_utils_get_gpu_info (void) |
| Retrieves currently active GPU hardware and driver information. | |
| static void | append_sanitized_char (char raw_char, char *buffer, size_t *dst_idx, size_t size) |
| void | render_utils_generate_gpu_identifier (const char *vendor, const char *renderer, char *buffer, size_t size) |
| Sanitizes GPU vendor and renderer strings into a filesystem-safe identifier. | |
| void | render_utils_get_gpu_identifier (char *buffer, size_t size) |
| Generates a filesystem-safe identifier for the current GPU. | |
| GLuint | render_utils_create_texture_2d (int width, int height, GLenum internal_format, GLint levels, const char *label) |
| Creates a 2D texture with specified parameters. | |
| void | render_utils_setup_sphere_instance_attributes (GLuint binding_point, GLsizei stride, size_t offset_albedo, size_t offset_metallic, size_t offset_prev_center) |
| Sets up instance attributes for sphere rendering (Model, Albedo, PBR). | |
| GLStateBackup | render_utils_save_state (void) |
| Saves current OpenGL state to a backup struct. | |
| void | render_utils_restore_state (const GLStateBackup *state) |
| Restores OpenGL state from a backup struct. | |
| void | render_utils_setup_ui_state (void) |
| Configures standard 2D/UI rendering state (Alpha blend, No depth). | |
|
static |
| void render_utils_bind_texture_safe | ( | GLenum | unit, |
| GLuint | texture, | ||
| GLuint | fallback_tex | ||
| ) |
Safely binds a texture to a specific unit, using a fallback if needed.
This function ensures that a valid texture is always bound to the specified unit. If texture is 0 (invalid), it binds fallback_tex instead. This is critical for robustness, preventing GL errors or black screens when resources fail to load or are temporarily unavailable.
| unit | The texture unit to bind to (e.g., GL_TEXTURE0). |
| texture | The primary texture to bind. |
| fallback_tex | The fallback texture to use if texture is 0. |
| int render_utils_check_framebuffer | ( | const char * | label | ) |
Checks the completeness of the currently bound framebuffer.
Logs an error message if the framebuffer is not complete.
| label | A string label to identify the framebuffer in logs. |
| GLuint render_utils_create_color_texture | ( | float | red, |
| float | green, | ||
| float | blue, | ||
| float | alpha | ||
| ) |
Creates a 1x1 floating-point texture of a specific color.
Useful for creating "dummy" textures (e.g., black, white, normal) to bind to shader slots when a real texture is not available. This prevents undefined behavior or warnings from validation layers on some drivers (like NVIDIA) which check that all active samplers have valid textures.
| red | Red component (0.0 - 1.0). |
| green | Green component (0.0 - 1.0). |
| blue | Blue component (0.0 - 1.0). |
| alpha | Alpha component (0.0 - 1.0). |
| void render_utils_create_empty_vao | ( | GLuint * | vao | ) |
Creates an empty Vertex Array Object (VAO).
Some OpenGL profiles (Core Profile) require a VAO to be bound for any draw call, even if the shader generates vertices without buffers (e.g., a full-screen triangle generated from gl_VertexID).
| [out] | vao | Pointer to store the generated VAO handle. |
| void render_utils_create_fullscreen_quad | ( | GLuint * | vao, |
| GLuint * | vbo | ||
| ) |
Creates a Full-Screen Quad VAO and VBO.
Generates a quad covering the entire Normalized Device Coordinates (NDC) space (-1.0 to 1.0). Used primarily for post-processing passes.
Includes:
| [out] | vao | Pointer to store the generated VAO handle. |
| [out] | vbo | Pointer to store the generated VBO handle. |
| void render_utils_create_quad_vbo | ( | GLuint * | vbo | ) |
Creates a standard centered Quad VBO.
Generates a VBO containing vertices for a quad centered at (0,0) with size 1x1 (-0.5 to 0.5). Useful for billboard rendering or simple sprites.
Format: 3 floats (x, y, z) per vertex. Vertex count: 6 (2 triangles).
| [out] | vbo | Pointer to store the generated VBO handle. |
| GLuint render_utils_create_texture_2d | ( | int | width, |
| int | height, | ||
| GLenum | internal_format, | ||
| GLint | levels, | ||
| const char * | label | ||
| ) |
Creates a 2D texture with specified parameters.
Encapsulates glGenTextures, glBindTexture, glTexStorage2D (or equivalent), and basic parameter setup (MIN/MAG filters, WRAP S/T).
| width | Texture width. |
| height | Texture height. |
| internal_format | Internal GPU format (e.g., GL_RGBA16F). |
| levels | Number of mipmap levels. |
| label | Debug label for the texture. |
| void render_utils_create_wire_cube_vbo | ( | GLuint * | vbo | ) |
Creates a wireframe Unit Cube VBO (GL_LINES).
Vertices range from -1.0 to 1.0. 12 edges * 2 vertices = 24 vertices.
| [out] | vbo | Pointer to store the generated VBO handle. |
| void render_utils_create_wire_quad_vbo | ( | GLuint * | vbo | ) |
Creates a wireframe Quad VBO (GL_LINE_LOOP).
Vertices are: (-0.5, 0.5), (0.5, 0.5), (0.5, -0.5), (-0.5, -0.5). 4 vertices.
| [out] | vbo | Pointer to store the generated VBO handle. |
| void render_utils_generate_gpu_identifier | ( | const char * | vendor, |
| const char * | renderer, | ||
| char * | buffer, | ||
| size_t | size | ||
| ) |
Sanitizes GPU vendor and renderer strings into a filesystem-safe identifier.
Lowercases alphanumeric characters and collapses multiple separators into a single underscore.
| vendor | The GPU vendor string. | |
| renderer | The GPU renderer string. | |
| [out] | buffer | Destination buffer for the identifier. |
| size | Size of the destination buffer. |
| void render_utils_get_gpu_identifier | ( | char * | buffer, |
| size_t | size | ||
| ) |
Generates a filesystem-safe identifier for the current GPU.
Sanitizes vendor and renderer strings (lowercase, alphanumeric, underscores).
| [out] | buffer | Destination buffer for the identifier. |
| size | Size of the destination buffer. |
| GPUInfo render_utils_get_gpu_info | ( | void | ) |
Retrieves currently active GPU hardware and driver information.
| void render_utils_reset_texture_units | ( | int | start_unit, |
| int | end_unit, | ||
| GLuint | fallback_tex | ||
| ) |
Resets a range of texture units to a safe state.
Binds the fallback_tex (usually a dummy black texture) to all texture units from start_unit to end_unit.
| start_unit | The starting texture unit index (0 for GL_TEXTURE0). |
| end_unit | The ending texture unit index (exclusive). |
| fallback_tex | The safe texture to bind. |
| void render_utils_restore_state | ( | const GLStateBackup * | state | ) |
Restores OpenGL state from a backup struct.
| state | Pointer to the state backup to restore. |
| GLStateBackup render_utils_save_state | ( | void | ) |
Saves current OpenGL state to a backup struct.
| void render_utils_setup_sphere_instance_attributes | ( | GLuint | binding_point, |
| GLsizei | stride, | ||
| size_t | offset_albedo, | ||
| size_t | offset_metallic, | ||
| size_t | offset_prev_center | ||
| ) |
Sets up instance attributes for sphere rendering (Model, Albedo, PBR).
Configures vertex attribute pointers for instanced arrays. Assumes the instance VBO is already bound.
| stride | Stride of the instance structure (sizeof(SphereInstance)). |
| offset_albedo | Byte offset of the albedo field. |
| offset_metallic | Byte offset of the metallic field (start of PBR block). |
| offset_prev_center | Byte offset of the prev_center field (motion blur). |
| void render_utils_setup_ui_state | ( | void | ) |
Configures standard 2D/UI rendering state (Alpha blend, No depth).