dawn-cmake/src/dawn_native/CommandBuffer.cpp

209 lines
8.2 KiB
C++
Raw Normal View History

// Copyright 2017 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.
2018-07-24 11:53:51 +00:00
#include "dawn_native/CommandBuffer.h"
#include "common/BitSetIterator.h"
#include "dawn_native/Buffer.h"
#include "dawn_native/CommandEncoder.h"
#include "dawn_native/Commands.h"
#include "dawn_native/Format.h"
#include "dawn_native/Texture.h"
namespace dawn_native {
CommandBufferBase::CommandBufferBase(CommandEncoder* encoder, const CommandBufferDescriptor*)
: ObjectBase(encoder->GetDevice()),
mCommands(encoder->AcquireCommands()),
mResourceUsages(encoder->AcquireResourceUsages()) {
}
CommandBufferBase::CommandBufferBase(DeviceBase* device, ObjectBase::ErrorTag tag)
: ObjectBase(device, tag) {
}
CommandBufferBase::~CommandBufferBase() {
Destroy();
}
// static
CommandBufferBase* CommandBufferBase::MakeError(DeviceBase* device) {
return new CommandBufferBase(device, ObjectBase::kError);
}
MaybeError CommandBufferBase::ValidateCanUseInSubmitNow() const {
ASSERT(!IsError());
if (mDestroyed) {
return DAWN_VALIDATION_ERROR("Command buffer reused in submit");
}
return {};
}
void CommandBufferBase::Destroy() {
FreeCommands(&mCommands);
mResourceUsages = {};
mDestroyed = true;
}
const CommandBufferResourceUsage& CommandBufferBase::GetResourceUsages() const {
return mResourceUsages;
}
bool IsCompleteSubresourceCopiedTo(const TextureBase* texture,
const Extent3D copySize,
const uint32_t mipLevel) {
Support BC5 formats on Vulkan This patch supports BC5 formats on Vulkan backends and adds related Dawn end2end tests. For the textures with BC formats, they could have non-multiple-of-4 sizes on the non-zero mipmap levels in sampling, but we are still required to provide texture data in complete 4x4 blocks in texture copies because that is the size of which they are stored in GPU memory. In this patch, we refer the term "physical memory size" as the memory size of the texture subresource in GPU memory, and the term "virtual memory size" as the size used in texture sampling. As Dawn requires the Extent3D in texture copies must fit in the physical memory size, while Vulkan requires it must fit in the virtual memory size, this patch recalculates the imageExtent to ensure it always follow this Vulkan validation rules. For Dawn end2end tests, note that we use pure green and pure red for the textures because BC5 does not support SRGB formats. Furthermore, "CopyPartofTextureSubResourceIntoNonZeroMipmapLevel" is skipped in this patch because there is an issue on the T2T copies from a region within the virtual size of one texture to another one that exceeds the virtual size of another texture in Vulkan SPEC. BUG=dawn:42 TEST=dawn_end2end_tests Change-Id: I17518cd335fb13125cb753bbf879bc06eb20e426 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/8260 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Austin Eng <enga@chromium.org> Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
2019-07-02 23:55:55 +00:00
Extent3D extent = texture->GetMipLevelPhysicalSize(mipLevel);
ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D);
if (extent.width == copySize.width && extent.height == copySize.height) {
return true;
}
return false;
}
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, copy.aspect};
default:
UNREACHABLE();
return {};
}
}
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);
SubresourceRange range = view->GetSubresourceRange();
// If the loadOp is Load, but the subresource is not initialized, use Clear instead.
if (attachmentInfo.loadOp == wgpu::LoadOp::Load &&
!view->GetTexture()->IsSubresourceContentInitialized(range)) {
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();
ASSERT(resolveView->GetLayerCount() == 1);
ASSERT(resolveView->GetLevelCount() == 1);
resolveView->GetTexture()->SetIsSubresourceContentInitialized(
true, resolveView->GetSubresourceRange());
}
switch (attachmentInfo.storeOp) {
case wgpu::StoreOp::Store:
view->GetTexture()->SetIsSubresourceContentInitialized(true, range);
break;
case wgpu::StoreOp::Clear:
view->GetTexture()->SetIsSubresourceContentInitialized(false, range);
break;
default:
UNREACHABLE();
break;
}
}
if (renderPass->attachmentState->HasDepthStencilAttachment()) {
auto& attachmentInfo = renderPass->depthStencilAttachment;
TextureViewBase* view = attachmentInfo.view.Get();
ASSERT(view->GetLayerCount() == 1);
ASSERT(view->GetLevelCount() == 1);
SubresourceRange range = view->GetSubresourceRange();
SubresourceRange depthRange = range;
depthRange.aspects = range.aspects & Aspect::Depth;
SubresourceRange stencilRange = range;
stencilRange.aspects = range.aspects & Aspect::Stencil;
// If the depth stencil texture has not been initialized, we want to use loadop
// clear to init the contents to 0's
if (!view->GetTexture()->IsSubresourceContentInitialized(depthRange) &&
attachmentInfo.depthLoadOp == wgpu::LoadOp::Load) {
attachmentInfo.clearDepth = 0.0f;
attachmentInfo.depthLoadOp = wgpu::LoadOp::Clear;
}
if (!view->GetTexture()->IsSubresourceContentInitialized(stencilRange) &&
attachmentInfo.stencilLoadOp == wgpu::LoadOp::Load) {
attachmentInfo.clearStencil = 0u;
attachmentInfo.stencilLoadOp = wgpu::LoadOp::Clear;
}
view->GetTexture()->SetIsSubresourceContentInitialized(
attachmentInfo.depthStoreOp == wgpu::StoreOp::Store, depthRange);
view->GetTexture()->SetIsSubresourceContentInitialized(
attachmentInfo.stencilStoreOp == wgpu::StoreOp::Store, stencilRange);
}
}
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 TexelBlockInfo& blockInfo =
texture->GetFormat().GetTexelBlockInfo(copy->source.aspect);
const uint64_t copyTextureDataSizePerRow =
copy->copySize.width / blockInfo.blockWidth * blockInfo.blockByteSize;
if (copy->destination.bytesPerRow > copyTextureDataSizePerRow) {
return false;
}
const uint64_t overwrittenRangeSize = copyTextureDataSizePerRow *
(copy->copySize.height / blockInfo.blockHeight) *
copy->copySize.depth;
if (copy->destination.buffer->GetSize() > overwrittenRangeSize) {
return false;
}
return true;
}
std::array<int32_t, 4> ConvertToSignedIntegerColor(dawn_native::Color color) {
const std::array<int32_t, 4> outputValue = {
static_cast<int32_t>(color.r), static_cast<int32_t>(color.g),
static_cast<int32_t>(color.b), static_cast<int32_t>(color.a)};
return outputValue;
}
std::array<uint32_t, 4> ConvertToUnsignedIntegerColor(dawn_native::Color color) {
const std::array<uint32_t, 4> outputValue = {
static_cast<uint32_t>(color.r), static_cast<uint32_t>(color.g),
static_cast<uint32_t>(color.b), static_cast<uint32_t>(color.a)};
return outputValue;
}
} // namespace dawn_native