openfl icon indicating copy to clipboard operation
openfl copied to clipboard

Shader system improvements and fixes.

Open EliteMasterEric opened this issue 1 year ago • 37 comments

Provides the following changes:

  • Store the original values for the fragment/vertex header such that they can be accessed by child classes.
  • Store the original values for the fragment/vertex body such that they can be accessed by child classes.
  • Store the original values for the fragment/vertex source such that they can be accessed by child classes.
  • Added a configurable glVersion annotation (and corresponding variables) which can be used to modify the GLSL version directive. The default value has been set to 120, which should resolve an issue on some platforms where the uniform type is unavailable.
  • Added checks to __processGLData to ensure a variable exists before it is assigned via Reflection.
    • This resolves an issue caused by custom implementation of variables in child classes.
    • Added caching to the field check to reduce calls to Reflect.fields, improving performance.

These changes were performed to support fixes for FlxRuntimeShader, as part of the pull request HaxeFlixel/flixel-addons#368. Making changes here prevents code duplication in FlxRuntimeShader, improving maintainability.

These changes have been tested on the Windows HXCPP platform.

EliteMasterEric avatar Jul 29 '22 23:07 EliteMasterEric

Interesting. I was looking for a feature to change a CONST value at runtime. I think storing fragment body and header would be easier for me than parsing, diassembling an reassembling the whole shader. Some shaders need to use CONST in a for loop because non-constant values are forbidden, but different values can provide a better accuracy depending on what has been set in the uniform (e.g: Glow, Outline...). So it's looking great.

That being said, if you have only tested on Windows. This need to be tested in HL, MAC, Android, iOS and HTML5 before being merged.

Also can you provide details on your issue on "some platforms", which platforms are your talking about?

loudoweb avatar Aug 14 '22 09:08 loudoweb

i tested it on mac and i can confirm it works on mac too

JonnycatMeow avatar Aug 14 '22 12:08 JonnycatMeow

@MasterEric This pull request makes android to crash, I think is because of glVersion you added, gl version is different in android btw

MAJigsaw77 avatar Oct 11 '22 10:10 MAJigsaw77

@MasterEric This pull request makes android to crash, I think is because of glVersion you added, gl version is different in android btw

what glversion should be put to use for android?

JonnycatMeow avatar Oct 11 '22 17:10 JonnycatMeow

Idk the exact version

MAJigsaw77 avatar Oct 12 '22 03:10 MAJigsaw77

You have to find out, is your pull request anyway

MAJigsaw77 avatar Oct 12 '22 03:10 MAJigsaw77

the glsl version for android is #version 300 es how i found out is because of the flxFixedShader https://github.com/YoshiCrafter29/YoshiCrafterEngine/blob/45ddea338a211ff2d05b7d85a3a2c38bb8ab287f/source/FlxFixedShader.hx#L55

JonnycatMeow avatar Oct 12 '22 05:10 JonnycatMeow

That version is wrong

MAJigsaw77 avatar Oct 12 '22 12:10 MAJigsaw77

Took a bit of time today to fast forward the branch, as well as implement a couple fixes.

Mainly, glVersion is now a string, and the value defaults to 300 es on Android as suggested.

Hopefully this works for people, I've been having reports that AMD Radeon cards are having issues specifically.

EliteMasterEric avatar Dec 01 '22 23:12 EliteMasterEric

Dude, the default version by openfl is 100

MAJigsaw77 avatar Dec 02 '22 05:12 MAJigsaw77

If you put 300 es it will broke everything

MAJigsaw77 avatar Dec 02 '22 05:12 MAJigsaw77

If you put 300 es it will broke everything

*break

JonnycatMeow avatar Dec 02 '22 05:12 JonnycatMeow

Btw, apply that for enscripted and ios too, they use gl es too

MAJigsaw77 avatar Dec 02 '22 05:12 MAJigsaw77

Marked this PR as draft pending further changes, including:

  • Fixes for crashes when setting glVersion too high (built-in fragment headers need to be migrated).
  • Thorough testing on desktop, web, and Android to ensure default values are widely compatible (since nobody can agree what the correct values are supposed to be).
  • A @:glExtensions annotation to resolve issue #2601.

EliteMasterEric avatar Dec 02 '22 06:12 EliteMasterEric

I've been doing some more investigation into what GLSL versions actually support what.

I was previously under the assumption that 120 implemented the uniform keyword, and that not using this was the cause of the crashes some users have experienced while using shaders.

However, I had actually misread the specification. uniform variables are available in all versions of GLSL, with 120 being the first version to support shader initialization of uniform variables, like so:

// This is valid in all versions of GLSL
uniform float iTime;
// This is only valid in GLSL 120 and above
uniform float iTime = 0.0;

120 is NOT actually necessary to run most shaders, and many of the shaders which are not compatible can easily be converted by performing the initialization outside of GLSL (i.e. in Haxe code). 100 is sufficient to run most shaders, and furthermore appears to be widely supported. In the event I find a use case or platform in which 100 is not supported, compile-time macros can be used to modify the default shader version.

If a specific shader needs a feature introduced in a later language version, they can increase the language version, keeping in mind that most platforms have a very limited range of support (and your only solid choices are 100 and 300 es). It is recommended that you rewrite the shader if you need a language feature unavailable in 100 or 300 es.

EliteMasterEric avatar Dec 02 '22 16:12 EliteMasterEric

300 es removes texture2D and some things that I forgot, 100 is the default version that openfl/lime uses because it still have these futures.

MAJigsaw77 avatar Dec 02 '22 16:12 MAJigsaw77

As i see in the commit, now it should work.

MAJigsaw77 avatar Dec 02 '22 16:12 MAJigsaw77

The feature was not removed. 300 es removes texture2D but replaces it with texture.

By modifying the macro that builds the shaders, I have successfully tested and confirmed support for all GLSL versions on Windows Desktop.

100 is still a good default version to work with.

EliteMasterEric avatar Dec 02 '22 16:12 EliteMasterEric

Btw, 300 es isn't the only version that needs texture2D to be replaced there's 310 es and 320 es.

MAJigsaw77 avatar Dec 02 '22 16:12 MAJigsaw77

Btw, 300 es isn't the only version that needs texture2D to be replaced there's 310 es and 320 es.

All versions after 300 es require this replacement.

EliteMasterEric avatar Dec 02 '22 17:12 EliteMasterEric

i get an error trying to build an fnf game for mac and here is the error: Screen Shot 2022-12-02 at 10 12 12 PM

JonnycatMeow avatar Dec 03 '22 06:12 JonnycatMeow

actually nvm i fixed it

JonnycatMeow avatar Dec 03 '22 06:12 JonnycatMeow

This PR is now ready and fully tested on a variety of platforms.

See more at my test workbench here: https://github.com/MasterEric/Flixel-GalaxyShaderWorkbench

EDIT: Make sure to use the build instructions to ensure the correct library versions are installed before testing.

EliteMasterEric avatar Dec 04 '22 02:12 EliteMasterEric

With help from @JonnycatMeow I was able to test the workbench on their Mac platform. Notably, their version of OpenGL ONLY supports 110 and 120. This may be significant for those looking to resolve compatibility issues.

EliteMasterEric avatar Dec 04 '22 17:12 EliteMasterEric

bro can someone merge this please!!!!

JonnycatMeow avatar Dec 07 '22 17:12 JonnycatMeow

The Sound modification seems pretty handy for streaming although is it on purpose that the samplerate is locked at 44100?

Raltyro avatar Feb 26 '23 07:02 Raltyro

The Sound modification seems pretty handy for streaming although is it on purpose that the samplerate is locked at 44100?

This pull request is not related to sound.

EliteMasterEric avatar Feb 27 '23 03:02 EliteMasterEric

This pull request is not related to sound.

Look on the Commits tab of this PR. You merged in a SampleDataEvent commit, and maybe some other stuff. I mentioned this to you on Discord, but you never responded.

joshtynjala avatar Feb 27 '23 16:02 joshtynjala

This pull request is not related to sound.

Look on the Commits tab of this PR. You merged in a SampleDataEvent commit, and maybe some other stuff. I mentioned this to you on Discord, but you never responded.

Thank you for informing me of this. I will re-review my branch, make appropriate fixes, and merge them ASAP so that review can continue.

EliteMasterEric avatar Feb 27 '23 17:02 EliteMasterEric

It appears that changes related to other pull requests (#2534 and #2515) accidentally got included in the working branch for this PR. Those changes have now been removed, and the PR has additionally been fast-forwarded to match the target branch.

Please inform me here of any additional review changes.

EliteMasterEric avatar Feb 27 '23 17:02 EliteMasterEric