2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "shaders/blit.h"
11 #include "shaders/blitblur.h"
12 #include "shaders/blitcolour.h"
15 #include "shaders/standard.h"
16 #include "shaders/vblend.h"
19 VG_STATIC
void render_water_texture( world_instance
*world
, camera
*cam
,
21 VG_STATIC
void render_water_surface( world_instance
*world
, camera
*cam
);
22 VG_STATIC
void render_world( world_instance
*world
, camera
*cam
,
24 VG_STATIC
void render_world_depth( world_instance
*world
, camera
*cam
);
29 typedef struct framebuffer framebuffer
;
32 * All standard buffers used in rendering
34 VG_STATIC
struct pipeline
43 float view_render_scale
,
46 gpipeline
= { .view_render_scale
= 1.0f
};
50 const char *display_name
;
51 int resolution_div
, /* definition */
55 render_w
, /* runtime */
58 struct framebuffer_attachment
60 const char *display_name
;
62 enum framebuffer_attachment_type
64 k_framebuffer_attachment_type_none
,
65 k_framebuffer_attachment_type_colour
,
66 k_framebuffer_attachment_type_renderbuffer
70 enum framebuffer_quality_profile
72 k_framebuffer_quality_all
,
73 k_framebuffer_quality_high_only
77 GLenum internalformat
,
95 * The primary draw target
98 .link
= &gpipeline
.fb_main
,
103 "colour", k_framebuffer_attachment_type_colour
,
105 .internalformat
= GL_RGB
,
107 .type
= GL_UNSIGNED_BYTE
,
108 .attachment
= GL_COLOR_ATTACHMENT0
111 "motion", k_framebuffer_attachment_type_colour
,
113 .quality
= k_framebuffer_quality_high_only
,
114 .internalformat
= GL_RG16F
,
117 .attachment
= GL_COLOR_ATTACHMENT1
120 "depth_stencil", k_framebuffer_attachment_type_renderbuffer
,
122 .internalformat
= GL_DEPTH24_STENCIL8
,
123 .attachment
= GL_DEPTH_STENCIL_ATTACHMENT
129 * Second rendered view from the perspective of the water reflection
132 .link
= &gpipeline
.fb_water_reflection
,
137 "colour", k_framebuffer_attachment_type_colour
,
138 .internalformat
= GL_RGB
,
140 .type
= GL_UNSIGNED_BYTE
,
141 .attachment
= GL_COLOR_ATTACHMENT0
144 "depth_stencil", k_framebuffer_attachment_type_renderbuffer
,
146 .internalformat
= GL_DEPTH24_STENCIL8
,
147 .attachment
= GL_DEPTH_STENCIL_ATTACHMENT
153 * Thid rendered view from the perspective of the camera, but just
154 * captures stuff thats under the water
157 .link
= &gpipeline
.fb_water_beneath
,
162 "colour", k_framebuffer_attachment_type_colour
,
163 .internalformat
= GL_RED
,
165 .type
= GL_UNSIGNED_BYTE
,
166 .attachment
= GL_COLOR_ATTACHMENT0
169 "depth_stencil", k_framebuffer_attachment_type_renderbuffer
,
171 .internalformat
= GL_DEPTH24_STENCIL8
,
172 .attachment
= GL_DEPTH_STENCIL_ATTACHMENT
179 * Get the current (automatically scaled or fixed) resolution of framebuffer
181 VG_STATIC
void render_fb_get_current_res( struct framebuffer
*fb
,
184 if( fb
->resolution_div
){
185 *x
= vg
.window_x
/ fb
->resolution_div
;
186 *y
= vg
.window_y
/ fb
->resolution_div
;
194 VG_STATIC
void render_fb_inverse_ratio( framebuffer
*fb
, v2f inverse
)
197 render_fb_get_current_res( fb
, &x
, &y
);
199 v2f render
= { fb
->render_w
, fb
->render_h
},
202 v2_div( render
, original
, inverse
);
206 * Bind framebuffer for drawing to
208 VG_STATIC
void render_fb_bind( framebuffer
*fb
, int use_scaling
)
211 render_fb_get_current_res( fb
, &x
, &y
);
214 x
= gpipeline
.view_render_scale
*(float)x
;
215 y
= gpipeline
.view_render_scale
*(float)y
;
224 glBindFramebuffer( GL_FRAMEBUFFER
, fb
->fb
);
225 glViewport( 0, 0, x
, y
);
229 * Bind framebuffer attachment's texture
231 VG_STATIC
void render_fb_bind_texture( framebuffer
*fb
,
232 int attachment
, int slot
)
234 struct framebuffer_attachment
*at
= &fb
->attachments
[attachment
];
236 if( at
->purpose
!= k_framebuffer_attachment_type_colour
)
238 vg_fatal_exit_loop( "illegal operation: bind non-colour framebuffer"
239 " attachment to texture slot" );
242 glActiveTexture( GL_TEXTURE0
+ slot
);
243 glBindTexture( GL_TEXTURE_2D
, fb
->attachments
[attachment
].id
);
251 #define FB_FORMAT_STR( E ) { E, #E },
254 * Convert OpenGL attachment ID enum to string
256 VG_STATIC
const char *render_fb_attachment_str( GLenum e
)
258 struct { GLenum e
; const char *str
; }
261 FB_FORMAT_STR(GL_COLOR_ATTACHMENT0
)
262 FB_FORMAT_STR(GL_COLOR_ATTACHMENT1
)
263 FB_FORMAT_STR(GL_COLOR_ATTACHMENT2
)
264 FB_FORMAT_STR(GL_COLOR_ATTACHMENT3
)
265 FB_FORMAT_STR(GL_COLOR_ATTACHMENT4
)
266 FB_FORMAT_STR(GL_DEPTH_STENCIL_ATTACHMENT
)
269 for( int i
=0; i
<vg_list_size(formats
); i
++ )
270 if( formats
[i
].e
== e
)
271 return formats
[i
].str
;
277 * Convert OpenGL texture format enums from TexImage2D table 1,2 &
278 * RenderBufferStorage Table 1, into strings
280 VG_STATIC
const char *render_fb_format_str( GLenum format
)
282 struct { GLenum e
; const char *str
; }
286 FB_FORMAT_STR(GL_DEPTH_COMPONENT
)
287 FB_FORMAT_STR(GL_DEPTH_STENCIL
)
288 FB_FORMAT_STR(GL_RED
)
290 FB_FORMAT_STR(GL_RGB
)
291 FB_FORMAT_STR(GL_RGBA
)
293 /* Render buffer formats */
294 FB_FORMAT_STR(GL_DEPTH_COMPONENT16
)
295 FB_FORMAT_STR(GL_DEPTH_COMPONENT24
)
296 FB_FORMAT_STR(GL_DEPTH_COMPONENT32F
)
297 FB_FORMAT_STR(GL_DEPTH24_STENCIL8
)
298 FB_FORMAT_STR(GL_DEPTH32F_STENCIL8
)
299 FB_FORMAT_STR(GL_STENCIL_INDEX8
)
303 FB_FORMAT_STR(GL_R8_SNORM
)
304 FB_FORMAT_STR(GL_R16
)
305 FB_FORMAT_STR(GL_R16_SNORM
)
306 FB_FORMAT_STR(GL_RG8
)
307 FB_FORMAT_STR(GL_RG8_SNORM
)
308 FB_FORMAT_STR(GL_RG16
)
309 FB_FORMAT_STR(GL_RG16_SNORM
)
310 FB_FORMAT_STR(GL_R3_G3_B2
)
311 FB_FORMAT_STR(GL_RGB4
)
312 FB_FORMAT_STR(GL_RGB5
)
313 FB_FORMAT_STR(GL_RGB8
)
314 FB_FORMAT_STR(GL_RGB8_SNORM
)
315 FB_FORMAT_STR(GL_RGB10
)
316 FB_FORMAT_STR(GL_RGB12
)
317 FB_FORMAT_STR(GL_RGB16_SNORM
)
318 FB_FORMAT_STR(GL_RGBA2
)
319 FB_FORMAT_STR(GL_RGBA4
)
320 FB_FORMAT_STR(GL_RGB5_A1
)
321 FB_FORMAT_STR(GL_RGBA8
)
322 FB_FORMAT_STR(GL_RGBA8_SNORM
)
323 FB_FORMAT_STR(GL_RGB10_A2
)
324 FB_FORMAT_STR(GL_RGB10_A2UI
)
325 FB_FORMAT_STR(GL_RGBA12
)
326 FB_FORMAT_STR(GL_RGBA16
)
327 FB_FORMAT_STR(GL_SRGB8
)
328 FB_FORMAT_STR(GL_SRGB8_ALPHA8
)
329 FB_FORMAT_STR(GL_R16F
)
330 FB_FORMAT_STR(GL_RG16F
)
331 FB_FORMAT_STR(GL_RGB16F
)
332 FB_FORMAT_STR(GL_RGBA16F
)
333 FB_FORMAT_STR(GL_R32F
)
334 FB_FORMAT_STR(GL_RG32F
)
335 FB_FORMAT_STR(GL_RGB32F
)
336 FB_FORMAT_STR(GL_RGBA32F
)
337 FB_FORMAT_STR(GL_R11F_G11F_B10F
)
338 FB_FORMAT_STR(GL_RGB9_E5
)
339 FB_FORMAT_STR(GL_R8I
)
340 FB_FORMAT_STR(GL_R8UI
)
341 FB_FORMAT_STR(GL_R16I
)
342 FB_FORMAT_STR(GL_R16UI
)
343 FB_FORMAT_STR(GL_R32I
)
344 FB_FORMAT_STR(GL_R32UI
)
345 FB_FORMAT_STR(GL_RG8I
)
346 FB_FORMAT_STR(GL_RG8UI
)
347 FB_FORMAT_STR(GL_RG16I
)
348 FB_FORMAT_STR(GL_RG16UI
)
349 FB_FORMAT_STR(GL_RG32I
)
350 FB_FORMAT_STR(GL_RG32UI
)
351 FB_FORMAT_STR(GL_RGB8I
)
352 FB_FORMAT_STR(GL_RGB8UI
)
353 FB_FORMAT_STR(GL_RGB16I
)
354 FB_FORMAT_STR(GL_RGB16UI
)
355 FB_FORMAT_STR(GL_RGB32I
)
356 FB_FORMAT_STR(GL_RGB32UI
)
357 FB_FORMAT_STR(GL_RGBA8I
)
358 FB_FORMAT_STR(GL_RGBA8UI
)
359 FB_FORMAT_STR(GL_RGBA16I
)
360 FB_FORMAT_STR(GL_RGBA16UI
)
361 FB_FORMAT_STR(GL_RGBA32I
)
362 FB_FORMAT_STR(GL_RGBA32UI
)
365 for( int i
=0; i
<vg_list_size(formats
); i
++ )
366 if( formats
[i
].e
== format
)
367 return formats
[i
].str
;
373 * Bind and allocate texture for framebuffer attachment
375 VG_STATIC
void render_fb_allocate_texture( struct framebuffer
*fb
,
376 struct framebuffer_attachment
*a
)
379 render_fb_get_current_res( fb
, &rx
, &ry
);
381 if( a
->purpose
== k_framebuffer_attachment_type_renderbuffer
)
383 glBindRenderbuffer( GL_RENDERBUFFER
, a
->id
);
384 glRenderbufferStorage( GL_RENDERBUFFER
, GL_DEPTH24_STENCIL8
, rx
, ry
);
386 else if( a
->purpose
== k_framebuffer_attachment_type_colour
)
388 glBindTexture( GL_TEXTURE_2D
, a
->id
);
389 glTexImage2D( GL_TEXTURE_2D
, 0, a
->internalformat
, rx
, ry
,
390 0, a
->format
, a
->type
, NULL
);
395 * Full allocation of a framebuffer
397 VG_STATIC
void render_fb_allocate( struct framebuffer
*fb
)
399 glGenFramebuffers( 1, &fb
->fb
);
400 glBindFramebuffer( GL_FRAMEBUFFER
, fb
->fb
);
403 render_fb_get_current_res( fb
, &rx
, &ry
);
405 vg_info( "allocate_framebuffer( %s, %dx%d )\n", fb
->display_name
, rx
, ry
);
408 GLenum colour_attachments
[4];
409 u32 colour_count
= 0;
411 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ )
413 struct framebuffer_attachment
*attachment
= &fb
->attachments
[j
];
415 if( attachment
->purpose
== k_framebuffer_attachment_type_none
)
418 vg_info( " %s: %s\n",
419 render_fb_attachment_str( attachment
->attachment
),
420 render_fb_format_str( attachment
->internalformat
) );
422 if( attachment
->purpose
== k_framebuffer_attachment_type_renderbuffer
)
424 glGenRenderbuffers( 1, &attachment
->id
);
425 render_fb_allocate_texture( fb
, attachment
);
426 glFramebufferRenderbuffer( GL_FRAMEBUFFER
,
427 GL_DEPTH_STENCIL_ATTACHMENT
,
428 GL_RENDERBUFFER
, attachment
->id
);
430 else if( attachment
->purpose
== k_framebuffer_attachment_type_colour
)
432 glGenTextures( 1, &attachment
->id
);
433 render_fb_allocate_texture( fb
, attachment
);
434 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
435 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
436 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
437 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
439 glFramebufferTexture2D( GL_FRAMEBUFFER
, attachment
->attachment
,
440 GL_TEXTURE_2D
, attachment
->id
, 0 );
442 colour_attachments
[ colour_count
++ ] = attachment
->attachment
;
446 glDrawBuffers( colour_count
, colour_attachments
);
451 GLenum result
= glCheckFramebufferStatus( GL_FRAMEBUFFER
);
453 if( result
== GL_FRAMEBUFFER_COMPLETE
)
456 * Attatch to gpipeline
461 vg_success( " status: complete\n" );
466 if( result
== GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
)
467 vg_error( " status: Incomplete attachment" );
468 else if( result
== GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
)
469 vg_error( " status: Missing attachment" );
470 else if( result
== GL_FRAMEBUFFER_UNSUPPORTED
)
471 vg_error( " status: Unsupported framebuffer format" );
473 vg_error( " status: Generic Error" );
476 vg_fatal_exit_loop( "Incomplete framebuffer (see logs)" );
481 * Resize/Update all framebuffers(we know about)
483 VG_STATIC
void render_fb_resize(void)
485 if( !gpipeline
.ready
)
488 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
490 struct framebuffer
*fb
= &framebuffers
[i
];
491 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ )
493 struct framebuffer_attachment
*attachment
= &fb
->attachments
[j
];
494 render_fb_allocate_texture( fb
, attachment
);
499 VG_STATIC
int render_framebuffer_control( int argc
, char const *argv
[] );
500 VG_STATIC
void render_framebuffer_poll( int argc
, char const *argv
[] );
501 VG_STATIC
void render_init_fs_quad(void)
503 vg_info( "[render] Allocate quad\n" );
507 0.00f
,0.00f
, 1.00f
,1.00f
, 0.00f
,1.00f
,
508 0.00f
,0.00f
, 1.00f
,0.00f
, 1.00f
,1.00f
,
510 0.20f
,0.00f
, 0.80f
,1.00f
, 0.20f
,1.00f
,
511 0.20f
,0.00f
, 0.80f
,0.00f
, 0.80f
,1.00f
,
515 0.00f
,0.00f
, 0.30f
,0.30f
, 0.00f
,0.30f
,
516 0.00f
,0.00f
, 0.30f
,0.00f
, 0.30f
,0.30f
,
517 0.30f
,0.00f
, 0.60f
,0.30f
, 0.30f
,0.30f
,
518 0.30f
,0.00f
, 0.60f
,0.00f
, 0.60f
,0.30f
,
519 0.60f
,0.00f
, 0.90f
,0.30f
, 0.60f
,0.30f
,
520 0.60f
,0.00f
, 0.90f
,0.00f
, 0.90f
,0.30f
,
522 0.00f
,0.30f
, 0.30f
,0.60f
, 0.00f
,0.60f
,
523 0.00f
,0.30f
, 0.30f
,0.30f
, 0.30f
,0.60f
,
524 0.30f
,0.30f
, 0.60f
,0.60f
, 0.30f
,0.60f
,
525 0.30f
,0.30f
, 0.60f
,0.30f
, 0.60f
,0.60f
,
526 0.60f
,0.30f
, 0.90f
,0.60f
, 0.60f
,0.60f
,
527 0.60f
,0.30f
, 0.90f
,0.30f
, 0.90f
,0.60f
,
529 0.00f
,0.60f
, 0.30f
,0.90f
, 0.00f
,0.90f
,
530 0.00f
,0.60f
, 0.30f
,0.60f
, 0.30f
,0.90f
,
531 0.30f
,0.60f
, 0.60f
,0.90f
, 0.30f
,0.90f
,
532 0.30f
,0.60f
, 0.60f
,0.60f
, 0.60f
,0.90f
,
533 0.60f
,0.60f
, 0.90f
,0.90f
, 0.60f
,0.90f
,
534 0.60f
,0.60f
, 0.90f
,0.60f
, 0.90f
,0.90f
,
537 vg_function_push( (struct vg_cmd
)
540 .function
= render_framebuffer_control
,
541 .poll_suggest
= render_framebuffer_poll
544 glGenVertexArrays( 1, &gpipeline
.fsquad
.vao
);
545 glGenBuffers( 1, &gpipeline
.fsquad
.vbo
);
546 glBindVertexArray( gpipeline
.fsquad
.vao
);
547 glBindBuffer( GL_ARRAY_BUFFER
, gpipeline
.fsquad
.vbo
);
548 glBufferData( GL_ARRAY_BUFFER
, sizeof(quad
), quad
, GL_STATIC_DRAW
);
549 glBindVertexArray( gpipeline
.fsquad
.vao
);
550 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
,
551 sizeof(float)*2, (void*)0 );
552 glEnableVertexAttribArray( 0 );
557 VG_STATIC
void render_init(void)
559 shader_blit_register();
560 shader_blitblur_register();
561 shader_blitcolour_register();
563 vg_acquire_thread_sync();
566 * Complete Framebuffers
568 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
570 struct framebuffer
*fb
= &framebuffers
[i
];
571 render_fb_allocate( fb
);
574 render_init_fs_quad();
576 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
580 vg_release_thread_sync();
586 VG_STATIC
void render_fsquad(void)
588 glBindVertexArray( gpipeline
.fsquad
.vao
);
589 glDrawArrays( GL_TRIANGLES
, 0, 6 );
592 VG_STATIC
void render_fsquad1(void)
594 glBindVertexArray( gpipeline
.fsquad
.vao
);
595 glDrawArrays( GL_TRIANGLES
, 6, 6 );
599 * Call this inside the UI function
601 VG_STATIC
void render_view_framebuffer_ui(void)
603 int viewing_count
= 0;
605 glBindVertexArray( gpipeline
.fsquad
.vao
);
607 shader_blit_uTexMain( 0 );
609 v2f identity
= { 1.0f
, 1.0f
};
610 shader_blit_uInverseRatio( identity
);
612 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ ){
613 struct framebuffer
*fb
= &framebuffers
[i
];
615 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ ){
616 struct framebuffer_attachment
*at
= &fb
->attachments
[j
];
618 if( !at
->debug_view
)
622 window
= { vg
.window_x
, vg
.window_y
};
624 corner
[0] = viewing_count
% 3;
625 corner
[1] = 1 + (viewing_count
/ 3);
626 v2_mul( corner
, window
, corner
);
627 v2_muls( corner
, 0.3f
, corner
);
628 corner
[1] = vg
.window_y
- corner
[1];
630 ui_text( (ui_rect
){ corner
[0], corner
[1], 0.0f
, 0.0f
},
631 fb
->display_name
, 2, k_text_align_left
);
632 ui_text( (ui_rect
){ corner
[0], corner
[1] + 32, 0.0f
, 0.0f
, },
633 at
->display_name
, 1, k_text_align_left
);
635 if( at
->purpose
== k_framebuffer_attachment_type_renderbuffer
){
637 v2_muladds( corner
, window
, 0.15f
, center
);
639 ui_text( (ui_rect
){ center
[0], center
[1], 0.0f
, 0.0f
},
640 "<hardware texture>", 1, k_text_align_center
);
643 render_fb_bind_texture( fb
, j
, 0 );
645 int start
= (viewing_count
+2) * 6,
647 glDrawArrays( GL_TRIANGLES
, start
, count
);
655 VG_STATIC
void render_framebuffer_show( struct framebuffer
*fb
,
656 struct framebuffer_attachment
*at
,
659 at
->debug_view
= operation
;
660 vg_info( "%s %s:%s\n", (operation
?"shown": "hidden"),
661 fb
->display_name
, at
->display_name
);
665 * arg0: command "show"/"hide"
666 * arg1: framebuffer name <name>/"all"
667 * arg2: subname <name>/none
669 VG_STATIC
int render_framebuffer_control( int argc
, char const *argv
[] )
673 vg_error( "Usage: fb \"show/hide\" <name>/\"all\" <name>/none\n" );
680 if( !strcmp( argv
[0], "show" ) )
682 else if( !strcmp( argv
[0], "hide" ) )
686 vg_error( "Unknown framebuffer operation: '%s'\n", argv
[0] );
690 if( !strcmp( argv
[1], "all" ) )
693 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
695 struct framebuffer
*fb
= &framebuffers
[i
];
697 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ )
699 struct framebuffer_attachment
*at
= &fb
->attachments
[j
];
701 if( at
->purpose
== k_framebuffer_attachment_type_none
)
706 render_framebuffer_show( fb
, at
, operation
);
710 if( !strcmp( fb
->display_name
, argv
[1] ) )
713 render_framebuffer_show( fb
, at
, operation
);
714 else if( !strcmp( at
->display_name
, argv
[2] ) )
715 render_framebuffer_show( fb
, at
, operation
);
724 VG_STATIC
void render_framebuffer_poll( int argc
, char const *argv
[] )
726 const char *term
= argv
[argc
-1];
730 console_suggest_score_text( "show", term
, 0 );
731 console_suggest_score_text( "hide", term
, 0 );
735 console_suggest_score_text( "all", term
, 0 );
737 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
739 struct framebuffer
*fb
= &framebuffers
[i
];
740 console_suggest_score_text( fb
->display_name
, term
, 0 );
747 if( !strcmp( argv
[1], "all" ) )
750 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
752 struct framebuffer
*fb
= &framebuffers
[i
];
754 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ )
756 struct framebuffer_attachment
*at
= &fb
->attachments
[j
];
758 if( at
->purpose
== k_framebuffer_attachment_type_none
)
763 console_suggest_score_text( at
->display_name
, term
, 0 );
765 else if( !strcmp( fb
->display_name
, argv
[1] ) )
767 console_suggest_score_text( at
->display_name
, term
, 0 );
774 #endif /* RENDER_H */