-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0010tasks-during-render.diff
394 lines (374 loc) · 15.4 KB
/
0010tasks-during-render.diff
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
diff -bNur 0009modify-instances-count/data_bulk.c 0010tasks-during-render/data_bulk.c
--- 0009modify-instances-count/data_bulk.c 2019-03-03 15:08:52.176914583 -0600
+++ 0010tasks-during-render/data_bulk.c 2019-03-03 15:08:52.180913714 -0600
@@ -263,12 +263,14 @@
offsetof(struct data_model_index, cube) / sizeof(uint32_t),
sizeof(data_model_index->cube) / sizeof(uint32_t),
0,
+ 0.5f,
},
{
"Bullet",
offsetof(struct data_model_index, other_cube) / sizeof(uint32_t),
sizeof(data_model_index->other_cube) / sizeof(uint32_t),
1,
+ 0.5f,
},
};
diff -bNur 0009modify-instances-count/data.h 0010tasks-during-render/data.h
--- 0009modify-instances-count/data.h 2019-03-03 15:08:52.176914583 -0600
+++ 0010tasks-during-render/data.h 2019-03-03 15:08:52.180913714 -0600
@@ -15,6 +15,7 @@
void data_init(VkExtent2D extent);
void data_update();
+void data_exit();
// Maximum number of bones per vertex
#define MAX_BONES_PER_VERTEX 4
@@ -39,6 +40,7 @@
uint32_t first;
uint32_t count;
size_t material;
+ float radius;
} data_model_t;
extern data_model_t data_models[];
diff -bNur 0009modify-instances-count/libdata.c 0010tasks-during-render/libdata.c
--- 0009modify-instances-count/libdata.c 2019-03-03 15:08:52.180913714 -0600
+++ 0010tasks-during-render/libdata.c 2019-03-03 15:08:52.180913714 -0600
@@ -1,7 +1,10 @@
#include "data_bulk.h"
+#include <asm/errno.h>
#include <assert.h>
#include <math.h>
+#include <pthread.h>
#include <stdbool.h>
+#include <stdio.h>
#include <string.h>
inline static void player_shoot();
@@ -63,12 +66,19 @@
const size_t data_material_shaders_count =
sizeof(data_material_shaders) / sizeof(data_material_shaders[0]);
+typedef struct
+{
+ float mass;
+ float speed;
+ bool solid;
+} instance_closure_t;
+
void shoot_forward(data_model_instance_t *ptr, double t)
{
mat4 rot0,
*model =
data_materials[data_models[ptr->model].material].get_model(ptr->ubo);
- glm_translate_z(*model, 3 * t);
+ glm_translate_z(*model, ((instance_closure_t *)ptr->closure)->speed * t);
glm_rotate_z(*model, t, rot0);
glm_mat4_copy(rot0, *model);
}
@@ -76,14 +86,21 @@
inline static void setup_model_instance(data_model_instance_t *inst);
#define SUBO data_model_shared_ubo
struct shared_ubo *SUBO = NULL;
+pthread_mutex_t data_data_mutex = PTHREAD_MUTEX_INITIALIZER;
inline static void player_shoot()
{
+ static const instance_closure_t bullet_closure = {
+ 0.02f,
+ 3.0f,
+ true,
+ };
+
data_model_instance_t *this_instance = NULL,
bullet_instance = {
"Bullet Object",
1,
shoot_forward,
- NULL,
+ &bullet_closure,
{NULL, NULL},
NULL,
VK_NULL_HANDLE,
@@ -110,7 +127,9 @@
}
}
+ assert((!pthread_mutex_lock(&data_data_mutex)) && "Player Shoot");
list_add_tail(&this_instance->list, &data_model_instances);
+ assert((!pthread_mutex_unlock(&data_data_mutex)) && "Player Shoot");
}
typedef struct
@@ -125,13 +144,120 @@
"Object",
0,
NULL,
- NULL,
+ &(instance_closure_t){
+ 1.0f,
+ 0.0f,
+ true,
+ },
{&(data_model_instances), &(data_model_instances)},
NULL,
VK_NULL_HANDLE,
},
};
+bool intersect(vec3 amin, vec3 amax, vec3 bmin, vec3 bmax)
+{
+ return (amin[0] <= bmax[0] && amax[0] >= bmin[0]) &&
+ (amin[1] <= bmax[1] && amax[1] >= bmin[1]) &&
+ (amin[2] <= bmax[2] && amax[2] >= bmin[2]);
+}
+
+pthread_t while_rendering_0;
+pthread_cond_t data_data_cond = PTHREAD_COND_INITIALIZER;
+#define MAX_COLLISIONS (64 * 64)
+typedef struct
+{
+ data_model_instance_t *x, *y;
+} collision_t;
+collision_t collisions[MAX_COLLISIONS];
+size_t collisions_count = 0;
+pthread_mutex_t collisions_mutex = PTHREAD_MUTEX_INITIALIZER;
+void *while_rendering_0_func(void *vargp)
+{
+ while (!glfwWindowShouldClose(data_window))
+ {
+
+ assert((!pthread_mutex_lock(&data_data_mutex)) && "While Rendering 0");
+ pthread_cond_wait(&data_data_cond, &data_data_mutex);
+
+ size_t instance_count = 0;
+ struct list_head *ptr;
+ list_for_each(ptr, &data_model_instances) instance_count++;
+ {
+ struct
+ {
+ data_model_instance_t *id;
+ mat4 model;
+ float radius;
+ vec4 pos;
+ vec3 pos_high;
+ vec3 pos_low;
+ } _data[instance_count];
+
+ {
+ size_t i = 0;
+ data_model_instance_t *ptr;
+ list_for_each_entry(ptr, &data_model_instances, list)
+ {
+ if (!((instance_closure_t *)ptr->closure)->solid)
+ {
+ --instance_count;
+ continue;
+ }
+ _data[i].id = ptr;
+ glm_mat4_copy(
+ *(data_materials[data_models[ptr->model].material].get_model(
+ ptr->ubo)),
+ _data[i].model);
+ _data[i++].radius = data_models[ptr->model].radius;
+ }
+ }
+
+ assert((!pthread_mutex_unlock(&data_data_mutex)) && "While Rendering 0");
+
+ for (size_t i = 0; i < instance_count; i++)
+ {
+ glm_mat4_mulv(_data[i].model, (vec4){0.0f, 0.0f, 0.0f, 1.0f},
+ _data[i].pos);
+ for (size_t a = 0; a < 3; a++)
+ {
+ _data[i].pos_high[a] = _data[i].pos[a] + _data[i].radius;
+ _data[i].pos_low[a] = _data[i].pos[a] - _data[i].radius;
+ }
+ }
+
+ assert((!pthread_mutex_lock(&collisions_mutex)) && "While Rendering 0");
+
+ collisions_count = 0;
+
+ for (size_t x = 0; x < instance_count; x++)
+ {
+ if (x + 1 < instance_count)
+ {
+ for (size_t y = x + 1; y < instance_count; y++)
+ {
+ if (intersect(_data[x].pos_low, _data[x].pos_high, _data[y].pos_low,
+ _data[y].pos_high))
+ {
+ if (glm_vec_distance(_data[x].pos, _data[y].pos) -
+ _data[x].radius - _data[y].radius <
+ 0)
+ {
+ assert((++collisions_count < MAX_COLLISIONS) &&
+ "Overflow collisions");
+ collisions[collisions_count - 1] =
+ (collision_t){_data[x].id, _data[y].id};
+ }
+ }
+ }
+ }
+ }
+ }
+ assert((!pthread_mutex_unlock(&collisions_mutex)) && "While Rendering 0");
+ }
+ return (NULL);
+}
+
uniform_map_t starting_map;
struct list_head uniform_maps = {&(starting_map.list), &(starting_map.list)};
uniform_map_t starting_map = {
@@ -241,6 +367,9 @@
setup_model_instance(inst);
ubo_0 = inst->ubo;
+ assert(
+ !pthread_create(&while_rendering_0, NULL, while_rendering_0_func, NULL));
+
glfwSetKeyCallback(data_window, key_callback);
{
@@ -276,6 +405,10 @@
update_vp();
previous_frame = glfwGetTime();
+
+ assert((!pthread_mutex_lock(&data_data_mutex)) && "Init");
+ assert((!pthread_cond_broadcast(&data_data_cond)) && "Init");
+ assert((!pthread_mutex_unlock(&data_data_mutex)) && "Init");
}
inline static void handle_input(double this_frame)
@@ -397,12 +530,88 @@
}
}
+inline static void process_collisions()
+{
+ for (size_t i = 0; i < collisions_count; i++)
+ {
+ data_model_instance_t *ids[2];
+ vec3 pos[2], zrot[2];
+ float zop[2], zadj[2];
+ mat4 *model[2];
+ ids[0] = collisions[i].x;
+ ids[1] = collisions[i].y;
+ for (size_t a = 0; a < 2; a++)
+ {
+ /* TODO: Skip no-longer available objects */
+ model[a] = data_materials[data_models[ids[a]->model].material].get_model(
+ ids[a]->ubo);
+ _glm_decompose(*model[a], pos[a], NULL, zrot[a], &zop[a], &zadj[a]);
+ }
+ {
+ vec3 N /* Collision */, add0[2] /* overlapping points */;
+ glm_vec_sub(pos[0], pos[1], N);
+ for (size_t a = 0; a < 2; a++)
+ {
+ vec3 scale0;
+ float table[] = {
+ -data_models[ids[a]->model].radius,
+ data_models[ids[a]->model].radius,
+ };
+ glm_vec_scale_as(N, table[a], scale0);
+ glm_vec_add(scale0, pos[a], add0[a]);
+ }
+ vec3 sub1 /* overlapping influence */;
+ glm_vec_sub(add0[0], add0[1], sub1);
+ {
+#define MASS(x) ((instance_closure_t *)ids[x]->closure)->mass
+#define SPEED(x) ((instance_closure_t *)ids[x]->closure)->speed
+ vec3 scale0, scale1, add0[2];
+ float div0 = 1 / (MASS(0) + MASS(1));
+ glm_vec_scale(zrot[0], SPEED(0) * (MASS(0) - MASS(1)), scale0);
+ glm_vec_scale(zrot[1], SPEED(1) * 2 * MASS(1), scale1);
+ glm_vec_add(scale0, scale1, add0[0]);
+ glm_vec_scale(zrot[1], SPEED(1) * (MASS(1) - MASS(0)), scale0);
+ glm_vec_scale(zrot[0], SPEED(0) * 2 * MASS(0), scale1);
+ glm_vec_scale(add0[0], div0, zrot[0]);
+ glm_vec_add(scale0, scale1, add0[1]);
+ glm_vec_scale(add0[1], div0, zrot[1]);
+ }
+ for (size_t a = 0; a < 2; a++)
+ {
+ vec3 scale1, add1, rot;
+ float table[] = {
+ -1.2f * MASS(0) / (MASS(0) + MASS(1)),
+ 1.2f * MASS(1) / (MASS(0) + MASS(1)),
+ };
+#undef MASS
+#undef SPEED
+ glm_vec_scale(sub1, table[a], scale1);
+ glm_vec_add(scale1, pos[a], add1);
+ glm_vec_copy(add1, pos[a]);
+
+ _glm_atan3(zrot[a], zop[a], zadj[a], rot);
+
+ {
+ mat4 trans0, rot0, rot1, rot2;
+ glm_rotate_x(GLM_MAT4_IDENTITY, rot[0], rot0);
+ glm_rotate_y(rot0, rot[1], rot1);
+ glm_rotate_z(rot1, rot[2], rot2);
+ glm_translate_make(trans0, pos[a]);
+ glm_mat4_mul(trans0, rot2, *model[a]);
+ }
+ }
+ }
+ }
+ collisions_count = 0;
+}
+
void data_update()
{
double this_frame = glfwGetTime();
handle_input(this_frame);
+ assert((!pthread_mutex_lock(&data_data_mutex)) && "Update");
data_model_instance_t *ptr;
list_for_each_entry(ptr, &data_model_instances, list)
take_turn(ptr, this_frame);
@@ -416,4 +625,32 @@
}
previous_frame = this_frame;
+
+ int err;
+ err = pthread_mutex_trylock(&collisions_mutex);
+ switch (err)
+ {
+ case 0:
+ if (collisions_count)
+ process_collisions();
+
+ assert((!pthread_cond_broadcast(&data_data_cond)) && "Update");
+ assert((!pthread_mutex_unlock(&collisions_mutex)) && "Update");
+
+ break;
+ case EBUSY:
+ break;
+ default:
+ assert((!err) && "pthread_mutex_trylock(&collisions_mutex): Update");
+ }
+
+ assert((!pthread_mutex_unlock(&data_data_mutex)) && "Update");
+}
+
+void data_exit()
+{
+ assert((!pthread_mutex_lock(&collisions_mutex)) && "Exit");
+ assert((!pthread_cond_broadcast(&data_data_cond)) && "Exit");
+ assert((!pthread_mutex_unlock(&collisions_mutex)) && "Exit");
+ assert(!pthread_join(while_rendering_0, NULL));
}
diff -bNur 0009modify-instances-count/Makefile 0010tasks-during-render/Makefile
--- 0009modify-instances-count/Makefile 2019-03-03 15:08:52.176914583 -0600
+++ 0010tasks-during-render/Makefile 2019-03-03 15:08:52.180913714 -0600
@@ -8,7 +8,7 @@
include ../misc-targets.mk
DEPENDS = libdata.so
-vulkan: $(MYLDLIBS) $(DEPENDS)
+vulkan: -lpthread $(MYLDLIBS) $(DEPENDS)
LIBDEPENDS_DYNAMIC = 0_vert.spv 0_frag.spv
data_dynamic.o: $(LIBDEPENDS_DYNAMIC) data.h
LIBDEPENDS = data_bulk.o data_dynamic.o
diff -bNur 0009modify-instances-count/vulkan.c 0010tasks-during-render/vulkan.c
--- 0009modify-instances-count/vulkan.c 2019-03-03 15:08:52.472850282 -0600
+++ 0010tasks-during-render/vulkan.c 2019-03-03 15:08:52.472850282 -0600
@@ -1629,6 +1629,8 @@
vkDestroyInstance(instance, NULL);
instance = VK_NULL_HANDLE;
+ data_exit();
+
glfwDestroyWindow(data_window);
glfwTerminate();