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] Simplify texture names and support more than two textures in the shaders. #534

Merged
merged 2 commits into from
Nov 10, 2016
Merged
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
2 changes: 1 addition & 1 deletion webrender/res/blur.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void main(void) {
lColorTexCoord.x <= 1.0 &&
lColorTexCoord.y >= 0.0 &&
lColorTexCoord.y <= 1.0 ?
texture(sDiffuse, lColorTexCoord * sourceTextureUvSize + sourceTextureUvOrigin) :
texture(sColor0, lColorTexCoord * sourceTextureUvSize + sourceTextureUvOrigin) :
vec4(0.0);

// Alpha must be premultiplied in order to properly blur the alpha channel.
Expand Down
2 changes: 1 addition & 1 deletion webrender/res/cs_text_run.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

void main(void) {
float a = texture(sDiffuse, vUv).a;
float a = texture(sColor0, vUv).a;
oFragColor = vec4(vColor.rgb, vColor.a * a);
}
2 changes: 1 addition & 1 deletion webrender/res/cs_text_run.vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main(void) {
vec2 origin = task.data0.xy + uDevicePixelRatio * (glyph.offset.xy - pg.local_rect.xy);
vec4 local_rect = vec4(origin, size);

vec2 texture_size = vec2(textureSize(sDiffuse, 0));
vec2 texture_size = vec2(textureSize(sColor0, 0));
vec2 st0 = glyph.uv_rect.xy / texture_size;
vec2 st1 = glyph.uv_rect.zw / texture_size;

Expand Down
4 changes: 2 additions & 2 deletions webrender/res/debug_font.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
void main(void)
{
#ifdef SERVO_ES2
float alpha = texture(sDiffuse, vColorTexCoord.xy).a;
float alpha = texture(sColor0, vColorTexCoord.xy).a;
#else
float alpha = texture(sDiffuse, vColorTexCoord.xy).r;
float alpha = texture(sColor0, vColorTexCoord.xy).r;
#endif
oFragColor = vec4(vColor.xyz, vColor.w * alpha);
}
2 changes: 1 addition & 1 deletion webrender/res/ps_image.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ void main(void) {
vec2 st = vTextureOffset + ((position_in_tile / vStretchSize) * vTextureSize);
alpha = alpha * float(all(bvec2(step(position_in_tile, vStretchSize))));

oFragColor = vec4(1, 1, 1, alpha) * texture(sDiffuse, st);
oFragColor = vec4(1, 1, 1, alpha) * texture(sColor0, st);
}
2 changes: 1 addition & 1 deletion webrender/res/ps_image.vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main(void) {
#endif

// vUv will contain how many times this image has wrapped around the image size.
vec2 texture_size = vec2(textureSize(sDiffuse, 0));
vec2 texture_size = vec2(textureSize(sColor0, 0));
vec2 st0 = image.st_rect.xy / texture_size;
vec2 st1 = image.st_rect.zw / texture_size;

Expand Down
2 changes: 1 addition & 1 deletion webrender/res/ps_image_clip.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ void main(void) {
vec2 st = vTextureOffset + ((position_in_tile / vStretchSize) * vTextureSize);
alpha = alpha * float(all(bvec2(step(position_in_tile, vStretchSize))));

oFragColor = texture(sDiffuse, st) * vec4(1, 1, 1, alpha);
oFragColor = texture(sColor0, st) * vec4(1, 1, 1, alpha);
}
2 changes: 1 addition & 1 deletion webrender/res/ps_image_clip.vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main(void) {
write_clip(clip);

// vUv will contain how many times this image has wrapped around the image size.
vec2 texture_size = vec2(textureSize(sDiffuse, 0));
vec2 texture_size = vec2(textureSize(sColor0, 0));
vec2 st0 = image.st_rect.xy / texture_size;
vec2 st1 = image.st_rect.zw / texture_size;

Expand Down
4 changes: 2 additions & 2 deletions webrender/res/ps_text_run.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

void main(void) {
#ifdef WR_FEATURE_SUBPIXEL_AA
oFragColor = texture(sDiffuse, vUv);
oFragColor = texture(sColor0, vUv);
#else
float a = texture(sDiffuse, vUv).a;
float a = texture(sColor0, vUv).a;
#ifdef WR_FEATURE_TRANSFORM
float alpha = 0.0;
init_transform_fs(vLocalPos, vLocalRect, alpha);
Expand Down
2 changes: 1 addition & 1 deletion webrender/res/ps_text_run.vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main(void) {
vec2 f = (vi.local_clamped_pos - vi.local_rect.p0) / (vi.local_rect.p1 - vi.local_rect.p0);
#endif

vec2 texture_size = vec2(textureSize(sDiffuse, 0));
vec2 texture_size = vec2(textureSize(sColor0, 0));
vec2 st0 = glyph.uv_rect.xy / texture_size;
vec2 st1 = glyph.uv_rect.zw / texture_size;

Expand Down
4 changes: 3 additions & 1 deletion webrender/res/shared.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
//======================================================================================
// Shared shader uniforms
//======================================================================================
uniform sampler2D sDiffuse;
uniform sampler2D sColor0;
uniform sampler2D sColor1;
uniform sampler2D sColor2;
uniform sampler2D sMask;

//======================================================================================
Expand Down
2 changes: 1 addition & 1 deletion webrender/src/debug_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl DebugRenderer {

// Glyphs
device.bind_program(self.font_program_id, &projection);
device.bind_texture(TextureSampler::Color, self.font_texture_id);
device.bind_texture(TextureSampler::Color0, self.font_texture_id);
device.bind_vao(self.font_vao);
device.update_vao_indices(self.font_vao,
&self.font_indices,
Expand Down
34 changes: 22 additions & 12 deletions webrender/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fnv::FnvHasher;
use gleam::gl;
use internal_types::{PackedVertex, PackedVertexForQuad};
use internal_types::{RenderTargetMode, TextureSampler};
use internal_types::{VertexAttribute, DebugFontVertex, DebugColorVertex};
use internal_types::{VertexAttribute, DebugFontVertex, DebugColorVertex, DEFAULT_TEXTURE};
//use notify::{self, Watcher};
use std::collections::HashMap;
use std::fs::File;
Expand Down Expand Up @@ -268,6 +268,8 @@ impl TextureId {
target: gl::TEXTURE_2D,
}
}

pub fn is_valid(&self) -> bool { *self != TextureId::invalid() }
}

impl ProgramId {
Expand Down Expand Up @@ -1032,7 +1034,7 @@ impl Device {

match mode {
RenderTargetMode::SimpleRenderTarget => {
self.bind_texture(TextureSampler::Color, texture_id);
self.bind_texture(DEFAULT_TEXTURE, texture_id);
self.set_texture_parameters(texture_id.target, filter);
self.upload_texture_image(texture_id.target,
width,
Expand All @@ -1044,12 +1046,12 @@ impl Device {
self.create_fbo_for_texture_if_necessary(texture_id, None);
}
RenderTargetMode::LayerRenderTarget(layer_count) => {
self.bind_texture(TextureSampler::Color, texture_id);
self.bind_texture(DEFAULT_TEXTURE, texture_id);
self.set_texture_parameters(texture_id.target, filter);
self.create_fbo_for_texture_if_necessary(texture_id, Some(layer_count));
}
RenderTargetMode::None => {
self.bind_texture(TextureSampler::Color, texture_id);
self.bind_texture(DEFAULT_TEXTURE, texture_id);
self.set_texture_parameters(texture_id.target, filter);
self.upload_texture_image(texture_id.target,
width,
Expand Down Expand Up @@ -1143,7 +1145,7 @@ impl Device {
self.create_fbo_for_texture_if_necessary(temp_texture_id, None);

self.bind_render_target(Some((texture_id, 0)), None);
self.bind_texture(TextureSampler::Color, temp_texture_id);
self.bind_texture(DEFAULT_TEXTURE, temp_texture_id);

gl::copy_tex_sub_image_2d(temp_texture_id.target,
0,
Expand All @@ -1158,7 +1160,7 @@ impl Device {
self.init_texture(texture_id, new_width, new_height, format, filter, mode, None);
self.create_fbo_for_texture_if_necessary(texture_id, None);
self.bind_render_target(Some((temp_texture_id, 0)), None);
self.bind_texture(TextureSampler::Color, texture_id);
self.bind_texture(DEFAULT_TEXTURE, texture_id);

gl::copy_tex_sub_image_2d(texture_id.target,
0,
Expand All @@ -1176,7 +1178,7 @@ impl Device {
pub fn deinit_texture(&mut self, texture_id: TextureId) {
debug_assert!(self.inside_frame);

self.bind_texture(TextureSampler::Color, texture_id);
self.bind_texture(DEFAULT_TEXTURE, texture_id);

let texture = self.textures.get_mut(&texture_id).unwrap();
let (internal_format, gl_format) = gl_texture_formats_for_image_format(texture.format);
Expand Down Expand Up @@ -1331,9 +1333,17 @@ impl Device {
program.u_transform = gl::get_uniform_location(program.id, "uTransform");

program_id.bind();
let u_diffuse = gl::get_uniform_location(program.id, "sDiffuse");
if u_diffuse != -1 {
gl::uniform_1i(u_diffuse, TextureSampler::Color as i32);
let u_color_0 = gl::get_uniform_location(program.id, "sColor0");
if u_color_0 != -1 {
gl::uniform_1i(u_color_0, TextureSampler::Color0 as i32);
}
let u_color1 = gl::get_uniform_location(program.id, "sColor1");
if u_color1 != -1 {
gl::uniform_1i(u_color1, TextureSampler::Color1 as i32);
}
let u_color_2 = gl::get_uniform_location(program.id, "sColor2");
if u_color_2 != -1 {
gl::uniform_1i(u_color_2, TextureSampler::Color2 as i32);
}
let u_mask = gl::get_uniform_location(program.id, "sMask");
if u_mask != -1 {
Expand Down Expand Up @@ -1501,7 +1511,7 @@ impl Device {
gl::pixel_store_i(gl::UNPACK_ROW_LENGTH, row_length as gl::GLint);
}

self.bind_texture(TextureSampler::Color, texture_id);
self.bind_texture(DEFAULT_TEXTURE, texture_id);
self.update_image_for_2d_texture(texture_id.target,
x0 as gl::GLint,
y0 as gl::GLint,
Expand All @@ -1524,7 +1534,7 @@ impl Device {
src_y: i32,
width: i32,
height: i32) {
self.bind_texture(TextureSampler::Color, texture_id);
self.bind_texture(DEFAULT_TEXTURE, texture_id);
gl::copy_tex_sub_image_2d(texture_id.target,
0,
dest_x,
Expand Down
37 changes: 36 additions & 1 deletion webrender/src/internal_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ pub type DrawListId = FreeListItemId;

#[derive(Debug, PartialEq, Eq)]
pub enum TextureSampler {
Color,
Color0,
Color1,
Color2,
Mask,
Cache,
Data16,
Expand All @@ -178,6 +180,39 @@ pub enum TextureSampler {
Geometry,
}

impl TextureSampler {
pub fn color(n: usize) -> TextureSampler {
match n {
0 => TextureSampler::Color0,
1 => TextureSampler::Color1,
2 => TextureSampler::Color2,
_ => {
panic!("There are only 3 color samplers.");
}
}
}
}

/// Optional textures that can be used as a source in the shaders.
/// Textures that are not used by the batch are equal to TextureId::invalid().
#[derive(Copy, Clone, Debug)]
pub struct BatchTextures {
pub colors: [TextureId; 3],
pub mask: TextureId,
}

impl BatchTextures {
pub fn no_texture() -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps just empty() ?

BatchTextures {
colors: [TextureId::invalid(); 3],
mask: TextureId::invalid(),
}
}
}

// In some places we need to temporarily bind a texture to any slot.
pub const DEFAULT_TEXTURE: TextureSampler = TextureSampler::Color0;

pub enum VertexAttribute {
Position,
PositionRect,
Expand Down
2 changes: 1 addition & 1 deletion webrender/src/prim_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,8 @@ impl PrimitiveStore {
}
PrimitiveKind::Image => {
let image_cpu = &mut self.cpu_images[metadata.cpu_prim_index.0];
prim_needs_resolve = true;

prim_needs_resolve = true;
match image_cpu.kind {
ImagePrimitiveKind::Image(image_key, image_rendering, tile_spacing) => {
resource_cache.request_image(image_key, image_rendering);
Expand Down
Loading