• 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_D3D12_BUFFERD3D12_H_
16 #define DAWNNATIVE_D3D12_BUFFERD3D12_H_
17 
18 #include "dawn_native/Buffer.h"
19 
20 #include "dawn_native/d3d12/ResourceHeapAllocationD3D12.h"
21 #include "dawn_native/d3d12/d3d12_platform.h"
22 
23 namespace dawn_native { namespace d3d12 {
24 
25     class CommandRecordingContext;
26     class Device;
27 
28     class Buffer final : public BufferBase {
29       public:
30         static ResultOrError<Ref<Buffer>> Create(Device* device,
31                                                  const BufferDescriptor* descriptor);
32 
33         ID3D12Resource* GetD3D12Resource() const;
34         D3D12_GPU_VIRTUAL_ADDRESS GetVA() const;
35 
36         bool TrackUsageAndGetResourceBarrier(CommandRecordingContext* commandContext,
37                                              D3D12_RESOURCE_BARRIER* barrier,
38                                              wgpu::BufferUsage newUsage);
39         void TrackUsageAndTransitionNow(CommandRecordingContext* commandContext,
40                                         wgpu::BufferUsage newUsage);
41 
42         bool CheckAllocationMethodForTesting(AllocationMethod allocationMethod) const;
43         bool CheckIsResidentForTesting() const;
44 
45         MaybeError EnsureDataInitialized(CommandRecordingContext* commandContext);
46         ResultOrError<bool> EnsureDataInitializedAsDestination(
47             CommandRecordingContext* commandContext,
48             uint64_t offset,
49             uint64_t size);
50         MaybeError EnsureDataInitializedAsDestination(CommandRecordingContext* commandContext,
51                                                       const CopyTextureToBufferCmd* copy);
52 
53         // Dawn API
54         void SetLabelImpl() override;
55 
56       private:
57         Buffer(Device* device, const BufferDescriptor* descriptor);
58         ~Buffer() override;
59 
60         MaybeError Initialize(bool mappedAtCreation);
61         MaybeError MapAsyncImpl(wgpu::MapMode mode, size_t offset, size_t size) override;
62         void UnmapImpl() override;
63         void DestroyImpl() override;
64         bool IsCPUWritableAtCreation() const override;
65         virtual MaybeError MapAtCreationImpl() override;
66         void* GetMappedPointerImpl() override;
67 
68         MaybeError MapInternal(bool isWrite, size_t start, size_t end, const char* contextInfo);
69 
70         bool TransitionUsageAndGetResourceBarrier(CommandRecordingContext* commandContext,
71                                                   D3D12_RESOURCE_BARRIER* barrier,
72                                                   wgpu::BufferUsage newUsage);
73 
74         MaybeError InitializeToZero(CommandRecordingContext* commandContext);
75         MaybeError ClearBuffer(CommandRecordingContext* commandContext,
76                                uint8_t clearValue,
77                                uint64_t offset = 0,
78                                uint64_t size = 0);
79 
80         ResourceHeapAllocation mResourceAllocation;
81         bool mFixedResourceState = false;
82         wgpu::BufferUsage mLastUsage = wgpu::BufferUsage::None;
83         ExecutionSerial mLastUsedSerial = std::numeric_limits<ExecutionSerial>::max();
84 
85         D3D12_RANGE mWrittenMappedRange = {0, 0};
86         void* mMappedData = nullptr;
87     };
88 
89 }}  // namespace dawn_native::d3d12
90 
91 #endif  // DAWNNATIVE_D3D12_BUFFERD3D12_H_
92