summaryrefslogtreecommitdiffstats
path: root/src/wgpuctx/mod.rs
blob: f8aa3ec4eb9298197d2f72df8ff8de952c93a6cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use std::sync::Arc;
use std::error::Error;
use wgpu::util::DeviceExt;
use winit::window::{Window};

use crate::texture::Texture;

pub mod pipeline;

pub struct WgpuCtx
{
    surface: wgpu::Surface<'static>,
    surface_conf: wgpu::SurfaceConfiguration,
    surface_texture: Option<wgpu::SurfaceTexture>,
    is_configured: bool,

    adapter: wgpu::Adapter,
    device: wgpu::Device,
    queue: wgpu::Queue,
}

pub struct RenderPassBuilder<'encoder>
{
    label: &'static str,
    view: &'encoder wgpu::TextureView,
    clear_color: wgpu::Color
}

impl WgpuCtx
{
    pub async fn new(
        instance: &wgpu::Instance,
        window: Arc<Window>,
    ) -> Self {
        let surface = instance.create_surface(window.clone()).unwrap();

        let adapter = instance
        .request_adapter(&wgpu::RequestAdapterOptions {
                compatible_surface: Some(&surface),
                ..Default::default()
            }).await.expect("Failed to find valid adapter");

        let (device, queue) = adapter
            .request_device(&wgpu::DeviceDescriptor {
                label: None,
                required_features: wgpu::Features::empty(),
                required_limits: wgpu::Limits::downlevel_defaults(),
                experimental_features: wgpu::ExperimentalFeatures::disabled(),
                memory_hints: wgpu::MemoryHints::Performance,
                trace: wgpu::Trace::Off
            }).await.expect("Failed to find device and queue");

        let size = window.inner_size();
        let surface_config = surface.get_default_config(&adapter, size.width, size.height).unwrap();

        Self {
            surface: surface,
            surface_conf: surface_config,
            surface_texture: None,
            is_configured: false,
            adapter: adapter,
            device: device,
            queue: queue
        }
    }

    pub fn resize(
        &mut self,
        width: u32,
        height: u32)
    {
        self.surface_conf.width = width;
        self.surface_conf.height = height;
        self.surface.configure(&self.device, &self.surface_conf);
        self.is_configured = true;
    }

    pub fn is_ready(
        &self)
    -> bool {
        self.is_configured
    }

    pub fn surface_config(
        &self)
    -> wgpu::SurfaceConfiguration {
        self.surface_conf.clone()
    }

    pub fn adapter(
        &self)
    -> &wgpu::Adapter {
        &self.adapter
    }

    pub fn device(
        &self)
    -> &wgpu::Device {
        &self.device
    }

    pub fn queue(
        &self)
    -> &wgpu::Queue {
        &self.queue
    }

    pub fn prepare_surface(
        &mut self,
        view_descr: &wgpu::TextureViewDescriptor)
    -> Result<wgpu::TextureView, wgpu::SurfaceError>
    {
        let texture = self.surface.get_current_texture()?;
        let view = texture.texture.create_view(view_descr);

        self.surface_texture = Some(texture);
        Ok(view)
    }

    pub fn create_encoder(
        &self,
        descr: &wgpu::CommandEncoderDescriptor)
    -> wgpu::CommandEncoder
    {
        self.device.create_command_encoder(descr)
    }

    pub fn create_default_encoder(
        &self,
        label: &'static str)
    -> wgpu::CommandEncoder {
        self.create_encoder(
            &wgpu::CommandEncoderDescriptor {
                label: Some(label)
            }
        )
    }

    pub fn submit_encoder(
        &self,
        encoder: wgpu::CommandEncoder)
    {
        self.queue.submit(std::iter::once(encoder.finish()));
    }

    pub fn present_surface(
        &mut self)
    {
        let texture = self.surface_texture.take();
        match texture {
            Some(t) => t.present(),
            None => {}
        }
    }

    pub fn create_shader(
        &self,
        module_descr: wgpu::ShaderModuleDescriptor)
    -> wgpu::ShaderModule {
        self.device.create_shader_module(module_descr)
    }

    pub fn create_pipeline_layout(
        &self,
        layout_descr: &wgpu::PipelineLayoutDescriptor)
    -> wgpu::PipelineLayout {
        self.device.create_pipeline_layout(layout_descr)
    }

    pub fn create_render_pipeline(
        &self,
        pipeline_layout: &wgpu::RenderPipelineDescriptor)
    -> wgpu::RenderPipeline {
        self.device.create_render_pipeline(pipeline_layout)
    }

    pub fn create_buffer_init(
        &self,
        descr: &wgpu::util::BufferInitDescriptor
    ) -> wgpu::Buffer {
        self.device.create_buffer_init(descr)
    }

    pub fn create_buffer(
        &self,
        descr: &wgpu::BufferDescriptor)
    -> wgpu::Buffer {
        self.device.create_buffer(descr)
    }

    pub fn new_texture(
        &self,
        descr: wgpu::TextureDescriptor)
    -> Texture
    {
        Texture::new(&self.device, descr)
    }
     
} //impl WgpuCtx

impl<'encoder> RenderPassBuilder<'encoder>
{
    pub fn new(
        label: &'static str,
        view: &'encoder wgpu::TextureView)
    -> Self {
        Self {
            label: label,
            view: view,
            clear_color: wgpu::Color { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }
        }
    }

    pub fn clear_color(
        mut self,
        color: wgpu::Color)
    -> Self {
        self.clear_color = color;
        self
    }

    pub fn build(
        self,
        encoder: &'encoder mut wgpu::CommandEncoder)
    -> wgpu::RenderPass<'encoder>
    {
        encoder.begin_render_pass(
            &wgpu::RenderPassDescriptor {
                label: Some(self.label),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: self.view,
                    resolve_target: None,
                    depth_slice: 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,
            }
        )
    }
}