Skip to content

Commit

Permalink
actually query texture size for atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
msparkles committed Jul 2, 2024
1 parent b1a5e7c commit 46d0a0e
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 23 deletions.
6 changes: 3 additions & 3 deletions crates/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ async fn run(body: impl ExampleBody) {

let sample_count = get_sample_count();

// yakui_app has a helper for setting up winit and wgpu.
let mut app = yakui_app::Graphics::new(&window, sample_count).await;

// Create our yakui state. This is where our UI will be built, laid out, and
// calculations for painting will happen.
let mut yak = yakui::Yakui::new();

// yakui_app has a helper for setting up winit and wgpu.
let mut app = yakui_app::Graphics::new(&mut yak, &window, sample_count).await;

// By default, yakui_winit will measure the system's scale factor and pass
// it to yakui.
//
Expand Down
2 changes: 1 addition & 1 deletion crates/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn app() {

async fn run(event_loop: EventLoop<()>, window: Window) {
let mut yak = Yakui::new();
let mut graphics = Graphics::new(&window, 4).await;
let mut graphics = Graphics::new(&mut yak, &window, 4).await;

event_loop.set_control_flow(ControlFlow::Poll);
event_loop
Expand Down
4 changes: 2 additions & 2 deletions crates/yakui-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Graphics {
}

impl Graphics {
pub async fn new(window: &Window, sample_count: u32) -> Self {
pub async fn new(state: &mut yakui_core::Yakui, window: &Window, sample_count: u32) -> Self {
let mut size = window.inner_size();

// FIXME: On web, we're receiving (0, 0) as the initial size of the
Expand Down Expand Up @@ -91,7 +91,7 @@ impl Graphics {

// yakui_wgpu takes paint output from yakui and renders it for us using
// wgpu.
let renderer = yakui_wgpu::YakuiWgpu::new(&device, &queue);
let renderer = yakui_wgpu::YakuiWgpu::new(state, &device, &queue);

// yakui_winit processes winit events and applies them to our yakui
// state.
Expand Down
24 changes: 24 additions & 0 deletions crates/yakui-core/src/paint/paint_dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ use super::layers::PaintLayers;
use super::primitives::{PaintMesh, Vertex};
use super::texture::{Texture, TextureChange};

#[derive(Debug, Clone, Copy, Default)]
/// Contains all information about the limits of the paint device.
pub struct PaintLimits {
/// Maximum texture size of a 1D texture.
pub max_texture_size_1d: u32,
/// Maximum texture size of a 2D texture.
pub max_texture_size_2d: u32,
/// Maximum texture size of a 3D texture.
pub max_texture_size_3d: u32,
}

/// Contains all information about how to paint the current set of widgets.
#[derive(Debug)]
pub struct PaintDom {
Expand All @@ -22,6 +33,7 @@ pub struct PaintDom {
surface_size: Vec2,
unscaled_viewport: Rect,
scale_factor: f32,
limits: PaintLimits,

layers: PaintLayers,
clip_stack: Vec<Rect>,
Expand All @@ -36,11 +48,23 @@ impl PaintDom {
surface_size: Vec2::ONE,
unscaled_viewport: Rect::ONE,
scale_factor: 1.0,
limits: PaintLimits::default(),

layers: PaintLayers::new(),
clip_stack: Vec::new(),
}
}

/// Gets the paint limits.
pub fn limits(&self) -> PaintLimits {
self.limits
}

/// Sets the paint limits, should be called once by rendering backends.
pub fn set_limit(&mut self, limits: PaintLimits) {
self.limits = limits
}

/// Prepares the PaintDom to be updated for the frame.
pub fn start(&mut self) {
self.texture_edits.clear();
Expand Down
7 changes: 6 additions & 1 deletion crates/yakui-core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::geometry::{Rect, Vec2};
use crate::id::ManagedTextureId;
use crate::input::InputState;
use crate::layout::LayoutDom;
use crate::paint::{PaintDom, Texture};
use crate::paint::{PaintDom, PaintLimits, Texture};

/// The entrypoint for yakui.
#[derive(Debug)]
Expand Down Expand Up @@ -120,4 +120,9 @@ impl Yakui {
pub fn layout_dom(&self) -> &LayoutDom {
&self.layout
}

/// Sets the paint limits, should be called once by rendering backends.
pub fn set_paint_limit(&mut self, limits: PaintLimits) {
self.paint.set_limit(limits)
}
}
2 changes: 1 addition & 1 deletion crates/yakui-to-image/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ pub fn paint_and_save_to<P: AsRef<Path>>(state: &mut yakui_core::Yakui, path: P)

pub fn paint(state: &mut yakui_core::Yakui) -> RgbaImage {
let graphics = pollster::block_on(Graphics::new());
let mut renderer = yakui_wgpu::YakuiWgpu::new(&graphics.device, &graphics.queue);
let mut renderer = yakui_wgpu::YakuiWgpu::new(state, &graphics.device, &graphics.queue);
graphics.paint(state, &mut renderer)
}
8 changes: 7 additions & 1 deletion crates/yakui-vulkan/examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ fn main() {
&vulkan_test.device,
vulkan_test.present_queue,
vulkan_test.device_memory_properties,
vulkan_test.device_properties,
);
let mut options = yakui_vulkan::Options::default();
options.render_pass = vulkan_test.render_pass;
let mut yakui_vulkan = YakuiVulkan::new(&vulkan_context, options);
let mut yakui_vulkan = YakuiVulkan::new(&mut yak, &vulkan_context, options);
// Prepare for one frame in flight
yakui_vulkan.transfers_submitted();
let gui_state = GuiState {
Expand Down Expand Up @@ -93,6 +94,7 @@ fn main() {
&vulkan_test.device,
vulkan_test.present_queue,
vulkan_test.device_memory_properties,
vulkan_test.device_properties,
);

yak.start();
Expand Down Expand Up @@ -232,6 +234,7 @@ struct VulkanTest {
instance: ash::Instance,
surface_loader: ash::khr::surface::Instance,
device_memory_properties: vk::PhysicalDeviceMemoryProperties,
device_properties: vk::PhysicalDeviceProperties,

present_queue: vk::Queue,

Expand Down Expand Up @@ -511,6 +514,8 @@ impl VulkanTest {
let device_memory_properties =
unsafe { instance.get_physical_device_memory_properties(physical_device) };

let device_properties = unsafe { instance.get_physical_device_properties(physical_device) };

Self {
device,
physical_device,
Expand All @@ -520,6 +525,7 @@ impl VulkanTest {
surface_loader,
swapchain_info,
device_memory_properties,
device_properties,
surface,
swapchain,
present_image_views,
Expand Down
13 changes: 12 additions & 1 deletion crates/yakui-vulkan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub use vulkan_context::VulkanContext;
use vulkan_texture::{UploadQueue, NO_TEXTURE_ID};
pub use vulkan_texture::{VulkanTexture, VulkanTextureCreateInfo};
use yakui::geometry::UVec2;
use yakui::paint::PaintLimits;
use yakui::{paint::Vertex as YakuiVertex, ManagedTextureId};

/// A struct wrapping everything needed to render yakui on Vulkan. This will be your main entry point.
Expand Down Expand Up @@ -151,7 +152,17 @@ impl YakuiVulkan {
/// ## Safety
/// - `vulkan_context` must have valid members
/// - the members of `render_surface` must have been created with the same [`ash::Device`] as `vulkan_context`.
pub fn new(vulkan_context: &VulkanContext, options: Options) -> Self {
pub fn new(
state: &mut yakui_core::Yakui,
vulkan_context: &VulkanContext,
options: Options,
) -> Self {
state.set_paint_limit(PaintLimits {
max_texture_size_1d: vulkan_context.properties.limits.max_image_dimension1_d,
max_texture_size_2d: vulkan_context.properties.limits.max_image_dimension2_d,
max_texture_size_3d: vulkan_context.properties.limits.max_image_dimension3_d,
});

let device = vulkan_context.device;
let descriptors = Descriptors::new(vulkan_context);

Expand Down
4 changes: 4 additions & 0 deletions crates/yakui-vulkan/src/vulkan_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct VulkanContext<'a> {
pub queue: vk::Queue,
/// Memory properties used for [`crate::YakuiVulkan`]'s allocation commands
pub memory_properties: vk::PhysicalDeviceMemoryProperties,
/// Device properties used for setting paint limits
pub properties: vk::PhysicalDeviceProperties,
}

impl<'a> VulkanContext<'a> {
Expand All @@ -26,11 +28,13 @@ impl<'a> VulkanContext<'a> {
device: &'a ash::Device,
queue: vk::Queue,
memory_properties: vk::PhysicalDeviceMemoryProperties,
properties: vk::PhysicalDeviceProperties,
) -> Self {
Self {
device,
queue,
memory_properties,
properties,
}
}

Expand Down
10 changes: 8 additions & 2 deletions crates/yakui-wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use bytemuck::{Pod, Zeroable};
use glam::UVec2;
use thunderdome::{Arena, Index};
use yakui_core::geometry::{Rect, Vec2, Vec4};
use yakui_core::paint::{PaintDom, Pipeline, Texture, TextureChange, TextureFormat};
use yakui_core::paint::{PaintDom, PaintLimits, Pipeline, Texture, TextureChange, TextureFormat};
use yakui_core::{ManagedTextureId, TextureId};

use self::pipeline_cache::PipelineCache;
Expand Down Expand Up @@ -65,7 +65,13 @@ impl Vertex {
}

impl YakuiWgpu {
pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
pub fn new(state: &mut yakui_core::Yakui, device: &wgpu::Device, queue: &wgpu::Queue) -> Self {
state.set_paint_limit(PaintLimits {
max_texture_size_1d: device.limits().max_texture_dimension_1d,
max_texture_size_2d: device.limits().max_texture_dimension_2d,
max_texture_size_3d: device.limits().max_texture_dimension_3d,
});

let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("yakui Bind Group Layout"),
entries: &[
Expand Down
15 changes: 4 additions & 11 deletions crates/yakui-widgets/src/text_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ use yakui_core::geometry::{Rect, URect, UVec2, Vec2};
use yakui_core::paint::{PaintDom, Texture, TextureFilter, TextureFormat};
use yakui_core::ManagedTextureId;

#[cfg(not(target_arch = "wasm32"))]
const TEXTURE_SIZE: u32 = 4096;

// When targeting the web, limit the texture atlas size to 2048x2048 to fit into
// WebGL 2's limitations. In the future, we should introduce a way to query for
// these limits.
#[cfg(target_arch = "wasm32")]
const TEXTURE_SIZE: u32 = 2048;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Kind {
Mask,
Expand Down Expand Up @@ -66,11 +57,13 @@ impl InnerAtlas {
}

fn ensure_texture(&mut self, paint: &mut PaintDom) {
let texture_size = paint.limits().max_texture_size_2d;

if self.texture.is_none() {
let mut texture = Texture::new(
self.kind.texture_format(),
UVec2::new(TEXTURE_SIZE, TEXTURE_SIZE),
vec![0; (TEXTURE_SIZE * TEXTURE_SIZE) as usize * self.kind.num_channels()],
UVec2::new(texture_size, texture_size),
vec![0; (texture_size * texture_size) as usize * self.kind.num_channels()],
);
texture.mag_filter = TextureFilter::Linear;
texture.min_filter = TextureFilter::Linear;
Expand Down

0 comments on commit 46d0a0e

Please sign in to comment.