Shader.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. auto program_it = m_allocated_programs.find(program);
  83. auto shader_it = m_allocated_shaders.find(shader);
  84. // FIXME: implement check "GL_INVALID_VALUE is generated if either program or shader is not a value generated by OpenGL."
  85. RETURN_WITH_ERROR_IF(program_it == m_allocated_programs.end(), GL_INVALID_OPERATION);
  86. RETURN_WITH_ERROR_IF(shader_it == m_allocated_shaders.end(), GL_INVALID_OPERATION);
  87. // NOTE: attach_result is Error if the shader is already attached to this program
  88. auto attach_result = program_it->value->attach_shader(*shader_it->value);
  89. RETURN_WITH_ERROR_IF(attach_result.is_error() && attach_result.error().is_errno() && attach_result.error().code() == ENOMEM, GL_OUT_OF_MEMORY);
  90. RETURN_WITH_ERROR_IF(attach_result.is_error(), GL_INVALID_OPERATION);
  91. }
  92. void GLContext::gl_link_program(GLuint program)
  93. {
  94. auto program_it = m_allocated_programs.find(program);
  95. // FIXME: implement check "GL_INVALID_VALUE is generated if program is not a value generated by OpenGL."
  96. RETURN_WITH_ERROR_IF(program_it == m_allocated_programs.end(), GL_INVALID_OPERATION);
  97. // FIXME: implement check "GL_INVALID_OPERATION is generated if program is the currently active program object and transform feedback mode is active."
  98. // NOTE: We are ignoring the link result since this is tracked inside the program object
  99. (void)program_it->value->link();
  100. }
  101. void GLContext::gl_use_program(GLuint program)
  102. {
  103. if (program == 0) {
  104. m_current_program = nullptr;
  105. return;
  106. }
  107. auto it = m_allocated_programs.find(program);
  108. // FIXME: implement check "GL_INVALID_VALUE is generated if program is not a value generated by OpenGL."
  109. RETURN_WITH_ERROR_IF(it == m_allocated_programs.end(), GL_INVALID_OPERATION);
  110. // FIXME: implement check "GL_INVALID_OPERATION is generated if transform feedback mode is active."
  111. RETURN_WITH_ERROR_IF(it->value->link_status() != true, GL_INVALID_OPERATION);
  112. m_current_program = it->value;
  113. }
  114. }