-
Notifications
You must be signed in to change notification settings - Fork 2
/
encode_shaders.go
54 lines (43 loc) · 973 Bytes
/
encode_shaders.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
func main() {
frag := EncodeDefine("FRAG_BINARY", "build/MidiDev/shaders/frag.spv")
vert := EncodeDefine("VERT_BINARY", "build/MidiDev/shaders/vert.spv")
f, err := os.Create("LyricMidiPlayer/ShaderBinaries.h")
check(err)
defer f.Close()
_, err = f.WriteString(frag + "\n" + vert)
check(err)
}
func EncodeDefine(NAME, path string) string {
bytes, err := ioutil.ReadFile(path)
check(err)
var byteString string
for _, b := range bytes {
if b > 127 {
byteString += "(char)" // signed unsigned goodness
}
byteString += fmt.Sprintf("0x%02x,", b)
}
byteString = TrimSuffix(byteString, ",")
byteString = "#define " + NAME + " {" + byteString + "}"
fmt.Println(byteString)
return byteString
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func TrimSuffix(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
s = s[:len(s)-len(suffix)]
}
return s
}