Shader.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2022, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibGL/GLContext.h>
  9. namespace GL {
  10. GLuint GLContext::gl_create_shader(GLenum shader_type)
  11. {
  12. // FIXME: Add support for GL_COMPUTE_SHADER, GL_TESS_CONTROL_SHADER, GL_TESS_EVALUATION_SHADER and GL_GEOMETRY_SHADER.
  13. RETURN_VALUE_WITH_ERROR_IF(shader_type != GL_VERTEX_SHADER
  14. && shader_type != GL_FRAGMENT_SHADER,
  15. GL_INVALID_ENUM,
  16. 0);
  17. GLuint shader_name;
  18. m_shader_name_allocator.allocate(1, &shader_name);
  19. auto shader = Shader::create(shader_type);
  20. m_allocated_shaders.set(shader_name, shader);
  21. return shader_name;
  22. }
  23. void GLContext::gl_delete_shader(GLuint shader)
  24. {
  25. // "A value of 0 for shader will be silently ignored." (https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDeleteShader.xhtml)
  26. if (shader == 0)
  27. return;
  28. auto it = m_allocated_shaders.find(shader);
  29. RETURN_WITH_ERROR_IF(it == m_allocated_shaders.end(), GL_INVALID_VALUE);
  30. // FIXME: According to the spec, we should only flag the shader for deletion here and delete it once it is detached from all programs.
  31. m_allocated_shaders.remove(it);
  32. m_shader_name_allocator.free(shader);
  33. }
  34. void GLContext::gl_shader_source(GLuint shader, GLsizei count, GLchar const** string, GLint const* length)
  35. {
  36. auto it = m_allocated_shaders.find(shader);
  37. // FIXME: implement check "GL_INVALID_VALUE is generated if shader is not a value generated by OpenGL."
  38. RETURN_WITH_ERROR_IF(it == m_allocated_shaders.end(), GL_INVALID_OPERATION);
  39. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  40. it->value->clear_sources();
  41. for (int i = 0; i < count; i++) {
  42. if (length == nullptr || length[i] < 0) {
  43. auto result = it->value->add_source(StringView(string[i], strlen(string[i])));
  44. RETURN_WITH_ERROR_IF(result.is_error() && result.error().is_errno() && result.error().code() == ENOMEM, GL_OUT_OF_MEMORY);
  45. RETURN_WITH_ERROR_IF(result.is_error(), GL_INVALID_OPERATION);
  46. } else {
  47. auto result = it->value->add_source(StringView(string[i], length[i]));
  48. RETURN_WITH_ERROR_IF(result.is_error() && result.error().is_errno() && result.error().code() == ENOMEM, GL_OUT_OF_MEMORY);
  49. RETURN_WITH_ERROR_IF(result.is_error(), GL_INVALID_OPERATION);
  50. }
  51. }
  52. }
  53. void GLContext::gl_compile_shader(GLuint shader)
  54. {
  55. dbgln("gl_compile_shader({}) unimplemented ", shader);
  56. TODO();
  57. }
  58. GLuint GLContext::gl_create_program()
  59. {
  60. GLuint program_name;
  61. m_program_name_allocator.allocate(1, &program_name);
  62. auto program = Program::create();
  63. m_allocated_programs.set(program_name, program);
  64. return program_name;
  65. }
  66. void GLContext::gl_delete_program(GLuint program)
  67. {
  68. // "A value of 0 for program will be silently ignored." (https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDeleteProgram.xhtml)
  69. if (program == 0)
  70. return;
  71. auto it = m_allocated_programs.find(program);
  72. RETURN_WITH_ERROR_IF(it == m_allocated_programs.end(), GL_INVALID_VALUE);
  73. // FIXME: According to the spec, we should only flag the program for deletion here and delete it once it is not used anymore.
  74. m_allocated_programs.remove(it);
  75. m_program_name_allocator.free(program);
  76. }
  77. void GLContext::gl_attach_shader(GLuint program, GLuint shader)
  78. {
  79. dbgln("gl_attach_shader({}, {}) unimplemented ", program, shader);
  80. TODO();
  81. }
  82. void GLContext::gl_link_program(GLuint program)
  83. {
  84. dbgln("gl_link_program({}) unimplemented ", program);
  85. TODO();
  86. }
  87. }