diff options
Diffstat (limited to 'manuals/chickadee/Shaders.html')
-rw-r--r-- | manuals/chickadee/Shaders.html | 201 |
1 files changed, 163 insertions, 38 deletions
diff --git a/manuals/chickadee/Shaders.html b/manuals/chickadee/Shaders.html index 9d3e555..31fcbca 100644 --- a/manuals/chickadee/Shaders.html +++ b/manuals/chickadee/Shaders.html @@ -1,6 +1,6 @@ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> -<!-- Copyright (C) 2017, 2018, 2019 David Thompson davet@gnu.org +<!-- Copyright (C) 2017-2020 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 @@ -18,7 +18,7 @@ Foundation Web site at http://www.gnu.org/licenses/fdl.html. The document was typeset with http://www.texinfo.org/ (GNU Texinfo). --> -<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> +<!-- Created by GNU Texinfo 6.6, 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> @@ -32,29 +32,20 @@ http://www.texinfo.org/ (GNU Texinfo). <link href="Index.html#Index" rel="index" title="Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Graphics.html#Graphics" rel="up" title="Graphics"> -<link href="Scripting.html#Scripting" rel="next" title="Scripting"> +<link href="Audio.html#Audio" rel="next" title="Audio"> <link href="Buffers.html#Buffers" rel="prev" title="Buffers"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} -blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} -blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} -div.smalldisplay {margin-left: 3.2em} -div.smallexample {margin-left: 3.2em} -div.smalllisp {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} -pre.smalldisplay {font-family: inherit; font-size: smaller} -pre.smallexample {font-size: smaller} -pre.smallformat {font-family: inherit; font-size: smaller} -pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} @@ -89,14 +80,12 @@ ul.no-bullet {list-style: none} </head> <body lang="en"> -<a name="Shaders"></a> -<div class="header"> +<span id="Shaders"></span><div class="header"> <p> Previous: <a href="Buffers.html#Buffers" accesskey="p" rel="prev">Buffers</a>, Up: <a href="Graphics.html#Graphics" accesskey="u" rel="up">Graphics</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html#Index" title="Index" rel="index">Index</a>]</p> </div> <hr> -<a name="Shaders-1"></a> -<h4 class="subsection">2.3.12 Shaders</h4> +<span id="Shaders-1"></span><h4 class="subsection">2.3.14 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 @@ -165,92 +154,228 @@ recommend searching the web for more information and examples. What we can say, though, is how to use our API: </p> <dl> -<dt><a name="index-strings_002d_003eshader"></a>Procedure: <strong>strings->shader</strong> <em>vertex-source fragment-source</em></dt> +<dt id="index-strings_002d_003eshader">Procedure: <strong>strings->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><a name="index-load_002dshader"></a>Procedure: <strong>load-shader</strong> <em>vertex-source-file fragment-source-file</em></dt> +<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><a name="index-make_002dshader"></a>Procedure: <strong>make-shader</strong> <em>vertex-port fragment-port</em></dt> +<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><a name="index-shader_003f"></a>Procedure: <strong>shader?</strong> <em>obj</em></dt> +<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><a name="index-null_002dshader"></a>Variable: <strong>null-shader</strong></dt> +<dt id="index-null_002dshader">Variable: <strong>null-shader</strong></dt> <dd><p>Represents the absence shader program. </p></dd></dl> <dl> -<dt><a name="index-shader_002duniform"></a>Procedure: <strong>shader-uniform</strong> <em>shader name</em></dt> +<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><a name="index-shader_002duniforms"></a>Procedure: <strong>shader-uniforms</strong> <em>shader</em></dt> +<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><a name="index-shader_002dattributes"></a>Procedure: <strong>shader-attributes</strong> <em>shader</em></dt> +<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><a name="index-uniform_003f"></a>Procedure: <strong>uniform?</strong> <em>obj</em></dt> +<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">2.3.14.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">2.3.14.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><a name="index-uniform_002dname"></a>Procedure: <strong>uniform-name</strong> <em>uniform</em></dt> +<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><a name="index-uniform_002dtype"></a>Procedure: <strong>uniform-type</strong> <em>uniform</em></dt> +<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><a name="index-uniform_002dvalue"></a>Procedure: <strong>uniform-value</strong> <em>uniform</em></dt> +<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">2.3.14.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 DirectionalLight { + vec3 direction; + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +uniform DirectionalLight light; +</pre></div> + +<p>While <code>light</code> is declared as a single uniform in the shader code, +OpenGL translates this into <em>four</em> uniforms in this case: One +uniform each member of the <code>DirectionalLight</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>DirectionalLight</code> struct: +</p> +<div class="example"> +<pre class="example">(define-shader-type <directional-light> + make-directional-light + directional-light? + (float-vec3 direction directional-light-direction) + (float-vec3 ambient directional-light-ambient) + (float-vec3 diffuse directional-light-diffuse) + (float-vec3 specular directional-light-specular) + (float shininess directional-light-shininess)) +</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><a name="index-uniform_002ddefault_002dvalue"></a>Procedure: <strong>uniform-default-value</strong> <em>uniform</em></dt> -<dd><p>Return the default value of <var>uniform</var>. +<dt id="index-bool">Variable: <strong>bool</strong></dt> +<dd><p>Either <code>#t</code> or <code>#f</code>. </p></dd></dl> <dl> -<dt><a name="index-attribute_003f"></a>Procedure: <strong>attribute?</strong> <em>obj</em></dt> -<dd><p>Return <code>#t</code> if <var>obj</var> is an attribute. +<dt id="index-int">Variable: <strong>int</strong></dt> +<dd><p>An integer. </p></dd></dl> <dl> -<dt><a name="index-attribute_002dname"></a>Procedure: <strong>attribute-name</strong> <em>attribute</em></dt> -<dd><p>Return the variable name of <var>attribute</var>. +<dt id="index-unsigned_002dint">Variable: <strong>unsigned-int</strong></dt> +<dd><p>An unsigned integer. </p></dd></dl> <dl> -<dt><a name="index-attribute_002dlocation"></a>Procedure: <strong>attribute-location</strong> <em>attribute</em></dt> -<dd><p>Return the binding location of <var>attribute</var>. +<dt id="index-float">Variable: <strong>float</strong></dt> +<dd><p>A floating point number. </p></dd></dl> <dl> -<dt><a name="index-attribute_002dtype"></a>Procedure: <strong>attribute-type</strong> <em>attribute</em></dt> -<dd><p>Return the data type of <var>attribute</var>. +<dt id="index-float_002dvec2">Variable: <strong>float-vec2</strong></dt> +<dd><p>A 2D vector (see <a href="Vectors.html#Vectors">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">Vectors</a>.) +</p></dd></dl> + +<dl> +<dt id="index-float_002dvec4">Variable: <strong>float-vec4</strong></dt> +<dd><p>A color. +</p></dd></dl> + +<dl> +<dt id="index-mat4">Variable: <strong>mat4</strong></dt> +<dd><p>A matrix (see <a href="Matrices.html#Matrices">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">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><name> constructor predicate (field-type field-name [field-getter] [field-setter]) …</em></dt> +<dd> +<p>Define a new shader data type called <var><name></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><name></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> |