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

WIP upload instance data in RenderBackend #344

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
88 changes: 75 additions & 13 deletions webrender/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use api::{YuvColorSpace, YuvFormat, ColorDepth, ColorRange, PremultipliedColorF,
use api::units::*;
use crate::clip::{ClipDataStore, ClipNodeFlags, ClipNodeRange, ClipItemKind, ClipStore};
use crate::clip_scroll_tree::{ClipScrollTree, ROOT_SPATIAL_NODE_INDEX, SpatialNodeIndex, CoordinateSystemId};
#[cfg(not(feature = "gl"))]
use crate::device::{InstanceBufferManager, InstanceLocation, PrimitiveType};
use crate::composite::{CompositeState, CompositeTile, CompositeTileSurface};
use crate::glyph_rasterizer::GlyphFormat;
use crate::gpu_cache::{GpuBlockData, GpuCache, GpuCacheHandle, GpuCacheAddress};
Expand All @@ -27,6 +29,8 @@ use crate::render_task::RenderTaskAddress;
use crate::renderer::{BlendMode, ImageBufferKind, ShaderColorMode};
use crate::renderer::{BLOCKS_PER_UV_RECT, MAX_VERTEX_TEXTURE_WIDTH};
use crate::resource_cache::{CacheItem, GlyphFetchResult, ImageRequest, ResourceCache, ImageProperties};
#[cfg(not(feature = "gl"))]
use rendy_memory::Heaps;
use smallvec::SmallVec;
use std::{f32, i32, usize};
use crate::util::{project_rect, TransformedRectKind};
Expand Down Expand Up @@ -337,6 +341,7 @@ impl OpaqueBatchList {
pub struct PrimitiveBatch {
pub key: BatchKey,
pub instances: Vec<PrimitiveInstanceData>,
pub instance_locations: Vec<InstanceLocation>,
pub features: BatchFeatures,
}

Expand All @@ -363,6 +368,7 @@ impl PrimitiveBatch {
PrimitiveBatch {
key,
instances: Vec::new(),
instance_locations: Vec::new(),
features: BatchFeatures::empty(),
}
}
Expand All @@ -371,6 +377,14 @@ impl PrimitiveBatch {
self.instances.extend(other.instances);
self.features |= other.features;
}

fn build<B: hal::Backend>(
&mut self,
ctx: &mut RenderTargetContext<B>,
) {
let instances = self.instances.drain(..).map(|i| i.to_primitive_type()).collect::<Vec<_>>();
self.instance_locations = ctx.upload_primitive_data(&instances);
}
}

#[cfg_attr(feature = "capture", derive(Serialize))]
Expand Down Expand Up @@ -439,6 +453,15 @@ impl AlphaBatchContainer {
min_batch_index = self.alpha_batches.len();
}
}
}
}

pub fn build<B: hal::Backend>(
&mut self,
ctx: &mut RenderTargetContext<B>,
) {
for batch in self.opaque_batches.iter_mut().chain(self.alpha_batches.iter_mut()) {
batch.build(ctx);
}
}
}
Expand Down Expand Up @@ -633,10 +656,10 @@ impl BatchBuilder {
}

/// Add a picture to a given batch builder.
pub fn add_pic_to_batch(
pub fn add_pic_to_batch<B: hal::Backend>(
&mut self,
pic: &PicturePrimitive,
ctx: &RenderTargetContext,
ctx: &RenderTargetContext<B>,
gpu_cache: &mut GpuCache,
render_tasks: &RenderTaskGraph,
deferred_resolves: &mut Vec<DeferredResolve>,
Expand Down Expand Up @@ -672,11 +695,11 @@ impl BatchBuilder {
// It can recursively call itself in some situations, for
// example if it encounters a picture where the items
// in that picture are being drawn into the same target.
fn add_prim_to_batch(
fn add_prim_to_batch<B: hal::Backend>(
&mut self,
prim_instance: &PrimitiveInstance,
prim_spatial_node_index: SpatialNodeIndex,
ctx: &RenderTargetContext,
ctx: &RenderTargetContext<B>,
gpu_cache: &mut GpuCache,
render_tasks: &RenderTaskGraph,
deferred_resolves: &mut Vec<DeferredResolve>,
Expand Down Expand Up @@ -2393,7 +2416,7 @@ impl BatchBuilder {
}

/// Add a single segment instance to a batch.
fn add_segment_to_batch(
fn add_segment_to_batch<B: hal::Backend>(
&mut self,
segment: &BrushSegment,
segment_data: &SegmentInstanceData,
Expand All @@ -2409,7 +2432,7 @@ impl BatchBuilder {
prim_opacity: PrimitiveOpacity,
clip_task_index: ClipTaskIndex,
prim_vis_mask: PrimitiveVisibilityMask,
ctx: &RenderTargetContext,
ctx: &RenderTargetContext<B>,
) {
debug_assert!(clip_task_index != ClipTaskIndex::INVALID);

Expand Down Expand Up @@ -2452,7 +2475,7 @@ impl BatchBuilder {
}

/// Add any segment(s) from a brush to batches.
fn add_segmented_prim_to_batch(
fn add_segmented_prim_to_batch<B: hal::Backend>(
&mut self,
brush_segments: Option<&[BrushSegment]>,
prim_opacity: PrimitiveOpacity,
Expand All @@ -2467,7 +2490,7 @@ impl BatchBuilder {
z_id: ZBufferId,
clip_task_index: ClipTaskIndex,
prim_vis_mask: PrimitiveVisibilityMask,
ctx: &RenderTargetContext,
ctx: &RenderTargetContext<B>,
) {
match (brush_segments, &params.segment_data) {
(Some(ref brush_segments), SegmentDataKind::Instanced(ref segment_data)) => {
Expand Down Expand Up @@ -2793,28 +2816,58 @@ pub fn resolve_image(
pub struct ClipBatchList {
/// Rectangle draws fill up the rectangles with rounded corners.
pub slow_rectangles: Vec<ClipMaskInstance>,
pub slow_rectangle_locations: Vec<InstanceLocation>,
pub fast_rectangles: Vec<ClipMaskInstance>,
pub fast_rectangle_locations: Vec<InstanceLocation>,
/// Image draws apply the image masking.
pub images: FastHashMap<TextureSource, Vec<ClipMaskInstance>>,
pub image_locations: FastHashMap<TextureSource, Vec<InstanceLocation>>,
pub box_shadows: FastHashMap<TextureSource, Vec<ClipMaskInstance>>,
pub box_shadow_locations: FastHashMap<TextureSource, Vec<InstanceLocation>>,
}

impl ClipBatchList {
fn new() -> Self {
ClipBatchList {
slow_rectangles: Vec::new(),
slow_rectangle_locations: Vec::new(),
fast_rectangles: Vec::new(),
fast_rectangle_locations: Vec::new(),
images: FastHashMap::default(),
image_locations: FastHashMap::default(),
box_shadows: FastHashMap::default(),
box_shadow_locations: FastHashMap::default(),
}
}

#[cfg(not(feature = "gl"))]
fn build<B: hal::Backend>(
&mut self,
ctx: &mut RenderTargetContext<B>,
) {
let slow_rects = self.slow_rectangles.drain(..).map(|i| i.to_primitive_type()).collect::<Vec<_>>();
self.slow_rectangle_locations = ctx.upload_primitive_data(&slow_rects);

let fast_rects = self.fast_rectangles.drain(..).map(|i| i.to_primitive_type()).collect::<Vec<_>>();
self.fast_rectangle_locations = ctx.upload_primitive_data(&fast_rects);

for (source, instances) in self.images.drain() {
let locations = ctx.upload_primitive_data(&instances.iter().map(|i| i.to_primitive_type()).collect::<Vec<_>>());
self.image_locations.insert(source, locations);
}

for (source, instances) in self.box_shadows.drain() {
let locations = ctx.upload_primitive_data(&instances.iter().map(|i| i.to_primitive_type()).collect::<Vec<_>>());
self.box_shadow_locations.insert(source, locations);
}
}

#[cfg(not(feature = "gl"))]
pub fn is_empty(&self) -> bool {
self.slow_rectangles.is_empty()
&& self.fast_rectangles.is_empty()
&& self.images.is_empty()
&& self.box_shadows.is_empty()
self.slow_rectangle_locations.is_empty()
&& self.fast_rectangle_locations.is_empty()
&& self.image_locations.is_empty()
&& self.box_shadow_locations.is_empty()
}
}

Expand Down Expand Up @@ -2850,6 +2903,15 @@ impl ClipBatcher {
self.primary_clips.is_empty() && self.secondary_clips.is_empty()
}

#[cfg(not(feature = "gl"))]
pub fn build<B: hal::Backend>(
&mut self,
ctx: &mut RenderTargetContext<B>,
) {
self.primary_clips.build(ctx);
self.secondary_clips.build(ctx);
}

pub fn add_clip_region(
&mut self,
clip_data_address: GpuCacheAddress,
Expand Down Expand Up @@ -3192,7 +3254,7 @@ fn get_shader_opacity(opacity: f32) -> i32 {
(opacity * 65535.0).round() as i32
}

impl<'a, 'rc> RenderTargetContext<'a, 'rc> {
impl<'a, 'rc, B: hal::Backend> RenderTargetContext<'a, 'rc, B> {
/// Retrieve the GPU task address for a given clip task instance.
/// Returns None if the segment was completely clipped out.
/// Returns Some(OPAQUE_TASK_ADDRESS) if no clip mask is needed.
Expand Down
Loading