main.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "bytes"
  7. "flag"
  8. "fmt"
  9. "go/format"
  10. "io/ioutil"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "text/template"
  15. )
  16. const (
  17. PROGNAME = "g3nshaders"
  18. VMAJOR = 0
  19. VMINOR = 1
  20. SHADEREXT = ".glsl"
  21. DIR_INCLUDE = "include"
  22. TYPE_VERTEX = "vertex"
  23. TYPE_FRAGMENT = "fragment"
  24. TYPE_GEOMETRY = "geometry"
  25. )
  26. //
  27. // Go template to generate the output file with the shaders' sources and
  28. // maps describing the include and shader names and programs shaders.
  29. //
  30. const TEMPLATE = `// File generated by G3NSHADERS. Do not edit.
  31. // To regenerate this file install 'g3nshaders' and execute:
  32. // 'go generate' in this folder.
  33. package {{.Pkg}}
  34. {{range .Includes}}
  35. const include_{{.Name}}_source = ` + "`{{.Source}}`" + `
  36. {{end}}
  37. {{range .Shaders}}
  38. const {{.Name}}_source = ` + "`{{.Source}}`" + `
  39. {{end}}
  40. // Maps include name with its source code
  41. var includeMap = map[string]string {
  42. {{range .Includes}}
  43. "{{- .Name}}": include_{{.Name}}_source, {{end}}
  44. }
  45. // Maps shader name with its source code
  46. var shaderMap = map[string]string {
  47. {{range .Shaders}}
  48. "{{- .Name}}": {{.Name}}_source, {{end}}
  49. }
  50. // Maps program name with Proginfo struct with shaders names
  51. var programMap = map[string]ProgramInfo{
  52. {{ range $progName, $progInfo := .Programs }}
  53. "{{$progName}}": { "{{$progInfo.Vertex}}","{{$progInfo.Fragment}}","{{$progInfo.Geometry}}" }, {{end}}
  54. }
  55. `
  56. // Command line options
  57. var (
  58. oVersion = flag.Bool("version", false, "Show version and exits")
  59. oInp = flag.String("in", ".", "Input directory")
  60. oOut = flag.String("out", "sources.go", "Go output file")
  61. oPackage = flag.String("pkg", "shaders", "Package name")
  62. oVerbose = flag.Bool("v", false, "Show files being processed")
  63. )
  64. // Valid shader types
  65. var shaderTypes = map[string]bool{
  66. TYPE_VERTEX: true,
  67. TYPE_FRAGMENT: true,
  68. TYPE_GEOMETRY: true,
  69. }
  70. // fileInfo describes a shader or include file name and source code
  71. type fileInfo struct {
  72. Name string // shader or include name
  73. Source string // shader or include source code
  74. Include bool // true if include, false otherwise
  75. }
  76. // progInfo describes all the shader names of an specific program
  77. // If the program doesn't use the geometry shader it is set as an empty string
  78. type progInfo struct {
  79. Vertex string // vertex shader name
  80. Fragment string // fragment shader name
  81. Geometry string // geometry shader name
  82. }
  83. // templInfo contains all information needed for the template expansion
  84. type templInfo struct {
  85. Pkg string
  86. Includes []fileInfo
  87. Shaders []fileInfo
  88. Programs map[string]progInfo
  89. }
  90. var templData templInfo
  91. func main() {
  92. // Parse command line parameters
  93. flag.Usage = usage
  94. flag.Parse()
  95. // If requested, print version and exits
  96. if *oVersion == true {
  97. fmt.Fprintf(os.Stderr, "%s v%d.%d\n", PROGNAME, VMAJOR, VMINOR)
  98. return
  99. }
  100. // Initialize template data
  101. templData.Pkg = *oPackage
  102. templData.Programs = make(map[string]progInfo)
  103. // Process the current directory and its subdirectories recursively
  104. // appending information into templData
  105. processDir(*oInp, false)
  106. // Generates output file from TEMPLATE
  107. generate(*oOut)
  108. }
  109. // processDir processes recursively all shaders files in the specified directory
  110. func processDir(dir string, include bool) {
  111. // Open directory
  112. f, err := os.Open(dir)
  113. if err != nil {
  114. panic(err)
  115. }
  116. defer f.Close()
  117. // Read all fileinfos
  118. finfos, err := f.Readdir(0)
  119. if err != nil {
  120. panic(err)
  121. }
  122. // Process subdirectory recursively or process file
  123. for _, fi := range finfos {
  124. if fi.IsDir() {
  125. dirInclude := include
  126. if fi.Name() == DIR_INCLUDE {
  127. dirInclude = true
  128. }
  129. processDir(filepath.Join(dir, fi.Name()), dirInclude)
  130. } else {
  131. processFile(filepath.Join(dir, fi.Name()), include)
  132. }
  133. }
  134. }
  135. // processFile process one file checking if it has the shaders extension,
  136. // otherwise it is ignored.
  137. // If the include flag is true the file is an include file otherwise it
  138. // it a shader
  139. func processFile(file string, include bool) {
  140. // Ignore file if it has not the shader extension
  141. fext := filepath.Ext(file)
  142. if fext != SHADEREXT {
  143. if *oVerbose {
  144. fmt.Printf("Ignored file (not shader): %s\n", file)
  145. }
  146. return
  147. }
  148. // Get the file base name and its name with the extension
  149. fbase := filepath.Base(file)
  150. fname := fbase[:len(fbase)-len(fext)]
  151. // If not in include directory, the file must be a shader program
  152. // which name must have the format: <name>_<shader_type>
  153. if !include {
  154. parts := strings.Split(string(fname), "_")
  155. if len(parts) < 2 {
  156. fmt.Printf("Ignored file (INVALID NAME): %s\n", file)
  157. return
  158. }
  159. stype := parts[len(parts)-1]
  160. if !shaderTypes[stype] {
  161. fmt.Printf("Ignored file (INVALID SHADER TYPE): %s\n", file)
  162. return
  163. }
  164. sname := strings.Join(parts[:len(parts)-1], "_")
  165. pinfo, ok := templData.Programs[sname]
  166. if !ok {
  167. templData.Programs[sname] = pinfo
  168. }
  169. switch stype {
  170. case TYPE_VERTEX:
  171. pinfo.Vertex = fname
  172. case TYPE_FRAGMENT:
  173. pinfo.Fragment = fname
  174. case TYPE_GEOMETRY:
  175. pinfo.Geometry = fname
  176. }
  177. templData.Programs[sname] = pinfo
  178. }
  179. // Reads all file data
  180. f, err := os.Open(file)
  181. if err != nil {
  182. panic(err)
  183. }
  184. defer f.Close()
  185. data, err := ioutil.ReadAll(f)
  186. if err != nil {
  187. panic(err)
  188. }
  189. // Appends entry in Includes or Shaders
  190. if include {
  191. templData.Includes = append(templData.Includes, fileInfo{
  192. Name: fname,
  193. Source: string(data),
  194. })
  195. } else {
  196. templData.Shaders = append(templData.Shaders, fileInfo{
  197. Name: fname,
  198. Source: string(data),
  199. })
  200. }
  201. if *oVerbose {
  202. fmt.Printf("%s (%v bytes)\n", file, len(data))
  203. }
  204. }
  205. // generate generates output go file with shaders sources from TEMPLATE
  206. func generate(file string) {
  207. // Parses the template
  208. tmpl := template.New("tmpl")
  209. tmpl, err := tmpl.Parse(TEMPLATE)
  210. if err != nil {
  211. panic(err)
  212. }
  213. // Expands template to buffer
  214. var buf bytes.Buffer
  215. err = tmpl.Execute(&buf, &templData)
  216. if err != nil {
  217. panic(err)
  218. }
  219. // Formats buffer as Go source
  220. p, err := format.Source(buf.Bytes())
  221. if err != nil {
  222. panic(err)
  223. }
  224. // Writes formatted source to output file
  225. f, err := os.Create(file)
  226. if err != nil {
  227. panic(err)
  228. }
  229. f.Write(p)
  230. f.Close()
  231. }
  232. // usage shows the application usage
  233. func usage() {
  234. fmt.Fprintf(os.Stderr, "%s v%d.%d\n", PROGNAME, VMAJOR, VMINOR)
  235. fmt.Fprintf(os.Stderr, "usage: %s [options]\n", strings.ToLower(PROGNAME))
  236. flag.PrintDefaults()
  237. os.Exit(2)
  238. }