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
/*!

Provides a high performance [Vulkan 1.2](https://www.vulkan.org/) driver using smart pointers.
Supports windowed and headless rendering with a fully-featured render graph and resource pooling
types.

This crate allows graphics programmers to focus on acceleration structure, buffer, and image
resources and the shader pipelines they are accessed from. There are no restrictions or opinions
placed on the types of graphics algorithms you might create. Some implementations of common graphics
patterns are provided in the `contrib` directory.

# Getting Sarted

Typical usage involves creating an operating system window event loop, and rendering frames
using the provided [`FrameContext`] closure. The [`EventLoop`] builder handles creating an instance
of the [`Device`] driver, however you may construct one manually for headless rendering.

```no_run
use screen_13::prelude::*;

fn main() -> Result<(), DisplayError> {
    let event_loop = EventLoop::new().build()?;

    // Use the device to create resources and pipelines before running
    let device = &event_loop.device;

    event_loop.run(|frame| {
        // You may also create resources and pipelines while running
        let device = &frame.device;
    })
}
```

# Resources and Pipelines

All resources and pipelines, as well as the driver itself, use shared reference tracking to keep
pointers alive. _Screen 13_ uses `std::sync::Arc` to track references.

## Information

All [`driver`] types have associated information structures which describe their properties.
Each object provides a `create` function which uses the information to return an instance.

| Resource                      | Create Using                                        |
|-------------------------------|-----------------------------------------------------|
| [`AccelerationStructureInfo`] | [`AccelerationStructure::create`]                   |
| [`BufferInfo`]                | [`Buffer::create`] or [`Buffer::create_from_slice`] |
| [`ImageInfo`]                 | [`Image::create`]                                   |

For example, a typical host-mappable buffer:

```no_run
# use std::sync::Arc;
# use ash::vk;
# use screen_13::driver::DriverError;
# use screen_13::driver::device::{Device, DeviceInfo};
# use screen_13::driver::buffer::{Buffer, BufferInfo};
# fn main() -> Result<(), DriverError> {
# let device = Arc::new(Device::create_headless(DeviceInfo::new())?);
let info = BufferInfo::host_mem(1024, vk::BufferUsageFlags::STORAGE_BUFFER);
let my_buf = Buffer::create(&device, info)?;
# Ok(()) }
```

| Pipeline                      | Create Using                                        |
|-------------------------------|-----------------------------------------------------|
| [`ComputePipelineInfo`]       | [`ComputePipeline::create`]                         |
| [`GraphicPipelineInfo`]       | [`GraphicPipeline::create`]                         |
| [`RayTracePipelineInfo`]      | [`RayTracePipeline::create`]                        |

For example, a graphics pipeline:

```no_run
# use std::sync::Arc;
# use ash::vk;
# use screen_13::driver::DriverError;
# use screen_13::driver::device::{Device, DeviceInfo};
# use screen_13::driver::graphic::{GraphicPipeline, GraphicPipelineInfo};
# use screen_13::driver::shader::Shader;
# fn main() -> Result<(), DriverError> {
# let device = Arc::new(Device::create_headless(DeviceInfo::new())?);
# let my_frag_code = [0u8; 1];
# let my_vert_code = [0u8; 1];
// shader code is raw SPIR-V code as bytes
let vert = Shader::new_vertex(my_vert_code.as_slice());
let frag = Shader::new_fragment(my_frag_code.as_slice());
let info = GraphicPipelineInfo::default();
let my_pipeline = GraphicPipeline::create(&device, info, [vert, frag])?;
# Ok(()) }
```

## Pooling

Multiple [`pool`] types are available to reduce the impact of frequently creating and dropping
resources. Leased resources behave identically to owned resources and can be used in a render graph.

Resource aliasing is also availble as an optional way to reduce the number of concurrent resources
that may be required.

For example, leasing an image:

```no_run
# use std::sync::Arc;
# use ash::vk;
# use screen_13::driver::DriverError;
# use screen_13::driver::device::{Device, DeviceInfo};
# use screen_13::driver::image::{ImageInfo};
# use screen_13::pool::{Pool};
# use screen_13::pool::lazy::{LazyPool};
# fn main() -> Result<(), DriverError> {
# let device = Arc::new(Device::create_headless(DeviceInfo::default())?);
let mut pool = LazyPool::new(&device);

let info = ImageInfo::image_2d(8, 8, vk::Format::R8G8B8A8_UNORM, vk::ImageUsageFlags::STORAGE);
let my_image = pool.lease(info)?;
# Ok(()) }
```

# Render Graph Operations

All rendering in _Screen 13_ is performed using a [`RenderGraph`] composed of user-specified passes,
which may include pipelines and read/write access to resources. Recorded passes are automatically
optimized before submission to the graphics hardware.

Some notes about the awesome render pass optimization which was _totally stolen_ from [Granite]:

- Scheduling: passes are submitted to the Vulkan API using batches designed for low-latency
- Re-ordering: passes are shuffled using a heuristic which gives the GPU more time to complete work
- Merging: compatible passes are merged into dynamic subpasses when it is more efficient (_on-tile
  rendering_)
- Aliasing: resources and pipelines are optimized to emit minimal barriers per unit of work (_max
  one, typically zero_)

## Nodes

Resources may be directly bound to a render graph. During the time a resource is bound we refer to
it as a node. Bound nodes may only be used with the graphs they were bound to. Nodes implement
`Copy` to make using them easier.

```no_run
# use std::sync::Arc;
# use ash::vk;
# use screen_13::driver::DriverError;
# use screen_13::driver::device::{Device, DeviceInfo};
# use screen_13::driver::buffer::{Buffer, BufferInfo};
# use screen_13::driver::image::{Image, ImageInfo};
# use screen_13::graph::RenderGraph;
# use screen_13::pool::{Pool};
# use screen_13::pool::lazy::{LazyPool};
# fn main() -> Result<(), DriverError> {
# let device = Arc::new(Device::create_headless(DeviceInfo::default())?);
# let info = BufferInfo::host_mem(1024, vk::BufferUsageFlags::STORAGE_BUFFER);
# let buffer = Buffer::create(&device, info)?;
# let info = ImageInfo::image_2d(8, 8, vk::Format::R8G8B8A8_UNORM, vk::ImageUsageFlags::STORAGE);
# let image = Image::create(&device, info)?;
# let mut graph = RenderGraph::new();
println!("{:?}", buffer); // Buffer
println!("{:?}", image); // Image

// Bind our resources into opaque "usize" nodes
let buffer = graph.bind_node(buffer);
let image = graph.bind_node(image);

// The results have unique types!
println!("{:?}", buffer); // BufferNode
println!("{:?}", image); // ImageNode

// Unbind nodes back into resources (Optional!)
let buffer = graph.unbind_node(buffer);
let image = graph.unbind_node(image);

// Magically, they return to the correct types! (the graph wrapped them in Arc for us)
println!("{:?}", buffer); // Arc<Buffer>
println!("{:?}", image); // Arc<Image>
# Ok(()) }
```

_Note:_ See [this code](https://github.com/attackgoat/screen-13/blob/master/src/graph/edge.rs#L34)
for all the things that can be bound or unbound from a graph.

_Note:_ Once unbound, the node is invalid and should be dropped.

## Access and synchronization

Render graphs and their passes contain a set of functions used to handle Vulkan synchronization with
prefixes of `access`, `read`, or `write`. For each resource used in a computing, graphics subpass,
ray tracing, or general command buffer you must call an access function. Generally choose a `read`
or `write` function unless you want to be most efficient.

Example:

```no_run
# use std::sync::Arc;
# use ash::vk;
# use screen_13::driver::DriverError;
# use screen_13::driver::device::{Device, DeviceInfo};
# use screen_13::driver::buffer::{Buffer, BufferInfo};
# use screen_13::driver::image::{Image, ImageInfo};
# use screen_13::graph::RenderGraph;
# use screen_13::pool::{Pool};
# use screen_13::pool::lazy::{LazyPool};
# fn main() -> Result<(), DriverError> {
# let device = Arc::new(Device::create_headless(DeviceInfo::default())?);
# let info = BufferInfo::host_mem(1024, vk::BufferUsageFlags::STORAGE_BUFFER);
# let buffer = Buffer::create(&device, info)?;
# let info = ImageInfo::image_2d(8, 8, vk::Format::R8G8B8A8_UNORM, vk::ImageUsageFlags::STORAGE);
# let image = Image::create(&device, info)?;
let mut graph = RenderGraph::new();
let buffer_node = graph.bind_node(buffer);
let image_node = graph.bind_node(image);
graph
    .begin_pass("Do some raw Vulkan or interop with another Vulkan library")
    .record_cmd_buf(move |device, cmd_buf, bindings| unsafe {
        // I always run first!
    })
    .read_node(buffer_node) // <-- These two functions, read_node/write_node, completely
    .write_node(image_node) //     handle vulkan synchronization.
    .record_cmd_buf(move |device, cmd_buf, bindings| unsafe {
        // device is &ash::Device
        // cmd_buf is vk::CommandBuffer
        // bindings is a magical object you can retrieve the Vulkan resource from
        let vk_buffer: vk::Buffer = *bindings[buffer_node];
        let vk_image: vk::Image = *bindings[image_node];

        // You are free to READ vk_buffer and WRITE vk_image!
    });
# Ok(()) }
```

## Shader pipelines

Pipeline instances may be bound to a [`PassRef`] in order to execute the associated shader code:

```no_run
# use std::sync::Arc;
# use ash::vk;
# use screen_13::driver::DriverError;
# use screen_13::driver::device::{Device, DeviceInfo};
# use screen_13::driver::compute::{ComputePipeline, ComputePipelineInfo};
# use screen_13::driver::shader::{Shader};
# use screen_13::graph::RenderGraph;
# fn main() -> Result<(), DriverError> {
# let device = Arc::new(Device::create_headless(DeviceInfo::new())?);
# let my_shader_code = [0u8; 1];
# let info = ComputePipelineInfo::default();
# let shader = Shader::new_compute(my_shader_code.as_slice());
# let my_compute_pipeline = Arc::new(ComputePipeline::create(&device, info, shader)?);
# let mut graph = RenderGraph::new();
graph
    .begin_pass("My compute pass")
    .bind_pipeline(&my_compute_pipeline)
    .record_compute(|compute, _| {
        compute.push_constants(&42u32.to_ne_bytes())
               .dispatch(128, 1, 1);
    });
# Ok(()) }
```

## Image samplers

By default, _Screen 13_ will use "linear repeat-mode" samplers unless a special suffix appears as
part of the name within GLSL or HLSL shader code. The `_sampler_123` suffix should be used where
`1`, `2`, and `3` are replaced with:

1. `l` for `LINEAR` texel filtering (default) or `n` for `NEAREST`
1. `l` (default) or `n`, as above, but for mipmap filtering
1. Addressing mode where:
    - `b` is `CLAMP_TO_BORDER`
    - `e` is `CLAMP_TO_EDGE`
    - `m` is `MIRRORED_REPEAT`
    - `r` is `REPEAT`

For example, the following sampler named `pages_sampler_nnr` specifies nearest texel/mipmap modes and repeat addressing:

```glsl
layout(set = 0, binding = 0) uniform sampler2D pages_sampler_nnr[NUM_PAGES];
```

For more complex image sampling, use [`ShaderBuilder::image_sampler`] to specify the exact image
sampling mode.

## Vertex input

Optional name suffixes are used in the same way with vertex input as with image samplers. The
additional attribution of your shader code is optional but may help in a few scenarios:

- Per-instance vertex rate data
- Multiple vertex buffer binding indexes

The data for vertex input is assumed to be per-vertex and bound to vertex buffer binding index zero.
Add `_ibindX` for per-instance data, or the matching `_vbindX` for per-vertex data where `X` is
replaced with the vertex buffer binding index in each case.

For more complex vertex layouts, use the [`ShaderBuilder::vertex_input`] to specify the exact
layout.

[`AccelerationStructureInfo`]: driver::accel_struct::AccelerationStructureInfo
[`AccelerationStructure::create`]: driver::accel_struct::AccelerationStructure::create
[`Buffer::create`]: driver::buffer::Buffer::create
[`Buffer::create_from_slice`]: driver::buffer::Buffer::create_from_slice
[`BufferInfo`]: driver::buffer::BufferInfo
[`ComputePipeline::create`]: driver::compute::ComputePipeline::create
[`ComputePipelineInfo`]: driver::compute::ComputePipelineInfo
[`Device`]: driver::device::Device
[`EventLoop`]: EventLoop
[`FrameContext`]: FrameContext
[Granite]: https://github.com/Themaister/Granite
[`GraphicPipeline::create`]: driver::graphic::GraphicPipeline::create
[`GraphicPipelineInfo`]: driver::graphic::GraphicPipelineInfo
[`Image::create`]: driver::image::Image::create
[`ImageInfo`]: driver::image::ImageInfo
[`PassRef`]: graph::pass_ref::PassRef
[`RayTracePipeline::create`]: driver::ray_trace::RayTracePipeline::create
[`RayTracePipelineInfo`]: driver::ray_trace::RayTracePipelineInfo
[`RenderGraph`]: graph::RenderGraph
[`ShaderBuilder::image_sampler`]: driver::shader::ShaderBuilder::image_sampler
[`ShaderBuilder::vertex_input`]: driver::shader::ShaderBuilder::vertex_input

*/

#![warn(missing_docs)]

pub mod driver;
pub mod graph;
pub mod pool;

mod display;
mod event_loop;
mod frame;

/// Things which are used in almost every single _Screen 13_ program.
pub mod prelude {
    pub use {
        super::{
            display::{Display, DisplayError, ResolverPool},
            driver::{
                accel_struct::{
                    AccelerationStructure, AccelerationStructureGeometry,
                    AccelerationStructureGeometryData, AccelerationStructureGeometryInfo,
                    AccelerationStructureInfo, AccelerationStructureInfoBuilder,
                    AccelerationStructureSize, DeviceOrHostAddress,
                },
                buffer::{Buffer, BufferInfo, BufferInfoBuilder, BufferSubresource},
                compute::{ComputePipeline, ComputePipelineInfo, ComputePipelineInfoBuilder},
                device::{Device, DeviceInfo, DeviceInfoBuilder},
                graphic::{
                    BlendMode, BlendModeBuilder, DepthStencilMode, DepthStencilModeBuilder,
                    GraphicPipeline, GraphicPipelineInfo, GraphicPipelineInfoBuilder, StencilMode,
                },
                image::{
                    Image, ImageInfo, ImageInfoBuilder, ImageSubresource, ImageType, ImageViewInfo,
                    ImageViewInfoBuilder, SampleCount,
                },
                physical_device::{
                    AccelerationStructureProperties, PhysicalDevice, RayQueryFeatures,
                    RayTraceFeatures, RayTraceProperties, Vulkan10Features, Vulkan10Limits,
                    Vulkan10Properties, Vulkan11Features, Vulkan11Properties, Vulkan12Features,
                    Vulkan12Properties,
                },
                ray_trace::{
                    RayTracePipeline, RayTracePipelineInfo, RayTracePipelineInfoBuilder,
                    RayTraceShaderGroup, RayTraceShaderGroupType,
                },
                render_pass::ResolveMode,
                shader::{
                    SamplerInfo, SamplerInfoBuilder, Shader, ShaderBuilder, ShaderCode,
                    SpecializationInfo,
                },
                surface::Surface,
                swapchain::{
                    Swapchain, SwapchainError, SwapchainImage, SwapchainInfo, SwapchainInfoBuilder,
                },
                AccessType, CommandBuffer, DriverError, Instance,
            },
            event_loop::{EventLoop, EventLoopBuilder, FullscreenMode},
            frame::{center_cursor, set_cursor_position, FrameContext},
            graph::{
                node::{
                    AccelerationStructureLeaseNode, AccelerationStructureNode,
                    AnyAccelerationStructureNode, AnyBufferNode, AnyImageNode, BufferLeaseNode,
                    BufferNode, ImageLeaseNode, ImageNode, SwapchainImageNode,
                },
                pass_ref::{PassRef, PipelinePassRef},
                Bind, ClearColorValue, RenderGraph, Unbind,
            },
            pool::{
                alias::{Alias, AliasPool},
                fifo::FifoPool,
                hash::HashPool,
                lazy::LazyPool,
                Lease, Pool, PoolInfo, PoolInfoBuilder,
            },
        },
        ash::vk,
        log::{debug, error, info, logger, trace, warn}, // Everyone wants a log
        winit::{
            self,
            dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize},
            event::{Event, WindowEvent},
            keyboard::KeyCode,
            monitor::{MonitorHandle, VideoMode},
            window::{
                BadIcon, CursorGrabMode, CursorIcon, Fullscreen, Icon, Theme, UserAttentionType,
                Window, WindowBuilder, WindowId,
            },
        },
    };
}

pub use self::{
    display::{Display, DisplayError, ResolverPool},
    event_loop::{EventLoop, EventLoopBuilder, FullscreenMode},
    frame::FrameContext,
};