thatcosmonaut / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
1 stars 2 forks source link

Improve clarity of TextureCreateInfo #67

Open TheSpydog opened 5 days ago

TheSpydog commented 5 days ago

While the current SDL_GpuTextureCreateInfo struct design gets the job done, I think it could be improved for usability and clarity. What we have currently is this:

typedef struct SDL_GpuTextureCreateInfo
{
    Uint32 width;
    Uint32 height;
    Uint32 depth;
    SDL_bool isCube;
    Uint32 layerCount;
    Uint32 levelCount;
    SDL_GpuSampleCount sampleCount;
    SDL_GpuTextureFormat format;
    SDL_GpuTextureUsageFlags usageFlags;
} SDL_GpuTextureCreateInfo;

This is a collection of texture properties that covers all possible scenarios (depth, 2D, arrays, etc.), but it's not clear which properties are compatible with one another, or what the "default" values should be. It raises questions like:

Having this many individual, incompatible settings all presented in one grab-bag is clearly less than ideal. A potentially better option would be to follow the D3D11_VIEW_DESC approach, where we have a tagged union of texture types that only contain the options actually applicable for that type. Something like:

typedef struct SDL_GpuTextureCreateInfo
{
    SDL_GpuTextureType type; // 2D, 2D_MULTISAMPLE, 2D_ARRAY, 3D, CUBE
    SDL_GpuTextureFormat format;
    SDL_GpuTextureUsageFlags usageFlags;
    union
    {
        struct Texture2D
        {
            Uint32 width;
            Uint32 height;
            Uint32 levelCount;
        };
        struct Texture2DMultisample
        {
            Uint32 width;
            Uint32 height;
            Uint32 sampleCount;
        };
        struct Texture2DArray
        {
            Uint32 width;
            Uint32 height;
            Uint32 levelCount;
            Uint32 layerCount; // could also rename to arraySize or something for clarity?
        };
        struct Texture3D
        {
            Uint32 width;
            Uint32 height;
            Uint32 depth;
            Uint32 levelCount;
        };
        struct TextureCube
        {
            Uint32 size;
            Uint32 levelCount;
        };
    };
} SDL_GpuTextureCreateInfo;

This isn't 100% perfect since you can still have incompatible type/format/usage pairings, but I think it substantially lightens the mental load of having to remember all the rules about which properties work with which types.