StarlingGraphics / Starling-Extension-Graphics

flash.display.Graphics style extension for the Starling Flash GPU rendering framework
https://github.com/StarlingGraphics/Starling-Extension-Graphics/wiki
MIT License
282 stars 89 forks source link

Texture restoration after context loss #141

Closed fraylopez closed 8 years ago

fraylopez commented 8 years ago

Hi,

There are several issues about this. Just in case, make sure your are not using the .swc package which is OUTDATED.

greglindquist commented 8 years ago

I second this issue. Losing context seems to clear out all the graphics for me.

greglindquist commented 8 years ago

@fraylopez - I was actually able to resolve my issue. The logic is somewhat different in the latest update for the Straling Extension graphics package (Mine was up to date as of April or so). The issue comes down to how you restore the graphics - and in my case, a flush was not getting called which prevented the Program3DCache logic from uploading the graphics to the gpu again..

https://github.com/StarlingGraphics/Starling-Extension-Graphics/commit/69abe62b9b9d0b3efc1ce2ffe73573abae51d928

In my version, the 'releaseProgram3D method was never really calling flush, which never removed the program3D from cache, and therefore was never calling the renderer to upload to the gpu after it lost context.

Here's my updated version:

    public static function releaseProgram3D( program3D:Program3D ):void
    {
        if ( !numReferencesByProgramTable[program3D] )
        {
            throw( new Error( "Program3D is not in cache" ) );
            return;
        }

        var numReferences:int = numReferencesByProgramTable[program3D];         
        numReferences--;
        numReferencesByProgramTable[program3D] = numReferences; 

        if(numReferences == 0){             
            flush();
        }
    }

Again, the new version has this - but the new version is also relying on the new version of starling - which I haven't had time to update to. You may need to trace some code out, but my guess is that this block of code is never getting called after losing context and therefore is never updating the gpu with restored graphics. My issue is that the code was never getting back into the program3D to restore the graphics again.

        if ( program3D == null )
        {
            program3D = programByUIDTable[program3DUID] = context.createProgram();
            uidByProgramTable[program3D] = program3DUID;
            program3D.upload( vertexShader.opCode, fragmentShader.opCode );
            numReferencesByProgramTable[program3D] = 0;
            cacheSize++;
        }
IonSwitz commented 8 years ago

A fix for this has been submitted