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
20  * \ingroup draw
21  */
22 
23 /* This is the Render Functions used by Realtime engines to draw with OpenGL */
24 
25 #pragma once
26 
27 #include "DRW_engine_types.h"
28 
29 #include "BLI_listbase.h"
30 #include "BLI_math_matrix.h"
31 #include "BLI_math_vector.h"
32 #include "BLI_string.h"
33 
34 #include "BKE_context.h"
35 #include "BKE_layer.h"
36 #include "BKE_material.h"
37 #include "BKE_scene.h"
38 
39 #include "BLT_translation.h"
40 
41 #include "DNA_light_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_world_types.h"
46 
47 #include "GPU_framebuffer.h"
48 #include "GPU_primitive.h"
49 #include "GPU_shader.h"
50 #include "GPU_texture.h"
51 #include "GPU_uniform_buffer.h"
52 
53 #include "draw_cache.h"
54 #include "draw_common.h"
55 #include "draw_view.h"
56 
57 #include "draw_debug.h"
58 #include "draw_manager_profiling.h"
59 
60 #include "MEM_guardedalloc.h"
61 
62 #include "RE_engine.h"
63 
64 #include "DEG_depsgraph.h"
65 
66 struct GPUBatch;
67 struct GPUFrameBuffer;
68 struct GPUMaterial;
69 struct GPUShader;
70 struct GPUTexture;
71 struct GPUUniformBuf;
72 struct Object;
73 struct ParticleSystem;
74 struct RenderEngineType;
75 struct bContext;
76 struct rcti;
77 
78 typedef struct DRWCallBuffer DRWCallBuffer;
79 typedef struct DRWInterface DRWInterface;
80 typedef struct DRWPass DRWPass;
81 typedef struct DRWShaderLibrary DRWShaderLibrary;
82 typedef struct DRWShadingGroup DRWShadingGroup;
83 typedef struct DRWUniform DRWUniform;
84 typedef struct DRWView DRWView;
85 
86 /* TODO Put it somewhere else? */
87 typedef struct BoundSphere {
88   float center[3], radius;
89 } BoundSphere;
90 
91 /* declare members as empty (unused) */
92 typedef char DRWViewportEmptyList;
93 
94 #define DRW_VIEWPORT_LIST_SIZE(list) \
95   (sizeof(list) == sizeof(DRWViewportEmptyList) ? 0 : ((sizeof(list)) / sizeof(void *)))
96 
97 /* Unused members must be either pass list or 'char *' when not usd. */
98 #define DRW_VIEWPORT_DATA_SIZE(ty) \
99   { \
100     DRW_VIEWPORT_LIST_SIZE(*(((ty *)NULL)->fbl)), DRW_VIEWPORT_LIST_SIZE(*(((ty *)NULL)->txl)), \
101         DRW_VIEWPORT_LIST_SIZE(*(((ty *)NULL)->psl)), \
102         DRW_VIEWPORT_LIST_SIZE(*(((ty *)NULL)->stl)), \
103   }
104 
105 typedef struct DrawEngineDataSize {
106   int fbl_len;
107   int txl_len;
108   int psl_len;
109   int stl_len;
110 } DrawEngineDataSize;
111 
112 typedef struct DrawEngineType {
113   struct DrawEngineType *next, *prev;
114 
115   char idname[32];
116 
117   const DrawEngineDataSize *vedata_size;
118 
119   void (*engine_init)(void *vedata);
120   void (*engine_free)(void);
121 
122   void (*cache_init)(void *vedata);
123   void (*cache_populate)(void *vedata, struct Object *ob);
124   void (*cache_finish)(void *vedata);
125 
126   void (*draw_scene)(void *vedata);
127 
128   void (*view_update)(void *vedata);
129   void (*id_update)(void *vedata, struct ID *id);
130 
131   void (*render_to_image)(void *vedata,
132                           struct RenderEngine *engine,
133                           struct RenderLayer *layer,
134                           const struct rcti *rect);
135 } DrawEngineType;
136 
137 /* Textures */
138 typedef enum {
139   DRW_TEX_FILTER = (1 << 0),
140   DRW_TEX_WRAP = (1 << 1),
141   DRW_TEX_COMPARE = (1 << 2),
142   DRW_TEX_MIPMAP = (1 << 3),
143 } DRWTextureFlag;
144 
145 /* Textures from DRW_texture_pool_query_* have the options
146  * DRW_TEX_FILTER for color float textures, and no options
147  * for depth textures and integer textures. */
148 struct GPUTexture *DRW_texture_pool_query_2d(int w,
149                                              int h,
150                                              eGPUTextureFormat format,
151                                              DrawEngineType *engine_type);
152 struct GPUTexture *DRW_texture_pool_query_fullscreen(eGPUTextureFormat format,
153                                                      DrawEngineType *engine_type);
154 
155 struct GPUTexture *DRW_texture_create_1d(int w,
156                                          eGPUTextureFormat format,
157                                          DRWTextureFlag flags,
158                                          const float *fpixels);
159 struct GPUTexture *DRW_texture_create_2d(
160     int w, int h, eGPUTextureFormat format, DRWTextureFlag flags, const float *fpixels);
161 struct GPUTexture *DRW_texture_create_2d_array(
162     int w, int h, int d, eGPUTextureFormat format, DRWTextureFlag flags, const float *fpixels);
163 struct GPUTexture *DRW_texture_create_3d(
164     int w, int h, int d, eGPUTextureFormat format, DRWTextureFlag flags, const float *fpixels);
165 struct GPUTexture *DRW_texture_create_cube(int w,
166                                            eGPUTextureFormat format,
167                                            DRWTextureFlag flags,
168                                            const float *fpixels);
169 struct GPUTexture *DRW_texture_create_cube_array(
170     int w, int d, eGPUTextureFormat format, DRWTextureFlag flags, const float *fpixels);
171 
172 void DRW_texture_ensure_fullscreen_2d(struct GPUTexture **tex,
173                                       eGPUTextureFormat format,
174                                       DRWTextureFlag flags);
175 void DRW_texture_ensure_2d(
176     struct GPUTexture **tex, int w, int h, eGPUTextureFormat format, DRWTextureFlag flags);
177 
178 void DRW_texture_generate_mipmaps(struct GPUTexture *tex);
179 void DRW_texture_free(struct GPUTexture *tex);
180 #define DRW_TEXTURE_FREE_SAFE(tex) \
181   do { \
182     if (tex != NULL) { \
183       DRW_texture_free(tex); \
184       tex = NULL; \
185     } \
186   } while (0)
187 
188 #define DRW_UBO_FREE_SAFE(ubo) \
189   do { \
190     if (ubo != NULL) { \
191       GPU_uniformbuf_free(ubo); \
192       ubo = NULL; \
193     } \
194   } while (0)
195 
196 /* Shaders */
197 
198 #ifndef __GPU_MATERIAL_H__
199 /* FIXME: Meh avoid including all GPUMaterial. */
200 typedef void (*GPUMaterialEvalCallbackFn)(struct GPUMaterial *mat,
201                                           int options,
202                                           const char **vert_code,
203                                           const char **geom_code,
204                                           const char **frag_lib,
205                                           const char **defines);
206 #endif
207 
208 struct GPUShader *DRW_shader_create_ex(
209     const char *vert, const char *geom, const char *frag, const char *defines, const char *name);
210 struct GPUShader *DRW_shader_create_with_lib_ex(const char *vert,
211                                                 const char *geom,
212                                                 const char *frag,
213                                                 const char *lib,
214                                                 const char *defines,
215                                                 const char *name);
216 struct GPUShader *DRW_shader_create_with_shaderlib_ex(const char *vert,
217                                                       const char *geom,
218                                                       const char *frag,
219                                                       const DRWShaderLibrary *lib,
220                                                       const char *defines,
221                                                       const char *name);
222 struct GPUShader *DRW_shader_create_with_transform_feedback(const char *vert,
223                                                             const char *geom,
224                                                             const char *defines,
225                                                             const eGPUShaderTFBType prim_type,
226                                                             const char **varying_names,
227                                                             const int varying_count);
228 struct GPUShader *DRW_shader_create_fullscreen_ex(const char *frag,
229                                                   const char *defines,
230                                                   const char *name);
231 struct GPUShader *DRW_shader_create_fullscreen_with_shaderlib_ex(const char *frag,
232                                                                  const DRWShaderLibrary *lib,
233                                                                  const char *defines,
234                                                                  const char *name);
235 #define DRW_shader_create(vert, geom, frag, defines) \
236   DRW_shader_create_ex(vert, geom, frag, defines, __func__)
237 #define DRW_shader_create_with_lib(vert, geom, frag, lib, defines) \
238   DRW_shader_create_with_lib_ex(vert, geom, frag, lib, defines, __func__)
239 #define DRW_shader_create_with_shaderlib(vert, geom, frag, lib, defines) \
240   DRW_shader_create_with_shaderlib_ex(vert, geom, frag, lib, defines, __func__)
241 #define DRW_shader_create_fullscreen(frag, defines) \
242   DRW_shader_create_fullscreen_ex(frag, defines, __func__)
243 #define DRW_shader_create_fullscreen_with_shaderlib(frag, lib, defines) \
244   DRW_shader_create_fullscreen_with_shaderlib_ex(frag, lib, defines, __func__)
245 
246 struct GPUMaterial *DRW_shader_find_from_world(struct World *wo,
247                                                const void *engine_type,
248                                                const int options,
249                                                bool deferred);
250 struct GPUMaterial *DRW_shader_find_from_material(struct Material *ma,
251                                                   const void *engine_type,
252                                                   const int options,
253                                                   bool deferred);
254 struct GPUMaterial *DRW_shader_create_from_world(struct Scene *scene,
255                                                  struct World *wo,
256                                                  struct bNodeTree *ntree,
257                                                  const void *engine_type,
258                                                  const int options,
259                                                  const bool is_volume_shader,
260                                                  const char *vert,
261                                                  const char *geom,
262                                                  const char *frag_lib,
263                                                  const char *defines,
264                                                  bool deferred,
265                                                  GPUMaterialEvalCallbackFn callback);
266 struct GPUMaterial *DRW_shader_create_from_material(struct Scene *scene,
267                                                     struct Material *ma,
268                                                     struct bNodeTree *ntree,
269                                                     const void *engine_type,
270                                                     const int options,
271                                                     const bool is_volume_shader,
272                                                     const char *vert,
273                                                     const char *geom,
274                                                     const char *frag_lib,
275                                                     const char *defines,
276                                                     bool deferred,
277                                                     GPUMaterialEvalCallbackFn callback);
278 void DRW_shader_free(struct GPUShader *shader);
279 #define DRW_SHADER_FREE_SAFE(shader) \
280   do { \
281     if (shader != NULL) { \
282       DRW_shader_free(shader); \
283       shader = NULL; \
284     } \
285   } while (0)
286 
287 DRWShaderLibrary *DRW_shader_library_create(void);
288 
289 /* Warning: Each library must be added after all its dependencies. */
290 void DRW_shader_library_add_file(DRWShaderLibrary *lib, char *lib_code, const char *lib_name);
291 #define DRW_SHADER_LIB_ADD(lib, lib_name) \
292   DRW_shader_library_add_file(lib, datatoc_##lib_name##_glsl, STRINGIFY(lib_name) ".glsl")
293 
294 char *DRW_shader_library_create_shader_string(const DRWShaderLibrary *lib,
295                                               const char *shader_code);
296 
297 void DRW_shader_library_free(DRWShaderLibrary *lib);
298 #define DRW_SHADER_LIB_FREE_SAFE(lib) \
299   do { \
300     if (lib != NULL) { \
301       DRW_shader_library_free(lib); \
302       lib = NULL; \
303     } \
304   } while (0)
305 
306 /* Batches */
307 /* DRWState is a bitmask that stores the current render state and the desired render state. Based
308  * on the differences the minimum state changes can be invoked to setup the desired render state.
309  *
310  * The Write Stencil, Stencil test, Depth test and Blend state options are mutual exclusive
311  * therefore they aren't ordered as a bit mask.*/
312 typedef enum {
313   /** Write mask */
314   DRW_STATE_WRITE_DEPTH = (1 << 0),
315   DRW_STATE_WRITE_COLOR = (1 << 1),
316   /* Write Stencil. These options are mutual exclusive and packed into 2 bits */
317   DRW_STATE_WRITE_STENCIL = (1 << 2),
318   DRW_STATE_WRITE_STENCIL_SHADOW_PASS = (2 << 2),
319   DRW_STATE_WRITE_STENCIL_SHADOW_FAIL = (3 << 2),
320   /** Depth test. These options are mutual exclusive and packed into 3 bits */
321   DRW_STATE_DEPTH_ALWAYS = (1 << 4),
322   DRW_STATE_DEPTH_LESS = (2 << 4),
323   DRW_STATE_DEPTH_LESS_EQUAL = (3 << 4),
324   DRW_STATE_DEPTH_EQUAL = (4 << 4),
325   DRW_STATE_DEPTH_GREATER = (5 << 4),
326   DRW_STATE_DEPTH_GREATER_EQUAL = (6 << 4),
327   /** Culling test */
328   DRW_STATE_CULL_BACK = (1 << 7),
329   DRW_STATE_CULL_FRONT = (1 << 8),
330   /** Stencil test . These options are mutal exclusive and packed into 2 bits*/
331   DRW_STATE_STENCIL_ALWAYS = (1 << 9),
332   DRW_STATE_STENCIL_EQUAL = (2 << 9),
333   DRW_STATE_STENCIL_NEQUAL = (3 << 9),
334 
335   /** Blend state. These options are mutual exclusive and packed into 4 bits */
336   DRW_STATE_BLEND_ADD = (1 << 11),
337   /** Same as additive but let alpha accumulate without premult. */
338   DRW_STATE_BLEND_ADD_FULL = (2 << 11),
339   /** Standard alpha blending. */
340   DRW_STATE_BLEND_ALPHA = (3 << 11),
341   /** Use that if color is already premult by alpha. */
342   DRW_STATE_BLEND_ALPHA_PREMUL = (4 << 11),
343   DRW_STATE_BLEND_BACKGROUND = (5 << 11),
344   DRW_STATE_BLEND_OIT = (6 << 11),
345   DRW_STATE_BLEND_MUL = (7 << 11),
346   DRW_STATE_BLEND_SUB = (8 << 11),
347   /** Use dual source blending. WARNING: Only one color buffer allowed. */
348   DRW_STATE_BLEND_CUSTOM = (9 << 11),
349   DRW_STATE_LOGIC_INVERT = (10 << 11),
350   DRW_STATE_BLEND_ALPHA_UNDER_PREMUL = (11 << 11),
351 
352 
353   DRW_STATE_IN_FRONT_SELECT = (1 << 27),
354   DRW_STATE_SHADOW_OFFSET = (1 << 28),
355   DRW_STATE_CLIP_PLANES = (1 << 29),
356   DRW_STATE_FIRST_VERTEX_CONVENTION = (1 << 30),
357   /** DO NOT USE. Assumed always enabled. Only used internally. */
358   DRW_STATE_PROGRAM_POINT_SIZE = (1u << 31),
359 } DRWState;
360 
361 #define DRW_STATE_DEFAULT \
362   (DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL)
363 #define DRW_STATE_BLEND_ENABLED \
364   (DRW_STATE_BLEND_ADD | DRW_STATE_BLEND_ADD_FULL | DRW_STATE_BLEND_ALPHA | \
365    DRW_STATE_BLEND_ALPHA_PREMUL | DRW_STATE_BLEND_BACKGROUND | DRW_STATE_BLEND_OIT | \
366    DRW_STATE_BLEND_MUL | DRW_STATE_BLEND_SUB | DRW_STATE_BLEND_CUSTOM | DRW_STATE_LOGIC_INVERT)
367 #define DRW_STATE_RASTERIZER_ENABLED \
368   (DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_STENCIL | \
369    DRW_STATE_WRITE_STENCIL_SHADOW_PASS | DRW_STATE_WRITE_STENCIL_SHADOW_FAIL)
370 #define DRW_STATE_DEPTH_TEST_ENABLED \
371   (DRW_STATE_DEPTH_ALWAYS | DRW_STATE_DEPTH_LESS | DRW_STATE_DEPTH_LESS_EQUAL | \
372    DRW_STATE_DEPTH_EQUAL | DRW_STATE_DEPTH_GREATER | DRW_STATE_DEPTH_GREATER_EQUAL)
373 #define DRW_STATE_STENCIL_TEST_ENABLED \
374   (DRW_STATE_STENCIL_ALWAYS | DRW_STATE_STENCIL_EQUAL | DRW_STATE_STENCIL_NEQUAL)
375 #define DRW_STATE_WRITE_STENCIL_ENABLED \
376   (DRW_STATE_WRITE_STENCIL | DRW_STATE_WRITE_STENCIL_SHADOW_PASS | \
377    DRW_STATE_WRITE_STENCIL_SHADOW_FAIL)
378 
379 typedef enum {
380   DRW_ATTR_INT,
381   DRW_ATTR_FLOAT,
382 } eDRWAttrType;
383 
384 typedef struct DRWInstanceAttrFormat {
385   char name[32];
386   eDRWAttrType type;
387   int components;
388 } DRWInstanceAttrFormat;
389 
390 struct GPUVertFormat *DRW_shgroup_instance_format_array(const DRWInstanceAttrFormat attrs[],
391                                                         int arraysize);
392 #define DRW_shgroup_instance_format(format, ...) \
393   do { \
394     if (format == NULL) { \
395       DRWInstanceAttrFormat drw_format[] = __VA_ARGS__; \
396       format = DRW_shgroup_instance_format_array( \
397           drw_format, (sizeof(drw_format) / sizeof(DRWInstanceAttrFormat))); \
398     } \
399   } while (0)
400 
401 DRWShadingGroup *DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass);
402 DRWShadingGroup *DRW_shgroup_create_sub(DRWShadingGroup *shgroup);
403 DRWShadingGroup *DRW_shgroup_material_create(struct GPUMaterial *material, DRWPass *pass);
404 DRWShadingGroup *DRW_shgroup_transform_feedback_create(struct GPUShader *shader,
405                                                        DRWPass *pass,
406                                                        struct GPUVertBuf *tf_target);
407 
408 void DRW_shgroup_add_material_resources(DRWShadingGroup *grp, struct GPUMaterial *material);
409 
410 /* return final visibility */
411 typedef bool(DRWCallVisibilityFn)(bool vis_in, void *user_data);
412 
413 void DRW_shgroup_call_ex(DRWShadingGroup *shgroup,
414                          Object *ob,
415                          float (*obmat)[4],
416                          struct GPUBatch *geom,
417                          bool bypass_culling,
418                          void *user_data);
419 
420 /* If ob is NULL, unit modelmatrix is assumed and culling is bypassed. */
421 #define DRW_shgroup_call(shgroup, geom, ob) \
422   DRW_shgroup_call_ex(shgroup, ob, NULL, geom, false, NULL)
423 
424 /* Same as DRW_shgroup_call but override the obmat. Not culled. */
425 #define DRW_shgroup_call_obmat(shgroup, geom, obmat) \
426   DRW_shgroup_call_ex(shgroup, NULL, obmat, geom, false, NULL)
427 
428 /* TODO(fclem): remove this when we have DRWView */
429 /* user_data is used by DRWCallVisibilityFn defined in DRWView. */
430 #define DRW_shgroup_call_with_callback(shgroup, geom, ob, user_data) \
431   DRW_shgroup_call_ex(shgroup, ob, NULL, geom, false, user_data)
432 
433 /* Same as DRW_shgroup_call but bypass culling even if ob is not NULL. */
434 #define DRW_shgroup_call_no_cull(shgroup, geom, ob) \
435   DRW_shgroup_call_ex(shgroup, ob, NULL, geom, true, NULL)
436 
437 void DRW_shgroup_call_range(
438     DRWShadingGroup *shgroup, Object *ob, struct GPUBatch *geom, uint v_sta, uint v_ct);
439 void DRW_shgroup_call_instance_range(
440     DRWShadingGroup *shgroup, Object *ob, struct GPUBatch *geom, uint i_sta, uint i_ct);
441 
442 void DRW_shgroup_call_procedural_points(DRWShadingGroup *sh, Object *ob, uint point_count);
443 void DRW_shgroup_call_procedural_lines(DRWShadingGroup *sh, Object *ob, uint line_count);
444 void DRW_shgroup_call_procedural_triangles(DRWShadingGroup *sh, Object *ob, uint tri_count);
445 /* Warning: Only use with Shaders that have IN_PLACE_INSTANCES defined. */
446 void DRW_shgroup_call_instances(DRWShadingGroup *shgroup,
447                                 Object *ob,
448                                 struct GPUBatch *geom,
449                                 uint count);
450 /* Warning: Only use with Shaders that have INSTANCED_ATTR defined. */
451 void DRW_shgroup_call_instances_with_attrs(DRWShadingGroup *shgroup,
452                                            Object *ob,
453                                            struct GPUBatch *geom,
454                                            struct GPUBatch *inst_attributes);
455 
456 void DRW_shgroup_call_sculpt(DRWShadingGroup *sh, Object *ob, bool wire, bool mask);
457 void DRW_shgroup_call_sculpt_with_materials(DRWShadingGroup **sh, int num_sh, Object *ob);
458 
459 DRWCallBuffer *DRW_shgroup_call_buffer(DRWShadingGroup *shgroup,
460                                        struct GPUVertFormat *format,
461                                        GPUPrimType prim_type);
462 DRWCallBuffer *DRW_shgroup_call_buffer_instance(DRWShadingGroup *shgroup,
463                                                 struct GPUVertFormat *format,
464                                                 struct GPUBatch *geom);
465 
466 void DRW_buffer_add_entry_struct(DRWCallBuffer *callbuf, const void *data);
467 void DRW_buffer_add_entry_array(DRWCallBuffer *callbuf, const void *attr[], uint attr_len);
468 
469 #define DRW_buffer_add_entry(buffer, ...) \
470   do { \
471     const void *array[] = {__VA_ARGS__}; \
472     DRW_buffer_add_entry_array(buffer, array, (sizeof(array) / sizeof(*array))); \
473   } while (0)
474 
475 /* Can only be called during iter phase. */
476 uint32_t DRW_object_resource_id_get(Object *UNUSED(ob));
477 
478 void DRW_shgroup_state_enable(DRWShadingGroup *shgroup, DRWState state);
479 void DRW_shgroup_state_disable(DRWShadingGroup *shgroup, DRWState state);
480 
481 /* Reminders:
482  * - (compare_mask & reference) is what is tested against (compare_mask & stencil_value)
483  *   stencil_value being the value stored in the stencil buffer.
484  * - (write-mask & reference) is what gets written if the test condition is fulfilled.
485  **/
486 void DRW_shgroup_stencil_set(DRWShadingGroup *shgroup,
487                              uint write_mask,
488                              uint reference,
489                              uint compare_mask);
490 /* TODO remove this function. Obsolete version. mask is actually reference value. */
491 void DRW_shgroup_stencil_mask(DRWShadingGroup *shgroup, uint mask);
492 
493 /* Issue a clear command. */
494 void DRW_shgroup_clear_framebuffer(DRWShadingGroup *shgroup,
495                                    eGPUFrameBufferBits channels,
496                                    uchar r,
497                                    uchar g,
498                                    uchar b,
499                                    uchar a,
500                                    float depth,
501                                    uchar stencil);
502 
503 void DRW_shgroup_uniform_texture_ex(DRWShadingGroup *shgroup,
504                                     const char *name,
505                                     const struct GPUTexture *tex,
506                                     eGPUSamplerState sampler_state);
507 void DRW_shgroup_uniform_texture_ref_ex(DRWShadingGroup *shgroup,
508                                         const char *name,
509                                         GPUTexture **tex,
510                                         eGPUSamplerState sampler_state);
511 void DRW_shgroup_uniform_texture(DRWShadingGroup *shgroup,
512                                  const char *name,
513                                  const struct GPUTexture *tex);
514 void DRW_shgroup_uniform_texture_ref(DRWShadingGroup *shgroup,
515                                      const char *name,
516                                      struct GPUTexture **tex);
517 void DRW_shgroup_uniform_block(DRWShadingGroup *shgroup,
518                                const char *name,
519                                const struct GPUUniformBuf *ubo);
520 void DRW_shgroup_uniform_block_ref(DRWShadingGroup *shgroup,
521                                    const char *name,
522                                    struct GPUUniformBuf **ubo);
523 void DRW_shgroup_uniform_float(DRWShadingGroup *shgroup,
524                                const char *name,
525                                const float *value,
526                                int arraysize);
527 void DRW_shgroup_uniform_vec2(DRWShadingGroup *shgroup,
528                               const char *name,
529                               const float *value,
530                               int arraysize);
531 void DRW_shgroup_uniform_vec3(DRWShadingGroup *shgroup,
532                               const char *name,
533                               const float *value,
534                               int arraysize);
535 void DRW_shgroup_uniform_vec4(DRWShadingGroup *shgroup,
536                               const char *name,
537                               const float *value,
538                               int arraysize);
539 /* Boolean are expected to be 4bytes longs for opengl! */
540 void DRW_shgroup_uniform_bool(DRWShadingGroup *shgroup,
541                               const char *name,
542                               const int *value,
543                               int arraysize);
544 void DRW_shgroup_uniform_int(DRWShadingGroup *shgroup,
545                              const char *name,
546                              const int *value,
547                              int arraysize);
548 void DRW_shgroup_uniform_ivec2(DRWShadingGroup *shgroup,
549                                const char *name,
550                                const int *value,
551                                int arraysize);
552 void DRW_shgroup_uniform_ivec3(DRWShadingGroup *shgroup,
553                                const char *name,
554                                const int *value,
555                                int arraysize);
556 void DRW_shgroup_uniform_ivec4(DRWShadingGroup *shgroup,
557                                const char *name,
558                                const int *value,
559                                int arraysize);
560 void DRW_shgroup_uniform_mat3(DRWShadingGroup *shgroup, const char *name, const float (*value)[3]);
561 void DRW_shgroup_uniform_mat4(DRWShadingGroup *shgroup, const char *name, const float (*value)[4]);
562 /* Only to be used when image load store is supported (GPU_shader_image_load_store_support()). */
563 void DRW_shgroup_uniform_image(DRWShadingGroup *shgroup, const char *name, const GPUTexture *tex);
564 void DRW_shgroup_uniform_image_ref(DRWShadingGroup *shgroup, const char *name, GPUTexture **tex);
565 /* Store value instead of referencing it. */
566 void DRW_shgroup_uniform_int_copy(DRWShadingGroup *shgroup, const char *name, const int value);
567 void DRW_shgroup_uniform_ivec2_copy(DRWShadingGroup *shgroup, const char *name, const int *value);
568 void DRW_shgroup_uniform_ivec3_copy(DRWShadingGroup *shgroup, const char *name, const int *value);
569 void DRW_shgroup_uniform_ivec4_copy(DRWShadingGroup *shgroup, const char *name, const int *value);
570 void DRW_shgroup_uniform_bool_copy(DRWShadingGroup *shgroup, const char *name, const bool value);
571 void DRW_shgroup_uniform_float_copy(DRWShadingGroup *shgroup, const char *name, const float value);
572 void DRW_shgroup_uniform_vec2_copy(DRWShadingGroup *shgroup, const char *name, const float *value);
573 void DRW_shgroup_uniform_vec3_copy(DRWShadingGroup *shgroup, const char *name, const float *value);
574 void DRW_shgroup_uniform_vec4_copy(DRWShadingGroup *shgroup, const char *name, const float *value);
575 void DRW_shgroup_uniform_vec4_array_copy(DRWShadingGroup *shgroup,
576                                          const char *name,
577                                          const float (*value)[4],
578                                          int arraysize);
579 
580 bool DRW_shgroup_is_empty(DRWShadingGroup *shgroup);
581 
582 /* Passes */
583 DRWPass *DRW_pass_create(const char *name, DRWState state);
584 DRWPass *DRW_pass_create_instance(const char *name, DRWPass *original, DRWState state);
585 void DRW_pass_link(DRWPass *first, DRWPass *second);
586 void DRW_pass_foreach_shgroup(DRWPass *pass,
587                               void (*callback)(void *userData, DRWShadingGroup *shgroup),
588                               void *userData);
589 void DRW_pass_sort_shgroup_z(DRWPass *pass);
590 void DRW_pass_sort_shgroup_reverse(DRWPass *pass);
591 
592 bool DRW_pass_is_empty(DRWPass *pass);
593 
594 #define DRW_PASS_CREATE(pass, state) (pass = DRW_pass_create(#pass, state))
595 #define DRW_PASS_INSTANCE_CREATE(pass, original, state) \
596   (pass = DRW_pass_create_instance(#pass, (original), state))
597 
598 /* Views */
599 DRWView *DRW_view_create(const float viewmat[4][4],
600                          const float winmat[4][4],
601                          const float (*culling_viewmat)[4],
602                          const float (*culling_winmat)[4],
603                          DRWCallVisibilityFn *visibility_fn);
604 DRWView *DRW_view_create_sub(const DRWView *parent_view,
605                              const float viewmat[4][4],
606                              const float winmat[4][4]);
607 
608 void DRW_view_update(DRWView *view,
609                      const float viewmat[4][4],
610                      const float winmat[4][4],
611                      const float (*culling_viewmat)[4],
612                      const float (*culling_winmat)[4]);
613 void DRW_view_update_sub(DRWView *view, const float viewmat[4][4], const float winmat[4][4]);
614 
615 const DRWView *DRW_view_default_get(void);
616 void DRW_view_default_set(DRWView *view);
617 void DRW_view_reset(void);
618 void DRW_view_set_active(DRWView *view);
619 
620 void DRW_view_clip_planes_set(DRWView *view, float (*planes)[4], int plane_len);
621 void DRW_view_camtexco_set(DRWView *view, float texco[4]);
622 
623 /* For all getters, if view is NULL, default view is assumed. */
624 void DRW_view_winmat_get(const DRWView *view, float mat[4][4], bool inverse);
625 void DRW_view_viewmat_get(const DRWView *view, float mat[4][4], bool inverse);
626 void DRW_view_persmat_get(const DRWView *view, float mat[4][4], bool inverse);
627 
628 void DRW_view_frustum_corners_get(const DRWView *view, BoundBox *corners);
629 void DRW_view_frustum_planes_get(const DRWView *view, float planes[6][4]);
630 
631 /* These are in view-space, so negative if in perspective.
632  * Extract near and far clip distance from the projection matrix. */
633 float DRW_view_near_distance_get(const DRWView *view);
634 float DRW_view_far_distance_get(const DRWView *view);
635 bool DRW_view_is_persp_get(const DRWView *view);
636 
637 /* Culling, return true if object is inside view frustum. */
638 bool DRW_culling_sphere_test(const DRWView *view, const BoundSphere *bsphere);
639 bool DRW_culling_box_test(const DRWView *view, const BoundBox *bbox);
640 bool DRW_culling_plane_test(const DRWView *view, const float plane[4]);
641 bool DRW_culling_min_max_test(const DRWView *view, float obmat[4][4], float min[3], float max[3]);
642 
643 void DRW_culling_frustum_corners_get(const DRWView *view, BoundBox *corners);
644 void DRW_culling_frustum_planes_get(const DRWView *view, float planes[6][4]);
645 
646 /* Viewport */
647 
648 const float *DRW_viewport_size_get(void);
649 const float *DRW_viewport_invert_size_get(void);
650 const float *DRW_viewport_screenvecs_get(void);
651 const float *DRW_viewport_pixelsize_get(void);
652 
653 struct DefaultFramebufferList *DRW_viewport_framebuffer_list_get(void);
654 struct DefaultTextureList *DRW_viewport_texture_list_get(void);
655 
656 void DRW_viewport_request_redraw(void);
657 
658 void DRW_render_to_image(struct RenderEngine *engine, struct Depsgraph *depsgraph);
659 void DRW_render_object_iter(void *vedata,
660                             struct RenderEngine *engine,
661                             struct Depsgraph *depsgraph,
662                             void (*callback)(void *vedata,
663                                              struct Object *ob,
664                                              struct RenderEngine *engine,
665                                              struct Depsgraph *depsgraph));
666 void DRW_render_instance_buffer_finish(void);
667 void DRW_render_set_time(struct RenderEngine *engine,
668                          struct Depsgraph *depsgraph,
669                          int frame,
670                          float subframe);
671 void DRW_render_viewport_size_set(const int size[2]);
672 
673 void DRW_custom_pipeline(DrawEngineType *draw_engine_type,
674                          struct Depsgraph *depsgraph,
675                          void (*callback)(void *vedata, void *user_data),
676                          void *user_data);
677 
678 void DRW_cache_restart(void);
679 
680 /* ViewLayers */
681 void *DRW_view_layer_engine_data_get(DrawEngineType *engine_type);
682 void **DRW_view_layer_engine_data_ensure_ex(struct ViewLayer *view_layer,
683                                             DrawEngineType *engine_type,
684                                             void (*callback)(void *storage));
685 void **DRW_view_layer_engine_data_ensure(DrawEngineType *engine_type,
686                                          void (*callback)(void *storage));
687 
688 /* DrawData */
689 DrawData *DRW_drawdata_get(ID *id, DrawEngineType *engine_type);
690 DrawData *DRW_drawdata_ensure(ID *id,
691                               DrawEngineType *engine_type,
692                               size_t size,
693                               DrawDataInitCb init_cb,
694                               DrawDataFreeCb free_cb);
695 void **DRW_duplidata_get(void *vedata);
696 
697 /* Settings */
698 bool DRW_object_is_renderable(const struct Object *ob);
699 bool DRW_object_is_in_edit_mode(const struct Object *ob);
700 int DRW_object_visibility_in_active_context(const struct Object *ob);
701 bool DRW_object_is_flat_normal(const struct Object *ob);
702 bool DRW_object_use_hide_faces(const struct Object *ob);
703 
704 bool DRW_object_is_visible_psys_in_active_context(const struct Object *object,
705                                                   const struct ParticleSystem *psys);
706 
707 struct Object *DRW_object_get_dupli_parent(const struct Object *ob);
708 struct DupliObject *DRW_object_get_dupli(const struct Object *ob);
709 
710 /* Draw commands */
711 void DRW_draw_pass(DRWPass *pass);
712 void DRW_draw_pass_subset(DRWPass *pass, DRWShadingGroup *start_group, DRWShadingGroup *end_group);
713 
714 void DRW_draw_callbacks_pre_scene(void);
715 void DRW_draw_callbacks_post_scene(void);
716 
717 void DRW_state_reset_ex(DRWState state);
718 void DRW_state_reset(void);
719 void DRW_state_lock(DRWState state);
720 
721 /* Selection */
722 void DRW_select_load_id(uint id);
723 
724 /* Draw State */
725 bool DRW_state_is_fbo(void);
726 bool DRW_state_is_select(void);
727 bool DRW_state_is_depth(void);
728 bool DRW_state_is_image_render(void);
729 bool DRW_state_do_color_management(void);
730 bool DRW_state_is_scene_render(void);
731 bool DRW_state_is_opengl_render(void);
732 bool DRW_state_is_playback(void);
733 bool DRW_state_is_navigating(void);
734 bool DRW_state_show_text(void);
735 bool DRW_state_draw_support(void);
736 bool DRW_state_draw_background(void);
737 
738 /* Avoid too many lookups while drawing */
739 typedef struct DRWContextState {
740 
741   struct ARegion *region;       /* 'CTX_wm_region(C)' */
742   struct RegionView3D *rv3d;    /* 'CTX_wm_region_view3d(C)' */
743   struct View3D *v3d;           /* 'CTX_wm_view3d(C)' */
744   struct SpaceLink *space_data; /* 'CTX_wm_space_data(C)' */
745 
746   struct Scene *scene;          /* 'CTX_data_scene(C)' */
747   struct ViewLayer *view_layer; /* 'CTX_data_view_layer(C)' */
748 
749   /* Use 'object_edit' for edit-mode */
750   struct Object *obact; /* 'OBACT' */
751 
752   struct RenderEngineType *engine_type;
753 
754   struct Depsgraph *depsgraph;
755 
756   struct TaskGraph *task_graph;
757 
758   eObjectMode object_mode;
759 
760   eGPUShaderConfig sh_cfg;
761 
762   /** Last resort (some functions take this as an arg so we can't easily avoid).
763    * May be NULL when used for selection or depth buffer. */
764   const struct bContext *evil_C;
765 
766   /* ---- */
767 
768   /* Cache: initialized by 'drw_context_state_init'. */
769   struct Object *object_pose;
770   struct Object *object_edit;
771 
772 } DRWContextState;
773 
774 const DRWContextState *DRW_context_state_get(void);
775