Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / draw / engines / workbench / workbench_render.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 2016, Blender Foundation.
17  */
18
19 /** \file \ingroup draw_engine
20  *
21  * Render functions for final render output.
22  */
23
24 #include "BLI_rect.h"
25
26 #include "BKE_report.h"
27
28 #include "DRW_render.h"
29
30 #include "GPU_shader.h"
31
32 #include "DEG_depsgraph.h"
33 #include "DEG_depsgraph_query.h"
34
35 #include "RE_pipeline.h"
36
37 #include "workbench_private.h"
38
39 static void workbench_render_deferred_cache(
40         void *vedata, struct Object *ob,
41         struct RenderEngine *UNUSED(engine), struct Depsgraph *UNUSED(depsgraph))
42 {
43         workbench_deferred_solid_cache_populate(vedata, ob);
44 }
45
46 static void workbench_render_forward_cache(
47         void *vedata, struct Object *ob,
48         struct RenderEngine *UNUSED(engine), struct Depsgraph *UNUSED(depsgraph))
49 {
50         workbench_forward_cache_populate(vedata, ob);
51 }
52
53 static void workbench_render_matrices_init(RenderEngine *engine, Depsgraph *depsgraph)
54 {
55         /* TODO(sergey): Shall render hold pointer to an evaluated camera instead? */
56         Scene *scene = DEG_get_evaluated_scene(depsgraph);
57         struct Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, RE_GetCamera(engine->re));
58         float frame = BKE_scene_frame_get(scene);
59
60         /* Set the persective, view and window matrix. */
61         float winmat[4][4], wininv[4][4];
62         float viewmat[4][4], viewinv[4][4];
63         float persmat[4][4], persinv[4][4];
64
65         RE_GetCameraWindow(engine->re, ob_camera_eval, frame, winmat);
66         RE_GetCameraModelMatrix(engine->re, ob_camera_eval, viewinv);
67
68         invert_m4_m4(viewmat, viewinv);
69         mul_m4_m4m4(persmat, winmat, viewmat);
70         invert_m4_m4(persinv, persmat);
71         invert_m4_m4(wininv, winmat);
72
73         DRW_viewport_matrix_override_set(persmat, DRW_MAT_PERS);
74         DRW_viewport_matrix_override_set(persinv, DRW_MAT_PERSINV);
75         DRW_viewport_matrix_override_set(winmat, DRW_MAT_WIN);
76         DRW_viewport_matrix_override_set(wininv, DRW_MAT_WININV);
77         DRW_viewport_matrix_override_set(viewmat, DRW_MAT_VIEW);
78         DRW_viewport_matrix_override_set(viewinv, DRW_MAT_VIEWINV);
79 }
80
81 static bool workbench_render_framebuffers_init(void)
82 {
83         /* For image render, allocate own buffers because we don't have a viewport. */
84         const float *viewport_size = DRW_viewport_size_get();
85         const int size[2] = {(int)viewport_size[0], (int)viewport_size[1]};
86
87         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
88         dtxl->color = GPU_texture_create_2D(size[0], size[1], GPU_RGBA8, NULL, NULL);
89         dtxl->depth = GPU_texture_create_2D(size[0], size[1], GPU_DEPTH24_STENCIL8, NULL, NULL);
90
91         if (!(dtxl->depth && dtxl->color)) {
92                 return false;
93         }
94
95         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
96
97         GPU_framebuffer_ensure_config(&dfbl->default_fb, {
98                 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
99                 GPU_ATTACHMENT_TEXTURE(dtxl->color)
100         });
101
102         GPU_framebuffer_ensure_config(&dfbl->depth_only_fb, {
103                 GPU_ATTACHMENT_TEXTURE(dtxl->depth),
104                 GPU_ATTACHMENT_NONE
105         });
106
107         GPU_framebuffer_ensure_config(&dfbl->color_only_fb, {
108                 GPU_ATTACHMENT_NONE,
109                 GPU_ATTACHMENT_TEXTURE(dtxl->color)
110         });
111
112         bool ok = true;
113         ok = ok && GPU_framebuffer_check_valid(dfbl->default_fb, NULL);
114         ok = ok && GPU_framebuffer_check_valid(dfbl->color_only_fb, NULL);
115         ok = ok && GPU_framebuffer_check_valid(dfbl->depth_only_fb, NULL);
116
117         return ok;
118 }
119
120 static void workbench_render_framebuffers_finish(void)
121 {
122 }
123
124 void workbench_render(WORKBENCH_Data *data, RenderEngine *engine, RenderLayer *render_layer, const rcti *rect)
125 {
126         const DRWContextState *draw_ctx = DRW_context_state_get();
127         const Scene *scene = draw_ctx->scene;
128         Depsgraph *depsgraph = draw_ctx->depsgraph;
129         workbench_render_matrices_init(engine, depsgraph);
130
131         if (!workbench_render_framebuffers_init()) {
132                 RE_engine_report(engine, RPT_ERROR, "Failed to allocate OpenGL buffers");
133                 return;
134         }
135
136         const bool deferred = (scene->display.shading.flag & XRAY_FLAG(&scene->display)) == 0;
137
138         if (deferred) {
139                 /* Init engine. */
140                 workbench_deferred_engine_init(data);
141
142                 /* Init objects. */
143                 workbench_deferred_cache_init(data);
144                 DRW_render_object_iter(data, engine, depsgraph, workbench_render_deferred_cache);
145                 workbench_deferred_cache_finish(data);
146                 DRW_render_instance_buffer_finish();
147
148                 /* Draw. */
149                 int num_samples = workbench_taa_calculate_num_iterations(data);
150                 for (int sample = 0; sample < num_samples; sample++) {
151                         if (RE_engine_test_break(engine)) {
152                                 break;
153                         }
154                         /* TODO: Save matrices instead of recomputing them for each samples. */
155                         workbench_render_matrices_init(engine, depsgraph);
156
157                         workbench_deferred_draw_background(data);
158                         workbench_deferred_draw_scene(data);
159                 }
160
161                 workbench_deferred_draw_finish(data);
162         }
163         else {
164                 /* Init engine. */
165                 workbench_forward_engine_init(data);
166
167                 /* Init objects. */
168                 workbench_forward_cache_init(data);
169                 DRW_render_object_iter(data, engine, depsgraph, workbench_render_forward_cache);
170                 workbench_forward_cache_finish(data);
171                 DRW_render_instance_buffer_finish();
172
173                 /* Draw. */
174                 int num_samples = workbench_taa_calculate_num_iterations(data);
175                 for (int sample = 0; sample < num_samples; sample++) {
176                         if (RE_engine_test_break(engine)) {
177                                 break;
178                         }
179
180                         workbench_forward_draw_background(data);
181                         workbench_forward_draw_scene(data);
182                 }
183
184                 workbench_forward_draw_finish(data);
185         }
186
187         /* Write render output. */
188         const char *viewname = RE_GetActiveRenderView(engine->re);
189         RenderPass *rp = RE_pass_find_by_name(render_layer, RE_PASSNAME_COMBINED, viewname);
190
191         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
192         GPU_framebuffer_bind(dfbl->color_only_fb);
193         GPU_framebuffer_read_color(dfbl->color_only_fb,
194                                    rect->xmin, rect->ymin,
195                                    BLI_rcti_size_x(rect), BLI_rcti_size_y(rect),
196                                    4, 0, rp->rect);
197
198         workbench_render_framebuffers_finish();
199 }
200
201 void workbench_render_update_passes(RenderEngine *engine, Scene *scene, ViewLayer *view_layer)
202 {
203         RE_engine_register_pass(engine, scene, view_layer, RE_PASSNAME_COMBINED, 4, "RGBA", SOCK_RGBA);
204 }