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