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