uniform.go 1.5 KB

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