Skip to content

Instantly share code, notes, and snippets.

@skwee357
Last active August 6, 2020 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skwee357/781273530875c0253f688b9bd23d1984 to your computer and use it in GitHub Desktop.
Save skwee357/781273530875c0253f688b9bd23d1984 to your computer and use it in GitHub Desktop.
let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("sk:graphics:encoder")
});
let render_pipeline_layout = self.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[]
});
let vertex_shader_module = self.device.create_shader_module(
&wgpu::read_spirv(std::io::Cursor::new(SHADER_VERT)).expect("Failed to read vertex shader")
);
let fragment_shader_module = self.device.create_shader_module(
&wgpu::read_spirv(std::io::Cursor::new(SHADER_FRAG)).expect("Failed to read fragment shader")
);
let render_pipeline = self.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &render_pipeline_layout,
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vertex_shader_module,
entry_point: "main",
},
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
module: &fragment_shader_module,
entry_point: "main",
}),
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
color_states: &[
wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8UnormSrgb,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
write_mask: wgpu::ColorWrite::ALL,
}
],
depth_stencil_state: None,
vertex_state: wgpu::VertexStateDescriptor {
index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[Vertex2D::description()],
},
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
});
let vertex_buffer = self.device.create_buffer_with_data(bytemuck::cast_slice(VERTICES), wgpu::BufferUsage::VERTEX);
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[
wgpu::RenderPassColorAttachmentDescriptor {
attachment: &view,
resolve_target: None,
load_op: wgpu::LoadOp::Clear,
store_op: wgpu::StoreOp::Store,
clear_color: wgpu::Color::BLACK,
}
],
depth_stencil_attachment: None,
});
render_pass.set_pipeline(&render_pipeline);
render_pass.set_vertex_buffer(0, &vertex_buffer, 0, 0);
render_pass.draw(0..3, 0..1);
}
self.queue.submit(&[encoder.finish()]);
#version 450
layout(location=0) in vec4 v_Color;
layout(location=0) out vec4 f_Color;
void main() {
f_Color = v_Color;
}
#version 450
layout(location=0) in vec2 a_Position;
layout(location=1) in vec4 a_Color;
layout(location=0) out vec4 v_Color;
void main() {
v_Color = a_Color;
gl_Position = vec4(a_Position, 0.0, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment