5 cvar_t r_meshqueue_entries = {CVAR_SAVE, "r_meshqueue_entries", "16"};
6 cvar_t r_meshqueue_immediaterender = {0, "r_meshqueue_immediaterender", "0"};
7 cvar_t r_meshqueue_sort = {0, "r_meshqueue_sort", "0"};
9 typedef struct meshqueue_s
11 struct meshqueue_s *next;
12 void (*callback)(const void *data1, int data2);
19 float mqt_viewplanedist;
20 meshqueue_t *mq_array, *mqt_array, *mq_listhead;
21 int mq_count, mqt_count;
22 int mq_total, mqt_total;
24 mempool_t *meshqueuemempool;
26 void R_MeshQueue_Init(void)
28 Cvar_RegisterVariable(&r_meshqueue_entries);
29 Cvar_RegisterVariable(&r_meshqueue_immediaterender);
30 Cvar_RegisterVariable(&r_meshqueue_sort);
32 meshqueuemempool = Mem_AllocPool("R_MeshQueue");
39 void R_MeshQueue_Render(void)
44 for (mq = mq_listhead;mq;mq = mq->next)
45 mq->callback(mq->data1, mq->data2);
50 static void R_MeshQueue_EnlargeTransparentArray(int newtotal)
52 meshqueue_t *newarray;
53 newarray = Mem_Alloc(meshqueuemempool, newtotal * sizeof(meshqueue_t));
56 memcpy(newarray, mqt_array, mqt_total * sizeof(meshqueue_t));
63 void R_MeshQueue_Add(void (*callback)(const void *data1, int data2), const void *data1, int data2)
65 meshqueue_t *mq, **mqnext;
66 if (r_meshqueue_immediaterender.integer)
68 callback(data1, data2);
71 if (mq_count >= mq_total)
73 mq = &mq_array[mq_count++];
74 mq->callback = callback;
78 if (r_meshqueue_sort.integer)
80 // bubble-insert sort into meshqueue
81 for(mqnext = &mq_listhead;*mqnext;mqnext = &(*mqnext)->next)
83 if (mq->callback == (*mqnext)->callback)
85 if (mq->data1 == (*mqnext)->data1)
87 if (mq->data2 <= (*mqnext)->data2)
90 else if (mq->data1 < (*mqnext)->data1)
93 else if (mq->callback < (*mqnext)->callback)
100 for(mqnext = &mq_listhead;*mqnext;mqnext = &(*mqnext)->next);
106 void R_MeshQueue_AddTransparent(const vec3_t center, void (*callback)(const void *data1, int data2), const void *data1, int data2)
109 if (mqt_count >= mqt_total)
110 R_MeshQueue_EnlargeTransparentArray(mqt_total + 100);
111 mq = &mqt_array[mqt_count++];
112 mq->callback = callback;
115 mq->dist = DotProduct(center, vpn) - mqt_viewplanedist;
119 void R_MeshQueue_RenderTransparent(void)
124 meshqueue_t *hash[4096], **hashpointer[4096];
126 R_MeshQueue_Render();
129 memset(hash, 0, sizeof(hash));
130 for (i = 0;i < 4096;i++)
131 hashpointer[i] = &hash[i];
132 for (i = 0, mqt = mqt_array;i < mqt_count;i++, mqt++)
135 hashdist = (int) (mqt->dist);
136 hashdist = bound(0, hashdist, 4095);
137 // link to tail of hash chain (to preserve render order)
139 *hashpointer[hashdist] = mqt;
140 hashpointer[hashdist] = &mqt->next;
142 for (i = 4095;i >= 0;i--)
144 for (mqt = hash[i];mqt;mqt = mqt->next)
145 mqt->callback(mqt->data1, mqt->data2);
149 void R_MeshQueue_BeginScene(void)
151 if (r_meshqueue_entries.integer < 1)
152 Cvar_SetValueQuick(&r_meshqueue_entries, 1);
153 if (r_meshqueue_entries.integer > 65536)
154 Cvar_SetValueQuick(&r_meshqueue_entries, 65536);
156 if (mq_total != r_meshqueue_entries.integer || mq_array == NULL)
158 mq_total = r_meshqueue_entries.integer;
161 mq_array = Mem_Alloc(meshqueuemempool, mq_total * sizeof(meshqueue_t));
164 if (mqt_array == NULL)
165 mqt_array = Mem_Alloc(meshqueuemempool, mqt_total * sizeof(meshqueue_t));
170 mqt_viewplanedist = DotProduct(r_origin, vpn);
173 void R_MeshQueue_EndScene(void)
177 Con_Printf("R_MeshQueue_EndScene: main mesh queue still has %i items left, flushing\n", mq_count);
178 R_MeshQueue_Render();
182 Con_Printf("R_MeshQueue_EndScene: transparent mesh queue still has %i items left, flushing\n", mqt_count);
183 R_MeshQueue_RenderTransparent();