kgl icon indicating copy to clipboard operation
kgl copied to clipboard

Allow different implementations of TextureResource

Open wakaztahir opened this issue 8 months ago • 1 comments

Use Case

Android has GL calls to allow providing a Bitmap as texture data, Currently you need to copy the bitmap to a buffer before you can pass it to Kgl

  • When you load an image in android, You usually load a Bitmap for it
  • If you do load an image as buffer, You also need to provide a width and height, easy solution becomes loading a bitmap since it contains width and height, If you need to be memory efficient, You'll load the bitmap inJustDecodeBounds to get width and height and then load the bitmap as buffer which is a bit complex
  • Bitmap doesn't provide access to a Buffer object, It does but that's HardwareBuffer which doesn't extend Java Buffer
  • The constructor for Buffer that takes a Java Buffer is internal (even if it were public, there's no buffer)
  • When you load a bitmap, You must copy over the pixels from bitmap to a Buffer which is more work & not the best way !

Solution : Introduce TextureAsset interface, Make TextureResource implementation of TextureAsset

TextureResource can be a deprecated typealias for BufferTextureAsset (Later)

  • [x] Split JVM to android & desktop target
  • [x] Keep JVM TextureResource same in desktop, Keep other types same in both android and desktop
  • [x] Create TextureAsset interface in commonMain & make TextureResource implement TextureAsset
  • [x] TextureBitmapAsset can be implemented on Android
  • [x] TextureAsset basically has methods that are called by Kgl itself to make gl calls & its given a Kgl instance with that call and parameters (There is no other way)

The only CON with this solution is that anyone can implement TextureAsset, in my opinion that is OK since

I've implemented it in the linked pull request, except for the iOS platform since I work on windows !

wakaztahir avatar Oct 11 '23 10:10 wakaztahir