blob: d6e2af896773930601706aa8f3e82682c93d0fab [file] [log] [blame]
Lionel Landwerlin2d9f5632022-01-08 01:12:47 +02001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
4 * Copyright (C) 2015-2022 Google Inc.
Jeremy Gebben1dfbd172021-05-19 14:00:58 -06005 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Courtney Goeltzenleuchter <courtneygo@google.com>
20 * Author: Tobin Ehlis <tobine@google.com>
21 * Author: Chris Forbes <chrisf@ijw.co.nz>
22 * Author: Mark Lobodzinski <mark@lunarg.com>
23 * Author: Dave Houlton <daveh@lunarg.com>
24 * Author: John Zulauf <jzulauf@lunarg.com>
25 * Author: Tobias Hector <tobias.hector@amd.com>
26 * Author: Jeremy Gebben <jeremyg@lunarg.com>
27 */
28#pragma once
29#include "device_memory_state.h"
Jeremy Gebbenc1063462022-02-11 09:31:18 -070030#include "range_vector.h"
Jeremy Gebben1dfbd172021-05-19 14:00:58 -060031
Jeremy Gebbenc8937b62021-09-24 15:50:56 -060032class ValidationStateTracker;
33
Jeremy Gebben1dfbd172021-05-19 14:00:58 -060034class BUFFER_STATE : public BINDABLE {
35 public:
Jeremy Gebbenf2912cd2021-07-07 07:57:39 -060036 const safe_VkBufferCreateInfo safe_create_info;
37 const VkBufferCreateInfo &createInfo;
Jeremy Gebben1dfbd172021-05-19 14:00:58 -060038 VkDeviceAddress deviceAddress;
Jeremy Gebbenc8937b62021-09-24 15:50:56 -060039 const VkMemoryRequirements requirements;
Aitor Camacho3294edd2022-05-16 22:34:19 +020040 const VkMemoryRequirements *const memory_requirements_pointer = &requirements;
Jeremy Gebben6fbf8242021-06-21 09:14:46 -060041 bool memory_requirements_checked;
42
Jeremy Gebbenc8937b62021-09-24 15:50:56 -060043 BUFFER_STATE(ValidationStateTracker *dev_data, VkBuffer buff, const VkBufferCreateInfo *pCreateInfo);
Jeremy Gebben1dfbd172021-05-19 14:00:58 -060044
45 BUFFER_STATE(BUFFER_STATE const &rh_obj) = delete;
46
47 VkBuffer buffer() const { return handle_.Cast<VkBuffer>(); }
48
Jeremy Gebbenc1063462022-02-11 09:31:18 -070049 sparse_container::range<VkDeviceAddress> DeviceAddressRange() const {
50 return {deviceAddress, deviceAddress + createInfo.size};
51 }
Jeremy Gebben1dfbd172021-05-19 14:00:58 -060052};
53
Aitor Camacho3294edd2022-05-16 22:34:19 +020054using BUFFER_STATE_LINEAR = MEMORY_TRACKED_RESOURCE_STATE<BUFFER_STATE, BindableLinearMemoryTracker>;
55template <bool IS_RESIDENT>
56using BUFFER_STATE_SPARSE = MEMORY_TRACKED_RESOURCE_STATE<BUFFER_STATE, BindableSparseMemoryTracker<IS_RESIDENT>>;
57
Tony-LunarGffb5b522022-06-15 15:49:27 -060058#ifdef VK_USE_PLATFORM_METAL_EXT
59static bool GetMetalExport(const VkBufferViewCreateInfo *info) {
60 bool retval = false;
61 auto export_metal_object_info = LvlFindInChain<VkExportMetalObjectCreateInfoEXT>(info->pNext);
62 while (export_metal_object_info) {
63 if (export_metal_object_info->exportObjectType == VK_EXPORT_METAL_OBJECT_TYPE_METAL_TEXTURE_BIT_EXT) {
64 retval = true;
65 break;
66 }
67 export_metal_object_info = LvlFindInChain<VkExportMetalObjectCreateInfoEXT>(export_metal_object_info->pNext);
68 }
69 return retval;
70}
71#endif
72
Jeremy Gebben1dfbd172021-05-19 14:00:58 -060073class BUFFER_VIEW_STATE : public BASE_NODE {
74 public:
Jeremy Gebbenf2912cd2021-07-07 07:57:39 -060075 const VkBufferViewCreateInfo create_info;
Jeremy Gebben1dfbd172021-05-19 14:00:58 -060076 std::shared_ptr<BUFFER_STATE> buffer_state;
Tony-LunarGffb5b522022-06-15 15:49:27 -060077#ifdef VK_USE_PLATFORM_METAL_EXT
78 const bool metal_bufferview_export;
79#endif // VK_USE_PLATFORM_METAL_EXT
ziga-lunarg7b29e662022-08-29 02:32:48 +020080 // Format features that matter when accessing the buffer
81 // both as a buffer (ex OpLoad) or image (ex OpImageWrite)
Lionel Landwerlin519100a2022-05-12 17:23:58 +030082 const VkFormatFeatureFlags2KHR buf_format_features;
Jeremy Gebbenf2912cd2021-07-07 07:57:39 -060083
Tony-LunarGffb5b522022-06-15 15:49:27 -060084
Jeremy Gebbenf2912cd2021-07-07 07:57:39 -060085 BUFFER_VIEW_STATE(const std::shared_ptr<BUFFER_STATE> &bf, VkBufferView bv, const VkBufferViewCreateInfo *ci,
ziga-lunarg7b29e662022-08-29 02:32:48 +020086 VkFormatFeatureFlags2KHR buf_ff)
Lionel Landwerlin519100a2022-05-12 17:23:58 +030087 : BASE_NODE(bv, kVulkanObjectTypeBufferView),
88 create_info(*ci),
89 buffer_state(bf),
Tony-LunarGffb5b522022-06-15 15:49:27 -060090#ifdef VK_USE_PLATFORM_METAL_EXT
91 metal_bufferview_export(GetMetalExport(ci)),
92#endif
ziga-lunarg7b29e662022-08-29 02:32:48 +020093 buf_format_features(buf_ff) {}
Jeremy Gebben610d3a62022-01-01 12:53:17 -070094
Tony-LunarGffb5b522022-06-15 15:49:27 -060095
Jeremy Gebben610d3a62022-01-01 12:53:17 -070096 void LinkChildNodes() override {
97 // Connect child node(s), which cannot safely be done in the constructor.
98 buffer_state->AddParent(this);
Jeremy Gebben1dfbd172021-05-19 14:00:58 -060099 }
Jeremy Gebben6fbf8242021-06-21 09:14:46 -0600100 virtual ~BUFFER_VIEW_STATE() {
101 if (!Destroyed()) {
102 Destroy();
103 }
104 }
Jeremy Gebben1dfbd172021-05-19 14:00:58 -0600105
106 BUFFER_VIEW_STATE(const BUFFER_VIEW_STATE &rh_obj) = delete;
107
108 VkBufferView buffer_view() const { return handle_.Cast<VkBufferView>(); }
109
110 void Destroy() override {
111 if (buffer_state) {
112 buffer_state->RemoveParent(this);
Jeremy Gebbenf2912cd2021-07-07 07:57:39 -0600113 buffer_state = nullptr;
Jeremy Gebben1dfbd172021-05-19 14:00:58 -0600114 }
115 BASE_NODE::Destroy();
116 }
Jeremy Gebbena08da232022-02-01 15:14:52 -0700117 bool Invalid() const override { return Destroyed() || !buffer_state || buffer_state->Invalid(); }
Jeremy Gebben1dfbd172021-05-19 14:00:58 -0600118};