summaryrefslogtreecommitdiffstats
path: root/examples/integration
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--examples/integration/.gitignore (renamed from examples/integration_wgpu/.gitignore)0
-rw-r--r--examples/integration/Cargo.toml (renamed from examples/integration_wgpu/Cargo.toml)6
-rw-r--r--examples/integration/README.md (renamed from examples/integration_wgpu/README.md)0
-rw-r--r--examples/integration/index.html (renamed from examples/integration_wgpu/index.html)4
-rw-r--r--examples/integration/src/controls.rs (renamed from examples/integration_wgpu/src/controls.rs)10
-rw-r--r--examples/integration/src/main.rs (renamed from examples/integration_wgpu/src/main.rs)122
-rw-r--r--examples/integration/src/scene.rs (renamed from examples/integration_wgpu/src/scene.rs)2
-rw-r--r--examples/integration/src/shader/frag.wgsl (renamed from examples/integration_wgpu/src/shader/frag.wgsl)0
-rw-r--r--examples/integration/src/shader/vert.wgsl (renamed from examples/integration_wgpu/src/shader/vert.wgsl)0
-rw-r--r--examples/integration_opengl/Cargo.toml12
-rw-r--r--examples/integration_opengl/README.md16
-rw-r--r--examples/integration_opengl/src/controls.rs101
-rw-r--r--examples/integration_opengl/src/main.rs187
-rw-r--r--examples/integration_opengl/src/scene.rs102
14 files changed, 75 insertions, 487 deletions
diff --git a/examples/integration_wgpu/.gitignore b/examples/integration/.gitignore
index e188dc28..e188dc28 100644
--- a/examples/integration_wgpu/.gitignore
+++ b/examples/integration/.gitignore
diff --git a/examples/integration_wgpu/Cargo.toml b/examples/integration/Cargo.toml
index c0e4fd81..22914742 100644
--- a/examples/integration_wgpu/Cargo.toml
+++ b/examples/integration/Cargo.toml
@@ -1,5 +1,5 @@
[package]
-name = "integration_wgpu"
+name = "integration"
version = "0.1.0"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2021"
@@ -7,7 +7,9 @@ publish = false
[dependencies]
iced_winit = { path = "../../winit" }
-iced_wgpu = { path = "../../wgpu", features = ["webgl"] }
+iced_wgpu = { path = "../../wgpu" }
+iced_widget = { path = "../../widget" }
+iced_renderer = { path = "../../renderer", features = ["wgpu"] }
env_logger = "0.10"
[target.'cfg(target_arch = "wasm32")'.dependencies]
diff --git a/examples/integration_wgpu/README.md b/examples/integration/README.md
index ece9ba1e..ece9ba1e 100644
--- a/examples/integration_wgpu/README.md
+++ b/examples/integration/README.md
diff --git a/examples/integration_wgpu/index.html b/examples/integration/index.html
index 461e67a4..920bc4a0 100644
--- a/examples/integration_wgpu/index.html
+++ b/examples/integration/index.html
@@ -8,8 +8,8 @@
<h1>integration_wgpu</h1>
<canvas id="iced_canvas"></canvas>
<script type="module">
- import init from "./integration_wgpu.js";
- init('./integration_wgpu_bg.wasm');
+ import init from "./integration.js";
+ init('./integration_bg.wasm');
</script>
<style>
body {
diff --git a/examples/integration_wgpu/src/controls.rs b/examples/integration/src/controls.rs
index 42623b15..14e53ede 100644
--- a/examples/integration_wgpu/src/controls.rs
+++ b/examples/integration/src/controls.rs
@@ -1,6 +1,8 @@
use iced_wgpu::Renderer;
-use iced_winit::widget::{slider, text_input, Column, Row, Text};
-use iced_winit::{Alignment, Color, Command, Element, Length, Program};
+use iced_widget::{slider, text_input, Column, Row, Text};
+use iced_winit::core::{Alignment, Color, Element, Length};
+use iced_winit::runtime::{Command, Program};
+use iced_winit::style::Theme;
pub struct Controls {
background_color: Color,
@@ -27,7 +29,7 @@ impl Controls {
}
impl Program for Controls {
- type Renderer = Renderer;
+ type Renderer = Renderer<Theme>;
type Message = Message;
fn update(&mut self, message: Message) -> Command<Message> {
@@ -43,7 +45,7 @@ impl Program for Controls {
Command::none()
}
- fn view(&self) -> Element<Message, Renderer> {
+ fn view(&self) -> Element<Message, Renderer<Theme>> {
let background_color = self.background_color;
let text = &self.text;
diff --git a/examples/integration_wgpu/src/main.rs b/examples/integration/src/main.rs
index 15901db8..c935aca7 100644
--- a/examples/integration_wgpu/src/main.rs
+++ b/examples/integration/src/main.rs
@@ -4,11 +4,14 @@ mod scene;
use controls::Controls;
use scene::Scene;
-use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport};
-use iced_winit::{
- conversion, futures, program, renderer, winit, Clipboard, Color, Debug,
- Size,
-};
+use iced_wgpu::graphics::Viewport;
+use iced_wgpu::{wgpu, Backend, Renderer, Settings};
+use iced_winit::core::renderer;
+use iced_winit::core::{Color, Size};
+use iced_winit::runtime::program;
+use iced_winit::runtime::Debug;
+use iced_winit::style::Theme;
+use iced_winit::{conversion, futures, winit, Clipboard};
use winit::{
dpi::PhysicalPosition,
@@ -34,7 +37,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
.and_then(|win| win.document())
.and_then(|doc| doc.get_element_by_id("iced_canvas"))
.and_then(|element| element.dyn_into::<HtmlCanvasElement>().ok())
- .expect("Canvas with id `iced_canvas` is missing")
+ .expect("Get canvas element")
};
#[cfg(not(target_arch = "wasm32"))]
env_logger::init();
@@ -74,47 +77,49 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
});
let surface = unsafe { instance.create_surface(&window) }?;
- let (format, (device, queue)) = futures::executor::block_on(async {
- let adapter = wgpu::util::initialize_adapter_from_env_or_default(
- &instance,
- backend,
- Some(&surface),
- )
- .await
- .expect("No suitable GPU adapters found on the system!");
-
- let adapter_features = adapter.features();
-
- #[cfg(target_arch = "wasm32")]
- let needed_limits = wgpu::Limits::downlevel_webgl2_defaults()
- .using_resolution(adapter.limits());
-
- #[cfg(not(target_arch = "wasm32"))]
- let needed_limits = wgpu::Limits::default();
-
- let capabilities = surface.get_capabilities(&adapter);
-
- (
- capabilities
- .formats
- .iter()
- .copied()
- .find(wgpu::TextureFormat::is_srgb)
- .or_else(|| capabilities.formats.first().copied())
- .expect("Get preferred format"),
- adapter
- .request_device(
- &wgpu::DeviceDescriptor {
- label: None,
- features: adapter_features & wgpu::Features::default(),
- limits: needed_limits,
- },
- None,
- )
- .await
- .expect("Request device"),
- )
- });
+ let (format, (device, queue)) =
+ futures::futures::executor::block_on(async {
+ let adapter = wgpu::util::initialize_adapter_from_env_or_default(
+ &instance,
+ backend,
+ Some(&surface),
+ )
+ .await
+ .expect("Create adapter");
+
+ let adapter_features = adapter.features();
+
+ #[cfg(target_arch = "wasm32")]
+ let needed_limits = wgpu::Limits::downlevel_webgl2_defaults()
+ .using_resolution(adapter.limits());
+
+ #[cfg(not(target_arch = "wasm32"))]
+ let needed_limits = wgpu::Limits::default();
+
+ let capabilities = surface.get_capabilities(&adapter);
+
+ (
+ capabilities
+ .formats
+ .iter()
+ .copied()
+ .find(wgpu::TextureFormat::is_srgb)
+ .or_else(|| capabilities.formats.first().copied())
+ .expect("Get preferred format"),
+ adapter
+ .request_device(
+ &wgpu::DeviceDescriptor {
+ label: None,
+ features: adapter_features
+ & wgpu::Features::default(),
+ limits: needed_limits,
+ },
+ None,
+ )
+ .await
+ .expect("Request device"),
+ )
+ });
surface.configure(
&device,
@@ -131,17 +136,18 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut resized = false;
- // Initialize staging belt
- let mut staging_belt = wgpu::util::StagingBelt::new(5 * 1024);
-
// Initialize scene and GUI controls
let scene = Scene::new(&device, format);
let controls = Controls::new();
// Initialize iced
let mut debug = Debug::new();
- let mut renderer =
- Renderer::new(Backend::new(&device, Settings::default(), format));
+ let mut renderer = Renderer::new(Backend::new(
+ &device,
+ &queue,
+ Settings::default(),
+ format,
+ ));
let mut state = program::State::new(
controls,
@@ -193,7 +199,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
viewport.scale_factor(),
),
&mut renderer,
- &iced_wgpu::Theme::Dark,
+ &Theme::Dark,
&renderer::Style { text_color: Color::WHITE },
&mut clipboard,
&mut debug,
@@ -221,7 +227,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
height: size.height,
present_mode: wgpu::PresentMode::AutoVsync,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
- view_formats: vec![]
+ view_formats: vec![],
},
);
@@ -254,8 +260,9 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
renderer.with_primitives(|backend, primitive| {
backend.present(
&device,
- &mut staging_belt,
+ &queue,
&mut encoder,
+ None,
&view,
primitive,
&viewport,
@@ -264,7 +271,6 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
});
// Then we submit the work
- staging_belt.finish();
queue.submit(Some(encoder.finish()));
frame.present();
@@ -274,10 +280,6 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
state.mouse_interaction(),
),
);
-
- // And recall staging buffers
- staging_belt.recall();
-
}
Err(error) => match error {
wgpu::SurfaceError::OutOfMemory => {
diff --git a/examples/integration_wgpu/src/scene.rs b/examples/integration/src/scene.rs
index 3e41fbda..90c7efbf 100644
--- a/examples/integration_wgpu/src/scene.rs
+++ b/examples/integration/src/scene.rs
@@ -1,5 +1,5 @@
use iced_wgpu::wgpu;
-use iced_winit::Color;
+use iced_winit::core::Color;
pub struct Scene {
pipeline: wgpu::RenderPipeline,
diff --git a/examples/integration_wgpu/src/shader/frag.wgsl b/examples/integration/src/shader/frag.wgsl
index cf27bb56..cf27bb56 100644
--- a/examples/integration_wgpu/src/shader/frag.wgsl
+++ b/examples/integration/src/shader/frag.wgsl
diff --git a/examples/integration_wgpu/src/shader/vert.wgsl b/examples/integration/src/shader/vert.wgsl
index e353e6ba..e353e6ba 100644
--- a/examples/integration_wgpu/src/shader/vert.wgsl
+++ b/examples/integration/src/shader/vert.wgsl
diff --git a/examples/integration_opengl/Cargo.toml b/examples/integration_opengl/Cargo.toml
deleted file mode 100644
index 6dac999c..00000000
--- a/examples/integration_opengl/Cargo.toml
+++ /dev/null
@@ -1,12 +0,0 @@
-[package]
-name = "integration_opengl"
-version = "0.1.0"
-authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
-edition = "2021"
-publish = false
-
-[dependencies]
-iced_glutin = { path = "../../glutin" }
-iced_glow = { path = "../../glow" }
-iced_winit = { path = "../../winit" }
-env_logger = "0.8"
diff --git a/examples/integration_opengl/README.md b/examples/integration_opengl/README.md
deleted file mode 100644
index b7c2c074..00000000
--- a/examples/integration_opengl/README.md
+++ /dev/null
@@ -1,16 +0,0 @@
-## OpenGL integration
-
-A demonstration of how to integrate Iced in an existing graphical OpenGL application.
-
-The __[`main`]__ file contains all the code of the example.
-
-<div align="center">
- <a href="https://imgbox.com/9P9ETcod" target="_blank"><img src="https://images2.imgbox.com/2a/51/9P9ETcod_o.gif" alt="image host"/></a>
-</div>
-
-You can run it with `cargo run`:
-```
-cargo run --package integration_opengl
-```
-
-[`main`]: src/main.rs
diff --git a/examples/integration_opengl/src/controls.rs b/examples/integration_opengl/src/controls.rs
deleted file mode 100644
index c3648f44..00000000
--- a/examples/integration_opengl/src/controls.rs
+++ /dev/null
@@ -1,101 +0,0 @@
-use iced_glow::Renderer;
-use iced_glutin::widget::Slider;
-use iced_glutin::widget::{Column, Row, Text};
-use iced_glutin::{Alignment, Color, Command, Element, Length, Program};
-
-pub struct Controls {
- background_color: Color,
-}
-
-#[derive(Debug, Clone)]
-pub enum Message {
- BackgroundColorChanged(Color),
-}
-
-impl Controls {
- pub fn new() -> Controls {
- Controls {
- background_color: Color::BLACK,
- }
- }
-
- pub fn background_color(&self) -> Color {
- self.background_color
- }
-}
-
-impl Program for Controls {
- type Renderer = Renderer;
- type Message = Message;
-
- fn update(&mut self, message: Message) -> Command<Message> {
- match message {
- Message::BackgroundColorChanged(color) => {
- self.background_color = color;
- }
- }
-
- Command::none()
- }
-
- fn view(&self) -> Element<Message, Renderer> {
- let background_color = self.background_color;
-
- let sliders = Row::new()
- .width(500)
- .spacing(20)
- .push(
- Slider::new(0.0..=1.0, background_color.r, move |r| {
- Message::BackgroundColorChanged(Color {
- r,
- ..background_color
- })
- })
- .step(0.01),
- )
- .push(
- Slider::new(0.0..=1.0, background_color.g, move |g| {
- Message::BackgroundColorChanged(Color {
- g,
- ..background_color
- })
- })
- .step(0.01),
- )
- .push(
- Slider::new(0.0..=1.0, background_color.b, move |b| {
- Message::BackgroundColorChanged(Color {
- b,
- ..background_color
- })
- })
- .step(0.01),
- );
-
- Row::new()
- .width(Length::Fill)
- .height(Length::Fill)
- .align_items(Alignment::End)
- .push(
- Column::new()
- .width(Length::Fill)
- .align_items(Alignment::End)
- .push(
- Column::new()
- .padding(10)
- .spacing(10)
- .push(
- Text::new("Background color")
- .style(Color::WHITE),
- )
- .push(sliders)
- .push(
- Text::new(format!("{background_color:?}"))
- .size(14)
- .style(Color::WHITE),
- ),
- ),
- )
- .into()
- }
-}
diff --git a/examples/integration_opengl/src/main.rs b/examples/integration_opengl/src/main.rs
deleted file mode 100644
index f161c8a0..00000000
--- a/examples/integration_opengl/src/main.rs
+++ /dev/null
@@ -1,187 +0,0 @@
-mod controls;
-mod scene;
-
-use controls::Controls;
-use scene::Scene;
-
-use glow::*;
-use glutin::dpi::PhysicalPosition;
-use glutin::event::{Event, ModifiersState, WindowEvent};
-use glutin::event_loop::ControlFlow;
-use iced_glow::glow;
-use iced_glow::{Backend, Renderer, Settings, Viewport};
-use iced_glutin::conversion;
-use iced_glutin::glutin;
-use iced_glutin::renderer;
-use iced_glutin::{program, Clipboard, Color, Debug, Size};
-
-pub fn main() {
- env_logger::init();
- let (gl, event_loop, windowed_context, shader_version) = {
- let el = glutin::event_loop::EventLoop::new();
-
- let wb = glutin::window::WindowBuilder::new()
- .with_title("OpenGL integration example")
- .with_inner_size(glutin::dpi::LogicalSize::new(1024.0, 768.0));
-
- let windowed_context = glutin::ContextBuilder::new()
- .with_vsync(true)
- .build_windowed(wb, &el)
- .unwrap();
-
- unsafe {
- let windowed_context = windowed_context.make_current().unwrap();
-
- let gl = glow::Context::from_loader_function(|s| {
- windowed_context.get_proc_address(s) as *const _
- });
-
- // Enable auto-conversion from/to sRGB
- gl.enable(glow::FRAMEBUFFER_SRGB);
-
- // Enable alpha blending
- gl.enable(glow::BLEND);
- gl.blend_func(glow::SRC_ALPHA, glow::ONE_MINUS_SRC_ALPHA);
-
- // Disable multisampling by default
- gl.disable(glow::MULTISAMPLE);
-
- (gl, el, windowed_context, "#version 410")
- }
- };
-
- let physical_size = windowed_context.window().inner_size();
- let mut viewport = Viewport::with_physical_size(
- Size::new(physical_size.width, physical_size.height),
- windowed_context.window().scale_factor(),
- );
-
- let mut cursor_position = PhysicalPosition::new(-1.0, -1.0);
- let mut modifiers = ModifiersState::default();
- let mut clipboard = Clipboard::connect(windowed_context.window());
-
- let mut renderer = Renderer::new(Backend::new(&gl, Settings::default()));
-
- let mut debug = Debug::new();
-
- let controls = Controls::new();
- let mut state = program::State::new(
- controls,
- viewport.logical_size(),
- &mut renderer,
- &mut debug,
- );
- let mut resized = false;
-
- let scene = Scene::new(&gl, shader_version);
-
- event_loop.run(move |event, _, control_flow| {
- *control_flow = ControlFlow::Wait;
-
- match event {
- Event::WindowEvent { event, .. } => {
- match event {
- WindowEvent::CursorMoved { position, .. } => {
- cursor_position = position;
- }
- WindowEvent::ModifiersChanged(new_modifiers) => {
- modifiers = new_modifiers;
- }
- WindowEvent::Resized(physical_size) => {
- viewport = Viewport::with_physical_size(
- Size::new(
- physical_size.width,
- physical_size.height,
- ),
- windowed_context.window().scale_factor(),
- );
-
- resized = true;
- }
- WindowEvent::CloseRequested => {
- scene.cleanup(&gl);
- *control_flow = ControlFlow::Exit
- }
- _ => (),
- }
-
- // Map window event to iced event
- if let Some(event) = iced_winit::conversion::window_event(
- &event,
- windowed_context.window().scale_factor(),
- modifiers,
- ) {
- state.queue_event(event);
- }
- }
- Event::MainEventsCleared => {
- // If there are events pending
- if !state.is_queue_empty() {
- // We update iced
- let _ = state.update(
- viewport.logical_size(),
- conversion::cursor_position(
- cursor_position,
- viewport.scale_factor(),
- ),
- &mut renderer,
- &iced_glow::Theme::Dark,
- &renderer::Style {
- text_color: Color::WHITE,
- },
- &mut clipboard,
- &mut debug,
- );
-
- // and request a redraw
- windowed_context.window().request_redraw();
- }
- }
- Event::RedrawRequested(_) => {
- if resized {
- let size = windowed_context.window().inner_size();
-
- unsafe {
- gl.viewport(
- 0,
- 0,
- size.width as i32,
- size.height as i32,
- );
- }
-
- resized = false;
- }
-
- let program = state.program();
- {
- // We clear the frame
- scene.clear(&gl, program.background_color());
-
- // Draw the scene
- scene.draw(&gl);
- }
-
- // And then iced on top
- renderer.with_primitives(|backend, primitive| {
- backend.present(
- &gl,
- primitive,
- &viewport,
- &debug.overlay(),
- );
- });
-
- // Update the mouse cursor
- windowed_context.window().set_cursor_icon(
- iced_winit::conversion::mouse_interaction(
- state.mouse_interaction(),
- ),
- );
-
- windowed_context.swap_buffers().unwrap();
- }
- _ => (),
- }
- });
-}
diff --git a/examples/integration_opengl/src/scene.rs b/examples/integration_opengl/src/scene.rs
deleted file mode 100644
index c1d05b65..00000000
--- a/examples/integration_opengl/src/scene.rs
+++ /dev/null
@@ -1,102 +0,0 @@
-use glow::*;
-use iced_glow::glow;
-use iced_glow::Color;
-
-pub struct Scene {
- program: glow::Program,
- vertex_array: glow::VertexArray,
-}
-
-impl Scene {
- pub fn new(gl: &glow::Context, shader_version: &str) -> Self {
- unsafe {
- let vertex_array = gl
- .create_vertex_array()
- .expect("Cannot create vertex array");
- gl.bind_vertex_array(Some(vertex_array));
-
- let program = gl.create_program().expect("Cannot create program");
-
- let (vertex_shader_source, fragment_shader_source) = (
- r#"const vec2 verts[3] = vec2[3](
- vec2(0.5f, 1.0f),
- vec2(0.0f, 0.0f),
- vec2(1.0f, 0.0f)
- );
- out vec2 vert;
- void main() {
- vert = verts[gl_VertexID];
- gl_Position = vec4(vert - 0.5, 0.0, 1.0);
- }"#,
- r#"precision highp float;
- in vec2 vert;
- out vec4 color;
- void main() {
- color = vec4(vert, 0.5, 1.0);
- }"#,
- );
-
- let shader_sources = [
- (glow::VERTEX_SHADER, vertex_shader_source),
- (glow::FRAGMENT_SHADER, fragment_shader_source),
- ];
-
- let mut shaders = Vec::with_capacity(shader_sources.len());
-
- for (shader_type, shader_source) in shader_sources.iter() {
- let shader = gl
- .create_shader(*shader_type)
- .expect("Cannot create shader");
- gl.shader_source(
- shader,
- &format!("{shader_version}\n{shader_source}"),
- );
- gl.compile_shader(shader);
- if !gl.get_shader_compile_status(shader) {
- panic!("{}", gl.get_shader_info_log(shader));
- }
- gl.attach_shader(program, shader);
- shaders.push(shader);
- }
-
- gl.link_program(program);
- if !gl.get_program_link_status(program) {
- panic!("{}", gl.get_program_info_log(program));
- }
-
- for shader in shaders {
- gl.detach_shader(program, shader);
- gl.delete_shader(shader);
- }
-
- gl.use_program(Some(program));
- Self {
- program,
- vertex_array,
- }
- }
- }
-
- pub fn clear(&self, gl: &glow::Context, background_color: Color) {
- let [r, g, b, a] = background_color.into_linear();
- unsafe {
- gl.clear_color(r, g, b, a);
- gl.clear(glow::COLOR_BUFFER_BIT);
- }
- }
-
- pub fn draw(&self, gl: &glow::Context) {
- unsafe {
- gl.bind_vertex_array(Some(self.vertex_array));
- gl.use_program(Some(self.program));
- gl.draw_arrays(glow::TRIANGLES, 0, 3);
- }
- }
-
- pub fn cleanup(&self, gl: &glow::Context) {
- unsafe {
- gl.delete_program(self.program);
- gl.delete_vertex_array(self.vertex_array);
- }
- }
-}