Skip to content
This repository was archived by the owner on Feb 14, 2022. It is now read-only.
This repository was archived by the owner on Feb 14, 2022. It is now read-only.

TextImage methods only supports ReadOnlySpan<byte> #11

@danielcaceresm

Description

@danielcaceresm

The provided methods to set image data only supports ReadOnlySpan as parameter.
Would be nice to implements more overloads or a generic method.

At this moment in WaveEngine we have implemented a converter to use the overload that receive a ITypedArray, but will be nice that this conversion is done by the WebGL.Backend and we work with ReadOnlySpan<byte|sbyte|...>.

This is the code of our converter:

    private static unsafe ITypedArray GetData(uint pixelType, void* data, uint sourceSizeInBytes)
    {
        // https://www.khronos.org/registry/webgl/specs/latest/2.0/ under 'void texImage2D' section
        WebAssembly.Core.ITypedArray array;
        switch (pixelType)
        {
            case GLConstants.ColorPointerTypeByte:
                array = WebAssembly.Core.Int8Array.From(new ReadOnlySpan<sbyte>(data, (int)sourceSizeInBytes));
                break;
            case GLConstants.ColorPointerTypeUnsignedByte:
                array = WebAssembly.Core.Uint8Array.From(new ReadOnlySpan<byte>(data, (int)sourceSizeInBytes));
                break;
            case GLConstants.ColorPointerTypeShort:
                array = WebAssembly.Core.Int16Array.From(new ReadOnlySpan<short>(data, (int)sourceSizeInBytes));
                break;
            case GLConstants.ColorPointerTypeUnsignedShort:
            case GLConstants.ExtensionsUnsignedShort565:
            case GLConstants.PixelTypeUnsignedShort5551:
            case GLConstants.PixelTypeUnsignedShort4444:
                array = WebAssembly.Core.Uint16Array.From(new ReadOnlySpan<ushort>(data, (int)sourceSizeInBytes));
                break;
            case GLConstants.ColorPointerTypeInt:
                array = WebAssembly.Core.Int32Array.From(new ReadOnlySpan<int>(data, (int)sourceSizeInBytes));
                break;
            case GLConstants.ColorPointerTypeUnsignedInt:
            case GLConstants.ExtensionsUnsignedInt5999Rev:
            case GLConstants.VertexAttribTypeUnsignedInt2101010Rev:
            case GLConstants.VertexAttribPointerTypeUnsignedInt10f11f11fRev:
            case GLConstants.ExtensionsUnsignedInt248:
                array = WebAssembly.Core.Uint32Array.From(new ReadOnlySpan<uint>(data, (int)sourceSizeInBytes));
                break;
            case GLConstants.VertexAttribPointerTypeHalfFloat:
                array = WebAssembly.Core.Uint16Array.From(new ReadOnlySpan<ushort>(data, (int)sourceSizeInBytes));
                break;
            case GLConstants.VertexAttribPointerTypeFloat:
                array = WebAssembly.Core.Float32Array.From(new ReadOnlySpan<float>(data, (int)sourceSizeInBytes));
                break;
            default:
                array = WebAssembly.Core.Uint8Array.From(new ReadOnlySpan<byte>(data, (int)sourceSizeInBytes));
                break;
        }
        return array;
    }

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions