5 cvar_t r_meshqueue_entries = {CVAR_SAVE, "r_meshqueue_entries", "16", "maximum number of meshes to batch together and sort before issuing render calls (unused)"};
6 cvar_t r_meshqueue_immediaterender = {0, "r_meshqueue_immediaterender", "0", "immediately render non-transparent meshes rather than batching"};
7 cvar_t r_meshqueue_sort = {0, "r_meshqueue_sort", "0", "whether to sort meshes in a batch before issuing calls"};
9 typedef struct meshqueue_s
11 struct meshqueue_s *next;
12 void (*callback)(const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight);
13 const entity_render_t *ent;
15 const rtlight_t *rtlight;
20 float mqt_viewplanedist;
21 float mqt_viewmaxdist;
22 meshqueue_t *mq_array, *mqt_array, *mq_listhead;
23 int mq_count, mqt_count;
24 int mq_total, mqt_total;
26 void R_MeshQueue_Init(void)
28 Cvar_RegisterVariable(&r_meshqueue_entries);
29 Cvar_RegisterVariable(&r_meshqueue_immediaterender);
30 Cvar_RegisterVariable(&r_meshqueue_sort);
38 void R_MeshQueue_Render(void)
43 for (mq = mq_listhead;mq;mq = mq->next)
44 mq->callback(mq->ent, mq->surfacenumber, mq->rtlight);
49 static void R_MeshQueue_EnlargeTransparentArray(int newtotal)
51 meshqueue_t *newarray;
52 newarray = (meshqueue_t *)Mem_Alloc(cls.permanentmempool, newtotal * sizeof(meshqueue_t));
55 memcpy(newarray, mqt_array, mqt_total * sizeof(meshqueue_t));
62 void R_MeshQueue_Add(void (*callback)(const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight), const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
64 meshqueue_t *mq, **mqnext;
65 if (r_meshqueue_immediaterender.integer)
67 callback(ent, surfacenumber, rtlight);
70 if (mq_count >= mq_total)
72 mq = &mq_array[mq_count++];
73 mq->callback = callback;
75 mq->surfacenumber = surfacenumber;
76 mq->rtlight = rtlight;
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->ent == (*mqnext)->ent)
87 if (mq->surfacenumber == (*mqnext)->surfacenumber)
89 if (mq->rtlight <= (*mqnext)->rtlight)
92 else if (mq->surfacenumber < (*mqnext)->surfacenumber)
95 else if (mq->ent < (*mqnext)->ent)
98 else if (mq->callback < (*mqnext)->callback)
104 // maintain the order
105 for(mqnext = &mq_listhead;*mqnext;mqnext = &(*mqnext)->next);
111 void R_MeshQueue_AddTransparent(const vec3_t center, void (*callback)(const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight), const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
114 if (mqt_count >= mqt_total)
115 R_MeshQueue_EnlargeTransparentArray(mqt_total + 100);
116 mq = &mqt_array[mqt_count++];
117 mq->callback = callback;
119 mq->surfacenumber = surfacenumber;
120 mq->rtlight = rtlight;
121 mq->dist = DotProduct(center, r_viewforward) - mqt_viewplanedist;
123 mqt_viewmaxdist = max(mqt_viewmaxdist, mq->dist);
126 void R_MeshQueue_RenderTransparent(void)
132 meshqueue_t *hash[4096], **hashpointer[4096];
134 R_MeshQueue_Render();
137 memset(hash, 0, sizeof(hash));
138 for (i = 0;i < 4096;i++)
139 hashpointer[i] = &hash[i];
140 distscale = 4095.0f / max(mqt_viewmaxdist, 4095);
141 for (i = 0, mqt = mqt_array;i < mqt_count;i++, mqt++)
144 hashdist = (int) (mqt->dist * distscale);
145 hashdist = bound(0, hashdist, 4095);
146 // link to tail of hash chain (to preserve render order)
148 *hashpointer[hashdist] = mqt;
149 hashpointer[hashdist] = &mqt->next;
151 for (i = 4095;i >= 0;i--)
153 for (mqt = hash[i];mqt;mqt = mqt->next)
154 mqt->callback(mqt->ent, mqt->surfacenumber, mqt->rtlight);
158 void R_MeshQueue_BeginScene(void)
160 if (r_meshqueue_entries.integer < 1)
161 Cvar_SetValueQuick(&r_meshqueue_entries, 1);
162 if (r_meshqueue_entries.integer > 65536)
163 Cvar_SetValueQuick(&r_meshqueue_entries, 65536);
165 if (mq_total != r_meshqueue_entries.integer || mq_array == NULL)
167 mq_total = r_meshqueue_entries.integer;
170 mq_array = (meshqueue_t *)Mem_Alloc(cls.permanentmempool, mq_total * sizeof(meshqueue_t));
173 if (mqt_array == NULL)
174 mqt_array = (meshqueue_t *)Mem_Alloc(cls.permanentmempool, mqt_total * sizeof(meshqueue_t));
179 mqt_viewplanedist = DotProduct(r_vieworigin, r_viewforward);
183 void R_MeshQueue_EndScene(void)
187 Con_Printf("R_MeshQueue_EndScene: main mesh queue still has %i items left, flushing\n", mq_count);
188 R_MeshQueue_Render();
192 Con_Printf("R_MeshQueue_EndScene: transparent mesh queue still has %i items left, flushing\n", mqt_count);
193 R_MeshQueue_RenderTransparent();