GPU: refactor clipped drawing from DRW into GPU
[blender.git] / source / blender / draw / engines / external / external_engine.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2017, Blender Foundation.
17  */
18
19 /** \file external_engine.c
20  *  \ingroup draw_engine
21  *
22  * Base engine for external render engines.
23  * We use it for depth and non-mesh objects.
24  */
25
26 #include "DRW_render.h"
27
28 #include "DNA_screen_types.h"
29 #include "DNA_view3d_types.h"
30
31
32 #include "ED_screen.h"
33
34 #include "GPU_matrix.h"
35 #include "GPU_shader.h"
36 #include "GPU_viewport.h"
37
38 /* Shaders */
39
40 #define EXTERNAL_ENGINE "BLENDER_EXTERNAL"
41
42 /* *********** LISTS *********** */
43
44 /* GPUViewport.storage
45  * Is freed everytime the viewport engine changes */
46 typedef struct EXTERNAL_Storage {
47         int dummy;
48 } EXTERNAL_Storage;
49
50 typedef struct EXTERNAL_StorageList {
51         struct EXTERNAL_Storage *storage;
52         struct EXTERNAL_PrivateData *g_data;
53 } EXTERNAL_StorageList;
54
55 typedef struct EXTERNAL_FramebufferList {
56         struct GPUFrameBuffer *default_fb;
57 } EXTERNAL_FramebufferList;
58
59 typedef struct EXTERNAL_TextureList {
60         /* default */
61         struct GPUTexture *depth;
62 } EXTERNAL_TextureList;
63
64 typedef struct EXTERNAL_PassList {
65         struct DRWPass *depth_pass;
66 } EXTERNAL_PassList;
67
68 typedef struct EXTERNAL_Data {
69         void *engine_type;
70         EXTERNAL_FramebufferList *fbl;
71         EXTERNAL_TextureList *txl;
72         EXTERNAL_PassList *psl;
73         EXTERNAL_StorageList *stl;
74         char info[GPU_INFO_SIZE];
75 } EXTERNAL_Data;
76
77 /* *********** STATIC *********** */
78
79 static struct {
80         /* Depth Pre Pass */
81         struct GPUShader *depth_sh;
82 } e_data = {NULL}; /* Engine data */
83
84 typedef struct EXTERNAL_PrivateData {
85         DRWShadingGroup *depth_shgrp;
86 } EXTERNAL_PrivateData; /* Transient data */
87
88 /* Functions */
89
90 static void external_engine_init(void *UNUSED(vedata))
91 {
92         /* Depth prepass */
93         if (!e_data.depth_sh) {
94                 e_data.depth_sh = DRW_shader_create_3D_depth_only(GPU_SHADER_CFG_DEFAULT);
95         }
96 }
97
98 static void external_cache_init(void *vedata)
99 {
100         EXTERNAL_PassList *psl = ((EXTERNAL_Data *)vedata)->psl;
101         EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl;
102
103         if (!stl->g_data) {
104                 /* Alloc transient pointers */
105                 stl->g_data = MEM_mallocN(sizeof(*stl->g_data), __func__);
106         }
107
108         /* Depth Pass */
109         {
110                 psl->depth_pass = DRW_pass_create("Depth Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS_EQUAL);
111                 stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);
112         }
113 }
114
115 static void external_cache_populate(void *vedata, Object *ob)
116 {
117         EXTERNAL_StorageList *stl = ((EXTERNAL_Data *)vedata)->stl;
118
119         if (!DRW_object_is_renderable(ob)) {
120                 return;
121         }
122
123         struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
124         if (geom) {
125                 /* Depth Prepass */
126                 DRW_shgroup_call_add(stl->g_data->depth_shgrp, geom, ob->obmat);
127         }
128 }
129
130 static void external_cache_finish(void *UNUSED(vedata))
131 {
132 }
133
134 static void external_draw_scene_do(void *vedata)
135 {
136         const DRWContextState *draw_ctx = DRW_context_state_get();
137         Scene *scene = draw_ctx->scene;
138         RegionView3D *rv3d = draw_ctx->rv3d;
139         ARegion *ar = draw_ctx->ar;
140         RenderEngineType *type;
141
142         DRW_state_reset_ex(DRW_STATE_DEFAULT & ~DRW_STATE_DEPTH_LESS_EQUAL);
143
144         /* Create render engine. */
145         if (!rv3d->render_engine) {
146                 RenderEngineType *engine_type = draw_ctx->engine_type;
147
148                 if (!(engine_type->view_update && engine_type->view_draw)) {
149                         return;
150                 }
151
152                 RenderEngine *engine = RE_engine_create_ex(engine_type, true);
153                 engine->tile_x = scene->r.tilex;
154                 engine->tile_y = scene->r.tiley;
155                 engine_type->view_update(engine, draw_ctx->evil_C);
156                 rv3d->render_engine = engine;
157         }
158
159         /* Rendered draw. */
160         GPU_matrix_push_projection();
161         ED_region_pixelspace(ar);
162
163         /* Render result draw. */
164         type = rv3d->render_engine->type;
165         type->view_draw(rv3d->render_engine, draw_ctx->evil_C);
166
167         GPU_matrix_pop_projection();
168
169         /* Set render info. */
170         EXTERNAL_Data *data = vedata;
171         if (rv3d->render_engine->text[0] != '\0') {
172                 BLI_strncpy(data->info, rv3d->render_engine->text, sizeof(data->info));
173         }
174         else {
175                 data->info[0] = '\0';
176         }
177 }
178
179 static void external_draw_scene(void *vedata)
180 {
181         const DRWContextState *draw_ctx = DRW_context_state_get();
182         EXTERNAL_PassList *psl = ((EXTERNAL_Data *)vedata)->psl;
183
184         /* Will be NULL during OpenGL render.
185          * OpenGL render is used for quick preview (thumbnails or sequencer preview)
186          * where using the rendering engine to preview doesn't make so much sense. */
187         if (draw_ctx->evil_C) {
188                 external_draw_scene_do(vedata);
189         }
190         DRW_draw_pass(psl->depth_pass);
191 }
192
193 static void external_engine_free(void)
194 {
195         /* All shaders are builtin. */
196 }
197
198 static const DrawEngineDataSize external_data_size = DRW_VIEWPORT_DATA_SIZE(EXTERNAL_Data);
199
200 DrawEngineType draw_engine_external_type = {
201         NULL, NULL,
202         N_("External"),
203         &external_data_size,
204         &external_engine_init,
205         &external_engine_free,
206         &external_cache_init,
207         &external_cache_populate,
208         &external_cache_finish,
209         NULL,
210         &external_draw_scene,
211         NULL,
212         NULL,
213         NULL,
214 };
215
216 /* Note: currently unused, we should not register unless we want to see this when debugging the view. */
217
218 RenderEngineType DRW_engine_viewport_external_type = {
219         NULL, NULL,
220         EXTERNAL_ENGINE, N_("External"), RE_INTERNAL,
221         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
222         &draw_engine_external_type,
223         {NULL, NULL, NULL},
224 };
225
226 #undef EXTERNAL_ENGINE