summaryrefslogtreecommitdiff
path: root/src/effects_software.cpp
blob: a5c0296e22ebd49f623279e15a35023e00251c3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#if SPECIAL
#include "main.h"
#endif


static void
Effect_Software_DrawColor(int Width, int Height, int BytesPerPixel, void *EffectBitmapAddress, v4 Color, blend_mode BlendMode)
{
    render_byte_info Bits = Bitmap_ByteInfo(BytesPerPixel);
    rectangle RenderRegion = {0, 0, Width, Height};
    transform_info T;
    T.BlendMode = BlendMode;
    for (int32 Y = RenderRegion.Min.y; Y < RenderRegion.Max.y; Y++) {
        for (int32 X = RenderRegion.Min.x; X < RenderRegion.Max.x; X++) {
            uint32 Offset = Y*Width*BytesPerPixel + X*BytesPerPixel;
            uint8 *LayerPixel = (uint8 *)EffectBitmapAddress + Offset;

            uint32 *R_DestAddress = (uint32 *)(LayerPixel + Bits.ByteOffset * 0);
            uint32 *G_DestAddress = (uint32 *)(LayerPixel + Bits.ByteOffset * 1);
            uint32 *B_DestAddress = (uint32 *)(LayerPixel + Bits.ByteOffset * 2);
            uint32 *A_DestAddress = (uint32 *)(LayerPixel + Bits.ByteOffset * 3);

            real32 R_Dest = (real32)(*R_DestAddress & Bits.MaskPixel) * Bits.Normalized;
            real32 G_Dest = (real32)(*G_DestAddress & Bits.MaskPixel) * Bits.Normalized;
            real32 B_Dest = (real32)(*B_DestAddress & Bits.MaskPixel) * Bits.Normalized;
            real32 A_Dest = (real32)(*A_DestAddress & Bits.MaskPixel) * Bits.Normalized;

            real32 R_Col = Color.r;
            real32 G_Col = Color.g;
            real32 B_Col = Color.b;
            real32 A_Col = Color.a;

            real32 LayerAlpha = A_Col * 1;

            real32 R_Blend = R_Col;
            real32 G_Blend = G_Col;
            real32 B_Blend = B_Col;
            real32 A_Blend = A_Col;

            Fallback_Blend();

            uint32 R_Out = (uint32)(Normalize(R_Blend) * Bits.Bits);
            uint32 G_Out = (uint32)(Normalize(G_Blend) * Bits.Bits);
            uint32 B_Out = (uint32)(Normalize(B_Blend) * Bits.Bits);
            uint32 A_Out = (uint32)(Normalize(A_Blend) * Bits.Bits);

            *R_DestAddress = (*R_DestAddress & ~Bits.MaskPixel) | R_Out;
            *G_DestAddress = (*G_DestAddress & ~Bits.MaskPixel) | G_Out;
            *B_DestAddress = (*B_DestAddress & ~Bits.MaskPixel) | B_Out;
            *A_DestAddress = (*A_DestAddress & ~Bits.MaskPixel) | A_Out;
        }
    }
}
static void
CurvesSolver(real32 *LUT, v2 Point_P1, v2 Point_P2, v2 m1, v2 m2, int i)
{

    real32 Precision = ((real32)1 / 256) * 0.1;
    real32 Point_Span = Point_P2.x - Point_P1.x;
    v2 Cache[256];

   if (i == 0) {
       // Don't know how to fix this, so I'm just gonna linear interpolate
       // until I try quadratic solving.
       real32 Count_Start = (Point_P1.x * 256);
       real32 Count_End = (Point_P2.x * 256);
       real32 Count_Total = Count_End - Count_Start;
       real32 Width = Point_P2.x - Point_P1.x;
       real32 Height = Point_P2.y - Point_P1.y;
       real32 Count = Count_Start;
       real32 t = 1;
       while (Count < Count_End) {
            LUT[(uint32)Count] = Normalize(Point_P1.y + (Height*((Count-Count_Start)/(Count_End - (Count_End - 256)))/Width));
            Count++;
       }
   } else {
       real32 Count_Start = (Point_P1.x * 256);
       real32 Count_End = (Point_P2.x * 256);
       real32 Count_Total = Count_End - Count_Start;
       real32 Count = Count_Start;
       real32 t = 0;
       int Timeout = 0;

       // This solver actually works kinda decently when the graph isn't that
       // complex, taking less than 10 iterations per LUT value. It fails
       // towards the edges and with harsh curves, going into the hundreds. The
       // 1000 condition should only be hit when the solver is locked, which can
       // happen when two points are close together on X.

       while (Count < Count_End) {

           real32 c = 2*t*t*t - 3*t*t;
           real32 c0 = c + 1;
           real32 c1 = t*t*t - 2*t*t + t;
           real32 c2 = -c;
           real32 c3 = t*t*t - t*t;

           v2 Point = (c0 * Point_P1) + (c1 * m1) + (c2 * Point_P2) + (c3 * m2);

           real32 TargetX = Count / 256;

           if (Timeout == 1000) {
               Point.x = TargetX;
               printf("Solve between %.1f and %.1f reached 1000 iterations at %.f!\n", Count_Start, Count_End, Count);
           }

           // Only record the value if it's within a certain precision.

           if (Point.x <= TargetX - Precision ||
               Point.x >= TargetX + Precision) {
               t = t * TargetX / Point.x;
               Timeout++;
           } else {
               if (Point.y > 1.0f) {
                   LUT[(uint32)Count] = 1.0f;
               } else if (Point.y < 0.0f)
                   LUT[(uint32)Count] = 0.0f;
               else {
                   LUT[(uint32)Count] = Point.y;
               }
               t += (Point_Span / Count_Total);
               Count++;
               Timeout = 0;
           }
       }
   }
}

static void
Effect_Software_Curves(int Width, int Height, int BytesPerPixel, void *EffectBitmapAddress, v2 *PointData, real32 PointCount, v4 PointCount_Col)
{
    real32 LUT[5][256] = {};

    for (int a = 0; a < 5; a++) {

        int Num = (a == 0) ? (int)PointCount : (int)PointCount_Col.E[a-1];
        v2 *CurvePoint = PointData + (MAX_PROPERTIES_PER_EFFECT / 5 * a);

        for (int i = 0; i < Num; i++) {
            v2 Point_P1 = CurvePoint[i];
            v2 Point_P2 = CurvePoint[i + 1];
            v2 Point_P0 = (i != 0) ? CurvePoint[i - 1] : V2(0, 0);
            v2 Point_P3 = (i != (Num - 2)) ? CurvePoint[i + 2] : V2(1, 1);

            v2 m1 = (Point_P2 - Point_P0) / (2 * Tau);
            v2 m2 = (Point_P3 - Point_P1) / (2 * Tau);

            CurvesSolver(LUT[a], Point_P1, Point_P2, m1, m2, i);
        }

        if (CurvePoint[0].x > 0.0f) {
           real32 Count_Start = 0;
           real32 Count_End = (CurvePoint[0].x * 255);
           real32 Count = Count_Start;
           while (Count < Count_End) {
               LUT[a][(uint32)Count] = LUT[a][(uint32)Count_End];
               Count++;
           }
        }

        if (CurvePoint[Num-1].x < 1.0f) {
           real32 Count_Start = (CurvePoint[Num-1].x * 255) - 0.5;
           real32 Count_End = 255;
           real32 Count = Count_Start;
           while (Count < Count_End) {
               LUT[a][(uint32)Count] = LUT[a][(uint32)Count_Start];
               Count++;
           }
        }

        for (int i = 0; i < Num; i++) {
            if (CurvePoint[i].y == 1.0f)
                LUT[a][255] = 1.0f;
        }
    }


    uint64 Size = Width*Height;
    int i = 0;
    Assert(BytesPerPixel == 4);
    while (i < Size) {
        uint32 *Pixel = (uint32 *)EffectBitmapAddress + i;

        uint8 A = (*Pixel >> 24);
        uint8 B = (*Pixel >> 16);
        uint8 G = (*Pixel >> 8);
        uint8 R = (*Pixel >> 0);

#if 1
        real32 R_Lookup = LUT[1][R];
        real32 G_Lookup = LUT[2][G];
        real32 B_Lookup = LUT[3][B];
        real32 A_Lookup = LUT[4][A];

        real32 R_Lookup_All = LUT[0][(uint32)(R_Lookup*255)];
        real32 G_Lookup_All = LUT[0][(uint32)(G_Lookup*255)];
        real32 B_Lookup_All = LUT[0][(uint32)(B_Lookup*255)];
#else
        real32 R_Lookup_All = LUT[0][(uint32)(t.r)];
        real32 G_Lookup_All = LUT[0][(uint32)(t.g)];
        real32 B_Lookup_All = LUT[0][(uint32)(t.b)];
#endif


        uint32 Result = (((uint32)((A_Lookup * 255.0f)     + 0.5) << 24) |
                         ((uint32)((B_Lookup_All * 255.0f) + 0.5)  << 16) |
                         ((uint32)((G_Lookup_All * 255.0f) + 0.5)  << 8) |
                         ((uint32)((R_Lookup_All * 255.0f) + 0.5)  << 0));

        *Pixel = Result;
        i++;
    }
}