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

Instantiating more than 4 WebGLRenderers causes black textures #1238

Closed
erd0s opened this issue Dec 2, 2014 · 13 comments
Closed

Instantiating more than 4 WebGLRenderers causes black textures #1238

erd0s opened this issue Dec 2, 2014 · 13 comments
Assignees
Labels
🕷 Bug Verified that it’s actually a legit bug that exists in the current release.
Milestone

Comments

@erd0s
Copy link

erd0s commented Dec 2, 2014

If I instantiate and destroy more than 4 WebGLRenderers on a single page I get a black canvas, this behavior is consistent across chrome/ff/safari, I've included a minimal example, any ideas why this would be happening?

I'm in an "mvc" type page where the page is rarely refreshed, changing pages just swaps out content on the page, and my pages have a pixi instances on them.

var stage, renderer, container, redText, blueText, keepGoing;

var animate = function() {
    if (keepGoing) {
        requestAnimFrame(animate);
        renderer.render(stage);
    }
};

var startPixi = function() {
    stage = new PIXI.Stage();
    renderer = PIXI.autoDetectRenderer(1080, 393, {transparent: true});
    document.querySelector('body').appendChild(renderer.view);

    redText = PIXI.Sprite.fromImage("img/red-text.png");

    stage.addChild(redText);

    lastTime = new Date().getTime() / 1000;
    keepGoing = true;
    requestAnimFrame(animate);
};

var endPixi = function() {

    stage.removeChild(redText);

    redText.texture.destroy(true);
    keepGoing = false;
    renderer.destroy();

    var c = document.querySelector('canvas');
    document.querySelector('body').removeChild(c);
};

So calling startPixi() then endPixi() more than 4 times causes this behavior

@erd0s erd0s changed the title Instantiating more than 4 WebGLRenderers causes black canvas Instantiating more than 4 WebGLRenderers causes black textures Dec 2, 2014
@erd0s
Copy link
Author

erd0s commented Dec 2, 2014

After testing further it appears that the texture's the problem. I noticed that redText.texture.baseTexture._glTextures is empty after the 5th renderer

@erd0s
Copy link
Author

erd0s commented Dec 2, 2014

I think I found the problem! This will need some input from a maintainer tho, in WebGLSpriteBatch.prototype.renderBatch the _dirty array never has more than 4 elements. Once WebGLRenderer.gl.id is >= 4 the expression

if(texture._dirty[gl.id])

... always returns undefined and the texture is never updated with the current renderer. Although I'm not exactly sure what the process for setting textures as dirty is, or why there's only ever 4, or even what a dirty texture is.

I'm able to get around this problem by manually calling:

renderer.updateTexture(redTexture.texture.baseTexture);

Directly after I create the texture/sprite

Thoughts?

@franciscopreller
Copy link

Hey, I'm having this issue as well. Could anyone shed any light?

@alexmahan
Copy link

Ran into this issue as well with 5 renderers on the same page. A sprited icon in the last renderer would always render as a black square.

As suggested above, calling renderer.updateTexture(myIcon.texture.baseTexture); after the sprite is added fixes the issue.

@mattdesl
Copy link
Contributor

The problem is because of this really ugly line:
https://github.com/GoodBoyDigital/pixi.js/blob/2dfd58b3757e16fad4b26a1fb9edf944f38aa5b8/src/pixi/textures/BaseTexture.js#L110

One fix would be to use an empty initial array for _dirty, and then do a check like this instead:

texture._dirty[gl.id] !== false

There are a lot of places in PIXI that do the above dirty check so the patch would actually have to go through and change all of them. Instead it would be better to avoid repetition by providing a getter or method to check "dirtyness" of a texture.

I think these problems would appear less if Pixi contributors / maintainers took the DRY principle more seriously.

EDIT: An alternative approach would be to make _dirty a getter (Object.defineProperty) that accesses something like __dirty and does the !== false check. This is getting kinda nasty though.

@noseglid
Copy link

noseglid commented Jan 7, 2015

I've run into this as well now. Is there a workaround for 2.x? I can't go back to 1.6.1 since I need features from 2.x.

@mattdesl
Copy link
Contributor

mattdesl commented Jan 7, 2015

@noseglid
you can just add a few more true's to the problematic code

😐

@noseglid
Copy link

noseglid commented Jan 7, 2015

Won't really help me.. I don't know how many I need.

@mattdesl
Copy link
Contributor

mattdesl commented Jan 7, 2015

Well most browsers aren't capable of handling more than a couple dozen, so that's a starting point. Why do you need so many?

Sent from my iPhone

On Jan 7, 2015, at 3:18 PM, Alexander Olsson notifications@github.com wrote:

Won't really help me.. I don't know how many I need.


Reply to this email directly or view it on GitHub.

@englercj englercj added the 🕷 Bug Verified that it’s actually a legit bug that exists in the current release. label Jan 8, 2015
@englercj englercj added this to the v3.0 milestone Jan 8, 2015
@englercj englercj self-assigned this Jan 8, 2015
@noseglid
Copy link

noseglid commented Jan 8, 2015

It's not that I have many at the same time. I create and destroy them as I go along.

My project is an SPA; so the page is never really reloaded.

@mattdesl
Copy link
Contributor

mattdesl commented Jan 8, 2015

In previous projects I've used a singleton to re-use canvas contexts, but that can arguably get annoying to test and also isn't easily supported by some frameworks.

I suspect even if this bug is fixed you will find other issues with Pixi not cleaning up resources from earlier renderers (just off the top of my head: the texture cache). 😞

@englercj
Copy link
Member

englercj commented Jan 9, 2015

This should be fixed now in v3. Using events from the texture that any number of renderers can listen for to update/destroy that texture.

@lock
Copy link

lock bot commented Feb 26, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Feb 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🕷 Bug Verified that it’s actually a legit bug that exists in the current release.
Projects
None yet
Development

No branches or pull requests

6 participants