Shader.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2022, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <LibGL/GLContext.h>
  8. namespace GL {
  9. GLuint GLContext::gl_create_shader(GLenum shader_type)
  10. {
  11. // FIXME: Add support for GL_COMPUTE_SHADER, GL_TESS_CONTROL_SHADER, GL_TESS_EVALUATION_SHADER and GL_GEOMETRY_SHADER.
  12. RETURN_VALUE_WITH_ERROR_IF(shader_type != GL_VERTEX_SHADER
  13. && shader_type != GL_FRAGMENT_SHADER,
  14. GL_INVALID_ENUM,
  15. 0);
  16. GLuint shader_name;
  17. m_shader_name_allocator.allocate(1, &shader_name);
  18. auto shader = Shader::create(shader_type);
  19. m_allocated_shaders.set(shader_name, shader);
  20. return shader_name;
  21. }
  22. void GLContext::gl_delete_shader(GLuint shader)
  23. {
  24. // "A value of 0 for shader will be silently ignored." (https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDeleteShader.xhtml)
  25. if (shader == 0)
  26. return;
  27. auto it = m_allocated_shaders.find(shader);
  28. RETURN_WITH_ERROR_IF(it == m_allocated_shaders.end(), GL_INVALID_VALUE);
  29. // FIXME: According to the spec, we should only flag the shader for deletion here and delete it once it is detached from all programs.
  30. m_allocated_shaders.remove(it);
  31. m_shader_name_allocator.free(shader);
  32. }
  33. void GLContext::gl_shader_source(GLuint shader, GLsizei count, GLchar const** string, GLint const* length)
  34. {
  35. auto it = m_allocated_shaders.find(shader);
  36. // FIXME: implement check "GL_INVALID_VALUE is generated if shader is not a value generated by OpenGL."
  37. RETURN_WITH_ERROR_IF(it == m_allocated_shaders.end(), GL_INVALID_OPERATION);
  38. RETURN_WITH_ERROR_IF(count < 0, GL_INVALID_VALUE);
  39. it->value->clear_sources();
  40. for (int i = 0; i < count; i++) {
  41. if (length == nullptr || length[i] < 0) {
  42. auto result = it->value->add_source(StringView(string[i], strlen(string[i])));
  43. RETURN_WITH_ERROR_IF(result.is_error() && result.error().is_errno() && result.error().code() == ENOMEM, GL_OUT_OF_MEMORY);
  44. RETURN_WITH_ERROR_IF(result.is_error(), GL_INVALID_OPERATION);
  45. } else {
  46. auto result = it->value->add_source(StringView(string[i], length[i]));
  47. RETURN_WITH_ERROR_IF(result.is_error() && result.error().is_errno() && result.error().code() == ENOMEM, GL_OUT_OF_MEMORY);
  48. RETURN_WITH_ERROR_IF(result.is_error(), GL_INVALID_OPERATION);
  49. }
  50. }
  51. }
  52. void GLContext::gl_compile_shader(GLuint shader)
  53. {
  54. auto it = m_allocated_shaders.find(shader);
  55. // FIXME: implement check "GL_INVALID_VALUE is generated if shader is not a value generated by OpenGL."
  56. RETURN_WITH_ERROR_IF(it == m_allocated_shaders.end(), GL_INVALID_OPERATION);
  57. // NOTE: We are ignoring the compilation result here since it is tracked inside the shader object
  58. (void)it->value->compile();
  59. }
  60. void GLContext::gl_get_shader(GLuint shader, GLenum pname, GLint* params)
  61. {
  62. RETURN_WITH_ERROR_IF(pname != GL_SHADER_TYPE
  63. && pname != GL_DELETE_STATUS
  64. && pname != GL_COMPILE_STATUS
  65. && pname != GL_INFO_LOG_LENGTH
  66. && pname != GL_SHADER_SOURCE_LENGTH,
  67. GL_INVALID_ENUM);
  68. // FIXME: implement check "GL_INVALID_VALUE is generated if shader is not a value generated by OpenGL."
  69. // FIXME: implement check "GL_INVALID_OPERATION is generated if pname is GL_COMPILE_STATUS, GL_INFO_LOG_LENGTH, or GL_SHADER_SOURCE_LENGTH but a shader compiler is not supported."
  70. auto it = m_allocated_shaders.find(shader);
  71. RETURN_WITH_ERROR_IF(it == m_allocated_shaders.end(), GL_INVALID_OPERATION);
  72. switch (pname) {
  73. case GL_SHADER_TYPE:
  74. *params = it->value->type();
  75. break;
  76. case GL_DELETE_STATUS:
  77. // FIXME: Return the actual delete status once we implement this missing feature
  78. *params = GL_FALSE;
  79. break;
  80. case GL_COMPILE_STATUS:
  81. *params = it->value->compile_status() ? GL_TRUE : GL_FALSE;
  82. break;
  83. case GL_INFO_LOG_LENGTH:
  84. *params = it->value->info_log_length();
  85. break;
  86. case GL_SHADER_SOURCE_LENGTH:
  87. *params = it->value->combined_source_length();
  88. break;
  89. default:
  90. VERIFY_NOT_REACHED();
  91. }
  92. }
  93. GLuint GLContext::gl_create_program()
  94. {
  95. GLuint program_name;
  96. m_program_name_allocator.allocate(1, &program_name);
  97. auto program = Program::create();
  98. m_allocated_programs.set(program_name, program);
  99. return program_name;
  100. }
  101. void GLContext::gl_delete_program(GLuint program)
  102. {
  103. // "A value of 0 for program will be silently ignored." (https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDeleteProgram.xhtml)
  104. if (program == 0)
  105. return;
  106. auto it = m_allocated_programs.find(program);
  107. RETURN_WITH_ERROR_IF(it == m_allocated_programs.end(), GL_INVALID_VALUE);
  108. // FIXME: According to the spec, we should only flag the program for deletion here and delete it once it is not used anymore.
  109. m_allocated_programs.remove(it);
  110. m_program_name_allocator.free(program);
  111. }
  112. void GLContext::gl_attach_shader(GLuint program, GLuint shader)
  113. {
  114. auto program_it = m_allocated_programs.find(program);
  115. auto shader_it = m_allocated_shaders.find(shader);
  116. // FIXME: implement check "GL_INVALID_VALUE is generated if either program or shader is not a value generated by OpenGL."
  117. RETURN_WITH_ERROR_IF(program_it == m_allocated_programs.end(), GL_INVALID_OPERATION);
  118. RETURN_WITH_ERROR_IF(shader_it == m_allocated_shaders.end(), GL_INVALID_OPERATION);
  119. // NOTE: attach_result is Error if the shader is already attached to this program
  120. auto attach_result = program_it->value->attach_shader(*shader_it->value);
  121. RETURN_WITH_ERROR_IF(attach_result.is_error() && attach_result.error().is_errno() && attach_result.error().code() == ENOMEM, GL_OUT_OF_MEMORY);
  122. RETURN_WITH_ERROR_IF(attach_result.is_error(), GL_INVALID_OPERATION);
  123. }
  124. void GLContext::gl_link_program(GLuint program)
  125. {
  126. auto program_it = m_allocated_programs.find(program);
  127. // FIXME: implement check "GL_INVALID_VALUE is generated if program is not a value generated by OpenGL."
  128. RETURN_WITH_ERROR_IF(program_it == m_allocated_programs.end(), GL_INVALID_OPERATION);
  129. // FIXME: implement check "GL_INVALID_OPERATION is generated if program is the currently active program object and transform feedback mode is active."
  130. // NOTE: We are ignoring the link result since this is tracked inside the program object
  131. (void)program_it->value->link(*m_rasterizer);
  132. }
  133. void GLContext::gl_use_program(GLuint program)
  134. {
  135. if (program == 0) {
  136. m_current_program = nullptr;
  137. return;
  138. }
  139. auto it = m_allocated_programs.find(program);
  140. // FIXME: implement check "GL_INVALID_VALUE is generated if program is not a value generated by OpenGL."
  141. RETURN_WITH_ERROR_IF(it == m_allocated_programs.end(), GL_INVALID_OPERATION);
  142. // FIXME: implement check "GL_INVALID_OPERATION is generated if transform feedback mode is active."
  143. RETURN_WITH_ERROR_IF(it->value->link_status() != true, GL_INVALID_OPERATION);
  144. m_current_program = it->value;
  145. }
  146. void GLContext::gl_get_program(GLuint program, GLenum pname, GLint* params)
  147. {
  148. auto it = m_allocated_programs.find(program);
  149. // FIXME: implement check "GL_INVALID_VALUE is generated if program is not a value generated by OpenGL."
  150. RETURN_WITH_ERROR_IF(it == m_allocated_programs.end(), GL_INVALID_OPERATION);
  151. // FIXME: implement check "GL_INVALID_OPERATION is generated if pname is GL_GEOMETRY_VERTICES_OUT, GL_GEOMETRY_INPUT_TYPE, or GL_GEOMETRY_OUTPUT_TYPE, and program does not contain a geometry shader."
  152. // FIXME: implement all the other allowed pname values (https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGetProgram.xhtml)
  153. RETURN_WITH_ERROR_IF(pname != GL_DELETE_STATUS
  154. && pname != GL_LINK_STATUS
  155. && pname != GL_INFO_LOG_LENGTH,
  156. GL_INVALID_ENUM);
  157. // FIXME: implement check "GL_INVALID_OPERATION is generated if pname is GL_COMPUTE_WORK_GROUP_SIZE and program does not contain a binary for the compute shader stage."
  158. switch (pname) {
  159. case GL_DELETE_STATUS:
  160. // FIXME: Return the actual delete status once we implement this missing feature
  161. *params = GL_FALSE;
  162. break;
  163. case GL_LINK_STATUS:
  164. *params = it->value->link_status() ? GL_TRUE : GL_FALSE;
  165. break;
  166. case GL_INFO_LOG_LENGTH:
  167. *params = it->value->info_log_length();
  168. break;
  169. default:
  170. VERIFY_NOT_REACHED();
  171. }
  172. }
  173. }