Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GBlendModeFunc allocation issue (Flash) #17

Open
DigiEggz opened this issue Jul 14, 2020 · 3 comments
Open

GBlendModeFunc allocation issue (Flash) #17

DigiEggz opened this issue Jul 14, 2020 · 3 comments

Comments

@DigiEggz
Copy link

DigiEggz commented Jul 14, 2020

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: pshtif/Genome2D-ContextCommon#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
Copy link
Author

DigiEggz commented Jul 14, 2020

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);
        
	}
}

@pshtif
Copy link
Owner

pshtif commented Jul 14, 2020

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.

@DigiEggz
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants