mirror of
https://github.com/lisk77/comet.git
synced 2025-12-12 17:18:50 +00:00
fix(examples): changed function calls from the new Renderer2D implementation
This commit is contained in:
parent
1f983fb2ad
commit
40d60771a3
8 changed files with 3 additions and 809 deletions
|
|
@ -1,42 +0,0 @@
|
||||||
// Vertex shader
|
|
||||||
struct CameraUniform {
|
|
||||||
view_proj: mat4x4<f32>,
|
|
||||||
};
|
|
||||||
@group(1) @binding(0) // 1.
|
|
||||||
var<uniform> camera: CameraUniform;
|
|
||||||
|
|
||||||
struct VertexInput {
|
|
||||||
@location(0) position: vec3<f32>,
|
|
||||||
@location(1) tex_coords: vec2<f32>,
|
|
||||||
@location(2) color: vec4<f32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct VertexOutput {
|
|
||||||
@builtin(position) clip_position: vec4<f32>,
|
|
||||||
@location(0) tex_coords: vec2<f32>,
|
|
||||||
@location(1) color: vec4<f32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
@vertex
|
|
||||||
fn vs_main(
|
|
||||||
model: VertexInput,
|
|
||||||
) -> VertexOutput {
|
|
||||||
var out: VertexOutput;
|
|
||||||
out.tex_coords = model.tex_coords;
|
|
||||||
out.color = model.color;
|
|
||||||
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fragment shader
|
|
||||||
|
|
||||||
@group(0) @binding(0)
|
|
||||||
var t_diffuse: texture_2d<f32>;
|
|
||||||
@group(0) @binding(1)
|
|
||||||
var s_diffuse: sampler;
|
|
||||||
|
|
||||||
@fragment
|
|
||||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
||||||
let sample_color = textureSample(t_diffuse, s_diffuse, in.tex_coords);
|
|
||||||
return sample_color * in.color;
|
|
||||||
}
|
|
||||||
|
|
@ -1,134 +0,0 @@
|
||||||
use std::ops::Range;
|
|
||||||
|
|
||||||
use crate::texture;
|
|
||||||
|
|
||||||
pub trait Vertex {
|
|
||||||
fn desc() -> wgpu::VertexBufferLayout<'static>;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(C)]
|
|
||||||
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
|
||||||
pub struct ModelVertex {
|
|
||||||
pub position: [f32; 3],
|
|
||||||
pub tex_coords: [f32; 2],
|
|
||||||
pub normal: [f32; 3],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Vertex for ModelVertex {
|
|
||||||
fn desc() -> wgpu::VertexBufferLayout<'static> {
|
|
||||||
use std::mem;
|
|
||||||
wgpu::VertexBufferLayout {
|
|
||||||
array_stride: mem::size_of::<ModelVertex>() as wgpu::BufferAddress,
|
|
||||||
step_mode: wgpu::VertexStepMode::Vertex,
|
|
||||||
attributes: &[
|
|
||||||
wgpu::VertexAttribute {
|
|
||||||
offset: 0,
|
|
||||||
shader_location: 0,
|
|
||||||
format: wgpu::VertexFormat::Float32x3,
|
|
||||||
},
|
|
||||||
wgpu::VertexAttribute {
|
|
||||||
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
|
|
||||||
shader_location: 1,
|
|
||||||
format: wgpu::VertexFormat::Float32x2,
|
|
||||||
},
|
|
||||||
wgpu::VertexAttribute {
|
|
||||||
offset: mem::size_of::<[f32; 5]>() as wgpu::BufferAddress,
|
|
||||||
shader_location: 2,
|
|
||||||
format: wgpu::VertexFormat::Float32x3,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Material {
|
|
||||||
#[allow(unused)]
|
|
||||||
pub name: String,
|
|
||||||
#[allow(unused)]
|
|
||||||
pub diffuse_texture: texture::Texture,
|
|
||||||
pub bind_group: wgpu::BindGroup,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Mesh {
|
|
||||||
#[allow(unused)]
|
|
||||||
pub name: String,
|
|
||||||
pub vertex_buffer: wgpu::Buffer,
|
|
||||||
pub index_buffer: wgpu::Buffer,
|
|
||||||
pub num_elements: u32,
|
|
||||||
pub material: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Model {
|
|
||||||
pub meshes: Vec<Mesh>,
|
|
||||||
pub materials: Vec<Material>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait DrawModel<'a> {
|
|
||||||
#[allow(unused)]
|
|
||||||
fn draw_mesh(
|
|
||||||
&mut self,
|
|
||||||
mesh: &'a Mesh,
|
|
||||||
material: &'a Material,
|
|
||||||
camera_bind_group: &'a wgpu::BindGroup,
|
|
||||||
);
|
|
||||||
fn draw_mesh_instanced(
|
|
||||||
&mut self,
|
|
||||||
mesh: &'a Mesh,
|
|
||||||
material: &'a Material,
|
|
||||||
instances: Range<u32>,
|
|
||||||
camera_bind_group: &'a wgpu::BindGroup,
|
|
||||||
);
|
|
||||||
|
|
||||||
#[allow(unused)]
|
|
||||||
fn draw_model(&mut self, model: &'a Model, camera_bind_group: &'a wgpu::BindGroup);
|
|
||||||
fn draw_model_instanced(
|
|
||||||
&mut self,
|
|
||||||
model: &'a Model,
|
|
||||||
instances: Range<u32>,
|
|
||||||
camera_bind_group: &'a wgpu::BindGroup,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, 'b> DrawModel<'b> for wgpu::RenderPass<'a>
|
|
||||||
where
|
|
||||||
'b: 'a,
|
|
||||||
{
|
|
||||||
fn draw_mesh(
|
|
||||||
&mut self,
|
|
||||||
mesh: &'b Mesh,
|
|
||||||
material: &'b Material,
|
|
||||||
camera_bind_group: &'b wgpu::BindGroup,
|
|
||||||
) {
|
|
||||||
self.draw_mesh_instanced(mesh, material, 0..1, camera_bind_group);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_mesh_instanced(
|
|
||||||
&mut self,
|
|
||||||
mesh: &'b Mesh,
|
|
||||||
material: &'b Material,
|
|
||||||
instances: Range<u32>,
|
|
||||||
camera_bind_group: &'b wgpu::BindGroup,
|
|
||||||
) {
|
|
||||||
self.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
|
|
||||||
self.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
|
|
||||||
self.set_bind_group(0, &material.bind_group, &[]);
|
|
||||||
self.set_bind_group(1, camera_bind_group, &[]);
|
|
||||||
self.draw_indexed(0..mesh.num_elements, 0, instances);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_model(&mut self, model: &'b Model, camera_bind_group: &'b wgpu::BindGroup) {
|
|
||||||
self.draw_model_instanced(model, 0..1, camera_bind_group);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_model_instanced(
|
|
||||||
&mut self,
|
|
||||||
model: &'b Model,
|
|
||||||
instances: Range<u32>,
|
|
||||||
camera_bind_group: &'b wgpu::BindGroup,
|
|
||||||
) {
|
|
||||||
for mesh in &model.meshes {
|
|
||||||
let material = &model.materials[mesh.material];
|
|
||||||
self.draw_mesh_instanced(mesh, material, instances.clone(), camera_bind_group);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,313 +0,0 @@
|
||||||
use crate::camera::{CameraUniform, RenderCamera};
|
|
||||||
use crate::draw_info::DrawInfo;
|
|
||||||
use crate::render_pass::{RenderPassInfo, RenderPassType};
|
|
||||||
use crate::renderer::Renderer;
|
|
||||||
use comet_colors::Color;
|
|
||||||
use comet_ecs::{Camera2D, Component, Position2D, Render, Render2D, Scene, Text, Transform2D};
|
|
||||||
use comet_log::*;
|
|
||||||
use comet_math::{p2, p3, v2, v3};
|
|
||||||
use comet_resources::texture_atlas::TextureRegion;
|
|
||||||
use comet_resources::{graphic_resource_manager::GraphicResourceManager, Texture, Vertex};
|
|
||||||
use comet_structs::ComponentSet;
|
|
||||||
use std::iter;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Instant;
|
|
||||||
use wgpu::core::command::DrawKind::Draw;
|
|
||||||
use wgpu::naga::ShaderStage;
|
|
||||||
use wgpu::util::DeviceExt;
|
|
||||||
use wgpu::BufferUsages;
|
|
||||||
use winit::dpi::PhysicalSize;
|
|
||||||
use winit::window::Window;
|
|
||||||
|
|
||||||
pub struct Renderer2D<'a> {
|
|
||||||
surface: wgpu::Surface<'a>,
|
|
||||||
device: wgpu::Device,
|
|
||||||
queue: wgpu::Queue,
|
|
||||||
config: wgpu::SurfaceConfiguration,
|
|
||||||
size: PhysicalSize<u32>,
|
|
||||||
render_pipeline_layout: wgpu::PipelineLayout,
|
|
||||||
universal_render_pipeline: wgpu::RenderPipeline,
|
|
||||||
texture_bind_group_layout: wgpu::BindGroupLayout,
|
|
||||||
dummy_texture_bind_group: wgpu::BindGroup,
|
|
||||||
texture_sampler: wgpu::Sampler,
|
|
||||||
camera: RenderCamera,
|
|
||||||
camera_uniform: CameraUniform,
|
|
||||||
camera_buffer: wgpu::Buffer,
|
|
||||||
camera_bind_group: wgpu::BindGroup,
|
|
||||||
render_pass: Vec<RenderPassInfo>,
|
|
||||||
draw_info: Vec<DrawInfo>,
|
|
||||||
graphic_resource_manager: GraphicResourceManager,
|
|
||||||
delta_time: f32,
|
|
||||||
last_frame_time: Instant,
|
|
||||||
clear_color: wgpu::Color,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Renderer2D<'a> {
|
|
||||||
pub fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> Renderer2D<'a> {
|
|
||||||
let size = PhysicalSize::<u32>::new(1920, 1080);
|
|
||||||
|
|
||||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
|
||||||
backends: wgpu::Backends::PRIMARY,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
let surface = instance.create_surface(window).unwrap();
|
|
||||||
|
|
||||||
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
|
|
||||||
power_preference: wgpu::PowerPreference::default(),
|
|
||||||
compatible_surface: Some(&surface),
|
|
||||||
force_fallback_adapter: false,
|
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (device, queue) = pollster::block_on(adapter.request_device(
|
|
||||||
&wgpu::DeviceDescriptor {
|
|
||||||
label: None,
|
|
||||||
required_features: wgpu::Features::empty(),
|
|
||||||
required_limits: wgpu::Limits::default(),
|
|
||||||
memory_hints: Default::default(),
|
|
||||||
},
|
|
||||||
None, // Trace path
|
|
||||||
))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let surface_caps = surface.get_capabilities(&adapter);
|
|
||||||
let surface_format = surface_caps
|
|
||||||
.formats
|
|
||||||
.iter()
|
|
||||||
.copied()
|
|
||||||
.find(|f| f.is_srgb())
|
|
||||||
.unwrap_or(surface_caps.formats[0]);
|
|
||||||
let config = wgpu::SurfaceConfiguration {
|
|
||||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
|
||||||
format: surface_format,
|
|
||||||
width: size.width,
|
|
||||||
height: size.height,
|
|
||||||
present_mode: surface_caps.present_modes[0],
|
|
||||||
alpha_mode: surface_caps.alpha_modes[0],
|
|
||||||
view_formats: vec![],
|
|
||||||
desired_maximum_frame_latency: 2,
|
|
||||||
};
|
|
||||||
|
|
||||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
|
||||||
label: Some("Shader"),
|
|
||||||
source: wgpu::ShaderSource::Wgsl(include_str!("base2d.wgsl").into()),
|
|
||||||
});
|
|
||||||
|
|
||||||
let graphic_resource_manager = GraphicResourceManager::new();
|
|
||||||
|
|
||||||
let diffuse_bytes = include_bytes!(r"../../../res/textures/comet_icon.png");
|
|
||||||
let diffuse_texture =
|
|
||||||
Texture::from_bytes(&device, &queue, diffuse_bytes, "comet_icon.png", false).unwrap();
|
|
||||||
|
|
||||||
let texture_bind_group_layout =
|
|
||||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
||||||
entries: &[
|
|
||||||
wgpu::BindGroupLayoutEntry {
|
|
||||||
binding: 0,
|
|
||||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
|
||||||
ty: wgpu::BindingType::Texture {
|
|
||||||
multisampled: false,
|
|
||||||
view_dimension: wgpu::TextureViewDimension::D2,
|
|
||||||
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
|
||||||
},
|
|
||||||
count: None,
|
|
||||||
},
|
|
||||||
wgpu::BindGroupLayoutEntry {
|
|
||||||
binding: 1,
|
|
||||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
|
||||||
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
|
||||||
count: None,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
label: Some("texture_bind_group_layout"),
|
|
||||||
});
|
|
||||||
|
|
||||||
let camera = RenderCamera::new(1.0, v2::new(2.0, 2.0), v3::new(0.0, 0.0, 0.0));
|
|
||||||
|
|
||||||
let mut camera_uniform = CameraUniform::new();
|
|
||||||
camera_uniform.update_view_proj(&camera);
|
|
||||||
|
|
||||||
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
||||||
label: Some("Camera Buffer"),
|
|
||||||
contents: bytemuck::cast_slice(&[camera_uniform]),
|
|
||||||
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
|
|
||||||
});
|
|
||||||
|
|
||||||
let camera_bind_group_layout =
|
|
||||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
||||||
entries: &[wgpu::BindGroupLayoutEntry {
|
|
||||||
binding: 0,
|
|
||||||
visibility: wgpu::ShaderStages::VERTEX,
|
|
||||||
ty: wgpu::BindingType::Buffer {
|
|
||||||
ty: wgpu::BufferBindingType::Uniform,
|
|
||||||
has_dynamic_offset: false,
|
|
||||||
min_binding_size: None,
|
|
||||||
},
|
|
||||||
count: None,
|
|
||||||
}],
|
|
||||||
label: Some("camera_bind_group_layout"),
|
|
||||||
});
|
|
||||||
|
|
||||||
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
||||||
layout: &camera_bind_group_layout,
|
|
||||||
entries: &[wgpu::BindGroupEntry {
|
|
||||||
binding: 0,
|
|
||||||
resource: camera_buffer.as_entire_binding(),
|
|
||||||
}],
|
|
||||||
label: Some("camera_bind_group"),
|
|
||||||
});
|
|
||||||
|
|
||||||
let render_pipeline_layout =
|
|
||||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
||||||
label: Some("Render Pipeline Layout"),
|
|
||||||
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
|
|
||||||
push_constant_ranges: &[],
|
|
||||||
});
|
|
||||||
|
|
||||||
let universal_render_pipeline =
|
|
||||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
||||||
label: Some("Render Pipeline"),
|
|
||||||
layout: Some(&render_pipeline_layout),
|
|
||||||
vertex: wgpu::VertexState {
|
|
||||||
module: &shader,
|
|
||||||
entry_point: "vs_main",
|
|
||||||
buffers: &[Vertex::desc()],
|
|
||||||
compilation_options: Default::default(),
|
|
||||||
},
|
|
||||||
fragment: Some(wgpu::FragmentState {
|
|
||||||
module: &shader,
|
|
||||||
entry_point: "fs_main",
|
|
||||||
targets: &[Some(wgpu::ColorTargetState {
|
|
||||||
format: config.format,
|
|
||||||
blend: Some(wgpu::BlendState {
|
|
||||||
color: wgpu::BlendComponent {
|
|
||||||
src_factor: wgpu::BlendFactor::SrcAlpha,
|
|
||||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
|
||||||
operation: wgpu::BlendOperation::Add,
|
|
||||||
},
|
|
||||||
alpha: wgpu::BlendComponent {
|
|
||||||
src_factor: wgpu::BlendFactor::One,
|
|
||||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
|
||||||
operation: wgpu::BlendOperation::Add,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
|
||||||
})],
|
|
||||||
compilation_options: Default::default(),
|
|
||||||
}),
|
|
||||||
primitive: wgpu::PrimitiveState {
|
|
||||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
|
||||||
strip_index_format: None,
|
|
||||||
front_face: wgpu::FrontFace::Ccw,
|
|
||||||
cull_mode: Some(wgpu::Face::Back),
|
|
||||||
polygon_mode: wgpu::PolygonMode::Fill,
|
|
||||||
unclipped_depth: false,
|
|
||||||
conservative: false,
|
|
||||||
},
|
|
||||||
depth_stencil: None,
|
|
||||||
multisample: wgpu::MultisampleState {
|
|
||||||
count: 1,
|
|
||||||
mask: !0,
|
|
||||||
alpha_to_coverage_enabled: false,
|
|
||||||
},
|
|
||||||
multiview: None,
|
|
||||||
cache: None,
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut render_pass: Vec<RenderPassInfo> = Vec::new();
|
|
||||||
/*render_pass.push(RenderPassInfo::new_engine_pass(
|
|
||||||
&device,
|
|
||||||
"Standard Render Pass".to_string(),
|
|
||||||
&texture_bind_group_layout,
|
|
||||||
&diffuse_texture,
|
|
||||||
vec![],
|
|
||||||
vec![],
|
|
||||||
));*/
|
|
||||||
|
|
||||||
let clear_color = match clear_color {
|
|
||||||
Some(color) => color.to_wgpu(),
|
|
||||||
None => wgpu::Color {
|
|
||||||
r: 0.0,
|
|
||||||
g: 0.0,
|
|
||||||
b: 0.0,
|
|
||||||
a: 1.0,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
let texture_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
|
||||||
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
|
||||||
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
|
||||||
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
|
||||||
mag_filter: wgpu::FilterMode::Linear,
|
|
||||||
min_filter: wgpu::FilterMode::Linear,
|
|
||||||
mipmap_filter: wgpu::FilterMode::Linear,
|
|
||||||
lod_min_clamp: 0.0,
|
|
||||||
lod_max_clamp: 100.0,
|
|
||||||
compare: None,
|
|
||||||
anisotropy_clamp: 16,
|
|
||||||
border_color: None,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
let empty_texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
||||||
label: Some("Empty Texture"),
|
|
||||||
size: wgpu::Extent3d {
|
|
||||||
width: config.width,
|
|
||||||
height: config.height,
|
|
||||||
depth_or_array_layers: 1,
|
|
||||||
},
|
|
||||||
mip_level_count: 1,
|
|
||||||
sample_count: 1,
|
|
||||||
dimension: wgpu::TextureDimension::D2,
|
|
||||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
|
||||||
usage: wgpu::TextureUsages::COPY_SRC
|
|
||||||
| wgpu::TextureUsages::COPY_DST
|
|
||||||
| wgpu::TextureUsages::TEXTURE_BINDING,
|
|
||||||
view_formats: &[wgpu::TextureFormat::Bgra8UnormSrgb],
|
|
||||||
});
|
|
||||||
|
|
||||||
let dummy_texture_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
||||||
layout: &texture_bind_group_layout,
|
|
||||||
entries: &[
|
|
||||||
wgpu::BindGroupEntry {
|
|
||||||
binding: 0,
|
|
||||||
resource: wgpu::BindingResource::TextureView(
|
|
||||||
&empty_texture.create_view(&wgpu::TextureViewDescriptor::default()),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
wgpu::BindGroupEntry {
|
|
||||||
binding: 1,
|
|
||||||
resource: wgpu::BindingResource::Sampler(&texture_sampler),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
label: Some("dummy_texture_bind_group"),
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut draw_info: Vec<DrawInfo> = Vec::new();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
surface,
|
|
||||||
device,
|
|
||||||
queue,
|
|
||||||
config,
|
|
||||||
size,
|
|
||||||
render_pipeline_layout,
|
|
||||||
universal_render_pipeline,
|
|
||||||
texture_bind_group_layout,
|
|
||||||
dummy_texture_bind_group,
|
|
||||||
texture_sampler,
|
|
||||||
camera,
|
|
||||||
camera_uniform,
|
|
||||||
camera_buffer,
|
|
||||||
camera_bind_group,
|
|
||||||
render_pass,
|
|
||||||
draw_info,
|
|
||||||
graphic_resource_manager,
|
|
||||||
delta_time: 0.0,
|
|
||||||
last_frame_time: Instant::now(),
|
|
||||||
clear_color,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
struct VertexInput {
|
|
||||||
@location(0) position: vec3<f32>,
|
|
||||||
@location(1) tex_coords: vec2<f32>,
|
|
||||||
@location(2) color: vec4<f32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct VertexOutput {
|
|
||||||
@builtin(position) clip_position: vec4<f32>,
|
|
||||||
@location(0) tex_coords: vec2<f32>,
|
|
||||||
@location(1) color: vec4<f32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
@vertex
|
|
||||||
fn vs_main(input: VertexInput) -> VertexOutput {
|
|
||||||
var out: VertexOutput;
|
|
||||||
out.clip_position = vec4(input.position, 1.0);
|
|
||||||
out.tex_coords = input.tex_coords;
|
|
||||||
out.color = input.color;
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
@fragment
|
|
||||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
||||||
return in.color;
|
|
||||||
}
|
|
||||||
|
|
@ -1,196 +0,0 @@
|
||||||
mod render_context;
|
|
||||||
|
|
||||||
use render_context::*;
|
|
||||||
|
|
||||||
use crate::renderer::Renderer;
|
|
||||||
use comet_colors::Color;
|
|
||||||
use comet_resources::{graphic_resource_manager::GraphicResourceManager, Vertex};
|
|
||||||
use std::iter;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use wgpu::util::DeviceExt;
|
|
||||||
use winit::dpi::PhysicalSize;
|
|
||||||
use winit::window::Window;
|
|
||||||
|
|
||||||
pub struct Renderer2D_<'a> {
|
|
||||||
render_context: RenderContext<'a>,
|
|
||||||
universal_render_pipeline: wgpu::RenderPipeline,
|
|
||||||
graphic_resource_manager: GraphicResourceManager,
|
|
||||||
vertex_vec: Vec<Vertex>,
|
|
||||||
vertex_buffer: wgpu::Buffer,
|
|
||||||
index_vec: Vec<u32>,
|
|
||||||
index_buffer: wgpu::Buffer,
|
|
||||||
num_indices: u32,
|
|
||||||
clear_color: wgpu::Color,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Renderer2D_<'a> {
|
|
||||||
pub fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> Renderer2D_<'a> {
|
|
||||||
let render_context = RenderContext::new(window.clone(), clear_color);
|
|
||||||
let graphic_resource_manager = GraphicResourceManager::new();
|
|
||||||
let clear_color = match clear_color {
|
|
||||||
Some(color) => color.to_wgpu(),
|
|
||||||
None => wgpu::Color {
|
|
||||||
r: 0.0,
|
|
||||||
g: 0.0,
|
|
||||||
b: 0.0,
|
|
||||||
a: 1.0,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
let universal_renderpipeline_module =
|
|
||||||
render_context
|
|
||||||
.device
|
|
||||||
.create_shader_module(wgpu::ShaderModuleDescriptor {
|
|
||||||
label: Some("Universal Render Pipeline Shader Module"),
|
|
||||||
source: wgpu::ShaderSource::Wgsl(include_str!("base.wgsl").into()),
|
|
||||||
});
|
|
||||||
|
|
||||||
let universal_renderpipeline_layout =
|
|
||||||
render_context
|
|
||||||
.device
|
|
||||||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
||||||
label: Some("Universal Render Pipeline Layout"),
|
|
||||||
bind_group_layouts: &[],
|
|
||||||
push_constant_ranges: &[],
|
|
||||||
});
|
|
||||||
|
|
||||||
let universal_render_pipeline =
|
|
||||||
render_context
|
|
||||||
.device
|
|
||||||
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
||||||
label: Some("Universal Render Pipeline"),
|
|
||||||
layout: Some(&universal_renderpipeline_layout),
|
|
||||||
vertex: wgpu::VertexState {
|
|
||||||
module: &universal_renderpipeline_module,
|
|
||||||
entry_point: "vs_main",
|
|
||||||
buffers: &[Vertex::desc()],
|
|
||||||
compilation_options: Default::default(),
|
|
||||||
},
|
|
||||||
fragment: Some(wgpu::FragmentState {
|
|
||||||
module: &universal_renderpipeline_module,
|
|
||||||
entry_point: "fs_main",
|
|
||||||
targets: &[Some(wgpu::ColorTargetState {
|
|
||||||
format: render_context.config.format,
|
|
||||||
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
|
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
|
||||||
})],
|
|
||||||
compilation_options: Default::default(),
|
|
||||||
}),
|
|
||||||
primitive: wgpu::PrimitiveState {
|
|
||||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
|
||||||
strip_index_format: None,
|
|
||||||
front_face: wgpu::FrontFace::Ccw,
|
|
||||||
cull_mode: Some(wgpu::Face::Back),
|
|
||||||
polygon_mode: wgpu::PolygonMode::Fill,
|
|
||||||
unclipped_depth: false,
|
|
||||||
conservative: false,
|
|
||||||
},
|
|
||||||
depth_stencil: None,
|
|
||||||
multisample: wgpu::MultisampleState {
|
|
||||||
count: 1,
|
|
||||||
mask: !0,
|
|
||||||
alpha_to_coverage_enabled: false,
|
|
||||||
},
|
|
||||||
multiview: None,
|
|
||||||
cache: None,
|
|
||||||
});
|
|
||||||
|
|
||||||
let vertex_buffer =
|
|
||||||
render_context
|
|
||||||
.device
|
|
||||||
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
||||||
label: Some("Vertex Buffer"),
|
|
||||||
contents: &[],
|
|
||||||
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
|
|
||||||
});
|
|
||||||
|
|
||||||
let index_buffer =
|
|
||||||
render_context
|
|
||||||
.device
|
|
||||||
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
||||||
label: Some("Index Buffer"),
|
|
||||||
contents: &[],
|
|
||||||
usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
|
|
||||||
});
|
|
||||||
|
|
||||||
Self {
|
|
||||||
render_context,
|
|
||||||
universal_render_pipeline,
|
|
||||||
graphic_resource_manager,
|
|
||||||
vertex_buffer,
|
|
||||||
vertex_vec: vec![],
|
|
||||||
index_buffer,
|
|
||||||
index_vec: vec![],
|
|
||||||
num_indices: 0,
|
|
||||||
clear_color,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Renderer for Renderer2D_<'a> {
|
|
||||||
fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> Renderer2D_<'a> {
|
|
||||||
Self::new(window, clear_color)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn size(&self) -> PhysicalSize<u32> {
|
|
||||||
self.render_context.size()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn resize(&mut self, new_size: PhysicalSize<u32>) {
|
|
||||||
self.render_context.resize(new_size)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scale_factor(&self) -> f64 {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_scale_factor(&mut self, scale_factor: f64) {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self) -> f32 {
|
|
||||||
self.render_context.update()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
|
||||||
let output = self.render_context.surface.get_current_texture()?;
|
|
||||||
let output_view = output
|
|
||||||
.texture
|
|
||||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
|
||||||
|
|
||||||
let mut encoder =
|
|
||||||
self.render_context
|
|
||||||
.device
|
|
||||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
|
||||||
label: Some("Render Encoder"),
|
|
||||||
});
|
|
||||||
|
|
||||||
{
|
|
||||||
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
||||||
label: Some("Universal Render Pass"),
|
|
||||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
|
||||||
view: &output_view,
|
|
||||||
resolve_target: None,
|
|
||||||
ops: wgpu::Operations {
|
|
||||||
load: wgpu::LoadOp::Clear(self.clear_color),
|
|
||||||
store: wgpu::StoreOp::Store,
|
|
||||||
},
|
|
||||||
})],
|
|
||||||
depth_stencil_attachment: None,
|
|
||||||
occlusion_query_set: None,
|
|
||||||
timestamp_writes: None,
|
|
||||||
});
|
|
||||||
|
|
||||||
render_pass.set_pipeline(&self.universal_render_pipeline);
|
|
||||||
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
|
|
||||||
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
|
|
||||||
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.render_context
|
|
||||||
.queue
|
|
||||||
.submit(iter::once(encoder.finish()));
|
|
||||||
output.present();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,96 +0,0 @@
|
||||||
use comet_colors::Color;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Instant;
|
|
||||||
use winit::dpi::PhysicalSize;
|
|
||||||
use winit::window::Window;
|
|
||||||
|
|
||||||
pub struct RenderContext<'a> {
|
|
||||||
pub surface: wgpu::Surface<'a>,
|
|
||||||
pub device: wgpu::Device,
|
|
||||||
pub queue: wgpu::Queue,
|
|
||||||
pub config: wgpu::SurfaceConfiguration,
|
|
||||||
pub size: PhysicalSize<u32>,
|
|
||||||
pub last_frame_time: Instant,
|
|
||||||
pub delta_time: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> RenderContext<'a> {
|
|
||||||
pub fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> RenderContext<'a> {
|
|
||||||
let size = window.inner_size();
|
|
||||||
|
|
||||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
|
||||||
backends: wgpu::Backends::PRIMARY,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
let surface = instance.create_surface(window).unwrap();
|
|
||||||
|
|
||||||
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
|
|
||||||
power_preference: wgpu::PowerPreference::default(),
|
|
||||||
compatible_surface: Some(&surface),
|
|
||||||
force_fallback_adapter: false,
|
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (device, queue) = pollster::block_on(adapter.request_device(
|
|
||||||
&wgpu::DeviceDescriptor {
|
|
||||||
label: None,
|
|
||||||
required_features: wgpu::Features::empty(),
|
|
||||||
required_limits: wgpu::Limits::default(),
|
|
||||||
memory_hints: Default::default(),
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let surface_caps = surface.get_capabilities(&adapter);
|
|
||||||
let surface_format = surface_caps
|
|
||||||
.formats
|
|
||||||
.iter()
|
|
||||||
.copied()
|
|
||||||
.find(|f| f.is_srgb())
|
|
||||||
.unwrap_or(surface_caps.formats[0]);
|
|
||||||
|
|
||||||
let config = wgpu::SurfaceConfiguration {
|
|
||||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
|
||||||
format: surface_format,
|
|
||||||
width: size.width,
|
|
||||||
height: size.height,
|
|
||||||
present_mode: surface_caps.present_modes[0],
|
|
||||||
alpha_mode: surface_caps.alpha_modes[0],
|
|
||||||
view_formats: vec![],
|
|
||||||
desired_maximum_frame_latency: 2,
|
|
||||||
};
|
|
||||||
|
|
||||||
Self {
|
|
||||||
surface,
|
|
||||||
device,
|
|
||||||
queue,
|
|
||||||
config,
|
|
||||||
size,
|
|
||||||
last_frame_time: Instant::now(),
|
|
||||||
delta_time: 0.0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn size(&self) -> PhysicalSize<u32> {
|
|
||||||
self.size
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn resize(&mut self, new_size: PhysicalSize<u32>) {
|
|
||||||
if new_size.width > 0 && new_size.height > 0 {
|
|
||||||
self.size = new_size;
|
|
||||||
self.config.width = new_size.width;
|
|
||||||
self.config.height = new_size.height;
|
|
||||||
self.surface.configure(&self.device, &self.config);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update(&mut self) -> f32 {
|
|
||||||
let now = Instant::now();
|
|
||||||
let delta_time = now.duration_since(self.last_frame_time).as_millis() as f32 / 1000.0;
|
|
||||||
self.last_frame_time = now;
|
|
||||||
self.delta_time = delta_time;
|
|
||||||
delta_time
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,7 +2,7 @@ use comet::prelude::*;
|
||||||
|
|
||||||
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
||||||
// Initialize the texture atlas
|
// Initialize the texture atlas
|
||||||
renderer.initialize_atlas();
|
renderer.init_atlas();
|
||||||
|
|
||||||
// Register components
|
// Register components
|
||||||
app.register_component::<Position2D>();
|
app.register_component::<Position2D>();
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use winit_input_helper::WinitInputHelper;
|
||||||
|
|
||||||
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
||||||
// Takes all the textures from res/textures and puts them into a texture atlas
|
// Takes all the textures from res/textures and puts them into a texture atlas
|
||||||
renderer.initialize_atlas();
|
renderer.init_atlas();
|
||||||
|
|
||||||
let camera = app.new_entity();
|
let camera = app.new_entity();
|
||||||
app.add_component(camera, Transform2D::new());
|
app.add_component(camera, Transform2D::new());
|
||||||
|
|
@ -14,7 +14,7 @@ fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
||||||
|
|
||||||
app.add_component(e1, Transform2D::new());
|
app.add_component(e1, Transform2D::new());
|
||||||
|
|
||||||
let mut renderer2d = Render2D::with_texture("res/textures/comet_icon.png");
|
let renderer2d = Render2D::with_texture("res/textures/comet_icon.png");
|
||||||
|
|
||||||
app.add_component(e1, renderer2d);
|
app.add_component(e1, renderer2d);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue