commit bad gpu stuff
This commit is contained in:
parent
5a6b7756e4
commit
8842c5a364
4 changed files with 854 additions and 61 deletions
679
Cargo.lock
generated
679
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -6,9 +6,12 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytemuck = "1.16.0"
|
bytemuck = "1.16.0"
|
||||||
eframe = {version = "0.27", features = ["wgpu"]}
|
eframe = {version = "0.27", features = ["wgpu"]}
|
||||||
|
egui-file-dialog = "0.5.0"
|
||||||
egui_extras = "0.27.2"
|
egui_extras = "0.27.2"
|
||||||
env_logger = "0.11.3"
|
env_logger = "0.11.3"
|
||||||
|
image = "0.25.1"
|
||||||
nalgebra = "0.32.5"
|
nalgebra = "0.32.5"
|
||||||
|
threadpool = "1.8.1"
|
||||||
# winit = { version = "0.30", features = ["rwh_05" ] }
|
# winit = { version = "0.30", features = ["rwh_05" ] }
|
||||||
# log = "0.4"
|
# log = "0.4"
|
||||||
# console_error_panic_hook = "0.1.6"
|
# console_error_panic_hook = "0.1.6"
|
||||||
|
|
197
src/lib.rs
197
src/lib.rs
|
@ -1,16 +1,22 @@
|
||||||
use std::num::NonZeroU64;
|
use std::{num::NonZeroU64, path::PathBuf};
|
||||||
|
|
||||||
use cb::TriangleRenderResources;
|
use cb::TriangleRenderResources;
|
||||||
use eframe::{
|
use eframe::{
|
||||||
egui, egui_wgpu,
|
egui, egui_wgpu,
|
||||||
wgpu::{self, util::DeviceExt},
|
wgpu::{self, util::DeviceExt},
|
||||||
};
|
};
|
||||||
|
use egui_file_dialog::FileDialog;
|
||||||
use nalgebra::{Matrix3, SMatrix};
|
use nalgebra::{Matrix3, SMatrix};
|
||||||
|
|
||||||
use crate::cb::CustomTriangleCallback;
|
use crate::cb::CustomTriangleCallback;
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
mat: SMatrix<f32, 3, 3>,
|
color_matrix: SMatrix<f32, 4, 4>,
|
||||||
|
pos_matrix: SMatrix<f32, 3, 3>,
|
||||||
|
file_dialog: FileDialog,
|
||||||
|
new_file: Option<PathBuf>,
|
||||||
|
cur_img_size: Option<egui::Vec2>,
|
||||||
|
image_loaded: bool,
|
||||||
}
|
}
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Option<Self> {
|
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Option<Self> {
|
||||||
|
@ -20,28 +26,88 @@ impl App {
|
||||||
|
|
||||||
let device = &wgpu_render_state.device;
|
let device = &wgpu_render_state.device;
|
||||||
|
|
||||||
|
// let cat = include_bytes!("./cat.jpg");
|
||||||
|
// let cat_img = image::load_from_memory(cat).unwrap();
|
||||||
|
// let cat_rgba = cat_img.to_rgba();
|
||||||
|
|
||||||
|
// use image::GenericImageView;
|
||||||
|
// let dimensions = cat_img.dimensions();
|
||||||
|
|
||||||
|
// let tex_size = wgpu::Extent3d {
|
||||||
|
// width: dimensions.0,
|
||||||
|
// height: dimensions.1,
|
||||||
|
// depth_or_array_layers: 1,
|
||||||
|
// };
|
||||||
|
// let cat_tex = device.create_texture(&wgpu::TextureDescriptor {
|
||||||
|
// size: tex_size,
|
||||||
|
// mip_level_count: 1,
|
||||||
|
// sample_count: 1,
|
||||||
|
// dimension: wgpu::TextureDimension::D2,
|
||||||
|
// format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||||
|
// usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||||
|
// label: Some("cat"),
|
||||||
|
// view_formats: &[],
|
||||||
|
// });
|
||||||
|
|
||||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||||
label: Some("custom3d"),
|
label: Some("shader"),
|
||||||
source: wgpu::ShaderSource::Wgsl(include_str!("./custom3d_wgpu_shader.wgsl").into()),
|
source: wgpu::ShaderSource::Wgsl(include_str!("./shader.wgsl").into()),
|
||||||
});
|
});
|
||||||
|
|
||||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
let tex_bind_group_layout =
|
||||||
label: Some("custom3d"),
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
entries: &[wgpu::BindGroupLayoutEntry {
|
label: Some("tex_bind_group_layout"),
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
visibility: wgpu::ShaderStages::VERTEX,
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
|
ty: wgpu::BindingType::Texture {
|
||||||
|
multisampled: false,
|
||||||
|
view_dimension: wgpu::TextureViewDimension::D2,
|
||||||
|
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
||||||
|
},
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 1,
|
||||||
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
|
// This should match the filterable field of the
|
||||||
|
// corresponding Texture entry above.
|
||||||
|
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
let matrices_bind_group_layout =
|
||||||
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
|
label: Some("matrices_bind_group_layout"),
|
||||||
|
entries: &[
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 0,
|
||||||
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
ty: wgpu::BindingType::Buffer {
|
ty: wgpu::BindingType::Buffer {
|
||||||
ty: wgpu::BufferBindingType::Uniform,
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
has_dynamic_offset: false,
|
has_dynamic_offset: false,
|
||||||
min_binding_size: NonZeroU64::new(16),
|
min_binding_size: Some(NonZeroU64::new(64)?),
|
||||||
},
|
},
|
||||||
count: None,
|
count: None,
|
||||||
}],
|
},
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 1,
|
||||||
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
|
ty: wgpu::BindingType::Buffer {
|
||||||
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
|
has_dynamic_offset: false,
|
||||||
|
min_binding_size: Some(NonZeroU64::new(36)?),
|
||||||
|
},
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
label: Some("custom3d"),
|
label: Some("pipeline_layout"),
|
||||||
bind_group_layouts: &[&bind_group_layout],
|
bind_group_layouts: &[&tex_bind_group_layout, &matrices_bind_group_layout],
|
||||||
push_constant_ranges: &[],
|
push_constant_ranges: &[],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -66,64 +132,98 @@ impl App {
|
||||||
multiview: None,
|
multiview: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
// let color_matrix_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
label: Some("custom3d"),
|
// label: Some("color_matrix_buffer"),
|
||||||
contents: bytemuck::cast_slice(&[0.0_f32; 4]), // 16 bytes aligned!
|
// contents: bytemuck::cast_slice(&[0.0_f32; 4]), // 16 bytes aligned!
|
||||||
// Mapping at creation (as done by the create_buffer_init utility) doesn't require us to to add the MAP_WRITE usage
|
// // Mapping at creation (as done by the create_buffer_init utility) doesn't require us to to add the MAP_WRITE usage
|
||||||
// (this *happens* to workaround this bug )
|
// // (this *happens* to workaround this bug )
|
||||||
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::UNIFORM,
|
// usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::UNIFORM,
|
||||||
});
|
// });
|
||||||
|
|
||||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
// let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
label: Some("custom3d"),
|
// label: Some("custom3d"),
|
||||||
layout: &bind_group_layout,
|
// layout: &bind_group_layout,
|
||||||
entries: &[wgpu::BindGroupEntry {
|
// entries: &[wgpu::BindGroupEntry {
|
||||||
binding: 0,
|
// binding: 0,
|
||||||
resource: uniform_buffer.as_entire_binding(),
|
// resource: uniform_buffer.as_entire_binding(),
|
||||||
}],
|
// }],
|
||||||
});
|
// });
|
||||||
|
|
||||||
// Because the graphics pipeline must have the same lifetime as the egui render pass,
|
// Because the graphics pipeline must have the same lifetime as the egui render pass,
|
||||||
// instead of storing the pipeline in our `Custom3D` struct, we insert it into the
|
// instead of storing the pipeline in our `Custom3D` struct, we insert it into the
|
||||||
// `paint_callback_resources` type map, which is stored alongside the render pass.
|
// `paint_callback_resources` type map, which is stored alongside the render pass.
|
||||||
wgpu_render_state
|
|
||||||
.renderer
|
// wgpu_render_state
|
||||||
.write()
|
// .renderer
|
||||||
.callback_resources
|
// .write()
|
||||||
.insert(TriangleRenderResources {
|
// .callback_resources
|
||||||
pipeline,
|
// .insert(TriangleRenderResources {
|
||||||
bind_group,
|
// pipeline,
|
||||||
uniform_buffer,
|
// bind_group,
|
||||||
});
|
// uniform_buffer,
|
||||||
|
// });
|
||||||
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
mat: SMatrix::identity(),
|
color_matrix: SMatrix::identity(),
|
||||||
|
pos_matrix: SMatrix::identity(),
|
||||||
|
file_dialog: FileDialog::new(),
|
||||||
|
new_file: None,
|
||||||
|
image_loaded: false,
|
||||||
|
cur_img_size: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl eframe::App for App {
|
impl eframe::App for App {
|
||||||
fn update(&mut self, ctx: &eframe::egui::Context, frame: &mut eframe::Frame) {
|
fn update(&mut self, ctx: &eframe::egui::Context, frame: &mut eframe::Frame) {
|
||||||
|
let mut img = None;
|
||||||
|
egui::SidePanel::right("sidebar").show(ctx, |ui| {
|
||||||
|
if ui.button("Select Image").clicked() {
|
||||||
|
self.file_dialog.select_file()
|
||||||
|
}
|
||||||
|
if self.file_dialog.update(ctx).selected().is_some() {
|
||||||
|
if let Some(path) = self.file_dialog.take_selected() {
|
||||||
|
println!("path: {path:?}");
|
||||||
|
if path.ends_with(".jpg") || path.ends_with(".jpeg") || path.ends_with(".png") {
|
||||||
|
img = Some(image::open(path).unwrap().to_rgba8());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.add_space(8.);
|
||||||
|
ui.label("Color Matrix Editor");
|
||||||
|
components::mat_editor(ui, &mut self.color_matrix, "color_matrix");
|
||||||
|
ui.add_space(8.);
|
||||||
|
ui.label("Position Matrix Editor");
|
||||||
|
components::mat_editor(ui, &mut self.pos_matrix, "pos_matrix");
|
||||||
|
});
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
ui.heading("My egui Application");
|
ui.heading("My egui Application");
|
||||||
|
|
||||||
components::mat_editor(ui, &mut self.mat);
|
if let Some(dimensions) = self.cur_img_size {
|
||||||
|
|
||||||
egui::Frame::canvas(ui.style()).show(ui, |ui| {
|
egui::Frame::canvas(ui.style()).show(ui, |ui| {
|
||||||
let (rect, response) =
|
let (rect, response) = ui.allocate_exact_size(dimensions, egui::Sense::drag());
|
||||||
ui.allocate_exact_size(egui::Vec2::splat(300.0), egui::Sense::drag());
|
|
||||||
dbg!(response);
|
|
||||||
|
|
||||||
let callback = egui_wgpu::Callback::new_paint_callback(
|
// let callback = egui_wgpu::Callback::new_paint_callback(
|
||||||
rect,
|
// rect,
|
||||||
CustomTriangleCallback { angle: 0.5 },
|
// CustomTriangleCallback { angle: 0.5 },
|
||||||
);
|
// );
|
||||||
ui.painter().add(callback);
|
// ui.painter().add(callback);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod gpu_proc {
|
||||||
|
use eframe::wgpu;
|
||||||
|
|
||||||
|
pub struct GpuResources {
|
||||||
|
pipeline: wgpu::RenderPipeline,
|
||||||
|
// img_buffer: Option<wgpu::
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod components {
|
mod components {
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
use nalgebra::{Matrix, SMatrix};
|
use nalgebra::{Matrix, SMatrix};
|
||||||
|
@ -131,8 +231,9 @@ mod components {
|
||||||
pub fn mat_editor<const R: usize, const C: usize>(
|
pub fn mat_editor<const R: usize, const C: usize>(
|
||||||
ui: &mut egui::Ui,
|
ui: &mut egui::Ui,
|
||||||
mat: &mut SMatrix<f32, R, C>,
|
mat: &mut SMatrix<f32, R, C>,
|
||||||
|
id: &str,
|
||||||
) -> egui::Response {
|
) -> egui::Response {
|
||||||
egui::Grid::new("mat")
|
egui::Grid::new(id)
|
||||||
.show(ui, |ui| {
|
.show(ui, |ui| {
|
||||||
mat.row_iter_mut().enumerate().for_each(|(i, mut row)| {
|
mat.row_iter_mut().enumerate().for_each(|(i, mut row)| {
|
||||||
row.iter_mut().for_each(|item| {
|
row.iter_mut().for_each(|item| {
|
||||||
|
|
18
src/shader.wgsl
Normal file
18
src/shader.wgsl
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
var<private> v_positions: array<vec2<f32>, 6> = array<vec2<f32>, 6>(
|
||||||
|
vec2<f32>(1., 1.),
|
||||||
|
vec2<f32>(1., -1.),
|
||||||
|
vec2<f32>(-1., 1.),
|
||||||
|
vec2<f32>(1., -1.),
|
||||||
|
vec2<f32>(-1., 1.),
|
||||||
|
vec2<f32>(-1., -1.),
|
||||||
|
);
|
||||||
|
|
||||||
|
@vertex
|
||||||
|
fn vs_main(@builtin(vertex_index) v_idx: u32) -> @builtin(position) vec4<f32> {
|
||||||
|
return vec4<f32>(v_positions[v_idx], 0., 0.);
|
||||||
|
}
|
||||||
|
|
||||||
|
@fragment
|
||||||
|
fn fs_main() -> @location(0) vec4<f32> {
|
||||||
|
return vec4<f32>(0.8, 0.2, 0.8, 1.);
|
||||||
|
}
|
Loading…
Reference in a new issue