uniform.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 gls
  5. import (
  6. "fmt"
  7. )
  8. type Uniform struct {
  9. name string // base name
  10. nameIdx string // cached indexed name
  11. handle uint32 // program handle
  12. location int32 // last cached location
  13. lastIndex int32 // last index
  14. }
  15. // Init initializes this uniform location cache and sets its name
  16. func (u *Uniform) Init(name string) {
  17. u.name = name
  18. u.handle = 0 // invalid program handle
  19. u.location = -1 // invalid location
  20. u.lastIndex = -1 // invalid index
  21. }
  22. // Name returns the uniform name.
  23. func (u *Uniform) Name() string {
  24. return u.name
  25. }
  26. // Location returns the location of this uniform for the current shader program.
  27. // The returned location can be -1 if not found.
  28. func (u *Uniform) Location(gs *GLS) int32 {
  29. handle := gs.prog.Handle()
  30. if handle != u.handle {
  31. u.location = gs.prog.GetUniformLocation(u.name)
  32. u.handle = handle
  33. }
  34. return u.location
  35. }
  36. // LocationIdx returns the location of this indexed uniform for the current shader program.
  37. // The returned location can be -1 if not found.
  38. func (u *Uniform) LocationIdx(gs *GLS, idx int32) int32 {
  39. if idx != u.lastIndex {
  40. u.nameIdx = fmt.Sprintf("%s[%d]", u.name, idx)
  41. u.lastIndex = idx
  42. u.handle = 0
  43. }
  44. handle := gs.prog.Handle()
  45. if handle != u.handle {
  46. u.location = gs.prog.GetUniformLocation(u.nameIdx)
  47. u.handle = handle
  48. }
  49. return u.location
  50. }