Best way to clear a buffer for resource textures fast

Looping through the buffer and setting to a clear color to each value is too slow.

If I try to remake the buffer every frame it crashes on HTML5 build

Uncaught abort("Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 268435456, (2) compile with -s ALLOW_MEMORY_GROWTH=1 which adjusts the size at runtime but prevents some optimizations, (3) set Module.TOTAL_MEMORY to a higher value before the program runs, or if you want malloc to return NULL (0) instead of this abort, compile with -s ABORTING_MALLOC=0 ") at Error

Doing

	surface.buffer = buffer.create(surface.width * surface.height, { {name=hash("rgba"), type=buffer.VALUE_TYPE_UINT8, count=4} } )
	surface.stream = buffer.get_stream(surface.buffer, hash("rgba"))
	resource.set_texture(surface.resource_path,surface.header,surface.buffer)

Also gives inconsistent results desktop vs HTML5. Desktop clears the resource texture, HTML5 doesn’t and does some weird stuff.

Is there a better way? I think maybe having two buffers and copy the blank buffer onto the primary buffer might solve memory issue on HTML5?

Here’s the project it’s for if anyone wants to tinker

Getting clear to work and be fast is crucial for this project to be viable.

Copying a blank buffer seems to work for HTML5 build. Have to manually clear the buffers for HTML5 build though on start as it doesn’t clear them properly when used with resources like desktop does.

The more pixels modified a frame the slower it is no matter what. How can that be made faster? As long as not too much is drawn it’s fast but the more being modified it gets very slow.

Well, setting each pixel one by one via Lua is never going to be fast. Each call from Lua to C has an overhead cost. If you want it as fast as possible, you’ll have to do it inside the native extension. Essentially, you want to set as many pixels at once in once call E.g. a clear function, or a copy function.

2 Likes

Each bufferstream call from Lua has an overhead too so it would be 4x for each RGBA?

Is there a way I can build a bufferstream chunk and set it all at once without an extra native extension?

Not today, no

I’d guess that a single Lua to C call with a bufferstream batch would be faster than dozens of calls for drawing individual shapes so I’ll test making a bufferstream batch native extension with cls and batching of streams all at once. If that’s still too slow there are other ideas I can test.