Skip to content

Commit

Permalink
Cleanup names.
Browse files Browse the repository at this point in the history
  • Loading branch information
tychedelia committed Sep 27, 2024
1 parent c27fb1c commit ebc74c8
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 219 deletions.
34 changes: 17 additions & 17 deletions bevy_nannou_draw/src/draw/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ where
Err(err) => eprintln!("drawing failed to borrow state and finish: {}", err),
Ok(mut state) => {
match &self.draw {
// If we are "Owned", that means we mutated our material and so need to
// If we are "Owned", that means we mutated our shader model and so need to
// spawn a new entity just for this primitive.
DrawRef::Owned(draw) => {
let id = draw.shader_model.clone();
let material_cmd = state
let shader_model_cmd = state
.draw_commands
.get_mut(self.shader_model_index)
.expect("Expected a valid material index");
if let None = material_cmd {
*material_cmd = Some(DrawCommand::ShaderModel(id));
.expect("Expected a valid shdaer model index");
if let None = shader_model_cmd {
*shader_model_cmd = Some(DrawCommand::ShaderModel(id));
}
}
DrawRef::Borrowed(_) => (),
Expand All @@ -140,7 +140,7 @@ where
self.finish_inner()
}

// Map the the parent's material to a new material type, taking ownership over the
// Map the the parent's shader model to a new shader model type, taking ownership over the
// draw instance clone.
pub fn map_shader_model<F>(mut self, map: F) -> Drawing<'a, T, SM>
where
Expand All @@ -151,12 +151,12 @@ where
let Drawing {
ref draw,
index,
shader_model_index: material_index,
shader_model_index: shader_model_index,
..
} = self;

let state = draw.state.clone();
let material = state.read().unwrap().shader_models[&self.draw.shader_model]
let shader_model = state.read().unwrap().shader_models[&self.draw.shader_model]
.downcast_ref::<SM>()
.unwrap()
.clone();
Expand All @@ -166,10 +166,10 @@ where
uuid: Uuid::new_v4(),
};

let material = map(material.clone());
let shader_model = map(shader_model.clone());
let mut state = state.write().unwrap();
state.shader_models.insert(new_id.clone(), Box::new(material));
// Mark the last material as the new material so that further drawings use the same material
state.shader_models.insert(new_id.clone(), Box::new(shader_model));
// Mark the last shader model as the new model so that further drawings use the same model
// as the parent draw ref.
state.last_shader_model = Some(new_id.clone());

Expand All @@ -178,13 +178,13 @@ where
context: draw.context.clone(),
shader_model: new_id.clone(),
window: draw.window,
_material: Default::default(),
_shader_model: Default::default(),
};

Drawing {
draw: DrawRef::Owned(draw),
index,
shader_model_index: material_index,
shader_model_index,
finish_on_drop: true,
_ty: PhantomData,
}
Expand All @@ -208,13 +208,13 @@ where
let Drawing {
ref draw,
index,
shader_model_index: material_index,
shader_model_index,
..
} = self;
Drawing {
draw: draw.clone(),
index,
shader_model_index: material_index,
shader_model_index,
finish_on_drop: true,
_ty: PhantomData,
}
Expand Down Expand Up @@ -242,13 +242,13 @@ where
let Drawing {
ref draw,
index,
shader_model_index: material_index,
shader_model_index,
..
} = self;
Drawing {
draw: draw.clone(),
index,
shader_model_index: material_index,
shader_model_index,
finish_on_drop: true,
_ty: PhantomData,
}
Expand Down
4 changes: 2 additions & 2 deletions bevy_nannou_draw/src/draw/indirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ impl<P: PhaseItem, SM: ShaderModel, const I: usize> RenderCommand<P>
let Some(asset_id) = instances.get(&item.entity()) else {
return RenderCommandResult::Skip;
};
let Some(material) = models.get(*asset_id) else {
let Some(model) = models.get(*asset_id) else {
return RenderCommandResult::Skip;
};
pass.set_bind_group(I, &material.bind_group, &[]);
pass.set_bind_group(I, &model.bind_group, &[]);
RenderCommandResult::Success
}
}
Expand Down
4 changes: 2 additions & 2 deletions bevy_nannou_draw/src/draw/instanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ impl<P: PhaseItem, SM: ShaderModel, const I: usize> RenderCommand<P>
let Some(asset_id) = instances.get(&item.entity()) else {
return RenderCommandResult::Skip;
};
let Some(material) = models.get(*asset_id) else {
let Some(shader_model) = models.get(*asset_id) else {
return RenderCommandResult::Skip;
};
pass.set_bind_group(I, &material.bind_group, &[]);
pass.set_bind_group(I, &shader_model.bind_group, &[]);
RenderCommandResult::Success
}
}
Expand Down
40 changes: 20 additions & 20 deletions bevy_nannou_draw/src/draw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ where
pub state: Arc<RwLock<State>>,
/// The current context of this [Draw] instance.
context: DrawContext,
/// The current material of this [Draw] instance.
/// The current type erased shader model of this [Draw] instance.
shader_model: UntypedAssetId,
/// The window to which this [Draw] instance is associated.
pub(crate) window: Entity,
/// The type of material used by this [Draw] instance.
_material: PhantomData<SM>,
/// The type of shader model used by this [Draw] instance.
_shader_model: PhantomData<SM>,
}

/// A reference to a [Draw] instance that is either borrowed or owned.
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Default for DrawContext {
}
}

/// Commands generated by drawing that instruct how to create the meshes and materials that will be
/// Commands generated by drawing that instruct how to create the meshes and shader model that will be
/// rendered.
#[derive(Clone, Debug)]
pub enum DrawCommand {
Expand All @@ -138,7 +138,7 @@ pub enum DrawCommand {
/// drawing stuff. In order to be friendlier to new users, we want to avoid requiring them to think
/// about mutability and instead focus on creativity. Rust-lang nuances can come later.
pub struct State {
/// The last material used to draw an image, used to detect changes and emit commands for them.
/// The last shader model used to draw an image, used to detect changes and emit commands for them.
last_shader_model: Option<UntypedAssetId>,
/// The last context used to draw an image, used to detect changes and emit commands for them.
last_draw_context: Option<DrawContext>,
Expand All @@ -148,7 +148,7 @@ pub struct State {
///
/// Keys are indices into the `draw_commands` Vec.
drawing: HashMap<usize, Primitive>,
/// A map of all type erased materials used by the draw.
/// A map of all type erased shader models used by the draw.
pub(crate) shader_models: HashMap<UntypedAssetId, Box<dyn Any + Send + Sync>>,
/// A list of indices of primitives that are being drawn via an alternate method and
/// should not be drawn
Expand Down Expand Up @@ -245,7 +245,7 @@ where
context,
shader_model: model_id,
window,
_material: PhantomData,
_shader_model: PhantomData,
}
}

Expand Down Expand Up @@ -458,27 +458,27 @@ where
/// Produce a new [Draw] instance with the given context.
fn context(&self, context: DrawContext) -> Draw<SM> {
let state = self.state.clone();
let material = self.shader_model.clone();
let shader_model = self.shader_model.clone();
let window = self.window;
Draw {
state,
context,
shader_model: material,
shader_model,
window,
_material: PhantomData,
_shader_model: PhantomData,
}
}

fn clone_shader_model(&self) -> SM {
let mut state = self.state.write().unwrap();
let material = state.shader_models.get_mut(&self.shader_model).unwrap();
material
let shader_model = state.shader_models.get_mut(&self.shader_model).unwrap();
shader_model
.downcast_ref::<SM>()
.expect("Expected material to be of the correct type")
.expect("Expected shader model to be of the correct type")
.clone()
}

/// Produce a new [Draw] instance with a new material type.
/// Produce a new [Draw] instance with a new shader model type.
pub fn shader_model<SM2: ShaderModel + Default>(&self, model: SM2) -> Draw<SM2> {
let context = self.context.clone();
let DrawContext { transform, .. } = context;
Expand All @@ -501,7 +501,7 @@ where
context,
shader_model: model_id,
window,
_material: PhantomData,
_shader_model: PhantomData,
}
}

Expand Down Expand Up @@ -545,16 +545,16 @@ where
state.last_shader_model = Some(id.clone());
}

// Insert a material slot to be used if the drawing switches materials.
let material_index = state.draw_commands.len();
// Insert a model slot to be used if the drawing switches models.
let shader_model_index = state.draw_commands.len();
state.draw_commands.push(None);

// The primitive will be inserted in the next element.
let index = state.draw_commands.len();
let primitive: Primitive = primitive.into();
state.draw_commands.push(None);
state.drawing.insert(index, primitive);
(index, material_index)
(index, shader_model_index)
};
drawing::new(self, index, model_index)
}
Expand Down Expand Up @@ -694,15 +694,15 @@ impl Default for IntermediaryState {

impl Default for State {
fn default() -> Self {
let last_material = None;
let last_shader_model = None;
let last_draw_context = None;
let background_color = Default::default();
let draw_commands = Default::default();
let drawing = Default::default();
let intermediary_state = Arc::new(Default::default());
let theme = Default::default();
State {
last_shader_model: last_material,
last_shader_model,
last_draw_context,
draw_commands,
drawing,
Expand Down
Loading

0 comments on commit ebc74c8

Please sign in to comment.