imgui stuff
[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_error( "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_error( "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
507 VG_STATIC void async_render_init( void *payload, u32 size )
508 {
509 /*
510 * Complete Framebuffers
511 */
512 for( int i=0; i<vg_list_size(framebuffers); i++ ){
513 struct framebuffer *fb = &framebuffers[i];
514 render_fb_allocate( fb );
515 }
516
517 float quad[] = {
518 0.00f,0.00f, 1.00f,1.00f, 0.00f,1.00f, /* fsquad */
519 0.00f,0.00f, 1.00f,0.00f, 1.00f,1.00f,
520
521 0.00f,0.00f, 1.00f,0x1p-4f,0.00f,0x1p-4f, /* fsquad1 */
522 0.00f,0.00f, 1.00f,0.00f, 1.00f,0x1p-4f,
523
524 /* 9x9 debug grid */
525 /* row0 */
526 0.00f,0.00f, 0.30f,0.30f, 0.00f,0.30f,
527 0.00f,0.00f, 0.30f,0.00f, 0.30f,0.30f,
528 0.30f,0.00f, 0.60f,0.30f, 0.30f,0.30f,
529 0.30f,0.00f, 0.60f,0.00f, 0.60f,0.30f,
530 0.60f,0.00f, 0.90f,0.30f, 0.60f,0.30f,
531 0.60f,0.00f, 0.90f,0.00f, 0.90f,0.30f,
532 /* row1 */
533 0.00f,0.30f, 0.30f,0.60f, 0.00f,0.60f,
534 0.00f,0.30f, 0.30f,0.30f, 0.30f,0.60f,
535 0.30f,0.30f, 0.60f,0.60f, 0.30f,0.60f,
536 0.30f,0.30f, 0.60f,0.30f, 0.60f,0.60f,
537 0.60f,0.30f, 0.90f,0.60f, 0.60f,0.60f,
538 0.60f,0.30f, 0.90f,0.30f, 0.90f,0.60f,
539 /* row2 */
540 0.00f,0.60f, 0.30f,0.90f, 0.00f,0.90f,
541 0.00f,0.60f, 0.30f,0.60f, 0.30f,0.90f,
542 0.30f,0.60f, 0.60f,0.90f, 0.30f,0.90f,
543 0.30f,0.60f, 0.60f,0.60f, 0.60f,0.90f,
544 0.60f,0.60f, 0.90f,0.90f, 0.60f,0.90f,
545 0.60f,0.60f, 0.90f,0.60f, 0.90f,0.90f,
546 };
547
548 vg_console_reg_cmd( "fb", render_framebuffer_control,
549 render_framebuffer_poll );
550
551 glGenVertexArrays( 1, &gpipeline.fsquad.vao );
552 glGenBuffers( 1, &gpipeline.fsquad.vbo );
553 glBindVertexArray( gpipeline.fsquad.vao );
554 glBindBuffer( GL_ARRAY_BUFFER, gpipeline.fsquad.vbo );
555 glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
556 glBindVertexArray( gpipeline.fsquad.vao );
557 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE,
558 sizeof(float)*2, (void*)0 );
559 glEnableVertexAttribArray( 0 );
560
561 VG_CHECK_GL_ERR();
562
563 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
564 gpipeline.ready = 1;
565 }
566
567 VG_STATIC void render_init(void)
568 {
569 shader_blit_register();
570 shader_blitblur_register();
571 shader_blitcolour_register();
572
573 vg_async_call( async_render_init, NULL, 0 );
574 }
575
576 /*
577 * Utility
578 */
579 VG_STATIC void render_fsquad(void)
580 {
581 glBindVertexArray( gpipeline.fsquad.vao );
582 glDrawArrays( GL_TRIANGLES, 0, 6 );
583 }
584
585 VG_STATIC void render_fsquad1(void)
586 {
587 glBindVertexArray( gpipeline.fsquad.vao );
588 glDrawArrays( GL_TRIANGLES, 6, 6 );
589 }
590
591 /*
592 * Call this inside the UI function
593 */
594 VG_STATIC void render_view_framebuffer_ui(void)
595 {
596 #if 0
597 int viewing_count = 0;
598
599 glBindVertexArray( gpipeline.fsquad.vao );
600 shader_blit_use();
601 shader_blit_uTexMain( 0 );
602
603 v2f identity = { 1.0f, 1.0f };
604 shader_blit_uInverseRatio( identity );
605
606 for( int i=0; i<vg_list_size(framebuffers); i++ ){
607 struct framebuffer *fb = &framebuffers[i];
608
609 for( int j=0; j<vg_list_size(fb->attachments); j++ ){
610 struct framebuffer_attachment *at = &fb->attachments[j];
611
612 if( !at->debug_view )
613 continue;
614
615 v2f corner,
616 window = { vg.window_x, vg.window_y };
617
618 corner[0] = viewing_count % 3;
619 corner[1] = 1 + (viewing_count / 3);
620 v2_mul( corner, window, corner );
621 v2_muls( corner, 0.3f, corner );
622 corner[1] = vg.window_y - corner[1];
623
624 ui_text( (ui_rect){ corner[0], corner[1], 0.0f, 0.0f },
625 fb->display_name, 2, k_text_align_left );
626 ui_text( (ui_rect){ corner[0], corner[1] + 32, 0.0f, 0.0f, },
627 at->display_name, 1, k_text_align_left );
628
629 if( at->purpose == k_framebuffer_attachment_type_renderbuffer ){
630 v2f center;
631 v2_muladds( corner, window, 0.15f, center );
632
633 ui_text( (ui_rect){ center[0], center[1], 0.0f, 0.0f },
634 "<hardware texture>", 1, k_text_align_center );
635 }
636 else{
637 render_fb_bind_texture( fb, j, 0 );
638
639 int start = (viewing_count+2) * 6,
640 count = 6;
641 glDrawArrays( GL_TRIANGLES, start, count );
642 }
643
644 viewing_count ++;
645 }
646 }
647 #endif
648 }
649
650 VG_STATIC void render_framebuffer_show( struct framebuffer *fb,
651 struct framebuffer_attachment *at,
652 int operation )
653 {
654 at->debug_view = operation;
655 vg_info( "%s %s:%s\n", (operation?"shown": "hidden"),
656 fb->display_name, at->display_name );
657 }
658
659 /*
660 * arg0: command "show"/"hide"
661 * arg1: framebuffer name <name>/"all"
662 * arg2: subname <name>/none
663 */
664 VG_STATIC int render_framebuffer_control( int argc, char const *argv[] )
665 {
666 if( argc < 2 ){
667 vg_error( "Usage: fb \"show/hide\" <name>/\"all\" <name>/none\n" );
668 return 0;
669 }
670
671 int modify_all = 0,
672 operation = 0;
673
674 if( !strcmp( argv[0], "show" ) )
675 operation = 1;
676 else if( !strcmp( argv[0], "hide" ) )
677 operation = 0;
678 else{
679 vg_error( "Unknown framebuffer operation: '%s'\n", argv[0] );
680 return 0;
681 }
682
683 if( !strcmp( argv[1], "all" ) )
684 modify_all = 1;
685
686 for( int i=0; i<vg_list_size(framebuffers); i++ ){
687 struct framebuffer *fb = &framebuffers[i];
688
689 for( int j=0; j<vg_list_size(fb->attachments); j++ ){
690 struct framebuffer_attachment *at = &fb->attachments[j];
691
692 if( at->purpose == k_framebuffer_attachment_type_none )
693 continue;
694
695 if( modify_all ){
696 render_framebuffer_show( fb, at, operation );
697 }
698 else{
699 if( !strcmp( fb->display_name, argv[1] ) ){
700 if( argc == 2 )
701 render_framebuffer_show( fb, at, operation );
702 else if( !strcmp( at->display_name, argv[2] ) )
703 render_framebuffer_show( fb, at, operation );
704 }
705 }
706 }
707 }
708
709 return 0;
710 }
711
712 VG_STATIC void render_framebuffer_poll( int argc, char const *argv[] )
713 {
714 const char *term = argv[argc-1];
715
716 if( argc == 1 ){
717 console_suggest_score_text( "show", term, 0 );
718 console_suggest_score_text( "hide", term, 0 );
719 }
720 else if( argc == 2 ){
721 console_suggest_score_text( "all", term, 0 );
722
723 for( int i=0; i<vg_list_size(framebuffers); i++ ){
724 struct framebuffer *fb = &framebuffers[i];
725 console_suggest_score_text( fb->display_name, term, 0 );
726 }
727 }
728 else if( argc == 3 ){
729 int modify_all = 0;
730
731 if( !strcmp( argv[1], "all" ) )
732 modify_all = 1;
733
734 for( int i=0; i<vg_list_size(framebuffers); i++ ){
735 struct framebuffer *fb = &framebuffers[i];
736
737 for( int j=0; j<vg_list_size(fb->attachments); j++ ){
738 struct framebuffer_attachment *at = &fb->attachments[j];
739
740 if( at->purpose == k_framebuffer_attachment_type_none )
741 continue;
742
743 if( modify_all ){
744 console_suggest_score_text( at->display_name, term, 0 );
745 }
746 else if( !strcmp( fb->display_name, argv[1] ) ){
747 console_suggest_score_text( at->display_name, term, 0 );
748 }
749 }
750 }
751 }
752 }
753
754 #endif /* RENDER_H */