// Copyright 2018 The Dawn Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef DAWNNATIVE_METAL_DEVICEMTL_H_ #define DAWNNATIVE_METAL_DEVICEMTL_H_ #include "dawn_native/dawn_platform.h" #include "dawn_native/Commands.h" #include "dawn_native/Device.h" #include "dawn_native/metal/CommandRecordingContext.h" #include "dawn_native/metal/Forward.h" #import #import #import #include #include #include namespace dawn_native { namespace metal { class Device : public DeviceBase { public: static ResultOrError Create(AdapterBase* adapter, NSPRef> mtlDevice, const DeviceDescriptor* descriptor); ~Device() override; MaybeError Initialize(); CommandBufferBase* CreateCommandBuffer(CommandEncoder* encoder, const CommandBufferDescriptor* descriptor) override; MaybeError TickImpl() override; id GetMTLDevice(); id GetMTLQueue(); CommandRecordingContext* GetPendingCommandContext(); void SubmitPendingCommandBuffer(); TextureBase* CreateTextureWrappingIOSurface(const ExternalImageDescriptor* descriptor, IOSurfaceRef ioSurface, uint32_t plane); void WaitForCommandsToBeScheduled(); ResultOrError> CreateStagingBuffer(size_t size) override; MaybeError CopyFromStagingToBuffer(StagingBufferBase* source, uint64_t sourceOffset, BufferBase* destination, uint64_t destinationOffset, uint64_t size) override; MaybeError CopyFromStagingToTexture(const StagingBufferBase* source, const TextureDataLayout& dataLayout, TextureCopy* dst, const Extent3D& copySizePixels) override; uint32_t GetOptimalBytesPerRowAlignment() const override; uint64_t GetOptimalBufferToTextureCopyOffsetAlignment() const override; private: Device(AdapterBase* adapter, NSPRef> mtlDevice, const DeviceDescriptor* descriptor); ResultOrError CreateBindGroupImpl( const BindGroupDescriptor* descriptor) override; ResultOrError CreateBindGroupLayoutImpl( const BindGroupLayoutDescriptor* descriptor) override; ResultOrError> CreateBufferImpl( const BufferDescriptor* descriptor) override; ResultOrError CreateComputePipelineImpl( const ComputePipelineDescriptor* descriptor) override; ResultOrError CreatePipelineLayoutImpl( const PipelineLayoutDescriptor* descriptor) override; ResultOrError CreateQuerySetImpl( const QuerySetDescriptor* descriptor) override; ResultOrError CreateRenderPipelineImpl( const RenderPipelineDescriptor* descriptor) override; ResultOrError CreateSamplerImpl(const SamplerDescriptor* descriptor) override; ResultOrError CreateShaderModuleImpl( const ShaderModuleDescriptor* descriptor) override; ResultOrError CreateSwapChainImpl( const SwapChainDescriptor* descriptor) override; ResultOrError CreateSwapChainImpl( Surface* surface, NewSwapChainBase* previousSwapChain, const SwapChainDescriptor* descriptor) override; ResultOrError> CreateTextureImpl( const TextureDescriptor* descriptor) override; ResultOrError CreateTextureViewImpl( TextureBase* texture, const TextureViewDescriptor* descriptor) override; void InitTogglesFromDriver(); void ShutDownImpl() override; MaybeError WaitForIdleForDestruction() override; ExecutionSerial CheckAndUpdateCompletedSerials() override; NSPRef> mMtlDevice; NSPRef> mCommandQueue; CommandRecordingContext mCommandContext; // The completed serial is updated in a Metal completion handler that can be fired on a // different thread, so it needs to be atomic. std::atomic mCompletedSerial; // mLastSubmittedCommands will be accessed in a Metal schedule handler that can be fired on // a different thread so we guard access to it with a mutex. std::mutex mLastSubmittedCommandsMutex; NSPRef> mLastSubmittedCommands; }; }} // namespace dawn_native::metal #endif // DAWNNATIVE_METAL_DEVICEMTL_H_