• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_VULKAN_BUFFERVK_H_
16 #define DAWNNATIVE_VULKAN_BUFFERVK_H_
17 
18 #include "dawn_native/Buffer.h"
19 
20 #include "common/SerialQueue.h"
21 #include "common/vulkan_platform.h"
22 #include "dawn_native/ResourceMemoryAllocation.h"
23 
24 namespace dawn_native { namespace vulkan {
25 
26     struct CommandRecordingContext;
27     class Device;
28 
29     class Buffer final : public BufferBase {
30       public:
31         static ResultOrError<Ref<Buffer>> Create(Device* device,
32                                                  const BufferDescriptor* descriptor);
33 
34         VkBuffer GetHandle() const;
35 
36         // Transitions the buffer to be used as `usage`, recording any necessary barrier in
37         // `commands`.
38         // TODO(crbug.com/dawn/851): coalesce barriers and do them early when possible.
39         void TransitionUsageNow(CommandRecordingContext* recordingContext, wgpu::BufferUsage usage);
40         bool TransitionUsageAndGetResourceBarrier(wgpu::BufferUsage usage,
41                                                   VkBufferMemoryBarrier* barrier,
42                                                   VkPipelineStageFlags* srcStages,
43                                                   VkPipelineStageFlags* dstStages);
44 
45         // All the Ensure methods return true if the buffer was initialized to zero.
46         bool EnsureDataInitialized(CommandRecordingContext* recordingContext);
47         bool EnsureDataInitializedAsDestination(CommandRecordingContext* recordingContext,
48                                                 uint64_t offset,
49                                                 uint64_t size);
50         bool EnsureDataInitializedAsDestination(CommandRecordingContext* recordingContext,
51                                                 const CopyTextureToBufferCmd* copy);
52 
53         // Dawn API
54         void SetLabelImpl() override;
55 
56       private:
57         ~Buffer() override;
58         using BufferBase::BufferBase;
59 
60         MaybeError Initialize(bool mappedAtCreation);
61         void InitializeToZero(CommandRecordingContext* recordingContext);
62         void ClearBuffer(CommandRecordingContext* recordingContext,
63                          uint32_t clearValue,
64                          uint64_t offset = 0,
65                          uint64_t size = 0);
66 
67         MaybeError MapAsyncImpl(wgpu::MapMode mode, size_t offset, size_t size) override;
68         void UnmapImpl() override;
69         void DestroyImpl() override;
70         bool IsCPUWritableAtCreation() const override;
71         MaybeError MapAtCreationImpl() override;
72         void* GetMappedPointerImpl() override;
73 
74         VkBuffer mHandle = VK_NULL_HANDLE;
75         ResourceMemoryAllocation mMemoryAllocation;
76 
77         wgpu::BufferUsage mLastUsage = wgpu::BufferUsage::None;
78     };
79 
80 }}  // namespace dawn_native::vulkan
81 
82 #endif  // DAWNNATIVE_VULKAN_BUFFERVK_H_
83