npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prettier-plugin-glsl

v0.2.0

Published

Prettier (https://prettier.io) plugin for GLSL (OpenGL Shading Language).

Downloads

13,372

Readme

npm NPM

prettier-plugin-glsl

This is a plugin for Prettier (version 3.x), the opinionated code formatter, for GLSL, the shading language used in WebGL and other places. It uses a custom parser based on Chevrotain and does not require any external dependencies.

NB: this is still in active development, breaking/formatting changes may be included in any version.

Formatting

This plugin tries to match the formatting rules for JavaScript as closely as possible. Issues or PRs which make the formatting closer to JavaScript are welcome.

Formatting of macros

This plugin will attempt to parse #define macros as top-level declarations, statements or expressions. If successful, these will be formatted as usual.

For example, #define MAX3(genType) genType max3(genType a, genType b, genType c) { /* comment */ return max(max(a, b), c); } will be formatted as:

#define MAX3(genType)                                      \
  genType max3(genType a, genType b, genType c) {          \
    /* comment */                                          \
    return max(max(a, b), c);                              \
  }

Formatting of comments

Comments which start with /** will be passed to the markdown formatter.

For an example, see ./builtins.glsl.

Installation

npm install --save-dev prettier-plugin-glsl

Prettier will automatically pick up the plugin. The following extensions will be recognized as GLSL files by default.

.fp .frag .frg .fs .fsh .fshader .geo .geom .glsl .glslf .glslv .gs .gshader .rchit .rmiss .shader .tesc .tese .vert .vrx .vsh .vshader

Note that .frag files are recognized as JavaScript files by default. Add the following to your Prettier configuration to format them as GLSL.

"overrides": [{"files": ["*.frag"], "options": {"parser": "glsl-parser"}}]

Limitations due to preprocessor

As GLSL includes a C++-style preprocessor, this presents some difficulties when formatting. For example, the plugin does not attempt to be able to format crazy (but technically valid) constructs such as:

#define LBRACE {
void main() LBRACE
}

Instead, the plugin effectively treats preprocessor directives such as #define as their own statements. This covers most cases, for example, the following works fine:

#define AA 2
#define ZERO (min(iFrame, 0))
void main() {
  // ...
  #if AA > 1
  for (int m = ZERO; m < AA; m++)
  for (int n = ZERO; n < AA; n++) {
    // pixel coordinates
    vec2 o = vec2(float(m), float(n)) / float(AA) - 0.5;
    vec2 p = (2.0 * (fragCoord + o) - iResolution.xy) / iResolution.y;
    #else
    vec2 p = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
    #endif

    // use p

    #if AA > 1
  }
  tot /= float(AA * AA);
  #endif
}

However, the following does not, as the #else and #endif are treated as the bodies of the if-statement, which leads to the else following a {} block instead of an if block, which is invalid.

#if FOO
if (a())
#else
if (b())
#endif
{ }
else
{ }

In general this approach works well. Of the top 100 Shadertoy shaders, 145/152 compilation units (95%) can be formatted without any changes.

  • 1 has actually invalid code. (It doesn't cause compilation errors as it is excluded via #if/#endif.)
  • 1 uses a not self-contained function macro. (It requires a specific token before its call.)
  • 4 mix #if/#else/#endif and if/else which leads to issues as above.
  • 1 uses complicated macro constructions to compose music.