// 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() }