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
;
33 * All standard buffers used in rendering
35 VG_STATIC
struct pipeline
51 float shadow_spread
, shadow_length
;
61 .colour
= { 1.36f
, 1.35f
, 1.01f
},
62 .dir
= { 0.63f
, -0.08f
}
66 .colour
= { 0.33f
, 0.56f
, 0.64f
},
67 .dir
= { -2.60f
, -0.13f
}
71 .colour
= { 0.05f
, 0.05f
, 0.23f
},
72 .dir
= { 2.60f
, -0.84f
}
75 .shadow_spread
= 0.65f
,
76 .shadow_length
= 9.50f
,
81 const char *display_name
;
86 struct framebuffer_attachment
88 const char *display_name
;
90 enum framebuffer_attachment_type
92 k_framebuffer_attachment_type_none
,
93 k_framebuffer_attachment_type_colour
,
94 k_framebuffer_attachment_type_renderbuffer
98 enum framebuffer_quality_profile
100 k_framebuffer_quality_all
,
101 k_framebuffer_quality_high_only
105 GLenum internalformat
,
123 * The primary draw target
126 .link
= &gpipeline
.fb_main
,
131 "colour", k_framebuffer_attachment_type_colour
,
133 .internalformat
= GL_RGB
,
135 .type
= GL_UNSIGNED_BYTE
,
136 .attachment
= GL_COLOR_ATTACHMENT0
139 "motion", k_framebuffer_attachment_type_colour
,
141 .quality
= k_framebuffer_quality_high_only
,
142 .internalformat
= GL_RG16F
,
145 .attachment
= GL_COLOR_ATTACHMENT1
148 "depth_stencil", k_framebuffer_attachment_type_renderbuffer
,
150 .internalformat
= GL_DEPTH24_STENCIL8
,
151 .attachment
= GL_DEPTH_STENCIL_ATTACHMENT
157 * Second rendered view from the perspective of the water reflection
160 .link
= &gpipeline
.fb_water_reflection
,
165 "colour", k_framebuffer_attachment_type_colour
,
166 .internalformat
= GL_RGB
,
168 .type
= GL_UNSIGNED_BYTE
,
169 .attachment
= GL_COLOR_ATTACHMENT0
172 "depth_stencil", k_framebuffer_attachment_type_renderbuffer
,
174 .internalformat
= GL_DEPTH24_STENCIL8
,
175 .attachment
= GL_DEPTH_STENCIL_ATTACHMENT
181 * Thid rendered view from the perspective of the camera, but just
182 * captures stuff thats under the water
185 .link
= &gpipeline
.fb_water_beneath
,
190 "colour", k_framebuffer_attachment_type_colour
,
191 .internalformat
= GL_RED
,
193 .type
= GL_UNSIGNED_BYTE
,
194 .attachment
= GL_COLOR_ATTACHMENT0
197 "depth_stencil", k_framebuffer_attachment_type_renderbuffer
,
199 .internalformat
= GL_DEPTH24_STENCIL8
,
200 .attachment
= GL_DEPTH_STENCIL_ATTACHMENT
207 * Get the current (automatically scaled or fixed) resolution of framebuffer
209 VG_STATIC
void render_fb_get_current_res( struct framebuffer
*fb
,
212 if( fb
->resolution_div
)
214 *x
= vg
.window_x
/ fb
->resolution_div
;
215 *y
= vg
.window_y
/ fb
->resolution_div
;
225 * Bind framebuffer for drawing to
227 VG_STATIC
void render_fb_bind( framebuffer
*fb
)
230 render_fb_get_current_res( fb
, &x
, &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
)
245 vg_fatal_exit_loop( "illegal operation: bind non-colour framebuffer"
246 " attachment to texture slot" );
249 glActiveTexture( GL_TEXTURE0
+ slot
);
250 glBindTexture( GL_TEXTURE_2D
, fb
->attachments
[attachment
].id
);
258 #define FB_FORMAT_STR( E ) { E, #E },
261 * Convert OpenGL attachment ID enum to string
263 VG_STATIC
const char *render_fb_attachment_str( GLenum e
)
265 struct { GLenum e
; const char *str
; }
268 FB_FORMAT_STR(GL_COLOR_ATTACHMENT0
)
269 FB_FORMAT_STR(GL_COLOR_ATTACHMENT1
)
270 FB_FORMAT_STR(GL_COLOR_ATTACHMENT2
)
271 FB_FORMAT_STR(GL_COLOR_ATTACHMENT3
)
272 FB_FORMAT_STR(GL_COLOR_ATTACHMENT4
)
273 FB_FORMAT_STR(GL_DEPTH_STENCIL_ATTACHMENT
)
276 for( int i
=0; i
<vg_list_size(formats
); i
++ )
277 if( formats
[i
].e
== e
)
278 return formats
[i
].str
;
284 * Convert OpenGL texture format enums from TexImage2D table 1,2 &
285 * RenderBufferStorage Table 1, into strings
287 VG_STATIC
const char *render_fb_format_str( GLenum format
)
289 struct { GLenum e
; const char *str
; }
293 FB_FORMAT_STR(GL_DEPTH_COMPONENT
)
294 FB_FORMAT_STR(GL_DEPTH_STENCIL
)
295 FB_FORMAT_STR(GL_RED
)
297 FB_FORMAT_STR(GL_RGB
)
298 FB_FORMAT_STR(GL_RGBA
)
300 /* Render buffer formats */
301 FB_FORMAT_STR(GL_DEPTH_COMPONENT16
)
302 FB_FORMAT_STR(GL_DEPTH_COMPONENT24
)
303 FB_FORMAT_STR(GL_DEPTH_COMPONENT32F
)
304 FB_FORMAT_STR(GL_DEPTH24_STENCIL8
)
305 FB_FORMAT_STR(GL_DEPTH32F_STENCIL8
)
306 FB_FORMAT_STR(GL_STENCIL_INDEX8
)
310 FB_FORMAT_STR(GL_R8_SNORM
)
311 FB_FORMAT_STR(GL_R16
)
312 FB_FORMAT_STR(GL_R16_SNORM
)
313 FB_FORMAT_STR(GL_RG8
)
314 FB_FORMAT_STR(GL_RG8_SNORM
)
315 FB_FORMAT_STR(GL_RG16
)
316 FB_FORMAT_STR(GL_RG16_SNORM
)
317 FB_FORMAT_STR(GL_R3_G3_B2
)
318 FB_FORMAT_STR(GL_RGB4
)
319 FB_FORMAT_STR(GL_RGB5
)
320 FB_FORMAT_STR(GL_RGB8
)
321 FB_FORMAT_STR(GL_RGB8_SNORM
)
322 FB_FORMAT_STR(GL_RGB10
)
323 FB_FORMAT_STR(GL_RGB12
)
324 FB_FORMAT_STR(GL_RGB16_SNORM
)
325 FB_FORMAT_STR(GL_RGBA2
)
326 FB_FORMAT_STR(GL_RGBA4
)
327 FB_FORMAT_STR(GL_RGB5_A1
)
328 FB_FORMAT_STR(GL_RGBA8
)
329 FB_FORMAT_STR(GL_RGBA8_SNORM
)
330 FB_FORMAT_STR(GL_RGB10_A2
)
331 FB_FORMAT_STR(GL_RGB10_A2UI
)
332 FB_FORMAT_STR(GL_RGBA12
)
333 FB_FORMAT_STR(GL_RGBA16
)
334 FB_FORMAT_STR(GL_SRGB8
)
335 FB_FORMAT_STR(GL_SRGB8_ALPHA8
)
336 FB_FORMAT_STR(GL_R16F
)
337 FB_FORMAT_STR(GL_RG16F
)
338 FB_FORMAT_STR(GL_RGB16F
)
339 FB_FORMAT_STR(GL_RGBA16F
)
340 FB_FORMAT_STR(GL_R32F
)
341 FB_FORMAT_STR(GL_RG32F
)
342 FB_FORMAT_STR(GL_RGB32F
)
343 FB_FORMAT_STR(GL_RGBA32F
)
344 FB_FORMAT_STR(GL_R11F_G11F_B10F
)
345 FB_FORMAT_STR(GL_RGB9_E5
)
346 FB_FORMAT_STR(GL_R8I
)
347 FB_FORMAT_STR(GL_R8UI
)
348 FB_FORMAT_STR(GL_R16I
)
349 FB_FORMAT_STR(GL_R16UI
)
350 FB_FORMAT_STR(GL_R32I
)
351 FB_FORMAT_STR(GL_R32UI
)
352 FB_FORMAT_STR(GL_RG8I
)
353 FB_FORMAT_STR(GL_RG8UI
)
354 FB_FORMAT_STR(GL_RG16I
)
355 FB_FORMAT_STR(GL_RG16UI
)
356 FB_FORMAT_STR(GL_RG32I
)
357 FB_FORMAT_STR(GL_RG32UI
)
358 FB_FORMAT_STR(GL_RGB8I
)
359 FB_FORMAT_STR(GL_RGB8UI
)
360 FB_FORMAT_STR(GL_RGB16I
)
361 FB_FORMAT_STR(GL_RGB16UI
)
362 FB_FORMAT_STR(GL_RGB32I
)
363 FB_FORMAT_STR(GL_RGB32UI
)
364 FB_FORMAT_STR(GL_RGBA8I
)
365 FB_FORMAT_STR(GL_RGBA8UI
)
366 FB_FORMAT_STR(GL_RGBA16I
)
367 FB_FORMAT_STR(GL_RGBA16UI
)
368 FB_FORMAT_STR(GL_RGBA32I
)
369 FB_FORMAT_STR(GL_RGBA32UI
)
372 for( int i
=0; i
<vg_list_size(formats
); i
++ )
373 if( formats
[i
].e
== format
)
374 return formats
[i
].str
;
380 * Bind and allocate texture for framebuffer attachment
382 VG_STATIC
void render_fb_allocate_texture( struct framebuffer
*fb
,
383 struct framebuffer_attachment
*a
)
386 render_fb_get_current_res( fb
, &rx
, &ry
);
388 if( a
->purpose
== k_framebuffer_attachment_type_renderbuffer
)
390 glBindRenderbuffer( GL_RENDERBUFFER
, a
->id
);
391 glRenderbufferStorage( GL_RENDERBUFFER
, GL_DEPTH24_STENCIL8
, rx
, ry
);
393 else if( a
->purpose
== k_framebuffer_attachment_type_colour
)
395 glBindTexture( GL_TEXTURE_2D
, a
->id
);
396 glTexImage2D( GL_TEXTURE_2D
, 0, a
->internalformat
, rx
, ry
,
397 0, a
->format
, a
->type
, NULL
);
402 * Full allocation of a framebuffer
404 VG_STATIC
void render_fb_allocate( struct framebuffer
*fb
)
406 glGenFramebuffers( 1, &fb
->fb
);
407 glBindFramebuffer( GL_FRAMEBUFFER
, fb
->fb
);
410 render_fb_get_current_res( fb
, &rx
, &ry
);
412 vg_info( "allocate_framebuffer( %s, %dx%d )\n", fb
->display_name
, rx
, ry
);
415 GLenum colour_attachments
[4];
416 u32 colour_count
= 0;
418 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ )
420 struct framebuffer_attachment
*attachment
= &fb
->attachments
[j
];
422 if( attachment
->purpose
== k_framebuffer_attachment_type_none
)
425 vg_info( " %s: %s\n",
426 render_fb_attachment_str( attachment
->attachment
),
427 render_fb_format_str( attachment
->internalformat
) );
429 if( attachment
->purpose
== k_framebuffer_attachment_type_renderbuffer
)
431 glGenRenderbuffers( 1, &attachment
->id
);
432 render_fb_allocate_texture( fb
, attachment
);
433 glFramebufferRenderbuffer( GL_FRAMEBUFFER
,
434 GL_DEPTH_STENCIL_ATTACHMENT
,
435 GL_RENDERBUFFER
, attachment
->id
);
437 else if( attachment
->purpose
== k_framebuffer_attachment_type_colour
)
439 glGenTextures( 1, &attachment
->id
);
440 render_fb_allocate_texture( fb
, attachment
);
441 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
442 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
443 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
444 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
446 glFramebufferTexture2D( GL_FRAMEBUFFER
, attachment
->attachment
,
447 GL_TEXTURE_2D
, attachment
->id
, 0 );
449 colour_attachments
[ colour_count
++ ] = attachment
->attachment
;
453 glDrawBuffers( colour_count
, colour_attachments
);
458 GLenum result
= glCheckFramebufferStatus( GL_FRAMEBUFFER
);
460 if( result
== GL_FRAMEBUFFER_COMPLETE
)
463 * Attatch to gpipeline
468 vg_success( " status: complete\n" );
473 if( result
== GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
)
474 vg_error( " status: Incomplete attachment" );
475 else if( result
== GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
)
476 vg_error( " status: Missing attachment" );
477 else if( result
== GL_FRAMEBUFFER_UNSUPPORTED
)
478 vg_error( " status: Unsupported framebuffer format" );
480 vg_error( " status: Generic Error" );
483 vg_fatal_exit_loop( "Incomplete framebuffer (see logs)" );
488 * Resize/Update all framebuffers(we know about)
490 VG_STATIC
void render_fb_resize(void)
492 if( !gpipeline
.ready
)
495 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
497 struct framebuffer
*fb
= &framebuffers
[i
];
498 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ )
500 struct framebuffer_attachment
*attachment
= &fb
->attachments
[j
];
501 render_fb_allocate_texture( fb
, attachment
);
506 VG_STATIC
int render_framebuffer_control( int argc
, char const *argv
[] );
507 VG_STATIC
void render_framebuffer_poll( int argc
, char const *argv
[] );
508 VG_STATIC
void render_init_fs_quad(void)
510 vg_info( "[render] Allocate quad\n" );
514 0.00f
,0.00f
, 1.00f
,1.00f
, 0.00f
,1.00f
,
515 0.00f
,0.00f
, 1.00f
,0.00f
, 1.00f
,1.00f
,
517 0.20f
,0.00f
, 0.80f
,1.00f
, 0.20f
,1.00f
,
518 0.20f
,0.00f
, 0.80f
,0.00f
, 0.80f
,1.00f
,
522 0.00f
,0.00f
, 0.30f
,0.30f
, 0.00f
,0.30f
,
523 0.00f
,0.00f
, 0.30f
,0.00f
, 0.30f
,0.30f
,
524 0.30f
,0.00f
, 0.60f
,0.30f
, 0.30f
,0.30f
,
525 0.30f
,0.00f
, 0.60f
,0.00f
, 0.60f
,0.30f
,
526 0.60f
,0.00f
, 0.90f
,0.30f
, 0.60f
,0.30f
,
527 0.60f
,0.00f
, 0.90f
,0.00f
, 0.90f
,0.30f
,
529 0.00f
,0.30f
, 0.30f
,0.60f
, 0.00f
,0.60f
,
530 0.00f
,0.30f
, 0.30f
,0.30f
, 0.30f
,0.60f
,
531 0.30f
,0.30f
, 0.60f
,0.60f
, 0.30f
,0.60f
,
532 0.30f
,0.30f
, 0.60f
,0.30f
, 0.60f
,0.60f
,
533 0.60f
,0.30f
, 0.90f
,0.60f
, 0.60f
,0.60f
,
534 0.60f
,0.30f
, 0.90f
,0.30f
, 0.90f
,0.60f
,
536 0.00f
,0.60f
, 0.30f
,0.90f
, 0.00f
,0.90f
,
537 0.00f
,0.60f
, 0.30f
,0.60f
, 0.30f
,0.90f
,
538 0.30f
,0.60f
, 0.60f
,0.90f
, 0.30f
,0.90f
,
539 0.30f
,0.60f
, 0.60f
,0.60f
, 0.60f
,0.90f
,
540 0.60f
,0.60f
, 0.90f
,0.90f
, 0.60f
,0.90f
,
541 0.60f
,0.60f
, 0.90f
,0.60f
, 0.90f
,0.90f
,
544 vg_function_push( (struct vg_cmd
)
547 .function
= render_framebuffer_control
,
548 .poll_suggest
= render_framebuffer_poll
551 glGenVertexArrays( 1, &gpipeline
.fsquad
.vao
);
552 glGenBuffers( 1, &gpipeline
.fsquad
.vbo
);
553 glBindVertexArray( gpipeline
.fsquad
.vao
);
554 glBindBuffer( GL_ARRAY_BUFFER
, gpipeline
.fsquad
.vbo
);
555 glBufferData( GL_ARRAY_BUFFER
, sizeof(quad
), quad
, GL_STATIC_DRAW
);
556 glBindVertexArray( gpipeline
.fsquad
.vao
);
557 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
,
558 sizeof(float)*2, (void*)0 );
559 glEnableVertexAttribArray( 0 );
564 VG_STATIC
void render_init(void)
566 shader_blit_register();
567 shader_blitblur_register();
568 shader_blitcolour_register();
570 vg_acquire_thread_sync();
573 * Complete Framebuffers
575 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
577 struct framebuffer
*fb
= &framebuffers
[i
];
578 render_fb_allocate( fb
);
581 render_init_fs_quad();
583 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
587 vg_release_thread_sync();
593 VG_STATIC
void render_fsquad(void)
595 glBindVertexArray( gpipeline
.fsquad
.vao
);
596 glDrawArrays( GL_TRIANGLES
, 0, 6 );
599 VG_STATIC
void render_fsquad1(void)
601 glBindVertexArray( gpipeline
.fsquad
.vao
);
602 glDrawArrays( GL_TRIANGLES
, 6, 6 );
606 * Call this inside the UI function
608 VG_STATIC
void render_view_framebuffer_ui(void)
610 int viewing_count
= 0;
612 glBindVertexArray( gpipeline
.fsquad
.vao
);
614 shader_blit_uTexMain( 0 );
616 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
618 struct framebuffer
*fb
= &framebuffers
[i
];
620 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ )
622 struct framebuffer_attachment
*at
= &fb
->attachments
[j
];
624 if( !at
->debug_view
)
628 window
= { vg
.window_x
, vg
.window_y
};
630 corner
[0] = viewing_count
% 3;
631 corner
[1] = 1 + (viewing_count
/ 3);
632 v2_mul( corner
, window
, corner
);
633 v2_muls( corner
, 0.3f
, corner
);
634 corner
[1] = vg
.window_y
- corner
[1];
636 ui_text( (ui_rect
){ corner
[0], corner
[1], 0.0f
, 0.0f
},
637 fb
->display_name
, 2, k_text_align_left
);
638 ui_text( (ui_rect
){ corner
[0], corner
[1] + 32, 0.0f
, 0.0f
, },
639 at
->display_name
, 1, k_text_align_left
);
641 if( at
->purpose
== k_framebuffer_attachment_type_renderbuffer
)
644 v2_muladds( corner
, window
, 0.15f
, center
);
646 ui_text( (ui_rect
){ center
[0], center
[1], 0.0f
, 0.0f
},
647 "<hardware texture>", 1, k_text_align_center
);
651 render_fb_bind_texture( fb
, j
, 0 );
653 int start
= (viewing_count
+2) * 6,
655 glDrawArrays( GL_TRIANGLES
, start
, count
);
663 VG_STATIC
void render_framebuffer_show( struct framebuffer
*fb
,
664 struct framebuffer_attachment
*at
,
667 at
->debug_view
= operation
;
668 vg_info( "%s %s:%s\n", (operation
?"shown": "hidden"),
669 fb
->display_name
, at
->display_name
);
673 * arg0: command "show"/"hide"
674 * arg1: framebuffer name <name>/"all"
675 * arg2: subname <name>/none
677 VG_STATIC
int render_framebuffer_control( int argc
, char const *argv
[] )
681 vg_error( "Usage: fb \"show/hide\" <name>/\"all\" <name>/none\n" );
688 if( !strcmp( argv
[0], "show" ) )
690 else if( !strcmp( argv
[0], "hide" ) )
694 vg_error( "Unknown framebuffer operation: '%s'\n", argv
[0] );
698 if( !strcmp( argv
[1], "all" ) )
701 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
703 struct framebuffer
*fb
= &framebuffers
[i
];
705 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ )
707 struct framebuffer_attachment
*at
= &fb
->attachments
[j
];
709 if( at
->purpose
== k_framebuffer_attachment_type_none
)
714 render_framebuffer_show( fb
, at
, operation
);
718 if( !strcmp( fb
->display_name
, argv
[1] ) )
721 render_framebuffer_show( fb
, at
, operation
);
722 else if( !strcmp( at
->display_name
, argv
[2] ) )
723 render_framebuffer_show( fb
, at
, operation
);
732 VG_STATIC
void render_framebuffer_poll( int argc
, char const *argv
[] )
734 const char *term
= argv
[argc
-1];
738 console_suggest_score_text( "show", term
, 0 );
739 console_suggest_score_text( "hide", term
, 0 );
743 console_suggest_score_text( "all", term
, 0 );
745 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
747 struct framebuffer
*fb
= &framebuffers
[i
];
748 console_suggest_score_text( fb
->display_name
, term
, 0 );
755 if( !strcmp( argv
[1], "all" ) )
758 for( int i
=0; i
<vg_list_size(framebuffers
); i
++ )
760 struct framebuffer
*fb
= &framebuffers
[i
];
762 for( int j
=0; j
<vg_list_size(fb
->attachments
); j
++ )
764 struct framebuffer_attachment
*at
= &fb
->attachments
[j
];
766 if( at
->purpose
== k_framebuffer_attachment_type_none
)
771 console_suggest_score_text( at
->display_name
, term
, 0 );
773 else if( !strcmp( fb
->display_name
, argv
[1] ) )
775 console_suggest_score_text( at
->display_name
, term
, 0 );
782 #endif /* RENDER_H */