File: buffer_state.h

package info (click to toggle)
vulkan-validationlayers 1.4.321.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,412 kB
  • sloc: cpp: 594,175; python: 11,321; sh: 24; makefile: 20; xml: 14
file content (154 lines) | stat: -rw-r--r-- 5,501 bytes parent folder | download | duplicates (5)
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
/* Copyright (c) 2015-2025 The Khronos Group Inc.
 * Copyright (c) 2015-2025 Valve Corporation
 * Copyright (c) 2015-2025 LunarG, Inc.
 * Copyright (C) 2015-2024 Google Inc.
 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
 * Modifications Copyright (C) 2022 RasterGrid Kft.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once
#include <variant>
#include "state_tracker/device_memory_state.h"
#include "containers/range.h"

namespace vvl {

class BufferSubState;
class BufferViewSubState;
class DeviceState;
class VideoProfileDesc;

class Buffer : public Bindable, public SubStateManager<BufferSubState> {
  public:
    const vku::safe_VkBufferCreateInfo safe_create_info;
    const VkBufferCreateInfo &create_info;

    const VkMemoryRequirements requirements;
    VkDeviceAddress deviceAddress = 0;
    // VkBufferUsageFlags2CreateInfo can be used instead over the VkBufferCreateInfo::usage
    const VkBufferUsageFlags2 usage;

    unordered_set<std::shared_ptr<const VideoProfileDesc>> supported_video_profiles;

    Buffer(DeviceState &dev_data, VkBuffer handle, const VkBufferCreateInfo *pCreateInfo);

    Buffer(Buffer const &rh_obj) = delete;

    // This destructor is needed because Bindable depends on the tracker_ variant defined in this
    // class. So we need to do the Destroy() work before tracker_ is destroyed.
    virtual ~Buffer() {
        if (!Destroyed()) {
            Destroy();
        }
    }

    void Destroy() override;
    void NotifyInvalidate(const StateObject::NodeList &invalid_nodes, bool unlink) override;

    VkBuffer VkHandle() const { return handle_.Cast<VkBuffer>(); }

    VkDeviceSize GetRegionSize(VkDeviceSize offset, VkDeviceSize size) const {
        if (offset < create_info.size) {
            if (size == VK_WHOLE_SIZE) {
                return create_info.size - offset;
            } else if ((offset + size) <= create_info.size) {
                return size;
            }
        }
        return 0;
    }

    static VkDeviceSize GetRegionSize(const std::shared_ptr<const Buffer> &buffer_state, VkDeviceSize offset, VkDeviceSize size) {
        return buffer_state ? buffer_state->GetRegionSize(offset, size) : 0;
    }

    vvl::range<VkDeviceAddress> DeviceAddressRange() const { return {deviceAddress, deviceAddress + create_info.size}; }

    // This function is only used for comparing Imported External Dedicated Memory
    bool CompareCreateInfo(const Buffer &other) const;

  private:
    std::variant<std::monostate, BindableLinearMemoryTracker, BindableSparseMemoryTracker> tracker_;
};

class BufferSubState {
  public:
    explicit BufferSubState(Buffer &buf) : base(buf) {}
    BufferSubState(const BufferSubState &) = delete;
    BufferSubState &operator=(const BufferSubState &) = delete;
    virtual ~BufferSubState() {}
    virtual void Destroy() {}
    virtual void NotifyInvalidate(const StateObject::NodeList &invalid_nodes, bool unlink) {}

    Buffer &base;
};

class BufferView : public StateObject, public SubStateManager<BufferViewSubState> {
  public:
    const vku::safe_VkBufferViewCreateInfo safe_create_info;
    const VkBufferViewCreateInfo &create_info;

    std::shared_ptr<Buffer> buffer_state;
#ifdef VK_USE_PLATFORM_METAL_EXT
    const bool metal_bufferview_export;
#endif  // VK_USE_PLATFORM_METAL_EXT
    // Format features that matter when accessing the buffer
    // both as a buffer (ex OpLoad) or image (ex OpImageWrite)
    const VkFormatFeatureFlags2KHR buffer_format_features;

    BufferView(const std::shared_ptr<Buffer> &bf, VkBufferView handle, const VkBufferViewCreateInfo *pCreateInfo,
               VkFormatFeatureFlags2KHR format_features);

    void LinkChildNodes() override {
        // Connect child node(s), which cannot safely be done in the constructor.
        buffer_state->AddParent(this);
    }
    virtual ~BufferView() {
        if (!Destroyed()) {
            Destroy();
        }
    }

    BufferView(const BufferView &rh_obj) = delete;

    VkBufferView VkHandle() const { return handle_.Cast<VkBufferView>(); }

    void Destroy() override;

    void NotifyInvalidate(const StateObject::NodeList &invalid_nodes, bool unlink) override;

    bool Invalid() const override { return Destroyed() || !buffer_state || buffer_state->Invalid(); }

    VkDeviceSize Size() const {
        VkDeviceSize size = create_info.range;
        if (size == VK_WHOLE_SIZE) {
            size = buffer_state->create_info.size - create_info.offset;
        }
        return size;
    }
};

class BufferViewSubState {
  public:
    explicit BufferViewSubState(BufferView &buf) : base(buf) {}
    BufferViewSubState(const BufferViewSubState &) = delete;
    BufferViewSubState &operator=(const BufferViewSubState &) = delete;
    virtual ~BufferViewSubState() {}
    virtual void Destroy() {}
    virtual void NotifyInvalidate(const StateObject::NodeList &invalid_nodes, bool unlink) {}

    BufferView &base;
};

}  // namespace vvl