render-zig

A 3D rendering engine written in Zig
git clone git://git.christianermann.dev/render-zig
Log | Files | Refs

lines.wgsl (1432B)


      1 
      2 struct VertexInput {
      3     @location(0) position: vec4<f32>,
      4 };
      5 
      6 struct VertexOutput {
      7     @builtin(position) clip_position: vec4<f32>,
      8     @location(0) @interpolate(flat) instance_id: u32,
      9 };
     10 
     11 @group(0) @binding(0)
     12 var<storage, read> instances: array<Instance>;
     13 struct Instance {
     14     model_matrix: mat4x4<f32>,
     15     material: Material,
     16 };
     17 struct Material {
     18     albedo: u32,
     19     normal: u32,
     20     roughness: u32,
     21     metalness: u32,
     22 };
     23 
     24 @group(0) @binding(1)
     25 var<uniform> uniform_data: UniformData;
     26 struct UniformData {
     27     pw_camera: vec3<f32>,
     28     view_matrix: mat4x4<f32>,
     29     proj_matrix: mat4x4<f32>,
     30 };
     31 
     32 @vertex fn vertex(
     33     in: VertexInput,
     34     @builtin(instance_index) idx: u32,
     35 ) -> VertexOutput {
     36     let model_matrix = instances[0].model_matrix;
     37     let camera_matrix = uniform_data.proj_matrix * uniform_data.view_matrix;
     38     let pc_vertex = camera_matrix * model_matrix * in.position;
     39     return VertexOutput(pc_vertex, idx);
     40 }
     41 
     42 @group(0) @binding(2) var material_sampler: sampler;
     43 @group(0) @binding(3) var material_texture: texture_2d_array<f32>;
     44 
     45 @fragment fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
     46     let colors = array<vec3<f32>, 6>(
     47         vec3<f32>(1, 1, 0),
     48         vec3<f32>(0, 1, 1),
     49         vec3<f32>(1, 0, 1),
     50         vec3<f32>(1, 0, 0),
     51         vec3<f32>(0, 1, 0),
     52         vec3<f32>(0, 0, 1),
     53     );
     54     let color = colors[in.instance_id % 6];
     55 
     56     return vec4<f32>(0.0, 0.0, 0.0, 1.0);
     57 }