-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhotcold.cpp
More file actions
189 lines (166 loc) · 4.82 KB
/
Copy pathhotcold.cpp
File metadata and controls
189 lines (166 loc) · 4.82 KB
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
// Hot Cold data separation
#include "common.h"
const int NUM_PARTICLES = 10000;
const int FRAMES_PER_SECOND = 60;
const int NUM_UPDATES = FRAMES_PER_SECOND * 10; // ten seconds of particle updates at 60fps;
const float UPDATE_DELTA = 1000.0f / FRAMES_PER_SECOND; // delta in ms
struct particle_buffer_Simple {
struct particle {
Vec3 pos;
Vec3 velocity;
float lifetime;
uint32_t colour;
float size;
uint32_t materialOrUVLookupData;
};
particle *p;
particle_buffer_Simple() {
p = (particle*)malloc( sizeof(particle) * NUM_PARTICLES );
for( int i = 0; i < NUM_PARTICLES; ++i ) {
p[i].pos.x = (i%7)-3;
p[i].pos.y = (i%11)-5;
p[i].pos.z = (i%9)-4;
p[i].velocity.x = 2.0f;
p[i].velocity.y = 100.0f;
p[i].velocity.z = 7.0f;
p[i].lifetime = 10.0f;
p[i].colour = 0xffeebbff;
p[i].size = 0.5f;
p[i].materialOrUVLookupData = 12415;
}
}
~particle_buffer_Simple() {
free(p);
}
};
struct particle_buffer_HotColdSplit {
struct particle_hot {
Vec3 pos;
Vec3 velocity;
};
struct particle_cold {
float lifetime;
uint32_t colour;
float size;
uint32_t materialOrUVLookupData;
};
particle_hot *ph;
particle_cold *pc;
particle_buffer_HotColdSplit() {
ph = (particle_hot*)malloc( sizeof(particle_hot) * NUM_PARTICLES );
pc = (particle_cold*)malloc( sizeof(particle_cold) * NUM_PARTICLES );
for( int i = 0; i < NUM_PARTICLES; ++i ) {
ph[i].pos.x = (i%7)-3;
ph[i].pos.y = (i%11)-5;
ph[i].pos.z = (i%9)-4;
ph[i].velocity.x = 2.0f;
ph[i].velocity.y = 100.0f;
ph[i].velocity.z = 7.0f;
pc[i].lifetime = 10.0f;
pc[i].colour = 0xffeebbff;
pc[i].size = 0.5f;
pc[i].materialOrUVLookupData = 12415;
}
}
~particle_buffer_HotColdSplit() {
free(ph);
free(pc);
}
};
struct particle_buffer_ReadWriteSplit {
struct particle_read {
Vec3 velocity;
};
struct particle_write {
Vec3 pos;
};
struct particle_cold {
float lifetime;
uint32_t colour;
float size;
uint32_t materialOrUVLookupData;
};
particle_read *pr;
particle_write *pw;
particle_cold *pc;
particle_buffer_ReadWriteSplit() {
pr = (particle_read*)malloc( sizeof(particle_read) * NUM_PARTICLES );
pw = (particle_write*)malloc( sizeof(particle_write) * NUM_PARTICLES );
pc = (particle_cold*)malloc( sizeof(particle_cold) * NUM_PARTICLES );
for( int i = 0; i < NUM_PARTICLES; ++i ) {
pw[i].pos.x = (i%7)-3;
pw[i].pos.y = (i%11)-5;
pw[i].pos.z = (i%9)-4;
pr[i].velocity.x = 2.0f;
pr[i].velocity.y = 100.0f;
pr[i].velocity.z = 7.0f;
pc[i].lifetime = 10.0f;
pc[i].colour = 0xffeebbff;
pc[i].size = 0.5f;
pc[i].materialOrUVLookupData = 12415;
}
}
~particle_buffer_ReadWriteSplit() {
free(pr);
free(pw);
free(pc);
}
};
struct Data {
particle_buffer_Simple pbSimple;
particle_buffer_HotColdSplit pbHotCold;
particle_buffer_ReadWriteSplit pbReadWrite;
};
Data *gData;
void TestUpdateParticles_Simple() {
particle_buffer_Simple *pb = &gData->pbSimple;
pcg32_random_t rng;
pcg32_srandom_r(&rng, 1234, 5678);
for( int u = 0; u < NUM_UPDATES; ++u ) {
// ensure that compiler cannot optimise out delta_time, but it remains repeatable
float delta_time = pcg32_random_r_rangef(&rng, UPDATE_DELTA * 0.9f, UPDATE_DELTA * 1.1f );
for( int i = 0; i < NUM_PARTICLES; ++i ) {
particle_buffer_Simple::particle *p = pb->p+i;
p->pos += p->velocity * delta_time;
}
}
}
void TestUpdateParticles_HotColdSplit() {
particle_buffer_HotColdSplit *pb = &gData->pbHotCold;
pcg32_random_t rng;
pcg32_srandom_r(&rng, 1234, 5678);
for( int u = 0; u < NUM_UPDATES; ++u ) {
// ensure that compiler cannot optimise out delta_time, but it remains repeatable
float delta_time = pcg32_random_r_rangef(&rng, UPDATE_DELTA * 0.9f, UPDATE_DELTA * 1.1f );
for( int i = 0; i < NUM_PARTICLES; ++i ) {
particle_buffer_HotColdSplit::particle_hot *p = pb->ph+i;
p->pos += p->velocity * delta_time;
}
}
}
void TestUpdateParticles_ReadWriteSplit() {
particle_buffer_ReadWriteSplit *pb = &gData->pbReadWrite;
pcg32_random_t rng;
pcg32_srandom_r(&rng, 1234, 5678);
for( int u = 0; u < NUM_UPDATES; ++u ) {
// ensure that compiler cannot optimise out delta_time, but it remains repeatable
float delta_time = pcg32_random_r_rangef(&rng, UPDATE_DELTA * 0.9f, UPDATE_DELTA * 1.1f );
for( int i = 0; i < NUM_PARTICLES; ++i ) {
particle_buffer_ReadWriteSplit::particle_read *pr = pb->pr+i;
particle_buffer_ReadWriteSplit::particle_write *pw = pb->pw+i;
pw->pos += pr->velocity * delta_time;
}
}
}
int main() {
Data data;
gData = &data;
Test tests[] = {
(Test){ TestUpdateParticles_Simple, "Simple struct array" },
(Test){ TestUpdateParticles_HotColdSplit, "HotCold split struct array" },
(Test){ TestUpdateParticles_ReadWriteSplit, "ReadWrite split struct array" },
};
printf("Trialling with %i particles over %i updates\n", NUM_PARTICLES, NUM_UPDATES );
RunTests( tests );
return 0;
}