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

lanpr_snake.c « lanpr « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 76f754333be11ed264708998ba6d3be2997eefcf (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
#include "DRW_engine.h"
#include "DRW_render.h"
#include "BLI_listbase.h"
#include "BLI_linklist.h"
#include "lanpr_all.h"
#include "DRW_render.h"
#include "BKE_object.h"
#include "DNA_mesh_types.h"
#include "DNA_camera_types.h"
#include "GPU_immediate.h"
#include "GPU_immediate_util.h"
#include "GPU_framebuffer.h"
#include "DNA_lanpr_types.h"
#include "DEG_depsgraph_query.h"
#include "GPU_draw.h"
#include "GPU_batch.h"
#include "GPU_framebuffer.h"
#include "GPU_shader.h"
#include "GPU_uniformbuffer.h"
#include "GPU_viewport.h"
#include "bmesh.h"

extern struct LANPR_SharedResource lanpr_share;

int _TNS_colOffsets[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
int _TNS_rowOffsets[] = { -1, -1, -1, 0, 1, 1, 1, 0 };

int _TNS_Deviates[8][8] = {
	{ 0, 1, 2, 3, 4, 3, 2, 1 },
	{ 1, 0, 1, 2, 3, 4, 3, 2 },
	{ 2, 1, 0, 1, 2, 3, 4, 3 },
	{ 3, 2, 1, 0, 1, 2, 3, 4 },
	{ 4, 3, 2, 1, 0, 1, 2, 3 },
	{ 3, 4, 3, 2, 1, 0, 1, 2 },
	{ 2, 3, 4, 3, 2, 1, 0, 1 },
	{ 1, 2, 3, 4, 3, 2, 1, 0 }
};

#define TNS_CLAMP_TEXTURE_W(t, col) \
	{if (col >= t->width) col = t->width - 1; if (col < 0) col = 0; }

#define TNS_CLAMP_TEXTURE_H(t, row) \
	{if (row >= t->height) row = t->height - 1; if (row < 0) row = 0; }

#define TNS_CLAMP_TEXTURE_CONTINUE(t, col, row) \
	{if (col >= t->width) continue; if (col < 0) continue; \
	 if (row >= t->height) continue; if (row < 0) continue; }


static LANPR_TextureSample *lanpr_any_uncovered_samples(LANPR_PrivateData *pd){
	return BLI_pophead(&pd->pending_samples);
}

int lanpr_direction_deviate(int From, int To) {
	return _TNS_Deviates[From - 1][To - 1];
}

int lanpr_detect_direction(LANPR_PrivateData *pd, int col, int row, int LastDirection) {
	int Deviate[9] = {100};
	int MinDeviate = 0;
	int i;
	LANPR_TextureSample *ts;

	for (i = 0; i < 8; i++) {
		TNS_CLAMP_TEXTURE_CONTINUE(pd, (_TNS_colOffsets[i] + col), (_TNS_rowOffsets[i] + row));
		if (ts = pd->sample_table[(_TNS_colOffsets[i] + col) + (_TNS_rowOffsets[i] + row) * pd->width]) {
			if (!LastDirection) return i + 1;
			Deviate[i + 1] = lanpr_direction_deviate(i, LastDirection);
			if (!MinDeviate || Deviate[MinDeviate] > Deviate[i + 1]) MinDeviate = i + 1;
		}
	}

	return MinDeviate;
}

LANPR_LineStrip *lanpr_create_line_strip(LANPR_PrivateData *pd) {
	LANPR_LineStrip *ls = BLI_mempool_calloc(pd->mp_line_strip);
	return ls;
}
LANPR_LineStripPoint *lanpr_append_point(LANPR_PrivateData *pd, LANPR_LineStrip *ls, real X, real Y, real Z) {
	LANPR_LineStripPoint *lsp = BLI_mempool_calloc(pd->mp_line_strip_point);

	lsp->P[0] = X;
	lsp->P[1] = Y;
	lsp->P[2] = Z;

	BLI_addtail(&ls->points, lsp);

	ls->point_count++;

	return lsp;
}
LANPR_LineStripPoint *lanpr_push_point(LANPR_PrivateData *pd, LANPR_LineStrip *ls, real X, real Y, real Z) {
	LANPR_LineStripPoint *lsp = BLI_mempool_calloc(pd->mp_line_strip_point);

	lsp->P[0] = X;
	lsp->P[1] = Y;
	lsp->P[2] = Z;

	BLI_addhead(&ls->points, lsp);

	ls->point_count++;

	return lsp;
}

void lanpr_destroy_line_strip(LANPR_PrivateData *pd, LANPR_LineStrip *ls) {
	LANPR_LineStripPoint *lsp;
	while (lsp = BLI_pophead(&ls->points)) {
		BLI_mempool_free(pd->mp_line_strip_point, lsp);
	}
	BLI_mempool_free(pd->mp_line_strip, ls);
}

void lanpr_remove_sample(LANPR_PrivateData *pd, int row, int col) {
	LANPR_TextureSample *ts;
	ts = pd->sample_table[row * pd->width + col];
	pd->sample_table[row * pd->width + col] = NULL;

	BLI_remlink(&pd->pending_samples, ts);
	ts->Item.prev = NULL; ts->Item.next = NULL;
	BLI_addtail(&pd->erased_samples, ts);
}

void lanpr_grow_snake_r(LANPR_PrivateData *pd, LANPR_LineStrip *ls, LANPR_LineStripPoint *ThisP, int Direction) {
	LANPR_LineStripPoint *NewP = ThisP, *p2;
	int Length = 5;
	int l = 0;
	int Deviate, Dir = Direction, NewDir;
	int AddPoint;
	int TX = NewP->P[0], TY = NewP->P[1];

	while (NewDir = lanpr_detect_direction(pd, TX, TY, Dir)) {
		AddPoint = 0;
		Deviate = lanpr_direction_deviate(NewDir, Dir);
		Dir = NewDir;

		l++;
		TX += _TNS_colOffsets[NewDir - 1];
		TY += _TNS_rowOffsets[NewDir - 1];

		if (Deviate < 2) {
			lanpr_remove_sample(pd, TY, TX);
		} elif(Deviate < 3)
		{
			lanpr_remove_sample(pd, TY, TX);
			AddPoint = 1;
		}
		else {
			lanpr_remove_sample(pd, TY, TX);
			return;
		}

		if (AddPoint || l == Length) {
			p2 = lanpr_append_point(pd, ls, TX, TY, 0);
			NewP = p2;
			l = 0;
		}
	}
	if (TX != ThisP->P[0] || TY != ThisP->P[1])
		lanpr_append_point(pd, ls, TX, TY, 0);
}

void lanpr_grow_snake_l(LANPR_PrivateData *pd, LANPR_LineStrip *ls, LANPR_LineStripPoint *ThisP, int Direction) {
	LANPR_LineStripPoint *NewP = ThisP, *p2;
	int Length = 5;
	int l = 0;
	int Deviate, Dir = Direction, NewDir;
	int AddPoint;
	int TX = NewP->P[0], TY = NewP->P[1];

	while (NewDir = lanpr_detect_direction(pd, TX, TY, Dir)) {
		AddPoint = 0;
		Deviate = lanpr_direction_deviate(NewDir, Dir);
		Dir = NewDir;

		l++;
		TX += _TNS_colOffsets[NewDir - 1];
		TY += _TNS_rowOffsets[NewDir - 1];

		if (Deviate < 2) {
			lanpr_remove_sample(pd, TY, TX);
		} elif(Deviate < 4)
		{
			lanpr_remove_sample(pd, TY, TX);
			AddPoint = 1;
		}
		else {
			lanpr_remove_sample(pd, TY, TX);
			return;
		}

		if (AddPoint || l == Length) {
			p2 = lanpr_push_point(pd, ls, TX, TY, 0);
			NewP = p2;
			l = 0;
		}
	}
	if (TX != ThisP->P[0] || TY != ThisP->P[1])
		lanpr_push_point(pd, ls, TX, TY, 0);
}

int lanpr_reverse_direction(int From) {
	From -= 4;
	if (From <= 0) From += 8;
	return From;
}



void lanpr_texture_to_ndc(int x, int y, int w, int h, float *x_ndc, float *y_ndc){
	*x_ndc = tnsLinearItp(-1, 1, (float)x / (float)w);
	*y_ndc = tnsLinearItp(-1, 1, (float)y / (float)h);
}

void lanpr_count_drawing_elements(LANPR_PrivateData *pd, int *vert_count, int *index_adjacent_count){
	int v_count = 0;
	int e_count = 0;
	LANPR_LineStrip *ls;
	for (ls = (LANPR_LineStrip *)(pd->line_strips.first); ls; ls = (LANPR_LineStrip *)(ls->Item.next)) {
		v_count += (ls->point_count);
		e_count += ((ls->point_count - 1) * 4);
	}
	*vert_count = v_count;
	*index_adjacent_count = e_count;
}

GPUBatch *lanpr_get_snake_batch(LANPR_PrivateData *pd){
	LANPR_LineStrip *ls;
	LANPR_LineStripPoint *lsp, *plsp;
	int i;
	float *Verts;
	float *Lengths;
	float TotalLength = 0;
	int v_count, e_count;

	lanpr_count_drawing_elements(pd, &v_count, &e_count);

	Verts = MEM_callocN(sizeof(float) * v_count * 2, "Verts buffer pre alloc");
	Lengths = MEM_callocN(sizeof(float) * v_count * 2, "Length buffer pre alloc");

	GPUIndexBufBuilder elb;
	GPU_indexbuf_init_ex(&elb, GPU_PRIM_LINES_ADJ, e_count, v_count, true);

	int vert_offset = 0;

	for (ls = (LANPR_LineStrip *)(pd->line_strips.first); ls; ls = (LANPR_LineStrip *)(ls->Item.next)) {
		for (i = 0; i < ls->point_count - 1; i++) {
			int v1 = i + vert_offset - 1;
			int v2 = i + vert_offset;
			int v3 = i + vert_offset + 1;
			int v4 = i + vert_offset + 2;
			if (v1 < 0) v1 = 0;
			if (v4 >= v_count) v4 = v_count - 1;
			GPU_indexbuf_add_line_adj_verts(&elb, v1, v2, v3, v4);
		}

		i = 0;
		float xf, yf;
		TotalLength = 0;
		for (lsp = (LANPR_LineStripPoint *)(ls->points.first); lsp; lsp = (LANPR_LineStripPoint *)(lsp->Item.next)) {
			lanpr_texture_to_ndc(lsp->P[0], lsp->P[1], pd->width, pd->height, &xf, &yf);
			Verts[vert_offset * 2 + i * 2 + 0] = xf;
			Verts[vert_offset * 2 + i * 2 + 1] = yf;
			if (plsp = (LANPR_LineStripPoint *)(lsp->Item.prev)) {
				TotalLength += tMatDist2v(plsp->P, lsp->P);
				Lengths[(vert_offset + i) * 2] = TotalLength;
			}
			i++;
		}

		ls->total_length = TotalLength;
		i = 0;
		for (lsp = (LANPR_LineStripPoint *)(ls->points.first); lsp; lsp = (LANPR_LineStripPoint *)(lsp->Item.next)) {
			if (plsp = (LANPR_LineStripPoint *)(lsp->Item.prev)) {
				Lengths[(vert_offset + i) * 2 + 1] = ls->total_length - Lengths[(vert_offset + i) * 2];
			}
			i++;
		}

		vert_offset += (ls->point_count);
	}

	static GPUVertFormat format = { 0 };
	static struct { uint pos, uvs; } attr_id;
	if (format.attr_len == 0) {
		attr_id.pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
		attr_id.uvs = GPU_vertformat_attr_add(&format, "uvs", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
	}

	GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
	GPU_vertbuf_data_alloc(vbo, v_count);

	for (int i = 0; i < v_count; ++i) {
		GPU_vertbuf_attr_set(vbo, attr_id.pos, i, &Verts[i * 2]);
		GPU_vertbuf_attr_set(vbo, attr_id.uvs, i, &Lengths[i * 2]);
	}

	MEM_freeN(Verts);
	MEM_freeN(Lengths);

	return GPU_batch_create_ex(GPU_PRIM_LINES_ADJ, vbo, GPU_indexbuf_build(&elb), GPU_USAGE_STATIC | GPU_BATCH_OWNS_VBO);
}

void lanpr_snake_free_pool_data(LANPR_PrivateData *pd) {
	if (pd->line_result_8bit) MEM_freeN(pd->line_result_8bit);
	pd->line_result_8bit = 0;
	if (pd->line_result) MEM_freeN(pd->line_result);
	pd->line_result = 0;
	BLI_mempool_clear(pd->mp_line_strip);
	BLI_mempool_clear(pd->mp_line_strip_point);
	BLI_mempool_clear(pd->mp_sample);
	BLI_mempool_clear(pd->mp_batch_list);
}
void lanpr_snake_free_readback_data(LANPR_PrivateData *pd) {
	if (pd->line_result_8bit) MEM_freeN(pd->line_result_8bit);
	pd->line_result_8bit = 0;
	if (pd->line_result) MEM_freeN(pd->line_result);
	pd->line_result = 0;
}

void lanpr_snake_draw_scene(LANPR_TextureList *txl, LANPR_FramebufferList *fbl, LANPR_PassList *psl, LANPR_PrivateData *pd, SceneLANPR *lanpr, GPUFrameBuffer *DefaultFB, int is_render){
	eGPUFrameBufferBits clear_bits = GPU_COLOR_BIT | GPU_DEPTH_BIT;
	float clear_col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
	float clear_depth = 1.0f;
	uint clear_stencil = 0xFF;

	const DRWContextState *draw_ctx = DRW_context_state_get();
	Scene *scene = DEG_get_evaluated_scene(draw_ctx->depsgraph);
	View3D *v3d = draw_ctx->v3d;
	Object *camera;
	if (v3d) {
		RegionView3D *rv3d = draw_ctx->rv3d;
		camera = (rv3d->persp == RV3D_CAMOB) ? v3d->camera : NULL;
	}
	else {
		camera = scene->camera;
	}

	pd->znear = camera ? ((Camera *)camera->data)->clip_start : 0.1;
	pd->zfar = camera ? ((Camera *)camera->data)->clip_end : 100;
	pd->normal_clamp =    lanpr->normal_clamp;
	pd->normal_strength = lanpr->normal_strength;
	pd->depth_clamp =     lanpr->depth_clamp;
	pd->depth_strength =  lanpr->depth_strength;

	GPU_framebuffer_bind(fbl->edge_intermediate);
	DRW_draw_pass(psl->edge_intermediate);

	if ((!lanpr->enable_vector_trace) && (!lanpr->display_thinning_result)) {
		GPU_framebuffer_bind(DefaultFB);
		DRW_multisamples_resolve(txl->depth, txl->edge_intermediate, 1);
		return;
	}

	if (lanpr->display_thinning_result || lanpr->enable_vector_trace) {
		pd->stage = 0;

		GPU_framebuffer_bind(DefaultFB);
		DRW_multisamples_resolve(txl->depth, txl->edge_intermediate, 1);

		GPU_framebuffer_bind(fbl->edge_thinning);
		DRW_draw_pass(psl->edge_thinning);
		GPU_framebuffer_bind(DefaultFB);
		DRW_multisamples_resolve(txl->depth, txl->color, 1);

		pd->stage = 1;
		GPU_framebuffer_bind(fbl->edge_thinning);
		DRW_draw_pass(psl->edge_thinning);
		GPU_framebuffer_bind(DefaultFB);
		DRW_multisamples_resolve(txl->depth, txl->color, 1);

		pd->stage = 0;
		GPU_framebuffer_bind(fbl->edge_thinning);
		DRW_draw_pass(psl->edge_thinning);
		GPU_framebuffer_bind(DefaultFB);
		DRW_multisamples_resolve(txl->depth, txl->color, 1);

		pd->stage = 1;
		GPU_framebuffer_bind(fbl->edge_thinning);
		DRW_draw_pass(psl->edge_thinning);
		GPU_framebuffer_bind(DefaultFB);
		DRW_multisamples_resolve(txl->depth, txl->color, 1);

		if (!lanpr->enable_vector_trace) return;
	}

	int texw = GPU_texture_width(txl->edge_intermediate), texh = GPU_texture_height(txl->edge_intermediate);;
	int tsize = texw * texh;
	int recreate = 0;
	if (tsize != pd->width * pd->height) recreate = 1;

	if (recreate || !pd->line_result) {
		if (pd->line_result) MEM_freeN(pd->line_result);
		pd->line_result = MEM_callocN(sizeof(float) * tsize, "Texture readback buffer");

		if (pd->line_result_8bit) MEM_freeN(pd->line_result_8bit);
		pd->line_result_8bit = MEM_callocN(sizeof(unsigned char) * tsize, "Texture readback buffer 8bit");

		if (pd->sample_table) MEM_freeN(pd->sample_table);
		pd->sample_table = MEM_callocN(sizeof(void *) * tsize, "Texture readback buffer 8bit");

		pd->width = texw;
		pd->height = texh;
	}

	GPU_framebuffer_bind(DefaultFB);
	GPU_framebuffer_read_color(DefaultFB, 0, 0, texw, texh, 1, 0, pd->line_result);

	float sample;
	int h, w;
	for (h = 0; h < texh; h++) {
		for (w = 0; w < texw; w++) {
			int index = h * texw + w;
			if ((sample = pd->line_result[index]) > 0.9) {
				pd->line_result_8bit[index] = 255;
				LANPR_TextureSample *ts = BLI_mempool_calloc(pd->mp_sample);
				BLI_addtail(&pd->pending_samples, ts);
				pd->sample_table[index] = ts;
				ts->X = w;
				ts->Y = h;
			}
			else {
				pd->sample_table[index] = 0;
			}
		}
	}

	LANPR_TextureSample *ts;
	LANPR_LineStrip *ls;
	LANPR_LineStripPoint *lsp;
	while (ts = lanpr_any_uncovered_samples(pd)) {
		int Direction = 0;
		LANPR_LineStripPoint tlsp = { 0 };

		tlsp.P[0] = ts->X;
		tlsp.P[1] = ts->Y;

		if (Direction = lanpr_detect_direction(pd, ts->X, ts->Y, Direction)) {
			BLI_addtail(&pd->line_strips, (ls = lanpr_create_line_strip(pd)));
			lsp = lanpr_append_point(pd, ls, ts->X, ts->Y, 0);
			lanpr_remove_sample(pd, ts->Y, ts->X);

			lanpr_grow_snake_r(pd, ls, lsp, Direction);

			lanpr_grow_snake_l(pd, ls, lsp, lanpr_reverse_direction(Direction));
		}
	}

	GPU_framebuffer_bind(DefaultFB);
	GPU_framebuffer_clear(DefaultFB, clear_bits, lanpr->background_color, clear_depth, clear_stencil);

	GPU_framebuffer_bind(fbl->edge_intermediate);
	clear_bits = GPU_COLOR_BIT;
	GPU_framebuffer_clear(fbl->edge_intermediate, clear_bits, lanpr->background_color, clear_depth, clear_stencil);

	float *tld = &lanpr->taper_left_distance, *tls = &lanpr->taper_left_strength,
	      *trd = &lanpr->taper_right_distance, *trs = &lanpr->taper_right_strength;

	GPUBatch *snake_batch = lanpr_get_snake_batch(pd);

	lanpr_snake_free_pool_data(pd);

	psl->snake_pass = DRW_pass_create("Snake Visualization Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
	pd->snake_shgrp = DRW_shgroup_create(lanpr_share.snake_connection_shader, psl->snake_pass);
	DRW_shgroup_uniform_float(pd->snake_shgrp, "line_width", &lanpr->line_thickness, 1);
	DRW_shgroup_uniform_float(pd->snake_shgrp, "taper_l_dist", tld, 1);
	DRW_shgroup_uniform_float(pd->snake_shgrp, "taper_r_dist", tls, 1);
	DRW_shgroup_uniform_float(pd->snake_shgrp, "taper_l_strength", lanpr->use_same_taper ? tld : trd, 1);
	DRW_shgroup_uniform_float(pd->snake_shgrp, "taper_r_strength", lanpr->use_same_taper ? tls : trs, 1);
	DRW_shgroup_uniform_vec4(pd->snake_shgrp, "line_color", lanpr->line_color, 1);

	DRW_shgroup_call(pd->snake_shgrp, snake_batch, NULL);
	GPU_framebuffer_bind(fbl->edge_intermediate);

	DRW_draw_pass(psl->snake_pass);
	GPU_batch_discard(snake_batch);

	BLI_mempool_clear(pd->mp_sample);
	BLI_mempool_clear(pd->mp_line_strip);
	BLI_mempool_clear(pd->mp_line_strip_point);

	pd->pending_samples.first = pd->pending_samples.last = 0;
	pd->erased_samples.first = pd->erased_samples.last = 0;
	pd->line_strips.first = pd->line_strips.last = 0;

	GPU_framebuffer_bind(DefaultFB);
	DRW_multisamples_resolve(txl->depth, txl->edge_intermediate, 1);
}