Skip to content

Commit

Permalink
beautify hello triangle
Browse files Browse the repository at this point in the history
  • Loading branch information
boralg committed Jun 26, 2024
1 parent d927932 commit acf0fc2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
33 changes: 26 additions & 7 deletions examples/src/hello_triangle/assets/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
// Vertex Shader
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) color: vec4<f32>,
};

@vertex
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> {
let x = f32(i32(in_vertex_index) - 1);
let y = f32(i32(in_vertex_index & 1u) * 2 - 1);
return vec4<f32>(x, y, 0.0, 1.0);
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> VertexOutput {
var positions = array<vec2<f32>, 3>(
vec2<f32>(0.0, 0.5), // Top vertex
vec2<f32>(-0.5, -0.5), // Bottom left vertex
vec2<f32>(0.5, -0.5) // Bottom right vertex
);
var colors = array<vec4<f32>, 3>(
vec4<f32>(1.0, 0.0, 0.0, 1.0), // Red color
vec4<f32>(0.0, 1.0, 0.0, 1.0), // Green color
vec4<f32>(0.0, 0.0, 1.0, 1.0) // Blue color
);

var output: VertexOutput;
output.position = vec4<f32>(positions[in_vertex_index], 0.0, 1.0);
output.color = colors[in_vertex_index];
return output;
}

// Fragment Shader
@fragment
fn fs_main() -> @location(0) vec4<f32> {
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
}
fn fs_main(@location(0) in_color: vec4<f32>) -> @location(0) vec4<f32> {
return in_color;
}
21 changes: 9 additions & 12 deletions examples/src/hello_triangle/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use sursface::{display::Display, std::{clear_screen, create_render_pipeline, create_shader}, wgpu::{self, Color, CommandEncoder, RenderPass, RenderPipeline, Surface, SurfaceTexture, TextureView}, winit::event::WindowEvent};
use sursface::{app::App, display::{self, Display}, std::{clear_screen, create_render_pipeline, create_shader, get_framebuffer}, wgpu::{self, Color, CommandEncoder, RenderPass, RenderPipeline, Surface, SurfaceTexture, TextureView}, winit::event::WindowEvent};

#[cfg(not(target_arch = "wasm32"))]
fn main() {
use sursface::winit::dpi::PhysicalSize;
use sursface::winit::{dpi::PhysicalSize, event};
sursface::start::create_window_desktop(PhysicalSize::new(1280, 720), &init, &render, &event);
}

Expand All @@ -11,7 +11,7 @@ fn main() {}


#[cfg(target_arch = "wasm32")]
#[sursface::wasm_bindgen::prelude::wasm_bindgen]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn start_browser(canvas: sursface::wgpu::web_sys::HtmlCanvasElement) {
use sursface::start;

Expand All @@ -23,6 +23,8 @@ struct TriangleState {
}

fn init(display: &mut Display) -> TriangleState {
use std::borrow::Cow;

let device = &display.device;

let shader = create_shader(device, include_str!("assets/shader.wgsl"));
Expand All @@ -32,7 +34,7 @@ fn init(display: &mut Display) -> TriangleState {
bind_group_layouts: &[],
push_constant_ranges: &[],
});

let render_pipeline = create_render_pipeline(display, pipeline_layout, shader, &[]);
TriangleState { render_pipeline }
}
Expand All @@ -45,7 +47,9 @@ fn render(display: &mut Display, state: &mut TriangleState) {
a: 255.0 / 255.0,
};

let mut encoder = display.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
let device = &display.device;

let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Encoder"),
});

Expand All @@ -59,12 +63,6 @@ fn render(display: &mut Display, state: &mut TriangleState) {
output.present();
}

fn get_framebuffer(surface: &Surface) -> (SurfaceTexture, TextureView) {
let output = surface.get_current_texture().unwrap();
let view = output.texture.create_view(&wgpu::TextureViewDescriptor::default());
(output, view)
}

pub fn draw_triangle<'a>(
rpass: &mut wgpu::RenderPass<'a>,
pipeline: &'a RenderPipeline,
Expand All @@ -73,5 +71,4 @@ pub fn draw_triangle<'a>(
rpass.draw(0..3, 0..1);
}


fn event<'a>(_display: &mut Display, _state: &mut TriangleState, _event: WindowEvent) {}

0 comments on commit acf0fc2

Please sign in to comment.