Skip to content

Commit b931bba

Browse files
Almost fix Opacity Mask with parameter blend taken from textures
1 parent 1f16592 commit b931bba

File tree

5 files changed

+391
-542
lines changed

5 files changed

+391
-542
lines changed

include/nbl/ext/MitsubaLoader/CMitsubaLoader.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
#include "nbl/ext/MitsubaLoader/SContext.h"
1717

1818

19-
namespace nbl
20-
{
21-
namespace ext
22-
{
23-
namespace MitsubaLoader
19+
namespace nbl::ext::MitsubaLoader
2420
{
2521

2622

@@ -78,7 +74,7 @@ class CMitsubaLoader : public asset::IRenderpassIndependentPipelineLoader
7874
core::vector<SContext::shape_ass_type> loadShapeGroup(SContext& ctx, uint32_t hierarchyLevel, const CElementShape::ShapeGroup* shapegroup, const core::matrix3x4SIMD& relTform);
7975
SContext::shape_ass_type loadBasicShape(SContext& ctx, uint32_t hierarchyLevel, CElementShape* shape, const core::matrix3x4SIMD& relTform);
8076

81-
SContext::tex_ass_type cacheTexture(SContext& ctx, uint32_t hierarchyLevel, const CElementTexture* texture, bool _restore = false);
77+
void cacheTexture(SContext& ctx, uint32_t hierarchyLevel, const CElementTexture* texture, const SContext::E_IMAGE_VIEW_SEMANTIC semantic);
8278

8379
SContext::bsdf_type getBSDFtreeTraversal(SContext& ctx, const CElementBSDF* bsdf);
8480
SContext::bsdf_type genBSDFtreeTraversal(SContext& ctx, const CElementBSDF* bsdf);
@@ -105,7 +101,5 @@ class CMitsubaLoader : public asset::IRenderpassIndependentPipelineLoader
105101
asset::SAssetBundle loadAsset(io::IReadFile* _file, const asset::IAssetLoader::SAssetLoadParams& _params, asset::IAssetLoader::IAssetLoaderOverride* _override = nullptr, uint32_t _hierarchyLevel = 0u) override;
106102
};
107103

108-
}
109-
}
110104
}
111105
#endif

include/nbl/ext/MitsubaLoader/CMitsubaMaterialCompilerFrontend.h

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,42 @@
88
#include "nbl/ext/MitsubaLoader/CElementBSDF.h"
99
#include <nbl/asset/material_compiler/IR.h>
1010

11-
namespace nbl
11+
namespace nbl::ext::MitsubaLoader
1212
{
13-
namespace ext
14-
{
15-
namespace MitsubaLoader
16-
{
17-
struct SContext;
13+
14+
struct SContext;
1815

1916
class CMitsubaMaterialCompilerFrontend
2017
{
21-
using IRNode = asset::material_compiler::IR::INode;
22-
using tex_ass_type = std::tuple<core::smart_refctd_ptr<asset::ICPUImageView>, core::smart_refctd_ptr<asset::ICPUSampler>, float>;
18+
using IRNode = asset::material_compiler::IR::INode;
19+
using tex_ass_type = std::tuple<core::smart_refctd_ptr<asset::ICPUImageView>,core::smart_refctd_ptr<asset::ICPUSampler>,float>;
2320

24-
const SContext* m_loaderContext;
21+
const SContext* m_loaderContext;
2522

26-
tex_ass_type getDerivMap(const CElementBSDF::BumpMap& _bump) const;
27-
tex_ass_type getBlendWeightTex(const CElementTexture* _element) const;
23+
std::pair<const CElementTexture*,float> unwindTextureScale(const CElementTexture* _element) const;
2824

29-
std::pair<const CElementTexture*, float> getTexture_common(const CElementTexture* _element) const;
25+
tex_ass_type getDerivMap(const CElementBSDF::BumpMap& _bump) const;
26+
tex_ass_type getBlendWeightTex(const CElementTexture* _element) const;
27+
tex_ass_type getTexture(const CElementTexture* _element) const;
3028

31-
tex_ass_type getTexture(const CElementTexture* _element) const;
32-
tex_ass_type getTexture(const std::string& _key, const CElementTexture* _element, float _scale) const;
29+
tex_ass_type getTexture(const std::string& _viewKey, const CElementTexture::Bitmap& _bitmap, float _scale) const;
3330

34-
tex_ass_type getErrorTexture() const;
31+
tex_ass_type getErrorTexture() const;
3532

36-
IRNode* createIRNode(asset::material_compiler::IR* ir, const CElementBSDF* _bsdf);
33+
IRNode* createIRNode(asset::material_compiler::IR* ir, const CElementBSDF* _bsdf);
3734

38-
public:
39-
struct front_and_back_t
40-
{
41-
IRNode* front;
42-
IRNode* back;
43-
};
35+
public:
36+
struct front_and_back_t
37+
{
38+
IRNode* front;
39+
IRNode* back;
40+
};
4441

45-
explicit CMitsubaMaterialCompilerFrontend(const SContext* _ctx) : m_loaderContext(_ctx) {}
42+
explicit CMitsubaMaterialCompilerFrontend(const SContext* _ctx) : m_loaderContext(_ctx) {}
4643

47-
front_and_back_t compileToIRTree(asset::material_compiler::IR* ir, const CElementBSDF* _bsdf);
44+
front_and_back_t compileToIRTree(asset::material_compiler::IR* ir, const CElementBSDF* _bsdf);
4845
};
4946

50-
}}}
47+
}
5148

5249
#endif

include/nbl/ext/MitsubaLoader/SContext.h

Lines changed: 119 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -48,91 +48,147 @@ namespace MitsubaLoader
4848
using shape_ass_type = core::smart_refctd_ptr<asset::ICPUMesh>;
4949
core::map<const CElementShape*, shape_ass_type> shapeCache;
5050
//image, sampler
51-
using tex_ass_type = std::tuple<core::smart_refctd_ptr<asset::ICPUImageView>, core::smart_refctd_ptr<asset::ICPUSampler>>;
51+
using tex_ass_type = std::tuple<core::smart_refctd_ptr<asset::ICPUImageView>,core::smart_refctd_ptr<asset::ICPUSampler>>;
5252
//image, scale
53-
core::map<core::smart_refctd_ptr<asset::ICPUImage>, float> derivMapCache;
53+
core::map<core::smart_refctd_ptr<asset::ICPUImage>,float> derivMapCache;
5454

55-
static std::string blendWeightImageCacheKey(const CElementTexture* bitmap)
56-
{
57-
using namespace std::string_literals;
58-
return bitmap->bitmap.filename.svalue + "?blend"s;
59-
}
60-
61-
static std::string imageViewCacheKey(const std::string& imageCacheKey)
55+
//
56+
enum E_IMAGE_VIEW_SEMANTIC : uint8_t
6257
{
63-
return imageCacheKey + "?view";
64-
}
65-
66-
static std::string derivMapCacheKey(const CElementTexture* bitmap, bool wasNormal)
58+
EIVS_IDENTITIY,
59+
EIVS_BLEND_WEIGHT,
60+
EIVS_NORMAL_MAP,
61+
EIVS_BUMP_MAP,
62+
EIVS_COUNT
63+
};
64+
static std::string imageViewCacheKey(const CElementTexture::Bitmap& bitmap, const E_IMAGE_VIEW_SEMANTIC semantic)
6765
{
68-
using namespace std::string_literals;
69-
static const char* wrap[5]
66+
std::string key = bitmap.filename.svalue;
67+
switch (bitmap.channel)
7068
{
71-
"?repeat",
72-
"?mirror",
73-
"?clamp",
74-
"?zero",
75-
"?one"
76-
};
77-
78-
std::string key = bitmap->bitmap.filename.svalue + "?deriv"s;
79-
key += wasNormal ? "?n"s:"?h"s;
80-
key += wrap[bitmap->bitmap.wrapModeU];
81-
key += wrap[bitmap->bitmap.wrapModeV];
82-
69+
case CElementTexture::Bitmap::CHANNEL::R:
70+
key += "?rrrr";
71+
break;
72+
case CElementTexture::Bitmap::CHANNEL::G:
73+
key += "?gggg";
74+
break;
75+
case CElementTexture::Bitmap::CHANNEL::B:
76+
key += "?bbbb";
77+
break;
78+
case CElementTexture::Bitmap::CHANNEL::A:
79+
key += "?aaaa";
80+
break;
81+
default:
82+
break;
83+
}
84+
switch (semantic)
85+
{
86+
case EIVS_BLEND_WEIGHT:
87+
key += "?blend";
88+
break;
89+
case EIVS_NORMAL_MAP:
90+
key += "?deriv?n";
91+
break;
92+
case EIVS_BUMP_MAP:
93+
key += "?deriv?h";
94+
{
95+
static const char* wrap[5]
96+
{
97+
"?repeat",
98+
"?mirror",
99+
"?clamp",
100+
"?zero",
101+
"?one"
102+
};
103+
key += wrap[bitmap.wrapModeU];
104+
key += wrap[bitmap.wrapModeV];
105+
}
106+
break;
107+
default:
108+
break;
109+
}
110+
key += "?view";
83111
return key;
84112
}
85113

86-
static std::string derivMapViewCacheKey(const CElementTexture* bitmap, bool wasNormal)
114+
static auto computeSamplerParameters(const CElementTexture::Bitmap& bitmap)
87115
{
88-
return imageViewCacheKey(derivMapCacheKey(bitmap,wasNormal));
89-
}
90-
91-
static std::string blendWeightViewCacheKey(const CElementTexture* bitmap)
92-
{
93-
return imageViewCacheKey(blendWeightImageCacheKey(bitmap));
116+
asset::ICPUSampler::SParams params;
117+
auto getWrapMode = [](CElementTexture::Bitmap::WRAP_MODE mode)
118+
{
119+
switch (mode)
120+
{
121+
case CElementTexture::Bitmap::WRAP_MODE::CLAMP:
122+
return asset::ISampler::ETC_CLAMP_TO_EDGE;
123+
break;
124+
case CElementTexture::Bitmap::WRAP_MODE::MIRROR:
125+
return asset::ISampler::ETC_MIRROR;
126+
break;
127+
case CElementTexture::Bitmap::WRAP_MODE::ONE:
128+
_NBL_DEBUG_BREAK_IF(true); // TODO : replace whole texture?
129+
break;
130+
case CElementTexture::Bitmap::WRAP_MODE::ZERO:
131+
_NBL_DEBUG_BREAK_IF(true); // TODO : replace whole texture?
132+
break;
133+
default:
134+
break;
135+
}
136+
return asset::ISampler::ETC_REPEAT;
137+
};
138+
params.TextureWrapU = getWrapMode(bitmap.wrapModeU);
139+
params.TextureWrapV = getWrapMode(bitmap.wrapModeV);
140+
params.TextureWrapW = asset::ISampler::ETC_REPEAT;
141+
params.BorderColor = asset::ISampler::ETBC_FLOAT_OPAQUE_BLACK;
142+
switch (bitmap.filterType)
143+
{
144+
case CElementTexture::Bitmap::FILTER_TYPE::EWA:
145+
[[fallthrough]]; // we dont support this fancy stuff
146+
case CElementTexture::Bitmap::FILTER_TYPE::TRILINEAR:
147+
params.MinFilter = asset::ISampler::ETF_LINEAR;
148+
params.MaxFilter = asset::ISampler::ETF_LINEAR;
149+
params.MipmapMode = asset::ISampler::ESMM_LINEAR;
150+
break;
151+
default:
152+
params.MinFilter = asset::ISampler::ETF_NEAREST;
153+
params.MaxFilter = asset::ISampler::ETF_NEAREST;
154+
params.MipmapMode = asset::ISampler::ESMM_NEAREST;
155+
break;
156+
}
157+
params.AnisotropicFilter = core::max(core::findMSB<uint32_t>(bitmap.maxAnisotropy),1u);
158+
params.CompareEnable = false;
159+
params.CompareFunc = asset::ISampler::ECO_NEVER;
160+
params.LodBias = 0.f;
161+
params.MaxLod = 10000.f;
162+
params.MinLod = 0.f;
163+
return params;
94164
}
95-
96-
static std::string samplerCacheKey(const std::string& base, const CElementTexture* tex)
165+
// TODO: commonalize this to all loaders
166+
static std::string samplerCacheKey(const std::string& base, const asset::ICPUSampler::SParams& samplerParams)
97167
{
98168
std::string samplerCacheKey = base;
99169

100-
switch (tex->bitmap.filterType)
101-
{
102-
case CElementTexture::Bitmap::FILTER_TYPE::EWA:
103-
[[fallthrough]]; //not supported
104-
case CElementTexture::Bitmap::FILTER_TYPE::TRILINEAR:
170+
if (samplerParams.MinFilter==asset::ISampler::ETF_LINEAR)
105171
samplerCacheKey += "?trilinear";
106-
break;
107-
default:
172+
else
108173
samplerCacheKey += "?nearest";
109-
break;
110-
}
111174

112-
auto perWrapMode = [](CElementTexture::Bitmap::WRAP_MODE mode)
175+
static const char* wrapModeName[] =
113176
{
114-
switch (mode)
115-
{
116-
case CElementTexture::Bitmap::WRAP_MODE::CLAMP:
117-
return "?clamp";
118-
case CElementTexture::Bitmap::WRAP_MODE::MIRROR:
119-
return "?clamp";
120-
case CElementTexture::Bitmap::WRAP_MODE::ONE:
121-
return "?one";
122-
case CElementTexture::Bitmap::WRAP_MODE::ZERO:
123-
return "?zero";
124-
default:
125-
return "?repeat";
126-
}
177+
"?repeat",
178+
"?clamp_to_edge",
179+
"?clamp_to_border",
180+
"?mirror",
181+
"?mirror_clamp_to_edge",
182+
"?mirror_clamp_to_border"
127183
};
128-
samplerCacheKey += perWrapMode(tex->bitmap.wrapModeU);
129-
samplerCacheKey += perWrapMode(tex->bitmap.wrapModeV);
184+
samplerCacheKey += wrapModeName[samplerParams.TextureWrapU];
185+
samplerCacheKey += wrapModeName[samplerParams.TextureWrapV];
130186

131187
return samplerCacheKey;
132188
}
133-
std::string samplerCacheKey(const CElementTexture* tex) const
189+
std::string samplerCacheKey(const asset::ICPUSampler::SParams& samplerParams) const
134190
{
135-
return samplerCacheKey(samplerCacheKeyBase, tex);
191+
return samplerCacheKey(samplerCacheKeyBase,samplerParams);
136192
}
137193

138194
//index of root node in IR

0 commit comments

Comments
 (0)