From 915dd4b023d0826166d4887100dbed64d46821a9 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 30 Jun 2017 15:02:26 +0200 Subject: playing with Z buffering in VGA driver --- src/drivers/video/vga.c | 21 +++++++++++++++++---- src/drivers/video/vga.h | 9 +++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/video/vga.c b/src/drivers/video/vga.c index 9d540af..bcd4366 100644 --- a/src/drivers/video/vga.c +++ b/src/drivers/video/vga.c @@ -253,7 +253,7 @@ bool vga_set_mode( vga_t *vga, const vga_mode_t mode ) vga->mode = mode; vga->mode.segment = get_frame_buffer_segment( vga ); - vga->use_z_buffer = false; + vga_use_z_buffer( vga, false ); return true; } @@ -317,7 +317,7 @@ void vga_set_pixel( vga_t *vga, const int x, const int y, const vga_color_t colo uint8_t color_idx = get_color_index( color ); - uint8_t *addr = vga->mode.segment + vga->mode.x * y + x; + uint8_t *addr = vga->base_addr + vga->mode.x * y + x; *addr = color_idx; } @@ -337,14 +337,14 @@ void vga_draw_rectangle( vga_t *vga, const int x, const int y, const int w, cons uint8_t color_idx = get_color_index( color ); for( int yy = y; yy < y + h; yy++ ) { - uint8_t *addr = vga->mode.segment + vga->mode.x * yy + x; + uint8_t *addr = vga->base_addr + vga->mode.x * yy + x; memset( addr, color_idx, w ); } } void vga_clear_screen( vga_t *vga, const vga_color_t color ) { - memset( vga->mode.segment, get_color_index( color ), + memset( vga->base_addr, get_color_index( color ), vga->mode.x * vga->mode.y ); } @@ -385,6 +385,19 @@ void vga_wait_for_retrace( vga_t *vga ) void vga_use_z_buffer( vga_t *vga, bool use ) { vga->use_z_buffer = use; + + if( vga->use_z_buffer ) { + vga->base_addr = &vga->zbuffer[0]; + } else { + vga->base_addr = vga->mode.segment; + } } +void vga_refresh( vga_t *vga ) +{ + if( vga->use_z_buffer ) { + vga_wait_for_retrace( vga ); + memcpy( vga->mode.segment, vga->zbuffer, 65535 ); + } +} diff --git a/src/drivers/video/vga.h b/src/drivers/video/vga.h index 0f55f74..d15a55d 100644 --- a/src/drivers/video/vga.h +++ b/src/drivers/video/vga.h @@ -43,6 +43,14 @@ typedef struct { vga_mode_t mode; void *context; bool use_z_buffer; + // TODO: the Z-buffer should actually be allocated dynamically + // depending on the current mode, for now it's a static buffer + // able to store the biggest resolution, being planar + // 320x200x8 aka 64k + uint8_t zbuffer[65535]; + // stores either the address to the beginning of the segment + // (real mapped I/O memory or the beginning of the Z buffer + uint8_t *base_addr; } vga_t; typedef struct { @@ -83,5 +91,6 @@ void vga_clear_screen( vga_t *vga, const vga_color_t color ); void vga_draw_char( vga_t *vga, const unsigned char c, const int x, const int y, const vga_color_t background, const vga_color_t foreground ); void vga_wait_for_retrace( vga_t *vga ); void vga_use_z_buffer( vga_t *vga, bool use ); +void vga_refresh( vga_t *vga ); #endif // VGA_H -- cgit v1.2.3-54-g00ecf