Skip to content

Commit 88a1b7a

Browse files
committed
fix Programmer not deduplicating and add test to prevent regression
1 parent d078123 commit 88a1b7a

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

glbuild/glbuild.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,10 @@ func (p *Programmer) WriteFragVisualizerSDF3(w io.Writer, obj Shader3D) (n int,
183183
func (p *Programmer) writeShaders(w io.Writer, nodes []Shader) (n int, err error) {
184184
clear(p.names)
185185
for i := len(nodes) - 1; i >= 0; i-- {
186-
scratch := p.scratch // Scratch buffer is renewed if capacity is increased.
187186
node := nodes[i]
188187
var name, body []byte
189188
p.scratch, name, body = AppendShaderSource(p.scratch[:0], node)
190-
nameHash := hash(scratch, 0)
189+
nameHash := hash(name, 0)
191190
bodyHash := hash(body, nameHash) // Body hash mixes name as well.
192191
gotBodyHash, nameConflict := p.names[nameHash]
193192
if nameConflict {

glbuild/glbuild_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package glbuild_test
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
"testing"
7+
8+
"github.com/soypat/gsdf"
9+
"github.com/soypat/gsdf/glbuild"
10+
)
11+
12+
func TestShaderNameDeduplication(t *testing.T) {
13+
// s1 and s2 are identical in name and body but different primitives.
14+
s1, _ := gsdf.NewCylinder(1, 2, 0)
15+
s2, _ := gsdf.NewCylinder(1, 2, 0)
16+
s1s1 := gsdf.Union(s1, s1)
17+
s1s2 := gsdf.Union(s1, s2)
18+
s1Name := string(s1.AppendShaderName(nil))
19+
s2Name := string(s2.AppendShaderName(nil))
20+
if s1Name != s2Name {
21+
t.Error("expected same name, got\n", s1Name, "\n", s2Name)
22+
}
23+
decl := "float " + s1Name + "(vec3 p)"
24+
for _, obj := range []glbuild.Shader3D{s1s1, s1s2} {
25+
programmer := glbuild.NewDefaultProgrammer()
26+
source := new(bytes.Buffer)
27+
n, err := programmer.WriteFragVisualizerSDF3(source, obj)
28+
if n != source.Len() {
29+
t.Fatal("written length mismatch", err)
30+
}
31+
if err != nil {
32+
t.Error(err)
33+
}
34+
src := source.String()
35+
declCount := strings.Count(src, decl)
36+
if declCount != 1 {
37+
t.Errorf("\n%s\nVisualizer: want one declaration, got %d", src, declCount)
38+
}
39+
source.Reset()
40+
n, err = programmer.WriteComputeSDF3(source, obj)
41+
if n != source.Len() {
42+
t.Fatal("written length mismatch", err)
43+
}
44+
if err != nil {
45+
t.Error(err)
46+
}
47+
src = source.String()
48+
declCount = strings.Count(src, decl)
49+
if declCount != 1 {
50+
t.Errorf("\n%s\nCompute: want one declaration, got %d", src, declCount)
51+
}
52+
}
53+
}

glrender/glrender_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
func TestRenderImage(t *testing.T) {
1717
img := image.NewRGBA(image.Rect(0, 0, 256, 256))
18-
renderer, err := NewImageRendererSDF2(256, nil)
18+
renderer, err := NewImageRendererSDF2(4096, nil)
1919
if err != nil {
2020
panic(err)
2121
}
@@ -45,22 +45,22 @@ func TestIcube(t *testing.T) {
4545
if origin != (ms3.Vec{}) {
4646
t.Error("expected origin at (0,0,0)")
4747
}
48-
t.Error("truebb", bb)
48+
t.Log("truebb", bb)
4949
levelsVisual("levels.glsl", topcube, 3, origin, RES)
5050

5151
subcubes := topcube.octree()
52-
t.Error("top", topcube.lvl, topcube.lvlIdx(), topcube.box(origin, topcube.size(RES)), topcube.size(RES))
52+
t.Log("top", topcube.lvl, topcube.lvlIdx(), topcube.box(origin, topcube.size(RES)), topcube.size(RES))
5353
for i, subcube := range subcubes {
5454
subsize := subcube.size(RES)
5555
subbox := subcube.box(origin, subsize)
5656
size := subbox.Size()
5757
if math32.Abs(size.Max()-subsize) > tol || math32.Abs(size.Min()-subsize) > tol {
58-
t.Error("size mismatch", size, subsize)
58+
t.Log("size mismatch", size, subsize)
5959
}
60-
t.Error("sub", subcube.lvl, subcube.lvlIdx(), subbox, subsize)
60+
t.Log("sub", subcube.lvl, subcube.lvlIdx(), subbox, subsize)
6161
if (i == 0 || i == 1) && subcube.lvl > 1 {
6262
subcube = subcube.octree()[1]
63-
t.Error("subsub", subcube.lvl, subcube.lvlIdx(), subcube.box(origin, subcube.size(RES)), subcube.size(RES))
63+
t.Log("subsub", subcube.lvl, subcube.lvlIdx(), subcube.box(origin, subcube.size(RES)), subcube.size(RES))
6464
}
6565
}
6666
levels := topcube.lvl

0 commit comments

Comments
 (0)