mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-14 15:46:28 +00:00
Fixing offset alignments when using DynamicUploader
When using a dynamic uploader we didn't align the offset that the allocated memory might have already had. That fixes WriteTexture, WriteBuffer, ClearTexture and on D3D12 ClearBuffer. Bug: dawn:512 Change-Id: I64c7511ad6b0d3d6a28a494e1324a10ad4d38091 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/27020 Commit-Queue: Tomek Ponitka <tommek@google.com> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Austin Eng <enga@chromium.org> Reviewed-by: Jiawei Shao <jiawei.shao@intel.com>
This commit is contained in:
committed by
Commit Bot service account
parent
eff9ef0f22
commit
7f265d1d40
@@ -75,6 +75,8 @@ static_library("dawn_utils") {
|
||||
"SystemUtils.h",
|
||||
"TerribleCommandBuffer.cpp",
|
||||
"TerribleCommandBuffer.h",
|
||||
"TestUtils.cpp",
|
||||
"TestUtils.h",
|
||||
"TextureFormatUtils.cpp",
|
||||
"TextureFormatUtils.h",
|
||||
"Timer.h",
|
||||
|
||||
@@ -27,6 +27,8 @@ target_sources(dawn_utils PRIVATE
|
||||
"SystemUtils.h"
|
||||
"TerribleCommandBuffer.cpp"
|
||||
"TerribleCommandBuffer.h"
|
||||
"TestUtils.cpp"
|
||||
"TestUtils.h"
|
||||
"TextureFormatUtils.cpp"
|
||||
"TextureFormatUtils.h"
|
||||
"Timer.h"
|
||||
|
||||
107
src/utils/TestUtils.cpp
Normal file
107
src/utils/TestUtils.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
// 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 bytesPerTexel = utils::GetTexelBlockSizeInBytes(format);
|
||||
return Align(bytesPerTexel * width, kTextureBytesPerRowAlignment);
|
||||
}
|
||||
|
||||
uint32_t GetBytesInBufferTextureCopy(wgpu::TextureFormat format,
|
||||
uint32_t width,
|
||||
uint32_t bytesPerRow,
|
||||
uint32_t rowsPerImage,
|
||||
uint32_t copyArrayLayerCount) {
|
||||
ASSERT(rowsPerImage > 0);
|
||||
const uint32_t bytesPerTexel = utils::GetTexelBlockSizeInBytes(format);
|
||||
const uint32_t bytesAtLastImage = bytesPerRow * (rowsPerImage - 1) + bytesPerTexel * width;
|
||||
return bytesPerRow * rowsPerImage * (copyArrayLayerCount - 1) + bytesAtLastImage;
|
||||
}
|
||||
|
||||
// TODO(jiawei.shao@intel.com): support compressed texture formats
|
||||
TextureDataCopyLayout GetTextureDataCopyLayoutForTexture2DAtLevel(
|
||||
wgpu::TextureFormat format,
|
||||
wgpu::Extent3D textureSizeAtLevel0,
|
||||
uint32_t mipmapLevel,
|
||||
uint32_t rowsPerImage) {
|
||||
TextureDataCopyLayout layout;
|
||||
|
||||
layout.mipSize = {textureSizeAtLevel0.width >> mipmapLevel,
|
||||
textureSizeAtLevel0.height >> mipmapLevel, textureSizeAtLevel0.depth};
|
||||
|
||||
layout.bytesPerRow = GetMinimumBytesPerRow(format, layout.mipSize.width);
|
||||
|
||||
uint32_t appliedRowsPerImage = rowsPerImage > 0 ? rowsPerImage : layout.mipSize.height;
|
||||
layout.bytesPerImage = layout.bytesPerRow * appliedRowsPerImage;
|
||||
|
||||
layout.byteLength =
|
||||
GetBytesInBufferTextureCopy(format, layout.mipSize.width, layout.bytesPerRow,
|
||||
appliedRowsPerImage, textureSizeAtLevel0.depth);
|
||||
|
||||
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) {
|
||||
if (copyExtent.width == 0 || copyExtent.height == 0 || copyExtent.depth == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
uint32_t blockSize = utils::GetTexelBlockSizeInBytes(textureFormat);
|
||||
uint32_t blockWidth = utils::GetTextureFormatBlockWidth(textureFormat);
|
||||
uint32_t blockHeight = utils::GetTextureFormatBlockHeight(textureFormat);
|
||||
|
||||
uint64_t texelBlockRowsPerImage = rowsPerImage / blockHeight;
|
||||
uint64_t bytesPerImage = bytesPerRow * texelBlockRowsPerImage;
|
||||
uint64_t bytesInLastSlice = bytesPerRow * (copyExtent.height / blockHeight - 1) +
|
||||
(copyExtent.width / blockWidth * blockSize);
|
||||
return bytesPerImage * (copyExtent.depth - 1) + bytesInLastSlice;
|
||||
}
|
||||
}
|
||||
|
||||
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::TextureCopyView textureCopyView = utils::CreateTextureCopyView(texture, 0, {0, 0, 0});
|
||||
wgpu::TextureDataLayout textureDataLayout = utils::CreateTextureDataLayout(0, 0, 0);
|
||||
wgpu::Extent3D copyExtent = {1, 1, 1};
|
||||
|
||||
// WriteTexture with exactly 1 byte of data.
|
||||
device.GetDefaultQueue().WriteTexture(&textureCopyView, data.data(), 1, &textureDataLayout,
|
||||
©Extent);
|
||||
}
|
||||
} // namespace utils
|
||||
57
src/utils/TestUtils.h
Normal file
57
src/utils/TestUtils.h
Normal file
@@ -0,0 +1,57 @@
|
||||
// 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.
|
||||
|
||||
#ifndef UTILS_TESTHELPERS_H_
|
||||
#define UTILS_TESTHELPERS_H_
|
||||
|
||||
#include <dawn/webgpu_cpp.h>
|
||||
|
||||
namespace utils {
|
||||
|
||||
struct TextureDataCopyLayout {
|
||||
uint64_t byteLength;
|
||||
uint64_t texelBlockCount;
|
||||
uint32_t bytesPerRow;
|
||||
uint32_t texelBlocksPerRow;
|
||||
uint32_t bytesPerImage;
|
||||
uint32_t texelBlocksPerImage;
|
||||
wgpu::Extent3D mipSize;
|
||||
};
|
||||
|
||||
uint32_t GetMinimumBytesPerRow(wgpu::TextureFormat format, uint32_t width);
|
||||
uint32_t GetBytesInBufferTextureCopy(wgpu::TextureFormat format,
|
||||
uint32_t width,
|
||||
uint32_t bytesPerRow,
|
||||
uint32_t rowsPerImage,
|
||||
uint32_t copyArrayLayerCount);
|
||||
TextureDataCopyLayout GetTextureDataCopyLayoutForTexture2DAtLevel(
|
||||
wgpu::TextureFormat format,
|
||||
wgpu::Extent3D textureSizeAtLevel0,
|
||||
uint32_t mipmapLevel,
|
||||
uint32_t rowsPerImage);
|
||||
|
||||
uint64_t RequiredBytesInCopy(uint64_t bytesPerRow,
|
||||
uint64_t rowsPerImage,
|
||||
wgpu::Extent3D copyExtent,
|
||||
wgpu::TextureFormat textureFormat);
|
||||
|
||||
// A helper function used for testing DynamicUploader offset alignment.
|
||||
// A call of this function will do a Queue::WriteTexture with 1 byte of data,
|
||||
// so that assuming that WriteTexture uses DynamicUploader, the first RingBuffer
|
||||
// in it will contain 1 byte of data.
|
||||
void UnalignDynamicUploader(wgpu::Device device);
|
||||
|
||||
} // namespace utils
|
||||
|
||||
#endif // UTILS_TESTHELPERS_H_
|
||||
@@ -78,6 +78,15 @@ namespace utils {
|
||||
wgpu::TextureFormat::BC7RGBAUnormSrgb,
|
||||
};
|
||||
|
||||
static constexpr std::array<wgpu::TextureFormat, 14> kBCFormats = {
|
||||
wgpu::TextureFormat::BC1RGBAUnorm, wgpu::TextureFormat::BC1RGBAUnormSrgb,
|
||||
wgpu::TextureFormat::BC2RGBAUnorm, wgpu::TextureFormat::BC2RGBAUnormSrgb,
|
||||
wgpu::TextureFormat::BC3RGBAUnorm, wgpu::TextureFormat::BC3RGBAUnormSrgb,
|
||||
wgpu::TextureFormat::BC4RUnorm, wgpu::TextureFormat::BC4RSnorm,
|
||||
wgpu::TextureFormat::BC5RGUnorm, wgpu::TextureFormat::BC5RGSnorm,
|
||||
wgpu::TextureFormat::BC6HRGBUfloat, wgpu::TextureFormat::BC6HRGBSfloat,
|
||||
wgpu::TextureFormat::BC7RGBAUnorm, wgpu::TextureFormat::BC7RGBAUnormSrgb};
|
||||
|
||||
const char* GetColorTextureComponentTypePrefix(wgpu::TextureFormat textureFormat);
|
||||
bool TextureFormatSupportsStorageTexture(wgpu::TextureFormat format);
|
||||
|
||||
|
||||
@@ -14,11 +14,8 @@
|
||||
|
||||
#include "utils/WGPUHelpers.h"
|
||||
|
||||
#include "common/Assert.h"
|
||||
#include "common/Constants.h"
|
||||
#include "common/Log.h"
|
||||
#include "common/Math.h"
|
||||
#include "utils/TextureFormatUtils.h"
|
||||
|
||||
#include <shaderc/shaderc.hpp>
|
||||
|
||||
@@ -393,76 +390,4 @@ namespace utils {
|
||||
return device.CreateBindGroup(&descriptor);
|
||||
}
|
||||
|
||||
uint32_t GetMinimumBytesPerRow(wgpu::TextureFormat format, uint32_t width) {
|
||||
const uint32_t bytesPerTexel = utils::GetTexelBlockSizeInBytes(format);
|
||||
return Align(bytesPerTexel * width, kTextureBytesPerRowAlignment);
|
||||
}
|
||||
|
||||
uint32_t GetBytesInBufferTextureCopy(wgpu::TextureFormat format,
|
||||
uint32_t width,
|
||||
uint32_t bytesPerRow,
|
||||
uint32_t rowsPerImage,
|
||||
uint32_t copyArrayLayerCount) {
|
||||
ASSERT(rowsPerImage > 0);
|
||||
const uint32_t bytesPerTexel = utils::GetTexelBlockSizeInBytes(format);
|
||||
const uint32_t bytesAtLastImage = bytesPerRow * (rowsPerImage - 1) + bytesPerTexel * width;
|
||||
return bytesPerRow * rowsPerImage * (copyArrayLayerCount - 1) + bytesAtLastImage;
|
||||
}
|
||||
|
||||
// TODO(jiawei.shao@intel.com): support compressed texture formats
|
||||
TextureDataCopyLayout GetTextureDataCopyLayoutForTexture2DAtLevel(
|
||||
wgpu::TextureFormat format,
|
||||
wgpu::Extent3D textureSizeAtLevel0,
|
||||
uint32_t mipmapLevel,
|
||||
uint32_t rowsPerImage) {
|
||||
TextureDataCopyLayout layout;
|
||||
|
||||
layout.mipSize = {textureSizeAtLevel0.width >> mipmapLevel,
|
||||
textureSizeAtLevel0.height >> mipmapLevel, textureSizeAtLevel0.depth};
|
||||
|
||||
layout.bytesPerRow = GetMinimumBytesPerRow(format, layout.mipSize.width);
|
||||
|
||||
uint32_t appliedRowsPerImage = rowsPerImage > 0 ? rowsPerImage : layout.mipSize.height;
|
||||
layout.bytesPerImage = layout.bytesPerRow * appliedRowsPerImage;
|
||||
|
||||
layout.byteLength =
|
||||
GetBytesInBufferTextureCopy(format, layout.mipSize.width, layout.bytesPerRow,
|
||||
appliedRowsPerImage, textureSizeAtLevel0.depth);
|
||||
|
||||
const uint32_t bytesPerTexel = utils::GetTexelBlockSizeInBytes(format);
|
||||
layout.texelBlocksPerRow = layout.bytesPerRow / bytesPerTexel;
|
||||
layout.texelBlocksPerImage = layout.bytesPerImage / bytesPerTexel;
|
||||
layout.texelBlockCount = layout.byteLength / bytesPerTexel;
|
||||
|
||||
return layout;
|
||||
}
|
||||
|
||||
const std::array<wgpu::TextureFormat, 14> kBCFormats = {
|
||||
wgpu::TextureFormat::BC1RGBAUnorm, wgpu::TextureFormat::BC1RGBAUnormSrgb,
|
||||
wgpu::TextureFormat::BC2RGBAUnorm, wgpu::TextureFormat::BC2RGBAUnormSrgb,
|
||||
wgpu::TextureFormat::BC3RGBAUnorm, wgpu::TextureFormat::BC3RGBAUnormSrgb,
|
||||
wgpu::TextureFormat::BC4RUnorm, wgpu::TextureFormat::BC4RSnorm,
|
||||
wgpu::TextureFormat::BC5RGUnorm, wgpu::TextureFormat::BC5RGSnorm,
|
||||
wgpu::TextureFormat::BC6HRGBUfloat, wgpu::TextureFormat::BC6HRGBSfloat,
|
||||
wgpu::TextureFormat::BC7RGBAUnorm, wgpu::TextureFormat::BC7RGBAUnormSrgb};
|
||||
|
||||
uint64_t RequiredBytesInCopy(uint64_t bytesPerRow,
|
||||
uint64_t rowsPerImage,
|
||||
wgpu::Extent3D copyExtent,
|
||||
wgpu::TextureFormat textureFormat) {
|
||||
if (copyExtent.width == 0 || copyExtent.height == 0 || copyExtent.depth == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
uint32_t blockSize = utils::GetTexelBlockSizeInBytes(textureFormat);
|
||||
uint32_t blockWidth = utils::GetTextureFormatBlockWidth(textureFormat);
|
||||
uint32_t blockHeight = utils::GetTextureFormatBlockHeight(textureFormat);
|
||||
|
||||
uint64_t texelBlockRowsPerImage = rowsPerImage / blockHeight;
|
||||
uint64_t bytesPerImage = bytesPerRow * texelBlockRowsPerImage;
|
||||
uint64_t bytesInLastSlice = bytesPerRow * (copyExtent.height / blockHeight - 1) +
|
||||
(copyExtent.width / blockWidth * blockSize);
|
||||
return bytesPerImage * (copyExtent.depth - 1) + bytesInLastSlice;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
|
||||
@@ -137,35 +137,6 @@ namespace utils {
|
||||
const wgpu::BindGroupLayout& layout,
|
||||
std::initializer_list<BindingInitializationHelper> entriesInitializer);
|
||||
|
||||
struct TextureDataCopyLayout {
|
||||
uint64_t byteLength;
|
||||
uint64_t texelBlockCount;
|
||||
uint32_t bytesPerRow;
|
||||
uint32_t texelBlocksPerRow;
|
||||
uint32_t bytesPerImage;
|
||||
uint32_t texelBlocksPerImage;
|
||||
wgpu::Extent3D mipSize;
|
||||
};
|
||||
|
||||
uint32_t GetMinimumBytesPerRow(wgpu::TextureFormat format, uint32_t width);
|
||||
uint32_t GetBytesInBufferTextureCopy(wgpu::TextureFormat format,
|
||||
uint32_t width,
|
||||
uint32_t bytesPerRow,
|
||||
uint32_t rowsPerImage,
|
||||
uint32_t copyArrayLayerCount);
|
||||
TextureDataCopyLayout GetTextureDataCopyLayoutForTexture2DAtLevel(
|
||||
wgpu::TextureFormat format,
|
||||
wgpu::Extent3D textureSizeAtLevel0,
|
||||
uint32_t mipmapLevel,
|
||||
uint32_t rowsPerImage);
|
||||
|
||||
extern const std::array<wgpu::TextureFormat, 14> kBCFormats;
|
||||
|
||||
uint64_t RequiredBytesInCopy(uint64_t bytesPerRow,
|
||||
uint64_t rowsPerImage,
|
||||
wgpu::Extent3D copyExtent,
|
||||
wgpu::TextureFormat textureFormat);
|
||||
|
||||
} // namespace utils
|
||||
|
||||
#endif // UTILS_DAWNHELPERS_H_
|
||||
|
||||
Reference in New Issue
Block a user