Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparency example (PR only) #804

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion examples/headless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
let mut renderer = vello::Renderer::new(
device,
RendererOptions {
surface_format: None,
use_cpu: args.use_cpu,
num_init_threads: NonZeroUsize::new(1),
antialiasing_support: vello::AaSupport::area_only(),
Expand Down
15 changes: 15 additions & 0 deletions examples/scenes/src/test_scenes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export_scenes!(
labyrinth(labyrinth),
robust_paths(robust_paths),
base_color_test(base_color_test: animated),
translucent_base(translucent_base),
clip_test(clip_test: animated),
longpathdash_butt(impls::longpathdash(Cap::Butt), "longpathdash (butt caps)", false),
longpathdash_round(impls::longpathdash(Cap::Round), "longpathdash (round caps)", false),
Expand Down Expand Up @@ -1539,6 +1540,20 @@ mod impls {
);
}

pub(super) fn translucent_base(scene: &mut Scene, params: &mut SceneParams<'_>) {
let background_color = Color::TRANSPARENT;
params.base_color = Some(background_color);

// Blend a white square over it.
scene.fill(
Fill::NonZero,
Affine::IDENTITY,
palette::css::WHITE.with_alpha(0.5),
None,
&Rect::new(50.0, 50.0, 500.0, 500.0),
);
}

pub(super) fn clip_test(scene: &mut Scene, params: &mut SceneParams<'_>) {
let clip = {
const X0: f64 = 50.0;
Expand Down
35 changes: 25 additions & 10 deletions examples/simple/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,15 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
// Get a handle to the device
let device_handle = &self.context.devices[surface.dev_id];

// Get the surface's texture
let surface_texture = surface
.surface
.get_current_texture()
.expect("failed to get surface texture");

// Render to the surface's texture
// Render to a texture, which we will later copy into the surface
self.renderers[surface.dev_id]
.as_mut()
.unwrap()
.render_to_surface(
.render_to_texture(
&device_handle.device,
&device_handle.queue,
&self.scene,
&surface_texture,
&surface.target_view,
&vello::RenderParams {
base_color: palette::css::BLACK, // Background color
width,
Expand All @@ -155,6 +149,28 @@ impl ApplicationHandler for SimpleVelloApp<'_> {
)
.expect("failed to render to surface");

// Get the surface's texture
let surface_texture = surface
.surface
.get_current_texture()
.expect("failed to get surface texture");

// Perform the copy
let mut encoder =
device_handle
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Surface Blit"),
});
surface.blitter.copy(
&device_handle.device,
&mut encoder,
&surface.target_view,
&surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default()),
);
device_handle.queue.submit([encoder.finish()]);
// Queue the texture to be presented on the surface
surface_texture.present();

Expand Down Expand Up @@ -196,7 +212,6 @@ fn create_vello_renderer(render_cx: &RenderContext, surface: &RenderSurface<'_>)
Renderer::new(
&render_cx.devices[surface.dev_id].device,
RendererOptions {
surface_format: Some(surface.format),
use_cpu: false,
antialiasing_support: vello::AaSupport::all(),
num_init_threads: NonZeroUsize::new(1),
Expand Down
30 changes: 22 additions & 8 deletions examples/simple_sdl2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,14 @@ fn main() {

let device_handle = &context.devices[surface.dev_id];

let surface_texture = surface
.surface
.get_current_texture()
.expect("failed to get surface texture");

renderers[surface.dev_id]
.as_mut()
.unwrap()
.render_to_surface(
.render_to_texture(
&device_handle.device,
&device_handle.queue,
&scene,
&surface_texture,
&surface.target_view,
&vello::RenderParams {
base_color: palette::css::BLACK, // Background color
width,
Expand All @@ -89,6 +84,26 @@ fn main() {
)
.expect("failed to render to surface");

let surface_texture = surface
.surface
.get_current_texture()
.expect("failed to get surface texture");

let mut encoder =
device_handle
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Surface Blit"),
});
surface.blitter.copy(
&device_handle.device,
&mut encoder,
&surface.target_view,
&surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default()),
);
device_handle.queue.submit([encoder.finish()]);
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
Expand All @@ -108,7 +123,6 @@ fn create_vello_renderer(render_cx: &RenderContext, surface: &RenderSurface<'_>)
Renderer::new(
&render_cx.devices[surface.dev_id].device,
RendererOptions {
surface_format: Some(surface.format),
use_cpu: false,
antialiasing_support: vello::AaSupport::all(),
num_init_threads: NonZeroUsize::new(1),
Expand Down
78 changes: 36 additions & 42 deletions examples/with_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use winit::dpi::LogicalSize;
use winit::event_loop::EventLoop;
use winit::window::{Window, WindowAttributes};

use vello::wgpu;
use vello::wgpu::{self, CommandEncoderDescriptor};

#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
mod hot_reload;
Expand Down Expand Up @@ -213,7 +213,6 @@ impl ApplicationHandler<UserEvent> for VelloApp<'_> {
let mut renderer = Renderer::new(
&self.context.devices[id].device,
RendererOptions {
surface_format: Some(render_state.surface.format),
use_cpu: self.use_cpu,
antialiasing_support: AA_CONFIGS.iter().copied().collect(),
num_init_threads: NonZeroUsize::new(self.num_init_threads),
Expand Down Expand Up @@ -547,53 +546,47 @@ impl ApplicationHandler<UserEvent> for VelloApp<'_> {
}
}
drop(encoding_span);
let texture_span = tracing::trace_span!("Getting texture").entered();
let surface_texture = surface
.surface
.get_current_texture()
.expect("failed to get surface texture");

drop(texture_span);
let render_span = tracing::trace_span!("Dispatching render").entered();
// Note: we don't run the async/"robust" pipeline, as
// it requires more async wiring for the readback. See
// [#gpu > async on wasm](https://xi.zulipchat.com/#narrow/stream/197075-gpu/topic/async.20on.20wasm)
#[allow(deprecated)]
// #[expect(deprecated, reason = "This deprecation is not targeted at us.")] // Our MSRV is too low to use `expect`
if self.async_pipeline && cfg!(not(target_arch = "wasm32")) {
self.scene_complexity = vello::util::block_on_wgpu(
self.renderers[surface.dev_id]
.as_mut()
.unwrap()
.render_to_texture(
&device_handle.device,
self.renderers[surface.dev_id]
.as_mut()
.unwrap()
.render_to_surface_async(
&device_handle.device,
&device_handle.queue,
&self.scene,
&surface_texture,
&render_params,
self.debug,
),
&device_handle.queue,
&self.scene,
&surface.target_view,
&render_params,
)
.expect("failed to render to surface");
} else {
self.renderers[surface.dev_id]
.as_mut()
.unwrap()
.render_to_surface(
&device_handle.device,
&device_handle.queue,
&self.scene,
&surface_texture,
&render_params,
)
.expect("failed to render to surface");
}
surface_texture.present();
drop(render_span);

let texture_span = tracing::trace_span!("Blitting to surface").entered();
let surface_texture = surface
.surface
.get_current_texture()
.expect("failed to get surface texture");
// Perform the copy
// (TODO: Does it improve throughput to acquire the surface after the previous texture render has happened?)
let mut encoder =
device_handle
.device
.create_command_encoder(&CommandEncoderDescriptor {
label: Some("Surface Blit"),
});
surface.blitter.copy(
&device_handle.device,
&mut encoder,
&surface.target_view,
&surface_texture
.texture
.create_view(&wgpu::TextureViewDescriptor::default()),
);
device_handle.queue.submit([encoder.finish()]);
surface_texture.present();
drop(texture_span);

{
let _poll_aspan = tracing::trace_span!("Polling wgpu device").entered();
let _poll_span = tracing::trace_span!("Polling wgpu device").entered();
device_handle.device.poll(wgpu::Maintain::Poll);
}
let new_time = Instant::now();
Expand Down Expand Up @@ -789,6 +782,7 @@ fn window_attributes() -> WindowAttributes {
Window::default_attributes()
.with_inner_size(LogicalSize::new(1044, 800))
.with_resizable(true)
.with_transparent(true)
.with_title("Vello demo")
}

Expand Down
3 changes: 1 addition & 2 deletions vello/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default = ["wgpu"]
# bump-allocated GPU memory.
# TODO: Turn this into a runtime option used at resolve time and remove the feature.
bump_estimate = ["vello_encoding/bump_estimate"]
wgpu = ["dep:wgpu", "dep:vello_shaders", "dep:futures-intrusive"]
wgpu = ["dep:wgpu", "dep:vello_shaders"]

# Development only features

Expand Down Expand Up @@ -50,7 +50,6 @@ peniko = { workspace = true }
wgpu = { workspace = true, optional = true }
log = { workspace = true }
static_assertions = { workspace = true }
futures-intrusive = { workspace = true, optional = true }
wgpu-profiler = { workspace = true, optional = true }
thiserror = { workspace = true }
# TODO: Add feature for built-in bitmap emoji support?
Expand Down
12 changes: 6 additions & 6 deletions vello/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright 2023 the Vello Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

#[cfg(all(feature = "debug_layers", feature = "wgpu"))]
mod renderer;
#[cfg(all(feature = "debug_layers", feature = "wgpu"))]
mod validate;
// #[cfg(all(feature = "debug_layers", feature = "wgpu"))]
// mod renderer;
// #[cfg(all(feature = "debug_layers", feature = "wgpu"))]
// mod validate;

use std::fmt::Debug;

#[cfg(all(feature = "debug_layers", feature = "wgpu"))]
pub(crate) use renderer::*;
// #[cfg(all(feature = "debug_layers", feature = "wgpu"))]
// pub(crate) use renderer::*;

/// Bitflags for enabled debug operations.
///
Expand Down
Loading
Loading