Skip to content

Commit

Permalink
Translate shaded_square shader to WGSL as proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Jul 27, 2022
1 parent 463ab68 commit 697494d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
8 changes: 4 additions & 4 deletions crates/kas-wgpu/src/draw/shaded_square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl Pipeline {
label: Some("SS render_pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shaders.vert_shaded_square,
entry_point: "main",
module: &shaders.shaded_square,
entry_point: "vert",
buffers: &[wgpu::VertexBufferLayout {
array_stride: size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
Expand All @@ -72,8 +72,8 @@ impl Pipeline {
depth_stencil: None,
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &shaders.frag_shaded_square,
entry_point: "main",
module: &shaders.shaded_square,
entry_point: "frag",
targets: &[Some(wgpu::ColorTargetState {
format: super::RENDER_TEX_FORMAT,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
Expand Down
12 changes: 5 additions & 7 deletions crates/kas-wgpu/src/draw/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@

//! Shader management

use wgpu::{include_spirv, ShaderModule};
use wgpu::{include_spirv, include_wgsl, ShaderModule};

/// Shader manager
pub struct ShaderManager {
pub vert_flat_round: ShaderModule,
pub vert_round_2col: ShaderModule,
pub vert_shaded_square: ShaderModule,
pub vert_shaded_round: ShaderModule,
pub vert_image: ShaderModule,
pub vert_glyph: ShaderModule,
pub frag_flat_round: ShaderModule,
pub frag_round_2col: ShaderModule,
pub frag_shaded_square: ShaderModule,
pub frag_shaded_round: ShaderModule,
pub frag_image: ShaderModule,
pub frag_glyph: ShaderModule,
pub shaded_square: ShaderModule,
}

macro_rules! create {
Expand All @@ -33,31 +32,30 @@ impl ShaderManager {
pub fn new(device: &wgpu::Device) -> Self {
let vert_flat_round = create!(device, "shaders/flat_round.vert.spv");
let vert_round_2col = create!(device, "shaders/round_2col.vert.spv");
let vert_shaded_square = create!(device, "shaders/shaded_square.vert.spv");
let vert_shaded_round = create!(device, "shaders/shaded_round.vert.spv");
let vert_image = create!(device, "shaders/image.vert.spv");
let vert_glyph = create!(device, "shaders/glyph.vert.spv");

let frag_flat_round = create!(device, "shaders/flat_round.frag.spv");
let frag_round_2col = create!(device, "shaders/round_2col.frag.spv");
let frag_shaded_square = create!(device, "shaders/shaded_square.frag.spv");
let frag_shaded_round = create!(device, "shaders/shaded_round.frag.spv");
let frag_image = create!(device, "shaders/image.frag.spv");
let frag_glyph = create!(device, "shaders/glyph.frag.spv");

let shaded_square = device.create_shader_module(include_wgsl!("shaders/shaded_square.wgsl"));

ShaderManager {
vert_image,
vert_glyph,
vert_flat_round,
vert_round_2col,
vert_shaded_square,
vert_shaded_round,
frag_flat_round,
frag_round_2col,
frag_shaded_square,
frag_shaded_round,
frag_image,
frag_glyph,
shaded_square,
}
}
}
51 changes: 51 additions & 0 deletions crates/kas-wgpu/src/draw/shaders/shaded_square.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0

struct VertexCommon {
offset: vec2<f32>,
scale: vec2<f32>,
}
@group(0) @binding(0)
var<uniform> global: VertexCommon;

struct FragCommon {
lightNorm: vec3<f32>,
_padding: f32,
}
@group(0) @binding(1)
var<uniform> global2: FragCommon;

struct VertexOutput {
@location(0) fragColor: vec4<f32>,
@location(1) norm2: vec2<f32>,
@builtin(position) member: vec4<f32>,
}

struct FragmentOutput {
@location(0) outColor: vec4<f32>,
}

@vertex
fn vert(
@location(0) a_pos: vec2<f32>,
@location(1) a_col: vec4<f32>,
@location(2) a1: vec2<f32>,
) -> VertexOutput {
let pos = global.scale * (a_pos.xy + global.offset);
let gl_Position = vec4<f32>(pos.x, pos.y, 0.0, 1.0);
return VertexOutput(a_col, a1, gl_Position);
}

@fragment
fn frag(
@location(0) fragColor: vec4<f32>,
@location(1) norm2: vec2<f32>,
) -> FragmentOutput {
let n3: f32 = 1.0 - sqrt(norm2.x * norm2.x + norm2.y * norm2.y);
let norm = vec3<f32>(norm2.x, norm2.y, n3);
let c: vec3<f32> = (fragColor.xyz * dot(norm, global2.lightNorm));
let outColor = vec4<f32>(c.x, c.y, c.z, fragColor.w);
return FragmentOutput(outColor);
}

0 comments on commit 697494d

Please sign in to comment.