| 1 |
/* |
| 2 |
* Copyright (C) 2021 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 |
#include "filament-iblprefilter/IBLPrefilterContext.h" |
| 18 |
|
| 19 |
#include <filament/Engine.h> |
| 20 |
#include <filament/IndexBuffer.h> |
| 21 |
#include <filament/Material.h> |
| 22 |
#include <filament/RenderTarget.h> |
| 23 |
#include <filament/RenderableManager.h> |
| 24 |
#include <filament/Renderer.h> |
| 25 |
#include <filament/Scene.h> |
| 26 |
#include <filament/Texture.h> |
| 27 |
#include <filament/TextureSampler.h> |
| 28 |
#include <filament/VertexBuffer.h> |
| 29 |
#include <filament/View.h> |
| 30 |
#include <filament/Viewport.h> |
| 31 |
|
| 32 |
#include <utils/Panic.h> |
| 33 |
#include <utils/EntityManager.h> |
| 34 |
#include <utils/Systrace.h> |
| 35 |
|
| 36 |
#include <math/mat3.h> |
| 37 |
#include <math/vec3.h> |
| 38 |
|
| 39 |
#include "generated/resources/iblprefilter_materials.h" |
| 40 |
|
| 41 |
using namespace filament::math; |
| 42 |
using namespace filament; |
| 43 |
|
| 44 |
constexpr static float4 sFullScreenTriangleVertices[3] = { |
| 45 |
{ -1.0f, -1.0f, 1.0f, 1.0f }, |
| 46 |
{ 3.0f, -1.0f, 1.0f, 1.0f }, |
| 47 |
{ -1.0f, 3.0f, 1.0f, 1.0f } |
| 48 |
}; |
| 49 |
|
| 50 |
constexpr static const uint16_t sFullScreenTriangleIndices[3] = { 0, 1, 2 }; |
| 51 |
|
| 52 |
static float lodToPerceptualRoughness(float lod) noexcept { |
| 53 |
// Inverse perceptualRoughness-to-LOD mapping: |
| 54 |
// The LOD-to-perceptualRoughness mapping is a quadratic fit for |
| 55 |
// log2(perceptualRoughness)+iblMaxMipLevel when iblMaxMipLevel is 4. |
| 56 |
// We found empirically that this mapping works very well for a 256 cubemap with 5 levels used, |
| 57 |
// but also scales well for other iblMaxMipLevel values. |
| 58 |
const float a = 2.0f; |
| 59 |
const float b = -1.0f; |
| 60 |
return (lod != 0) |
| 61 |
? saturate((std::sqrt(a * a + 4.0f * b * lod) - a) / (2.0f * b)) |
| 62 |
: 0.0f; |
| 63 |
} |
| 64 |
|
| 65 |
template<typename T> |
| 66 |
static inline constexpr T log4(T x) { |
| 67 |
return std::log2(x) * T(0.5); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
IBLPrefilterContext::IBLPrefilterContext(Engine& engine) |
| 72 |
: mEngine(engine) { |
| 73 |
utils::EntityManager& em = utils::EntityManager::get(); |
| 74 |
mCameraEntity = em.create(); |
| 75 |
mFullScreenQuadEntity = em.create(); |
| 76 |
|
| 77 |
mIntegrationMaterial = Material::Builder().package( |
| 78 |
IBLPREFILTER_MATERIALS_IBLPREFILTER_DATA, |
| 79 |
IBLPREFILTER_MATERIALS_IBLPREFILTER_SIZE).build(engine); |
| 80 |
|
| 81 |
mVertexBuffer = VertexBuffer::Builder() |
| 82 |
.vertexCount(3) |
| 83 |
.bufferCount(1) |
| 84 |
.attribute(VertexAttribute::POSITION, 0, |
| 85 |
VertexBuffer::AttributeType::FLOAT4, 0) |
| 86 |
.build(engine); |
| 87 |
|
| 88 |
mIndexBuffer = IndexBuffer::Builder() |
| 89 |
.indexCount(3) |
| 90 |
.bufferType(IndexBuffer::IndexType::USHORT) |
| 91 |
.build(engine); |
| 92 |
|
| 93 |
|
| 94 |
mVertexBuffer->setBufferAt(engine, 0, |
| 95 |
{ sFullScreenTriangleVertices, sizeof(sFullScreenTriangleVertices) }); |
| 96 |
|
| 97 |
mIndexBuffer->setBuffer(engine, |
| 98 |
{ sFullScreenTriangleIndices, sizeof(sFullScreenTriangleIndices) }); |
| 99 |
|
| 100 |
|
| 101 |
RenderableManager::Builder(1) |
| 102 |
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, mVertexBuffer, mIndexBuffer) |
| 103 |
.material(0, mIntegrationMaterial->getDefaultInstance()) |
| 104 |
.culling(false) |
| 105 |
.castShadows(false) |
| 106 |
.receiveShadows(false) |
| 107 |
.build(engine, mFullScreenQuadEntity); |
| 108 |
|
| 109 |
mView = engine.createView(); |
| 110 |
mScene = engine.createScene(); |
| 111 |
mRenderer = engine.createRenderer(); |
| 112 |
mCamera = engine.createCamera(mCameraEntity); |
| 113 |
|
| 114 |
mScene->addEntity(mFullScreenQuadEntity); |
| 115 |
|
| 116 |
View* const view = mView; |
| 117 |
view->setCamera(mCamera); |
| 118 |
view->setScene(mScene); |
| 119 |
view->setScreenSpaceRefractionEnabled(false); |
| 120 |
view->setShadowingEnabled(false); |
| 121 |
view->setPostProcessingEnabled(false); |
| 122 |
view->setFrustumCullingEnabled(false); |
| 123 |
} |
| 124 |
|
| 125 |
IBLPrefilterContext::~IBLPrefilterContext() noexcept { |
| 126 |
utils::EntityManager& em = utils::EntityManager::get(); |
| 127 |
auto& engine = mEngine; |
| 128 |
engine.destroy(mView); |
| 129 |
engine.destroy(mScene); |
| 130 |
engine.destroy(mRenderer); |
| 131 |
engine.destroy(mVertexBuffer); |
| 132 |
engine.destroy(mIndexBuffer); |
| 133 |
engine.destroy(mIntegrationMaterial); |
| 134 |
engine.destroy(mFullScreenQuadEntity); |
| 135 |
engine.destroyCameraComponent(mCameraEntity); |
| 136 |
em.destroy(mFullScreenQuadEntity); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
IBLPrefilterContext::IBLPrefilterContext(IBLPrefilterContext&& rhs) noexcept |
| 141 |
: mEngine(rhs.mEngine) { |
| 142 |
this->operator=(std::move(rhs)); |
| 143 |
} |
| 144 |
|
| 145 |
IBLPrefilterContext& IBLPrefilterContext::operator=(IBLPrefilterContext&& rhs) { |
| 146 |
using std::swap; |
| 147 |
if (this != & rhs) { |
| 148 |
swap(mRenderer, rhs.mRenderer); |
| 149 |
swap(mScene, rhs.mScene); |
| 150 |
swap(mVertexBuffer, rhs.mVertexBuffer); |
| 151 |
swap(mIndexBuffer, rhs.mIndexBuffer); |
| 152 |
swap(mCamera, rhs.mCamera); |
| 153 |
swap(mFullScreenQuadEntity, rhs.mFullScreenQuadEntity); |
| 154 |
swap(mCameraEntity, rhs.mCameraEntity); |
| 155 |
swap(mView, rhs.mView); |
| 156 |
swap(mIntegrationMaterial, rhs.mIntegrationMaterial); |
| 157 |
} |
| 158 |
return *this; |
| 159 |
} |
| 160 |
|
| 161 |
// ------------------------------------------------------------------------------------------------ |
| 162 |
|
| 163 |
IBLPrefilterContext::EquirectangularToCubemap::EquirectangularToCubemap( |
| 164 |
IBLPrefilterContext& context) : mContext(context) { |
| 165 |
Engine& engine = mContext.mEngine; |
| 166 |
mEquirectMaterial = Material::Builder().package( |
| 167 |
IBLPREFILTER_MATERIALS_EQUIRECTTOCUBE_DATA, |
| 168 |
IBLPREFILTER_MATERIALS_EQUIRECTTOCUBE_SIZE).build(engine); |
| 169 |
} |
| 170 |
|
| 171 |
IBLPrefilterContext::EquirectangularToCubemap::~EquirectangularToCubemap() noexcept { |
| 172 |
Engine& engine = mContext.mEngine; |
| 173 |
engine.destroy(mEquirectMaterial); |
| 174 |
} |
| 175 |
|
| 176 |
IBLPrefilterContext::EquirectangularToCubemap::EquirectangularToCubemap( |
| 177 |
IBLPrefilterContext::EquirectangularToCubemap&& rhs) noexcept |
| 178 |
: mContext(rhs.mContext) { |
| 179 |
using std::swap; |
| 180 |
swap(mEquirectMaterial, rhs.mEquirectMaterial); |
| 181 |
} |
| 182 |
|
| 183 |
IBLPrefilterContext::EquirectangularToCubemap& |
| 184 |
IBLPrefilterContext::EquirectangularToCubemap::operator=( |
| 185 |
IBLPrefilterContext::EquirectangularToCubemap&& rhs) { |
| 186 |
using std::swap; |
| 187 |
if (this != &rhs) { |
| 188 |
swap(mEquirectMaterial, rhs.mEquirectMaterial); |
| 189 |
} |
| 190 |
return *this; |
| 191 |
} |
| 192 |
|
| 193 |
Texture* IBLPrefilterContext::EquirectangularToCubemap::operator()( |
| 194 |
Texture const* equirect, Texture* outCube) { |
| 195 |
SYSTRACE_CALL(); |
| 196 |
using namespace backend; |
| 197 |
|
| 198 |
const TextureCubemapFace faces[2][3] = { |
| 199 |
{ TextureCubemapFace::POSITIVE_X, TextureCubemapFace::POSITIVE_Y, TextureCubemapFace::POSITIVE_Z }, |
| 200 |
{ TextureCubemapFace::NEGATIVE_X, TextureCubemapFace::NEGATIVE_Y, TextureCubemapFace::NEGATIVE_Z } |
| 201 |
}; |
| 202 |
|
| 203 |
Engine& engine = mContext.mEngine; |
| 204 |
View* const view = mContext.mView; |
| 205 |
Renderer* const renderer = mContext.mRenderer; |
| 206 |
MaterialInstance* const mi = mEquirectMaterial->getDefaultInstance(); |
| 207 |
|
| 208 |
ASSERT_PRECONDITION(equirect != nullptr, "equirect is null!"); |
| 209 |
|
| 210 |
ASSERT_PRECONDITION(equirect->getTarget() == Texture::Sampler::SAMPLER_2D, |
| 211 |
"equirect must be a 2D texture."); |
| 212 |
|
| 213 |
UTILS_UNUSED_IN_RELEASE |
| 214 |
const uint8_t maxLevelCount = uint8_t(std::log2(equirect->getWidth()) + 0.5f) + 1u; |
| 215 |
|
| 216 |
ASSERT_PRECONDITION(equirect->getLevels() == maxLevelCount, |
| 217 |
"equirect must have %u mipmap levels allocated.", +maxLevelCount); |
| 218 |
|
| 219 |
if (outCube == nullptr) { |
| 220 |
outCube = Texture::Builder() |
| 221 |
.sampler(Texture::Sampler::SAMPLER_CUBEMAP) |
| 222 |
.format(Texture::InternalFormat::R11F_G11F_B10F) |
| 223 |
.usage(Texture::Usage::COLOR_ATTACHMENT | Texture::Usage::SAMPLEABLE) |
| 224 |
.width(256).height(256).levels(0xFF) |
| 225 |
.build(engine); |
| 226 |
} |
| 227 |
|
| 228 |
ASSERT_PRECONDITION(outCube->getTarget() == Texture::Sampler::SAMPLER_CUBEMAP, |
| 229 |
"outCube must be a Cubemap texture."); |
| 230 |
|
| 231 |
const uint32_t dim = outCube->getWidth(); |
| 232 |
|
| 233 |
RenderableManager& rcm = engine.getRenderableManager(); |
| 234 |
rcm.setMaterialInstanceAt( |
| 235 |
rcm.getInstance(mContext.mFullScreenQuadEntity), 0, mi); |
| 236 |
|
| 237 |
TextureSampler environmentSampler; |
| 238 |
environmentSampler.setMagFilter(SamplerMagFilter::LINEAR); |
| 239 |
environmentSampler.setMinFilter(SamplerMinFilter::LINEAR_MIPMAP_LINEAR); |
| 240 |
environmentSampler.setAnisotropy(16.0f); // maybe make this an option |
| 241 |
|
| 242 |
mi->setParameter("equirect", equirect, environmentSampler); |
| 243 |
|
| 244 |
// We need mipmaps because we're sampling down |
| 245 |
equirect->generateMipmaps(engine); |
| 246 |
|
| 247 |
view->setViewport({ 0, 0, dim, dim }); |
| 248 |
|
| 249 |
RenderTarget::Builder builder; |
| 250 |
builder.texture(RenderTarget::AttachmentPoint::COLOR0, outCube) |
| 251 |
.texture(RenderTarget::AttachmentPoint::COLOR1, outCube) |
| 252 |
.texture(RenderTarget::AttachmentPoint::COLOR2, outCube); |
| 253 |
|
| 254 |
for (size_t i = 0; i < 2; i++) { |
| 255 |
mi->setParameter("side", i == 0 ? 1.0f : -1.0f); |
| 256 |
|
| 257 |
builder.face(RenderTarget::AttachmentPoint::COLOR0, faces[i][0]) |
| 258 |
.face(RenderTarget::AttachmentPoint::COLOR1, faces[i][1]) |
| 259 |
.face(RenderTarget::AttachmentPoint::COLOR2, faces[i][2]); |
| 260 |
|
| 261 |
RenderTarget* const rt = builder.build(engine); |
| 262 |
view->setRenderTarget(rt); |
| 263 |
renderer->renderStandaloneView(view); |
| 264 |
engine.destroy(rt); |
| 265 |
} |
| 266 |
|
| 267 |
return outCube; |
| 268 |
} |
| 269 |
// ------------------------------------------------------------------------------------------------ |
| 270 |
|
| 271 |
IBLPrefilterContext::SpecularFilter::SpecularFilter(IBLPrefilterContext& context, Config config) |
| 272 |
: mContext(context) { |
| 273 |
SYSTRACE_CALL(); |
| 274 |
using namespace backend; |
| 275 |
|
| 276 |
Engine& engine = mContext.mEngine; |
| 277 |
View* const view = mContext.mView; |
| 278 |
Renderer* const renderer = mContext.mRenderer; |
| 279 |
|
| 280 |
|
| 281 |
mSampleCount = std::min(config.sampleCount, uint16_t(2048)); |
| 282 |
mLevelCount = std::max(config.levelCount, uint8_t(1u)); |
| 283 |
|
| 284 |
mKernelMaterial = Material::Builder().package( |
| 285 |
IBLPREFILTER_MATERIALS_GENERATEKERNEL_DATA, |
| 286 |
IBLPREFILTER_MATERIALS_GENERATEKERNEL_SIZE).build(engine); |
| 287 |
|
| 288 |
|
| 289 |
// { L.x, L.y, L.z, lod } |
| 290 |
mKernelTexture = Texture::Builder() |
| 291 |
.sampler(Texture::Sampler::SAMPLER_2D) |
| 292 |
.format(Texture::InternalFormat::RGBA16F) |
| 293 |
.usage(Texture::Usage::SAMPLEABLE | Texture::Usage::COLOR_ATTACHMENT) |
| 294 |
.width(mLevelCount) |
| 295 |
.height(mSampleCount) |
| 296 |
.build(engine); |
| 297 |
|
| 298 |
MaterialInstance* const mi = mKernelMaterial->getDefaultInstance(); |
| 299 |
mi->setParameter("size", uint2{ mLevelCount, mSampleCount }); |
| 300 |
mi->setParameter("sampleCount", float(mSampleCount)); |
| 301 |
mi->setParameter("oneOverLevelsMinusOne", 1.0f / (mLevelCount - 1.0f)); |
| 302 |
|
| 303 |
RenderableManager& rcm = engine.getRenderableManager(); |
| 304 |
rcm.setMaterialInstanceAt( |
| 305 |
rcm.getInstance(mContext.mFullScreenQuadEntity), 0, mi); |
| 306 |
|
| 307 |
RenderTarget* const rt = RenderTarget::Builder() |
| 308 |
.texture(RenderTarget::AttachmentPoint::COLOR0, mKernelTexture) |
| 309 |
.build(engine); |
| 310 |
|
| 311 |
view->setRenderTarget(rt); |
| 312 |
view->setViewport({ 0, 0, mLevelCount, mSampleCount }); |
| 313 |
|
| 314 |
renderer->renderStandaloneView(view); |
| 315 |
|
| 316 |
engine.destroy(rt); |
| 317 |
} |
| 318 |
|
| 319 |
UTILS_NOINLINE |
| 320 |
IBLPrefilterContext::SpecularFilter::SpecularFilter(IBLPrefilterContext& context) |
| 321 |
: SpecularFilter(context, {}) { |
| 322 |
} |
| 323 |
|
| 324 |
IBLPrefilterContext::SpecularFilter::~SpecularFilter() noexcept { |
| 325 |
Engine& engine = mContext.mEngine; |
| 326 |
engine.destroy(mKernelTexture); |
| 327 |
engine.destroy(mKernelMaterial); |
| 328 |
} |
| 329 |
|
| 330 |
IBLPrefilterContext::SpecularFilter::SpecularFilter(SpecularFilter&& rhs) noexcept |
| 331 |
: mContext(rhs.mContext) { |
| 332 |
this->operator=(std::move(rhs)); |
| 333 |
} |
| 334 |
|
| 335 |
IBLPrefilterContext::SpecularFilter& IBLPrefilterContext::SpecularFilter::operator=(SpecularFilter&& rhs) { |
| 336 |
using std::swap; |
| 337 |
if (this != & rhs) { |
| 338 |
swap(mKernelMaterial, rhs.mKernelMaterial); |
| 339 |
swap(mKernelTexture, rhs.mKernelTexture); |
| 340 |
mSampleCount = rhs.mSampleCount; |
| 341 |
mLevelCount = rhs.mLevelCount; |
| 342 |
} |
| 343 |
return *this; |
| 344 |
} |
| 345 |
|
| 346 |
Texture* IBLPrefilterContext::SpecularFilter::createReflectionsTexture() { |
| 347 |
Engine& engine = mContext.mEngine; |
| 348 |
|
| 349 |
const uint8_t levels = mLevelCount; |
| 350 |
|
| 351 |
// default texture is 256 or larger to accommodate the level count requested |
| 352 |
const uint32_t dim = std::max(256u, 1u << (levels - 1u)); |
| 353 |
|
| 354 |
Texture* const outCubemap = Texture::Builder() |
| 355 |
.sampler(Texture::Sampler::SAMPLER_CUBEMAP) |
| 356 |
.format(Texture::InternalFormat::R11F_G11F_B10F) |
| 357 |
.usage(Texture::Usage::COLOR_ATTACHMENT | Texture::Usage::SAMPLEABLE) |
| 358 |
.width(dim).height(dim).levels(levels) |
| 359 |
.build(engine); |
| 360 |
|
| 361 |
return outCubemap; |
| 362 |
} |
| 363 |
|
| 364 |
UTILS_NOINLINE |
| 365 |
Texture* IBLPrefilterContext::SpecularFilter::operator()( |
| 366 |
Texture const* environmentCubemap, Texture* outReflectionsTexture) { |
| 367 |
return operator()({}, environmentCubemap, outReflectionsTexture); |
| 368 |
} |
| 369 |
|
| 370 |
Texture* IBLPrefilterContext::SpecularFilter::operator()( |
| 371 |
IBLPrefilterContext::SpecularFilter::Options options, |
| 372 |
Texture const* environmentCubemap, Texture* outReflectionsTexture) { |
| 373 |
|
| 374 |
SYSTRACE_CALL(); |
| 375 |
using namespace backend; |
| 376 |
|
| 377 |
ASSERT_PRECONDITION(environmentCubemap != nullptr, "environmentCubemap is null!"); |
| 378 |
|
| 379 |
ASSERT_PRECONDITION(environmentCubemap->getTarget() == Texture::Sampler::SAMPLER_CUBEMAP, |
| 380 |
"environmentCubemap must be a cubemap."); |
| 381 |
|
| 382 |
UTILS_UNUSED_IN_RELEASE |
| 383 |
const uint8_t maxLevelCount = uint8_t(std::log2(environmentCubemap->getWidth()) + 0.5f) + 1u; |
| 384 |
|
| 385 |
ASSERT_PRECONDITION(environmentCubemap->getLevels() == maxLevelCount, |
| 386 |
"environmentCubemap must have %u mipmap levels allocated.", +maxLevelCount); |
| 387 |
|
| 388 |
if (outReflectionsTexture == nullptr) { |
| 389 |
outReflectionsTexture = createReflectionsTexture(); |
| 390 |
} |
| 391 |
|
| 392 |
ASSERT_PRECONDITION(outReflectionsTexture->getTarget() == Texture::Sampler::SAMPLER_CUBEMAP, |
| 393 |
"outReflectionsTexture must be a cubemap."); |
| 394 |
|
| 395 |
ASSERT_PRECONDITION(mLevelCount <= outReflectionsTexture->getLevels(), |
| 396 |
"outReflectionsTexture has %u levels but %u are requested.", |
| 397 |
+outReflectionsTexture->getLevels(), +mLevelCount); |
| 398 |
|
| 399 |
const TextureCubemapFace faces[2][3] = { |
| 400 |
{ TextureCubemapFace::POSITIVE_X, TextureCubemapFace::POSITIVE_Y, TextureCubemapFace::POSITIVE_Z }, |
| 401 |
{ TextureCubemapFace::NEGATIVE_X, TextureCubemapFace::NEGATIVE_Y, TextureCubemapFace::NEGATIVE_Z } |
| 402 |
}; |
| 403 |
|
| 404 |
Engine& engine = mContext.mEngine; |
| 405 |
View* const view = mContext.mView; |
| 406 |
Renderer* const renderer = mContext.mRenderer; |
| 407 |
MaterialInstance* const mi = mContext.mIntegrationMaterial->getDefaultInstance(); |
| 408 |
|
| 409 |
RenderableManager& rcm = engine.getRenderableManager(); |
| 410 |
rcm.setMaterialInstanceAt( |
| 411 |
rcm.getInstance(mContext.mFullScreenQuadEntity), 0, mi); |
| 412 |
|
| 413 |
const uint32_t sampleCount = mSampleCount; |
| 414 |
const float linear = options.hdrLinear; |
| 415 |
const float compress = options.hdrMax; |
| 416 |
const uint8_t levels = outReflectionsTexture->getLevels(); |
| 417 |
uint32_t dim = outReflectionsTexture->getWidth(); |
| 418 |
const float omegaP = (4.0f * f::PI) / float(6 * dim * dim); |
| 419 |
|
| 420 |
TextureSampler environmentSampler; |
| 421 |
environmentSampler.setMagFilter(SamplerMagFilter::LINEAR); |
| 422 |
environmentSampler.setMinFilter(SamplerMinFilter::LINEAR_MIPMAP_LINEAR); |
| 423 |
|
| 424 |
mi->setParameter("environment", environmentCubemap, environmentSampler); |
| 425 |
mi->setParameter("kernel", mKernelTexture, TextureSampler{ SamplerMagFilter::NEAREST }); |
| 426 |
mi->setParameter("compress", float2{ linear, compress }); |
| 427 |
mi->setParameter("lodOffset", options.lodOffset - log4(omegaP)); |
| 428 |
|
| 429 |
if (options.generateMipmap) { |
| 430 |
// We need mipmaps for prefiltering |
| 431 |
environmentCubemap->generateMipmaps(engine); |
| 432 |
} |
| 433 |
|
| 434 |
RenderTarget::Builder builder; |
| 435 |
builder.texture(RenderTarget::AttachmentPoint::COLOR0, outReflectionsTexture) |
| 436 |
.texture(RenderTarget::AttachmentPoint::COLOR1, outReflectionsTexture) |
| 437 |
.texture(RenderTarget::AttachmentPoint::COLOR2, outReflectionsTexture); |
| 438 |
|
| 439 |
for (size_t lod = 0; lod < levels; lod++) { |
| 440 |
SYSTRACE_NAME("executeFilterLOD"); |
| 441 |
|
| 442 |
mi->setParameter("sampleCount", uint32_t(lod == 0 ? 1u : sampleCount)); |
| 443 |
mi->setParameter("attachmentLevel", uint32_t(lod)); |
| 444 |
|
| 445 |
if (lod == levels - 1) { |
| 446 |
// this is the last lod, use a more agressive filtering because this level is also |
| 447 |
// used for the diffuse brdf by filament, and we need it to be very smooth. |
| 448 |
// So we set the lod offset to at least 2. |
| 449 |
mi->setParameter("lodOffset", std::max(2.0f, options.lodOffset) - log4(omegaP)); |
| 450 |
} |
| 451 |
|
| 452 |
builder.mipLevel(RenderTarget::AttachmentPoint::COLOR0, lod) |
| 453 |
.mipLevel(RenderTarget::AttachmentPoint::COLOR1, lod) |
| 454 |
.mipLevel(RenderTarget::AttachmentPoint::COLOR2, lod); |
| 455 |
|
| 456 |
view->setViewport({ 0, 0, dim, dim }); |
| 457 |
|
| 458 |
for (size_t i = 0; i < 2; i++) { |
| 459 |
mi->setParameter("side", i == 0 ? 1.0f : -1.0f); |
| 460 |
|
| 461 |
builder.face(RenderTarget::AttachmentPoint::COLOR0, faces[i][0]) |
| 462 |
.face(RenderTarget::AttachmentPoint::COLOR1, faces[i][1]) |
| 463 |
.face(RenderTarget::AttachmentPoint::COLOR2, faces[i][2]); |
| 464 |
|
| 465 |
RenderTarget* const rt = builder.build(engine); |
| 466 |
view->setRenderTarget(rt); |
| 467 |
renderer->renderStandaloneView(view); |
| 468 |
engine.destroy(rt); |
| 469 |
} |
| 470 |
|
| 471 |
dim >>= 1; |
| 472 |
} |
| 473 |
|
| 474 |
return outReflectionsTexture; |
| 475 |
} |