Struct GraphicsShaderBuilder

Source
pub struct GraphicsShaderBuilder { /* private fields */ }
Expand description

Builder for creating graphics shaders.

This builder allows you to set the WGSL vertex and fragment shader source code from files or strings. You can also set the vertex and fragment shader source code separately.

Implementations§

Source§

impl GraphicsShaderBuilder

Source

pub fn set_file(self, path: &str) -> Self

Sets the WGSL vertex and fragment shader source code from a file.

Source

pub fn set_source(self, source: &str) -> Self

Sets the WGSL vertex and fragment shader source code from a string.

Source

pub fn set_vertex_file(self, path: &str) -> Self

Sets the WGSL vertex shader source code from a file.

You need to also set the fragment shader source code using set_fragment_file or set_fragment_code.

Source

pub fn set_fragment_file(self, path: &str) -> Self

Sets the WGSL fragment shader source code from a file.

You need to also set the vertex shader source code using set_vertex_file or set_vertex_code.

Source

pub fn set_vertex_code(self, source: &str) -> Self

Sets the WGSL vertex shader source code from a string.

You need to also set the fragment shader source code using set_fragment_code or set_fragment_file.

Examples found in repository?
examples/pipeline.rs (line 92)
61fn main() {
62    let mut runner = est_render::runner::new().expect("Failed to create runner");
63    let mut window = runner
64        .create_window("Engine Example", Point2::new(800, 600))
65        .build()
66        .expect("Failed to create window");
67
68    let mut gpu = est_render::gpu::new(Some(&mut window))
69        .build()
70        .expect("Failed to create GPU");
71
72    let mut msaa_texture = gpu
73        .create_texture()
74        .set_render_target(Point2::new(800, 600), None)
75        .set_sample_count(SampleCount::SampleCount4)
76        .build()
77        .expect("Failed to create MSAA texture");
78
79    let blank_texture = gpu
80        .create_texture()
81        .set_raw_image(
82            &[255u8; 4],
83            Point2::new(1, 1),
84            TextureFormat::Bgra8Unorm,
85        )
86        .set_usage(TextureUsage::Sampler)
87        .build()
88        .expect("Failed to create blank texture");
89
90    let shader = gpu
91        .create_graphics_shader()
92        .set_vertex_code(VERTEX_DRAWING_SHADER)
93        .set_fragment_code(FRAGMENT_DRAWING_SHADER)
94        .build()
95        .expect("Failed to create graphics shader");
96
97    let compute_shader = gpu
98        .create_compute_shader()
99        .set_source(COMPUTE_NOOP_SHADER)
100        .build()
101        .expect("Failed to create compute shader");
102
103    let pipeline = gpu
104        .create_render_pipeline()
105        .set_shader(Some(&shader))
106        .set_blend(Some(&BlendState::ALPHA_BLEND))
107        .set_attachment_texture(0, 0, Some(&blank_texture))
108        .set_attachment_sampler(0, 1, Some(&TextureSampler::DEFAULT))
109        .build()
110        .expect("Failed to create render pipeline");
111
112    let compute_pipeline = gpu
113        .create_compute_pipeline()
114        .set_shader(Some(&compute_shader))
115        .build()
116        .expect("Failed to create compute pipeline");
117
118    // Triangle vertices
119    let vertices = vec![
120        Vertex {
121            position: Vector3::new(-0.5, -0.5, 0.0),
122            color: Color::new(1.0, 0.0, 0.0, 1.0),
123            texcoord: Vector2::new(0.0, 1.0),
124        },
125        Vertex {
126            position: Vector3::new(0.5, -0.5, 0.0),
127            color: Color::new(0.0, 1.0, 0.0, 1.0),
128            texcoord: Vector2::new(1.0, 1.0),
129        },
130        Vertex {
131            position: Vector3::new(0.0, 0.5, 0.0),
132            color: Color::new(0.0, 0.0, 1.0, 1.0),
133            texcoord: Vector2::new(0.5, 0.0),
134        },
135    ];
136
137    let indexes = vec![0u16, 1u16, 2u16];
138
139    let vbo = gpu
140        .create_buffer()
141        .set_data_vec(vertices)
142        .set_usage(BufferUsage::VERTEX)
143        .build()
144        .expect("Failed to create vertex buffer");
145
146    let ibo = gpu
147        .create_buffer()
148        .set_data_vec(indexes)
149        .set_usage(BufferUsage::INDEX)
150        .build()
151        .expect("Failed to create index buffer");
152
153    while runner.pool_events(PollMode::WaitDraw) {
154        for event in runner.get_events() {
155            match event {
156                Event::KeyboardInput {
157                    window_id,
158                    key,
159                    pressed,
160                } => {
161                    if *window_id == window.id() && key == "Escape" && *pressed {
162                        window.quit();
163                    }
164                }
165                Event::WindowResized { window_id: _, size } => {
166                    if size.x <= 0 || size.y <= 0 {
167                        continue; // Skip invalid sizes
168                    }
169
170                    msaa_texture = gpu
171                        .create_texture()
172                        .set_render_target(Point2::new(size.x as u32, size.y as u32), None)
173                        .set_sample_count(SampleCount::SampleCount4)
174                        .build()
175                        .expect("Failed to resize MSAA texture");
176                }
177                Event::RedrawRequested { window_id: _ } => {
178                    if let Ok(mut cmd) = gpu.begin_command() {
179                        if let Ok(mut cm) = cmd.begin_computepass() {
180                            cm.set_pipeline(Some(&compute_pipeline));
181                            cm.dispatch(1, 1, 1);
182                        }
183
184                        if let Ok(mut rp) = cmd.begin_renderpass() {
185                            rp.set_clear_color(Color::BLACK);
186                            rp.push_msaa_texture(&msaa_texture);
187
188                            rp.set_pipeline(Some(&pipeline));
189                            rp.set_gpu_buffer(Some(&vbo), Some(&ibo));
190                            rp.draw_indexed(0..3, 0, 1);
191                        }
192                    }
193
194                    window.request_redraw();
195                }
196                _ => {}
197            }
198        }
199    }
200}
Source

pub fn set_fragment_code(self, source: &str) -> Self

Sets the WGSL fragment shader source code from a string.

You need to also set the vertex shader source code using set_vertex_code or set_vertex_file.

Examples found in repository?
examples/pipeline.rs (line 93)
61fn main() {
62    let mut runner = est_render::runner::new().expect("Failed to create runner");
63    let mut window = runner
64        .create_window("Engine Example", Point2::new(800, 600))
65        .build()
66        .expect("Failed to create window");
67
68    let mut gpu = est_render::gpu::new(Some(&mut window))
69        .build()
70        .expect("Failed to create GPU");
71
72    let mut msaa_texture = gpu
73        .create_texture()
74        .set_render_target(Point2::new(800, 600), None)
75        .set_sample_count(SampleCount::SampleCount4)
76        .build()
77        .expect("Failed to create MSAA texture");
78
79    let blank_texture = gpu
80        .create_texture()
81        .set_raw_image(
82            &[255u8; 4],
83            Point2::new(1, 1),
84            TextureFormat::Bgra8Unorm,
85        )
86        .set_usage(TextureUsage::Sampler)
87        .build()
88        .expect("Failed to create blank texture");
89
90    let shader = gpu
91        .create_graphics_shader()
92        .set_vertex_code(VERTEX_DRAWING_SHADER)
93        .set_fragment_code(FRAGMENT_DRAWING_SHADER)
94        .build()
95        .expect("Failed to create graphics shader");
96
97    let compute_shader = gpu
98        .create_compute_shader()
99        .set_source(COMPUTE_NOOP_SHADER)
100        .build()
101        .expect("Failed to create compute shader");
102
103    let pipeline = gpu
104        .create_render_pipeline()
105        .set_shader(Some(&shader))
106        .set_blend(Some(&BlendState::ALPHA_BLEND))
107        .set_attachment_texture(0, 0, Some(&blank_texture))
108        .set_attachment_sampler(0, 1, Some(&TextureSampler::DEFAULT))
109        .build()
110        .expect("Failed to create render pipeline");
111
112    let compute_pipeline = gpu
113        .create_compute_pipeline()
114        .set_shader(Some(&compute_shader))
115        .build()
116        .expect("Failed to create compute pipeline");
117
118    // Triangle vertices
119    let vertices = vec![
120        Vertex {
121            position: Vector3::new(-0.5, -0.5, 0.0),
122            color: Color::new(1.0, 0.0, 0.0, 1.0),
123            texcoord: Vector2::new(0.0, 1.0),
124        },
125        Vertex {
126            position: Vector3::new(0.5, -0.5, 0.0),
127            color: Color::new(0.0, 1.0, 0.0, 1.0),
128            texcoord: Vector2::new(1.0, 1.0),
129        },
130        Vertex {
131            position: Vector3::new(0.0, 0.5, 0.0),
132            color: Color::new(0.0, 0.0, 1.0, 1.0),
133            texcoord: Vector2::new(0.5, 0.0),
134        },
135    ];
136
137    let indexes = vec![0u16, 1u16, 2u16];
138
139    let vbo = gpu
140        .create_buffer()
141        .set_data_vec(vertices)
142        .set_usage(BufferUsage::VERTEX)
143        .build()
144        .expect("Failed to create vertex buffer");
145
146    let ibo = gpu
147        .create_buffer()
148        .set_data_vec(indexes)
149        .set_usage(BufferUsage::INDEX)
150        .build()
151        .expect("Failed to create index buffer");
152
153    while runner.pool_events(PollMode::WaitDraw) {
154        for event in runner.get_events() {
155            match event {
156                Event::KeyboardInput {
157                    window_id,
158                    key,
159                    pressed,
160                } => {
161                    if *window_id == window.id() && key == "Escape" && *pressed {
162                        window.quit();
163                    }
164                }
165                Event::WindowResized { window_id: _, size } => {
166                    if size.x <= 0 || size.y <= 0 {
167                        continue; // Skip invalid sizes
168                    }
169
170                    msaa_texture = gpu
171                        .create_texture()
172                        .set_render_target(Point2::new(size.x as u32, size.y as u32), None)
173                        .set_sample_count(SampleCount::SampleCount4)
174                        .build()
175                        .expect("Failed to resize MSAA texture");
176                }
177                Event::RedrawRequested { window_id: _ } => {
178                    if let Ok(mut cmd) = gpu.begin_command() {
179                        if let Ok(mut cm) = cmd.begin_computepass() {
180                            cm.set_pipeline(Some(&compute_pipeline));
181                            cm.dispatch(1, 1, 1);
182                        }
183
184                        if let Ok(mut rp) = cmd.begin_renderpass() {
185                            rp.set_clear_color(Color::BLACK);
186                            rp.push_msaa_texture(&msaa_texture);
187
188                            rp.set_pipeline(Some(&pipeline));
189                            rp.set_gpu_buffer(Some(&vbo), Some(&ibo));
190                            rp.draw_indexed(0..3, 0, 1);
191                        }
192                    }
193
194                    window.request_redraw();
195                }
196                _ => {}
197            }
198        }
199    }
200}
Source

pub fn set_binary_source(self, binary: &[u8]) -> Self

Sets the precompiled binary shader source code.

This is useful for using shaders compiled with tools like glslangValidator or shaderc.

Source

pub fn set_binary_file(self, path: &str) -> Self

Sets the precompiled binary vertex and fragment shader source code.

This is useful for using shaders compiled with tools like glslangValidator or shaderc.

Source

pub fn set_binary_vertex(self, binary: &[u8]) -> Self

Sets the precompiled binary vertex shader source code.

You need to also set the fragment shader source code using set_binary_fragment.

Source

pub fn set_binary_fragment(self, binary: &[u8]) -> Self

Sets the precompiled binary fragment shader source code.

You need to also set the vertex shader source code using set_binary_vertex.

Source

pub fn build(self) -> Result<GraphicsShader, String>

Examples found in repository?
examples/pipeline.rs (line 94)
61fn main() {
62    let mut runner = est_render::runner::new().expect("Failed to create runner");
63    let mut window = runner
64        .create_window("Engine Example", Point2::new(800, 600))
65        .build()
66        .expect("Failed to create window");
67
68    let mut gpu = est_render::gpu::new(Some(&mut window))
69        .build()
70        .expect("Failed to create GPU");
71
72    let mut msaa_texture = gpu
73        .create_texture()
74        .set_render_target(Point2::new(800, 600), None)
75        .set_sample_count(SampleCount::SampleCount4)
76        .build()
77        .expect("Failed to create MSAA texture");
78
79    let blank_texture = gpu
80        .create_texture()
81        .set_raw_image(
82            &[255u8; 4],
83            Point2::new(1, 1),
84            TextureFormat::Bgra8Unorm,
85        )
86        .set_usage(TextureUsage::Sampler)
87        .build()
88        .expect("Failed to create blank texture");
89
90    let shader = gpu
91        .create_graphics_shader()
92        .set_vertex_code(VERTEX_DRAWING_SHADER)
93        .set_fragment_code(FRAGMENT_DRAWING_SHADER)
94        .build()
95        .expect("Failed to create graphics shader");
96
97    let compute_shader = gpu
98        .create_compute_shader()
99        .set_source(COMPUTE_NOOP_SHADER)
100        .build()
101        .expect("Failed to create compute shader");
102
103    let pipeline = gpu
104        .create_render_pipeline()
105        .set_shader(Some(&shader))
106        .set_blend(Some(&BlendState::ALPHA_BLEND))
107        .set_attachment_texture(0, 0, Some(&blank_texture))
108        .set_attachment_sampler(0, 1, Some(&TextureSampler::DEFAULT))
109        .build()
110        .expect("Failed to create render pipeline");
111
112    let compute_pipeline = gpu
113        .create_compute_pipeline()
114        .set_shader(Some(&compute_shader))
115        .build()
116        .expect("Failed to create compute pipeline");
117
118    // Triangle vertices
119    let vertices = vec![
120        Vertex {
121            position: Vector3::new(-0.5, -0.5, 0.0),
122            color: Color::new(1.0, 0.0, 0.0, 1.0),
123            texcoord: Vector2::new(0.0, 1.0),
124        },
125        Vertex {
126            position: Vector3::new(0.5, -0.5, 0.0),
127            color: Color::new(0.0, 1.0, 0.0, 1.0),
128            texcoord: Vector2::new(1.0, 1.0),
129        },
130        Vertex {
131            position: Vector3::new(0.0, 0.5, 0.0),
132            color: Color::new(0.0, 0.0, 1.0, 1.0),
133            texcoord: Vector2::new(0.5, 0.0),
134        },
135    ];
136
137    let indexes = vec![0u16, 1u16, 2u16];
138
139    let vbo = gpu
140        .create_buffer()
141        .set_data_vec(vertices)
142        .set_usage(BufferUsage::VERTEX)
143        .build()
144        .expect("Failed to create vertex buffer");
145
146    let ibo = gpu
147        .create_buffer()
148        .set_data_vec(indexes)
149        .set_usage(BufferUsage::INDEX)
150        .build()
151        .expect("Failed to create index buffer");
152
153    while runner.pool_events(PollMode::WaitDraw) {
154        for event in runner.get_events() {
155            match event {
156                Event::KeyboardInput {
157                    window_id,
158                    key,
159                    pressed,
160                } => {
161                    if *window_id == window.id() && key == "Escape" && *pressed {
162                        window.quit();
163                    }
164                }
165                Event::WindowResized { window_id: _, size } => {
166                    if size.x <= 0 || size.y <= 0 {
167                        continue; // Skip invalid sizes
168                    }
169
170                    msaa_texture = gpu
171                        .create_texture()
172                        .set_render_target(Point2::new(size.x as u32, size.y as u32), None)
173                        .set_sample_count(SampleCount::SampleCount4)
174                        .build()
175                        .expect("Failed to resize MSAA texture");
176                }
177                Event::RedrawRequested { window_id: _ } => {
178                    if let Ok(mut cmd) = gpu.begin_command() {
179                        if let Ok(mut cm) = cmd.begin_computepass() {
180                            cm.set_pipeline(Some(&compute_pipeline));
181                            cm.dispatch(1, 1, 1);
182                        }
183
184                        if let Ok(mut rp) = cmd.begin_renderpass() {
185                            rp.set_clear_color(Color::BLACK);
186                            rp.push_msaa_texture(&msaa_texture);
187
188                            rp.set_pipeline(Some(&pipeline));
189                            rp.set_gpu_buffer(Some(&vbo), Some(&ibo));
190                            rp.draw_indexed(0..3, 0, 1);
191                        }
192                    }
193
194                    window.request_redraw();
195                }
196                _ => {}
197            }
198        }
199    }
200}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more