source: project/wiki/eggref/4/glls @ 33036

Last change on this file since 33036 was 33036, checked in by acharlton, 4 years ago

wiki/glls: clarify glsl-version parameter

File size: 30.1 KB
Line 
1
2== glls
3[[toc:]]
4glls (GL Lisp Shaders) lets you write [[https://www.opengl.org/documentation/glsl/|GLSL]] (OpenGL Shader Language) shaders in a convenient pseudo-scheme language in Chicken Scheme. The compilation into GLSL happens at compile-time for zero run-time cost. Run-time compilation and dynamic recompilation is also supported. To those that want to dynamically construct shaders: I solute you.
5
6In addition to the eponymous module, glls also provides the {{glls-render}} module. {{glls-render}} enhances glls to create automatic rendering functions for each pipeline. When compiled, these rendering functions are created in efficient C, although dynamic functions are also provided. See the section [[#automatic-render-functions|Automatic render functions]] for details.
7
8The idea for glls was hugely inspired by [[https://github.com/cbaggers/varjo|Varjo]]. Before learning about Varjo, I had never considered the possibility of writing shaders in anything but the GLSL. Seeing them being written in Lisp was a major, "Of course!" moment.
9
10That said, while this library bears some superficial resemblance to Varjo, the approach is quite different. While Varjo does a lot of work to validate the the lispy-glls expressions (including type checking), glls only performs cursory syntactic checking. The result of this is that one could probably write shaders in Varjo without knowing the GLSL and could be reasonably sure that those shaders would always compile to something that would mostly work. glls makes no such promises, so it is entirely possible to generate GLSL that won’t compile. Being able to understand GLSL code is therefore a prerequisite for successful shader debugging. The GLSL code output by glls is beautifully formatted, thanks to Alex Shinn’s amazing [[http://synthcode.com/scheme/fmt/|fmt]] library. fmt is responsible for far more than just the GLSL formatting, since it is basically a compiler of its own. The compilation portion of glsl is more or less a thin layer on top of fmt.
11
12glls should work on Linux, Mac OS X, Windows, and with OpenGL ES. glls will automatically compile with ES support on ARM hardware, or when {{gles}} is defined during compilation (e.g. {{chicken-install -D gles}}).
13
14
15=== Requirements
16* make
17* fmt
18* matchable
19* miscmacros
20* opengl-glew
21* srfi-42
22
23
24=== Documentation
25glls contains three modules: glls-render, glls, and glls-compiler. glls-render rexports glls and glls-compiler and is used when you want the functionality of glls plus the addition of [[#automatic-render-functions|automatically generated render functions]]. glls is the primary module, providing the main interface to shaders and pipelines, which also rexports glls-compiler. glls-compiler provides the functions used to compile shaders.
26
27<parameter> glsl-version</parameter>
28
29The default GLSL version used by shaders. Defaults to {{120}} on GL ES platforms, {{330}} otherwise. When compiling a file with a shader, this modifying this parameter will only take effect if you change it before the compilation phase. E.g.:
30
31<enscript highlight="scheme">    
32(use-for-syntax glls)
33(begin-for-syntax (glsl-version 300))
34</enscript>
35
36
37==== Shaders
38<record> (shader TYPE SOURCE INPUTS OUTPUTS UNIFORMS PROGRAM)</record>
39
40Used to represent shaders. Returned by {{define-shader}} and {{create-shader}}. It should not typically be necessary to access the slots of this record.
41
42<macro> (define-shader SHADER-NAME GLLS-SHADER)</macro>
43
44Defines a new {{shader}} named {{NAME}}. The (unquoted) form {{GLLS-SHADER}} should conform to language defined in the section [[#the-glls-shader-language|The glls shader language]]. Before shaders are used, they must be compiled by OpenGL with {{compile-shader}}.
45
46<procedure> (create-shader GLLS-SHADER )</procedure>
47
48Creates (at run-time) a new {{shader}}. The form {{GLLS-SHADER}} should conform to language defined in the section [[#the-glls-shader-language|The glls shader language]]. Before shaders are used, they must be compiled by OpenGL with {{compile-shader}}.
49
50<procedure> (compile-glls GLLS-SHADER)</procedure>
51
52Returns the source string for a shader. The form {{GLLS-SHADER}} should conform to language defined in the section [[#the-glls-shader-language|The glls shader language]].
53
54<procedure> (compile-shader SHADER)</procedure>
55
56Compile (in OpenGL) {{SHADER}}. Nothing is done if the shader has already been compiled. This typically does not need to be called, since {{compile-pipeline}} does so. Must be called while there is an active OpenGL context.
57
58
59==== Pipelines
60''Pipelines'' are the term that glsl uses to describe a collection of shaders that will be linked together. This is equivalent to a GL ''program'', just less ambiguously named.
61
62<record> (pipeline SHADERS ATTRIBUTES UNIFORMS PROGRAM)</record>
63
64Created with {{define-pipeline}} or {{create-pipeline}}, contains the data needed for a pipeline. {{SHADERS}} is the list of shader records. {{ATTRIBUTES}} and {{UNIFORMS}} are lists of the attributes and uniforms of the shader, specified as {{(name . type)}} pairs before compilation (with {{compile-pipeline}} or {{compile-pipelines}}) and {{(name location type)}} lists after compilation. {{PROGRAM}} is the GL ID of the program (always 0 before compilation).
65
66<macro> (define-pipeline PIPELINE-NAME . SHADERS)</macro>
67
68Defines a new {{pipeline}} named {{NAME}}. The {{SHADERS}} should either be forms conforming to language defined in the section [[#the-glls-shader-language|The glls shader language]], {{shader}}s defined by {{define-shader}}, or a mix of the two. Pipelines must have at least one vertex and one fragment shader to be able to compile. Before pipelines are used, they must be compiled by OpenGL with {{compile-pipeline}} or {{compile-pipelines}}.
69
70{{define-pipeline}} behaves differently when it is being evaluated ''and'' when a given pipeline is being redefined. In this case, the new pipeline inherits the GL program ID of the old one. Additionally, the pipeline is compiled by OpenGL right away (and as a consequence, so are any pipelines that are pending compilation). This is done so that pipelines can be edited and reevaluated in a REPL session and one’s scene will be updated as expected. See the [[https://github.com/AlexCharlton/glls/blob/master/examples/interactive.scm|interactive example]] for an example of how this can be accomplished.
71
72{{define-pipeline}} has additional effects when used with the {{glls-render}} module (see [[#automatic-render-functions|Automatic render functions]]).
73
74<procedure> (create-pipeline . SHADERS)</procedure>
75
76Creates (at run-time) a new {{pipeline}}. The {{SHADERS}} should either be forms conforming to language defined in the section [[#the-glls-shader-language|The glls shader language]], {{shader}}s, or a mix of the two. Pipelines must have at least one vertex and one fragment shader to be able to compile. Before pipelines are used, they must be compiled by OpenGL with {{compile-pipeline}} or {{compile-pipelines}}.
77
78<procedure> (compile-pipeline PIPELINE)</procedure>
79
80Compile (in OpenGL) the {{PIPELINE}} and sets its {{PROGRAM}} slot to the OpenGL program ID. If the pipeline’s {{PROGRAM}} slot is already set to a non-zero value, this ID will be reused for the new program. Compiles all of the pipeline’s shaders with {{compile-shader}}. Must be called while there is an active OpenGL context.
81
82<procedure> (compile-pipelines)</procedure>
83
84Compile (as per {{compile-pipeline}}) all the pipelines defined by {{define-pipeline}} and {{create-pipeline}}. Must be called while there is an active OpenGL context.
85
86<procedure> (pipeline-uniform UNIFORM PIPELINE)</procedure>
87
88Return the location of {{UNIFORM}}. The {{PIPELINE}} must be compiled before this function can be used.
89
90<procedure> (pipeline-attribute ATTRIBUTE PIPELINE)</procedure>
91
92Return the location of {{ATTRIBUTE}}. The {{PIPELINE}} must be compiled before this function can be used.
93
94<procedure> (pipeline-mesh-attributes PIPELINE)</procedure>
95
96Return a list of {{(ATTRIBUTE-NAME . LOCATION)}} pairs, suitable for passing to [[http://wiki.call-cc.org/eggref/4/gl-utils|gl-utils’]] {{mesh-make-vao!}}.
97
98
99==== The glls shader language
100
101===== Shader syntax
102The shaders of glls – the forms that {{define-shader}}, {{define-pipeline}}, etc. expect – have the following syntax:
103
104    (<type> [input: <inputs>] [uniform: <uniforms>] [output: <outputs>]
105            [version: <version>] [use: <imports>] [export: <exports])
106    <body> ...
107
108{{type}} is the keyword type of the shader. It must be one of {{#:vertex}}, {{#:fragment}}, {{#:geometry}}, {{#:tess-control}}, {{#:tess-evaluation}}, or {{#:compute}}.
109
110{{inputs}} is a list of the input variables for the shader. These are given in {{(name type)}} lists.
111
112{{uniforms}} is a list of the uniform variables for the shader. These are given in {{(name type)}} lists.
113
114{{outputs}} is a list of the output variables from the shader. These are given in {{(name type)}} lists.
115
116{{version}} is the integer version number of the shader, i.e. the number you would write at the top of the shader source (e.g. {{#version 410}}). Defaults to the {{glsl-version}} parameter.
117
118{{imports}} is the list of shaders that the current shader depends on. See the section [[#shaders-that-export|Shaders that export]] for more details. Defaults to {{()}}
119
120{{exports}} is the list of symbols that the current shader exports. See the section [[#shaders-that-export|Shaders that export]] for more details. Defaults to {{()}}
121
122{{body}} is the form representing the code of the shader. See the section [[#shader-lisp|Shader Lisp]] for an explanation of the kind of code that is expected.
123
124
125===== Shader Lisp
126For the most part, the Lisp used to define glls shaders looks like Scheme with one notable difference: types must be specified whenever a variable or function is defined. Under the hood, forms are being passed to [[https://wiki.call-cc.org/eggref/4/fmt#c-as-s-expressions|fmt]], so everything that you can do there will work in glls. Details of the Lisp used for shaders is provided in the following sections.
127
128It should be possible to do almost anything in glls that you would want to do with the GLSL. Known exceptions to this is are: layout qualifiers (which I don’t feel are terribly relevant in the context of Scheme, at least not until uniform locations become prevalent), do-while loops (which have no Scheme analog), and uniform blocks, {{#line}}, {{#undef}}, and struct uniforms all for no good reason. Let me know if there are any features that you find lacking.
129
130Keep in mind that glls cannot do anything that the GLSL can’t, such as making anonymous or recursive functions.
131
132
133====== Variables and naming
134Symbols in glls are transformed from a Scheme style into the C style used in the GLSL. Letters after dashes are uppercased (i.e., symbols become camelCased). Symbols prefixed by {{gl:}} in glls become prefixed by {{gl_}} in GLSL. {{#f}} and {{#t}} may be used instead of {{false}} and {{true}}.
135
136For programmer-defined variables this has little consequence. The importance of learning the renaming conventions comes when you want to call GLSL functions or variables. Examples of mappings between glls and GLSL names are: {{gl:position}} → {{gl_Position}}, {{float-bits-to-uint}} → {{floatBitsToUint}}, {{shadow-2d-proj-lod}} → {{shadow2DProjLod}}, and {{sampler-2d-ms-array}} → {{sampler2DMSArray}}. Two special cases are {{emit-vertex}} and {{end-primitive}} which are translated into the functions {{EmitVertex}} and {{EndPrimitive}} respectively (which, for some reason, go against the usual GLSL naming conventions).
137
138
139====== Types
140When defining variables or functions in glls, types must be supplied. Basic types (e.g. {{int}}, {{mat2x2}}) are given either as a symbol or keyword (e.g. {{int}}, {{#:mat2x2}}), whichever is preferred. Types with qualifiers (e.g. {{lowp float}}, {{out mediump vec2}}) are given as lists (e.g. {{(lowp float)}}, {{(out mediump vec2)}}).
141
142Arrays are specified as lists beginning with the keyword {{#:array}}. The next element in the list is the type, while the optional third element is the size. E.g. {{(#:array int 5)}}. When used with qualifiers, the array takes the place of the type, e.g. {{(highp (#:array float))}}.
143
144
145====== Functions
146GLSL functions and operators are all called like normal Lisp functions. In almost all cases the GLSL symbol (taking into account the renaming described in [[#variables-and-naming|Variables and naming]] can be used, while many operators can be called with their Scheme counterpart. The only operators that may not be used directly are {{|}}, {{||}}, {{|=}}, {{.}}, {{=}}, and array reference which must be called with their counterparts.
147
148The following is a mapping between glls aliases for GLSL functions and operators:
149
150* {{modulo}}: {{%}}
151* {{expt}}: {{pow}}
152* {{equal?}}, {{eqv?}}, {{eq}}, {{=}}: {{==}}
153* {{set!}}: {{=}}
154* {{and}}: {{&&}}
155* {{or}}: {{||}}
156* {{not}}: {{!}}
157* {{bitwise-and}}: {{&}}
158* {{bitwise-ior}}: {{|}}
159* {{bitwise-xor}}: {{^}}
160* {{bitwise-not}}: {{~}}
161* {{arithmetic-shift}}: {{<<}}
162* {{bitwise-and=}}: {{&=}}
163* {{bitwise-ior=}}: {{|=}}
164* {{bitwise-xor=}}: {{^=}}
165* {{arithmetic-shift=}}: {{<<=}}
166* {{++/post}}: Postfix {{++}}
167* {{--/post}}: Postfix {{--}}
168* {{field}}, {{..}}: {{.}} (struct field reference, e.g. {{(field point x)}} → {{point.x}})
169* {{swizzle}}, {{~~}}: {{.}} (vector swizzling, e.g. {{(swizzle color r g)}} → {{color.rg}})
170* {{array-ref}}, {{vector-ref}}: {{[]}} (array reference, e.g. {{(array-ref a 4)}} → {{a[4]}})
171* {{array-set!}}, {{vector-set!}}: {{[] =}} (array setting, e.g. {{(array-set! a 4 x)}} → {{a[4] = x}})
172* {{length}}: {{.length()}} (vector length, e.g. {{(length vec)}} → {{vec.length()}})
173
174
175====== Definition
176Variables, functions, and records (structs) are defined much like they are in Scheme, with the additional requirement of including types.
177
178    (define <name> <type> [<value>])
179
180Defines the variable {{name}}. When {{type}} is an array, a vector literal (eg. {{#(1 2 3)}}) may be used as the initial {{value}}.
181
182    (define (<name> [(<parameter> <type>) ...]) <return-type> <body> ...)
183
184Defines the function {{name}}. The last expression in the body of a non-void function is automatically returned. If {{body}} is omitted, a function prototype is created.
185
186    (let ((<name> <type> [<value>]) ...) <body> ...)
187
188Defines the supplied variables. When {{type}} is an array, a vector literal (eg. {{#(1 2 3)}}) may be used as the initial {{value}}. Note that, unlike Scheme, the variables created will continue to exist outside of the {{let}} (until the extent of whatever lexical scope the {{let}} exists within). In other words, {{let}} does not introduce scope. Note also that variables defined in {{let}} are within the scope of variables that are subsequently defined in the same {{let}} (i.e. {{let}} functions like {{let*}} in Scheme, and in fact {{let*}} may be used if preferred).
189
190    (define-record <name> (<type> <field>) ...)
191
192Defines the struct {{name}}.
193
194
195====== Control
196The following can be used with identical syntax to scheme:
197
198    (if <test> <true> [<false>])
199   
200    (cond (<test> <result> ...) ... (else <result>))
201   
202    (begin <body> ...)
203
204Keep in mind that they may only be used in the same place as their corresponding GLSL statements, with the exception of {{begin}}, which can only be used where it is possible to have multiple expressions.
205
206Additionally, the following forms can be used for switch/case statements in the GLSL fashion:
207
208    (c-switch <clause> ...)
209   
210    (c-case <values> <body> ...)
211   
212    (c-case/fallthrough <values> <body> ...)
213   
214    (c-default <body> ...)
215
216
217====== Iteration
218    (for <init> <condition> <update> <body> ...)
219
220GLSL style {{for}} loop.
221
222    (do-times (<var> [<start>] <end>) <body> ...)
223
224Equivalent to {{(for (define <var> #:int <start>) (< <var> <end>) (++ <var>) <body> ...)}}. {{start}} defaults to 0.
225
226    (while <condition> <body> ...)
227
228GLSL style {{while}} loop.
229
230
231====== Jumps
232All GLSL jumps ({{continue}}, {{break}}, {{return}}, {{discard}}) can be called like functions. Return may accept one argument. Keep in mind that the last expression in a non void function is automatically returned.
233
234
235====== Pre-processor
236The following forms can be used to add pre-processor directives:
237
238    (%define <name> [<value>])
239   
240    (%if <test> <true> [<false>])
241   
242    (%ifdef <value> <true> [<false>])
243   
244    (%ifndef <value> <true> [<false>])
245   
246    (%elif <value> <true> [<false>])
247   
248    (%else <fallback>)
249   
250    (%error <value> ...)
251   
252    (%pragma [#:stdgl] <name> <behaviour>)
253
254The optional {{#:stdgl}} keyword is used when a {{STDGL}} pragma is desired. {{<name>}} and {{<behaviour>}} will be formatted without modification as {{<name>(<behaviour>)}}.
255
256    (%extension <name> <behaviour>)
257
258{{<name>}} and {{<behaviour>}} will be formatted without modification as {{<name> : <behaviour>}}.
259
260
261==== Shaders that export
262It is often desirable to have shaders that contain generic, reusable functions. These shaders are linked into the pipeline (or program, in GLSL parlance) so that they can be accessed by other shaders. In order for another shader to reuse a function, it first has to (as in C) include a function prototype. glsl automates this process.
263
264glls lets you define shaders that export symbols through the use of the [[#shader-syntax|{{export}} keyword]]. These shaders can then be imported by others (through the [[#shader-syntax|{{use}} keyword]]). Prototypes are automatically generated for top-level functions or variables whose names match the symbols in the {{export}} keyword list. These prototypes are then inserted into shaders that {{use}} the exporting shader. Shaders that are {{use}}d by another are automatically linked into the resulting pipeline.
265
266Shaders that export should not have any inputs or outputs.
267
268See the example [[https://github.com/AlexCharlton/glls/blob/master/examples/exports.scm|exports.scm]] to see this in action.
269
270
271==== Automatic render functions
272By using the {{glls-render}} module, you can have glls automatically generate functions that will render an object with your glls pipeline. {{glls-render}} wraps {{define-pipeline}} so that it also defines a set of functions used for rendering and managing the ''renderable'' objects that are specific to that pipeline: one to create them, several to render them, and others to manipulate them. {{glls-render}} should not be used with the {{glls}} module: It reexports everything that you need from {{glls}}.
273
274Recalling {{define-pipeline}}:
275
276    (define-pipeline PIPELINE-NAME . SHADERS)
277
278There is one difference with {{glls-render}}’s {{define-pipeline}}: All shaders must include a list of the shader’s uniforms since the uniforms are the important information needed to derive rendering functions. This means that if you previously define some shaders (for example: {{my-vertex-shader}} and {{my-fragment-shader}}) and you wish to combine them in a pipeline, you ''must'' include the uniforms in the pipeline definition. This is done with a list that takes the form {{(SHADER uniform: [UNIFORM] ...)}}. This list must be present even if the shader does not use any uniforms. For example:
279
280    (define-pipeline my-pipeline
281      (my-vertex-shader uniform: mvp-matrix inverse-transpose-matrix)
282      (my-fragment-shader uniform:))
283
284Of course, if you are defining the shaders in the pipeline, then a separate list of uniforms is not necessary.
285
286
287===== Renderables
288{{glls-render}}’s {{define-piplelines}} defines a function for creating renderable objects:
289
290    (make-PIPELINE-NAME-renderable [vao: VAO] [mode: MODE] [n-elements: N-ELEMENTS] [element-type: ELEMENT-TYPE] [mesh: MESH] [offset: OFFSET] [data: DATA] + PIPELINE-SPECIFIC-KEYWORDS)
291
292Where {{PIPELINE-NAME}} is the name of the pipeline who’s renderables are being created.
293
294* {{VAO}} – A VAO such as those returned by [[http://api.call-cc.org/doc/opengl-glew/make-vao|opengl-glew’s {{make-vao}}]]. I.e.: A VAO that binds an array of attributes – for each element in the pipeline – as well as an element array.
295* {{MODE}} – The drawing mode to use when drawing the elements of the VAO. Must be mode that is accepted by (gl-utils’) [[http://api.call-cc.org/doc/gl-utils/mode-%3Egl|mode->gl]]. Defaults to {{#:triangles}}.
296* {{N-ELEMENTS}} – The number of elements (vertices) to draw.
297* {{ELEMENT-TYPE}} – The type of the values in the VAO’s element array. Must be one of {{#:unsigned-byte}}, {{#:unsigned-short}}, or {{#:unsigned-int}}. Not required if the VAO has no element array (i.e. {{render-arrays-PIPELINE-NAME}} is being used to render).
298* {{MESH}} – A [[http://wiki.call-cc.org/eggref/4/gl-utils|gl-utils]] mesh, provided in place of {{VAO}}, {{MODE}}, {{N-ELEMENTS}}, and {{ELEMENT-TYPE}}.
299* {{OFFSET}} – A byte offset to the location of the desired indices to draw.
300* {{DATA}} – An optional pointer to an appropriate glls renderable object. If not provided, a fresh renderable object will be created. [[https://github.com/AlexCharlton/glls/blob/master/gllsRender.h|gllsRenderable.h]] defines the structs used for renderables. Which struct is used for a given pipeline is chosen based on the number of uniforms present in the pipeline.
301
302See the [[https://www.opengl.org/sdk/docs/man/html/glDrawElements.xhtml|{{glDrawElements}} documentation]] for more information about these expected arguments.
303
304{{make-PIPELINE-NAME-renderable}} also expects one keyword argument for each uniform in the pipeline. These arguments should either be an f32vector, an s32vector, a u32vector, a pointer to the uniform data, or – in the case of a texture – a fixnum. Even if the uniform is a single value (e.g. a float), it must still be passed as a vector (or a pointer). This lets the value of the uniform be updated independently of the renderable.
305
306Additionally, there are a number of renderable setters for each of the keyword arguments accepted by {{make-PIPELINE-NAME-renderable}}:
307
308<procedure> (set-renderable-vao! RENDERABLE VAO)</procedure>
309<procedure> (set-renderable-n-elements! RENDERABLE N-ELEMENTS)</procedure>
310<procedure> (set-renderable-element-type! RENDERABLE TYPE)</procedure>
311<procedure> (set-renderable-mode! RENDERABLE MODE)</procedure>
312<procedure> (set-renderable-offset! RENDERABLE OFFSET)</procedure>
313   
314    (set-PIPELINE-NAME-renderable-UNIFORM-NAME! RENDERABLE UNIFORM-VALUE)
315
316These setters accept two arguments: a renderable and a value. The values correspond to those that {{make-PIPELINE-NAME-renderable}} accepts. For each uniform in the pipeline, a function named {{set-PIPELINE-NAME-renderable-UNIFORM-NAME!}} is created.
317
318<procedure> (renderable-size PIPELINE)</procedure>
319
320Returns the size, in bytes, of the memory needed for a renderable belonging to {{PIPELINE}}.
321
322
323===== Rendering renderables
324    (render-PIPELINE-NAME RENDERABLE)
325    (render-arrays-PIPELINE-NAME RENDERABLE)
326
327Where {{PIPELINE-NAME}} is the name of the pipeline who’s renderables are being rendered. {{render-PIPELINE-NAME}} and {{render-arrays-PIPELINE-NAME}} both render the given renderable.
328
329These render functions work differently depending on whether the {{define-pipeline}} has been compiled or interpreted (although the end results should look the same). When {{define-pipeline}} is compiled, the resulting render functions are compiled directly to efficient (non-branching) C. When {{define-pipeline}} is interpreted, the render functions call a generic rendering function that is not nearly as fast.
330
331The {{render-PIPELINE-NAME}} function draws the renderable using {{draw-elements}}. While this is typically the most efficient way to render a set of vertices, sometimes {{draw-arrays}} is preferable. {{render-arrays-PIPELINE-NAME}} functions identically except for calling {{draw-arrays}} instead of {{draw-elements}}.
332
333
334====== Fast render functions
335When compiled, the render function defined by {{define-pipeline}} is actually a combination of three “fast” render functions: a ''begin-render'' function, a ''render'' function, and an ''end-render'' function. The array rendering function is a similar combination: the ''begin-render'' function, an ''array-render'' function and the ''end-render'' function. This is done so that, if desired, all of the renderables that belong to the same pipeline may be rendered at the same time, without needing to perform expensive calls like program changes or texture binding more than once. To use these functions, call the ''begin-render'' function with the first renderable, then call the ''render'' (or ''array-render'') function on all renderables (including the first), finally calling the ''end-render'' function (with no arguments) to clean up.
336
337    (PIPELINE-NAME-fast-render-functions)
338
339{{define-pipeline}} does not define all of the render functions separately, but instead defines a single function with which to access them: {{PIPELINE-NAME-fast-render-functions}}, where {{PIPELINE-NAME}} is the name of the pipeline. This function returns eight values: the ''begin-render'' function, the ''render'' function, the ''end-render'' function, the ''array-render'' function, and pointers to those same C functions in that order.
340
341<parameter> unique-textures?</parameter>
342
343This parameter, defaulting to {{#t}}, controls where textures are bound in the fast render functions. When {{unique-textures?}} is {{#t}}, textures are bound in the main render function. When {{unique-textures?}} is {{#f}}, textures are bound in the begin render function. In other words: when {{unique-textures?}} is {{#f}}, it is assumed that that all of the renderables belonging to the same pipeline share a common “sprite sheet” (or other shared texture type). This parameter must be set for syntax (i.e. in a {{begin-for-syntax}} form) in order to have an effect.
344
345
346===== Utilities
347<macro> (export-pipeline . PIPELINES)</macro>
348
349Since glls-render causes {{define-pipeline}} to define multiple functions, this macro exports everything related to each pipeline in {{PIPELINES}}, except for the {{set-PIPELINE-NAME-renderable-UNIFORM!}} setters. These must be exported individually.
350
351
352=== Examples
353These examples depends on the [[http://wiki.call-cc.org/eggref/4/glfw3|glfw3]] egg for window and context creation. The examples presented here illustrate only very basic shader definition and loading. For more complete examples, see the [[https://github.com/AlexCharlton/glls/tree/master/examples|examples directory]] of the source.
354
355Aside from knowing how to write glls shaders, only one macro, one function, and one record is necessary to use glls: {{define-pipeline}}, {{compile-pipelines}}, and the record {{pipeline}}. This example illustrates this minimal pipeline creation
356
357<enscript highlight="scheme">    
358(import chicken scheme)
359
360(use glls (prefix glfw3 glfw:) (prefix opengl-glew gl:))
361
362(define-pipeline foo
363  ((#:vertex input: ((vertex #:vec2) (color #:vec3))
364             uniform: ((mvp #:mat4))
365             output: ((c #:vec3)))
366   (define (main) #:void
367     (set! gl:position (* mvp (vec4 vertex 0.0 1.0)))
368     (set! c color)))
369  ((#:fragment input: ((c #:vec3))
370               output: ((frag-color #:vec4)))
371   (define (main) #:void
372     (set! frag-color (vec4 c 1.0)))))
373
374(glfw:with-window (640 480 "Example" resizable: #f)
375   (gl:init)
376   (compile-pipelines)
377   (print foo)
378   (gl:use-program (pipeline-program foo)))
379</enscript>
380
381This example is similar to the first, but also illustrates the ability to define pipelines in different ways.
382
383<enscript highlight="scheme">    
384(import chicken scheme)
385
386(use glls (prefix glfw3 glfw:) (prefix opengl-glew gl:))
387
388(define-pipeline foo
389  ((#:vertex input: ((vertex #:vec2) (color #:vec3))
390                             uniform: ((mvp #:mat4))
391                             output: ((c #:vec3)))
392   (define (main) #:void
393     (set! gl:position (* mvp (vec4 vertex 0.0 1.0)))
394     (set! c color)))
395  ((#:fragment input: ((c #:vec3))
396               output: ((frag-color #:vec4)))
397   (define (main) #:void
398     (set! frag-color (vec4 c 1.0)))))
399
400(define-shader bar (#:vertex input: ((vertex #:vec2) (color #:vec3))
401                             uniform: ((mvp #:mat4))
402                             output: ((c #:vec3)))
403  (define (main) #:void
404    (set! gl:position (* mvp (vec4 vertex 0.0 1.0)))
405    (set! c color)))
406
407(define-pipeline baz
408  bar
409  (cadr (pipeline-shaders foo)))
410
411(glfw:with-window (640 480 "Example" resizable: #f)
412   (gl:init)
413   (compile-pipelines)
414   (print foo)
415   (print baz))
416</enscript>
417
418
419=== Version history
420
421==== Version 0.11.0
42223 January 2014
423
424* Overhaul compiler so new syntax can be introduced, and so existing syntax more closely maps to GLSL
425* Add array initialization
426* Move {{pragma}} and {{extension}} from keywords to forms
427
428
429==== Version 0.10.0
43025 December 2014
431
432* Export {{set-renderable-*!}} functions
433
434
435==== Version 0.9.0
43624 December 2014
437
438* Add {{unique-textures?}} parameter
439* {{export-pipeline}} exports primary setters
440* Better renderable setter error values
441
442
443==== Version 0.8.0
4448 December 2014
445
446* Add {{draw-array}} render functions
447* Support OpenGL ES
448* Bug fixes
449
450
451==== Version 0.7.0
45220 October 2014
453
454* Renderable element-type and mode setters take keywords instead of integers
455
456
457==== Version 0.6.0
45810 September 2014
459
460* Integrate with gl-utils’ meshes
461* Remove obsolete {{load-ply-renderable}}
462
463
464==== Version 0.5.2
46524 August 2014
466
467* Update to new gl-utils
468
469'''Version 0.5.1'''
470
471* Allow {{#t}}, {{#f}}
472* Fix swizzle
473
474'''Version 0.5.0'''
475
47614 August 2014
477
478* New shader syntax
479
480
481==== Version 0.4.1
48212 August 2014
483
484* Fix export name matching
485
486'''Version 0.4.0'''
487
48811 August 2014
489
490* Add shader exports
491* Add function prototyping
492* Add {{export-pipeline}}
493* Add aliases to {{field}}, {{swizzle}} ({{..}}, {{~~}})
494* Export {{renderable-size}} rather than {{allocate-renderable}}
495* Improve error messages
496* Bug fixes
497
498
499==== Version 0.3.3
5004 June 2014
501
502* Bug fixes relating to dynamic reevaluated pipelines
503
504'''Version 0.3.2'''
505
5063 June 2014
507
508* Update examples for GC safety
509
510'''Version 0.3.1'''
511
5122 June 2014
513
514* Fix {{load-ply-renderable}}
515* Fix gllsRender.h
516
517'''Version 0.3.0'''
518
51930 May 2014
520
521* Support dynamic reevaluation of pipelines
522
523
524==== Version 0.2.2
52529 May 2014
526
527* Add make as a dependency
528
529'''Version 0.2.1'''
530
531* Fix bug affecting dynamic use of glls-render
532
533'''Version 0.2.0'''
534
53528 May 2014
536
537* Automatic render function generation
538* Removed {{eval}} from {{defpipeline}} (which broke some things when used in modules)
539* Renamed {{defpipeline}}, {{defshader}}
540
541
542==== Version 0.1.0
543* Initial release
544
545
546=== Source repository
547Source available on [[https://github.com/AlexCharlton/glls|GitHub]].
548
549Bug reports and patches welcome! Bugs can be reported via GitHub or to alex.n.charlton at gmail.
550
551
552=== Author
553Alex Charlton
554
555
556=== Licence
557BSD
Note: See TracBrowser for help on using the repository browser.