p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

filter() crashes on createGraphics(WEBGL) buffers

Open aashu2006 opened this issue 9 months ago • 12 comments

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [ ] Color
  • [x] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] p5.strands
  • [x] WebGL
  • [ ] DevOps, Build process, Unit testing
  • [ ] Internationalization (i18n)
  • [ ] Friendly Errors
  • [ ] Other (specify if possible)

p5.js version

v2.2.1 and v2.2.2-rc.2 (reproduces in both)

Web browser and version

Google Chrome 145.0.7632.75 (Official Build, 64-bit)

Operating system

Linux Ubuntu 24.04 LTS

Steps to reproduce this

  1. Create a WebGL canvas.
  2. Create a secondary createGraphics() buffer in WEBGL mode.
  3. Draw a shape on the graphics buffer.
  4. Call filter() on the graphics buffer.

The sketch crashes with a TypeError

Snippet:


function setup() {
  createCanvas(100, 100, WEBGL);

  const g = createGraphics(100, 100, WEBGL);
  g.noStroke();
  g.fill('red');
  g.circle(0, 0, 40);

  g.filter(INVERT); // crashes
}

Console Error:

Uncaught (in promise) TypeError: e.getColor is not a function
    at _makeFilterShader (p5.min.js)
    at filter (p5.min.js)
    at Graphics.filter

Calling filter() on a createGraphics(WEBGL) renderer causes a runtime TypeError originating inside the WebGL filter shader pipeline.

aashu2006 avatar Feb 21 '26 22:02 aashu2006

Hii, @ksen0 I would be interested in working on this, if that’s okay.

aashu2006 avatar Feb 21 '26 22:02 aashu2006

Thanks for finding this! As you observe, it was introduced in 2.2. I've assigned it to you. As you know there's a lot of active development around this, so when you have some idea(s) for the solution, please post an update here and tag @davepagurek for visibility

ksen0 avatar Feb 22 '26 19:02 ksen0

I suspect part of the problem is that a p5.Graphics doesn't automatically get functions defined on p5.prototype. We add shader hook objects to fn here to make them visible: https://github.com/processing/p5.js/blob/9c7a97fc70364eb4588a6aace82bfe8b1a05f3ec/src/strands/strands_api.js#L716

...but that doesn't get added to p5.Graphics. One approach could be to manually add this to p5.Graphics there too, although arguably there's a wider problem of deciding whether p5.Graphics should automatically get fn extensions added to it with some exceptions (like draw loop things), or whether by default nothing is added except what we manually add. See also https://github.com/processing/p5.js/issues/8170 for some previous discussion on that if you have thoughts!

davepagurek avatar Feb 22 '26 20:02 davepagurek

Thanks for the insight @davepagurek I will look into how shader hook functions are attached and whether they are missing on p5.Graphics instances. I will post findings shortly.

aashu2006 avatar Feb 24 '26 14:02 aashu2006

Hi @davepagurek I checked and confirmed that p5.Graphics does not inherit the strands hook functions added to p5.prototype e.g - getColor. Since _makeFilterShader passes the graphics instance as the p5 scope, shader callbacks attempt to call p5.getColor, which does not exist on p5.Graphics.prototype, causing the TypeError.

This seems to be an architectural mismatch between how strands attaches hooks (to p5.prototype) and how p5.Graphics is structured (manual prototype composition).

I see 2 possible directions:

  1. Extending strands hooks to p5.Graphics.prototype
  2. Adjusting how the shader scope binds p5 for Graphics contexts

aashu2006 avatar Feb 24 '26 15:02 aashu2006

I think (1) is likely the easiest to start with -- in addition to setting fn[name] = hook, also explicitly attaching to p5.Graphics.prototype too -- but I think updating how p5.Graphics works generally is also a possibility, but needs a bit more discussion. I think I generally fall in the camp of thinking p5.Graphics should be equivalent to a p5 instance with the exception of the draw loop rather than the reverse. @limzykenneth do you have some thoughts here?

Since doing that is sort of blocked on higher level decisionmaking, it could make sense to do a quick fix using (1) at first, and then we could do a longer term fix for p5.Graphics later if we decide that's the way to go.

davepagurek avatar Feb 24 '26 15:02 davepagurek

@davepagurek getTexture and other strands API functions are installed via initGlobalStrandsAPI directly onto p5.prototype, not through createShaderHooksFunctions

So addressing only createShaderHooksFunctions is not sufficient. p5.Graphics is missing the broader strands API layer as well. It seems we could either extend initGlobalStrandsAPI to also attach its functions to p5.Graphics.prototype or rethink how p5.Graphics inherits from p5.prototype.

aashu2006 avatar Feb 24 '26 18:02 aashu2006

Yep you're right! We'd need it there too. Basically anywhere in p5.strands where we augment fn we'd also have to augment p5.Graphics.prototype.

davepagurek avatar Feb 24 '26 18:02 davepagurek

@davepagurek I went ahead with the approach we discussed. Instead of patching individual functions, I mirrored all fn (p5.prototype) augmentations onto p5.Graphics.prototype.

  • Permanent strands APIs e.g - getTexture, vec*, dot etc. are mirrored via a helper augmentFn with unconditional overwrite.
  • Temporary shader hooks e.g. getColor are mirrored via augmentFnTemporary and restored in deinitStrandsContext.

This ensures createGraphics(WEBGL).filter() has access to the full strands layer without leaking temporary hooks. WebGL unit + visual tests pass locally, and the manual test confirms the crash is resolved. Let me know if this direction looks good. Thanks 🙂

aashu2006 avatar Feb 24 '26 20:02 aashu2006

that sounds great, thanks!

davepagurek avatar Feb 25 '26 13:02 davepagurek

Great, I will clean things up and open a PR shortly.

aashu2006 avatar Feb 25 '26 13:02 aashu2006

Opened PR #8568 which implements the structural fix discussed above. It mirrors strands augmentations onto p5.Graphics.prototype, ensuring createGraphics(WEBGL).filter() has access to the full strands layer while properly cleaning up temporary hooks. Please feel free to review there, happy to adjust if needed.

aashu2006 avatar Feb 25 '26 19:02 aashu2006