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
{
42 float view_render_scale
,
45 gpipeline
= { .view_render_scale
= 1.0f
};
48 const char *display_name
;
49 int resolution_div
, /* definition */
53 render_w
, /* runtime */
56 struct framebuffer_attachment
{
57 const char *display_name
;
59 enum framebuffer_attachment_type
{
60 k_framebuffer_attachment_type_none
,
61 k_framebuffer_attachment_type_colour
,
62 k_framebuffer_attachment_type_renderbuffer
66 enum framebuffer_quality_profile
{
67 k_framebuffer_quality_all
,
68 k_framebuffer_quality_high_only
72 GLenum internalformat
,
90 * The primary draw target
93 .link
= &gpipeline
.fb_main
,
98 "colour", k_framebuffer_attachment_type_colour
,
100 .internalformat
= GL_RGB
,
102 .type
= GL_UNSIGNED_BYTE
,
103 .attachment
= GL_COLOR_ATTACHMENT0
106 "motion", k_framebuffer_attachment_type_colour
,
108 .quality
= k_framebuffer_quality_high_only
,
109 .internalformat
= GL_RG16F
,
112 .attachment
= GL_COLOR_ATTACHMENT1
116 "depth_stencil", k_framebuffer_attachment_type_renderbuffer
,
118 .internalformat
= GL_DEPTH24_STENCIL8
,
120 "depth_stencil", k_framebuffer_attachment_type_colour
,
121 .internalformat
= GL_DEPTH24_STENCIL8
,
122 .format
= GL_DEPTH_STENCIL
,
123 .type
= GL_UNSIGNED_INT_24_8
,
125 .attachment
= GL_DEPTH_STENCIL_ATTACHMENT
131 * Second rendered view from the perspective of the water reflection
134 .link
= &gpipeline
.fb_water_reflection
,
139 "colour", k_framebuffer_attachment_type_colour
,
140 .internalformat
= GL_RGB
,
142 .type
= GL_UNSIGNED_BYTE
,
143 .attachment
= GL_COLOR_ATTACHMENT0
146 "depth_stencil", k_framebuffer_attachment_type_renderbuffer
,
148 .internalformat
= GL_DEPTH24_STENCIL8
,
149 .attachment
= GL_DEPTH_STENCIL_ATTACHMENT
155 * Thid rendered view from the perspective of the camera, but just
156 * captures stuff thats under the water
159 .link
= &gpipeline
.fb_water_beneath
,
164 "colour", k_framebuffer_attachment_type_colour
,
165 .internalformat
= GL_RED
,
167 .type
= GL_UNSIGNED_BYTE
,
168 .attachment
= GL_COLOR_ATTACHMENT0
171 "depth_stencil", k_framebuffer_attachment_type_renderbuffer
,
173 .internalformat
= GL_DEPTH24_STENCIL8
,
174 .attachment
= GL_DEPTH_STENCIL_ATTACHMENT
181 * Get the current (automatically scaled or fixed) resolution of framebuffer
183 VG_STATIC
void render_fb_get_current_res( struct framebuffer
*fb
,
186 if( fb
->resolution_div
){
187 *x
= vg
.window_x
/ fb
->resolution_div
;
188 *y
= vg
.window_y
/ fb
->resolution_div
;
196 VG_STATIC
void render_fb_inverse_ratio( framebuffer
*fb
, v2f inverse
)
200 render_fb_get_current_res( fb
, &x
, &y
);
202 v2f render
= { fb
->render_w
, fb
->render_h
},
205 v2_div( render
, original
, inverse
);
208 v2_div( (v2f
){1.0f
,1.0f
}, (v2f
){ vg
.window_x
, vg
.window_y
}, inverse
);
213 * Bind framebuffer for drawing to
215 VG_STATIC
void render_fb_bind( framebuffer
*fb
, int use_scaling
)
218 render_fb_get_current_res( fb
, &x
, &y
);
221 x
= gpipeline
.view_render_scale
*(float)x
;
222 y
= gpipeline
.view_render_scale
*(float)y
;
231 glBindFramebuffer( GL_FRAMEBUFFER
, fb
->fb
);
232 glViewport( 0, 0, x
, y
);
236 * Bind framebuffer attachment's texture
238 VG_STATIC
void render_fb_bind_texture( framebuffer
*fb
,
239 int attachment
, int slot
)
241 struct framebuffer_attachment
*at
= &fb
->attachments
[attachment
];
243 if( at
->purpose
!= k_framebuffer_attachment_type_colour
){
244 vg_fatal_exit_loop( "illegal operation: bind non-colour framebuffer"
245 " attachment to texture slot" );
248 glActiveTexture( GL_TEXTURE0
+ slot
);
249 glBindTexture( GL_TEXTURE_2D
, fb
->attachments
[attachment
].id
);
257 #define FB_FORMAT_STR( E ) { E, #E },
260 * Convert OpenGL attachment ID enum to string
262 VG_STATIC
const char *render_fb_attachment_str( GLenum e
)
264 struct { GLenum e
; const char *str
; }
267 FB_FORMAT_STR(GL_COLOR_ATTACHMENT0
)
268 FB_FORMAT_STR(GL_COLOR_ATTACHMENT1
)
269 FB_FORMAT_STR(GL_COLOR_ATTACHMENT2
)
270 FB_FORMAT_STR(GL_COLOR_ATTACHMENT3
)
271 FB_FORMAT_STR(GL_COLOR_ATTACHMENT4
)
272 FB_FORMAT_STR(GL_DEPTH_STENCIL_ATTACHMENT
)
275 for( int i
=0; i
<vg_list_size(formats
); i
++ )
276 if( formats
[i
].e
== e
)
277 return formats
[i
].str
;
283 * Convert OpenGL texture format enums from TexImage2D table 1,2 &
284 * RenderBufferStorage Table 1, into strings
286 VG_STATIC
const char *render_fb_format_str( GLenum format
)
288 struct { GLenum e
; const char *str
; }
292 FB_FORMAT_STR(GL_DEPTH_COMPONENT
)
293 FB_FORMAT_STR(GL_DEPTH_STENCIL
)
294 FB_FORMAT_STR(GL_RED
)
296 FB_FORMAT_STR(GL_RGB
)
297 FB_FORMAT_STR(GL_RGBA
)
299 /* Render buffer formats */
300 FB_FORMAT_STR(GL_DEPTH_COMPONENT16
)
301 FB_FORMAT_STR(GL_DEPTH_COMPONENT24
)
302 FB_FORMAT_STR(GL_DEPTH_COMPONENT32F
)
303 FB_FORMAT_STR(GL_DEPTH24_STENCIL8
)
304 FB_FORMAT_STR(GL_DEPTH32F_STENCIL8
)
305 FB_FORMAT_STR(GL_STENCIL_INDEX8
)
309 FB_FORMAT_STR(GL_R8_SNORM
)
310 FB_FORMAT_STR(GL_R16
)
311 FB_FORMAT_STR(GL_R16_SNORM
)
312 FB_FORMAT_STR(GL_RG8
)
313 FB_FORMAT_STR(GL_RG8_SNORM
)
314 FB_FORMAT_STR(GL_RG16
)
315 FB_FORMAT_STR(GL_RG16_SNORM
)
316 FB_FORMAT_STR(GL_R3_G3_B2
)
317 FB_FORMAT_STR(GL_RGB4
)
318 FB_FORMAT_STR(GL_RGB5
)
319 FB_FORMAT_STR(GL_RGB8
)
320 FB_FORMAT_STR(GL_RGB8_SNORM
)
321 FB_FORMAT_STR(GL_RGB10
)
322 FB_FORMAT_STR(GL_RGB12
)
323 FB_FORMAT_STR(GL_RGB16_SNORM
)
324 FB_FORMAT_STR(GL_RGBA2
)
325 FB_FORMAT_STR(GL_RGBA4
)
326 FB_FORMAT_STR(GL_RGB5_A1
)
327 FB_FORMAT_STR(GL_RGBA8
)
328 FB_FORMAT_STR(GL_RGBA8_SNORM
)
329 FB_FORMAT_STR(GL_RGB10_A2
)
330 FB_FORMAT_STR(GL_RGB10_A2UI
)
331 FB_FORMAT_STR(GL_RGBA12
)
332 FB_FORMAT_STR(GL_RGBA16
)
333 FB_FORMAT_STR(GL_SRGB8
)
334 FB_FORMAT_STR(GL_SRGB8_ALPHA8
)
335 FB_FORMAT_STR(GL_R16F
)
336 FB_FORMAT_STR(GL_RG16F
)
337 FB_FORMAT_STR(GL_RGB16F
)
338 FB_FORMAT_STR(GL_RGBA16F
)
339 FB_FORMAT_STR(GL_R32F
)
340 FB_FORMAT_STR(GL_RG32F
)
341 FB_FORMAT_STR(GL_RGB32F
)
342 FB_FORMAT_STR(GL_RGBA32F
)
343 FB_FORMAT_STR(GL_R11F_G11F_B10F
)
344 FB_FORMAT_STR(GL_RGB9_E5
)
345 FB_FORMAT_STR(GL_R8I
)
346 FB_FORMAT_STR(GL_R8UI
)
347 FB_FORMAT_STR(GL_R16I
)
348 FB_FORMAT_STR(GL_R16UI
)
349 FB_FORMAT_STR(GL_R32I
)
350 FB_FORMAT_STR(GL_R32UI
)
351 FB_FORMAT_STR(GL_RG8I
)
352 FB_FORMAT_STR(GL_RG8UI
)
353 FB_FORMAT_STR(GL_RG16I
)
354 FB_FORMAT_STR(GL_RG16UI
)
355 FB_FORMAT_STR(GL_RG32I
)
356 FB_FORMAT_STR(GL_RG32UI
)
357 FB_FORMAT_STR(GL_RGB8I
)
358 FB_FORMAT_STR(GL_RGB8UI
)
359 FB_FORMAT_STR(GL_RGB16I
)
360 FB_FORMAT_STR(GL_RGB16UI
)
361 FB_FORMAT_STR(GL_RGB32I
)
362 FB_FORMAT_STR(GL_RGB32UI
)
363 FB_FORMAT_STR(GL_RGBA8I
)
364 FB_FORMAT_STR(GL_RGBA8UI
)
365 FB_FORMAT_STR(GL_RGBA16I
)
366 FB_FORMAT_STR(GL_RGBA16UI
)
367 FB_FORMAT_STR(GL_RGBA32I
)
368 FB_FORMAT_STR(GL_RGBA32UI
)
371 for( int i
=0; i
<vg_list_size(formats
); i
++ )
372 if( formats
[i
].e
== format
)
373 return formats
[i
].str
;
379 * Bind and allocate texture for framebuffer attachment
381 VG_STATIC
void render_fb_allocate_texture( struct framebuffer
*fb
,
382 struct framebuffer_attachment
*a
)
385 render_fb_get_current_res( fb
, &rx
, &ry
);
387 if( a
->purpose
== k_framebuffer_attachment_type_renderbuffer
){
388 glBindRenderbuffer( GL_RENDERBUFFER
, a
->id
);
389 glRenderbufferStorage( GL_RENDERBUFFER
, a
->internalformat
, rx
, ry
);
391 else if( a
->purpose
== k_framebuffer_attachment_type_colour
){
392 glBindTexture( GL_TEXTURE_2D
, a
->id
);
393 glTexImage2D( GL_TEXTURE_2D
, 0, a
->internalformat
, rx
, ry
,
394 0, a
->format
, a
->type
, NULL
);
399 * Full allocation of a framebuffer
401 VG_STATIC
void render_fb_allocate( struct framebuffer
*fb
)
403 glGenFramebuffers( 1, &fb
->fb
);
404 glBindFramebuffer( GL_FRAMEBUFFER
, fb
->fb
);
407 render_fb_get_current_res( fb
, &rx
, &ry
);
409 vg_info( "allocate_framebuffer( %s, %dx%d )\n", fb
->display_name
, rx
, ry
);
412 GLenum colour_attachments
[4];
413 u32 colour_count
= 0;
415 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ ){
416 struct framebuffer_attachment
*attachment
= &fb
->attachments
[j
];
418 if( attachment
->purpose
== k_framebuffer_attachment_type_none
)
421 vg_info( " %s: %s\n",
422 render_fb_attachment_str( attachment
->attachment
),
423 render_fb_format_str( attachment
->internalformat
) );
425 if( attachment
->purpose
== k_framebuffer_attachment_type_renderbuffer
){
426 glGenRenderbuffers( 1, &attachment
->id
);
427 render_fb_allocate_texture( fb
, attachment
);
428 glFramebufferRenderbuffer( GL_FRAMEBUFFER
,
429 GL_DEPTH_STENCIL_ATTACHMENT
,
430 GL_RENDERBUFFER
, attachment
->id
);
432 else if( attachment
->purpose
== k_framebuffer_attachment_type_colour
){
433 glGenTextures( 1, &attachment
->id
);
434 render_fb_allocate_texture( fb
, attachment
);
435 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
436 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
437 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
438 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
440 glFramebufferTexture2D( GL_FRAMEBUFFER
, attachment
->attachment
,
441 GL_TEXTURE_2D
, attachment
->id
, 0 );
443 colour_attachments
[ colour_count
++ ] = attachment
->attachment
;
447 glDrawBuffers( colour_count
, colour_attachments
);
452 GLenum result
= glCheckFramebufferStatus( GL_FRAMEBUFFER
);
454 if( result
== GL_FRAMEBUFFER_COMPLETE
){
456 * Attatch to gpipeline
461 vg_success( " status: complete\n" );
465 if( result
== GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
)
466 vg_error( " status: Incomplete attachment" );
467 else if( result
== GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
)
468 vg_error( " status: Missing attachment" );
469 else if( result
== GL_FRAMEBUFFER_UNSUPPORTED
)
470 vg_error( " status: Unsupported framebuffer format" );
472 vg_error( " status: Generic Error" );
475 vg_fatal_exit_loop( "Incomplete framebuffer (see logs)" );
480 * Resize/Update all framebuffers(we know about)
482 VG_STATIC
void render_fb_resize(void)
484 if( !gpipeline
.ready
)
487 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ ){
488 struct framebuffer
*fb
= &framebuffers
[i
];
489 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ ){
490 struct framebuffer_attachment
*attachment
= &fb
->attachments
[j
];
491 render_fb_allocate_texture( fb
, attachment
);
496 VG_STATIC
int render_framebuffer_control( int argc
, char const *argv
[] );
497 VG_STATIC
void render_framebuffer_poll( int argc
, char const *argv
[] );
498 VG_STATIC
void render_init_fs_quad(void)
500 vg_info( "[render] Allocate quad\n" );
503 0.00f
,0.00f
, 1.00f
,1.00f
, 0.00f
,1.00f
,
504 0.00f
,0.00f
, 1.00f
,0.00f
, 1.00f
,1.00f
,
506 0.20f
,0.00f
, 0.80f
,1.00f
, 0.20f
,1.00f
,
507 0.20f
,0.00f
, 0.80f
,0.00f
, 0.80f
,1.00f
,
511 0.00f
,0.00f
, 0.30f
,0.30f
, 0.00f
,0.30f
,
512 0.00f
,0.00f
, 0.30f
,0.00f
, 0.30f
,0.30f
,
513 0.30f
,0.00f
, 0.60f
,0.30f
, 0.30f
,0.30f
,
514 0.30f
,0.00f
, 0.60f
,0.00f
, 0.60f
,0.30f
,
515 0.60f
,0.00f
, 0.90f
,0.30f
, 0.60f
,0.30f
,
516 0.60f
,0.00f
, 0.90f
,0.00f
, 0.90f
,0.30f
,
518 0.00f
,0.30f
, 0.30f
,0.60f
, 0.00f
,0.60f
,
519 0.00f
,0.30f
, 0.30f
,0.30f
, 0.30f
,0.60f
,
520 0.30f
,0.30f
, 0.60f
,0.60f
, 0.30f
,0.60f
,
521 0.30f
,0.30f
, 0.60f
,0.30f
, 0.60f
,0.60f
,
522 0.60f
,0.30f
, 0.90f
,0.60f
, 0.60f
,0.60f
,
523 0.60f
,0.30f
, 0.90f
,0.30f
, 0.90f
,0.60f
,
525 0.00f
,0.60f
, 0.30f
,0.90f
, 0.00f
,0.90f
,
526 0.00f
,0.60f
, 0.30f
,0.60f
, 0.30f
,0.90f
,
527 0.30f
,0.60f
, 0.60f
,0.90f
, 0.30f
,0.90f
,
528 0.30f
,0.60f
, 0.60f
,0.60f
, 0.60f
,0.90f
,
529 0.60f
,0.60f
, 0.90f
,0.90f
, 0.60f
,0.90f
,
530 0.60f
,0.60f
, 0.90f
,0.60f
, 0.90f
,0.90f
,
533 vg_console_reg_cmd( "fb", render_framebuffer_control
,
534 render_framebuffer_poll
);
536 glGenVertexArrays( 1, &gpipeline
.fsquad
.vao
);
537 glGenBuffers( 1, &gpipeline
.fsquad
.vbo
);
538 glBindVertexArray( gpipeline
.fsquad
.vao
);
539 glBindBuffer( GL_ARRAY_BUFFER
, gpipeline
.fsquad
.vbo
);
540 glBufferData( GL_ARRAY_BUFFER
, sizeof(quad
), quad
, GL_STATIC_DRAW
);
541 glBindVertexArray( gpipeline
.fsquad
.vao
);
542 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
,
543 sizeof(float)*2, (void*)0 );
544 glEnableVertexAttribArray( 0 );
549 VG_STATIC
void render_init(void)
551 shader_blit_register();
552 shader_blitblur_register();
553 shader_blitcolour_register();
555 vg_acquire_thread_sync();
558 * Complete Framebuffers
560 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ ){
561 struct framebuffer
*fb
= &framebuffers
[i
];
562 render_fb_allocate( fb
);
565 render_init_fs_quad();
567 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
571 vg_release_thread_sync();
577 VG_STATIC
void render_fsquad(void)
579 glBindVertexArray( gpipeline
.fsquad
.vao
);
580 glDrawArrays( GL_TRIANGLES
, 0, 6 );
583 VG_STATIC
void render_fsquad1(void)
585 glBindVertexArray( gpipeline
.fsquad
.vao
);
586 glDrawArrays( GL_TRIANGLES
, 6, 6 );
590 * Call this inside the UI function
592 VG_STATIC
void render_view_framebuffer_ui(void)
594 int viewing_count
= 0;
596 glBindVertexArray( gpipeline
.fsquad
.vao
);
598 shader_blit_uTexMain( 0 );
600 v2f identity
= { 1.0f
, 1.0f
};
601 shader_blit_uInverseRatio( identity
);
603 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ ){
604 struct framebuffer
*fb
= &framebuffers
[i
];
606 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ ){
607 struct framebuffer_attachment
*at
= &fb
->attachments
[j
];
609 if( !at
->debug_view
)
613 window
= { vg
.window_x
, vg
.window_y
};
615 corner
[0] = viewing_count
% 3;
616 corner
[1] = 1 + (viewing_count
/ 3);
617 v2_mul( corner
, window
, corner
);
618 v2_muls( corner
, 0.3f
, corner
);
619 corner
[1] = vg
.window_y
- corner
[1];
621 ui_text( (ui_rect
){ corner
[0], corner
[1], 0.0f
, 0.0f
},
622 fb
->display_name
, 2, k_text_align_left
);
623 ui_text( (ui_rect
){ corner
[0], corner
[1] + 32, 0.0f
, 0.0f
, },
624 at
->display_name
, 1, k_text_align_left
);
626 if( at
->purpose
== k_framebuffer_attachment_type_renderbuffer
){
628 v2_muladds( corner
, window
, 0.15f
, center
);
630 ui_text( (ui_rect
){ center
[0], center
[1], 0.0f
, 0.0f
},
631 "<hardware texture>", 1, k_text_align_center
);
634 render_fb_bind_texture( fb
, j
, 0 );
636 int start
= (viewing_count
+2) * 6,
638 glDrawArrays( GL_TRIANGLES
, start
, count
);
646 VG_STATIC
void render_framebuffer_show( struct framebuffer
*fb
,
647 struct framebuffer_attachment
*at
,
650 at
->debug_view
= operation
;
651 vg_info( "%s %s:%s\n", (operation
?"shown": "hidden"),
652 fb
->display_name
, at
->display_name
);
656 * arg0: command "show"/"hide"
657 * arg1: framebuffer name <name>/"all"
658 * arg2: subname <name>/none
660 VG_STATIC
int render_framebuffer_control( int argc
, char const *argv
[] )
663 vg_error( "Usage: fb \"show/hide\" <name>/\"all\" <name>/none\n" );
670 if( !strcmp( argv
[0], "show" ) )
672 else if( !strcmp( argv
[0], "hide" ) )
675 vg_error( "Unknown framebuffer operation: '%s'\n", argv
[0] );
679 if( !strcmp( argv
[1], "all" ) )
682 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ ){
683 struct framebuffer
*fb
= &framebuffers
[i
];
685 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ ){
686 struct framebuffer_attachment
*at
= &fb
->attachments
[j
];
688 if( at
->purpose
== k_framebuffer_attachment_type_none
)
692 render_framebuffer_show( fb
, at
, operation
);
695 if( !strcmp( fb
->display_name
, argv
[1] ) ){
697 render_framebuffer_show( fb
, at
, operation
);
698 else if( !strcmp( at
->display_name
, argv
[2] ) )
699 render_framebuffer_show( fb
, at
, operation
);
708 VG_STATIC
void render_framebuffer_poll( int argc
, char const *argv
[] )
710 const char *term
= argv
[argc
-1];
713 console_suggest_score_text( "show", term
, 0 );
714 console_suggest_score_text( "hide", term
, 0 );
716 else if( argc
== 2 ){
717 console_suggest_score_text( "all", term
, 0 );
719 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ ){
720 struct framebuffer
*fb
= &framebuffers
[i
];
721 console_suggest_score_text( fb
->display_name
, term
, 0 );
724 else if( argc
== 3 ){
727 if( !strcmp( argv
[1], "all" ) )
730 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ ){
731 struct framebuffer
*fb
= &framebuffers
[i
];
733 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ ){
734 struct framebuffer_attachment
*at
= &fb
->attachments
[j
];
736 if( at
->purpose
== k_framebuffer_attachment_type_none
)
740 console_suggest_score_text( at
->display_name
, term
, 0 );
742 else if( !strcmp( fb
->display_name
, argv
[1] ) ){
743 console_suggest_score_text( at
->display_name
, term
, 0 );
750 #endif /* RENDER_H */