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

draw_cache_impl_displist.c « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f18d02f5ee7813968c9d550ffebbe41ba6389b3b (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
/*
 * ***** 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) 2017 by Blender Foundation.
 * All rights reserved.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file draw_cache_impl_displist.c
 *  \ingroup draw
 *
 * \brief DispList API for render engines
 *
 * \note DispList may be removed soon! This is a utility for object types that use render.
 */

#include "MEM_guardedalloc.h"

#include "BLI_utildefines.h"
#include "BLI_math_vector.h"

#include "DNA_curve_types.h"

#include "BKE_displist.h"

#include "GPU_batch.h"

#include "draw_cache_impl.h"  /* own include */

static int dl_vert_len(const DispList *dl)
{
	switch (dl->type) {
		case DL_INDEX3:
		case DL_INDEX4:
			return dl->nr;
		case DL_SURF:
			return dl->parts * dl->nr;
	}
	return 0;
}

static int dl_tri_len(const DispList *dl)
{
	switch (dl->type) {
		case DL_INDEX3:
			return dl->parts;
		case DL_INDEX4:
			return dl->parts * 2;
		case DL_SURF:
			return dl->totindex * 2;
	}
	return 0;
}

/* see: displist_get_allverts */
static int curve_render_surface_vert_len_get(const ListBase *lb)
{
	int vert_len = 0;
	for (const DispList *dl = lb->first; dl; dl = dl->next) {
		vert_len += dl_vert_len(dl);
	}
	return vert_len;
}

static int curve_render_surface_tri_len_get(const ListBase *lb)
{
	int tri_len = 0;
	for (const DispList *dl = lb->first; dl; dl = dl->next) {
		tri_len += dl_tri_len(dl);
	}
	return tri_len;
}

Batch *BLI_displist_batch_calc_surface(ListBase *lb)
{
	const int tri_len = curve_render_surface_tri_len_get(lb);
	if (tri_len == 0) {
		return NULL;
	}

	static VertexFormat format = { 0 };
	static unsigned int pos_id, nor_id;
	if (format.attrib_ct == 0) {
		/* initialize vertex format */
		pos_id = VertexFormat_add_attrib(&format, "pos", COMP_F32, 3, KEEP_FLOAT);
		nor_id = VertexFormat_add_attrib(&format, "nor", COMP_F32, 3, KEEP_FLOAT);
	}

	const int vert_len = curve_render_surface_vert_len_get(lb);
	VertexBuffer *vbo = VertexBuffer_create_with_format(&format);
	{
		const int vbo_len_capacity = vert_len;
		int vbo_len_used = 0;
		VertexBuffer_allocate_data(vbo, vbo_len_capacity);

		BKE_displist_normals_add(lb);

		for (const DispList *dl = lb->first; dl; dl = dl->next) {
			const bool ndata_is_single = dl->type == DL_INDEX3;
			if (ELEM(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)) {
				const float *fp_co = dl->verts;
				const float *fp_no = dl->nors;
				const int vbo_end = vbo_len_used + dl_vert_len(dl);
				while (vbo_len_used < vbo_end) {
					VertexBuffer_set_attrib(vbo, pos_id, vbo_len_used, fp_co);
					if (fp_no) {
						VertexBuffer_set_attrib(vbo, nor_id, vbo_len_used, fp_no);
						if (ndata_is_single == false) {
							fp_no += 3;
						}
					}
					fp_co += 3;
					vbo_len_used += 1;
				}
			}
		}
	}

	{
		ElementListBuilder elb;
		ElementListBuilder_init(&elb, PRIM_TRIANGLES, tri_len, vert_len);

		int ofs = 0;
		int tri_len_used = 0;
		for (const DispList *dl = lb->first; dl; dl = dl->next) {
			if (ELEM(dl->type, DL_INDEX3, DL_INDEX4, DL_SURF)) {
				if (dl->type == DL_INDEX3) {
					const int *idx = dl->index;
					const int i_end = dl->parts;
					for (int i = 0; i < i_end; i++) {
						add_triangle_vertices(&elb, idx[0] + ofs, idx[1] + ofs, idx[2] + ofs);
						tri_len_used += 1;
						idx += 3;
					}
				}
				else if (ELEM(dl->type, DL_INDEX4, DL_SURF)) {
					const int *idx = dl->index;
					const int i_end = dl->totindex;
					for (int i = 0; i < i_end; i++) {
						add_triangle_vertices(&elb, idx[0] + ofs, idx[1] + ofs, idx[2] + ofs);
						tri_len_used += 1;
						add_triangle_vertices(&elb, idx[0] + ofs, idx[2] + ofs, idx[3] + ofs);
						tri_len_used += 1;
						idx += 4;
					}
				}
				ofs += dl_vert_len(dl);
			}
		}

		return Batch_create(PRIM_TRIANGLES, vbo, ElementList_build(&elb));
	}
}