2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-07-24 11:53:51 +00:00
|
|
|
#include "dawn_native/CommandBuffer.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-01-16 00:12:10 +00:00
|
|
|
#include "common/BitSetIterator.h"
|
2020-07-28 01:58:50 +00:00
|
|
|
#include "dawn_native/Buffer.h"
|
2019-02-20 11:46:16 +00:00
|
|
|
#include "dawn_native/CommandEncoder.h"
|
2020-01-16 00:12:10 +00:00
|
|
|
#include "dawn_native/Commands.h"
|
|
|
|
#include "dawn_native/Format.h"
|
2019-06-11 18:11:05 +00:00
|
|
|
#include "dawn_native/Texture.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-24 14:45:45 +00:00
|
|
|
namespace dawn_native {
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
CommandBufferBase::CommandBufferBase(CommandEncoder* encoder, const CommandBufferDescriptor*)
|
2019-07-10 20:43:13 +00:00
|
|
|
: ObjectBase(encoder->GetDevice()), mResourceUsages(encoder->AcquireResourceUsages()) {
|
2018-11-07 10:02:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 12:54:08 +00:00
|
|
|
CommandBufferBase::CommandBufferBase(DeviceBase* device, ObjectBase::ErrorTag tag)
|
|
|
|
: ObjectBase(device, tag) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CommandBufferBase* CommandBufferBase::MakeError(DeviceBase* device) {
|
|
|
|
return new CommandBufferBase(device, ObjectBase::kError);
|
|
|
|
}
|
|
|
|
|
2018-11-07 10:02:43 +00:00
|
|
|
const CommandBufferResourceUsage& CommandBufferBase::GetResourceUsages() const {
|
|
|
|
return mResourceUsages;
|
2017-07-12 00:49:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:11:05 +00:00
|
|
|
bool IsCompleteSubresourceCopiedTo(const TextureBase* texture,
|
|
|
|
const Extent3D copySize,
|
|
|
|
const uint32_t mipLevel) {
|
2019-07-02 23:55:55 +00:00
|
|
|
Extent3D extent = texture->GetMipLevelPhysicalSize(mipLevel);
|
2019-06-25 03:09:26 +00:00
|
|
|
|
2020-06-21 09:33:44 +00:00
|
|
|
ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D);
|
|
|
|
if (extent.width == copySize.width && extent.height == copySize.height) {
|
2019-06-11 18:11:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-16 00:12:10 +00:00
|
|
|
|
2020-06-26 11:07:00 +00:00
|
|
|
SubresourceRange GetSubresourcesAffectedByCopy(const TextureCopy& copy,
|
|
|
|
const Extent3D& copySize) {
|
|
|
|
switch (copy.texture->GetDimension()) {
|
|
|
|
case wgpu::TextureDimension::e2D:
|
|
|
|
return {copy.mipLevel, 1, copy.origin.z, copySize.depth};
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 00:12:10 +00:00
|
|
|
void LazyClearRenderPassAttachments(BeginRenderPassCmd* renderPass) {
|
|
|
|
for (uint32_t i : IterateBitSet(renderPass->attachmentState->GetColorAttachmentsMask())) {
|
|
|
|
auto& attachmentInfo = renderPass->colorAttachments[i];
|
|
|
|
TextureViewBase* view = attachmentInfo.view.Get();
|
|
|
|
bool hasResolveTarget = attachmentInfo.resolveTarget.Get() != nullptr;
|
|
|
|
|
|
|
|
ASSERT(view->GetLayerCount() == 1);
|
|
|
|
ASSERT(view->GetLevelCount() == 1);
|
2020-06-12 00:37:31 +00:00
|
|
|
SubresourceRange range = view->GetSubresourceRange();
|
2020-01-16 00:12:10 +00:00
|
|
|
|
|
|
|
// If the loadOp is Load, but the subresource is not initialized, use Clear instead.
|
|
|
|
if (attachmentInfo.loadOp == wgpu::LoadOp::Load &&
|
2020-06-12 00:37:31 +00:00
|
|
|
!view->GetTexture()->IsSubresourceContentInitialized(range)) {
|
2020-01-16 00:12:10 +00:00
|
|
|
attachmentInfo.loadOp = wgpu::LoadOp::Clear;
|
|
|
|
attachmentInfo.clearColor = {0.f, 0.f, 0.f, 0.f};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasResolveTarget) {
|
|
|
|
// We need to set the resolve target to initialized so that it does not get
|
|
|
|
// cleared later in the pipeline. The texture will be resolved from the
|
|
|
|
// source color attachment, which will be correctly initialized.
|
|
|
|
TextureViewBase* resolveView = attachmentInfo.resolveTarget.Get();
|
2020-06-12 00:37:31 +00:00
|
|
|
ASSERT(resolveView->GetLayerCount() == 1);
|
|
|
|
ASSERT(resolveView->GetLevelCount() == 1);
|
2020-01-16 00:12:10 +00:00
|
|
|
resolveView->GetTexture()->SetIsSubresourceContentInitialized(
|
2020-06-12 00:37:31 +00:00
|
|
|
true, resolveView->GetSubresourceRange());
|
2020-01-16 00:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (attachmentInfo.storeOp) {
|
|
|
|
case wgpu::StoreOp::Store:
|
2020-06-12 00:37:31 +00:00
|
|
|
view->GetTexture()->SetIsSubresourceContentInitialized(true, range);
|
2020-01-16 00:12:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case wgpu::StoreOp::Clear:
|
2020-06-12 00:37:31 +00:00
|
|
|
view->GetTexture()->SetIsSubresourceContentInitialized(false, range);
|
2020-01-16 00:12:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (renderPass->attachmentState->HasDepthStencilAttachment()) {
|
|
|
|
auto& attachmentInfo = renderPass->depthStencilAttachment;
|
|
|
|
TextureViewBase* view = attachmentInfo.view.Get();
|
2020-06-12 00:37:31 +00:00
|
|
|
ASSERT(view->GetLayerCount() == 1);
|
|
|
|
ASSERT(view->GetLevelCount() == 1);
|
|
|
|
SubresourceRange range = view->GetSubresourceRange();
|
2020-01-16 00:12:10 +00:00
|
|
|
|
|
|
|
// If the depth stencil texture has not been initialized, we want to use loadop
|
|
|
|
// clear to init the contents to 0's
|
2020-06-12 00:37:31 +00:00
|
|
|
if (!view->GetTexture()->IsSubresourceContentInitialized(range)) {
|
2020-01-16 00:12:10 +00:00
|
|
|
if (view->GetTexture()->GetFormat().HasDepth() &&
|
|
|
|
attachmentInfo.depthLoadOp == wgpu::LoadOp::Load) {
|
|
|
|
attachmentInfo.clearDepth = 0.0f;
|
|
|
|
attachmentInfo.depthLoadOp = wgpu::LoadOp::Clear;
|
|
|
|
}
|
|
|
|
if (view->GetTexture()->GetFormat().HasStencil() &&
|
|
|
|
attachmentInfo.stencilLoadOp == wgpu::LoadOp::Load) {
|
|
|
|
attachmentInfo.clearStencil = 0u;
|
|
|
|
attachmentInfo.stencilLoadOp = wgpu::LoadOp::Clear;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If these have different store ops, make them both Store because we can't track
|
|
|
|
// initialized state separately yet. TODO(crbug.com/dawn/145)
|
|
|
|
if (attachmentInfo.depthStoreOp != attachmentInfo.stencilStoreOp) {
|
|
|
|
attachmentInfo.depthStoreOp = wgpu::StoreOp::Store;
|
|
|
|
attachmentInfo.stencilStoreOp = wgpu::StoreOp::Store;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachmentInfo.depthStoreOp == wgpu::StoreOp::Store &&
|
|
|
|
attachmentInfo.stencilStoreOp == wgpu::StoreOp::Store) {
|
2020-06-12 00:37:31 +00:00
|
|
|
view->GetTexture()->SetIsSubresourceContentInitialized(true, range);
|
2020-01-16 00:12:10 +00:00
|
|
|
} else {
|
|
|
|
ASSERT(attachmentInfo.depthStoreOp == wgpu::StoreOp::Clear &&
|
|
|
|
attachmentInfo.stencilStoreOp == wgpu::StoreOp::Clear);
|
2020-06-12 00:37:31 +00:00
|
|
|
view->GetTexture()->SetIsSubresourceContentInitialized(false, range);
|
2020-01-16 00:12:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 01:58:50 +00:00
|
|
|
// TODO(jiawei.shao@intel.com): support copying with depth stencil textures
|
|
|
|
bool IsFullBufferOverwrittenInTextureToBufferCopy(const CopyTextureToBufferCmd* copy) {
|
|
|
|
ASSERT(copy != nullptr);
|
|
|
|
|
|
|
|
if (copy->destination.offset > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy->destination.rowsPerImage > copy->copySize.height) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TextureBase* texture = copy->source.texture.Get();
|
|
|
|
const uint64_t copyTextureDataSizePerRow = copy->copySize.width /
|
|
|
|
texture->GetFormat().blockWidth *
|
|
|
|
texture->GetFormat().blockByteSize;
|
|
|
|
if (copy->destination.bytesPerRow > copyTextureDataSizePerRow) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint64_t overwrittenRangeSize =
|
|
|
|
copyTextureDataSizePerRow * (copy->copySize.height / texture->GetFormat().blockHeight) *
|
|
|
|
copy->copySize.depth;
|
|
|
|
if (copy->destination.buffer->GetSize() > overwrittenRangeSize) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-24 14:45:45 +00:00
|
|
|
} // namespace dawn_native
|