Demo update and start counting tests times

-Fixed performance issue with multithreading and rendering
-start making better shaders (by using many macros)
-speed up rendeing when maximum objects count was reached
-remove map rendering form Snake demo, and render entities by themself
-start adding depth and color rendering parameters
-added properly names to jobs (for debugging purpses)
-starts adding multithreaded rendering
-added some math to vectors
-changes execute() to willExecute(). Probably should have different name.
This commit is contained in:
Mergul 2020-05-07 14:07:07 +02:00
parent 46aba822d0
commit 4bd5a37b5d
13 changed files with 429 additions and 198 deletions

View file

@ -9,6 +9,9 @@ import ecs_utils.gfx.buffer;
import ecs_utils.gfx.texture;
import ecs_utils.math.vector;
import bubel.ecs.block_allocator;
import bubel.ecs.vector;
import glad.gl.gl;
version = ver1;
@ -41,9 +44,10 @@ enum RenderTechnique
struct Renderer
{
//static SDL_Renderer* main_sdl_renderer;
BlockAllocator allocator;
enum MaxObjects = 1024 * 64 * 4;
enum BufferUsage = GL_STATIC_DRAW;
enum BufferUsage = GL_DYNAMIC_DRAW;
//SDL_Window* sdl_window;
//SDL_Renderer* sdl_renderer;
@ -53,8 +57,35 @@ struct Renderer
vec2 view_pos = vec2(-1,-1);
vec2 view_size = vec2(1,1);
const uint batch_size = 16_384;
//uint[2] time_queries;
struct VertexBlock
{
float[] batch_vertices;
ushort[] batch_indices;
void* memory;
uint itmes = 0;
}
VertexBlock getBlock()
{
VertexBlock block;
block.memory = allocator.getBlock();
block.batch_vertices = (cast(float*)block.memory)[0 .. 1];
return block;
}
struct Thread
{
Vector!VertexBlock block;
RenderData[] render_list;
}
Thread[] threads;
Buffer[2] ubos;
int block_alignment = 1;
int block_max_size = 16384;
@ -253,6 +284,8 @@ struct Renderer
SDL_Log("Uniform block alignment: %u",block_alignment);
SDL_Log("Uniform block max size: %u",block_max_size);
SDL_Log("Data offset: %u",data_offset);
allocator = BlockAllocator(1245184, 32);
}
}
@ -261,12 +294,13 @@ struct Renderer
}
void draw(Texture tex, vec2 pos, vec2 size, vec4 coords, float angle = 0, uint material_id = 0, uint mesh_id = 0)
void draw(Texture tex, vec2 pos, vec2 size, vec4 coords, short depth = 0, uint color = uint.max, float angle = 0, uint material_id = 0, uint mesh_id = 0, uint thread_id = 0)
{
__draw(this,tex,pos,size,coords,angle,material_id,mesh_id);
if(item_id >= MaxObjects)return;
__draw(this,tex,pos,size,coords,depth,color,angle,material_id,mesh_id,thread_id);
}
private static void __draw_sdl(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, float angle, uint material_id, uint mesh_id)
private static void __draw_sdl(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, short depth, uint color, float angle, uint material_id, uint mesh_id, uint thread_id)
{
/*with(this_)
{
@ -286,7 +320,7 @@ struct Renderer
}*/
}
private static void __draw_gl(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, float angle, uint material_id, uint mesh_id)
private static void __draw_gl(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, short depth, uint color, float angle, uint material_id, uint mesh_id, uint thread_id)
{
//import core.stdc.string;
with(this_)
@ -333,13 +367,13 @@ struct Renderer
}
}
private static void __draw_gl_vbo_batch(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, float angle, uint material_id, uint mesh_id)
private static void __draw_gl_vbo_batch(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, short depth, uint color, float angle, uint material_id, uint mesh_id, uint thread_id = 0)
{
import ecs_utils.gfx.config;
//import core.stdc.string;
with(this_)
{
if(item_id >= MaxObjects)return;
//if(item_id >= MaxObjects)return;
//pos += view_pos;
size.x *= view_size.x;
size.y *= view_size.y;
@ -400,7 +434,7 @@ struct Renderer
batch_vertices[item_id*16+14] = GfxConfig.meshes[mesh_id].vertices[14] * coords.z + coords.x;
batch_vertices[item_id*16+15] = GfxConfig.meshes[mesh_id].vertices[15] * coords.w + coords.y;
uint ind_id = item_id % 16_384;
uint ind_id = item_id % batch_size;
batch_indices[item_id*6] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[0] + ind_id*4);
batch_indices[item_id*6+1] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[1] + ind_id*4);
@ -575,7 +609,7 @@ struct Renderer
//glBeginQuery(GL_TIME_ELAPSED, time_queries[0]);
if(technique == Technique.vbo_batch)
{
uint items = item_id/16_384+1;
uint items = item_id/batch_size+1;
foreach(i; 0..items)
{
if(material_id != render_list[i].material_id)
@ -591,16 +625,16 @@ struct Renderer
render_list[i].texture.bind();
}
uint instance_count = 16_384;
if((i+1)*16_384 > item_id)
uint instance_count = batch_size;
if((i+1)*batch_size > item_id)
{
instance_count = item_id%16_384;
instance_count = item_id%batch_size;
}
glVertexAttribPointer(0,2,GL_FLOAT,false,16,cast(void*)(i*16_384*4*16));
glVertexAttribPointer(1,2,GL_FLOAT,false,16,cast(void*)(i*16_384*4*16+8));
glVertexAttribPointer(0,2,GL_FLOAT,false,16,cast(void*)(i*batch_size*4*16));
glVertexAttribPointer(1,2,GL_FLOAT,false,16,cast(void*)(i*batch_size*4*16+8));
glDrawElements(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2));
glDrawElements(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*batch_size*6*2));
//glDrawElementsBaseVertex(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2),i*16_384*4);
}
@ -803,7 +837,7 @@ struct Renderer
view_pos = (pos - size * 0.5) * view_size;
}
__gshared void function(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, float angle, uint material_id, uint mesh_id) __draw;
__gshared void function(ref Renderer this_, Texture tex, vec2 pos, vec2 size, vec4 coords, short depth, uint color, float angle, uint material_id, uint mesh_id, uint thread_id) __draw;
__gshared void function(ref Renderer this_) __present;
__gshared void function(ref Renderer this_) __clear;
__gshared void function(ref Renderer this_) __initialize;