Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Strict 32 Bit Alignment Platforms #332

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions geometry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ license = "MIT/Apache-2.0"
repository = "https://github.com/servo/pathfinder"
homepage = "https://github.com/servo/pathfinder"

[features]
shader_alignment_32_bits = []

[dependencies]

[dependencies.log]
Expand Down
19 changes: 19 additions & 0 deletions geometry/src/alignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[cfg(feature = "shader_alignment_32_bits")]
pub type AlignedU8 = u32;
#[cfg(not(feature = "shader_alignment_32_bits"))]
pub type AlignedU8 = u8;

#[cfg(feature = "shader_alignment_32_bits")]
pub type AlignedU16 = u32;
#[cfg(not(feature = "shader_alignment_32_bits"))]
pub type AlignedU16 = u16;

#[cfg(feature = "shader_alignment_32_bits")]
pub type AlignedI8 = i32;
#[cfg(not(feature = "shader_alignment_32_bits"))]
pub type AlignedI8 = i8;

#[cfg(feature = "shader_alignment_32_bits")]
pub type AlignedI16 = i32;
#[cfg(not(feature = "shader_alignment_32_bits"))]
pub type AlignedI16 = i16;
1 change: 1 addition & 0 deletions geometry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ pub mod transform3d;
pub mod unit_vector;
pub mod util;
pub mod vector;
pub mod alignment;
7 changes: 4 additions & 3 deletions geometry/src/line_segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
//! Line segment types, optimized with SIMD.

use crate::transform2d::Matrix2x2F;
use crate::vector::{Vector2F, vec2f};
use crate::alignment::AlignedU8;
use crate::util;
use crate::vector::{vec2f, Vector2F};
use pathfinder_simd::default::F32x4;
use std::ops::{Add, Mul, MulAssign, Sub};

Expand Down Expand Up @@ -294,8 +295,8 @@ impl MulAssign<Vector2F> for LineSegment2F {
#[derive(Clone, Copy, Debug, Default)]
#[repr(C)]
pub struct LineSegmentU4 {
pub from: u8,
pub to: u8,
pub from: AlignedU8,
pub to: AlignedU8,
}

#[derive(Clone, Copy, Debug, Default)]
Expand Down
2 changes: 2 additions & 0 deletions gl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,8 +1329,10 @@ impl VertexAttrTypeExt for VertexAttrType {
fn to_gl_type(self) -> GLuint {
match self {
VertexAttrType::F32 => gl::FLOAT,
VertexAttrType::I32 => gl::INT,
VertexAttrType::I16 => gl::SHORT,
VertexAttrType::I8 => gl::BYTE,
VertexAttrType::U32 => gl::UNSIGNED_INT,
VertexAttrType::U16 => gl::UNSIGNED_SHORT,
VertexAttrType::U8 => gl::UNSIGNED_BYTE,
}
Expand Down
3 changes: 3 additions & 0 deletions gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ license = "MIT/Apache-2.0"
repository = "https://github.com/servo/pathfinder"
homepage = "https://github.com/servo/pathfinder"

[features]
shader_alignment_32_bits = ["pathfinder_geometry/shader_alignment_32_bits"]

[dependencies]
bitflags = "1.0"
half = "1.5"
Expand Down
84 changes: 81 additions & 3 deletions gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,51 @@ pub enum TextureFormat {
RGBA32F,
}

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum VertexAttrType {
F32,
I32,
I16,
I8,
U32,
U16,
U8,
}

impl VertexAttrType {
pub fn get_size(&self) -> usize {
match *self {
VertexAttrType::F32 => 4,
VertexAttrType::I32 => 4,
VertexAttrType::I16 => 2,
VertexAttrType::I8 => 1,
VertexAttrType::U32 => 4,
VertexAttrType::U16 => 2,
VertexAttrType::U8 => 1,
}
}
}

#[cfg(feature = "shader_alignment_32_bits")]
pub const ALIGNED_U8_ATTR: VertexAttrType = VertexAttrType::U32;
#[cfg(not(feature = "shader_alignment_32_bits"))]
pub const ALIGNED_U8_ATTR: VertexAttrType = VertexAttrType::U8;

#[cfg(feature = "shader_alignment_32_bits")]
pub const ALIGNED_U16_ATTR: VertexAttrType = VertexAttrType::U32;
#[cfg(not(feature = "shader_alignment_32_bits"))]
pub const ALIGNED_U16_ATTR: VertexAttrType = VertexAttrType::U16;

#[cfg(feature = "shader_alignment_32_bits")]
pub const ALIGNED_I8_ATTR: VertexAttrType = VertexAttrType::I32;
#[cfg(not(feature = "shader_alignment_32_bits"))]
pub const ALIGNED_I8_ATTR: VertexAttrType = VertexAttrType::I8;

#[cfg(feature = "shader_alignment_32_bits")]
pub const ALIGNED_I16_ATTR: VertexAttrType = VertexAttrType::I32;
#[cfg(not(feature = "shader_alignment_32_bits"))]
pub const ALIGNED_I16_ATTR: VertexAttrType = VertexAttrType::I16;

#[derive(Clone, Copy, Debug)]
pub enum BufferData<'a, T> {
Uninitialized(usize),
Expand Down Expand Up @@ -420,7 +456,7 @@ impl UniformData {
}
}

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct VertexAttrDescriptor {
pub size: usize,
pub class: VertexAttrClass,
Expand All @@ -431,7 +467,49 @@ pub struct VertexAttrDescriptor {
pub buffer_index: u32,
}

#[derive(Clone, Copy, Debug, PartialEq)]
impl VertexAttrDescriptor {
pub const fn datatype_only(class: VertexAttrClass, attr_type: VertexAttrType, size: usize) -> Self {
VertexAttrDescriptor {
size,
class,
attr_type,
divisor: 0,
buffer_index: 0,
stride: 0,
offset: 0,
}
}
}

pub struct VertexBufferDescriptor {
pub index: u32,
pub divisor: u32,
pub vertex_attrs: Vec<VertexAttrDescriptor>,
}

impl VertexBufferDescriptor {
pub fn update_attrs(&mut self) {
let mut offset = 0;
for attr in self.vertex_attrs.iter_mut() {
attr.buffer_index = self.index;
attr.divisor = self.divisor;
attr.offset = offset;
offset += attr.size * attr.attr_type.get_size();
}

for attr in self.vertex_attrs.iter_mut() {
attr.stride = offset;
}
}

pub fn configure_vertex_attrs<D: Device>(&self, device: &D, vertex_array: &D::VertexArray, attrs: &[D::VertexAttr]) {
for (attr, descriptor) in attrs.iter().zip(self.vertex_attrs.iter()) {
device.configure_vertex_attr(vertex_array, attr, &descriptor);
}
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VertexAttrClass {
Float,
FloatNorm,
Expand Down
2 changes: 2 additions & 0 deletions metal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ impl Device for MetalDevice {
}
(VertexAttrClass::Int, VertexAttrType::I16, 1) => MTLVertexFormat::Short,
(VertexAttrClass::Int, VertexAttrType::U16, 1) => MTLVertexFormat::UShort,
(VertexAttrClass::Int, VertexAttrType::I32, 1) => MTLVertexFormat::Int,
(VertexAttrClass::Int, VertexAttrType::U32, 1) => MTLVertexFormat::UInt,
(VertexAttrClass::FloatNorm, VertexAttrType::U16, 1) => {
MTLVertexFormat::UShortNormalized
}
Expand Down
1 change: 1 addition & 0 deletions renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ homepage = "https://github.com/servo/pathfinder"
bitflags = "1.0"
byteorder = "1.2"
crossbeam-channel = "0.4"
once_cell = "1.3.1"
fxhash = "0.2"
half = "1.5"
hashbrown = "0.7"
Expand Down
29 changes: 15 additions & 14 deletions renderer/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::z_buffer::{DepthMetadata, ZBuffer};
use pathfinder_content::effects::{BlendMode, Filter};
use pathfinder_content::fill::FillRule;
use pathfinder_content::render_target::RenderTargetId;
use pathfinder_geometry::alignment::{AlignedI16, AlignedU8, AlignedU16, AlignedI8};
use pathfinder_geometry::line_segment::{LineSegment2F, LineSegmentU4, LineSegmentU8};
use pathfinder_geometry::rect::{RectF, RectI};
use pathfinder_geometry::transform2d::Transform2F;
Expand Down Expand Up @@ -648,14 +649,14 @@ impl ObjectBuilder {
self.fills.push(FillBatchEntry {
page: alpha_tile_id.page(),
fill: Fill {
px: LineSegmentU4 { from: px[0] as u8, to: px[2] as u8 },
px: LineSegmentU4 { from: px[0] as AlignedU8, to: px[2] as AlignedU8 },
subpx: LineSegmentU8 {
from_x: from_x as u8,
from_y: from_y as u8,
to_x: to_x as u8,
to_y: to_y as u8,
},
alpha_tile_index: alpha_tile_id.tile(),
alpha_tile_index: alpha_tile_id.tile() as AlignedU16,
},
});
}
Expand Down Expand Up @@ -841,14 +842,14 @@ impl Tile {
}

Tile {
tile_x: tile_origin.x() as i16,
tile_y: tile_origin.y() as i16,
mask_0_u: mask_0_uv.x() as u8,
mask_0_v: mask_0_uv.y() as u8,
mask_0_backdrop: draw_tile_backdrop,
ctrl: ctrl as u16,
tile_x: tile_origin.x() as AlignedI16,
tile_y: tile_origin.y() as AlignedI16,
mask_0_u: mask_0_uv.x() as AlignedU8,
mask_0_v: mask_0_uv.y() as AlignedU8,
mask_0_backdrop: draw_tile_backdrop as AlignedI8,
ctrl: ctrl as AlignedU16,
pad: 0,
color: draw_tiling_path_info.paint_id.0,
color: draw_tiling_path_info.paint_id.0 as AlignedU16,
}
}

Expand All @@ -864,11 +865,11 @@ impl Clip {
let dest_uv = calculate_mask_uv(dest_tile_index);
let src_uv = calculate_mask_uv(src_tile_index);
Clip {
dest_u: dest_uv.x() as u8,
dest_v: dest_uv.y() as u8,
src_u: src_uv.x() as u8,
src_v: src_uv.y() as u8,
backdrop: src_backdrop,
dest_u: dest_uv.x() as AlignedU8,
dest_v: dest_uv.y() as AlignedU8,
src_u: src_uv.x() as AlignedU8,
src_v: src_uv.y() as AlignedU8,
backdrop: src_backdrop as AlignedI8,
pad_0: 0,
pad_1: 0,
}
Expand Down
4 changes: 2 additions & 2 deletions renderer/src/gpu/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use pathfinder_geometry::line_segment::LineSegment2F;
use pathfinder_geometry::rect::RectI;
use pathfinder_geometry::transform3d::Transform4F;
use pathfinder_geometry::util;
use pathfinder_geometry::vector::{Vector2F, Vector2I, Vector4F, vec2f, vec2i};
use pathfinder_geometry::{alignment::AlignedU16, vector::{Vector2F, Vector2I, Vector4F, vec2f, vec2i}};
use pathfinder_gpu::{BlendFactor, BlendOp, BlendState, BufferData, BufferTarget, BufferUploadMode};
use pathfinder_gpu::{ClearOps, ComputeDimensions, ComputeState, DepthFunc, DepthState, Device};
use pathfinder_gpu::{ImageAccess, ImageBinding, Primitive, RenderOptions, RenderState};
Expand All @@ -47,7 +47,7 @@ use std::ops::{Add, Div};
use std::time::Duration;
use std::u32;

static QUAD_VERTEX_POSITIONS: [u16; 8] = [0, 0, 1, 0, 1, 1, 0, 1];
static QUAD_VERTEX_POSITIONS: [AlignedU16; 8] = [0, 0, 1, 0, 1, 1, 0, 1];
static QUAD_VERTEX_INDICES: [u32; 6] = [0, 1, 3, 1, 2, 3];

pub(crate) const MASK_TILES_ACROSS: u32 = 256;
Expand Down
Loading