Gamua / Starling-Framework

The Cross Platform Game Engine
http://www.starling-framework.org
Other
2.82k stars 821 forks source link

Render Texture: "Error #3605: Sampler 0 binds an invalid texture" when not cleared or drawn before actual Image rendering #1087

Closed Adolio closed 2 years ago

Adolio commented 2 years ago

Hi Daniel!

I just found this issue while trying to implement a post-process stack using render textures.

Best, Aurélien


Description

When Starling::enableErrorChecking is active, if a RenderTexture is created & assigned to an Image without being cleared or drawn before an Error from Context3D::drawTriangles() is raised when the rendering procedure is executed.

Full stack trace

Error: Error #3605: Sampler 0 binds an invalid texture.
    at flash.display3D::Context3D/drawTriangles()
    at starling.rendering::Effect/render()[/Users/redge/Development/starling/starling/src/starling/rendering/Effect.as:236]
    at starling.display::MeshBatch/render()[/Users/redge/Development/starling/starling/src/starling/display/MeshBatch.as:254]
    at starling.rendering::Painter/drawBatch()[/Users/redge/Development/starling/starling/src/starling/rendering/Painter.as:709]
    at BatchProcessor/finishBatch()[/Users/redge/Development/starling/starling/src/starling/rendering/BatchProcessor.as:117]
    at starling.rendering::Painter/finishMeshBatch()[/Users/redge/Development/starling/starling/src/starling/rendering/Painter.as:568]
    at starling.filters::FragmentFilter/renderPasses()[/Users/redge/Development/starling/starling/src/starling/filters/FragmentFilter.as:256]
    at starling.filters::FragmentFilter/render()[/Users/redge/Development/starling/starling/src/starling/filters/FragmentFilter.as:161]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:395]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.display::DisplayObjectContainer/render()[/Users/redge/Development/starling/starling/src/starling/display/DisplayObjectContainer.as:396]
    at starling.core::Starling/render()[/Users/redge/Development/starling/starling/src/starling/core/Starling.as:460]
    at starling.core::Starling/nextFrame()[/Users/redge/Development/starling/starling/src/starling/core/Starling.as:407]
    at starling.core::Starling/onEnterFrame()[/Users/redge/Development/starling/starling/src/starling/core/Starling.as:635]

Potentially related forum thread

How to reproduce?

Error checking must be on

Starling.current.enableErrorChecking = true;

Failing code

var texture:RenderTexture = new RenderTexture(800, 480, false);
var image:Image = new Image(texture);
addChild(image);

Workaround

var texture:RenderTexture = new RenderTexture(800, 480, false);
texture.clear(); // workaround
var image:Image = new Image(texture);
addChild(image);

Solution idea / questions

Should Starling ensures that the RenderTexture is properly cleared at creation or should this be stated in the documentation (I didn't managed to found it)?

PrimaryFeather commented 2 years ago

Hey Aurélien, this is "as designed", although you are perfectly right: this information needs to be part of the documentation! I just extended the asdoc info accordingly.

I also had a look if I could automate this — call "clear" automatically on first render if it hasn't yet happened – but that is not so easy. The error occurs when the image/quad/mesh that has the texture assigned is rendered; and from the perspective of that object, it's just a normal texture, like any other, so there's no way to "ask" if the texture is ready. And the RenderTexture itself isn't notified that it's being rendered at all.

So I guess the best thing to do is inform users about this requirement (it's easy to follow, anyway), and otherwise leave this as it is.

In any case, thanks for making me aware of this!

Adolio commented 2 years ago

Thanks Daniel, it looks good to me as well 👍

Thanks for the investigations & the asdoc update 🙂

PrimaryFeather commented 2 years ago

You're very welcome, my friend! 😃