1 /*
2     DSDL
3     Copyright (C) 2025 Inochi2D Project <luna@foxgirls.gay>
4 
5     This software is provided 'as-is', without any express or implied
6     warranty.  In no event will the authors be held liable for any damages
7     arising from the use of this software.
8 
9     Permission is granted to anyone to use this software for any purpose,
10     including commercial applications, and to alter it and redistribute it
11     freely, subject to the following restrictions:
12 
13     1. The origin of this software must not be misrepresented; you must not
14         claim that you wrote the original software. If you use this software
15         in a product, an acknowledgment in the product documentation would be
16         appreciated but is not required.
17     2. Altered source versions must be plainly marked as such, and must not be
18         misrepresented as being the original software.
19     3. This notice may not be removed or altered from any source distribution.
20 
21     ==========================================================================
22 
23     Simple DirectMedia Layer
24     Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
25 
26     This software is provided 'as-is', without any express or implied
27     warranty.  In no event will the authors be held liable for any damages
28     arising from the use of this software.
29 
30     Permission is granted to anyone to use this software for any purpose,
31     including commercial applications, and to alter it and redistribute it
32     freely, subject to the following restrictions:
33 
34     1. The origin of this software must not be misrepresented; you must not
35         claim that you wrote the original software. If you use this software
36         in a product, an acknowledgment in the product documentation would be
37         appreciated but is not required.
38     2. Altered source versions must be plainly marked as such, and must not be
39         misrepresented as being the original software.
40     3. This notice may not be removed or altered from any source distribution.
41 */
42 
43 /**
44     SDL Blending Modes
45 
46     See_Also:
47         $(LINK2 https://wiki.libsdl.org/SDL3/CategoryBlendmode, SDL3 Blending Documentation)
48     
49     Copyright: © 2025 Inochi2D Project, © 1997-2025 Sam Lantinga
50     License: Subject to the terms of the Zlib License, as written in the LICENSE file.
51     Authors: 
52         Luna Nielsen
53 */
54 module sdl.blendmode;
55 import sdl.stdc;
56 
57 extern(C) nothrow @nogc:
58 
59 /**
60     A set of blend modes used in drawing operations.
61 
62     These predefined blend modes are supported everywhere.
63 
64     Additional values may be obtained from SDL_ComposeCustomBlendMode.
65     
66     See_Also:
67         $(D SDL_ComposeCustomBlendMode)
68 */
69 alias SDL_BlendMode = Uint32;
70 
71 /**
72     no blending: dstRGBA = srcRGBA
73 */
74 enum SDL_BlendMode SDL_BLENDMODE_NONE =                  0x00000000u;
75 
76 /**
77     alpha blending: dstRGB = (srcRGB * srcA) + (dstRGB * (1-srcA)), dstA = srcA + (dstA * (1-srcA))
78 */
79 enum SDL_BlendMode SDL_BLENDMODE_BLEND =                 0x00000001u;
80 
81 /**
82     pre-multiplied alpha blending: dstRGBA = srcRGBA + (dstRGBA * (1-srcA))
83 */
84 enum SDL_BlendMode SDL_BLENDMODE_BLEND_PREMULTIPLIED =   0x00000010u;
85 
86 /**
87     additive blending: dstRGB = (srcRGB * srcA) + dstRGB, dstA = dstA
88 */
89 enum SDL_BlendMode SDL_BLENDMODE_ADD =                   0x00000002u;
90 
91 /**
92     pre-multiplied additive blending: dstRGB = srcRGB + dstRGB, dstA = dstA
93 */
94 enum SDL_BlendMode SDL_BLENDMODE_ADD_PREMULTIPLIED =     0x00000020u;
95 
96 /**
97     color modulate: dstRGB = srcRGB * dstRGB, dstA = dstA
98 */
99 enum SDL_BlendMode SDL_BLENDMODE_MOD =                   0x00000004u;
100 
101 /**
102     color multiply: dstRGB = (srcRGB * dstRGB) + (dstRGB * (1-srcA)), dstA = dstA
103 */
104 enum SDL_BlendMode SDL_BLENDMODE_MUL =                   0x00000008u;
105 enum SDL_BlendMode SDL_BLENDMODE_INVALID =               0x7FFFFFFFu;
106 
107 /**
108     The blend operation used when combining source and destination pixel
109     components.
110 
111     \since This enum is available since SDL 3.2.0.
112 */
113 enum SDL_BlendOperation {
114     
115     /**
116         dst + src: supported by all renderers
117     */
118     SDL_BLENDOPERATION_ADD              = 0x1,  
119     
120     /**
121         src - dst : supported by D3D, OpenGL, OpenGLES, and Vulkan
122     */
123     SDL_BLENDOPERATION_SUBTRACT         = 0x2,  
124     
125     /**
126         dst - src : supported by D3D, OpenGL, OpenGLES, and Vulkan
127     */
128     SDL_BLENDOPERATION_REV_SUBTRACT     = 0x3,  
129     
130     /**
131         min(dst, src) : supported by D3D, OpenGL, OpenGLES, and Vulkan
132     */
133     SDL_BLENDOPERATION_MINIMUM          = 0x4,  
134     
135     /**
136         max(dst, src) : supported by D3D, OpenGL, OpenGLES, and Vulkan
137     */
138     SDL_BLENDOPERATION_MAXIMUM          = 0x5   
139 }
140 
141 /**
142     The normalized factor used to multiply pixel components.
143 
144     The blend factors are multiplied with the pixels from a drawing operation
145     (src) and the pixels from the render target (dst) before the blend
146     operation. The comma-separated factors listed above are always applied in
147     the component order red, green, blue, and alpha.
148 
149     \since This enum is available since SDL 3.2.0.
150 */
151 enum SDL_BlendFactor {
152     
153     /**
154         0, 0, 0, 0
155     */
156     SDL_BLENDFACTOR_ZERO                = 0x1,  
157     
158     /**
159         1, 1, 1, 1
160     */
161     SDL_BLENDFACTOR_ONE                 = 0x2,  
162     
163     /**
164         srcR, srcG, srcB, srcA
165     */
166     SDL_BLENDFACTOR_SRC_COLOR           = 0x3,  
167     
168     /**
169         1-srcR, 1-srcG, 1-srcB, 1-srcA
170     */
171     SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR = 0x4,  
172     
173     /**
174         srcA, srcA, srcA, srcA
175     */
176     SDL_BLENDFACTOR_SRC_ALPHA           = 0x5,  
177     
178     /**
179         1-srcA, 1-srcA, 1-srcA, 1-srcA
180     */
181     SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA = 0x6,  
182     
183     /**
184         dstR, dstG, dstB, dstA
185     */
186     SDL_BLENDFACTOR_DST_COLOR           = 0x7,  
187     
188     /**
189         1-dstR, 1-dstG, 1-dstB, 1-dstA
190     */
191     SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR = 0x8,  
192     
193     /**
194         dstA, dstA, dstA, dstA
195     */
196     SDL_BLENDFACTOR_DST_ALPHA           = 0x9,  
197     
198     /**
199         1-dstA, 1-dstA, 1-dstA, 1-dstA
200     */
201     SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA = 0xA   
202 }
203 
204 /**
205     Compose a custom blend mode for renderers.
206 
207     The functions SDL_SetRenderDrawBlendMode and SDL_SetTextureBlendMode accept
208     the SDL_BlendMode returned by this function if the renderer supports it.
209 
210     A blend mode controls how the pixels from a drawing operation (source) get
211     combined with the pixels from the render target (destination). First, the
212     components of the source and destination pixels get multiplied with their
213     blend factors. Then, the blend operation takes the two products and
214     calculates the result that will get stored in the render target.
215 
216     Expressed in pseudocode, it would look like this:
217 
218     ```c
219     dstRGB = colorOperation(srcRGB * srcColorFactor, dstRGB * dstColorFactor);
220     dstA = alphaOperation(srcA * srcAlphaFactor, dstA * dstAlphaFactor);
221     ```
222 
223     Where the functions `colorOperation(src, dst)` and `alphaOperation(src,
224     dst)` can return one of the following:
225 
226     - `src + dst`
227     - `src - dst`
228     - `dst - src`
229     - `min(src, dst)`
230     - `max(src, dst)`
231 
232     The red, green, and blue components are always multiplied with the first,
233     second, and third components of the SDL_BlendFactor, respectively. The
234     fourth component is not used.
235 
236     The alpha component is always multiplied with the fourth component of the
237     SDL_BlendFactor. The other components are not used in the alpha
238     calculation.
239 
240     Support for these blend modes varies for each renderer. To check if a
241     specific SDL_BlendMode is supported, create a renderer and pass it to
242     either SDL_SetRenderDrawBlendMode or SDL_SetTextureBlendMode. They will
243     return with an error if the blend mode is not supported.
244 
245     This list describes the support of custom blend modes for each renderer.
246     All renderers support the four blend modes listed in the SDL_BlendMode
247     enumeration.
248 
249     - **direct3d**: Supports all operations with all factors. However, some
250     factors produce unexpected results with `SDL_BLENDOPERATION_MINIMUM` and
251     `SDL_BLENDOPERATION_MAXIMUM`.
252     - **direct3d11**: Same as Direct3D 9.
253     - **opengl**: Supports the `SDL_BLENDOPERATION_ADD` operation with all
254     factors. OpenGL versions 1.1, 1.2, and 1.3 do not work correctly here.
255     - **opengles2**: Supports the `SDL_BLENDOPERATION_ADD`,
256     `SDL_BLENDOPERATION_SUBTRACT`, `SDL_BLENDOPERATION_REV_SUBTRACT`
257     operations with all factors.
258     - **psp**: No custom blend mode support.
259     - **software**: No custom blend mode support.
260 
261     Some renderers do not provide an alpha component for the default render
262     target. The `SDL_BLENDFACTOR_DST_ALPHA` and
263     `SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA` factors do not have an effect in this
264     case.
265 
266     Params:
267         srcColorFactor =    the SDL_BlendFactor applied to the red, green, and
268                             blue components of the source pixels.
269         dstColorFactor =    the SDL_BlendFactor applied to the red, green, and
270                             blue components of the destination pixels.
271         colorOperation =    the SDL_BlendOperation used to combine the red,
272                             green, and blue components of the source and
273                             destination pixels.
274         srcAlphaFactor =    the SDL_BlendFactor applied to the alpha component of
275                             the source pixels.
276         dstAlphaFactor =    the SDL_BlendFactor applied to the alpha component of
277                             the destination pixels.
278         alphaOperation =    The SDL_BlendOperation used to combine the alpha
279                             component of the source and destination pixels.
280     
281     Returns:
282         An SDL_BlendMode that represents the chosen factors and
283         operations.
284 
285     Threadsafety:
286         It is safe to call this function from any thread.
287 
288     See_Also:
289         $(D SDL_SetRenderDrawBlendMode)
290         $(D SDL_GetRenderDrawBlendMode)
291         $(D SDL_SetTextureBlendMode)
292         $(D SDL_GetTextureBlendMode)
293 */
294 extern SDL_BlendMode SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor,
295                                                 SDL_BlendFactor dstColorFactor,
296                                                 SDL_BlendOperation colorOperation,
297                                                 SDL_BlendFactor srcAlphaFactor,
298                                                 SDL_BlendFactor dstAlphaFactor,
299                                                 SDL_BlendOperation alphaOperation);