ShaderProgramUniformVariable.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "anki/resource/ShaderProgramUniformVariable.h"
  2. #include "anki/resource/ShaderProgram.h"
  3. #include "anki/resource/Texture.h"
  4. #include "anki/gl/GlStateMachine.h"
  5. namespace anki {
  6. //==============================================================================
  7. void ShaderProgramUniformVariable::doSanityChecks() const
  8. {
  9. ANKI_ASSERT(getLocation() != -1);
  10. ANKI_ASSERT(GlStateMachineSingleton::get().getCurrentProgramGlId() ==
  11. getFatherSProg().getGlId());
  12. ANKI_ASSERT(glGetUniformLocation(getFatherSProg().getGlId(),
  13. getName().c_str()) == getLocation());
  14. }
  15. //==============================================================================
  16. // set uniforms =
  17. //==============================================================================
  18. void ShaderProgramUniformVariable::set(const float x) const
  19. {
  20. doSanityChecks();
  21. ANKI_ASSERT(getGlDataType() == GL_FLOAT);
  22. glUniform1f(getLocation(), x);
  23. }
  24. void ShaderProgramUniformVariable::set(const Vec2& x) const
  25. {
  26. glUniform2f(getLocation(), x.x(), x.y());
  27. }
  28. void ShaderProgramUniformVariable::set(const float x[], uint size) const
  29. {
  30. doSanityChecks();
  31. ANKI_ASSERT(getGlDataType() == GL_FLOAT);
  32. ANKI_ASSERT(size > 1);
  33. glUniform1fv(getLocation(), size, x);
  34. }
  35. void ShaderProgramUniformVariable::set(const Vec2 x[], uint size) const
  36. {
  37. doSanityChecks();
  38. ANKI_ASSERT(getGlDataType() == GL_FLOAT_VEC2);
  39. ANKI_ASSERT(size > 1);
  40. glUniform2fv(getLocation(), size, &(const_cast<Vec2&>(x[0]))[0]);
  41. }
  42. void ShaderProgramUniformVariable::set(const Vec3 x[], uint size) const
  43. {
  44. doSanityChecks();
  45. ANKI_ASSERT(getGlDataType() == GL_FLOAT_VEC3);
  46. ANKI_ASSERT(size > 0);
  47. glUniform3fv(getLocation(), size, &(const_cast<Vec3&>(x[0]))[0]);
  48. }
  49. void ShaderProgramUniformVariable::set(const Vec4 x[], uint size) const
  50. {
  51. doSanityChecks();
  52. ANKI_ASSERT(getGlDataType() == GL_FLOAT_VEC4);
  53. ANKI_ASSERT(size > 0);
  54. glUniform4fv(getLocation(), size, &(const_cast<Vec4&>(x[0]))[0]);
  55. }
  56. void ShaderProgramUniformVariable::set(const Mat3 x[], uint size) const
  57. {
  58. doSanityChecks();
  59. ANKI_ASSERT(getGlDataType() == GL_FLOAT_MAT3);
  60. ANKI_ASSERT(size > 0);
  61. glUniformMatrix3fv(getLocation(), size, true, &(x[0])[0]);
  62. }
  63. void ShaderProgramUniformVariable::set(const Mat4 x[], uint size) const
  64. {
  65. doSanityChecks();
  66. ANKI_ASSERT(getGlDataType() == GL_FLOAT_MAT4);
  67. ANKI_ASSERT(size > 0);
  68. glUniformMatrix4fv(getLocation(), size, true, &(x[0])[0]);
  69. }
  70. void ShaderProgramUniformVariable::set(const Texture& tex, uint texUnit) const
  71. {
  72. doSanityChecks();
  73. ANKI_ASSERT(getGlDataType() == GL_SAMPLER_2D ||
  74. getGlDataType() == GL_SAMPLER_2D_SHADOW);
  75. tex.bind(texUnit);
  76. glUniform1i(getLocation(), texUnit);
  77. }
  78. } // end namespace