curv3d / curv

a language for making art using mathematics
Apache License 2.0
1.14k stars 73 forks source link

Lazy viewer geometry updates #38

Open ivocavalcante opened 6 years ago

ivocavalcante commented 6 years ago

Hi Doug,

I realized that Curv, as of now, keeps rendering the geometry continuously. It's ok for geometries that depend on the "time" variable (color changing ones, for example) and benchmark purposes. I found out, however, that it's not ideal for mobile laptop users (like me), since it increases temperature (and cooling need) and drains battery very fast.

I was thinking that maybe Curv should render the geometry only when really needed - I mean, when geometry changes and/or on users input (viewing changes, zooming, etc.). Of course, for geometries where time component is needed, the current behavior is right.

I've implemented the "lazy" viewer behavior on "lazy_viewer_update" branch, my fork. To be honest, I was trying to infer the need for "time" component from shape properties (there's a needTime() member that does it, supposedly) but the way it's implemented today makes it always consider time. I can't understand language parser well enough yet to try to tackle this duty myself, so I went "poor man" way: -O lazy command-line option. Obviously, it breaks some geometries (as your twistor.curv, for example).

What do you think, is this a feature worth dealing with?

-- Ivo

doug-moen commented 6 years ago

A -Olazy flag is a good idea. Thanks, I will take the change.

I've wanted this feature for a while. The -Olazy flag is a first step in solving the problem, until we have a way to automatically detect if time is being used. We can delete the command line interface for setting the flag once it can be set automatically.

To solve the larger problem, I am going to implement a smarter, optimizing compiler. I'll begin work on that in a few weeks. The new compiler will provide a basis for solving a number of problems, including:

  1. Detect if time is being used for lazy refresh.
  2. Better performance on the Mesa driver.
  3. It's part of my plan for implementing graphical sliders for interactively tweaking numeric parameters. The new code generator will be able to emit GLSL uniform variables.
doug-moen commented 5 years ago

The -Olazy command line argument is now implemented.

The final step, to close off this feature, is automatic lazy refresh. Fortunately, it doesn't look hard.

I want to set the lazy refresh flag automatically, without user intervention. It should depend on the model you are viewing, not on command line arguments. If the model is animated, lazy refresh is disabled, otherwise it is enabled.

The easy way to do this is to add a new boolean field, called "animated", to the shape record. All of the shape primitives will set this to either true or false, as appropriate. Then, there is a bit of C++ code under libcurv/geom that enables lazy refresh if the animated flag is false.

Currently, there are two boolean shape fields, "is_2d" and "is_3d". This adds a third boolean field that needs to be maintained. The relevant source that needs to be modified is the *.curv files in the lib directory (to define "animated" in all of the shape primitives). For example, cube will set animated=false, while union(s1,s2) will set animated=s1.animated||s2.animated.

I had previously considered modifying the compiler to automatically detect whether a given shape program depends on the time parameter, but that looks too difficult for the "1.0" release.