| 1 |
/* |
| 2 |
* Copyright (C) 2017 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
//! \file |
| 18 |
|
| 19 |
#ifndef TNT_FILAMAT_MATERIAL_PACKAGE_BUILDER_H |
| 20 |
#define TNT_FILAMAT_MATERIAL_PACKAGE_BUILDER_H |
| 21 |
|
| 22 |
#include <cstddef> |
| 23 |
#include <cstdint> |
| 24 |
|
| 25 |
#include <atomic> |
| 26 |
#include <string> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include <backend/DriverEnums.h> |
| 30 |
#include <backend/TargetBufferInfo.h> |
| 31 |
#include <filament/MaterialEnums.h> |
| 32 |
|
| 33 |
#include <filamat/IncludeCallback.h> |
| 34 |
#include <filamat/Package.h> |
| 35 |
|
| 36 |
#include <utils/BitmaskEnum.h> |
| 37 |
#include <utils/bitset.h> |
| 38 |
#include <utils/compiler.h> |
| 39 |
#include <utils/CString.h> |
| 40 |
|
| 41 |
namespace utils { |
| 42 |
class JobSystem; |
| 43 |
} |
| 44 |
|
| 45 |
namespace filamat { |
| 46 |
|
| 47 |
struct MaterialInfo; |
| 48 |
class ChunkContainer; |
| 49 |
struct Variant; |
| 50 |
|
| 51 |
class UTILS_PUBLIC MaterialBuilderBase { |
| 52 |
public: |
| 53 |
/** |
| 54 |
* High-level hint that works in concert with TargetApi to determine the shader models (used to |
| 55 |
* generate GLSL) and final output representations (spirv and/or text). |
| 56 |
*/ |
| 57 |
enum class Platform { |
| 58 |
DESKTOP, |
| 59 |
MOBILE, |
| 60 |
ALL |
| 61 |
}; |
| 62 |
|
| 63 |
enum class TargetApi : uint8_t { |
| 64 |
OPENGL = 0x01u, |
| 65 |
VULKAN = 0x02u, |
| 66 |
METAL = 0x04u, |
| 67 |
ALL = OPENGL | VULKAN | METAL |
| 68 |
}; |
| 69 |
|
| 70 |
enum class TargetLanguage { |
| 71 |
GLSL, // GLSL with OpenGL semantics |
| 72 |
SPIRV // GLSL with Vulkan semantics |
| 73 |
}; |
| 74 |
|
| 75 |
enum class Optimization { |
| 76 |
NONE, |
| 77 |
PREPROCESSOR, |
| 78 |
SIZE, |
| 79 |
PERFORMANCE |
| 80 |
}; |
| 81 |
|
| 82 |
/** |
| 83 |
* Initialize MaterialBuilder. |
| 84 |
* |
| 85 |
* init must be called first before building any materials. |
| 86 |
*/ |
| 87 |
static void init(); |
| 88 |
|
| 89 |
/** |
| 90 |
* Release internal MaterialBuilder resources. |
| 91 |
* |
| 92 |
* Call shutdown when finished building materials to release all internal resources. After |
| 93 |
* calling shutdown, another call to MaterialBuilder::init must precede another material build. |
| 94 |
*/ |
| 95 |
static void shutdown(); |
| 96 |
|
| 97 |
protected: |
| 98 |
// Looks at platform and target API, then decides on shader models and output formats. |
| 99 |
void prepare(bool vulkanSemantics); |
| 100 |
|
| 101 |
using ShaderModel = filament::backend::ShaderModel; |
| 102 |
Platform mPlatform = Platform::DESKTOP; |
| 103 |
TargetApi mTargetApi = (TargetApi) 0; |
| 104 |
Optimization mOptimization = Optimization::PERFORMANCE; |
| 105 |
bool mPrintShaders = false; |
| 106 |
bool mGenerateDebugInfo = false; |
| 107 |
utils::bitset32 mShaderModels; |
| 108 |
struct CodeGenParams { |
| 109 |
int shaderModel; |
| 110 |
TargetApi targetApi; |
| 111 |
TargetLanguage targetLanguage; |
| 112 |
}; |
| 113 |
std::vector<CodeGenParams> mCodeGenPermutations; |
| 114 |
// For finding properties and running semantic analysis, we always use the same code gen |
| 115 |
// permutation. This is the first permutation generated with default arguments passed to matc. |
| 116 |
const CodeGenParams mSemanticCodeGenParams = { |
| 117 |
.shaderModel = (int) ShaderModel::GL_ES_30, |
| 118 |
.targetApi = TargetApi::OPENGL, |
| 119 |
.targetLanguage = TargetLanguage::SPIRV |
| 120 |
}; |
| 121 |
uint8_t mVariantFilter = 0; |
| 122 |
|
| 123 |
// Keeps track of how many times MaterialBuilder::init() has been called without a call to |
| 124 |
// MaterialBuilder::shutdown(). Internally, glslang does something similar. We keep track for |
| 125 |
// ourselves so we can inform the user if MaterialBuilder::init() hasn't been called before |
| 126 |
// attempting to build a material. |
| 127 |
static std::atomic<int> materialBuilderClients; |
| 128 |
}; |
| 129 |
|
| 130 |
// Utility function that looks at an Engine backend to determine TargetApi |
| 131 |
inline constexpr MaterialBuilderBase::TargetApi targetApiFromBackend( |
| 132 |
filament::backend::Backend backend) noexcept { |
| 133 |
using filament::backend::Backend; |
| 134 |
using TargetApi = MaterialBuilderBase::TargetApi; |
| 135 |
switch (backend) { |
| 136 |
case Backend::DEFAULT: return TargetApi::ALL; |
| 137 |
case Backend::OPENGL: return TargetApi::OPENGL; |
| 138 |
case Backend::VULKAN: return TargetApi::VULKAN; |
| 139 |
case Backend::METAL: return TargetApi::METAL; |
| 140 |
case Backend::NOOP: return TargetApi::OPENGL; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* MaterialBuilder builds Filament materials from shader code. |
| 146 |
* |
| 147 |
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 148 |
* #include <filamat/MaterialBuilder.h> |
| 149 |
* using namespace filamat; |
| 150 |
* |
| 151 |
* // Must be called before any materials can be built. |
| 152 |
* MaterialBuilder::init(); |
| 153 |
|
| 154 |
* MaterialBuilder builder; |
| 155 |
* builder |
| 156 |
* .name("My material") |
| 157 |
* .material("void material (inout MaterialInputs material) {" |
| 158 |
* " prepareMaterial(material);" |
| 159 |
* " material.baseColor.rgb = float3(1.0, 0.0, 0.0);" |
| 160 |
* "}") |
| 161 |
* .shading(MaterialBuilder::Shading::LIT) |
| 162 |
* .targetApi(MaterialBuilder::TargetApi::ALL) |
| 163 |
* .platform(MaterialBuilder::Platform::ALL); |
| 164 |
|
| 165 |
* Package package = builder.build(); |
| 166 |
* if (package.isValid()) { |
| 167 |
* // success! |
| 168 |
* } |
| 169 |
|
| 170 |
* // Call when finished building all materials to release internal |
| 171 |
* // MaterialBuilder resources. |
| 172 |
* MaterialBuilder::shutdown(); |
| 173 |
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 174 |
* |
| 175 |
* @see filament::Material |
| 176 |
*/ |
| 177 |
class UTILS_PUBLIC MaterialBuilder : public MaterialBuilderBase { |
| 178 |
public: |
| 179 |
MaterialBuilder(); |
| 180 |
|
| 181 |
static constexpr size_t MATERIAL_VARIABLES_COUNT = 4; |
| 182 |
enum class Variable : uint8_t { |
| 183 |
CUSTOM0, |
| 184 |
CUSTOM1, |
| 185 |
CUSTOM2, |
| 186 |
CUSTOM3 |
| 187 |
// when adding more variables, make sure to update MATERIAL_VARIABLES_COUNT |
| 188 |
}; |
| 189 |
|
| 190 |
using MaterialDomain = filament::MaterialDomain; |
| 191 |
using RefractionMode = filament::RefractionMode; |
| 192 |
using RefractionType = filament::RefractionType; |
| 193 |
|
| 194 |
using ShaderQuality = filament::ShaderQuality; |
| 195 |
using BlendingMode = filament::BlendingMode; |
| 196 |
using Shading = filament::Shading; |
| 197 |
using Interpolation = filament::Interpolation; |
| 198 |
using VertexDomain = filament::VertexDomain; |
| 199 |
using TransparencyMode = filament::TransparencyMode; |
| 200 |
using SpecularAmbientOcclusion = filament::SpecularAmbientOcclusion; |
| 201 |
|
| 202 |
using UniformType = filament::backend::UniformType; |
| 203 |
using SamplerType = filament::backend::SamplerType; |
| 204 |
using SubpassType = filament::backend::SubpassType; |
| 205 |
using SamplerFormat = filament::backend::SamplerFormat; |
| 206 |
using SamplerPrecision = filament::backend::Precision; |
| 207 |
using CullingMode = filament::backend::CullingMode; |
| 208 |
|
| 209 |
enum class VariableQualifier : uint8_t { |
| 210 |
OUT |
| 211 |
}; |
| 212 |
|
| 213 |
enum class OutputTarget : uint8_t { |
| 214 |
COLOR, |
| 215 |
DEPTH |
| 216 |
}; |
| 217 |
|
| 218 |
enum class OutputType : uint8_t { |
| 219 |
FLOAT, |
| 220 |
FLOAT2, |
| 221 |
FLOAT3, |
| 222 |
FLOAT4 |
| 223 |
}; |
| 224 |
|
| 225 |
struct PreprocessorDefine { |
| 226 |
std::string name; |
| 227 |
std::string value; |
| 228 |
|
| 229 |
PreprocessorDefine(const std::string& name, const std::string& value) : |
| 230 |
name(name), value(value) {} |
| 231 |
}; |
| 232 |
using PreprocessorDefineList = std::vector<PreprocessorDefine>; |
| 233 |
|
| 234 |
//! Set the name of this material. |
| 235 |
MaterialBuilder& name(const char* name) noexcept; |
| 236 |
|
| 237 |
//! Set the file name of this material file. Used in error reporting. |
| 238 |
MaterialBuilder& fileName(const char* name) noexcept; |
| 239 |
|
| 240 |
//! Set the shading model. |
| 241 |
MaterialBuilder& shading(Shading shading) noexcept; |
| 242 |
|
| 243 |
//! Set the interpolation mode. |
| 244 |
MaterialBuilder& interpolation(Interpolation interpolation) noexcept; |
| 245 |
|
| 246 |
//! Add a parameter (i.e., a uniform) to this material. |
| 247 |
MaterialBuilder& parameter(UniformType type, const char* name) noexcept; |
| 248 |
|
| 249 |
//! Add a parameter array to this material. |
| 250 |
MaterialBuilder& parameter(UniformType type, size_t size, const char* name) noexcept; |
| 251 |
|
| 252 |
/** |
| 253 |
* Add a sampler parameter to this material. |
| 254 |
* |
| 255 |
* When SamplerType::SAMPLER_EXTERNAL is specifed, format and precision are ignored. |
| 256 |
*/ |
| 257 |
MaterialBuilder& parameter(SamplerType samplerType, SamplerFormat format, |
| 258 |
SamplerPrecision precision, const char* name) noexcept; |
| 259 |
/// @copydoc parameter(SamplerType, SamplerFormat, SamplerPrecision, const char*) |
| 260 |
MaterialBuilder& parameter(SamplerType samplerType, SamplerFormat format, |
| 261 |
const char* name) noexcept; |
| 262 |
/// @copydoc parameter(SamplerType, SamplerFormat, SamplerPrecision, const char*) |
| 263 |
MaterialBuilder& parameter(SamplerType samplerType, SamplerPrecision precision, |
| 264 |
const char* name) noexcept; |
| 265 |
/// @copydoc parameter(SamplerType, SamplerFormat, SamplerPrecision, const char*) |
| 266 |
MaterialBuilder& parameter(SamplerType samplerType, const char* name) noexcept; |
| 267 |
|
| 268 |
//! Custom variables (all float4). |
| 269 |
MaterialBuilder& variable(Variable v, const char* name) noexcept; |
| 270 |
|
| 271 |
/** |
| 272 |
* Require a specified attribute. |
| 273 |
* |
| 274 |
* position is always required and normal depends on the shading model. |
| 275 |
*/ |
| 276 |
MaterialBuilder& require(filament::VertexAttribute attribute) noexcept; |
| 277 |
|
| 278 |
//! Specify the domain that this material will operate in. |
| 279 |
MaterialBuilder& materialDomain(MaterialDomain materialDomain) noexcept; |
| 280 |
|
| 281 |
/** |
| 282 |
* Set the code content of this material. |
| 283 |
* |
| 284 |
* Surface Domain |
| 285 |
* -------------- |
| 286 |
* |
| 287 |
* Materials in the SURFACE domain must declare a function: |
| 288 |
* ~~~~~ |
| 289 |
* void material(inout MaterialInputs material) { |
| 290 |
* prepareMaterial(material); |
| 291 |
* material.baseColor.rgb = float3(1.0, 0.0, 0.0); |
| 292 |
* } |
| 293 |
* ~~~~~ |
| 294 |
* this function *must* call `prepareMaterial(material)` before it returns. |
| 295 |
* |
| 296 |
* Post-process Domain |
| 297 |
* ------------------- |
| 298 |
* |
| 299 |
* Materials in the POST_PROCESS domain must declare a function: |
| 300 |
* ~~~~~ |
| 301 |
* void postProcess(inout PostProcessInputs postProcess) { |
| 302 |
* postProcess.color = float4(1.0); |
| 303 |
* } |
| 304 |
* ~~~~~ |
| 305 |
* |
| 306 |
* @param code The source code of the material. |
| 307 |
* @param line The line number offset of the material, where 0 is the first line. Used for error |
| 308 |
* reporting |
| 309 |
*/ |
| 310 |
MaterialBuilder& material(const char* code, size_t line = 0) noexcept; |
| 311 |
|
| 312 |
/** |
| 313 |
* Set the callback used for resolving include directives. |
| 314 |
* The default is no callback, which disallows all includes. |
| 315 |
*/ |
| 316 |
MaterialBuilder& includeCallback(IncludeCallback callback) noexcept; |
| 317 |
|
| 318 |
/** |
| 319 |
* Set the vertex code content of this material. |
| 320 |
* |
| 321 |
* Surface Domain |
| 322 |
* -------------- |
| 323 |
* |
| 324 |
* Materials in the SURFACE domain must declare a function: |
| 325 |
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 326 |
* void materialVertex(inout MaterialVertexInputs material) { |
| 327 |
* |
| 328 |
* } |
| 329 |
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 330 |
* |
| 331 |
* Post-process Domain |
| 332 |
* ------------------- |
| 333 |
* |
| 334 |
* Materials in the POST_PROCESS domain must declare a function: |
| 335 |
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 336 |
* void postProcessVertex(inout PostProcessVertexInputs postProcess) { |
| 337 |
* |
| 338 |
* } |
| 339 |
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 340 |
|
| 341 |
* @param code The source code of the material. |
| 342 |
* @param line The line number offset of the material, where 0 is the first line. Used for error |
| 343 |
* reporting |
| 344 |
*/ |
| 345 |
MaterialBuilder& materialVertex(const char* code, size_t line = 0) noexcept; |
| 346 |
|
| 347 |
|
| 348 |
MaterialBuilder& quality(ShaderQuality quality) noexcept; |
| 349 |
|
| 350 |
//! Set the blending mode for this material. |
| 351 |
MaterialBuilder& blending(BlendingMode blending) noexcept; |
| 352 |
|
| 353 |
/** |
| 354 |
* Set the blending mode of the post-lighting color for this material. |
| 355 |
* Only OPAQUE, TRANSPARENT and ADD are supported, the default is TRANSPARENT. |
| 356 |
* This setting requires the material property "postLightingColor" to be set. |
| 357 |
*/ |
| 358 |
MaterialBuilder& postLightingBlending(BlendingMode blending) noexcept; |
| 359 |
|
| 360 |
//! Set the vertex domain for this material. |
| 361 |
MaterialBuilder& vertexDomain(VertexDomain domain) noexcept; |
| 362 |
|
| 363 |
/** |
| 364 |
* How triangles are culled by default (doesn't affect points or lines, BACK by default). |
| 365 |
* Material instances can override this. |
| 366 |
*/ |
| 367 |
MaterialBuilder& culling(CullingMode culling) noexcept; |
| 368 |
|
| 369 |
//! Enable / disable color-buffer write (enabled by default, material instances can override). |
| 370 |
MaterialBuilder& colorWrite(bool enable) noexcept; |
| 371 |
|
| 372 |
//! Enable / disable depth-buffer write (enabled by default for opaque, disabled for others, material instances can override). |
| 373 |
MaterialBuilder& depthWrite(bool enable) noexcept; |
| 374 |
|
| 375 |
//! Enable / disable depth based culling (enabled by default, material instances can override). |
| 376 |
MaterialBuilder& depthCulling(bool enable) noexcept; |
| 377 |
|
| 378 |
/** |
| 379 |
* Double-sided materials don't cull faces, equivalent to culling(CullingMode::NONE). |
| 380 |
* doubleSided() overrides culling() if called. |
| 381 |
* When called with "false", this enables the capability for a run-time toggle. |
| 382 |
*/ |
| 383 |
MaterialBuilder& doubleSided(bool doubleSided) noexcept; |
| 384 |
|
| 385 |
/** |
| 386 |
* Any fragment with an alpha below this threshold is clipped (MASKED blending mode only). |
| 387 |
* The mask threshold can also be controlled by using the float material parameter called |
| 388 |
* `_maskThreshold`, or by calling |
| 389 |
* @ref filament::MaterialInstance::setMaskThreshold "MaterialInstance::setMaskThreshold". |
| 390 |
*/ |
| 391 |
MaterialBuilder& maskThreshold(float threshold) noexcept; |
| 392 |
|
| 393 |
//! The material output is multiplied by the shadowing factor (UNLIT model only). |
| 394 |
MaterialBuilder& shadowMultiplier(bool shadowMultiplier) noexcept; |
| 395 |
|
| 396 |
/** |
| 397 |
* Reduces specular aliasing for materials that have low roughness. Turning this feature on also |
| 398 |
* helps preserve the shapes of specular highlights as an object moves away from the camera. |
| 399 |
* When turned on, two float material parameters are added to control the effect: |
| 400 |
* `_specularAAScreenSpaceVariance` and `_specularAAThreshold`. You can also use |
| 401 |
* @ref filament::MaterialInstance::setSpecularAntiAliasingVariance |
| 402 |
* "MaterialInstance::setSpecularAntiAliasingVariance" and |
| 403 |
* @ref filament::MaterialInstance::setSpecularAntiAliasingThreshold |
| 404 |
* "setSpecularAntiAliasingThreshold" |
| 405 |
* |
| 406 |
* Disabled by default. |
| 407 |
*/ |
| 408 |
MaterialBuilder& specularAntiAliasing(bool specularAntiAliasing) noexcept; |
| 409 |
|
| 410 |
/** |
| 411 |
* Sets the screen-space variance of the filter kernel used when applying specular |
| 412 |
* anti-aliasing. The default value is set to 0.15. The specified value should be between 0 and |
| 413 |
* 1 and will be clamped if necessary. |
| 414 |
*/ |
| 415 |
MaterialBuilder& specularAntiAliasingVariance(float screenSpaceVariance) noexcept; |
| 416 |
|
| 417 |
/** |
| 418 |
* Sets the clamping threshold used to suppress estimation errors when applying specular |
| 419 |
* anti-aliasing. The default value is set to 0.2. The specified value should be between 0 and 1 |
| 420 |
* and will be clamped if necessary. |
| 421 |
*/ |
| 422 |
MaterialBuilder& specularAntiAliasingThreshold(float threshold) noexcept; |
| 423 |
|
| 424 |
/** |
| 425 |
* Enables or disables the index of refraction (IoR) change caused by the clear coat layer when |
| 426 |
* present. When the IoR changes, the base color is darkened. Disabling this feature preserves |
| 427 |
* the base color as initially specified. |
| 428 |
* |
| 429 |
* Enabled by default. |
| 430 |
*/ |
| 431 |
MaterialBuilder& clearCoatIorChange(bool clearCoatIorChange) noexcept; |
| 432 |
|
| 433 |
//! Enable / disable flipping of the Y coordinate of UV attributes, enabled by default. |
| 434 |
MaterialBuilder& flipUV(bool flipUV) noexcept; |
| 435 |
|
| 436 |
//! Enable / disable multi-bounce ambient occlusion, disabled by default on mobile. |
| 437 |
MaterialBuilder& multiBounceAmbientOcclusion(bool multiBounceAO) noexcept; |
| 438 |
|
| 439 |
//! Set the specular ambient occlusion technique. Disabled by default on mobile. |
| 440 |
MaterialBuilder& specularAmbientOcclusion(SpecularAmbientOcclusion specularAO) noexcept; |
| 441 |
|
| 442 |
//! Specify the refraction |
| 443 |
MaterialBuilder& refractionMode(RefractionMode refraction) noexcept; |
| 444 |
|
| 445 |
//! Specify the refraction type |
| 446 |
MaterialBuilder& refractionType(RefractionType refractionType) noexcept; |
| 447 |
|
| 448 |
//! Specifies how transparent objects should be rendered (default is DEFAULT). |
| 449 |
MaterialBuilder& transparencyMode(TransparencyMode mode) noexcept; |
| 450 |
|
| 451 |
/** |
| 452 |
* Specifies desktop vs mobile; works in concert with TargetApi to determine the shader models |
| 453 |
* (used to generate code) and final output representations (spirv and/or text). |
| 454 |
*/ |
| 455 |
MaterialBuilder& platform(Platform platform) noexcept; |
| 456 |
|
| 457 |
/** |
| 458 |
* Specifies OpenGL, Vulkan, or Metal. |
| 459 |
* This can be called repeatedly to build for multiple APIs. |
| 460 |
* Works in concert with Platform to determine the shader models (used to generate code) and |
| 461 |
* final output representations (spirv and/or text). |
| 462 |
* If linking against filamat_lite, only `OPENGL` is allowed. |
| 463 |
*/ |
| 464 |
MaterialBuilder& targetApi(TargetApi targetApi) noexcept; |
| 465 |
|
| 466 |
/** |
| 467 |
* Specifies the level of optimization to apply to the shaders (default is PERFORMANCE). |
| 468 |
* If linking against filamat_lite, this _must_ be called with Optimization::NONE. |
| 469 |
*/ |
| 470 |
MaterialBuilder& optimization(Optimization optimization) noexcept; |
| 471 |
|
| 472 |
// TODO: this is present here for matc's "--print" flag, but ideally does not belong inside |
| 473 |
// MaterialBuilder. |
| 474 |
//! If true, will output the generated GLSL shader code to stdout. |
| 475 |
MaterialBuilder& printShaders(bool printShaders) noexcept; |
| 476 |
|
| 477 |
//! If true, will include debugging information in generated SPIRV. |
| 478 |
MaterialBuilder& generateDebugInfo(bool generateDebugInfo) noexcept; |
| 479 |
|
| 480 |
//! Specifies a list of variants that should be filtered out during code generation. |
| 481 |
MaterialBuilder& variantFilter(uint8_t variantFilter) noexcept; |
| 482 |
|
| 483 |
//! Adds a new preprocessor macro definition to the shader code. Can be called repeatedly. |
| 484 |
MaterialBuilder& shaderDefine(const char* name, const char* value) noexcept; |
| 485 |
|
| 486 |
//! Add a new fragment shader output variable. Only valid for materials in the POST_PROCESS domain. |
| 487 |
MaterialBuilder& output(VariableQualifier qualifier, OutputTarget target, |
| 488 |
OutputType type, const char* name, int location = -1) noexcept; |
| 489 |
|
| 490 |
MaterialBuilder& enableFramebufferFetch() noexcept; |
| 491 |
|
| 492 |
/** |
| 493 |
* Build the material. If you are using the Filament engine with this library, you should use |
| 494 |
* the job system provided by Engine. |
| 495 |
*/ |
| 496 |
Package build(utils::JobSystem& jobSystem) noexcept; |
| 497 |
|
| 498 |
public: |
| 499 |
// The methods and types below are for internal use |
| 500 |
/// @cond never |
| 501 |
|
| 502 |
/** |
| 503 |
* Add a subpass parameter to this material. |
| 504 |
*/ |
| 505 |
MaterialBuilder& parameter(SubpassType subpassType, SamplerFormat format, SamplerPrecision |
| 506 |
precision, const char* name) noexcept; |
| 507 |
MaterialBuilder& parameter(SubpassType subpassType, SamplerFormat format, const char* name) |
| 508 |
noexcept; |
| 509 |
MaterialBuilder& parameter(SubpassType subpassType, SamplerPrecision precision, |
| 510 |
const char* name) noexcept; |
| 511 |
MaterialBuilder& parameter(SubpassType subpassType, const char* name) noexcept; |
| 512 |
|
| 513 |
struct Parameter { |
| 514 |
Parameter() noexcept : parameterType(INVALID) {} |
| 515 |
Parameter(const char* paramName, SamplerType t, SamplerFormat f, SamplerPrecision p) |
| 516 |
: name(paramName), size(1), samplerType(t), format(f), precision(p), |
| 517 |
parameterType(SAMPLER) { } |
| 518 |
Parameter(const char* paramName, UniformType t, size_t typeSize) |
| 519 |
: name(paramName), size(typeSize), uniformType(t), parameterType(UNIFORM) { } |
| 520 |
Parameter(const char* paramName, SubpassType t, SamplerFormat f, SamplerPrecision p) |
| 521 |
: name(paramName), size(1), subpassType(t), format(f), precision(p), |
| 522 |
parameterType(SUBPASS) { } |
| 523 |
utils::CString name; |
| 524 |
size_t size; |
| 525 |
union { |
| 526 |
UniformType uniformType; |
| 527 |
struct { |
| 528 |
union { |
| 529 |
SamplerType samplerType; |
| 530 |
SubpassType subpassType; |
| 531 |
}; |
| 532 |
SamplerFormat format; |
| 533 |
SamplerPrecision precision; |
| 534 |
}; |
| 535 |
}; |
| 536 |
enum { |
| 537 |
INVALID, |
| 538 |
UNIFORM, |
| 539 |
SAMPLER, |
| 540 |
SUBPASS |
| 541 |
} parameterType; |
| 542 |
|
| 543 |
bool isSampler() const { return parameterType == SAMPLER; } |
| 544 |
bool isUniform() const { return parameterType == UNIFORM; } |
| 545 |
bool isSubpass() const { return parameterType == SUBPASS; } |
| 546 |
}; |
| 547 |
|
| 548 |
struct Output { |
| 549 |
Output() noexcept = default; |
| 550 |
Output(const char* outputName, VariableQualifier qualifier, OutputTarget target, |
| 551 |
OutputType type, int location) noexcept |
| 552 |
: name(outputName), qualifier(qualifier), target(target), type(type), |
| 553 |
location(location) { } |
| 554 |
|
| 555 |
utils::CString name; |
| 556 |
VariableQualifier qualifier; |
| 557 |
OutputTarget target; |
| 558 |
OutputType type; |
| 559 |
int location; |
| 560 |
}; |
| 561 |
|
| 562 |
static constexpr size_t MATERIAL_PROPERTIES_COUNT = filament::MATERIAL_PROPERTIES_COUNT; |
| 563 |
using Property = filament::Property; |
| 564 |
|
| 565 |
using PropertyList = bool[MATERIAL_PROPERTIES_COUNT]; |
| 566 |
using VariableList = utils::CString[MATERIAL_VARIABLES_COUNT]; |
| 567 |
using OutputList = std::vector<Output>; |
| 568 |
|
| 569 |
static constexpr size_t MAX_COLOR_OUTPUT = filament::backend::MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT; |
| 570 |
static constexpr size_t MAX_DEPTH_OUTPUT = 1; |
| 571 |
static_assert(MAX_COLOR_OUTPUT == 8, |
| 572 |
"When updating MRT::TARGET_COUNT, manually update post_process_inputs.fs" |
| 573 |
" and post_process.fs"); |
| 574 |
|
| 575 |
// Preview the first shader generated by the given CodeGenParams. |
| 576 |
// This is used to run Static Code Analysis before generating a package. |
| 577 |
const std::string peek(filament::backend::ShaderType type, |
| 578 |
const CodeGenParams& params, const PropertyList& properties) noexcept; |
| 579 |
|
| 580 |
// Returns true if any of the parameter samplers is of type samplerExternal |
| 581 |
bool hasExternalSampler() const noexcept; |
| 582 |
|
| 583 |
static constexpr size_t MAX_PARAMETERS_COUNT = 48; |
| 584 |
static constexpr size_t MAX_SUBPASS_COUNT = 1; |
| 585 |
using ParameterList = Parameter[MAX_PARAMETERS_COUNT]; |
| 586 |
|
| 587 |
// returns the number of parameters declared in this material |
| 588 |
uint8_t getParameterCount() const noexcept { return mParameterCount; } |
| 589 |
|
| 590 |
// returns a list of at least getParameterCount() parameters |
| 591 |
const ParameterList& getParameters() const noexcept { return mParameters; } |
| 592 |
|
| 593 |
uint8_t getVariantFilter() const { return mVariantFilter; } |
| 594 |
|
| 595 |
/// @endcond |
| 596 |
|
| 597 |
private: |
| 598 |
void prepareToBuild(MaterialInfo& info) noexcept; |
| 599 |
|
| 600 |
// Return true if the shader is syntactically and semantically valid. |
| 601 |
// This method finds all the properties defined in the fragment and |
| 602 |
// vertex shaders of the material. |
| 603 |
bool findAllProperties() noexcept; |
| 604 |
// Multiple calls to findProperties accumulate the property sets across fragment |
| 605 |
// and vertex shaders in mProperties. |
| 606 |
bool findProperties(filament::backend::ShaderType type, |
| 607 |
MaterialBuilder::PropertyList& p) noexcept; |
| 608 |
bool runSemanticAnalysis() noexcept; |
| 609 |
|
| 610 |
bool checkLiteRequirements() noexcept; |
| 611 |
|
| 612 |
void writeCommonChunks(ChunkContainer& container, MaterialInfo& info) const noexcept; |
| 613 |
void writeSurfaceChunks(ChunkContainer& container) const noexcept; |
| 614 |
|
| 615 |
bool generateShaders( |
| 616 |
utils::JobSystem& jobSystem, |
| 617 |
const std::vector<Variant>& variants, ChunkContainer& container, |
| 618 |
const MaterialInfo& info) const noexcept; |
| 619 |
|
| 620 |
bool isLit() const noexcept { return mShading != filament::Shading::UNLIT; } |
| 621 |
|
| 622 |
utils::CString mMaterialName; |
| 623 |
utils::CString mFileName; |
| 624 |
|
| 625 |
class ShaderCode { |
| 626 |
public: |
| 627 |
void setLineOffset(size_t offset) noexcept { mLineOffset = offset; } |
| 628 |
void setUnresolved(const utils::CString& code) noexcept { |
| 629 |
mIncludesResolved = false; |
| 630 |
mCode = code; |
| 631 |
} |
| 632 |
|
| 633 |
// Resolve all the #include directives, returns true if successful. |
| 634 |
bool resolveIncludes(IncludeCallback callback, const utils::CString& fileName) noexcept; |
| 635 |
|
| 636 |
const utils::CString& getResolved() const noexcept { |
| 637 |
assert(mIncludesResolved); |
| 638 |
return mCode; |
| 639 |
} |
| 640 |
|
| 641 |
size_t getLineOffset() const noexcept { return mLineOffset; } |
| 642 |
|
| 643 |
private: |
| 644 |
utils::CString mCode; |
| 645 |
size_t mLineOffset = 0; |
| 646 |
bool mIncludesResolved = false; |
| 647 |
}; |
| 648 |
|
| 649 |
ShaderCode mMaterialCode; |
| 650 |
ShaderCode mMaterialVertexCode; |
| 651 |
|
| 652 |
IncludeCallback mIncludeCallback = nullptr; |
| 653 |
|
| 654 |
PropertyList mProperties; |
| 655 |
ParameterList mParameters; |
| 656 |
VariableList mVariables; |
| 657 |
OutputList mOutputs; |
| 658 |
|
| 659 |
ShaderQuality mShaderQuality = ShaderQuality::DEFAULT; |
| 660 |
BlendingMode mBlendingMode = BlendingMode::OPAQUE; |
| 661 |
BlendingMode mPostLightingBlendingMode = BlendingMode::TRANSPARENT; |
| 662 |
CullingMode mCullingMode = CullingMode::BACK; |
| 663 |
Shading mShading = Shading::LIT; |
| 664 |
MaterialDomain mMaterialDomain = MaterialDomain::SURFACE; |
| 665 |
RefractionMode mRefractionMode = RefractionMode::NONE; |
| 666 |
RefractionType mRefractionType = RefractionType::SOLID; |
| 667 |
Interpolation mInterpolation = Interpolation::SMOOTH; |
| 668 |
VertexDomain mVertexDomain = VertexDomain::OBJECT; |
| 669 |
TransparencyMode mTransparencyMode = TransparencyMode::DEFAULT; |
| 670 |
|
| 671 |
filament::AttributeBitset mRequiredAttributes; |
| 672 |
|
| 673 |
float mMaskThreshold = 0.4f; |
| 674 |
float mSpecularAntiAliasingVariance = 0.15f; |
| 675 |
float mSpecularAntiAliasingThreshold = 0.2f; |
| 676 |
|
| 677 |
bool mShadowMultiplier = false; |
| 678 |
|
| 679 |
uint8_t mParameterCount = 0; |
| 680 |
|
| 681 |
bool mDoubleSided = false; |
| 682 |
bool mDoubleSidedCapability = false; |
| 683 |
bool mColorWrite = true; |
| 684 |
bool mDepthTest = true; |
| 685 |
bool mDepthWrite = true; |
| 686 |
bool mDepthWriteSet = false; |
| 687 |
|
| 688 |
bool mSpecularAntiAliasing = false; |
| 689 |
bool mClearCoatIorChange = true; |
| 690 |
|
| 691 |
bool mFlipUV = true; |
| 692 |
|
| 693 |
bool mMultiBounceAO = false; |
| 694 |
bool mMultiBounceAOSet = false; |
| 695 |
|
| 696 |
SpecularAmbientOcclusion mSpecularAO = SpecularAmbientOcclusion::NONE; |
| 697 |
bool mSpecularAOSet = false; |
| 698 |
|
| 699 |
bool mEnableFramebufferFetch = false; |
| 700 |
|
| 701 |
PreprocessorDefineList mDefines; |
| 702 |
}; |
| 703 |
|
| 704 |
} // namespace filamat |
| 705 |
|
| 706 |
template<> struct utils::EnableBitMaskOperators<filamat::MaterialBuilder::TargetApi> |
| 707 |
: public std::true_type {}; |
| 708 |
|
| 709 |
#endif |