Skip to content

Commit

Permalink
Rename RenderPrimitive2 to RenderPrimitive and switch Renderer to it
Browse files Browse the repository at this point in the history
  • Loading branch information
dzil123 committed Jul 28, 2021
1 parent 6a2f3da commit 5f9ae3a
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 76 deletions.
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ impl Into<Option<Arrow>> for Primitive {
}
}

impl draw::renderer::RenderPrimitive2 for Arrow {
impl draw::renderer::RenderPrimitive for Arrow {
fn render_primitive<R>(
self,
_ctxt: draw::renderer::RenderContext2,
_ctxt: draw::renderer::RenderContext,
mut renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/ellipse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ impl Ellipse {

// Trait implementations.

impl draw::renderer::RenderPrimitive2 for Ellipse {
impl draw::renderer::RenderPrimitive for Ellipse {
fn render_primitive<R>(
self,
_ctxt: draw::renderer::RenderContext2,
_ctxt: draw::renderer::RenderContext,
renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ impl Into<Option<Line>> for Primitive {
}
}

impl draw::renderer::RenderPrimitive2 for Line {
impl draw::renderer::RenderPrimitive for Line {
fn render_primitive<R>(
self,
_ctxt: draw::renderer::RenderContext2,
_ctxt: draw::renderer::RenderContext,
mut renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/mesh/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ pub fn render_mesh(
}
}

impl draw::renderer::RenderPrimitive2 for Mesh {
impl draw::renderer::RenderPrimitive for Mesh {
fn render_primitive<R>(
self,
ctxt: draw::renderer::RenderContext2,
ctxt: draw::renderer::RenderContext,
mut renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/path/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ pub(crate) fn render_path_points_textured<I>(
}
}

impl draw::renderer::RenderPrimitive2 for Path {
impl draw::renderer::RenderPrimitive for Path {
fn render_primitive<R>(
self,
ctxt: draw::renderer::RenderContext2,
ctxt: draw::renderer::RenderContext,
mut renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
6 changes: 3 additions & 3 deletions nannou/src/draw/primitive/polygon/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ pub fn render_points_themed<I, R>(
);
}

impl draw::renderer::RenderPrimitive2 for Polygon {
impl draw::renderer::RenderPrimitive for Polygon {
fn render_primitive<R>(
self,
ctxt: draw::renderer::RenderContext2,
ctxt: draw::renderer::RenderContext,
mut renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand All @@ -77,7 +77,7 @@ impl draw::renderer::RenderPrimitive2 for Polygon {
},
texture_view,
} = self;
let draw::renderer::RenderContext2 {
let draw::renderer::RenderContext {
path_event_buffer,
path_points_colored_buffer,
path_points_textured_buffer,
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ impl Quad {
}

// Trait implementations.
impl draw::renderer::RenderPrimitive2 for Quad {
impl draw::renderer::RenderPrimitive for Quad {
fn render_primitive<R>(
self,
_ctxt: draw::renderer::RenderContext2,
_ctxt: draw::renderer::RenderContext,
renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ impl<'a> DrawingRect<'a> {
}
}

impl draw::renderer::RenderPrimitive2 for Rect {
impl draw::renderer::RenderPrimitive for Rect {
fn render_primitive<R>(
self,
_ctxt: draw::renderer::RenderContext2,
_ctxt: draw::renderer::RenderContext,
renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/text/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ pub fn render_text(
}
}

impl draw::renderer::RenderPrimitive2 for Text {
impl draw::renderer::RenderPrimitive for Text {
fn render_primitive<R>(
self,
ctxt: draw::renderer::RenderContext2,
ctxt: draw::renderer::RenderContext,
mut renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ impl<'a> DrawingTexture<'a> {
}
}

impl draw::renderer::RenderPrimitive2 for Texture {
impl draw::renderer::RenderPrimitive for Texture {
fn render_primitive<R>(
self,
_ctxt: draw::renderer::RenderContext2,
_ctxt: draw::renderer::RenderContext,
mut renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
4 changes: 2 additions & 2 deletions nannou/src/draw/primitive/tri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ impl<'a> DrawingTri<'a> {

// Trait implementations.

impl draw::renderer::RenderPrimitive2 for Tri {
impl draw::renderer::RenderPrimitive for Tri {
fn render_primitive<R>(
self,
_ctxt: draw::renderer::RenderContext2,
_ctxt: draw::renderer::RenderContext,
renderer: R,
) -> draw::renderer::PrimitiveRender
where
Expand Down
53 changes: 19 additions & 34 deletions nannou/src/draw/renderer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
pub mod primitives;

pub use self::primitives::{PrimitiveRenderer, RenderContext2, RenderPrimitive2};
pub use self::primitives::{PrimitiveRenderer, RenderContext, RenderPrimitive};
use crate::draw;
use crate::draw::mesh::vertex::{Color, TexCoords};
use crate::frame::Frame;
use crate::geom::{self, Point2, Rect};
use crate::geom::{self, Rect};
use crate::glam::{Mat4, Vec2, Vec3};
use crate::math::map_range;
use crate::text;
use crate::wgpu;
use lyon::path::PathEvent;
use lyon::tessellation::{FillTessellator, StrokeTessellator};
use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use wgpu::util::{BufferInitDescriptor, DeviceExt};

/// Draw API primitives that may be rendered via the **Renderer** type.
pub trait RenderPrimitive {
/// Render self into the given mesh.
fn render_primitive(self, ctxt: RenderContext, mesh: &mut draw::Mesh) -> PrimitiveRender;
}

/// Information about the way in which a primitive was rendered.
pub struct PrimitiveRender {
/// Whether or not a specific texture must be available when this primitive is drawn.
Expand All @@ -35,20 +27,16 @@ pub struct PrimitiveRender {
pub vertex_mode: VertexMode,
}

/// The context provided to primitives to assist with the rendering process.
pub struct RenderContext<'a> {
pub transform: &'a Mat4,
pub intermediary_mesh: &'a draw::Mesh,
pub path_event_buffer: &'a [PathEvent],
pub path_points_colored_buffer: &'a [(Point2, Color)],
pub path_points_textured_buffer: &'a [(Point2, TexCoords)],
pub text_buffer: &'a str,
pub theme: &'a draw::Theme,
pub glyph_cache: &'a mut GlyphCache,
pub fill_tessellator: &'a mut FillTessellator,
pub stroke_tessellator: &'a mut StrokeTessellator,
pub output_attachment_size: Vec2, // logical coords
pub output_attachment_scale_factor: f32,
struct PrimitiveRendererImpl<'a> {
mesh: &'a mut draw::Mesh,
transform: &'a Mat4,
intermediary_mesh: &'a draw::Mesh,
theme: &'a draw::Theme,
glyph_cache: &'a mut GlyphCache,
fill_tessellator: &'a mut FillTessellator,
stroke_tessellator: &'a mut StrokeTessellator,
output_attachment_size: Vec2, // logical coords
output_attachment_scale_factor: f32,
}

pub struct GlyphCache {
Expand Down Expand Up @@ -174,14 +162,6 @@ impl Default for PrimitiveRender {
}
}

impl RenderPrimitive for draw::Primitive {
fn render_primitive(self, _ctxt: RenderContext, _mesh: &mut draw::Mesh) -> PrimitiveRender {
match self {
_ => PrimitiveRender::default(),
}
}
}

impl fmt::Debug for GlyphCache {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("GlyphCache")
Expand Down Expand Up @@ -556,14 +536,19 @@ impl Renderer {

// Info required during rendering.
let ctxt = RenderContext {
intermediary_mesh: &intermediary_state.intermediary_mesh,
path_event_buffer: &intermediary_state.path_event_buffer,
path_points_colored_buffer: &intermediary_state.path_points_colored_buffer,
path_points_textured_buffer: &intermediary_state
.path_points_textured_buffer,
text_buffer: &intermediary_state.text_buffer,
theme: &draw_state.theme,
};

let renderer = PrimitiveRendererImpl {
mesh: &mut self.mesh,
transform: &curr_ctxt.transform,
intermediary_mesh: &intermediary_state.intermediary_mesh,
theme: &draw_state.theme,
fill_tessellator: &mut fill_tessellator,
stroke_tessellator: &mut stroke_tessellator,
glyph_cache: &mut self.glyph_cache,
Expand All @@ -572,7 +557,7 @@ impl Renderer {
};

// Render the primitive.
let render = RenderPrimitive::render_primitive(prim, ctxt, &mut self.mesh);
let render = RenderPrimitive::render_primitive(prim, ctxt, renderer);

// If the mesh indices are unchanged, there's nothing to be drawn.
if prev_index_count == self.mesh.indices().len() as u32 {
Expand Down
27 changes: 8 additions & 19 deletions nannou/src/draw/renderer/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ use crate::draw::primitive::path::render::{
render_path_events, render_path_points_colored, render_path_points_textured,
};
use crate::draw::primitive::text::render_text;
use crate::draw::renderer::PrimitiveRendererImpl;
use crate::geom::Point2;
use crate::glam::{Mat4, Vec2};
use draw::primitive::path::Options;
use draw::renderer::{GlyphCache, PrimitiveRender};
use draw::renderer::PrimitiveRender;
use lyon::path::PathEvent;
use lyon::tessellation::{FillTessellator, StrokeTessellator};

pub struct RenderContext2<'a> {
/// The context provided to primitives to assist with the rendering process.
pub struct RenderContext<'a> {
pub path_event_buffer: &'a [PathEvent],
pub path_points_colored_buffer: &'a [(Point2, Color)],
pub path_points_textured_buffer: &'a [(Point2, TexCoords)],
pub text_buffer: &'a str,
pub theme: &'a draw::Theme,
}

pub trait RenderPrimitive2 {
pub trait RenderPrimitive {
/// Render self into the given PrimitiveRenderer.
fn render_primitive<R>(self, ctxt: RenderContext2, renderer: R) -> PrimitiveRender
fn render_primitive<R>(self, ctxt: RenderContext, renderer: R) -> PrimitiveRender
where
R: PrimitiveRenderer;
}
Expand Down Expand Up @@ -71,8 +72,8 @@ pub trait PrimitiveRenderer {
);
}

impl RenderPrimitive2 for draw::Primitive {
fn render_primitive<R>(self, ctxt: RenderContext2, renderer: R) -> PrimitiveRender
impl RenderPrimitive for draw::Primitive {
fn render_primitive<R>(self, ctxt: RenderContext, renderer: R) -> PrimitiveRender
where
R: PrimitiveRenderer,
{
Expand All @@ -93,18 +94,6 @@ impl RenderPrimitive2 for draw::Primitive {
}
}

struct PrimitiveRendererImpl<'a> {
mesh: &'a mut draw::Mesh,
transform: &'a Mat4,
intermediary_mesh: &'a draw::Mesh,
theme: &'a draw::Theme,
glyph_cache: &'a mut GlyphCache,
fill_tessellator: &'a mut FillTessellator,
stroke_tessellator: &'a mut StrokeTessellator,
output_attachment_size: Vec2, // logical coords
output_attachment_scale_factor: f32,
}

impl<'a> PrimitiveRenderer for PrimitiveRendererImpl<'a> {
fn path_flat_color(
&mut self,
Expand Down

0 comments on commit 5f9ae3a

Please sign in to comment.