386 lines
17 KiB
HTML
386 lines
17 KiB
HTML
<!DOCTYPE HTML>
|
|
<html lang="en" class="light sidebar-visible" dir="ltr">
|
|
<head>
|
|
<!-- Book generated using mdBook -->
|
|
<meta charset="UTF-8">
|
|
<title>filamat - Filament</title>
|
|
|
|
|
|
<!-- Custom HTML head -->
|
|
|
|
<meta name="description" content="">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="theme-color" content="#ffffff">
|
|
|
|
<link rel="shortcut icon" href="../favicon.png">
|
|
<link rel="stylesheet" href="../css/variables.css">
|
|
<link rel="stylesheet" href="../css/general.css">
|
|
<link rel="stylesheet" href="../css/chrome.css">
|
|
|
|
<!-- Fonts -->
|
|
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
|
|
<link rel="stylesheet" href="../fonts/fonts.css">
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
<link rel="stylesheet" href="../highlight.css">
|
|
<link rel="stylesheet" href="../tomorrow-night.css">
|
|
<link rel="stylesheet" href="../ayu-highlight.css">
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
|
<!-- MathJax -->
|
|
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
|
|
|
<!-- Provide site root to javascript -->
|
|
<script>
|
|
var path_to_root = "../";
|
|
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "light" : "light";
|
|
</script>
|
|
<!-- Start loading toc.js asap -->
|
|
<script src="../toc.js"></script>
|
|
</head>
|
|
<body>
|
|
<div id="body-container">
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
<script>
|
|
try {
|
|
var theme = localStorage.getItem('mdbook-theme');
|
|
var sidebar = localStorage.getItem('mdbook-sidebar');
|
|
|
|
if (theme.startsWith('"') && theme.endsWith('"')) {
|
|
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
|
}
|
|
|
|
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
|
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
|
}
|
|
} catch (e) { }
|
|
</script>
|
|
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
<script>
|
|
var theme;
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
const html = document.documentElement;
|
|
html.classList.remove('light')
|
|
html.classList.add(theme);
|
|
html.classList.add("js");
|
|
</script>
|
|
|
|
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
<script>
|
|
var sidebar = null;
|
|
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
|
|
if (document.body.clientWidth >= 1080) {
|
|
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
|
sidebar = sidebar || 'visible';
|
|
} else {
|
|
sidebar = 'hidden';
|
|
}
|
|
sidebar_toggle.checked = sidebar === 'visible';
|
|
html.classList.remove('sidebar-visible');
|
|
html.classList.add("sidebar-" + sidebar);
|
|
</script>
|
|
|
|
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
|
<div style="display:flex;align-items:center;justify-content:center">
|
|
<img class="flogo" src="../images/filament_logo_small.png"></img>
|
|
</div>
|
|
<!-- populated by js -->
|
|
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
|
|
<noscript>
|
|
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
|
|
</noscript>
|
|
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
|
|
<div class="sidebar-resize-indicator"></div>
|
|
</div>
|
|
</nav>
|
|
|
|
<div id="page-wrapper" class="page-wrapper">
|
|
|
|
<div class="page">
|
|
<div id="menu-bar-hover-placeholder"></div>
|
|
<div id="menu-bar" class="menu-bar sticky">
|
|
<div class="left-buttons">
|
|
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
|
<i class="fa fa-bars"></i>
|
|
</label>
|
|
<!-- Filament: disable themes because the markdeep part does not look good for dark themes -->
|
|
<!--
|
|
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
|
<i class="fa fa-paint-brush"></i>
|
|
</button>
|
|
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
|
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
|
</ul>
|
|
-->
|
|
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
|
<i class="fa fa-search"></i>
|
|
</button>
|
|
</div>
|
|
|
|
<h1 class="menu-title">Filament</h1>
|
|
|
|
<div class="right-buttons">
|
|
<a href="https://github.com/google/filament" title="Git repository" aria-label="Git repository">
|
|
<i id="git-repository-button" class="fa fa-github"></i>
|
|
</a>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div id="search-wrapper" class="hidden">
|
|
<form id="searchbar-outer" class="searchbar-outer">
|
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
</form>
|
|
<div id="searchresults-outer" class="searchresults-outer hidden">
|
|
<div id="searchresults-header" class="searchresults-header"></div>
|
|
<ul id="searchresults">
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
<script>
|
|
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
});
|
|
</script>
|
|
|
|
<div id="content" class="content">
|
|
<main>
|
|
<h1 id="filamat"><a class="header" href="#filamat">Filamat</a></h1>
|
|
<p>Filamat allows for generating materials programatically on the device as opposed to with the <code>matc</code>
|
|
tool on the host machine. The cost is a binary size increase of your app due to the relatively
|
|
larger size of the <code>filamat</code> library.</p>
|
|
<p>For a smaller-sized library, see <a href="#filamat-lite"><code>filamat_lite</code></a>. It has no dependencies on
|
|
<code>glslang</code>, but can only compile materials for OpenGL and does no shader code optimization.</p>
|
|
<p>The filamat package is included in the releases available on
|
|
<a href="https://github.com/google/filament/releases">GitHub</a>.</p>
|
|
<h2 id="libraries"><a class="header" href="#libraries">Libraries</a></h2>
|
|
<p>Filamat is distributed as a set of static libraries you must link against:</p>
|
|
<ul>
|
|
<li><code>filamat</code>, Filamat main library</li>
|
|
<li><code>filabridge</code>, Support library for Filament / Filamat</li>
|
|
<li><code>shaders</code>, Shader text for material generation</li>
|
|
<li><code>utils</code>, Support library for Filament / Filamat</li>
|
|
<li><code>smol-v</code>, SPIR-V compression library</li>
|
|
</ul>
|
|
<p>To use Filamat from Java you must use the following two libraries instead:</p>
|
|
<ul>
|
|
<li><code>filamat-java.jar</code>, Contains Filamat's Java classes</li>
|
|
<li><code>filamat-jni</code>, Filamat's JNI bindings</li>
|
|
</ul>
|
|
<h2 id="linking-against-filamat"><a class="header" href="#linking-against-filamat">Linking against Filamat</a></h2>
|
|
<p>This walkthrough will get you successfully compiling and linking native code against Filamat with
|
|
minimum dependencies.</p>
|
|
<p>To start, download Filament's <a href="https://github.com/google/filament/releases">latest binary release</a>
|
|
and extract into a directory of your choosing. Binary releases are suffixed with the platform name,
|
|
for example, <code>filament-20181009-linux.tgz</code>.</p>
|
|
<p>Create a file, <code>main.cpp</code>, in the same directory with the following contents:</p>
|
|
<pre><code class="language-c++">#include <filamat/MaterialBuilder.h>
|
|
|
|
#include <iostream>
|
|
|
|
using namespace filamat;
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
// Must be called before any materials can be built.
|
|
MaterialBuilder::init();
|
|
|
|
MaterialBuilder builder;
|
|
builder
|
|
.name("My material")
|
|
.material("void material (inout MaterialInputs material) {"
|
|
" prepareMaterial(material);"
|
|
" material.baseColor.rgb = float3(1.0, 0.0, 0.0);"
|
|
"}")
|
|
.shading(MaterialBuilder::Shading::LIT)
|
|
.targetApi(MaterialBuilder::TargetApi::ALL)
|
|
.platform(MaterialBuilder::Platform::ALL);
|
|
|
|
Package package = builder.build();
|
|
if (package.isValid()) {
|
|
std::cout << "Success!" << std::endl;
|
|
}
|
|
|
|
// Call when finished building all materials to release internal MaterialBuilder resources.
|
|
MaterialBuilder::shutdown();
|
|
return 0;
|
|
}
|
|
</code></pre>
|
|
<p>The directory should look like:</p>
|
|
<pre><code>|-- README.md
|
|
|-- bin
|
|
|-- docs
|
|
|-- include
|
|
|-- lib
|
|
|-- main.cpp
|
|
</code></pre>
|
|
<p>We'll use a platform-specific Makefile to compile and link <code>main.cpp</code> with Filamat's libraries.
|
|
Copy your platform's Makefile below into a <code>Makefile</code> inside the same directory.</p>
|
|
<h3 id="linux"><a class="header" href="#linux">Linux</a></h3>
|
|
<pre><code class="language-make">FILAMENT_LIBS=-lfilamat -lfilabridge -lshaders -lutils -lsmol-v
|
|
CC=clang++
|
|
|
|
main: main.o
|
|
$(CC) -Llib/x86_64/ -stdlib=libc++ main.o $(FILAMENT_LIBS) -lpthread -ldl -o main
|
|
|
|
main.o: main.cpp
|
|
$(CC) -Iinclude/ -std=c++20 -stdlib=libc++ -pthread -c main.cpp
|
|
|
|
clean:
|
|
rm -f main main.o
|
|
|
|
.PHONY: clean
|
|
</code></pre>
|
|
<h3 id="macos"><a class="header" href="#macos">macOS</a></h3>
|
|
<pre><code class="language-make">FILAMENT_LIBS=-lfilamat -lfilabridge -lshaders -lutils -lsmol-v
|
|
CC=clang++
|
|
|
|
main: main.o
|
|
$(CC) -Llib/x86_64/ main.o $(FILAMENT_LIBS) -o main
|
|
|
|
main.o: main.cpp
|
|
$(CC) -Iinclude/ -std=c++20 -c main.cpp
|
|
|
|
clean:
|
|
rm -f main main.o
|
|
|
|
.PHONY: clean
|
|
</code></pre>
|
|
<h3 id="windows"><a class="header" href="#windows">Windows</a></h3>
|
|
<p>Note that the static libraries distributed for Windows include several
|
|
variants: mt, md, mtd, mdd. These correspond to the <a href="https://docs.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library?view=vs-2017">run-time library
|
|
flags</a>
|
|
<code>/MT</code>, <code>/MD</code>, <code>/MTd</code>, and <code>/MDd</code>, respectively. Here we use the mt variant.</p>
|
|
<p>When building Filamat from source, the <code>USE_STATIC_CRT</code> CMake option can be
|
|
used to change the run-time library version.</p>
|
|
<pre><code class="language-make">FILAMENT_LIBS=lib/x86_64/mt/filamat.lib lib/x86_64/mt/filabridge.lib lib/x86_64/mt/shaders.lib \
|
|
lib/x86_64/mt/utils.lib lib/x86_64/mt/smol-v.lib
|
|
CC=clang-cl.exe
|
|
|
|
main.exe: main.obj
|
|
$(CC) main.obj $(FILAMENT_LIBS) gdi32.lib user32.lib opengl32.lib
|
|
|
|
main.obj: main.cpp
|
|
$(CC) /MT /Iinclude/ /std:c++20 /c main.cpp
|
|
|
|
clean:
|
|
del main.exe main.obj
|
|
|
|
.PHONY: clean
|
|
</code></pre>
|
|
<h2 id="compiling"><a class="header" href="#compiling">Compiling</a></h2>
|
|
<p>You should be able to invoke <code>make</code> and run the executable successfully:</p>
|
|
<pre><code>$ make
|
|
$ ./main
|
|
Success!
|
|
</code></pre>
|
|
<p>On Windows, you'll need to open up a Visual Studio Native Tools Command Prompt
|
|
and invoke <code>nmake</code> instead of <code>make</code>.</p>
|
|
<h2 id="using-the-material-with-filament"><a class="header" href="#using-the-material-with-filament">Using the Material with Filament</a></h2>
|
|
<p>For simplicity, this demo doesn't do anything useful with the built material package. To use the
|
|
material with Filament, pass the material package's data into a Filament Material builder:</p>
|
|
<pre><code class="language-c++"> Package package = builder.build();
|
|
filament::Material* myMaterial = Material::Builder()
|
|
.package(package.getData(), package.getSize())
|
|
.build(*engine);
|
|
</code></pre>
|
|
<p>Note that this will require <a href="../../filament/README.html">linking against Filament's libraries</a> in
|
|
addition to Filamat's.</p>
|
|
<h2 id="filamat-lite"><a class="header" href="#filamat-lite">Filamat Lite</a></h2>
|
|
<p>The <code>filamat_lite</code> library is interchangeable with <code>filamat</code>, with a few caveats:</p>
|
|
<ol>
|
|
<li>Material compilation is only supported for the OpenGL backend.</li>
|
|
<li>No shader-level optimization is performed.</li>
|
|
<li>GLSL correctness is not checked.</li>
|
|
</ol>
|
|
<p>In addition, <code>filamat_lite</code> only performs a simple text match to determine which properties on the
|
|
<code>MaterialInputs</code> structure are set. The <code>material</code> input variable must also always be refered to by
|
|
the name <code>material</code>.</p>
|
|
<pre><code class="language-glsl">void anotherFunction(inout MaterialInputs m) {
|
|
// Incorrect! The MaterialInputs is being referred to by the name "m".
|
|
m.metallic = 0.0;
|
|
}
|
|
|
|
void aFunction(inout MaterialInputs material) {
|
|
// Works, but only because the variable name "material" is used.
|
|
material.reflectance = 0.5;
|
|
}
|
|
|
|
// The MaterialInputs variable must be named material.
|
|
void material(inout MaterialInputs material) {
|
|
prepareMaterial(material);
|
|
|
|
// Good.
|
|
material.roughness = materialParams.roughness;
|
|
material.baseColor.rgb = vec3(1.0, 0.0, 1.0);
|
|
|
|
aFunction(material);
|
|
anotherFunction(material);
|
|
}
|
|
</code></pre>
|
|
|
|
</main>
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
<!-- Mobile navigation buttons -->
|
|
<a rel="prev" href="../dup/fgviewer.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
<a rel="next prefetch" href="../dup/gltfio.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
<div style="clear: both"></div>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
|
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
<a rel="prev" href="../dup/fgviewer.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
<a rel="next prefetch" href="../dup/gltfio.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
</nav>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
window.playground_copyable = true;
|
|
</script>
|
|
|
|
|
|
<script src="../elasticlunr.min.js"></script>
|
|
<script src="../mark.min.js"></script>
|
|
<script src="../searcher.js"></script>
|
|
|
|
<script src="../clipboard.min.js"></script>
|
|
<script src="../highlight.js"></script>
|
|
<script src="../book.js"></script>
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
|
|
|
</div>
|
|
</body>
|
|
</html>
|