Apply HiDPI scaling to quads
The anti-aliasing strategy is pretty naive, but we will manage for now.
This commit is contained in:
parent
db716b3bdf
commit
5ff05b7f02
6 changed files with 61 additions and 32 deletions
|
|
@ -6,7 +6,7 @@ use std::mem;
|
||||||
pub struct Pipeline {
|
pub struct Pipeline {
|
||||||
pipeline: wgpu::RenderPipeline,
|
pipeline: wgpu::RenderPipeline,
|
||||||
constants: wgpu::BindGroup,
|
constants: wgpu::BindGroup,
|
||||||
transform: wgpu::Buffer,
|
constants_buffer: wgpu::Buffer,
|
||||||
vertices: wgpu::Buffer,
|
vertices: wgpu::Buffer,
|
||||||
indices: wgpu::Buffer,
|
indices: wgpu::Buffer,
|
||||||
instances: wgpu::Buffer,
|
instances: wgpu::Buffer,
|
||||||
|
|
@ -23,20 +23,20 @@ impl Pipeline {
|
||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
|
|
||||||
let transform = device
|
let constants_buffer = device
|
||||||
.create_buffer_mapped(
|
.create_buffer_mapped(
|
||||||
16,
|
1,
|
||||||
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||||
)
|
)
|
||||||
.fill_from_slice(Transformation::identity().as_ref());
|
.fill_from_slice(&[Uniforms::default()]);
|
||||||
|
|
||||||
let constants = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let constants = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
layout: &constant_layout,
|
layout: &constant_layout,
|
||||||
bindings: &[wgpu::Binding {
|
bindings: &[wgpu::Binding {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
resource: wgpu::BindingResource::Buffer {
|
resource: wgpu::BindingResource::Buffer {
|
||||||
buffer: &transform,
|
buffer: &constants_buffer,
|
||||||
range: 0..64,
|
range: 0..std::mem::size_of::<Uniforms>() as u64,
|
||||||
},
|
},
|
||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
|
|
@ -124,7 +124,7 @@ impl Pipeline {
|
||||||
},
|
},
|
||||||
wgpu::VertexAttributeDescriptor {
|
wgpu::VertexAttributeDescriptor {
|
||||||
shader_location: 4,
|
shader_location: 4,
|
||||||
format: wgpu::VertexFormat::Uint,
|
format: wgpu::VertexFormat::Float,
|
||||||
offset: 4 * (2 + 2 + 4),
|
offset: 4 * (2 + 2 + 4),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
@ -151,7 +151,7 @@ impl Pipeline {
|
||||||
Pipeline {
|
Pipeline {
|
||||||
pipeline,
|
pipeline,
|
||||||
constants,
|
constants,
|
||||||
transform,
|
constants_buffer,
|
||||||
vertices,
|
vertices,
|
||||||
indices,
|
indices,
|
||||||
instances,
|
instances,
|
||||||
|
|
@ -164,19 +164,22 @@ impl Pipeline {
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
instances: &[Quad],
|
instances: &[Quad],
|
||||||
transformation: Transformation,
|
transformation: Transformation,
|
||||||
|
scale: f32,
|
||||||
bounds: Rectangle<u32>,
|
bounds: Rectangle<u32>,
|
||||||
target: &wgpu::TextureView,
|
target: &wgpu::TextureView,
|
||||||
) {
|
) {
|
||||||
let transform_buffer = device
|
let uniforms = Uniforms::new(transformation, scale);
|
||||||
.create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC)
|
|
||||||
.fill_from_slice(transformation.as_ref());
|
let constants_buffer = device
|
||||||
|
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
||||||
|
.fill_from_slice(&[uniforms]);
|
||||||
|
|
||||||
encoder.copy_buffer_to_buffer(
|
encoder.copy_buffer_to_buffer(
|
||||||
&transform_buffer,
|
&constants_buffer,
|
||||||
0,
|
0,
|
||||||
&self.transform,
|
&self.constants_buffer,
|
||||||
0,
|
0,
|
||||||
16 * 4,
|
std::mem::size_of::<Uniforms>() as u64,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
|
@ -271,9 +274,34 @@ pub struct Quad {
|
||||||
pub position: [f32; 2],
|
pub position: [f32; 2],
|
||||||
pub scale: [f32; 2],
|
pub scale: [f32; 2],
|
||||||
pub color: [f32; 4],
|
pub color: [f32; 4],
|
||||||
pub border_radius: u32,
|
pub border_radius: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Quad {
|
impl Quad {
|
||||||
const MAX: usize = 100_000;
|
const MAX: usize = 100_000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
struct Uniforms {
|
||||||
|
transform: [f32; 16],
|
||||||
|
scale: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Uniforms {
|
||||||
|
fn new(transformation: Transformation, scale: f32) -> Uniforms {
|
||||||
|
Self {
|
||||||
|
transform: *transformation.as_ref(),
|
||||||
|
scale,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Uniforms {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
transform: *Transformation::identity().as_ref(),
|
||||||
|
scale: 1.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -239,7 +239,7 @@ impl Renderer {
|
||||||
color: match background {
|
color: match background {
|
||||||
Background::Color(color) => color.into_linear(),
|
Background::Color(color) => color.into_linear(),
|
||||||
},
|
},
|
||||||
border_radius: *border_radius as u32,
|
border_radius: *border_radius as f32,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Primitive::Image { path, bounds } => {
|
Primitive::Image { path, bounds } => {
|
||||||
|
|
@ -327,13 +327,14 @@ impl Renderer {
|
||||||
encoder,
|
encoder,
|
||||||
&layer.quads,
|
&layer.quads,
|
||||||
transformation,
|
transformation,
|
||||||
|
dpi,
|
||||||
bounds,
|
bounds,
|
||||||
target,
|
target,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if layer.images.len() > 0 {
|
if layer.images.len() > 0 {
|
||||||
let translated = transformation
|
let translated_and_scaled = transformation
|
||||||
* Transformation::scale(dpi, dpi)
|
* Transformation::scale(dpi, dpi)
|
||||||
* Transformation::translate(
|
* Transformation::translate(
|
||||||
-(layer.offset.x as f32),
|
-(layer.offset.x as f32),
|
||||||
|
|
@ -344,7 +345,7 @@ impl Renderer {
|
||||||
&mut self.device,
|
&mut self.device,
|
||||||
encoder,
|
encoder,
|
||||||
&layer.images,
|
&layer.images,
|
||||||
translated,
|
translated_and_scaled,
|
||||||
bounds,
|
bounds,
|
||||||
target,
|
target,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
layout(location = 0) in vec4 v_Color;
|
layout(location = 0) in vec4 v_Color;
|
||||||
layout(location = 1) in vec2 v_Pos;
|
layout(location = 1) in vec2 v_Pos;
|
||||||
layout(location = 2) in vec2 v_Scale;
|
layout(location = 2) in vec2 v_Scale;
|
||||||
layout(location = 3) in flat uint v_BorderRadius;
|
layout(location = 3) in float v_BorderRadius;
|
||||||
|
|
||||||
layout(location = 0) out vec4 o_Color;
|
layout(location = 0) out vec4 o_Color;
|
||||||
|
|
||||||
|
|
@ -27,11 +27,7 @@ float rounded(in vec2 frag_coord, in vec2 position, in vec2 size, float radius,
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
float radius_alpha = 1.0;
|
float radius_alpha = rounded(gl_FragCoord.xy, v_Pos, v_Scale, v_BorderRadius, 0.5);
|
||||||
|
|
||||||
if(v_BorderRadius > 0.0) {
|
|
||||||
radius_alpha = rounded(gl_FragCoord.xy, v_Pos, v_Scale, v_BorderRadius, 0.5);
|
|
||||||
}
|
|
||||||
|
|
||||||
o_Color = vec4(v_Color.xyz, v_Color.w * radius_alpha);
|
o_Color = vec4(v_Color.xyz, v_Color.w * radius_alpha);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -4,29 +4,33 @@ layout(location = 0) in vec2 v_Pos;
|
||||||
layout(location = 1) in vec2 i_Pos;
|
layout(location = 1) in vec2 i_Pos;
|
||||||
layout(location = 2) in vec2 i_Scale;
|
layout(location = 2) in vec2 i_Scale;
|
||||||
layout(location = 3) in vec4 i_Color;
|
layout(location = 3) in vec4 i_Color;
|
||||||
layout(location = 4) in uint i_BorderRadius;
|
layout(location = 4) in float i_BorderRadius;
|
||||||
|
|
||||||
layout (set = 0, binding = 0) uniform Globals {
|
layout (set = 0, binding = 0) uniform Globals {
|
||||||
mat4 u_Transform;
|
mat4 u_Transform;
|
||||||
|
float u_Scale;
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(location = 0) out vec4 o_Color;
|
layout(location = 0) out vec4 o_Color;
|
||||||
layout(location = 1) out vec2 o_Pos;
|
layout(location = 1) out vec2 o_Pos;
|
||||||
layout(location = 2) out vec2 o_Scale;
|
layout(location = 2) out vec2 o_Scale;
|
||||||
layout(location = 3) out uint o_BorderRadius;
|
layout(location = 3) out float o_BorderRadius;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
vec2 p_Pos = i_Pos * u_Scale;
|
||||||
|
vec2 p_Scale = i_Scale * u_Scale;
|
||||||
|
|
||||||
mat4 i_Transform = mat4(
|
mat4 i_Transform = mat4(
|
||||||
vec4(i_Scale.x, 0.0, 0.0, 0.0),
|
vec4(p_Scale.x + 1.0, 0.0, 0.0, 0.0),
|
||||||
vec4(0.0, i_Scale.y, 0.0, 0.0),
|
vec4(0.0, p_Scale.y + 1.0, 0.0, 0.0),
|
||||||
vec4(0.0, 0.0, 1.0, 0.0),
|
vec4(0.0, 0.0, 1.0, 0.0),
|
||||||
vec4(i_Pos, 0.0, 1.0)
|
vec4(p_Pos - vec2(0.5, 0.5), 0.0, 1.0)
|
||||||
);
|
);
|
||||||
|
|
||||||
o_Color = i_Color;
|
o_Color = i_Color;
|
||||||
o_Pos = i_Pos;
|
o_Pos = p_Pos;
|
||||||
o_Scale = i_Scale;
|
o_Scale = p_Scale;
|
||||||
o_BorderRadius = i_BorderRadius;
|
o_BorderRadius = i_BorderRadius * u_Scale;
|
||||||
|
|
||||||
gl_Position = u_Transform * i_Transform * vec4(v_Pos, 0.0, 1.0);
|
gl_Position = u_Transform * i_Transform * vec4(v_Pos, 0.0, 1.0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue