Elaztek Developer Hub
Blamite Game Engine - blam!  00453.06.08.26.0624.blamite
The core library for the Blamite Game Engine.
bitmaps.cpp File Reference
+ Include dependency graph for bitmaps.cpp:

Classes

struct  bgfx_texture_info_data
 Data structure used to group together information about a texture. More...
 

Functions

bgfx::TextureFormat::Enum determine_gpu_pixel_format (bitmap::format_enum tag_format)
 Converts a bitmap pixel format enum value to the equivalent bgfx pixel format. More...
 
bgfx_texture_info_datacreate_default_color_texture (uint32_t color)
 
uint32_t compute_sampler_flags_from_bitmap (bitmap *bitmap_tag)
 Determines sampler flags from a bitmap tag. More...
 
float srgb_to_linear (float srgb_value)
 Converts a single sRGB-encoded color channel value to linear space. More...
 
float linear_to_srgb (float linear_value)
 Converts a single linear-space color channel value to sRGB encoding. More...
 
void downsample_mip_2x2 (const uint8_t *source, int source_width, int source_height, uint8_t *destination, int destination_width, int destination_height, bool treat_as_srgb)
 Generates the next mip level for an RGBA8 texture by averaging 2x2 pixel blocks. More...
 
int compute_mip_count (int width, int height)
 Computes the number of mip levels for a 2D texture of the given dimensions. More...
 
int compute_mip_chain_size_rgba8 (int base_width, int base_height)
 Computes the total byte count required to hold a complete mip chain for an RGBA8 texture of the given base dimensions. More...
 
const bgfx::Memorybuild_mip_chain_rgba8 (const void *base_pixel_data, int base_width, int base_height, bool treat_as_srgb)
 Builds the complete mip chain for an RGBA8 texture and packages it as a bgfx::Memory pointer ready to pass to bgfx::createTexture2D. More...
 

Variables

bgfx::TextureHandle default_texture = BGFX_INVALID_HANDLE
 
bgfx::TextureHandle fallback_white = BGFX_INVALID_HANDLE
 
bgfx::TextureHandle fallback_normal = BGFX_INVALID_HANDLE
 
bgfx::TextureHandle fallback_metal_rough = BGFX_INVALID_HANDLE
 
bgfx::TextureHandle fallback_gray_50 = BGFX_INVALID_HANDLE
 
std::unordered_map< std::string, bgfx_texture_info_data * > bgfx_texture_map = std::unordered_map<std::string, bgfx_texture_info_data*>()
 Map containing information on all loaded textures. More...
 

Function Documentation

◆ build_mip_chain_rgba8()

const bgfx::Memory* build_mip_chain_rgba8 ( const void *  base_pixel_data,
int  base_width,
int  base_height,
bool  treat_as_srgb 
)

Builds the complete mip chain for an RGBA8 texture and packages it as a bgfx::Memory pointer ready to pass to bgfx::createTexture2D.

Allocates a single bgfx-managed buffer sized for the entire chain, copies the provided base data into the mip 0 region, then iteratively downsamples each subsequent mip from the previous one using a 2x2 box filter. The output is packed in the layout bgfx expects (mip 0 first, followed by each subsequent mip contiguously).

Parameters
base_pixel_data- Pointer to the source mip 0 data (RGBA8, tightly packed).
base_width- Width of the base texture in pixels.
base_height- Height of the base texture in pixels.
treat_as_srgb- Whether downsampling should be gamma-correct for color textures.
Returns
A bgfx::Memory* containing the full mip chain. Memory is bgfx-managed, so the caller does not need to free it - just pass it to createTexture2D.
Author
Claude (Opus 4.7)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compute_mip_chain_size_rgba8()

int compute_mip_chain_size_rgba8 ( int  base_width,
int  base_height 
)

Computes the total byte count required to hold a complete mip chain for an RGBA8 texture of the given base dimensions.

The mip chain follows the standard "halve each dimension, minimum 1" rule and terminates when both dimensions reach 1. Each mip's contribution is its width * height * 4 bytes.

Parameters
base_width- Width of mip 0 in pixels.
base_height- Height of mip 0 in pixels.
Returns
The sum of all mip levels' byte sizes.
Author
Claude (Opus 4.7)
+ Here is the caller graph for this function:

◆ compute_mip_count()

int compute_mip_count ( int  width,
int  height 
)

Computes the number of mip levels for a 2D texture of the given dimensions.

The mip count is determined by the largest dimension, halved repeatedly until it reaches 1. The base level (mip 0) is always counted, so this returns at least 1.

Parameters
width- Width of the base texture in pixels.
height- Height of the base texture in pixels.
Returns
The number of mip levels in a complete chain from the base down to 1x1.
Author
Claude (Opus 4.7)

◆ compute_sampler_flags_from_bitmap()

uint32_t compute_sampler_flags_from_bitmap ( bitmap bitmap_tag)

Determines sampler flags from a bitmap tag.

This will determine the sampler flags for this bitmap based on the filtering modes for this bitmap tag.

Parameters
bitmap_tag- The bitmap tag to determine sampler flags for.
Returns
The sampler flags based on this bitmap tag. Will return 0 if the tag was nullptr.
Author
Claude (Opus 4.7)
+ Here is the caller graph for this function:

◆ create_default_color_texture()

bgfx_texture_info_data* create_default_color_texture ( uint32_t  color)
+ Here is the caller graph for this function:

◆ determine_gpu_pixel_format()

bgfx::TextureFormat::Enum determine_gpu_pixel_format ( bitmap::format_enum  tag_format)

Converts a bitmap pixel format enum value to the equivalent bgfx pixel format.

Parameters
tag_format- The bitmap pixel format value to convert.
Returns
The equivalent bgfx pixel format. If no matching format is found, then bgfx::TextureFormat::Unknown is returned.
+ Here is the caller graph for this function:

◆ downsample_mip_2x2()

void downsample_mip_2x2 ( const uint8_t source,
int  source_width,
int  source_height,
uint8_t destination,
int  destination_width,
int  destination_height,
bool  treat_as_srgb 
)

Generates the next mip level for an RGBA8 texture by averaging 2x2 pixel blocks.

When sRGB correctness is requested, the color channels (RGB) are converted to linear space before averaging and converted back to sRGB afterward. This avoids the brightness-shift artifact that occurs when sRGB-encoded values are averaged directly. Alpha is always averaged in raw bytes since it isn't gamma encoded. Source data is sampled with edge-clamp behavior so non-power-of-two dimensions are handled correctly.

Parameters
source- Pointer to the source mip's pixel buffer (RGBA8, tightly packed).
source_width- Width in pixels of the source buffer.
source_height- Height in pixels of the source buffer.
destination- Pointer to the destination buffer to write the new mip into.
destination_width- Width in pixels of the destination buffer.
destination_height- Height in pixels of the destination buffer.
treat_as_srgb- If true, RGB channels go through gamma-correct averaging.
Author
Claude (Opus 4.7)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ linear_to_srgb()

float linear_to_srgb ( float  linear_value)

Converts a single linear-space color channel value to sRGB encoding.

The inverse of srgb_to_linear. Used after averaging color values in linear space to produce a result that's correctly gamma-encoded for storage in an sRGB texture.

Parameters
linear_value- The linear-space value, normalized to the [0, 1] range.
Returns
The equivalent sRGB-encoded value, also in the [0, 1] range.
Author
Claude (Opus 4.7)
+ Here is the caller graph for this function:

◆ srgb_to_linear()

float srgb_to_linear ( float  srgb_value)

Converts a single sRGB-encoded color channel value to linear space.

Uses the proper piecewise transform from the sRGB specification rather than the gamma-2.2 approximation. The piecewise version handles values near black more accurately - the gamma curve goes flat in that region and a pure power function would produce slightly wrong results.

Parameters
srgb_value- The sRGB-encoded value, normalized to the [0, 1] range.
Returns
The equivalent value in linear color space, also in the [0, 1] range.
Author
Claude (Opus 4.7)
+ Here is the caller graph for this function:

Variable Documentation

◆ bgfx_texture_map

std::unordered_map<std::string, bgfx_texture_info_data*> bgfx_texture_map = std::unordered_map<std::string, bgfx_texture_info_data*>()

Map containing information on all loaded textures.

Each key contains the tag path of the texture's bitmap tag, and each value stores the texture information structure.

◆ default_texture

bgfx::TextureHandle default_texture = BGFX_INVALID_HANDLE

◆ fallback_gray_50

bgfx::TextureHandle fallback_gray_50 = BGFX_INVALID_HANDLE

◆ fallback_metal_rough

bgfx::TextureHandle fallback_metal_rough = BGFX_INVALID_HANDLE

◆ fallback_normal

bgfx::TextureHandle fallback_normal = BGFX_INVALID_HANDLE

◆ fallback_white

bgfx::TextureHandle fallback_white = BGFX_INVALID_HANDLE