summaryrefslogtreecommitdiff
path: root/manuals/chickadee/Shaders.html
blob: 95c518647f9b6a3a63f67356063223e588a14e8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 2017-2021  David Thompson davet@gnu.org

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled "GNU
Free Documentation License".

A copy of the license is also available from the Free Software
Foundation Web site at http://www.gnu.org/licenses/fdl.html.


* Chickadee: (chickadee).     Game programming toolkit for Guile.

The document was typeset with
http://www.texinfo.org/ (GNU Texinfo).
 -->
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Shaders (The Chickadee Game Toolkit)</title>

<meta name="description" content="Shaders (The Chickadee Game Toolkit)">
<meta name="keywords" content="Shaders (The Chickadee Game Toolkit)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Index.html" rel="index" title="Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Graphics.html" rel="up" title="Graphics">
<link href="Framebuffers.html" rel="next" title="Framebuffers">
<link href="Buffers.html" rel="prev" title="Buffers">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
@media (min-width: 1140px) {
    body {
        margin-left: 14rem;
        margin-right: 4rem;
        max-width: 52rem;
    }
}

@media (min-width: 800px) and (max-width: 1140px) {
    body {
        margin-left: 6rem;
        margin-right: 4rem;
        max-width: 52rem;
    }
}

@media (max-width: 800px) {
    body {
        margin: 1rem;
    }
}

-->
</style>
<link rel="stylesheet" type="text/css" href="https://dthompson.us/css/dthompson.css">


</head>

<body lang="en">
<span id="Shaders"></span><div class="header">
<p>
Next: <a href="Framebuffers.html" accesskey="n" rel="next">Framebuffers</a>, Previous: <a href="Buffers.html" accesskey="p" rel="prev">Buffers</a>, Up: <a href="Graphics.html" accesskey="u" rel="up">Graphics</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Shaders-1"></span><h4 class="subsection">5.3.13 Shaders</h4>

<p>Shaders are programs that the GPU can evaluate that allow the
programmer to completely customized the final output of a GPU draw
call.  The <code>(chickadee graphics shader)</code> module provides an API for
building custom shaders.
</p>
<p>Shaders are written in the OpenGL Shading Language, or GLSL for short.
Chickadee aspires to provide a domain specific language for writing
shaders in Scheme, but we are not there yet.
</p>
<p>Shader programs consist of two components: A vertex shader and a
fragment shader.  A vertex shader receives vertex data (position
coordinates, texture coordinates, normals, etc.) and transforms them
as desired, whereas a fragment shader controls the color of each
pixel.
</p>
<p>Sample vertex shader:
</p>
<div class="example">
<pre class="verbatim">#version 130

in vec2 position;
in vec2 tex;
out vec2 fragTex;
uniform mat4 mvp;

void main(void) {
    fragTex = tex;
    gl_Position = mvp * vec4(position.xy, 0.0, 1.0);
}
</pre></div>

<p>Sample fragment shader:
</p>
<div class="example">
<pre class="verbatim">#version 130

in vec2 fragTex;
uniform sampler2D colorTexture;

void main (void) {
    gl_FragColor = texture2D(colorTexture, fragTex);
}
</pre></div>

<p>This manual will not cover GLSL features and syntax as there is lots
of information already available about this topic.
</p>
<p>One way to think about rendering with shaders, and the metaphor
Chickadee uses, is to think about it as a function call: The shader is
a function, and it is applied to some &ldquo;attributes&rdquo; (positional
arguments), and some &ldquo;uniforms&rdquo; (keyword arguments).
</p>
<div class="example">
<pre class="example">(define my-shader (load-shader &quot;vert.glsl&quot; &quot;frag.glsl&quot;))
(define vertices (make-vertex-array &hellip;))
(shader-apply my-shader vertices #:color red)
</pre></div>

<p>See <a href="Rendering-Engine.html">Rendering Engine</a> for more details about the <code>shader-apply</code>
procedure.
</p>
<p>Shaders are incredibly powerful tools, and there&rsquo;s more information
about them than we could ever fit into this manual, so we highly
recommend searching the web for more information and examples.  What
we can say, though, is how to use our API:
</p>
<dl>
<dt id="index-strings_002d_003eshader">Procedure: <strong>strings-&gt;shader</strong> <em>vertex-source fragment-source</em></dt>
<dd><p>Compile <var>vertex-source</var>, the GLSL code for the vertex shader, and
<var>fragment-source</var>, the GLSL code for the fragment shader, into a
GPU shader program.
</p></dd></dl>

<dl>
<dt id="index-load_002dshader">Procedure: <strong>load-shader</strong> <em>vertex-source-file fragment-source-file</em></dt>
<dd><p>Compile the GLSL source code within <var>vertex-source-file</var> and
<var>fragment-source-file</var> into a GPU shader program.
</p></dd></dl>

<dl>
<dt id="index-make_002dshader">Procedure: <strong>make-shader</strong> <em>vertex-port fragment-port</em></dt>
<dd><p>Read GLSL source from <var>vertex-port</var> and <var>fragment-port</var> and
compile them into a GPU shader program.
</p></dd></dl>

<dl>
<dt id="index-shader_003f">Procedure: <strong>shader?</strong> <em>obj</em></dt>
<dd><p>Return <code>#t</code> if <var>obj</var> is a shader.
</p></dd></dl>

<dl>
<dt id="index-null_002dshader">Variable: <strong>null-shader</strong></dt>
<dd><p>Represents the absence shader program.
</p></dd></dl>

<dl>
<dt id="index-shader_002duniform">Procedure: <strong>shader-uniform</strong> <em>shader name</em></dt>
<dd><p>Return the metadata for the uniform <var>name</var> in <var>shader</var>.
</p></dd></dl>

<dl>
<dt id="index-shader_002duniforms">Procedure: <strong>shader-uniforms</strong> <em>shader</em></dt>
<dd><p>Return a hash table of uniforms for <var>shader</var>.
</p></dd></dl>

<dl>
<dt id="index-shader_002dattributes">Procedure: <strong>shader-attributes</strong> <em>shader</em></dt>
<dd><p>Return a hash table of attributes for <var>shader</var>.
</p></dd></dl>

<dl>
<dt id="index-shader_002duniform_002dset_0021">Procedure: <strong>shader-uniform-set!</strong> <em>shader uniform value</em></dt>
</dl>

<span id="Attributes"></span><h4 class="subsubsection">5.3.13.1 Attributes</h4>

<dl>
<dt id="index-attribute_003f">Procedure: <strong>attribute?</strong> <em>obj</em></dt>
<dd><p>Return <code>#t</code> if <var>obj</var> is an attribute.
</p></dd></dl>

<dl>
<dt id="index-attribute_002dname">Procedure: <strong>attribute-name</strong> <em>attribute</em></dt>
<dd><p>Return the variable name of <var>attribute</var>.
</p></dd></dl>

<dl>
<dt id="index-attribute_002dlocation">Procedure: <strong>attribute-location</strong> <em>attribute</em></dt>
<dd><p>Return the binding location of <var>attribute</var>.
</p></dd></dl>

<dl>
<dt id="index-attribute_002dtype">Procedure: <strong>attribute-type</strong> <em>attribute</em></dt>
<dd><p>Return the data type of <var>attribute</var>.
</p></dd></dl>

<span id="Uniforms"></span><h4 class="subsubsection">5.3.13.2 Uniforms</h4>

<dl>
<dt id="index-uniform_003f">Procedure: <strong>uniform?</strong> <em>obj</em></dt>
<dd><p>Return <code>#t</code> if <var>obj</var> is a uniform.
</p></dd></dl>

<dl>
<dt id="index-uniform_002dname">Procedure: <strong>uniform-name</strong> <em>uniform</em></dt>
<dd><p>Return the variable name of <var>uniform</var>.
</p></dd></dl>

<dl>
<dt id="index-uniform_002dtype">Procedure: <strong>uniform-type</strong> <em>uniform</em></dt>
<dd><p>Return the data type of <var>uniform</var>.
</p></dd></dl>

<dl>
<dt id="index-uniform_002dvalue">Procedure: <strong>uniform-value</strong> <em>uniform</em></dt>
<dd><p>Return the current value of <var>uniform</var>.
</p></dd></dl>

<span id="User_002dDefined-Shader-Types"></span><h4 class="subsubsection">5.3.13.3 User-Defined Shader Types</h4>

<p>The shader examples in this manual thus far have only shown uniforms
defined using primitive types.  However, GLSL shaders support
user-defined compound structs, such as this one:
</p>
<div class="example">
<pre class="verbatim">struct Light {
  bool enabled;
  int type;
  vec3 position;
  vec3 direction;
  vec4 color;
  float intensity;
  float cutOff;
};

uniform Light light;
</pre></div>

<p>While <code>light</code> is declared as a single uniform in the shader code,
OpenGL translates this into <em>seven</em> uniforms in this case: One
uniform each member of the <code>Light</code> struct.  This poses a problem
for sending Scheme data to the GPU.  How can compound Scheme data
translate into compound uniform data on the GPU?  The answer is with
shader types.  Shader types are a special kind of Guile struct that
provide a one-to-one mapping between a Scheme data structure and a
shader struct.
</p>
<p>Some example code will explain this concept best.  Here is the Scheme
equivalent of the <code>Light</code> struct:
</p>
<div class="example">
<pre class="example">(define-shader-type &lt;light&gt;
  make-light
  light?
  (bool enabled light-enabled?)
  (int type light-type)
  (float-vec3 position light-position)
  (float-vec3 direction light-direction)
  (float-vec4 color light-color)
  (float intensity light-intensity)
  (float cut-off light-cut-off))
</pre></div>

<p>The macro <code>define-shader-type</code> closely resembles the familiar
<code>define-record-type</code> from SRFI-9, but with one notable
difference: Each struct field contains type information.  The type
must be one of several primitive types (documented below) or another
shader type in the case of a nested structure.
</p>
<p>It is important to note that the names of the shader type fields
<em>must</em> match the names of the struct members in the GLSL code,
otherwise Chickadee will be unable to perform the proper translation.
</p>
<p>As of this writing, this interface is new and experimental.  It
remains to be seen if this model is robust enough for all use-cases.
</p>
<p>Primitive data types:
</p>
<dl>
<dt id="index-bool">Variable: <strong>bool</strong></dt>
<dd><p>Either <code>#t</code> or <code>#f</code>.
</p></dd></dl>

<dl>
<dt id="index-int">Variable: <strong>int</strong></dt>
<dd><p>An integer.
</p></dd></dl>

<dl>
<dt id="index-unsigned_002dint">Variable: <strong>unsigned-int</strong></dt>
<dd><p>An unsigned integer.
</p></dd></dl>

<dl>
<dt id="index-float">Variable: <strong>float</strong></dt>
<dd><p>A floating point number.
</p></dd></dl>

<dl>
<dt id="index-float_002dvec2">Variable: <strong>float-vec2</strong></dt>
<dd><p>A 2D vector (see <a href="Vectors.html">Vectors</a>.)
</p></dd></dl>

<dl>
<dt id="index-float_002dvec3">Variable: <strong>float-vec3</strong></dt>
<dd><p>A 3D vector (see <a href="Vectors.html">Vectors</a>.)
</p></dd></dl>

<dl>
<dt id="index-float_002dvec4">Variable: <strong>float-vec4</strong></dt>
<dd><p>A color (see <a href="Colors.html">Colors</a>) or rectangle (see <a href="Rectangles.html">Rectangles</a>.)
</p></dd></dl>

<dl>
<dt id="index-mat3">Variable: <strong>mat3</strong></dt>
<dd><p>A 3x3 matrix (see <a href="Matrices.html">Matrices</a>.)
</p></dd></dl>

<dl>
<dt id="index-mat4">Variable: <strong>mat4</strong></dt>
<dd><p>A 4x4 matrix (see <a href="Matrices.html">Matrices</a>.)
</p></dd></dl>

<dl>
<dt id="index-sampler_002d2d">Variable: <strong>sampler-2d</strong></dt>
<dd><p>A texture (see <a href="Textures.html">Textures</a>.)
</p></dd></dl>

<dl>
<dt id="index-sampler_002dcube">Variable: <strong>sampler-cube</strong></dt>
<dd><p>A cube map (see <a href="Textures.html">Textures</a>.)
</p></dd></dl>

<dl>
<dt id="index-local_002dfield">Variable: <strong>local-field</strong></dt>
<dd><p>A special type that means that the data is for the client-side
(Scheme-side) only and should not be sent to the GPU.  Any object may
be stored in a local field.
</p></dd></dl>

<dl>
<dt id="index-define_002dshader_002dtype">Syntax: <strong>define-shader-type</strong> <em>&lt;name&gt; constructor predicate                 (field-type field-name [field-getter] [field-setter]) &hellip;</em></dt>
<dd>
<p>Define a new shader data type called <var>&lt;name&gt;</var>.
</p>
<p>Instances of this data type are created by calling the
<var>constructor</var> procedure.  This procedure maps each field to a
keyword argument.  A shader data type with the fields <code>foo</code>,
<code>bar</code>, and <code>baz</code> would have a constructor that accepts the
keyword arguments <code>#:foo</code>, <code>#:bar</code>, and <code>#:baz</code>.
</p>
<p>A procedure named <var>predicate</var> will test if an object is a
<var>&lt;name&gt;</var> shader data type.
</p>
<p>Fields follow the format <code>(field-type field-name [field-getter]
[field-setter])</code>.  <var>field-type</var> and <var>field-name</var> are required
for each field, but <var>field-getter</var> and <var>field-setter</var> are
optional.
</p>
</dd></dl>

<dl>
<dt id="index-shader_002ddata_002dtype_003f">Procedure: <strong>shader-data-type?</strong> <em>obj</em></dt>
<dd><p>Return <code>#t</code> if <var>obj</var> is a shader data type object.
</p></dd></dl>

<hr>
<div class="header">
<p>
Next: <a href="Framebuffers.html" accesskey="n" rel="next">Framebuffers</a>, Previous: <a href="Buffers.html" accesskey="p" rel="prev">Buffers</a>, Up: <a href="Graphics.html" accesskey="u" rel="up">Graphics</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>