fyrox_graphics/read_buffer.rs
1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! A [`GpuAsyncReadBufferTrait`] object handles the transfering pixel data from a frame buffer
22//! into a color buffer in the background, and provides a method to poll the object and take the
23//! data once it arrives.
24
25use crate::{
26 core::math::Rect, define_shared_wrapper, error::FrameworkError,
27 framebuffer::GpuFrameBufferTrait,
28};
29use bytemuck::Pod;
30use fyrox_core::define_as_any_trait;
31
32define_as_any_trait!(GpuAsyncReadBufferAsAny => GpuAsyncReadBufferTrait);
33
34/// Trait for objects that represent a request to transfer pixel data from some frame buffer
35/// into a color buffer.
36pub trait GpuAsyncReadBufferTrait: GpuAsyncReadBufferAsAny {
37 /// Begin the pixel data transfer by specifying a particular attachment of a particular framebuffer to read from.
38 /// * `framebuffer`: The source of the data.
39 /// * `color_buffer_index`: The index of the color attachment of the framebuffer.
40 /// * `rect`: The portion of the framebuffer to read.
41 ///
42 /// After this is called, [`is_request_running`](GpuAsyncReadBufferTrait::is_request_running) will
43 /// return true.
44 fn schedule_pixels_transfer(
45 &self,
46 framebuffer: &dyn GpuFrameBufferTrait,
47 color_buffer_index: u32,
48 rect: Option<Rect<i32>>,
49 ) -> Result<(), FrameworkError>;
50 /// Return true if a request has been made and the data has not yet been
51 /// retrieved with [`try_read`](GpuAsyncReadBufferTrait::try_read).
52 /// Returning false does *not* indicate that the data is read; this method
53 /// will continue to return true long after the data is read, until `try_read` is
54 /// called to actually read the data.
55 fn is_request_running(&self) -> bool;
56 /// Check the state of the data and return the data if it is read, or else
57 /// return None. After the data is returned, [`is_request_running`](GpuAsyncReadBufferTrait::is_request_running)
58 /// will return false.
59 fn try_read(&self) -> Option<Vec<u8>>;
60}
61
62impl dyn GpuAsyncReadBufferTrait {
63 /// Check the state of the data and return the data if it is read, or else
64 /// return None. The data is converted into the given type before being returned.
65 /// After the data is returned, [`is_request_running`](GpuAsyncReadBufferTrait::is_request_running)
66 /// will return false.
67 pub fn try_read_of_type<T>(&self) -> Option<Vec<T>>
68 where
69 T: Pod,
70 {
71 let mut bytes = self.try_read()?;
72 let typed = unsafe {
73 Some(Vec::from_raw_parts(
74 bytes.as_mut_ptr() as *mut T,
75 bytes.len() / size_of::<T>(),
76 bytes.capacity() / size_of::<T>(),
77 ))
78 };
79 std::mem::forget(bytes);
80 typed
81 }
82}
83
84define_shared_wrapper!(GpuAsyncReadBuffer<dyn GpuAsyncReadBufferTrait>);