Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpu_raster.c « intern « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7526cfc7e55d272e3d3d4d47ab884f159ad671d4 (plain)
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2013 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Jason Wilkins.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/gpu/intern/gpu_raster.c
 *  \ingroup gpu
 */

#define GPU_MANGLE_DEPRECATED 0

/* my interface */
#include "intern/gpu_raster_intern.h"

/* my library */
#include "GPU_blender_aspect.h"
#include "GPU_extensions.h"
#include "GPU_safety.h"

/* internal */
#include "intern/gpu_common_intern.h"
#include "intern/gpu_immediate_intern.h"
#include "intern/gpu_matrix_intern.h"

/* external */

#include "BLI_dynstr.h"

#include "DNA_userdef_types.h"

#include "MEM_guardedalloc.h"

/* standard */
#include <string.h>



/* patterns */

const GLubyte GPU_stipple_halftone[128] = {
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55, 
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
	0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};


/*  repeat this pattern
 *
 *     X000X000
 *     00000000
 *     00X000X0
 *     00000000 */


const GLubyte GPU_stipple_quarttone[128] = {
	136, 136, 136, 136, 0, 0, 0, 0, 34, 34, 34, 34, 0, 0, 0, 0,
	136, 136, 136, 136, 0, 0, 0, 0, 34, 34, 34, 34, 0, 0, 0, 0,
	136, 136, 136, 136, 0, 0, 0, 0, 34, 34, 34, 34, 0, 0, 0, 0,
	136, 136, 136, 136, 0, 0, 0, 0, 34, 34, 34, 34, 0, 0, 0, 0,
	136, 136, 136, 136, 0, 0, 0, 0, 34, 34, 34, 34, 0, 0, 0, 0,
	136, 136, 136, 136, 0, 0, 0, 0, 34, 34, 34, 34, 0, 0, 0, 0,
	136, 136, 136, 136, 0, 0, 0, 0, 34, 34, 34, 34, 0, 0, 0, 0,
	136, 136, 136, 136, 0, 0, 0, 0, 34, 34, 34, 34, 0, 0, 0, 0
};


const GLubyte GPU_stipple_diag_stripes_pos[128] = {
	0x00, 0xff, 0x00, 0xff, 0x01, 0xfe, 0x01, 0xfe,
	0x03, 0xfc, 0x03, 0xfc, 0x07, 0xf8, 0x07, 0xf8,
	0x0f, 0xf0, 0x0f, 0xf0, 0x1f, 0xe0, 0x1f, 0xe0,
	0x3f, 0xc0, 0x3f, 0xc0, 0x7f, 0x80, 0x7f, 0x80,
	0xff, 0x00, 0xff, 0x00, 0xfe, 0x01, 0xfe, 0x01,
	0xfc, 0x03, 0xfc, 0x03, 0xf8, 0x07, 0xf8, 0x07,
	0xf0, 0x0f, 0xf0, 0x0f, 0xe0, 0x1f, 0xe0, 0x1f,
	0xc0, 0x3f, 0xc0, 0x3f, 0x80, 0x7f, 0x80, 0x7f,
	0x00, 0xff, 0x00, 0xff, 0x01, 0xfe, 0x01, 0xfe,
	0x03, 0xfc, 0x03, 0xfc, 0x07, 0xf8, 0x07, 0xf8,
	0x0f, 0xf0, 0x0f, 0xf0, 0x1f, 0xe0, 0x1f, 0xe0,
	0x3f, 0xc0, 0x3f, 0xc0, 0x7f, 0x80, 0x7f, 0x80,
	0xff, 0x00, 0xff, 0x00, 0xfe, 0x01, 0xfe, 0x01,
	0xfc, 0x03, 0xfc, 0x03, 0xf8, 0x07, 0xf8, 0x07,
	0xf0, 0x0f, 0xf0, 0x0f, 0xe0, 0x1f, 0xe0, 0x1f,
	0xc0, 0x3f, 0xc0, 0x3f, 0x80, 0x7f, 0x80, 0x7f
};


const GLubyte GPU_stipple_diag_stripes_neg[128] = {
	0xff, 0x00, 0xff, 0x00, 0xfe, 0x01, 0xfe, 0x01,
	0xfc, 0x03, 0xfc, 0x03, 0xf8, 0x07, 0xf8, 0x07,
	0xf0, 0x0f, 0xf0, 0x0f, 0xe0, 0x1f, 0xe0, 0x1f,
	0xc0, 0x3f, 0xc0, 0x3f, 0x80, 0x7f, 0x80, 0x7f,
	0x00, 0xff, 0x00, 0xff, 0x01, 0xfe, 0x01, 0xfe,
	0x03, 0xfc, 0x03, 0xfc, 0x07, 0xf8, 0x07, 0xf8,
	0x0f, 0xf0, 0x0f, 0xf0, 0x1f, 0xe0, 0x1f, 0xe0,
	0x3f, 0xc0, 0x3f, 0xc0, 0x7f, 0x80, 0x7f, 0x80,
	0xff, 0x00, 0xff, 0x00, 0xfe, 0x01, 0xfe, 0x01,
	0xfc, 0x03, 0xfc, 0x03, 0xf8, 0x07, 0xf8, 0x07,
	0xf0, 0x0f, 0xf0, 0x0f, 0xe0, 0x1f, 0xe0, 0x1f,
	0xc0, 0x3f, 0xc0, 0x3f, 0x80, 0x7f, 0x80, 0x7f,
	0x00, 0xff, 0x00, 0xff, 0x01, 0xfe, 0x01, 0xfe,
	0x03, 0xfc, 0x03, 0xfc, 0x07, 0xf8, 0x07, 0xf8,
	0x0f, 0xf0, 0x0f, 0xf0, 0x1f, 0xe0, 0x1f, 0xe0,
	0x3f, 0xc0, 0x3f, 0xc0, 0x7f, 0x80, 0x7f, 0x80
};



/* State */

static struct RASTER {
	uint32_t options;

	struct GPUShader* gpushader[GPU_RASTER_OPTION_COMBINATIONS];
	bool              failed   [GPU_RASTER_OPTION_COMBINATIONS];
	struct GPUcommon  common   [GPU_RASTER_OPTION_COMBINATIONS];

	GLubyte  polygon_stipple[32*32];

	GLint    line_stipple_factor;
	GLushort line_stipple_pattern;

	GLfloat  line_width;

	GLenum   polygon_mode;

} RASTER;

#if GPU_SAFETY
static bool RASTER_BEGUN = false;
#endif



/* Init / exit */

void gpu_raster_init(void)
{
	memset(&RASTER, 0, sizeof(RASTER));

	gpu_raster_reset_stipple();

	RASTER.line_width = 1;

	RASTER.line_stipple_factor  = 1;
	RASTER.line_stipple_pattern = 0xFFFF;

	RASTER.polygon_mode = GL_FILL;

	GPU_CHECK_NO_ERROR();
}



void gpu_raster_exit(void)
{
	int i;

	for (i = 0; i < GPU_RASTER_OPTION_COMBINATIONS; i++)
		if (RASTER.gpushader[i] != NULL)
			GPU_shader_free(RASTER.gpushader[i]);
}



void gpu_raster_reset_stipple(void)
{
	int a, x, y;
	GLubyte mask[32*32];

	a = 0;
	for (x = 0; x<32; x++) {
		for (y = 0; y<4; y++) {
			if (x & 1)
				mask[a++]= 0x88;
			else
				mask[a++]= 0x22;
		}
	}

	gpuPolygonStipple((GLubyte*)mask);
}



/* Shader feature enable/disable */

void gpu_raster_enable(uint32_t options)
{
	RASTER.options |= options;
}



void gpu_raster_disable(uint32_t options)
{
	RASTER.options &= ~options;
}



static void raster_shader_bind(void)
{
	/* glsl code */
	extern const char datatoc_gpu_shader_raster_frag_glsl[];
	extern const char datatoc_gpu_shader_raster_vert_glsl[];
	extern const char datatoc_gpu_shader_raster_uniforms_glsl[];

	const uint32_t tweaked_options = RASTER.options;//tweak_options();

	/* create shader if it doesn't exist yet */
	if (RASTER.gpushader[tweaked_options] != NULL) {
		GPU_shader_bind(RASTER.gpushader[tweaked_options]);
		gpu_set_common(RASTER.common + tweaked_options);
	}
	else if (!RASTER.failed[tweaked_options]) {
		DynStr* vert = BLI_dynstr_new();
		DynStr* frag = BLI_dynstr_new();
		DynStr* defs = BLI_dynstr_new();

		char* vert_cstring;
		char* frag_cstring;
		char* defs_cstring;

		char nickname[20];

		gpu_include_common_vert(vert);
		BLI_dynstr_append(vert, datatoc_gpu_shader_raster_uniforms_glsl);
		BLI_dynstr_append(vert, datatoc_gpu_shader_raster_vert_glsl);

		gpu_include_common_frag(frag);
		BLI_dynstr_append(vert, datatoc_gpu_shader_raster_uniforms_glsl);
		BLI_dynstr_append(frag, datatoc_gpu_shader_raster_frag_glsl);

		gpu_include_common_defs(defs);

		if (tweaked_options & GPU_RASTER_STIPPLE)
			BLI_dynstr_append(defs, "#define USE_STIPPLE\n");

		if (tweaked_options & GPU_RASTER_AA)
			BLI_dynstr_append(defs, "#define USE_AA\n");

		if (tweaked_options & GPU_RASTER_POLYGON)
			BLI_dynstr_append(defs, "#define USE_POLYGON\n");

		vert_cstring = BLI_dynstr_get_cstring(vert);
		frag_cstring = BLI_dynstr_get_cstring(frag);
		defs_cstring = BLI_dynstr_get_cstring(defs);

		sprintf(nickname, "Raster[0x%04X]", tweaked_options);

		RASTER.gpushader[tweaked_options] =
			GPU_shader_create(nickname, vert_cstring, frag_cstring, NULL, defs_cstring);

		MEM_freeN(vert_cstring);
		MEM_freeN(frag_cstring);
		MEM_freeN(defs_cstring);

		BLI_dynstr_free(vert);
		BLI_dynstr_free(frag);
		BLI_dynstr_free(defs);

		if (RASTER.gpushader[tweaked_options] != NULL) {
			gpu_common_get_symbols(RASTER.common + tweaked_options, RASTER.gpushader[tweaked_options]);
			gpu_set_common (RASTER.common + tweaked_options);

			GPU_shader_bind(RASTER.gpushader[tweaked_options]);
		}
		else {
			RASTER.failed[tweaked_options] = true;
			gpu_set_common(NULL);
		}
	}
	else {
		gpu_set_common(NULL);
	}
}



void gpu_raster_bind(void)
{
	bool glsl_support = GPU_glsl_support();

	GPU_ASSERT(RASTER_BEGUN);
	
	if (glsl_support)
		raster_shader_bind();

#if defined(WITH_GL_PROFILE_COMPAT)
	if (!glsl_support) {
		GPU_CHECK_NO_ERROR();

		if (RASTER.options & GPU_RASTER_STIPPLE) {
			glEnable(GL_LINE_STIPPLE);
			glEnable(GL_POLYGON_STIPPLE);
		}
		else {
			glDisable(GL_LINE_STIPPLE);
			glDisable(GL_POLYGON_STIPPLE);
		}

		if (RASTER.options & GPU_RASTER_AA) {
			glEnable(GL_LINE_SMOOTH);
			glEnable(GL_POLYGON_SMOOTH);
		}
		else {
			glDisable(GL_LINE_SMOOTH);
			glDisable(GL_POLYGON_SMOOTH);
		}

		glLineWidth(RASTER.line_width);
		glLineStipple(RASTER.line_stipple_factor, RASTER.line_stipple_pattern);

		glPolygonMode(GL_FRONT_AND_BACK, RASTER.polygon_mode);
		glPolygonStipple(RASTER.polygon_stipple);
		
		GPU_CHECK_NO_ERROR();
	}
#endif

	gpu_commit_matrix();
}



void gpu_raster_unbind(void)
{
	bool glsl_support = GPU_glsl_support();

	GPU_ASSERT(RASTER_BEGUN);
	
	if (glsl_support)
		GPU_shader_unbind();

#if defined(WITH_GL_PROFILE_COMPAT)
	if (!glsl_support) {
		GPU_CHECK_NO_ERROR();

		glDisable(GL_LINE_STIPPLE);
		glDisable(GL_POLYGON_STIPPLE);

		glDisable(GL_LINE_SMOOTH);
		glDisable(GL_POLYGON_SMOOTH);

		glLineWidth(1);

		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		
		GPU_CHECK_NO_ERROR();
	}
#endif
}



void gpuPolygonStipple(const GLubyte* mask)
{
	memcpy(RASTER.polygon_stipple, mask, sizeof(RASTER.polygon_stipple));
}



void gpuLineStipple(GLint factor, GLushort pattern)
{
	RASTER.line_stipple_factor  = factor;
	RASTER.line_stipple_pattern = pattern;
}



void gpuLineWidth(GLfloat width)
{
	RASTER.line_width = width;
}



GLfloat gpuGetLineWidth(void)
{
	return RASTER.line_width;
}



void gpuPolygonMode(GLenum mode)
{
	RASTER.polygon_mode = mode;
}



GLenum gpuGetPolygonMode(void)
{
	return RASTER.polygon_mode;
}



void GPU_raster_set_line_style(int factor)
{
	if (factor == 0) {
		GPU_aspect_disable(GPU_ASPECT_RASTER, GPU_RASTER_STIPPLE);
	}
	else {
		GPU_aspect_enable(GPU_ASPECT_RASTER, GPU_RASTER_STIPPLE);

		if (U.pixelsize > 1.0f)
			gpuLineStipple(factor, 0xCCCC);
		else
			gpuLineStipple(factor, 0xAAAA);
	}
}



void GPU_raster_begin()
{
#if GPU_SAFETY
	GPU_ASSERT(!RASTER_BEGUN);
	RASTER_BEGUN = true;
#endif

	// SSS End (Assuming the basic aspect is ending)
	GPU_aspect_end();

	// SSS Begin Raster
	GPU_aspect_begin(GPU_ASPECT_RASTER, NULL);
}



void GPU_raster_end()
{
#if GPU_SAFETY
	GPU_ASSERT(RASTER_BEGUN);
#endif

	// SSS End Raster
	GPU_aspect_end();

#if GPU_SAFETY
	RASTER_BEGUN = false;
#endif

	// SSS Begin Basic
	GPU_aspect_begin(GPU_ASPECT_BASIC, NULL);
}




static GLboolean end_begin(void)
{
#if GPU_SAFETY
	GPU_IMMEDIATE->hasOverflowed = GL_TRUE;
#endif

	if (!ELEM6(
			GPU_IMMEDIATE->mode,
			GL_NOOP,
			GL_LINE_LOOP,
			GL_POLYGON,
			GL_QUAD_STRIP,
			GL_LINE_STRIP,
			GL_TRIANGLE_STRIP)) // XXX jwilkins: can restart some of these, but need to put in the logic (could be problematic with mapped VBOs?)
	{
		gpu_end_buffer_gl();

		GPU_IMMEDIATE->mappedBuffer = NULL;
		GPU_IMMEDIATE->offset       = 0;
		GPU_IMMEDIATE->count        = 1; /* count the vertex that triggered this */

		gpu_begin_buffer_gl();

		return GL_TRUE;
	}
	else {
		return GL_FALSE;
	}
}



static void gpu_copy_vertex_thick_line(void)
{
	size_t i;
	size_t size;
	size_t offset;
	GLubyte *restrict mappedBuffer;

#if GPU_SAFETY
	{
	int maxVertexCountOK;
	GPU_SAFE_RETURN(GPU_IMMEDIATE->maxVertexCount != 0, maxVertexCountOK,);
	}
#endif

	if (GPU_IMMEDIATE->count == GPU_IMMEDIATE->lastPrimVertex) {
		GLboolean restarted;

		restarted = end_begin(); /* draw and clear buffer */

		GPU_ASSERT(restarted);

		if (!restarted)
			return;
	}
	else {
		GPU_IMMEDIATE->count++;
	}

	mappedBuffer = GPU_IMMEDIATE->mappedBuffer;
	offset = GPU_IMMEDIATE->offset;

	/* vertex */

	size = (size_t)(GPU_IMMEDIATE->format.vertexSize) * sizeof(GLfloat);
	memcpy(mappedBuffer + offset, GPU_IMMEDIATE->vertex, size);
	offset += size;

	/* normal */

	if (GPU_IMMEDIATE->format.normalSize != 0) {
		/* normals are always have 3 components */
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->normal, 3*sizeof(GLfloat));
		offset += 3*sizeof(GLfloat);
	}

	/* color */

	if (GPU_IMMEDIATE->format.colorSize != 0) {
		/* 4 bytes are always reserved for color, for efficient memory alignment */
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->color, 4*sizeof(GLubyte));
		offset += 4*sizeof(GLubyte);
	}

	/* texture coordinate(s) */

	for (i = 0; i < GPU_IMMEDIATE->format.texCoordCount; i++) {
		size = (size_t)(GPU_IMMEDIATE->format.texCoordSize[i]) * sizeof(GLfloat);
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->texCoord[i], size);
		offset += size;
	}

	/* float vertex attribute(s) */

	for (i = 0; i < GPU_IMMEDIATE->format.attribCount_f; i++) {
		size = (size_t)(GPU_IMMEDIATE->format.attribSize_f[i]) * sizeof(GLfloat);
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->attrib_f[i], size);
		offset += size;
	}

	/* unsigned byte vertex attribute(s) */

	for (i = 0; i < GPU_IMMEDIATE->format.attribCount_ub; i++) {
		/* 4 bytes are always reserved for byte attributes, for efficient memory alignment */
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->attrib_ub[i], 4*sizeof(GLubyte));
		offset += 4*sizeof(GLubyte);
	}

	GPU_IMMEDIATE->offset = offset;
}



static void gpu_copy_vertex_wire_polygon(void)
{
	size_t i;
	size_t size;
	size_t offset;
	GLubyte *restrict mappedBuffer;

#if GPU_SAFETY
	{
	int maxVertexCountOK;
	GPU_SAFE_RETURN(GPU_IMMEDIATE->maxVertexCount != 0, maxVertexCountOK,);
	}
#endif

	if (GPU_IMMEDIATE->count == GPU_IMMEDIATE->lastPrimVertex) {
		GLboolean restarted;

		restarted = end_begin(); /* draw and clear buffer */

		GPU_ASSERT(restarted);

		if (!restarted)
			return;
	}
	else {
		GPU_IMMEDIATE->count++;
	}

	mappedBuffer = GPU_IMMEDIATE->mappedBuffer;
	offset = GPU_IMMEDIATE->offset;

	/* vertex */

	size = (size_t)(GPU_IMMEDIATE->format.vertexSize) * sizeof(GLfloat);
	memcpy(mappedBuffer + offset, GPU_IMMEDIATE->vertex, size);
	offset += size;

	/* normal */

	if (GPU_IMMEDIATE->format.normalSize != 0) {
		/* normals are always have 3 components */
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->normal, 3*sizeof(GLfloat));
		offset += 3*sizeof(GLfloat);
	}

	/* color */

	if (GPU_IMMEDIATE->format.colorSize != 0) {
		/* 4 bytes are always reserved for color, for efficient memory alignment */
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->color, 4*sizeof(GLubyte));
		offset += 4*sizeof(GLubyte);
	}

	/* texture coordinate(s) */

	for (i = 0; i < GPU_IMMEDIATE->format.texCoordCount; i++) {
		size = (size_t)(GPU_IMMEDIATE->format.texCoordSize[i]) * sizeof(GLfloat);
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->texCoord[i], size);
		offset += size;
	}

	/* float vertex attribute(s) */

	for (i = 0; i < GPU_IMMEDIATE->format.attribCount_f; i++) {
		size = (size_t)(GPU_IMMEDIATE->format.attribSize_f[i]) * sizeof(GLfloat);
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->attrib_f[i], size);
		offset += size;
	}

	/* unsigned byte vertex attribute(s) */

	for (i = 0; i < GPU_IMMEDIATE->format.attribCount_ub; i++) {
		/* 4 bytes are always reserved for byte attributes, for efficient memory alignment */
		memcpy(mappedBuffer + offset, GPU_IMMEDIATE->attrib_ub[i], 4*sizeof(GLubyte));
		offset += 4*sizeof(GLubyte);
	}

	GPU_IMMEDIATE->offset = offset;
}