Compare commits

...

1 Commits

Author SHA1 Message Date
Powei Feng
ce4199b5f7 Testing rendering to quad 2024-08-27 13:47:43 -07:00
7 changed files with 264 additions and 213 deletions

View File

@@ -34,6 +34,8 @@
#include <math/mat4.h> #include <math/mat4.h>
#include <utils/Log.h>
namespace filament { namespace filament {
using namespace backend; using namespace backend;
@@ -120,6 +122,8 @@ void PerViewUniforms::prepareViewport(
s.resolution = { physical.zw, 1.0f / physical.zw }; s.resolution = { physical.zw, 1.0f / physical.zw };
s.logicalViewportScale = physical.zw / logical.zw; s.logicalViewportScale = physical.zw / logical.zw;
s.logicalViewportOffset = -logical.xy / logical.zw; s.logicalViewportOffset = -logical.xy / logical.zw;
utils::slog.e << "resolution=" << s.resolution << " logical=" <<
s.logicalViewportScale << " " << s.logicalViewportOffset << utils::io::endl;
} }
void PerViewUniforms::prepareTime(FEngine& engine, math::float4 const& userTime) noexcept { void PerViewUniforms::prepareTime(FEngine& engine, math::float4 const& userTime) noexcept {

View File

@@ -2455,18 +2455,24 @@ FrameGraphId<FrameGraphTexture> PostProcessManager::colorGrading(FrameGraph& fg,
const float temporalNoise = mUniformDistribution(mEngine.getRandomEngine()); const float temporalNoise = mUniformDistribution(mEngine.getRandomEngine());
auto yy = float4{
(float)vp.left / input.width,
(float)vp.bottom / input.height,
(float)vp.width / input.width,
(float)vp.height / input.height
};
mi->setParameter("dithering", colorGradingConfig.dithering); mi->setParameter("dithering", colorGradingConfig.dithering);
mi->setParameter("bloom", bloomParameters); mi->setParameter("bloom", bloomParameters);
mi->setParameter("vignette", vignetteParameters); mi->setParameter("vignette", vignetteParameters);
mi->setParameter("vignetteColor", vignetteOptions.color); mi->setParameter("vignetteColor", vignetteOptions.color);
mi->setParameter("fxaa", colorGradingConfig.fxaa); mi->setParameter("fxaa", colorGradingConfig.fxaa);
mi->setParameter("temporalNoise", temporalNoise); mi->setParameter("temporalNoise", temporalNoise);
mi->setParameter("viewport", float4{ mi->setParameter("viewport", yy);
(float)vp.left / input.width,
(float)vp.bottom / input.height, utils::slog.e <<"yy------------yy in=" <<
(float)vp.width / input.width, input.width << "x" << input.height << " viewport=" << yy << utils::io::endl;
(float)vp.height / input.height
});
const uint8_t variant = uint8_t(colorGradingConfig.translucent ? const uint8_t variant = uint8_t(colorGradingConfig.translucent ?
PostProcessVariant::TRANSLUCENT : PostProcessVariant::OPAQUE); PostProcessVariant::TRANSLUCENT : PostProcessVariant::OPAQUE);
@@ -2508,6 +2514,17 @@ FrameGraphId<FrameGraphTexture> PostProcessManager::fxaa(FrameGraph& fg,
.filterMag = SamplerMagFilter::LINEAR, .filterMag = SamplerMagFilter::LINEAR,
.filterMin = SamplerMinFilter::LINEAR .filterMin = SamplerMinFilter::LINEAR
}); });
auto xx = float4{
(float)vp.left / inDesc.width,
(float)vp.bottom / inDesc.height,
(float)vp.width / inDesc.width,
(float)vp.height / inDesc.height
};
utils::slog.e <<"xx-----------xx" <<
"in=" << inDesc.width <<"x" << inDesc.height << " vp=" << vp << " viewport=" << xx << utils::io::endl;
mi->setParameter("viewport", float4{ mi->setParameter("viewport", float4{
(float)vp.left / inDesc.width, (float)vp.left / inDesc.width,
(float)vp.bottom / inDesc.height, (float)vp.bottom / inDesc.height,

View File

@@ -597,6 +597,7 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
bool hasColorGrading = hasPostProcess; bool hasColorGrading = hasPostProcess;
bool hasDithering = view.getDithering() == Dithering::TEMPORAL; bool hasDithering = view.getDithering() == Dithering::TEMPORAL;
bool hasFXAA = view.getAntiAliasing() == AntiAliasing::FXAA; bool hasFXAA = view.getAntiAliasing() == AntiAliasing::FXAA;
// bool hasFXAA = false;//view.getAntiAliasing() == AntiAliasing::FXAA;
float2 scale = view.updateScale(engine, mFrameInfoManager.getLastFrameInfo(), mFrameRateOptions, mDisplayInfo); float2 scale = view.updateScale(engine, mFrameInfoManager.getLastFrameInfo(), mFrameRateOptions, mDisplayInfo);
auto msaaOptions = view.getMultiSampleAntiAliasingOptions(); auto msaaOptions = view.getMultiSampleAntiAliasingOptions();
auto dsrOptions = view.getDynamicResolutionOptions(); auto dsrOptions = view.getDynamicResolutionOptions();
@@ -610,6 +611,7 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
auto guardBandOptions = view.getGuardBandOptions(); auto guardBandOptions = view.getGuardBandOptions();
const bool isRenderingMultiview = view.hasStereo() && const bool isRenderingMultiview = view.hasStereo() &&
engine.getConfig().stereoscopicType == backend::StereoscopicType::MULTIVIEW; engine.getConfig().stereoscopicType == backend::StereoscopicType::MULTIVIEW;
utils::slog.e <<"hasFXAA=" <<hasFXAA << " view=" << &view << utils::io::endl;
// FIXME: This is to override some settings that are not supported for multiview at the moment. // FIXME: This is to override some settings that are not supported for multiview at the moment.
// Remove this when all features are supported. // Remove this when all features are supported.
if (isRenderingMultiview) { if (isRenderingMultiview) {
@@ -703,6 +705,7 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
const bool noBufferPadding = (colorGradingConfig.asSubpass && !hasFXAA && !scaled) const bool noBufferPadding = (colorGradingConfig.asSubpass && !hasFXAA && !scaled)
|| engine.debug.renderer.disable_buffer_padding; || engine.debug.renderer.disable_buffer_padding;
// guardBandOptions.enabled = !noBufferPadding;
// guardBand must be a multiple of 16 to guarantee the same exact rendering up to 4 mip levels. // guardBand must be a multiple of 16 to guarantee the same exact rendering up to 4 mip levels.
float const guardBand = guardBandOptions.enabled ? 16.0f : 0.0f; float const guardBand = guardBandOptions.enabled ? 16.0f : 0.0f;
@@ -719,7 +722,7 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
// the SwapChain, and we might want to keep it this way. // the SwapChain, and we might want to keep it this way.
auto round = [](uint32_t x) { auto round = [](uint32_t x) {
constexpr uint32_t rounding = 16u; constexpr uint32_t rounding = 256u;
return (x + (rounding - 1u)) & ~(rounding - 1u); return (x + (rounding - 1u)) & ~(rounding - 1u);
}; };
@@ -727,6 +730,9 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
const float width = float(round(svp.width )) + 2.0f * guardBand; const float width = float(round(svp.width )) + 2.0f * guardBand;
const float height = float(round(svp.height)) + 2.0f * guardBand; const float height = float(round(svp.height)) + 2.0f * guardBand;
utils::slog.e << "new dim=" << width <<"x" << height <<
" old dim=" << svp.width << "x" << svp.height << utils::io::endl;
// scale the field-of-view up, so it covers exactly the extra pixels // scale the field-of-view up, so it covers exactly the extra pixels
const float3 clipSpaceScaling{ const float3 clipSpaceScaling{
float(svp.width) / width, float(svp.width) / width,
@@ -744,9 +750,14 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
1.0f - clipSpaceScaling.y - 2.0f * guardBand / height 1.0f - clipSpaceScaling.y - 2.0f * guardBand / height
}; };
utils::slog.e <<"clipspace scaling=" << clipSpaceScaling << utils::io::endl;
utils::slog.e <<"clipspace translation=" << clipSpaceTranslation << utils::io::endl;
mat4f ts = mat4f::scaling(clipSpaceScaling); mat4f ts = mat4f::scaling(clipSpaceScaling);
ts[3].xy = -clipSpaceTranslation; ts[3].xy = -clipSpaceTranslation;
utils::slog.e <<"mat=" << ts << utils::io::endl;
// update the camera projection // update the camera projection
cameraInfo.projection = highPrecisionMultiply(ts, cameraInfo.projection); cameraInfo.projection = highPrecisionMultiply(ts, cameraInfo.projection);
@@ -759,6 +770,7 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
svp.height = uint32_t(height); svp.height = uint32_t(height);
xvp.left = int32_t(guardBand); xvp.left = int32_t(guardBand);
xvp.bottom = int32_t(guardBand); xvp.bottom = int32_t(guardBand);
utils::slog.e <<"xvp.left=" << int32_t(guardBand) << utils::io::endl;
} }
view.prepare(engine, driver, rootArenaScope, svp, cameraInfo, getShaderUserTime(), needsAlphaChannel); view.prepare(engine, driver, rootArenaScope, svp, cameraInfo, getShaderUserTime(), needsAlphaChannel);
@@ -870,6 +882,9 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
const TextureFormat hdrFormat = getHdrFormat(view, needsAlphaChannel); const TextureFormat hdrFormat = getHdrFormat(view, needsAlphaChannel);
utils::slog.e <<"padding=" << !noBufferPadding << " svp=" << svp << utils::io::endl;
utils::slog.e <<"padding=" << !noBufferPadding << " xvp=" << xvp << utils::io::endl;
// the clearFlags and clearColor specified below will only apply when rendering into the // the clearFlags and clearColor specified below will only apply when rendering into the
// temporary color buffer. In particular, they won't apply when rendering into the main // temporary color buffer. In particular, they won't apply when rendering into the main
// swapchain (imported render target above) // swapchain (imported render target above)
@@ -1074,6 +1089,9 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
.format = config.hdrFormat .format = config.hdrFormat
}; };
utils::slog.e <<"color buffer output=" << colorBufferDesc.width <<"x" << colorBufferDesc.height
<< utils::io::endl;
// Set the depth to the number of layers if we're rendering multiview. // Set the depth to the number of layers if we're rendering multiview.
if (isRenderingMultiview) { if (isRenderingMultiview) {
colorBufferDesc.depth = engine.getConfig().stereoscopicEyeCount; colorBufferDesc.depth = engine.getConfig().stereoscopicEyeCount;
@@ -1227,11 +1245,13 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
bloomOptions, vignetteOptions); bloomOptions, vignetteOptions);
// the padded buffer is resolved now // the padded buffer is resolved now
xvp.left = xvp.bottom = 0; xvp.left = xvp.bottom = 0;
utils::slog.e <<"color grading is resolved" << utils::io::endl;
svp = xvp; svp = xvp;
} }
} }
if (hasFXAA) { if (hasFXAA) {
utils::slog.e <<"fxaa=" << xvp << utils::io::endl;
input = ppm.fxaa(fg, input, xvp, colorGradingConfig.ldrFormat, input = ppm.fxaa(fg, input, xvp, colorGradingConfig.ldrFormat,
!hasColorGrading || needsAlphaChannel); !hasColorGrading || needsAlphaChannel);
// the padded buffer is resolved now // the padded buffer is resolved now
@@ -1245,6 +1265,7 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
.width = viewport.width, .height = viewport.height, .width = viewport.width, .height = viewport.height,
.format = colorGradingConfig.ldrFormat }, SamplerMagFilter::LINEAR); .format = colorGradingConfig.ldrFormat }, SamplerMagFilter::LINEAR);
xvp.left = xvp.bottom = 0; xvp.left = xvp.bottom = 0;
utils::slog.e <<"scaled" << utils::io::endl;
svp = xvp; svp = xvp;
} }
} }
@@ -1277,6 +1298,9 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
const bool outputIsSwapChain = const bool outputIsSwapChain =
(input == colorPassOutput) && (viewRenderTarget == mRenderTargetHandle); (input == colorPassOutput) && (viewRenderTarget == mRenderTargetHandle);
if (mightNeedFinalBlit) { if (mightNeedFinalBlit) {
utils::slog.e <<"inside might need final blit xvp!=svp=" << (xvp != svp) <<
" xvp=" << xvp << " svp=" << svp <<
utils::io::endl;
if (blendModeTranslucent || if (blendModeTranslucent ||
xvp != svp || xvp != svp ||
(outputIsSwapChain && (outputIsSwapChain &&
@@ -1285,6 +1309,7 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
hasScreenSpaceRefraction || hasScreenSpaceRefraction ||
ssReflectionsOptions.enabled))) { ssReflectionsOptions.enabled))) {
assert_invariant(!scaled); assert_invariant(!scaled);
utils::slog.e <<"doing final blit " << svp << " " << xvp << utils::io::endl;
input = ppm.blit(fg, blendModeTranslucent, input, xvp, { input = ppm.blit(fg, blendModeTranslucent, input, xvp, {
.width = vp.width, .height = vp.height, .width = vp.width, .height = vp.height,
.format = colorGradingConfig.ldrFormat }, .format = colorGradingConfig.ldrFormat },
@@ -1314,6 +1339,10 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {
fg.forwardResource(fgViewRenderTarget, input); fg.forwardResource(fgViewRenderTarget, input);
utils::slog.e <<"present view=" << &view << " scaled=" << scaled << " mightNeedFinalBlit=" <<
mightNeedFinalBlit <<
utils::io::endl;
fg.present(fgViewRenderTarget); fg.present(fgViewRenderTarget);
fg.compile(); fg.compile();

View File

@@ -562,6 +562,8 @@ int parse(jsmntok_t const* tokens, int i, const char* jsonChunk, Settings* out)
} }
void applySettings(Engine* engine, const ViewSettings& settings, View* dest) { void applySettings(Engine* engine, const ViewSettings& settings, View* dest) {
utils::slog.e << "antialiasing=" << (int) settings.antiAliasing << " view=" << dest
<< utils::io::endl;
dest->setAntiAliasing(settings.antiAliasing); dest->setAntiAliasing(settings.antiAliasing);
dest->setTemporalAntiAliasingOptions(settings.taa); dest->setTemporalAntiAliasingOptions(settings.taa);
dest->setMultiSampleAntiAliasingOptions(settings.msaa); dest->setMultiSampleAntiAliasingOptions(settings.msaa);

View File

@@ -276,7 +276,7 @@ if (NOT ANDROID)
target_link_libraries(hellopbr PRIVATE filameshio suzanne-resources) target_link_libraries(hellopbr PRIVATE filameshio suzanne-resources)
target_link_libraries(image_viewer PRIVATE viewer imageio) target_link_libraries(image_viewer PRIVATE viewer imageio)
target_link_libraries(multiple_windows PRIVATE filameshio suzanne-resources) target_link_libraries(multiple_windows PRIVATE filameshio suzanne-resources)
target_link_libraries(rendertarget PRIVATE filameshio suzanne-resources) target_link_libraries(rendertarget PRIVATE gltf-demo-resources filameshio suzanne-resources gltfio viewer)
target_link_libraries(sample_cloth PRIVATE filameshio) target_link_libraries(sample_cloth PRIVATE filameshio)
target_link_libraries(sample_normal_map PRIVATE filameshio) target_link_libraries(sample_normal_map PRIVATE filameshio)
target_link_libraries(suzanne PRIVATE filameshio suzanne-resources) target_link_libraries(suzanne PRIVATE filameshio suzanne-resources)

View File

@@ -12,6 +12,8 @@ material {
uv0 uv0
], ],
shadingModel : unlit, shadingModel : unlit,
vertexDomain: device,
// vertexDomainDeviceJittered: true,
} }
fragment { fragment {
@@ -20,17 +22,11 @@ fragment {
if (gl_FrontFacing) { if (gl_FrontFacing) {
// Make a lookup into the mirror texture. // Make a lookup into the mirror texture.
vec2 uv = getResolution().zw * gl_FragCoord.xy; // vec2 uv = getResolution().zw * gl_FragCoord.xy;
vec2 uv = getNormalizedViewportCoord().xy;
material.baseColor.rgb = texture(materialParams_albedo, uv).rgb; material.baseColor.rgb = texture(materialParams_albedo, uv).rgb;
} else {
// Add black borders to the mirror. material.baseColor = vec4(1, 0, 0, 1);
vec2 st = getUV0();
float minDist0 = min(st.x, st.y);
float minDist1 = min(1.0 - st.x, 1.0 - st.y);
float minDist = min(minDist0, minDist1);
material.baseColor.rgb *= smoothstep(0.0, 0.1, minDist);
} }
material.baseColor.a = 1.0;
} }
} }

View File

@@ -28,10 +28,17 @@
#include <filament/VertexBuffer.h> #include <filament/VertexBuffer.h>
#include <filament/View.h> #include <filament/View.h>
#include <gltfio/AssetLoader.h>
#include <gltfio/FilamentAsset.h>
#include <gltfio/ResourceLoader.h>
#include <gltfio/TextureProvider.h>
#include <utils/NameComponentManager.h>
#include <utils/EntityManager.h> #include <utils/EntityManager.h>
#include <imgui.h>
#include <filagui/ImGuiExtensions.h>
#include <viewer/ViewerGui.h>
#include <filameshio/MeshReader.h> #include <filameshio/MeshReader.h>
#include <filamentapp/Config.h> #include <filamentapp/Config.h>
#include <filamentapp/FilamentApp.h> #include <filamentapp/FilamentApp.h>
@@ -40,11 +47,16 @@
#include <iostream> #include <iostream>
#include "generated/resources/resources.h" #include "generated/resources/resources.h"
#include "generated/resources/monkey.h" //#include "generated/resources/monkey.h"
#include "generated/resources/gltf_demo.h"
using namespace filament; using namespace filament;
using namespace filamesh; using namespace filamesh;
using namespace filament::math; using namespace filament::math;
using namespace filament::viewer;
using namespace filament::gltfio;
#include <utils/Log.h>
struct Vertex { struct Vertex {
float3 position; float3 position;
@@ -55,8 +67,6 @@ struct App {
utils::Entity lightEntity; utils::Entity lightEntity;
Material* meshMaterial; Material* meshMaterial;
MaterialInstance* meshMatInstance; MaterialInstance* meshMatInstance;
MeshReader::Mesh monkeyMesh;
utils::Entity reflectedMonkey;
mat4f transform; mat4f transform;
Texture* offscreenColorTexture = nullptr; Texture* offscreenColorTexture = nullptr;
@@ -66,12 +76,6 @@ struct App {
Scene* offscreenScene = nullptr; Scene* offscreenScene = nullptr;
Camera* offscreenCamera = nullptr; Camera* offscreenCamera = nullptr;
enum class ReflectionMode {
RENDERABLES,
CAMERA,
};
ReflectionMode mode = ReflectionMode::CAMERA;
Config config; Config config;
utils::Entity quadEntity; utils::Entity quadEntity;
@@ -80,46 +84,22 @@ struct App {
Material* quadMaterial = nullptr; Material* quadMaterial = nullptr;
MaterialInstance* quadMatInstance = nullptr; MaterialInstance* quadMatInstance = nullptr;
float3 quadCenter; MaterialProvider* materials;
float3 quadNormal;
AssetLoader* assetLoader;
ResourceLoader* resourceLoader;
FilamentAsset* asset = nullptr;
utils::NameComponentManager* names;
gltfio::TextureProvider* stbDecoder = nullptr;
gltfio::TextureProvider* ktxDecoder = nullptr;
Engine* engine;
ViewerGui* viewer;
utils::Entity rootTransformEntity;
}; };
static mat4f reflectionMatrix(float4 plane) { static const char* DEFAULT_IBL = "assets/ibl/lightroom_14b";
mat4f m;
m[0][0] = -2 * plane.x * plane.x + 1;
m[0][1] = -2 * plane.x * plane.y;
m[0][2] = -2 * plane.x * plane.z;
m[0][3] = -2 * plane.x * plane.w;
m[1][0] = -2 * plane.x * plane.y;
m[1][1] = -2 * plane.y * plane.y + 1;
m[1][2] = -2 * plane.y * plane.z;
m[1][3] = -2 * plane.y * plane.w;
m[2][0] = -2 * plane.z * plane.x;
m[2][1] = -2 * plane.z * plane.y;
m[2][2] = -2 * plane.z * plane.z + 1;
m[2][3] = -2 * plane.z * plane.w;
m[3][0] = 0;
m[3][1] = 0;
m[3][2] = 0;
m[3][3] = 1;
return transpose(m);
}
static void setReflectionMode(App& app, App::ReflectionMode mode) {
switch (mode) {
case App::ReflectionMode::RENDERABLES:
app.offscreenScene->addEntity(app.reflectedMonkey);
app.offscreenScene->remove(app.monkeyMesh.renderable);
app.offscreenView->setFrontFaceWindingInverted(false);
break;
case App::ReflectionMode::CAMERA:
app.offscreenScene->addEntity(app.monkeyMesh.renderable);
app.offscreenScene->remove(app.reflectedMonkey);
app.offscreenView->setFrontFaceWindingInverted(true);
break;
}
app.mode = mode;
}
static void printUsage(char* name) { static void printUsage(char* name) {
std::string exec_name(utils::Path(name).getName()); std::string exec_name(utils::Path(name).getName());
@@ -172,14 +152,6 @@ static int handleCommandLineArguments(int argc, char* argv[], App* app) {
} }
break; break;
case 'm': case 'm':
if (arg == "camera") {
app->mode = App::ReflectionMode::CAMERA;
} else if (arg == "renderables") {
app->mode = App::ReflectionMode::RENDERABLES;
} else {
std::cerr << "Unrecognized mode. Must be 'camera'|'renderables'.\n";
exit(1);
}
break; break;
} }
} }
@@ -189,126 +161,182 @@ static int handleCommandLineArguments(int argc, char* argv[], App* app) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
App app{}; App app{};
app.config.title = "rendertarget"; app.config.title = "rendertarget";
app.config.iblDirectory = FilamentApp::getRootAssetsPath() + DEFAULT_IBL;
handleCommandLineArguments(argc, argv, &app); handleCommandLineArguments(argc, argv, &app);
auto setup = [&app](Engine* engine, View* view, Scene* scene) { auto gui = [&app](Engine*, View*) {
app.viewer->updateUserInterface();
FilamentApp::get().setSidebarWidth(app.viewer->getSidebarWidth());
};
auto loadResources = [&app] () {
// Load external textures and buffers.
std::string const gltfPath;
ResourceConfiguration configuration = {};
configuration.engine = app.engine;
configuration.gltfPath = gltfPath.c_str();
configuration.normalizeSkinningWeights = true;
app.resourceLoader = new gltfio::ResourceLoader(configuration);
app.stbDecoder = createStbProvider(app.engine);
app.ktxDecoder = createKtx2Provider(app.engine);
app.resourceLoader->addTextureProvider("image/png", app.stbDecoder);
app.resourceLoader->addTextureProvider("image/jpeg", app.stbDecoder);
app.resourceLoader->addTextureProvider("image/ktx2", app.ktxDecoder);
if (!app.resourceLoader->loadResources(app.asset)) {
std::cerr << "Unable to start loading resources" << std::endl;
exit(1);
}
app.asset->getInstance()->recomputeBoundingBoxes();
app.asset->releaseSourceData();
// Enable stencil writes on all material instances.
auto instance = app.asset->getInstance();
const size_t matInstanceCount = instance->getMaterialInstanceCount();
MaterialInstance* const* const instances = instance->getMaterialInstances();
for (int mi = 0; mi < matInstanceCount; mi++) {
instances[mi]->setStencilWrite(true);
instances[mi]->setStencilOpDepthStencilPass(MaterialInstance::StencilOperation::INCR);
}
auto ibl = FilamentApp::get().getIBL();
if (ibl) {
app.viewer->setIndirectLight(ibl->getIndirectLight(), ibl->getSphericalHarmonics());
app.viewer->getSettings().view.fogSettings.fogColorTexture = ibl->getFogTexture();
}
};
auto setup = [&app, loadResources](Engine* engine, View* view, Scene* scene) {
app.engine = engine;
auto& em = utils::EntityManager::get();
auto& tcm = engine->getTransformManager(); auto& tcm = engine->getTransformManager();
auto& rcm = engine->getRenderableManager(); auto& rcm = engine->getRenderableManager();
auto& em = utils::EntityManager::get();
auto vp = view->getViewport(); // Offscreen view
{
// Instantiate offscreen render target.
app.offscreenView = engine->createView();
app.offscreenScene = engine->createScene();
app.offscreenView->setScene(app.offscreenScene);
// Instantiate offscreen render target. app.viewer = new ViewerGui(engine, app.offscreenScene, app.offscreenView, 410);
app.offscreenView = engine->createView(); app.viewer->setUiCallback([&app, scene, view, engine]() {
app.offscreenScene = engine->createScene(); if (ImGui::CollapsingHeader("Debug")) {
app.offscreenView->setScene(app.offscreenScene); auto& debug = engine->getDebugRegistry();
app.offscreenView->setPostProcessingEnabled(false); ImGui::Checkbox("Disable buffer padding",
app.offscreenColorTexture = Texture::Builder() debug.getPropertyAddress<bool>("d.renderer.disable_buffer_padding"));
.width(vp.width).height(vp.height).levels(1) ImGui::Checkbox("Disable sub-passes",
.usage(Texture::Usage::COLOR_ATTACHMENT | Texture::Usage::SAMPLEABLE) debug.getPropertyAddress<bool>("d.renderer.disable_subpasses"));
.format(Texture::InternalFormat::RGBA8).build(*engine); }
app.offscreenDepthTexture = Texture::Builder() });
.width(vp.width).height(vp.height).levels(1) Viewport vp = view->getViewport();
.usage(Texture::Usage::DEPTH_ATTACHMENT) vp.width = vp.width - app.viewer->getSidebarWidth();
.format(Texture::InternalFormat::DEPTH24).build(*engine);
app.offscreenRenderTarget = RenderTarget::Builder()
.texture(RenderTarget::AttachmentPoint::COLOR, app.offscreenColorTexture)
.texture(RenderTarget::AttachmentPoint::DEPTH, app.offscreenDepthTexture)
.build(*engine);
app.offscreenView->setRenderTarget(app.offscreenRenderTarget);
app.offscreenView->setViewport({0, 0, vp.width, vp.height});
app.offscreenCamera = engine->createCamera(em.create());
app.offscreenView->setCamera(app.offscreenCamera);
FilamentApp::get().addOffscreenView(app.offscreenView);
// Position and orient the mirror in an interesting way. app.offscreenColorTexture =
float3 c = app.quadCenter = {-2, 0, -5}; Texture::Builder()
float3 n = app.quadNormal = normalize(float3 {1, 0, 2}); .width(vp.width)
float3 u = normalize(cross(app.quadNormal, float3(0, 1, 0))); .height(vp.height)
float3 v = cross(n, u); .levels(1)
u = 1.5 * u; .usage(Texture::Usage::COLOR_ATTACHMENT | Texture::Usage::SAMPLEABLE)
v = 1.5 * v; .format(Texture::InternalFormat::RGBA8)
static Vertex kQuadVertices[4] = { {{}, {1, 0}}, {{}, {0, 0}}, {{}, {1, 1}}, {{}, {0, 1}} }; .build(*engine);
kQuadVertices[0].position = c - u - v; app.offscreenDepthTexture = Texture::Builder()
kQuadVertices[1].position = c + u - v; .width(vp.width)
kQuadVertices[2].position = c - u + v; .height(vp.height)
kQuadVertices[3].position = c + u + v; .levels(1)
.usage(Texture::Usage::DEPTH_ATTACHMENT)
.format(Texture::InternalFormat::DEPTH32F)
.build(*engine);
app.offscreenRenderTarget = RenderTarget::Builder()
.texture(RenderTarget::AttachmentPoint::COLOR,
app.offscreenColorTexture)
.texture(RenderTarget::AttachmentPoint::DEPTH,
app.offscreenDepthTexture)
.build(*engine);
app.offscreenView->setRenderTarget(app.offscreenRenderTarget);
app.offscreenView->setViewport({0, 0, vp.width, vp.height});
app.offscreenCamera = engine->createCamera(em.create());
app.offscreenView->setCamera(app.offscreenCamera);
FilamentApp::get().addOffscreenView(app.offscreenView);
}
// Create quad vertex buffer. // damaged helmet gltf
static_assert(sizeof(Vertex) == 20, "Strange vertex size."); {
app.quadVb = VertexBuffer::Builder() app.names = new utils::NameComponentManager(em);
.vertexCount(4) app.materials = createJitShaderProvider(engine, true);
.bufferCount(1) app.assetLoader = AssetLoader::create({engine, app.materials, app.names});
.attribute(VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3, 0, 20)
.attribute(VertexAttribute::UV0, 0, VertexBuffer::AttributeType::FLOAT2, 12, 20)
.build(*engine);
app.quadVb->setBufferAt(*engine, 0,
VertexBuffer::BufferDescriptor(kQuadVertices, 80, nullptr));
// Create quad index buffer. app.asset = app.assetLoader->createAsset(GLTF_DEMO_DAMAGEDHELMET_DATA,
static constexpr uint16_t kQuadIndices[6] = { 0, 1, 2, 3, 2, 1 }; GLTF_DEMO_DAMAGEDHELMET_SIZE);
app.quadIb = IndexBuffer::Builder()
.indexCount(6)
.bufferType(IndexBuffer::IndexType::USHORT)
.build(*engine);
app.quadIb->setBuffer(*engine, IndexBuffer::BufferDescriptor(kQuadIndices, 12, nullptr));
// Create quad material and renderable. loadResources();
app.quadMaterial = Material::Builder()
.package(RESOURCES_MIRROR_DATA, RESOURCES_MIRROR_SIZE)
.build(*engine);
app.quadMatInstance = app.quadMaterial->createInstance();
TextureSampler sampler(TextureSampler::MinFilter::LINEAR, TextureSampler::MagFilter::LINEAR);
app.quadMatInstance->setParameter("albedo", app.offscreenColorTexture, sampler);
app.quadEntity = em.create();
RenderableManager::Builder(1)
.boundingBox({{ -1, -1, -1 }, { 1, 1, 1 }})
.material(0, app.quadMatInstance)
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, app.quadVb, app.quadIb, 0, 6)
.culling(false)
.receiveShadows(false)
.castShadows(false)
.build(*engine, app.quadEntity);
scene->addEntity(app.quadEntity);
// Instantiate mesh material. app.offscreenScene->addEntities(app.asset->getLightEntities(),
app.meshMaterial = Material::Builder() app.asset->getLightEntityCount());
.package(RESOURCES_AIDEFAULTMAT_DATA, RESOURCES_AIDEFAULTMAT_SIZE).build(*engine); static constexpr int kNumAvailable = 128;
auto mi = app.meshMatInstance = app.meshMaterial->createInstance(); utils::Entity renderables[kNumAvailable];
mi->setParameter("baseColor", RgbType::LINEAR, {0.8, 1.0, 1.0}); gltfio::FilamentAsset::SceneMask mask;
mi->setParameter("metallic", 0.0f); mask.set(0);
mi->setParameter("roughness", 0.4f); while (size_t numWritten = app.asset->popRenderables(renderables, kNumAvailable)) {
mi->setParameter("reflectance", 0.5f); app.asset->addEntitiesToScene(*app.offscreenScene, renderables, numWritten, mask);
}
// Add monkey into the scene. app.rootTransformEntity = engine->getEntityManager().create();
app.monkeyMesh = MeshReader::loadMeshFromBuffer(engine, MONKEY_SUZANNE_DATA, nullptr, nullptr, mi); tcm.create(app.rootTransformEntity);
auto ti = tcm.getInstance(app.monkeyMesh.renderable); TransformManager::Instance const& root = tcm.getInstance(app.rootTransformEntity);
app.transform = mat4f{ mat3f(1), float3(0, 0, -4) } * tcm.getWorldTransform(ti); tcm.setParent(tcm.getInstance(app.asset->getRoot()), root);
rcm.setCastShadows(rcm.getInstance(app.monkeyMesh.renderable), false); tcm.setTransform(root, mat4::translation(float3(0, 0, -4)));
scene->addEntity(app.monkeyMesh.renderable); }
// Create a reflected monkey, which is used only for App::ReflectionMode::RENDERABLES. // full-screen quad
app.reflectedMonkey = em.create(); {
RenderableManager::Builder(1) static Vertex kQuadVertices[4] = {{{1, 1, 0}, {1, 1}}, {{1, -1, 0}, {1, 0}},
.boundingBox({{ -2, -2, -2 }, { 2, 2, 2 }}) {{-1, -1, 0}, {0, 0}}, {{-1, 1, 0}, {0, 1}}};
.material(0, mi) // Create quad vertex buffer.
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, app.monkeyMesh.vertexBuffer, app.monkeyMesh.indexBuffer) static_assert(sizeof(Vertex) == 20, "Strange vertex size.");
.receiveShadows(true) app.quadVb = VertexBuffer::Builder()
.castShadows(false) .vertexCount(4)
.build(*engine, app.reflectedMonkey); .bufferCount(1)
setReflectionMode(app, app.mode); .attribute(VertexAttribute::POSITION, 0,
VertexBuffer::AttributeType::FLOAT3, 0, 20)
.attribute(VertexAttribute::UV0, 0,
VertexBuffer::AttributeType::FLOAT2, 12, 20)
.build(*engine);
app.quadVb->setBufferAt(*engine, 0,
VertexBuffer::BufferDescriptor(kQuadVertices, 80, nullptr));
// Add light source to both scenes. // Create quad index buffer.
// NOTE: this is slightly wrong when the reflection mode is RENDERABLES. static constexpr uint16_t kQuadIndices[6] = {0, 2, 1, 2, 0, 3};
app.lightEntity = em.create(); app.quadIb = IndexBuffer::Builder()
LightManager::Builder(LightManager::Type::SUN) .indexCount(6)
.color(Color::toLinear<ACCURATE>(sRGBColor(0.98f, 0.92f, 0.89f))) .bufferType(IndexBuffer::IndexType::USHORT)
.intensity(110000) .build(*engine);
.direction({ 0.7, -1, -0.8 }) app.quadIb->setBuffer(*engine,
.sunAngularRadius(1.9f) IndexBuffer::BufferDescriptor(kQuadIndices, 12, nullptr));
.castShadows(false)
.build(*engine, app.lightEntity); // Create quad material and renderable.
scene->addEntity(app.lightEntity); // NOTE: this material is VertexDomain=device
app.offscreenScene->addEntity(app.lightEntity); app.quadMaterial = Material::Builder()
.package(RESOURCES_MIRROR_DATA, RESOURCES_MIRROR_SIZE)
.build(*engine);
app.quadMatInstance = app.quadMaterial->createInstance();
TextureSampler sampler(TextureSampler::MinFilter::LINEAR,
TextureSampler::MagFilter::LINEAR);
app.quadMatInstance->setParameter("albedo", app.offscreenColorTexture, sampler);
app.quadEntity = em.create();
RenderableManager::Builder(1)
.boundingBox({{-1, -1, -1}, {1, 1, 1}})
.material(0, app.quadMatInstance)
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, app.quadVb,
app.quadIb, 0, 6)
.culling(false)
.receiveShadows(false)
.castShadows(false)
.build(*engine, app.quadEntity);
scene->addEntity(app.quadEntity);
}
}; };
auto cleanup = [&app](Engine* engine, View*, Scene*) { auto cleanup = [&app](Engine* engine, View*, Scene*) {
@@ -318,14 +346,10 @@ int main(int argc, char** argv) {
engine->destroyCameraComponent(camera); engine->destroyCameraComponent(camera);
em.destroy(camera); em.destroy(camera);
engine->destroy(app.reflectedMonkey);
engine->destroy(app.lightEntity); engine->destroy(app.lightEntity);
engine->destroy(app.quadEntity); engine->destroy(app.quadEntity);
engine->destroy(app.meshMatInstance); engine->destroy(app.meshMatInstance);
engine->destroy(app.meshMaterial); engine->destroy(app.meshMaterial);
engine->destroy(app.monkeyMesh.renderable);
engine->destroy(app.monkeyMesh.vertexBuffer);
engine->destroy(app.monkeyMesh.indexBuffer);
engine->destroy(app.offscreenColorTexture); engine->destroy(app.offscreenColorTexture);
engine->destroy(app.offscreenDepthTexture); engine->destroy(app.offscreenDepthTexture);
engine->destroy(app.offscreenRenderTarget); engine->destroy(app.offscreenRenderTarget);
@@ -337,43 +361,22 @@ int main(int argc, char** argv) {
engine->destroy(app.quadMaterial); engine->destroy(app.quadMaterial);
}; };
auto preRender = [&app](Engine*, View*, Scene*, Renderer* renderer) { auto preRender = [&app](Engine* engine, View* view, Scene*, Renderer* renderer) {
renderer->setClearOptions({.clearColor = {0.1,0.2,0.4,1.0}, .clear = true}); renderer->setClearOptions({.clearColor = {0.1,0.2,0.4,1.0}, .clear = true});
};
FilamentApp::get().animate([&app](Engine* engine, View* view, double now) { utils::slog.e <<"offscreen view=" << app.offscreenView << " primary view=" <<
auto& tcm = engine->getTransformManager(); view << utils::io::endl;
// Animate the monkey by spinning and sliding back and forth along Z.
auto ti = tcm.getInstance(app.monkeyMesh.renderable);
mat4f xlate = mat4f::translation(float3(0, 0, 0.5 + sin(now)));
mat4f xform = app.transform * xlate * mat4f::rotation(now, float3{ 0, 1, 0 });
tcm.setTransform(ti, xform);
// Generate a reflection matrix from the plane equation Ax + By + Cz + D = 0.
const float3 planeNormal = app.quadNormal;
const float4 planeEquation(planeNormal, -dot(planeNormal, app.quadCenter));
const mat4f reflection = reflectionMatrix(planeEquation);
// Apply the reflection matrix to either the renderable or the camera, depending on mode.
Camera const& camera = view->getCamera(); Camera const& camera = view->getCamera();
const auto model = camera.getModelMatrix();
const auto renderingProjection = camera.getProjectionMatrix(); const auto renderingProjection = camera.getProjectionMatrix();
const auto cullingProjection = camera.getCullingProjectionMatrix(); const auto cullingProjection = camera.getCullingProjectionMatrix();
app.offscreenCamera->setCustomProjection(renderingProjection, cullingProjection, app.offscreenCamera->setCustomProjection(renderingProjection, cullingProjection,
camera.getNear(), camera.getCullingFar()); camera.getNear(), camera.getCullingFar());
switch (app.mode) { const auto model = camera.getModelMatrix();
case App::ReflectionMode::RENDERABLES: app.offscreenCamera->setModelMatrix(model);
tcm.setTransform(tcm.getInstance(app.reflectedMonkey), reflection * xform); };
app.offscreenCamera->setModelMatrix(model);
break;
case App::ReflectionMode::CAMERA:
app.offscreenCamera->setModelMatrix(reflection * model);
break;
}
});
FilamentApp::get().run(app.config, setup, cleanup, FilamentApp::ImGuiCallback(), preRender); FilamentApp::get().run(app.config, setup, cleanup, gui, preRender);
return 0; return 0;
} }