-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.c
110 lines (100 loc) · 2.71 KB
/
thread.c
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
/*
** thread.c for raytracer2 in /home/nicolaspolomack/graphical/raytracer2
**
** Made by Nicolas Polomack
** Login <[email protected]>
**
** Started on Tue Mar 28 16:22:29 2017 Nicolas Polomack
** Last update Sat May 27 22:13:55 2017 Full Name
*/
#include <stdlib.h>
#include <pthread.h>
#include "my.h"
#include "raytracer.h"
static void *render_thread(void *arg)
{
t_thread *t;
t = (t_thread *)arg;
set_focal_dist(t);
if (t->params->config.stereo)
render_stereo_frame(t);
else if (t->params->config.ssaa > 1)
render_ssaa(t);
else if (t->params->config.tesla)
render_frame_tesla(t);
else
render_frame(t);
return (NULL);
}
void update_frame(t_window *w, t_params *params)
{
if (!params->config.bmp)
{
sfTexture_updateFromPixels(w->texture, w->buffer->pixels,
w->buffer->width, w->buffer->height, 0, 0);
sfRenderWindow_drawSprite(w->window, w->sprite, NULL);
sfRenderWindow_display(w->window);
}
}
static void set_limits(t_thread *t, t_window *w,
t_params *params, int i)
{
sfVector2i size;
size.x = params->config.end.x - params->config.offs.x;
size.y = params->config.end.y - params->config.offs.y;
if (!(i % 2))
{
t->offs.x = (size.x / (params->t_count / 2)) * (i / 2);
t->offs.y = params->config.offs.y;
}
else
{
t->offs.x = (size.x / (params->t_count / 2)) * ((i - 1) / 2);
t->offs.y = params->config.offs.y + (size.y / 2);
}
t->end.x = (t->offs.x + (size.x / (params->t_count / 2)));
t->end.y = (t->offs.y + (size.y / 2));
t->end.x += (i >= (params->t_count - 2)) ?
size.x % (params->t_count / 2) : 0;
}
static t_thread *prepare_thread(t_window *w, t_params *params, int i)
{
t_thread *t;
if ((t = malloc(sizeof(t_thread))) == NULL)
return (NULL);
t->params = params;
t->w = w;
t->id = i;
t->tesla_lvl = params->tesla_lvl;
t->start = params->start;
if ((t->depth_col = malloc(sizeof(sfColor) *
t->params->config.depth_rays *
t->params->config.depth_rays)) == NULL)
return (NULL);
return (t);
}
int init_thread(t_window *w, t_params *params)
{
int i;
t_thread *t;
i = -1;
params->t_count = get_core_count() * 2;
if ((params->t = malloc(sizeof(pthread_t) * params->t_count)) == NULL)
return (-1);
w->progress = 0;
if (params->config.bmp)
my_printf("\nProgress: 0 %%");
pthread_mutex_init(¶ms->mutex, NULL);
while (++i < params->t_count)
{
if ((t = prepare_thread(w, params, i)) == NULL)
return (-1);
set_limits(t, w, params, i);
pthread_create(&(params->t[i]), NULL, render_thread, (void *)t);
}
i = -1;
while (++i < params->t_count)
pthread_join(params->t[i], NULL);
check_render(params, w);
return (0);
}