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

Why its crashes? #128

Open
suprohub opened this issue Jan 21, 2025 · 4 comments
Open

Why its crashes? #128

suprohub opened this issue Jan 21, 2025 · 4 comments

Comments

@suprohub
Copy link

suprohub commented Jan 21, 2025

use std::sync::Arc;

use glyphon::{
    Buffer, Cache, Color, FontSystem, Metrics, Resolution, SwashCache, TextArea, TextAtlas,
    TextBounds, TextRenderer, Viewport,
};
use wgpu::{
    Adapter, Device, Instance, LoadOp, Operations, Queue, RenderPassColorAttachment,
    RenderPassDescriptor, StoreOp, Surface, SurfaceConfiguration,
};
use winit::window::Window;

pub struct Renderer<'w> {
    surface: Surface<'w>,
    config: SurfaceConfiguration,
    adapter: Adapter,
    device: Device,
    queue: Queue,

    pub font_system: FontSystem,
    pub swash_cache: SwashCache,
    pub viewport: Viewport,
    pub atlas: TextAtlas,
    pub text_renderer: TextRenderer,
    pub text_buffer: Buffer,
}

impl Renderer<'_> {
    pub async fn new(window: Arc<Window>) -> Self {
        let size = window.inner_size();

        let instance = Instance::default();
        let surface = instance.create_surface(window).unwrap();
        let adapter = instance.request_adapter(&Default::default()).await.unwrap();
        let (device, queue) = adapter
            .request_device(&Default::default(), None)
            .await
            .unwrap();

        let config = surface
            .get_default_config(&adapter, size.width.max(1), size.height.max(1))
            .unwrap();

        surface.configure(&device, &config);

        let mut font_system = FontSystem::new();
        let swash_cache = SwashCache::new();
        let cache = Cache::new(&device);
        let viewport = Viewport::new(&device, &cache);
        let mut atlas = TextAtlas::new(&device, &queue, &cache, config.format);
        let text_renderer = TextRenderer::new(&mut atlas, &device, Default::default(), None);
        let mut buffer = Buffer::new(&mut font_system, Metrics::new(30.0, 42.0));

        buffer.set_size(
            &mut font_system,
            Some(config.width as f32),
            Some(config.height as f32),
        );

        Self {
            surface,
            adapter,
            queue,
            config,
            device,
            font_system,
            swash_cache,
            viewport,
            atlas,
            text_renderer,
            text_buffer: buffer,
        }
    }

    pub fn new_blocked(window: Arc<Window>) -> Self {
        pollster::block_on(Self::new(window))
    }

    pub fn resize(&mut self, new_size: (u32, u32)) {
        let (width, height) = new_size;
        self.config.width = width.max(1);
        self.config.height = height.max(1);

        self.surface.configure(&self.device, &self.config);
    }

    pub fn draw(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        self.viewport.update(
            &self.queue,
            Resolution {
                width: self.config.width,
                height: self.config.height,
            },
        );
        self.text_renderer.prepare(
            &self.device,
            &self.queue,
            &mut self.font_system,
            &mut self.atlas,
            &self.viewport,
            [TextArea {
                buffer: &self.text_buffer,
                left: 10.0,
                top: 10.0,
                scale: 1.0,
                bounds: TextBounds {
                    left: 0,
                    top: 0,
                    right: 600,
                    bottom: 160,
                },
                default_color: Color::rgb(255, 255, 255),
                custom_glyphs: &[],
            }],
            &mut self.swash_cache,
        )?;

        let frame = self.surface.get_current_texture()?;
        let view = frame.texture.create_view(&Default::default());

        let mut encoder = self.device.create_command_encoder(&Default::default());

        {
            let mut rpass = encoder.begin_render_pass(&RenderPassDescriptor {
                label: None,
color_attachments: &[Some(RenderPassColorAttachment {
                    view: &view,
                    resolve_target: None,
                    ops: Operations {
                        load: LoadOp::Clear(wgpu::Color::BLACK),
                        store: StoreOp::Store,
                    },
                })],
                ..Default::default()
            });

            self.text_renderer
                .render(&self.atlas, &self.viewport, &mut rpass)?;
        }

        self.queue.submit(Some(encoder.finish()));
        frame.present();

        self.atlas.trim();
        Ok(())
    }
}

When i set text in text buffer its just crashes

@grovesNL
Copy link
Owner

Hi! 👋 Could you please provide the full error message? If it crashes at set_text then it could be an issue in cosmic_text instead of glyphon

@suprohub
Copy link
Author

Hi! 👋 Could you please provide the full error message? If it crashes at set_text then it could be an issue in cosmic_text instead of glyphon

It crashes at set text, but i cant provide error masage because of android 💀 (idk why android logger not working)
I also dont know how i can set font. Maybe i just not set it?

Will be cool if you test it, because im on aarch64

@suprohub
Copy link
Author

@suprohub
Copy link
Author

 💀 💀 💀

Maybe you can fix it, and if target is android you just get fonts from system?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants