Merge remote-tracking branch 'origin/master' into blender2.8
[blender.git] / source / blender / draw / modes / edit_text_mode.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file blender/draw/modes/edit_text_mode.c
23  *  \ingroup draw
24  */
25
26 #include "DRW_engine.h"
27 #include "DRW_render.h"
28
29 #include "DNA_curve_types.h"
30
31 #include "BIF_glutil.h"
32
33 /* If builtin shaders are needed */
34 #include "GPU_shader.h"
35 #include "GPU_batch.h"
36
37 #include "draw_common.h"
38
39 #include "draw_mode_engines.h"
40
41 /* If needed, contains all global/Theme colors
42  * Add needed theme colors / values to DRW_globals_update() and update UBO
43  * Not needed for constant color. */
44 extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */
45 extern struct GlobalsUboStorage ts; /* draw_common.c */
46
47 /* *********** LISTS *********** */
48 /* All lists are per viewport specific datas.
49  * They are all free when viewport changes engines
50  * or is free itself. Use EDIT_TEXT_engine_init() to
51  * initialize most of them and EDIT_TEXT_cache_init()
52  * for EDIT_TEXT_PassList */
53
54 typedef struct EDIT_TEXT_PassList {
55         /* Declare all passes here and init them in
56          * EDIT_TEXT_cache_init().
57          * Only contains (DRWPass *) */
58         struct DRWPass *wire_pass;
59         struct DRWPass *overlay_select_pass;
60         struct DRWPass *overlay_cursor_pass;
61         struct DRWPass *text_box_pass;
62 } EDIT_TEXT_PassList;
63
64 typedef struct EDIT_TEXT_FramebufferList {
65         /* Contains all framebuffer objects needed by this engine.
66          * Only contains (GPUFrameBuffer *) */
67         struct GPUFrameBuffer *fb;
68 } EDIT_TEXT_FramebufferList;
69
70 typedef struct EDIT_TEXT_TextureList {
71         /* Contains all framebuffer textures / utility textures
72          * needed by this engine. Only viewport specific textures
73          * (not per object). Only contains (GPUTexture *) */
74         struct GPUTexture *texture;
75 } EDIT_TEXT_TextureList;
76
77 typedef struct EDIT_TEXT_StorageList {
78         /* Contains any other memory block that the engine needs.
79          * Only directly MEM_(m/c)allocN'ed blocks because they are
80          * free with MEM_freeN() when viewport is freed.
81          * (not per object) */
82         struct CustomStruct *block;
83         struct EDIT_TEXT_PrivateData *g_data;
84 } EDIT_TEXT_StorageList;
85
86 typedef struct EDIT_TEXT_Data {
87         /* Struct returned by DRW_viewport_engine_data_ensure.
88          * If you don't use one of these, just make it a (void *) */
89         // void *fbl;
90         void *engine_type; /* Required */
91         EDIT_TEXT_FramebufferList *fbl;
92         EDIT_TEXT_TextureList *txl;
93         EDIT_TEXT_PassList *psl;
94         EDIT_TEXT_StorageList *stl;
95 } EDIT_TEXT_Data;
96
97 /* *********** STATIC *********** */
98
99 static struct {
100         /* Custom shaders :
101          * Add sources to source/blender/draw/modes/shaders
102          * init in EDIT_TEXT_engine_init();
103          * free in EDIT_TEXT_engine_free(); */
104         GPUShader *wire_sh;
105         GPUShader *overlay_select_sh;
106         GPUShader *overlay_cursor_sh;
107 } e_data = {NULL}; /* Engine data */
108
109 typedef struct EDIT_TEXT_PrivateData {
110         /* resulting curve as 'wire' for fast editmode drawing */
111         DRWShadingGroup *wire_shgrp;
112         DRWShadingGroup *overlay_select_shgrp;
113         DRWShadingGroup *overlay_cursor_shgrp;
114         DRWShadingGroup *box_shgrp;
115         DRWShadingGroup *box_active_shgrp;
116 } EDIT_TEXT_PrivateData; /* Transient data */
117
118 /* *********** FUNCTIONS *********** */
119
120 /* Init Textures, Framebuffers, Storage and Shaders.
121  * It is called for every frames.
122  * (Optional) */
123 static void EDIT_TEXT_engine_init(void *vedata)
124 {
125         EDIT_TEXT_TextureList *txl = ((EDIT_TEXT_Data *)vedata)->txl;
126         EDIT_TEXT_FramebufferList *fbl = ((EDIT_TEXT_Data *)vedata)->fbl;
127         EDIT_TEXT_StorageList *stl = ((EDIT_TEXT_Data *)vedata)->stl;
128
129         UNUSED_VARS(txl, fbl, stl);
130
131         /* Init Framebuffers like this: order is attachment order (for color texs) */
132         /*
133          * DRWFboTexture tex[2] = {{&txl->depth, GPU_DEPTH_COMPONENT24, 0},
134          *                         {&txl->color, GPU_RGBA8, DRW_TEX_FILTER}};
135          */
136
137         /* DRW_framebuffer_init takes care of checking if
138          * the framebuffer is valid and has the right size*/
139         /*
140          * float *viewport_size = DRW_viewport_size_get();
141          * DRW_framebuffer_init(&fbl->occlude_wire_fb,
142          *                     (int)viewport_size[0], (int)viewport_size[1],
143          *                     tex, 2);
144          */
145
146         if (!e_data.wire_sh) {
147                 e_data.wire_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
148         }
149
150         if (!e_data.overlay_select_sh) {
151                 e_data.overlay_select_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
152         }
153
154         if (!e_data.overlay_cursor_sh) {
155                 e_data.overlay_cursor_sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR);
156         }
157 }
158
159 /* Here init all passes and shading groups
160  * Assume that all Passes are NULL */
161 static void EDIT_TEXT_cache_init(void *vedata)
162 {
163         EDIT_TEXT_PassList *psl = ((EDIT_TEXT_Data *)vedata)->psl;
164         EDIT_TEXT_StorageList *stl = ((EDIT_TEXT_Data *)vedata)->stl;
165
166         if (!stl->g_data) {
167                 /* Alloc transient pointers */
168                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
169         }
170
171         {
172                 /* Text outline (fast drawing!) */
173                 psl->wire_pass = DRW_pass_create(
174                         "Font Wire",
175                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_WIRE);
176                 stl->g_data->wire_shgrp = DRW_shgroup_create(e_data.wire_sh, psl->wire_pass);
177
178                 psl->overlay_select_pass = DRW_pass_create(
179                         "Font Select",
180                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH);
181                 stl->g_data->overlay_select_shgrp = DRW_shgroup_create(e_data.overlay_select_sh, psl->overlay_select_pass);
182
183                 psl->overlay_cursor_pass = DRW_pass_create(
184                         "Font Cursor",
185                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH);
186                 stl->g_data->overlay_cursor_shgrp = DRW_shgroup_create(e_data.overlay_cursor_sh, psl->overlay_cursor_pass);
187
188                 psl->text_box_pass = DRW_pass_create(
189                         "Font Text Boxes",
190                         DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH);
191                 stl->g_data->box_shgrp = shgroup_dynlines_dashed_uniform_color(psl->text_box_pass, ts.colorWire);
192                 stl->g_data->box_active_shgrp = shgroup_dynlines_dashed_uniform_color(psl->text_box_pass, ts.colorActive);
193         }
194 }
195
196 static void edit_text_cache_populate_boxes(void *vedata, Object *ob)
197 {
198         EDIT_TEXT_StorageList *stl = ((EDIT_TEXT_Data *)vedata)->stl;
199         const Curve *cu = ob->data;
200
201         DRWShadingGroup *shading_groups[] = {
202             stl->g_data->box_active_shgrp,
203             stl->g_data->box_shgrp,
204         };
205
206         float vec[3], vec1[3], vec2[3];
207         for (int i = 0; i < cu->totbox; i++) {
208                 TextBox *tb = &cu->tb[i];
209
210                 if ((tb->w == 0.0f) && (tb->h == 0.0f)) {
211                         continue;
212                 }
213
214                 const bool is_active = i == (cu->actbox - 1);
215                 DRWShadingGroup *shading_group = shading_groups[is_active ? 0 : 1];
216
217                 vec[0] = cu->xof + tb->x;
218                 vec[1] = cu->yof + tb->y + cu->fsize_realtime;
219                 vec[2] = 0.001;
220
221                 mul_v3_m4v3(vec1, ob->obmat, vec);
222                 vec[0] += tb->w;
223                 mul_v3_m4v3(vec2, ob->obmat, vec);
224
225                 DRW_shgroup_call_dynamic_add(shading_group, vec1);
226                 DRW_shgroup_call_dynamic_add(shading_group, vec2);
227
228                 vec[1] -= tb->h;
229                 copy_v3_v3(vec1, vec2);
230                 mul_v3_m4v3(vec2, ob->obmat, vec);
231
232                 DRW_shgroup_call_dynamic_add(shading_group, vec1);
233                 DRW_shgroup_call_dynamic_add(shading_group, vec2);
234
235                 vec[0] -= tb->w;
236                 copy_v3_v3(vec1, vec2);
237                 mul_v3_m4v3(vec2, ob->obmat, vec);
238
239                 DRW_shgroup_call_dynamic_add(shading_group, vec1);
240                 DRW_shgroup_call_dynamic_add(shading_group, vec2);
241
242                 vec[1] += tb->h;
243                 copy_v3_v3(vec1, vec2);
244                 mul_v3_m4v3(vec2, ob->obmat, vec);
245
246                 DRW_shgroup_call_dynamic_add(shading_group, vec1);
247                 DRW_shgroup_call_dynamic_add(shading_group, vec2);
248         }
249 }
250
251 /* Add geometry to shadingGroups. Execute for each objects */
252 static void EDIT_TEXT_cache_populate(void *vedata, Object *ob)
253 {
254         EDIT_TEXT_PassList *psl = ((EDIT_TEXT_Data *)vedata)->psl;
255         EDIT_TEXT_StorageList *stl = ((EDIT_TEXT_Data *)vedata)->stl;
256         const DRWContextState *draw_ctx = DRW_context_state_get();
257
258         UNUSED_VARS(psl, stl);
259
260         if (ob->type == OB_FONT) {
261                 if (ob == draw_ctx->object_edit) {
262                         const Curve *cu = ob->data;
263                         /* Get geometry cache */
264                         struct GPUBatch *geom;
265
266                         if (cu->flag & CU_FAST) {
267                                 geom = DRW_cache_text_edge_wire_get(ob);
268                                 if (geom) {
269                                         DRW_shgroup_call_add(stl->g_data->wire_shgrp, geom, ob->obmat);
270                                 }
271                         }
272                         else {
273                                 /* object mode draws */
274                         }
275
276                         geom = DRW_cache_text_select_overlay_get(ob);
277                         if (geom) {
278                                 DRW_shgroup_call_add(stl->g_data->overlay_select_shgrp, geom, ob->obmat);
279                         }
280
281                         geom = DRW_cache_text_cursor_overlay_get(ob);
282                         if (geom) {
283                                 DRW_shgroup_call_add(stl->g_data->overlay_cursor_shgrp, geom, ob->obmat);
284                         }
285
286                         edit_text_cache_populate_boxes(vedata, ob);
287                 }
288         }
289 }
290
291 /* Optional: Post-cache_populate callback */
292 static void EDIT_TEXT_cache_finish(void *vedata)
293 {
294         EDIT_TEXT_PassList *psl = ((EDIT_TEXT_Data *)vedata)->psl;
295         EDIT_TEXT_StorageList *stl = ((EDIT_TEXT_Data *)vedata)->stl;
296
297         /* Do something here! dependent on the objects gathered */
298         UNUSED_VARS(psl, stl);
299 }
300
301 /* Draw time ! Control rendering pipeline from here */
302 static void EDIT_TEXT_draw_scene(void *vedata)
303 {
304         EDIT_TEXT_PassList *psl = ((EDIT_TEXT_Data *)vedata)->psl;
305         EDIT_TEXT_FramebufferList *fbl = ((EDIT_TEXT_Data *)vedata)->fbl;
306
307         /* Default framebuffer and texture */
308         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
309         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
310
311         UNUSED_VARS(fbl, dfbl, dtxl);
312
313         /* Show / hide entire passes, swap framebuffers ... whatever you fancy */
314         /*
315          * DRW_framebuffer_texture_detach(dtxl->depth);
316          * DRW_framebuffer_bind(fbl->custom_fb);
317          * DRW_draw_pass(psl->pass);
318          * DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0);
319          * DRW_framebuffer_bind(dfbl->default_fb);
320          */
321
322         DRW_draw_pass(psl->wire_pass);
323
324         if (!DRW_pass_is_empty(psl->text_box_pass)) {
325                 DRW_draw_pass(psl->text_box_pass);
326         }
327
328         set_inverted_drawing(1);
329         DRW_draw_pass(psl->overlay_select_pass);
330         DRW_draw_pass(psl->overlay_cursor_pass);
331         set_inverted_drawing(0);
332
333         /* If you changed framebuffer, double check you rebind
334          * the default one with its textures attached before finishing */
335 }
336
337 /* Cleanup when destroying the engine.
338  * This is not per viewport ! only when quitting blender.
339  * Mostly used for freeing shaders */
340 static void EDIT_TEXT_engine_free(void)
341 {
342         // DRW_SHADER_FREE_SAFE(custom_shader);
343 }
344
345 static const DrawEngineDataSize EDIT_TEXT_data_size = DRW_VIEWPORT_DATA_SIZE(EDIT_TEXT_Data);
346
347 DrawEngineType draw_engine_edit_text_type = {
348         NULL, NULL,
349         N_("EditTextMode"),
350         &EDIT_TEXT_data_size,
351         &EDIT_TEXT_engine_init,
352         &EDIT_TEXT_engine_free,
353         &EDIT_TEXT_cache_init,
354         &EDIT_TEXT_cache_populate,
355         &EDIT_TEXT_cache_finish,
356         NULL, /* draw_background but not needed by mode engines */
357         &EDIT_TEXT_draw_scene,
358         NULL,
359         NULL,
360 };