2019-02-15 12:54:08 +00:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
|
|
|
#include "dawn_native/CommandEncoder.h"
|
|
|
|
|
2019-02-20 11:46:16 +00:00
|
|
|
#include "common/BitSetIterator.h"
|
2020-08-05 16:43:24 +00:00
|
|
|
#include "common/Math.h"
|
2019-02-20 11:46:16 +00:00
|
|
|
#include "dawn_native/BindGroup.h"
|
|
|
|
#include "dawn_native/Buffer.h"
|
2019-02-15 12:54:08 +00:00
|
|
|
#include "dawn_native/CommandBuffer.h"
|
2019-02-20 11:46:16 +00:00
|
|
|
#include "dawn_native/CommandBufferStateTracker.h"
|
2019-08-13 00:22:28 +00:00
|
|
|
#include "dawn_native/CommandValidation.h"
|
2019-02-20 11:46:16 +00:00
|
|
|
#include "dawn_native/Commands.h"
|
|
|
|
#include "dawn_native/ComputePassEncoder.h"
|
2019-02-15 12:54:08 +00:00
|
|
|
#include "dawn_native/Device.h"
|
2019-02-20 11:46:16 +00:00
|
|
|
#include "dawn_native/ErrorData.h"
|
2021-01-14 03:26:08 +00:00
|
|
|
#include "dawn_native/QueryHelper.h"
|
2020-07-01 10:48:16 +00:00
|
|
|
#include "dawn_native/QuerySet.h"
|
2021-01-14 03:26:08 +00:00
|
|
|
#include "dawn_native/Queue.h"
|
2019-02-20 11:46:16 +00:00
|
|
|
#include "dawn_native/RenderPassEncoder.h"
|
|
|
|
#include "dawn_native/RenderPipeline.h"
|
2019-12-12 01:29:01 +00:00
|
|
|
#include "dawn_native/ValidationUtils_autogen.h"
|
2019-10-28 23:15:40 +00:00
|
|
|
#include "dawn_platform/DawnPlatform.h"
|
2019-08-13 19:00:34 +00:00
|
|
|
#include "dawn_platform/tracing/TraceEvent.h"
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2020-02-18 02:44:05 +00:00
|
|
|
#include <cmath>
|
2019-02-20 11:46:16 +00:00
|
|
|
#include <map>
|
2019-02-15 12:54:08 +00:00
|
|
|
|
|
|
|
namespace dawn_native {
|
|
|
|
|
2019-02-20 11:46:16 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
MaybeError ValidateB2BCopyAlignment(uint64_t dataSize,
|
|
|
|
uint64_t srcOffset,
|
|
|
|
uint64_t dstOffset) {
|
2019-02-27 02:46:27 +00:00
|
|
|
// Copy size must be a multiple of 4 bytes on macOS.
|
|
|
|
if (dataSize % 4 != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Copy size must be a multiple of 4 bytes");
|
|
|
|
}
|
|
|
|
|
|
|
|
// SourceOffset and destinationOffset must be multiples of 4 bytes on macOS.
|
|
|
|
if (srcOffset % 4 != 0 || dstOffset % 4 != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Source offset and destination offset must be multiples of 4 bytes");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-09-01 18:40:18 +00:00
|
|
|
MaybeError ValidateTextureSampleCountInBufferCopyCommands(const TextureBase* texture) {
|
2019-03-04 12:01:59 +00:00
|
|
|
if (texture->GetSampleCount() > 1) {
|
2019-03-26 11:06:23 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
2020-09-01 18:40:18 +00:00
|
|
|
"The sample count of textures must be 1 when copying between buffers and "
|
|
|
|
"textures");
|
2019-03-26 11:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-10-27 19:30:57 +00:00
|
|
|
MaybeError ValidateLinearTextureCopyOffset(const TextureDataLayout& layout,
|
2021-06-24 05:38:00 +00:00
|
|
|
const TexelBlockInfo& blockInfo,
|
|
|
|
const bool hasDepthOrStencil) {
|
|
|
|
if (hasDepthOrStencil) {
|
|
|
|
// For depth-stencil texture, buffer offset must be a multiple of 4.
|
|
|
|
if (layout.offset % 4 != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"offset must be a multiple of 4 for depth/stencil texture.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (layout.offset % blockInfo.byteSize != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"offset must be a multiple of the texel block byte size.");
|
|
|
|
}
|
2020-10-27 19:30:57 +00:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:13:45 +00:00
|
|
|
MaybeError ValidateTextureDepthStencilToBufferCopyRestrictions(
|
|
|
|
const ImageCopyTexture& src) {
|
2020-10-27 19:30:57 +00:00
|
|
|
Aspect aspectUsed;
|
2021-03-04 18:13:45 +00:00
|
|
|
DAWN_TRY_ASSIGN(aspectUsed, SingleAspectUsedByImageCopyTexture(src));
|
2020-10-27 19:30:57 +00:00
|
|
|
if (aspectUsed == Aspect::Depth) {
|
|
|
|
switch (src.texture->GetFormat().format) {
|
2020-07-30 15:29:57 +00:00
|
|
|
case wgpu::TextureFormat::Depth24Plus:
|
|
|
|
case wgpu::TextureFormat::Depth24PlusStencil8:
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The depth aspect of depth24plus texture cannot be selected in a "
|
|
|
|
"texture to buffer copy");
|
|
|
|
case wgpu::TextureFormat::Depth32Float:
|
|
|
|
break;
|
2020-09-24 14:56:50 +00:00
|
|
|
|
2020-07-30 15:29:57 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-02-27 09:21:56 +00:00
|
|
|
MaybeError ValidateAttachmentArrayLayersAndLevelCount(const TextureViewBase* attachment) {
|
|
|
|
// Currently we do not support layered rendering.
|
|
|
|
if (attachment->GetLayerCount() > 1) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The layer count of the texture view used as attachment cannot be greater than "
|
|
|
|
"1");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachment->GetLevelCount() > 1) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The mipmap level count of the texture view used as attachment cannot be "
|
|
|
|
"greater than 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeError ValidateOrSetAttachmentSize(const TextureViewBase* attachment,
|
|
|
|
uint32_t* width,
|
|
|
|
uint32_t* height) {
|
2020-10-02 16:15:40 +00:00
|
|
|
const Extent3D& attachmentSize =
|
|
|
|
attachment->GetTexture()->GetMipLevelVirtualSize(attachment->GetBaseMipLevel());
|
2019-02-27 09:21:56 +00:00
|
|
|
|
|
|
|
if (*width == 0) {
|
|
|
|
DAWN_ASSERT(*height == 0);
|
2020-10-02 16:15:40 +00:00
|
|
|
*width = attachmentSize.width;
|
|
|
|
*height = attachmentSize.height;
|
2019-02-27 09:21:56 +00:00
|
|
|
DAWN_ASSERT(*width != 0 && *height != 0);
|
2020-10-02 16:15:40 +00:00
|
|
|
} else if (*width != attachmentSize.width || *height != attachmentSize.height) {
|
2019-02-27 09:21:56 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Attachment size mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-03-11 18:41:02 +00:00
|
|
|
MaybeError ValidateOrSetColorAttachmentSampleCount(const TextureViewBase* colorAttachment,
|
|
|
|
uint32_t* sampleCount) {
|
|
|
|
if (*sampleCount == 0) {
|
|
|
|
*sampleCount = colorAttachment->GetTexture()->GetSampleCount();
|
|
|
|
DAWN_ASSERT(*sampleCount != 0);
|
|
|
|
} else if (*sampleCount != colorAttachment->GetTexture()->GetSampleCount()) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Color attachment sample counts mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-09-10 15:50:19 +00:00
|
|
|
MaybeError ValidateResolveTarget(const DeviceBase* device,
|
|
|
|
const RenderPassColorAttachment& colorAttachment) {
|
2019-09-20 22:59:47 +00:00
|
|
|
if (colorAttachment.resolveTarget == nullptr) {
|
2019-03-11 18:41:02 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-09-20 22:59:47 +00:00
|
|
|
const TextureViewBase* resolveTarget = colorAttachment.resolveTarget;
|
2021-09-14 10:42:22 +00:00
|
|
|
const TextureViewBase* attachment = colorAttachment.view;
|
2019-09-20 22:59:47 +00:00
|
|
|
DAWN_TRY(device->ValidateObject(colorAttachment.resolveTarget));
|
2021-01-22 11:31:08 +00:00
|
|
|
DAWN_TRY(ValidateCanUseAs(colorAttachment.resolveTarget->GetTexture(),
|
|
|
|
wgpu::TextureUsage::RenderAttachment));
|
2019-03-11 18:41:02 +00:00
|
|
|
|
2019-09-20 22:59:47 +00:00
|
|
|
if (!attachment->GetTexture()->IsMultisampledTexture()) {
|
2019-03-11 18:41:02 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Cannot set resolve target when the sample count of the color attachment is 1");
|
|
|
|
}
|
|
|
|
|
2019-09-20 22:59:47 +00:00
|
|
|
if (resolveTarget->GetTexture()->IsMultisampledTexture()) {
|
2019-03-11 18:41:02 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Cannot use multisampled texture as resolve target");
|
|
|
|
}
|
|
|
|
|
2019-09-20 22:59:47 +00:00
|
|
|
if (resolveTarget->GetLayerCount() > 1) {
|
2019-03-11 18:41:02 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The array layer count of the resolve target must be 1");
|
|
|
|
}
|
|
|
|
|
2019-09-20 22:59:47 +00:00
|
|
|
if (resolveTarget->GetLevelCount() > 1) {
|
2019-03-11 18:41:02 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("The mip level count of the resolve target must be 1");
|
|
|
|
}
|
|
|
|
|
2021-04-30 17:51:58 +00:00
|
|
|
const Extent3D& colorTextureSize =
|
|
|
|
attachment->GetTexture()->GetMipLevelVirtualSize(attachment->GetBaseMipLevel());
|
|
|
|
const Extent3D& resolveTextureSize =
|
|
|
|
resolveTarget->GetTexture()->GetMipLevelVirtualSize(
|
|
|
|
resolveTarget->GetBaseMipLevel());
|
|
|
|
if (colorTextureSize.width != resolveTextureSize.width ||
|
|
|
|
colorTextureSize.height != resolveTextureSize.height) {
|
2019-03-11 18:41:02 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The size of the resolve target must be the same as the color attachment");
|
|
|
|
}
|
|
|
|
|
2019-10-23 11:57:41 +00:00
|
|
|
wgpu::TextureFormat resolveTargetFormat = resolveTarget->GetFormat().format;
|
2019-09-20 22:59:47 +00:00
|
|
|
if (resolveTargetFormat != attachment->GetFormat().format) {
|
2019-03-11 18:41:02 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The format of the resolve target must be the same as the color attachment");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-02-27 09:21:56 +00:00
|
|
|
MaybeError ValidateRenderPassColorAttachment(
|
2021-04-17 01:51:53 +00:00
|
|
|
DeviceBase* device,
|
2021-09-10 15:50:19 +00:00
|
|
|
const RenderPassColorAttachment& colorAttachment,
|
2019-02-27 09:21:56 +00:00
|
|
|
uint32_t* width,
|
2019-03-11 18:41:02 +00:00
|
|
|
uint32_t* height,
|
|
|
|
uint32_t* sampleCount) {
|
2021-09-14 10:42:22 +00:00
|
|
|
TextureViewBase* attachment = colorAttachment.view;
|
2021-04-17 01:51:53 +00:00
|
|
|
DAWN_TRY(device->ValidateObject(attachment));
|
|
|
|
DAWN_TRY(
|
|
|
|
ValidateCanUseAs(attachment->GetTexture(), wgpu::TextureUsage::RenderAttachment));
|
2019-02-27 09:21:56 +00:00
|
|
|
|
2020-10-15 09:15:53 +00:00
|
|
|
if (!(attachment->GetAspects() & Aspect::Color) ||
|
|
|
|
!attachment->GetFormat().isRenderable) {
|
2019-02-27 09:21:56 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The format of the texture view used as color attachment is not color "
|
|
|
|
"renderable");
|
|
|
|
}
|
|
|
|
|
2019-12-12 01:29:01 +00:00
|
|
|
DAWN_TRY(ValidateLoadOp(colorAttachment.loadOp));
|
2021-09-14 16:02:00 +00:00
|
|
|
DAWN_TRY(ValidateStoreOp(colorAttachment.storeOp));
|
2019-12-12 01:29:01 +00:00
|
|
|
|
|
|
|
if (colorAttachment.loadOp == wgpu::LoadOp::Clear) {
|
|
|
|
if (std::isnan(colorAttachment.clearColor.r) ||
|
|
|
|
std::isnan(colorAttachment.clearColor.g) ||
|
|
|
|
std::isnan(colorAttachment.clearColor.b) ||
|
|
|
|
std::isnan(colorAttachment.clearColor.a)) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Color clear value cannot contain NaN");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 18:41:02 +00:00
|
|
|
DAWN_TRY(ValidateOrSetColorAttachmentSampleCount(attachment, sampleCount));
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateResolveTarget(device, colorAttachment));
|
|
|
|
|
2019-02-27 09:21:56 +00:00
|
|
|
DAWN_TRY(ValidateAttachmentArrayLayersAndLevelCount(attachment));
|
|
|
|
DAWN_TRY(ValidateOrSetAttachmentSize(attachment, width, height));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeError ValidateRenderPassDepthStencilAttachment(
|
2021-04-17 01:51:53 +00:00
|
|
|
DeviceBase* device,
|
2021-09-10 15:50:19 +00:00
|
|
|
const RenderPassDepthStencilAttachment* depthStencilAttachment,
|
2019-02-27 09:21:56 +00:00
|
|
|
uint32_t* width,
|
2019-03-15 05:16:41 +00:00
|
|
|
uint32_t* height,
|
|
|
|
uint32_t* sampleCount) {
|
2019-02-27 09:21:56 +00:00
|
|
|
DAWN_ASSERT(depthStencilAttachment != nullptr);
|
|
|
|
|
2021-09-14 10:42:22 +00:00
|
|
|
TextureViewBase* attachment = depthStencilAttachment->view;
|
2021-04-17 01:51:53 +00:00
|
|
|
DAWN_TRY(device->ValidateObject(attachment));
|
|
|
|
DAWN_TRY(
|
|
|
|
ValidateCanUseAs(attachment->GetTexture(), wgpu::TextureUsage::RenderAttachment));
|
2019-02-27 09:21:56 +00:00
|
|
|
|
2021-06-11 14:25:29 +00:00
|
|
|
const Format& format = attachment->GetFormat();
|
|
|
|
if (!format.HasDepthOrStencil()) {
|
2019-02-27 09:21:56 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The format of the texture view used as depth stencil attachment is not a "
|
|
|
|
"depth stencil format");
|
|
|
|
}
|
2021-06-11 14:25:29 +00:00
|
|
|
if (!format.isRenderable) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The format of the texture view used as depth stencil attachment is not "
|
|
|
|
"renderable");
|
|
|
|
}
|
|
|
|
if (attachment->GetAspects() != format.aspects) {
|
|
|
|
// TODO(https://crbug.com/dawn/812): Investigate if this limitation should be added
|
|
|
|
// to the WebGPU spec of lifted from Dawn.
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The texture view used as depth stencil view must encompass all aspects");
|
|
|
|
}
|
2019-02-27 09:21:56 +00:00
|
|
|
|
2019-12-12 01:29:01 +00:00
|
|
|
DAWN_TRY(ValidateLoadOp(depthStencilAttachment->depthLoadOp));
|
|
|
|
DAWN_TRY(ValidateLoadOp(depthStencilAttachment->stencilLoadOp));
|
2021-09-14 16:02:00 +00:00
|
|
|
DAWN_TRY(ValidateStoreOp(depthStencilAttachment->depthStoreOp));
|
|
|
|
DAWN_TRY(ValidateStoreOp(depthStencilAttachment->stencilStoreOp));
|
2019-12-12 01:29:01 +00:00
|
|
|
|
2020-10-15 09:15:53 +00:00
|
|
|
if (attachment->GetAspects() == (Aspect::Depth | Aspect::Stencil) &&
|
2020-07-10 23:13:58 +00:00
|
|
|
depthStencilAttachment->depthReadOnly != depthStencilAttachment->stencilReadOnly) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"depthReadOnly and stencilReadOnly must be the same when texture aspect is "
|
|
|
|
"'all'");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depthStencilAttachment->depthReadOnly &&
|
|
|
|
(depthStencilAttachment->depthLoadOp != wgpu::LoadOp::Load ||
|
|
|
|
depthStencilAttachment->depthStoreOp != wgpu::StoreOp::Store)) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"depthLoadOp must be load and depthStoreOp must be store when depthReadOnly "
|
|
|
|
"is true.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depthStencilAttachment->stencilReadOnly &&
|
|
|
|
(depthStencilAttachment->stencilLoadOp != wgpu::LoadOp::Load ||
|
|
|
|
depthStencilAttachment->stencilStoreOp != wgpu::StoreOp::Store)) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"stencilLoadOp must be load and stencilStoreOp must be store when "
|
|
|
|
"stencilReadOnly "
|
|
|
|
"is true.");
|
|
|
|
}
|
|
|
|
|
2019-12-12 01:29:01 +00:00
|
|
|
if (depthStencilAttachment->depthLoadOp == wgpu::LoadOp::Clear &&
|
|
|
|
std::isnan(depthStencilAttachment->clearDepth)) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Depth clear value cannot be NaN");
|
|
|
|
}
|
|
|
|
|
2019-03-15 05:16:41 +00:00
|
|
|
// *sampleCount == 0 must only happen when there is no color attachment. In that case we
|
|
|
|
// do not need to validate the sample count of the depth stencil attachment.
|
2019-03-19 01:12:01 +00:00
|
|
|
const uint32_t depthStencilSampleCount = attachment->GetTexture()->GetSampleCount();
|
|
|
|
if (*sampleCount != 0) {
|
|
|
|
if (depthStencilSampleCount != *sampleCount) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Depth stencil attachment sample counts mismatch");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*sampleCount = depthStencilSampleCount;
|
2019-03-15 05:16:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 09:21:56 +00:00
|
|
|
DAWN_TRY(ValidateAttachmentArrayLayersAndLevelCount(attachment));
|
|
|
|
DAWN_TRY(ValidateOrSetAttachmentSize(attachment, width, height));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-04-17 01:51:53 +00:00
|
|
|
MaybeError ValidateRenderPassDescriptor(DeviceBase* device,
|
2019-07-10 20:43:13 +00:00
|
|
|
const RenderPassDescriptor* descriptor,
|
2019-03-19 01:12:01 +00:00
|
|
|
uint32_t* width,
|
|
|
|
uint32_t* height,
|
|
|
|
uint32_t* sampleCount) {
|
2019-07-10 20:43:13 +00:00
|
|
|
if (descriptor->colorAttachmentCount > kMaxColorAttachments) {
|
2019-02-27 09:21:56 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Setting color attachments out of bounds");
|
|
|
|
}
|
|
|
|
|
2019-07-10 20:43:13 +00:00
|
|
|
for (uint32_t i = 0; i < descriptor->colorAttachmentCount; ++i) {
|
|
|
|
DAWN_TRY(ValidateRenderPassColorAttachment(device, descriptor->colorAttachments[i],
|
2019-03-19 01:12:01 +00:00
|
|
|
width, height, sampleCount));
|
2019-02-27 09:21:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 20:43:13 +00:00
|
|
|
if (descriptor->depthStencilAttachment != nullptr) {
|
2019-02-27 09:21:56 +00:00
|
|
|
DAWN_TRY(ValidateRenderPassDepthStencilAttachment(
|
2019-07-10 20:43:13 +00:00
|
|
|
device, descriptor->depthStencilAttachment, width, height, sampleCount));
|
2019-02-27 09:21:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 03:15:16 +00:00
|
|
|
if (descriptor->occlusionQuerySet != nullptr) {
|
2020-11-18 09:47:52 +00:00
|
|
|
DAWN_TRY(device->ValidateObject(descriptor->occlusionQuerySet));
|
|
|
|
|
|
|
|
if (descriptor->occlusionQuerySet->GetQueryType() != wgpu::QueryType::Occlusion) {
|
|
|
|
return DAWN_VALIDATION_ERROR("The type of query set must be Occlusion");
|
|
|
|
}
|
2020-07-11 03:15:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 20:43:13 +00:00
|
|
|
if (descriptor->colorAttachmentCount == 0 &&
|
|
|
|
descriptor->depthStencilAttachment == nullptr) {
|
2019-02-27 09:21:56 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Cannot use render pass with no attachments.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-07-10 20:43:13 +00:00
|
|
|
MaybeError ValidateComputePassDescriptor(const DeviceBase* device,
|
|
|
|
const ComputePassDescriptor* descriptor) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-07-17 09:02:46 +00:00
|
|
|
MaybeError ValidateQuerySetResolve(const QuerySetBase* querySet,
|
|
|
|
uint32_t firstQuery,
|
|
|
|
uint32_t queryCount,
|
|
|
|
const BufferBase* destination,
|
|
|
|
uint64_t destinationOffset) {
|
|
|
|
if (firstQuery >= querySet->GetQueryCount()) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Query index out of bounds");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queryCount > querySet->GetQueryCount() - firstQuery) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The sum of firstQuery and queryCount exceeds the number of queries in query "
|
|
|
|
"set");
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:51:20 +00:00
|
|
|
if (destinationOffset % 256 != 0) {
|
2020-07-17 09:02:46 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
2021-09-22 02:51:20 +00:00
|
|
|
"The alignment offset into the destination buffer must be a multiple of 256 "
|
2020-07-17 09:02:46 +00:00
|
|
|
"bytes");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t bufferSize = destination->GetSize();
|
|
|
|
// The destination buffer must have enough storage, from destination offset, to contain
|
|
|
|
// the result of resolved queries
|
|
|
|
bool fitsInBuffer = destinationOffset <= bufferSize &&
|
|
|
|
(static_cast<uint64_t>(queryCount) * sizeof(uint64_t) <=
|
|
|
|
(bufferSize - destinationOffset));
|
|
|
|
if (!fitsInBuffer) {
|
|
|
|
return DAWN_VALIDATION_ERROR("The resolved query data would overflow the buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-03-31 11:24:42 +00:00
|
|
|
MaybeError EncodeTimestampsToNanosecondsConversion(CommandEncoder* encoder,
|
|
|
|
QuerySetBase* querySet,
|
2021-05-18 01:13:08 +00:00
|
|
|
uint32_t firstQuery,
|
2021-03-31 11:24:42 +00:00
|
|
|
uint32_t queryCount,
|
|
|
|
BufferBase* destination,
|
|
|
|
uint64_t destinationOffset) {
|
2021-01-14 03:26:08 +00:00
|
|
|
DeviceBase* device = encoder->GetDevice();
|
|
|
|
|
2021-02-10 14:01:36 +00:00
|
|
|
// The availability got from query set is a reference to vector<bool>, need to covert
|
|
|
|
// bool to uint32_t due to a user input in pipeline must not contain a bool type in
|
|
|
|
// WGSL.
|
|
|
|
std::vector<uint32_t> availability{querySet->GetQueryAvailability().begin(),
|
|
|
|
querySet->GetQueryAvailability().end()};
|
2021-01-14 03:26:08 +00:00
|
|
|
|
|
|
|
// Timestamp availability storage buffer
|
|
|
|
BufferDescriptor availabilityDesc = {};
|
|
|
|
availabilityDesc.usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopyDst;
|
|
|
|
availabilityDesc.size = querySet->GetQueryCount() * sizeof(uint32_t);
|
2021-04-19 08:52:35 +00:00
|
|
|
Ref<BufferBase> availabilityBuffer;
|
|
|
|
DAWN_TRY_ASSIGN(availabilityBuffer, device->CreateBuffer(&availabilityDesc));
|
|
|
|
|
2021-03-31 11:24:42 +00:00
|
|
|
DAWN_TRY(device->GetQueue()->WriteBuffer(availabilityBuffer.Get(), 0,
|
|
|
|
availability.data(),
|
|
|
|
availability.size() * sizeof(uint32_t)));
|
2021-01-14 03:26:08 +00:00
|
|
|
|
|
|
|
// Timestamp params uniform buffer
|
2021-05-18 01:13:08 +00:00
|
|
|
TimestampParams params = {firstQuery, queryCount,
|
|
|
|
static_cast<uint32_t>(destinationOffset),
|
2021-01-14 03:26:08 +00:00
|
|
|
device->GetTimestampPeriodInNS()};
|
2021-04-19 08:52:35 +00:00
|
|
|
|
2021-01-14 03:26:08 +00:00
|
|
|
BufferDescriptor parmsDesc = {};
|
|
|
|
parmsDesc.usage = wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst;
|
|
|
|
parmsDesc.size = sizeof(params);
|
2021-04-19 08:52:35 +00:00
|
|
|
Ref<BufferBase> paramsBuffer;
|
|
|
|
DAWN_TRY_ASSIGN(paramsBuffer, device->CreateBuffer(&parmsDesc));
|
|
|
|
|
2021-03-31 11:24:42 +00:00
|
|
|
DAWN_TRY(
|
|
|
|
device->GetQueue()->WriteBuffer(paramsBuffer.Get(), 0, ¶ms, sizeof(params)));
|
2021-01-14 03:26:08 +00:00
|
|
|
|
2021-04-19 08:52:35 +00:00
|
|
|
return EncodeConvertTimestampsToNanoseconds(
|
|
|
|
encoder, destination, availabilityBuffer.Get(), paramsBuffer.Get());
|
2021-01-14 03:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 11:46:16 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
CommandEncoder::CommandEncoder(DeviceBase* device, const CommandEncoderDescriptor*)
|
2021-08-23 23:14:36 +00:00
|
|
|
: ObjectBase(device, kLabelNotImplemented), mEncodingContext(device, this) {
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
CommandBufferResourceUsage CommandEncoder::AcquireResourceUsages() {
|
2021-05-05 15:41:13 +00:00
|
|
|
return CommandBufferResourceUsage{
|
|
|
|
mEncodingContext.AcquireRenderPassUsages(), mEncodingContext.AcquireComputePassUsages(),
|
|
|
|
std::move(mTopLevelBuffers), std::move(mTopLevelTextures), std::move(mUsedQuerySets)};
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
CommandIterator CommandEncoder::AcquireCommands() {
|
2019-07-24 18:15:24 +00:00
|
|
|
return mEncodingContext.AcquireCommands();
|
2019-02-15 12:54:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 10:48:16 +00:00
|
|
|
void CommandEncoder::TrackUsedQuerySet(QuerySetBase* querySet) {
|
|
|
|
mUsedQuerySets.insert(querySet);
|
|
|
|
}
|
|
|
|
|
2020-11-16 02:24:06 +00:00
|
|
|
void CommandEncoder::TrackQueryAvailability(QuerySetBase* querySet, uint32_t queryIndex) {
|
|
|
|
DAWN_ASSERT(querySet != nullptr);
|
|
|
|
|
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
TrackUsedQuerySet(querySet);
|
2020-10-21 08:38:31 +00:00
|
|
|
}
|
2020-11-16 02:24:06 +00:00
|
|
|
|
2021-02-10 14:01:36 +00:00
|
|
|
// Set the query at queryIndex to available for resolving in query set.
|
2021-04-07 05:39:21 +00:00
|
|
|
querySet->SetQueryAvailability(queryIndex, true);
|
2020-10-21 08:38:31 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 11:46:16 +00:00
|
|
|
// Implementation of the API's command recording methods
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
ComputePassEncoder* CommandEncoder::APIBeginComputePass(
|
|
|
|
const ComputePassDescriptor* descriptor) {
|
2019-03-05 01:02:47 +00:00
|
|
|
DeviceBase* device = GetDevice();
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
bool success =
|
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
DAWN_TRY(ValidateComputePassDescriptor(device, descriptor));
|
2019-07-10 20:43:13 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
allocator->Allocate<BeginComputePassCmd>(Command::BeginComputePass);
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
|
|
|
|
|
|
|
if (success) {
|
2019-11-13 17:00:37 +00:00
|
|
|
ComputePassEncoder* passEncoder =
|
|
|
|
new ComputePassEncoder(device, this, &mEncodingContext);
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext.EnterPass(passEncoder);
|
|
|
|
return passEncoder;
|
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
return ComputePassEncoder::MakeError(device, this, &mEncodingContext);
|
2019-02-15 12:54:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
RenderPassEncoder* CommandEncoder::APIBeginRenderPass(const RenderPassDescriptor* descriptor) {
|
2019-02-27 09:21:56 +00:00
|
|
|
DeviceBase* device = GetDevice();
|
|
|
|
|
2021-05-05 19:55:23 +00:00
|
|
|
RenderPassResourceUsageTracker usageTracker;
|
2020-10-11 18:39:32 +00:00
|
|
|
|
|
|
|
uint32_t width = 0;
|
|
|
|
uint32_t height = 0;
|
2021-01-27 17:20:16 +00:00
|
|
|
Ref<AttachmentState> attachmentState;
|
2021-09-23 00:15:19 +00:00
|
|
|
mEncodingContext.WillBeginRenderPass();
|
2019-07-24 18:15:24 +00:00
|
|
|
bool success =
|
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
uint32_t sampleCount = 0;
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateRenderPassDescriptor(device, descriptor, &width, &height,
|
|
|
|
&sampleCount));
|
|
|
|
|
|
|
|
ASSERT(width > 0 && height > 0 && sampleCount > 0);
|
|
|
|
|
|
|
|
BeginRenderPassCmd* cmd =
|
|
|
|
allocator->Allocate<BeginRenderPassCmd>(Command::BeginRenderPass);
|
|
|
|
|
2019-07-26 19:08:18 +00:00
|
|
|
cmd->attachmentState = device->GetOrCreateAttachmentState(descriptor);
|
2021-01-27 17:20:16 +00:00
|
|
|
attachmentState = cmd->attachmentState;
|
2019-07-26 19:08:18 +00:00
|
|
|
|
2020-09-09 00:08:38 +00:00
|
|
|
for (ColorAttachmentIndex index :
|
|
|
|
IterateBitSet(cmd->attachmentState->GetColorAttachmentsMask())) {
|
|
|
|
uint8_t i = static_cast<uint8_t>(index);
|
2021-04-17 01:51:53 +00:00
|
|
|
TextureViewBase* view = descriptor->colorAttachments[i].view;
|
2019-11-21 22:09:41 +00:00
|
|
|
TextureViewBase* resolveTarget = descriptor->colorAttachments[i].resolveTarget;
|
|
|
|
|
2020-09-09 00:08:38 +00:00
|
|
|
cmd->colorAttachments[index].view = view;
|
|
|
|
cmd->colorAttachments[index].resolveTarget = resolveTarget;
|
|
|
|
cmd->colorAttachments[index].loadOp = descriptor->colorAttachments[i].loadOp;
|
|
|
|
cmd->colorAttachments[index].storeOp = descriptor->colorAttachments[i].storeOp;
|
|
|
|
cmd->colorAttachments[index].clearColor =
|
2019-09-20 22:59:47 +00:00
|
|
|
descriptor->colorAttachments[i].clearColor;
|
2019-11-21 22:09:41 +00:00
|
|
|
|
2020-10-27 15:35:56 +00:00
|
|
|
usageTracker.TextureViewUsedAs(view, wgpu::TextureUsage::RenderAttachment);
|
2019-11-21 22:09:41 +00:00
|
|
|
|
|
|
|
if (resolveTarget != nullptr) {
|
2020-05-04 17:10:49 +00:00
|
|
|
usageTracker.TextureViewUsedAs(resolveTarget,
|
2020-10-27 15:35:56 +00:00
|
|
|
wgpu::TextureUsage::RenderAttachment);
|
2019-11-21 22:09:41 +00:00
|
|
|
}
|
2019-07-24 18:15:24 +00:00
|
|
|
}
|
2019-03-19 01:12:01 +00:00
|
|
|
|
2019-07-26 19:08:18 +00:00
|
|
|
if (cmd->attachmentState->HasDepthStencilAttachment()) {
|
2021-04-17 01:51:53 +00:00
|
|
|
TextureViewBase* view = descriptor->depthStencilAttachment->view;
|
2019-11-21 22:09:41 +00:00
|
|
|
|
|
|
|
cmd->depthStencilAttachment.view = view;
|
2019-07-24 18:15:24 +00:00
|
|
|
cmd->depthStencilAttachment.clearDepth =
|
|
|
|
descriptor->depthStencilAttachment->clearDepth;
|
|
|
|
cmd->depthStencilAttachment.clearStencil =
|
|
|
|
descriptor->depthStencilAttachment->clearStencil;
|
|
|
|
cmd->depthStencilAttachment.depthLoadOp =
|
|
|
|
descriptor->depthStencilAttachment->depthLoadOp;
|
|
|
|
cmd->depthStencilAttachment.depthStoreOp =
|
|
|
|
descriptor->depthStencilAttachment->depthStoreOp;
|
|
|
|
cmd->depthStencilAttachment.stencilLoadOp =
|
|
|
|
descriptor->depthStencilAttachment->stencilLoadOp;
|
|
|
|
cmd->depthStencilAttachment.stencilStoreOp =
|
|
|
|
descriptor->depthStencilAttachment->stencilStoreOp;
|
2019-11-21 22:09:41 +00:00
|
|
|
|
2020-10-27 15:35:56 +00:00
|
|
|
usageTracker.TextureViewUsedAs(view, wgpu::TextureUsage::RenderAttachment);
|
2019-07-24 18:15:24 +00:00
|
|
|
}
|
2019-02-27 09:21:56 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
cmd->width = width;
|
|
|
|
cmd->height = height;
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2021-02-02 05:30:50 +00:00
|
|
|
cmd->occlusionQuerySet = descriptor->occlusionQuerySet;
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
if (success) {
|
2021-01-27 17:20:16 +00:00
|
|
|
RenderPassEncoder* passEncoder = new RenderPassEncoder(
|
|
|
|
device, this, &mEncodingContext, std::move(usageTracker),
|
|
|
|
std::move(attachmentState), descriptor->occlusionQuerySet, width, height);
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext.EnterPass(passEncoder);
|
|
|
|
return passEncoder;
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
return RenderPassEncoder::MakeError(device, this, &mEncodingContext);
|
2019-02-15 12:54:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APICopyBufferToBuffer(BufferBase* source,
|
|
|
|
uint64_t sourceOffset,
|
|
|
|
BufferBase* destination,
|
|
|
|
uint64_t destinationOffset,
|
|
|
|
uint64_t size) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2020-05-05 08:33:55 +00:00
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(source));
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(destination));
|
|
|
|
|
2020-05-19 00:11:11 +00:00
|
|
|
if (source == destination) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Source and destination cannot be the same buffer.");
|
|
|
|
}
|
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
DAWN_TRY(ValidateCopySizeFitsInBuffer(source, sourceOffset, size));
|
|
|
|
DAWN_TRY(ValidateCopySizeFitsInBuffer(destination, destinationOffset, size));
|
|
|
|
DAWN_TRY(ValidateB2BCopyAlignment(size, sourceOffset, destinationOffset));
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateCanUseAs(source, wgpu::BufferUsage::CopySrc));
|
|
|
|
DAWN_TRY(ValidateCanUseAs(destination, wgpu::BufferUsage::CopyDst));
|
|
|
|
|
|
|
|
mTopLevelBuffers.insert(source);
|
|
|
|
mTopLevelBuffers.insert(destination);
|
|
|
|
}
|
2019-07-24 18:15:24 +00:00
|
|
|
|
2021-06-10 02:40:18 +00:00
|
|
|
CopyBufferToBufferCmd* copy =
|
|
|
|
allocator->Allocate<CopyBufferToBufferCmd>(Command::CopyBufferToBuffer);
|
|
|
|
copy->source = source;
|
|
|
|
copy->sourceOffset = sourceOffset;
|
|
|
|
copy->destination = destination;
|
|
|
|
copy->destinationOffset = destinationOffset;
|
|
|
|
copy->size = size;
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-02-15 12:54:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APICopyBufferToTexture(const ImageCopyBuffer* source,
|
|
|
|
const ImageCopyTexture* destination,
|
|
|
|
const Extent3D* copySize) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2020-05-05 08:33:55 +00:00
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
2021-03-04 18:13:45 +00:00
|
|
|
DAWN_TRY(ValidateImageCopyBuffer(GetDevice(), *source));
|
2020-07-07 10:18:51 +00:00
|
|
|
DAWN_TRY(ValidateCanUseAs(source->buffer, wgpu::BufferUsage::CopySrc));
|
2020-05-05 08:33:55 +00:00
|
|
|
|
2021-05-13 17:51:23 +00:00
|
|
|
DAWN_TRY(ValidateImageCopyTexture(GetDevice(), *destination, *copySize));
|
2020-07-07 10:18:51 +00:00
|
|
|
DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst));
|
2020-09-01 18:40:18 +00:00
|
|
|
DAWN_TRY(ValidateTextureSampleCountInBufferCopyCommands(destination->texture));
|
2019-07-24 18:15:24 +00:00
|
|
|
|
2020-10-27 19:30:57 +00:00
|
|
|
DAWN_TRY(ValidateLinearToDepthStencilCopyRestrictions(*destination));
|
2020-07-17 09:44:46 +00:00
|
|
|
// We validate texture copy range before validating linear texture data,
|
|
|
|
// because in the latter we divide copyExtent.width by blockWidth and
|
|
|
|
// copyExtent.height by blockHeight while the divisibility conditions are
|
|
|
|
// checked in validating texture copy range.
|
2021-05-13 17:51:23 +00:00
|
|
|
DAWN_TRY(ValidateTextureCopyRange(GetDevice(), *destination, *copySize));
|
2020-10-12 23:13:53 +00:00
|
|
|
}
|
|
|
|
const TexelBlockInfo& blockInfo =
|
2020-10-15 09:05:03 +00:00
|
|
|
destination->texture->GetFormat().GetAspectInfo(destination->aspect).block;
|
2020-10-12 23:13:53 +00:00
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
2021-06-24 05:38:00 +00:00
|
|
|
DAWN_TRY(ValidateLinearTextureCopyOffset(
|
2021-07-04 18:31:29 +00:00
|
|
|
source->layout, blockInfo,
|
|
|
|
destination->texture->GetFormat().HasDepthOrStencil()));
|
|
|
|
DAWN_TRY(ValidateLinearTextureData(source->layout, source->buffer->GetSize(),
|
|
|
|
blockInfo, *copySize));
|
2020-05-05 08:33:55 +00:00
|
|
|
|
2020-05-13 17:26:05 +00:00
|
|
|
mTopLevelBuffers.insert(source->buffer);
|
2020-05-05 08:33:55 +00:00
|
|
|
mTopLevelTextures.insert(destination->texture);
|
|
|
|
}
|
|
|
|
|
2021-07-04 18:31:29 +00:00
|
|
|
TextureDataLayout srcLayout = source->layout;
|
2021-05-13 17:51:23 +00:00
|
|
|
ApplyDefaultTextureDataLayoutOptions(&srcLayout, blockInfo, *copySize);
|
2020-08-05 16:43:24 +00:00
|
|
|
|
2021-06-10 02:40:18 +00:00
|
|
|
CopyBufferToTextureCmd* copy =
|
|
|
|
allocator->Allocate<CopyBufferToTextureCmd>(Command::CopyBufferToTexture);
|
|
|
|
copy->source.buffer = source->buffer;
|
|
|
|
copy->source.offset = srcLayout.offset;
|
|
|
|
copy->source.bytesPerRow = srcLayout.bytesPerRow;
|
|
|
|
copy->source.rowsPerImage = srcLayout.rowsPerImage;
|
|
|
|
copy->destination.texture = destination->texture;
|
|
|
|
copy->destination.origin = destination->origin;
|
|
|
|
copy->destination.mipLevel = destination->mipLevel;
|
|
|
|
copy->destination.aspect =
|
|
|
|
ConvertAspect(destination->texture->GetFormat(), destination->aspect);
|
|
|
|
copy->copySize = *copySize;
|
2020-06-16 03:05:17 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-02-15 12:54:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APICopyTextureToBuffer(const ImageCopyTexture* source,
|
|
|
|
const ImageCopyBuffer* destination,
|
|
|
|
const Extent3D* copySize) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2020-05-05 08:33:55 +00:00
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
2021-05-13 17:51:23 +00:00
|
|
|
DAWN_TRY(ValidateImageCopyTexture(GetDevice(), *source, *copySize));
|
2020-07-07 10:18:51 +00:00
|
|
|
DAWN_TRY(ValidateCanUseAs(source->texture, wgpu::TextureUsage::CopySrc));
|
2020-09-01 18:40:18 +00:00
|
|
|
DAWN_TRY(ValidateTextureSampleCountInBufferCopyCommands(source->texture));
|
2020-10-27 19:30:57 +00:00
|
|
|
DAWN_TRY(ValidateTextureDepthStencilToBufferCopyRestrictions(*source));
|
2020-05-05 08:33:55 +00:00
|
|
|
|
2021-03-04 18:13:45 +00:00
|
|
|
DAWN_TRY(ValidateImageCopyBuffer(GetDevice(), *destination));
|
2020-07-07 10:18:51 +00:00
|
|
|
DAWN_TRY(ValidateCanUseAs(destination->buffer, wgpu::BufferUsage::CopyDst));
|
2020-05-05 08:33:55 +00:00
|
|
|
|
2020-07-17 09:44:46 +00:00
|
|
|
// We validate texture copy range before validating linear texture data,
|
|
|
|
// because in the latter we divide copyExtent.width by blockWidth and
|
|
|
|
// copyExtent.height by blockHeight while the divisibility conditions are
|
|
|
|
// checked in validating texture copy range.
|
2021-05-13 17:51:23 +00:00
|
|
|
DAWN_TRY(ValidateTextureCopyRange(GetDevice(), *source, *copySize));
|
2020-10-12 23:13:53 +00:00
|
|
|
}
|
|
|
|
const TexelBlockInfo& blockInfo =
|
2020-10-15 09:05:03 +00:00
|
|
|
source->texture->GetFormat().GetAspectInfo(source->aspect).block;
|
2020-10-12 23:13:53 +00:00
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
2021-06-24 05:38:00 +00:00
|
|
|
DAWN_TRY(ValidateLinearTextureCopyOffset(
|
2021-07-04 18:31:29 +00:00
|
|
|
destination->layout, blockInfo,
|
|
|
|
source->texture->GetFormat().HasDepthOrStencil()));
|
|
|
|
DAWN_TRY(ValidateLinearTextureData(
|
|
|
|
destination->layout, destination->buffer->GetSize(), blockInfo, *copySize));
|
2020-05-05 08:33:55 +00:00
|
|
|
|
|
|
|
mTopLevelTextures.insert(source->texture);
|
2020-05-13 17:26:05 +00:00
|
|
|
mTopLevelBuffers.insert(destination->buffer);
|
2020-05-05 08:33:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 18:31:29 +00:00
|
|
|
TextureDataLayout dstLayout = destination->layout;
|
2021-05-13 17:51:23 +00:00
|
|
|
ApplyDefaultTextureDataLayoutOptions(&dstLayout, blockInfo, *copySize);
|
2020-08-05 16:43:24 +00:00
|
|
|
|
2021-06-10 02:40:18 +00:00
|
|
|
CopyTextureToBufferCmd* copy =
|
|
|
|
allocator->Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer);
|
|
|
|
copy->source.texture = source->texture;
|
|
|
|
copy->source.origin = source->origin;
|
|
|
|
copy->source.mipLevel = source->mipLevel;
|
|
|
|
copy->source.aspect = ConvertAspect(source->texture->GetFormat(), source->aspect);
|
|
|
|
copy->destination.buffer = destination->buffer;
|
|
|
|
copy->destination.offset = dstLayout.offset;
|
|
|
|
copy->destination.bytesPerRow = dstLayout.bytesPerRow;
|
|
|
|
copy->destination.rowsPerImage = dstLayout.rowsPerImage;
|
|
|
|
copy->copySize = *copySize;
|
2020-06-16 03:05:17 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-02-15 12:54:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APICopyTextureToTexture(const ImageCopyTexture* source,
|
|
|
|
const ImageCopyTexture* destination,
|
|
|
|
const Extent3D* copySize) {
|
2021-08-05 22:55:09 +00:00
|
|
|
APICopyTextureToTextureHelper<false>(source, destination, copySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandEncoder::APICopyTextureToTextureInternal(const ImageCopyTexture* source,
|
|
|
|
const ImageCopyTexture* destination,
|
|
|
|
const Extent3D* copySize) {
|
|
|
|
APICopyTextureToTextureHelper<true>(source, destination, copySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool Internal>
|
|
|
|
void CommandEncoder::APICopyTextureToTextureHelper(const ImageCopyTexture* source,
|
|
|
|
const ImageCopyTexture* destination,
|
|
|
|
const Extent3D* copySize) {
|
2019-07-24 18:15:24 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2020-05-05 08:33:55 +00:00
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(source->texture));
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(destination->texture));
|
|
|
|
|
2021-05-13 17:51:23 +00:00
|
|
|
DAWN_TRY(ValidateImageCopyTexture(GetDevice(), *source, *copySize));
|
|
|
|
DAWN_TRY(ValidateImageCopyTexture(GetDevice(), *destination, *copySize));
|
2020-09-02 18:50:09 +00:00
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
DAWN_TRY(
|
2021-05-13 17:51:23 +00:00
|
|
|
ValidateTextureToTextureCopyRestrictions(*source, *destination, *copySize));
|
2020-05-05 08:33:55 +00:00
|
|
|
|
2021-05-13 17:51:23 +00:00
|
|
|
DAWN_TRY(ValidateTextureCopyRange(GetDevice(), *source, *copySize));
|
|
|
|
DAWN_TRY(ValidateTextureCopyRange(GetDevice(), *destination, *copySize));
|
2020-05-05 08:33:55 +00:00
|
|
|
|
2021-08-05 22:55:09 +00:00
|
|
|
// For internal usages (CopyToCopyInternal) we don't care if the user has added
|
|
|
|
// CopySrc as a usage for this texture, but we will always add it internally.
|
|
|
|
if (Internal) {
|
|
|
|
DAWN_TRY(
|
|
|
|
ValidateInternalCanUseAs(source->texture, wgpu::TextureUsage::CopySrc));
|
|
|
|
DAWN_TRY(ValidateInternalCanUseAs(destination->texture,
|
|
|
|
wgpu::TextureUsage::CopyDst));
|
|
|
|
} else {
|
|
|
|
DAWN_TRY(ValidateCanUseAs(source->texture, wgpu::TextureUsage::CopySrc));
|
|
|
|
DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst));
|
|
|
|
}
|
2020-05-05 08:33:55 +00:00
|
|
|
|
|
|
|
mTopLevelTextures.insert(source->texture);
|
|
|
|
mTopLevelTextures.insert(destination->texture);
|
|
|
|
}
|
2019-07-24 18:15:24 +00:00
|
|
|
|
2021-06-10 02:40:18 +00:00
|
|
|
CopyTextureToTextureCmd* copy =
|
|
|
|
allocator->Allocate<CopyTextureToTextureCmd>(Command::CopyTextureToTexture);
|
|
|
|
copy->source.texture = source->texture;
|
|
|
|
copy->source.origin = source->origin;
|
|
|
|
copy->source.mipLevel = source->mipLevel;
|
|
|
|
copy->source.aspect = ConvertAspect(source->texture->GetFormat(), source->aspect);
|
|
|
|
copy->destination.texture = destination->texture;
|
|
|
|
copy->destination.origin = destination->origin;
|
|
|
|
copy->destination.mipLevel = destination->mipLevel;
|
|
|
|
copy->destination.aspect =
|
|
|
|
ConvertAspect(destination->texture->GetFormat(), destination->aspect);
|
|
|
|
copy->copySize = *copySize;
|
2019-03-26 11:06:23 +00:00
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
return {};
|
|
|
|
});
|
2019-03-26 11:06:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APIInjectValidationError(const char* message) {
|
2020-11-14 01:24:03 +00:00
|
|
|
if (mEncodingContext.CheckCurrentEncoder(this)) {
|
2021-05-05 17:37:43 +00:00
|
|
|
mEncodingContext.HandleError(DAWN_VALIDATION_ERROR(message));
|
2020-11-14 01:24:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APIInsertDebugMarker(const char* groupLabel) {
|
2019-09-10 08:20:40 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
InsertDebugMarkerCmd* cmd =
|
|
|
|
allocator->Allocate<InsertDebugMarkerCmd>(Command::InsertDebugMarker);
|
|
|
|
cmd->length = strlen(groupLabel);
|
|
|
|
|
|
|
|
char* label = allocator->AllocateData<char>(cmd->length + 1);
|
|
|
|
memcpy(label, groupLabel, cmd->length + 1);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APIPopDebugGroup() {
|
2019-09-10 08:20:40 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
2021-01-27 16:03:32 +00:00
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
if (mDebugGroupStackSize == 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Pop must be balanced by a corresponding Push.");
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 08:20:40 +00:00
|
|
|
allocator->Allocate<PopDebugGroupCmd>(Command::PopDebugGroup);
|
2021-01-27 16:03:32 +00:00
|
|
|
mDebugGroupStackSize--;
|
2019-09-10 08:20:40 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APIPushDebugGroup(const char* groupLabel) {
|
2019-09-10 08:20:40 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
PushDebugGroupCmd* cmd =
|
|
|
|
allocator->Allocate<PushDebugGroupCmd>(Command::PushDebugGroup);
|
|
|
|
cmd->length = strlen(groupLabel);
|
|
|
|
|
|
|
|
char* label = allocator->AllocateData<char>(cmd->length + 1);
|
|
|
|
memcpy(label, groupLabel, cmd->length + 1);
|
|
|
|
|
2021-01-27 16:03:32 +00:00
|
|
|
mDebugGroupStackSize++;
|
|
|
|
|
2019-09-10 08:20:40 +00:00
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APIResolveQuerySet(QuerySetBase* querySet,
|
|
|
|
uint32_t firstQuery,
|
|
|
|
uint32_t queryCount,
|
|
|
|
BufferBase* destination,
|
|
|
|
uint64_t destinationOffset) {
|
2020-07-17 09:02:46 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(querySet));
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(destination));
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateQuerySetResolve(querySet, firstQuery, queryCount, destination,
|
|
|
|
destinationOffset));
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateCanUseAs(destination, wgpu::BufferUsage::QueryResolve));
|
|
|
|
|
|
|
|
TrackUsedQuerySet(querySet);
|
|
|
|
mTopLevelBuffers.insert(destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolveQuerySetCmd* cmd =
|
|
|
|
allocator->Allocate<ResolveQuerySetCmd>(Command::ResolveQuerySet);
|
|
|
|
cmd->querySet = querySet;
|
|
|
|
cmd->firstQuery = firstQuery;
|
|
|
|
cmd->queryCount = queryCount;
|
|
|
|
cmd->destination = destination;
|
|
|
|
cmd->destinationOffset = destinationOffset;
|
|
|
|
|
2021-01-14 03:26:08 +00:00
|
|
|
// Encode internal compute pipeline for timestamp query
|
2021-03-31 19:31:42 +00:00
|
|
|
if (querySet->GetQueryType() == wgpu::QueryType::Timestamp) {
|
2021-05-18 01:13:08 +00:00
|
|
|
DAWN_TRY(EncodeTimestampsToNanosecondsConversion(
|
|
|
|
this, querySet, firstQuery, queryCount, destination, destinationOffset));
|
2021-01-14 03:26:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 09:02:46 +00:00
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-20 19:25:05 +00:00
|
|
|
void CommandEncoder::APIWriteBuffer(BufferBase* buffer,
|
|
|
|
uint64_t bufferOffset,
|
|
|
|
const uint8_t* data,
|
|
|
|
uint64_t size) {
|
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(ValidateWriteBuffer(GetDevice(), buffer, bufferOffset, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteBufferCmd* cmd = allocator->Allocate<WriteBufferCmd>(Command::WriteBuffer);
|
|
|
|
cmd->buffer = buffer;
|
|
|
|
cmd->offset = bufferOffset;
|
|
|
|
cmd->size = size;
|
|
|
|
|
|
|
|
uint8_t* inlinedData = allocator->AllocateData<uint8_t>(size);
|
|
|
|
memcpy(inlinedData, data, size);
|
|
|
|
|
|
|
|
mTopLevelBuffers.insert(buffer);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
void CommandEncoder::APIWriteTimestamp(QuerySetBase* querySet, uint32_t queryIndex) {
|
2020-07-01 10:48:16 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(querySet));
|
2020-11-16 02:24:06 +00:00
|
|
|
DAWN_TRY(ValidateTimestampQuery(querySet, queryIndex));
|
2020-07-01 10:48:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 02:24:06 +00:00
|
|
|
TrackQueryAvailability(querySet, queryIndex);
|
2020-10-21 08:38:31 +00:00
|
|
|
|
2020-07-01 10:48:16 +00:00
|
|
|
WriteTimestampCmd* cmd =
|
|
|
|
allocator->Allocate<WriteTimestampCmd>(Command::WriteTimestamp);
|
|
|
|
cmd->querySet = querySet;
|
|
|
|
cmd->queryIndex = queryIndex;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:02:05 +00:00
|
|
|
CommandBufferBase* CommandEncoder::APIFinish(const CommandBufferDescriptor* descriptor) {
|
2021-03-31 18:36:32 +00:00
|
|
|
Ref<CommandBufferBase> commandBuffer;
|
|
|
|
if (GetDevice()->ConsumedError(FinishInternal(descriptor), &commandBuffer)) {
|
|
|
|
return CommandBufferBase::MakeError(GetDevice());
|
|
|
|
}
|
|
|
|
ASSERT(!IsError());
|
|
|
|
return commandBuffer.Detach();
|
|
|
|
}
|
|
|
|
|
2021-09-23 00:15:19 +00:00
|
|
|
void CommandEncoder::EncodeSetValidatedBufferLocationsInternal(
|
|
|
|
std::vector<DeferredBufferLocationUpdate> updates) {
|
|
|
|
ASSERT(GetDevice()->IsValidationEnabled());
|
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
SetValidatedBufferLocationsInternalCmd* cmd =
|
|
|
|
allocator->Allocate<SetValidatedBufferLocationsInternalCmd>(
|
|
|
|
Command::SetValidatedBufferLocationsInternal);
|
|
|
|
cmd->updates = std::move(updates);
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-31 18:36:32 +00:00
|
|
|
ResultOrError<Ref<CommandBufferBase>> CommandEncoder::FinishInternal(
|
|
|
|
const CommandBufferDescriptor* descriptor) {
|
2019-11-21 22:09:41 +00:00
|
|
|
DeviceBase* device = GetDevice();
|
2021-03-31 18:36:32 +00:00
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
// Even if mEncodingContext.Finish() validation fails, calling it will mutate the internal
|
|
|
|
// state of the encoding context. The internal state is set to finished, and subsequent
|
|
|
|
// calls to encode commands will generate errors.
|
2021-03-31 18:36:32 +00:00
|
|
|
DAWN_TRY(mEncodingContext.Finish());
|
|
|
|
DAWN_TRY(device->ValidateIsAlive());
|
|
|
|
|
|
|
|
if (device->IsValidationEnabled()) {
|
2021-05-05 15:41:13 +00:00
|
|
|
DAWN_TRY(ValidateFinish());
|
2019-02-15 12:54:08 +00:00
|
|
|
}
|
2019-11-21 22:09:41 +00:00
|
|
|
return device->CreateCommandBuffer(this, descriptor);
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of the command buffer validation that can be precomputed before submit
|
2021-05-05 15:41:13 +00:00
|
|
|
MaybeError CommandEncoder::ValidateFinish() const {
|
2019-11-13 17:00:37 +00:00
|
|
|
TRACE_EVENT0(GetDevice()->GetPlatform(), Validation, "CommandEncoder::ValidateFinish");
|
2019-02-15 12:54:08 +00:00
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(this));
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2021-05-05 19:55:23 +00:00
|
|
|
for (const RenderPassResourceUsage& passUsage : mEncodingContext.GetRenderPassUsages()) {
|
2021-05-05 15:41:13 +00:00
|
|
|
DAWN_TRY(ValidateSyncScopeResourceUsage(passUsage));
|
2019-11-21 22:09:41 +00:00
|
|
|
}
|
2021-05-06 19:20:14 +00:00
|
|
|
|
|
|
|
for (const ComputePassResourceUsage& passUsage : mEncodingContext.GetComputePassUsages()) {
|
|
|
|
for (const SyncScopeResourceUsage& scope : passUsage.dispatchUsages) {
|
|
|
|
DAWN_TRY(ValidateSyncScopeResourceUsage(scope));
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2021-01-27 16:03:32 +00:00
|
|
|
if (mDebugGroupStackSize != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Each Push must be balanced by a corresponding Pop.");
|
|
|
|
}
|
2019-09-10 08:20:40 +00:00
|
|
|
|
2019-02-15 12:54:08 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dawn_native
|