took out all the sfml shader stuff. will port the rest to vulkan if buffer swapping becomes a perf issue

master
mitchellhansen 5 years ago
parent ceb138c391
commit e7d4d6a8e2

@ -47,90 +47,14 @@ mod timer;
mod input; mod input;
mod util; mod util;
// The container trait for all the shaders
trait Effect: Drawable {
fn update(&mut self, t: f32, x: f32, y: f32);
fn name(&self) -> &str;
fn as_drawable(&self) -> &Drawable;
}
// ======= LARGE MULTISPRITE SHADER DEMO ===========
struct Edge<'t> {
surface: RenderTexture,
bg_sprite: Sprite<'t>,
entities: Vec<Sprite<'t>>,
shader: Shader<'static>,
}
impl<'t> Edge<'t> {
fn new(bg_texture: &'t Texture, entity_texture: &'t Texture) -> Self {
let mut surface = RenderTexture::new(800, 600, false).unwrap();
surface.set_smooth(true);
let mut bg_sprite = Sprite::with_texture(bg_texture);
bg_sprite.set_position((0., 0.));
let mut entities = Vec::new();
for i in 0..6 {
let mut entity = Sprite::with_texture(entity_texture);
entity.set_texture_rect(&IntRect::new(96 * i, 0, 96, 96));
entities.push(entity);
}
let mut shader = Shader::from_file(None, None, Some("resources/shaders/edge.frag")).unwrap();
shader.set_uniform_current_texture("texture");
Self {
surface,
bg_sprite,
entities,
shader,
}
}
}
impl<'t> Drawable for Edge<'t> {
fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
&'a self,
target: &mut RenderTarget,
mut states: RenderStates<'texture, 'shader, 'shader_texture>,
) {
states.shader = Some(&self.shader);
target.draw_with_renderstates(&Sprite::with_texture(self.surface.texture()), states);
}
}
impl<'t> Effect for Edge<'t> {
fn update(&mut self, t: f32, x: f32, y: f32) {
self.shader
.set_uniform_float("edge_threshold", 1. - (x + y) / 2.);
let entities_len = self.entities.len() as f32;
for (i, en) in self.entities.iter_mut().enumerate() {
let pos = (
(0.25 * (t * i as f32 + (entities_len - i as f32))).cos() * 300. + 350.,
(0.25 * (t * (entities_len - i as f32) + i as f32)).cos() * 200. + 250.,
);
en.set_position(pos);
}
self.surface.clear(&Color::WHITE);
self.surface.draw(&self.bg_sprite);
for en in &self.entities {
self.surface.draw(en);
}
self.surface.display();
}
fn as_drawable(&self) -> &Drawable {
self
}
fn name(&self) -> &str {
"edge post-effect"
}
}
// =================================================
fn main() { fn main() {
// Load up the input image, determine some details
let mut img = image::open("resources/images/test2.png").unwrap();
let xy = img.dimensions();
let data_length = xy.0 * xy.1 * 4;
let mut image_buffer = Vec::new();
{
// Create the vulkan instance, device, and device queue // Create the vulkan instance, device, and device queue
let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap(); let instance = Instance::new(None, &InstanceExtensions::none(), None).unwrap();
let physical = PhysicalDevice::enumerate(&instance).next().unwrap(); let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
@ -154,7 +78,6 @@ fn main() {
vulkano::pipeline::shader::ShaderModule::from_words(device.clone(), &shader.compute) vulkano::pipeline::shader::ShaderModule::from_words(device.clone(), &shader.compute)
}.unwrap(); }.unwrap();
// Compile the shader and add it to a pipeline // Compile the shader and add it to a pipeline
let pipeline = Arc::new({ let pipeline = Arc::new({
unsafe { unsafe {
@ -165,15 +88,9 @@ fn main() {
} }
}); });
// Load up the input image, determine some details
let mut img = image::open("resources/images/test.png").unwrap();
let xy = img.dimensions();
let data_length = xy.0*xy.1*4;
let pixel_count = img.raw_pixels().len(); let pixel_count = img.raw_pixels().len();
println!("Pixel count {}", pixel_count); println!("Pixel count {}", pixel_count);
let mut image_buffer = Vec::new();
if pixel_count != data_length as usize { if pixel_count != data_length as usize {
for i in img.raw_pixels().iter() { for i in img.raw_pixels().iter() {
if (image_buffer.len() + 1) % 4 == 0 { if (image_buffer.len() + 1) % 4 == 0 {
@ -182,8 +99,7 @@ fn main() {
image_buffer.push(*i); image_buffer.push(*i);
} }
image_buffer.push(255); image_buffer.push(255);
} } else {
else {
image_buffer = img.raw_pixels(); image_buffer = img.raw_pixels();
} }
@ -192,27 +108,11 @@ fn main() {
println!("Allocating Buffers..."); println!("Allocating Buffers...");
{ {
//CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::all(), image_buffer.clone()).unwrap()
// Pull out the image data and place it in a buffer for the kernel to write to and for us to read from // Pull out the image data and place it in a buffer for the kernel to write to and for us to read from
let write_buffer = { let write_buffer = {
let mut buff = image_buffer.iter(); let mut buff = image_buffer.iter();
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap())); let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap(); CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap()
unsafe {
let uninitialized =
CpuAccessibleBuffer::raw(device, data_length as usize, BufferUsage::all(), iter::empty()).unwrap();
{
let mut mapping = uninitialized.write().unwrap();
ptr::write(&mut *mapping, image_buffer.as_slice())
}
uninitialized
}
}; };
// Pull out the image data and place it in a buffer for the kernel to read from // Pull out the image data and place it in a buffer for the kernel to read from
@ -220,11 +120,6 @@ fn main() {
let mut buff = image_buffer.iter(); let mut buff = image_buffer.iter();
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap())); let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap() CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap()
// let q = ImmutableBuffer::from_data(image_buffer.clone(), BufferUsage::all(), queue.clone()).unwrap();
// q.1.flush();
// q.0
//CpuAccessibleBuffer::from_data(device.clone(), BufferUsage::all(), image_buffer.clone()).unwrap()
}; };
// A buffer to hold many i32 values to use as settings // A buffer to hold many i32 values to use as settings
@ -259,13 +154,14 @@ fn main() {
future.wait(None).unwrap(); future.wait(None).unwrap();
println!("Done running kernel"); println!("Done running kernel");
println!("Reading output");
// The buffer is sync'd so we can just read straight from the handle // The buffer is sync'd so we can just read straight from the handle
let data_buffer_content = write_buffer.read().unwrap(); let mut data_buffer_content = write_buffer.read().unwrap();
println!("Reading output");
for y in 0..xy.1 { for y in 0..xy.1 {
for x in 0..xy.0 { for x in 0..xy.0 {
let r = data_buffer_content[((xy.0 * y + x) * 4 + 0) as usize] as u8; let r = data_buffer_content[((xy.0 * y + x) * 4 + 0) as usize] as u8;
let g = data_buffer_content[((xy.0 * y + x) * 4 + 1) as usize] as u8; let g = data_buffer_content[((xy.0 * y + x) * 4 + 1) as usize] as u8;
let b = data_buffer_content[((xy.0 * y + x) * 4 + 2) as usize] as u8; let b = data_buffer_content[((xy.0 * y + x) * 4 + 2) as usize] as u8;
@ -279,11 +175,12 @@ fn main() {
img.put_pixel(x, y, image::Rgba([r, g, b, a])) img.put_pixel(x, y, image::Rgba([r, g, b, a]))
} }
} }
} }// Currently bringing all this start shit outta scope to see if it stops my gpu from screaming
println!("Saving output"); println!("Saving output");
img.save(format!("output/{}.png", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs())); img.save(format!("output/{}.png", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()));
}
let mut window = RenderWindow::new( let mut window = RenderWindow::new(
(900, 900), (900, 900),
@ -292,32 +189,16 @@ fn main() {
&Default::default(), &Default::default(),
); );
let mut timer = Timer::new(); let mut timer = Timer::new();
let mut input = Input::new(); let mut input = Input::new();
//==========================================
let font = Font::from_file("resources/fonts/sansation.ttf").unwrap(); let font = Font::from_file("resources/fonts/sansation.ttf").unwrap();
let mut bg_texture = Texture::new(xy.0, xy.1).unwrap(); let mut bg_texture = Texture::new(xy.0, xy.1).unwrap();
bg_texture.update_from_pixels(image_buffer.as_slice(), xy.0, xy.1, 0, 0); bg_texture.update_from_pixels(image_buffer.as_slice(), xy.0, xy.1, 0, 0);
//let mut bg_texture = Texture::from_file("resources/images/sfml.png").unwrap();
//bg_texture.set_smooth(true);
let mut entity_texture = Texture::from_file("resources/images/devices.png").unwrap(); let mut background_sprite = Sprite::with_texture(&bg_texture);
entity_texture.set_smooth(true); background_sprite.set_position((0., 0.));
let mut effects: [Box<Effect>; 1] = [
Box::new(Edge::new(&bg_texture, &entity_texture)),
];
let mut current = 0;
let text_bg_texture = Texture::from_file("resources/images/text-background.png").unwrap();
let mut text_bg = Sprite::with_texture(&text_bg_texture);
text_bg.set_position((0., 520.));
text_bg.set_color(&Color::rgba(255, 255, 255, 200));
//==========================================
let mut slider = Slider::new(40.0, None); let mut slider = Slider::new(40.0, None);
@ -363,13 +244,9 @@ fn main() {
accumulator_time -= step_size; accumulator_time -= step_size;
} }
let x = 0.;//window.mouse_position().x as f32 / window.size().x as f32;
let y = 0.;//window.mouse_position().y as f32 / window.size().y as f32;
effects[current].update(elapsed_time*1.0, x, y);
window.clear(&Color::BLACK); window.clear(&Color::BLACK);
window.draw(effects[current].as_drawable()); window.draw(&background_sprite);
window.draw(&slider); window.draw(&slider);

Loading…
Cancel
Save