Lionel Landwerlin | 2d9f563 | 2022-01-08 01:12:47 +0200 | [diff] [blame] | 1 | /* 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 Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 5 | * 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 Gebben | c106346 | 2022-02-11 09:31:18 -0700 | [diff] [blame] | 30 | #include "range_vector.h" |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 31 | |
Jeremy Gebben | c8937b6 | 2021-09-24 15:50:56 -0600 | [diff] [blame] | 32 | class ValidationStateTracker; |
| 33 | |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 34 | class BUFFER_STATE : public BINDABLE { |
| 35 | public: |
Jeremy Gebben | f2912cd | 2021-07-07 07:57:39 -0600 | [diff] [blame] | 36 | const safe_VkBufferCreateInfo safe_create_info; |
| 37 | const VkBufferCreateInfo &createInfo; |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 38 | VkDeviceAddress deviceAddress; |
Jeremy Gebben | c8937b6 | 2021-09-24 15:50:56 -0600 | [diff] [blame] | 39 | const VkMemoryRequirements requirements; |
Aitor Camacho | 3294edd | 2022-05-16 22:34:19 +0200 | [diff] [blame] | 40 | const VkMemoryRequirements *const memory_requirements_pointer = &requirements; |
Jeremy Gebben | 6fbf824 | 2021-06-21 09:14:46 -0600 | [diff] [blame] | 41 | bool memory_requirements_checked; |
| 42 | |
Jeremy Gebben | c8937b6 | 2021-09-24 15:50:56 -0600 | [diff] [blame] | 43 | BUFFER_STATE(ValidationStateTracker *dev_data, VkBuffer buff, const VkBufferCreateInfo *pCreateInfo); |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 44 | |
| 45 | BUFFER_STATE(BUFFER_STATE const &rh_obj) = delete; |
| 46 | |
| 47 | VkBuffer buffer() const { return handle_.Cast<VkBuffer>(); } |
| 48 | |
Jeremy Gebben | c106346 | 2022-02-11 09:31:18 -0700 | [diff] [blame] | 49 | sparse_container::range<VkDeviceAddress> DeviceAddressRange() const { |
| 50 | return {deviceAddress, deviceAddress + createInfo.size}; |
| 51 | } |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 52 | }; |
| 53 | |
Aitor Camacho | 3294edd | 2022-05-16 22:34:19 +0200 | [diff] [blame] | 54 | using BUFFER_STATE_LINEAR = MEMORY_TRACKED_RESOURCE_STATE<BUFFER_STATE, BindableLinearMemoryTracker>; |
| 55 | template <bool IS_RESIDENT> |
| 56 | using BUFFER_STATE_SPARSE = MEMORY_TRACKED_RESOURCE_STATE<BUFFER_STATE, BindableSparseMemoryTracker<IS_RESIDENT>>; |
| 57 | |
Tony-LunarG | ffb5b52 | 2022-06-15 15:49:27 -0600 | [diff] [blame] | 58 | #ifdef VK_USE_PLATFORM_METAL_EXT |
| 59 | static 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 Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 73 | class BUFFER_VIEW_STATE : public BASE_NODE { |
| 74 | public: |
Jeremy Gebben | f2912cd | 2021-07-07 07:57:39 -0600 | [diff] [blame] | 75 | const VkBufferViewCreateInfo create_info; |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 76 | std::shared_ptr<BUFFER_STATE> buffer_state; |
Tony-LunarG | ffb5b52 | 2022-06-15 15:49:27 -0600 | [diff] [blame] | 77 | #ifdef VK_USE_PLATFORM_METAL_EXT |
| 78 | const bool metal_bufferview_export; |
| 79 | #endif // VK_USE_PLATFORM_METAL_EXT |
ziga-lunarg | 7b29e66 | 2022-08-29 02:32:48 +0200 | [diff] [blame] | 80 | // Format features that matter when accessing the buffer |
| 81 | // both as a buffer (ex OpLoad) or image (ex OpImageWrite) |
Lionel Landwerlin | 519100a | 2022-05-12 17:23:58 +0300 | [diff] [blame] | 82 | const VkFormatFeatureFlags2KHR buf_format_features; |
Jeremy Gebben | f2912cd | 2021-07-07 07:57:39 -0600 | [diff] [blame] | 83 | |
Tony-LunarG | ffb5b52 | 2022-06-15 15:49:27 -0600 | [diff] [blame] | 84 | |
Jeremy Gebben | f2912cd | 2021-07-07 07:57:39 -0600 | [diff] [blame] | 85 | BUFFER_VIEW_STATE(const std::shared_ptr<BUFFER_STATE> &bf, VkBufferView bv, const VkBufferViewCreateInfo *ci, |
ziga-lunarg | 7b29e66 | 2022-08-29 02:32:48 +0200 | [diff] [blame] | 86 | VkFormatFeatureFlags2KHR buf_ff) |
Lionel Landwerlin | 519100a | 2022-05-12 17:23:58 +0300 | [diff] [blame] | 87 | : BASE_NODE(bv, kVulkanObjectTypeBufferView), |
| 88 | create_info(*ci), |
| 89 | buffer_state(bf), |
Tony-LunarG | ffb5b52 | 2022-06-15 15:49:27 -0600 | [diff] [blame] | 90 | #ifdef VK_USE_PLATFORM_METAL_EXT |
| 91 | metal_bufferview_export(GetMetalExport(ci)), |
| 92 | #endif |
ziga-lunarg | 7b29e66 | 2022-08-29 02:32:48 +0200 | [diff] [blame] | 93 | buf_format_features(buf_ff) {} |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 94 | |
Tony-LunarG | ffb5b52 | 2022-06-15 15:49:27 -0600 | [diff] [blame] | 95 | |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 96 | void LinkChildNodes() override { |
| 97 | // Connect child node(s), which cannot safely be done in the constructor. |
| 98 | buffer_state->AddParent(this); |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 99 | } |
Jeremy Gebben | 6fbf824 | 2021-06-21 09:14:46 -0600 | [diff] [blame] | 100 | virtual ~BUFFER_VIEW_STATE() { |
| 101 | if (!Destroyed()) { |
| 102 | Destroy(); |
| 103 | } |
| 104 | } |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 105 | |
| 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 Gebben | f2912cd | 2021-07-07 07:57:39 -0600 | [diff] [blame] | 113 | buffer_state = nullptr; |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 114 | } |
| 115 | BASE_NODE::Destroy(); |
| 116 | } |
Jeremy Gebben | a08da23 | 2022-02-01 15:14:52 -0700 | [diff] [blame] | 117 | bool Invalid() const override { return Destroyed() || !buffer_state || buffer_state->Invalid(); } |
Jeremy Gebben | 1dfbd17 | 2021-05-19 14:00:58 -0600 | [diff] [blame] | 118 | }; |