Skip to content

Commit 3fe9214

Browse files
committed
wip
1 parent e8c92e8 commit 3fe9214

File tree

12 files changed

+243
-133
lines changed

12 files changed

+243
-133
lines changed

webrender/res/prim_shared.glsl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ uniform sampler2D sData16;
5555
uniform sampler2D sData32;
5656
uniform sampler2D sData64;
5757
uniform sampler2D sData128;
58+
uniform sampler2D sResourceRects;
5859

5960
ivec2 get_fetch_uv(int index, int vecs_per_item) {
6061
int items_per_row = WR_MAX_VERTEX_TEXTURE_WIDTH / vecs_per_item;
@@ -186,16 +187,14 @@ GradientStop fetch_gradient_stop(int index) {
186187

187188
struct Glyph {
188189
vec4 offset;
189-
vec4 uv_rect;
190190
};
191191

192192
Glyph fetch_glyph(int index) {
193193
Glyph glyph;
194194

195-
ivec2 uv = get_fetch_uv_2(index);
195+
ivec2 uv = get_fetch_uv_1(index);
196196

197-
glyph.offset = texelFetchOffset(sData32, uv, 0, ivec2(0, 0));
198-
glyph.uv_rect = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
197+
glyph.offset = texelFetchOffset(sData16, uv, 0, ivec2(0, 0));
199198

200199
return glyph;
201200
}
@@ -584,6 +583,20 @@ TransformVertexInfo write_transform_vertex(vec4 instance_rect,
584583

585584
#endif //WR_FEATURE_TRANSFORM
586585

586+
struct ResourceRect {
587+
vec4 uv_rect;
588+
};
589+
590+
ResourceRect fetch_resource_rect(int index) {
591+
ResourceRect rect;
592+
593+
ivec2 uv = get_fetch_uv_1(index);
594+
595+
rect.uv_rect = texelFetchOffset(sResourceRects, uv, 0, ivec2(0, 0));
596+
597+
return rect;
598+
}
599+
587600
struct Rectangle {
588601
vec4 color;
589602
};
@@ -613,18 +626,16 @@ TextRun fetch_text_run(int index) {
613626
}
614627

615628
struct Image {
616-
vec4 st_rect; // Location of the image texture in the texture atlas.
617629
vec4 stretch_size_and_tile_spacing; // Size of the actual image and amount of space between
618630
// tiled instances of this image.
619631
};
620632

621633
Image fetch_image(int index) {
622634
Image image;
623635

624-
ivec2 uv = get_fetch_uv_2(index);
636+
ivec2 uv = get_fetch_uv_1(index);
625637

626-
image.st_rect = texelFetchOffset(sData32, uv, 0, ivec2(0, 0));
627-
image.stretch_size_and_tile_spacing = texelFetchOffset(sData32, uv, 0, ivec2(1, 0));
638+
image.stretch_size_and_tile_spacing = texelFetchOffset(sData16, uv, 0, ivec2(0, 0));
628639

629640
return image;
630641
}

webrender/res/ps_image.vs.glsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
void main(void) {
77
Primitive prim = load_primitive(gl_InstanceID);
88
Image image = fetch_image(prim.prim_index);
9+
ResourceRect res = fetch_resource_rect(prim.user_data.x);
910

1011
#ifdef WR_FEATURE_TRANSFORM
1112
TransformVertexInfo vi = write_transform_vertex(prim.local_rect,
@@ -24,8 +25,8 @@ void main(void) {
2425

2526
// vUv will contain how many times this image has wrapped around the image size.
2627
vec2 texture_size = vec2(textureSize(sColor0, 0));
27-
vec2 st0 = image.st_rect.xy / texture_size;
28-
vec2 st1 = image.st_rect.zw / texture_size;
28+
vec2 st0 = res.uv_rect.xy / texture_size;
29+
vec2 st1 = res.uv_rect.zw / texture_size;
2930

3031
vTextureSize = st1 - st0;
3132
vTextureOffset = st0;

webrender/res/ps_text_run.vs.glsl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ void main(void) {
77
Primitive prim = load_primitive(gl_InstanceID);
88
TextRun text = fetch_text_run(prim.prim_index);
99
Glyph glyph = fetch_glyph(prim.sub_index);
10-
vec4 local_rect = vec4(glyph.offset.xy, (glyph.uv_rect.zw - glyph.uv_rect.xy) / uDevicePixelRatio);
10+
ResourceRect res = fetch_resource_rect(prim.user_data.x);
11+
12+
vec4 local_rect = vec4(glyph.offset.xy, (res.uv_rect.zw - res.uv_rect.xy) / uDevicePixelRatio);
1113

1214
#ifdef WR_FEATURE_TRANSFORM
1315
TransformVertexInfo vi = write_transform_vertex(local_rect,
@@ -26,8 +28,8 @@ void main(void) {
2628
#endif
2729

2830
vec2 texture_size = vec2(textureSize(sColor0, 0));
29-
vec2 st0 = glyph.uv_rect.xy / texture_size;
30-
vec2 st1 = glyph.uv_rect.zw / texture_size;
31+
vec2 st0 = res.uv_rect.xy / texture_size;
32+
vec2 st1 = res.uv_rect.zw / texture_size;
3133

3234
vColor = text.color;
3335
vUv = mix(st0, st1, f);

webrender/src/device.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,11 @@ impl Device {
13661366
if u_data128 != -1 {
13671367
gl::uniform_1i(u_data128, TextureSampler::Data128 as i32);
13681368
}
1369+
1370+
let u_resource_rects = gl::get_uniform_location(program.id, "sResourceRects");
1371+
if u_resource_rects != -1 {
1372+
gl::uniform_1i(u_resource_rects, TextureSampler::ResourceRects as i32);
1373+
}
13691374
}
13701375
}
13711376
}

webrender/src/internal_types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub enum SourceTexture {
5555
WebGL(u32), // Is actually a gl::GLuint
5656

5757
// TODO(gw): Implement external image support via callback
58-
//External(i32),
58+
External(u64),
5959
}
6060

6161
pub enum GLContextHandleWrapper {
@@ -213,6 +213,7 @@ pub enum TextureSampler {
213213
Layers,
214214
RenderTasks,
215215
Geometry,
216+
ResourceRects,
216217
}
217218

218219
impl TextureSampler {

webrender/src/prim_store.rs

Lines changed: 86 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ use webrender_traits::{AuxiliaryLists, ColorF, ImageKey, ImageRendering};
1616
use webrender_traits::{FontRenderMode, WebGLContextId};
1717
use webrender_traits::{ClipRegion, FontKey, ItemRange, ComplexClipRegion, GlyphKey};
1818

19+
#[derive(Clone)]
20+
pub struct TexelRect {
21+
uv0: Point2D<f32>,
22+
uv1: Point2D<f32>,
23+
}
24+
25+
pub struct DeferredResolve {
26+
resource_address: GpuStoreAddress,
27+
source_texture: SourceTexture,
28+
}
29+
30+
impl Default for TexelRect {
31+
fn default() -> TexelRect {
32+
TexelRect {
33+
uv0: Point2D::zero(),
34+
uv1: Point2D::zero(),
35+
}
36+
}
37+
}
38+
1939
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
2040
pub struct SpecificPrimitiveIndex(pub usize);
2141

@@ -96,12 +116,11 @@ pub enum ImagePrimitiveKind {
96116
pub struct ImagePrimitiveCpu {
97117
pub kind: ImagePrimitiveKind,
98118
pub color_texture_id: SourceTexture,
119+
pub resource_address: GpuStoreAddress,
99120
}
100121

101122
#[derive(Debug, Clone)]
102123
pub struct ImagePrimitiveGpu {
103-
pub uv0: Point2D<f32>,
104-
pub uv1: Point2D<f32>,
105124
pub stretch_size: Size2D<f32>,
106125
pub tile_spacing: Size2D<f32>,
107126
}
@@ -191,14 +210,13 @@ pub struct TextRunPrimitiveCpu {
191210
pub color_texture_id: SourceTexture,
192211
pub color: ColorF,
193212
pub render_mode: FontRenderMode,
213+
pub resource_address: GpuStoreAddress,
194214
}
195215

196216
#[derive(Debug, Clone)]
197217
struct GlyphPrimitive {
198218
offset: Point2D<f32>,
199219
padding: Point2D<f32>,
200-
uv0: Point2D<f32>,
201-
uv1: Point2D<f32>,
202220
}
203221

204222
#[derive(Debug, Clone)]
@@ -353,6 +371,9 @@ pub struct PrimitiveStore {
353371
pub gpu_data64: GpuStore<GpuBlock64>,
354372
pub gpu_data128: GpuStore<GpuBlock128>,
355373

374+
// Resolved resource rects.
375+
pub gpu_resource_rects: GpuStore<TexelRect>,
376+
356377
// General
357378
device_pixel_ratio: f32,
358379
prims_to_resolve: Vec<PrimitiveIndex>,
@@ -372,6 +393,7 @@ impl PrimitiveStore {
372393
gpu_data32: GpuStore::new(),
373394
gpu_data64: GpuStore::new(),
374395
gpu_data128: GpuStore::new(),
396+
gpu_resource_rects: GpuStore::new(),
375397
device_pixel_ratio: device_pixel_ratio,
376398
prims_to_resolve: Vec::new(),
377399
}
@@ -425,9 +447,10 @@ impl PrimitiveStore {
425447

426448
metadata
427449
}
428-
PrimitiveContainer::TextRun(text_cpu, text_gpu) => {
450+
PrimitiveContainer::TextRun(mut text_cpu, text_gpu) => {
429451
let gpu_address = self.gpu_data16.push(text_gpu);
430-
let gpu_glyphs_address = self.gpu_data32.alloc(text_cpu.glyph_range.length);
452+
let gpu_glyphs_address = self.gpu_data16.alloc(text_cpu.glyph_range.length);
453+
text_cpu.resource_address = self.gpu_resource_rects.alloc(text_cpu.glyph_range.length);
431454

432455
let metadata = PrimitiveMetadata {
433456
is_opaque: false,
@@ -445,8 +468,10 @@ impl PrimitiveStore {
445468
self.cpu_text_runs.push(text_cpu);
446469
metadata
447470
}
448-
PrimitiveContainer::Image(image_cpu, image_gpu) => {
449-
let gpu_address = self.gpu_data32.push(image_gpu);
471+
PrimitiveContainer::Image(mut image_cpu, image_gpu) => {
472+
image_cpu.resource_address = self.gpu_resource_rects.alloc(1);
473+
474+
let gpu_address = self.gpu_data16.push(image_gpu);
450475

451476
let metadata = PrimitiveMetadata {
452477
is_opaque: false,
@@ -564,12 +589,14 @@ impl PrimitiveStore {
564589
PrimitiveIndex(prim_index)
565590
}
566591

567-
pub fn resolve_primitives(&mut self, resource_cache: &ResourceCache) {
592+
pub fn resolve_primitives(&mut self, resource_cache: &ResourceCache) -> Vec<DeferredResolve> {
593+
let mut deferred_resolves = Vec::new();
594+
568595
for prim_index in self.prims_to_resolve.drain(..) {
569596
let metadata = &mut self.cpu_metadata[prim_index.0];
570597

571598
if let &PrimitiveClipSource::Region(ClipRegion { image_mask: Some(mask), .. }) = metadata.clip_source.as_ref() {
572-
let tex_cache = resource_cache.get_image(mask.image, ImageRendering::Auto);
599+
let tex_cache = resource_cache.get_image(mask.image, ImageRendering::Auto).expect("masks shouldn't be external!");
573600
metadata.mask_texture_id = tex_cache.texture_id;
574601
if let Some(address) = metadata.clip_index {
575602
let clip_data = self.gpu_data32.get_slice_mut(address, 6);
@@ -590,44 +617,56 @@ impl PrimitiveStore {
590617
PrimitiveKind::TextRun => {
591618
let text = &mut self.cpu_text_runs[metadata.cpu_prim_index.0];
592619

593-
let dest_glyphs = self.gpu_data32.get_slice_mut(metadata.gpu_data_address,
594-
text.glyph_range.length);
620+
let dest_rects = self.gpu_resource_rects.get_slice_mut(text.resource_address,
621+
text.glyph_range.length);
595622

596623
let texture_id = resource_cache.get_glyphs(text.font_key,
597624
text.font_size,
598625
&text.glyph_indices,
599626
text.render_mode, |index, uv0, uv1| {
600-
let dest_glyph = &mut dest_glyphs[index];
601-
let dest: &mut GlyphPrimitive = unsafe {
602-
mem::transmute(dest_glyph)
603-
};
604-
dest.uv0 = uv0;
605-
dest.uv1 = uv1;
627+
let dest_rect = &mut dest_rects[index];
628+
dest_rect.uv0 = uv0;
629+
dest_rect.uv1 = uv1;
606630
});
607631

608632
text.color_texture_id = texture_id;
609633
}
610634
PrimitiveKind::Image => {
611635
let image_cpu = &mut self.cpu_images[metadata.cpu_prim_index.0];
612-
let image_gpu: &mut ImagePrimitiveGpu = unsafe {
613-
mem::transmute(self.gpu_data32.get_mut(metadata.gpu_prim_index))
614-
};
615636

616-
let cache_item = match image_cpu.kind {
637+
match image_cpu.kind {
617638
ImagePrimitiveKind::Image(image_key, image_rendering, _) => {
618-
resource_cache.get_image(image_key, image_rendering)
639+
match resource_cache.get_image(image_key, image_rendering) {
640+
Ok(cache_item) => {
641+
let resource_rect = self.gpu_resource_rects.get_mut(image_cpu.resource_address);
642+
resource_rect.uv0 = cache_item.uv0;
643+
resource_rect.uv1 = cache_item.uv1;
644+
image_cpu.color_texture_id = cache_item.texture_id;
645+
}
646+
Err(source_texture) => {
647+
// This is an external texture - we will add it to
648+
// the deferred resolves list to be patched by
649+
// the render thread...
650+
deferred_resolves.push(DeferredResolve {
651+
resource_address: image_cpu.resource_address,
652+
source_texture: source_texture,
653+
});
654+
}
655+
}
619656
}
620657
ImagePrimitiveKind::WebGL(context_id) => {
621-
resource_cache.get_webgl_texture(&context_id)
658+
let cache_item = resource_cache.get_webgl_texture(&context_id);
659+
let resource_rect = self.gpu_resource_rects.get_mut(image_cpu.resource_address);
660+
resource_rect.uv0 = cache_item.uv0;
661+
resource_rect.uv1 = cache_item.uv1;
662+
image_cpu.color_texture_id = cache_item.texture_id;
622663
}
623-
};
624-
625-
image_cpu.color_texture_id = cache_item.texture_id;
626-
image_gpu.uv0 = cache_item.uv0;
627-
image_gpu.uv1 = cache_item.uv1;
664+
}
628665
}
629666
}
630667
}
668+
669+
deferred_resolves
631670
}
632671

633672
pub fn get_bounding_rect(&self, index: PrimitiveIndex) -> &Option<DeviceRect> {
@@ -747,7 +786,7 @@ impl PrimitiveStore {
747786
debug_assert!(metadata.gpu_data_count == text.glyph_range.length as i32);
748787
debug_assert!(text.glyph_indices.is_empty());
749788
let src_glyphs = auxiliary_lists.glyph_instances(&text.glyph_range);
750-
let dest_glyphs = self.gpu_data32.get_slice_mut(metadata.gpu_data_address,
789+
let dest_glyphs = self.gpu_data16.get_slice_mut(metadata.gpu_data_address,
751790
text.glyph_range.length);
752791
let mut glyph_key = GlyphKey::new(text.font_key,
753792
text.font_size,
@@ -776,9 +815,7 @@ impl PrimitiveStore {
776815
Size2D::new(width, height));
777816
local_rect = local_rect.union(&local_glyph_rect);
778817

779-
dest_glyphs[actual_glyph_count] = GpuBlock32::from(GlyphPrimitive {
780-
uv0: Point2D::zero(),
781-
uv1: Point2D::zero(),
818+
dest_glyphs[actual_glyph_count] = GpuBlock16::from(GlyphPrimitive {
782819
padding: Point2D::zero(),
783820
offset: local_glyph_rect.origin,
784821
});
@@ -918,6 +955,22 @@ impl From<InstanceRect> for GpuBlock16 {
918955
}
919956
}
920957

958+
impl From<ImagePrimitiveGpu> for GpuBlock16 {
959+
fn from(data: ImagePrimitiveGpu) -> GpuBlock16 {
960+
unsafe {
961+
mem::transmute::<ImagePrimitiveGpu, GpuBlock16>(data)
962+
}
963+
}
964+
}
965+
966+
impl From<GlyphPrimitive> for GpuBlock16 {
967+
fn from(data: GlyphPrimitive) -> GpuBlock16 {
968+
unsafe {
969+
mem::transmute::<GlyphPrimitive, GpuBlock16>(data)
970+
}
971+
}
972+
}
973+
921974
#[derive(Clone)]
922975
pub struct GpuBlock32 {
923976
data: [f32; 8],
@@ -947,22 +1000,6 @@ impl From<GradientStop> for GpuBlock32 {
9471000
}
9481001
}
9491002

950-
impl From<GlyphPrimitive> for GpuBlock32 {
951-
fn from(data: GlyphPrimitive) -> GpuBlock32 {
952-
unsafe {
953-
mem::transmute::<GlyphPrimitive, GpuBlock32>(data)
954-
}
955-
}
956-
}
957-
958-
impl From<ImagePrimitiveGpu> for GpuBlock32 {
959-
fn from(data: ImagePrimitiveGpu) -> GpuBlock32 {
960-
unsafe {
961-
mem::transmute::<ImagePrimitiveGpu, GpuBlock32>(data)
962-
}
963-
}
964-
}
965-
9661003
impl From<ClipRect> for GpuBlock32 {
9671004
fn from(data: ClipRect) -> GpuBlock32 {
9681005
unsafe {

0 commit comments

Comments
 (0)