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

Refactor Code of TextureConverter #3844

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

TheRealIndianBoi
Copy link

I have Refactored the code a bit:

For upload, combining both and adding default values reduces duplicate Code, also when is the size_vram_words given? I did not find any usecases

@OpenGOALBot
Copy link
Collaborator

Can one of the admins verify this patch?

@xTVaser
Copy link
Member

xTVaser commented Jan 19, 2025

Assuming this refactor cleans up the code in an effective way -- it does get rid of a bunch of seemingly (from my perspective) valuable comments and documentation. (A lot of the newly introduced comments are just stating the obvious as well)

@TheRealIndianBoi
Copy link
Author

@xTVaser

Assuming this refactor cleans up the code in an effective way -- it does get rid of a bunch of seemingly (from my perspective) valuable comments and documentation. (A lot of the newly introduced comments are just stating the obvious as well)

I can try to add the comments back, and for the newly introduced comments, i tried to describe the functions, so if someone hovers over it, it gives some details

Copy link
Collaborator

@water111 water111 left a comment

Choose a reason for hiding this comment

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

Thanks for taking a look at this!

I don't think this refactoring is correct for at least PSM4T.

In the future, if you are going to make a refactoring like this, please try to either include tests or some other proof that your changes are correct. Or make the changes so clear that a reviewer can be convinced easily.

};

// Pixel processing function
auto process_pixel = [&](u32 x, u32 y, u32 addr, int clut_psm) -> u32 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

x and y are not used in this lambda

Copy link
Author

Choose a reason for hiding this comment

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

yea i refactored everything again

// Main loop
for (u32 y = 0; y < h; y++) {
for (u32 x = 0; x < w; x++) {
u32 addr = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This addr variable is used incorrectly - in some cases it is treated as an address into the index texture, and others it is a value from the index texture (which is an encoding of an address into the clut)

Copy link
Author

Choose a reason for hiding this comment

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

I tried to initialize address depending on the old code. Now it should be right. Hopefully

for (u32 x = 0; x < w; x++) {
u32 addr = 0;
if (psm == int(PSM::PSMT8)) {
addr = psmt8_addr(x, y, read_width) + vram_addr * 256;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here, it is an address into the index texture.

Comment on lines 88 to 89
value = (addr4 & 1) ? (value >> 4) : (value & 0x0F);
addr = value;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here, it is set to a value from the index texture, which contains an encoded address into the clut. (notice the u8 value = *(u8*)(m_vram.data() + addr4 / 2); that's reading the texture).

Copy link
Author

Choose a reason for hiding this comment

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

Jep that was wrong. Fixed it


// Pixel processing function
auto process_pixel = [&](u32 x, u32 y, u32 addr, int clut_psm) -> u32 {
u8 value = *(u8*)(m_vram.data() + addr);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method of reading doesn't handle 4-bit index textures correctly (PSMT4)

Copy link
Author

Choose a reason for hiding this comment

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

fixed it

Comment on lines 48 to 54
u32 clut_chunk = value / 16;
u32 off_in_chunk = value % 16;
u8 clx = (clut_chunk & 1) ? 8 : 0;
u8 cly = (clut_chunk >> 1) * 2;
if (off_in_chunk >= 8) {
off_in_chunk -= 8;
cly++;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This scrambling is only valid for PSMT8 index textures and doesn't work for PSMT4 (see deleted comments about consulting GS manual 2.7.3 CLUT Storage Mode, IDTEX8 in CSM1 mode.)

Copy link
Author

Choose a reason for hiding this comment

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

True. Fixed it

Comment on lines 66 to 67
// Pixel processing function
auto process_pixel = [&](u32 x, u32 y, u32 addr, int clut_psm) -> u32 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is not very meaningful, and it's not intuitive to figure out what this function does. Is addr an address into the clut or the index texture? What does it return? Something like

auto lookup_rgba_from_clut = [&](u32 index_texture_value, int clut_psm) -> u32

may be more clear.

Or, since this lambda is only used one, you could avoid creating this entirely.

Copy link
Author

Choose a reason for hiding this comment

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

The lambda has been renamed, but since its called twice i avoided removing it

* @param height (Optional) Texture height in texels. Used if `size_vram_words` is not provided.
* @param size_vram_words (Optional) Texture size in VRAM words. Assumes a width of 128 texels.
*/
void upload(const u8* data, u32 dest, u32 width = 0, u32 height = 0, u32 size_vram_words = 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I prefer to use default arguments only when the defaults are valid, and prefer multiple implementations over tricky use of defaults.

For example, converter.upload(data, dest); will fail the validation, but looks like a valid call.

Copy link
Author

Choose a reason for hiding this comment

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

the test-case with converter.upload(data,dest) is validated by:
if ((size_vram_words == 0 && (width == 0 || height == 0)) || data == nullptr) {
throw std::invalid_argument(
"Invalid parameters: Provide either size_vram_words or width and height, and ensure data "
"is not null.");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants