Files
jiggablend/internal/runner/blender/detect.go
Justin Harms 5303f01f7c Implement GPU backend detection for Blender compatibility
- Added functionality to detect GPU backends (HIP and NVIDIA) during runner registration, enhancing compatibility for Blender versions below 4.x.
- Introduced a new method, DetectAndStoreGPUBackends, to download the latest Blender and run a detection script, storing the results for future rendering decisions.
- Updated rendering logic to force CPU rendering when HIP is detected on systems with Blender < 4.x, ensuring stability and compatibility.
- Enhanced the Context structure to include flags for GPU detection status, improving error handling and rendering decisions based on GPU availability.
2026-03-13 18:32:05 -05:00

46 lines
1.3 KiB
Go

// Package blender: GPU backend detection for HIP vs NVIDIA.
package blender
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"jiggablend/pkg/scripts"
)
// DetectGPUBackends runs a minimal Blender script to detect whether HIP (AMD) and/or
// NVIDIA (CUDA/OptiX) devices are available. Use this to decide whether to force CPU
// for Blender < 4.x (only force when HIP is present, since HIP has no official support pre-4).
func DetectGPUBackends(blenderBinary, scriptDir string) (hasHIP, hasNVIDIA bool, err error) {
scriptPath := filepath.Join(scriptDir, "detect_gpu_backends.py")
if err := os.WriteFile(scriptPath, []byte(scripts.DetectGPUBackends), 0644); err != nil {
return false, false, fmt.Errorf("write detection script: %w", err)
}
defer os.Remove(scriptPath)
env := TarballEnv(blenderBinary, os.Environ())
cmd := exec.Command(blenderBinary, "-b", "--python", scriptPath)
cmd.Env = env
cmd.Dir = scriptDir
out, err := cmd.CombinedOutput()
if err != nil {
return false, false, fmt.Errorf("run blender detection: %w (output: %s)", err, string(out))
}
scanner := bufio.NewScanner(strings.NewReader(string(out)))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
switch line {
case "HAS_HIP":
hasHIP = true
case "HAS_NVIDIA":
hasNVIDIA = true
}
}
return hasHIP, hasNVIDIA, scanner.Err()
}