diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index be82cc9d21..7c85052787 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -7,7 +7,8 @@ A new header is inserted each time a *tag* is created. - WebGL: upgraded the JS bindings to work with emsdk 3.1.15 - WebGL: added missing IBL builder to TypeScript annotations -- engine : Fix incorrect precision restoration when computing accurate world translations +- engine: Fix incorrect precision restoration when computing accurate world translations +- engine: make `MaterialInstance` public API friendly to `std::string_view` parameters ## v1.25.4 diff --git a/filament/include/filament/MaterialInstance.h b/filament/include/filament/MaterialInstance.h index 2cf194fa26..c339bff7c1 100644 --- a/filament/include/filament/MaterialInstance.h +++ b/filament/include/filament/MaterialInstance.h @@ -37,6 +37,18 @@ class UniformBuffer; class UniformInterfaceBlock; class UTILS_PUBLIC MaterialInstance : public FilamentAPI { + template + using StringLiteralHelper = const char[N]; + + struct StringLiteral { + const char* data; + size_t size; + template + StringLiteral(StringLiteralHelper const& s) noexcept // NOLINT(google-explicit-constructor) + : data(s), size(N - 1) { + } + }; + public: using CullingMode = filament::backend::CullingMode; using TransparencyMode = filament::TransparencyMode; @@ -88,23 +100,51 @@ public: /** * Set a uniform by name * - * @param name Name of the parameter as defined by Material. Cannot be nullptr. - * @param value Value of the parameter to set. + * @param name Name of the parameter as defined by Material. Cannot be nullptr. + * @param nameLength Length in `char` of the name parameter. + * @param value Value of the parameter to set. * @throws utils::PreConditionPanic if name doesn't exist or no-op if exceptions are disabled. */ template> - void setParameter(const char* name, T const& value) noexcept; + void setParameter(const char* name, size_t nameLength, T const& value); + + /** inline helper to provide the name as a null-terminated string literal */ + template> + inline void setParameter(StringLiteral name, T const& value) { + setParameter(name.data, name.size, value); + } + + /** inline helper to provide the name as a null-terminated C string */ + template> + inline void setParameter(const char* name, T const& value) { + setParameter(name, strlen(name), value); + } + /** * Set a uniform array by name * - * @param name Name of the parameter array as defined by Material. Cannot be nullptr. - * @param values Array of values to set to the named parameter array. - * @param count Size of the array to set. + * @param name Name of the parameter array as defined by Material. Cannot be nullptr. + * @param nameLength Length in `char` of the name parameter. + * @param values Array of values to set to the named parameter array. + * @param count Size of the array to set. * @throws utils::PreConditionPanic if name doesn't exist or no-op if exceptions are disabled. */ template> - void setParameter(const char* name, const T* values, size_t count) noexcept; + void setParameter(const char* name, size_t nameLength, const T* values, size_t count); + + /** inline helper to provide the name as a null-terminated string literal */ + template> + inline void setParameter(StringLiteral name, const T* values, size_t count) { + setParameter(name.data, name.size, values, count); + } + + /** inline helper to provide the name as a null-terminated C string */ + template> + inline void setParameter(const char* name, const T* values, size_t count) { + setParameter(name, strlen(name), values, count); + } + /** * Set a texture as the named parameter @@ -112,35 +152,73 @@ public: * Note: Depth textures can't be sampled with a linear filter unless the comparison mode is set * to COMPARE_TO_TEXTURE. * - * @param name Name of the parameter as defined by Material. Cannot be nullptr. - * @param texture Non nullptr Texture object pointer. - * @param sampler Sampler parameters. + * @param name Name of the parameter as defined by Material. Cannot be nullptr. + * @param nameLength Length in `char` of the name parameter. + * @param texture Non nullptr Texture object pointer. + * @param sampler Sampler parameters. * @throws utils::PreConditionPanic if name doesn't exist or no-op if exceptions are disabled. */ - void setParameter(const char* name, - Texture const* texture, TextureSampler const& sampler) noexcept; + void setParameter(const char* name, size_t nameLength, + Texture const* texture, TextureSampler const& sampler); + + /** inline helper to provide the name as a null-terminated string literal */ + inline void setParameter(StringLiteral name, + Texture const* texture, TextureSampler const& sampler) { + setParameter(name.data, name.size, texture, sampler); + } + + /** inline helper to provide the name as a null-terminated C string */ + inline void setParameter(const char* name, + Texture const* texture, TextureSampler const& sampler) { + setParameter(name, strlen(name), texture, sampler); + } + /** * Set an RGB color as the named parameter. * A conversion might occur depending on the specified type * - * @param name Name of the parameter as defined by Material. Cannot be nullptr. - * @param type Whether the color value is encoded as Linear or sRGB. - * @param color Array of read, green, blue channels values. + * @param name Name of the parameter as defined by Material. Cannot be nullptr. + * @param nameLength Length in `char` of the name parameter. + * @param type Whether the color value is encoded as Linear or sRGB. + * @param color Array of read, green, blue channels values. * @throws utils::PreConditionPanic if name doesn't exist or no-op if exceptions are disabled. */ - void setParameter(const char* name, RgbType type, math::float3 color) noexcept; + void setParameter(const char* name, size_t nameLength, RgbType type, math::float3 color); + + /** inline helper to provide the name as a null-terminated string literal */ + inline void setParameter(StringLiteral name, RgbType type, math::float3 color) { + setParameter(name.data, name.size, type, color); + } + + /** inline helper to provide the name as a null-terminated C string */ + inline void setParameter(const char* name, RgbType type, math::float3 color) { + setParameter(name, strlen(name), type, color); + } + /** * Set an RGBA color as the named parameter. * A conversion might occur depending on the specified type * - * @param name Name of the parameter as defined by Material. Cannot be nullptr. - * @param type Whether the color value is encoded as Linear or sRGB/A. - * @param color Array of read, green, blue and alpha channels values. + * @param name Name of the parameter as defined by Material. Cannot be nullptr. + * @param nameLength Length in `char` of the name parameter. + * @param type Whether the color value is encoded as Linear or sRGB/A. + * @param color Array of read, green, blue and alpha channels values. * @throws utils::PreConditionPanic if name doesn't exist or no-op if exceptions are disabled. */ - void setParameter(const char* name, RgbaType type, math::float4 color) noexcept; + void setParameter(const char* name, size_t nameLength, RgbaType type, math::float4 color); + + /** inline helper to provide the name as a null-terminated string literal */ + inline void setParameter(StringLiteral name, RgbaType type, math::float4 color) { + setParameter(name.data, name.size, type, color); + } + + /** inline helper to provide the name as a null-terminated C string */ + inline void setParameter(const char* name, RgbaType type, math::float4 color) { + setParameter(name, strlen(name), type, color); + } + /** * Set up a custom scissor rectangle; by default this encompasses the View. diff --git a/filament/src/MaterialInstance.cpp b/filament/src/MaterialInstance.cpp index 5ac715c440..6f2b096bfa 100644 --- a/filament/src/MaterialInstance.cpp +++ b/filament/src/MaterialInstance.cpp @@ -29,7 +29,7 @@ using namespace backend; // vec4. This must not be inlined (this is the whole point). template UTILS_NOINLINE -void FMaterialInstance::setParameterUntypedImpl(const char* name, const void* value) noexcept { +void FMaterialInstance::setParameterUntypedImpl(std::string_view name, const void* value) { ssize_t offset = mMaterial->getUniformInterfaceBlock().getUniformOffset(name, 0); if (UTILS_LIKELY(offset >= 0)) { mUniforms.setUniformUntyped(size_t(offset), value); // handles specialization for mat3f @@ -41,14 +41,14 @@ void FMaterialInstance::setParameterUntypedImpl(const char* name, const void* va // this converts typed calls into the untyped-sized call. template UTILS_ALWAYS_INLINE -inline void FMaterialInstance::setParameterImpl(const char* name, T const& value) noexcept { +inline void FMaterialInstance::setParameterImpl(std::string_view name, T const& value) { static_assert(!std::is_same_v); setParameterUntypedImpl(name, &value); } // specialization for mat3f template<> -inline void FMaterialInstance::setParameterImpl(const char* name, mat3f const& value) noexcept { +inline void FMaterialInstance::setParameterImpl(std::string_view name, mat3f const& value) { ssize_t offset = mMaterial->getUniformInterfaceBlock().getUniformOffset(name, 0); if (UTILS_LIKELY(offset >= 0)) { mUniforms.setUniform(size_t(offset), value); @@ -59,7 +59,8 @@ inline void FMaterialInstance::setParameterImpl(const char* name, mat3f const& v template UTILS_NOINLINE -void FMaterialInstance::setParameterUntypedImpl(const char* name, const void* value, size_t count) noexcept { +void FMaterialInstance::setParameterUntypedImpl(std::string_view name, + const void* value, size_t count) { ssize_t offset = mMaterial->getUniformInterfaceBlock().getUniformOffset(name, 0); if (UTILS_LIKELY(offset >= 0)) { mUniforms.setUniformArrayUntyped(size_t(offset), value, count); @@ -68,116 +69,117 @@ void FMaterialInstance::setParameterUntypedImpl(const char* name, const void* va template UTILS_ALWAYS_INLINE -inline void FMaterialInstance::setParameterImpl(const char* name, const T* value, size_t count) noexcept { +inline void FMaterialInstance::setParameterImpl(std::string_view name, + const T* value, size_t count) { static_assert(!std::is_same_v); setParameterUntypedImpl(name, value, count); } // ------------------------------------------------------------------------------------------------ -template -void MaterialInstance::setParameter(const char* name, T const& value) noexcept { - upcast(this)->setParameterImpl(name, value); +template +void MaterialInstance::setParameter(const char* name, size_t nameLength, T const& value) { + upcast(this)->setParameterImpl({ name, nameLength }, value); } template<> -UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, bool const& v) noexcept { +UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, bool const& v) { // this kills tail-call optimization - MaterialInstance::setParameter(name, (uint32_t)v); + MaterialInstance::setParameter(name, nameLength, (uint32_t)v); } template<> -UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, bool2 const& v) noexcept { +UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, bool2 const& v) { // this kills tail-call optimization - MaterialInstance::setParameter(name, uint2(v)); + MaterialInstance::setParameter(name, nameLength, uint2(v)); } template<> -UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, bool3 const& v) noexcept { +UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, bool3 const& v) { // this kills tail-call optimization - MaterialInstance::setParameter(name, uint3(v)); + MaterialInstance::setParameter(name, nameLength, uint3(v)); } template<> -UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, bool4 const& v) noexcept { +UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, bool4 const& v) { // this kills tail-call optimization - MaterialInstance::setParameter(name, uint4(v)); + MaterialInstance::setParameter(name, nameLength, uint4(v)); } // explicit template instantiation of our supported types -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, float const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, int32_t const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, uint32_t const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, int2 const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, int3 const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, int4 const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, uint2 const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, uint3 const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, uint4 const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, float2 const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, float3 const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, float4 const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, mat3f const& v) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, mat4f const& v) noexcept; +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, float const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, int32_t const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, uint32_t const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, int2 const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, int3 const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, int4 const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, uint2 const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, uint3 const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, uint4 const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, float2 const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, float3 const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, float4 const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, mat3f const& v); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, mat4f const& v); // ------------------------------------------------------------------------------------------------ template -void MaterialInstance::setParameter(const char* name, const T* value, size_t count) noexcept { - upcast(this)->setParameterImpl(name, value, count); +void MaterialInstance::setParameter(const char* name, size_t nameLength, const T* value, size_t count) { + upcast(this)->setParameterImpl({ name, nameLength }, value, count); } template<> -UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, const bool* v, size_t c) noexcept { +UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, const bool* v, size_t c) { auto* p = new uint32_t[c]; std::copy_n(v, c, p); - MaterialInstance::setParameter(name, p, c); + MaterialInstance::setParameter(name, nameLength, p, c); delete [] p; } template<> -UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, const bool2* v, size_t c) noexcept { +UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, const bool2* v, size_t c) { auto* p = new uint2[c]; std::copy_n(v, c, p); - MaterialInstance::setParameter(name, p, c); + MaterialInstance::setParameter(name, nameLength, p, c); delete [] p; } template<> -UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, const bool3* v, size_t c) noexcept { +UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, const bool3* v, size_t c) { auto* p = new uint3[c]; std::copy_n(v, c, p); - MaterialInstance::setParameter(name, p, c); + MaterialInstance::setParameter(name, nameLength, p, c); delete [] p; } template<> -UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, const bool4* v, size_t c) noexcept { +UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, const bool4* v, size_t c) { auto* p = new uint4[c]; std::copy_n(v, c, p); - MaterialInstance::setParameter(name, p, c); + MaterialInstance::setParameter(name, nameLength, p, c); delete [] p; } template<> -UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, const mat3f* v, size_t c) noexcept { +UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, const mat3f* v, size_t c) { // pretend each mat3 is an array of 3 float3 - MaterialInstance::setParameter(name, reinterpret_cast(v), c * 3); + MaterialInstance::setParameter(name, nameLength, reinterpret_cast(v), c * 3); } -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const float *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const int32_t *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, const uint32_t *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const int2 *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const int3 *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const int4 *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const uint2 *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const uint3 *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const uint4 *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const float2 *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const float3 *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const float4 *v, size_t c) noexcept; -template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, const mat4f *v, size_t c) noexcept; +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const float *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const int32_t *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter(const char* name, size_t nameLength, const uint32_t *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const int2 *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const int3 *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const int4 *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const uint2 *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const uint3 *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const uint4 *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const float2 *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const float3 *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const float4 *v, size_t c); +template UTILS_PUBLIC void MaterialInstance::setParameter (const char* name, size_t nameLength, const mat4f *v, size_t c); // ------------------------------------------------------------------------------------------------ @@ -189,17 +191,17 @@ const char* MaterialInstance::getName() const noexcept { return upcast(this)->getName(); } -void MaterialInstance::setParameter(const char* name, Texture const* texture, - TextureSampler const& sampler) noexcept { - return upcast(this)->setParameterImpl(name, texture, sampler); +void MaterialInstance::setParameter(const char* name, size_t nameLength, Texture const* texture, + TextureSampler const& sampler) { + return upcast(this)->setParameterImpl({ name, nameLength }, texture, sampler); } -void MaterialInstance::setParameter(const char* name, RgbType type, float3 color) noexcept { - upcast(this)->setParameterImpl(name, Color::toLinear(type, color)); +void MaterialInstance::setParameter(const char* name, size_t nameLength, RgbType type, float3 color) { + upcast(this)->setParameterImpl({ name, nameLength }, Color::toLinear(type, color)); } -void MaterialInstance::setParameter(const char* name, RgbaType type, float4 color) noexcept { - upcast(this)->setParameterImpl(name, Color::toLinear(type, color)); +void MaterialInstance::setParameter(const char* name, size_t nameLength, RgbaType type, float4 color) { + upcast(this)->setParameterImpl({ name, nameLength }, Color::toLinear(type, color)); } void MaterialInstance::setScissor(uint32_t left, uint32_t bottom, uint32_t width, diff --git a/filament/src/details/Material.cpp b/filament/src/details/Material.cpp index 74ca16288b..5677f7f4f9 100644 --- a/filament/src/details/Material.cpp +++ b/filament/src/details/Material.cpp @@ -342,7 +342,7 @@ bool FMaterial::isSampler(const char* name) const noexcept { UniformInterfaceBlock::UniformInfo const* FMaterial::reflect( utils::StaticString const& name) const noexcept { - return mUniformInterfaceBlock.getUniformInfo(name.c_str()); + return mUniformInterfaceBlock.getUniformInfo({ name.c_str(), name.length() }); } void FMaterial::prepareProgramSlow(Variant variant) const noexcept { diff --git a/filament/src/details/MaterialInstance.cpp b/filament/src/details/MaterialInstance.cpp index e15f27bcb1..ce9ebef93a 100644 --- a/filament/src/details/MaterialInstance.cpp +++ b/filament/src/details/MaterialInstance.cpp @@ -139,14 +139,14 @@ void FMaterialInstance::commitSlow(DriverApi& driver) const { // ------------------------------------------------------------------------------------------------ -void FMaterialInstance::setParameter(const char* name, +void FMaterialInstance::setParameter(std::string_view name, backend::Handle texture, backend::SamplerParams params) noexcept { size_t index = mMaterial->getSamplerInterfaceBlock().getSamplerInfo(name)->offset; mSamplers.setSampler(index, { texture, params }); } -void FMaterialInstance::setParameterImpl(const char* name, - Texture const* texture, TextureSampler const& sampler) noexcept { +void FMaterialInstance::setParameterImpl(std::string_view name, + Texture const* texture, TextureSampler const& sampler) { #ifndef NDEBUG // Per GLES3.x specification, depth texture can't be filtered unless in compare mode. @@ -161,8 +161,8 @@ void FMaterialInstance::setParameterImpl(const char* name, minFilter == SamplerMinFilter::NEAREST_MIPMAP_LINEAR) { PANIC_LOG("Depth textures can't be sampled with a linear filter " "unless the comparison mode is set to COMPARE_TO_TEXTURE. " - "(material: \"%s\", parameter: \"%s\")", - getMaterial()->getName().c_str(), name); + "(material: \"%s\", parameter: \"%.*s\")", + getMaterial()->getName().c_str(), name.size(), name.data()); } } } diff --git a/filament/src/details/MaterialInstance.h b/filament/src/details/MaterialInstance.h index be564257b2..1893f75716 100644 --- a/filament/src/details/MaterialInstance.h +++ b/filament/src/details/MaterialInstance.h @@ -122,7 +122,7 @@ public: const char* getName() const noexcept; - void setParameter(const char* name, + void setParameter(std::string_view name, backend::Handle texture, backend::SamplerParams params) noexcept; using MaterialInstance::setParameter; @@ -132,19 +132,19 @@ private: friend class MaterialInstance; template - void setParameterUntypedImpl(const char* name, const void* value) noexcept; + void setParameterUntypedImpl(std::string_view name, const void* value); template - void setParameterUntypedImpl(const char* name, const void* value, size_t count) noexcept; + void setParameterUntypedImpl(std::string_view name, const void* value, size_t count); template - void setParameterImpl(const char* name, T const& value) noexcept; + void setParameterImpl(std::string_view name, T const& value); template - void setParameterImpl(const char* name, const T* value, size_t count) noexcept; + void setParameterImpl(std::string_view name, const T* value, size_t count); - void setParameterImpl(const char* name, - Texture const* texture, TextureSampler const& sampler) noexcept; + void setParameterImpl(std::string_view name, + Texture const* texture, TextureSampler const& sampler); FMaterialInstance() noexcept; void initDefaultInstance(FEngine& engine, FMaterial const* material); diff --git a/libs/filabridge/include/private/filament/SamplerInterfaceBlock.h b/libs/filabridge/include/private/filament/SamplerInterfaceBlock.h index 2262f742b2..55ab4b920a 100644 --- a/libs/filabridge/include/private/filament/SamplerInterfaceBlock.h +++ b/libs/filabridge/include/private/filament/SamplerInterfaceBlock.h @@ -26,6 +26,7 @@ #include #include +#include namespace filament { @@ -105,9 +106,9 @@ public: } // information record for sampler of the given name - SamplerInfo const* getSamplerInfo(const char* name) const; + SamplerInfo const* getSamplerInfo(std::string_view name) const; - bool hasSampler(const char* name) const noexcept { + bool hasSampler(std::string_view name) const noexcept { return mInfoMap.find(name) != mInfoMap.end(); } @@ -123,7 +124,7 @@ private: utils::CString mName; backend::ShaderStageFlags mStageFlags{}; // It's needed to check if MAX_SAMPLER_COUNT is exceeded. utils::FixedCapacityVector mSamplersInfoList; - std::unordered_map mInfoMap; + std::unordered_map mInfoMap; }; } // namespace filament diff --git a/libs/filabridge/include/private/filament/UniformInterfaceBlock.h b/libs/filabridge/include/private/filament/UniformInterfaceBlock.h index 27ee9a6a55..e287ddcf7b 100644 --- a/libs/filabridge/include/private/filament/UniformInterfaceBlock.h +++ b/libs/filabridge/include/private/filament/UniformInterfaceBlock.h @@ -116,11 +116,11 @@ public: } // negative value if name doesn't exist or Panic if exceptions are enabled - ssize_t getUniformOffset(const char* name, size_t index) const; + ssize_t getUniformOffset(std::string_view name, size_t index) const; - UniformInfo const* getUniformInfo(const char* name) const; + UniformInfo const* getUniformInfo(std::string_view name) const; - bool hasUniform(const char* name) const noexcept { + bool hasUniform(std::string_view name) const noexcept { return mInfoMap.find(name) != mInfoMap.end(); } @@ -136,7 +136,7 @@ private: utils::CString mName; utils::FixedCapacityVector mUniformsInfoList; - std::unordered_map mInfoMap; + std::unordered_map mInfoMap; uint32_t mSize = 0; // size in bytes rounded to multiple of 4 }; diff --git a/libs/filabridge/src/SamplerInterfaceBlock.cpp b/libs/filabridge/src/SamplerInterfaceBlock.cpp index 28e1ff6326..1b3fa9f774 100644 --- a/libs/filabridge/src/SamplerInterfaceBlock.cpp +++ b/libs/filabridge/src/SamplerInterfaceBlock.cpp @@ -83,17 +83,16 @@ SamplerInterfaceBlock::SamplerInterfaceBlock(Builder const& builder) noexcept assert_invariant(i == e.offset); SamplerInfo& info = samplersInfoList[i++]; info = e; - infoMap[info.name.c_str()] = info.offset; // info.name.c_str() guaranteed constant + infoMap[{ info.name.data(), info.name.size() }] = info.offset; // info.name.c_str() guaranteed constant } assert_invariant(i == samplersInfoList.size()); } const SamplerInterfaceBlock::SamplerInfo* SamplerInterfaceBlock::getSamplerInfo( - const char* name) const { - auto const& pos = mInfoMap.find(name); - if (!ASSERT_PRECONDITION_NON_FATAL(pos != mInfoMap.end(), "sampler named \"%s\" not found", name)) { - return nullptr; - } + std::string_view name) const { + auto pos = mInfoMap.find(name); + ASSERT_PRECONDITION(pos != mInfoMap.end(), "sampler named \"%.*s\" not found", + name.size(), name.data()); return &mSamplersInfoList[pos->second]; } diff --git a/libs/filabridge/src/UniformInterfaceBlock.cpp b/libs/filabridge/src/UniformInterfaceBlock.cpp index a61f42395a..d4a8017be8 100644 --- a/libs/filabridge/src/UniformInterfaceBlock.cpp +++ b/libs/filabridge/src/UniformInterfaceBlock.cpp @@ -112,7 +112,7 @@ UniformInterfaceBlock::UniformInterfaceBlock(Builder const& builder) noexcept info = { e.name, offset, stride, e.type, e.size, e.precision, e.structName }; // record this uniform info - infoMap[info.name.c_str()] = i; + infoMap[{ info.name.data(), info.name.size() }] = i; // advance offset to next slot offset += stride * std::max(1u, e.size); @@ -123,23 +123,20 @@ UniformInterfaceBlock::UniformInterfaceBlock(Builder const& builder) noexcept mSize = sizeof(uint32_t) * ((offset + 3) & ~3); } -ssize_t UniformInterfaceBlock::getUniformOffset(const char* name, size_t index) const { +ssize_t UniformInterfaceBlock::getUniformOffset(std::string_view name, size_t index) const { auto const* info = getUniformInfo(name); - if (!info) { - return -1; - } - return info->getBufferOffset(index); + assert_invariant(info); + return (ssize_t)info->getBufferOffset(index); } -UniformInterfaceBlock::UniformInfo const* UniformInterfaceBlock::getUniformInfo(const char* name) const { - auto const& pos = mInfoMap.find(name); - if (!ASSERT_PRECONDITION_NON_FATAL(pos != mInfoMap.end(), "uniform named \"%s\" not found", name)) { - return nullptr; - } +UniformInterfaceBlock::UniformInfo const* UniformInterfaceBlock::getUniformInfo( + std::string_view name) const { + auto pos = mInfoMap.find(name); + ASSERT_PRECONDITION(pos != mInfoMap.end(), + "uniform named \"%.*s\" not found", name.size(), name.data()); return &mUniformsInfoList[pos->second]; } - uint8_t UTILS_NOINLINE UniformInterfaceBlock::baseAlignmentForType(UniformInterfaceBlock::Type type) noexcept { switch (type) { case Type::BOOL: