Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions sbr-overlay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,17 @@ impl winit::application::ApplicationHandler for App<'_> {
if let Some(subs) = self.subs.as_ref() {
let surface_texture =
wgpu.surface.get_current_texture().unwrap();
let target = wgpu
let mut target = wgpu
.rasterizer
.target_from_texture(surface_texture.texture.clone());
self.renderer.set_subtitles(Some(subs));

let mut frame_rasterizer = wgpu.rasterizer.begin_frame();
self.renderer
.render_to_wgpu(&mut wgpu.rasterizer, target, &ctx, t)
.render_to(&mut frame_rasterizer, &mut target, &ctx, t)
.unwrap();
frame_rasterizer.end_frame();
self.renderer.end_raster();

surface_texture.present();
}
Expand Down
2 changes: 2 additions & 0 deletions sbr-rasterize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ edition.workspace = true
wgpu = ["dep:wgpu"]

[dependencies]
thiserror = "1"

util = { workspace = true }
wgpu = { workspace = true, optional = true }

Expand Down
1 change: 1 addition & 0 deletions sbr-rasterize/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod color;
mod rasterizer;
pub mod scene;
pub use rasterizer::*;
101 changes: 34 additions & 67 deletions sbr-rasterize/src/rasterizer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{fmt::Write, mem::MaybeUninit};
use std::{any::Any, fmt::Write, mem::MaybeUninit};

use util::math::{Rect2f, Vec2};
use thiserror::Error;
use util::{math::Vec2, AnyError};

use crate::color::BGRA8;
use crate::scene::SceneNode;

pub mod sw;
#[cfg(feature = "wgpu")]
Expand All @@ -24,20 +25,17 @@ impl PixelFormat {
}

enum RenderTargetInner<'a> {
Software(sw::RenderTargetImpl<'a>),
Software(sw::RenderTarget<'a>),
#[cfg(feature = "wgpu")]
Wgpu(Box<wgpu::RenderTargetImpl>),
#[cfg(feature = "wgpu")]
// For zero-sized renders, TODO: move this logic into RenderTargetImpl
WgpuEmpty,
Wgpu(Box<wgpu::RenderTarget>),
}

impl RenderTargetInner<'_> {
fn variant_name(&self) -> &'static str {
match self {
Self::Software(_) => "software",
#[cfg(feature = "wgpu")]
Self::Wgpu(_) | Self::WgpuEmpty => "wgpu",
Self::Wgpu(_) => "wgpu",
}
}
}
Expand All @@ -47,32 +45,26 @@ pub struct RenderTarget<'a>(RenderTargetInner<'a>);
impl RenderTarget<'_> {
pub fn width(&self) -> u32 {
match &self.0 {
// TODO: Make these fields private and have all the impls define accessors for them
RenderTargetInner::Software(sw) => sw.width,
#[cfg(feature = "wgpu")]
RenderTargetInner::Wgpu(wgpu) => wgpu.tex.width(),
RenderTargetInner::Software(sw) => sw.width(),
#[cfg(feature = "wgpu")]
RenderTargetInner::WgpuEmpty => 0,
RenderTargetInner::Wgpu(wgpu) => wgpu.width(),
}
}

pub fn height(&self) -> u32 {
match &self.0 {
// TODO: Make these fields private and have all the impls define accessors for them
RenderTargetInner::Software(sw) => sw.height,
#[cfg(feature = "wgpu")]
RenderTargetInner::Wgpu(wgpu) => wgpu.tex.height(),
RenderTargetInner::Software(sw) => sw.height(),
#[cfg(feature = "wgpu")]
RenderTargetInner::WgpuEmpty => 0,
RenderTargetInner::Wgpu(wgpu) => wgpu.height(),
}
}
}

#[derive(Clone)]
enum TextureInner {
Software(sw::TextureImpl),
Software(sw::Texture),
#[cfg(feature = "wgpu")]
Wgpu(wgpu::TextureImpl),
Wgpu(wgpu::Texture),
}

impl TextureInner {
Expand All @@ -99,15 +91,15 @@ impl Texture {

pub fn width(&self) -> u32 {
match &self.0 {
TextureInner::Software(sw) => sw.width,
TextureInner::Software(sw) => sw.width(),
#[cfg(feature = "wgpu")]
TextureInner::Wgpu(wgpu) => wgpu.width(),
}
}

pub fn height(&self) -> u32 {
match &self.0 {
TextureInner::Software(sw) => sw.height,
TextureInner::Software(sw) => sw.height(),
#[cfg(feature = "wgpu")]
TextureInner::Wgpu(wgpu) => wgpu.height(),
}
Expand All @@ -122,6 +114,16 @@ impl Texture {
}
}

#[derive(Debug, Error)]
enum SceneRenderErrorInner {
#[error(transparent)]
ToBitmaps(AnyError),
}

#[derive(Debug, Error)]
#[error(transparent)]
pub struct SceneRenderError(#[from] SceneRenderErrorInner);

pub trait Rasterizer {
// Used for displaying debug information
fn name(&self) -> &'static str;
Expand Down Expand Up @@ -162,52 +164,17 @@ pub trait Rasterizer {
self.create_texture_mapped(width, height, format, callback)
}

fn create_mono_texture_rendered(&mut self, width: u32, height: u32) -> RenderTarget<'static>;
fn finalize_texture_render(&mut self, target: RenderTarget<'static>) -> Texture;
fn blur_texture(&mut self, texture: &Texture, blur_sigma: f32) -> BlurOutput;

fn horizontal_line(
fn render_scene(
&mut self,
target: &mut RenderTarget,
y: f32,
x0: f32,
x1: f32,
color: BGRA8,
);

fn fill_axis_aligned_rect(&mut self, target: &mut RenderTarget, rect: Rect2f, color: BGRA8);

fn blit(
&mut self,
target: &mut RenderTarget,
dx: i32,
dy: i32,
texture: &Texture,
color: BGRA8,
);

/// Blits `texture` onto `target` at (`dx`, `dy`).
///
/// `target` must be a monochrome render texture and `texture` must be a monochrome texture.
fn blit_to_mono_texture(
&mut self,
target: &mut RenderTarget,
dx: i32,
dy: i32,
texture: &Texture,
);
scene: &[SceneNode],
user_data: &(dyn Any + 'static),
) -> Result<(), SceneRenderError>;
}

/// Flushes pending buffered draws.
///
/// Some rasterizers, like the wgpu one, may batch some operations to reduce the amount of
/// binding and draw calls. These batched operations may be flushed to ensure they're executed
/// now rather than on the next batch-incompatible operation.
///
/// Note that currently this function will also defragment texture atlases although this may
/// be changed in the future to use a separate once per-frame housekeeping function.
fn flush(&mut self, _target: &mut RenderTarget) {}

fn blur_prepare(&mut self, width: u32, height: u32, sigma: f32);
fn blur_buffer_blit(&mut self, dx: i32, dy: i32, texture: &Texture);
fn blur_padding(&mut self) -> Vec2<u32>;
fn blur_to_mono_texture(&mut self) -> Texture;
pub struct BlurOutput {
pub padding: Vec2<u32>,
pub texture: Texture,
}
Loading