• 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_METAL_BUFFERMTL_H_
16 #define DAWNNATIVE_METAL_BUFFERMTL_H_
17 
18 #include "common/NSRef.h"
19 #include "common/SerialQueue.h"
20 #include "dawn_native/Buffer.h"
21 
22 #import <Metal/Metal.h>
23 
24 namespace dawn_native { namespace metal {
25 
26     class 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         id<MTLBuffer> GetMTLBuffer() const;
34 
35         bool EnsureDataInitialized(CommandRecordingContext* commandContext);
36         bool EnsureDataInitializedAsDestination(CommandRecordingContext* commandContext,
37                                                 uint64_t offset,
38                                                 uint64_t size);
39         bool EnsureDataInitializedAsDestination(CommandRecordingContext* commandContext,
40                                                 const CopyTextureToBufferCmd* copy);
41 
42         static uint64_t QueryMaxBufferLength(id<MTLDevice> mtlDevice);
43 
44       private:
45         using BufferBase::BufferBase;
46         MaybeError Initialize(bool mappedAtCreation);
47 
48         ~Buffer() override;
49         MaybeError MapAsyncImpl(wgpu::MapMode mode, size_t offset, size_t size) override;
50         void UnmapImpl() override;
51         void DestroyImpl() override;
52         void* GetMappedPointerImpl() override;
53         bool IsCPUWritableAtCreation() const override;
54         MaybeError MapAtCreationImpl() override;
55 
56         void InitializeToZero(CommandRecordingContext* commandContext);
57         void ClearBuffer(CommandRecordingContext* commandContext,
58                          uint8_t clearValue,
59                          uint64_t offset = 0,
60                          uint64_t size = 0);
61 
62         NSPRef<id<MTLBuffer>> mMtlBuffer;
63     };
64 
65 }}  // namespace dawn_native::metal
66 
67 #endif  // DAWNNATIVE_METAL_BUFFERMTL_H_
68