aboutsummaryrefslogtreecommitdiff
path: root/app/rendercoord.cpp
blob: fd325e375cc73984119c3a60ad134a83c39610e6 (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
#include "rendercoord.hpp"
#include <algorithm>
#include <qobject.h>
#include <iostream>
#include <qrgb.h>

#include <qsemaphore.h>
#include <render.hpp>
#include <sstream>

uint32_t colorToUint32(const Color &c) {
    Color cnew = Color(c);
    cnew.clamp();
    return (0xFF << 24) + 
        (cnew.r() << 16) +
        (cnew.g() << 8) +
        cnew.b();
}

// Run by main thread
RenderThread::RenderThread(Renderer r, unsigned threads, QObject *parent, unsigned id) 
    : QThread(parent),
    m_lock(1),
    m_render(r),
    m_pause(1)
{
    m_id = id;
    m_workers = threads;
}

// Run on new thread
void RenderThread::run() {
    while (1) {
        // Wait for work
        m_work.acquire();

        // Very expensive, but necesary to get live rendering
        Color *sum = new Color[m_render.m_width * m_render.m_height];

        m_current_samples = 0;

        for (int sample = 0; sample < m_samples; sample++) {
            m_current_samples = sample;
            // Probably not that smart
            m_pause.acquire();
            m_pause.release();

            for (unsigned y = m_id; y < m_render.m_height; y += m_workers) {
                for (unsigned x = 0; x < m_render.m_width; x++) {
                    auto index = x + y * m_render.m_width;
                    sum[index] += m_render.render(m_render.m_width - x, m_render.m_height - y, 1);

                    m_writebuffer[index] = colorToUint32(sum[index] / (sample+1));
                }
            }

        }

        // Signal done
        m_lock.release();
        emit done(m_id);
    }
}

void RenderThread::pause() {
    m_pause.acquire();
}

void RenderThread::resume() {
    m_pause.release();
}

int RenderThread::render(QRgb *buffer, unsigned samples) {
    // Check if already running
    if (!m_lock.tryAcquire()) {
        return 1;
    }
    m_writebuffer = buffer;
    m_samples = samples;
    m_work.release();

    return 0;
}

unsigned RenderThread::stop() {
    stopAt(m_current_samples);
    return m_current_samples;
}

void RenderThread::stopAt(int at) {
    m_samples = at;
}

// Running on main thread
unsigned RenderThread::current_samples() {
    // No sync should not be a problem here.
    return m_current_samples;
}

RenderCoordinator::RenderCoordinator(QObject *parent, DrawWidget &target, Renderer r, QLabel *status) 
    : QObject(parent),
    m_target(target),
    m_renderer(r),
    m_timer(this)
{
    m_status = status;

    // Create and start workers
    for (int i = 0; i < 4; i++) {
        auto thread = new RenderThread(m_renderer, 4, this, i);

        thread->start();
        QObject::connect(thread, &RenderThread::done, this, &RenderCoordinator::workerDone);

        m_workers.push_back(thread);
    }

    render();

}

void RenderCoordinator::render() {
    m_started = 0;
    for (auto thd : m_workers) {
        thd->render(m_target.m_drawbuffer, 20);
        m_started++;
    }

    m_state = running;
    updateUi();

    QObject::connect(&m_timer, &QTimer::timeout, this, &RenderCoordinator::updateUi);

    m_timer.start(500);
}

void RenderCoordinator::stop() {
    unsigned max = 0;
    for (auto thd : m_workers) {
        thd->pause();

        auto val = thd->current_samples();
        if (val>max) {
            max = val;
        }
    }

    std::cout << max << std::endl;

    for (auto thd : m_workers) {
        thd->stopAt(max+1);
        thd->resume();
    }

    m_state = stopping;
    updateUi();
}

void RenderCoordinator::workerDone(unsigned workerid) {
    std::cout << "Worker " << workerid << " done!" << std::endl;
    if (--m_started) {
        return;
    }
    std::cout << "All done :-)" << std::endl;

    // All workers are done
    m_state = stopped;
    m_timer.stop();
    updateUi();
}

unsigned RenderCoordinator::calcStats(unsigned *max, unsigned *min, double *avg) {
    unsigned count = 0;
    unsigned sum = 0;
    for (auto thd : m_workers) {
        auto val = thd->current_samples();
        if (min && (val < *min || !count)) {
            *min = val;
        }
        if (max && (val > *max || !count)) {
            *max = val;
        }

        sum += val;
        count++;
    }

    if (avg) {
        *avg = (double)sum / count;
    }

    return count;
}


void RenderCoordinator::updateUi() {
    m_target.repaint();

    if (!m_status) {
        return;
    }

    // Gather statictics from workers
    unsigned max;
    unsigned min;
    double avg;
    unsigned count = calcStats(&max, &min, &avg);

    std::ostringstream status;
    status << states[m_state] << 
        " Threads: " << count <<
        " Max: " << max << " samples" <<
        " Min: " << min << " samples" <<
        " Avg: " << avg << " samples";

    m_status->setText(QString::fromStdString(status.str()));
}