1 /*
2   Copyright Chris Jones 2020.
3   Distributed under the Boost Software License, Version 1.0.
4   See accompanying file Licence.txt or copy at...
5   https://www.boost.org/LICENSE_1_0.txt
6 */
7 
8 module dg2d.blend;
9 
10 import dg2d.rasterizer;
11 import dg2d.misc;
12 
13 /*
14     How to to seperate blend modes from blitting? some way to mixin blend code
15     into a blitter?
16 
17     Main downside is maybe 5 blitters, 2 fill rules, 2 or 3 repeat modes, 20 to 30 
18     blend modes ==> 800 variations. 
19 
20     Last I checked blitter code is clocking in at about 1k, so do you want to
21     bundle 1MB of blitter code, how long would that take to compile etc???
22 
23     Might be nice to be able to have control such that some permutations get specific
24     implementation, some are handled by a generic function.
25 
26     Need to specify context for blend mixin...
27 
28     Pixel / blend variable formats...
29 
30     ARGB, alpha, red, green, blue
31     XRGB, unused, red, green, blue
32     AAAA, alpha, alpha, alpha, alpha
33 
34     left most component is in MSB, rightmost is in LSB.
35 
36     Most components will be 16 bits, but they will usually have been converted from an
37     8 bit source, so sometimes they may have different representation in 16 bit...
38 
39     MSB16, component uses all 16 bits.
40     LSB16, component uses lower 8 bits, upper 8 bits are zero
41 
42     If more than one pixel is in an __m128i then the LSB holds the leftmost pixel and
43     the MSB holds the rightmost.
44 
45     blend code expects these variables defineded in the scope it is injected into
46     
47     __m128i d0,d1 : 4 destination pixels, ARGB or XRGB, LSB16
48     __m128i c0,c1 : 4 source colours, XRGB, MSB16
49     __m128i a0,a0 : 4 blend alphas, AAAA, MSB16
50 
51     if a blend mode needs destination alpha it should unpack it from the destination
52     pixels itself.
53 
54     the source alpha is unpacked and multiplied by the coverage, this gives the final
55     source alpha.
56 
57     ===========
58 
59     If we just consider "clip to source" and a destination
60     that has no alpha we need to handle these situations....
61 
62     source coverage is 0%, just skip the pixels
63 
64     source coverage is 100%, do a solid fill
65 
66     source coverage is > 0% and < 100%, blended fill
67 
68     source coverage changes, delta fill
69 
70 */
71 /*
72 struct SourceOver_XRGB
73 {
74     string initVars() // blemd masks
75     {
76     }
77 
78     // if provided can be used when alpha = 100%
79     // context code sets 's0' ahead
80 
81     string opaqueFill()
82     {
83         return " __m128i r0 = s0; ";
84     }
85 
86     // if provided can be used when alpha inbetween 0%..100%
87     // context code sets 's0' ahead
88 
89     string semiFill()
90     {
91         d0 = _mm_mulhi_epu16 (d0,a0);
92         d1 = _mm_mulhi_epu16 (d1,a1);
93         d0 = _mm_packus_epi16 (d0,d1);
94         d0 = _mm_adds_epi8 (d0, s0);
95     }
96 
97 
98     string blend() // regular blend, no premultiplication
99     {
100         d0 = _mm_mulhi_epu16 (d0,a0);
101         d1 = _mm_mulhi_epu16 (d1,a1);
102         d0 = _mm_packus_epi16 (d0,d1);
103         d0 =  _mm_adds_epi8 (d0, s0);
104     }
105 }
106 */
107 /*
108 auto getBlendStuff(bool destHasAlpha, bool clipToSource, string opName)
109 {
110     if (opName = "SrcOver")
111     {
112         if 
113 
114     }
115 
116 
117 
118 
119 }
120 */