pub struct MaterialBuffer<T> { /* private fields */ }
Expand description
This is a chunk of memory that will get bound to all shaders at a particular register slot. StereoKit uses this to provide engine values to the shader, and you can also use this to drive graphical shader systems of your own!
For example, if your application has a custom lighting system, fog, wind, or some other system that multiple shaders might need to refer to, this is the perfect tool to use.
The type ‘T’ for this buffer must be a struct that uses the #[repr(C)] attribute for proper copying. It should also match the layout of your equivalent cbuffer in the shader file. Note that shaders often have specific byte alignment requirements! https://stereokit.net/Pages/StereoKit/MaterialBuffer.html
§Examples
use stereokit_rust::material::MaterialBuffer;
// This struct must mirror the layout of the cbuffer in your shader.
// #[repr(C)] ensures a predictable memory layout.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
struct Globals {
time: f32,
wind: [f32; 3], // Example of packing a vec3
// Alignment padding if your shader expects 16-byte alignment for next values.
}
// Create the GPU buffer once.
let buffer = MaterialBuffer::<Globals>::new();
// Update data you want the shader(s) to read.
let mut globals = Globals { time: 1.234, wind: [0.1, 0.2, 0.3], ..Default::default() };
// Upload to GPU so every shader using this global slot can access it.
buffer.set(&mut globals as *mut _);
// In your shader, declare a matching cbuffer bound to the slot you
// bind this MaterialBuffer to (see Renderer::set_global_buffer in StereoKit).
Implementations§
Source§impl<T> MaterialBuffer<T>
impl<T> MaterialBuffer<T>
Sourcepub fn new() -> MaterialBuffer<T>
pub fn new() -> MaterialBuffer<T>
Create a new global MaterialBuffer that can be bound to a register slot id via Renderer.SetGlobalBuffer. All
shaders will have access to the data provided via this instance’s Set
.
https://stereokit.net/Pages/StereoKit/MaterialBuffer/MaterialBuffer.html
see also material_buffer_create
Sourcepub fn set(&self, in_data: *mut T)
pub fn set(&self, in_data: *mut T)
This will upload your data to the GPU for shaders to use. https://stereokit.net/Pages/StereoKit/MaterialBuffer/Set.html
see also material_buffer_set_data