Skip to content

Commit

Permalink
TinyGo support added for CPU loads
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Aug 17, 2024
1 parent ee6562c commit 2e13f0c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Offshoot from [this project](https://github.com/soypat/sdf/pull/13). Is WIP.
- GPU and CPU implementations for all shapes and operations. CPU implementations are actually faster for simple parts.
- Design your part using one API, switch between CPU and GPU after design.
- Extremely coherent API design.
- TinyGo supported for CPU evaluation :)

## Package layour/structure

Expand Down
8 changes: 1 addition & 7 deletions examples/npt-flange/flange.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"runtime"

"github.com/soypat/glgl/v4.6-core/glgl"
"github.com/soypat/gsdf"
"github.com/soypat/gsdf/forge/threads"
"github.com/soypat/gsdf/glbuild"
Expand All @@ -31,12 +30,7 @@ const visualization = "nptflange.glsl"

func main() {
if useGPU {
_, terminate, err := glgl.InitWithCurrentWindow33(glgl.WindowConfig{
Title: "compute",
Version: [2]int{4, 6},
Width: 1,
Height: 1,
})
terminate, err := gleval.Init1x1GLFW()
if err != nil {
log.Fatal("FAIL to start GLFW", err.Error())
}
Expand Down
12 changes: 12 additions & 0 deletions gleval/gpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import (
"github.com/soypat/glgl/v4.6-core/glgl"
)

// Init1x1GLFW starts a 1x1 sized GLFW so that user can start working with GPU.
// It returns a termination function that should be called when user is done running loads on GPU.
func Init1x1GLFW() (terminate func(), err error) {
_, terminate, err = glgl.InitWithCurrentWindow33(glgl.WindowConfig{
Title: "compute",
Version: [2]int{4, 6},
Width: 1,
Height: 1,
})
return terminate, err
}

// NewComputeGPUSDF3 instantiates a [SDF3] that runs on the GPU.
func NewComputeGPUSDF3(glglSourceCode io.Reader, bb ms3.Box) (*SDF3Compute, error) {
combinedSource, err := glgl.ParseCombined(glglSourceCode)
Expand Down
8 changes: 7 additions & 1 deletion gleval/gpu_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (

var errNoCGO = errors.New("GPU evaluation requires CGo and is not supported on TinyGo")

// Init1x1GLFW starts a 1x1 sized GLFW so that user can start working with GPU.
// It returns a termination function that should be called when user is done running loads on GPU.
func Init1x1GLFW() (terminate func(), err error) {
return nil, errNoCGO
}

// NewComputeGPUSDF3 instantiates a [SDF3] that runs on the GPU.
func NewComputeGPUSDF3(glglSourceCode io.Reader, bb ms3.Box) (*SDF3Compute, error) {
return nil, errNoCGO
Expand All @@ -26,7 +32,7 @@ func (sdf *SDF3Compute) Bounds() ms3.Box {
}

func (sdf *SDF3Compute) Evaluate(pos []ms3.Vec, dist []float32, userData any) error {
return nil, errNoCGO
return errNoCGO
}

// NewComputeGPUSDF2 instantiates a [SDF2] that runs on the GPU.
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/soypat/gsdf
go 1.22.1

require (
github.com/chewxy/math32 v1.10.1
github.com/chewxy/math32 v1.11.0
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71
github.com/soypat/glgl v0.0.0-20240816031826-41aea5d4ce11
github.com/soypat/glgl v0.0.0-20240817151124-b984688a7f05
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/chewxy/math32 v1.10.1 h1:LFpeY0SLJXeaiej/eIp2L40VYfscTvKh/FSEZ68uMkU=
github.com/chewxy/math32 v1.10.1/go.mod h1:dOB2rcuFrCn6UHrze36WSLVPKtzPMRAQvBvUwkSsLqs=
github.com/chewxy/math32 v1.11.0 h1:8sek2JWqeaKkVnHa7bPVqCEOUPbARo4SGxs6toKyAOo=
github.com/chewxy/math32 v1.11.0/go.mod h1:dOB2rcuFrCn6UHrze36WSLVPKtzPMRAQvBvUwkSsLqs=
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 h1:5BVwOaUSBTlVZowGO6VZGw2H/zl9nrd3eCZfYV+NfQA=
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw=
github.com/go-gl/glfw v0.0.0-20221017161538-93cebf72946b h1:2hdUMUOJuLQkhaPAwoyOeSzoaBydYEkXkBEuqDuDBfg=
Expand All @@ -10,5 +12,7 @@ github.com/soypat/glgl v0.0.0-20240811035753-f615f56436f0 h1:9CU+IDi7gSeBRWxekqm
github.com/soypat/glgl v0.0.0-20240811035753-f615f56436f0/go.mod h1:jcTX7fb8Y1WYrBfHyqZtzZyXzEQaOVp/z+ngMn9IQuk=
github.com/soypat/glgl v0.0.0-20240816031826-41aea5d4ce11 h1:fM68EXUFrx6EmIQOUz6PV4X5Jnrx0FhSzrzY9h2Ut24=
github.com/soypat/glgl v0.0.0-20240816031826-41aea5d4ce11/go.mod h1:jcTX7fb8Y1WYrBfHyqZtzZyXzEQaOVp/z+ngMn9IQuk=
github.com/soypat/glgl v0.0.0-20240817151124-b984688a7f05 h1:xNPZV7iwDmhYLze/1Bhd4j4cuWFYxMS6bAEQYPN6d94=
github.com/soypat/glgl v0.0.0-20240817151124-b984688a7f05/go.mod h1:jcTX7fb8Y1WYrBfHyqZtzZyXzEQaOVp/z+ngMn9IQuk=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30 h1:m9O6OTJ627iFnN2JIWfdqlZCzneRO6EEBsHXI25P8ws=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=

0 comments on commit 2e13f0c

Please sign in to comment.