Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[d3d9] Track dirty regions for UpdateTexture #1759

Merged
merged 3 commits into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/d3d9/d3d9_common_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ namespace dxvk {
? D3D9Format::D32
: D3D9Format::X8R8G8B8;

for (uint32_t i = 0; i < m_updateDirtyBoxes.size(); i++) {
AddUpdateDirtyBox(nullptr, i);
}

m_mapping = pDevice->LookupFormat(m_desc.Format);

m_mapMode = DetermineMapMode();
Expand Down
36 changes: 36 additions & 0 deletions src/d3d9/d3d9_common_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,40 @@ namespace dxvk {
void PreLoadAll();
void PreLoadSubresource(UINT Subresource);

void AddUpdateDirtyBox(CONST D3DBOX* pDirtyBox, uint32_t layer) {
if (pDirtyBox) {
D3DBOX box = *pDirtyBox;
if (box.Right <= box.Left
|| box.Bottom <= box.Top
|| box.Back <= box.Front)
return;

D3DBOX& updateBox = m_updateDirtyBoxes[layer];
if (updateBox.Left == updateBox.Right) {
updateBox = box;
} else {
updateBox.Left = std::min(updateBox.Left, box.Left);
updateBox.Right = std::max(updateBox.Right, box.Right);
updateBox.Top = std::min(updateBox.Top, box.Top);
updateBox.Bottom = std::max(updateBox.Bottom, box.Bottom);
updateBox.Front = std::min(updateBox.Front, box.Front);
updateBox.Back = std::max(updateBox.Back, box.Back);
}
} else {
m_updateDirtyBoxes[layer] = { 0, 0, m_desc.Width, m_desc.Height, 0, m_desc.Depth };
}
}

void ClearUpdateDirtyBoxes() {
for (uint32_t i = 0; i < m_updateDirtyBoxes.size(); i++) {
m_updateDirtyBoxes[i] = { 0, 0, 0, 0, 0, 0 };
}
}

const D3DBOX& GetUpdateDirtyBox(uint32_t layer) const {
return m_updateDirtyBoxes[layer];
}

private:

D3D9DeviceEx* m_device;
Expand Down Expand Up @@ -406,6 +440,8 @@ namespace dxvk {

D3DTEXTUREFILTERTYPE m_mipFilter = D3DTEXF_LINEAR;

std::array<D3DBOX, 6> m_updateDirtyBoxes;

/**
* \brief Mip level
* \returns Size of packed mip level in bytes
Expand Down
78 changes: 63 additions & 15 deletions src/d3d9/d3d9_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,19 @@ namespace dxvk {
if (unlikely(srcTextureInfo->Desc()->Format != dstTextureInfo->Desc()->Format))
return D3DERR_INVALIDCALL;

if (dst->GetMipLevel() == 0) {
if (pSourceRect) {
D3DBOX updateDirtyRect = { UINT(pSourceRect->left), UINT(pSourceRect->top), UINT(pSourceRect->right), UINT(pSourceRect->bottom), 0, 1 };
if (pDestinationSurface) {
updateDirtyRect.Left = pDestPoint->x;
updateDirtyRect.Top = pDestPoint->y;
}
dstTextureInfo->AddUpdateDirtyBox(&updateDirtyRect, dst->GetFace());
} else {
dstTextureInfo->AddUpdateDirtyBox(nullptr, dst->GetFace());
}
}

const DxvkFormatInfo* formatInfo = imageFormatInfo(dstTextureInfo->GetFormatMapping().FormatColor);

VkOffset3D srcBlockOffset = { 0u, 0u, 0u };
Expand Down Expand Up @@ -723,39 +736,69 @@ namespace dxvk {
return D3DERR_INVALIDCALL;

const Rc<DxvkImage> dstImage = dstTexInfo->GetImage();
const DxvkFormatInfo* formatInfo = imageFormatInfo(dstTexInfo->GetFormatMapping().FormatColor);
uint32_t mipLevels = std::min(srcTexInfo->Desc()->MipLevels, dstTexInfo->Desc()->MipLevels);
uint32_t arraySlices = std::min(srcTexInfo->Desc()->ArraySize, dstTexInfo->Desc()->ArraySize);

if (unlikely(srcTexInfo->IsAutomaticMip() && !dstTexInfo->IsAutomaticMip()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm scared of this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs seem to say this is correct so I guess we can go with it for now...

Docs have lied in the past however 🐸

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write a test then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return D3DERR_INVALIDCALL;

if (dstTexInfo->IsAutomaticMip())
mipLevels = 1;

for (uint32_t a = 0; a < arraySlices; a++) {
const D3DBOX& box = srcTexInfo->GetUpdateDirtyBox(a);
for (uint32_t m = 0; m < mipLevels; m++) {
Rc<DxvkBuffer> srcBuffer = srcTexInfo->GetBuffer(srcTexInfo->CalcSubresource(a, m));

VkImageSubresourceLayers dstLayers = { VK_IMAGE_ASPECT_COLOR_BIT, m, a, 1 };

VkExtent3D extent = dstImage->mipLevelExtent(m);


VkOffset3D offset = {
alignDown(int32_t(box.Left) >> m, int32_t(formatInfo->blockSize.width)),
alignDown(int32_t(box.Top) >> m, int32_t(formatInfo->blockSize.height)),
alignDown(int32_t(box.Front) >> m, int32_t(formatInfo->blockSize.depth))
};
VkExtent3D extent = {
alignDown(uint32_t((box.Right - box.Left) >> m), formatInfo->blockSize.width),
alignDown(uint32_t((box.Bottom - box.Top) >> m), formatInfo->blockSize.height),
alignDown(uint32_t((box.Back - box.Front) >> m), formatInfo->blockSize.depth),
};

VkExtent3D levelExtent = dstImage->mipLevelExtent(m);
VkExtent3D blockCount = util::computeBlockCount(levelExtent, formatInfo->blockSize);
VkOffset3D srcByteOffset = {
offset.x / int32_t(formatInfo->blockSize.width),
offset.y / int32_t(formatInfo->blockSize.height),
offset.z / int32_t(formatInfo->blockSize.depth)
};
VkDeviceSize srcOffset = srcByteOffset.z * formatInfo->elementSize * blockCount.depth
+ srcByteOffset.y * formatInfo->elementSize * blockCount.width
+ srcByteOffset.x * formatInfo->elementSize;
VkExtent2D srcExtent = VkExtent2D{ blockCount.width * formatInfo->blockSize.width,
blockCount.height * formatInfo->blockSize.height };

EmitCs([
cDstImage = dstImage,
cSrcBuffer = srcBuffer,
cDstLayers = dstLayers,
cExtent = extent
cExtent = extent,
cOffset = offset,
cSrcOffset = srcOffset,
cSrcExtent = srcExtent
] (DxvkContext* ctx) {
ctx->copyBufferToImage(
cDstImage, cDstLayers,
VkOffset3D{ 0, 0, 0 }, cExtent,
cSrcBuffer, 0, { 0u, 0u });
cOffset, cExtent,
cSrcBuffer, cSrcOffset,
cSrcExtent);
});

dstTexInfo->SetDirty(dstTexInfo->CalcSubresource(a, m), true);
}
}

if (dstTexInfo->IsAutomaticMip()) {
for (uint32_t i = 0; i < dstTexInfo->Desc()->ArraySize; i++)
dstTexInfo->SetDirty(dstTexInfo->CalcSubresource(i, 0), true);

srcTexInfo->ClearUpdateDirtyBoxes();
if (dstTexInfo->IsAutomaticMip() && mipLevels != dstTexInfo->Desc()->MipLevels)
MarkTextureMipsDirty(dstTexInfo);
}
else
dstTexInfo->MarkAllDirty();

FlushImplicit(false);

Expand Down Expand Up @@ -3923,6 +3966,10 @@ namespace dxvk {
if (unlikely((Flags & (D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE)) == (D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE)))
Flags &= ~D3DLOCK_DISCARD;

if (!(Flags & D3DLOCK_NO_DIRTY_UPDATE) && !(Flags & D3DLOCK_READONLY)) {
pResource->AddUpdateDirtyBox(pBox, Face);
}

auto& desc = *(pResource->Desc());

bool alloced = pResource->CreateBufferSubresource(Subresource);
Expand Down Expand Up @@ -4969,6 +5016,7 @@ namespace dxvk {

void D3D9DeviceEx::MarkTextureMipsDirty(D3D9CommonTexture* pResource) {
pResource->SetNeedsMipGen(true);
pResource->MarkAllDirty();

for (uint32_t tex = m_activeTextures; tex; tex &= tex - 1) {
// Guaranteed to not be nullptr...
Expand Down
14 changes: 13 additions & 1 deletion src/d3d9/d3d9_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ namespace dxvk {


HRESULT STDMETHODCALLTYPE D3D9Texture2D::AddDirtyRect(CONST RECT* pDirtyRect) {
if (pDirtyRect) {
D3DBOX box = { UINT(pDirtyRect->left), UINT(pDirtyRect->top), UINT(pDirtyRect->right), UINT(pDirtyRect->bottom), 0, 1 };
m_texture.AddUpdateDirtyBox(&box, 0);
} else {
m_texture.AddUpdateDirtyBox(nullptr, 0);
}
return D3D_OK;
}

Expand Down Expand Up @@ -151,8 +157,8 @@ namespace dxvk {
return GetSubresource(Level)->UnlockBox();
}


HRESULT STDMETHODCALLTYPE D3D9Texture3D::AddDirtyBox(CONST D3DBOX* pDirtyBox) {
m_texture.AddUpdateDirtyBox(pDirtyBox, 0);
return D3D_OK;
}

Expand Down Expand Up @@ -230,6 +236,12 @@ namespace dxvk {


HRESULT STDMETHODCALLTYPE D3D9TextureCube::AddDirtyRect(D3DCUBEMAP_FACES Face, CONST RECT* pDirtyRect) {
if (pDirtyRect) {
D3DBOX box = { UINT(pDirtyRect->left), UINT(pDirtyRect->top), UINT(pDirtyRect->right), UINT(pDirtyRect->bottom), 0, 1 };
m_texture.AddUpdateDirtyBox(&box, Face);
} else {
m_texture.AddUpdateDirtyBox(nullptr, Face);
}
return D3D_OK;
}

Expand Down