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
285 stars 88 forks source link

Allowing Graphic.validateNow to work as a pre-cacher of StrokeVertices? #60

Closed IonSwitz closed 10 years ago

IonSwitz commented 11 years ago

As I was checking through the Stroke code tonight, I saw that calling validateNow will calculate the vertex data etc for the Graphic, BUT it will run twice in a frame.

As a frame rate fiend, I like to be able to pre-calculate everything in a shape, before it renders. The way validateNow , and the building of vertices is run today, there is no way to do that, since validateNow will run within the render method, and re-generate the vertices even if no change has occured since the last time validateNow was run.

So I would suggest adding a member variable, something like:

protected var hasBeenValidatedButNeedsRender:Boolean = false;

that gets set to true after running through validateNow, regardless of where it is called from.

and, in render, we would check this flag, before running into validateNow.

override public function render( renderSupport:RenderSupport, parentAlpha:Number ):void { if ( hasBeenValidatedButNeedsRender == false ) validateNow();

This would allow us to precalculate everything in a Graphic class, and the only thing render would actually do would be to push the triangles to the hardware.

Doesn't that sound like a very pleasant option?

jonathanrpace commented 10 years ago

Hey,

I'm not able to see how or why the stroke class would do its work twice in a frame. The validateNow() function only calls buildGeometry() if 'isInvalid == true'.

How are reproducing this?

IonSwitz commented 10 years ago

The thing I was trying out was to avoid having the geometry generated during the render phase, and instead be able to call validateNow before rendering starts, such as in a initialization stage.

However, since the "isInvalid" is only set to false in the render phase, this isn't possible.

So, even if I call validateNow in the initialization stage, the geometry will be re-generated during the first call to render(), and that can cause frame-rate hickups.

Maybe I was being unclear about the "being called twice"-reasoning, but I hope this explains the issue better?

jonathanrpace commented 10 years ago

Gotcha - seems like a bug to me (it really shouldn't be validating the geometry again if you've already called validateNow()). Will take a look in a few days (out of the country at the moment)

IonSwitz commented 10 years ago

It's a no-brainer, really, to implement. I could throw something together if you prefer.

All it needs is a boolean flag to check if the geometry has been built already.:)

IonSwitz commented 10 years ago

A fix for this has been checked in. Please see separate issue for explanation of what changed.

https://github.com/StarlingGraphics/Starling-Extension-Graphics/issues/79