D3D12: Add copy splitting for texture copies not 512-byte aligned
This commit is contained in:
parent
78c8b837ea
commit
0506138567
|
@ -245,6 +245,8 @@ if (NXT_ENABLE_D3D12)
|
|||
${D3D12_DIR}/ShaderModuleD3D12.h
|
||||
${D3D12_DIR}/SwapChainD3D12.cpp
|
||||
${D3D12_DIR}/SwapChainD3D12.h
|
||||
${D3D12_DIR}/TextureCopySplitter.cpp
|
||||
${D3D12_DIR}/TextureCopySplitter.h
|
||||
${D3D12_DIR}/TextureD3D12.cpp
|
||||
${D3D12_DIR}/TextureD3D12.h
|
||||
)
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include "backend/d3d12/RenderPipelineD3D12.h"
|
||||
#include "backend/d3d12/ResourceAllocator.h"
|
||||
#include "backend/d3d12/SamplerD3D12.h"
|
||||
#include "backend/d3d12/TextureCopySplitter.h"
|
||||
#include "backend/d3d12/TextureD3D12.h"
|
||||
#include "common/Assert.h"
|
||||
|
||||
|
@ -201,28 +202,6 @@ namespace d3d12 {
|
|||
D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
|
||||
}
|
||||
}
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION D3D12PlacedTextureCopyLocation(BufferCopyLocation& bufferLocation, Texture* texture, const TextureCopyLocation& textureLocation, uint32_t rowPitch) {
|
||||
D3D12_TEXTURE_COPY_LOCATION d3d12Location;
|
||||
d3d12Location.pResource = ToBackend(bufferLocation.buffer.Get())->GetD3D12Resource().Get();
|
||||
d3d12Location.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
d3d12Location.PlacedFootprint.Offset = bufferLocation.offset;
|
||||
d3d12Location.PlacedFootprint.Footprint.Format = texture->GetD3D12Format();
|
||||
d3d12Location.PlacedFootprint.Footprint.Width = textureLocation.width;
|
||||
d3d12Location.PlacedFootprint.Footprint.Height = textureLocation.height;
|
||||
d3d12Location.PlacedFootprint.Footprint.Depth = textureLocation.depth;
|
||||
d3d12Location.PlacedFootprint.Footprint.RowPitch = rowPitch;
|
||||
|
||||
return d3d12Location;
|
||||
}
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION D3D12TextureCopyLocation(TextureCopyLocation& textureLocation) {
|
||||
D3D12_TEXTURE_COPY_LOCATION d3d12Location;
|
||||
d3d12Location.pResource = ToBackend(textureLocation.texture.Get())->GetD3D12Resource().Get();
|
||||
d3d12Location.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
d3d12Location.SubresourceIndex = textureLocation.level;
|
||||
return d3d12Location;
|
||||
}
|
||||
}
|
||||
|
||||
CommandBuffer::CommandBuffer(Device* device, CommandBufferBuilder* builder)
|
||||
|
@ -317,13 +296,46 @@ namespace d3d12 {
|
|||
Buffer* buffer = ToBackend(copy->source.buffer.Get());
|
||||
Texture* texture = ToBackend(copy->destination.texture.Get());
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION srcLocation = D3D12PlacedTextureCopyLocation(copy->source, texture, copy->destination, copy->rowPitch);
|
||||
D3D12_TEXTURE_COPY_LOCATION dstLocation = D3D12TextureCopyLocation(copy->destination);
|
||||
auto copySplit = ComputeTextureCopySplit(
|
||||
copy->destination.x,
|
||||
copy->destination.y,
|
||||
copy->destination.z,
|
||||
copy->destination.width,
|
||||
copy->destination.height,
|
||||
copy->destination.depth,
|
||||
static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat())),
|
||||
copy->source.offset,
|
||||
copy->rowPitch
|
||||
);
|
||||
|
||||
uint64_t totalBytes = (copy->rowPitch * (copy->destination.height - 1) + copy->destination.width) * copy->destination.depth;
|
||||
ASSERT(totalBytes <= buffer->GetD3D12Size());
|
||||
D3D12_TEXTURE_COPY_LOCATION textureLocation;
|
||||
textureLocation.pResource = texture->GetD3D12Resource().Get();
|
||||
textureLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
textureLocation.SubresourceIndex = copy->destination.level;
|
||||
|
||||
commandList->CopyTextureRegion(&dstLocation, copy->destination.x, copy->destination.y, copy->destination.z, &srcLocation, nullptr);
|
||||
for (uint32_t i = 0; i < copySplit.count; ++i) {
|
||||
auto& info = copySplit.copies[i];
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION bufferLocation;
|
||||
bufferLocation.pResource = buffer->GetD3D12Resource().Get();
|
||||
bufferLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
bufferLocation.PlacedFootprint.Offset = copySplit.offset;
|
||||
bufferLocation.PlacedFootprint.Footprint.Format = texture->GetD3D12Format();
|
||||
bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width;
|
||||
bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height;
|
||||
bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth;
|
||||
bufferLocation.PlacedFootprint.Footprint.RowPitch = copy->rowPitch;
|
||||
|
||||
D3D12_BOX sourceRegion;
|
||||
sourceRegion.left = info.bufferOffset.x;
|
||||
sourceRegion.top = info.bufferOffset.y;
|
||||
sourceRegion.front = info.bufferOffset.z;
|
||||
sourceRegion.right = info.bufferOffset.x + info.copySize.width;
|
||||
sourceRegion.bottom = info.bufferOffset.y + info.copySize.height;
|
||||
sourceRegion.back = info.bufferOffset.z + info.copySize.depth;
|
||||
|
||||
commandList->CopyTextureRegion(&textureLocation, info.textureOffset.x, info.textureOffset.y, info.textureOffset.z, &bufferLocation, &sourceRegion);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -333,20 +345,46 @@ namespace d3d12 {
|
|||
Texture* texture = ToBackend(copy->source.texture.Get());
|
||||
Buffer* buffer = ToBackend(copy->destination.buffer.Get());
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION srcLocation = D3D12TextureCopyLocation(copy->source);
|
||||
D3D12_TEXTURE_COPY_LOCATION dstLocation = D3D12PlacedTextureCopyLocation(copy->destination, texture, copy->source, copy->rowPitch);
|
||||
auto copySplit = ComputeTextureCopySplit(
|
||||
copy->source.x,
|
||||
copy->source.y,
|
||||
copy->source.z,
|
||||
copy->source.width,
|
||||
copy->source.height,
|
||||
copy->source.depth,
|
||||
static_cast<uint32_t>(TextureFormatPixelSize(texture->GetFormat())),
|
||||
copy->destination.offset,
|
||||
copy->rowPitch
|
||||
);
|
||||
|
||||
uint64_t totalBytes = (copy->rowPitch * (copy->source.height - 1) + copy->source.width) * copy->source.depth;
|
||||
ASSERT(totalBytes <= buffer->GetD3D12Size());
|
||||
D3D12_TEXTURE_COPY_LOCATION textureLocation;
|
||||
textureLocation.pResource = texture->GetD3D12Resource().Get();
|
||||
textureLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
|
||||
textureLocation.SubresourceIndex = copy->source.level;
|
||||
|
||||
D3D12_BOX sourceRegion;
|
||||
sourceRegion.left = copy->source.x;
|
||||
sourceRegion.top = copy->source.y;
|
||||
sourceRegion.front = copy->source.z;
|
||||
sourceRegion.right = copy->source.x + copy->source.width;
|
||||
sourceRegion.bottom = copy->source.y + copy->source.height;
|
||||
sourceRegion.back = copy->source.z + copy->source.depth;
|
||||
commandList->CopyTextureRegion(&dstLocation, 0, 0, 0, &srcLocation, &sourceRegion);
|
||||
for (uint32_t i = 0; i < copySplit.count; ++i) {
|
||||
auto& info = copySplit.copies[i];
|
||||
|
||||
D3D12_TEXTURE_COPY_LOCATION bufferLocation;
|
||||
bufferLocation.pResource = buffer->GetD3D12Resource().Get();
|
||||
bufferLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
|
||||
bufferLocation.PlacedFootprint.Offset = copySplit.offset;
|
||||
bufferLocation.PlacedFootprint.Footprint.Format = texture->GetD3D12Format();
|
||||
bufferLocation.PlacedFootprint.Footprint.Width = info.bufferSize.width;
|
||||
bufferLocation.PlacedFootprint.Footprint.Height = info.bufferSize.height;
|
||||
bufferLocation.PlacedFootprint.Footprint.Depth = info.bufferSize.depth;
|
||||
bufferLocation.PlacedFootprint.Footprint.RowPitch = copy->rowPitch;
|
||||
|
||||
D3D12_BOX sourceRegion;
|
||||
sourceRegion.left = info.textureOffset.x;
|
||||
sourceRegion.top = info.textureOffset.y;
|
||||
sourceRegion.front = info.textureOffset.z;
|
||||
sourceRegion.right = info.textureOffset.x + info.copySize.width;
|
||||
sourceRegion.bottom = info.textureOffset.y + info.copySize.height;
|
||||
sourceRegion.back = info.textureOffset.z + info.copySize.depth;
|
||||
|
||||
commandList->CopyTextureRegion(&bufferLocation, info.bufferOffset.x, info.bufferOffset.y, info.bufferOffset.z, &textureLocation, &sourceRegion);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
|
@ -0,0 +1,199 @@
|
|||
// Copyright 2017 The NXT 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 "backend/d3d12/TextureCopySplitter.h"
|
||||
|
||||
#include "backend/d3d12/d3d12_platform.h"
|
||||
#include "common/Assert.h"
|
||||
|
||||
namespace backend {
|
||||
namespace d3d12 {
|
||||
|
||||
namespace {
|
||||
void ComputeTexelOffsets(uint32_t offset, uint32_t rowPitch, uint32_t slicePitch, uint32_t texelSize, uint32_t* texelOffsetX, uint32_t* texelOffsetY, uint32_t* texelOffsetZ) {
|
||||
uint32_t byteOffsetX = offset % rowPitch;
|
||||
offset -= byteOffsetX;
|
||||
uint32_t byteOffsetY = offset % slicePitch;
|
||||
uint32_t byteOffsetZ = offset - byteOffsetY;
|
||||
|
||||
*texelOffsetX = byteOffsetX / texelSize;
|
||||
*texelOffsetY = byteOffsetY / rowPitch;
|
||||
*texelOffsetZ = byteOffsetZ / slicePitch;
|
||||
}
|
||||
}
|
||||
|
||||
TextureCopySplit ComputeTextureCopySplit(uint32_t x, uint32_t y, uint32_t z, uint32_t width, uint32_t height, uint32_t depth, uint32_t texelSize, uint32_t offset, uint32_t rowPitch) {
|
||||
|
||||
TextureCopySplit copy;
|
||||
|
||||
if (z != 0 || depth > 1) {
|
||||
// TODO(enga@google.com): Handle 3D / 2D arrays
|
||||
ASSERT(false);
|
||||
return copy;
|
||||
}
|
||||
|
||||
ASSERT(rowPitch % texelSize == 0);
|
||||
|
||||
uint32_t alignedOffset = offset & ~(D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT - 1);
|
||||
|
||||
copy.offset = alignedOffset;
|
||||
if (offset == alignedOffset) {
|
||||
copy.count = 1;
|
||||
|
||||
copy.copies[0].textureOffset.x = x;
|
||||
copy.copies[0].textureOffset.y = y;
|
||||
copy.copies[0].textureOffset.z = z;
|
||||
|
||||
copy.copies[0].copySize.width = width;
|
||||
copy.copies[0].copySize.height = height;
|
||||
copy.copies[0].copySize.depth = depth;
|
||||
|
||||
copy.copies[0].bufferOffset.x = 0;
|
||||
copy.copies[0].bufferOffset.y = 0;
|
||||
copy.copies[0].bufferOffset.z = 0;
|
||||
copy.copies[0].bufferSize.width = width;
|
||||
copy.copies[0].bufferSize.height = height;
|
||||
copy.copies[0].bufferSize.depth = depth;
|
||||
|
||||
// Return early. There is only one copy needed because the offset is already 512-byte aligned
|
||||
return copy;
|
||||
}
|
||||
|
||||
ASSERT(alignedOffset < offset);
|
||||
|
||||
uint32_t texelOffsetX, texelOffsetY, texelOffsetZ;
|
||||
ComputeTexelOffsets(offset - alignedOffset, rowPitch, rowPitch * height, texelSize, &texelOffsetX, &texelOffsetY, &texelOffsetZ);
|
||||
|
||||
uint32_t rowPitchInTexels = rowPitch / texelSize;
|
||||
|
||||
if (width + texelOffsetX <= rowPitchInTexels) {
|
||||
// The region's rows fit inside the row pitch. In this case, extend the width of the PlacedFootprint and copy the buffer with an offset location
|
||||
// |<--------------- row pitch --------------->|
|
||||
//
|
||||
// |-------------------------------------------|
|
||||
// | |
|
||||
// | +++++++++++++++++~~~~~~~~~|
|
||||
// |~~~~~~~~~~~~~~~~~+++++++++++++++++~~~~~~~~~|
|
||||
// |~~~~~~~~~~~~~~~~~+++++++++++++++++~~~~~~~~~|
|
||||
// |~~~~~~~~~~~~~~~~~+++++++++++++++++~~~~~~~~~|
|
||||
// |~~~~~~~~~~~~~~~~~+++++++++++++++++ |
|
||||
// |-------------------------------------------|
|
||||
|
||||
// Copy 0:
|
||||
// |----------------------------------|
|
||||
// | |
|
||||
// | +++++++++++++++++|
|
||||
// |~~~~~~~~~~~~~~~~~+++++++++++++++++|
|
||||
// |~~~~~~~~~~~~~~~~~+++++++++++++++++|
|
||||
// |~~~~~~~~~~~~~~~~~+++++++++++++++++|
|
||||
// |~~~~~~~~~~~~~~~~~+++++++++++++++++|
|
||||
// |----------------------------------|
|
||||
|
||||
copy.count = 1;
|
||||
|
||||
copy.copies[0].textureOffset.x = x;
|
||||
copy.copies[0].textureOffset.y = y;
|
||||
copy.copies[0].textureOffset.z = z;
|
||||
|
||||
copy.copies[0].copySize.width = width;
|
||||
copy.copies[0].copySize.height = height;
|
||||
copy.copies[0].copySize.depth = depth;
|
||||
|
||||
copy.copies[0].bufferOffset.x = texelOffsetX;
|
||||
copy.copies[0].bufferOffset.y = texelOffsetY;
|
||||
copy.copies[0].bufferOffset.z = texelOffsetZ;
|
||||
copy.copies[0].bufferSize.width = width + texelOffsetX;
|
||||
copy.copies[0].bufferSize.height = height + texelOffsetY;
|
||||
copy.copies[0].bufferSize.depth = depth + texelOffsetZ;
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
// The region's rows straddle the row pitch. Split the copy into two copies
|
||||
// |<--------------- row pitch --------------->|
|
||||
//
|
||||
// |-------------------------------------------|
|
||||
// | |
|
||||
// | ++++++++|
|
||||
// |+++++++++~~~~~~~~~~~~~~~~~~~~~~~~~~++++++++|
|
||||
// |+++++++++~~~~~~~~~~~~~~~~~~~~~~~~~~++++++++|
|
||||
// |+++++++++~~~~~~~~~~~~~~~~~~~~~~~~~~++++++++|
|
||||
// |+++++++++~~~~~~~~~~~~~~~~~~~~~~~~~~++++++++|
|
||||
// |+++++++++ |
|
||||
// |-------------------------------------------|
|
||||
|
||||
|
||||
// Copy 0:
|
||||
// |-------------------------------------------|
|
||||
// | |
|
||||
// | ++++++++|
|
||||
// |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~++++++++|
|
||||
// |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~++++++++|
|
||||
// |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~++++++++|
|
||||
// |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~++++++++|
|
||||
// |-------------------------------------------|
|
||||
|
||||
// Copy 1:
|
||||
// |---------|
|
||||
// | |
|
||||
// | |
|
||||
// |+++++++++|
|
||||
// |+++++++++|
|
||||
// |+++++++++|
|
||||
// |+++++++++|
|
||||
// |+++++++++|
|
||||
// |---------|
|
||||
|
||||
copy.count = 2;
|
||||
|
||||
copy.copies[0].textureOffset.x = x;
|
||||
copy.copies[0].textureOffset.y = y;
|
||||
copy.copies[0].textureOffset.z = z;
|
||||
|
||||
ASSERT(rowPitchInTexels > texelOffsetX);
|
||||
copy.copies[0].copySize.width = rowPitchInTexels - texelOffsetX;
|
||||
copy.copies[0].copySize.height = height;
|
||||
copy.copies[0].copySize.depth = depth;
|
||||
|
||||
copy.copies[0].bufferOffset.x = texelOffsetX;
|
||||
copy.copies[0].bufferOffset.y = texelOffsetY;
|
||||
copy.copies[0].bufferOffset.z = texelOffsetZ;
|
||||
copy.copies[0].bufferSize.width = rowPitchInTexels;
|
||||
copy.copies[0].bufferSize.height = height + texelOffsetY;
|
||||
copy.copies[0].bufferSize.depth = depth + texelOffsetZ;
|
||||
|
||||
|
||||
copy.copies[1].textureOffset.x = x + copy.copies[0].copySize.width;
|
||||
copy.copies[1].textureOffset.y = y;
|
||||
copy.copies[1].textureOffset.z = z;
|
||||
|
||||
ASSERT(width > copy.copies[0].copySize.width);
|
||||
copy.copies[1].copySize.width = width - copy.copies[0].copySize.width;
|
||||
copy.copies[1].copySize.height = height;
|
||||
copy.copies[1].copySize.depth = depth;
|
||||
|
||||
copy.copies[1].bufferOffset.x = 0;
|
||||
copy.copies[1].bufferOffset.y = texelOffsetY + 1;
|
||||
copy.copies[1].bufferOffset.z = texelOffsetZ;
|
||||
copy.copies[1].bufferSize.width = copy.copies[1].copySize.width;
|
||||
copy.copies[1].bufferSize.height = height + texelOffsetY + 1;
|
||||
copy.copies[1].bufferSize.depth = depth + texelOffsetZ;
|
||||
|
||||
|
||||
return copy;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright 2017 The NXT 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 BACKEND_D3D12_TEXTURECOPYSPLITTER_H_
|
||||
#define BACKEND_D3D12_TEXTURECOPYSPLITTER_H_
|
||||
|
||||
#include "nxt/nxtcpp.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace backend {
|
||||
namespace d3d12 {
|
||||
|
||||
|
||||
struct TextureCopySplit {
|
||||
|
||||
static constexpr unsigned int kMaxTextureCopyRegions = 2;
|
||||
|
||||
struct Extent {
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
uint32_t depth = 0;
|
||||
};
|
||||
|
||||
struct Origin {
|
||||
uint32_t x = 0;
|
||||
uint32_t y = 0;
|
||||
uint32_t z = 0;
|
||||
};
|
||||
|
||||
struct CopyInfo {
|
||||
Origin textureOffset;
|
||||
Origin bufferOffset;
|
||||
Extent bufferSize;
|
||||
|
||||
Extent copySize;
|
||||
};
|
||||
|
||||
uint32_t offset = 0;
|
||||
uint32_t count = 0;
|
||||
std::array<CopyInfo, kMaxTextureCopyRegions> copies;
|
||||
};
|
||||
|
||||
TextureCopySplit ComputeTextureCopySplit(uint32_t x, uint32_t y, uint32_t z, uint32_t width, uint32_t height, uint32_t depth, uint32_t texelSize, uint32_t offset, uint32_t rowPitch);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif // BACKEND_D3D12_TEXTURECOPYSPLITTER_H_
|
Loading…
Reference in New Issue