Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. // Small!
  2. struct ModelEntityDepthPtr {
  3. // Does this really need to be double?
  4. double depth;
  5. U32 index;
  6. };
  7.  
  8. internal inline
  9. int entity_depth_cmp(const void *e1, const void *e2)
  10. {
  11. // Smallest Z first (furthest away from camera)
  12. // Uglier but faster than using temp vars with -O0
  13. return ( ((ModelEntityDepthPtr*)e1)->depth > ((ModelEntityDepthPtr*)e2)->depth ) -
  14. ( ((ModelEntityDepthPtr*)e1)->depth < ((ModelEntityDepthPtr*)e2)->depth );
  15. }
  16.  
  17. void render_frame()
  18. {
  19. // Cache!
  20. Renderer *r= g_env.renderer;
  21. ModelEntity *entities= r->entities;
  22.  
  23. // Eww, global maybe stick to renderer, but don't allocate a new one every frame
  24. static ModelEntityDepthPtr sorted_entities[MAX_MODELENTITY_COUNT];
  25. U32 num_visible_entities= 0;
  26.  
  27. // This part still trashes the cache
  28. for (U32 i = 0; i < MAX_MODELENTITY_COUNT; i++)
  29. {
  30. ModelEntity *e= entities[i];
  31.  
  32. // Maybe store these in separate bit-array for performance if this becomes slow
  33. // if (r->entity_has_model[i / 32] & (1 << i % 32)) // Complexity! Would relieve cache pressure ~500x if lots of empty entities though
  34. if (!e->model_name[0])
  35. continue;
  36.  
  37. // Do some super cheap cull test here (can be skipped in the beginning and this will still probably be faster, less copying)
  38. if (is_visible(e, camera))
  39. {
  40. // Store only the depth and index so they are cheaper to shuffle around when sorting
  41. sorted_entities[num_visible_entities]= (ModelEntityDepthPtr) { .depth = e->pos.z, .index = i };
  42. ++num_visible_entities;
  43. }
  44. }
  45.  
  46. // Z-sort
  47. qsort(sorted_entities, num_visible_entities, sizeof(*sorted_entities), entity_depth_cmp);
  48.  
  49. // Generate the geometry here
  50. for (U32 i = 0; i < num_visible_entities; i++)
  51. {
  52. // This looks bad but entities array is already in cache
  53. ModelEntity *e= entities[sorted_entities[i].index];
  54.  
  55. // Draw!
  56. }
  57.  
  58. // Actually render!
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement