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"
|
|
|
|
#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"
|
|
|
|
#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-28 01:01:32 +00:00
|
|
|
// TODO(jiawei.shao@intel.com): add validations on the texture-to-texture copies within the
|
|
|
|
// same texture.
|
2020-05-05 08:33:55 +00:00
|
|
|
MaybeError ValidateCopySizeFitsInTexture(const TextureCopyView& textureCopy,
|
2019-02-20 11:46:16 +00:00
|
|
|
const Extent3D& copySize) {
|
2020-05-05 08:33:55 +00:00
|
|
|
const TextureBase* texture = textureCopy.texture;
|
2019-07-08 09:41:51 +00:00
|
|
|
if (textureCopy.mipLevel >= texture->GetNumMipLevels()) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Copy mipLevel out of range");
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 01:01:32 +00:00
|
|
|
if (static_cast<uint64_t>(textureCopy.arrayLayer) +
|
|
|
|
static_cast<uint64_t>(copySize.depth) >
|
|
|
|
static_cast<uint64_t>(texture->GetArrayLayers())) {
|
2019-07-08 09:41:51 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Copy arrayLayer out of range");
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 09:41:51 +00:00
|
|
|
Extent3D extent = texture->GetMipLevelPhysicalSize(textureCopy.mipLevel);
|
|
|
|
|
2019-02-20 11:46:16 +00:00
|
|
|
// All texture dimensions are in uint32_t so by doing checks in uint64_t we avoid
|
|
|
|
// overflows.
|
2020-05-28 01:01:32 +00:00
|
|
|
if (static_cast<uint64_t>(textureCopy.origin.x) +
|
|
|
|
static_cast<uint64_t>(copySize.width) >
|
2019-06-25 03:09:26 +00:00
|
|
|
static_cast<uint64_t>(extent.width) ||
|
2020-05-28 01:01:32 +00:00
|
|
|
static_cast<uint64_t>(textureCopy.origin.y) +
|
|
|
|
static_cast<uint64_t>(copySize.height) >
|
2019-06-25 03:09:26 +00:00
|
|
|
static_cast<uint64_t>(extent.height)) {
|
2019-02-20 11:46:16 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Copy would touch outside of the texture");
|
|
|
|
}
|
|
|
|
|
2020-05-28 01:01:32 +00:00
|
|
|
// TODO(cwallez@chromium.org): Check the depth bound differently for 3D textures.
|
|
|
|
if (textureCopy.origin.z != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("No support for z != 0 for now");
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-06-07 03:06:47 +00:00
|
|
|
MaybeError ValidateCopySizeFitsInBuffer(const Ref<BufferBase>& buffer,
|
|
|
|
uint64_t offset,
|
|
|
|
uint64_t size) {
|
2019-04-05 20:51:29 +00:00
|
|
|
uint64_t bufferSize = buffer->GetSize();
|
2019-06-07 03:06:47 +00:00
|
|
|
bool fitsInBuffer = offset <= bufferSize && (size <= (bufferSize - offset));
|
|
|
|
if (!fitsInBuffer) {
|
2019-02-20 11:46:16 +00:00
|
|
|
return DAWN_VALIDATION_ERROR("Copy would overflow the buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
MaybeError ValidateCopySizeFitsInBuffer(const BufferCopyView& bufferCopy,
|
|
|
|
uint64_t dataSize) {
|
2019-06-07 03:06:47 +00:00
|
|
|
return ValidateCopySizeFitsInBuffer(bufferCopy.buffer, bufferCopy.offset, dataSize);
|
|
|
|
}
|
|
|
|
|
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-05-05 08:33:55 +00:00
|
|
|
MaybeError ValidateTexelBufferOffset(const BufferCopyView& bufferCopy,
|
|
|
|
const Format& format) {
|
2019-06-21 10:16:15 +00:00
|
|
|
if (bufferCopy.offset % format.blockByteSize != 0) {
|
2019-06-18 01:06:09 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Buffer offset must be a multiple of the texel or block size");
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:02:43 +00:00
|
|
|
MaybeError ValidateRowsPerImage(const Format& format,
|
|
|
|
uint32_t rowsPerImage,
|
|
|
|
uint32_t copyHeight) {
|
|
|
|
if (rowsPerImage < copyHeight) {
|
|
|
|
return DAWN_VALIDATION_ERROR("rowsPerImage must not be less than the copy height.");
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:02:43 +00:00
|
|
|
if (rowsPerImage % format.blockHeight != 0) {
|
2019-07-02 00:06:55 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
2020-04-27 16:42:40 +00:00
|
|
|
"rowsPerImage must be a multiple of compressed texture format block height");
|
2019-07-02 00:06:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 11:46:16 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-03-04 12:01:59 +00:00
|
|
|
MaybeError ValidateTextureSampleCountInCopyCommands(const TextureBase* texture) {
|
|
|
|
if (texture->GetSampleCount() > 1) {
|
|
|
|
return DAWN_VALIDATION_ERROR("The sample count of textures must be 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
MaybeError ValidateEntireSubresourceCopied(const TextureCopyView& src,
|
|
|
|
const TextureCopyView& dst,
|
2019-03-26 11:06:23 +00:00
|
|
|
const Extent3D& copySize) {
|
2020-05-05 08:33:55 +00:00
|
|
|
Extent3D srcSize = src.texture->GetSize();
|
2019-03-26 11:06:23 +00:00
|
|
|
|
|
|
|
if (dst.origin.x != 0 || dst.origin.y != 0 || dst.origin.z != 0 ||
|
|
|
|
srcSize.width != copySize.width || srcSize.height != copySize.height ||
|
|
|
|
srcSize.depth != copySize.depth) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The entire subresource must be copied when using a depth/stencil texture or "
|
|
|
|
"when samples are greater than 1.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
MaybeError ValidateTextureToTextureCopyRestrictions(const TextureCopyView& src,
|
|
|
|
const TextureCopyView& dst,
|
2019-03-26 11:06:23 +00:00
|
|
|
const Extent3D& copySize) {
|
2020-05-05 08:33:55 +00:00
|
|
|
const uint32_t srcSamples = src.texture->GetSampleCount();
|
|
|
|
const uint32_t dstSamples = dst.texture->GetSampleCount();
|
2019-03-26 11:06:23 +00:00
|
|
|
|
|
|
|
if (srcSamples != dstSamples) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Source and destination textures must have matching sample counts.");
|
|
|
|
} else if (srcSamples > 1) {
|
|
|
|
// D3D12 requires entire subresource to be copied when using CopyTextureRegion when
|
|
|
|
// samples > 1.
|
|
|
|
DAWN_TRY(ValidateEntireSubresourceCopied(src, dst, copySize));
|
|
|
|
}
|
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
if (src.texture->GetFormat().format != dst.texture->GetFormat().format) {
|
2019-03-26 11:06:23 +00:00
|
|
|
// Metal requires texture-to-texture copies be the same format
|
|
|
|
return DAWN_VALIDATION_ERROR("Source and destination texture formats must match.");
|
2019-06-21 10:16:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
if (src.texture->GetFormat().HasDepthOrStencil()) {
|
2019-03-26 11:06:23 +00:00
|
|
|
// D3D12 requires entire subresource to be copied when using CopyTextureRegion is
|
|
|
|
// used with depth/stencil.
|
|
|
|
DAWN_TRY(ValidateEntireSubresourceCopied(src, dst, copySize));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-06-21 10:16:15 +00:00
|
|
|
MaybeError ComputeTextureCopyBufferSize(const Format& textureFormat,
|
2019-06-12 08:53:45 +00:00
|
|
|
const Extent3D& copySize,
|
2020-04-24 10:02:43 +00:00
|
|
|
uint32_t bytesPerRow,
|
|
|
|
uint32_t rowsPerImage,
|
2019-02-20 11:46:16 +00:00
|
|
|
uint32_t* bufferSize) {
|
2020-04-24 10:02:43 +00:00
|
|
|
ASSERT(rowsPerImage >= copySize.height);
|
2019-06-21 10:16:15 +00:00
|
|
|
uint32_t blockByteSize = textureFormat.blockByteSize;
|
|
|
|
uint32_t blockWidth = textureFormat.blockWidth;
|
|
|
|
uint32_t blockHeight = textureFormat.blockHeight;
|
2019-06-18 01:06:09 +00:00
|
|
|
|
2019-02-20 11:46:16 +00:00
|
|
|
// TODO(cwallez@chromium.org): check for overflows
|
2020-04-24 10:02:43 +00:00
|
|
|
uint32_t slicePitch = bytesPerRow * rowsPerImage / blockWidth;
|
|
|
|
uint32_t sliceSize = bytesPerRow * (copySize.height / blockHeight - 1) +
|
2019-06-21 10:16:15 +00:00
|
|
|
(copySize.width / blockWidth) * blockByteSize;
|
2019-02-20 11:46:16 +00:00
|
|
|
*bufferSize = (slicePitch * (copySize.depth - 1)) + sliceSize;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-24 10:02:43 +00:00
|
|
|
uint32_t ComputeDefaultBytesPerRow(const Format& format, uint32_t width) {
|
2019-06-21 10:16:15 +00:00
|
|
|
return width / format.blockWidth * format.blockByteSize;
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:02:43 +00:00
|
|
|
MaybeError ValidateBytesPerRow(const Format& format,
|
|
|
|
const Extent3D& copySize,
|
|
|
|
uint32_t bytesPerRow) {
|
|
|
|
if (bytesPerRow % kTextureBytesPerRowAlignment != 0) {
|
|
|
|
return DAWN_VALIDATION_ERROR("bytesPerRow must be a multiple of 256");
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:02:43 +00:00
|
|
|
if (bytesPerRow < copySize.width / format.blockWidth * format.blockByteSize) {
|
2019-02-20 11:46:16 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
2020-04-24 10:02:43 +00:00
|
|
|
"bytesPerRow must not be less than the number of bytes per row");
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-06-21 10:16:15 +00:00
|
|
|
MaybeError ValidateImageOrigin(const Format& format, const Origin3D& offset) {
|
|
|
|
if (offset.x % format.blockWidth != 0) {
|
2019-06-18 01:06:09 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Offset.x must be a multiple of compressed texture format block width");
|
|
|
|
}
|
|
|
|
|
2019-06-21 10:16:15 +00:00
|
|
|
if (offset.y % format.blockHeight != 0) {
|
2019-06-18 01:06:09 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Offset.y must be a multiple of compressed texture format block height");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-06-21 10:16:15 +00:00
|
|
|
MaybeError ValidateImageCopySize(const Format& format, const Extent3D& extent) {
|
|
|
|
if (extent.width % format.blockWidth != 0) {
|
2019-06-18 01:06:09 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Extent.width must be a multiple of compressed texture format block width");
|
|
|
|
}
|
|
|
|
|
2019-06-21 10:16:15 +00:00
|
|
|
if (extent.height % format.blockHeight != 0) {
|
2019-06-18 01:06:09 +00:00
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"Extent.height must be a multiple of compressed texture format block height");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
MaybeError ValidateCanUseAs(const BufferBase* buffer, wgpu::BufferUsage usage) {
|
2019-10-23 11:57:41 +00:00
|
|
|
ASSERT(wgpu::HasZeroOrOneBits(usage));
|
2019-02-20 11:46:16 +00:00
|
|
|
if (!(buffer->GetUsage() & usage)) {
|
|
|
|
return DAWN_VALIDATION_ERROR("buffer doesn't have the required usage.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
MaybeError ValidateCanUseAs(const TextureBase* texture, wgpu::TextureUsage usage) {
|
2019-10-23 11:57:41 +00:00
|
|
|
ASSERT(wgpu::HasZeroOrOneBits(usage));
|
2019-02-20 11:46:16 +00:00
|
|
|
if (!(texture->GetUsage() & usage)) {
|
|
|
|
return DAWN_VALIDATION_ERROR("texture doesn't have the required usage.");
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const Extent3D& textureSize = attachment->GetTexture()->GetSize();
|
|
|
|
const uint32_t attachmentWidth = textureSize.width >> attachment->GetBaseMipLevel();
|
|
|
|
const uint32_t attachmentHeight = textureSize.height >> attachment->GetBaseMipLevel();
|
|
|
|
|
|
|
|
if (*width == 0) {
|
|
|
|
DAWN_ASSERT(*height == 0);
|
|
|
|
*width = attachmentWidth;
|
|
|
|
*height = attachmentHeight;
|
|
|
|
DAWN_ASSERT(*width != 0 && *height != 0);
|
|
|
|
} else if (*width != attachmentWidth || *height != attachmentHeight) {
|
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeError ValidateResolveTarget(
|
|
|
|
const DeviceBase* device,
|
2019-09-20 22:59:47 +00:00
|
|
|
const RenderPassColorAttachmentDescriptor& colorAttachment) {
|
|
|
|
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;
|
|
|
|
const TextureViewBase* attachment = colorAttachment.attachment;
|
|
|
|
DAWN_TRY(device->ValidateObject(colorAttachment.resolveTarget));
|
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");
|
|
|
|
}
|
|
|
|
|
2019-09-20 22:59:47 +00:00
|
|
|
uint32_t colorAttachmentBaseMipLevel = attachment->GetBaseMipLevel();
|
|
|
|
const Extent3D& colorTextureSize = attachment->GetTexture()->GetSize();
|
2019-03-11 18:41:02 +00:00
|
|
|
uint32_t colorAttachmentWidth = colorTextureSize.width >> colorAttachmentBaseMipLevel;
|
|
|
|
uint32_t colorAttachmentHeight = colorTextureSize.height >> colorAttachmentBaseMipLevel;
|
|
|
|
|
2019-09-20 22:59:47 +00:00
|
|
|
uint32_t resolveTargetBaseMipLevel = resolveTarget->GetBaseMipLevel();
|
|
|
|
const Extent3D& resolveTextureSize = resolveTarget->GetTexture()->GetSize();
|
2019-03-11 18:41:02 +00:00
|
|
|
uint32_t resolveTargetWidth = resolveTextureSize.width >> resolveTargetBaseMipLevel;
|
|
|
|
uint32_t resolveTargetHeight = resolveTextureSize.height >> resolveTargetBaseMipLevel;
|
|
|
|
if (colorAttachmentWidth != resolveTargetWidth ||
|
|
|
|
colorAttachmentHeight != resolveTargetHeight) {
|
|
|
|
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(
|
|
|
|
const DeviceBase* device,
|
2019-09-20 22:59:47 +00:00
|
|
|
const RenderPassColorAttachmentDescriptor& 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) {
|
2019-09-20 22:59:47 +00:00
|
|
|
DAWN_TRY(device->ValidateObject(colorAttachment.attachment));
|
2019-02-27 09:21:56 +00:00
|
|
|
|
2019-09-20 22:59:47 +00:00
|
|
|
const TextureViewBase* attachment = colorAttachment.attachment;
|
2019-06-21 10:16:15 +00:00
|
|
|
if (!attachment->GetFormat().IsColor() || !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));
|
|
|
|
DAWN_TRY(ValidateStoreOp(colorAttachment.storeOp));
|
|
|
|
|
|
|
|
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(
|
|
|
|
const DeviceBase* device,
|
|
|
|
const RenderPassDepthStencilAttachmentDescriptor* depthStencilAttachment,
|
|
|
|
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);
|
|
|
|
|
|
|
|
DAWN_TRY(device->ValidateObject(depthStencilAttachment->attachment));
|
|
|
|
|
|
|
|
const TextureViewBase* attachment = depthStencilAttachment->attachment;
|
2019-06-21 10:16:15 +00:00
|
|
|
if (!attachment->GetFormat().HasDepthOrStencil() ||
|
|
|
|
!attachment->GetFormat().isRenderable) {
|
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");
|
|
|
|
}
|
|
|
|
|
2019-12-12 01:29:01 +00:00
|
|
|
DAWN_TRY(ValidateLoadOp(depthStencilAttachment->depthLoadOp));
|
|
|
|
DAWN_TRY(ValidateLoadOp(depthStencilAttachment->stencilLoadOp));
|
|
|
|
DAWN_TRY(ValidateStoreOp(depthStencilAttachment->depthStoreOp));
|
|
|
|
DAWN_TRY(ValidateStoreOp(depthStencilAttachment->stencilStoreOp));
|
|
|
|
|
|
|
|
if (depthStencilAttachment->depthLoadOp == wgpu::LoadOp::Clear &&
|
|
|
|
std::isnan(depthStencilAttachment->clearDepth)) {
|
|
|
|
return DAWN_VALIDATION_ERROR("Depth clear value cannot be NaN");
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:08:28 +00:00
|
|
|
// This validates that the depth storeOp and stencil storeOps are the same
|
|
|
|
if (depthStencilAttachment->depthStoreOp != depthStencilAttachment->stencilStoreOp) {
|
|
|
|
return DAWN_VALIDATION_ERROR(
|
|
|
|
"The depth storeOp and stencil storeOp are not the same");
|
|
|
|
}
|
|
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
|
2019-03-19 01:12:01 +00:00
|
|
|
MaybeError ValidateRenderPassDescriptor(const 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
|
|
|
}
|
|
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
|
2019-02-20 11:46:16 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
CommandEncoder::CommandEncoder(DeviceBase* device, const CommandEncoderDescriptor*)
|
2019-07-24 18:15:24 +00:00
|
|
|
: ObjectBase(device), mEncodingContext(device, this) {
|
2019-02-20 11:46:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
CommandBufferResourceUsage CommandEncoder::AcquireResourceUsages() {
|
2019-11-21 22:09:41 +00:00
|
|
|
return CommandBufferResourceUsage{mEncodingContext.AcquirePassUsages(),
|
|
|
|
std::move(mTopLevelBuffers),
|
|
|
|
std::move(mTopLevelTextures)};
|
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
|
|
|
}
|
|
|
|
|
2019-02-20 11:46:16 +00:00
|
|
|
// Implementation of the API's command recording methods
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
ComputePassEncoder* CommandEncoder::BeginComputePass(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
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
RenderPassEncoder* CommandEncoder::BeginRenderPass(const RenderPassDescriptor* descriptor) {
|
2019-02-27 09:21:56 +00:00
|
|
|
DeviceBase* device = GetDevice();
|
|
|
|
|
2020-04-22 00:55:43 +00:00
|
|
|
PassResourceUsageTracker usageTracker(PassType::Render);
|
2019-07-24 18:15:24 +00:00
|
|
|
bool success =
|
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
uint32_t width = 0;
|
|
|
|
uint32_t height = 0;
|
|
|
|
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);
|
|
|
|
|
|
|
|
for (uint32_t i : IterateBitSet(cmd->attachmentState->GetColorAttachmentsMask())) {
|
2019-11-21 22:09:41 +00:00
|
|
|
TextureViewBase* view = descriptor->colorAttachments[i].attachment;
|
|
|
|
TextureViewBase* resolveTarget = descriptor->colorAttachments[i].resolveTarget;
|
|
|
|
|
|
|
|
cmd->colorAttachments[i].view = view;
|
|
|
|
cmd->colorAttachments[i].resolveTarget = resolveTarget;
|
2019-09-20 22:59:47 +00:00
|
|
|
cmd->colorAttachments[i].loadOp = descriptor->colorAttachments[i].loadOp;
|
|
|
|
cmd->colorAttachments[i].storeOp = descriptor->colorAttachments[i].storeOp;
|
2019-07-26 19:08:18 +00:00
|
|
|
cmd->colorAttachments[i].clearColor =
|
2019-09-20 22:59:47 +00:00
|
|
|
descriptor->colorAttachments[i].clearColor;
|
2019-11-21 22:09:41 +00:00
|
|
|
|
2020-05-04 17:10:49 +00:00
|
|
|
usageTracker.TextureViewUsedAs(view, wgpu::TextureUsage::OutputAttachment);
|
2019-11-21 22:09:41 +00:00
|
|
|
|
|
|
|
if (resolveTarget != nullptr) {
|
2020-05-04 17:10:49 +00:00
|
|
|
usageTracker.TextureViewUsedAs(resolveTarget,
|
|
|
|
wgpu::TextureUsage::OutputAttachment);
|
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()) {
|
2019-11-21 22:09:41 +00:00
|
|
|
TextureViewBase* view = descriptor->depthStencilAttachment->attachment;
|
|
|
|
|
|
|
|
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-05-04 17:10:49 +00:00
|
|
|
usageTracker.TextureViewUsedAs(view, wgpu::TextureUsage::OutputAttachment);
|
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
|
|
|
|
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) {
|
2019-11-21 22:09:41 +00:00
|
|
|
RenderPassEncoder* passEncoder =
|
|
|
|
new RenderPassEncoder(device, this, &mEncodingContext, std::move(usageTracker));
|
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
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void CommandEncoder::CopyBufferToBuffer(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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void CommandEncoder::CopyBufferToTexture(const BufferCopyView* source,
|
|
|
|
const TextureCopyView* 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
|
|
|
// Validate objects before doing the defaulting.
|
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
2020-05-13 17:26:05 +00:00
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(source->buffer));
|
2020-05-05 08:33:55 +00:00
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(destination->texture));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute default values for bytesPerRow/rowsPerImage
|
2020-05-13 17:26:05 +00:00
|
|
|
uint32_t defaultedBytesPerRow = source->bytesPerRow;
|
2020-05-05 08:33:55 +00:00
|
|
|
if (defaultedBytesPerRow == 0) {
|
|
|
|
defaultedBytesPerRow =
|
|
|
|
ComputeDefaultBytesPerRow(destination->texture->GetFormat(), copySize->width);
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:26:05 +00:00
|
|
|
uint32_t defaultedRowsPerImage = source->rowsPerImage;
|
2020-05-05 08:33:55 +00:00
|
|
|
if (defaultedRowsPerImage == 0) {
|
|
|
|
defaultedRowsPerImage = copySize->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the rest of the validation using the default values.
|
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(ValidateTextureSampleCountInCopyCommands(destination->texture));
|
2019-07-24 18:15:24 +00:00
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
DAWN_TRY(ValidateRowsPerImage(destination->texture->GetFormat(),
|
|
|
|
defaultedRowsPerImage, copySize->height));
|
|
|
|
DAWN_TRY(
|
|
|
|
ValidateImageOrigin(destination->texture->GetFormat(), destination->origin));
|
|
|
|
DAWN_TRY(ValidateImageCopySize(destination->texture->GetFormat(), *copySize));
|
|
|
|
|
|
|
|
uint32_t bufferCopySize = 0;
|
|
|
|
DAWN_TRY(ValidateBytesPerRow(destination->texture->GetFormat(), *copySize,
|
|
|
|
defaultedBytesPerRow));
|
|
|
|
|
|
|
|
DAWN_TRY(ComputeTextureCopyBufferSize(destination->texture->GetFormat(), *copySize,
|
|
|
|
defaultedBytesPerRow, defaultedRowsPerImage,
|
|
|
|
&bufferCopySize));
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateCopySizeFitsInTexture(*destination, *copySize));
|
2020-05-13 17:26:05 +00:00
|
|
|
DAWN_TRY(ValidateCopySizeFitsInBuffer(*source, bufferCopySize));
|
|
|
|
DAWN_TRY(ValidateTexelBufferOffset(*source, destination->texture->GetFormat()));
|
2020-05-05 08:33:55 +00:00
|
|
|
|
2020-05-13 17:26:05 +00:00
|
|
|
DAWN_TRY(ValidateCanUseAs(source->buffer, wgpu::BufferUsage::CopySrc));
|
2020-05-05 08:33:55 +00:00
|
|
|
DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst));
|
|
|
|
|
2020-05-13 17:26:05 +00:00
|
|
|
mTopLevelBuffers.insert(source->buffer);
|
2020-05-05 08:33:55 +00:00
|
|
|
mTopLevelTextures.insert(destination->texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the copy command.
|
2019-07-24 18:15:24 +00:00
|
|
|
CopyBufferToTextureCmd* copy =
|
|
|
|
allocator->Allocate<CopyBufferToTextureCmd>(Command::CopyBufferToTexture);
|
2020-05-13 17:26:05 +00:00
|
|
|
copy->source.buffer = source->buffer;
|
|
|
|
copy->source.offset = source->offset;
|
2020-05-05 08:33:55 +00:00
|
|
|
copy->source.bytesPerRow = defaultedBytesPerRow;
|
|
|
|
copy->source.rowsPerImage = defaultedRowsPerImage;
|
2019-07-24 18:15:24 +00:00
|
|
|
copy->destination.texture = destination->texture;
|
|
|
|
copy->destination.origin = destination->origin;
|
|
|
|
copy->copySize = *copySize;
|
|
|
|
copy->destination.mipLevel = destination->mipLevel;
|
|
|
|
copy->destination.arrayLayer = destination->arrayLayer;
|
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
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void CommandEncoder::CopyTextureToBuffer(const TextureCopyView* source,
|
|
|
|
const BufferCopyView* 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
|
|
|
// Validate objects before doing the defaulting.
|
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(source->texture));
|
2020-05-13 17:26:05 +00:00
|
|
|
DAWN_TRY(GetDevice()->ValidateObject(destination->buffer));
|
2020-05-05 08:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute default values for bytesPerRow/rowsPerImage
|
2020-05-13 17:26:05 +00:00
|
|
|
uint32_t defaultedBytesPerRow = destination->bytesPerRow;
|
2020-05-05 08:33:55 +00:00
|
|
|
if (defaultedBytesPerRow == 0) {
|
|
|
|
defaultedBytesPerRow =
|
|
|
|
ComputeDefaultBytesPerRow(source->texture->GetFormat(), copySize->width);
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:26:05 +00:00
|
|
|
uint32_t defaultedRowsPerImage = destination->rowsPerImage;
|
2020-05-05 08:33:55 +00:00
|
|
|
if (defaultedRowsPerImage == 0) {
|
|
|
|
defaultedRowsPerImage = copySize->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the rest of the validation using the default values.
|
|
|
|
if (GetDevice()->IsValidationEnabled()) {
|
|
|
|
DAWN_TRY(ValidateTextureSampleCountInCopyCommands(source->texture));
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateRowsPerImage(source->texture->GetFormat(), defaultedRowsPerImage,
|
|
|
|
copySize->height));
|
|
|
|
DAWN_TRY(ValidateImageOrigin(source->texture->GetFormat(), source->origin));
|
|
|
|
DAWN_TRY(ValidateImageCopySize(source->texture->GetFormat(), *copySize));
|
|
|
|
|
|
|
|
uint32_t bufferCopySize = 0;
|
|
|
|
DAWN_TRY(ValidateBytesPerRow(source->texture->GetFormat(), *copySize,
|
|
|
|
defaultedBytesPerRow));
|
|
|
|
DAWN_TRY(ComputeTextureCopyBufferSize(source->texture->GetFormat(), *copySize,
|
|
|
|
defaultedBytesPerRow, defaultedRowsPerImage,
|
|
|
|
&bufferCopySize));
|
2019-07-24 18:15:24 +00:00
|
|
|
|
2020-05-05 08:33:55 +00:00
|
|
|
DAWN_TRY(ValidateCopySizeFitsInTexture(*source, *copySize));
|
2020-05-13 17:26:05 +00:00
|
|
|
DAWN_TRY(ValidateCopySizeFitsInBuffer(*destination, bufferCopySize));
|
|
|
|
DAWN_TRY(ValidateTexelBufferOffset(*destination, source->texture->GetFormat()));
|
2020-05-05 08:33:55 +00:00
|
|
|
|
|
|
|
DAWN_TRY(ValidateCanUseAs(source->texture, wgpu::TextureUsage::CopySrc));
|
2020-05-13 17:26:05 +00:00
|
|
|
DAWN_TRY(ValidateCanUseAs(destination->buffer, wgpu::BufferUsage::CopyDst));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Record the copy command.
|
2019-07-24 18:15:24 +00:00
|
|
|
CopyTextureToBufferCmd* copy =
|
|
|
|
allocator->Allocate<CopyTextureToBufferCmd>(Command::CopyTextureToBuffer);
|
|
|
|
copy->source.texture = source->texture;
|
|
|
|
copy->source.origin = source->origin;
|
|
|
|
copy->copySize = *copySize;
|
|
|
|
copy->source.mipLevel = source->mipLevel;
|
|
|
|
copy->source.arrayLayer = source->arrayLayer;
|
2020-05-13 17:26:05 +00:00
|
|
|
copy->destination.buffer = destination->buffer;
|
|
|
|
copy->destination.offset = destination->offset;
|
2020-05-05 08:33:55 +00:00
|
|
|
copy->destination.bytesPerRow = defaultedBytesPerRow;
|
|
|
|
copy->destination.rowsPerImage = defaultedRowsPerImage;
|
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
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void CommandEncoder::CopyTextureToTexture(const TextureCopyView* source,
|
|
|
|
const TextureCopyView* 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));
|
|
|
|
|
|
|
|
DAWN_TRY(
|
|
|
|
ValidateTextureToTextureCopyRestrictions(*source, *destination, *copySize));
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateImageOrigin(source->texture->GetFormat(), source->origin));
|
|
|
|
DAWN_TRY(ValidateImageCopySize(source->texture->GetFormat(), *copySize));
|
|
|
|
DAWN_TRY(
|
|
|
|
ValidateImageOrigin(destination->texture->GetFormat(), destination->origin));
|
|
|
|
DAWN_TRY(ValidateImageCopySize(destination->texture->GetFormat(), *copySize));
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateCopySizeFitsInTexture(*source, *copySize));
|
|
|
|
DAWN_TRY(ValidateCopySizeFitsInTexture(*destination, *copySize));
|
|
|
|
|
|
|
|
DAWN_TRY(ValidateCanUseAs(source->texture, wgpu::TextureUsage::CopySrc));
|
|
|
|
DAWN_TRY(ValidateCanUseAs(destination->texture, wgpu::TextureUsage::CopyDst));
|
|
|
|
|
|
|
|
mTopLevelTextures.insert(source->texture);
|
|
|
|
mTopLevelTextures.insert(destination->texture);
|
|
|
|
}
|
2019-07-24 18:15:24 +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.arrayLayer = source->arrayLayer;
|
|
|
|
copy->destination.texture = destination->texture;
|
|
|
|
copy->destination.origin = destination->origin;
|
|
|
|
copy->destination.mipLevel = destination->mipLevel;
|
|
|
|
copy->destination.arrayLayer = destination->arrayLayer;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void CommandEncoder::InsertDebugMarker(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 {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void CommandEncoder::PopDebugGroup() {
|
2019-09-10 08:20:40 +00:00
|
|
|
mEncodingContext.TryEncode(this, [&](CommandAllocator* allocator) -> MaybeError {
|
|
|
|
allocator->Allocate<PopDebugGroupCmd>(Command::PopDebugGroup);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
void CommandEncoder::PushDebugGroup(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);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-13 17:00:37 +00:00
|
|
|
CommandBufferBase* CommandEncoder::Finish(const CommandBufferDescriptor* descriptor) {
|
2019-11-21 22:09:41 +00:00
|
|
|
DeviceBase* device = GetDevice();
|
|
|
|
// 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.
|
|
|
|
if (device->ConsumedError(mEncodingContext.Finish()) ||
|
2020-01-31 19:26:49 +00:00
|
|
|
device->ConsumedError(device->ValidateIsAlive()) ||
|
2019-11-21 22:09:41 +00:00
|
|
|
(device->IsValidationEnabled() &&
|
|
|
|
device->ConsumedError(ValidateFinish(mEncodingContext.GetIterator(),
|
|
|
|
mEncodingContext.GetPassUsages())))) {
|
|
|
|
return CommandBufferBase::MakeError(device);
|
2019-02-15 12:54:08 +00:00
|
|
|
}
|
|
|
|
ASSERT(!IsError());
|
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
|
2019-11-21 22:09:41 +00:00
|
|
|
MaybeError CommandEncoder::ValidateFinish(CommandIterator* commands,
|
|
|
|
const PerPassUsages& perPassUsages) 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
|
|
|
|
2019-11-21 22:09:41 +00:00
|
|
|
for (const PassResourceUsage& passUsage : perPassUsages) {
|
|
|
|
DAWN_TRY(ValidatePassResourceUsage(passUsage));
|
|
|
|
}
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2019-09-10 08:20:40 +00:00
|
|
|
uint64_t debugGroupStackSize = 0;
|
|
|
|
|
2019-07-24 18:15:24 +00:00
|
|
|
commands->Reset();
|
2019-02-20 11:46:16 +00:00
|
|
|
Command type;
|
2019-07-24 18:15:24 +00:00
|
|
|
while (commands->NextCommandId(&type)) {
|
2019-02-20 11:46:16 +00:00
|
|
|
switch (type) {
|
|
|
|
case Command::BeginComputePass: {
|
2019-07-24 18:15:24 +00:00
|
|
|
commands->NextCommand<BeginComputePassCmd>();
|
2019-11-21 22:09:41 +00:00
|
|
|
DAWN_TRY(ValidateComputePass(commands));
|
2020-04-02 16:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-02-20 11:46:16 +00:00
|
|
|
|
|
|
|
case Command::BeginRenderPass: {
|
2019-11-21 22:09:41 +00:00
|
|
|
const BeginRenderPassCmd* cmd = commands->NextCommand<BeginRenderPassCmd>();
|
|
|
|
DAWN_TRY(ValidateRenderPass(commands, cmd));
|
2020-04-02 16:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-02-20 11:46:16 +00:00
|
|
|
|
|
|
|
case Command::CopyBufferToBuffer: {
|
2020-05-05 08:33:55 +00:00
|
|
|
commands->NextCommand<CopyBufferToBufferCmd>();
|
2020-04-02 16:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-02-20 11:46:16 +00:00
|
|
|
|
|
|
|
case Command::CopyBufferToTexture: {
|
2020-05-05 08:33:55 +00:00
|
|
|
commands->NextCommand<CopyBufferToTextureCmd>();
|
2020-04-02 16:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-02-20 11:46:16 +00:00
|
|
|
|
|
|
|
case Command::CopyTextureToBuffer: {
|
2020-05-05 08:33:55 +00:00
|
|
|
commands->NextCommand<CopyTextureToBufferCmd>();
|
2020-04-02 16:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-02-20 11:46:16 +00:00
|
|
|
|
2019-03-26 11:06:23 +00:00
|
|
|
case Command::CopyTextureToTexture: {
|
2020-05-05 08:33:55 +00:00
|
|
|
commands->NextCommand<CopyTextureToTextureCmd>();
|
2020-04-02 16:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-03-26 11:06:23 +00:00
|
|
|
|
2019-09-10 08:20:40 +00:00
|
|
|
case Command::InsertDebugMarker: {
|
2019-11-21 22:09:41 +00:00
|
|
|
const InsertDebugMarkerCmd* cmd = commands->NextCommand<InsertDebugMarkerCmd>();
|
2019-09-10 08:20:40 +00:00
|
|
|
commands->NextData<char>(cmd->length + 1);
|
2020-04-02 16:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-10 08:20:40 +00:00
|
|
|
|
|
|
|
case Command::PopDebugGroup: {
|
|
|
|
commands->NextCommand<PopDebugGroupCmd>();
|
|
|
|
DAWN_TRY(ValidateCanPopDebugGroup(debugGroupStackSize));
|
|
|
|
debugGroupStackSize--;
|
2020-04-02 16:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-10 08:20:40 +00:00
|
|
|
|
|
|
|
case Command::PushDebugGroup: {
|
2019-11-21 22:09:41 +00:00
|
|
|
const PushDebugGroupCmd* cmd = commands->NextCommand<PushDebugGroupCmd>();
|
2019-09-10 08:20:40 +00:00
|
|
|
commands->NextData<char>(cmd->length + 1);
|
|
|
|
debugGroupStackSize++;
|
2020-04-02 16:45:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-02-20 11:46:16 +00:00
|
|
|
default:
|
|
|
|
return DAWN_VALIDATION_ERROR("Command disallowed outside of a pass");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 08:20:40 +00:00
|
|
|
DAWN_TRY(ValidateFinalDebugGroupStackSize(debugGroupStackSize));
|
|
|
|
|
2019-02-15 12:54:08 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dawn_native
|