Skip to content

Conversation

@wlewNV
Copy link
Contributor

@wlewNV wlewNV commented Jan 15, 2026

Fixes #599

Adds a configurable callback mechanism for handling bitmap formats that don't have a direct mapping in the standard FORMAT_TABLE (e.g., YA/greyscale+alpha bitmaps).

When loading YA (greyscale+alpha) bitmaps, there are two options:

  1. Expand to RGBA (wastes memory but shader code is consistent)
  2. Keep as RG (efficient but shader must interpret R=luminance, G=alpha)

Previously, slangpy always expanded YA to RGBA with no way to customize.

This change adds an optional format_callback to TextureLoader.Options that is invoked when the standard format table doesn't have a mapping. Users can return a FormatOverride to specify the desired format, or None to use the default behavior.

Before

# YA bitmap always expands to RGBA (4 channels, wasteful)
loader = TextureLoader(device)
texture = loader.load_texture(ya_bitmap)

After:

def ya_to_rg(device, bitmap):
    if bitmap.pixel_format == PixelFormat.ya:
        return FormatOverride(format=Format.rg8_unorm, convert_to=None)
    return None  # Default for other formats

loader = TextureLoader(device)
options = TextureLoader.Options()
options.format_callback = ya_to_rg
texture = loader.load_texture(ya_bitmap, options)

@wlewNV wlewNV requested a review from a team as a code owner January 15, 2026 23:25
@ccummingsNV
Copy link
Contributor

A callback to Python feels like overkill for this problem, and doesn't help the native case. I feel like this should just be an additional option to the texture loader. @skallweitNV what's your take?

Copy link
Contributor

@skallweitNV skallweitNV left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before anything, I think there is a big issue here as the callback can be called from multiple loader worker threads. You'd have to handle GIL when calling back into python.

While the callback feels like a very generic solution it might not be the right call. The other issue I see if users do not specify the convert_to correctly.

I think this needs more thought. Maybe @tdavidovicNV can chime in as well, as the initial request comes from him.

@wlewNV wlewNV marked this pull request as draft January 16, 2026 18:40
@wlewNV
Copy link
Contributor Author

wlewNV commented Jan 27, 2026

From the discussion in https://discord.com/channels/1303735196696445038/1463292987931885598,

I've replaced the target_format approach with a simpler YAHandling enum strategy since that approach was redundant (users can already do the conversion manually) and it wouldn't be comprehensive (skipping component type conversions, compressed format conversion and depth/stencil format conversion).

For the time being, a simple solution using the enum should suffice because it covers the two common YA use cases (expand to RGBA or preserve as RG), and users who need more granular control can already manually convert the Bitmap before loading.

@wlewNV wlewNV marked this pull request as ready for review January 27, 2026 16:05
@wlewNV wlewNV requested a review from skallweitNV February 2, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generalize the Bitmap->Texture conversion logic

4 participants