Update shader and renderer to use a dummy render texture

This commit is contained in:
Jarrod Doyle 2023-04-08 11:28:36 +01:00
parent 34da4bf0fc
commit 9cf17a8066
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 46 additions and 6 deletions

View File

@ -1,5 +1,15 @@
var<private> DATA: array<vec4<f32>, 6> = array<vec4<f32>, 6>(
vec4<f32>( -1.0, 1.0, 0.0, 1.0 ),
vec4<f32>( -1.0, -1.0, 0.0, 0.0 ),
vec4<f32>( 1.0, -1.0, 1.0, 0.0 ),
vec4<f32>( -1.0, 1.0, 0.0, 1.0 ),
vec4<f32>( 1.0, -1.0, 1.0, 0.0 ),
vec4<f32>( 1.0, 1.0, 1.0, 1.0 )
);
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
};
@vertex
@ -7,13 +17,17 @@ fn vertex(
@builtin(vertex_index) in_vertex_index: u32,
) -> VertexOutput {
var out: VertexOutput;
let x = f32(1 - i32(in_vertex_index)) * 0.5;
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
out.clip_position = vec4<f32>(DATA[in_vertex_index].xy, 0.0, 1.0);
out.tex_coords = DATA[in_vertex_index].zw;
return out;
}
@group(0) @binding(0)
var t_diffuse: texture_2d<f32>;
@group(0) @binding(1)
var s_diffuse: sampler;
@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(0.3, 0.2, 0.1, 1.0);
return textureSample(t_diffuse, s_diffuse, in.tex_coords);
}

View File

@ -1,5 +1,7 @@
use winit::{dpi::PhysicalSize, window::Window};
use crate::texture::{Texture, TextureBuilder};
pub(crate) struct RenderContext {
pub instance: wgpu::Instance,
pub size: PhysicalSize<u32>,
@ -71,6 +73,7 @@ impl RenderContext {
pub(crate) struct Renderer {
clear_color: wgpu::Color,
render_pipeline: wgpu::RenderPipeline,
render_texture: Texture,
}
impl Renderer {
@ -79,6 +82,23 @@ impl Renderer {
let shader_descriptor = wgpu::include_wgsl!("../assets/shaders/shader.wgsl");
let shader = context.device.create_shader_module(shader_descriptor);
log::info!("Creating render texture...");
let render_texture = TextureBuilder::new()
.with_size(context.size.width, context.size.height, 1)
.build(&context);
let data_len = context.size.width * context.size.height * 4;
let mut data: Vec<u8> = Vec::with_capacity(data_len.try_into().unwrap());
for y in 0..context.size.height {
for x in 0..context.size.width {
data.push(255u8);
data.push(128u8);
data.push(128u8);
data.push(255u8);
}
}
render_texture.update(&context, &data);
log::info!("Creating render pipeline...");
let render_pipeline =
context
@ -88,7 +108,7 @@ impl Renderer {
layout: Some(&context.device.create_pipeline_layout(
&wgpu::PipelineLayoutDescriptor {
label: Some("draw"),
bind_group_layouts: &[],
bind_group_layouts: &[&render_texture.bind_group_layout],
push_constant_ranges: &[],
},
)),
@ -118,6 +138,7 @@ impl Renderer {
Self {
clear_color,
render_pipeline,
render_texture,
}
}
@ -131,7 +152,7 @@ impl Renderer {
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Render Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
@ -143,6 +164,11 @@ impl Renderer {
})],
depth_stencil_attachment: None,
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.render_texture.bind_group, &[]);
render_pass.draw(0..6, 0..1);
drop(render_pass);
context.queue.submit(Some(encoder.finish()));
frame.present();