Blending.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2024, Jelle Raaijmakers <jelle@gmta.nl>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGL/GLContext.h>
  7. namespace GL {
  8. void GLContext::gl_blend_equation_separate(GLenum rgb_mode, GLenum alpha_mode)
  9. {
  10. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_equation_separate, rgb_mode, alpha_mode);
  11. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  12. RETURN_WITH_ERROR_IF(!(rgb_mode == GL_FUNC_ADD
  13. || rgb_mode == GL_FUNC_SUBTRACT
  14. || rgb_mode == GL_FUNC_REVERSE_SUBTRACT
  15. || rgb_mode == GL_MIN
  16. || rgb_mode == GL_MAX),
  17. GL_INVALID_ENUM);
  18. RETURN_WITH_ERROR_IF(!(alpha_mode == GL_FUNC_ADD
  19. || alpha_mode == GL_FUNC_SUBTRACT
  20. || alpha_mode == GL_FUNC_REVERSE_SUBTRACT
  21. || alpha_mode == GL_MIN
  22. || alpha_mode == GL_MAX),
  23. GL_INVALID_ENUM);
  24. m_blend_equation_rgb = rgb_mode;
  25. m_blend_equation_alpha = alpha_mode;
  26. auto map_gl_blend_equation_to_device = [](GLenum equation) constexpr {
  27. switch (equation) {
  28. case GL_FUNC_ADD:
  29. return GPU::BlendEquation::Add;
  30. case GL_FUNC_SUBTRACT:
  31. return GPU::BlendEquation::Subtract;
  32. case GL_FUNC_REVERSE_SUBTRACT:
  33. return GPU::BlendEquation::ReverseSubtract;
  34. case GL_MIN:
  35. return GPU::BlendEquation::Min;
  36. case GL_MAX:
  37. return GPU::BlendEquation::Max;
  38. default:
  39. VERIFY_NOT_REACHED();
  40. }
  41. };
  42. auto options = m_rasterizer->options();
  43. options.blend_equation_rgb = map_gl_blend_equation_to_device(m_blend_equation_rgb);
  44. options.blend_equation_alpha = map_gl_blend_equation_to_device(m_blend_equation_alpha);
  45. m_rasterizer->set_options(options);
  46. }
  47. void GLContext::gl_blend_func(GLenum src_factor, GLenum dst_factor)
  48. {
  49. APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(gl_blend_func, src_factor, dst_factor);
  50. RETURN_WITH_ERROR_IF(m_in_draw_state, GL_INVALID_OPERATION);
  51. // FIXME: The list of allowed enums differs between API versions
  52. // This was taken from the 2.0 spec on https://docs.gl/gl2/glBlendFunc
  53. RETURN_WITH_ERROR_IF(!(src_factor == GL_ZERO
  54. || src_factor == GL_ONE
  55. || src_factor == GL_SRC_COLOR
  56. || src_factor == GL_ONE_MINUS_SRC_COLOR
  57. || src_factor == GL_DST_COLOR
  58. || src_factor == GL_ONE_MINUS_DST_COLOR
  59. || src_factor == GL_SRC_ALPHA
  60. || src_factor == GL_ONE_MINUS_SRC_ALPHA
  61. || src_factor == GL_DST_ALPHA
  62. || src_factor == GL_ONE_MINUS_DST_ALPHA
  63. || src_factor == GL_CONSTANT_COLOR
  64. || src_factor == GL_ONE_MINUS_CONSTANT_COLOR
  65. || src_factor == GL_CONSTANT_ALPHA
  66. || src_factor == GL_ONE_MINUS_CONSTANT_ALPHA
  67. || src_factor == GL_SRC_ALPHA_SATURATE),
  68. GL_INVALID_ENUM);
  69. RETURN_WITH_ERROR_IF(!(dst_factor == GL_ZERO
  70. || dst_factor == GL_ONE
  71. || dst_factor == GL_SRC_COLOR
  72. || dst_factor == GL_ONE_MINUS_SRC_COLOR
  73. || dst_factor == GL_DST_COLOR
  74. || dst_factor == GL_ONE_MINUS_DST_COLOR
  75. || dst_factor == GL_SRC_ALPHA
  76. || dst_factor == GL_ONE_MINUS_SRC_ALPHA
  77. || dst_factor == GL_DST_ALPHA
  78. || dst_factor == GL_ONE_MINUS_DST_ALPHA
  79. || dst_factor == GL_CONSTANT_COLOR
  80. || dst_factor == GL_ONE_MINUS_CONSTANT_COLOR
  81. || dst_factor == GL_CONSTANT_ALPHA
  82. || dst_factor == GL_ONE_MINUS_CONSTANT_ALPHA),
  83. GL_INVALID_ENUM);
  84. m_blend_source_factor = src_factor;
  85. m_blend_destination_factor = dst_factor;
  86. auto map_gl_blend_factor_to_device = [](GLenum factor) constexpr {
  87. switch (factor) {
  88. case GL_ZERO:
  89. return GPU::BlendFactor::Zero;
  90. case GL_ONE:
  91. return GPU::BlendFactor::One;
  92. case GL_SRC_COLOR:
  93. return GPU::BlendFactor::SrcColor;
  94. case GL_ONE_MINUS_SRC_COLOR:
  95. return GPU::BlendFactor::OneMinusSrcColor;
  96. case GL_DST_COLOR:
  97. return GPU::BlendFactor::DstColor;
  98. case GL_ONE_MINUS_DST_COLOR:
  99. return GPU::BlendFactor::OneMinusDstColor;
  100. case GL_SRC_ALPHA:
  101. return GPU::BlendFactor::SrcAlpha;
  102. case GL_ONE_MINUS_SRC_ALPHA:
  103. return GPU::BlendFactor::OneMinusSrcAlpha;
  104. case GL_DST_ALPHA:
  105. return GPU::BlendFactor::DstAlpha;
  106. case GL_ONE_MINUS_DST_ALPHA:
  107. return GPU::BlendFactor::OneMinusDstAlpha;
  108. case GL_SRC_ALPHA_SATURATE:
  109. return GPU::BlendFactor::SrcAlphaSaturate;
  110. default:
  111. VERIFY_NOT_REACHED();
  112. }
  113. };
  114. auto options = m_rasterizer->options();
  115. options.blend_source_factor = map_gl_blend_factor_to_device(m_blend_source_factor);
  116. options.blend_destination_factor = map_gl_blend_factor_to_device(m_blend_destination_factor);
  117. m_rasterizer->set_options(options);
  118. }
  119. }