Program.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Format.h>
  8. #include <LibAccelGfx/GL.h>
  9. #include <LibAccelGfx/Program.h>
  10. namespace AccelGfx {
  11. Optional<GL::Program> programs_cache[to_underlying(Program::Name::ProgramCount)];
  12. Program Program::create(Name name, char const* vertex_shader_source, char const* fragment_shader_source)
  13. {
  14. if (programs_cache[to_underlying(name)].has_value()) {
  15. return Program { *programs_cache[to_underlying(name)] };
  16. }
  17. auto vertex_shader = GL::create_shader(GL::ShaderType::Vertex, vertex_shader_source);
  18. auto fragment_shader = GL::create_shader(GL::ShaderType::Fragment, fragment_shader_source);
  19. auto program = GL::create_program(vertex_shader, fragment_shader);
  20. programs_cache[to_underlying(name)] = program;
  21. return Program { program };
  22. }
  23. void Program::use()
  24. {
  25. GL::use_program(m_program);
  26. }
  27. GL::VertexAttribute Program::get_attribute_location(char const* name)
  28. {
  29. return GL::get_attribute_location(m_program, name);
  30. }
  31. GL::Uniform Program::get_uniform_location(char const* name)
  32. {
  33. return GL::get_uniform_location(m_program, name);
  34. }
  35. }