GithubHelp home page GithubHelp logo

salvatorepreviti / spglsl Goto Github PK

View Code? Open in Web Editor NEW
26.0 4.0 2.0 3.15 MB

A webgl GLSL minifier based on Angle compiler

CMake 0.22% JavaScript 48.92% TypeScript 5.21% HTML 25.90% GLSL 5.41% CSS 0.08% C++ 13.64% Shell 0.19% C 0.44%
glsl webgl2 webgl compiler minifier nodejs javascript webassembly node

spglsl's Introduction

spglsl - a webgl glsl minifier based on Google Angle, written for JS13K

Executes preprocessor directives, removes whitespaces, applies constant folding, mangle non exported functions and variable names. Used in "Island Not Found" https://github.com/SalvatorePreviti/js13k-2020 and "Dante" https://github.com/SalvatorePreviti/js13k-2022 (first place on JS13K 2022)

Installation

Package is precompiled at https://www.npmjs.com/package/spglsl

It includes a rollup plugin.

For an esbuild plugin, see https://github.com/rottencandy/esbuild-plugin-spglsl

npm i spglsl --save

Usage

import { spglslAngleCompile } from "spglsl";

async function compileFile() {
  const result = await spglslAngleCompile({
    mainFilePath: "myfile.frag",
    mainSourceCode: fs.readFileSync("myfile.frag", "utf8"),

    minify: true,

    // Mangle everything, except uniforms and globals, "main" and function starting with "main"
    mangle: true,

    // Map of global variables to rename
    mangle_global_map: {
      my_uniform_to_rename: "x",
      my_fragment_input_to_rename: "y",
    },
  });

  if (!result.valid) {
    console.log(result.infoLog);
    throw new Error("compilation failed");
  }

  // Prints all the uniform used, key is the original uniform name and value is the renamed uniform, if it was defined in mangle_global_map
  console.log(result.uniforms);

  // Prints all the globals used, key is the original variable name and value is the renamed uniform, if it was defined in mangle_global_map
  // Globals are varying, vertex and fragment input, outputs and buffers ...
  // Globals do not include uniforms.
  console.log(result.globals);

  return result.output;
}

There is also an handy rollup and vite plugin:

import { rollupPluginSpglsl } from "spglsl";

const pluginToPassToRollupOrVite = rollupPluginSpglsl({ ...options });

License

MIT license Salvatore Previti Copyright 2020-2022

Angle Copyright 2018 The ANGLE Project Authors.

Development

The project is mostly C++, compiled to WASM with Emscripten and runnable in NodeJS. Look at project/scripts folder on how to install dependencies and build.

spglsl's People

Contributors

salvatorepreviti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

spglsl's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.