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

Conversation

K0bin
Copy link
Collaborator

@K0bin K0bin commented Sep 7, 2020

Fixes #1757

Battle Engine Aquila relies on us not copying anything outside of the dirty regions that were set by LockRect and AddDirtyRect.

This could technically either be considered a game bug if the documentation is to be believed:

If the source texture has dirty regions, the copy can be optimized by restricting the copy to only those regions. It is not guaranteed that only those bytes marked dirty will be copied.

This has serious potential for regressions and I have not yet tested it with a variety of games. I'd like to get some feedback on the implementation first.

@K0bin K0bin force-pushed the update-texture-regions branch 6 times, most recently from 4736209 to d8f38ae Compare September 8, 2020 16:29
Copy link
Collaborator

@Joshua-Ashton Joshua-Ashton left a comment

Choose a reason for hiding this comment

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

Also needs to handle:

  • Autogen mipmaps (should probably dirty everything?)
  • Render target/depth stencil
  • pInitialData

Does native actually hold a list of boxes to update? Or does it just grow a single box?

@K0bin
Copy link
Collaborator Author

K0bin commented Sep 11, 2020

pInitialData is handled (notice I set the whole texture dirty in the constructor of CommonTexture).

Aside from that, I've implemented it the way the docs describe it:

You can dirty a portion of a texture by locking it, or by calling one of the following methods.
IDirect3DCubeTexture9::AddDirtyRect
IDirect3DTexture9::AddDirtyRect
IDirect3DVolumeTexture9::AddDirtyBox
IDirect3DDevice9::UpdateSurface

https://docs.microsoft.com/en-us/windows/win32/api/d3d9helper/nf-d3d9helper-idirect3dtexture9-adddirtyrect

Why does auto mip gen matter? We only track mip 0.

Does native actually hold a list of boxes to update? Or does it just grow a single box?

Honestly no idea. Nine just grows a single box. I initially did multiple boxes and have changed it to a single box since. That seems good enough for the broken game.

@Joshua-Ashton
Copy link
Collaborator

I am mainly thinking of the case where someone calls UpdateTexture after mips have been generated.

@K0bin
Copy link
Collaborator Author

K0bin commented Sep 13, 2020

If pSourceTexture is a non-autogenerated mipmap and pDestinationTexture is an autogenerated mipmap, only the topmost matching level is updated, and the destination sublevels are regenerated. All other source sublevels are ignored.
If both pSourceTexture and pDestinationTexture are autogenerated mipmaps, only the topmost matching level is updated. The sublevels from the source are ignored and the destination sublevels are regenerated.
If pSourceTexture is an autogenerated mipmap and pDestinationTexture a non-autogenerated mipmap, UpdateTexture will fail.

@K0bin
Copy link
Collaborator Author

K0bin commented Sep 13, 2020

Adjusted how auto mip gen textures are handled in UpdateTexture according to the docs above.

src/d3d9/d3d9_common_texture.h Outdated Show resolved Hide resolved
src/d3d9/d3d9_common_texture.h Outdated Show resolved Hide resolved
VkDeviceSize srcOffset = srcByteOffset.y * formatInfo->elementSize * blockCount.width
+ srcByteOffset.x * formatInfo->elementSize;
VkExtent2D srcExtent = VkExtent2D{ blockCount.width * formatInfo->blockSize.width,
blockCount.height * formatInfo->blockSize.height };
Copy link
Collaborator

Choose a reason for hiding this comment

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

spacing

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need anything more for srcOffset on 3D images?

offset.x / int32_t(formatInfo->blockSize.width),
offset.y / int32_t(formatInfo->blockSize.height)
};
VkDeviceSize srcOffset = srcByteOffset.y * formatInfo->elementSize * blockCount.width
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to do anything different here for 3D textures?

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.

@Joshua-Ashton
Copy link
Collaborator

Going to merge this so it can get some testing.

@Joshua-Ashton Joshua-Ashton merged commit ec5c324 into doitsujin:master Feb 14, 2021
@K0bin K0bin deleted the update-texture-regions branch December 3, 2022 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[d3d9] Battle Engine Aquila → broken textures
3 participants