Skip to content

Commit

Permalink
work on adding poly gpu processing
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Nov 25, 2024
1 parent 009b86d commit 1870c49
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
3 changes: 2 additions & 1 deletion examples/ui-text/uitext.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func main() {
Height: 600,
})
if err != nil {
log.Fatal("UI:", err)
fmt.Println(err)
return
}
}
18 changes: 18 additions & 0 deletions gsdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"errors"
"fmt"
"unsafe"

"github.com/chewxy/math32"
"github.com/soypat/glgl/math/ms2"
Expand Down Expand Up @@ -35,6 +36,23 @@ type Builder struct {
// flags is a bitfield controlling behaviour of Builder.
flags Flags
accumErrs []error
// limVecGPU
limVecGPU int
}

func (bld *Builder) useGPU(n int) bool {
return bld.limVecGPU != 0 && n > bld.limVecGPU || n > 1024
}

func makeHashName[T any](dst []byte, name string, vec []T) []byte {
var z T
data := unsafe.Pointer(&vec[0])
bdata := unsafe.Slice((*byte)(data), len(vec)*int(unsafe.Sizeof(z)))
_ = bdata
// for _, b := range bdata {

// }
return fmt.Appendf(dst, "%s%x", name, uintptr(data))
}

func (bld *Builder) Flags() Flags {
Expand Down
41 changes: 41 additions & 0 deletions gsdf2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ func (bld *Builder) NewPolygon(vertices []ms2.Vec) glbuild.Shader2D {
}
prevIdx = i
}
println("poly")
// if bld.useGPU(len(vertices)) {
// return &polyGPU{poly2D: poly2D{vert: vertices}, bufname: makeHashName(nil, "ssboPoly", vertices)}
// }
return &poly2D{vert: vertices}
}

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

type polyGPU struct {
poly2D
bufname []byte
}

func (c *polyGPU) AppendShaderBody(b []byte) []byte {
b = glbuild.AppendDefineDecl(b, "ver", string(c.bufname))
b = append(b, `const int num = v.length();
float d = dot(p-v[0],p-v[0]);
float s = 1.0;
for( int i=0, j=num-1; i<num; j=i, i++ )
{
// distance
vec2 e = v[j] - v[i];
vec2 w = p - v[i];
vec2 b = w - e*clamp( dot(w,e)/dot(e,e), 0.0, 1.0 );
d = min( d, dot(b,b) );
// winding number from http://geomalgorithms.com/a03-_inclusion.html
bvec3 cond = bvec3( p.y>=v[i].y,
p.y <v[j].y,
e.x*w.y>e.y*w.x );
if( all(cond) || all(not(cond)) ) s=-s;
}
return s*sqrt(d);
`...)
b = glbuild.AppendUndefineDecl(b, "ver")
return b
}

func (u *polyGPU) AppendShaderObjects(objects []glbuild.ShaderObject) []glbuild.ShaderObject {
ssbo, err := glbuild.MakeShaderBufferReadOnly(u.bufname, u.vert)
if err != nil {
panic(err)
}
return append(objects, ssbo)
}

// Extrude converts a 2D SDF into a 3D extrusion. Extrudes in both positive and negative Z direction, half of h both ways.
func (bld *Builder) Extrude(s glbuild.Shader2D, h float32) glbuild.Shader3D {
if s == nil {
Expand Down
6 changes: 4 additions & 2 deletions gsdfaux/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package gsdfaux

import (
"bytes"
"fmt"
"log"
"math"
"time"
Expand Down Expand Up @@ -35,6 +36,7 @@ func ui(s glbuild.Shader3D, cfg UIConfig) error {
}
// Print OpenGL version
// // Compile shaders and link program
fragSrc := makeFragSource(root, sdfDecl.String())
prog, err := glgl.CompileProgram(glgl.ShaderSource{
Vertex: `#version 460
in vec2 aPos;
Expand All @@ -44,10 +46,10 @@ void main() {
gl_Position = vec4(aPos, 0.0, 1.0);
}
` + "\x00",
Fragment: makeFragSource(root, sdfDecl.String()),
Fragment: fragSrc,
})
if err != nil {
log.Fatal(err)
return fmt.Errorf("%s\n\n%w", fragSrc, err)
}
prog.Bind()
// Define a quad covering the screen
Expand Down

0 comments on commit 1870c49

Please sign in to comment.