Skip to content

Conversation

@Florian-Simon-RedArtGames

With a 160x144 texture, the pitch would be aligned to be 1024 pixels wide, not 768 as D3D12 calculates it to be. This was causing issues on our end, where we would be drawing pixels with offsets that would accumulate over each row of pixels.

The width does not need to be a power of 2 - it turns out it just needs to be a multiple of 256, which is what D3D12_TEXTURE_DATA_PITCH_ALIGNMENT is defined to be.

@Florian-Simon-RedArtGames
Copy link
Author

A minor adjustment needs to be made to support all platforms that have D3D12 support. I'll try to update this branch with the necessary change.

@Florian-Simon-RedArtGames Florian-Simon-RedArtGames changed the title Fix incorrect calculations of row pitch in d3d12 implementation WIP: Fix incorrect calculations of row pitch in d3d12 implementation Jul 7, 2025
@MJ35512
Copy link

MJ35512 commented Aug 29, 2025

I try to write in a Texture2D with a native unity plugin in d3d12 mode and I don't understand how the row pitch of the texture works.
In d3d11 or OpenGL, the row pitch is "width x pixel size" but not in d3d12.
And this project seems not to work in d3d12.
Is there any bypass to do it ?

@Florian-Simon-RedArtGames
Copy link
Author

There is padding added for efficiency reasons. Width x pixel size doesn't work.

This PR changes the pitch calculation, but it doesn't work on all D3D12 platforms, and I haven't had time to include the fix we use internally to support all D3D12 platforms.

@MJ35512
Copy link

MJ35512 commented Aug 29, 2025

Could you give me this padding computing formula for PC ?

@Florian-Simon-RedArtGames
Copy link
Author

I don't remember the exact details, but the fomula stays unchanged. The only difference is the preprocessor defined used to get the alignment (D3D12_TEXTURE_DATA_PITCH_ALIGNMENT ). Have a look in your D3D12 header to find the actual preprocessor define for PC.

@MJ35512
Copy link

MJ35512 commented Aug 29, 2025

I carried out some tests.
To write on a 1300x800 texture the row pitch is 5374
To write on a 1772x984 texture the row pitch is 7168
So the padding change with the texture size. I'm trying to understand but I'm missing the rule used
I found D3D12_TEXTURE_DATA_PITCH_ALIGNMENT and the value is 256 but it is never used

@Florian-Simon-RedArtGames
Copy link
Author

Basically, the pitch has to be a multiple of 256 IIRC. If you found the define, then you can just use the code in this PR.

@Florian-Simon-RedArtGames
Copy link
Author

But my understanding might be wrong, because 5374 is not divisble by 256.

@MJ35512
Copy link

MJ35512 commented Aug 29, 2025

I found a formula but I can't explain why it is correct :-)
This is the actual rowPitch :
outRowPitch = max((int)AlignPow2(textureWidth * 4), 256);
And this is my solution to write correctly in the texture (and it works) :
realRowPitch = textureWidth * 4 + (outRowPitch - (textureWidth * 4)) % 256
Does this seem correct to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants