SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     pub fn draw_triangle_frame(&mut self, triangle: Triangle) -> Result<(), &'static str> {
  2.        // FRAME SETUP
  3.        let image_available = &self.image_available_semaphores[self.current_frame];
  4.        let render_finished = &self.render_finished_semaphores[self.current_frame];
  5.  
  6.        // advance the frame before early returns can happen
  7.        self.current_frame = (self.current_frame + 1) % self.frames_in_flight;
  8.  
  9.        let (i_u32, i_usize) = unsafe {
  10.            let image_index = self
  11.                .swapchain
  12.                .acquire_image(core::u64::MAX, FrameSync::Semaphore(image_available))
  13.                .map_err(|_| "Couldn't acquire an image from the swapchain!")?;
  14.            (image_index, image_index as usize)
  15.        };
  16.  
  17.        let flight_fence = &self.in_flight_fences[i_usize];
  18.        unsafe {
  19.            self.device
  20.                .wait_for_fence(flight_fence, core::u64::MAX)
  21.                .map_err(|_| "Failed to wait on the fence!")?;
  22.            self.device
  23.                .reset_fence(flight_fence)
  24.                .map_err(|_| "Couldn't reset fence!")?;
  25.        }
  26.  
  27.        unsafe {
  28.            let mut data_target = self
  29.                .device
  30.                .acquire_mapping_writer(&*self.memory, 0..self.requirements.size)
  31.                .map_err(|_| "Failed to acquire a memory writer!")?;
  32.            let points = triangle.points_flat();
  33.            data_target[..points.len()].copy_from_slice(&points);
  34.            self.device
  35.                .release_mapping_writer(data_target)
  36.                .map_err(|_| "Couldn't release the mapping writer")?;
  37.        }
  38.  
  39.        // record commands
  40.        unsafe {
  41.            let buffer = &mut self.command_buffers[i_usize];
  42.            const TRIANGLE_CLEAR: [ClearValue; 1] =
  43.                [ClearValue::Color(ClearColor::Float([0.1, 0.2, 0.3, 1.0]))];
  44.            buffer.begin(false);
  45.            {
  46.                let mut encoder = buffer.begin_render_pass_inline(
  47.                    &self.render_pass,
  48.                    &self.framebuffers[i_usize],
  49.                    self.render_area,
  50.                    TRIANGLE_CLEAR.iter(),
  51.                );
  52.                encoder.bind_graphics_pipeline(&self.graphics_pipeline);
  53.                // force deref impl of ManuallyDrop to do stuff
  54.                let buffer_ref: &<back::Backend as Backend>::Buffer = &self.buffer;
  55.                let buffers: ArrayVec<[_; 1]> = [(buffer_ref, 0)].into();
  56.                encoder.bind_vertex_buffers(0, buffers);
  57.                encoder.draw(0..3, 0..1);
  58.            }
  59.            buffer.finish()
  60.        }
  61.  
  62.        // Submission
  63.        let command_buffers = &self.command_buffers[i_usize..=i_usize];
  64.        let wait_semaphores: ArrayVec<[_; 1]> =
  65.            [(image_available, PipelineStage::COLOR_ATTACHMENT_OUTPUT)].into();
  66.        let signal_semaphores: ArrayVec<[_; 1]> = [render_finished].into();
  67.        // apparently, you gotta do this twice, because reasons
  68.        let present_wait_semaphores: ArrayVec<[_; 1]> = [render_finished].into();
  69.        let submission = Submission {
  70.            command_buffers,
  71.            wait_semaphores,
  72.            signal_semaphores,
  73.        };
  74.        let the_command_queue = &mut self.queue_group.queues[0];
  75.        unsafe {
  76.            the_command_queue.submit(submission, Some(flight_fence));
  77.            self.swapchain
  78.                .present(the_command_queue, i_u32, present_wait_semaphores)
  79.                .map_err(|_| "Failed to present into the swapchain!")
  80.        }
  81.    }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top