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