textured.pipeline.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#pragma once


#include <planet/affine/point3d.hpp>
#include <planet/telemetry/minmax.hpp>
#include <planet/vk/engine/app.hpp>
#include <planet/vk/engine/render_parameters.hpp>


namespace planet::vk::engine::pipeline {

Textured triangle pipeline

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
    class textured final : private telemetry::id {
      public:
        textured(
                engine::renderer &r,
                std::string_view const vertex_shader,
                std::uint32_t const textures_per_frame = 256)
        : textured{
                  "planet_vk_engine_pipeline_textured", r, vertex_shader,
                  textures_per_frame, id::suffix::yes} {}
        textured(
                std::string_view,
                engine::renderer &,
                std::string_view vertex_shader,
                std::uint32_t textures_per_frame = 256,
                id::suffix = id::suffix::no);

        vk::descriptor_set_layout texture_layout;
        vk::graphics_pipeline pipeline;


        struct pos {
            float x = {}, y = {};
            friend constexpr pos operator+(pos const l, pos const r) {
                return {l.x + r.x, l.y + r.y};
            }
        };
        struct vertex {
            planet::affine::point3d p;
            colour col = colour::white;
            pos uv;
        };

Texture data to be drawn

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        class data {
            friend class textured;
            std::vector<vertex> vertices;
            std::vector<std::uint32_t> indices;
            std::vector<VkDescriptorImageInfo> textures;


          public:
            void clear() {
                vertices.clear();
                indices.clear();
                textures.clear();
            }
            [[nodiscard]] bool empty() const noexcept {
                return vertices.empty();
            }

2D Z layer height

67
            float z_layer = 0.75f;

Drawing API

Draw texture stretched to the axis aligned rectangle

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
            void
                    draw(vk::texture const &t,
                         affine::rectangle2d const &r,
                         colour const &c,
                         float const z) {
                draw({t, {{0, 0}, affine::extents2d{1, 1}}}, r, c, z);
            }
            void
                    draw(vk::texture const &t,
                         affine::rectangle2d const &r,
                         colour const &c = colour::white) {
                draw({t, {{0, 0}, affine::extents2d{1, 1}}}, r, c, z_layer);
            }
            void
                    draw(vk::sub_texture const &,
                         affine::rectangle2d const &,
                         colour const &,
                         float z);
            void
                    draw(vk::sub_texture const &t,
                         affine::rectangle2d const &r,
                         colour const &c = colour::white) {
                draw(t, r, c, z_layer);
            }

Draw a textured mesh

 98
 99
100
101
102
103
104
105
            void
                    draw(std::span<vertex const>,
                         std::span<std::uint32_t const>,
                         std::span<VkDescriptorImageInfo const>);
            void draw(data const &d) {
                draw(d.vertices, d.indices, d.textures);
            }
        };

This frame's draw data and commands

109
        data this_frame;

Add draw commands to command buffer

113
114
115
116
117
118
119
120
121
122
123
        void render(render_parameters);


      private:
        std::uint32_t max_textures_per_frame;

        vk::descriptor_pool texture_pool;
        std::array<vk::descriptor_sets, max_frames_in_flight> texture_sets;

        std::array<buffer<vertex>, max_frames_in_flight> vertex_buffers;
        std::array<buffer<std::uint32_t>, max_frames_in_flight> index_buffers;

Performance counters

127
128
129
130
131
        telemetry::max textures_in_frame;
    };


}