/
drawlist.cpp
592 lines (504 loc) · 18.3 KB
/
drawlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/** @file drawlist.cpp Drawable primitive list.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "render/drawlist.h"
#include "DrawLists"
#include "render/rend_main.h"
#include "clientapp.h"
#include <de/concurrency.h>
#include <de/memoryzone.h>
using namespace de;
DENG2_PIMPL(DrawList)
{
/**
* Each Element begins a block of GL commands/geometry to apply/transfer.
*/
struct Element {
// Must be an offset since the list is sometimes reallocated.
uint size; ///< Size of this element (zero = n/a).
struct Data {
Store *buffer;
gl::Primitive type;
// Element indices into the global backing store for the geometry.
// These are always contiguous and all are used (some are shared):
// indices[0] is the base, and indices[1...n] > indices[0].
uint numIndices;
uint *indices;
bool oneLight;
bool manyLights;
blendmode_t blendMode;
DGLuint modTexture;
Vector3f modColor;
Vector2f ptexOffset;
Vector2f ptexScale;
Vector2f texOffset;
Vector2f texScale;
void setFromTexUnit(GLTextureUnit const &texUnit, bool p = false)
{
if(p)
{
ptexScale = texUnit.scale;
ptexOffset = texUnit.offset * texUnit.scale;
}
else
{
texScale = texUnit.scale;
texOffset = texUnit.offset * texUnit.scale;
}
}
/**
* Draw the geometry for this element.
*/
void draw(DrawConditions const &conditions, TexUnitMap const &texUnitMap)
{
if(conditions & SetLightEnv)
{
// Use the correct texture and color for the light.
glActiveTexture((conditions & SetLightEnv0)? GL_TEXTURE0 : GL_TEXTURE1);
GL_BindTextureUnmanaged(!renderTextures? 0 : modTexture,
GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
float modColorV[4] = { modColor.x, modColor.y, modColor.z, 0 };
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, modColorV);
}
if(conditions & SetMatrixDTexture)
{
// Primitive-specific texture translation & scale.
if(conditions & SetMatrixDTexture0)
{
glActiveTexture(GL_TEXTURE0);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glTranslatef(texOffset.x, texOffset.y, 1);
glScalef(texScale.x, texScale.y, 1);
}
if(conditions & SetMatrixDTexture1)
{
glActiveTexture(GL_TEXTURE1);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glTranslatef(texOffset.x, texOffset.y, 1);
glScalef(texScale.x, texScale.y, 1);
}
}
if(conditions & SetMatrixTexture)
{
// Primitive-specific texture translation & scale.
if(conditions & SetMatrixTexture0)
{
glActiveTexture(GL_TEXTURE0);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glTranslatef(ptexOffset.x, ptexOffset.y, 1);
glScalef(ptexScale.x, ptexScale.y, 1);
}
if(conditions & SetMatrixTexture1)
{
glActiveTexture(GL_TEXTURE1);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glTranslatef(ptexOffset.x, ptexOffset.y, 1);
glScalef(ptexScale.x, ptexScale.y, 1);
}
}
if(conditions & SetBlendMode)
{
// Primitive-specific blending. Not used in all lists.
GL_BlendMode(blendMode);
}
glBegin(type == gl::TriangleStrip? GL_TRIANGLE_STRIP : GL_TRIANGLE_FAN);
for(uint i = 0; i < numIndices; ++i)
{
uint const index = indices[i];
for(int j = 0; j < numTexUnits; ++j)
{
if(texUnitMap[j])
{
Vector2f const &tc = buffer->texCoords[texUnitMap[j] - 1][index];
glMultiTexCoord2f(GL_TEXTURE0 + j, tc.x, tc.y);
}
}
if(!(conditions & NoColor))
{
Vector4ub const &color = buffer->colorCoords[index];
glColor4ub(color.x, color.y, color.z, color.w);
}
Vector3f const &pos = buffer->posCoords[index];
glVertex3f(pos.x, pos.z, pos.y);
}
glEnd();
// Restore the texture matrix if changed.
if(conditions & SetMatrixTexture)
{
if(conditions & SetMatrixTexture0)
{
glActiveTexture(GL_TEXTURE0);
glMatrixMode(GL_TEXTURE);
glPopMatrix();
}
if(conditions & SetMatrixTexture1)
{
glActiveTexture(GL_TEXTURE1);
glMatrixMode(GL_TEXTURE);
glPopMatrix();
}
}
if(conditions & SetMatrixDTexture)
{
if(conditions & SetMatrixDTexture0)
{
glActiveTexture(GL_TEXTURE0);
glMatrixMode(GL_TEXTURE);
glPopMatrix();
}
if(conditions & SetMatrixDTexture1)
{
glActiveTexture(GL_TEXTURE1);
glMatrixMode(GL_TEXTURE);
glPopMatrix();
}
}
}
} data;
Element *next() {
if(!size) return 0;
Element *elem = (Element *) ((byte *) (this) + size);
if(!elem->size) return 0;
return elem;
}
};
/// Logical geometry group for which the list is currently configured.
GeomGroup group;
/// Texture unit configuration. Note that texture matrix scale and offset
/// factors are ignore because these values are writen to the Element.
GLTextureUnit texUnits[NUM_TEXTURE_UNITS];
size_t dataSize; ///< Number of bytes allocated for the data.
byte *data; ///< Data for a number of polygons (The List).
byte *cursor; ///< Data pointer for reading/writing.
Element *last; ///< Last element (if any).
Instance(Public *i)
: Base(i),
group(UnlitGeom),
dataSize(0),
data(0),
cursor(0),
last(0)
{}
~Instance()
{
clearAllData();
}
void clearAllData()
{
if(data)
{
// All the list data will be destroyed.
Z_Free(data); data = 0;
#ifdef DENG_DEBUG
Z_CheckHeap();
#endif
}
cursor = 0;
last = 0;
dataSize = 0;
}
/**
* @return Start of the allocated data.
*/
void *allocateData(uint bytes)
{
// Number of extra bytes to keep allocated in the end of each list.
int const PADDING = 16;
if(!bytes) return 0;
// We require the extra bytes because we want that the end of the list
// data is always safe for writing-in-advance. This is needed when the
// 'end of data' marker is written.
int const startOffset = cursor - data;
size_t const required = startOffset + bytes + PADDING;
// First check that the data buffer of the list is large enough.
if(required > dataSize)
{
// Offsets must be preserved.
byte *oldData = data;
int const cursorOffset = (cursor? cursor - oldData : -1);
int const lastOffset = (last? (byte *) last - oldData : -1);
// Allocate more memory for the data buffer.
if(dataSize == 0)
{
dataSize = 1024;
}
while(dataSize < required)
{
dataSize *= 2;
}
data = (byte *) Z_Realloc(data, dataSize, PU_APPSTATIC);
// Restore main pointers.
cursor = (cursorOffset >= 0? data + cursorOffset : data);
last = (lastOffset >= 0? (Element *) (data + lastOffset) : 0);
// Restore in-list pointers.
// When the list is resized, pointers in the primitives need to be
// restored so that they point to the new list data.
if(oldData)
{
for(Element *elem = first(); elem && elem <= last; elem = elem->next())
{
if(elem->data.indices)
{
elem->data.indices = (uint *) (data + ((byte *) elem->data.indices - oldData));
}
}
}
}
// Advance the cursor.
cursor += bytes;
return data + startOffset;
}
void allocateIndices(uint numIndices, uint base)
{
// Note that last may be reallocated during allocateData.
last->data.numIndices = numIndices;
last->data.indices = (uint *) allocateData(sizeof(uint) * numIndices);
for(uint i = 0; i < numIndices; ++i)
{
last->data.indices[i] = base + i;
}
}
Element *newElement(Store &buffer, gl::Primitive primitive)
{
// This becomes the new last element.
last = (Element *) allocateData(sizeof(Element));
last->size = 0;
last->data.buffer = &buffer;
last->data.type = primitive;
last->data.indices = 0;
last->data.numIndices = 0;
last->data.oneLight = last->data.manyLights = false;
return last;
}
void endWrite()
{
// The element has been written, update the size in the header.
last->size = cursor - (byte *) last;
// Write the end marker (will be overwritten by the next write). The
// idea is that this zero is interpreted as the size of the following
// Element.
*(int *) cursor = 0;
}
/// Returns a pointer to the first element in the list; otherwise @c 0.
Element *first() const
{
Element *elem = (Element *)data;
if(!elem->size) return 0;
return elem;
}
};
DrawList::DrawList(GeomGroup initialGeomGroup) : d(new Instance(this))
{
setGeomGroup(initialGeomGroup);
}
GeomGroup DrawList::geomGroup() const
{
return d->group;
}
void DrawList::setGeomGroup(GeomGroup newGroup)
{
d->group = newGroup;
}
bool DrawList::isEmpty() const
{
return d->last == 0;
}
DrawList &DrawList::write(gl::Primitive primitive, bool isLit, uint vertCount,
Vector3f const *posCoords, Vector4f const *colorCoords, Vector2f const *texCoords,
Vector2f const *interTexCoords, DGLuint modTexture, Vector3f const *modColor,
Vector2f const *modTexCoords)
{
DENG2_ASSERT(vertCount >= 3);
// Rationalize write arguments.
if(d->group == SkyMaskGeom || d->group == LightGeom || d->group == ShadowGeom)
{
isLit = false;
modTexture = 0;
modColor = 0;
}
Instance::Element *elem =
d->newElement(ClientApp::renderSystem().buffer(), primitive);
// Is the geometry lit?
if(modTexture && !isLit)
{
elem->data.oneLight = true; // Using modulation.
}
else if(modTexture || isLit)
{
elem->data.manyLights = true;
}
// Configure the GL state to be applied when this geometry is drawn later.
GLTextureUnit const **texunits = RL_RtuState();
elem->data.blendMode = texunits[TU_PRIMARY]->blendMode;
elem->data.modTexture = modTexture;
elem->data.modColor = modColor? *modColor : Vector3f();
elem->data.ptexOffset = Vector2f(0, 0);
elem->data.ptexScale = Vector2f(0, 0);
elem->data.texScale = Vector2f(1, 1);
elem->data.texOffset = Vector2f(1, 1);
// GL texture translation parameters come from the tex unit map and
// differ according to the (logical) type of primitive to be written.
if(d->group == ShineGeom && texunits[TU_INTER]->hasTexture())
{
elem->data.setFromTexUnit(*texunits[TU_INTER], true);
}
else if(texunits[TU_PRIMARY]->hasTexture())
{
elem->data.setFromTexUnit(*texunits[TU_PRIMARY], true);
}
if(texunits[TU_PRIMARY_DETAIL]->hasTexture())
{
elem->data.setFromTexUnit(*texunits[TU_PRIMARY_DETAIL]);
}
// Allocate geometry from the backing store.
uint base = elem->data.buffer->allocateVertices(vertCount);
// Setup the indices.
d->allocateIndices(vertCount, base);
for(uint i = 0; i < vertCount; ++i)
{
elem->data.buffer->posCoords[base + i] = posCoords[i];
// Sky masked polys need nothing more.
if(d->group == SkyMaskGeom) continue;
// Primary texture coordinates.
if(unit(TU_PRIMARY).hasTexture())
{
DENG2_ASSERT(texCoords != 0);
elem->data.buffer->texCoords[Store::TCA_MAIN][base + i] = texCoords[i];
}
// Secondary texture coordinates.
if(unit(TU_INTER).hasTexture())
{
DENG2_ASSERT(interTexCoords != 0);
elem->data.buffer->texCoords[Store::TCA_BLEND][base + i] = interTexCoords[i];
}
// First light texture coordinates.
if((elem->data.oneLight || elem->data.manyLights) && IS_MTEX_LIGHTS)
{
DENG2_ASSERT(modTexCoords != 0);
elem->data.buffer->texCoords[Store::TCA_LIGHT][base + i] = modTexCoords[i];
}
// Color.
Vector4ub &color = elem->data.buffer->colorCoords[base + i];
if(colorCoords)
{
Vector4f const &srcColor = colorCoords[i];
color = Vector4ub(dbyte(255 * de::clamp(0.f, srcColor.x, 1.f)),
dbyte(255 * de::clamp(0.f, srcColor.y, 1.f)),
dbyte(255 * de::clamp(0.f, srcColor.z, 1.f)),
dbyte(255 * de::clamp(0.f, srcColor.w, 1.f)));
}
else
{
color = Vector4ub(255, 255, 255, 255);
}
}
d->endWrite();
return *this;
}
void DrawList::draw(DrawConditions conditions, TexUnitMap const &texUnitMap) const
{
// Should we just skip all this?
if(conditions & Skip) return;
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
bool bypass = false;
if(unit(TU_INTER).hasTexture())
{
// Is blending allowed?
if(conditions.testFlag(NoBlend))
{
return;
}
// Should all blended primitives be included?
if(conditions.testFlag(Blend))
{
// The other conditions will be bypassed.
bypass = true;
}
}
// Check conditions dependant on primitive-specific values once before
// entering the loop. If none of the conditions are true for this list
// then we can bypass the skip tests completely during iteration.
if(!bypass)
{
if(!conditions.testFlag(JustOneLight) &&
!conditions.testFlag(ManyLights))
{
bypass = true;
}
}
bool skip = false;
for(Instance::Element *elem = d->first(); elem; elem = elem->next())
{
// Check for skip conditions.
if(!bypass)
{
skip = false;
if(conditions.testFlag(JustOneLight) && elem->data.manyLights)
{
skip = true;
}
else if(conditions.testFlag(ManyLights) && elem->data.oneLight)
{
skip = true;
}
}
if(!skip)
{
elem->data.draw(conditions, texUnitMap);
DENG2_ASSERT(!Sys_GLCheckError());
}
}
}
GLTextureUnit &DrawList::unit(int index)
{
DENG2_ASSERT(index >= 0 && index < NUM_TEXTURE_UNITS);
return d->texUnits[index];
}
GLTextureUnit const &DrawList::unit(int index) const
{
DENG2_ASSERT(index >= 0 && index < NUM_TEXTURE_UNITS);
return d->texUnits[index];
}
void DrawList::clear()
{
d->clearAllData();
unit(TU_INTER_DETAIL).texture.glName = 0;
unit(TU_INTER_DETAIL).texture.variant = 0;
}
void DrawList::rewind()
{
d->cursor = d->data;
d->last = 0;
// The interpolation target must be explicitly set.
unit(TU_INTER).texture.glName = 0;
unit(TU_INTER).texture.variant = 0;
unit(TU_INTER).opacity = 0;
unit(TU_INTER_DETAIL).texture.glName = 0;
unit(TU_INTER_DETAIL).texture.variant = 0;
unit(TU_INTER_DETAIL).opacity = 0;
}