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

select_engine.c « select « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6687a7be92777845ced3f8ebbf97fd4f8c8a503d (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
/*
 * Copyright 2016, Blender Foundation.
 *
 * 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.
 *
 * Contributor(s): Blender Institute
 *
 */

#include "DRW_render.h"

#include "BKE_icons.h"
#include "BKE_idprop.h"
#include "BKE_main.h"

#include "GPU_shader.h"

#include "select_engine.h"
/* Shaders */

#define SELECT_ENGINE "BLENDER_SELECT"

/* we may want this later? */
#define USE_DEPTH

/* *********** LISTS *********** */

/* GPUViewport.storage
 * Is freed everytime the viewport engine changes */
typedef struct SELECT_Storage {
	int dummy;
} SELECT_Storage;

typedef struct SELECT_StorageList {
	struct SELECT_Storage *storage;
	struct g_data *g_data;
} SELECT_StorageList;

typedef struct SELECT_FramebufferList {
	/* default */
	struct GPUFrameBuffer *default_fb;
	/* engine specific */
#ifdef USE_DEPTH
	struct GPUFrameBuffer *dupli_depth;
#endif
} SELECT_FramebufferList;

typedef struct SELECT_TextureList {
	/* default */
	struct GPUTexture *color;
#ifdef USE_DEPTH
	struct GPUTexture *depth;
	/* engine specific */
	struct GPUTexture *depth_dup;
#endif
} SELECT_TextureList;

typedef struct SELECT_PassList {
#ifdef USE_DEPTH
	struct DRWPass *depth_pass;
	struct DRWPass *depth_pass_cull;
#endif
	struct DRWPass *color_pass;
	struct g_data *g_data;
} SELECT_PassList;

typedef struct SELECT_Data {
	void *engine_type;
	SELECT_FramebufferList *fbl;
	SELECT_TextureList *txl;
	SELECT_PassList *psl;
	SELECT_StorageList *stl;
} SELECT_Data;

/* *********** STATIC *********** */

static struct {
#ifdef USE_DEPTH
	/* Depth Pre Pass */
	struct GPUShader *depth_sh;
#endif
	/* Shading Pass */
	struct GPUShader *color_sh;
} e_data = {NULL}; /* Engine data */

typedef struct g_data {
#ifdef USE_DEPTH
	DRWShadingGroup *depth_shgrp;
	DRWShadingGroup *depth_shgrp_cull;
#endif
	DRWShadingGroup *color_shgrp;
} g_data; /* Transient data */

/* Functions */

static void SELECT_engine_init(void *vedata)
{
	SELECT_StorageList *stl = ((SELECT_Data *)vedata)->stl;
	SELECT_TextureList *txl = ((SELECT_Data *)vedata)->txl;
	SELECT_FramebufferList *fbl = ((SELECT_Data *)vedata)->fbl;

#ifdef USE_DEPTH
	/* Depth prepass */
	if (!e_data.depth_sh) {
		e_data.depth_sh = DRW_shader_create_3D_depth_only();
	}
#endif

	/* Shading pass */
	if (!e_data.color_sh) {
		e_data.color_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
	}

	if (!stl->storage) {
		stl->storage = MEM_callocN(sizeof(SELECT_Storage), "SELECT_Storage");
	}

#ifdef USE_DEPTH
	if (DRW_state_is_fbo()) {
		const float *viewport_size = DRW_viewport_size_get();
		DRWFboTexture tex = {&txl->depth_dup, DRW_BUF_DEPTH_24, 0};
		DRW_framebuffer_init(&fbl->dupli_depth,
		                     (int)viewport_size[0], (int)viewport_size[1],
		                     &tex, 1);
	}
#endif
}

static void SELECT_cache_init(void *vedata)
{
	SELECT_PassList *psl = ((SELECT_Data *)vedata)->psl;
	SELECT_StorageList *stl = ((SELECT_Data *)vedata)->stl;

	if (!stl->g_data) {
		/* Alloc transient pointers */
		stl->g_data = MEM_mallocN(sizeof(g_data), "g_data");
	}

#ifdef USE_DEPTH
	/* Depth Pass */
	{
		psl->depth_pass = DRW_pass_create("Depth Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
		stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);

		psl->depth_pass_cull = DRW_pass_create(
		        "Depth Pass Cull",
		        DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_CULL_BACK);
		stl->g_data->depth_shgrp_cull = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass_cull);
	}
#endif

	/* Color Pass */
	{
		psl->color_pass = DRW_pass_create("Color Pass", DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL);
		stl->g_data->color_shgrp = DRW_shgroup_create(e_data.color_sh, psl->color_pass);
	}
}

static void SELECT_cache_populate(void *vedata, Object *ob)
{
	SELECT_StorageList *stl = ((SELECT_Data *)vedata)->stl;

	if (!DRW_is_object_renderable(ob))
		return;

	struct Batch *geom = DRW_cache_object_surface_get(ob);
	if (geom) {
		bool do_cull = false;  /* TODO (we probably wan't to take this from the viewport?) */
#ifdef USE_DEPTH
		/* Depth Prepass */
		DRW_shgroup_call_add((do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp, geom, ob->obmat);
#endif
		/* Shading */
		DRW_shgroup_call_add(stl->g_data->color_shgrp, geom, ob->obmat);
	}
}

static void SELECT_cache_finish(void *vedata)
{
	SELECT_StorageList *stl = ((SELECT_Data *)vedata)->stl;

	UNUSED_VARS(stl);
}

static void SELECT_draw_scene(void *vedata)
{

	SELECT_PassList *psl = ((SELECT_Data *)vedata)->psl;
	SELECT_FramebufferList *fbl = ((SELECT_Data *)vedata)->fbl;
	DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();

#ifdef USE_DEPTH
	/* Pass 1 : Depth pre-pass */
	DRW_draw_pass(psl->depth_pass);
	DRW_draw_pass(psl->depth_pass_cull);

	/* Pass 2 : Duplicate depth */
	/* Unless we go for deferred shading we need this to avoid manual depth test and artifacts */
	if (DRW_state_is_fbo()) {
		DRW_framebuffer_blit(dfbl->default_fb, fbl->dupli_depth, true);
	}
#endif

	/* Pass 3 : Shading */
	DRW_draw_pass(psl->color_pass);
}

static void SELECT_engine_free(void)
{
	/* all shaders are builtin */
}

static const DrawEngineDataSize SELECT_data_size = DRW_VIEWPORT_DATA_SIZE(SELECT_Data);

DrawEngineType draw_engine_select_type = {
	NULL, NULL,
	N_("SelectID"),
	&SELECT_data_size,
	&SELECT_engine_init,
	&SELECT_engine_free,
	&SELECT_cache_init,
	&SELECT_cache_populate,
	&SELECT_cache_finish,
	NULL,
	&SELECT_draw_scene
};

RenderEngineType DRW_engine_viewport_select_type = {
	NULL, NULL,
	SELECT_ENGINE, N_("SelectID"), RE_INTERNAL | RE_USE_OGL_PIPELINE,
	NULL, NULL, NULL, NULL, NULL, NULL, NULL,
	&draw_engine_select_type,
	{NULL, NULL, NULL}
};


#undef SELECT_ENGINE