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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
//! The SSBO module for CGL
#![allow(non_camel_case_types)]
use libc::{c_void, c_int, size_t};
/// The internal handle used by CGL
#[repr(C)]
pub(crate) struct CGL_ssbo {
_private: c_void
}
/// The Shader Storage Buffer Object
pub struct ShaderStorageBufferObject {
pub(crate) handle: *mut CGL_ssbo,
pub(crate) has_been_destroyed: bool
}
// This is just an alias for ShaderStorageBufferObject
pub type SSBO = ShaderStorageBufferObject;
extern {
fn CGL_ssbo_create(binding: u32) -> *mut CGL_ssbo;
fn CGL_ssbo_destroy(ssbo: *mut CGL_ssbo);
fn CGL_ssbo_bind(ssbo: *mut CGL_ssbo);
fn CGL_ssbo_bind2(ssbo: *mut CGL_ssbo, binding: u32);
fn CGL_ssbo_set_data(ssbo: *mut CGL_ssbo, size: size_t, data: *mut c_void, static_draw: c_int);
fn CGL_ssbo_set_sub_data(ssbo: *mut CGL_ssbo, offset: size_t, size: size_t, data: *mut c_void, static_draw: c_int);
fn CGL_ssbo_get_data(ssbo: *mut CGL_ssbo, size: *mut size_t, data: *mut c_void);
fn CGL_ssbo_get_sub_data(ssbo: *mut CGL_ssbo, offset: size_t, size: size_t, data: *mut c_void);
fn CGL_ssbo_set_user_data(ssbo: *mut CGL_ssbo, user_data: *mut c_void);
fn CGL_ssbo_get_user_data(ssbo: *mut CGL_ssbo) -> *mut c_void;
fn CGL_ssbo_get_size(ssbo: *mut CGL_ssbo) -> size_t;
fn CGL_ssbo_copy(dst: *mut CGL_ssbo, src: *mut CGL_ssbo, src_offset: size_t, dst_offset: size_t, size: size_t);
}
impl ShaderStorageBufferObject {
/// Creates a new Shader Storage Buffer Object with the specified binding.
///
/// # Arguments
///
/// * `binding` - The binding point for the SSBO.
///
/// # Returns
///
/// A new instance of `ShaderStorageBufferObject` with the specified binding, or `Err(())` if the creation failed.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut ssbo = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn new(binding: u32) -> Result<ShaderStorageBufferObject, ()> {
let handle = unsafe { CGL_ssbo_create(binding) };
if handle.is_null() {
Err(())
} else {
Ok(ShaderStorageBufferObject {
handle,
has_been_destroyed: false
})
}
}
/// Creates a new Shader Storage Buffer Object with the specified binding and sets its data to the provided byte slice.
///
/// # Arguments
///
/// * `data` - The byte slice containing the data to be set.
/// * `binding` - The binding point for the SSBO.
///
/// # Returns
///
/// A new instance of `ShaderStorageBufferObject` with the specified binding and data, or `Err(())` if the creation failed.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let data = vec![0u8; 1024];
/// let ssbo = cgl_rs::graphics::ShaderStorageBufferObject::from(&data, 0).unwrap();
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn from(data: &[u8], binding: u32) -> Result<ShaderStorageBufferObject, ()> {
let mut ssbo = ShaderStorageBufferObject::new(binding)?;
ssbo.set_data(data.len(), data.as_ptr() as *mut u8, false);
Ok(ssbo)
}
/// Destroys the ssbo and frees its resources. If it has already been destroyed, this method does nothing.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut texture = cgl_rs::graphics::Texture::dummy().unwrap();
/// texture.destroy(); // optional
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn destroy(&mut self) {
if !self.has_been_destroyed {
unsafe {
CGL_ssbo_destroy(self.handle);
}
self.has_been_destroyed = true;
}
}
/// Binds the Shader Storage Buffer Object to the current context.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let ssbo = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// ssbo.bind();
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn bind(&self) {
unsafe {
CGL_ssbo_bind(self.handle);
}
}
/// Binds the Shader Storage Buffer (Base) to the specified binding point.
///
/// # Arguments
///
/// * `binding` - The binding point to bind the Shader Storage Buffer Object to.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut ssbo = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// ssbo.bind2(1);
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn bind2(&mut self, binding: u32) {
unsafe {
CGL_ssbo_bind2(self.handle, binding);
}
}
/// Sets the data of the Shader Storage Buffer Object.
/// This also allocates the memory on the GPU if necessary.
///
/// If you only want to update a part of the data, use `set_sub_data` instead.
///
/// # Arguments
///
/// * `size` - The size of the data in bytes.
/// * `data` - A pointer to the data.
/// * `static_draw` - A flag indicating whether the data is static or not.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut ssbo = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// let data: [u8; 4] = [0, 1, 2, 3];
/// ssbo.set_data(data.len(), data.as_ptr() as *mut u8, true);
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
///
/// # See also
///
/// * `set_sub_data`
pub fn set_data(&mut self, size: usize, data: *mut u8, static_draw: bool) {
unsafe {
CGL_ssbo_set_data(self.handle, size, data as *mut c_void, static_draw as c_int);
}
}
/// Sets a portion of the data of the Shader Storage Buffer Object.
///
/// Note: This does not allocate the memory on the GPU if necessary.
/// So, if the data is not allocated on the GPU, this method will do nothing.
/// To allocate the memory on the GPU, use `set_data` instead.
///
/// # Arguments
///
/// * `offset` - The offset in bytes from the beginning of the buffer where the data should be written.
/// * `size` - The size of the data in bytes.
/// * `data` - A pointer to the data.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut ssbo = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// let data: [u8; 4] = [0, 1, 2, 3];
/// ssbo.set_data(data.len(), data.as_ptr() as *mut u8, true);
/// let sub_data: [u8; 2] = [4, 5];
/// ssbo.set_sub_data(2, sub_data.len(), sub_data.as_ptr() as *mut u8);
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
///
/// # See also
///
/// * `set_data`
pub fn set_sub_data(&mut self, offset: usize, size: usize, data: *mut u8) {
unsafe {
CGL_ssbo_set_sub_data(self.handle, offset, size, data as *mut c_void, false as c_int);
}
}
/// Gets the entire data of the Shader Storage Buffer Object.
///
/// If you only want to get a part of the data, use `get_sub_data` instead.
///
/// # Returns
///
/// A vector containing the data of the Shader Storage Buffer Object.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut ssbo = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// let data: [u8; 4] = [0, 1, 2, 3];
/// ssbo.set_data(data.len(), data.as_ptr() as *mut u8, true);
/// let retrieved_data = ssbo.get_data();
/// assert_eq!(data, retrieved_data.as_slice());
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
///
/// # See also
///
/// * `get_sub_data`
pub fn get_data(&self) -> Vec<u8> {
unsafe {
let mut size: usize = CGL_ssbo_get_size(self.handle);
let mut data: Vec<u8> = Vec::with_capacity(size);
CGL_ssbo_get_data(self.handle, &mut size, data.as_mut_ptr() as *mut c_void);
data.set_len(size);
data
}
}
/// Gets a part of the data of the Shader Storage Buffer Object.
///
/// If you want to get the entire data, use `get_data` instead.
///
/// # Arguments
///
/// * `offset` - The offset in bytes from the beginning of the buffer where the data should be read.
/// * `size` - The size of the data in bytes.
///
/// # Returns
///
/// A vector containing the requested part of the data of the Shader Storage Buffer Object.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut ssbo = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// let data: [u8; 4] = [0, 1, 2, 3];
/// ssbo.set_data(data.len(), data.as_ptr() as *mut u8, true);
/// let retrieved_data = ssbo.get_sub_data(1, 2);
/// assert_eq!(&data[1..3], retrieved_data.as_slice());
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
///
/// # See also
///
/// * `get_data`
pub fn get_sub_data(&self, offset: usize, size: usize) -> Vec<u8> {
unsafe {
let mut data: Vec<u8> = Vec::with_capacity(size);
CGL_ssbo_get_sub_data(self.handle, offset, size, data.as_mut_ptr() as *mut c_void);
data.set_len(size);
data
}
}
/// Gets the size of the Shader Storage Buffer Object.
///
/// # Returns
///
/// The size of the Shader Storage Buffer Object in bytes.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut ssbo = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// let data: [u8; 4] = [0, 1, 2, 3];
/// ssbo.set_data(data.len(), data.as_ptr() as *mut u8, true);
/// assert_eq!(data.len(), ssbo.size());
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn size(&self) -> usize {
unsafe {
CGL_ssbo_get_size(self.handle) as usize
}
}
/// Copies a part of the data of the Shader Storage Buffer Object to another Shader Storage Buffer Object.
///
/// Note: This does not allocate the memory if the destination buffer is not big enough.
/// In such a case nothing will happen.
///
/// # Arguments
///
/// * `other` - The destination Shader Storage Buffer Object.
/// * `src_offset` - The offset in bytes from the beginning of the source buffer where the data should be read.
/// * `dst_offset` - The offset in bytes from the beginning of the destination buffer where the data should be written.
/// * `size` - The size of the data in bytes.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut ssbo1 = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// let mut ssbo2 = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// let data: [u8; 4] = [0, 1, 2, 3];
/// let data2: [u8; 2] = [4, 5];
/// ssbo1.set_data(data.len(), data.as_ptr() as *mut u8, true);
/// ssbo2.set_data(data2.len(), data2.as_ptr() as *mut u8, true);
/// ssbo1.copy_to(&ssbo2, 1, 0, 2);
/// let retrieved_data = ssbo2.get_data();
/// assert_eq!(&data[1..3], retrieved_data.as_slice());
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn copy_to(&self, other: &ShaderStorageBufferObject, src_offset: usize, dst_offset: usize, size: usize) {
unsafe {
CGL_ssbo_copy(other.handle, self.handle, src_offset, dst_offset, size);
}
}
/// Copies the entire data of the Shader Storage Buffer Object to another Shader Storage Buffer Object.
///
/// Note: This does not allocate the memory if the destination buffer is not big enough.
/// In such a case nothing will happen.
///
/// # Arguments
///
/// * `other` - The destination Shader Storage Buffer Object.
///
/// # Example
///
/// ```
/// cgl_rs::init();
/// let mut window = cgl_rs::Window::new("Hello, World!", 800, 600).unwrap();
/// cgl_rs::graphics::init();
/// {
/// let mut ssbo1 = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// let mut ssbo2 = cgl_rs::graphics::ShaderStorageBufferObject::new(0).unwrap();
/// let data: [u8; 4] = [0, 1, 2, 3];
/// let data2: [u8; 4] = [4, 5, 6, 7];
/// ssbo1.set_data(data.len(), data.as_ptr() as *mut u8, true);
/// ssbo2.set_data(data2.len(), data2.as_ptr() as *mut u8, true);
/// ssbo1.copy_whole_to(&ssbo2);
/// let retrieved_data = ssbo2.get_data();
/// assert_eq!(data, retrieved_data.as_slice());
/// }
/// cgl_rs::graphics::shutdown();
/// window.destroy();
/// cgl_rs::shutdown();
/// ```
pub fn copy_whole_to(&self, other: &ShaderStorageBufferObject) {
unsafe {
CGL_ssbo_copy(other.handle, self.handle, 0, 0, self.size());
}
}
}
impl Drop for ShaderStorageBufferObject {
fn drop(&mut self) {
self.destroy();
}
}
impl Clone for ShaderStorageBufferObject {
/// Clones the ssbo.
///
/// NOTE: The new instance will have the same handle, `has_been_destroyed` flag.
/// This means that the new instance will not be able to receive events nor will the internal window handle be
/// destroyed when the new instance is dropped. The internal window handle will be destroyed when the original
/// instance is dropped.
///
///
/// # Returns
///
/// A new instance of `ShaderStorageBufferObject` with the same handle as the original ssbo.
fn clone(&self) -> Self {
ShaderStorageBufferObject {
handle: self.handle.clone(),
has_been_destroyed: true
}
}
}