-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
183 lines (162 loc) · 4.66 KB
/
process.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2022, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// heavily modified from go src/cmd/gofmt/internal.go:
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"go/ast"
"go/token"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/emer/gosl/v2/alignsl"
"github.com/emer/gosl/v2/slprint"
"golang.org/x/tools/go/packages"
)
// does all the file processing
func ProcessFiles(paths []string) (map[string][]byte, error) {
fls := FilesFromPaths(paths)
gosls := ExtractGoFiles(fls) // extract Go files to shader/*.go
hlslFiles := []string{}
for _, fn := range fls {
if strings.HasSuffix(fn, ".hlsl") {
hlslFiles = append(hlslFiles, fn)
}
}
pf := "./" + *outDir
pkgs, err := packages.Load(&packages.Config{Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedTypesSizes}, pf)
if err != nil {
log.Println(err)
return nil, err
}
if len(pkgs) != 1 {
err := fmt.Errorf("More than one package for path: %v", pf)
log.Println(err)
return nil, err
}
pkg := pkgs[0]
if len(pkg.GoFiles) == 0 {
err := fmt.Errorf("No Go files found in package: %+v", pkg)
log.Println(err)
return nil, err
}
// fmt.Printf("go files: %+v", pkg.GoFiles)
// return nil, err
// map of files with a main function that needs to be compiled
needsCompile := map[string]bool{}
serr := alignsl.CheckPackage(pkg)
if serr != nil {
fmt.Println(serr)
}
slrandCopied := false
for fn := range gosls {
gofn := fn + ".go"
if *debug {
fmt.Printf("###################################\nProcessing Go file: %s\n", gofn)
}
var afile *ast.File
var fpos token.Position
for _, sy := range pkg.Syntax {
pos := pkg.Fset.Position(sy.Package)
_, posfn := filepath.Split(pos.Filename)
if posfn == gofn {
fpos = pos
afile = sy
break
}
}
if afile == nil {
fmt.Printf("Warning: File named: %s not found in processed package\n", gofn)
continue
}
var buf bytes.Buffer
cfg := slprint.Config{Mode: printerMode, Tabwidth: tabWidth, ExcludeFuns: excludeFunMap}
cfg.Fprint(&buf, pkg, fpos, afile)
// ioutil.WriteFile(filepath.Join(*outDir, fn+".tmp"), buf.Bytes(), 0644)
slfix, hasSlrand := SlEdits(buf.Bytes())
if hasSlrand && !slrandCopied {
if *debug {
fmt.Printf("\tcopying slrand.hlsl to shaders\n")
}
CopySlrand()
slrandCopied = true
}
exsl, hasMain := ExtractHLSL(slfix)
gosls[fn] = exsl
if hasMain {
needsCompile[fn] = true
}
if !*keepTmp {
os.Remove(fpos.Filename)
}
// add hlsl code
for _, hlfn := range hlslFiles {
if fn+".hlsl" != hlfn {
continue
}
buf, err := os.ReadFile(hlfn)
if err != nil {
fmt.Println(err)
continue
}
exsl = append(exsl, []byte(fmt.Sprintf("\n// from file: %s\n", hlfn))...)
exsl = append(exsl, buf...)
gosls[fn] = exsl
needsCompile[fn] = true // assume any standalone has main
break
}
upfn := strings.ToUpper(fn)
once := fmt.Sprintf("#ifndef __%s_HLSL__\n#define __%s_HLSL__\n\n", upfn, upfn)
exsl = append([]byte(once), exsl...)
oncend := fmt.Sprintf("#endif // __%s_HLSL__\n", upfn)
exsl = append(exsl, []byte(oncend)...)
slfn := filepath.Join(*outDir, fn+".hlsl")
ioutil.WriteFile(slfn, exsl, 0644)
}
// check for hlsl files that had no go equivalent
for _, hlfn := range hlslFiles {
hasGo := false
for fn := range gosls {
if fn+".hlsl" == hlfn {
hasGo = true
break
}
}
if hasGo {
continue
}
_, hlfno := filepath.Split(hlfn) // could be in a subdir
tofn := filepath.Join(*outDir, hlfno)
CopyFile(hlfn, tofn)
fn := strings.TrimSuffix(hlfno, ".hlsl")
needsCompile[fn] = true // assume any standalone hlsl is a main
}
for fn := range needsCompile {
CompileFile(fn + ".hlsl")
}
return gosls, nil
}
func CompileFile(fn string) error {
ext := filepath.Ext(fn)
ofn := fn[:len(fn)-len(ext)] + ".spv"
// todo: figure out how to use 1.2 here -- see bug issue #1
// cmd := exec.Command("glslc", "-fshader-stage=compute", "-O", "--target-env=vulkan1.1", "-o", ofn, fn)
// dxc is the reference compiler for hlsl!
cmd := exec.Command("dxc", "-spirv", "-O3", "-T", "cs_6_0", "-E", "main", "-Fo", ofn, fn)
cmd.Dir, _ = filepath.Abs(*outDir)
out, err := cmd.CombinedOutput()
fmt.Printf("\n-----------------------------------------------------\ndxc output for: %s\n%s", fn, out)
if err != nil {
log.Println(err)
return err
}
return nil
}