-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgizmo.rs
281 lines (233 loc) · 8.27 KB
/
gizmo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use math::{Vec2, Vec3, UnitQuaternion, Unit};
use gpu::{self, BufferImpl, CmdListImpl, DeviceImpl, TextureImpl};
#[repr(C)]
struct Vertex {
position: Vec3,
size: f32,
color: u32,
// TODO: Depth/visibility based alpha
}
fn circle_iter(radius: f32, segments: usize) -> impl Iterator<Item = Vec2> {
(0..segments).map(move |i| {
let angle = i as f32 * std::f32::consts::TAU / segments as f32;
let sin_cos = angle.sin_cos();
Vec2::new(sin_cos.0, sin_cos.1) * radius
})
}
fn arc_iter(radius: f32, segments: usize, start: f32, end: f32) -> impl Iterator<Item = Vec2> {
(0..segments + 1).map(move |i| {
let angle = start + (end - start) * i as f32 / segments as f32;
let sin_cos = angle.sin_cos();
Vec2::new(sin_cos.0, sin_cos.1) * radius
})
}
#[derive(Clone, Copy)]
pub struct Stroke {
pub color: u32,
pub width: f32,
}
impl From<u32> for Stroke {
fn from(color: u32) -> Self {
Self { color, width: 3.0 }
}
}
impl From<(u32, f32)> for Stroke {
fn from((color, width): (u32, f32)) -> Self {
Self { color, width }
}
}
pub struct Gizmo {
vertices: Vec<Vertex>,
}
impl Gizmo {
pub fn new() -> Self {
Self {
vertices: Vec::new(),
}
}
pub fn line(&mut self, start: Vec3, end: Vec3, stroke: impl Into<Stroke>) {
let stroke = stroke.into();
self.vertices.push(Vertex {
position: start,
size: stroke.width,
color: stroke.color,
});
self.vertices.push(Vertex {
position: end,
size: stroke.width,
color: stroke.color,
});
}
pub fn line_loop(&mut self, points: impl IntoIterator<Item = Vec3>, color: u32) {
let mut points = points.into_iter();
let mut prev = points.next().unwrap();
let first = prev;
for point in points {
self.line(prev, point, color);
prev = point;
}
// Close the loop
self.line(prev, first, color);
}
pub fn circle(&mut self, center: Vec3, normal: Unit<Vec3>, radius: f32, color: u32) {
let rotation = UnitQuaternion::between(Vec3::Z, normal);
let positions = circle_iter(radius, 64).map(|p| center + rotation * p.extend(0.0));
self.line_loop(positions, color);
}
pub fn sphere(&mut self, center: Vec3, radius: f32, color: u32) {
let positions = circle_iter(radius, 64).map(|p| center + Vec3::new(0.0, p.x, p.y));
self.line_loop(positions, color);
let positions = circle_iter(radius, 64).map(|p| center + Vec3::new(p.x, 0.0, p.y));
self.line_loop(positions, color);
let positions = circle_iter(radius, 64).map(|p| center + Vec3::new(p.x, p.y, 0.0));
self.line_loop(positions, color);
}
pub fn capsule(&mut self, center: Vec3, radius: f32, height: f32, color: u32) {
let cylinder_half_height = height / 2.0 - radius;
let pi = std::f32::consts::PI;
let res = 64;
let top_arc_x = arc_iter(radius, res, - pi / 2.0, pi / 2.0).map(|p| center + Vec3::new(0.0, p.x, p.y + cylinder_half_height));
let bottom_arc_x = arc_iter(radius, res, pi / 2.0, 3.0 * pi / 2.0).map(|p| center + Vec3::new(0.0, p.x, p.y - cylinder_half_height));
let positions = top_arc_x.chain(bottom_arc_x);
self.line_loop(positions, color);
let top_arc_y = arc_iter(radius, res, - pi / 2.0, pi / 2.0).map(|p| center + Vec3::new(p.x, 0.0, p.y + cylinder_half_height));
let bottom_arc_y = arc_iter(radius, res, pi / 2.0, 3.0 * pi / 2.0).map(|p| center + Vec3::new(p.x, 0.0, p.y - cylinder_half_height));
let positions = top_arc_y.chain(bottom_arc_y);
self.line_loop(positions, color);
let positions = circle_iter(radius, res).map(|p| center + Vec3::new(p.x, p.y, cylinder_half_height));
self.line_loop(positions, color);
let positions = circle_iter(radius, res).map(|p| center + Vec3::new(p.x, p.y, -cylinder_half_height));
self.line_loop(positions, color);
}
pub fn cylinder(&mut self, center: Vec3, radius: f32, height: f32, color: u32) {
let half_height = height / 2.0;
let res = 64;
let positions = circle_iter(radius, res).map(|p| center + Vec3::new(p.x, p.y, half_height));
self.line_loop(positions, color);
let positions = circle_iter(radius, res).map(|p| center + Vec3::new(p.x, p.y, -half_height));
self.line_loop(positions, color);
for (a, b) in circle_iter(radius, 4).map(|p| ((center + Vec3::new(p.x, p.y, -half_height)), (center + Vec3::new(p.x, p.y, half_height)))) {
self.line(a, b, color);
}
}
}
// TODO: Dynamically reallocate
const VERTEX_BUFFER_SIZE: usize = 1024 * 1024;
#[repr(C)]
struct PushConstants {
view_projection: [[f32; 4]; 4],
screen_size: [f32; 2],
vb_index: u32,
depth_texture_id: u32,
}
pub struct GizmoRenderer {
resolution: [u32; 2],
vb: gpu::Buffer,
pipeline: gpu::GraphicsPipeline,
pub texture: gpu::Texture,
}
impl GizmoRenderer {
pub fn new(resolution: [u32; 2], device: &mut gpu::Device, shader_compiler: &gpu::ShaderCompiler) -> Self {
let vb = device.create_buffer(&gpu::BufferDesc {
size: size_of::<egui::epaint::Vertex>() * VERTEX_BUFFER_SIZE,
usage: gpu::BufferUsage::SHADER_RESOURCE,
memory: gpu::Memory::CpuToGpu,
}).unwrap();
let vertex_shader = shader_compiler.compile("shaders/editor/gizmo.slang", "main_vs");
let pixel_shader = shader_compiler.compile("shaders/editor/gizmo.slang", "main_ps");
let pipeline_desc = gpu::GraphicsPipelineDesc {
vs: Some(&vertex_shader),
ps: Some(&pixel_shader),
descriptor_layout: gpu::DescriptorLayout {
push_constants: Some(gpu::PushConstantBinding {
size: size_of::<PushConstants>() as u32,
}),
bindings: Some(vec![
gpu::DescriptorBinding::bindless_srv(1), // buffers
gpu::DescriptorBinding::bindless_srv(2), // textures
]),
static_samplers: Some(vec![
gpu::SamplerBinding {
shader_register: 0,
register_space: 0,
sampler_desc: gpu::SamplerDesc {
filter_min: gpu::FilterMode::Linear,
filter_mag: gpu::FilterMode::Linear,
filter_mip: gpu::FilterMode::Linear,
..Default::default()
},
},
]),
},
rasterizer: gpu::RasterizerDesc::default(),
depth_stencil: gpu::DepthStencilDesc::default(),
color_attachments: &[gpu::ColorAttachment {
format: gpu::Format::RGBA8UNorm, // TODO: Hardcoded
blend: Some(gpu::BlendDesc {
src_color: gpu::BlendFactor::One,
dst_color: gpu::BlendFactor::InvSrcAlpha,
color_op: gpu::BlendOp::Add,
src_alpha: gpu::BlendFactor::One,
dst_alpha: gpu::BlendFactor::InvSrcAlpha,
alpha_op: gpu::BlendOp::Add,
}),
write_mask: gpu::ColorWriteMask::ALL,
}],
topology: gpu::Topology::TriangleStrip,
};
let pipeline = device.create_graphics_pipeline(&pipeline_desc).unwrap();
let texture = device.create_texture(&gpu::TextureDesc {
width: resolution[0] as _,
height: resolution[1] as _,
depth: 1,
array_size: 1,
mip_levels: 1,
format: gpu::Format::RGBA8UNorm,
usage: gpu::TextureUsage::SHADER_RESOURCE | gpu::TextureUsage::RENDER_TARGET,
layout: gpu::TextureLayout::ShaderResource,
}).unwrap();
Self {
resolution,
vb,
pipeline,
texture,
}
}
pub fn render(&mut self, cmd: &mut gpu::CmdList, gizmo: &Gizmo, view_projection: &[[f32; 4]; 4], depth_texture: &gpu::Texture) {
let map_vb = self.vb.cpu_ptr() as *mut Vertex;
unsafe {
std::ptr::copy_nonoverlapping(gizmo.vertices.as_ptr(), map_vb, gizmo.vertices.len());
}
let push_constants = PushConstants {
view_projection: *view_projection,
screen_size: [self.resolution[0] as f32, self.resolution[1] as f32],
vb_index: self.vb.srv_index().unwrap(),
depth_texture_id: depth_texture.srv_index().unwrap(),
};
cmd.set_graphics_pipeline(&self.pipeline);
cmd.graphics_push_constants(0, gpu::as_u8_slice(&push_constants));
cmd.barriers(&gpu::Barriers::texture(&[gpu::TextureBarrier {
texture: &self.texture,
old_layout: gpu::TextureLayout::ShaderResource,
new_layout: gpu::TextureLayout::RenderTarget,
}]));
cmd.render_pass_begin(&gpu::RenderPassDesc {
colors: &[gpu::RenderTarget {
texture: &self.texture,
load_op: gpu::LoadOp::Clear(gpu::Color { r: 0.0, g: 0.0, b: 0.0, a: 0.0 }),
store_op: gpu::StoreOp::Store,
}],
depth_stencil: None,
});
let rect = gpu::Rect::from_size(self.resolution);
cmd.set_viewport(&rect.into(), 0.0..1.0);
cmd.set_scissor(&rect);
cmd.draw(0..4, 0..gizmo.vertices.len() as u32 / 2);
cmd.render_pass_end();
cmd.barriers(&gpu::Barriers::texture(&[gpu::TextureBarrier {
texture: &self.texture,
old_layout: gpu::TextureLayout::RenderTarget,
new_layout: gpu::TextureLayout::ShaderResource,
}]));
}
}