Shader.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2022, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <LibGL/Shaders/Shader.h>
  8. #include <LibGLSL/Compiler.h>
  9. namespace GL {
  10. NonnullRefPtr<Shader> Shader::create(GLenum shader_type)
  11. {
  12. return adopt_ref(*new Shader(shader_type));
  13. }
  14. ErrorOr<void> Shader::add_source(StringView source_code)
  15. {
  16. auto source_code_content = TRY(String::from_utf8(source_code));
  17. TRY(m_sources.try_append(move(source_code_content)));
  18. return {};
  19. }
  20. ErrorOr<void> Shader::compile()
  21. {
  22. m_info_log = String {};
  23. GLSL::Compiler compiler;
  24. auto object_file_or_error = compiler.compile(m_sources);
  25. if (object_file_or_error.is_error()) {
  26. m_compile_status = false;
  27. m_info_log = compiler.messages();
  28. return object_file_or_error.release_error();
  29. }
  30. m_object_file = object_file_or_error.release_value();
  31. m_compile_status = true;
  32. return {};
  33. }
  34. size_t Shader::info_log_length() const
  35. {
  36. if (!m_info_log.has_value())
  37. return 0;
  38. // Per the spec we return the size including the null terminator
  39. return m_info_log.value().bytes().size() + 1;
  40. }
  41. size_t Shader::combined_source_length() const
  42. {
  43. if (m_sources.is_empty())
  44. return 0;
  45. size_t combined_size = 0;
  46. for (auto source : m_sources)
  47. combined_size += source.bytes().size();
  48. // Per the spec we return the size including the null terminator
  49. return combined_size + 1;
  50. }
  51. }