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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]
// use dx::core30::gl;
use crate::{
    gles::{core20::gl, enums::*},
    Color,
};
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use std::{
    borrow::Cow,
    ffi::CString,
    mem,
    num::NonZeroU8,
    ops::Range,
    os::raw::c_void,
    ptr, str,
    sync::{mpsc::Receiver, Arc},
};
pub use wgpu_types::{
    AddressMode, BindGroupLayoutEntry, BlendState, BufferAddress, BufferSize, BufferUsage,
    ColorTargetState, ColorWrite, CompareFunction, DepthStencilState, Extent3d, Face, FilterMode,
    FrontFace, ImageDataLayout, ImageSubresourceRange, InputStepMode, MultisampleState, Origin3d,
    PolygonMode, PresentMode, PrimitiveState, PrimitiveTopology, PushConstantRange, QueryType,
    SamplerBorderColor, SwapChainDescriptor, TextureFormat, TextureUsage, VertexAttribute,
};
use winit::{
    dpi::{LogicalSize, PhysicalSize, Size},
    event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent},
    event_loop::{ControlFlow, EventLoop},
    window::{Window, WindowBuilder},
};

pub struct ShaderFlags(u32);

impl ShaderFlags {
    pub const VALIDATION: ShaderFlags = ShaderFlags(0);
    pub const EXPERIMENTAL_TRANSLATION: ShaderFlags = ShaderFlags(1);
}

impl Default for ShaderFlags {
    fn default() -> Self {
        ShaderFlags::VALIDATION
    }
}

pub type Label<'a> = Option<&'a str>;

pub type CommandEncoderDescriptor<'a> = wgpu_types::CommandEncoderDescriptor<Label<'a>>;

pub type ImageCopyBuffer<'a> = wgpu_types::ImageCopyBuffer<&'a Buffer>;

pub type ImageCopyTexture<'a> = wgpu_types::ImageCopyTexture<&'a Texture>;

pub enum ShaderSource<'a> {
    Vertex(Cow<'a, [u8]>),
    Fragment(Cow<'a, [u8]>),
    Geometry(Cow<'a, [u8]>),       // GL30, ES32
    Compute(Cow<'a, [u8]>),        // GL43, ES31
    TessControl(Cow<'a, [u8]>),    // GL4, ES32
    TessEvaluation(Cow<'a, [u8]>), // GL4, ES32
}

pub struct ShaderModule(u32);

pub struct ShaderModuleDescriptor<'a> {
    pub label: Label<'a>,
    pub source: ShaderSource<'a>,
    pub flags: ShaderFlags,
}

pub enum LoadOp<V> {
    Clear(V),
    Load,
}

pub struct Operations<V> {
    pub load: LoadOp<V>,
    pub store: bool,
}

pub struct RenderPassColorAttachment<'a> {
    pub view: &'a TextureView,
    pub resolve_target: Option<&'a TextureView>,
    pub ops: Operations<Color>,
}

pub struct RenderPassDepthStencilAttachment<'a> {
    pub view: &'a TextureView,
    pub depth_ops: Option<Operations<f32>>,
    pub stencil_ops: Option<Operations<u32>>,
}

pub struct RenderPassDescriptor<'a, 'b> {
    pub label: Label<'a>,
    pub color_attachments: &'b [RenderPassColorAttachment<'a>],
    pub depth_stencil_attachment: Option<RenderPassDepthStencilAttachment<'a>>,
}

pub struct CommandBuffer {/* fields omitted */}

pub struct RenderPass<'a> {
    label: Label<'a>,
}

pub struct ComputePass<'a> {
    label: Label<'a>,
}

pub struct ComputePassDescriptor<'a> {
    pub label: Label<'a>,
}

pub struct CommandEncoder;

impl CommandEncoder {
    pub fn finish(&self) -> CommandBuffer {
        unimplemented!()
    }

    pub fn begin_render_pass<'a>(
        &'a mut self,
        desc: &RenderPassDescriptor<'a, '_>,
    ) -> RenderPass<'a> {
        RenderPass {
            label: desc.label.clone(),
        }
    }

    pub fn begin_compute_pass<'a>(&mut self, desc: &ComputePassDescriptor<'a>) -> ComputePass<'a> {
        ComputePass {
            label: desc.label.clone(),
        }
    }

    pub fn copy_buffer_to_buffer(
        &mut self,
        source: &Buffer,
        source_offset: BufferAddress,
        destination: &Buffer,
        destination_offset: BufferAddress,
        copy_size: BufferAddress,
    ) {
    }

    pub fn copy_buffer_to_texture(
        &mut self,
        source: ImageCopyBuffer<'_>,
        destination: ImageCopyTexture<'_>,
        copy_size: Extent3d,
    ) {
    }

    pub fn copy_texture_to_buffer(
        &mut self,
        source: ImageCopyTexture<'_>,
        destination: ImageCopyBuffer<'_>,
        copy_size: Extent3d,
    ) {
    }

    pub fn copy_texture_to_texture(
        &mut self,
        source: ImageCopyTexture<'_>,
        destination: ImageCopyTexture<'_>,
        copy_size: Extent3d,
    ) {
    }

    pub fn clear_texture(&mut self, texture: &Texture, subresource_range: &ImageSubresourceRange) {}

    pub fn clear_buffer(
        &mut self,
        buffer: &Buffer,
        offset: BufferAddress,
        size: Option<BufferSize>,
    ) {
    }

    pub fn insert_debug_marker(&mut self, label: &str) {}

    pub fn push_debug_group(&mut self, label: &str) {}

    pub fn pop_debug_group(&mut self) {}

    pub fn write_timestamp(&mut self, query_set: &QuerySet, query_index: u32) {}

    pub fn resolve_query_set(
        &mut self,
        query_set: &QuerySet,
        query_range: Range<u32>,
        destination: &Buffer,
        destination_offset: BufferAddress,
    ) {
    }
}

pub struct RenderBundleEncoder;

pub struct RenderBundleEncoderDescriptor<'a> {
    pub label: Label<'a>,
    pub color_formats: &'a [TextureFormat],
    pub depth_stencil_format: Option<TextureFormat>,
    pub sample_count: u32,
}

pub struct BindGroup;

pub struct TextureView {/* fields omitted */}

pub struct BufferBinding<'a> {
    pub buffer: &'a Buffer,
    pub offset: BufferAddress,
    pub size: Option<BufferSize>,
}

#[non_exhaustive]
pub enum BindingResource<'a> {
    Buffer(BufferBinding<'a>),
    BufferArray(&'a [BufferBinding<'a>]),
    Sampler(&'a Sampler),
    TextureView(&'a TextureView),
    TextureViewArray(&'a [&'a TextureView]),
}

pub struct BindGroupEntry<'a> {
    pub binding: u32,
    pub resource: BindingResource<'a>,
}

pub struct BindGroupDescriptor<'a> {
    pub label: Label<'a>,
    pub layout: &'a BindGroupLayout,
    pub entries: &'a [BindGroupEntry<'a>],
}

pub struct BindGroupLayout;

pub struct BindGroupLayoutDescriptor<'a> {
    pub label: Label<'a>,
    pub entries: &'a [BindGroupLayoutEntry],
}

pub struct PipelineLayout;

pub struct PipelineLayoutDescriptor<'a> {
    pub label: Label<'a>,
    pub bind_group_layouts: &'a [&'a BindGroupLayout],
    pub push_constant_ranges: &'a [PushConstantRange],
}

pub struct RenderPipeline(u32);

pub struct VertexBufferLayout<'a> {
    pub array_stride: BufferAddress,
    pub step_mode: InputStepMode,
    pub attributes: &'a [VertexAttribute],
}

pub struct VertexState<'a> {
    pub module: &'a ShaderModule,
    pub entry_point: &'a str,
    pub buffers: &'a [VertexBufferLayout<'a>],
}

pub struct FragmentState<'a> {
    pub module: &'a ShaderModule,
    pub entry_point: &'a str,
    pub targets: &'a [ColorTargetState],
}

pub struct RenderPipelineDescriptor<'a> {
    pub label: Label<'a>,
    pub layout: Option<&'a PipelineLayout>,
    pub vertex: VertexState<'a>,
    pub primitive: PrimitiveState,
    pub depth_stencil: Option<DepthStencilState>,
    pub multisample: MultisampleState,
    pub fragment: Option<FragmentState<'a>>,
}

pub struct ComputePipeline(u32);

pub struct ComputePipelineDescriptor<'a> {
    pub label: Label<'a>,
    pub layout: Option<&'a PipelineLayout>,
    pub module: &'a ShaderModule,
    pub entry_point: &'a str,
}

pub struct Buffer;

pub type BufferDescriptor<'a> = wgpu_types::BufferDescriptor<Label<'a>>;

pub struct Texture;

pub type TextureDescriptor<'a> = wgpu_types::TextureDescriptor<Label<'a>>;

pub struct Sampler;

pub struct SamplerDescriptor<'a> {
    pub label: Label<'a>,
    pub address_mode_u: AddressMode,
    pub address_mode_v: AddressMode,
    pub address_mode_w: AddressMode,
    pub mag_filter: FilterMode,
    pub min_filter: FilterMode,
    pub mipmap_filter: FilterMode,
    pub lod_min_clamp: f32,
    pub lod_max_clamp: f32,
    pub compare: Option<CompareFunction>,
    pub anisotropy_clamp: Option<NonZeroU8>,
    pub border_color: Option<SamplerBorderColor>,
}

pub struct QuerySet;

pub struct QuerySetDescriptor {
    pub ty: QueryType,
    pub count: u32,
}

pub struct BufferInitDescriptor<'a> {
    pub label: Label<'a>,
    pub contents: &'a [u8],
    pub usage: BufferUsage,
}

#[derive(Debug)]
pub enum SwapChainError {
    Timeout,
    Outdated,
    Lost,
    OutOfMemory,
}

pub struct SwapChainTexture {
    pub view: TextureView,
    // some fields omitted
}

pub struct SwapChainFrame {
    pub output: SwapChainTexture,
    pub suboptimal: bool,
}

pub struct SwapChain {
    egl: Arc<egl::Instance<egl::Dynamic<libloading::Library, egl::EGL1_4>>>,
    display: egl::Display,
}

impl SwapChain {
    pub fn present(&self, surface: &Surface) {
        self.egl
            .swap_buffers(self.display, surface.raw)
            .expect("unable to post EGL context");
    }

    pub fn get_current_frame(&self) -> Result<SwapChainFrame, SwapChainError> {
        Ok(SwapChainFrame {
            output: SwapChainTexture {
                view: TextureView {},
            },
            suboptimal: true,
        })
    }
}

// #[repr(C)]
// pub struct SwapChainDescriptor {
//     pub usage: TextureUsage,
//     pub format: TextureFormat,
//     pub width: u32,
//     pub height: u32,
//     pub present_mode: PresentMode,
// }

// impl SwapChainDescriptor {
//     fn new() -> Self {
//         Self {
//             usage: TextureUsage::SAMPLED,
//             format: TextureFormat::Rgba8Uint,
//             width: 0,
//             height: 0,
//             present_mode: PresentMode::Immediate,
//         }
//     }
// }

// impl Default for SwapChainDescriptor {
//     fn default() -> Self {
//         Self::new()
//     }
// }

pub struct Surface {
    pub(crate) raw: egl::Surface,
}

impl Surface {}

pub struct Queue;

impl Queue {
    fn new() -> Self {
        Self {}
    }

    pub fn write_buffer(&self, buffer: &Buffer, offset: BufferAddress, data: &[u8]) {
        unimplemented!()
    }

    pub fn write_texture(
        &self,
        texture: ImageCopyTexture<'_>,
        data: &[u8],
        data_layout: ImageDataLayout,
        size: Extent3d,
    ) {
        unimplemented!()
    }

    pub fn submit<I: IntoIterator<Item = CommandBuffer>>(&self, command_buffers: I) {
        unimplemented!()
    }

    pub fn get_timestamp_period(&self) -> f32 {
        unimplemented!()
    }
}

pub struct Device {
    egl: Arc<egl::Instance<egl::Dynamic<libloading::Library, egl::EGL1_4>>>,
    display: egl::Display,
}

impl Device {
    pub fn create_shader_module(&self, desc: &ShaderModuleDescriptor) -> ShaderModule {
        let (source, kind) = match &desc.source {
            ShaderSource::Vertex(source) => (source, GL_VERTEX_SHADER),
            ShaderSource::Fragment(source) => (source, GL_FRAGMENT_SHADER),
            ShaderSource::Compute(source) => (source, GL_COMPUTE_SHADER),
            ShaderSource::Geometry(source) => (source, GL_GEOMETRY_SHADER),
            ShaderSource::TessControl(source) => (source, GL_TESS_CONTROL_SHADER),
            ShaderSource::TessEvaluation(source) => (source, GL_TESS_EVALUATION_SHADER),
        };

        let id = gl::create_shader(kind);

        gl::shader_source(id, source.as_ref());
        gl::compile_shader(id);

        let success = gl::get_shaderiv(id, GL_COMPILE_STATUS);

        if success == 0 {
            let len = gl::get_shaderiv(id, GL_INFO_LOG_LENGTH);

            match gl::get_shader_info_log(id, len) {
                Some(message) => panic!("{}: {}", desc.label.unwrap_or("Shader"), message),
                None => panic!("{}: Compile error", desc.label.unwrap_or("Shader")),
            };
        }

        ShaderModule(id)
    }

    pub fn create_command_encoder(&self, desc: &CommandEncoderDescriptor) -> CommandEncoder {
        CommandEncoder {}
    }

    pub fn create_render_bundle_encoder(
        &self,
        desc: &RenderBundleEncoderDescriptor,
    ) -> RenderBundleEncoder {
        unimplemented!()
    }

    pub fn create_bind_group(&self, desc: &BindGroupDescriptor) -> BindGroup {
        unimplemented!()
    }

    pub fn create_bind_group_layout(&self, desc: &BindGroupLayoutDescriptor) -> BindGroupLayout {
        unimplemented!()
    }

    pub fn create_pipeline_layout(&self, desc: &PipelineLayoutDescriptor) -> PipelineLayout {
        PipelineLayout {}
    }

    pub fn create_render_pipeline(&self, desc: &RenderPipelineDescriptor) -> RenderPipeline {
        let program_id = gl::create_program();

        gl::attach_shader(program_id, desc.vertex.module.0);
        if let Some(fragment) = &desc.fragment {
            gl::attach_shader(program_id, fragment.module.0);
        }

        gl::link_program(program_id);

        // error handling here
        let success = gl::get_programiv(program_id, GL_LINK_STATUS);

        if success == 0 {
            let len = gl::get_programiv(program_id, GL_INFO_LOG_LENGTH);

            match gl::get_program_info_log(program_id, len) {
                Some(message) => panic!("{}: {}", desc.label.unwrap_or("Render Pipeline"), message),
                None => panic!("{}: Link error", desc.label.unwrap_or("Render Pipeline")),
            };
        }

        gl::detach_shader(program_id, desc.vertex.module.0);
        if let Some(fragment) = &desc.fragment {
            gl::detach_shader(program_id, fragment.module.0);
        }

        RenderPipeline(program_id)
    }

    pub fn create_compute_pipeline(&self, desc: &ComputePipelineDescriptor) -> ComputePipeline {
        unimplemented!()
    }

    pub fn create_buffer(&self, desc: &BufferDescriptor) -> Buffer {
        unimplemented!()
    }

    pub fn create_texture(&self, desc: &TextureDescriptor) -> Texture {
        unimplemented!()
    }

    pub fn create_sampler(&self, desc: &SamplerDescriptor) -> Sampler {
        unimplemented!()
    }

    pub fn create_query_set(&self, desc: &QuerySetDescriptor) -> QuerySet {
        unimplemented!()
    }

    pub fn create_swap_chain(&self, surface: &Surface, desc: &SwapChainDescriptor) -> SwapChain {
        SwapChain {
            egl: self.egl.clone(),
            display: self.display,
        }
    }

    pub fn on_uncaptured_error(&self) {
        unimplemented!()
    }

    pub fn start_capture(&self) {
        unimplemented!()
    }

    pub fn stop_capture(&self) {
        unimplemented!()
    }

    // DeviceExt

    pub fn create_buffer_init(&self, desc: &BufferInitDescriptor) -> Buffer {
        unimplemented!()
    }

    pub fn create_texture_with_data(
        &self,
        queue: &Queue,
        desc: &TextureDescriptor,
        data: &[u8],
    ) -> Texture {
        unimplemented!()
    }
}

pub struct Adapter {
    egl: Arc<egl::Instance<egl::Dynamic<libloading::Library, egl::EGL1_4>>>,
    display: egl::Display,
}

impl Adapter {
    pub fn request_device(&self) -> (Device, Queue) {
        let device = Device {
            egl: self.egl.clone(),
            display: self.display,
        };

        let queue = Queue {};

        (device, queue)
    }
}

pub struct Instance {
    egl: Arc<egl::Instance<egl::Dynamic<libloading::Library, egl::EGL1_4>>>,
    display: egl::Display,
}

impl Instance {
    pub fn new() -> Self {
        // let lib = libloading::Library::new("libEGL.so.1").expect("unable to find libEGL.so.1");
        // let egl = unsafe { egl::DynamicInstance::<egl::EGL1_4>::load_required_from(lib).expect("unable to load libEGL.so.1") };

        // EGL setup here
        let egl = unsafe {
            Arc::new(
                egl::DynamicInstance::<egl::EGL1_4>::load_required()
                    .expect("unable to load libEGL.so.1"),
            )
        };

        // Setup OpenGL ES API
        egl.bind_api(egl::OPENGL_ES_API)
            .expect("unable to select OpenGL ES API"); // for OpenGL ES

        // Setup Display
        let display = egl
            .get_display(egl::DEFAULT_DISPLAY)
            .expect("unable to get display");

        egl.initialize(display).expect("unable to init EGL");

        use std::ffi::CStr;

        unsafe {
            libloading::Library::new("libGLESv2.so.2").expect("unable to find libGLESv2.so.2");
            crate::gles::ffi::load_global_gl_with(|c_str| {
                let procname = CStr::from_ptr(c_str).to_str().unwrap();
                egl.get_proc_address(procname).unwrap() as *mut std::ffi::c_void
            });
            // unsafe { mem::transmute(egli::egl::get_proc_address(s)) }
        };

        Self { egl, display }
    }

    pub fn request_adapter(&self) -> Adapter {
        Adapter {
            egl: self.egl.clone(),
            display: self.display,
        }
    }

    pub fn create_surface(&self, window: &Window) -> Surface {
        // Create context
        let attrib_list = [
            egl::BUFFER_SIZE,
            16,
            egl::DEPTH_SIZE,
            16,
            egl::STENCIL_SIZE,
            0,
            egl::SURFACE_TYPE,
            egl::WINDOW_BIT,
            egl::NONE,
        ];

        // // Get the number of matching configurations.
        // let count = egl
        //     .matching_config_count(display, &attrib_list)
        //     .expect("no available configurations");

        // Get the matching configuration.
        let config = self
            .egl
            .choose_first_config(self.display, &attrib_list)
            .expect("unable to choose EGL configuration")
            .expect("no EGL configuration found");

        // if ou not set CONTEXT_CLIENT_VERSION if will set to GLES v.1, otherwise 2 or more;
        let ctx_attribs = [egl::CONTEXT_CLIENT_VERSION, 2, egl::NONE]; // GLESv2/3+
                                                                       // let ctx_attribs = [egl::NONE]; // GLESv1+
        let ctx = self
            .egl
            .create_context(self.display, config, None, &ctx_attribs)
            .expect("unable to create EGL context");

        // Create a EGL surface
        let surface = unsafe {
            let window_handle = match window.raw_window_handle() {
                RawWindowHandle::Xlib(handle) => handle.window as egl::NativeWindowType,
                RawWindowHandle::Xcb(handle) => handle.window as egl::NativeWindowType,
                RawWindowHandle::Wayland(handle) => handle.surface as egl::NativeWindowType,
                _ => {
                    panic!("Other handle type");
                }
            };

            self.egl
                .create_window_surface(self.display, config, window_handle, None)
                .expect("unable to create an EGL surface")
        };

        self.egl
            .make_current(self.display, Some(surface), Some(surface), Some(ctx))
            .expect("unable to bind the context");

        Surface { raw: surface }
    }
}