$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
texture.c File Reference
#include "texture.h"
#include "gl_common.h"
#include "io.h"
#include "log.h"
#include "profiler.h"
#include "utils.h"
#include <math.h>
#include <stb_image.h>
#include <stddef.h>
#include <stdio.h>
Include dependency graph for texture.c:

Enumerations

enum  { MAX_TEXTURE_FILE_SIZE = 64 * 1024 * 1024 }
 

Functions

float * texture_load_pixels (const char *path, int *width, int *height, int *channels)
 Decodes an HDR image into RAM without uploading to GPU. More...
 
void texture_ensure_pbo (GLuint *pbo_id, GLsizeiptr *current_size, GLsizeiptr required_size)
 Ensures a PBO is allocated with sufficient size. More...
 
void * texture_map_pbo (GLuint pbo_id, size_t size_bytes)
 Maps a PBO for writing. More...
 
static bool texture_matches_hdr (int width, int height)
 Check if an existing texture matches the expected HDR format. More...
 
GLuint texture_preallocate_hdr (int width, int height, GLuint old_tex)
 Pre-allocates VRAM for an HDR texture (glTexStorage2D only). More...
 
static GLuint texture_reuse_or_create_hdr (int width, int height, GLuint reuse_tex_id, bool *is_reused, int *levels_out)
 
static bool texture_allocate_storage_hdr (int width, int height, int levels)
 
GLuint texture_upload_hdr_from_pbo (GLuint pbo_id, void *ptr, int width, int height, GLuint reuse_tex_id)
 Finalizes HDR upload from a PBO (Unmap -> Upload -> Mipmaps). More...
 
void texture_generate_hdr_mipmap (GLuint tex)
 Generates mipmaps for an HDR texture. More...
 
GLuint texture_load_rgba_png (const char *path)
 Loads a PNG image from disk and creates an OpenGL RGBA texture. More...
 

Variables

static const uint32_t TRACY_COLOR_TEXTURE_UPLOAD = 0xAAAA55
 
static const uint32_t TRACY_COLOR_TEXTURE_STORAGE = 0xAA55AA
 
static const uint32_t TRACY_COLOR_MIPMAP_GEN = 0x55AAAA
 
static const uint32_t TRACY_COLOR_TEXTURE_UPLOAD_FULL = 0xFF8800
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
MAX_TEXTURE_FILE_SIZE 

Function Documentation

◆ texture_allocate_storage_hdr()

static bool texture_allocate_storage_hdr ( int  width,
int  height,
int  levels 
)
static

◆ texture_ensure_pbo()

void texture_ensure_pbo ( GLuint *  pbo_id,
GLsizeiptr *  current_size,
GLsizeiptr  required_size 
)

Ensures a PBO is allocated with sufficient size.

Checks if internal *current_size is >= required_size. If not, reallocates (orphans) the buffer using glBufferData.

Parameters
pbo_idPointer to the PBO ID (generated if 0).
current_sizePointer to the currently tracking size (updated on realloc).
required_sizeThe minimum size needed.

◆ texture_generate_hdr_mipmap()

void texture_generate_hdr_mipmap ( GLuint  tex)

Generates mipmaps for an HDR texture.

Parameters
texThe OpenGL ID of the texture.

◆ texture_load_pixels()

float* texture_load_pixels ( const char *  path,
int *  width,
int *  height,
int *  channels 
)

Decodes an HDR image into RAM without uploading to GPU.

Useful for asynchronous loading or CPU-side processing.

Parameters
pathFile system path.
[out]widthWidth.
[out]heightHeight.
[out]channelsNumber of color channels.
Returns
Pointer to heap-allocated data. Buffer must be freed by caller.
Here is the call graph for this function:

◆ texture_load_rgba_png()

GLuint texture_load_rgba_png ( const char *  path)

Loads a PNG image from disk and creates an OpenGL RGBA texture.

Parameters
pathFile system path to the PNG image.
Returns
GLuint The OpenGL texture ID, or 0 on failure.

◆ texture_map_pbo()

void* texture_map_pbo ( GLuint  pbo_id,
size_t  size_bytes 
)

Maps a PBO for writing.

Parameters
pbo_idID of the PBO to map.
size_bytesSize to map (should match creation size).
Returns
Pointer to mapped memory, or NULL on failure.

◆ texture_matches_hdr()

static bool texture_matches_hdr ( int  width,
int  height 
)
static

Check if an existing texture matches the expected HDR format.

Queries GL state for the texture's level-0 dimensions and internal format. The texture must already be bound to GL_TEXTURE_2D before calling.

Returns
true if width, height and format (GL_RGBA16F) all match.

◆ texture_preallocate_hdr()

GLuint texture_preallocate_hdr ( int  width,
int  height,
GLuint  old_tex 
)

Pre-allocates VRAM for an HDR texture (glTexStorage2D only).

Call this early (e.g. when async loader requests a PBO) to spread the cost of texture allocation across frames. If old_tex already matches the requested dimensions, it is returned as-is (zero-cost reuse).

Parameters
widthTexture width.
heightTexture height.
old_texExisting texture ID to check for reuse (0 if none).
Returns
GLuint Pre-allocated texture ID, or 0 on failure.
Here is the call graph for this function:

◆ texture_reuse_or_create_hdr()

static GLuint texture_reuse_or_create_hdr ( int  width,
int  height,
GLuint  reuse_tex_id,
bool *  is_reused,
int *  levels_out 
)
static
Here is the call graph for this function:

◆ texture_upload_hdr_from_pbo()

GLuint texture_upload_hdr_from_pbo ( GLuint  pbo_id,
void *  ptr,
int  width,
int  height,
GLuint  reuse_tex_id 
)

Finalizes HDR upload from a PBO (Unmap -> Upload -> Mipmaps).

Parameters
pbo_idID of the PBO (must be bound).
ptrPointer to the mapped PBO memory (will be unmapped).
widthTexture width.
heightTexture height.
reuse_tex_idExisting texture ID to update/reuse.
Returns
GLuint The texture ID (new or reused).
Here is the call graph for this function:

Variable Documentation

◆ TRACY_COLOR_MIPMAP_GEN

const uint32_t TRACY_COLOR_MIPMAP_GEN = 0x55AAAA
static

◆ TRACY_COLOR_TEXTURE_STORAGE

const uint32_t TRACY_COLOR_TEXTURE_STORAGE = 0xAA55AA
static

◆ TRACY_COLOR_TEXTURE_UPLOAD

const uint32_t TRACY_COLOR_TEXTURE_UPLOAD = 0xAAAA55
static

◆ TRACY_COLOR_TEXTURE_UPLOAD_FULL

const uint32_t TRACY_COLOR_TEXTURE_UPLOAD_FULL = 0xFF8800
static