Skip to content

Commit

Permalink
Fix cube example
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Sep 22, 2021
1 parent a81670c commit 129324f
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 176 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ Per Keep a Changelog there are 6 main categories of changes:

## Unreleased

#### Updated
- updated imgui dependency to `>=0.1,<0.9`

#### Removed
- unstable simple-api is now it's own, unpublished, crate.

## v0.17.0

Released 2021-09-04
Expand Down
32 changes: 9 additions & 23 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[workspace]
members = [".", "simple-api"]
default-members = [".", "simple-api"]
resolver = "2"

[package]
name = "imgui-wgpu"
version = "0.17.0"
Expand All @@ -11,22 +16,13 @@ readme = "README.md"
categories = ["gui", "graphics", "rendering", "rendering::graphics-api"]
keywords = ["gui", "graphics", "wgpu", "imgui"]
license = "MIT OR Apache-2.0"
resolver = "2"

exclude = [
".gitignore",
".github",
"resources",
]

[[example]]
name = "basic_simple_api"
required-features = ["simple_api_unstable"]

[[example]]
name = "fullscreen_simple_api"
required-features = ["simple_api_unstable"]

[[package.metadata.release.pre-release-replacements]]
file = "CHANGELOG.md"
search = "\\[Unreleased\\]\\(#unreleased\\)"
Expand All @@ -50,32 +46,22 @@ search = "<!-- Begin Diffs -->"
replace = "- [Unreleased](https://github.com/Yatekii/imgui-wgpu-rs/compare/v{{version}}...HEAD)"
min = 0 # allow non-first increment

[features]
# used in src/simple_api.rs
default = []
simple_api_unstable = [ "winit", "pollster", "imgui-winit-support" ]

[dependencies]
bytemuck = "1"
imgui = "0.7"
imgui = ">=0.1, <0.9"
log = "0.4"
smallvec = "1"
wgpu = { version = "0.10", features = ["spirv"] }

# deps for simple_api_unstable
imgui-winit-support = { version = "0.7", optional = true }
pollster = { version = "0.2.0", optional = true } # for block_on executor
winit = { version = ">= 0.23, < 0.25", optional = true }
wgpu = "0.10"

[dev-dependencies]
bytemuck = { version = "1.4", features = ["derive"] }
cgmath = "0.18"
env_logger = "0.9"
image = { version = "0.23", default-features = false, features = ["jpeg"] }
imgui-winit-support = "0.7"
imgui-winit-support = "0.8"
pollster = "0.2"
raw-window-handle = "0.3"
winit = "0.24"
winit = "0.25"

[package.metadata.docs.rs]
all-features = true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ cargo run --release --example hello_world

# Status

Basic features are useable. Uses `wgpu-0.9` and `imgui-0.7` upstream. `winit-0.24` is used with the examples.
Uses `wgpu-0.10` upstream. Compatible with all imgui versions after `0.1`. `winit-0.25` is used with the examples.

Contributions are very welcome.

Expand Down
3 changes: 1 addition & 2 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ skip = [
{ name = "dlib", version = "0.4.2" },
{ name = "libloading", version = "0.6.7" },
{ name = "nix", version = "0.18.0" },
{ name = "winapi", version = "0.2.8" },
{ name = "proc-macro-crate", version = "0.1.5" },
]

[advisories]
vulnerability = "deny"
unmaintained = "deny"
ignore = [
"RUSTSEC-2020-0016"
]

[sources]
Expand Down
10 changes: 0 additions & 10 deletions examples/basic_simple_api.rs

This file was deleted.

129 changes: 42 additions & 87 deletions examples/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ use bytemuck::{Pod, Zeroable};
use imgui::*;
use imgui_wgpu::{Renderer, RendererConfig, Texture, TextureConfig};
use pollster::block_on;
use std::num::NonZeroU32;
use std::time::Instant;
use wgpu::{util::DeviceExt, BlendState, Extent3d};
use wgpu::{include_wgsl, util::DeviceExt, Extent3d};
use winit::{
dpi::LogicalSize,
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::Window,
};

// Example code modified from https://github.com/gfx-rs/wgpu-rs/tree/master/examples/cube

const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 1.0,
);

// Example code modified from https://github.com/gfx-rs/wgpu-rs/tree/master/examples/cube
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct Vertex {
Expand Down Expand Up @@ -79,10 +77,8 @@ fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
}

fn create_texels(size: usize) -> Vec<u8> {
use std::iter;

(0..size * size)
.flat_map(|id| {
.map(|id| {
// get high five for recognizing this ;)
let cx = 3.0 * (id % size) as f32 / (size - 1) as f32 - 2.0;
let cy = 2.0 * (id / size) as f32 / (size - 1) as f32 - 1.0;
Expand All @@ -93,10 +89,7 @@ fn create_texels(size: usize) -> Vec<u8> {
y = 2.0 * old_x * y + cy;
count += 1;
}
iter::once(0xFF - (count * 5) as u8)
.chain(iter::once(0xFF - (count * 15) as u8))
.chain(iter::once(0xFF - (count * 50) as u8))
.chain(iter::once(1))
count
})
.collect()
}
Expand All @@ -112,10 +105,10 @@ struct Example {
}

impl Example {
fn generate_matrix(aspect_ratio: f32, theta: f32) -> cgmath::Matrix4<f32> {
fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4<f32> {
let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 10.0);
let mx_view = cgmath::Matrix4::look_at_rh(
cgmath::Point3::new(6.0 * theta.cos(), 6.0 * theta.sin(), 3.0),
cgmath::Point3::new(1.5f32, -5.0, 3.0),
cgmath::Point3::new(0f32, 0.0, 0.0),
cgmath::Vector3::unit_z(),
);
Expand All @@ -126,7 +119,7 @@ impl Example {

impl Example {
fn init(
surface_desc: &wgpu::SurfaceConfiguration,
config: &wgpu::SurfaceConfiguration,
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> Self {
Expand Down Expand Up @@ -167,20 +160,11 @@ impl Example {
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
sample_type: wgpu::TextureSampleType::Uint,
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler {
filtering: true,
comparison: false,
},
count: None,
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
Expand All @@ -203,38 +187,23 @@ impl Example {
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
format: wgpu::TextureFormat::R8Uint,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
});
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
queue.write_texture(
wgpu::ImageCopyTexture {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
texture.as_image_copy(),
&texels,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: NonZeroU32::new(4 * size),
bytes_per_row: Some(std::num::NonZeroU32::new(size).unwrap()),
rows_per_image: None,
},
texture_extent,
);

// Create other resources
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Nearest,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});
let mx_total =
Self::generate_matrix(surface_desc.width as f32 / surface_desc.height as f32, 0.0);
let mx_total = Self::generate_matrix(config.width as f32 / config.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();
let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
Expand All @@ -254,62 +223,48 @@ impl Example {
binding: 1,
resource: wgpu::BindingResource::TextureView(&texture_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::Sampler(&sampler),
},
],
label: None,
});

// Create the render pipeline
let vs_module =
device.create_shader_module(&wgpu::include_spirv!("../resources/cube.vert.spv"));
let fs_module =
device.create_shader_module(&wgpu::include_spirv!("../resources/cube.frag.spv"));
let shader = device.create_shader_module(&include_wgsl!("../resources/cube.wgsl"));

let vertex_buffers = [wgpu::VertexBufferLayout {
array_stride: vertex_size as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x4,
offset: 0,
shader_location: 0,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 4 * 4,
shader_location: 1,
},
],
}];

let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &vs_module,
entry_point: "main",
buffers: &[wgpu::VertexBufferLayout {
array_stride: vertex_size as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x4,
offset: 0,
shader_location: 0,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 4 * 4,
shader_location: 1,
},
],
}],
module: &shader,
entry_point: "vs_main",
buffers: &vertex_buffers,
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
targets: &[config.format.into()],
}),
primitive: wgpu::PrimitiveState {
front_face: wgpu::FrontFace::Ccw,
cull_mode: Some(wgpu::Face::Back),
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
fragment: Some(wgpu::FragmentState {
module: &fs_module,
entry_point: "main",
targets: &[wgpu::ColorTargetState {
format: surface_desc.format,
blend: Some(BlendState {
color: wgpu::BlendComponent::REPLACE,
alpha: wgpu::BlendComponent::REPLACE,
}),
write_mask: wgpu::ColorWrites::ALL,
}],
}),
});

// Done
Expand All @@ -329,7 +284,7 @@ impl Example {
}

fn setup_camera(&mut self, queue: &wgpu::Queue, size: [f32; 2]) {
let mx_total = Self::generate_matrix(size[0] / size[1], self.time * 0.1);
let mx_total = Self::generate_matrix(size[0] / size[1]);
let mx_ref: &[f32; 16] = mx_total.as_ref();
queue.write_buffer(&self.uniform_buf, 0, bytemuck::cast_slice(mx_ref));
}
Expand All @@ -341,14 +296,14 @@ impl Example {
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &view,
view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.1,
g: 0.2,
b: 0.3,
a: 0.1, // semi-transparent background
a: 1.0,
}),
store: true,
},
Expand Down Expand Up @@ -551,7 +506,7 @@ fn main() {
// Store the new size of Image() or None to indicate that the window is collapsed.
let mut new_example_size: Option<[f32; 2]> = None;

imgui::Window::new(im_str!("Cube"))
imgui::Window::new("Cube")
.size([512.0, 512.0], Condition::FirstUseEver)
.build(&ui, || {
new_example_size = Some(ui.content_region_avail());
Expand Down
6 changes: 3 additions & 3 deletions examples/custom_textures.rs → examples/custom-texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ fn main() {

{
let size = [width as f32, height as f32];
let window = imgui::Window::new(im_str!("Hello world"));
let window = imgui::Window::new("Hello world");
window
.size([400.0, 600.0], Condition::FirstUseEver)
.build(&ui, || {
ui.text(im_str!("Hello textures!"));
ui.text(im_str!("Say hello to Lenna.jpg"));
ui.text("Hello textures!");
ui.text("Say hello to Lenna.jpg");
Image::new(lenna_texture_id, size).build(&ui);
});
}
Expand Down
Loading

0 comments on commit 129324f

Please sign in to comment.