Using parametric checkbox

7 views
Skip to first unread message

Wink Saville

unread,
Jun 12, 2020, 3:39:01 PM6/12/20
to cu...@googlegroups.com
What am I doing wrong?

This fails:

parametric
  boxMitred :: checkbox = true;
in
  //let boxMitred = true in
  let boxF=if (boxMitred) box.mitred else box in
    boxF [25, 25, 25]

with:

$ curv -l box.curv
ERROR: indexing error
left side: (if (<param boxMitred>) <function mitred> else {bbox:[[-1,-1,-1],[1,1,1]],call:<function exact>,colour:<function>,dist:<function dist>,exact:<function exact>,is_2d:#false,is_3d:#true,mitred:<function mitred>}) (type Error)
right side: [25,25,25]
at file "box.curv":
6|     boxF [25, 25, 25]
       ^^^^^^^^^^^^^^^^^


But this works:

parametric
  boxMitred :: checkbox = true;
in
  let boxMitred = true in
  let boxF=if (boxMitred) box.mitred else box in
    boxF [25, 25, 25]


curv outputs:

$ curv -l box.curv
3D shape 25×25×25


Doug Moen

unread,
Jun 12, 2020, 5:16:43 PM6/12/20
to Curv
You aren't doing anything wrong, this is a limitation of the compiler that compiles Curv code into GPU instructions.

The problem is that on the GPU (I am compiling into GLSL), functions are not values. Unlike C, there is no concept of a function pointer.

box.mitred and box are both functions.

`if (boxMitred) box.mitred else box` is an expression that computes a function value *at GPU run time*, conditional on boxMitred. In C, this expression would return a function pointer. But there are no function pointers on the GPU, so my poor, stupid compiler just doesn't know what to do with this.

In the second example, boxMitred is a compile time constant, so the result of the expression is a function that is known at GPU compile time, so it works. The compiler uses partial evaluation to resolve as many subexpressions as possible at GPU compile time.

So it's a compiler bug, that I plan to have fixed for the Curv 1.0 release. The compiler will need an improved ability to perform static analysis and transform code from one form to another.

I will experiment with workarounds and post something more later.

Doug Moen.
--
You received this message because you are subscribed to the Google Groups "Curv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to curv+uns...@googlegroups.com.

Doug Moen

unread,
Jun 13, 2020, 8:05:40 AM6/13/20
to Curv
Okay, here is working code that implements a conditional shape (where the condition contains a GPU runtime variable).

let
  if_shape[c,s1,s2] = make_shape {
    dist p = if (c) s1.dist p else s2.dist p;
    colour p = if (c) s1.colour p else s2.colour p;
    bbox = [min[s1.bbox[MIN],s2.bbox[MIN]],max[s1.bbox[MAX],s2.bbox[MAX]]];
    is_2d = s1.is_2d && s2.is_2d;
    is_3d = s1.is_3d && s2.is_3d;
  };
  dim = [25,25,25];
in
parametric
  boxMitred :: checkbox = true;
in
  //if_shape[boxMitred, box.mitred dim, box dim]
  if_shape[boxMitred, cube, sphere]


Wink Saville

unread,
Jun 13, 2020, 12:21:57 PM6/13/20
to Doug Moen, Curv
Nice, worked great.
Reply all
Reply to author
Forward
0 new messages