commit bad gpu stuff

This commit is contained in:
Schrottkatze 2024-05-21 08:57:37 +02:00
parent 5a6b7756e4
commit 8842c5a364
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
4 changed files with 854 additions and 61 deletions

679
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,9 +6,12 @@ edition = "2021"
[dependencies]
bytemuck = "1.16.0"
eframe = {version = "0.27", features = ["wgpu"]}
egui-file-dialog = "0.5.0"
egui_extras = "0.27.2"
env_logger = "0.11.3"
image = "0.25.1"
nalgebra = "0.32.5"
threadpool = "1.8.1"
# winit = { version = "0.30", features = ["rwh_05" ] }
# log = "0.4"
# console_error_panic_hook = "0.1.6"

View file

@ -1,16 +1,22 @@
use std::num::NonZeroU64;
use std::{num::NonZeroU64, path::PathBuf};
use cb::TriangleRenderResources;
use eframe::{
egui, egui_wgpu,
wgpu::{self, util::DeviceExt},
};
use egui_file_dialog::FileDialog;
use nalgebra::{Matrix3, SMatrix};
use crate::cb::CustomTriangleCallback;
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 {
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Option<Self> {
@ -20,28 +26,88 @@ impl App {
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 {
label: Some("custom3d"),
source: wgpu::ShaderSource::Wgsl(include_str!("./custom3d_wgpu_shader.wgsl").into()),
label: Some("shader"),
source: wgpu::ShaderSource::Wgsl(include_str!("./shader.wgsl").into()),
});
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("custom3d"),
entries: &[wgpu::BindGroupLayoutEntry {
let tex_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("tex_bind_group_layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
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::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: NonZeroU64::new(16),
min_binding_size: Some(NonZeroU64::new(64)?),
},
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 {
label: Some("custom3d"),
bind_group_layouts: &[&bind_group_layout],
label: Some("pipeline_layout"),
bind_group_layouts: &[&tex_bind_group_layout, &matrices_bind_group_layout],
push_constant_ranges: &[],
});
@ -66,64 +132,98 @@ impl App {
multiview: None,
});
let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("custom3d"),
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
// (this *happens* to workaround this bug )
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::UNIFORM,
});
// let color_matrix_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
// label: Some("color_matrix_buffer"),
// 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
// // (this *happens* to workaround this bug )
// usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::UNIFORM,
// });
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("custom3d"),
layout: &bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
}],
});
// let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
// label: Some("custom3d"),
// layout: &bind_group_layout,
// entries: &[wgpu::BindGroupEntry {
// binding: 0,
// resource: uniform_buffer.as_entire_binding(),
// }],
// });
// 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
// `paint_callback_resources` type map, which is stored alongside the render pass.
wgpu_render_state
.renderer
.write()
.callback_resources
.insert(TriangleRenderResources {
pipeline,
bind_group,
uniform_buffer,
});
// wgpu_render_state
// .renderer
// .write()
// .callback_resources
// .insert(TriangleRenderResources {
// pipeline,
// bind_group,
// uniform_buffer,
// });
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 {
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| {
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| {
let (rect, response) =
ui.allocate_exact_size(egui::Vec2::splat(300.0), egui::Sense::drag());
dbg!(response);
let (rect, response) = ui.allocate_exact_size(dimensions, egui::Sense::drag());
let callback = egui_wgpu::Callback::new_paint_callback(
rect,
CustomTriangleCallback { angle: 0.5 },
);
ui.painter().add(callback);
// let callback = egui_wgpu::Callback::new_paint_callback(
// rect,
// CustomTriangleCallback { angle: 0.5 },
// );
// ui.painter().add(callback);
});
}
});
}
}
mod gpu_proc {
use eframe::wgpu;
pub struct GpuResources {
pipeline: wgpu::RenderPipeline,
// img_buffer: Option<wgpu::
}
}
mod components {
use eframe::egui;
use nalgebra::{Matrix, SMatrix};
@ -131,8 +231,9 @@ mod components {
pub fn mat_editor<const R: usize, const C: usize>(
ui: &mut egui::Ui,
mat: &mut SMatrix<f32, R, C>,
id: &str,
) -> egui::Response {
egui::Grid::new("mat")
egui::Grid::new(id)
.show(ui, |ui| {
mat.row_iter_mut().enumerate().for_each(|(i, mut row)| {
row.iter_mut().for_each(|item| {

18
src/shader.wgsl Normal file
View 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.);
}