mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-05-16 12:21:35 +00:00
If we are copying a partial of rows, the buffer size doesn't need to be that large of the entire image which has "rowsPerImage" rows. There was a bug in texture copy splitter. And this workaround was introduced at https://dawn-review.googlesource.com/c/dawn/+/30741 in order to workaround that bug. D3D12 validation is actually correct. Now that we have fixed the bug at https://dawn-review.googlesource.com/c/dawn/+/52680/. The workaround is not needed. Bug: dawn:547, dawn:520 Change-Id: I92292c71dc5479fc2ba863eb9f897516bd1a96a0 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/53360 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Austin Eng <enga@chromium.org> Commit-Queue: Yunchao He <yunchao.he@intel.com>
132 lines
5.6 KiB
C++
132 lines
5.6 KiB
C++
// Copyright 2020 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 "utils/TestUtils.h"
|
|
|
|
#include "common/Assert.h"
|
|
#include "common/Constants.h"
|
|
#include "common/Math.h"
|
|
#include "utils/TextureFormatUtils.h"
|
|
#include "utils/WGPUHelpers.h"
|
|
|
|
#include <vector>
|
|
|
|
namespace utils {
|
|
|
|
uint32_t GetMinimumBytesPerRow(wgpu::TextureFormat format, uint32_t width) {
|
|
const uint32_t bytesPerBlock = utils::GetTexelBlockSizeInBytes(format);
|
|
const uint32_t blockWidth = utils::GetTextureFormatBlockWidth(format);
|
|
ASSERT(width % blockWidth == 0);
|
|
return Align(bytesPerBlock * (width / blockWidth), kTextureBytesPerRowAlignment);
|
|
}
|
|
|
|
TextureDataCopyLayout GetTextureDataCopyLayoutForTexture2DAtLevel(
|
|
wgpu::TextureFormat format,
|
|
wgpu::Extent3D textureSizeAtLevel0,
|
|
uint32_t mipmapLevel,
|
|
uint32_t rowsPerImage) {
|
|
// TODO(jiawei.shao@intel.com): support compressed texture formats
|
|
ASSERT(utils::GetTextureFormatBlockWidth(format) == 1);
|
|
|
|
TextureDataCopyLayout layout;
|
|
|
|
layout.mipSize = {std::max(textureSizeAtLevel0.width >> mipmapLevel, 1u),
|
|
std::max(textureSizeAtLevel0.height >> mipmapLevel, 1u),
|
|
textureSizeAtLevel0.depthOrArrayLayers};
|
|
|
|
layout.bytesPerRow = GetMinimumBytesPerRow(format, layout.mipSize.width);
|
|
|
|
if (rowsPerImage == wgpu::kCopyStrideUndefined) {
|
|
rowsPerImage = layout.mipSize.height;
|
|
}
|
|
layout.rowsPerImage = rowsPerImage;
|
|
|
|
uint32_t appliedRowsPerImage = rowsPerImage > 0 ? rowsPerImage : layout.mipSize.height;
|
|
layout.bytesPerImage = layout.bytesPerRow * appliedRowsPerImage;
|
|
|
|
layout.byteLength =
|
|
RequiredBytesInCopy(layout.bytesPerRow, appliedRowsPerImage, layout.mipSize, format);
|
|
|
|
const uint32_t bytesPerTexel = utils::GetTexelBlockSizeInBytes(format);
|
|
layout.texelBlocksPerRow = layout.bytesPerRow / bytesPerTexel;
|
|
layout.texelBlocksPerImage = layout.bytesPerImage / bytesPerTexel;
|
|
layout.texelBlockCount = layout.byteLength / bytesPerTexel;
|
|
|
|
return layout;
|
|
}
|
|
|
|
uint64_t RequiredBytesInCopy(uint64_t bytesPerRow,
|
|
uint64_t rowsPerImage,
|
|
wgpu::Extent3D copyExtent,
|
|
wgpu::TextureFormat textureFormat) {
|
|
uint32_t blockSize = utils::GetTexelBlockSizeInBytes(textureFormat);
|
|
uint32_t blockWidth = utils::GetTextureFormatBlockWidth(textureFormat);
|
|
uint32_t blockHeight = utils::GetTextureFormatBlockHeight(textureFormat);
|
|
ASSERT(copyExtent.width % blockWidth == 0);
|
|
uint32_t widthInBlocks = copyExtent.width / blockWidth;
|
|
ASSERT(copyExtent.height % blockHeight == 0);
|
|
uint32_t heightInBlocks = copyExtent.height / blockHeight;
|
|
return RequiredBytesInCopy(bytesPerRow, rowsPerImage, widthInBlocks, heightInBlocks,
|
|
copyExtent.depthOrArrayLayers, blockSize);
|
|
}
|
|
|
|
uint64_t RequiredBytesInCopy(uint64_t bytesPerRow,
|
|
uint64_t rowsPerImage,
|
|
uint64_t widthInBlocks,
|
|
uint64_t heightInBlocks,
|
|
uint64_t depth,
|
|
uint64_t bytesPerBlock) {
|
|
if (depth == 0) {
|
|
return 0;
|
|
}
|
|
|
|
uint64_t bytesPerImage = bytesPerRow * rowsPerImage;
|
|
uint64_t requiredBytesInCopy = bytesPerImage * (depth - 1);
|
|
if (heightInBlocks != 0) {
|
|
uint64_t lastRowBytes = widthInBlocks * bytesPerBlock;
|
|
uint64_t lastImageBytes = bytesPerRow * (heightInBlocks - 1) + lastRowBytes;
|
|
requiredBytesInCopy += lastImageBytes;
|
|
}
|
|
return requiredBytesInCopy;
|
|
}
|
|
|
|
uint64_t GetTexelCountInCopyRegion(uint64_t bytesPerRow,
|
|
uint64_t rowsPerImage,
|
|
wgpu::Extent3D copyExtent,
|
|
wgpu::TextureFormat textureFormat) {
|
|
return RequiredBytesInCopy(bytesPerRow, rowsPerImage, copyExtent, textureFormat) /
|
|
utils::GetTexelBlockSizeInBytes(textureFormat);
|
|
}
|
|
|
|
void UnalignDynamicUploader(wgpu::Device device) {
|
|
std::vector<uint8_t> data = {1};
|
|
|
|
wgpu::TextureDescriptor descriptor = {};
|
|
descriptor.size = {1, 1, 1};
|
|
descriptor.format = wgpu::TextureFormat::R8Unorm;
|
|
descriptor.usage = wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::CopySrc;
|
|
wgpu::Texture texture = device.CreateTexture(&descriptor);
|
|
|
|
wgpu::ImageCopyTexture imageCopyTexture =
|
|
utils::CreateImageCopyTexture(texture, 0, {0, 0, 0});
|
|
wgpu::TextureDataLayout textureDataLayout =
|
|
utils::CreateTextureDataLayout(0, wgpu::kCopyStrideUndefined);
|
|
wgpu::Extent3D copyExtent = {1, 1, 1};
|
|
|
|
// WriteTexture with exactly 1 byte of data.
|
|
device.GetQueue().WriteTexture(&imageCopyTexture, data.data(), 1, &textureDataLayout,
|
|
©Extent);
|
|
}
|
|
} // namespace utils
|