Skip to content

Commit 1870c49

Browse files
committed
work on adding poly gpu processing
1 parent 009b86d commit 1870c49

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

examples/ui-text/uitext.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func main() {
5555
Height: 600,
5656
})
5757
if err != nil {
58-
log.Fatal("UI:", err)
58+
fmt.Println(err)
59+
return
5960
}
6061
}

gsdf.go

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
_ "embed"
55
"errors"
66
"fmt"
7+
"unsafe"
78

89
"github.com/chewxy/math32"
910
"github.com/soypat/glgl/math/ms2"
@@ -35,6 +36,23 @@ type Builder struct {
3536
// flags is a bitfield controlling behaviour of Builder.
3637
flags Flags
3738
accumErrs []error
39+
// limVecGPU
40+
limVecGPU int
41+
}
42+
43+
func (bld *Builder) useGPU(n int) bool {
44+
return bld.limVecGPU != 0 && n > bld.limVecGPU || n > 1024
45+
}
46+
47+
func makeHashName[T any](dst []byte, name string, vec []T) []byte {
48+
var z T
49+
data := unsafe.Pointer(&vec[0])
50+
bdata := unsafe.Slice((*byte)(data), len(vec)*int(unsafe.Sizeof(z)))
51+
_ = bdata
52+
// for _, b := range bdata {
53+
54+
// }
55+
return fmt.Appendf(dst, "%s%x", name, uintptr(data))
3856
}
3957

4058
func (bld *Builder) Flags() Flags {

gsdf2d.go

+41
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,10 @@ func (bld *Builder) NewPolygon(vertices []ms2.Vec) glbuild.Shader2D {
555555
}
556556
prevIdx = i
557557
}
558+
println("poly")
559+
// if bld.useGPU(len(vertices)) {
560+
// return &polyGPU{poly2D: poly2D{vert: vertices}, bufname: makeHashName(nil, "ssboPoly", vertices)}
561+
// }
558562
return &poly2D{vert: vertices}
559563
}
560564

@@ -609,6 +613,43 @@ func (u *poly2D) AppendShaderObjects(objects []glbuild.ShaderObject) []glbuild.S
609613
return objects // TODO: implement shader buffer storage here!
610614
}
611615

616+
type polyGPU struct {
617+
poly2D
618+
bufname []byte
619+
}
620+
621+
func (c *polyGPU) AppendShaderBody(b []byte) []byte {
622+
b = glbuild.AppendDefineDecl(b, "ver", string(c.bufname))
623+
b = append(b, `const int num = v.length();
624+
float d = dot(p-v[0],p-v[0]);
625+
float s = 1.0;
626+
for( int i=0, j=num-1; i<num; j=i, i++ )
627+
{
628+
// distance
629+
vec2 e = v[j] - v[i];
630+
vec2 w = p - v[i];
631+
vec2 b = w - e*clamp( dot(w,e)/dot(e,e), 0.0, 1.0 );
632+
d = min( d, dot(b,b) );
633+
// winding number from http://geomalgorithms.com/a03-_inclusion.html
634+
bvec3 cond = bvec3( p.y>=v[i].y,
635+
p.y <v[j].y,
636+
e.x*w.y>e.y*w.x );
637+
if( all(cond) || all(not(cond)) ) s=-s;
638+
}
639+
return s*sqrt(d);
640+
`...)
641+
b = glbuild.AppendUndefineDecl(b, "ver")
642+
return b
643+
}
644+
645+
func (u *polyGPU) AppendShaderObjects(objects []glbuild.ShaderObject) []glbuild.ShaderObject {
646+
ssbo, err := glbuild.MakeShaderBufferReadOnly(u.bufname, u.vert)
647+
if err != nil {
648+
panic(err)
649+
}
650+
return append(objects, ssbo)
651+
}
652+
612653
// Extrude converts a 2D SDF into a 3D extrusion. Extrudes in both positive and negative Z direction, half of h both ways.
613654
func (bld *Builder) Extrude(s glbuild.Shader2D, h float32) glbuild.Shader3D {
614655
if s == nil {

gsdfaux/ui.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package gsdfaux
44

55
import (
66
"bytes"
7+
"fmt"
78
"log"
89
"math"
910
"time"
@@ -35,6 +36,7 @@ func ui(s glbuild.Shader3D, cfg UIConfig) error {
3536
}
3637
// Print OpenGL version
3738
// // Compile shaders and link program
39+
fragSrc := makeFragSource(root, sdfDecl.String())
3840
prog, err := glgl.CompileProgram(glgl.ShaderSource{
3941
Vertex: `#version 460
4042
in vec2 aPos;
@@ -44,10 +46,10 @@ void main() {
4446
gl_Position = vec4(aPos, 0.0, 1.0);
4547
}
4648
` + "\x00",
47-
Fragment: makeFragSource(root, sdfDecl.String()),
49+
Fragment: fragSrc,
4850
})
4951
if err != nil {
50-
log.Fatal(err)
52+
return fmt.Errorf("%s\n\n%w", fragSrc, err)
5153
}
5254
prog.Bind()
5355
// Define a quad covering the screen

0 commit comments

Comments
 (0)