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

Reorganize profiles into their own files #5

Closed
wants to merge 13 commits into from
Closed

Reorganize profiles into their own files #5

wants to merge 13 commits into from

Conversation

TheSpydog
Copy link
Contributor

Couldn't get to sleep last night, so I did what any normal person would do -- clean up MojoShader! I ripped out all the profiles and put them in their own files in the profiles/ subdirectory. IMO this has been long overdue, but after having done it, I can totally see why nobody attempted it before. :P

A few things:

  1. This works just fine in all of my tests, but it'd be good to make doubly sure I didn't break anything in the transition.
  2. It might be worth a pass over the new header files to make sure the formatting looks okay. Beauty wasn't a very high priority when working on this initial commit...
  3. We've got to figure out what to do about that mojoshader_internal.h error that I commented out. It's being fussy with the new files (probably due to some preprocessor ordering rule I'm not familiar with), but hopefully it won't be a difficult fix.
  4. Since we're already overhauling the project structure, what are your thoughts on killing off that ARB1 target? TBH I don't even know what it is -- the top google search for "arb1 shader" was MojoShader's website. But I do know that removing it would simplify some things and a fix a small weird bit in the current implementation.

@flibitijibibo
Copy link
Collaborator

flibitijibibo commented Apr 6, 2019

  1. I’ll most likely start with a side-by-side comparison along with a binary size comparison. I’m hoping it’ll be exactly the same without debug symbols...
  2. Any spots that are all new should be noted, just so we know what was actually added compared to the original.
  3. Probably just need to define that value before including stuff in the profile c files.
  4. I thought about this a lot, but only because the main file was so huge. ARB1 refers to ARB_vertex_program and ARB_fragment program, along with some NV shader extensions. Most (if not all) of this predates GLSL! This is how you can tell MojoShader was written in 2008... back then you needed these for good hardware support, but that’s not really true anymore. But I know Ryan likes his old hardware support, so I dunno if I want to mess with it. At least now it’s not as annoying to maintain.

@TheSpydog
Copy link
Contributor Author

  1. From my testing, the binary size (even when stripped) is quite a bit bigger than the previous version. I'm wondering if it's due to moving a lot of static inline functions out of mojoshader.c and into the mojoshader_profile_common.h where they're getting copied and pasted around several files.
  2. The profile header files are the only major addition, but they were copied directly from their corresponding source files (with small changes to macros and such). Beyond that, I don't think there was anything significant added...
  3. Yup, that was it.
  4. Makes sense!

@flibitijibibo
Copy link
Collaborator

Very well, I’ll try to review the refactor tomorrow.

We’ll have to figure out the best path for dealing with the static functions. May have to macro, may have to throw them in common.c. That may have to be a separate commit though, since this changes enough as it is...

Copy link
Collaborator

@flibitijibibo flibitijibibo left a comment

Choose a reason for hiding this comment

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

This was my first review pass. Overall it seems fine aside from style things, but I'm not crazy enough to expect perfect style from a 17k-line patch.

The main thing we need to document very very carefully in our final commit message is where everything moved. I tried marking spots that were clear transfers but then things fell apart a little since stuff got moved into profile_common, or were actually only used in certain spots, and some functions stayed as static inline while others did not, etc.

What might be the best next step is starting the process of replacing all the static inline functions with whatever is more appropriate now that it's all split up. Some of it could be macro-fied (i.e. all the one-liners) but that would mean editing even more code, so let's do that another time. What we can do now is try to group the related functions together and try to give them the same attributes. For example, if we have this...

static inline void Blorp1()
{
    lots;
    of;
    if (crap) {
       oh;
       good;
       grief;
    } // if
} // Blorp1

static inline int Blorp2(int zoop)
{
    return zoop == 69;
} // Blorp2

static inline int Blorp3(const char *why)
{
    return strcmp(why, "because");
} // Blorp3

... even though Blorp2/3 are trivial to inline, we have to be the bad guy and put all the Blorps in common.c together, instead of mixing/matching. Thankfully the only place where this got really messy was the register functions, everything else seemed clear.

The final version of this one patch will probably make the binary size a lot bigger, but fixing that can be a separate commit.

} Profile;


// Common utilities...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Marking this as a change of mojoshader.c:301...

ctx->free(ptr, ctx->malloc_data);
} // Free

int set_output(Context *ctx, Buffer **section);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Marking this as a change of mojoshader.c:342...

return ctx->isfail;
} // isfail

void failf(Context *ctx, const char *fmt, ...);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Marking this as a change of mojoshader.c:420...

void floatstr(Context *ctx, char *buf, size_t bufsize, float f,
int leavedecimal);

RegisterList *reglist_insert(Context *ctx, RegisterList *prev,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seeing lots of changes here compared to mojoshader.c:516...

dst->writemask3 = ((mask >> 3) & 1);
} // set_dstarg_writemask

int allocate_scratch_register(Context *ctx);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Marking as a change of mojoshader.c:743...

profiles/mojoshader_profile_d3d.c Outdated Show resolved Hide resolved
mojoshader.c Outdated Show resolved Hide resolved
mojoshader.c Show resolved Hide resolved
@TheSpydog
Copy link
Contributor Author

All right, all the formerly static inline functions from mojoshader_profile.h are now non-static, non-inline functions housed in mojoshader_profile_common.c. I've additionally tried to organize things a bit better to match the original declaration order of mojoshader.c, although there are still some notable changes and/or omissions:

Several functions are only ever used in mojoshader.c, so they're still defined as static functions in that file:

  • free_reglist()
  • reglist_exists()
  • register_was_written()
  • get_defined_register()
  • add_attribute_register()
  • add_sampler()
  • adjust_token_position()

A couple functions turned out to only be used by the ARB1 profile, so I moved them into mojoshader_profile_arb1.c:

  • allocate_scratch_register()
  • allocate_branch_label()

And finally, get_used_register() was literally never used anywhere, so I just removed it.

Also, I merged the PREDECLARE_PROFILE macro calls into the existing #if SUPPORT_PROFILE_* stuff, per your comment.

@TheSpydog
Copy link
Contributor Author

All right, with the help of Bloaty McBloatface and #pragma GCC visibility, I've managed to reduce the size difference between this version and the old version to a mere 56 bytes (when stripped and compiled with -DCOMPILER_SUPPORT=OFF).

Other notable change: usagestrs[] has been moved from mojoshader_profile.h to mojoshader_profile_d3d.c since only the D3D profile made use of this array.

@TheSpydog
Copy link
Contributor Author

What's left to do here before this can be merged? Are we waiting on the SPIR-V emitter so we can refactor that into the new format too?

@flibitijibibo
Copy link
Collaborator

flibitijibibo commented Apr 17, 2019

I'll do one last review of this today and if everything looks good we can make a Mercurial patch to push to upstream. EDIT: Have to push this to tomorrow, sorry!

int isscalar(Context *ctx, const MOJOSHADER_shaderType shader_type,
const RegisterType rtype, const int rnum);

static const char swizzle_channels[] = { 'x', 'y', 'z', 'w' };
Copy link
Collaborator

Choose a reason for hiding this comment

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

May be a possible cause of increased size, but just barely...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's annoying. I've tried moving it around and making it non-static, but no matter what it doesn't seem to affect the stripped binary size. :/

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's hopefully the optimizer being smart. Oh well, we can just leave this...

@flibitijibibo
Copy link
Collaborator

flibitijibibo commented Apr 23, 2019

Passed this through all of my compilers and they seem to be happy with everything. The very last thing we get to do also happens to be the most tedious: We'll need to fix parameter lists for functions that used to be static but aren't anymore. One example is isscalar, which looked like this...

https://github.com/FNA-XNA/MojoShader/pull/5/files#diff-d496920d66adab7260e5d2670f334605L766

... but now looks like this:

https://github.com/FNA-XNA/MojoShader/pull/5/files#diff-f6bd4f8f8bd83197cca2564b822858d9R357

We just have to line everything up again. Once that's done we should be good to go on the Hg version of this patch.

@TheSpydog
Copy link
Contributor Author

All right, I think I got 'em all. There could be a few that snuck by me, but we'll fix them as we see them.

Here's the hg patch (uploaded as a .txt file since GitHub doesn't like .patch files):
separateprofiles.txt

@flibitijibibo
Copy link
Collaborator

@krolli krolli mentioned this pull request Aug 26, 2019
TheSpydog pushed a commit to TheSpydog/MojoShader that referenced this pull request May 24, 2020
TheSpydog pushed a commit to TheSpydog/MojoShader that referenced this pull request May 26, 2020
TheSpydog pushed a commit to TheSpydog/MojoShader that referenced this pull request May 26, 2020
TheSpydog pushed a commit to TheSpydog/MojoShader that referenced this pull request Jun 24, 2020
TheSpydog pushed a commit to TheSpydog/MojoShader that referenced this pull request Jun 26, 2020
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