Skip to content

Commit 0e99ad7

Browse files
committed
Multiple bugfixes in mitsuba loader, material compiler and the example
1 parent 506b800 commit 0e99ad7

File tree

7 files changed

+68
-29
lines changed

7 files changed

+68
-29
lines changed

examples_tests/18.MitsubaLoader/main.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,23 @@ int main()
256256
params.WindowSize.Width = film.width;
257257
params.WindowSize.Height = film.height;
258258
}
259+
else return 1; // no cameras
260+
261+
const auto& sensor = globalMeta->sensors.front(); //always choose frist one
262+
auto isOkSensorType = [](const ext::MitsubaLoader::CElementSensor& sensor) -> bool {
263+
return sensor.type == ext::MitsubaLoader::CElementSensor::Type::PERSPECTIVE || sensor.type == ext::MitsubaLoader::CElementSensor::Type::THINLENS;
264+
};
265+
266+
if (!isOkSensorType(sensor))
267+
return 1;
268+
269+
bool leftHandedCamera = false;
270+
{
271+
auto relativeTransform = sensor.transform.matrix.extractSub3x4();
272+
if (relativeTransform.getPseudoDeterminant().x < 0.f)
273+
leftHandedCamera = true;
274+
}
275+
259276
params.DriverType = video::EDT_OPENGL;
260277
auto device = createDeviceEx(params);
261278

@@ -309,6 +326,7 @@ int main()
309326
//gather all meshes into core::vector and modify their pipelines
310327
core::vector<core::smart_refctd_ptr<asset::ICPUMesh>> cpumeshes;
311328
cpumeshes.reserve(meshes.getSize());
329+
uint32_t cc = cpumeshes.capacity();
312330
for (auto it = meshes.getContents().begin(); it != meshes.getContents().end(); ++it)
313331
{
314332
cpumeshes.push_back(core::smart_refctd_ptr_static_cast<asset::ICPUMesh>(std::move(*it)));
@@ -414,6 +432,8 @@ int main()
414432
modifiedShaders.insert({ core::smart_refctd_ptr<asset::ICPUSpecializedShader>(fs),newfs });
415433
pipeline->setShaderAtStage(asset::ICPUSpecializedShader::ESS_FRAGMENT, newfs.get());
416434
}
435+
// invert what is recognized as frontface in case of RH camera
436+
pipeline->getRasterizationParams().frontFaceIsCCW = !leftHandedCamera;
417437
modifiedPipelines.insert(pipeline);
418438
}
419439
}
@@ -438,7 +458,6 @@ int main()
438458
}
439459
}
440460

441-
//auto gpuVT = core::make_smart_refctd_ptr<video::IGPUVirtualTexture>(driver, globalMeta->VT.get());
442461
auto gpuds0 = driver->getGPUObjectsFromAssets(&cpuds0.get(), &cpuds0.get()+1)->front();
443462

444463
auto gpuds1layout = driver->getGPUObjectsFromAssets(&ds1layout, &ds1layout+1)->front();
@@ -568,28 +587,21 @@ int main()
568587
scene::ICameraSceneNode* camera = nullptr;
569588
core::recti viewport(core::position2di(0,0), core::position2di(params.WindowSize.Width,params.WindowSize.Height));
570589

571-
auto isOkSensorType = [](const ext::MitsubaLoader::CElementSensor& sensor) -> bool {
572-
return sensor.type==ext::MitsubaLoader::CElementSensor::Type::PERSPECTIVE || sensor.type==ext::MitsubaLoader::CElementSensor::Type::THINLENS;
573-
};
574590
//#define TESTING
575591
#ifdef TESTING
576592
if (0)
577593
#else
578594
if (globalMeta->sensors.size() && isOkSensorType(globalMeta->sensors.front()))
579595
#endif
580596
{
581-
const auto& sensor = globalMeta->sensors.front();
582597
const auto& film = sensor.film;
583598
viewport = core::recti(core::position2di(film.cropOffsetX,film.cropOffsetY), core::position2di(film.cropWidth,film.cropHeight));
584599

585600
auto extent = sceneBound.getExtent();
586601
camera = smgr->addCameraSceneNodeFPS(nullptr,100.f,core::min(extent.X,extent.Y,extent.Z)*0.0001f);
587602
// need to extract individual components
588-
bool leftHandedCamera = false;
589603
{
590604
auto relativeTransform = sensor.transform.matrix.extractSub3x4();
591-
if (relativeTransform.getPseudoDeterminant().x < 0.f)
592-
leftHandedCamera = true;
593605

594606
auto pos = relativeTransform.getTranslation();
595607
camera->setPosition(pos.getAsVector3df());
@@ -693,7 +705,7 @@ int main()
693705
uboData.NormalMat[11] = camera->getPosition().Z;
694706
driver->updateBufferRangeViaStagingBuffer(gpuubo.get(), 0u, sizeof(uboData), &uboData);
695707

696-
for (uint32_t j = 1u; j < gpumeshes->size(); ++j)
708+
for (uint32_t j = 0u; j < gpumeshes->size(); ++j)
697709
{
698710
auto& mesh = (*gpumeshes)[j];
699711

examples_tests/media/mitsuba/brdf_eval_test_diffuse.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,43 @@
3232

3333
<bsdf type="roughdiffuse" id="_01">
3434
<float name="alpha" value="0.1" />
35+
<rgb name="reflectance" value="1,1,1"/>
3536
</bsdf>
3637
<bsdf type="roughdiffuse" id="_02">
3738
<float name="alpha" value="0.2" />
39+
<rgb name="reflectance" value="1,1,1"/>
3840
</bsdf>
3941
<bsdf type="roughdiffuse" id="_03">
4042
<float name="alpha" value="0.3" />
43+
<rgb name="reflectance" value="1,1,1"/>
4144
</bsdf>
4245
<bsdf type="roughdiffuse" id="_04">
4346
<float name="alpha" value="0.4" />
47+
<rgb name="reflectance" value="1,1,1"/>
4448
</bsdf>
4549
<bsdf type="roughdiffuse" id="_05">
4650
<float name="alpha" value="0.5" />
51+
<rgb name="reflectance" value="1,1,1"/>
4752
</bsdf>
4853
<bsdf type="roughdiffuse" id="_06">
4954
<float name="alpha" value="0.6" />
55+
<rgb name="reflectance" value="1,1,1"/>
5056
</bsdf>
5157
<bsdf type="roughdiffuse" id="_07">
5258
<float name="alpha" value="0.7" />
59+
<rgb name="reflectance" value="1,1,1"/>
5360
</bsdf>
5461
<bsdf type="roughdiffuse" id="_08">
5562
<float name="alpha" value="0.8" />
63+
<rgb name="reflectance" value="1,1,1"/>
5664
</bsdf>
5765
<bsdf type="roughdiffuse" id="_09">
5866
<float name="alpha" value="0.9" />
67+
<rgb name="reflectance" value="1,1,1"/>
5968
</bsdf>
6069
<bsdf type="roughdiffuse" id="_10">
6170
<float name="alpha" value="1.0" />
71+
<rgb name="reflectance" value="1,1,1"/>
6272
</bsdf>
6373

6474
<shape type="sphere">

include/irr/asset/IVirtualTexture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class IVirtualTexture : public core::IReferenceCounted, public IVirtualTextureBa
317317

318318
const uint32_t pgTabSzxy = 1u<<_pgTabSzxy_log2;
319319
typename image_t::SCreationParams params;
320-
params.arrayLayers = _pgTabLayers;
320+
params.arrayLayers = std::max(_pgTabLayers, 1u); // page table must always be present
321321
params.extent = {pgTabSzxy,pgTabSzxy,1u};
322322
params.format = EF_R16G16_UINT;
323323
params.mipLevels = std::max<int32_t>(static_cast<int32_t>(_maxAllocatableTexSz_log2-_pgSzxy_log2+1u), 1);

include/irr/builtin/material_compiler/glsl/common.glsl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ bool op_hasSpecular(in uint op)
158158
#endif
159159

160160
#if !defined(OP_CONDUCTOR) && !defined(OP_DIELECTRIC)
161-
return false;
161+
false
162162
#endif
163163
;
164164
}
@@ -474,7 +474,11 @@ vec3 fetchTex(in uvec3 texid, in vec2 uv, in mat2 dUV)
474474
{
475475
float scale = uintBitsToFloat(texid.z);
476476

477+
#if _IRR_VT_FLOAT_VIEWS_COUNT
477478
return irr_glsl_vTextureGrad(texid.xy, uv, dUV).rgb*scale;
479+
#else
480+
return vec3(0.0);
481+
#endif
478482
}
479483

480484
void runTexPrefetchStream(in instr_stream_t stream, in vec2 uv, in mat2 dUV)
@@ -644,7 +648,7 @@ void instr_eval_and_pdf_execute(in instr_t instr, in MC_precomputed_t precomp, i
644648
float lambdaV = irr_glsl_smith_beckmann_Lambda(TdotV2, BdotV2, NdotV2, a2, ay2);
645649
G1_over_2NdotV = irr_glsl_smith_G1(lambdaV) / (2.0 * NdotV);
646650
G2_over_G1 = irr_glsl_beckmann_smith_G2_over_G1(lambdaV + 1.0, s.TdotL*s.TdotL, s.BdotL*s.BdotL, NdotL2, a2, ay2);
647-
ndf_val = irr_glsl_beckmann(ax, ay, a2, ay2, TdotH2, BdotH2, NdotH2);
651+
ndf_val = irr_glsl_beckmann(a, ay, a2, ay2, TdotH2, BdotH2, NdotH2);
648652
#endif
649653
} CASE_END
650654
#endif
@@ -660,7 +664,7 @@ void instr_eval_and_pdf_execute(in instr_t instr, in MC_precomputed_t precomp, i
660664
float lambdaV = irr_glsl_smith_beckmann_Lambda(TdotV2, BdotV2, NdotV2, a2, ay2);
661665
G1_over_2NdotV = irr_glsl_smith_G1(lambdaV) / (2.0 * NdotV);
662666
G2_over_G1 = irr_glsl_beckmann_smith_G2_over_G1(lambdaV + 1.0, s.TdotL*s.TdotL, s.BdotL*s.BdotL, NdotL2, a2, ay2);
663-
ndf_val = irr_glsl_beckmann(ax, ay, a2, ay2, TdotH2, BdotH2, NdotH2);
667+
ndf_val = irr_glsl_beckmann(a, ay, a2, ay2, TdotH2, BdotH2, NdotH2);
664668
#endif
665669
} CASE_END
666670
#endif
@@ -704,6 +708,7 @@ void instr_eval_and_pdf_execute(in instr_t instr, in MC_precomputed_t precomp, i
704708
}
705709
}
706710
#endif
711+
{} // empty else for when there are diffuse ops but arent any specular ones
707712
}
708713

709714
eval_and_pdf_t result = eval_and_pdf_t(eval, pdf);

include/irr/builtin/material_compiler/glsl/rasterization/impl.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void instr_eval_execute(in instr_t instr, in MC_precomputed_t precomp, inout irr
9999
#ifdef ALL_ISOTROPIC_BXDFS
100100
bxdf_eval_scalar_part = irr_glsl_ggx_height_correlated_cos_eval_DG_wo_clamps(NdotH2, NdotL, NdotL2, NdotV, NdotV2, a2);
101101
#else
102-
bxdf_eval_scalar_part = irr_glsl_ggx_height_correlated_aniso_cos_eval_DG_wo_clamps(NdotH2, TdotH2, BdotH2, NdotL, NdotL2, TdotL2, BdotL2, NdotV, NdotV2, TdotV2, BdotV2, ax, ax2, ay, ay2);
102+
bxdf_eval_scalar_part = irr_glsl_ggx_height_correlated_aniso_cos_eval_DG_wo_clamps(NdotH2, TdotH2, BdotH2, NdotL, NdotL2, TdotL2, BdotL2, NdotV, NdotV2, TdotV2, BdotV2, a, a2, ay, ay2);
103103
#endif
104104
} CASE_END
105105
#endif
@@ -109,7 +109,7 @@ void instr_eval_execute(in instr_t instr, in MC_precomputed_t precomp, inout irr
109109
#ifdef ALL_ISOTROPIC_BXDFS
110110
bxdf_eval_scalar_part = irr_glsl_beckmann_height_correlated_cos_eval_DG_wo_clamps(NdotH2, NdotL2, NdotV2, a2);
111111
#else
112-
bxdf_eval_scalar_part = irr_glsl_beckmann_aniso_height_correlated_cos_eval_DG_wo_clamps(NdotH2, TdotH2, BdotH2, NdotL2, TdotL2, BdotL2, NdotV2, TdotV2, BdotV2, ax, ax2, ay, ay2);
112+
bxdf_eval_scalar_part = irr_glsl_beckmann_aniso_height_correlated_cos_eval_DG_wo_clamps(NdotH2, TdotH2, BdotH2, NdotL2, TdotL2, BdotL2, NdotV2, TdotV2, BdotV2, a, a2, ay, ay2);
113113
#endif
114114
} CASE_END
115115
#endif
@@ -121,7 +121,7 @@ void instr_eval_execute(in instr_t instr, in MC_precomputed_t precomp, inout irr
121121
bxdf_eval_scalar_part = irr_glsl_blinn_phong_cos_eval_DG_wo_clamps(NdotH, NdotV2, NdotL2, n, a2);
122122
#else
123123
float ny = irr_glsl_alpha2_to_phong_exp(ay2);
124-
bxdf_eval_scalar_part = irr_glsl_blinn_phong_cos_eval_DG_wo_clamps(NdotH, NdotH2, TdotH2, BdotH2, TdotL2, BdotL2, TdotV2, BdotV2, NdotV2, NdotL2, nx, ny, ax2, ay2);
124+
bxdf_eval_scalar_part = irr_glsl_blinn_phong_cos_eval_DG_wo_clamps(NdotH, NdotH2, TdotH2, BdotH2, TdotL2, BdotL2, TdotV2, BdotV2, NdotV2, NdotL2, n, ny, a2, ay2);
125125
#endif
126126
} CASE_END
127127
#endif

src/irr/ext/MitsubaLoader/CMitsubaLoader.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ static core::smart_refctd_ptr<asset::ICPURenderpassIndependentPipeline> createPi
374374

375375
SRasterizationParams rasterParams;
376376
rasterParams.faceCullingMode = asset::EFCM_NONE;
377+
rasterParams.frontFaceIsCCW = 1;
377378
auto pipeline = core::make_smart_refctd_ptr<ICPURenderpassIndependentPipeline>(
378379
std::move(_layout),
379380
shaders, shaders+2,
@@ -1372,7 +1373,7 @@ SContext::tex_ass_type CMitsubaLoader::cacheTexture(SContext& ctx, uint32_t hier
13721373
auto outParams = viewParams.image->getCreationParameters();
13731374
asset::ICPUImage::SBufferCopy region;
13741375
const uint32_t bytesPerChannel = (getBytesPerPixel(outParams.format) * core::rational(1, getFormatChannelCount(outParams.format))).getIntegerApprox();
1375-
outParams.format = get1ChannelFormat(outParams.format);
1376+
outParams.format = get1ChannelFormat(bytesPerChannel);
13761377
const size_t texelBytesz = asset::getTexelOrBlockBytesize(outParams.format);
13771378
region.bufferRowLength = asset::IImageAssetHandlerBase::calcPitchInBlocks(outParams.extent.width, texelBytesz);
13781379
auto buffer = core::make_smart_refctd_ptr<asset::ICPUBuffer>(texelBytesz * region.bufferRowLength * outParams.extent.height);
@@ -1513,6 +1514,12 @@ auto CMitsubaLoader::genBSDFtreeTraversal(SContext& ctx, const CElementBSDF* _bs
15131514

15141515
return is_tex;
15151516
};
1517+
auto unrollScales = [](CElementTexture* tex)
1518+
{
1519+
while (tex->type == CElementTexture::SCALE)
1520+
tex = tex->scale.texture;
1521+
return tex;
1522+
};
15161523

15171524
core::stack<const CElementBSDF*> stack;
15181525
stack.push(_bsdf);
@@ -1569,11 +1576,12 @@ auto CMitsubaLoader::genBSDFtreeTraversal(SContext& ctx, const CElementBSDF* _bs
15691576
{
15701577
using namespace std::string_literals;
15711578

1572-
auto bm = cacheTexture(ctx, 0u, bsdf->bumpmap.texture);
1579+
auto* bumpmap_element = unrollScales(bsdf->bumpmap.texture);
1580+
auto bm = cacheTexture(ctx, 0u, bumpmap_element);
15731581
// TODO check and restore if dummy (image and sampler)
15741582
auto bumpmap = std::get<0>(bm)->getCreationParameters().image;
15751583
auto sampler = std::get<1>(bm);
1576-
const std::string key = ctx.derivMapCacheKey(bsdf->bumpmap.texture);
1584+
const std::string key = ctx.derivMapCacheKey(bumpmap_element);
15771585

15781586
if (!getBuiltinAsset<asset::ICPUImage, asset::IAsset::ET_IMAGE>(key.c_str(), m_manager))
15791587
{
@@ -1589,7 +1597,8 @@ auto CMitsubaLoader::genBSDFtreeTraversal(SContext& ctx, const CElementBSDF* _bs
15891597
case CElementBSDF::BLEND_BSDF:
15901598
if (cachePropertyTexture(bsdf->blendbsdf.weight, tex))
15911599
{
1592-
const std::string key = ctx.blendWeightImageCacheKey(bsdf->blendbsdf.weight.texture);
1600+
auto* weight_element = unrollScales(bsdf->blendbsdf.weight.texture);
1601+
const std::string key = ctx.blendWeightImageCacheKey(weight_element);
15931602

15941603
if (!getBuiltinAsset<asset::ICPUImage, asset::IAsset::ET_IMAGE>(key.c_str(), m_manager))
15951604
{

src/irr/ext/MitsubaLoader/CMitsubaMaterialCompilerFrontend.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ namespace MitsubaLoader
1212

1313
auto CMitsubaMaterialCompilerFrontend::getDerivMap(const CElementTexture* _element) const -> tex_ass_type
1414
{
15-
std::string key = m_loaderContext->derivMapCacheKey(_element);
1615
float scale = 1.f;
1716
std::tie(_element, scale) = getTexture_common(_element);
17+
std::string key = m_loaderContext->derivMapCacheKey(_element);
1818
if (_element->type != CElementTexture::BITMAP)
1919
return { nullptr, nullptr, 0.f };
2020

@@ -60,14 +60,17 @@ namespace MitsubaLoader
6060

6161
asset::IAsset::E_TYPE types[2]{ asset::IAsset::ET_IMAGE_VIEW, asset::IAsset::ET_TERMINATING_ZERO };
6262
auto viewBundle = m_loaderContext->override_->findCachedAsset(viewKey, types, m_loaderContext->inner, 0u);
63-
assert(!viewBundle.isEmpty());
64-
auto view = core::smart_refctd_ptr_static_cast<asset::ICPUImageView>(viewBundle.getContents().begin()[0]);
65-
types[0] = asset::IAsset::ET_SAMPLER;
66-
auto samplerBundle = m_loaderContext->override_->findCachedAsset(samplerKey, types, m_loaderContext->inner, 0u);
67-
assert(!samplerBundle.isEmpty());
68-
auto sampler = core::smart_refctd_ptr_static_cast<asset::ICPUSampler>(samplerBundle.getContents().begin()[0]);
69-
70-
return {view, sampler, _scale};
63+
if (!viewBundle.isEmpty())
64+
{
65+
auto view = core::smart_refctd_ptr_static_cast<asset::ICPUImageView>(viewBundle.getContents().begin()[0]);
66+
types[0] = asset::IAsset::ET_SAMPLER;
67+
auto samplerBundle = m_loaderContext->override_->findCachedAsset(samplerKey, types, m_loaderContext->inner, 0u);
68+
assert(!samplerBundle.isEmpty());
69+
auto sampler = core::smart_refctd_ptr_static_cast<asset::ICPUSampler>(samplerBundle.getContents().begin()[0]);
70+
71+
return {view, sampler, _scale};
72+
}
73+
return { nullptr, nullptr, _scale };
7174
}
7275

7376
auto CMitsubaMaterialCompilerFrontend::createIRNode(asset::material_compiler::IR* ir, const CElementBSDF* _bsdf) -> IRNode*

0 commit comments

Comments
 (0)