Skip to content

Commit 73fbc9c

Browse files
committed
Broken glTF rendering for test cases.
1 parent 90a659a commit 73fbc9c

File tree

4 files changed

+354
-49
lines changed

4 files changed

+354
-49
lines changed

test/gltf-comparison/CMakeLists.txt

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,119 @@ project(filament C ASM)
44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

7+
set(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../..)
8+
set(GENERATION_ROOT ${CMAKE_CURRENT_BINARY_DIR})
9+
set(RESOURCE_DIR "${GENERATION_ROOT}/generated/resources")
10+
set(MATERIAL_DIR "${GENERATION_ROOT}/generated/material")
11+
set(TEXTURE_DIR "${GENERATION_ROOT}/generated/texture")
12+
set(RESOURCE_BINS)
13+
14+
# Materials
15+
set(MATERIAL_SRCS
16+
${ROOT_DIR}/samples/materials/aiDefaultMat.mat
17+
${ROOT_DIR}/samples/materials/bakedColor.mat
18+
${ROOT_DIR}/samples/materials/bakedTexture.mat
19+
${ROOT_DIR}/samples/materials/aoPreview.mat
20+
${ROOT_DIR}/samples/materials/arrayTexture.mat
21+
${ROOT_DIR}/samples/materials/groundShadow.mat
22+
${ROOT_DIR}/samples/materials/heightfield.mat
23+
${ROOT_DIR}/samples/materials/image.mat
24+
${ROOT_DIR}/samples/materials/mirror.mat
25+
${ROOT_DIR}/samples/materials/overdraw.mat
26+
${ROOT_DIR}/samples/materials/sandboxCloth.mat
27+
${ROOT_DIR}/samples/materials/sandboxLit.mat
28+
${ROOT_DIR}/samples/materials/sandboxLitFade.mat
29+
${ROOT_DIR}/samples/materials/sandboxLitTransparent.mat
30+
${ROOT_DIR}/samples/materials/sandboxLitThinRefraction.mat
31+
${ROOT_DIR}/samples/materials/sandboxLitThinRefractionSsr.mat
32+
${ROOT_DIR}/samples/materials/sandboxLitSolidRefraction.mat
33+
${ROOT_DIR}/samples/materials/sandboxLitSolidRefractionSsr.mat
34+
${ROOT_DIR}/samples/materials/sandboxSpecGloss.mat
35+
${ROOT_DIR}/samples/materials/sandboxSubsurface.mat
36+
${ROOT_DIR}/samples/materials/sandboxUnlit.mat
37+
${ROOT_DIR}/samples/materials/texturedLit.mat
38+
)
39+
40+
# Tint does not support setting gl_PointSize, disable the relevant sample if using WebGPU
41+
# https://github.com/gpuweb/gpuweb/issues/1190
42+
if (NOT FILAMENT_SUPPORTS_WEBGPU)
43+
set(MATERIAL_SRCS ${MATERIAL_SRCS} materials/pointSprites.mat)
44+
endif ()
45+
46+
if (CMAKE_CROSSCOMPILING)
47+
include(${IMPORT_EXECUTABLES})
48+
endif()
49+
50+
file(MAKE_DIRECTORY ${MATERIAL_DIR})
51+
52+
set (MATC_FLAGS ${MATC_BASE_FLAGS})
53+
if (FILAMENT_SAMPLES_STEREO_TYPE STREQUAL "instanced")
54+
set (MATC_FLAGS ${MATC_FLAGS} -PstereoscopicType=instanced)
55+
add_definitions(-DFILAMENT_SAMPLES_STEREO_TYPE_INSTANCED)
56+
elseif (FILAMENT_SAMPLES_STEREO_TYPE STREQUAL "multiview")
57+
set (MATC_FLAGS ${MATC_FLAGS} -PstereoscopicType=multiview)
58+
add_definitions(-DFILAMENT_SAMPLES_STEREO_TYPE_MULTIVIEW)
59+
endif ()
60+
61+
foreach (mat_src ${MATERIAL_SRCS})
62+
get_filename_component(localname "${mat_src}" NAME_WE)
63+
get_filename_component(fullname "${mat_src}" ABSOLUTE)
64+
set(output_path "${MATERIAL_DIR}/${localname}.filamat")
65+
add_custom_command(
66+
OUTPUT ${output_path}
67+
COMMAND matc ${MATC_FLAGS} -o ${output_path} ${fullname}
68+
MAIN_DEPENDENCY ${mat_src}
69+
DEPENDS matc
70+
COMMENT "Compiling material ${mat_src} to ${output_path}"
71+
)
72+
list(APPEND RESOURCE_BINS ${output_path})
73+
endforeach()
74+
75+
# Resources
76+
file(MAKE_DIRECTORY ${RESOURCE_DIR})
77+
78+
get_resgen_vars(${RESOURCE_DIR} resources)
79+
80+
add_custom_command(
81+
OUTPUT ${RESGEN_OUTPUTS}
82+
COMMAND resgen ${RESGEN_FLAGS} ${RESOURCE_BINS}
83+
DEPENDS resgen ${RESOURCE_BINS}
84+
COMMENT "Aggregating resources"
85+
)
86+
87+
if (DEFINED RESGEN_SOURCE_FLAGS)
88+
set_source_files_properties(${RESGEN_SOURCE} PROPERTIES COMPILE_FLAGS ${RESGEN_SOURCE_FLAGS})
89+
endif()
90+
91+
add_library(gltf-resources ${DUMMY_SRC})
92+
set_target_properties(gltf-resources PROPERTIES FOLDER Samples/Resources)
93+
94+
set(GLTF_DEMO_RESOURCES
95+
${ROOT_DIR}/third_party/models/DamagedHelmet/DamagedHelmet.glb
96+
${MATERIAL_DIR}/groundShadow.filamat
97+
${MATERIAL_DIR}/overdraw.filamat
98+
)
99+
100+
get_resgen_vars(${RESOURCE_DIR} gltf_demo)
101+
102+
add_custom_command(
103+
OUTPUT ${RESGEN_OUTPUTS}
104+
COMMAND resgen ${RESGEN_FLAGS} ${GLTF_DEMO_RESOURCES}
105+
DEPENDS resgen ${GLTF_DEMO_RESOURCES}
106+
)
107+
108+
if (DEFINED RESGEN_SOURCE_FLAGS)
109+
set_source_files_properties(${RESGEN_SOURCE} PROPERTIES COMPILE_FLAGS ${RESGEN_SOURCE_FLAGS})
110+
endif()
111+
112+
target_sources(gltf-resources PRIVATE ${RESGEN_SOURCE})
113+
114+
115+
# CMake fails to invoke ar on Windows unless there is at least one C/C++ file in the library.
116+
set(DUMMY_SRC "${RESOURCE_DIR}/dummy.c")
117+
add_custom_command(OUTPUT ${DUMMY_SRC} COMMAND echo "//" > ${DUMMY_SRC})
118+
119+
# Test binary
7120
set(GLTF_COMPARISON_SOURCES
8121
src/test_CompareGLTF.cpp
9122
src/GLTFViewer.cpp
@@ -13,7 +126,7 @@ set(GLTF_COMPARISON_SOURCES
13126
src/main.cpp
14127
)
15128

16-
file(COPY glTF_cases DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/glTF_cases)
129+
file(COPY glTF_cases DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
17130
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/images/actual_images)
18131
file(COPY expected_images DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/images)
19132

@@ -24,8 +137,11 @@ target_include_directories(gltf_comparison PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/in
24137
target_link_libraries(gltf_comparison
25138
gtest
26139
filamentapp
27-
gltfio
28140
absl::str_format
141+
uberarchive
142+
gltf-resources
143+
gltfio
144+
viewer
29145
)
30146

31147

test/gltf-comparison/include/GLTFViewer.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,72 @@
1717
#ifndef TNT_GLTFVIEWER_H
1818
#define TNT_GLTFVIEWER_H
1919

20+
#include <string>
21+
22+
#include <filamentapp/Config.h>
23+
#include <filamentapp/FilamentApp.h>
24+
#include <filamentapp/IBL.h>
25+
26+
#include <filament/Camera.h>
27+
#include <filament/Engine.h>
28+
#include <filament/IndexBuffer.h>
29+
#include <filament/Material.h>
30+
#include <filament/MaterialInstance.h>
31+
#include <filament/RenderableManager.h>
32+
#include <filament/Scene.h>
33+
#include <filament/Skybox.h>
34+
#include <filament/TransformManager.h>
35+
#include <filament/VertexBuffer.h>
36+
#include <filament/View.h>
37+
38+
#include <gltfio/AssetLoader.h>
39+
#include <gltfio/FilamentAsset.h>
40+
#include <gltfio/ResourceLoader.h>
41+
#include <gltfio/TextureProvider.h>
42+
43+
#include <viewer/ViewerGui.h>
44+
45+
#include <utils/EntityManager.h>
46+
47+
class GLTFViewer {
48+
public:
49+
void setFilename(std::string fileName);
50+
51+
void runApp(std::function<void(filament::Engine*, filament::View*, filament::Scene*,
52+
filament::Renderer*)>
53+
postRender);
54+
55+
private:
56+
void setup(filament::Engine* engine, filament::View* view, filament::Scene* scene);
57+
void cleanup(filament::Engine* engine, filament::View* view, filament::Scene* scene);
58+
59+
void animate(filament::Engine* engine, filament::View* view, double now);
60+
61+
void loadAsset(utils::Path filename);
62+
void loadResources(utils::Path filename);
63+
64+
enum MaterialSource {
65+
JITSHADER,
66+
UBERSHADER,
67+
};
68+
struct App {
69+
filament::Engine* engine;
70+
filament::viewer::ViewerGui* viewer;
71+
Config config;
72+
filament::gltfio::AssetLoader* loader;
73+
filament::gltfio::FilamentAsset* asset = nullptr;
74+
utils::NameComponentManager* names;
75+
filament::gltfio::MaterialProvider* materials;
76+
MaterialSource materialSource = UBERSHADER;
77+
filament::gltfio::ResourceLoader* resourceLoader = nullptr;
78+
filament::gltfio::TextureProvider* stbDecoder = nullptr;
79+
filament::gltfio::TextureProvider* ktxDecoder = nullptr;
80+
filament::gltfio::FilamentInstance* instance;
81+
bool enableMSAA = false;
82+
};
83+
App mApp;
84+
85+
std::string mFilename;
86+
};
87+
2088
#endif // TNT_GLTFVIEWER_H

test/gltf-comparison/src/GLTFViewer.cpp

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,162 @@
1515
*/
1616

1717
#include "GLTFViewer.h"
18+
19+
#include <filamentapp/Config.h>
20+
#include <filamentapp/FilamentApp.h>
21+
#include <filamentapp/IBL.h>
22+
23+
#include <filament/Camera.h>
24+
#include <filament/Engine.h>
25+
#include <filament/IndexBuffer.h>
26+
#include <filament/Material.h>
27+
#include <filament/MaterialInstance.h>
28+
#include <filament/RenderableManager.h>
29+
#include <filament/Scene.h>
30+
#include <filament/Skybox.h>
31+
#include <filament/TransformManager.h>
32+
#include <filament/VertexBuffer.h>
33+
#include <filament/View.h>
34+
35+
#include <utils/EntityManager.h>
36+
#include <utils/NameComponentManager.h>
37+
38+
#include <gltfio/TextureProvider.h>
39+
40+
//#include "generated/resources/gltf.h"
41+
#include "materials/uberarchive.h"
42+
43+
#include <iostream>
44+
#include <fstream>
45+
46+
using namespace filament;
47+
using utils::Entity;
48+
using utils::EntityManager;
49+
50+
51+
void GLTFViewer::runApp(std::function<void(filament::Engine*, filament::View*, filament::Scene*,
52+
filament::Renderer*)> postRender) {
53+
FilamentApp::get().animate(
54+
[this](Engine* engine, View* view, double now) { animate(engine, view, now); });
55+
56+
// TODO: Remove
57+
mApp.config.backend = filament::Engine::Backend::METAL;
58+
59+
FilamentApp::get().run(
60+
mApp.config,
61+
[this](Engine* engine, View* view, Scene* scene) { setup(engine, view, scene); },
62+
[this](Engine* engine, View* view, Scene* scene) { cleanup(engine, view, scene); },
63+
FilamentApp::ImGuiCallback(), FilamentApp::PreRenderCallback(), std::move(postRender));
64+
}
65+
66+
void GLTFViewer::setup(Engine* engine, filament::View* view, filament::Scene* scene) {
67+
if (mApp.enableMSAA) {
68+
// TODO Investigate why
69+
// Enabling MSAA fixes the "transmission" sample but breaks the others
70+
view->setMultiSampleAntiAliasingOptions({ .enabled = true});
71+
}
72+
mApp.engine = engine;
73+
mApp.names = new utils::NameComponentManager(EntityManager::get());
74+
mApp.viewer = new viewer::ViewerGui(engine, scene, view);
75+
76+
// TODO: Support JIT
77+
mApp.materials = //(mApp.materialSource == JITSHADER)
78+
//? createJitShaderProvider(engine, false /* optimize */,
79+
// samples::getJitMaterialVariantFilter(mApp.config.backend))
80+
//:
81+
filament::gltfio::createUbershaderProvider(engine, UBERARCHIVE_DEFAULT_DATA,
82+
UBERARCHIVE_DEFAULT_SIZE);
83+
84+
mApp.loader = gltfio::AssetLoader::create({ engine, mApp.materials, mApp.names });
85+
86+
loadAsset(mFilename);
87+
loadResources(mFilename);
88+
mApp.viewer->setAsset(mApp.asset, mApp.instance);
89+
}
90+
91+
void GLTFViewer::cleanup(Engine* engine, filament::View* view, filament::Scene* scene) {
92+
mApp.loader->destroyAsset(mApp.asset);
93+
mApp.materials->destroyMaterials();
94+
95+
delete mApp.materials;
96+
delete mApp.names;
97+
delete mApp.viewer;
98+
delete mApp.resourceLoader;
99+
delete mApp.stbDecoder;
100+
delete mApp.ktxDecoder;
101+
102+
gltfio::AssetLoader::destroy(&mApp.loader);
103+
}
104+
105+
void GLTFViewer::animate(filament::Engine* engine, filament::View* view, double now) {
106+
mApp.resourceLoader->asyncUpdateLoad();
107+
108+
mApp.viewer->updateRootTransform();
109+
mApp.viewer->populateScene();
110+
111+
mApp.viewer->applyAnimation(now, mApp.instance);
112+
}
113+
114+
void GLTFViewer::setFilename(std::string filename) {
115+
mFilename = std::move(filename);
116+
}
117+
118+
static std::ifstream::pos_type getFileSize(const char* filename) {
119+
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
120+
return in.tellg();
121+
}
122+
123+
void GLTFViewer::loadAsset(utils::Path filename) {
124+
// Peek at the file size to allow pre-allocation.
125+
long contentSize = static_cast<long>(getFileSize(filename.c_str()));
126+
if (contentSize <= 0) {
127+
std::cerr << "Unable to open " << filename << std::endl;
128+
exit(1);
129+
}
130+
131+
// Consume the glTF file.
132+
std::ifstream in(filename.c_str(), std::ifstream::binary | std::ifstream::in);
133+
std::vector<uint8_t> buffer(static_cast<unsigned long>(contentSize));
134+
if (!in.read((char*) buffer.data(), contentSize)) {
135+
std::cerr << "Unable to read " << filename << std::endl;
136+
exit(1);
137+
}
138+
139+
// Parse the glTF file and create Filament entities.
140+
mApp.asset = mApp.loader->createInstancedAsset(buffer.data(), buffer.size(),
141+
&(mApp.instance), 1);
142+
buffer.clear();
143+
buffer.shrink_to_fit();
144+
145+
if (!mApp.asset) {
146+
std::cerr << "Unable to parse " << filename << std::endl;
147+
exit(1);
148+
}
149+
}
150+
151+
void GLTFViewer::loadResources(utils::Path filename) {
152+
// Load external textures and buffers.
153+
std::string gltfPath = filename.getAbsolutePath();
154+
gltfio::ResourceConfiguration configuration;
155+
configuration.engine = mApp.engine;
156+
configuration.gltfPath = gltfPath.c_str();
157+
configuration.normalizeSkinningWeights = true;
158+
if (!mApp.resourceLoader) {
159+
mApp.resourceLoader = new gltfio::ResourceLoader(configuration);
160+
mApp.stbDecoder = gltfio::createStbProvider(mApp.engine);
161+
mApp.ktxDecoder = gltfio::createKtx2Provider(mApp.engine);
162+
mApp.resourceLoader->addTextureProvider("image/png", mApp.stbDecoder);
163+
mApp.resourceLoader->addTextureProvider("image/jpeg", mApp.stbDecoder);
164+
mApp.resourceLoader->addTextureProvider("image/ktx2", mApp.ktxDecoder);
165+
}
166+
167+
if (!mApp.resourceLoader->asyncBeginLoad(mApp.asset)) {
168+
std::cerr << "Unable to start loading resources for " << filename << std::endl;
169+
exit(1);
170+
}
171+
172+
auto ibl = FilamentApp::get().getIBL();
173+
if (ibl) {
174+
mApp.viewer->setIndirectLight(ibl->getIndirectLight(), ibl->getSphericalHarmonics());
175+
}
176+
}

0 commit comments

Comments
 (0)