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