Simplify gridlines in the demo shader

This commit is contained in:
Patrick Walton 2019-05-02 13:13:43 -07:00
parent cf3dcfdd0e
commit 5e64876579
4 changed files with 43 additions and 121 deletions

View File

@ -10,9 +10,8 @@
//! GPU rendering code specifically for the demo.
use crate::GRIDLINE_COUNT;
use pathfinder_gpu::resources::ResourceLoader;
use pathfinder_gpu::{BufferData, BufferTarget, BufferUploadMode, Device, VertexAttrType};
use pathfinder_gpu::{BufferTarget, Device, VertexAttrType};
pub struct GroundProgram<D>
where
@ -20,7 +19,9 @@ where
{
pub program: D::Program,
pub transform_uniform: D::Uniform,
pub color_uniform: D::Uniform,
pub gridline_count_uniform: D::Uniform,
pub ground_color_uniform: D::Uniform,
pub gridline_color_uniform: D::Uniform,
}
impl<D> GroundProgram<D>
@ -30,23 +31,27 @@ where
pub fn new(device: &D, resources: &dyn ResourceLoader) -> GroundProgram<D> {
let program = device.create_program(resources, "demo_ground");
let transform_uniform = device.get_uniform(&program, "Transform");
let color_uniform = device.get_uniform(&program, "Color");
let gridline_count_uniform = device.get_uniform(&program, "GridlineCount");
let ground_color_uniform = device.get_uniform(&program, "GroundColor");
let gridline_color_uniform = device.get_uniform(&program, "GridlineColor");
GroundProgram {
program,
transform_uniform,
color_uniform,
gridline_count_uniform,
ground_color_uniform,
gridline_color_uniform,
}
}
}
pub struct GroundSolidVertexArray<D>
pub struct GroundVertexArray<D>
where
D: Device,
{
pub vertex_array: D::VertexArray,
}
impl<D> GroundSolidVertexArray<D>
impl<D> GroundVertexArray<D>
where
D: Device,
{
@ -54,7 +59,7 @@ where
device: &D,
ground_program: &GroundProgram<D>,
quad_vertex_positions_buffer: &D::Buffer,
) -> GroundSolidVertexArray<D> {
) -> GroundVertexArray<D> {
let vertex_array = device.create_vertex_array();
let position_attr = device.get_vertex_attr(&ground_program.program, "Position");
@ -64,57 +69,6 @@ where
device.bind_buffer(quad_vertex_positions_buffer, BufferTarget::Vertex);
device.configure_float_vertex_attr(&position_attr, 2, VertexAttrType::U8, false, 0, 0, 0);
GroundSolidVertexArray { vertex_array }
GroundVertexArray { vertex_array }
}
}
pub struct GroundLineVertexArray<D>
where
D: Device,
{
pub vertex_array: D::VertexArray,
#[allow(dead_code)]
grid_vertex_positions_buffer: D::Buffer,
}
impl<D> GroundLineVertexArray<D>
where
D: Device,
{
pub fn new(device: &D, ground_program: &GroundProgram<D>) -> GroundLineVertexArray<D> {
let grid_vertex_positions_buffer = device.create_buffer();
device.allocate_buffer(
&grid_vertex_positions_buffer,
BufferData::Memory(&create_grid_vertex_positions()),
BufferTarget::Vertex,
BufferUploadMode::Static,
);
let vertex_array = device.create_vertex_array();
let position_attr = device.get_vertex_attr(&ground_program.program, "Position");
device.bind_vertex_array(&vertex_array);
device.use_program(&ground_program.program);
device.bind_buffer(&grid_vertex_positions_buffer, BufferTarget::Vertex);
device.configure_float_vertex_attr(&position_attr, 2, VertexAttrType::U8, false, 0, 0, 0);
GroundLineVertexArray {
vertex_array,
grid_vertex_positions_buffer,
}
}
}
fn create_grid_vertex_positions() -> Vec<(u8, u8)> {
let mut positions = vec![];
for index in 0..(GRIDLINE_COUNT + 1) {
positions.extend_from_slice(&[
(0, index),
(GRIDLINE_COUNT, index),
(index, 0),
(index, GRIDLINE_COUNT),
]);
}
positions
}

View File

@ -14,7 +14,7 @@
extern crate log;
use crate::concurrent::DemoExecutor;
use crate::device::{GroundLineVertexArray, GroundProgram, GroundSolidVertexArray};
use crate::device::{GroundProgram, GroundVertexArray};
use crate::ui::{DemoUI, UIAction};
use crate::window::{Event, Keycode, OcularTransform, SVGPath, View, Window, WindowSize};
use clap::{App, Arg};
@ -27,7 +27,7 @@ use pathfinder_geometry::color::{ColorF, ColorU};
use pathfinder_gl::GLDevice;
use pathfinder_gpu::resources::ResourceLoader;
use pathfinder_gpu::{ClearParams, DepthFunc, DepthState, Device, Primitive, RenderState};
use pathfinder_gpu::{StencilFunc, StencilState, TextureFormat, UniformData};
use pathfinder_gpu::{TextureFormat, UniformData};
use pathfinder_renderer::concurrent::scene_proxy::{RenderCommandStream, SceneProxy};
use pathfinder_renderer::gpu::renderer::{DestFramebuffer, RenderMode, RenderStats, Renderer};
use pathfinder_renderer::gpu_data::RenderCommand;
@ -96,7 +96,7 @@ const MESSAGE_TIMEOUT_SECS: u64 = 5;
// Half of the eye separation distance.
const DEFAULT_EYE_OFFSET: f32 = 0.025;
pub const GRIDLINE_COUNT: u8 = 10;
pub const GRIDLINE_COUNT: i32 = 10;
pub mod window;
@ -134,8 +134,7 @@ pub struct DemoApp<W> where W: Window {
scene_framebuffer: Option<<GLDevice as Device>::Framebuffer>,
ground_program: GroundProgram<GLDevice>,
ground_solid_vertex_array: GroundSolidVertexArray<GLDevice>,
ground_line_vertex_array: GroundLineVertexArray<GLDevice>,
ground_vertex_array: GroundVertexArray<GLDevice>,
}
impl<W> DemoApp<W> where W: Window {
@ -168,13 +167,9 @@ impl<W> DemoApp<W> where W: Window {
let scene_proxy = SceneProxy::new(built_svg.scene, executor);
let ground_program = GroundProgram::new(&renderer.device, resources);
let ground_solid_vertex_array = GroundSolidVertexArray::new(
&renderer.device,
&ground_program,
&renderer.quad_vertex_positions_buffer(),
);
let ground_line_vertex_array =
GroundLineVertexArray::new(&renderer.device, &ground_program);
let ground_vertex_array = GroundVertexArray::new(&renderer.device,
&ground_program,
&renderer.quad_vertex_positions_buffer());
let mut ui = DemoUI::new(&renderer.device, resources, options.clone());
let mut message_epoch = 0;
@ -215,8 +210,7 @@ impl<W> DemoApp<W> where W: Window {
scene_framebuffer: None,
ground_program,
ground_solid_vertex_array,
ground_line_vertex_array,
ground_vertex_array,
}
}
@ -731,57 +725,28 @@ impl<W> DemoApp<W> where W: Window {
-0.5 * ground_scale,
));
// Draw gridlines. Use the stencil buffer to avoid Z-fighting.
let mut transform = base_transform;
let gridline_scale = ground_scale / GRIDLINE_COUNT as f32;
transform = transform.post_mul(&Transform3DF32::from_scale(
gridline_scale,
1.0,
gridline_scale,
));
let device = &self.renderer.device;
device.bind_vertex_array(&self.ground_line_vertex_array.vertex_array);
device.use_program(&self.ground_program.program);
device.set_uniform(
&self.ground_program.transform_uniform,
UniformData::Mat4([transform.c0, transform.c1, transform.c2, transform.c3]),
);
device.set_uniform(
&self.ground_program.color_uniform,
UniformData::Vec4(GROUND_LINE_COLOR.to_f32().0),
);
device.draw_arrays(
Primitive::Lines,
(GRIDLINE_COUNT as u32 + 1) * 4,
&RenderState {
depth: Some(DepthState {
func: DepthFunc::Always,
write: true,
}),
stencil: Some(StencilState {
func: StencilFunc::Always,
reference: 2,
mask: 2,
write: true,
}),
..RenderState::default()
},
);
// Fill ground.
let mut transform = base_transform;
transform =
transform.post_mul(&Transform3DF32::from_scale(ground_scale, 1.0, ground_scale));
device.bind_vertex_array(&self.ground_solid_vertex_array.vertex_array);
let device = &self.renderer.device;
device.bind_vertex_array(&self.ground_vertex_array.vertex_array);
device.use_program(&self.ground_program.program);
device.set_uniform(
&self.ground_program.transform_uniform,
UniformData::from_transform_3d(&transform),
);
device.set_uniform(
&self.ground_program.color_uniform,
&self.ground_program.ground_color_uniform,
UniformData::Vec4(GROUND_SOLID_COLOR.to_f32().0),
);
device.set_uniform(
&self.ground_program.gridline_color_uniform,
UniformData::Vec4(GROUND_LINE_COLOR.to_f32().0),
);
device.set_uniform(&self.ground_program.gridline_count_uniform,
UniformData::Int(GRIDLINE_COUNT));
device.draw_arrays(
Primitive::TriangleFan,
4,
@ -790,12 +755,7 @@ impl<W> DemoApp<W> where W: Window {
func: DepthFunc::Less,
write: true,
}),
stencil: Some(StencilState {
func: StencilFunc::NotEqual,
reference: 2,
mask: 2,
write: false,
}),
stencil: None,
..RenderState::default()
},
);

View File

@ -12,10 +12,14 @@
precision highp float;
uniform vec4 uColor;
uniform vec4 uGroundColor;
uniform vec4 uGridlineColor;
in vec2 vTexCoord;
out vec4 oFragColor;
void main() {
oFragColor = uColor;
vec2 texCoordPx = fract(vTexCoord) / fwidth(vTexCoord);
oFragColor = any(lessThanEqual(texCoordPx, vec2(1.0))) ? uGridlineColor : uGroundColor;
}

View File

@ -13,9 +13,13 @@
precision highp float;
uniform mat4 uTransform;
uniform int uGridlineCount;
in vec2 aPosition;
out vec2 vTexCoord;
void main() {
vTexCoord = aPosition * float(uGridlineCount);
gl_Position = uTransform * vec4(aPosition.x, 0.0, aPosition.y, 1.0);
}