shader.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package al
  2. /*
  3. #include <stdlib.h>
  4. #include <allegro5/allegro.h>
  5. #include "helpers.h"
  6. */
  7. import "C"
  8. // import "unsafe"
  9. import "runtime"
  10. type ShaderType C.ALLEGRO_SHADER_TYPE
  11. const (
  12. VertexShader = ShaderType(C.ALLEGRO_VERTEX_SHADER)
  13. PixelShader = ShaderType(C.ALLEGRO_PIXEL_SHADER)
  14. )
  15. func (st ShaderType) toC() C.ALLEGRO_SHADER_TYPE {
  16. return (C.ALLEGRO_SHADER_TYPE)(st)
  17. }
  18. type ShaderPlatform C.ALLEGRO_SHADER_PLATFORM
  19. const (
  20. ShaderAuto = ShaderPlatform(C.ALLEGRO_SHADER_AUTO)
  21. ShaderGLSL = ShaderPlatform(C.ALLEGRO_SHADER_GLSL)
  22. ShaderHLSL = ShaderPlatform(C.ALLEGRO_SHADER_HLSL)
  23. )
  24. func (sp ShaderPlatform) toC() C.ALLEGRO_SHADER_PLATFORM {
  25. return (C.ALLEGRO_SHADER_PLATFORM)(sp)
  26. }
  27. /* Shader variable names */
  28. const (
  29. ShaderVarColor = "al_color"
  30. ShaderVarPos = "al_pos"
  31. ShaderVarProjviewMatrix = "al_projview_matrix"
  32. ShaderVarTex = "al_tex"
  33. ShaderVarTextcoord = "al_texcoord"
  34. ShaderVarTextMatrix = "al_tex_matrix"
  35. ShaderVarUserAttr = "al_user_attr_"
  36. ShaderVarUseTex = "al_use_tex"
  37. ShaderVarUseTexMatrix = "al_use_tex_matrix"
  38. )
  39. type Shader struct {
  40. handle * C.ALLEGRO_SHADER
  41. }
  42. // Converts a shader to it's underlying C pointer
  43. func (self * Shader) toC() *C.ALLEGRO_SHADER {
  44. return (*C.ALLEGRO_SHADER)(self.handle)
  45. }
  46. // Destroys the shader.
  47. func (self *Shader) Destroy() {
  48. if self.handle != nil {
  49. C.al_destroy_shader(self.toC())
  50. }
  51. self.handle = nil
  52. }
  53. // Wraps a C shader into a go shader
  54. func wrapShaderRaw(data *C.ALLEGRO_SHADER) *Shader {
  55. if data == nil {
  56. return nil
  57. }
  58. return &Shader{data}
  59. }
  60. // Sets up a finalizer for this Shader that calls Destroy()
  61. func (self *Shader) SetDestroyFinalizer() *Shader {
  62. if self != nil {
  63. runtime.SetFinalizer(self, func(me *Shader) { me.Destroy() })
  64. }
  65. return self
  66. }
  67. // Wraps a C shader into a go shader and sets up a finalizer that calls Destroy()
  68. func wrapShader(data *C.ALLEGRO_SHADER) *Shader {
  69. self := wrapShaderRaw(data)
  70. return self.SetDestroyFinalizer()
  71. }
  72. func (sp ShaderPlatform) Create() *Shader {
  73. return wrapShader(C.al_create_shader(sp.toC()))
  74. }
  75. func (sh * Shader) AttachSource(shatype ShaderType, source string) bool {
  76. csource := cstr(source); defer cstrFree(csource)
  77. return bool(C.al_attach_shader_source(sh.toC(), shatype.toC(), csource))
  78. }
  79. func (sh * Shader) AttachSourceFile(shatype ShaderType, source string) bool {
  80. csource := cstr(source); defer cstrFree(csource)
  81. return bool(C.al_attach_shader_source_file(sh.toC(), shatype.toC(), csource))
  82. }
  83. func (sh * Shader) Build() bool {
  84. return bool(C.al_build_shader(sh.toC()))
  85. }
  86. func (sh * Shader) Log() string {
  87. return C.GoString(C.al_get_shader_log(sh.toC()))
  88. }
  89. func (sh * Shader) Platform() ShaderPlatform {
  90. return ShaderPlatform(C.al_get_shader_platform(sh.toC()))
  91. }
  92. func (sh * Shader) Use() bool {
  93. return bool(C.al_use_shader(sh.toC()))
  94. }
  95. func SetShaderSampler(name string, bitmap * Bitmap, unit int) bool {
  96. cname := cstr(name); defer cstrFree(cname)
  97. return bool(C.al_set_shader_sampler(cname, bitmap.toC(), C.int(unit)))
  98. }
  99. /*
  100. func (sh * Shader) SetMatrix(name string, matrix * Matrix) bool {
  101. cname := cstr(name); defer cstrFree(cname)
  102. return bool(C.al_set_shader_matrix(sh.toC(), cname, matrix.toC()))
  103. }
  104. */
  105. func SetShaderInt(name string, i int) bool {
  106. cname := cstr(name); defer cstrFree(cname)
  107. return bool(C.al_set_shader_int(cname, C.int(i)))
  108. }
  109. func SetShaderFloat(name string, f float32) bool {
  110. cname := cstr(name); defer cstrFree(cname)
  111. return bool(C.al_set_shader_float(cname, C.float(f)))
  112. }
  113. func SetShaderBool(name string, b bool) bool {
  114. cname := cstr(name); defer cstrFree(cname)
  115. return bool(C.al_set_shader_bool(cname, C.bool(b)))
  116. }
  117. func (sh * Shader) SetIntVector(name string, nc int, i []int) bool {
  118. cname := cstr(name); defer cstrFree(cname)
  119. csize, cvec := CInts(i) ; defer CIntsFree(csize, cvec)
  120. /* XXX, I doubt this will work for nc > 1 ... */
  121. return bool(C.al_set_shader_int_vector(cname, C.int(nc), cvec, csize / C.int(nc)))
  122. }
  123. func (sh * Shader) SetFloatVector(name string, nc int, f []float32) bool {
  124. cname := cstr(name); defer cstrFree(cname)
  125. csize, cvec := CFloats(f) ; defer CFloatsFree(csize, cvec)
  126. /* XXX, I doubt this will work for nc > 1 ... */
  127. return bool(C.al_set_shader_float_vector(cname, C.int(nc), cvec, csize / C.int(nc)))
  128. }
  129. func DefaultShaderSource(pla ShaderPlatform, ty ShaderType) string {
  130. return C.GoString(C.al_get_default_shader_source(pla.toC(), ty.toC()))
  131. }