Update shader and renderer to use a dummy render texture
This commit is contained in:
parent
34da4bf0fc
commit
9cf17a8066
|
@ -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 {
|
struct VertexOutput {
|
||||||
@builtin(position) clip_position: vec4<f32>,
|
@builtin(position) clip_position: vec4<f32>,
|
||||||
|
@location(0) tex_coords: vec2<f32>,
|
||||||
};
|
};
|
||||||
|
|
||||||
@vertex
|
@vertex
|
||||||
|
@ -7,13 +17,17 @@ fn vertex(
|
||||||
@builtin(vertex_index) in_vertex_index: u32,
|
@builtin(vertex_index) in_vertex_index: u32,
|
||||||
) -> VertexOutput {
|
) -> VertexOutput {
|
||||||
var out: VertexOutput;
|
var out: VertexOutput;
|
||||||
let x = f32(1 - i32(in_vertex_index)) * 0.5;
|
out.clip_position = vec4<f32>(DATA[in_vertex_index].xy, 0.0, 1.0);
|
||||||
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
|
out.tex_coords = DATA[in_vertex_index].zw;
|
||||||
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@group(0) @binding(0)
|
||||||
|
var t_diffuse: texture_2d<f32>;
|
||||||
|
@group(0) @binding(1)
|
||||||
|
var s_diffuse: sampler;
|
||||||
|
|
||||||
@fragment
|
@fragment
|
||||||
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use winit::{dpi::PhysicalSize, window::Window};
|
use winit::{dpi::PhysicalSize, window::Window};
|
||||||
|
|
||||||
|
use crate::texture::{Texture, TextureBuilder};
|
||||||
|
|
||||||
pub(crate) struct RenderContext {
|
pub(crate) struct RenderContext {
|
||||||
pub instance: wgpu::Instance,
|
pub instance: wgpu::Instance,
|
||||||
pub size: PhysicalSize<u32>,
|
pub size: PhysicalSize<u32>,
|
||||||
|
@ -71,6 +73,7 @@ impl RenderContext {
|
||||||
pub(crate) struct Renderer {
|
pub(crate) struct Renderer {
|
||||||
clear_color: wgpu::Color,
|
clear_color: wgpu::Color,
|
||||||
render_pipeline: wgpu::RenderPipeline,
|
render_pipeline: wgpu::RenderPipeline,
|
||||||
|
render_texture: Texture,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Renderer {
|
impl Renderer {
|
||||||
|
@ -79,6 +82,23 @@ impl Renderer {
|
||||||
let shader_descriptor = wgpu::include_wgsl!("../assets/shaders/shader.wgsl");
|
let shader_descriptor = wgpu::include_wgsl!("../assets/shaders/shader.wgsl");
|
||||||
let shader = context.device.create_shader_module(shader_descriptor);
|
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...");
|
log::info!("Creating render pipeline...");
|
||||||
let render_pipeline =
|
let render_pipeline =
|
||||||
context
|
context
|
||||||
|
@ -88,7 +108,7 @@ impl Renderer {
|
||||||
layout: Some(&context.device.create_pipeline_layout(
|
layout: Some(&context.device.create_pipeline_layout(
|
||||||
&wgpu::PipelineLayoutDescriptor {
|
&wgpu::PipelineLayoutDescriptor {
|
||||||
label: Some("draw"),
|
label: Some("draw"),
|
||||||
bind_group_layouts: &[],
|
bind_group_layouts: &[&render_texture.bind_group_layout],
|
||||||
push_constant_ranges: &[],
|
push_constant_ranges: &[],
|
||||||
},
|
},
|
||||||
)),
|
)),
|
||||||
|
@ -118,6 +138,7 @@ impl Renderer {
|
||||||
Self {
|
Self {
|
||||||
clear_color,
|
clear_color,
|
||||||
render_pipeline,
|
render_pipeline,
|
||||||
|
render_texture,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +152,7 @@ impl Renderer {
|
||||||
.device
|
.device
|
||||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
|
.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"),
|
label: Some("Render Pass"),
|
||||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||||
view: &view,
|
view: &view,
|
||||||
|
@ -143,6 +164,11 @@ impl Renderer {
|
||||||
})],
|
})],
|
||||||
depth_stencil_attachment: None,
|
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()));
|
context.queue.submit(Some(encoder.finish()));
|
||||||
frame.present();
|
frame.present();
|
||||||
|
|
Loading…
Reference in New Issue