mirror of
https://github.com/maplibre/maplibre-rs.git
synced 2025-12-08 19:05:57 +00:00
Add README and first files for rendering to a window
This commit is contained in:
commit
cce827be91
1956
Cargo.lock
generated
Normal file
1956
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
37
Cargo.toml
Normal file
37
Cargo.toml
Normal file
@ -0,0 +1,37 @@
|
||||
[package]
|
||||
name = "mapr"
|
||||
version = "0.1.0"
|
||||
authors = ["Maximilian Ammann <max@maxammann.org>"]
|
||||
edition = "2021"
|
||||
resolver = "2"
|
||||
|
||||
[features]
|
||||
|
||||
[dependencies]
|
||||
|
||||
# Rendering
|
||||
winit = "0.25"
|
||||
wgpu = "0.11"
|
||||
pollster = "0.2"
|
||||
|
||||
# Logging
|
||||
log = "0.4"
|
||||
env_logger = "0.9"
|
||||
|
||||
# Utils
|
||||
hexdump = "0.1"
|
||||
|
||||
# Serialization
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.3"
|
||||
# Support logging in tests
|
||||
test-env-log = "0.2"
|
||||
env_logger = "0.8" # Used for test-env-log
|
||||
|
||||
|
||||
[[bin]]
|
||||
name = "mapr"
|
||||
path = "src/main.rs"
|
||||
97
README.md
Normal file
97
README.md
Normal file
@ -0,0 +1,97 @@
|
||||
<h1 align="center">mapr</h1>
|
||||
<p align="center">
|
||||
<img width=200px alt="Logo" src="https://">
|
||||
</p>
|
||||
<div align="center">
|
||||
<strong>Native Maps for Web, Mobile and Linux</strong>
|
||||
</div>
|
||||
<div align="center">
|
||||
A map rendering library written in Rust.
|
||||
</div>
|
||||
|
||||
<div align="center">
|
||||
<img src="https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square"
|
||||
alt="Stability" />
|
||||
<a href="https://github.com/maxammann/mapr/actions/workflows/rust.yml">
|
||||
<img src="https://img.shields.io/github/workflow/status/maxammann/mapr/Rust?style=flat-square"
|
||||
alt="Build status" />
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div align="center">
|
||||
<h3>
|
||||
<a href="https://">
|
||||
Example
|
||||
</a>
|
||||
<span> | </span>
|
||||
<a href="https://maxammann.github.io/mapr">
|
||||
Documentation
|
||||
</a>
|
||||
</h3>
|
||||
</div>
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
TODO
|
||||
|
||||
|
||||
## Features
|
||||
|
||||
* None so far
|
||||
|
||||
## Goals
|
||||
|
||||
* Renders [vector tiles](https://docs.mapbox.com/vector-tiles/specification/).
|
||||
* Runs on:
|
||||
* Web via WebAssembly and WebGPU,
|
||||
* Linux (Xorg/Wayland) via Vulkan,
|
||||
* Android via OpenGL,
|
||||
* iOS via Metal.
|
||||
* Supports the [TileJSON](https://docs.mapbox.com/help/glossary/tilejson/) standard
|
||||
* Pimarily
|
||||
|
||||
## Non-Goals
|
||||
|
||||
* Rendering any kind of rasterized data
|
||||
|
||||
## Building
|
||||
|
||||
Now, to build the project:
|
||||
|
||||
```bash
|
||||
git clone git@github.com/maxammann/mapr
|
||||
cargo build
|
||||
```
|
||||
|
||||
## Running on Linux
|
||||
|
||||
Fuzz using three clients:
|
||||
|
||||
```bash
|
||||
cargo run --bin mapr --
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
```bash
|
||||
cargo test
|
||||
```
|
||||
|
||||
## Rust Setup
|
||||
|
||||
Install [rustup](https://rustup.rs/).
|
||||
|
||||
The toolchain will be automatically downloaded when building this project. See [./rust-toolchain.toml](./rust-toolchain.toml) for more details about the toolchain.
|
||||
|
||||
## Documentation
|
||||
|
||||
This generates the documentation for this crate and opens the browser. This also includes the documentation of every
|
||||
dependency.
|
||||
|
||||
```bash
|
||||
cargo doc --open
|
||||
```
|
||||
|
||||
You can also view the up-to-date documentation [here](https://).
|
||||
|
||||
3
rust-toolchain.toml
Normal file
3
rust-toolchain.toml
Normal file
@ -0,0 +1,3 @@
|
||||
[toolchain]
|
||||
channel = "1.56.1"
|
||||
targets = [ "x86_64-unknown-linux-gnu" ]
|
||||
178
src/main.rs
Normal file
178
src/main.rs
Normal file
@ -0,0 +1,178 @@
|
||||
use std::iter;
|
||||
|
||||
use winit::{
|
||||
event::*,
|
||||
event_loop::{ControlFlow, EventLoop},
|
||||
window::{Window, WindowBuilder},
|
||||
};
|
||||
|
||||
struct State {
|
||||
surface: wgpu::Surface,
|
||||
device: wgpu::Device,
|
||||
queue: wgpu::Queue,
|
||||
config: wgpu::SurfaceConfiguration,
|
||||
size: winit::dpi::PhysicalSize<u32>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
async fn new(window: &Window) -> Self {
|
||||
let size = window.inner_size();
|
||||
|
||||
// The instance is a handle to our GPU
|
||||
// BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
|
||||
let instance = wgpu::Instance::new(wgpu::Backends::all());
|
||||
let surface = unsafe { instance.create_surface(window) };
|
||||
let adapter = instance
|
||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||
power_preference: wgpu::PowerPreference::HighPerformance,
|
||||
compatible_surface: Some(&surface),
|
||||
force_fallback_adapter: false,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let (device, queue) = adapter
|
||||
.request_device(
|
||||
&wgpu::DeviceDescriptor {
|
||||
label: None,
|
||||
features: wgpu::Features::empty(),
|
||||
limits: wgpu::Limits::default(),
|
||||
},
|
||||
// Some(&std::path::Path::new("trace")), // Trace path
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let config = wgpu::SurfaceConfiguration {
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||
format: surface.get_preferred_format(&adapter).unwrap(),
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
present_mode: wgpu::PresentMode::Fifo,
|
||||
};
|
||||
surface.configure(&device, &config);
|
||||
|
||||
Self {
|
||||
surface,
|
||||
device,
|
||||
queue,
|
||||
config,
|
||||
size,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
|
||||
if new_size.width > 0 && new_size.height > 0 {
|
||||
self.size = new_size;
|
||||
self.config.width = new_size.width;
|
||||
self.config.height = new_size.height;
|
||||
self.surface.configure(&self.device, &self.config);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn input(&mut self, event: &WindowEvent) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn update(&mut self) {}
|
||||
|
||||
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
||||
let output = self.surface.get_current_texture()?;
|
||||
let view = output
|
||||
.texture
|
||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
|
||||
let mut encoder = self
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("Render Encoder"),
|
||||
});
|
||||
|
||||
{
|
||||
let _render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Render Pass"),
|
||||
color_attachments: &[wgpu::RenderPassColorAttachment {
|
||||
view: &view,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
||||
r: 0.1,
|
||||
g: 0.2,
|
||||
b: 0.3,
|
||||
a: 1.0,
|
||||
}),
|
||||
store: true,
|
||||
},
|
||||
}],
|
||||
depth_stencil_attachment: None,
|
||||
});
|
||||
}
|
||||
|
||||
self.queue.submit(iter::once(encoder.finish()));
|
||||
output.present();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
let event_loop = EventLoop::new();
|
||||
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||
|
||||
// State::new uses async code, so we're going to wait for it to finish
|
||||
let mut state: State = pollster::block_on(State::new(&window));
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
match event {
|
||||
Event::WindowEvent {
|
||||
ref event,
|
||||
window_id,
|
||||
} if window_id == window.id() => {
|
||||
if !state.input(event) {
|
||||
// UPDATED!
|
||||
match event {
|
||||
WindowEvent::CloseRequested
|
||||
| WindowEvent::KeyboardInput {
|
||||
input:
|
||||
KeyboardInput {
|
||||
state: ElementState::Pressed,
|
||||
virtual_keycode: Some(VirtualKeyCode::Escape),
|
||||
..
|
||||
},
|
||||
..
|
||||
} => *control_flow = ControlFlow::Exit,
|
||||
WindowEvent::Resized(physical_size) => {
|
||||
state.resize(*physical_size);
|
||||
}
|
||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||
// new_inner_size is &&mut so w have to dereference it twice
|
||||
state.resize(**new_inner_size);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::RedrawRequested(_) => {
|
||||
state.update();
|
||||
match state.render() {
|
||||
Ok(_) => {}
|
||||
// Reconfigure the surface if lost
|
||||
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
|
||||
// The system is out of memory, we should probably quit
|
||||
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||||
// All other errors (Outdated, Timeout) should be resolved by the next frame
|
||||
Err(e) => eprintln!("{:?}", e),
|
||||
}
|
||||
}
|
||||
Event::RedrawEventsCleared => {
|
||||
// RedrawRequested will only trigger once, unless we manually
|
||||
// request it.
|
||||
window.request_redraw();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user