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

added ordered dithering to gradient-like effects #966

Merged
merged 3 commits into from Mar 24, 2017
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

Fixup commit for gradient banding work

  • Loading branch information
squarewave committed Mar 23, 2017
commit 6fa2881340f59685ae09b10761862c2068937202
@@ -762,7 +762,7 @@ float do_clip() {
vec4 dither(vec4 color) {
const int matrix_mask = 7;

ivec2 pos = ivec2(gl_FragCoord) & ivec2(matrix_mask);
ivec2 pos = ivec2(gl_FragCoord.xy) & ivec2(matrix_mask);
float noise_factor = 4.0 / 255.0;
float noise = texelFetch(sDither, pos, 0).r * noise_factor;
return color + vec4(noise, noise, noise, 0);
@@ -12,9 +12,9 @@ void main(void) {
float x = mix(clamp(vOffset, 0.0, 1.0), fract(vOffset), vGradientRepeat) * 0.5 * texture_size.x;

x = 2.0 * floor(x) + 0.5 + fract(x);
float y = vGradientIndex + 1.0 / 256.0;

// Use linear filtering to mix in the low bits (vGradientIndex + 1) with the high
// bits (vGradientIndex)
float y = vGradientIndex * 2.0 + 0.5 + 1.0 / 256.0;
oFragColor = dither(texture(sGradients, vec2(x, y) / texture_size));
}
@@ -27,7 +27,7 @@ void main(void) {
vOffset = dot(vi.local_pos - start_point, dir) / dot(dir, dir);

// V coordinate of gradient row in lookup texture.
vGradientIndex = float(prim.sub_index) * 2.0 + 0.5;
vGradientIndex = float(prim.sub_index);

// Whether to repeat the gradient instead of clamping.
vGradientRepeat = float(int(gradient.extend_mode.x) == EXTEND_MODE_REPEAT);
@@ -54,6 +54,6 @@ void main(void) {

// Use linear filtering to mix in the low bits (vGradientIndex + 1) with the high
// bits (vGradientIndex)
float y = vGradientIndex + 1.0 / 256.0;
float y = vGradientIndex * 2.0 + 0.5 + 1.0 / 256.0;
oFragColor = dither(texture(sGradients, vec2(x, y) / texture_size));
}
@@ -27,7 +27,7 @@ void main(void) {
vEndRadius = gradient.start_end_radius_extend_mode.y;

// V coordinate of gradient row in lookup texture.
vGradientIndex = float(prim.sub_index) * 2.0 + 0.5;
vGradientIndex = float(prim.sub_index);

// Whether to repeat the gradient instead of clamping.
vGradientRepeat = float(int(gradient.start_end_radius_extend_mode.z) == EXTEND_MODE_REPEAT);
@@ -1717,7 +1717,7 @@ impl Device {
}
ImageFormat::RGB8 => (gl::RGB, 3, data),
ImageFormat::RGBA8 => (get_gl_format_bgra(self.gl()), 4, data),
ImageFormat::Invalid | ImageFormat::RGBA16 | ImageFormat::RGBAF32 => unreachable!(),
ImageFormat::Invalid | ImageFormat::RGBAF32 => unreachable!(),
};

let row_length = match stride {
@@ -2103,7 +2103,6 @@ fn gl_texture_formats_for_image_format(gl: &gl::Gl, format: ImageFormat) -> (gl:
}
}
}
ImageFormat::RGBA16 => (gl::RGBA16UI as gl::GLint, gl::RGBA_INTEGER),
ImageFormat::RGBAF32 => (gl::RGBA32F as gl::GLint, gl::RGBA),
ImageFormat::Invalid => unreachable!(),
}
@@ -2112,7 +2111,6 @@ fn gl_texture_formats_for_image_format(gl: &gl::Gl, format: ImageFormat) -> (gl:
fn gl_type_for_texture_format(format: ImageFormat) -> gl::GLuint {
match format {
ImageFormat::RGBAF32 => gl::FLOAT,
ImageFormat::RGBA16 => gl::UNSIGNED_SHORT,
_ => gl::UNSIGNED_BYTE,
}
}
@@ -30,7 +30,6 @@ pub trait GpuStoreLayout {
fn texel_size() -> usize {
match Self::image_format() {
ImageFormat::RGBA8 => 4,
ImageFormat::RGBA16 => 8,
ImageFormat::RGBAF32 => 16,
_ => unreachable!(),
}
@@ -316,20 +316,19 @@ pub struct PackedTexel {

impl PackedTexel {
pub fn high_bytes(color: &ColorF) -> PackedTexel {
PackedTexel {
b: (0.5 + color.b * COLOR_FLOAT_TO_FIXED).floor() as u8,
g: (0.5 + color.g * COLOR_FLOAT_TO_FIXED).floor() as u8,
r: (0.5 + color.r * COLOR_FLOAT_TO_FIXED).floor() as u8,
a: (0.5 + color.a * COLOR_FLOAT_TO_FIXED).floor() as u8,
}
Self::extract_bytes(color, COLOR_FLOAT_TO_FIXED)
}

pub fn low_bytes(color: &ColorF) -> PackedTexel {
Self::extract_bytes(color, COLOR_FLOAT_TO_FIXED_WIDE)
}

fn extract_bytes(color: &ColorF, multiplier: f32) -> PackedTexel {
PackedTexel {
b: ((0.5 + color.b * COLOR_FLOAT_TO_FIXED_WIDE).floor() as u16 & 0xff) as u8,
g: ((0.5 + color.g * COLOR_FLOAT_TO_FIXED_WIDE).floor() as u16 & 0xff) as u8,
r: ((0.5 + color.r * COLOR_FLOAT_TO_FIXED_WIDE).floor() as u16 & 0xff) as u8,
a: ((0.5 + color.a * COLOR_FLOAT_TO_FIXED_WIDE).floor() as u16 & 0xff) as u8,
b: ((0.5 + color.b * multiplier).floor() as u32 & 0xff) as u8,
g: ((0.5 + color.g * multiplier).floor() as u32 & 0xff) as u8,
r: ((0.5 + color.r * multiplier).floor() as u32 & 0xff) as u8,
a: ((0.5 + color.a * multiplier).floor() as u32 & 0xff) as u8,
}
}
}
@@ -277,7 +277,7 @@ pub struct GradientDataEntry {
// first the entry index is calculated to determine which two colors to interpolate between, then
// the offset within that entry bucket is used to interpolate between the two colors in that entry.
// This layout preserves hard stops, as the end color for a given entry can differ from the start
// color for the following entry, despite them being adjacent. Colors are stored within in RGBA16
// color for the following entry, despite them being adjacent. Colors are stored within in BGRA8
// format for texture upload.
pub struct GradientData {
pub colors_high: [GradientDataEntry; GRADIENT_DATA_RESOLUTION],
@@ -607,7 +607,7 @@ impl TextureCache {
ImageFormat::A8 => (&mut self.arena.pages_a8, &mut profile.pages_a8),
ImageFormat::RGBA8 => (&mut self.arena.pages_rgba8, &mut profile.pages_rgba8),
ImageFormat::RGB8 => (&mut self.arena.pages_rgb8, &mut profile.pages_rgb8),
ImageFormat::Invalid | ImageFormat::RGBA16 | ImageFormat::RGBAF32 => unreachable!(),
ImageFormat::Invalid | ImageFormat::RGBAF32 => unreachable!(),
};

// TODO(gw): Handle this sensibly (support failing to render items that can't fit?)
@@ -28,7 +28,6 @@ pub enum ImageFormat {
RGB8 = 2,
RGBA8 = 3,
RGBAF32 = 4,
RGBA16 = 5,
}

impl ImageFormat {
@@ -37,7 +36,6 @@ impl ImageFormat {
ImageFormat::A8 => Some(1),
ImageFormat::RGB8 => Some(3),
ImageFormat::RGBA8 => Some(4),
ImageFormat::RGBA16 => Some(8),
ImageFormat::RGBAF32 => Some(16),
ImageFormat::Invalid => None,
}
@@ -59,4 +59,4 @@ root:
start: [ 0, -2000 ]
end: [ 0, 4000 ]
stops: [ 0.0, red, 1.0, green ]
repeat: false
repeat: false
@@ -56,4 +56,4 @@ root:
end-center: [ 990, 540 ]
end-radius: 8000
stops: [ 0.0, black, 1.0, white ]
repeat: false
repeat: false
@@ -59,4 +59,4 @@ root:
start: [ 0, -2000 ]
end: [ 1, 4000 ]
stops: [ 0.0, red, 1.0, green ]
repeat: false
repeat: false
@@ -429,7 +429,7 @@ fn is_image_opaque(format: ImageFormat, bytes: &[u8]) -> bool {
}
ImageFormat::RGB8 => true,
ImageFormat::A8 => false,
ImageFormat::Invalid | ImageFormat::RGBA16 | ImageFormat::RGBAF32 => unreachable!(),
ImageFormat::Invalid | ImageFormat::RGBAF32 => unreachable!(),
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.