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

[shader-ast] texture2D call in vertexShader causes error. #278

Merged
merged 3 commits into from
Feb 23, 2021

Conversation

jamieowen
Copy link
Contributor

Hi,

When making a texture2D call in a vertexShader it causes errors because the last bias argument is defaulted to 0.0.

( Line 11 )

ERROR: 0:11: 'texture2D' : no matching overloaded function found
ERROR: 0:11: '=' : dimension mismatch
ERROR: 0:11: '=' : cannot convert from 'const mediump float' to 'highp 4-component vector of float'
�1: #version 100
2: uniform mat4 projectionMatrix;
3: uniform mat4 modelViewMatrix;
4: uniform sampler2D state_1;
5: uniform float point_size;
6: uniform vec2 resolution;
7: attribute vec3 position;
8: attribute float offset;
9: void main() {
10: vec2 _s9k = (vec2((offset / resolution.x), mod(offset, resolution.y)) / resolution);
11: vec4 _s9l = texture2D(state_1, _s9k, 0.0); // bias is 0.0 
12: gl_PointSize = point_size;
13: gl_Position = (projectionMatrix * (modelViewMatrix * vec4(position, 1.0)));
14: }

I've removed the defaults in this PR for texture2D and included texelFetch and textureOffset calls as well. It seemed the most logical thing to remove all defaults given the error!

The texture2D one works for me but haven't tested the situtations with texelFetch and textureOffset.

This webgl reference mentions the lack of bias arg on a vertexShader:
https://www.khronos.org/files/webgl/webgl-reference-card-1_0.pdf

But I can't seem to find the definitive webgl reference these days. Not that mention VS/FS differences anyway!
https://www.khronos.org/opengles/sdk/docs/manglsl/docbook4/

Oh actually, another thing.. is there any support for specifying names of variables in sym() calls? ( so no _s9k,s9l, etc ) It didn't work when i tried specifying...

Cheers!

@postspectacular
Copy link
Member

Thanks for this @jamieowen - I will have to do more studying of the different GLSL ES refs, specifically the WebGL2 texture functions. TBH I don't think I've ever used textures in a vertex shader, so would explain the oversight... will report back ASAP!

@postspectacular
Copy link
Member

Just wanted to briefly follow up re: your Q about symbol naming... You can assign a custom name via this function signature, e.g.

// sym(type, name, opts, initval)
sym("vec2", "foo", {}, vec2(1))
import { targetGLSL } from "@thi.ng/shader-ast-glsl";

targetGLSL()(decl(sym("vec2", "foo", {}, vec2(1))))
// 'vec2 foo = vec2(1.0)'

What's your use case for that? Human legible output / debugging of the generated GLSL?

@jamieowen
Copy link
Contributor Author

jamieowen commented Feb 23, 2021

Ahh.. cheers! I had overlooked that, makes sense it needs an init value.

Yes use case is mainly for debugging, especially on larger chunks of glsl.
Although i do like a good variable name!

Also, I had converted these photoshop blending modes a few years ago :
https://github.com/jamieowen/glsl-blend

They work with glslify, but i've been meaning to update them to include an es6 template string version as well.

I was potentially going to convert to typescript/shader-ast and then generate the source from there. may be with glsl 1 + 3 versions.

@postspectacular
Copy link
Member

Cool. I also hope you're aware that there're already are all 12 Porter-Duff blend operators available in the shader-ast-stdlib package here (i know they're not all the same as PS modes, but some are):

@jamieowen
Copy link
Contributor Author

Cheers.

That's good to know!

@postspectacular
Copy link
Member

So I just did some more digging through the WebGL2 / GLSL ES 3.0 docs and think your PR is good to go (I might finally add some of those other missing texture fns too)! Thank you!!! :)

Screenshot 2021-02-23 at 19 48 38

Screenshot 2021-02-23 at 19 50 05

@postspectacular postspectacular merged commit 8c56921 into thi-ng:develop Feb 23, 2021
@jamieowen
Copy link
Contributor Author

When I get around to converting the photoshop ones . I could add to thi.ng/shader-ast-stdlib if you like.

That is if you think they are relevant. Not sure how streamlined you would want to keep it.

Definitely appreciate this kind of modular approach though. It's been a refreshing learning curve!

The only thing similar to this for webgl has been stack.gl, but the micro module/git repo approach was pretty mental.

@jamieowen
Copy link
Contributor Author

Grand stuff! ( For texture stuff )

@postspectacular
Copy link
Member

Since all of these functions are easily tree-shakeable I'm not worried about the overall size for this package. So thank you for considering adding to it! 👍

Re: learning curve - I'm glad to hear it... The Twitter conversations these past few days seem to argue the opposite... but am always keen to hear to how we could make things easier to grasp!

@postspectacular
Copy link
Member

@jamieowen one more small thing to ask (for the future): Can you please use the Conventional Commits format for your commit messages? This is quite important for a project of this scale and used to auto-generate changelogs & visualization purposes (e.g. the timelines on the thi.ng website). Thank you! :)

@jamieowen
Copy link
Contributor Author

Yes no problem.. Apologies for not checking first!

@postspectacular
Copy link
Member

No worries & also just wanted to let you know that your changes are now published:

https://github.com/thi-ng/umbrella/blob/develop/packages/shader-ast/CHANGELOG.md

(ps. if you had used conventional commits, your commits would have been listed in this file now too... 😸 just sayin').

@jamieowen
Copy link
Contributor Author

Haha.. I am actually quite disappointed!

This will be a tough stain to remove from my quite limited open source contributions!

@postspectacular
Copy link
Member

Well, let's hope you're just getting started... keeping an eye out for that blendmode PR soon :) Happy to advise if you get stuck w/ shader-ast. After my relocation (likely in the next few months), I also want to start tackling the WGSL code target (WebGPU)... and there's a WASM target waiting too

@jamieowen
Copy link
Contributor Author

Thanks, I will get on it ASAP, will let you know if I have any problems.

Looking forward to the Web GPU target!

@jamieowen
Copy link
Contributor Author

Hey @postspectacular.

Not sure best place to post this but...

Managed to get a version going with the photoshop blend modes.. all composable and with vec3 or vec4 options.

All implementations are defined with one liners as either a vec 3/4 or float input/output:

export const blendAverageVec: BlendModeVec<"vec3" | "vec4"> = (base, blend) =>
  div(add(base, blend), float(2.0));

export const blendColorBurnFloat: BlendModeFloat = (base, blend) =>
  ternary(
    lte(blend, float(0.0)),
    blend,
    max(sub(float(1.0), div(sub(float(1.0), base), blend)), float(0.0))
  );

The float versions are converted to vec3 / vec4 versions :

export const blendFloatToVec3 = (
  blendFloat: BlendModeFloat
): BlendModeVec<"vec3"> => (base, blend) =>
  vec3(
    blendFloat($x(base), $x(blend)),
    blendFloat($y(base), $y(blend)),
    blendFloat($z(base), $z(blend))
  );

export const blendFloatToVec4 = (
  blendFloat: BlendModeFloat
): BlendModeVec<"vec4"> => (base, blend) =>
  vec4(
    blendFloat($x(base), $x(blend)),
    blendFloat($y(base), $y(blend)),
    blendFloat($z(base), $z(blend)),
    blendFloat($w(base), $w(blend))
  );

Then they are wrapped using the following to define the function and apply the opacity base/blend shenanigans..

export const blendLayerOpacity = (
  blendFn: BlendModeVec<"vec3" | "vec4">,
  base: Term<"vec3" | "vec4">,
  blend: Term<"vec3" | "vec4">,
  opacity: Term<"float">
) =>
  add(mul(blendFn(base, blend), opacity), mul(base, sub(float(1.0), opacity)));

export const defBlendFn3 = (blendFn: BlendModeVec<"vec3">, fnName: string) =>
  defn("vec3", fnName, ["vec3", "vec3", "float"], (base, blend, opacity) => [
    ret(blendLayerOpacity(blendFn, base, blend, opacity)),
  ]);

export const defBlendFn4 = (blendFn: BlendModeVec<"vec4">, fnName: string) =>
  defn("vec4", fnName, ["vec4", "vec4", "float"], (base, blend, opacity) => [
    ret(blendLayerOpacity(blendFn, base, blend, opacity)),
  ]);

Super pleased with the results. Makes defining new blend functions really easy.
Really enjoying this way of composing glsl!

Not sure if you have any other suggestions for handling vec3/vec4 options.
Or may be the typescript definitions..

Preview:
http://jamieowen.github.io/glsl-blend/shader-ast.html
And code:
https://github.com/jamieowen/glsl-blend/tree/v2/src

I've got a handful more to go.. but will have to look at them soon.

@postspectacular
Copy link
Member

This is awesome! I've forked your repo and will send you PR w/ some further possible suggestions/simplifications - but much impressed to see how you picked this up! 🙇

@jamieowen
Copy link
Contributor Author

Great stuff. Glad you like it!
Yeah it's a little confusing to begin with but once I got the Term/Sym distinction it all fits into place.

I think the trickiest thing is getting typescript types right..

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