shaders.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 shaders contains the several shaders used by the engine
  5. package shaders
  6. // Generates shaders sources from this directory and include directory *.glsl files
  7. //go:generate g3nshaders -in=. -out=sources.go -pkg=shaders -v
  8. // ProgramInfo contains information for a registered shader program
  9. type ProgramInfo struct {
  10. Vertex string // Vertex shader name
  11. Fragment string // Fragment shader name
  12. Geometry string // Geometry shader name (optional)
  13. }
  14. // AddInclude adds a chunk of shader code to the default shaders registry
  15. // which can be included in a shader using the "#include <name>" directive
  16. func AddInclude(name string, source string) {
  17. if len(name) == 0 || len(source) == 0 {
  18. panic("Invalid include name and/or source")
  19. }
  20. includeMap[name] = source
  21. }
  22. // AddShader add a shader to default shaders registry.
  23. // The specified name can be used when adding programs to the registry
  24. func AddShader(name string, source string) {
  25. if len(name) == 0 || len(source) == 0 {
  26. panic("Invalid shader name and/or source")
  27. }
  28. shaderMap[name] = source
  29. }
  30. // AddProgram adds a shader program to the default registry of programs.
  31. // Currently up to 3 shaders: vertex, fragment and geometry (optional) can be specified.
  32. func AddProgram(name string, vertex string, frag string, others ...string) {
  33. if len(name) == 0 || len(vertex) == 0 || len(frag) == 0 {
  34. panic("Program and/or shader name empty")
  35. }
  36. if shaderMap[vertex] == "" {
  37. panic("Invalid vertex shader name")
  38. }
  39. if shaderMap[frag] == "" {
  40. panic("Invalid vertex shader name")
  41. }
  42. var geom = ""
  43. if len(others) > 0 {
  44. geom = others[0]
  45. if shaderMap[geom] == "" {
  46. panic("Invalid geometry shader name")
  47. }
  48. }
  49. programMap[name] = ProgramInfo{
  50. Vertex: vertex,
  51. Fragment: frag,
  52. Geometry: geom,
  53. }
  54. }
  55. // Includes returns list with the names of all include chunks currently in the default shaders registry.
  56. func Includes() []string {
  57. list := make([]string, 0)
  58. for name := range includeMap {
  59. list = append(list, name)
  60. }
  61. return list
  62. }
  63. // IncludeSource returns the source code of the specified shader include chunk.
  64. // If the name is not found an empty string is returned.
  65. func IncludeSource(name string) string {
  66. return includeMap[name]
  67. }
  68. // Shaders returns list with the names of all shaders currently in the default shaders registry.
  69. func Shaders() []string {
  70. list := make([]string, 0)
  71. for name := range shaderMap {
  72. list = append(list, name)
  73. }
  74. return list
  75. }
  76. // ShaderSource returns the source code of the specified shader in the default shaders registry.
  77. // If the name is not found an empty string is returned
  78. func ShaderSource(name string) string {
  79. return shaderMap[name]
  80. }
  81. // Programs returns list with the names of all programs currently in the default shaders registry.
  82. func Programs() []string {
  83. list := make([]string, 0)
  84. for name := range programMap {
  85. list = append(list, name)
  86. }
  87. return list
  88. }
  89. // GetProgramInfo returns ProgramInfo struct for the specified program name
  90. // in the default shaders registry
  91. func GetProgramInfo(name string) ProgramInfo {
  92. return programMap[name]
  93. }