Common update:
-added multiple new function to allocate template and add entity -updated README.md (complete initial version) -empty components now don't take memory -fixedd small bug with TestRunner -added many new tests (HashMap, Vector, EntityMeta, ...) -added default hashing function to HashMap -fixed critical bug with adding entities -fixed small bug with adding entity with remplacement components -added asserts into code to better bug detection -small performance improvement for events -added ComponentRef structure which contain data pointer and componentID -remove EntityID from Event structure -now events are handled before removing entiteis -fixed GDC compilation -fixed rendering of rotated sprites -added weapons as separate entities to space ship and others -added Tower enemy to SpaceInvaders demo -added Boss to SpaceInvaders demo (boss has four tower attached to it) -Boss towers shoot multiple bullets upon death -fixed critical bug with demos switching -fixed critical bug related to adding/removing entities form inside onAdd/onRemove entity callback -added animation support -added particles sypport and particles for firing and explostions, and more -multithreaded rendering now has same rendering order as singlethreaded -application automaticallu detect host CPU threads count -added upgrades to SPaceInvaders demo -fixed texture memory freeing -improved documentation -improved multithreaded performance -improve shader code -fixed registration issue -some additional performance improvements -added depth and colors to rendering parameters -jobs now has names corresponding to their systems -change execute() -> willExecute() -added EntityMeta structure to speedup getting fetching components form entity -improved multithreading rendering -added possibility tio change number of threads runtime -added bullets collision detection in SpaceInvaders demo -some CI changes -added VBO batch rendering (current default, no render mode switch yet) -fixed camera positioning calculation -fixed buffer issue with WebGL -added viewport scalling (at least 300 pixels height). Pixels are scalled if screen is bigger. -center demos gameplay area -added fullpage html template for Emscripten build -added many new sprites to atlas -fixed critical bug with CPU usage in multithreaded mode -snake render tile coresponding to body part -snake is destroyed after collision and emit some particles -added some functionality to vectors -fixed documentation issue in Manager.d -more minor code changes and cleanup
This commit is contained in:
parent
2ddb97e9ce
commit
024356df9b
62 changed files with 5918 additions and 1673 deletions
|
|
@ -2,14 +2,17 @@ module ecs_utils.gfx.renderer;
|
|||
|
||||
import bindbc.sdl;
|
||||
|
||||
import ecs.std;
|
||||
import bubel.ecs.std;
|
||||
|
||||
//import ecs_utils.core : Backend;
|
||||
import ecs_utils.gfx.buffer;
|
||||
import ecs_utils.gfx.texture;
|
||||
import ecs_utils.math.vector;
|
||||
|
||||
import glad.gl.gl;
|
||||
import bubel.ecs.block_allocator;
|
||||
import bubel.ecs.vector;
|
||||
version(WebAssembly)import glad.gl.gles2;
|
||||
else import glad.gl.gl;
|
||||
|
||||
version = ver1;
|
||||
/*version(ver5)version = vv2;
|
||||
|
|
@ -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,136 @@ struct Renderer
|
|||
vec2 view_pos = vec2(-1,-1);
|
||||
vec2 view_size = vec2(1,1);
|
||||
|
||||
enum block_size = 2^^16;
|
||||
enum batch_size = block_size/68;//963;//16_384;
|
||||
//uint[2] time_queries;
|
||||
|
||||
struct VertexBlock
|
||||
{
|
||||
enum max_items = batch_size;//963;
|
||||
byte[] batch_vertices;
|
||||
ushort[] batch_indices;
|
||||
void* memory;
|
||||
uint items = 0;
|
||||
}
|
||||
|
||||
Mutex* get_block_mutex;
|
||||
Mutex* block_stack_mutex;
|
||||
|
||||
VertexBlock getBlock()
|
||||
{
|
||||
VertexBlock block;
|
||||
get_block_mutex.lock();
|
||||
block.memory = allocator.getBlock();
|
||||
get_block_mutex.unlock();
|
||||
block.batch_vertices = (cast(byte*)block.memory)[0 .. VertexBlock.max_items * 4 * 14];
|
||||
block.batch_indices = (cast(ushort*)block.memory)[VertexBlock.max_items * 4 * 7 .. VertexBlock.max_items * (4 * 7 + 6)];
|
||||
return block;
|
||||
}
|
||||
|
||||
Vector!VertexBlock blocks;
|
||||
uint current_block = 0;
|
||||
uint render_blocks = 0;
|
||||
|
||||
void pushBlock(VertexBlock block)
|
||||
{
|
||||
block_stack_mutex.lock();
|
||||
prepared_items += block.items;
|
||||
blocks.add(block);
|
||||
render_blocks++;
|
||||
block_stack_mutex.unlock();
|
||||
}
|
||||
|
||||
bool isRemainingBlocks()
|
||||
{
|
||||
if(render_blocks <= current_block)return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
VertexBlock fetchBlock()
|
||||
{
|
||||
block_stack_mutex.lock();
|
||||
VertexBlock block = blocks[current_block];
|
||||
current_block++;
|
||||
block_stack_mutex.unlock();
|
||||
return block;
|
||||
}
|
||||
|
||||
void freeBlocks()
|
||||
{
|
||||
/*block_stack_mutex.lock();
|
||||
render_blocks = 0;
|
||||
current_block = 0;
|
||||
foreach(VertexBlock block; blocks)
|
||||
{
|
||||
allocator.freeBlock(block.memory);
|
||||
}
|
||||
blocks.clear;
|
||||
prepared_items=0;
|
||||
draw_list.clear();
|
||||
block_stack_mutex.unlock();*/
|
||||
foreach(ref Thread thread; threads)
|
||||
{
|
||||
foreach(VertexBlock block; thread.blocks)
|
||||
{
|
||||
allocator.freeBlock(block.memory);
|
||||
}
|
||||
thread.blocks.clear();
|
||||
}
|
||||
render_blocks = 0;
|
||||
current_block = 0;
|
||||
prepared_items = 0;
|
||||
draw_list.clear();
|
||||
}
|
||||
|
||||
void pushData()
|
||||
{
|
||||
foreach(ref Thread thread; threads)
|
||||
{
|
||||
foreach(VertexBlock block; thread.blocks)
|
||||
{
|
||||
uint items = block.items;
|
||||
if(items + item_id >= MaxObjects)items = MaxObjects - item_id;
|
||||
batch_vbo[0].bufferSubData(Buffer.BindTarget.array,items*4*14,item_id*4*14,block.batch_vertices.ptr);
|
||||
batch_ibo[0].bufferSubData(Buffer.BindTarget.element_array,items*2*6,item_id*2*6,block.batch_indices.ptr);
|
||||
draw_list.add(DrawCall(item_id,items));
|
||||
item_id += items;
|
||||
}
|
||||
//thread.blocks.clear();
|
||||
}
|
||||
//if(!isRemainingBlocks())return;
|
||||
/* while(isRemainingBlocks())
|
||||
{
|
||||
VertexBlock block = fetchBlock();
|
||||
uint items = block.items;
|
||||
if(items + item_id >= MaxObjects)items = MaxObjects - item_id;
|
||||
batch_vbo[0].bufferSubData(Buffer.BindTarget.array,items*4*14,item_id*4*14,block.batch_vertices.ptr);
|
||||
batch_ibo[0].bufferSubData(Buffer.BindTarget.element_array,items*2*6,item_id*2*6,block.batch_indices.ptr);
|
||||
draw_list.add(DrawCall(item_id,items));
|
||||
item_id += items;
|
||||
}*/
|
||||
}
|
||||
|
||||
void pushThreadsBlocks()
|
||||
{
|
||||
foreach(i, ref Thread thread; threads)
|
||||
{
|
||||
//pushBlock(thread.block);
|
||||
thread.blocks.add(thread.block);
|
||||
thread.block = getBlock();
|
||||
}
|
||||
}
|
||||
|
||||
struct Thread
|
||||
{
|
||||
//Vector!VertexBlock block;
|
||||
RenderData[] render_list;
|
||||
VertexBlock block;
|
||||
Vector!VertexBlock blocks;
|
||||
}
|
||||
Thread[] threads;
|
||||
|
||||
|
||||
Buffer[2] ubos;
|
||||
int block_alignment = 1;
|
||||
int block_max_size = 16384;
|
||||
|
|
@ -71,7 +203,7 @@ struct Renderer
|
|||
Buffer[2] batch_vbo;
|
||||
Buffer[2] batch_ibo;
|
||||
|
||||
float[] batch_vertices;
|
||||
ubyte[] batch_vertices;
|
||||
ushort[] batch_indices;
|
||||
|
||||
Buffer indirect_buffer;
|
||||
|
|
@ -90,8 +222,17 @@ struct Renderer
|
|||
uint mesh_id;
|
||||
}
|
||||
|
||||
struct DrawCall
|
||||
{
|
||||
uint start;
|
||||
uint count;
|
||||
}
|
||||
|
||||
Vector!DrawCall draw_list;
|
||||
|
||||
RenderData[] render_list;
|
||||
uint item_id;
|
||||
uint prepared_items;
|
||||
|
||||
uint[] multi_count;
|
||||
uint[] multi_offset;
|
||||
|
|
@ -109,6 +250,18 @@ struct Renderer
|
|||
{
|
||||
//this.technique = __ecs_used_technique;
|
||||
__initialize(this);
|
||||
|
||||
get_block_mutex = Mallocator.make!Mutex();
|
||||
block_stack_mutex = Mallocator.make!Mutex();
|
||||
get_block_mutex.initialize();
|
||||
block_stack_mutex.initialize();
|
||||
|
||||
|
||||
threads = Mallocator.makeArray!Thread(32);
|
||||
foreach(ref Thread thread;threads)
|
||||
{
|
||||
thread.block = getBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private static void __initialize_gl(ref Renderer this_)
|
||||
|
|
@ -141,16 +294,16 @@ struct Renderer
|
|||
case Technique.vbo_batch:
|
||||
batch_vbo[0].create();
|
||||
batch_ibo[0].create();
|
||||
batch_vbo[0].bufferData(Buffer.BindTarget.array,16,4*MaxObjects,BufferUsage,null);
|
||||
batch_vbo[0].bufferData(Buffer.BindTarget.array,14,4*MaxObjects,BufferUsage,null);
|
||||
batch_ibo[0].bufferData(Buffer.BindTarget.element_array,2,6*MaxObjects,BufferUsage,null);
|
||||
|
||||
batch_vbo[1].create();
|
||||
batch_ibo[1].create();
|
||||
batch_vbo[1].bufferData(Buffer.BindTarget.array,16,4*MaxObjects,BufferUsage,null);
|
||||
batch_vbo[1].bufferData(Buffer.BindTarget.array,14,4*MaxObjects,BufferUsage,null);
|
||||
batch_ibo[1].bufferData(Buffer.BindTarget.element_array,2,6*MaxObjects,BufferUsage,null);
|
||||
|
||||
batch_vertices = Mallocator.makeArray!float(16*MaxObjects);
|
||||
batch_indices = Mallocator.makeArray!ushort(6*MaxObjects);
|
||||
//batch_vertices = Mallocator.makeArray!ubyte(14*4*MaxObjects);
|
||||
//batch_indices = Mallocator.makeArray!ushort(6*MaxObjects);
|
||||
break;
|
||||
case Technique.instanced_attrib_divisor:
|
||||
goto case(Technique.uniform_buffer_indexed);
|
||||
|
|
@ -253,6 +406,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(block_size, 32);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -261,12 +416,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(prepared_items >= 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 +442,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_)
|
||||
|
|
@ -330,19 +486,20 @@ struct Renderer
|
|||
|
||||
data_index += data_offset;
|
||||
item_id++;
|
||||
prepared_items++;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
short[3] mem = [depth, *cast(short*)&color, *(cast(short*)&color + 1)];
|
||||
//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;
|
||||
|
||||
pos.x = pos.x * view_size.x + view_pos.x;
|
||||
pos.y = pos.y * view_size.y + view_pos.y;//*/
|
||||
|
||||
|
|
@ -355,67 +512,134 @@ struct Renderer
|
|||
memcpy(ptr+16,pos.data.ptr,8);
|
||||
memcpy(ptr+32,coords.data.ptr,16);*/
|
||||
|
||||
short[] verts = cast(short[])threads[thread_id].block.batch_vertices;
|
||||
uint item_id = threads[thread_id].block.items;
|
||||
|
||||
if(angle == 0)
|
||||
{
|
||||
batch_vertices[item_id*16] = GfxConfig.meshes[mesh_id].vertices[0] * size.x + pos.x;
|
||||
batch_vertices[item_id*16+1] = GfxConfig.meshes[mesh_id].vertices[1] * size.y + pos.y;
|
||||
batch_vertices[item_id*16+4] = GfxConfig.meshes[mesh_id].vertices[4] * size.x + pos.x;
|
||||
batch_vertices[item_id*16+5] = GfxConfig.meshes[mesh_id].vertices[5] * size.y + pos.y;
|
||||
batch_vertices[item_id*16+8] = GfxConfig.meshes[mesh_id].vertices[8] * size.x + pos.x;
|
||||
batch_vertices[item_id*16+9] = GfxConfig.meshes[mesh_id].vertices[9] * size.y + pos.y;
|
||||
batch_vertices[item_id*16+12] = GfxConfig.meshes[mesh_id].vertices[12] * size.x + pos.x;
|
||||
batch_vertices[item_id*16+13] = GfxConfig.meshes[mesh_id].vertices[13] * size.y + pos.y;
|
||||
size.x *= view_size.x;
|
||||
size.y *= view_size.y;
|
||||
|
||||
verts[item_id*28] = cast(short)((GfxConfig.meshes[mesh_id].vertices[0] * size.x + pos.x) * 8191);
|
||||
verts[item_id*28+1] = cast(short)((GfxConfig.meshes[mesh_id].vertices[1] * size.y + pos.y) * 8191);
|
||||
verts[item_id*28+2] = cast(short)((GfxConfig.meshes[mesh_id].vertices[2] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+3] = cast(short)((GfxConfig.meshes[mesh_id].vertices[3] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+4,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+7] = cast(short)((GfxConfig.meshes[mesh_id].vertices[4] * size.x + pos.x) * 8191);
|
||||
verts[item_id*28+8] = cast(short)((GfxConfig.meshes[mesh_id].vertices[5] * size.y + pos.y) * 8191);
|
||||
verts[item_id*28+9] = cast(short)((GfxConfig.meshes[mesh_id].vertices[6] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+10] = cast(short)((GfxConfig.meshes[mesh_id].vertices[7] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+11,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+14] = cast(short)((GfxConfig.meshes[mesh_id].vertices[8] * size.x + pos.x) * 8191);
|
||||
verts[item_id*28+15] = cast(short)((GfxConfig.meshes[mesh_id].vertices[9] * size.y + pos.y) * 8191);
|
||||
verts[item_id*28+16] = cast(short)((GfxConfig.meshes[mesh_id].vertices[10] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+17] = cast(short)((GfxConfig.meshes[mesh_id].vertices[11] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+18,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+21] = cast(short)((GfxConfig.meshes[mesh_id].vertices[12] * size.x + pos.x) * 8191);
|
||||
verts[item_id*28+22] = cast(short)((GfxConfig.meshes[mesh_id].vertices[13] * size.y + pos.y) * 8191);
|
||||
verts[item_id*28+23] = cast(short)((GfxConfig.meshes[mesh_id].vertices[14] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+24] = cast(short)((GfxConfig.meshes[mesh_id].vertices[15] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+25,mem.ptr,6);
|
||||
}
|
||||
else
|
||||
{
|
||||
//import core.stdc.math;
|
||||
float sinn = sinf(angle);
|
||||
float coss = cosf(angle);
|
||||
float sinx = sinf(angle) * size.x * view_size.y;
|
||||
float cosx = cosf(angle) * size.x * view_size.x;
|
||||
float siny = sinf(angle) * size.y * view_size.x;
|
||||
float cosy = cosf(angle) * size.y * view_size.y;
|
||||
|
||||
/*batch_vertices[item_id*16] = GfxConfig.meshes[mesh_id].vertices[0] * size.x;
|
||||
batch_vertices[item_id*16+1] = GfxConfig.meshes[mesh_id].vertices[1] * size.y;
|
||||
batch_vertices[item_id*16+4] = GfxConfig.meshes[mesh_id].vertices[4] * size.x;
|
||||
batch_vertices[item_id*16+5] = GfxConfig.meshes[mesh_id].vertices[5] * size.y;
|
||||
batch_vertices[item_id*16+8] = GfxConfig.meshes[mesh_id].vertices[8] * size.x;
|
||||
batch_vertices[item_id*16+9] = GfxConfig.meshes[mesh_id].vertices[9] * size.y;
|
||||
batch_vertices[item_id*16+12] = GfxConfig.meshes[mesh_id].vertices[12] * size.x;
|
||||
batch_vertices[item_id*16+13] = GfxConfig.meshes[mesh_id].vertices[13] * size.y;*/
|
||||
/*batch_vertices[item_id*28] = GfxConfig.meshes[mesh_id].vertices[0] * size.x;
|
||||
batch_vertices[item_id*28+1] = GfxConfig.meshes[mesh_id].vertices[1] * size.y;
|
||||
batch_vertices[item_id*28+4] = GfxConfig.meshes[mesh_id].vertices[4] * size.x;
|
||||
batch_vertices[item_id*28+5] = GfxConfig.meshes[mesh_id].vertices[5] * size.y;
|
||||
batch_vertices[item_id*28+8] = GfxConfig.meshes[mesh_id].vertices[8] * size.x;
|
||||
batch_vertices[item_id*28+9] = GfxConfig.meshes[mesh_id].vertices[9] * size.y;
|
||||
batch_vertices[item_id*28+12] = GfxConfig.meshes[mesh_id].vertices[12] * size.x;
|
||||
batch_vertices[item_id*28+13] = GfxConfig.meshes[mesh_id].vertices[13] * size.y;*/
|
||||
|
||||
batch_vertices[item_id*16] = (GfxConfig.meshes[mesh_id].vertices[0] * coss + GfxConfig.meshes[mesh_id].vertices[1] * sinn) * size.x + pos.x;
|
||||
batch_vertices[item_id*16+1] = (GfxConfig.meshes[mesh_id].vertices[1] * coss - GfxConfig.meshes[mesh_id].vertices[0] * sinn) * size.y + pos.y;
|
||||
batch_vertices[item_id*16+4] = (GfxConfig.meshes[mesh_id].vertices[4] * coss + GfxConfig.meshes[mesh_id].vertices[5] * sinn) * size.x + pos.x;
|
||||
batch_vertices[item_id*16+5] = (GfxConfig.meshes[mesh_id].vertices[5] * coss - GfxConfig.meshes[mesh_id].vertices[4] * sinn) * size.y + pos.y;
|
||||
batch_vertices[item_id*16+8] = (GfxConfig.meshes[mesh_id].vertices[8] * coss + GfxConfig.meshes[mesh_id].vertices[9] * sinn) * size.x + pos.x;
|
||||
batch_vertices[item_id*16+9] = (GfxConfig.meshes[mesh_id].vertices[9] * coss - GfxConfig.meshes[mesh_id].vertices[8] * sinn) * size.y + pos.y;
|
||||
batch_vertices[item_id*16+12] = (GfxConfig.meshes[mesh_id].vertices[12] * coss + GfxConfig.meshes[mesh_id].vertices[13] * sinn) * size.x + pos.x;
|
||||
batch_vertices[item_id*16+13] = (GfxConfig.meshes[mesh_id].vertices[13] * coss - GfxConfig.meshes[mesh_id].vertices[12] * sinn) * size.y + pos.y;
|
||||
verts[item_id*28] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[0] * cosx + GfxConfig.meshes[mesh_id].vertices[1] * siny) + pos.x) * 8191);
|
||||
verts[item_id*28+1] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[1] * cosy - GfxConfig.meshes[mesh_id].vertices[0] * sinx) + pos.y) * 8191);
|
||||
verts[item_id*28+2] = cast(short)((GfxConfig.meshes[mesh_id].vertices[2] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+3] = cast(short)((GfxConfig.meshes[mesh_id].vertices[3] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+4,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+7] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[4] * cosx + GfxConfig.meshes[mesh_id].vertices[5] * siny) + pos.x) * 8191);
|
||||
verts[item_id*28+8] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[5] * cosy - GfxConfig.meshes[mesh_id].vertices[4] * sinx) + pos.y) * 8191);
|
||||
verts[item_id*28+9] = cast(short)((GfxConfig.meshes[mesh_id].vertices[6] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+10] = cast(short)((GfxConfig.meshes[mesh_id].vertices[7] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+11,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+14] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[8] * cosx + GfxConfig.meshes[mesh_id].vertices[9] * siny) + pos.x) * 8191);
|
||||
verts[item_id*28+15] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[9] * cosy - GfxConfig.meshes[mesh_id].vertices[8] * sinx) + pos.y) * 8191);
|
||||
verts[item_id*28+16] = cast(short)((GfxConfig.meshes[mesh_id].vertices[10] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+17] = cast(short)((GfxConfig.meshes[mesh_id].vertices[11] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+18,mem.ptr,6);
|
||||
|
||||
|
||||
verts[item_id*28+21] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[12] * cosx + GfxConfig.meshes[mesh_id].vertices[13] * siny) + pos.x) * 8191);
|
||||
verts[item_id*28+22] = cast(short)(((GfxConfig.meshes[mesh_id].vertices[13] * cosy - GfxConfig.meshes[mesh_id].vertices[12] * sinx) + pos.y) * 8191);
|
||||
verts[item_id*28+23] = cast(short)((GfxConfig.meshes[mesh_id].vertices[14] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+24] = cast(short)((GfxConfig.meshes[mesh_id].vertices[15] * coords.w + coords.y)*32767);
|
||||
memcpy(verts.ptr+item_id*28+25,mem.ptr,6);
|
||||
}
|
||||
|
||||
batch_vertices[item_id*16+2] = GfxConfig.meshes[mesh_id].vertices[2] * coords.z + coords.x;
|
||||
batch_vertices[item_id*16+3] = GfxConfig.meshes[mesh_id].vertices[3] * coords.w + coords.y;
|
||||
batch_vertices[item_id*16+6] = GfxConfig.meshes[mesh_id].vertices[6] * coords.z + coords.x;
|
||||
batch_vertices[item_id*16+7] = GfxConfig.meshes[mesh_id].vertices[7] * coords.w + coords.y;
|
||||
batch_vertices[item_id*16+10] = GfxConfig.meshes[mesh_id].vertices[10] * coords.z + coords.x;
|
||||
batch_vertices[item_id*16+11] = GfxConfig.meshes[mesh_id].vertices[11] * coords.w + coords.y;
|
||||
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;
|
||||
/*verts[item_id*28+2] = cast(short)((GfxConfig.meshes[mesh_id].vertices[2] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+3] = cast(short)((GfxConfig.meshes[mesh_id].vertices[3] * coords.w + coords.y)*32767);
|
||||
verts[item_id*28+9] = cast(short)((GfxConfig.meshes[mesh_id].vertices[6] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+10] = cast(short)((GfxConfig.meshes[mesh_id].vertices[7] * coords.w + coords.y)*32767);
|
||||
verts[item_id*28+16] = cast(short)((GfxConfig.meshes[mesh_id].vertices[10] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+17] = cast(short)((GfxConfig.meshes[mesh_id].vertices[11] * coords.w + coords.y)*32767);
|
||||
verts[item_id*28+23] = cast(short)((GfxConfig.meshes[mesh_id].vertices[14] * coords.z + coords.x)*32767);
|
||||
verts[item_id*28+24] = cast(short)((GfxConfig.meshes[mesh_id].vertices[15] * coords.w + coords.y)*32767);*/
|
||||
|
||||
uint ind_id = item_id % 16_384;
|
||||
/*verts[item_id*28+4] = depth;
|
||||
verts[item_id*28+11] = depth;
|
||||
verts[item_id*28+18] = depth;
|
||||
verts[item_id*28+25] = depth;
|
||||
|
||||
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);
|
||||
batch_indices[item_id*6+2] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[2] + ind_id*4);
|
||||
batch_indices[item_id*6+3] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[3] + ind_id*4);
|
||||
batch_indices[item_id*6+4] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[4] + ind_id*4);
|
||||
batch_indices[item_id*6+5] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[5] + ind_id*4);
|
||||
*cast(uint*)&verts[item_id*28+5] = color;
|
||||
*cast(uint*)&verts[item_id*28+12] = color;
|
||||
*cast(uint*)&verts[item_id*28+19] = color;
|
||||
*cast(uint*)&verts[item_id*28+26] = color;
|
||||
|
||||
memcpy(verts.ptr+item_id*28+4,mem.ptr,6);
|
||||
memcpy(verts.ptr+item_id*28+11,mem.ptr,6);
|
||||
memcpy(verts.ptr+item_id*28+18,mem.ptr,6);
|
||||
memcpy(verts.ptr+item_id*28+25,mem.ptr,6);*/
|
||||
|
||||
uint ind_id = (item_id % batch_size)*4;
|
||||
|
||||
ushort[] indices = threads[thread_id].block.batch_indices;
|
||||
|
||||
indices[item_id*6] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[0] + ind_id);
|
||||
indices[item_id*6+1] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[1] + ind_id);
|
||||
indices[item_id*6+2] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[2] + ind_id);
|
||||
indices[item_id*6+3] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[3] + ind_id);
|
||||
indices[item_id*6+4] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[4] + ind_id);
|
||||
indices[item_id*6+5] = cast(ushort)(GfxConfig.meshes[mesh_id].indices[5] + ind_id);
|
||||
|
||||
//render_list[item_id] = RenderData(tex,material_id,mesh_id);
|
||||
render_list[item_id].texture = tex;
|
||||
render_list[item_id].material_id = material_id;
|
||||
render_list[item_id].mesh_id = mesh_id;
|
||||
//render_list[item_id].texture = tex;
|
||||
//render_list[item_id].material_id = material_id;
|
||||
//render_list[item_id].mesh_id = mesh_id;
|
||||
|
||||
//data_index += 1;//data_offset;
|
||||
item_id++;
|
||||
threads[thread_id].block.items++;
|
||||
if(threads[thread_id].block.items >= VertexBlock.max_items)
|
||||
{
|
||||
//pushBlock(threads[thread_id].block);
|
||||
threads[thread_id].blocks.add(threads[thread_id].block);
|
||||
threads[thread_id].block = getBlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -433,9 +657,22 @@ struct Renderer
|
|||
{
|
||||
glClearColor(0,0,0,0);
|
||||
glViewport(0,0,this_.resolution.x,this_.resolution.y);
|
||||
glClear(GL_COLOR_BUFFER_BIT);// | GL_DEPTH_BUFFER_BIT);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
//glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDepthFunc(GL_LESS);
|
||||
|
||||
version(WebAssembly)
|
||||
{
|
||||
glDepthRangef(0,1);
|
||||
}
|
||||
else
|
||||
{
|
||||
glDepthRange(0,1);
|
||||
}
|
||||
//glDepthRange(0,1);
|
||||
//glClearDepth(1);
|
||||
}
|
||||
|
||||
void present()
|
||||
|
|
@ -450,6 +687,10 @@ struct Renderer
|
|||
|
||||
private static void __present_gl(ref Renderer this_)
|
||||
{
|
||||
|
||||
this_.pushThreadsBlocks();
|
||||
this_.pushData();
|
||||
|
||||
glViewport(0,0,this_.resolution.x,this_.resolution.y);
|
||||
//glEnable(GL_ALPHA_TEST);
|
||||
//glAlphaFunc(GL_GREATER, 0.01);
|
||||
|
|
@ -471,14 +712,17 @@ struct Renderer
|
|||
break;
|
||||
case Technique.vbo_batch:
|
||||
//if(data_index){
|
||||
batch_vbo[0].bufferSubData(Buffer.BindTarget.array,item_id*4*16,0,batch_vertices.ptr);
|
||||
batch_ibo[0].bufferSubData(Buffer.BindTarget.element_array,item_id*6*2,0,batch_indices.ptr);
|
||||
//batch_vbo[0].bufferSubData(Buffer.BindTarget.array,item_id*4*14,0,batch_vertices.ptr);
|
||||
//batch_ibo[0].bufferSubData(Buffer.BindTarget.element_array,item_id*6*2,0,batch_indices.ptr);
|
||||
|
||||
batch_vbo[0].bind(Buffer.BindTarget.array);
|
||||
batch_ibo[0].bind(Buffer.BindTarget.element_array);
|
||||
|
||||
glVertexAttribPointer(0,2,GL_FLOAT,false,16,null);
|
||||
glVertexAttribPointer(1,2,GL_FLOAT,false,16,cast(void*)8);//}
|
||||
//glVertexAttribPointer(0,2,GL_SHORT,true,14,null);
|
||||
//glVertexAttribPointer(1,2,GL_SHORT,true,14,cast(void*)4);//}
|
||||
glEnableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(3);
|
||||
//glVertexAttribPointer(2,1,GL_SHORT,true,14,cast(void*)6);//}
|
||||
break;
|
||||
case Technique.instanced_attrib_divisor:
|
||||
ubos[0].bufferSubData(Buffer.BindTarget.uniform,data_index,0,uniform_block.ptr);
|
||||
|
|
@ -575,8 +819,8 @@ struct Renderer
|
|||
//glBeginQuery(GL_TIME_ELAPSED, time_queries[0]);
|
||||
if(technique == Technique.vbo_batch)
|
||||
{
|
||||
uint items = item_id/16_384+1;
|
||||
foreach(i; 0..items)
|
||||
//uint items = item_id/batch_size+1;
|
||||
foreach(i; 0..draw_list.length)
|
||||
{
|
||||
if(material_id != render_list[i].material_id)
|
||||
{
|
||||
|
|
@ -591,16 +835,20 @@ 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));
|
||||
glVertexAttribPointer(0,2,GL_SHORT,true,14,cast(void*)(draw_list[i].start*4*14));
|
||||
glVertexAttribPointer(1,2,GL_SHORT,true,14,cast(void*)(draw_list[i].start*4*14+4));
|
||||
glVertexAttribPointer(2,1,GL_SHORT,true,14,cast(void*)(draw_list[i].start*4*14+8));
|
||||
glVertexAttribPointer(3,4,GL_UNSIGNED_BYTE,true,14,cast(void*)(draw_list[i].start*4*14+10));
|
||||
|
||||
glDrawElements(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2));
|
||||
glDrawElements(GL_TRIANGLES,draw_list[i].count*6,GL_UNSIGNED_SHORT,cast(void*)(draw_list[i].start*6*2));
|
||||
|
||||
//glDrawElementsBaseVertex(GL_TRIANGLES,instance_count*6,GL_UNSIGNED_SHORT,cast(void*)(i*16_384*6*2),i*16_384*4);
|
||||
}
|
||||
|
|
@ -783,6 +1031,9 @@ struct Renderer
|
|||
}
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glDisableVertexAttribArray(2);
|
||||
glDisableVertexAttribArray(3);
|
||||
this_.freeBlocks();
|
||||
/*glUseProgram(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);*/
|
||||
|
|
@ -803,7 +1054,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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue