| 属性 | 内容 |
|---|---|
| 项目名称 | GoGPU |
| GitHub | https://github.com/gogpu/gogpu |
| 开发语言 | Go 1.25+ |
| 项目定位 | Pure Go GPU 计算与图形框架 |
| 核心理念 | "GPU power, Go simplicity. Zero CGO." |
| 开源协议 | MIT |
| Star 数 | 178+ |
| 开发状态 | 活跃维护 (v0.22.9, 2026-03-07) |
"Pure Go Graphics Framework — GPU power, Go simplicity"GoGPU 是一个突破性的 GPU 计算框架,它让 Go 语言开发者能够:
┌─────────────────────────────────────────────────────────────────────┐
│ USER APPLICATION 用户应用层 │
│ │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ gogpu.App │ │
│ │ • 窗口管理 │ │
│ │ • 事件处理 │ │
│ │ • 渲染循环 │ │
│ └──────────────────────────┬───────────────────────────────────┘ │
│ │ │
│ ┌──────────────────────────▼───────────────────────────────────┐ │
│ │ gogpu.Context / Renderer │ │
│ │ • 绘图 API │ │
│ │ • 纹理管理 │ │
│ │ • 着色器管理 │ │
│ └──────────────────────────┬───────────────────────────────────┘ │
└─────────────────────────────┼────────────────────────────────────────┘
│
┌─────────────────────────────▼────────────────────────────────────────┐
│ BACKEND SELECTION 后端选择层 │
│ │
│ ┌─────────────────────────────┐ ┌─────────────────────────────┐ │
│ │ Pure Go Backend │ │ Rust Backend │ │
│ │ (default, 零依赖) │ │ (-tags rust, 高性能) │ │
│ └─────────────┬───────────────┘ └─────────────┬───────────────┘ │
└────────────────┼────────────────────────────────┼────────────────────┘
│ │
┌────────────────▼────────────────────────────────▼────────────────────┐
│ HAL (Hardware Abstraction Layer) │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Vulkan │ │ Metal │ │ DX12 │ │
│ │ (Windows │ │ (macOS) │ │ (Windows) │ │
│ │ Linux) │ │ │ │ │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ GLES │ │ Software │ │
│ │ (通用) │ │ (无 GPU) │ │
│ └──────────────┘ └──────────────┘ │
└──────────────────────────────────────────────────────────────────────┘
GoGPU 采用专业级多线程架构(类似 Ebiten/Gio):
┌─────────────────┐ ┌─────────────────┐
│ Main Thread │◄────►│ Render Thread │
│ │ │ │
│ • Window Events │ │ • GPU Device │
│ • Message Pump │ │ • Swapchain │
│ • OS callbacks │ │ • Commands │
└─────────────────┘ └─────────────────┘
│ │
│ "Never "Not Responding" │
│ │
▼ ▼
Win32/Cocoa/X11 Vulkan/Metal/DX12
优势:
| 后端 | 构建标签 | 依赖 | 适用场景 |
|---|---|---|---|
| Pure Go | (默认) | 无 | 简单部署、零依赖 |
| Rust | -tags rust | wgpu-native 动态库 | 最大性能、全平台 |
切换方式:
# Pure Go (默认)
go build ./...
# Rust 后端
go build -tags rust ./...
// 自动选择最佳 (默认)
app := gogpu.NewApp(gogpu.DefaultConfig())
// 强制 Vulkan
app := gogpu.NewApp(gogpu.DefaultConfig().
WithGraphicsAPI(gogpu.GraphicsAPIVulkan))
// 强制 DirectX 12 (Windows)
app := gogpu.NewApp(gogpu.DefaultConfig().
WithGraphicsAPI(gogpu.GraphicsAPIDX12))
// 强制 Metal (macOS)
app := gogpu.NewApp(gogpu.DefaultConfig().
WithGraphicsAPI(gogpu.GraphicsAPIMetal))
// 软件渲染 (无 GPU)
app := gogpu.NewApp(gogpu.DefaultConfig().
WithGraphicsAPI(gogpu.GraphicsAPISoftware))
package main
import (
"github.com/gogpu/gogpu"
"github.com/gogpu/gogpu/gmath"
)
func main() {
app := gogpu.NewApp(gogpu.DefaultConfig().
WithTitle("Hello GoGPU").
WithSize(800, 600))
app.OnDraw(func(dc *gogpu.Context) {
dc.DrawTriangleColor(gmath.DarkGray)
})
app.Run()
}
效果: 20 行代码实现 GPU 渲染窗口,对比原始 WebGPU 需要 480+ 行。
// 从文件加载
tex, err := renderer.LoadTexture("sprite.png")
defer tex.Destroy()
// 从 Go image 创建
img := image.NewRGBA(image.Rect(0, 0, 128, 128))
tex, err := renderer.NewTextureFromImage(img)
// 自定义选项
opts := gogpu.TextureOptions{
MagFilter: gputypes.FilterModeNearest, // 像素风
AddressModeU: gputypes.AddressModeRepeat, // 平铺
}
tex, err := renderer.LoadTextureWithOptions("tile.png", opts)
// 更新数据 (动态内容)
tex.UpdateData(newPixelData)
tex.UpdateRegion(x, y, w, h, partialData) // 局部更新
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Idle │────►│ Animating │────►│ Continuous │
│ (0% CPU) │ │ (VSync) │ │ (100% CPU) │
└──────▲──────┘ └──────┬──────┘ └─────────────┘
│ │
└───────────────────┘
Animation Token Stop
// UI 模式 (省电)
app := gogpu.NewApp(gogpu.DefaultConfig().
WithContinuousRender(false))
// 触发动画
token := app.StartAnimation()
// ... 动画运行中 (60fps) ...
token.Stop() // 返回 Idle (0% CPU)
// 请求单帧重绘
app.RequestRedraw() // 可从任意 goroutine 调用
// 创建计算管线
pipeline, _ := device.CreateComputePipeline(&hal.ComputePipelineDescriptor{
Layout: pipelineLayout,
Module: shaderModule,
EntryPoint: "main",
})
// 创建存储缓冲区
inputBuffer, _ := device.CreateBuffer(&hal.BufferDescriptor{
Size: dataSize,
Usage: gputypes.BufferUsageStorage | gputypes.BufferUsageCopyDst,
})
// 分派计算任务
encoder, _ := device.CreateCommandEncoder()
encoder.BeginEncoding("compute")
pass := encoder.BeginComputePass(&hal.ComputePassDescriptor{})
pass.SetPipeline(pipeline)
pass.SetBindGroup(0, bindGroup, nil)
pass.Dispatch(workgroupsX, 1, 1)
pass.End()
cmdBuf := encoder.EndEncoding()
queue.Submit([]hal.CommandBuffer{cmdBuf}, nil, 0)
| 项目 | GitHub | 描述 |
|---|---|---|
| gogpu/gogpu | github.com/gogpu/gogpu | GPU 框架 (本仓库) |
| gogpu/wgpu | github.com/gogpu/wgpu | Pure Go WebGPU 实现 |
| gogpu/naga | github.com/gogpu/naga | 着色器编译器 (WGSL→SPIR-V/MSL/GLSL) |
| gogpu/gg | github.com/gogpu/gg | 2D 图形库 (类似 HTML Canvas) |
| gogpu/ui | github.com/gogpu/ui | GUI 工具包 (计划中) |
| go-webgpu/webgpu | github.com/go-webgpu/webgpu | wgpu-native FFI 绑定 |
| go-webgpu/goffi | github.com/go-webgpu/goffi | Pure Go FFI 库 |
import "github.com/gogpu/gg"
// 创建绘图上下文
dc := gg.NewContext(512, 512)
// 绘制形状
dc.SetRGB(1, 0, 0)
dc.DrawCircle(256, 256, 100)
dc.Fill()
// 保存
dc.SavePNG("output.png")
特性:
用于包间依赖注入:
// 获取标准接口
provider := app.GPUContextProvider()
device := provider.Device() // gpucontext.Device
queue := provider.Queue() // gpucontext.Queue
events := app.EventSource() // gpucontext.EventSource
// 用于与 gg 等库集成
canvas, _ := ggcanvas.New(provider, 800, 600)
| 平台 | 图形 API | 后端 | 状态 |
|---|---|---|---|
| Windows | Vulkan, DX12, GLES | Pure Go / Rust | ✅ 完整支持 |
| Linux X11 | Vulkan, GLES | Pure Go / Rust | ✅ 完整支持 |
| Linux Wayland | Vulkan, GLES | Pure Go / Rust | ✅ 完整支持 |
| macOS | Metal | Pure Go / Rust | ✅ 完整支持 |
| macOS (Apple Silicon) | Metal | Pure Go / Rust | ✅ M1/M2/M3/M4 |
Windows:
gogpu
├── gpucontext (共享接口)
├── gputypes (WebGPU 类型)
├── gmath (数学库)
├── input (输入处理)
├── wgpu (Pure Go WebGPU)
│ ├── hal/vulkan
│ ├── hal/metal
│ ├── hal/dx12
│ └── hal/gles
└── go-webgpu/webgpu (可选, -tags rust)
└── wgpu-native (Rust 动态库)
# 基本要求
Go 1.25+
CGO_ENABLED=0 # 必须禁用 CGO
# 构建命令
CGO_ENABLED=0 go run .
CGO_ENABLED=0 go build ./...
# 运行测试
go test ./...
# 下载 wgpu-native
# Windows: wgpu_native.dll
# Linux: libwgpu_native.so
# macOS: libwgpu_native.dylib
# 放置到项目目录或系统 PATH
| 模式 | CPU 使用 | 延迟 | 适用场景 |
|---|---|---|---|
| Idle | 0% | <1ms | 静态 UI |
| Animating | VSync | 16ms | 动画 |
| Continuous | 100% | 最低 | 游戏 |
| 后端 | 性能 | 启动时间 | 二进制大小 |
|---|---|---|---|
| Pure Go | 良好 | 快 | 小 |
| Rust | 最佳 | 较慢 | 大 (含动态库) |
// 自动资源追踪
app.TrackResource(canvas) // 自动在关闭时清理
// 手动清理
app.OnClose(func() {
if canvas != nil {
canvas.Close()
}
})
清理顺序:
app.OnUpdate(func(dt float64) {
inp := app.Input()
// 键盘
if inp.Keyboard().JustPressed(input.KeySpace) {
player.Jump()
}
if inp.Keyboard().Pressed(input.KeyLeft) {
player.MoveLeft(dt)
}
// 鼠标
x, y := inp.Mouse().Position()
if inp.Mouse().JustPressed(input.MouseButtonLeft) {
player.Shoot(x, y)
}
})
// 系统偏好
if app.DarkMode() { /* 暗色主题 */ }
if app.ReduceMotion() { /* 禁用动画 */ }
if app.HighContrast() { /* 高对比度 */ }
// 缩放
scale := app.ScaleFactor() // 1.0 = 标准, 2.0 = Retina
fontMul := app.FontScale() // 用户字体偏好
// 剪贴板
text, _ := app.ClipboardRead()
app.ClipboardWrite("copied text")
// 光标
app.SetCursor(gpucontext.CursorPointer)
app.SetCursor(gpucontext.CursorText)
package main
import (
"log"
"github.com/gogpu/gogpu"
"github.com/gogpu/gogpu/gmath"
)
func main() {
// 配置应用
app := gogpu.NewApp(gogpu.DefaultConfig().
WithTitle("GoGPU Demo").
WithSize(1280, 720).
WithContinuousRender(false)) // 事件驱动模式
var texture *gogpu.Texture
// 初始化资源
app.OnDraw(func(dc *gogpu.Context) {
// 首次加载纹理
if texture == nil {
var err error
texture, err = dc.Renderer().LoadTexture("logo.png")
if err != nil {
log.Printf("Failed to load texture: %v", err)
return
}
app.TrackResource(texture) // 自动清理
}
// 清屏
dc.ClearColor(gmath.Color{R: 0.1, G: 0.1, B: 0.15, A: 1.0})
// 绘制纹理
dc.DrawTexture(texture, 100, 100)
})
// 更新逻辑
app.OnUpdate(func(dt float64) {
// 游戏逻辑
})
// 运行
if err := app.Run(); err != nil {
log.Fatal(err)
}
}
| 特性 | GoGPU | Ebiten | Gio | Fyne |
|---|---|---|---|---|
| GPU 访问 | ⭐⭐⭐ 完整 | ⭐⭐ 封装 | ⭐⭐ 封装 | ⭐ OpenGL |
| WebGPU | ✅ 原生 | ❌ | ❌ | ❌ |
| 零 CGO | ✅ | ✅ | ✅ | ❌ |
| 跨平台 | ✅✅✅ | ✅ | ✅ | ✅ |
| 游戏开发 | ⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐ | ⭐ |
| GUI 应用 | ⭐⭐ | ⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ |
| 学习曲线 | 中等 | 低 | 中等 | 低 |
定位差异:
✅ 零 CGO - 纯 Go,无交叉编译问题
✅ WebGPU 标准 - 现代 GPU API,未来-proof
✅ 双后端 - 灵活选择性能/依赖
✅ 多线程架构 - 专业级响应性
✅ 跨平台 - Windows/Linux/macOS
✅ 计算着色器 - GPGPU 支持
✅ 活跃生态 - gogpu/ 系列项目
⚠️ 相对较新 - API 可能不稳定
⚠️ 文档待完善 - 需要更多示例
⚠️ 社区较小 - 相比 Ebiten/Gio
⚠️ GUI 组件 - 需自己实现或等 gogpu/ui
⚠️ 移动平台 - iOS/Android 支持待验证
| 场景 | 推荐度 | 说明 |
|---|---|---|
| GPU 计算 | ⭐⭐⭐⭐⭐ | 计算着色器、并行处理 |
| 自定义渲染 | ⭐⭐⭐⭐⭐ | 需要底层 GPU 控制 |
| 游戏引擎开发 | ⭐⭐⭐⭐ | 作为底层渲染器 |
| 科学可视化 | ⭐⭐⭐⭐ | 大数据渲染 |
| 普通 GUI 应用 | ⭐⭐ | 使用 Gio/Fyne 更合适 |
| 快速原型 | ⭐⭐⭐ | API 简洁 |
研究时间: 2026-03-07
研究者: 小凯
标签: #GoGPU #WebGPU #Go语言 #GPU计算 #零CGO #图形编程*
还没有人回复