Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is in service of #7188: the JS shader API will not generate full shaders, just shader hooks, so to be able to make filter shaders this way, we need filter shader hooks.
Changes
baseFilterShader()
methodUnexpected things during implementation
Passing a texture to a hook
In other hooks, we just pass in a struct with all the inputs. A
sampler2D
can't work in a struct, though, since it isn't a basic data type. In the past we've avoided this by first sampling the texture, then passing the resulting color into the hook. Filters might need to sample the texture multiple times, though, so this is unavoidable.Instead, the hook format for filter shaders takes two parameters:
vec4 getColor(FilterInputs inputs, in sampler2D content)
. The other weirdness is that it has to be anin sampler2D
, not just asampler2D
, so we have to be aware that there may be qualifiers in hook parameters when parsing these into JS nodes for #7188.Pre-multiplied alpha
One of the hooks changes I've made in 2.0 is to make shader hooks deal exclusively in unmultiplied alpha. We need to use premultiplied alpha in general for rendering to appear correct. If you want to output 50% opacity white, you would have to output
vec4(0.5, 0.5, 0.5, 0.5)
, which is likely unintuitive since colors in the rest of p5 don't work this way. So, for other hooks, I divide out the alpha, pass that to the hook, and then assume the result is also unmultiplied, and multiply the alpha back in.In filter shaders, since we're passing the texture in directly for users to sample themselves, we can't unmultiply the alpha for them. To deal with this, I've created a
getTexture(in sampler2D tex, vec2 coord)
that users should use instead. This does the same thing as the nativetexture(tex, coord)
, but it returns unmultiplied alpha. When we are creating the JS shader API, we should likely map all texture calls to this new function for more expected results.