Genome2D-ContextFlash icon indicating copy to clipboard operation
Genome2D-ContextFlash copied to clipboard

GBlendModeFunc allocation issue (Flash)

Open DigiEggz opened this issue 5 years ago • 3 comments

I recently updated from an old version of the Genome2D SWC and started memory profiling because I was getting regular garbage collection events that cause application stutter. I found that the Haxe function Type.enumParameters is frequently called, mentioned in this request: https://github.com/pshtif/Genome2D-ContextCommon/pull/1

The issue is being caused by GBlendModeFunc.setBlendMode(), from IGContext.begin(). It seems like any time the blendFactors array is referenced, it generates a new entity and quickly consumes memory. Is there a way to avoid this?

Here's the hierarchy and allocations in my app after about 5 minutes of runtime: Screen Shot 2020-07-13 at 8 40 34 PM

DigiEggz avatar Jul 14 '20 00:07 DigiEggz

Could we use a switch in a scenario like this? The following code works and fixes the allocation issue:

import flash.display3D.Context3D;
import flash.display3D.Context3DBlendFactor;

class GBlendModeFunc
{   
    private static var sourceFactor:Context3DBlendFactor;
    private static var destinationFactor:Context3DBlendFactor;
    
	static public function setBlendMode(p_context:Context3D, p_mode:GBlendMode, p_premultiplied:Bool):Void {

        switch (p_mode) {
            case NONE:
                sourceFactor = Context3DBlendFactor.ONE;
                destinationFactor = Context3DBlendFactor.ZERO;
            case NORMAL:
                sourceFactor = (p_premultiplied) ? Context3DBlendFactor.ONE : Context3DBlendFactor.SOURCE_ALPHA;
                destinationFactor = (p_premultiplied) ? Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA : Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA;
            case ADD:
                sourceFactor = (p_premultiplied) ? Context3DBlendFactor.ONE : Context3DBlendFactor.SOURCE_ALPHA;
                destinationFactor = (p_premultiplied) ? Context3DBlendFactor.ONE : Context3DBlendFactor.DESTINATION_ALPHA;
            case MULTIPLY:
                sourceFactor = Context3DBlendFactor.DESTINATION_COLOR;
                destinationFactor = Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA;
            case SCREEN:
                sourceFactor = (p_premultiplied) ? Context3DBlendFactor.ONE : Context3DBlendFactor.SOURCE_ALPHA;
                destinationFactor = (p_premultiplied) ? Context3DBlendFactor.ONE_MINUS_SOURCE_COLOR : Context3DBlendFactor.ONE;
            case ERASE:
                sourceFactor = Context3DBlendFactor.ZERO;
                destinationFactor = Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA;
        }
        
         p_context.setBlendFactors(sourceFactor, destinationFactor);
        
	}
}

DigiEggz avatar Jul 14 '20 20:07 DigiEggz

This is definitely doable however it renders dynamic addition of custom blendmodes impossible. However not sure if that is a big issue as I am not aware of anyone using custom blendmodes in the years I worked or consulted on many Genome2D projects/games.

pshtif avatar Jul 14 '20 20:07 pshtif

What's the current method for adding a blendmode? The default switch statement could search an array or typed vector (if not null) based on a custom blendmode passed in during init. For now, I'll just take compiling an SWC with a custom blendmode if I ever need one.

DigiEggz avatar Jul 14 '20 22:07 DigiEggz