multi texturing works

master
mitchellhansen 5 years ago
parent 5f2092ef21
commit 73456dc58d

@ -1,4 +1,4 @@
use crate::vertex_2d::{Vertex2D}; use crate::vertex_2d::Vertex2D;
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use std::collections::HashMap; use std::collections::HashMap;
use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer}; use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer};
@ -35,8 +35,8 @@ pub trait Drawable {
fn collect(&self) -> Vec<Vertex2D> { fn collect(&self) -> Vec<Vertex2D> {
let color = self.get_color(); let color = self.get_color();
self.get_vertices().iter().zip(self.get_ti_coords().iter()).map(|(a,b)| self.get_vertices().iter().zip(self.get_ti_coords().iter()).map(|(a, b)|
Vertex2D{ Vertex2D {
v_position: [a.0, a.1], v_position: [a.0, a.1],
color: [color.0, color.1, color.2, color.3], color: [color.0, color.1, color.2, color.3],
ti_position: [b.0, b.1], ti_position: [b.0, b.1],
@ -68,7 +68,6 @@ pub struct CanvasImageHandle {
/// Canvas state also contains logic for writing the stored buffers to the command_buffer /// Canvas state also contains logic for writing the stored buffers to the command_buffer
#[derive(Clone)] #[derive(Clone)]
pub struct CanvasState { pub struct CanvasState {
/// Generated during new() /// Generated during new()
dynamic_state: DynamicState, dynamic_state: DynamicState,
/// Generated during new() /// Generated during new()
@ -98,11 +97,9 @@ pub struct CanvasState {
impl CanvasState { impl CanvasState {
/// This method is called once during initialization, then again whenever the window is resized /// This method is called once during initialization, then again whenever the window is resized
pub fn window_size_dependent_setup(&mut self, images: &[Arc<SwapchainImage<Window>>]) pub fn window_size_dependent_setup(&mut self, images: &[Arc<SwapchainImage<Window>>])
-> Vec<Arc<dyn FramebufferAbstract + Send + Sync>> { -> Vec<Arc<dyn FramebufferAbstract + Send + Sync>> {
let dimensions = images[0].dimensions(); let dimensions = images[0].dimensions();
self.dynamic_state.viewports = self.dynamic_state.viewports =
@ -126,8 +123,6 @@ impl CanvasState {
device: Arc<Device>, device: Arc<Device>,
physical: PhysicalDevice, physical: PhysicalDevice,
capabilities: Capabilities) -> CanvasState { capabilities: Capabilities) -> CanvasState {
let format = capabilities.supported_formats[0].0; let format = capabilities.supported_formats[0].0;
let render_pass = Arc::new(vulkano::single_pass_renderpass!( let render_pass = Arc::new(vulkano::single_pass_renderpass!(
@ -188,7 +183,6 @@ impl CanvasState {
/// Using the dimensions and suggested usage, load a CanvasImage and return it's handle /// Using the dimensions and suggested usage, load a CanvasImage and return it's handle
pub fn create_image(&mut self, dimensions: (u32, u32), usage: ImageUsage) -> Arc<CanvasImageHandle> { pub fn create_image(&mut self, dimensions: (u32, u32), usage: ImageUsage) -> Arc<CanvasImageHandle> {
let handle = Arc::new(CanvasImageHandle { handle: self.image_buffers.len() as u32 }); let handle = Arc::new(CanvasImageHandle { handle: self.image_buffers.len() as u32 });
let image = CanvasImage { let image = CanvasImage {
@ -281,7 +275,6 @@ impl CanvasState {
shader_type: ShaderType, shader_type: ShaderType,
physical: PhysicalDevice, physical: PhysicalDevice,
capabilities: Capabilities) -> Option<Arc<CanvasShaderHandle>> { capabilities: Capabilities) -> Option<Arc<CanvasShaderHandle>> {
let handle = Arc::new(CanvasShaderHandle { let handle = Arc::new(CanvasShaderHandle {
handle: self.shader_buffers.len() as u32 handle: self.shader_buffers.len() as u32
}); });
@ -378,10 +371,16 @@ impl CanvasState {
{ {
let g = hprof::enter("Textured Vertex Buffer"); let g = hprof::enter("Textured Vertex Buffer");
for (k, v) in self.textured_drawables.drain() { for (k, v) in self.textured_drawables.drain() {
let vertex_buffer = v.clone().iter()
.fold(Vec::new(), |mut a: Vec<Vertex2D>, b| {
a.extend(b);
a
});
self.textured_vertex_buffer.insert( self.textured_vertex_buffer.insert(
k.clone(), k.clone(),
ImmutableBuffer::from_iter( ImmutableBuffer::from_iter(
v.first().unwrap().iter().cloned(), // I don't think doing a first is the right thing vertex_buffer.iter().cloned(),
BufferUsage::vertex_buffer(), BufferUsage::vertex_buffer(),
self.queue.clone(), self.queue.clone(),
).unwrap().0, ).unwrap().0,
@ -393,11 +392,16 @@ impl CanvasState {
{ {
let g = hprof::enter("Image Vertex Buffer"); let g = hprof::enter("Image Vertex Buffer");
for (k, v) in self.image_drawables.drain() { for (k, v) in self.image_drawables.drain() {
let vertex_buffer = v.clone().iter()
.fold(Vec::new(), |mut a: Vec<Vertex2D>, b| {
a.extend(b);
a
});
self.image_vertex_buffer.insert( self.image_vertex_buffer.insert(
k.clone(), k.clone(),
ImmutableBuffer::from_iter( ImmutableBuffer::from_iter(
v.iter().fold(Vec::new(), |mut acc, x| [acc.as_slice(), x.as_slice()].concat()).iter(), vertex_buffer.iter().cloned(),
//v.first().unwrap().iter().cloned(),
BufferUsage::vertex_buffer(), BufferUsage::vertex_buffer(),
self.queue.clone(), self.queue.clone(),
).unwrap().0, ).unwrap().0,
@ -460,6 +464,7 @@ impl CanvasState {
command_buffer = command_buffer.draw( command_buffer = command_buffer.draw(
shader.get_pipeline().clone(), shader.get_pipeline().clone(),
// Multiple vertex buffers must have their definition in the pipeline!
&self.dynamic_state.clone(), vec![vertex_buffer], &self.dynamic_state.clone(), vec![vertex_buffer],
vec![descriptor_set], (), vec![descriptor_set], (),
).unwrap(); ).unwrap();
@ -480,6 +485,7 @@ impl CanvasState {
command_buffer = command_buffer.draw( command_buffer = command_buffer.draw(
shader.get_pipeline().clone(), shader.get_pipeline().clone(),
// Multiple vertex buffers must have their definition in the pipeline!
&self.dynamic_state.clone(), vec![vertex_buffer], &self.dynamic_state.clone(), vec![vertex_buffer],
vec![descriptor_set], (), vec![descriptor_set], (),
).unwrap(); ).unwrap();

@ -9,6 +9,7 @@ use vulkano::framebuffer::{Subpass, RenderPassAbstract, Framebuffer, Framebuffer
use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, SpecializationConstants, SpecializationMapEntry}; use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, SpecializationConstants, SpecializationMapEntry};
use vulkano::swapchain::Capabilities; use vulkano::swapchain::Capabilities;
use crate::vertex_2d::Vertex2D; use crate::vertex_2d::Vertex2D;
use vulkano::pipeline::vertex::SingleBufferDefinition;
/// Typed wrapper for a u32 shader handle (index id) /// Typed wrapper for a u32 shader handle (index id)
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
@ -112,7 +113,7 @@ impl CanvasShader {
CanvasShader { CanvasShader {
graphics_pipeline: Some(Arc::new(GraphicsPipeline::start() graphics_pipeline: Some(Arc::new(GraphicsPipeline::start()
.vertex_input_single_buffer::<Vertex2D>() .vertex_input(SingleBufferDefinition::<Vertex2D>::new())
.vertex_shader(vertex_entry_point.clone().unwrap(), ShaderSpecializationConstants { .vertex_shader(vertex_entry_point.clone().unwrap(), ShaderSpecializationConstants {
first_constant: 0, first_constant: 0,

@ -112,7 +112,7 @@ pub fn main() {
.expect("Can't find that kernel"); .expect("Can't find that kernel");
let funky_handle = processor.get_texture_handle(String::from("funky-bird.jpg")).unwrap(); let funky_handle = processor.get_texture_handle(String::from("funky-bird.jpg")).unwrap();
let sfml_handle = processor.get_texture_handle(String::from("funky-bird.jpg")).unwrap(); let sfml_handle = processor.get_texture_handle(String::from("sfml.png")).unwrap();
let sprite3 = Sprite::new_with_texture((0.0, -0.5), (0.5, 0.5), funky_handle.clone()); let sprite3 = Sprite::new_with_texture((0.0, -0.5), (0.5, 0.5), funky_handle.clone());
let sprite4 = Sprite::new_with_texture((0.3, 0.5), (0.9, 0.9), sfml_handle.clone()); let sprite4 = Sprite::new_with_texture((0.3, 0.5), (0.9, 0.9), sfml_handle.clone());

@ -1,4 +1,5 @@
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, Copy)]
pub struct Vertex2D { pub struct Vertex2D {
pub v_position: [f32; 2], pub v_position: [f32; 2],
pub color : [f32; 4], pub color : [f32; 4],

Loading…
Cancel
Save