SDL_blendmode.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryBlendmode
  20. *
  21. * Blend modes decide how two colors will mix together. There are both
  22. * standard modes for basic needs and a means to create custom modes,
  23. * dictating what sort of math to do on what color components.
  24. */
  25. #ifndef SDL_blendmode_h_
  26. #define SDL_blendmode_h_
  27. #include <SDL3/SDL_stdinc.h>
  28. #include <SDL3/SDL_begin_code.h>
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /**
  34. * A set of blend modes used in drawing operations.
  35. *
  36. * These predefined blend modes are supported everywhere.
  37. *
  38. * Additional values may be obtained from SDL_ComposeCustomBlendMode.
  39. *
  40. * \since This datatype is available since SDL 3.1.3.
  41. *
  42. * \sa SDL_ComposeCustomBlendMode
  43. */
  44. typedef Uint32 SDL_BlendMode;
  45. #define SDL_BLENDMODE_NONE 0x00000000u /**< no blending: dstRGBA = srcRGBA */
  46. #define SDL_BLENDMODE_BLEND 0x00000001u /**< alpha blending: dstRGB = (srcRGB * srcA) + (dstRGB * (1-srcA)), dstA = srcA + (dstA * (1-srcA)) */
  47. #define SDL_BLENDMODE_BLEND_PREMULTIPLIED 0x00000010u /**< pre-multiplied alpha blending: dstRGBA = srcRGBA + (dstRGBA * (1-srcA)) */
  48. #define SDL_BLENDMODE_ADD 0x00000002u /**< additive blending: dstRGB = (srcRGB * srcA) + dstRGB, dstA = dstA */
  49. #define SDL_BLENDMODE_ADD_PREMULTIPLIED 0x00000020u /**< pre-multiplied additive blending: dstRGB = srcRGB + dstRGB, dstA = dstA */
  50. #define SDL_BLENDMODE_MOD 0x00000004u /**< color modulate: dstRGB = srcRGB * dstRGB, dstA = dstA */
  51. #define SDL_BLENDMODE_MUL 0x00000008u /**< color multiply: dstRGB = (srcRGB * dstRGB) + (dstRGB * (1-srcA)), dstA = dstA */
  52. #define SDL_BLENDMODE_INVALID 0x7FFFFFFFu
  53. /**
  54. * The blend operation used when combining source and destination pixel
  55. * components.
  56. *
  57. * \since This enum is available since SDL 3.1.3.
  58. */
  59. typedef enum SDL_BlendOperation
  60. {
  61. SDL_BLENDOPERATION_ADD = 0x1, /**< dst + src: supported by all renderers */
  62. SDL_BLENDOPERATION_SUBTRACT = 0x2, /**< src - dst : supported by D3D, OpenGL, OpenGLES, and Vulkan */
  63. SDL_BLENDOPERATION_REV_SUBTRACT = 0x3, /**< dst - src : supported by D3D, OpenGL, OpenGLES, and Vulkan */
  64. SDL_BLENDOPERATION_MINIMUM = 0x4, /**< min(dst, src) : supported by D3D, OpenGL, OpenGLES, and Vulkan */
  65. SDL_BLENDOPERATION_MAXIMUM = 0x5 /**< max(dst, src) : supported by D3D, OpenGL, OpenGLES, and Vulkan */
  66. } SDL_BlendOperation;
  67. /**
  68. * The normalized factor used to multiply pixel components.
  69. *
  70. * The blend factors are multiplied with the pixels from a drawing operation
  71. * (src) and the pixels from the render target (dst) before the blend
  72. * operation. The comma-separated factors listed above are always applied in
  73. * the component order red, green, blue, and alpha.
  74. *
  75. * \since This enum is available since SDL 3.1.3.
  76. */
  77. typedef enum SDL_BlendFactor
  78. {
  79. SDL_BLENDFACTOR_ZERO = 0x1, /**< 0, 0, 0, 0 */
  80. SDL_BLENDFACTOR_ONE = 0x2, /**< 1, 1, 1, 1 */
  81. SDL_BLENDFACTOR_SRC_COLOR = 0x3, /**< srcR, srcG, srcB, srcA */
  82. SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR = 0x4, /**< 1-srcR, 1-srcG, 1-srcB, 1-srcA */
  83. SDL_BLENDFACTOR_SRC_ALPHA = 0x5, /**< srcA, srcA, srcA, srcA */
  84. SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA = 0x6, /**< 1-srcA, 1-srcA, 1-srcA, 1-srcA */
  85. SDL_BLENDFACTOR_DST_COLOR = 0x7, /**< dstR, dstG, dstB, dstA */
  86. SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR = 0x8, /**< 1-dstR, 1-dstG, 1-dstB, 1-dstA */
  87. SDL_BLENDFACTOR_DST_ALPHA = 0x9, /**< dstA, dstA, dstA, dstA */
  88. SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA = 0xA /**< 1-dstA, 1-dstA, 1-dstA, 1-dstA */
  89. } SDL_BlendFactor;
  90. /**
  91. * Compose a custom blend mode for renderers.
  92. *
  93. * The functions SDL_SetRenderDrawBlendMode and SDL_SetTextureBlendMode accept
  94. * the SDL_BlendMode returned by this function if the renderer supports it.
  95. *
  96. * A blend mode controls how the pixels from a drawing operation (source) get
  97. * combined with the pixels from the render target (destination). First, the
  98. * components of the source and destination pixels get multiplied with their
  99. * blend factors. Then, the blend operation takes the two products and
  100. * calculates the result that will get stored in the render target.
  101. *
  102. * Expressed in pseudocode, it would look like this:
  103. *
  104. * ```c
  105. * dstRGB = colorOperation(srcRGB * srcColorFactor, dstRGB * dstColorFactor);
  106. * dstA = alphaOperation(srcA * srcAlphaFactor, dstA * dstAlphaFactor);
  107. * ```
  108. *
  109. * Where the functions `colorOperation(src, dst)` and `alphaOperation(src,
  110. * dst)` can return one of the following:
  111. *
  112. * - `src + dst`
  113. * - `src - dst`
  114. * - `dst - src`
  115. * - `min(src, dst)`
  116. * - `max(src, dst)`
  117. *
  118. * The red, green, and blue components are always multiplied with the first,
  119. * second, and third components of the SDL_BlendFactor, respectively. The
  120. * fourth component is not used.
  121. *
  122. * The alpha component is always multiplied with the fourth component of the
  123. * SDL_BlendFactor. The other components are not used in the alpha
  124. * calculation.
  125. *
  126. * Support for these blend modes varies for each renderer. To check if a
  127. * specific SDL_BlendMode is supported, create a renderer and pass it to
  128. * either SDL_SetRenderDrawBlendMode or SDL_SetTextureBlendMode. They will
  129. * return with an error if the blend mode is not supported.
  130. *
  131. * This list describes the support of custom blend modes for each renderer.
  132. * All renderers support the four blend modes listed in the SDL_BlendMode
  133. * enumeration.
  134. *
  135. * - **direct3d**: Supports all operations with all factors. However, some
  136. * factors produce unexpected results with `SDL_BLENDOPERATION_MINIMUM` and
  137. * `SDL_BLENDOPERATION_MAXIMUM`.
  138. * - **direct3d11**: Same as Direct3D 9.
  139. * - **opengl**: Supports the `SDL_BLENDOPERATION_ADD` operation with all
  140. * factors. OpenGL versions 1.1, 1.2, and 1.3 do not work correctly here.
  141. * - **opengles2**: Supports the `SDL_BLENDOPERATION_ADD`,
  142. * `SDL_BLENDOPERATION_SUBTRACT`, `SDL_BLENDOPERATION_REV_SUBTRACT`
  143. * operations with all factors.
  144. * - **psp**: No custom blend mode support.
  145. * - **software**: No custom blend mode support.
  146. *
  147. * Some renderers do not provide an alpha component for the default render
  148. * target. The `SDL_BLENDFACTOR_DST_ALPHA` and
  149. * `SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA` factors do not have an effect in this
  150. * case.
  151. *
  152. * \param srcColorFactor the SDL_BlendFactor applied to the red, green, and
  153. * blue components of the source pixels.
  154. * \param dstColorFactor the SDL_BlendFactor applied to the red, green, and
  155. * blue components of the destination pixels.
  156. * \param colorOperation the SDL_BlendOperation used to combine the red,
  157. * green, and blue components of the source and
  158. * destination pixels.
  159. * \param srcAlphaFactor the SDL_BlendFactor applied to the alpha component of
  160. * the source pixels.
  161. * \param dstAlphaFactor the SDL_BlendFactor applied to the alpha component of
  162. * the destination pixels.
  163. * \param alphaOperation the SDL_BlendOperation used to combine the alpha
  164. * component of the source and destination pixels.
  165. * \returns an SDL_BlendMode that represents the chosen factors and
  166. * operations.
  167. *
  168. * \threadsafety It is safe to call this function from any thread.
  169. *
  170. * \since This function is available since SDL 3.1.3.
  171. *
  172. * \sa SDL_SetRenderDrawBlendMode
  173. * \sa SDL_GetRenderDrawBlendMode
  174. * \sa SDL_SetTextureBlendMode
  175. * \sa SDL_GetTextureBlendMode
  176. */
  177. extern SDL_DECLSPEC SDL_BlendMode SDLCALL SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor,
  178. SDL_BlendFactor dstColorFactor,
  179. SDL_BlendOperation colorOperation,
  180. SDL_BlendFactor srcAlphaFactor,
  181. SDL_BlendFactor dstAlphaFactor,
  182. SDL_BlendOperation alphaOperation);
  183. /* Ends C function definitions when using C++ */
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #include <SDL3/SDL_close_code.h>
  188. #endif /* SDL_blendmode_h_ */