From 1870c49c4a51006c87aef316c52ac9e5f11c4334 Mon Sep 17 00:00:00 2001 From: soypat Date: Mon, 25 Nov 2024 01:56:10 -0300 Subject: [PATCH] work on adding poly gpu processing --- examples/ui-text/uitext.go | 3 ++- gsdf.go | 18 +++++++++++++++++ gsdf2d.go | 41 ++++++++++++++++++++++++++++++++++++++ gsdfaux/ui.go | 6 ++++-- 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/examples/ui-text/uitext.go b/examples/ui-text/uitext.go index 211354c..819aae1 100644 --- a/examples/ui-text/uitext.go +++ b/examples/ui-text/uitext.go @@ -55,6 +55,7 @@ func main() { Height: 600, }) if err != nil { - log.Fatal("UI:", err) + fmt.Println(err) + return } } diff --git a/gsdf.go b/gsdf.go index 78a68b6..99508d7 100644 --- a/gsdf.go +++ b/gsdf.go @@ -4,6 +4,7 @@ import ( _ "embed" "errors" "fmt" + "unsafe" "github.com/chewxy/math32" "github.com/soypat/glgl/math/ms2" @@ -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 { diff --git a/gsdf2d.go b/gsdf2d.go index 8b1e2e9..a5c358c 100644 --- a/gsdf2d.go +++ b/gsdf2d.go @@ -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} } @@ -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=v[i].y, + p.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 { diff --git a/gsdfaux/ui.go b/gsdfaux/ui.go index 6513c9e..1d3adb4 100644 --- a/gsdfaux/ui.go +++ b/gsdfaux/ui.go @@ -4,6 +4,7 @@ package gsdfaux import ( "bytes" + "fmt" "log" "math" "time" @@ -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; @@ -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