Files
gio/gpu/internal/convertshaders/spirvcross.go
T
Elias Naur 2c7aba9e7c gpu/internal/convertshaders: move internal/cmd/convertshaders
The convert program is only used by the shaders from package gpu, and
we're about to make the backend package imported by the program internal
to package gpu. Move the converter below package gpu.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-03-06 14:26:47 +01:00

46 lines
914 B
Go

// SPDX-License-Identifier: Unlicense OR MIT
package main
import (
"bytes"
"fmt"
"os/exec"
)
// SPIRVCross cross-compiles spirv shaders to es, hlsl and others.
type SPIRVCross struct {
Bin string
}
func NewSPIRVCross() *SPIRVCross { return &SPIRVCross{Bin: "spirv-cross"} }
// Convert converts compute shader from spirv format to a target format.
func (spirv *SPIRVCross) Convert(input []byte, target, version string) (string, error) {
var cmd *exec.Cmd
switch target {
case "es":
cmd = exec.Command(spirv.Bin,
"--es",
"--version", version,
"-",
)
case "hlsl":
cmd = exec.Command(spirv.Bin,
"--hlsl",
"--shader-model", version,
"-",
)
default:
return "", fmt.Errorf("unknown target %q", target)
}
cmd.Stdin = bytes.NewBuffer(input)
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to run %v: %w", cmd.Args, err)
}
return string(out), nil
}