uniform.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 Uniform2 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 *Uniform2) 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. // Location returns the location of this uniform for
  23. // the current shader program
  24. // The returned location can be -1 if not found
  25. func (u *Uniform2) Location(gs *GLS) int32 {
  26. handle := gs.prog.Handle()
  27. if handle != u.handle {
  28. u.location = gs.prog.GetUniformLocation(u.name)
  29. u.handle = handle
  30. //log.Error("Uniform:%s location:%v", u.name, u.location)
  31. }
  32. return u.location
  33. }
  34. // LocationIdx returns the location of this indexed uniform for
  35. // the current shader program
  36. // The returned location can be -1 if not found
  37. func (u *Uniform2) LocationIdx(gs *GLS, idx int32) int32 {
  38. if idx != u.lastIndex {
  39. u.nameIdx = fmt.Sprintf("%s[%d]", u.name, idx)
  40. u.lastIndex = idx
  41. u.handle = 0
  42. //log.Error("Uniform:%s rebuild nameIdx", u.nameIdx)
  43. }
  44. handle := gs.prog.Handle()
  45. if handle != u.handle {
  46. u.location = gs.prog.GetUniformLocation(u.nameIdx)
  47. u.handle = handle
  48. //log.Error("Uniform:%s location:%v", u.nameIdx, u.location)
  49. }
  50. return u.location
  51. }