even more optimization
This commit is contained in:
parent
e66093d684
commit
04a03877a2
3 changed files with 109 additions and 40 deletions
82
cubes.c
82
cubes.c
|
@ -1,3 +1,5 @@
|
|||
// the messiest part of this whole codebase
|
||||
|
||||
#include "const.h"
|
||||
#include "gen.h"
|
||||
#include <pthread.h>
|
||||
|
@ -12,8 +14,13 @@ struct args {
|
|||
int a;
|
||||
int i;
|
||||
int a3;
|
||||
int *i2;
|
||||
};
|
||||
|
||||
int ox = -1024;
|
||||
int oy = -1024;
|
||||
int oz = -1024;
|
||||
|
||||
Chunk *chunk_old = NULL;
|
||||
int read_order = 0;
|
||||
|
||||
|
@ -69,31 +76,36 @@ void *gen_chunk(void *args) {
|
|||
int a3 = args2.a3;
|
||||
int a2 = args2.a;
|
||||
int a = a2 % a3;
|
||||
int *i2 = args2.i2;
|
||||
|
||||
int x2 = args2.pos.pos[0];
|
||||
int y2 = args2.pos.pos[1];
|
||||
int z2 = args2.pos.pos[2];
|
||||
|
||||
Vertex *cube = args2.cube;
|
||||
Vertex *text = args2.text;
|
||||
|
||||
Vertex vNul = {{-10.0, -10.0, -10.0}};
|
||||
if (x2 < 0 || y2 < 0 || z2 < 0) {
|
||||
for (int a = i; a < args2.i + (BLOCK_ALL * 18) * a3; a += 6 * a3) {
|
||||
cube[a] = vNul;
|
||||
text[a] = vNul;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// int is_text = args2.is_text;
|
||||
|
||||
int the_j = (((z2 / CHUNK_LENGTH) % CHUNK_DIAMETER_H) +
|
||||
int the_j = a2; /*(((z2 / CHUNK_LENGTH) % CHUNK_DIAMETER_H) +
|
||||
((x2 / CHUNK_LENGTH) % CHUNK_DIAMETER_H) * CHUNK_DIAMETER_H +
|
||||
((y2 / CHUNK_LENGTH) % CHUNK_DIAMETER_V) * CHUNK_DIAMETER_H *
|
||||
CHUNK_DIAMETER_H) %
|
||||
(CHUNK_ALL * CBUF_ALL);
|
||||
(CHUNK_ALL);*/
|
||||
|
||||
Chunk old_chunk = chunk_old[the_j > 0 ? the_j : 0];
|
||||
Chunk chunk = fetch_chunk(x2 / CHUNK_LENGTH, y2 / CHUNK_LENGTH,
|
||||
z2 / CHUNK_LENGTH, a, 0, read_order);
|
||||
|
||||
int cached =
|
||||
(old_chunk.x == x2 / CHUNK_LENGTH && old_chunk.y == y2 / CHUNK_LENGTH &&
|
||||
old_chunk.z == z2 / CHUNK_LENGTH && old_chunk.exists != 0);
|
||||
Chunk chunk =
|
||||
cached ? old_chunk
|
||||
: fetch_chunk(x2 / CHUNK_LENGTH, y2 / CHUNK_LENGTH,
|
||||
z2 / CHUNK_LENGTH, a, old_chunk.exists, read_order);
|
||||
|
||||
if (chunk.exists != 0 && the_j > 0)
|
||||
if (chunk.exists != 0 && the_j > -1)
|
||||
chunk_old[the_j] = chunk;
|
||||
|
||||
printf("Loading world... (%i / %i)\n", a2, CHUNK_ALL);
|
||||
|
@ -119,11 +131,16 @@ void *gen_chunk(void *args) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int a = i; a < args2.i + (BLOCK_ALL * 18) * a3; a += 6 * a3) {
|
||||
cube[a] = vNul;
|
||||
text[a] = vNul;
|
||||
}
|
||||
return (void *)i;
|
||||
}
|
||||
|
||||
int gen_cubes(Vertex *cube, Vertex *text, int x, int y, int z, int r,
|
||||
int is_purge) {
|
||||
int gen_cubes(Vertex *cube, Vertex *text, Vertex *cubeO, Vertex *textO, int x,
|
||||
int y, int z, int r, int is_purge, int *i2) {
|
||||
Vertex noth = {{-1, -1, -1}};
|
||||
for (int i = 0; i < CBLOCK_ALL * 18; i++) {
|
||||
cube[i] = noth;
|
||||
|
@ -150,10 +167,11 @@ int gen_cubes(Vertex *cube, Vertex *text, int x, int y, int z, int r,
|
|||
}
|
||||
|
||||
if (!chunk_old) {
|
||||
chunk_old = calloc((CHUNK_ALL * CBUF_ALL), sizeof(struct Chunk));
|
||||
chunk_old = calloc((CHUNK_ALL), sizeof(struct Chunk));
|
||||
}
|
||||
read_order = !read_order;
|
||||
pthread_t thread_id[a3];
|
||||
int j = 0;
|
||||
for (int y2 = y - (CHUNK_RADIUS_V + r) * CHUNK_LENGTH;
|
||||
y2 <= y + (CHUNK_RADIUS_V + r) * CHUNK_LENGTH; y2 += CHUNK_LENGTH) {
|
||||
|
||||
|
@ -182,10 +200,26 @@ int gen_cubes(Vertex *cube, Vertex *text, int x, int y, int z, int r,
|
|||
args[a2].a = a;
|
||||
args[a2].i = i;
|
||||
args[a2].a3 = a3;
|
||||
args[a2].i2 = i2;
|
||||
|
||||
/*gen_chunk((void *)&args); */
|
||||
|
||||
pthread_create(&(thread_id[a2]), NULL, gen_chunk, (void *)&(args[a2]));
|
||||
Chunk old_chunk = chunk_old[a];
|
||||
|
||||
int cached =
|
||||
(old_chunk.x == pos.pos[0] / CHUNK_LENGTH &&
|
||||
old_chunk.y == pos.pos[1] / CHUNK_LENGTH &&
|
||||
old_chunk.z == pos.pos[2] / CHUNK_LENGTH && old_chunk.exists != 0);
|
||||
if (cached) {
|
||||
thread_return[a2] = i2[a2 / a3];
|
||||
thread_id[a2] = -1;
|
||||
} else if (pos.pos[0] < 0 || pos.pos[1] < 0 || pos.pos[2] < 0 || pos.pos[1] < (y * 0.95)) {
|
||||
thread_return[a2] = -1;
|
||||
thread_id[a2] = -1;
|
||||
} else {
|
||||
pthread_create(&(thread_id[a2]), NULL, gen_chunk,
|
||||
(void *)&(args[a2]));
|
||||
}
|
||||
|
||||
i += 6;
|
||||
|
||||
|
@ -193,6 +227,8 @@ int gen_cubes(Vertex *cube, Vertex *text, int x, int y, int z, int r,
|
|||
}
|
||||
|
||||
for (int k = 0; k < a3; k++) {
|
||||
if (thread_id[k] == -1)
|
||||
continue;
|
||||
pthread_join((thread_id[k]), (void **)&thread_return[k]);
|
||||
}
|
||||
int max = 0;
|
||||
|
@ -202,10 +238,20 @@ int gen_cubes(Vertex *cube, Vertex *text, int x, int y, int z, int r,
|
|||
max = (max > thread_return[a]) ? max : thread_return[a];
|
||||
min = (min < thread_return[a]) ? min : thread_return[a];
|
||||
}
|
||||
i = max + 6;
|
||||
max += 6;
|
||||
i2[(a / a3) - 1] = max;
|
||||
if (max > i) {
|
||||
memcpy(&(cubeO[j]), &(cube[i - 6 * a3]), (max - i) * sizeof(Vertex));
|
||||
memcpy(&(textO[j]), &(text[i - 6 * a3]), (max - i) * sizeof(Vertex));
|
||||
j += max - i;
|
||||
}
|
||||
i = (BLOCK_ALL * 18) * a; // i = max + 6;
|
||||
}
|
||||
}
|
||||
|
||||
ox = x;
|
||||
oy = y;
|
||||
oz = z;
|
||||
/* if (i < CBLOCK_ALL * 9 && r < MAX_R) {
|
||||
printf("Expanding radius...\n");
|
||||
return gen_cubes(cube, text, x / CHUNK_LENGTH, y / CHUNK_LENGTH,
|
||||
|
@ -213,5 +259,5 @@ int gen_cubes(Vertex *cube, Vertex *text, int x, int y, int z, int r,
|
|||
}
|
||||
*/
|
||||
|
||||
return i;
|
||||
return j; // i;
|
||||
}
|
2
cubes.h
2
cubes.h
|
@ -1,3 +1,3 @@
|
|||
#include "const.h"
|
||||
|
||||
int gen_cubes(Vertex *cube, Vertex *text, int x, int y, int z, int r, int is_purge);
|
||||
int gen_cubes(Vertex *cube, Vertex *text, Vertex *cubeO, Vertex *textO, int x, int y, int z, int r, int is_purge, int *i2);
|
57
main.c
57
main.c
|
@ -45,7 +45,8 @@ static const char *fragment_shader_text =
|
|||
" if (v_tex.x < 0.0 || v_tex.y < 0.0) { discard; }\n"
|
||||
" dist = distance(v_pos_out, center + vec3(8,8,8))/65;\n"
|
||||
" if (dist > 1.0) { dist = 1.0; }\n"
|
||||
" color=vec4(texture(sampler_tex, v_tex).rgb,1.0)*(1.0-(dist*dist)) + vec4(0.2,0.5,0.7,1.0)*(dist*dist);\n"
|
||||
" color=vec4(texture(sampler_tex, v_tex).rgb,1.0)*(1.0-(dist*dist)) + "
|
||||
"vec4(0.2,0.5,0.7,1.0)*(dist*dist);\n"
|
||||
"}\n";
|
||||
|
||||
int isCursor = 0;
|
||||
|
@ -79,9 +80,12 @@ struct args {
|
|||
int *cube_count;
|
||||
Vertex **cube;
|
||||
Vertex **text;
|
||||
Vertex **cubeO;
|
||||
Vertex **textO;
|
||||
vec3 pos;
|
||||
GLuint *vertex_buffer;
|
||||
int *is_render;
|
||||
int *i2;
|
||||
};
|
||||
|
||||
void *render_chunks(void *args) {
|
||||
|
@ -94,8 +98,9 @@ void *render_chunks(void *args) {
|
|||
*args2.is_render = 0;
|
||||
|
||||
*cube_count =
|
||||
gen_cubes(*args2.cube, *args2.text, args2.pos[0] / CHUNK_LENGTH,
|
||||
args2.pos[1] / CHUNK_LENGTH, args2.pos[2] / CHUNK_LENGTH, 0, 0);
|
||||
gen_cubes(*args2.cube, *args2.text, *args2.cubeO, *args2.textO,
|
||||
args2.pos[0] / CHUNK_LENGTH, args2.pos[1] / CHUNK_LENGTH,
|
||||
args2.pos[2] / CHUNK_LENGTH, 0, 0, args2.i2);
|
||||
|
||||
*args2.is_render = 2;
|
||||
|
||||
|
@ -125,9 +130,13 @@ int main(void) {
|
|||
|
||||
Vertex *cube = malloc(CTRI_ALL);
|
||||
Vertex *text = malloc(CTRI_ALL);
|
||||
Vertex *cubeO = malloc(CTRI_ALL);
|
||||
Vertex *textO = malloc(CTRI_ALL);
|
||||
int bleh = (CHUNK_DIAMETER_H + MAX_R * 2) * (CHUNK_DIAMETER_V + MAX_R * 2);
|
||||
int *i2 = calloc(bleh, sizeof(int));
|
||||
|
||||
int cube_count = gen_cubes(cube, text, 10240 / CHUNK_LENGTH, 64 / CHUNK_LENGTH,
|
||||
10240 / CHUNK_LENGTH, 0, 1);
|
||||
int cube_count = gen_cubes(cube, text, cubeO, textO, 10240 / CHUNK_LENGTH,
|
||||
64 / CHUNK_LENGTH, 10240 / CHUNK_LENGTH, 0, 1, i2);
|
||||
GLuint vertex_buffer[2];
|
||||
|
||||
GLuint vertex_array;
|
||||
|
@ -182,7 +191,8 @@ int main(void) {
|
|||
int state = cube_exists(pos[0], pos[1], pos[2], ch7, 0, 0);
|
||||
|
||||
while (1) {
|
||||
if (state != cube_exists(pos[0], pos[1], pos[2], ch7, 0, 0)) break;
|
||||
if (state != cube_exists(pos[0], pos[1], pos[2], ch7, 0, 0))
|
||||
break;
|
||||
pos[1] += (state ? 1 : -1);
|
||||
}
|
||||
|
||||
|
@ -208,8 +218,10 @@ int main(void) {
|
|||
mat4x4_identity(m);
|
||||
mat4x4_perspective(p, 45.0 / 180.0 * M_PI, ratio, 0.1, 1000);
|
||||
|
||||
vec3 direction = {cosf(cy) * sinf(cx), sinf(cy), cosf(cy) * cosf(cx)};
|
||||
vec3 right = {sinf(cx - 3.14f / 2.0f), 0, cosf(cx - 3.14f / 2.0f)};
|
||||
vec3 direction = {cosf(cy) * sinf(cx) * 0.3, sinf(cy) * 0.3,
|
||||
cosf(cy) * cosf(cx) * 0.3};
|
||||
vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.3, 0,
|
||||
cosf(cx - 3.14f / 2.0f) * 0.3};
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
||||
vec3_add(pos, pos, direction);
|
||||
|
@ -238,7 +250,7 @@ int main(void) {
|
|||
mat4x4_mul(mvp, mvp, m);
|
||||
|
||||
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
|
||||
glUniform3fv(center_location, 1, (const GLfloat *)(&(oldPos[0])));
|
||||
glUniform3fv(center_location, 1, (const GLfloat *)(&(pos[0])));
|
||||
|
||||
int ax = (int)(oldPos[0] / CHUNK_LENGTH) - (int)(pos[0] / CHUNK_LENGTH);
|
||||
int ay = (int)(oldPos[1] / CHUNK_LENGTH) - (int)(pos[1] / CHUNK_LENGTH);
|
||||
|
@ -250,18 +262,21 @@ int main(void) {
|
|||
|
||||
args.cube = &cube;
|
||||
args.text = &text;
|
||||
args.pos[0] = pos[0];
|
||||
args.cubeO = &cubeO;
|
||||
args.textO = &textO;
|
||||
args.pos[0] = pos[0] + direction[0] * CHUNK_LENGTH;
|
||||
args.pos[1] = pos[1];
|
||||
args.pos[2] = pos[2];
|
||||
args.pos[2] = pos[2] + direction[2] * CHUNK_LENGTH;
|
||||
args.vertex_buffer = vertex_buffer;
|
||||
args.is_render = &is_render;
|
||||
args.i2 = i2;
|
||||
|
||||
last_render = 120;
|
||||
last_render = 40;
|
||||
|
||||
if (thread_id == 0) {
|
||||
oldPos[0] = (int)(pos[0]/ CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[1] = (int)(pos[1]/ CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[2] = (int)(pos[2]/ CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[0] = (int)(args.pos[0] / CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[1] = (int)(args.pos[1] / CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
oldPos[2] = (int)(args.pos[2] / CHUNK_LENGTH) * CHUNK_LENGTH;
|
||||
pthread_create(&(thread_id), NULL, render_chunks, (void *)&(args));
|
||||
}
|
||||
}
|
||||
|
@ -274,11 +289,11 @@ int main(void) {
|
|||
if (is_render == 2) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(Vertex),
|
||||
text);
|
||||
textO); // text);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(Vertex),
|
||||
cube);
|
||||
cubeO); // cube);
|
||||
if (thread_id != 0)
|
||||
pthread_join(thread_id, NULL);
|
||||
thread_id = 0;
|
||||
|
@ -299,6 +314,14 @@ int main(void) {
|
|||
glBindVertexArray(vertex_array);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, cube_count);
|
||||
|
||||
/*/
|
||||
for (int ch = 0; ch < bleh; ch++) {
|
||||
int o = ch * BLOCK_ALL * 18 * (CHUNK_DIAMETER_H);
|
||||
if (i2[ch] < o) continue;
|
||||
glDrawArrays(GL_TRIANGLES, o, i2[ch] - o);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
free(cube);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue