Skip to content

Commit 3cd92cd

Browse files
committed
Move resolving #includes from MaterialBuilder to MaterialCompiler, before parsing the material.
- resolving #includes was happening after parsing, now moving before parsing. this is because we could offload this resolution at build time for RuntimeMaterialCompiler - filamat::resolveIncludes used to have an assumption where the given text was already a shader block. this assumption is now broken so it finds the line offset internally. thus the line offset field is removed from IncludeResult.
1 parent f5c9d97 commit 3cd92cd

File tree

11 files changed

+111
-114
lines changed

11 files changed

+111
-114
lines changed

libs/filamat/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ set(PUBLIC_HDR_DIR include)
99
# ==================================================================================================
1010
set(HDRS
1111
include/filamat/Enums.h
12+
include/filamat/Includes.h
1213
include/filamat/IncludeCallback.h
1314
include/filamat/MaterialBuilder.h
14-
include/filamat/Package.h)
15+
include/filamat/Package.h
16+
)
1517

1618
set(COMMON_PRIVATE_HDRS
1719
src/eiff/Chunk.h
@@ -23,7 +25,6 @@ set(COMMON_PRIVATE_HDRS
2325
src/eiff/MaterialInterfaceBlockChunk.h
2426
src/eiff/ShaderEntry.h
2527
src/eiff/SimpleFieldChunk.h
26-
src/Includes.h
2728
src/PushConstantDefinitions.h)
2829

2930
set(COMMON_SRCS

libs/filamat/include/filamat/IncludeCallback.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace filamat {
2626
struct IncludeResult {
2727
// The include name of the root file, as if it were being included.
2828
// I.e., 'foobar.h' in the case of #include "foobar.h"
29+
// Only relevant when the ResolveOptions::insertLineDirectives is enabled.
2930
const utils::CString includeName;
3031

3132
// The following fields should be filled out by the IncludeCallback when processing an include,
@@ -35,9 +36,6 @@ struct IncludeResult {
3536
// directives.
3637
utils::CString text;
3738

38-
// The line number for the first line of text (first line is 0).
39-
size_t lineNumberOffset = 0;
40-
4139
// The name of the include file. This gets passed as "includerName" for any includes inside of
4240
// source. This field isn't used by the include system; it's up to the callback to give meaning
4341
// to this value and interpret it accordingly. In the case of DirIncluder, this is an empty

libs/filamat/src/Includes.h renamed to libs/filamat/include/filamat/Includes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#ifndef TNT_FILAMAT_INCLUDES_H
1818
#define TNT_FILAMAT_INCLUDES_H
1919

20-
#include <utils/CString.h>
21-
2220
#include <filamat/IncludeCallback.h>
2321

22+
#include <utils/CString.h>
23+
2424
#include <vector>
2525

2626
namespace filamat {
@@ -48,7 +48,7 @@ struct FoundInclude {
4848
utils::CString name;
4949
size_t startPosition;
5050
size_t length;
51-
size_t line; // the line number the include was found on (first line is 1)
51+
size_t line; // the line number the #include was found on.
5252
};
5353

5454
std::vector<FoundInclude> parseForIncludes(const utils::CString& source);

libs/filamat/include/filamat/MaterialBuilder.h

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,13 @@ class UTILS_PUBLIC MaterialBuilder : public MaterialBuilderBase {
384384
* }
385385
* ~~~~~
386386
*
387-
* @param code The source code of the material.
387+
* @param code The source code of the material. Expected it to be all inlined. (#includes are
388+
* resolved.)
388389
* @param line The line number offset of the material, where 0 is the first line. Used for error
389390
* reporting
390391
*/
391392
MaterialBuilder& material(const char* code, size_t line = 0) noexcept;
392393

393-
/**
394-
* Set the callback used for resolving include directives.
395-
* The default is no callback, which disallows all includes.
396-
*/
397-
MaterialBuilder& includeCallback(IncludeCallback callback) noexcept;
398-
399394
/**
400395
* Set the vertex code content of this material.
401396
*
@@ -419,7 +414,8 @@ class UTILS_PUBLIC MaterialBuilder : public MaterialBuilderBase {
419414
* }
420415
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
421416
422-
* @param code The source code of the material.
417+
* @param code The source code of the material. Expected it to be all inlined. (#includes are
418+
* resolved.)
423419
* @param line The line number offset of the material, where 0 is the first line. Used for error
424420
* reporting
425421
*/
@@ -888,22 +884,16 @@ class UTILS_PUBLIC MaterialBuilder : public MaterialBuilderBase {
888884
bool isLit() const noexcept { return mShading != filament::Shading::UNLIT; }
889885

890886
utils::CString mMaterialName;
891-
utils::CString mFileName;
892887
utils::CString mCompilationParameters;
893888

894889
class ShaderCode {
895890
public:
896891
void setLineOffset(size_t offset) noexcept { mLineOffset = offset; }
897-
void setUnresolved(const utils::CString& code) noexcept {
898-
mIncludesResolved = false;
892+
void setCode(const utils::CString& code) noexcept {
899893
mCode = code;
900894
}
901895

902-
// Resolve all the #include directives, returns true if successful.
903-
bool resolveIncludes(IncludeCallback callback, const utils::CString& fileName) noexcept;
904-
905-
const utils::CString& getResolved() const noexcept {
906-
assert(mIncludesResolved);
896+
const utils::CString& getCode() const noexcept {
907897
return mCode;
908898
}
909899

@@ -912,14 +902,11 @@ class UTILS_PUBLIC MaterialBuilder : public MaterialBuilderBase {
912902
private:
913903
utils::CString mCode;
914904
size_t mLineOffset = 0;
915-
bool mIncludesResolved = false;
916905
};
917906

918907
ShaderCode mMaterialFragmentCode;
919908
ShaderCode mMaterialVertexCode;
920909

921-
IncludeCallback mIncludeCallback = nullptr;
922-
923910
PropertyList mProperties;
924911
ParameterList mParameters;
925912
ConstantList mConstants;

libs/filamat/src/Includes.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "Includes.h"
17+
#include <filamat/Includes.h>
1818

1919
#include <utils/Log.h>
2020
#include <utils/compiler.h>
@@ -83,10 +83,16 @@ bool resolveIncludes(IncludeResult& root, IncludeCallback callback,
8383
return false;
8484
}
8585

86-
const size_t lineNumberOffset = root.lineNumberOffset;
8786
utils::CString& text = root.text;
8887

8988
std::vector<FoundInclude> includes = parseForIncludes(text);
89+
if (includes.empty()) {
90+
// No more to resolve.
91+
return true;
92+
}
93+
94+
const size_t lineNumberOffset = includes[0].line;
95+
9096
bool sourceDirty = false;
9197

9298
// If we weren't given an include name, use "0", which is default when no #line directives are
@@ -96,7 +102,7 @@ bool resolveIncludes(IncludeResult& root, IncludeCallback callback,
96102
auto insertLineDirective = [&options](utils::io::ostream& stream, size_t line, const char* filename) {
97103
if (options.insertLineDirectiveCheck) {
98104
stream << "#if defined(GL_GOOGLE_cpp_style_line_directive)\n";
99-
// The #endif itself will count as a line, so subtact 1.
105+
// The #endif itself will count as a line, so subtract 1.
100106
line--;
101107
}
102108
stream << "#line " << line << " \"" << filename << '\"';
@@ -128,9 +134,10 @@ bool resolveIncludes(IncludeResult& root, IncludeCallback callback,
128134
// 3
129135

130136
// We want to insert a closing directive with a line number of 3.
131-
// In this example, include.line is 2 and lineNumberOffset is 0.
137+
// In this example, include.line is 2 and lineNumberOffset is 1. (Since the offset is the
138+
// line number of the #include, thus it is inclusive)
132139
// So, the math works out as such:
133-
const size_t lineDirectiveLine = include.line + lineNumberOffset + 1;
140+
const size_t lineDirectiveLine = include.line + lineNumberOffset;
134141

135142
utils::io::sstream closingDirective;
136143

@@ -169,12 +176,13 @@ bool resolveIncludes(IncludeResult& root, IncludeCallback callback,
169176
sourceDirty = true;
170177
}
171178

172-
// Add a line directive on the first line for the root include.
179+
// Add a line directive on the previous line for the root include.
173180
if (options.insertLineDirectives && depth == 0) {
174181
utils::io::sstream lineDirective;
175-
insertLineDirective(lineDirective, lineNumberOffset + 1, rootIncludeName);
182+
insertLineDirective(lineDirective, lineNumberOffset, rootIncludeName);
176183
lineDirective << '\n';
177-
text.insert(0, utils::CString(lineDirective.c_str()));
184+
const size_t startPos = includes[0].startPosition;
185+
text.insert(startPos, utils::CString(lineDirective.c_str()));
178186
sourceDirty = true;
179187
}
180188

libs/filamat/src/MaterialBuilder.cpp

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
#include <filamat/IncludeCallback.h>
2121
#include <filamat/Package.h>
2222

23-
#include "Includes.h"
23+
#include "GLSLPostProcessor.h"
2424
#include "MaterialVariants.h"
2525
#include "PushConstantDefinitions.h"
26-
#include "GLSLPostProcessor.h"
2726

2827
#include "sca/GLSLTools.h"
2928

@@ -226,29 +225,19 @@ MaterialBuilder& MaterialBuilder::name(const char* name) noexcept {
226225
return *this;
227226
}
228227

229-
MaterialBuilder& MaterialBuilder::fileName(const char* name) noexcept {
230-
mFileName = CString(name);
231-
return *this;
232-
}
233-
234228
MaterialBuilder& MaterialBuilder::compilationParameters(const char* params) noexcept {
235229
mCompilationParameters = CString(params);
236230
return *this;
237231
}
238232

239233
MaterialBuilder& MaterialBuilder::material(const char* code, size_t const line) noexcept {
240-
mMaterialFragmentCode.setUnresolved(CString(code));
234+
mMaterialFragmentCode.setCode(CString(code));
241235
mMaterialFragmentCode.setLineOffset(line);
242236
return *this;
243237
}
244238

245-
MaterialBuilder& MaterialBuilder::includeCallback(IncludeCallback callback) noexcept {
246-
mIncludeCallback = std::move(callback);
247-
return *this;
248-
}
249-
250239
MaterialBuilder& MaterialBuilder::materialVertex(const char* code, size_t const line) noexcept {
251-
mMaterialVertexCode.setUnresolved(CString(code));
240+
mMaterialVertexCode.setCode(CString(code));
252241
mMaterialVertexCode.setLineOffset(line);
253242
return *this;
254243
}
@@ -650,7 +639,7 @@ bool MaterialBuilder::hasSamplerType(SamplerType const samplerType) const noexce
650639
void MaterialBuilder::prepareToBuild(MaterialInfo& info) noexcept {
651640
prepare(mEnableFramebufferFetch, mFeatureLevel);
652641

653-
const bool hasEmptyVertexCode = mMaterialVertexCode.getResolved().empty();
642+
const bool hasEmptyVertexCode = mMaterialVertexCode.getCode().empty();
654643
const bool isPostProcessMaterial = mMaterialDomain == MaterialDomain::POST_PROCESS;
655644
// TODO: Currently, for surface materials, we rely on the presence of a custom vertex shader to
656645
// infer the default shader stages. We could do better by analyzing the AST of the vertex shader
@@ -850,29 +839,6 @@ bool MaterialBuilder::runSemanticAnalysis(MaterialInfo* inOutInfo,
850839
return success;
851840
}
852841

853-
bool MaterialBuilder::ShaderCode::resolveIncludes(IncludeCallback callback,
854-
const CString& fileName) noexcept {
855-
if (!mCode.empty()) {
856-
ResolveOptions const options {
857-
.insertLineDirectives = true,
858-
.insertLineDirectiveCheck = true
859-
};
860-
IncludeResult source {
861-
.includeName = fileName,
862-
.text = mCode,
863-
.lineNumberOffset = getLineOffset(),
864-
.name = CString("")
865-
};
866-
if (!filamat::resolveIncludes(source, std::move(callback), options)) {
867-
return false;
868-
}
869-
mCode = source.text;
870-
}
871-
872-
mIncludesResolved = true;
873-
return true;
874-
}
875-
876842
static void showErrorMessage(const char* materialName, filament::Variant const variant,
877843
MaterialBuilder::TargetApi const targetApi, backend::ShaderStage const shaderType,
878844
MaterialBuilder::FeatureLevel const featureLevel,
@@ -944,8 +910,8 @@ bool MaterialBuilder::generateShaders(JobSystem& jobSystem, const std::vector<Va
944910
// End: must be protected by lock
945911

946912
ShaderGenerator sg(mProperties, mVariables, mOutputs, mDefines, mConstants, mPushConstants,
947-
mMaterialFragmentCode.getResolved(), mMaterialFragmentCode.getLineOffset(),
948-
mMaterialVertexCode.getResolved(), mMaterialVertexCode.getLineOffset(),
913+
mMaterialFragmentCode.getCode(), mMaterialFragmentCode.getLineOffset(),
914+
mMaterialVertexCode.getCode(), mMaterialVertexCode.getLineOffset(),
949915
mMaterialDomain);
950916

951917
container.emplace<bool>(MaterialHasCustomDepthShader,
@@ -1317,12 +1283,6 @@ Package MaterialBuilder::build(JobSystem& jobSystem) {
13171283

13181284
// TODO: maybe check MaterialDomain::COMPUTE has outputs
13191285

1320-
// Resolve all the #include directives within user code.
1321-
if (!mMaterialFragmentCode.resolveIncludes(mIncludeCallback, mFileName) ||
1322-
!mMaterialVertexCode.resolveIncludes(mIncludeCallback, mFileName)) {
1323-
goto error;
1324-
}
1325-
13261286
if (mCustomSurfaceShading && mShading != Shading::LIT) {
13271287
slog.e << "Error: customSurfaceShading can only be used with lit materials." << io::endl;
13281288
goto error;
@@ -1533,7 +1493,7 @@ bool MaterialBuilder::hasCustomVaryings() const noexcept {
15331493
}
15341494

15351495
bool MaterialBuilder::needsStandardDepthProgram() const noexcept {
1536-
const bool hasEmptyVertexCode = mMaterialVertexCode.getResolved().empty();
1496+
const bool hasEmptyVertexCode = mMaterialVertexCode.getCode().empty();
15371497
return !hasEmptyVertexCode ||
15381498
hasCustomVaryings() ||
15391499
mBlendingMode == BlendingMode::MASKED ||
@@ -1546,8 +1506,8 @@ std::string MaterialBuilder::peek(backend::ShaderStage const stage,
15461506
const CodeGenParams& params, const PropertyList& properties) noexcept {
15471507

15481508
ShaderGenerator const sg(properties, mVariables, mOutputs, mDefines, mConstants, mPushConstants,
1549-
mMaterialFragmentCode.getResolved(), mMaterialFragmentCode.getLineOffset(),
1550-
mMaterialVertexCode.getResolved(), mMaterialVertexCode.getLineOffset(),
1509+
mMaterialFragmentCode.getCode(), mMaterialFragmentCode.getLineOffset(),
1510+
mMaterialVertexCode.getCode(), mMaterialVertexCode.getLineOffset(),
15511511
mMaterialDomain);
15521512

15531513
MaterialInfo info;
@@ -1732,8 +1692,8 @@ void MaterialBuilder::writeCommonChunks(ChunkContainer& container, MaterialInfo&
17321692
}
17331693

17341694
// create a unique material id
1735-
auto const& vert = mMaterialVertexCode.getResolved();
1736-
auto const& frag = mMaterialFragmentCode.getResolved();
1695+
auto const& vert = mMaterialVertexCode.getCode();
1696+
auto const& frag = mMaterialFragmentCode.getCode();
17371697
std::hash<std::string_view> const hasher;
17381698
size_t const materialId = hash::combine(
17391699
MATERIAL_VERSION,

libs/filamat/tests/MockIncluder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "Includes.h"
17+
#include <filamat/Includes.h>
1818

1919
#include <unordered_map>
2020
#include <string>

libs/filamat/tests/test_filamat.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,12 @@ std::string shaderWithAllProperties(JobSystem& jobSystem, ShaderStage type,
5656
MockIncluder includer;
5757
includer
5858
.sourceForInclude("modify_normal.h", "material.normal = vec3(0.8);");
59-
6059
filamat::MaterialBuilder builder;
6160
builder.material(fragmentCode.c_str());
6261
builder.materialVertex(vertexCode.c_str());
6362
builder.platform(filamat::MaterialBuilder::Platform::MOBILE);
6463
builder.optimization(filamat::MaterialBuilder::Optimization::NONE);
6564
builder.shading(shadingModel);
66-
builder.includeCallback(includer);
6765
builder.refractionMode(refractionMode);
6866
builder.vertexDomain(vertexDomain);
6967

0 commit comments

Comments
 (0)