Skip to content

Commit

Permalink
- Changed syntaxes & naming conventions re: PR comments.
Browse files Browse the repository at this point in the history
- Adjusted default instances/triangles to be more consistent.
  • Loading branch information
bungoboingo committed Dec 23, 2022
1 parent 34d490e commit d253f95
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 61 deletions.
13 changes: 8 additions & 5 deletions examples/tour/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use iced::theme;
use iced::theme::Palette;
use iced::theme::{self, Palette};
use iced::widget::{
checkbox, column, container, horizontal_space, image, radio, row,
scrollable, slider, text, text_input, toggler, vertical_space,
Expand Down Expand Up @@ -55,7 +54,9 @@ impl Sandbox for Tour {
if steps.has_previous() {
controls = controls.push(
button("Back").on_press(Message::BackPressed).style(
theme::Button::Custom(Box::new(CustomButtonStyle::Secondary)),
theme::Button::Custom(Box::new(
CustomButtonStyle::Secondary,
)),
),
);
}
Expand Down Expand Up @@ -690,18 +691,20 @@ impl widget::button::StyleSheet for CustomButtonStyle {
.expect("Build gradient")
.into(),
text_color: Color::WHITE,
border_radius: 5.0,
..Default::default()
},
CustomButtonStyle::Secondary => widget::button::Appearance {
background: Gradient::linear(Radians(
3.0 * std::f32::consts::PI / 2.0,
))
.add_stop(0.0, Palette::LIGHT.danger)
.add_stop(1.0, Color::from_rgb8(125, 26, 24))
.add_stop(0.0, Color::from_rgb8(194, 194, 194))
.add_stop(1.0, Color::from_rgb8(126, 126, 126))
.build()
.expect("Build gradient")
.into(),
text_color: Color::WHITE,
border_radius: 5.0,
..Default::default()
},
}
Expand Down
12 changes: 6 additions & 6 deletions glow/src/quad/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use glow::HasContext;
use iced_graphics::layer;
use iced_native::Rectangle;

const MAX_INSTANCES: usize = 10_000;
const INITIAL_INSTANCES: usize = 10_000;

#[derive(Debug)]
pub struct Pipeline {
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Pipeline {

mod solid {
use crate::program::{self, Shader};
use crate::quad::core::{Uniforms, MAX_INSTANCES};
use crate::quad::core::{Uniforms, INITIAL_INSTANCES};
use glow::HasContext;
use iced_graphics::layer::quad;

Expand Down Expand Up @@ -216,7 +216,7 @@ mod solid {
};

let (vertex_array, instances) =
unsafe { Self::create_instance_buffer(gl, MAX_INSTANCES) };
unsafe { Self::create_instance_buffer(gl, INITIAL_INSTANCES) };

Self {
program,
Expand Down Expand Up @@ -330,7 +330,7 @@ mod solid {

mod gradient {
use crate::program::{self, Shader};
use crate::quad::core::{Uniforms, MAX_INSTANCES};
use crate::quad::core::{Uniforms, INITIAL_INSTANCES};
use glow::HasContext;
use iced_graphics::layer::quad;

Expand Down Expand Up @@ -391,7 +391,7 @@ mod gradient {
};

let (vertex_array, instances) =
unsafe { Self::create_instance_buffer(gl, MAX_INSTANCES) };
unsafe { Self::create_instance_buffer(gl, INITIAL_INSTANCES) };

Self {
program,
Expand Down Expand Up @@ -540,7 +540,7 @@ fn draw_instances<T>(gl: &glow::Context, instances: &[T])
where
T: bytemuck::Zeroable + bytemuck::Pod,
{
for instances_chunk in instances.chunks(MAX_INSTANCES) {
for instances_chunk in instances.chunks(INITIAL_INSTANCES) {
unsafe {
gl.buffer_sub_data_u8_slice(
glow::ARRAY_BUFFER,
Expand Down
10 changes: 5 additions & 5 deletions glow/src/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use iced_graphics::triangle::{ColoredVertex2D, GradientVertex2D};
use glow::{Context, HasContext, NativeProgram};
use std::marker::PhantomData;

const DEFAULT_VERTICES: usize = 1_000;
const DEFAULT_INDICES: usize = 1_000;
const INITIAL_VERTICES: usize = 10_000;
const INITIAL_INDICES: usize = 10_000;

#[derive(Debug)]
pub(crate) struct Pipeline {
Expand All @@ -25,7 +25,7 @@ impl Pipeline {
gl,
glow::ELEMENT_ARRAY_BUFFER,
glow::DYNAMIC_DRAW,
DEFAULT_INDICES,
INITIAL_INDICES,
)
};

Expand Down Expand Up @@ -337,7 +337,7 @@ mod solid {
gl,
glow::ARRAY_BUFFER,
glow::DYNAMIC_DRAW,
super::DEFAULT_VERTICES,
super::INITIAL_VERTICES,
)
};

Expand Down Expand Up @@ -442,7 +442,7 @@ mod gradient {
gl,
glow::ARRAY_BUFFER,
glow::DYNAMIC_DRAW,
super::DEFAULT_VERTICES,
super::INITIAL_VERTICES,
)
};

Expand Down
10 changes: 5 additions & 5 deletions wgpu/src/quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ const QUAD_VERTS: [QuadVertex; 4] = [
},
];

const MAX_INSTANCES: usize = 100_000;
const INITIAL_INSTANCES: usize = 10_000;

#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
#[repr(C)]
Expand Down Expand Up @@ -241,7 +241,7 @@ impl Default for Uniforms {

mod solid {
use crate::buffer;
use crate::quad::{QuadVertex, MAX_INSTANCES};
use crate::quad::{QuadVertex, INITIAL_INSTANCES};
use iced_graphics::layer::quad;
use std::mem;

Expand All @@ -261,7 +261,7 @@ mod solid {
device,
"iced_wgpu::quad::solid instance buffer",
wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
MAX_INSTANCES,
INITIAL_INSTANCES,
);

let layout = device.create_pipeline_layout(
Expand Down Expand Up @@ -368,7 +368,7 @@ mod solid {

mod gradient {
use crate::buffer;
use crate::quad::{QuadVertex, MAX_INSTANCES};
use crate::quad::{QuadVertex, INITIAL_INSTANCES};
use iced_graphics::layer::quad;
use std::mem;

Expand All @@ -388,7 +388,7 @@ mod gradient {
device,
"iced_wgpu::quad::gradient instance buffer",
wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
MAX_INSTANCES,
INITIAL_INSTANCES,
);

let layout = device.create_pipeline_layout(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ struct Globals {

@group(0) @binding(0) var<uniform> globals: Globals;

struct SolidVertexInput {
@location(0) position: vec2<f32>,
@location(1) color: vec4<f32>,
}

struct SolidVertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) color: vec4<f32>,
}

@vertex
fn solid_vs_main(input: SolidVertexInput) -> SolidVertexOutput {
var out: SolidVertexOutput;

out.color = input.color;
out.position = globals.transform * vec4<f32>(input.position, 0.0, 1.0);

return out;
}

@fragment
fn solid_fs_main(input: SolidVertexOutput) -> @location(0) vec4<f32> {
return input.color;
}

struct GradientVertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) raw_position: vec2<f32>,
Expand All @@ -21,7 +46,7 @@ struct GradientVertexOutput {
}

@vertex
fn vs_main(
fn gradient_vs_main(
@location(0) input: vec2<f32>,
@location(1) color_1: vec4<f32>,
@location(2) color_2: vec4<f32>,
Expand All @@ -39,7 +64,6 @@ fn vs_main(

output.position = globals.transform * vec4<f32>(input.xy, 0.0, 1.0);
output.raw_position = input;
//pass gradient data to frag shader
output.color_1 = color_1;
output.color_2 = color_2;
output.color_3 = color_3;
Expand Down Expand Up @@ -103,7 +127,7 @@ fn gradient(
}

@fragment
fn fs_main(input: GradientVertexOutput) -> @location(0) vec4<f32> {
fn gradient_fs_main(input: GradientVertexOutput) -> @location(0) vec4<f32> {
let colors = array<vec4<f32>, 8>(
input.color_1,
input.color_2,
Expand All @@ -126,7 +150,6 @@ fn fs_main(input: GradientVertexOutput) -> @location(0) vec4<f32> {
input.offsets_2.w,
);

//TODO could just pass this in to the shader but is probably more performant to just check it here
var last_index = 7;
for (var i: i32 = 0; i <= 7; i++) {
if (offsets[i] >= 1.0) {
Expand Down
30 changes: 0 additions & 30 deletions wgpu/src/shader/triangle/solid.wgsl

This file was deleted.

12 changes: 6 additions & 6 deletions wgpu/src/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ mod solid {
),
source: wgpu::ShaderSource::Wgsl(
std::borrow::Cow::Borrowed(include_str!(
"shader/triangle/solid.wgsl"
"shader/triangle.wgsl"
)),
),
});
Expand All @@ -386,7 +386,7 @@ mod solid {
layout: Some(&layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
entry_point: "solid_vs_main",
buffers: &[wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<
triangle::ColoredVertex2D,
Expand All @@ -403,7 +403,7 @@ mod solid {
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
entry_point: "solid_fs_main",
targets: &[triangle::fragment_target(format)],
}),
primitive: triangle::primitive_state(),
Expand Down Expand Up @@ -522,7 +522,7 @@ mod gradient {
),
source: wgpu::ShaderSource::Wgsl(
std::borrow::Cow::Borrowed(include_str!(
"shader/triangle/gradient.wgsl"
"shader/triangle.wgsl"
)),
),
});
Expand All @@ -533,7 +533,7 @@ mod gradient {
layout: Some(&layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
entry_point: "gradient_vs_main",
buffers: &[wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<GradientVertex2D>(
) as u64,
Expand Down Expand Up @@ -568,7 +568,7 @@ mod gradient {
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
entry_point: "gradient_fs_main",
targets: &[triangle::fragment_target(format)],
}),
primitive: triangle::primitive_state(),
Expand Down

0 comments on commit d253f95

Please sign in to comment.