summaryrefslogtreecommitdiff
path: root/src/egg/gfx/eggDrawPathDOF.cpp
blob: d7d5767f6716eeeead677ce0c496c4e6eb42243f (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "egg/gfx/eggDrawPathDOF.h"

#include "common.h"
#include "egg/gfx/eggDrawGX.h"
#include "egg/gfx/eggGlobalDrawState.h"
#include "egg/gfx/eggPostEffectBase.h"
#include "egg/gfx/eggPostEffectBlur.h"
#include "egg/gfx/eggPostEffectMask.h"
#include "egg/gfx/eggPostEffectMaskDOF.h"
#include "egg/gfx/eggPostEffectSimple.h"
#include "egg/gfx/eggScreen.h"
#include "egg/gfx/eggStateEfb.h"
#include "egg/gfx/eggStateGX.h"
#include "egg/gfx/eggTextureBuffer.h"
#include "nw4r/math/math_types.h"
#include "rvl/GX/GXTypes.h"

namespace EGG {

DrawPathDOF::DrawPathDOF()
    : field_0x1C(1),
      field_0x20(1),
      field_0x24(30.0f),
      field_0x28(20.0f),
      field_0x2C(20.0f),
      field_0x30(1.6f),
      field_0x34(0),
      field_0x38(2),
      field_0x5C(0.0f),
      field_0x60(0.0f),
      field_0x64(0.4f),
      field_0x68(0.2f),
      field_0x6C(-0.0075f),
      field_0x70(-0.0075f),
      field_0x74(10.0f),
      field_0x78(15.0f),
      field_0x7C(1.0f),
      field_0x80(21),
      mpBlur(nullptr),
      mpMask(nullptr),
      mpTextureBuffer(nullptr),
      mpMaskDof(nullptr) {
    mpMaskDof = new PostEffectMaskDOF();
    mpBlur = new PostEffectBlur();
    mpBlur->setField0x2C(1);
    mpMask = new PostEffectMask();

    mpMask->setField_0x00(0);
    mpMask->setField_0x34(0.0f);
    mpMask->setField_0x2C(0);
    field_0x1E = 0xFF;
    field_0x1F = 0xFF;
}

DrawPathDOF::~DrawPathDOF() {
    delete mpMaskDof;
    mpMaskDof = nullptr;
    delete mpBlur;
    mpBlur = nullptr;
    delete mpMask;
    mpMask = nullptr;
}

void DrawPathDOF::updateShimmer() {
    field_0x5C += field_0x6C * field_0x7C;
    field_0x60 += field_0x70 * field_0x7C;

    if (field_0x5C < -1.0f) {
        field_0x5C += 2.0f;
    } else if (field_0x5C > 1.0f) {
        // @bug typo?
        field_0x5C -= -2.0f;
    }

    if (field_0x60 < -1.0f) {
        field_0x60 += 2.0f;
    } else if (field_0x60 > 1.0f) {
        field_0x60 -= 2.0f;
    }
}

static int sMaskDofArray[] = {0, 1, 2};
static u8 sBlurArray[] = {4, 2, 1, 0};

void DrawPathDOF::internalResetForDraw() {
    // Tons of regswaps...
    f32 a = GlobalDrawState::getScreen().TranslateDepthToViewSpace(field_0x24);
    f32 b = GlobalDrawState::getScreen().TranslateDepthToViewSpace(field_0x24 - field_0x28 / 2.0f);
    f32 c = GlobalDrawState::getScreen().TranslateDepthToViewSpace(field_0x24 + field_0x28 / 2.0f);

    // TODO
    if (c < b + 1e-6f) {
        c = b + 1e-6f;

        if (b < 1.0f) {
            c = 1.0f;
            b -= 1e-6f;
        }
    }
    mpMaskDof->setParam0x48(b);
    mpMaskDof->setParam0x44(a);
    mpMaskDof->setParam0x4C(c);

    if (field_0x38 >= 3) {
        field_0x38 = 2;
    }

    mpMaskDof->setCpuTexArrIdx(sMaskDofArray[field_0x38]);
    mpBlur->setStage0Field0(sBlurArray[field_0x34]);
    mpBlur->setStage0F(field_0x34 != 2 ? field_0x30 : 0.0f);
    // HELP
    mpBlur->setStage0ColorThing(field_0x1E);
}

void DrawPathDOF::internalDraw(u16 idx) {
    if (idx == 0) {
        if (StateEfb::CheckFlag0x2() == 0) {
            StateEfb::pushWorkBuffer(
                static_cast<StateEfb::WorkBuffer>((field_0x1C >> 2) & 1), reinterpret_cast<u32>(this)
            );
        }
        StateGX::invalidateTexAllGX();
    }

    if (field_0x20 == 2) {
        internalDrawLite(idx);
    } else {
        internalDrawStandard(idx);
    }

    switch (idx) {
        case 1:
            StateEfb::popWorkBuffer(false, reinterpret_cast<u32>(this));
            StateEfb::releaseEfb(StateEfb::BUFFER_TYPE_3, reinterpret_cast<u32>(this));
            break;
        case 2:
            if (mpTextureBuffer != nullptr) {
                mpTextureBuffer->free();
                mpTextureBuffer = nullptr;
            }
    }
}

void DrawPathDOF::internalDrawStandard(u16 idx) {}

void DrawPathDOF::internalDrawLite(u16 idx) {
    const Screen &screen = GlobalDrawState::getScreen();
    const Screen::DataEfb &efb = screen.GetDataEfb();
    switch (idx) {
        case 0: {
            TextureBuffer *capturedBuf = StateEfb::captureEfb(StateEfb::BUFFER_TYPE_3, false, reinterpret_cast<u32>(this));
            StateGX::setPixelFormatGX(GX_PF_RGB8_Z24, GX_ZC_LINEAR);
            StateGX::ScopedColor colorGuard(true);
            StateGX::ScopedAlpha alphaGuard(false);
            StateGX::ScopedDither ditherGuard(false);
            PostEffectBase::setProjection(screen);
            f32 *floats = StateEfb::shiftWorkSpaceViewportGX();
            f32 scale = (field_0x1C & 4) != 0 ? 0.25f : 0.5f;
            f32 width1 = screen.GetWidth() * scale;
            f32 height1 = screen.GetHeight() * scale;

            f32 rot = 0.5f;

            u16 width = efb.vp.x2 * scale;
            u16 height = efb.vp.y2 * scale;

            PostEffectSimple effect;
            effect.setCapTexMapId(GX_TEXMAP0);
            effect.setField0x38(0);
            effect.setField_0x00(0);
            effect.setRot(rot);

            effect.setCapTexture(capturedBuf);
            // TODO tons of problems in these UnscaleByX inlines that probably exist
            effect.draw(screen.UnscaleByX(height), screen.UnscaleByY(width));
            TextureBuffer *pTex2 = TextureBuffer::alloc(height, width, GX_TF_RGBA8);
            pTex2->fn_8049D9D0(field_0x80);
            pTex2->capture(floats[0], floats[1], false, -1);
            mpBlur->setCapTexture(pTex2);
            mpBlur->setCapTexMapId(GX_TEXMAP0);
            mpBlur->setRot(-rot);
            mpBlur->draw(width1, height1);

            if (pTex2 != nullptr) {
                pTex2->free();
            }
            allocAndCaptureBlurBuffer(floats[0], floats[1], width, height);
            StateGX::setPixelFormatGX(StateGX::s_pixFormat, StateGX::s_zFmt16);
            break;
        }
        case 2: {
            StateGX::ScopedColor colorGuard(true);
            StateGX::ScopedAlpha alphaGuard(false);
            StateGX::ScopedDither ditherGuard(true);

            Screen screenCpy(screen);
            screenCpy.SetOffset(nw4r::math::VEC2(0.0f, 0.0f));
            screenCpy.SetScale(nw4r::math::VEC3(1.0f, 1.0f, 1.0f));
            screenCpy.SetFlag(Screen::FLAG_0x40);
            screenCpy.SetProjectionGX();
            DrawGX::SetMat_ColorChannel(DrawGX::COLORCHAN_1);
            DrawGX::SetMat_TexGen(DrawGX::TEXGEN_1);
            DrawGX::SetMat_Ind();
            DrawGX::SetMat_Tev(GX_TEVSTAGE0, DrawGX::TEV_2);
            GXSetTevAlphaIn(GX_TEVSTAGE0,GX_CA_ZERO,GX_CA_ZERO,GX_CA_ZERO,GX_CA_RASA);
            DrawGX::SetMat_PE(DrawGX::ZMODE_2, DrawGX::BLEND_0);
            DrawGX::SetVtxState(DrawGX::VTX_TYPE_7);
            mpTextureBuffer->load(GX_TEXMAP0);

            for (int i = 0; i < 2; i++) {
                // TODO
            }

            break;
        }
    }
}

void DrawPathDOF::SetBinaryInner(const Bin &bin) {
    // TODO
}

void DrawPathDOF::GetBinaryInner(Bin *pBin) const {
    // TODO
}

void DrawPathDOF::SetBinaryInner(const Bin &, const Bin &, f32) {
    // TODO
}

} // namespace EGG