Out with dear imgui, in with egui
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use egui::Widget;
|
||||
use wgpu::{util::DeviceExt as _, BindGroup, BindGroupLayout, RenderPipeline};
|
||||
|
||||
use crate::graphics::TextureSink;
|
||||
|
||||
pub struct GameScreen {
|
||||
bind_group: Arc<BindGroup>,
|
||||
}
|
||||
|
||||
impl GameScreen {
|
||||
pub fn init_pipeline(render_state: &egui_wgpu::RenderState) {
|
||||
let device = &render_state.device;
|
||||
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("texture bind group layout"),
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Texture {
|
||||
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
||||
view_dimension: wgpu::TextureViewDimension::D2,
|
||||
multisampled: false,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 2,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let shader = device.create_shader_module(wgpu::include_wgsl!("../anaglyph.wgsl"));
|
||||
let render_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("render pipeline layout"),
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
});
|
||||
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("render pipeline"),
|
||||
layout: Some(&render_pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &[],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: wgpu::TextureFormat::Bgra8Unorm,
|
||||
blend: Some(wgpu::BlendState::REPLACE),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
unclipped_depth: false,
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
},
|
||||
multiview: None,
|
||||
cache: None,
|
||||
});
|
||||
|
||||
render_state
|
||||
.renderer
|
||||
.write()
|
||||
.callback_resources
|
||||
.insert(SharedGameScreenResources {
|
||||
pipeline: render_pipeline,
|
||||
bind_group_layout,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn init(render_state: &egui_wgpu::RenderState) -> (Self, TextureSink) {
|
||||
let device = &render_state.device;
|
||||
let queue = &render_state.queue;
|
||||
|
||||
let (sink, texture_view) = TextureSink::new(device, queue.clone());
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor::default());
|
||||
let colors = Colors {
|
||||
left: [1.0, 0.0, 0.0, 1.0],
|
||||
right: [0.0, 0.7734375, 0.9375, 1.0],
|
||||
};
|
||||
|
||||
let color_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("colors"),
|
||||
contents: bytemuck::bytes_of(&colors),
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
});
|
||||
|
||||
let renderer = render_state.renderer.read();
|
||||
let resources: &SharedGameScreenResources = renderer.callback_resources.get().unwrap();
|
||||
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("bind group"),
|
||||
layout: &resources.bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(&texture_view),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::Sampler(&sampler),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 2,
|
||||
resource: color_buf.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
(
|
||||
Self {
|
||||
bind_group: Arc::new(bind_group),
|
||||
},
|
||||
sink,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for &GameScreen {
|
||||
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
|
||||
let response = ui.allocate_rect(ui.clip_rect(), egui::Sense::hover());
|
||||
let callback = egui_wgpu::Callback::new_paint_callback(
|
||||
response.rect,
|
||||
GameScreenCallback {
|
||||
bind_group: self.bind_group.clone(),
|
||||
},
|
||||
);
|
||||
ui.painter().add(callback);
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
struct GameScreenCallback {
|
||||
bind_group: Arc<BindGroup>,
|
||||
}
|
||||
|
||||
impl egui_wgpu::CallbackTrait for GameScreenCallback {
|
||||
fn paint(
|
||||
&self,
|
||||
_info: egui::PaintCallbackInfo,
|
||||
render_pass: &mut wgpu::RenderPass<'static>,
|
||||
callback_resources: &egui_wgpu::CallbackResources,
|
||||
) {
|
||||
let resources: &SharedGameScreenResources = callback_resources.get().unwrap();
|
||||
// TODO: maintain aspect ratio
|
||||
render_pass.set_pipeline(&resources.pipeline);
|
||||
render_pass.set_bind_group(0, &self.bind_group, &[]);
|
||||
render_pass.draw(0..6, 0..1);
|
||||
}
|
||||
}
|
||||
|
||||
struct SharedGameScreenResources {
|
||||
pipeline: RenderPipeline,
|
||||
bind_group_layout: BindGroupLayout,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
#[repr(C)]
|
||||
struct Colors {
|
||||
left: [f32; 4],
|
||||
right: [f32; 4],
|
||||
}
|
||||
Reference in New Issue
Block a user