Skip to content

Commit 24facb8

Browse files
committed
Add uniform parameter also to fragment shader
1 parent 6f9d3c1 commit 24facb8

File tree

15 files changed

+82
-70
lines changed

15 files changed

+82
-70
lines changed

core/examples/hello_tri.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
// Interpolate vertex colors in linear color space
1616
vertex(mvp.apply(&v.pos), v.attrib.to_linear())
1717
},
18-
|frag: Frag<Color3f<_>>| frag.var.to_srgb().to_color4(),
18+
|frag: Frag<Color3f<_>>, _| frag.var.to_srgb().to_color4(),
1919
);
2020
#[cfg(not(feature = "fp"))]
2121
let shader = shader::new(
@@ -24,7 +24,7 @@ fn main() {
2424
// Interpolate vertex colors in normal sRGB color space
2525
vertex(mvp.apply(&v.pos), v.attrib)
2626
},
27-
|frag: Frag<Color3f<_>>| frag.var.to_color4(),
27+
|frag: Frag<Color3f<_>>, _| frag.var.to_color4(),
2828
);
2929

3030
let dims @ (w, h) = (640, 480);

core/src/render.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ pub type NdcToScreen = RealToReal<3, Ndc, Screen>;
123123

124124
/// Alias for combined vertex+fragment shader types
125125
pub trait Shader<Vtx, Var, Uni>:
126-
VertexShader<Vtx, Uni, Output = Vertex<ProjVec3, Var>> + FragmentShader<Var>
126+
VertexShader<Vtx, Uni, Output = Vertex<ProjVec3, Var>>
127+
+ FragmentShader<Var, Uni>
127128
{
128129
}
129130
impl<S, Vtx, Var, Uni> Shader<Vtx, Var, Uni> for S where
130131
S: VertexShader<Vtx, Uni, Output = Vertex<ProjVec3, Var>>
131-
+ FragmentShader<Var>
132+
+ FragmentShader<Var, Uni>
132133
{
133134
}
134135

@@ -202,7 +203,7 @@ pub fn render<Prim, Vtx: Clone, Var, Uni: Copy, Shd>(
202203
// Convert to fragments, shade, and draw to target
203204
stats.frags += target
204205
.deref_mut()
205-
.rasterize(scanline, shader, ctx);
206+
.rasterize(scanline, uniform, shader, ctx);
206207
});
207208
}
208209
*ctx.stats.borrow_mut() += stats.finish();

core/src/render/cam.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl<T> Camera<T> {
214214
pub type CameraUni<'a, B, Uni> = (&'a ProjMat3<B>, Uni);
215215

216216
impl<T: Transform> Camera<T> {
217-
/// Returns the camera matrix.
217+
/// Returns the camera (view, eye) matrix.
218218
pub fn world_to_view(&self) -> Mat4<World, View> {
219219
self.transform.world_to_view()
220220
}
@@ -227,10 +227,6 @@ impl<T: Transform> Camera<T> {
227227
pub fn world_to_project(&self) -> ProjMat3<World> {
228228
self.world_to_view().then(&self.project)
229229
}
230-
/// Returns the camera (view) matrix.
231-
pub fn world_to_view(&self) -> Mat4<World, View> {
232-
self.transform.world_to_view()
233-
}
234230

235231
/// Renders the given geometry from the viewpoint of this camera.
236232
pub fn render<B, Prim, Vtx: Clone, Var: Vary, Uni: Copy, Shd>(

core/src/render/debug.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ impl<'a, B> VertexShader<Vertex3<Color4f, B>, &'a ProjMat3<B>> for Shader {
3030
}
3131
}
3232

33-
impl FragmentShader<Color4f> for Shader {
34-
fn shade_fragment(&self, f: Frag<Color4f>) -> Option<Color4> {
33+
impl<'a, B> FragmentShader<Color4f, &'a ProjMat3<B>> for Shader {
34+
fn shade_fragment(
35+
&self,
36+
f: Frag<Color4f>,
37+
_: &'a ProjMat3<B>,
38+
) -> Option<Color4> {
3539
Some(f.var.to_color4())
3640
}
3741
}

core/src/render/shader.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ pub trait VertexShader<In, Uni> {
4545
///
4646
/// # Type parameters
4747
/// * `Var`: The varying of the input fragment.
48-
pub trait FragmentShader<Var> {
48+
pub trait FragmentShader<Var, Uni> {
4949
/// Computes the color of `frag`. Returns either `Some(color)`, or `None`
5050
/// if the fragment should be discarded.
5151
///
5252
/// # Panics
5353
/// `shade_fragment` should never panic.
54-
fn shade_fragment(&self, frag: Frag<Var>) -> Option<Color4>;
54+
fn shade_fragment(&self, frag: Frag<Var>, uniform: Uni) -> Option<Color4>;
5555
}
5656

5757
impl<F, In, Out, Uni> VertexShader<In, Uni> for F
@@ -65,20 +65,20 @@ where
6565
}
6666
}
6767

68-
impl<F, Var, Out> FragmentShader<Var> for F
68+
impl<F, Var, Out, Uni> FragmentShader<Var, Uni> for F
6969
where
70-
F: Fn(Frag<Var>) -> Out,
70+
F: Fn(Frag<Var>, Uni) -> Out,
7171
Out: Into<Option<Color4>>,
7272
{
73-
fn shade_fragment(&self, frag: Frag<Var>) -> Option<Color4> {
74-
self(frag).into()
73+
fn shade_fragment(&self, frag: Frag<Var>, uniform: Uni) -> Option<Color4> {
74+
self(frag, uniform).into()
7575
}
7676
}
7777

7878
pub fn new<Vs, Fs, Vtx, Var, Uni>(vs: Vs, fs: Fs) -> Shader<Vs, Fs>
7979
where
8080
Vs: VertexShader<Vtx, Uni, Output = Vertex<ProjVec3, Var>>,
81-
Fs: FragmentShader<Var>,
81+
Fs: FragmentShader<Var, Uni>,
8282
{
8383
Shader::new(vs, fs)
8484
}
@@ -96,7 +96,7 @@ impl<Vs, Fs> Shader<Vs, Fs> {
9696
pub const fn new<In, Uni, Pos, Attr>(vs: Vs, fs: Fs) -> Self
9797
where
9898
Vs: VertexShader<In, Uni, Output = Vertex<Pos, Attr>>,
99-
Fs: FragmentShader<Attr>,
99+
Fs: FragmentShader<Attr, Uni>,
100100
{
101101
Self {
102102
vertex_shader: vs,
@@ -116,11 +116,11 @@ where
116116
}
117117
}
118118

119-
impl<Vs, Fs, Var> FragmentShader<Var> for Shader<Vs, Fs>
119+
impl<Vs, Fs, Var, Uni> FragmentShader<Var, Uni> for Shader<Vs, Fs>
120120
where
121-
Fs: FragmentShader<Var>,
121+
Fs: FragmentShader<Var, Uni>,
122122
{
123-
fn shade_fragment(&self, frag: Frag<Var>) -> Option<Color4> {
124-
self.fragment_shader.shade_fragment(frag)
123+
fn shade_fragment(&self, frag: Frag<Var>, uni: Uni) -> Option<Color4> {
124+
self.fragment_shader.shade_fragment(frag, uni)
125125
}
126126
}

core/src/render/target.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ pub trait Target {
1919
/// Writes a single scanline into `self`.
2020
///
2121
/// Returns count of fragments input and output.
22-
fn rasterize<V, Fs>(
22+
fn rasterize<V, U, Fs>(
2323
&mut self,
2424
scanline: Scanline<V>,
25+
uniform: U,
2526
frag_shader: &Fs,
2627
ctx: &Context,
2728
) -> Throughput
2829
where
2930
V: Vary,
30-
Fs: FragmentShader<V>;
31+
U: Copy,
32+
Fs: FragmentShader<V, U>;
3133
}
3234

3335
/// Framebuffer, combining a color (pixel) buffer and a depth buffer.
@@ -56,23 +58,25 @@ impl<T, B: AsMutSlice2<T>, F> AsMutSlice2<T> for Colorbuf<B, F> {
5658
}
5759

5860
impl<T: Target> Target for &mut T {
59-
fn rasterize<V: Vary, Fs: FragmentShader<V>>(
61+
fn rasterize<V: Vary, U: Copy, Fs: FragmentShader<V, U>>(
6062
&mut self,
6163
sl: Scanline<V>,
64+
uni: U,
6265
fs: &Fs,
6366
ctx: &Context,
6467
) -> Throughput {
65-
(*self).rasterize(sl, fs, ctx)
68+
(*self).rasterize(sl, uni, fs, ctx)
6669
}
6770
}
6871
impl<T: Target> Target for &RefCell<T> {
69-
fn rasterize<V: Vary, Fs: FragmentShader<V>>(
72+
fn rasterize<V: Vary, U: Copy, Fs: FragmentShader<V, U>>(
7073
&mut self,
7174
sl: Scanline<V>,
75+
uni: U,
7276
fs: &Fs,
7377
ctx: &Context,
7478
) -> Throughput {
75-
RefCell::borrow_mut(self).rasterize(sl, fs, ctx)
79+
RefCell::borrow_mut(self).rasterize(sl, uni, fs, ctx)
7680
}
7781
}
7882

@@ -83,14 +87,15 @@ where
8387
Color4: IntoPixel<u32, Fmt>,
8488
{
8589
/// Rasterizes `scanline` into this framebuffer.
86-
fn rasterize<V: Vary, Fs: FragmentShader<V>>(
90+
fn rasterize<V: Vary, U: Copy, Fs: FragmentShader<V, U>>(
8791
&mut self,
8892
sl: Scanline<V>,
93+
uni: U,
8994
fs: &Fs,
9095
ctx: &Context,
9196
) -> Throughput {
9297
let Self { color_buf, depth_buf } = self;
93-
rasterize_fb(color_buf, depth_buf, sl, fs, Color4::into_pixel, ctx)
98+
rasterize_fb(color_buf, depth_buf, sl, uni, fs, Color4::into_pixel, ctx)
9499
}
95100
}
96101

@@ -101,42 +106,46 @@ where
101106
{
102107
/// Rasterizes `scanline` into this `u32` color buffer.
103108
/// Does no z-buffering.
104-
fn rasterize<V: Vary, Fs: FragmentShader<V>>(
109+
fn rasterize<V: Vary, U: Copy, Fs: FragmentShader<V, U>>(
105110
&mut self,
106111
sl: Scanline<V>,
112+
uni: U,
107113
fs: &Fs,
108114
ctx: &Context,
109115
) -> Throughput {
110-
rasterize(&mut self.buf, sl, fs, Color4::into_pixel, ctx)
116+
rasterize(&mut self.buf, sl, uni, fs, Color4::into_pixel, ctx)
111117
}
112118
}
113119

114120
impl Target for Buf2<Color4> {
115-
fn rasterize<V: Vary, Fs: FragmentShader<V>>(
121+
fn rasterize<V: Vary, U: Copy, Fs: FragmentShader<V, U>>(
116122
&mut self,
117123
sl: Scanline<V>,
124+
uni: U,
118125
fs: &Fs,
119126
ctx: &Context,
120127
) -> Throughput {
121-
rasterize(self, sl, fs, |c| c, ctx)
128+
rasterize(self, sl, uni, fs, |c| c, ctx)
122129
}
123130
}
124131

125132
impl Target for Buf2<Color3> {
126-
fn rasterize<V: Vary, Fs: FragmentShader<V>>(
133+
fn rasterize<V: Vary, U: Copy, Fs: FragmentShader<V, U>>(
127134
&mut self,
128135
sl: Scanline<V>,
136+
uni: U,
129137
fs: &Fs,
130138
ctx: &Context,
131139
) -> Throughput {
132-
rasterize(self, sl, fs, |c| c.to_rgb(), ctx)
140+
rasterize(self, sl, uni, fs, |c| c.to_rgb(), ctx)
133141
}
134142
}
135143

136-
pub fn rasterize<T, V: Vary>(
144+
pub fn rasterize<T, V: Vary, U: Copy>(
137145
buf: &mut impl AsMutSlice2<T>,
138146
mut sl: Scanline<V>,
139-
fs: &impl FragmentShader<V>,
147+
uni: U,
148+
fs: &impl FragmentShader<V, U>,
140149
mut conv: impl FnMut(Color4) -> T,
141150
ctx: &Context,
142151
) -> Throughput {
@@ -148,7 +157,7 @@ pub fn rasterize<T, V: Vary>(
148157
sl.fragments()
149158
.zip(cbuf_span)
150159
.for_each(|(frag, curr_col)| {
151-
if let Some(new_col) = fs.shade_fragment(frag)
160+
if let Some(new_col) = fs.shade_fragment(frag, uni)
152161
&& ctx.color_write
153162
{
154163
io.o += 1;
@@ -158,11 +167,12 @@ pub fn rasterize<T, V: Vary>(
158167
io
159168
}
160169

161-
pub fn rasterize_fb<T, V: Vary>(
170+
pub fn rasterize_fb<T, V: Vary, U: Copy>(
162171
cbuf: &mut impl AsMutSlice2<T>,
163172
zbuf: &mut impl AsMutSlice2<f32>,
164173
mut sl: Scanline<V>,
165-
fs: &impl FragmentShader<V>,
174+
uni: U,
175+
fs: &impl FragmentShader<V, U>,
166176
mut conv: impl FnMut(Color4) -> T,
167177
ctx: &Context,
168178
) -> Throughput {
@@ -180,7 +190,7 @@ pub fn rasterize_fb<T, V: Vary>(
180190
let new_z = frag.pos.z();
181191

182192
if ctx.depth_test(new_z, *curr_z)
183-
&& let Some(new_col) = fs.shade_fragment(frag)
193+
&& let Some(new_col) = fs.shade_fragment(frag, uni)
184194
{
185195
if ctx.color_write {
186196
io.o += 1;

core/tests/rendering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn textured_quad() {
2727
|v: Vertex3<_>, mvp: &ProjMat3<Model>| {
2828
vertex(mvp.apply(&v.pos), v.attrib)
2929
},
30-
|frag: Frag<_>| SamplerClamp.sample(&checker, frag.var),
30+
|frag: Frag<_>, _| SamplerClamp.sample(&checker, frag.var),
3131
);
3232

3333
let (w, h) = (256, 256);

demos/src/bin/crates.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn main() {
4141
|v: Vertex3<_>, u: &Uniform| {
4242
vertex(u.mv.then(&u.proj).apply(&v.pos), v.attrib)
4343
},
44-
|frag: Frag<Vec2>| {
44+
|frag: Frag<Vec2>, _: &Uniform| {
4545
let even_odd = (frag.var.x() > 0.5) ^ (frag.var.y() > 0.5);
4646
gray(if even_odd { 0.8 } else { 0.1 }).to_color4()
4747
},
@@ -50,13 +50,14 @@ fn main() {
5050
|v: Vertex3<Varying>, u: &Uniform| {
5151
vertex(u.mv.then(&u.proj).apply(&v.pos), v.attrib)
5252
},
53-
|frag: Frag<Varying>| {
53+
|frag: Frag<Varying>, _: &Uniform| {
5454
let (n, uv) = frag.var;
5555
let kd = lerp(n.dot(&light_dir).max(0.0), 0.4, 1.0);
5656
let col = SamplerClamp.sample(&tex, uv);
5757
(col.to_color3f() * kd).to_color4()
5858
},
5959
);
60+
6061
let shader3 = shader::new(
6162
|v: Vertex3<Color3f>, u: &Uniform| {
6263
let pos = u.mv.apply(&v.pos);
@@ -66,10 +67,11 @@ fn main() {
6667
// TODO light_col * surface_col should be compwise multiplication
6768
vertex(u.proj.apply(&pos), light_col.mul(lam).mul(v.attrib.r()))
6869
},
69-
|f: Frag<Color3f>| f.var.to_color4(),
70+
|f: Frag<Color3f>, _: &_| f.var.to_color4(),
7071
);
7172

72-
let crate_shader2 = shader::new(
73+
let crate_shader_light = shader::new(
74+
// TODO
7375
|v: Vertex3<Normal3>, u: &Uniform| {
7476
let n_modl = v.attrib.to();
7577
let n_view = u.mv.apply(&n_modl);
@@ -91,7 +93,7 @@ fn main() {
9193

9294
vertex(pos_proj, color)
9395
},
94-
|frag: Frag<Color3f>| {
96+
|frag: Frag<Color3f>, _: &_| {
9597
//let [x, y, z] = ((f.var + splat(1.0)) / 2.0).0;
9698
//rgb(x, y, z).to_color4()
9799
frag.var.to_color4()
@@ -156,8 +158,6 @@ fn main() {
156158
// Render
157159
//
158160

159-
let world_to_project = &cam.world_to_project();
160-
161161
let batch = Batch::new()
162162
.viewport(cam.viewport)
163163
.target(frame.buf)
@@ -170,12 +170,13 @@ fn main() {
170170
let proj = cam.project;
171171
let mvp = mv.then(&proj);
172172
let light = Default::default();
173+
let uni = Uniform { mv, proj, light };
173174

174175
if bbox.visibility(&mvp) != Hidden {
175176
batch
176177
.clone()
177178
.mesh(geom)
178-
.uniform(&Uniform { mv, proj, light })
179+
.uniform(&uni)
179180
.shader(floor_shader)
180181
.render();
181182
}

0 commit comments

Comments
 (0)