Skip to content

Commit e0dd8fb

Browse files
committed
Refactor texture names and support more than two textures in the shaders.
1 parent 2792a21 commit e0dd8fb

15 files changed

+139
-97
lines changed

webrender/res/blur.fs.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void main(void) {
2828
lColorTexCoord.x <= 1.0 &&
2929
lColorTexCoord.y >= 0.0 &&
3030
lColorTexCoord.y <= 1.0 ?
31-
texture(sDiffuse, lColorTexCoord * sourceTextureUvSize + sourceTextureUvOrigin) :
31+
texture(sColor0, lColorTexCoord * sourceTextureUvSize + sourceTextureUvOrigin) :
3232
vec4(0.0);
3333

3434
// Alpha must be premultiplied in order to properly blur the alpha channel.

webrender/res/debug_font.fs.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
void main(void)
66
{
77
#ifdef SERVO_ES2
8-
float alpha = texture(sDiffuse, vColorTexCoord.xy).a;
8+
float alpha = texture(sColor0, vColorTexCoord.xy).a;
99
#else
10-
float alpha = texture(sDiffuse, vColorTexCoord.xy).r;
10+
float alpha = texture(sColor0, vColorTexCoord.xy).r;
1111
#endif
1212
oFragColor = vec4(vColor.xyz, vColor.w * alpha);
1313
}

webrender/res/ps_image.fs.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ void main(void) {
2525
vec2 st = vTextureOffset + ((position_in_tile / vStretchSize) * vTextureSize);
2626
alpha = alpha * float(all(bvec2(step(position_in_tile, vStretchSize))));
2727

28-
oFragColor = vec4(1, 1, 1, alpha) * texture(sDiffuse, st);
28+
oFragColor = vec4(1, 1, 1, alpha) * texture(sColor0, st);
2929
}

webrender/res/ps_image.vs.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void main(void) {
2323
#endif
2424

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

webrender/res/ps_image_clip.fs.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ void main(void) {
2828
vec2 st = vTextureOffset + ((position_in_tile / vStretchSize) * vTextureSize);
2929
alpha = alpha * float(all(bvec2(step(position_in_tile, vStretchSize))));
3030

31-
oFragColor = texture(sDiffuse, st) * vec4(1, 1, 1, alpha);
31+
oFragColor = texture(sColor0, st) * vec4(1, 1, 1, alpha);
3232
}

webrender/res/ps_image_clip.vs.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void main(void) {
2727
write_clip(clip);
2828

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

webrender/res/ps_text_run.fs.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
void main(void) {
66
#ifdef WR_FEATURE_SUBPIXEL_AA
7-
oFragColor = texture(sDiffuse, vUv);
7+
oFragColor = texture(sColor0, vUv);
88
#else
9-
float a = texture(sDiffuse, vUv).a;
9+
float a = texture(sColor0, vUv).a;
1010
#ifdef WR_FEATURE_TRANSFORM
1111
float alpha = 0.0;
1212
init_transform_fs(vLocalPos, vLocalRect, alpha);

webrender/res/ps_text_run.vs.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void main(void) {
2525
vec2 f = (vi.local_clamped_pos - vi.local_rect.p0) / (vi.local_rect.p1 - vi.local_rect.p0);
2626
#endif
2727

28-
vec2 texture_size = vec2(textureSize(sDiffuse, 0));
28+
vec2 texture_size = vec2(textureSize(sColor0, 0));
2929
vec2 st0 = glyph.uv_rect.xy / texture_size;
3030
vec2 st1 = glyph.uv_rect.zw / texture_size;
3131

webrender/res/shared.glsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
//======================================================================================
3434
// Shared shader uniforms
3535
//======================================================================================
36-
uniform sampler2D sDiffuse;
36+
uniform sampler2D sColor0;
37+
uniform sampler2D sColor1;
38+
uniform sampler2D sColor2;
3739
uniform sampler2D sMask;
3840

3941
//======================================================================================

webrender/src/debug_render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl DebugRenderer {
196196

197197
// Glyphs
198198
device.bind_program(self.font_program_id, &projection);
199-
device.bind_texture(TextureSampler::Color, self.font_texture_id);
199+
device.bind_texture(TextureSampler::Color0, self.font_texture_id);
200200
device.bind_vao(self.font_vao);
201201
device.update_vao_indices(self.font_vao,
202202
&self.font_indices,

webrender/src/device.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use fnv::FnvHasher;
77
use gleam::gl;
88
use internal_types::{PackedVertex, PackedVertexForQuad};
99
use internal_types::{PackedVertexForTextureCacheUpdate, RenderTargetMode, TextureSampler};
10-
use internal_types::{VertexAttribute, DebugFontVertex, DebugColorVertex};
10+
use internal_types::{VertexAttribute, DebugFontVertex, DebugColorVertex, DEFAULT_TEXTURE};
1111
//use notify::{self, Watcher};
1212
use std::collections::HashMap;
1313
use std::fs::File;
@@ -341,6 +341,8 @@ impl TextureId {
341341
target: gl::TEXTURE_2D,
342342
}
343343
}
344+
345+
pub fn is_valid(&self) -> bool { *self != TextureId::invalid() }
344346
}
345347

346348
impl ProgramId {
@@ -1105,7 +1107,7 @@ impl Device {
11051107

11061108
match mode {
11071109
RenderTargetMode::SimpleRenderTarget => {
1108-
self.bind_texture(TextureSampler::Color, texture_id);
1110+
self.bind_texture(DEFAULT_TEXTURE, texture_id);
11091111
self.set_texture_parameters(texture_id.target, filter);
11101112
self.upload_texture_image(texture_id.target,
11111113
width,
@@ -1117,12 +1119,12 @@ impl Device {
11171119
self.create_fbo_for_texture_if_necessary(texture_id, None);
11181120
}
11191121
RenderTargetMode::LayerRenderTarget(layer_count) => {
1120-
self.bind_texture(TextureSampler::Color, texture_id);
1122+
self.bind_texture(DEFAULT_TEXTURE, texture_id);
11211123
self.set_texture_parameters(texture_id.target, filter);
11221124
self.create_fbo_for_texture_if_necessary(texture_id, Some(layer_count));
11231125
}
11241126
RenderTargetMode::None => {
1125-
self.bind_texture(TextureSampler::Color, texture_id);
1127+
self.bind_texture(DEFAULT_TEXTURE, texture_id);
11261128
self.set_texture_parameters(texture_id.target, filter);
11271129
self.upload_texture_image(texture_id.target,
11281130
width,
@@ -1216,7 +1218,7 @@ impl Device {
12161218
self.create_fbo_for_texture_if_necessary(temp_texture_id, None);
12171219

12181220
self.bind_render_target(Some((texture_id, 0)), None);
1219-
self.bind_texture(TextureSampler::Color, temp_texture_id);
1221+
self.bind_texture(DEFAULT_TEXTURE, temp_texture_id);
12201222

12211223
gl::copy_tex_sub_image_2d(temp_texture_id.target,
12221224
0,
@@ -1231,7 +1233,7 @@ impl Device {
12311233
self.init_texture(texture_id, new_width, new_height, format, filter, mode, None);
12321234
self.create_fbo_for_texture_if_necessary(texture_id, None);
12331235
self.bind_render_target(Some((temp_texture_id, 0)), None);
1234-
self.bind_texture(TextureSampler::Color, texture_id);
1236+
self.bind_texture(DEFAULT_TEXTURE, texture_id);
12351237

12361238
gl::copy_tex_sub_image_2d(texture_id.target,
12371239
0,
@@ -1249,7 +1251,7 @@ impl Device {
12491251
pub fn deinit_texture(&mut self, texture_id: TextureId) {
12501252
debug_assert!(self.inside_frame);
12511253

1252-
self.bind_texture(TextureSampler::Color, texture_id);
1254+
self.bind_texture(DEFAULT_TEXTURE, texture_id);
12531255

12541256
let texture = self.textures.get_mut(&texture_id).unwrap();
12551257
let (internal_format, gl_format) = gl_texture_formats_for_image_format(texture.format);
@@ -1404,9 +1406,17 @@ impl Device {
14041406
program.u_transform = gl::get_uniform_location(program.id, "uTransform");
14051407

14061408
program_id.bind();
1407-
let u_diffuse = gl::get_uniform_location(program.id, "sDiffuse");
1408-
if u_diffuse != -1 {
1409-
gl::uniform_1i(u_diffuse, TextureSampler::Color as i32);
1409+
let u_color_0 = gl::get_uniform_location(program.id, "sColor0");
1410+
if u_color_0 != -1 {
1411+
gl::uniform_1i(u_color_0, TextureSampler::Color0 as i32);
1412+
}
1413+
let u_color1 = gl::get_uniform_location(program.id, "sColor1");
1414+
if u_color1 != -1 {
1415+
gl::uniform_1i(u_color1, TextureSampler::Color1 as i32);
1416+
}
1417+
let u_color_2 = gl::get_uniform_location(program.id, "sColor2");
1418+
if u_color_2 != -1 {
1419+
gl::uniform_1i(u_color_2, TextureSampler::Color2 as i32);
14101420
}
14111421
let u_mask = gl::get_uniform_location(program.id, "sMask");
14121422
if u_mask != -1 {
@@ -1574,7 +1584,7 @@ impl Device {
15741584
gl::pixel_store_i(gl::UNPACK_ROW_LENGTH, row_length as gl::GLint);
15751585
}
15761586

1577-
self.bind_texture(TextureSampler::Color, texture_id);
1587+
self.bind_texture(DEFAULT_TEXTURE, texture_id);
15781588
self.update_image_for_2d_texture(texture_id.target,
15791589
x0 as gl::GLint,
15801590
y0 as gl::GLint,
@@ -1597,7 +1607,7 @@ impl Device {
15971607
src_y: i32,
15981608
width: i32,
15991609
height: i32) {
1600-
self.bind_texture(TextureSampler::Color, texture_id);
1610+
self.bind_texture(DEFAULT_TEXTURE, texture_id);
16011611
gl::copy_tex_sub_image_2d(texture_id.target,
16021612
0,
16031613
dest_x,

webrender/src/internal_types.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ pub type DrawListId = FreeListItemId;
167167

168168
#[derive(Debug, PartialEq, Eq)]
169169
pub enum TextureSampler {
170-
Color,
170+
Color0,
171+
Color1,
172+
Color2,
171173
Mask,
172174
Cache,
173175
Data16,
@@ -179,6 +181,39 @@ pub enum TextureSampler {
179181
Geometry,
180182
}
181183

184+
impl TextureSampler {
185+
pub fn color(n: usize) -> TextureSampler {
186+
match n {
187+
0 => TextureSampler::Color0,
188+
1 => TextureSampler::Color1,
189+
2 => TextureSampler::Color2,
190+
_ => {
191+
panic!("There are only 3 color samplers.");
192+
}
193+
}
194+
}
195+
}
196+
197+
/// Optional textures that can be used as a source in the shaders.
198+
/// Textures that are not used by the batch are equal to TextureId::invalid().
199+
#[derive(Copy, Clone, Debug)]
200+
pub struct BatchTextures {
201+
pub colors: [TextureId; 3],
202+
pub mask: TextureId,
203+
}
204+
205+
impl BatchTextures {
206+
pub fn no_texture() -> Self {
207+
BatchTextures {
208+
colors: [TextureId::invalid(); 3],
209+
mask: TextureId::invalid(),
210+
}
211+
}
212+
}
213+
214+
// In some places we need to temporarily bind a texture to any slot.
215+
pub const DEFAULT_TEXTURE: TextureSampler = TextureSampler::Color0;
216+
182217
pub enum VertexAttribute {
183218
Position,
184219
PositionRect,

webrender/src/prim_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,8 @@ impl PrimitiveStore {
794794
}
795795
PrimitiveKind::Image => {
796796
let image_cpu = &mut self.cpu_images[metadata.cpu_prim_index.0];
797-
prim_needs_resolve = true;
798797

798+
prim_needs_resolve = true;
799799
match image_cpu.kind {
800800
ImagePrimitiveKind::Image(image_key, image_rendering, tile_spacing) => {
801801
resource_cache.request_image(image_key, image_rendering);

0 commit comments

Comments
 (0)