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 float mqt_viewmaxdist;
21 meshqueue_t *mq_array, *mqt_array, *mq_listhead;
22 int mq_count, mqt_count;
23 int mq_total, mqt_total;
25 void R_MeshQueue_Init(void)
27 Cvar_RegisterVariable(&r_meshqueue_entries);
28 Cvar_RegisterVariable(&r_meshqueue_immediaterender);
29 Cvar_RegisterVariable(&r_meshqueue_sort);
37 void R_MeshQueue_Render(void)
42 for (mq = mq_listhead;mq;mq = mq->next)
43 mq->callback(mq->data1, mq->data2);
48 static void R_MeshQueue_EnlargeTransparentArray(int newtotal)
50 meshqueue_t *newarray;
51 newarray = (meshqueue_t *)Mem_Alloc(cl_mempool, newtotal * sizeof(meshqueue_t));
54 memcpy(newarray, mqt_array, mqt_total * sizeof(meshqueue_t));
61 void R_MeshQueue_Add(void (*callback)(const void *data1, int data2), const void *data1, int data2)
63 meshqueue_t *mq, **mqnext;
64 if (r_meshqueue_immediaterender.integer)
66 callback(data1, data2);
69 if (mq_count >= mq_total)
71 mq = &mq_array[mq_count++];
72 mq->callback = callback;
76 if (r_meshqueue_sort.integer)
78 // bubble-insert sort into meshqueue
79 for(mqnext = &mq_listhead;*mqnext;mqnext = &(*mqnext)->next)
81 if (mq->callback == (*mqnext)->callback)
83 if (mq->data1 == (*mqnext)->data1)
85 if (mq->data2 <= (*mqnext)->data2)
88 else if (mq->data1 < (*mqnext)->data1)
91 else if (mq->callback < (*mqnext)->callback)
98 for(mqnext = &mq_listhead;*mqnext;mqnext = &(*mqnext)->next);
104 void R_MeshQueue_AddTransparent(const vec3_t center, void (*callback)(const void *data1, int data2), const void *data1, int data2)
107 if (mqt_count >= mqt_total)
108 R_MeshQueue_EnlargeTransparentArray(mqt_total + 100);
109 mq = &mqt_array[mqt_count++];
110 mq->callback = callback;
113 mq->dist = DotProduct(center, r_viewforward) - mqt_viewplanedist;
115 mqt_viewmaxdist = max(mqt_viewmaxdist, mq->dist);
118 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 distscale = 4095.0f / max(mqt_viewmaxdist, 4095);
133 for (i = 0, mqt = mqt_array;i < mqt_count;i++, mqt++)
136 hashdist = (int) (mqt->dist * distscale);
137 hashdist = bound(0, hashdist, 4095);
138 // link to tail of hash chain (to preserve render order)
140 *hashpointer[hashdist] = mqt;
141 hashpointer[hashdist] = &mqt->next;
143 for (i = 4095;i >= 0;i--)
145 for (mqt = hash[i];mqt;mqt = mqt->next)
146 mqt->callback(mqt->data1, mqt->data2);
150 void R_MeshQueue_BeginScene(void)
152 if (r_meshqueue_entries.integer < 1)
153 Cvar_SetValueQuick(&r_meshqueue_entries, 1);
154 if (r_meshqueue_entries.integer > 65536)
155 Cvar_SetValueQuick(&r_meshqueue_entries, 65536);
157 if (mq_total != r_meshqueue_entries.integer || mq_array == NULL)
159 mq_total = r_meshqueue_entries.integer;
162 mq_array = (meshqueue_t *)Mem_Alloc(cl_mempool, mq_total * sizeof(meshqueue_t));
165 if (mqt_array == NULL)
166 mqt_array = (meshqueue_t *)Mem_Alloc(cl_mempool, mqt_total * sizeof(meshqueue_t));
171 mqt_viewplanedist = DotProduct(r_vieworigin, r_viewforward);
175 void R_MeshQueue_EndScene(void)
179 Con_Printf("R_MeshQueue_EndScene: main mesh queue still has %i items left, flushing\n", mq_count);
180 R_MeshQueue_Render();
184 Con_Printf("R_MeshQueue_EndScene: transparent mesh queue still has %i items left, flushing\n", mqt_count);
185 R_MeshQueue_RenderTransparent();