wgpu.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //go:build !js
  2. package wgpu
  3. /*
  4. // Android
  5. #cgo android,amd64 LDFLAGS: -L${SRCDIR}/lib/android/amd64 -lwgpu_native
  6. #cgo android,386 LDFLAGS: -L${SRCDIR}/lib/android/386 -lwgpu_native
  7. #cgo android,arm64 LDFLAGS: -L${SRCDIR}/lib/android/arm64 -lwgpu_native
  8. #cgo android,arm LDFLAGS: -L${SRCDIR}/lib/android/arm -lwgpu_native
  9. #cgo android LDFLAGS: -landroid -lm -llog
  10. // Linux
  11. #cgo linux,!android,amd64 LDFLAGS: -L${SRCDIR}/lib/linux/amd64 -lwgpu_native
  12. #cgo linux,!android,arm64 LDFLAGS: -L${SRCDIR}/lib/linux/arm64 -lwgpu_native
  13. #cgo linux,!android LDFLAGS: -lm -ldl
  14. // iOS
  15. #cgo ios,amd64 LDFLAGS: -L${SRCDIR}/lib/ios/amd64 -lwgpu_native
  16. #cgo ios,arm64 LDFLAGS: -L${SRCDIR}/lib/ios/arm64 -lwgpu_native
  17. // Darwin
  18. #cgo darwin,!ios,amd64 LDFLAGS: -L${SRCDIR}/lib/darwin/amd64 -lwgpu_native
  19. #cgo darwin,!ios,arm64 LDFLAGS: -L${SRCDIR}/lib/darwin/arm64 -lwgpu_native
  20. #cgo darwin LDFLAGS: -framework QuartzCore -framework Metal
  21. // Windows
  22. #cgo windows,amd64 LDFLAGS: -L${SRCDIR}/lib/windows/amd64 -lwgpu_native
  23. #cgo windows,arm64 LDFLAGS: -L${SRCDIR}/lib/windows/arm64 -lwgpu_native
  24. #cgo windows LDFLAGS: -lopengl32 -lgdi32 -ld3dcompiler_47 -lws2_32 -luserenv -lbcrypt -lntdll
  25. #include <stdio.h>
  26. #include "./lib/wgpu.h"
  27. #ifdef __ANDROID__
  28. #include <android/log.h>
  29. void logCallback_cgo(WGPULogLevel level, char const *msg) {
  30. switch (level) {
  31. case WGPULogLevel_Error:
  32. __android_log_write(ANDROID_LOG_ERROR, "GoLogWGPU", msg);
  33. break;
  34. case WGPULogLevel_Warn:
  35. __android_log_write(ANDROID_LOG_WARN, "GoLogWGPU", msg);
  36. break;
  37. default:
  38. __android_log_write(ANDROID_LOG_INFO, "GoLogWGPU", msg);
  39. break;
  40. }
  41. }
  42. #else
  43. void logCallback_cgo(WGPULogLevel level, char const *msg) {
  44. char const *level_str;
  45. switch (level) {
  46. case WGPULogLevel_Error:
  47. level_str = "Error";
  48. break;
  49. case WGPULogLevel_Warn:
  50. level_str = "Warn";
  51. break;
  52. case WGPULogLevel_Info:
  53. level_str = "Info";
  54. break;
  55. case WGPULogLevel_Debug:
  56. level_str = "Debug";
  57. break;
  58. case WGPULogLevel_Trace:
  59. level_str = "Trace";
  60. break;
  61. default:
  62. level_str = "Unknown Level";
  63. }
  64. fprintf(stderr, "[wgpu] [%s] %s\n", level_str, msg);
  65. }
  66. #endif
  67. */
  68. import "C"
  69. func init() {
  70. C.wgpuSetLogCallback(C.WGPULogCallback(C.logCallback_cgo), nil)
  71. }
  72. func SetLogLevel(level LogLevel) {
  73. C.wgpuSetLogLevel(C.WGPULogLevel(level))
  74. }
  75. func GetVersion() Version {
  76. return Version(C.wgpuGetVersion())
  77. }
  78. type (
  79. BindGroup struct{ ref C.WGPUBindGroup }
  80. BindGroupLayout struct{ ref C.WGPUBindGroupLayout }
  81. CommandBuffer struct{ ref C.WGPUCommandBuffer }
  82. PipelineLayout struct{ ref C.WGPUPipelineLayout }
  83. QuerySet struct{ ref C.WGPUQuerySet }
  84. RenderBundle struct{ ref C.WGPURenderBundle }
  85. Sampler struct{ ref C.WGPUSampler }
  86. ShaderModule struct{ ref C.WGPUShaderModule }
  87. TextureView struct{ ref C.WGPUTextureView }
  88. )
  89. func (p *BindGroup) Release() { C.wgpuBindGroupRelease(p.ref) }
  90. func (p *BindGroupLayout) Release() { C.wgpuBindGroupLayoutRelease(p.ref) }
  91. func (p *CommandBuffer) Release() { C.wgpuCommandBufferRelease(p.ref) }
  92. func (p *PipelineLayout) Release() { C.wgpuPipelineLayoutRelease(p.ref) }
  93. func (p *QuerySet) Release() { C.wgpuQuerySetRelease(p.ref) }
  94. func (p *RenderBundle) Release() { C.wgpuRenderBundleRelease(p.ref) }
  95. func (p *Sampler) Release() { C.wgpuSamplerRelease(p.ref) }
  96. func (p *ShaderModule) Release() { C.wgpuShaderModuleRelease(p.ref) }
  97. func (p *TextureView) Release() { C.wgpuTextureViewRelease(p.ref) }
  98. // cBool converts the given Go bool to a C.WGPUBool.
  99. func cBool(b bool) C.WGPUBool {
  100. if b {
  101. return 1
  102. }
  103. return 0
  104. }
  105. // goBool converts the given C.WGPUBool to a Go bool.
  106. func goBool(b C.WGPUBool) bool {
  107. return b != 0
  108. }