update vg
[carveJwlIkooP6JGAAIwe30JlM.git] / render.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #include "common.h"
6 #include "model.h"
7 #include "camera.h"
8 #include "world.h"
9
10 #include "shaders/blit.h"
11 #include "shaders/blitblur.h"
12 #include "shaders/blitcolour.h"
13
14 #if 0
15 #include "shaders/standard.h"
16 #include "shaders/vblend.h"
17 #endif
18
19 VG_STATIC void render_water_texture( world_instance *world, camera *cam,
20 int layer_depth );
21 VG_STATIC void render_water_surface( world_instance *world, camera *cam );
22 VG_STATIC void render_world( world_instance *world, camera *cam,
23 int layer_depth );
24 VG_STATIC void render_world_depth( world_instance *world, camera *cam );
25
26 #ifndef RENDER_H
27 #define RENDER_H
28
29 typedef struct framebuffer framebuffer;
30
31 /*
32 * All standard buffers used in rendering
33 */
34 VG_STATIC struct pipeline{
35 glmesh fsquad;
36
37 framebuffer *fb_main,
38 *fb_water_reflection,
39 *fb_water_beneath;
40 int ready;
41
42 float view_render_scale,
43 water_render_scale;
44 }
45 gpipeline = { .view_render_scale = 1.0f };
46
47 struct framebuffer{
48 const char *display_name;
49 int resolution_div, /* definition */
50 fixed_w,
51 fixed_h,
52
53 render_w, /* runtime */
54 render_h;
55
56 struct framebuffer_attachment{
57 const char *display_name;
58
59 enum framebuffer_attachment_type{
60 k_framebuffer_attachment_type_none,
61 k_framebuffer_attachment_type_texture,
62 k_framebuffer_attachment_type_renderbuffer,
63 k_framebuffer_attachment_type_texture_depth
64 }
65 purpose;
66
67 enum framebuffer_quality_profile{
68 k_framebuffer_quality_all,
69 k_framebuffer_quality_high_only
70 }
71 quality;
72
73 GLenum internalformat,
74 format,
75 type,
76 attachment;
77
78 GLuint id;
79
80 /* Runtime */
81 int debug_view;
82 }
83 attachments[5];
84 GLuint fb;
85 framebuffer **link;
86 }
87 framebuffers[] =
88 {
89 {
90 /*
91 * The primary draw target
92 */
93 "main",
94 .link = &gpipeline.fb_main,
95 .resolution_div = 1,
96 .attachments =
97 {
98 {
99 "colour", k_framebuffer_attachment_type_texture,
100
101 .internalformat = GL_RGB,
102 .format = GL_RGB,
103 .type = GL_UNSIGNED_BYTE,
104 .attachment = GL_COLOR_ATTACHMENT0
105 },
106 {
107 "motion", k_framebuffer_attachment_type_texture,
108
109 .quality = k_framebuffer_quality_high_only,
110 .internalformat = GL_RG16F,
111 .format = GL_RG,
112 .type = GL_FLOAT,
113 .attachment = GL_COLOR_ATTACHMENT1
114 },
115 {
116 #if 0
117 "depth_stencil", k_framebuffer_attachment_type_renderbuffer,
118
119 .internalformat = GL_DEPTH24_STENCIL8,
120 #else
121 "depth_stencil", k_framebuffer_attachment_type_texture_depth,
122 .internalformat = GL_DEPTH24_STENCIL8,
123 .format = GL_DEPTH_STENCIL,
124 .type = GL_UNSIGNED_INT_24_8,
125 #endif
126 .attachment = GL_DEPTH_STENCIL_ATTACHMENT
127 }
128 }
129 },
130 {
131 /*
132 * Second rendered view from the perspective of the water reflection
133 */
134 "water_reflection",
135 .link = &gpipeline.fb_water_reflection,
136 .resolution_div = 2,
137 .attachments =
138 {
139 {
140 "colour", k_framebuffer_attachment_type_texture,
141 .internalformat = GL_RGB,
142 .format = GL_RGB,
143 .type = GL_UNSIGNED_BYTE,
144 .attachment = GL_COLOR_ATTACHMENT0
145 },
146 {
147 "depth_stencil", k_framebuffer_attachment_type_renderbuffer,
148
149 .internalformat = GL_DEPTH24_STENCIL8,
150 .attachment = GL_DEPTH_STENCIL_ATTACHMENT
151 }
152 }
153 },
154 {
155 /*
156 * Thid rendered view from the perspective of the camera, but just
157 * captures stuff thats under the water
158 */
159 "water_beneath",
160 .link = &gpipeline.fb_water_beneath,
161 .resolution_div = 2,
162 .attachments =
163 {
164 {
165 "colour", k_framebuffer_attachment_type_texture,
166 .internalformat = GL_RED,
167 .format = GL_RED,
168 .type = GL_UNSIGNED_BYTE,
169 .attachment = GL_COLOR_ATTACHMENT0
170 },
171 {
172 "depth_stencil", k_framebuffer_attachment_type_renderbuffer,
173
174 .internalformat = GL_DEPTH24_STENCIL8,
175 .attachment = GL_DEPTH_STENCIL_ATTACHMENT
176 }
177 }
178 }
179 };
180
181 /*
182 * Get the current (automatically scaled or fixed) resolution of framebuffer
183 */
184 VG_STATIC void render_fb_get_current_res( struct framebuffer *fb,
185 int *x, int *y )
186 {
187 if( fb->resolution_div ){
188 *x = vg.window_x / fb->resolution_div;
189 *y = vg.window_y / fb->resolution_div;
190 }
191 else{
192 *x = fb->fixed_w;
193 *y = fb->fixed_h;
194 }
195 }
196
197 VG_STATIC void render_fb_inverse_ratio( framebuffer *fb, v2f inverse )
198 {
199 if( fb ){
200 int x, y;
201 render_fb_get_current_res( fb, &x, &y );
202
203 v2f render = { fb->render_w, fb->render_h },
204 original = { x, y };
205
206 v2_div( render, original, inverse );
207 }
208 else{
209 v2_div( (v2f){1.0f,1.0f}, (v2f){ vg.window_x, vg.window_y }, inverse );
210 }
211 }
212
213 /*
214 * Bind framebuffer for drawing to
215 */
216 VG_STATIC void render_fb_bind( framebuffer *fb, int use_scaling )
217 {
218 int x, y;
219 render_fb_get_current_res( fb, &x, &y );
220
221 if( use_scaling ){
222 x = gpipeline.view_render_scale*(float)x;
223 y = gpipeline.view_render_scale*(float)y;
224
225 x = VG_MAX( 16, x );
226 y = VG_MAX( 16, y );
227
228 fb->render_w = x;
229 fb->render_h = y;
230 }
231
232 glBindFramebuffer( GL_FRAMEBUFFER, fb->fb );
233 glViewport( 0, 0, x, y );
234 }
235
236 /*
237 * Bind framebuffer attachment's texture
238 */
239 VG_STATIC void render_fb_bind_texture( framebuffer *fb,
240 int attachment, int slot )
241 {
242 struct framebuffer_attachment *at = &fb->attachments[attachment];
243
244 if( (at->purpose != k_framebuffer_attachment_type_texture) &&
245 (at->purpose != k_framebuffer_attachment_type_texture_depth) )
246 {
247 vg_fatal_exit_loop( "illegal operation: bind non-texture framebuffer"
248 " attachment to texture slot" );
249 }
250
251 glActiveTexture( GL_TEXTURE0 + slot );
252 glBindTexture( GL_TEXTURE_2D, fb->attachments[attachment].id );
253 }
254
255
256 /*
257 * Shaders
258 */
259
260 #define FB_FORMAT_STR( E ) { E, #E },
261
262 /*
263 * Convert OpenGL attachment ID enum to string
264 */
265 VG_STATIC const char *render_fb_attachment_str( GLenum e )
266 {
267 struct { GLenum e; const char *str; }
268 formats[] =
269 {
270 FB_FORMAT_STR(GL_COLOR_ATTACHMENT0)
271 FB_FORMAT_STR(GL_COLOR_ATTACHMENT1)
272 FB_FORMAT_STR(GL_COLOR_ATTACHMENT2)
273 FB_FORMAT_STR(GL_COLOR_ATTACHMENT3)
274 FB_FORMAT_STR(GL_COLOR_ATTACHMENT4)
275 FB_FORMAT_STR(GL_DEPTH_STENCIL_ATTACHMENT)
276 };
277
278 for( int i=0; i<vg_list_size(formats); i++ )
279 if( formats[i].e == e )
280 return formats[i].str;
281
282 return "UNDEFINED";
283 }
284
285 /*
286 * Convert OpenGL texture format enums from TexImage2D table 1,2 &
287 * RenderBufferStorage Table 1, into strings
288 */
289 VG_STATIC const char *render_fb_format_str( GLenum format )
290 {
291 struct { GLenum e; const char *str; }
292 formats[] =
293 {
294 /* Table 1 */
295 FB_FORMAT_STR(GL_DEPTH_COMPONENT)
296 FB_FORMAT_STR(GL_DEPTH_STENCIL)
297 FB_FORMAT_STR(GL_RED)
298 FB_FORMAT_STR(GL_RG)
299 FB_FORMAT_STR(GL_RGB)
300 FB_FORMAT_STR(GL_RGBA)
301
302 /* Render buffer formats */
303 FB_FORMAT_STR(GL_DEPTH_COMPONENT16)
304 FB_FORMAT_STR(GL_DEPTH_COMPONENT24)
305 FB_FORMAT_STR(GL_DEPTH_COMPONENT32F)
306 FB_FORMAT_STR(GL_DEPTH24_STENCIL8)
307 FB_FORMAT_STR(GL_DEPTH32F_STENCIL8)
308 FB_FORMAT_STR(GL_STENCIL_INDEX8)
309
310 /* Table 2 */
311 FB_FORMAT_STR(GL_R8)
312 FB_FORMAT_STR(GL_R8_SNORM)
313 FB_FORMAT_STR(GL_R16)
314 FB_FORMAT_STR(GL_R16_SNORM)
315 FB_FORMAT_STR(GL_RG8)
316 FB_FORMAT_STR(GL_RG8_SNORM)
317 FB_FORMAT_STR(GL_RG16)
318 FB_FORMAT_STR(GL_RG16_SNORM)
319 FB_FORMAT_STR(GL_R3_G3_B2)
320 FB_FORMAT_STR(GL_RGB4)
321 FB_FORMAT_STR(GL_RGB5)
322 FB_FORMAT_STR(GL_RGB8)
323 FB_FORMAT_STR(GL_RGB8_SNORM)
324 FB_FORMAT_STR(GL_RGB10)
325 FB_FORMAT_STR(GL_RGB12)
326 FB_FORMAT_STR(GL_RGB16_SNORM)
327 FB_FORMAT_STR(GL_RGBA2)
328 FB_FORMAT_STR(GL_RGBA4)
329 FB_FORMAT_STR(GL_RGB5_A1)
330 FB_FORMAT_STR(GL_RGBA8)
331 FB_FORMAT_STR(GL_RGBA8_SNORM)
332 FB_FORMAT_STR(GL_RGB10_A2)
333 FB_FORMAT_STR(GL_RGB10_A2UI)
334 FB_FORMAT_STR(GL_RGBA12)
335 FB_FORMAT_STR(GL_RGBA16)
336 FB_FORMAT_STR(GL_SRGB8)
337 FB_FORMAT_STR(GL_SRGB8_ALPHA8)
338 FB_FORMAT_STR(GL_R16F)
339 FB_FORMAT_STR(GL_RG16F)
340 FB_FORMAT_STR(GL_RGB16F)
341 FB_FORMAT_STR(GL_RGBA16F)
342 FB_FORMAT_STR(GL_R32F)
343 FB_FORMAT_STR(GL_RG32F)
344 FB_FORMAT_STR(GL_RGB32F)
345 FB_FORMAT_STR(GL_RGBA32F)
346 FB_FORMAT_STR(GL_R11F_G11F_B10F)
347 FB_FORMAT_STR(GL_RGB9_E5)
348 FB_FORMAT_STR(GL_R8I)
349 FB_FORMAT_STR(GL_R8UI)
350 FB_FORMAT_STR(GL_R16I)
351 FB_FORMAT_STR(GL_R16UI)
352 FB_FORMAT_STR(GL_R32I)
353 FB_FORMAT_STR(GL_R32UI)
354 FB_FORMAT_STR(GL_RG8I)
355 FB_FORMAT_STR(GL_RG8UI)
356 FB_FORMAT_STR(GL_RG16I)
357 FB_FORMAT_STR(GL_RG16UI)
358 FB_FORMAT_STR(GL_RG32I)
359 FB_FORMAT_STR(GL_RG32UI)
360 FB_FORMAT_STR(GL_RGB8I)
361 FB_FORMAT_STR(GL_RGB8UI)
362 FB_FORMAT_STR(GL_RGB16I)
363 FB_FORMAT_STR(GL_RGB16UI)
364 FB_FORMAT_STR(GL_RGB32I)
365 FB_FORMAT_STR(GL_RGB32UI)
366 FB_FORMAT_STR(GL_RGBA8I)
367 FB_FORMAT_STR(GL_RGBA8UI)
368 FB_FORMAT_STR(GL_RGBA16I)
369 FB_FORMAT_STR(GL_RGBA16UI)
370 FB_FORMAT_STR(GL_RGBA32I)
371 FB_FORMAT_STR(GL_RGBA32UI)
372 };
373
374 for( int i=0; i<vg_list_size(formats); i++ )
375 if( formats[i].e == format )
376 return formats[i].str;
377
378 return "UNDEFINED";
379 }
380
381 /*
382 * Bind and allocate texture for framebuffer attachment
383 */
384 VG_STATIC void render_fb_allocate_texture( struct framebuffer *fb,
385 struct framebuffer_attachment *a )
386 {
387 int rx, ry;
388 render_fb_get_current_res( fb, &rx, &ry );
389
390 if( a->purpose == k_framebuffer_attachment_type_renderbuffer ){
391 glBindRenderbuffer( GL_RENDERBUFFER, a->id );
392 glRenderbufferStorage( GL_RENDERBUFFER, a->internalformat, rx, ry );
393 }
394 else if( a->purpose == k_framebuffer_attachment_type_texture ||
395 a->purpose == k_framebuffer_attachment_type_texture_depth )
396 {
397 glBindTexture( GL_TEXTURE_2D, a->id );
398 glTexImage2D( GL_TEXTURE_2D, 0, a->internalformat, rx, ry,
399 0, a->format, a->type, NULL );
400 }
401 }
402
403 /*
404 * Full allocation of a framebuffer
405 */
406 VG_STATIC void render_fb_allocate( struct framebuffer *fb )
407 {
408 glGenFramebuffers( 1, &fb->fb );
409 glBindFramebuffer( GL_FRAMEBUFFER, fb->fb );
410
411 int rx, ry;
412 render_fb_get_current_res( fb, &rx, &ry );
413
414 vg_info( "allocate_framebuffer( %s, %dx%d )\n", fb->display_name, rx, ry );
415 vg_info( "{\n" );
416
417 GLenum colour_attachments[4];
418 u32 colour_count = 0;
419
420 for( int j=0; j<vg_list_size(fb->attachments); j++ ){
421 struct framebuffer_attachment *attachment = &fb->attachments[j];
422
423 if( attachment->purpose == k_framebuffer_attachment_type_none )
424 continue;
425
426 vg_info( " %s: %s\n",
427 render_fb_attachment_str( attachment->attachment ),
428 render_fb_format_str( attachment->internalformat ) );
429
430 if( attachment->purpose == k_framebuffer_attachment_type_renderbuffer ){
431 glGenRenderbuffers( 1, &attachment->id );
432 render_fb_allocate_texture( fb, attachment );
433 glFramebufferRenderbuffer( GL_FRAMEBUFFER,
434 GL_DEPTH_STENCIL_ATTACHMENT,
435 GL_RENDERBUFFER, attachment->id );
436 }
437 else if( attachment->purpose == k_framebuffer_attachment_type_texture ||
438 attachment->purpose == k_framebuffer_attachment_type_texture_depth )
439 {
440 glGenTextures( 1, &attachment->id );
441 render_fb_allocate_texture( fb, attachment );
442 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
443 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
444 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
445 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
446
447 glFramebufferTexture2D( GL_FRAMEBUFFER, attachment->attachment,
448 GL_TEXTURE_2D, attachment->id, 0 );
449
450 if( attachment->purpose == k_framebuffer_attachment_type_texture )
451 colour_attachments[ colour_count ++ ] = attachment->attachment;
452 }
453 }
454
455 glDrawBuffers( colour_count, colour_attachments );
456
457 /*
458 * Check result
459 */
460 GLenum result = glCheckFramebufferStatus( GL_FRAMEBUFFER );
461
462 if( result == GL_FRAMEBUFFER_COMPLETE ){
463 /*
464 * Attatch to gpipeline
465 */
466 if( fb->link )
467 *fb->link = fb;
468
469 vg_success( " status: complete\n" );
470 vg_info( "}\n" );
471 }
472 else{
473 if( result == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT )
474 vg_error( " status: Incomplete attachment" );
475 else if( result == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT )
476 vg_error( " status: Missing attachment" );
477 else if( result == GL_FRAMEBUFFER_UNSUPPORTED )
478 vg_error( " status: Unsupported framebuffer format" );
479 else
480 vg_error( " status: Generic Error" );
481
482 vg_info( "}\n" );
483 vg_fatal_exit_loop( "Incomplete framebuffer (see logs)" );
484 }
485 }
486
487 /*
488 * Resize/Update all framebuffers(we know about)
489 */
490 VG_STATIC void render_fb_resize(void)
491 {
492 if( !gpipeline.ready )
493 return;
494
495 for( int i=0; i<vg_list_size(framebuffers); i++ ){
496 struct framebuffer *fb = &framebuffers[i];
497 for( int j=0; j<vg_list_size(fb->attachments); j++ ){
498 struct framebuffer_attachment *attachment = &fb->attachments[j];
499 render_fb_allocate_texture( fb, attachment );
500 }
501 }
502 }
503
504 VG_STATIC int render_framebuffer_control( int argc, char const *argv[] );
505 VG_STATIC void render_framebuffer_poll( int argc, char const *argv[] );
506 VG_STATIC void render_init_fs_quad(void)
507 {
508 vg_info( "[render] Allocate quad\n" );
509
510 float quad[] = {
511 0.00f,0.00f, 1.00f,1.00f, 0.00f,1.00f,
512 0.00f,0.00f, 1.00f,0.00f, 1.00f,1.00f,
513
514 0.20f,0.00f, 0.80f,1.00f, 0.20f,1.00f,
515 0.20f,0.00f, 0.80f,0.00f, 0.80f,1.00f,
516
517 /* 9x9 debug grid */
518 /* row0 */
519 0.00f,0.00f, 0.30f,0.30f, 0.00f,0.30f,
520 0.00f,0.00f, 0.30f,0.00f, 0.30f,0.30f,
521 0.30f,0.00f, 0.60f,0.30f, 0.30f,0.30f,
522 0.30f,0.00f, 0.60f,0.00f, 0.60f,0.30f,
523 0.60f,0.00f, 0.90f,0.30f, 0.60f,0.30f,
524 0.60f,0.00f, 0.90f,0.00f, 0.90f,0.30f,
525 /* row1 */
526 0.00f,0.30f, 0.30f,0.60f, 0.00f,0.60f,
527 0.00f,0.30f, 0.30f,0.30f, 0.30f,0.60f,
528 0.30f,0.30f, 0.60f,0.60f, 0.30f,0.60f,
529 0.30f,0.30f, 0.60f,0.30f, 0.60f,0.60f,
530 0.60f,0.30f, 0.90f,0.60f, 0.60f,0.60f,
531 0.60f,0.30f, 0.90f,0.30f, 0.90f,0.60f,
532 /* row2 */
533 0.00f,0.60f, 0.30f,0.90f, 0.00f,0.90f,
534 0.00f,0.60f, 0.30f,0.60f, 0.30f,0.90f,
535 0.30f,0.60f, 0.60f,0.90f, 0.30f,0.90f,
536 0.30f,0.60f, 0.60f,0.60f, 0.60f,0.90f,
537 0.60f,0.60f, 0.90f,0.90f, 0.60f,0.90f,
538 0.60f,0.60f, 0.90f,0.60f, 0.90f,0.90f,
539 };
540
541 vg_console_reg_cmd( "fb", render_framebuffer_control,
542 render_framebuffer_poll );
543
544 glGenVertexArrays( 1, &gpipeline.fsquad.vao );
545 glGenBuffers( 1, &gpipeline.fsquad.vbo );
546 glBindVertexArray( gpipeline.fsquad.vao );
547 glBindBuffer( GL_ARRAY_BUFFER, gpipeline.fsquad.vbo );
548 glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
549 glBindVertexArray( gpipeline.fsquad.vao );
550 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE,
551 sizeof(float)*2, (void*)0 );
552 glEnableVertexAttribArray( 0 );
553
554 VG_CHECK_GL_ERR();
555 }
556
557 VG_STATIC void render_init(void)
558 {
559 shader_blit_register();
560 shader_blitblur_register();
561 shader_blitcolour_register();
562
563 vg_acquire_thread_sync();
564 {
565 /*
566 * Complete Framebuffers
567 */
568 for( int i=0; i<vg_list_size(framebuffers); i++ ){
569 struct framebuffer *fb = &framebuffers[i];
570 render_fb_allocate( fb );
571 }
572
573 render_init_fs_quad();
574
575 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
576 gpipeline.ready = 1;
577 }
578
579 vg_release_thread_sync();
580 }
581
582 /*
583 * Utility
584 */
585 VG_STATIC void render_fsquad(void)
586 {
587 glBindVertexArray( gpipeline.fsquad.vao );
588 glDrawArrays( GL_TRIANGLES, 0, 6 );
589 }
590
591 VG_STATIC void render_fsquad1(void)
592 {
593 glBindVertexArray( gpipeline.fsquad.vao );
594 glDrawArrays( GL_TRIANGLES, 6, 6 );
595 }
596
597 /*
598 * Call this inside the UI function
599 */
600 VG_STATIC void render_view_framebuffer_ui(void)
601 {
602 int viewing_count = 0;
603
604 glBindVertexArray( gpipeline.fsquad.vao );
605 shader_blit_use();
606 shader_blit_uTexMain( 0 );
607
608 v2f identity = { 1.0f, 1.0f };
609 shader_blit_uInverseRatio( identity );
610
611 for( int i=0; i<vg_list_size(framebuffers); i++ ){
612 struct framebuffer *fb = &framebuffers[i];
613
614 for( int j=0; j<vg_list_size(fb->attachments); j++ ){
615 struct framebuffer_attachment *at = &fb->attachments[j];
616
617 if( !at->debug_view )
618 continue;
619
620 v2f corner,
621 window = { vg.window_x, vg.window_y };
622
623 corner[0] = viewing_count % 3;
624 corner[1] = 1 + (viewing_count / 3);
625 v2_mul( corner, window, corner );
626 v2_muls( corner, 0.3f, corner );
627 corner[1] = vg.window_y - corner[1];
628
629 ui_text( (ui_rect){ corner[0], corner[1], 0.0f, 0.0f },
630 fb->display_name, 2, k_text_align_left );
631 ui_text( (ui_rect){ corner[0], corner[1] + 32, 0.0f, 0.0f, },
632 at->display_name, 1, k_text_align_left );
633
634 if( at->purpose == k_framebuffer_attachment_type_renderbuffer ){
635 v2f center;
636 v2_muladds( corner, window, 0.15f, center );
637
638 ui_text( (ui_rect){ center[0], center[1], 0.0f, 0.0f },
639 "<hardware texture>", 1, k_text_align_center );
640 }
641 else{
642 render_fb_bind_texture( fb, j, 0 );
643
644 int start = (viewing_count+2) * 6,
645 count = 6;
646 glDrawArrays( GL_TRIANGLES, start, count );
647 }
648
649 viewing_count ++;
650 }
651 }
652 }
653
654 VG_STATIC void render_framebuffer_show( struct framebuffer *fb,
655 struct framebuffer_attachment *at,
656 int operation )
657 {
658 at->debug_view = operation;
659 vg_info( "%s %s:%s\n", (operation?"shown": "hidden"),
660 fb->display_name, at->display_name );
661 }
662
663 /*
664 * arg0: command "show"/"hide"
665 * arg1: framebuffer name <name>/"all"
666 * arg2: subname <name>/none
667 */
668 VG_STATIC int render_framebuffer_control( int argc, char const *argv[] )
669 {
670 if( argc < 2 ){
671 vg_error( "Usage: fb \"show/hide\" <name>/\"all\" <name>/none\n" );
672 return 0;
673 }
674
675 int modify_all = 0,
676 operation = 0;
677
678 if( !strcmp( argv[0], "show" ) )
679 operation = 1;
680 else if( !strcmp( argv[0], "hide" ) )
681 operation = 0;
682 else{
683 vg_error( "Unknown framebuffer operation: '%s'\n", argv[0] );
684 return 0;
685 }
686
687 if( !strcmp( argv[1], "all" ) )
688 modify_all = 1;
689
690 for( int i=0; i<vg_list_size(framebuffers); i++ ){
691 struct framebuffer *fb = &framebuffers[i];
692
693 for( int j=0; j<vg_list_size(fb->attachments); j++ ){
694 struct framebuffer_attachment *at = &fb->attachments[j];
695
696 if( at->purpose == k_framebuffer_attachment_type_none )
697 continue;
698
699 if( modify_all ){
700 render_framebuffer_show( fb, at, operation );
701 }
702 else{
703 if( !strcmp( fb->display_name, argv[1] ) ){
704 if( argc == 2 )
705 render_framebuffer_show( fb, at, operation );
706 else if( !strcmp( at->display_name, argv[2] ) )
707 render_framebuffer_show( fb, at, operation );
708 }
709 }
710 }
711 }
712
713 return 0;
714 }
715
716 VG_STATIC void render_framebuffer_poll( int argc, char const *argv[] )
717 {
718 const char *term = argv[argc-1];
719
720 if( argc == 1 ){
721 console_suggest_score_text( "show", term, 0 );
722 console_suggest_score_text( "hide", term, 0 );
723 }
724 else if( argc == 2 ){
725 console_suggest_score_text( "all", term, 0 );
726
727 for( int i=0; i<vg_list_size(framebuffers); i++ ){
728 struct framebuffer *fb = &framebuffers[i];
729 console_suggest_score_text( fb->display_name, term, 0 );
730 }
731 }
732 else if( argc == 3 ){
733 int modify_all = 0;
734
735 if( !strcmp( argv[1], "all" ) )
736 modify_all = 1;
737
738 for( int i=0; i<vg_list_size(framebuffers); i++ ){
739 struct framebuffer *fb = &framebuffers[i];
740
741 for( int j=0; j<vg_list_size(fb->attachments); j++ ){
742 struct framebuffer_attachment *at = &fb->attachments[j];
743
744 if( at->purpose == k_framebuffer_attachment_type_none )
745 continue;
746
747 if( modify_all ){
748 console_suggest_score_text( at->display_name, term, 0 );
749 }
750 else if( !strcmp( fb->display_name, argv[1] ) ){
751 console_suggest_score_text( at->display_name, term, 0 );
752 }
753 }
754 }
755 }
756 }
757
758 #endif /* RENDER_H */