SDL icon indicating copy to clipboard operation
SDL copied to clipboard

Emscripten_UpdateWindowFramebuffer fails if pixel data is higher than 2gb in address space

Open sbc100 opened this issue 1 year ago • 1 comments

Specifically the line var src = pixels >> 2 needs updating.

It should probably be:

#if __wasm64__
   var src = pixels  / 4;
#else
   var src = pixels >>> 0; 
#endif 

(I can work look into uploading a PR for this)

sbc100 avatar Feb 12 '24 20:02 sbc100

Yes, please send a PR when you get a chance; this feels like something I'll get wrong if I try to do it myself. :)

icculus avatar Feb 15 '24 15:02 icculus

https://github.com/libsdl-org/SDL/blob/cd197be53b05f952900d9ff900770b3afd52ef4a/src/video/emscripten/SDL_emscriptenframebuffer.c#L92

Trying to understand the issuepixels is a void* (I guess of size either 4 or 8 depending on it being a wasm64) and HEAP32 sees the memory like a sequence of 32-bit int. The SDL_Surface->pixels is allocated using SDL_SIMDAlloc which takes the size in bytes.

>>> 0 is a trick to cast to 32-bit int, it's a weird change assuming the current >> 2 is working (this is the same as / 4 here?)

ericoporto avatar Mar 09 '24 18:03 ericoporto

Sorry I meant to write >>> 2. The triple right shift is an unsigned shift (which is want) and the double right shift is a signed shift (which don't work for 32-bit addresses in the upper half of the range).

sbc100 avatar Mar 11 '24 04:03 sbc100

If this is not in performance critical code you could just use a divide by 4 operation in all cases and keep the code simple.

sbc100 avatar Mar 11 '24 04:03 sbc100

Just coming back to this, do we just need that one var src = pixels >> 2; line fixed? I'll put that in revision control today if so!

icculus avatar May 22 '24 17:05 icculus

Any place >> or >>> is used on a pointer value would need to be fixed. I don't know if var src = pixels >> 2 is the only one.

sbc100 avatar May 22 '24 17:05 sbc100