Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 1 | // Copyright 2020 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 | #include "dawn_native/metal/CommandRecordingContext.h" |
| 16 | |
| 17 | #include "common/Assert.h" |
| 18 | |
| 19 | namespace dawn_native { namespace metal { |
| 20 | |
| 21 | CommandRecordingContext::CommandRecordingContext() = default; |
| 22 | |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 23 | CommandRecordingContext::~CommandRecordingContext() { |
| 24 | // Commands must be acquired. |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 25 | ASSERT(mCommands == nullptr); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | id<MTLCommandBuffer> CommandRecordingContext::GetCommands() { |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 29 | return mCommands.Get(); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Dawn Autoroller | 18f63b4 | 2021-07-21 15:41:29 +0000 | [diff] [blame] | 32 | void CommandRecordingContext::MarkUsed() { |
| 33 | mUsed = true; |
| 34 | } |
| 35 | bool CommandRecordingContext::WasUsed() const { |
| 36 | return mUsed; |
| 37 | } |
| 38 | |
| 39 | MaybeError CommandRecordingContext::PrepareNextCommandBuffer(id<MTLCommandQueue> queue) { |
| 40 | ASSERT(mCommands == nil); |
| 41 | ASSERT(!mUsed); |
| 42 | |
| 43 | // The MTLCommandBuffer will be autoreleased by default. |
| 44 | // The autorelease pool may drain before the command buffer is submitted. Retain so it stays |
| 45 | // alive. |
| 46 | mCommands = AcquireNSPRef([[queue commandBuffer] retain]); |
| 47 | if (mCommands == nil) { |
| 48 | return DAWN_INTERNAL_ERROR("Failed to allocate an MTLCommandBuffer"); |
| 49 | } |
| 50 | |
| 51 | return {}; |
| 52 | } |
| 53 | |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 54 | NSPRef<id<MTLCommandBuffer>> CommandRecordingContext::AcquireCommands() { |
| 55 | // A blit encoder can be left open from WriteBuffer, make sure we close it. |
| 56 | if (mCommands != nullptr) { |
| 57 | EndBlit(); |
Corentin Wallez | d3bbcc3 | 2020-04-16 09:51:26 +0000 | [diff] [blame] | 58 | } |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 59 | |
Corentin Wallez | d3bbcc3 | 2020-04-16 09:51:26 +0000 | [diff] [blame] | 60 | ASSERT(!mInEncoder); |
Dawn Autoroller | 18f63b4 | 2021-07-21 15:41:29 +0000 | [diff] [blame] | 61 | mUsed = false; |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 62 | return std::move(mCommands); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | id<MTLBlitCommandEncoder> CommandRecordingContext::EnsureBlit() { |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 66 | ASSERT(mCommands != nullptr); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 67 | |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 68 | if (mBlit == nullptr) { |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 69 | ASSERT(!mInEncoder); |
| 70 | mInEncoder = true; |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 71 | |
| 72 | // The encoder is created autoreleased. Retain it to avoid the autoreleasepool from |
| 73 | // draining from under us. |
Austin Eng | 700a5fb | 2021-06-24 19:21:31 +0000 | [diff] [blame] | 74 | mBlit.Acquire([[*mCommands blitCommandEncoder] retain]); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 75 | } |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 76 | return mBlit.Get(); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void CommandRecordingContext::EndBlit() { |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 80 | ASSERT(mCommands != nullptr); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 81 | |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 82 | if (mBlit != nullptr) { |
| 83 | [*mBlit endEncoding]; |
| 84 | mBlit = nullptr; |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 85 | mInEncoder = false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | id<MTLComputeCommandEncoder> CommandRecordingContext::BeginCompute() { |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 90 | ASSERT(mCommands != nullptr); |
| 91 | ASSERT(mCompute == nullptr); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 92 | ASSERT(!mInEncoder); |
| 93 | |
| 94 | mInEncoder = true; |
Austin Eng | 700a5fb | 2021-06-24 19:21:31 +0000 | [diff] [blame] | 95 | // The encoder is created autoreleased. Retain it to avoid the autoreleasepool from |
| 96 | // draining from under us. |
| 97 | mCompute.Acquire([[*mCommands computeCommandEncoder] retain]); |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 98 | return mCompute.Get(); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void CommandRecordingContext::EndCompute() { |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 102 | ASSERT(mCommands != nullptr); |
| 103 | ASSERT(mCompute != nullptr); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 104 | |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 105 | [*mCompute endEncoding]; |
| 106 | mCompute = nullptr; |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 107 | mInEncoder = false; |
| 108 | } |
| 109 | |
| 110 | id<MTLRenderCommandEncoder> CommandRecordingContext::BeginRender( |
| 111 | MTLRenderPassDescriptor* descriptor) { |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 112 | ASSERT(mCommands != nullptr); |
| 113 | ASSERT(mRender == nullptr); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 114 | ASSERT(!mInEncoder); |
| 115 | |
| 116 | mInEncoder = true; |
Austin Eng | 700a5fb | 2021-06-24 19:21:31 +0000 | [diff] [blame] | 117 | // The encoder is created autoreleased. Retain it to avoid the autoreleasepool from |
| 118 | // draining from under us. |
| 119 | mRender.Acquire([[*mCommands renderCommandEncoderWithDescriptor:descriptor] retain]); |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 120 | return mRender.Get(); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void CommandRecordingContext::EndRender() { |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 124 | ASSERT(mCommands != nullptr); |
| 125 | ASSERT(mRender != nullptr); |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 126 | |
Corentin Wallez | 0055d95 | 2020-11-16 23:07:56 +0000 | [diff] [blame] | 127 | [*mRender endEncoding]; |
| 128 | mRender = nullptr; |
Austin Eng | 0c66bcd | 2020-01-15 18:22:53 +0000 | [diff] [blame] | 129 | mInEncoder = false; |
| 130 | } |
| 131 | |
| 132 | }} // namespace dawn_native::metal |