Skip to content

Commit

Permalink
WR - create a new ImageRect shader for external image TextureRectHandle.
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryShih committed Mar 21, 2017
1 parent 833395f commit d0ffcd4
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 2 deletions.
35 changes: 35 additions & 0 deletions webrender/res/ps_image_rect.fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#line 1

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

void main(void) {
#ifdef WR_FEATURE_TRANSFORM
float alpha = 0.0;
vec2 pos = init_transform_fs(vLocalPos, vLocalRect, alpha);

// We clamp the texture coordinate calculation here to the local rectangle boundaries,
// which makes the edge of the texture stretch instead of repeat.
vec2 relative_pos_in_rect =
clamp(pos, vLocalRect.xy, vLocalRect.xy + vLocalRect.zw) - vLocalRect.xy;
#else
float alpha = 1.0;
vec2 relative_pos_in_rect = vLocalPos;
#endif

alpha = min(alpha, do_clip());

// We calculate the particular tile this fragment belongs to, taking into
// account the spacing in between tiles. We only paint if our fragment does
// not fall into that spacing.
vec2 position_in_tile = mod(relative_pos_in_rect, vStretchSize + vTileSpacing);
// We clamp the texture coordinates to the half-pixel offset from the borders
// in order to avoid sampling outside of the texture area.
vec2 st = vTextureOffset + ((position_in_tile / vStretchSize) * vTextureSize);
st = clamp(st, vStRect.xy, vStRect.zw);

alpha = alpha * float(all(bvec2(step(position_in_tile, vStretchSize))));

oFragColor = vec4(alpha) * texture(sColor0, st);
}
17 changes: 17 additions & 0 deletions webrender/res/ps_image_rect.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

flat varying vec2 vTextureOffset; // Offset of this image into the texture atlas.
flat varying vec2 vTextureSize; // Size of the image in the texture atlas.
flat varying vec2 vTileSpacing; // Amount of space between tiled instances of this image.
flat varying vec4 vStRect; // Rectangle of valid texture rect, in st-space.

#ifdef WR_FEATURE_TRANSFORM
varying vec3 vLocalPos;
flat varying vec4 vLocalRect;
flat varying vec2 vStretchSize;
#else
varying vec2 vLocalPos;
flat varying vec2 vStretchSize;
#endif
39 changes: 39 additions & 0 deletions webrender/res/ps_image_rect.vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#line 1
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

void main(void) {
Primitive prim = load_primitive();
Image image = fetch_image(prim.prim_index);
ResourceRect res = fetch_resource_rect(prim.user_data.x);

#ifdef WR_FEATURE_TRANSFORM
TransformVertexInfo vi = write_transform_vertex(prim.local_rect,
prim.local_clip_rect,
prim.z,
prim.layer,
prim.task);
vLocalRect = vi.clipped_local_rect;
vLocalPos = vi.local_pos;
#else
VertexInfo vi = write_vertex(prim.local_rect,
prim.local_clip_rect,
prim.z,
prim.layer,
prim.task);
vLocalPos = vi.local_pos - vi.local_rect.p0;
#endif

write_clip(vi.screen_pos, prim.clip_area);

vec2 st0 = res.uv_rect.xy;
vec2 st1 = res.uv_rect.zw;

vTextureSize = st1 - st0;
vTextureOffset = st0;
vTileSpacing = image.stretch_size_and_tile_spacing.zw;
vStretchSize = image.stretch_size_and_tile_spacing.xy;

vStRect = vec4(min(st0, st1), max(st0, st1));
}
6 changes: 6 additions & 0 deletions webrender/res/shared.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@
//======================================================================================
// Shared shader uniforms
//======================================================================================
#ifndef WR_FEATURE_TEXTURE_RECT
uniform sampler2D sColor0;
uniform sampler2D sColor1;
uniform sampler2D sColor2;
#else
uniform sampler2DRect sColor0;
uniform sampler2DRect sColor1;
uniform sampler2DRect sColor2;
#endif
uniform sampler2D sMask;

//======================================================================================
Expand Down
15 changes: 15 additions & 0 deletions webrender/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const GPU_TAG_INIT: GpuProfileTag = GpuProfileTag { label: "Init", color: debug_
const GPU_TAG_SETUP_TARGET: GpuProfileTag = GpuProfileTag { label: "Target", color: debug_colors::SLATEGREY };
const GPU_TAG_PRIM_RECT: GpuProfileTag = GpuProfileTag { label: "Rect", color: debug_colors::RED };
const GPU_TAG_PRIM_IMAGE: GpuProfileTag = GpuProfileTag { label: "Image", color: debug_colors::GREEN };
const GPU_TAG_PRIM_IMAGE_RECT: GpuProfileTag = GpuProfileTag { label: "ImageRect", color: debug_colors::GREENYELLOW };
const GPU_TAG_PRIM_YUV_IMAGE: GpuProfileTag = GpuProfileTag { label: "YuvImage", color: debug_colors::DARKGREEN };
const GPU_TAG_PRIM_BLEND: GpuProfileTag = GpuProfileTag { label: "Blend", color: debug_colors::LIGHTBLUE };
const GPU_TAG_PRIM_HW_COMPOSITE: GpuProfileTag = GpuProfileTag { label: "HwComposite", color: debug_colors::DODGERBLUE };
Expand Down Expand Up @@ -215,6 +216,7 @@ pub type GradientDataStore = GpuStore<GradientData, GradientDataTextureLayout>;
const TRANSFORM_FEATURE: &'static str = "TRANSFORM";
const SUBPIXEL_AA_FEATURE: &'static str = "SUBPIXEL_AA";
const CLIP_FEATURE: &'static str = "CLIP";
const TEXTURE_RECT_FEATURE: &'static str = "TEXTURE_RECT";

enum ShaderKind {
Primitive,
Expand Down Expand Up @@ -457,6 +459,7 @@ pub struct Renderer {
ps_text_run: PrimitiveShader,
ps_text_run_subpixel: PrimitiveShader,
ps_image: PrimitiveShader,
ps_image_rect: PrimitiveShader,
ps_yuv_image: PrimitiveShader,
ps_border: PrimitiveShader,
ps_gradient: PrimitiveShader,
Expand Down Expand Up @@ -659,6 +662,13 @@ impl Renderer {
options.precache_shaders)
};

let ps_image_rect = try!{
PrimitiveShader::new("ps_image_rect",
&mut device,
&[ TEXTURE_RECT_FEATURE ],
options.precache_shaders)
};

let ps_yuv_image = try!{
PrimitiveShader::new("ps_yuv_image",
&mut device,
Expand Down Expand Up @@ -880,6 +890,7 @@ impl Renderer {
ps_text_run: ps_text_run,
ps_text_run_subpixel: ps_text_run_subpixel,
ps_image: ps_image,
ps_image_rect: ps_image_rect,
ps_yuv_image: ps_yuv_image,
ps_border: ps_border,
ps_box_shadow: ps_box_shadow,
Expand Down Expand Up @@ -1309,6 +1320,10 @@ impl Renderer {
let shader = self.ps_image.get(&mut self.device, transform_kind);
(GPU_TAG_PRIM_IMAGE, shader)
}
AlphaBatchKind::ImageRect => {
let shader = self.ps_image_rect.get(&mut self.device, transform_kind);
(GPU_TAG_PRIM_IMAGE_RECT, shader)
}
AlphaBatchKind::YuvImage => {
let shader = self.ps_yuv_image.get(&mut self.device, transform_kind);
(GPU_TAG_PRIM_YUV_IMAGE, shader)
Expand Down
25 changes: 23 additions & 2 deletions webrender/src/tiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use webrender_traits::{AuxiliaryLists, ColorF, DeviceIntPoint, DeviceIntRect, De
use webrender_traits::{DeviceUintSize, FontRenderMode, ImageRendering, LayerPoint, LayerRect};
use webrender_traits::{LayerToWorldTransform, MixBlendMode, PipelineId, ScrollLayerId};
use webrender_traits::{WorldPoint4D, WorldToLayerTransform};
use webrender_traits::{ExternalImageType};

// Special sentinel value recognized by the shader. It is considered to be
// a dummy task that doesn't mask out anything.
Expand Down Expand Up @@ -68,7 +69,24 @@ impl AlphaBatchHelpers for PrimitiveStore {
let batch_kind = match metadata.prim_kind {
PrimitiveKind::Border => AlphaBatchKind::Border,
PrimitiveKind::BoxShadow => AlphaBatchKind::BoxShadow,
PrimitiveKind::Image => AlphaBatchKind::Image,
PrimitiveKind::Image => {
let image_cpu = &self.cpu_images[metadata.cpu_prim_index.0];

match image_cpu.color_texture_id {
SourceTexture::External(ext_image) => {
match ext_image.image_type {
ExternalImageType::Texture2DHandle => AlphaBatchKind::Image,
ExternalImageType::TextureRectHandle => AlphaBatchKind::ImageRect,
_ => {
panic!("Non-texture handle type should be handled in other way.");
}
}
}
_ => {
AlphaBatchKind::Image
}
}
}
PrimitiveKind::YuvImage => AlphaBatchKind::YuvImage,
PrimitiveKind::Rectangle => AlphaBatchKind::Rectangle,
PrimitiveKind::AlignedGradient => AlphaBatchKind::AlignedGradient,
Expand Down Expand Up @@ -269,7 +287,8 @@ impl AlphaBatchHelpers for PrimitiveStore {
});
}
}
AlphaBatchKind::Image => {
AlphaBatchKind::Image |
AlphaBatchKind::ImageRect => {
let image_cpu = &self.cpu_images[metadata.cpu_prim_index.0];

data.push(PrimitiveInstance {
Expand Down Expand Up @@ -1129,6 +1148,7 @@ pub enum AlphaBatchKind {
Rectangle,
TextRun,
Image,
ImageRect,
YuvImage,
Border,
AlignedGradient,
Expand Down Expand Up @@ -1259,6 +1279,7 @@ impl PrimitiveBatch {
AlphaBatchKind::Rectangle |
AlphaBatchKind::TextRun |
AlphaBatchKind::Image |
AlphaBatchKind::ImageRect |
AlphaBatchKind::YuvImage |
AlphaBatchKind::Border |
AlphaBatchKind::AlignedGradient |
Expand Down

0 comments on commit d0ffcd4

Please sign in to comment.