Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken shader example in the guide #576

Closed
edusporto opened this issue Jun 10, 2024 · 3 comments · Fixed by #577
Closed

Broken shader example in the guide #576

edusporto opened this issue Jun 10, 2024 · 3 comments · Fixed by #577

Comments

@edusporto
Copy link
Contributor

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

@edusporto
Copy link
Contributor Author

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

@developedby
Copy link
Member

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 

@edusporto
Copy link
Contributor Author

Oh 🤦 that's much easier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants