github.com/cybriq/giocore@v0.0.7-0.20210703034601-cfb9cb5f3900/gpu/internal/convertshaders/glslvalidate.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package main 4 5 import ( 6 "bytes" 7 "fmt" 8 "io/ioutil" 9 "os/exec" 10 "path/filepath" 11 ) 12 13 // GLSLValidator is OpenGL reference compiler. 14 type GLSLValidator struct { 15 Bin string 16 WorkDir WorkDir 17 } 18 19 func NewGLSLValidator() *GLSLValidator { return &GLSLValidator{Bin: "glslangValidator"} } 20 21 // Convert converts a glsl shader to spirv. 22 func (glsl *GLSLValidator) Convert(path, variant string, hlsl bool, input []byte) ([]byte, error) { 23 base := glsl.WorkDir.Path(filepath.Base(path), variant) 24 pathout := base + ".out" 25 26 cmd := exec.Command(glsl.Bin, 27 "--stdin", 28 "-I"+filepath.Dir(path), 29 "-V", // OpenGL ES 3.1. 30 "-w", // Suppress warnings. 31 "-S", filepath.Ext(path)[1:], 32 "-o", pathout, 33 ) 34 if hlsl { 35 cmd.Args = append(cmd.Args, "-DHLSL") 36 } 37 cmd.Stdin = bytes.NewBuffer(input) 38 39 out, err := cmd.Output() 40 if err != nil { 41 return nil, fmt.Errorf("%s\nfailed to run %v: %w", out, cmd.Args, err) 42 } 43 44 compiled, err := ioutil.ReadFile(pathout) 45 if err != nil { 46 return nil, fmt.Errorf("unable to read output %q: %w", pathout, err) 47 } 48 49 return compiled, nil 50 }