render to framebuffer

This commit is contained in:
CDaut 2025-09-30 23:01:40 +02:00
parent 3953ef2bfb
commit ad6722fd3c
Signed by: clara
GPG key ID: 223391B52FAD4463
4 changed files with 102 additions and 3 deletions

7
raytracer/Cargo.lock generated
View file

@ -20,6 +20,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "libc"
version = "0.2.176"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58f929b4d672ea937a23a1ab494143d968337a5f47e56d0815df1e0890ddf174"
[[package]]
name = "log"
version = "0.4.28"
@ -68,6 +74,7 @@ dependencies = [
name = "raytracer"
version = "0.1.0"
dependencies = [
"libc",
"log",
"uefi",
]

View file

@ -4,5 +4,6 @@ version = "0.1.0"
edition = "2024"
[dependencies]
libc = "0.2.176"
log = "0.4.28"
uefi = { version = "0.35.0", features = ["logger", "panic_handler"] }

55
raytracer/src/buffer.rs Normal file
View file

@ -0,0 +1,55 @@
use uefi::proto::console::gop::{BltOp, BltPixel, BltRegion, GraphicsOutput};
extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;
use uefi::Error;
pub struct Buffer {
width: usize,
height: usize,
pixels: Vec<BltPixel>,
}
impl Buffer {
/// Create a new `Buffer`.
pub fn new(width: usize, height: usize) -> Self {
Buffer {
width,
height,
pixels: vec![BltPixel::new(0, 0, 0); width * height],
}
}
/// Get a single pixel.
pub fn pixel(&mut self, x: usize, y: usize) -> Option<&mut BltPixel> {
self.pixels.get_mut(y * self.width + x)
}
/// Blit the buffer to the framebuffer.
pub fn blit(&self, gop: &mut GraphicsOutput) -> Result<(), Error> {
gop.blt(BltOp::BufferToVideo {
buffer: &self.pixels,
src: BltRegion::Full,
dest: (0, 0),
dims: (self.width, self.height),
})
}
/// Update only a pixel to the framebuffer.
fn blit_pixel(
&self,
gop: &mut GraphicsOutput,
coords: (usize, usize),
) -> Result<(), Error> {
gop.blt(BltOp::BufferToVideo {
buffer: &self.pixels,
src: BltRegion::SubRectangle {
coords,
px_stride: self.width,
},
dest: coords,
dims: (1, 1),
})
}
}

View file

@ -1,13 +1,49 @@
#![no_main]
#![no_std]
use log::info;
mod buffer;
extern crate alloc;
use uefi::allocator::Allocator;
use uefi::prelude::*;
use uefi::proto::console::gop::GraphicsOutput;
use uefi::{boot, Result};
use crate::buffer::Buffer;
#[global_allocator]
static ALLOCATOR: Allocator = uefi::allocator::Allocator;
fn clear_buffer() -> Result<()>{
// Open graphics output protocol.
let gop_handle = boot::get_handle_for_protocol::<GraphicsOutput>()?;
let mut gop = boot::open_protocol_exclusive::<GraphicsOutput>(gop_handle)?;
// Create a buffer to draw into.
let (width, height) = gop.current_mode_info().resolution();
let mut buffer = Buffer::new(width, height);
// Initialize the buffer with a simple gradient background.
for y in 0..height {
let r = ((y as f32) / ((height - 1) as f32)) * 255.0;
for x in 0..width {
let g = ((x as f32) / ((width - 1) as f32)) * 255.0;
let pixel = buffer.pixel(x, y).unwrap();
pixel.red = r as u8;
pixel.green = g as u8;
pixel.blue = r as u8;
}
}
// Draw background.
buffer.blit(&mut gop)?;
Ok(())
}
#[entry]
fn main() -> Status {
uefi::helpers::init().unwrap();
info!("Hello world!");
boot::stall(10_000_000);
clear_buffer().unwrap();
Status::SUCCESS
}