Shader.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. auto it = m_allocated_shaders.find(shader);
  56. // FIXME: implement check "GL_INVALID_VALUE is generated if shader is not a value generated by OpenGL."
  57. RETURN_WITH_ERROR_IF(it == m_allocated_shaders.end(), GL_INVALID_OPERATION);
  58. // NOTE: We are ignoring the compilation result here since it is tracked inside the shader object
  59. (void)it->value->compile();
  60. }
  61. GLuint GLContext::gl_create_program()
  62. {
  63. GLuint program_name;
  64. m_program_name_allocator.allocate(1, &program_name);
  65. auto program = Program::create();
  66. m_allocated_programs.set(program_name, program);
  67. return program_name;
  68. }
  69. void GLContext::gl_delete_program(GLuint program)
  70. {
  71. // "A value of 0 for program will be silently ignored." (https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDeleteProgram.xhtml)
  72. if (program == 0)
  73. return;
  74. auto it = m_allocated_programs.find(program);
  75. RETURN_WITH_ERROR_IF(it == m_allocated_programs.end(), GL_INVALID_VALUE);
  76. // FIXME: According to the spec, we should only flag the program for deletion here and delete it once it is not used anymore.
  77. m_allocated_programs.remove(it);
  78. m_program_name_allocator.free(program);
  79. }
  80. void GLContext::gl_attach_shader(GLuint program, GLuint shader)
  81. {
  82. dbgln("gl_attach_shader({}, {}) unimplemented ", program, shader);
  83. TODO();
  84. }
  85. void GLContext::gl_link_program(GLuint program)
  86. {
  87. dbgln("gl_link_program({}) unimplemented ", program);
  88. TODO();
  89. }
  90. }