HigherOrderCO / Bend

A massively parallel, high-level programming language

Home Page:https://higherorderco.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Broken shader example in the guide

edusporto opened this issue · comments

Reproducing the behavior

The PR HigherOrderCO/HVM#379 broke one of the examples in the guide, namely:

Bend/GUIDE.md

Lines 748 to 772 in c3b1a36

```python
# given a shader, returns a square image
def render(depth, shader):
bend d = 0, i = 0:
when d < depth:
color = (fork(d+1, i*2+0), fork(d+1, i*2+1))
else:
width = depth / 2
color = shader(i % width, i / width)
return color
# given a position, returns a color
# for this demo, it just busy loops
def demo_shader(x, y):
bend i = 0:
when i < 100000:
color = fork(i + 1)
else:
color = 0x000001
return color
# renders a 256x256 image using demo_shader
def main:
return render(16, demo_shader)
```

It's also one of Bend's automated tests:

# given a shader, returns a square image
def render(depth, shader):
bend d = 0, i = 0:
when d < depth:
color = (fork(d+1, i*2+0), fork(d+1, i*2+1))
else:
width = depth / 2
color = shader(i % width, i / width)
return color
# given a position, returns a color
# for this demo, it just busy loops
def demo_shader(x, y):
bend i = 0:
when i < 10:
color = fork(i + 1)
else:
color = 0x000001
return color
# renders a 256x256 image using demo_shader
def main:
return render(5, demo_shader)

This is because demo_shader is now marked as unsafe since it contains a DUP (it duplicates i), and render tries to duplicate it by running it in every step of its recursion.

We could fix this by inlining demo_shader.

System Settings

MacBook Air M2

Additional context

No response

I'm not a fan of this solution, but it does work:

# given a shader, returns a square image
def render(depth):
  bend d = 0, i = 0:
    when d < depth:
      # create more pixels
      pixel = (fork(d+1, i*2+0), fork(d+1, i*2+1))
    else:
      width = depth / 2
      # calculate the color of the pixel
      bend i = i % width, y = i / width:
        # for this demo, it just busy loops
        when i < 10:
          color = fork(i + 1, y)
        else:
          color = 0x000001
      pixel = color
  return pixel

# renders a 256x256 image using demo_shader
def main:
  return render(5)

Any other ideas? @developedby @imaqtkatt

you can just

 
# given a shader, returns a square image 
 def render(depth): 
   bend d = 0, i = 0: 
     when d < depth: 
       color = (fork(d+1, i*2+0), fork(d+1, i*2+1)) 
     else: 
       width = depth / 2 
       color = demo_shader(i % width, i / width) 
   return color 
  
 # given a position, returns a color 
 # for this demo, it just busy loops 
 def demo_shader(x, y): 
   bend i = 0: 
     when i < 100000: 
       color = fork(i + 1) 
     else: 
       color = 0x000001 
   return color 

Oh 🤦 that's much easier