Pass roughness & metalness factors to shader

This commit is contained in:
Lauri Räsänen 2023-11-11 17:31:04 +02:00
parent 8b99a617f4
commit f9798743b8
4 changed files with 50 additions and 10 deletions

View file

@ -27,6 +27,13 @@ struct GlobalUniforms {
@group(0) @binding(2) @group(0) @binding(2)
var<uniform> global_uniforms: GlobalUniforms; var<uniform> global_uniforms: GlobalUniforms;
struct MaterialUniform {
metallic_factor: f32,
rougness_factor: f32,
_padding1: f32,
_padding2: f32,
}
struct VertexInput { struct VertexInput {
@location(0) position: vec3<f32>, @location(0) position: vec3<f32>,
@location(1) tex_coords: vec2<f32>, @location(1) tex_coords: vec2<f32>,

View file

@ -67,6 +67,9 @@ var t_roughness_metalness: texture_2d<f32>;
@group(2) @binding(5) @group(2) @binding(5)
var s_roughness_metalness: sampler; var s_roughness_metalness: sampler;
@group(2) @binding(6)
var<uniform> material_uniform: MaterialUniform;
@fragment @fragment
fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> { fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
// textures // textures
@ -76,10 +79,9 @@ fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
t_roughness_metalness, s_roughness_metalness, vert.tex_coords); t_roughness_metalness, s_roughness_metalness, vert.tex_coords);
let albedo = object_color.xyz; let albedo = object_color.xyz;
// TODO: pass factors to shader let roughness = object_roughness_metalness.y * material_uniform.rougness_factor;
let roughness = object_roughness_metalness.y * 1.0; let metalness = object_roughness_metalness.z * material_uniform.metallic_factor;
let metalness = object_roughness_metalness.z * 1.0;
var total_radiance: vec3<f32>; var total_radiance: vec3<f32>;
let in_light = sample_direct_light(vert.world_position); let in_light = sample_direct_light(vert.world_position);

View file

@ -1,3 +1,4 @@
use wgpu::util::DeviceExt;
use crate::core::texture::Texture; use crate::core::texture::Texture;
pub struct Material { pub struct Material {
@ -5,13 +6,17 @@ pub struct Material {
pub diffuse_texture: Texture, pub diffuse_texture: Texture,
pub normal_texture: Texture, pub normal_texture: Texture,
pub metallic_roughness_texture: Texture, pub metallic_roughness_texture: Texture,
// TODO pass to shader pub material_uniform: MaterialUniform,
pub metallic_factor: f32,
// TODO pass to shader
pub roughness_factor: f32,
pub bind_group: wgpu::BindGroup, pub bind_group: wgpu::BindGroup,
} }
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct MaterialUniform {
// metallic, roughness, none, none
pub factors: [f32; 4],
}
impl Material { impl Material {
pub fn new( pub fn new(
device: &wgpu::Device, device: &wgpu::Device,
@ -23,6 +28,15 @@ impl Material {
roughness_factor: f32, roughness_factor: f32,
layout: &wgpu::BindGroupLayout, layout: &wgpu::BindGroupLayout,
) -> Self { ) -> Self {
let material_uniform = MaterialUniform {
factors: [metallic_factor, roughness_factor, 0.0, 0.0]
};
let material_uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Material Uniform UB"),
contents: bytemuck::cast_slice(&[material_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout, layout,
entries: &[ entries: &[
@ -53,6 +67,11 @@ impl Material {
binding: 5, binding: 5,
resource: wgpu::BindingResource::Sampler(&metallic_roughness_texture.sampler), resource: wgpu::BindingResource::Sampler(&metallic_roughness_texture.sampler),
}, },
// uniform
wgpu::BindGroupEntry {
binding: 6,
resource: material_uniform_buffer.as_entire_binding(),
}
], ],
label: None, label: None,
}); });
@ -62,8 +81,7 @@ impl Material {
diffuse_texture, diffuse_texture,
normal_texture, normal_texture,
metallic_roughness_texture, metallic_roughness_texture,
metallic_factor, material_uniform,
roughness_factor,
bind_group, bind_group,
} }
} }

View file

@ -5,6 +5,7 @@ use std::time::Duration;
use wgpu::util::DeviceExt; use wgpu::util::DeviceExt;
use winit::{event::*, window::Window}; use winit::{event::*, window::Window};
use crate::core::material::MaterialUniform;
use super::camera::{Camera, CameraController, CameraUniform}; use super::camera::{Camera, CameraController, CameraUniform};
use super::instance::{Instance, InstanceRaw}; use super::instance::{Instance, InstanceRaw};
@ -305,6 +306,7 @@ impl State {
let geometry_depth_bind_group = State::create_geometry_depth_bind_group(&device, &geometry_depth_bind_group_layout, &geometry_depth_texture); let geometry_depth_bind_group = State::create_geometry_depth_bind_group(&device, &geometry_depth_bind_group_layout, &geometry_depth_texture);
let material_uniform_size = mem::size_of::<MaterialUniform>() as u64;
let texture_bind_group_layout = let texture_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[ entries: &[
@ -359,6 +361,17 @@ impl State {
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None, count: None,
}, },
// material uniform
wgpu::BindGroupLayoutEntry {
binding: 6,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(material_uniform_size),
},
count: None,
},
], ],
label: Some("texture_bind_group_layout"), label: Some("texture_bind_group_layout"),
}); });