Skip to main content

No project description provided

Project description

hy2glsl: Hy to GLSL Language Translator

This is an exploratory experiment to translate Hy procedure to GLSL shader, inspired by the varjo translator.

Data Types

Uniform, varying and outputs need to be typed. It may be possible to infer the type by knowing the associated CPU (e.g. numpy) object type.

Function overloading is not supported as the current implementation maps one function signature to one function name.

For function signature and variables, the types are inferred using this process:

  • Variable definition types are inferred from the value expression:
    • look for known constructor like vec2()
    • look for known variable type already inferred
    • look for known symbol type like float? or integer?
  • Function argument types are inferred using a reverse order first pass
  • Function return types are inferred from the last expression of the body.

This type inferrence process works for such shader:

(setv var (vec2 0.0))
(defn nested-func [arg] ...)
(defn func [a b] (nested-func (+ a b)))
(defn main [] (func var 2))

But it will fail when function arguments are solely defined by return type:

(defn func [] (return 42))
(defn other-func [a] a)
(defn main [] (other-func (func)))

Fortunately, shader usualy mutates return value before using them as a function argument, therefor that primitive process works in most cases.

Example:

A mandelbrot fragment shader in Hy:

(setv MAX_ITER 42.0)
(print (hy2glsl `(shader
  (version 350)
  (uniform vec2 iResolution)
  (uniform vec2 center)
  (uniform float range)
  (defn mandelbrot-color [coord]
    (setv idx 0.0)
    (setv z (vec2 0.0))
    (setv c coord)
    (while (< idx ~MAX_ITER)
      (setv z (+ (vec2 (- (* z.x z.x) (* z.y z.y))
                       (* 2.0 z.x z.y))
                 c))
      (if (> (dot z z) 500.0)
        (break))
      (setv idx (+ idx 1)))
    (vec3 (* 1.0 (/ idx ~MAX_ITER))))
  (defn main []
    (setv uv (- (* (/ gl_FragCoord.xy iResolution.xy) 2.) 1.0))
    (setv uv.y (* uv.y (- (/ iResolution.y iResolution.x))))
    (setv pos (+ center (* uv range)))
    (setv gl_FragColor (vec4 (mandelbrot-color pos) 1.0))))))

Results in:

#version 350
uniform vec2 iResolution;
uniform vec2 center;
uniform float range;

vec3 mandelbrot_color(vec2 coord) {
  float idx = 0.0;
  vec2 z = vec2(0.0);
  vec2 c = coord;
  while (idx < 42.0) {
    z = vec2(z.x * z.x - z.y * z.y, 2.0 * z.x * z.y) + c;
    if (dot(z, z) > 500.0) {
      break;
    }
    idx = idx + 1;
  }
  return vec3(1.0 * idx / 42.0);
}

void main(void) {
  vec2 uv = gl_FragCoord.xy / iResolution.xy * 2.0 - 1.0;
  uv.y = uv.y * -iResolution.y / iResolution.x;
  vec2 pos = center + uv * range;
  gl_FragColor = vec4(mandelbrot_color(pos), 1.0);
}

Or using the library, you would write:

(setv mandelbrot `(setv z (+ (cSquare z) c)))
(print (hy2glsl (fragment-plane (color-ifs mandelbrot))))

Demo

In the example folder there is a loaded based on glumpy to test (and reload) shader module:

python setup.py develop --user
pip install --user glumpy
pushd example
./loader.hy mandelbrot.hy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hy2glsl-0.0.2.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

hy2glsl-0.0.2-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file hy2glsl-0.0.2.tar.gz.

File metadata

  • Download URL: hy2glsl-0.0.2.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for hy2glsl-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e4dcf18cc92be9225bf0d6ed05f14f89c0460a7fbc1c127e1fe01e4443e26671
MD5 1e3c350a2639e6b20183a2d16a012d8c
BLAKE2b-256 7d5f32b388d2a8110b70f8bc54ab3f06afb1cfdd7e786fc81356415cddd79036

See more details on using hashes here.

File details

Details for the file hy2glsl-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: hy2glsl-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for hy2glsl-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 153e3b7ac1d657d6648dc553d959ace678548479bd675371290138627f7cb375
MD5 32f797f70e731cd5df809584b21a85a6
BLAKE2b-256 482553d74b20dd78c56c8566a3cc11ed7eb8c7a8d06dc46a98a0746fa21afc3e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page