diff options
Diffstat (limited to 'indra/llrender')
| -rw-r--r-- | indra/llrender/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | indra/llrender/llfontfreetype.cpp | 184 | ||||
| -rw-r--r-- | indra/llrender/llfontfreetype.h | 20 | ||||
| -rw-r--r-- | indra/llrender/llfontgl.cpp | 13 | ||||
| -rw-r--r-- | indra/llrender/llfontgl.h | 3 | ||||
| -rw-r--r-- | indra/llrender/llfontregistry.cpp | 66 | ||||
| -rw-r--r-- | indra/llrender/llfontregistry.h | 31 | ||||
| -rw-r--r-- | indra/llrender/llfontvertexbuffer.cpp | 77 | ||||
| -rw-r--r-- | indra/llrender/llfontvertexbuffer.h | 46 | ||||
| -rw-r--r-- | indra/llrender/llgl.cpp | 6 | ||||
| -rw-r--r-- | indra/llrender/llgl.h | 6 | ||||
| -rw-r--r-- | indra/llrender/llglslshader.cpp | 21 | ||||
| -rw-r--r-- | indra/llrender/llimagegl.cpp | 4 | ||||
| -rw-r--r-- | indra/llrender/llrender.cpp | 16 | ||||
| -rw-r--r-- | indra/llrender/llshadermgr.cpp | 19 | ||||
| -rw-r--r-- | indra/llrender/lltexturemanagerbridge.cpp | 2 | ||||
| -rw-r--r-- | indra/llrender/llvertexbuffer.cpp | 213 | ||||
| -rw-r--r-- | indra/llrender/llvertexbuffer.h | 4 |
18 files changed, 543 insertions, 195 deletions
diff --git a/indra/llrender/CMakeLists.txt b/indra/llrender/CMakeLists.txt index 6de5685517..71423de95d 100644 --- a/indra/llrender/CMakeLists.txt +++ b/indra/llrender/CMakeLists.txt @@ -103,7 +103,12 @@ target_link_libraries(llrender llwindow ll::freetype OpenGL::GL - OpenGL::GLU ) +if (${LINUX_DISTRO} MATCHES freedesktop) + target_link_libraries(${PROJECT_NAME} ll::glu) +else () + target_link_libraries(${PROJECT_NAME} OpenGL::GLU) +endif () + include(LibraryInstall) diff --git a/indra/llrender/llfontfreetype.cpp b/indra/llrender/llfontfreetype.cpp index 9d6773ac5b..357dfc8ea8 100644 --- a/indra/llrender/llfontfreetype.cpp +++ b/indra/llrender/llfontfreetype.cpp @@ -39,6 +39,7 @@ // For some reason, this won't work if it's not wrapped in the ifdef #ifdef FT_FREETYPE_H #include FT_FREETYPE_H +#include FT_MULTIPLE_MASTERS_H #endif #include "lldir.h" @@ -118,6 +119,8 @@ LLFontGlyphInfo::LLFontGlyphInfo(U32 index, EFontGlyphType glyph_type) mYBitmapOffset(0), // Offset to the origin in the bitmap mXBearing(0), // Distance from baseline to left in pixels mYBearing(0), // Distance from baseline to top in pixels + mLsbDelta(0), + mRsbDelta(0), mBitmapEntry(std::make_pair(EFontGlyphType::Unspecified, -1)) // Which bitmap in the bitmap cache contains this glyph { } @@ -133,6 +136,8 @@ LLFontGlyphInfo::LLFontGlyphInfo(const LLFontGlyphInfo& fgi) , mYBitmapOffset(fgi.mYBitmapOffset) , mXBearing(fgi.mXBearing) , mYBearing(fgi.mYBearing) + , mLsbDelta(fgi.mLsbDelta) + , mRsbDelta(fgi.mRsbDelta) { mBitmapEntry = fgi.mBitmapEntry; } @@ -143,6 +148,7 @@ LLFontFreetype::LLFontFreetype() mDescender(0.f), mLineHeight(0.f), mIsFallback(false), + mHinting(EFontHinting::FORCE_AUTOHINT), mFTFace(nullptr), mRenderGlyphCount(0), mStyle(0), @@ -166,7 +172,7 @@ LLFontFreetype::~LLFontFreetype() // mFallbackFonts cleaned up by LLPointer destructor } -bool LLFontFreetype::loadFace(const std::string& filename, F32 point_size, F32 vert_dpi, F32 horz_dpi, bool is_fallback, S32 face_n) +bool LLFontFreetype::loadFace(const std::string& filename, F32 point_size, F32 vert_dpi, F32 horz_dpi, S32 weight, bool is_fallback, S32 face_n, EFontHinting hinting, S32 flags) { // Don't leak face objects. This is also needed to deal with // changed font file names. @@ -190,6 +196,20 @@ bool LLFontFreetype::loadFace(const std::string& filename, F32 point_size, F32 v return false; mIsFallback = is_fallback; + mHinting = hinting; + mFontFlags = flags; + mWeight = weight; + + bool variable_font = false; + if (weight >= 0) + { + variable_font = setVariationAxis("wght", static_cast<F32>(weight)); + + // For Inter, also set optical size based on point size + // This makes text look better at different sizes + setVariationAxis("opsz", point_size); + } + F32 pixels_per_em = (point_size / 72.f)*vert_dpi; // Size in inches * dpi error = FT_Set_Char_Size(mFTFace, /* handle to face object */ @@ -245,6 +265,18 @@ bool LLFontFreetype::loadFace(const std::string& filename, F32 point_size, F32 v { mStyle |= LLFontGL::BOLD; } + else if (flags & LLFontGL::BOLD) + { + // FontGL applies programmatic bolding to fonts that are a part of 'bold' descriptor but don't have the bold style set. + // Ex: Inter SemiBold doesn't have FT_STYLE_FLAG_BOLD and without this style it would be bolded programmatically. + mStyle |= LLFontGL::BOLD; + } + else if (weight >= 600 && variable_font) + { + // If the font is heavy enough, consider it bold and avoid programmatic bolding + // even if it doesn't have the bold style set. + mStyle |= LLFontGL::BOLD; + } if(mFTFace->style_flags & FT_STYLE_FLAG_ITALIC) { @@ -343,16 +375,10 @@ F32 LLFontFreetype::getXKerning(llwchar char_left, llwchar char_right) const //llassert(!mIsFallback); LLFontGlyphInfo* left_glyph_info = getGlyphInfo(char_left, EFontGlyphType::Unspecified);; - U32 left_glyph = left_glyph_info ? left_glyph_info->mGlyphIndex : 0; // Kern this puppy. LLFontGlyphInfo* right_glyph_info = getGlyphInfo(char_right, EFontGlyphType::Unspecified); - U32 right_glyph = right_glyph_info ? right_glyph_info->mGlyphIndex : 0; - - FT_Vector delta; - - llverify(!FT_Get_Kerning(mFTFace, left_glyph, right_glyph, ft_kerning_unfitted, &delta)); - return delta.x*(1.f/64.f); + return getXKerning(left_glyph_info, right_glyph_info); } F32 LLFontFreetype::getXKerning(const LLFontGlyphInfo* left_glyph_info, const LLFontGlyphInfo* right_glyph_info) const @@ -365,9 +391,28 @@ F32 LLFontFreetype::getXKerning(const LLFontGlyphInfo* left_glyph_info, const LL FT_Vector delta; - llverify(!FT_Get_Kerning(mFTFace, left_glyph, right_glyph, ft_kerning_unfitted, &delta)); + llverify(!FT_Get_Kerning(mFTFace, left_glyph, right_glyph, FT_KERNING_UNFITTED, &delta)); - return delta.x*(1.f/64.f); + // Apply the FreeType auto-hinter's subpixel side-bearing correction between + // adjacent glyphs. When the hinter has shifted the right side of the left + // glyph or the left side of the right glyph, (rsb_delta - lsb_delta) is the + // sub-pixel nudge that keeps spacing visually even. + F32 delta_correction = 0.0f; + if (left_glyph_info && right_glyph_info) + { + // According to FreeType docs, these delta values should only trigger + // discrete ±1 pixel adjustments when they cross certain thresholds. + // Substructing delta_diff from delta.x doesn't work as well as treating + // it as a thresholds + S32 delta_diff = left_glyph_info->mRsbDelta - right_glyph_info->mLsbDelta; + if (delta_diff > 32) + delta_correction = -1.0f; + else if (delta_diff < -31) + delta_correction = 1.0f; + } + + // ft_kerning_unfitted mode always returns 26.6 fixed-point values + return (F32)(delta.x * (1.f / 64.f)) + delta_correction; } bool LLFontFreetype::hasGlyph(llwchar wch) const @@ -512,6 +557,11 @@ LLFontGlyphInfo* LLFontFreetype::addGlyphFromFont(const LLFontFreetype *fontp, l gi->mHeight = height; gi->mXBearing = fontp->mFTFace->glyph->bitmap_left; gi->mYBearing = fontp->mFTFace->glyph->bitmap_top; + // FreeType fills these when the glyph has been auto-hinted; they describe how + // much the hinter nudged the left/right side bearings (in 26.6 pixels). Keep + // them so inter-glyph spacing can be corrected in getXKerning(). + gi->mLsbDelta = (S32)fontp->mFTFace->glyph->lsb_delta; + gi->mRsbDelta = (S32)fontp->mFTFace->glyph->rsb_delta; // Convert these from 26.6 units to float pixels. gi->mXAdvance = fontp->mFTFace->glyph->advance.x / 64.f; gi->mYAdvance = fontp->mFTFace->glyph->advance.y / 64.f; @@ -637,7 +687,7 @@ void LLFontFreetype::renderGlyph(EFontGlyphType bitmap_type, U32 glyph_index, ll if (mFTFace == nullptr) return; - FT_Int32 load_flags = FT_LOAD_FORCE_AUTOHINT; + FT_Int32 load_flags = (FT_Int32)mHinting; if (EFontGlyphType::Color == bitmap_type) { // We may not actually get a color render so our caller should always examine mFTFace->glyph->bitmap.pixel_mode @@ -680,7 +730,7 @@ void LLFontFreetype::renderGlyph(EFontGlyphType bitmap_type, U32 glyph_index, ll void LLFontFreetype::reset(F32 vert_dpi, F32 horz_dpi) { resetBitmapCache(); - loadFace(mName, mPointSize, vert_dpi ,horz_dpi, mIsFallback, 0); + loadFace(mName, mPointSize, vert_dpi ,horz_dpi, mWeight, mIsFallback, 0, mHinting, mFontFlags); if (!mIsFallback) { // This is the head of the list - need to rebuild ourself and all fallbacks. @@ -848,6 +898,73 @@ void LLFontFreetype::setSubImageLuminanceAlpha(U32 x, U32 y, U32 bitmap_num, U32 } } +bool LLFontFreetype::setVariationAxis(const std::string& axis_tag, F32 value) +{ + if (!mFTFace) + return false; + + // Check if this is a variable font + FT_MM_Var* master = nullptr; + if (FT_Get_MM_Var(mFTFace, &master) != 0) + { + // Not a variable font - this is not an error, just silently skip + return false; + } + + // Find the axis by tag (e.g., "wght" for weight) + FT_UInt axis_index = 0; + bool found = false; + for (FT_UInt i = 0; i < master->num_axis; i++) + { + // Compare the 4-byte tag + if (master->axis[i].tag == FT_MAKE_TAG(axis_tag[0], axis_tag[1], axis_tag[2], axis_tag[3])) + { + axis_index = i; + found = true; + + // Clamp value to valid range for this axis + F32 min_val = master->axis[i].minimum / 65536.0f; + F32 max_val = master->axis[i].maximum / 65536.0f; + value = llclamp(value, min_val, max_val); + + break; + } + } + + if (!found) + { + FT_Done_MM_Var(gFTLibrary, master); + LL_WARNS_ONCE("Font") << "Axis '" << axis_tag << "' not found in font: " << mName << LL_ENDL; + return false; + } + + FT_UInt num_coords = master->num_axis; + FT_Fixed* coords = new FT_Fixed[num_coords]; + + // Get current coordinates + FT_Get_Var_Design_Coordinates(mFTFace, num_coords, coords); + + // Update the specific axis + coords[axis_index] = (FT_Fixed)(value * 65536.0f); + + // Set all coordinates + int error = FT_Set_Var_Design_Coordinates(mFTFace, num_coords, coords); + + delete[] coords; + FT_Done_MM_Var(gFTLibrary, master); + + if (error != 0) + { + LL_WARNS() << "Failed to set variation coordinates for " << axis_tag + << " = " << value << " in font: " << mName << LL_ENDL; + return false; + } + + LL_DEBUGS("Font") << "Set " << axis_tag << " = " << value + << " for font: " << mName << LL_ENDL; + return true; +} + namespace ll { @@ -873,30 +990,39 @@ namespace ll U8 const* LLFontManager::loadFont( std::string const &aFilename, long &a_Size) { - a_Size = 0; - std::map< std::string, std::shared_ptr<ll::fonts::LoadedFont> >::iterator itr = m_LoadedFonts.find( aFilename ); - if( itr != m_LoadedFonts.end() ) + try { - ++itr->second->mRefs; - // A possible overflow cannot happen here, as it is asserted that the size is less than std::numeric_limits<long>::max() a few lines below. - a_Size = static_cast<long>(itr->second->mSize); - return reinterpret_cast<U8 const*>(itr->second->mAddress.c_str()); - } + a_Size = 0; + std::map< std::string, std::shared_ptr<ll::fonts::LoadedFont> >::iterator itr = m_LoadedFonts.find(aFilename); + if (itr != m_LoadedFonts.end()) + { + ++itr->second->mRefs; + // A possible overflow cannot happen here, as it is asserted that the size is less than std::numeric_limits<long>::max() a few lines below. + a_Size = static_cast<long>(itr->second->mSize); + return reinterpret_cast<U8 const*>(itr->second->mAddress.c_str()); + } - auto strContent = LLFile::getContents(aFilename); + auto strContent = LLFile::getContents(aFilename); - if( strContent.empty() ) - return nullptr; + if (strContent.empty()) + return nullptr; - // For fontconfig a type of long is required, std::string::size() returns size_t. I think it is safe to limit this to 2GiB and not support fonts that huge (can that even be a thing?) - llassert_always( strContent.size() < std::numeric_limits<long>::max() ); + // For fontconfig a type of long is required, std::string::size() returns size_t. I think it is safe to limit this to 2GiB and not support fonts that huge (can that even be a thing?) + llassert_always(strContent.size() < std::numeric_limits<long>::max()); - a_Size = static_cast<long>(strContent.size()); + a_Size = static_cast<long>(strContent.size()); - auto pCache = std::make_shared<ll::fonts::LoadedFont>( aFilename, strContent, a_Size ); - itr = m_LoadedFonts.insert( std::make_pair( aFilename, pCache ) ).first; + auto pCache = std::make_shared<ll::fonts::LoadedFont>(aFilename, strContent, a_Size); + itr = m_LoadedFonts.insert(std::make_pair(aFilename, pCache)).first; - return reinterpret_cast<U8 const*>(itr->second->mAddress.c_str()); + return reinterpret_cast<U8 const*>(itr->second->mAddress.c_str()); + } + catch (const std::bad_alloc&) + { + LLError::LLUserWarningMsg::showOutOfMemory(); + LL_ERRS() << "Failed to load font. Out of memory." << LL_ENDL; + } + return nullptr; } void LLFontManager::unloadAllFonts() diff --git a/indra/llrender/llfontfreetype.h b/indra/llrender/llfontfreetype.h index a9b3a944ee..d2164e8fa2 100644 --- a/indra/llrender/llfontfreetype.h +++ b/indra/llrender/llfontfreetype.h @@ -27,13 +27,14 @@ #ifndef LL_LLFONTFREETYPE_H #define LL_LLFONTFREETYPE_H -#include <boost/unordered_map.hpp> #include "llpointer.h" #include "llstl.h" #include "llimagegl.h" #include "llfontbitmapcache.h" +#include <unordered_map> + // Hack. FT_Face is just a typedef for a pointer to a struct, // but there's no simple forward declarations file for FreeType, // and the main include file is 200K. @@ -42,6 +43,7 @@ struct FT_FaceRec_; typedef struct FT_FaceRec_* LLFT_Face; struct FT_StreamRec_; typedef struct FT_StreamRec_ LLFT_Stream; +enum class EFontHinting : S32; namespace ll { @@ -86,6 +88,8 @@ struct LLFontGlyphInfo S32 mYBitmapOffset; // Offset to the origin in the bitmap S32 mXBearing; // Distance from baseline to left in pixels S32 mYBearing; // Distance from baseline to top in pixels + S32 mLsbDelta; // FreeType subpixel left side bearing delta (26.6 units) + S32 mRsbDelta; // FreeType subpixel right side bearing delta (26.6 units) std::pair<EFontGlyphType, S32> mBitmapEntry; // Which bitmap in the bitmap cache contains this glyph }; @@ -99,7 +103,7 @@ public: // is_fallback should be true for fallback fonts that aren't used // to render directly (Unicode backup, primarily) - bool loadFace(const std::string& filename, F32 point_size, F32 vert_dpi, F32 horz_dpi, bool is_fallback, S32 face_n); + bool loadFace(const std::string& filename, F32 point_size, F32 vert_dpi, F32 horz_dpi, S32 weight, bool is_fallback, S32 face_n, EFontHinting hinting, S32 flags); S32 getNumFaces(const std::string& filename); @@ -161,9 +165,14 @@ private: void resetBitmapCache(); void setSubImageLuminanceAlpha(U32 x, U32 y, U32 bitmap_num, U32 width, U32 height, U8 *data, S32 stride = 0) const; bool setSubImageBGRA(U32 x, U32 y, U32 bitmap_num, U16 width, U16 height, const U8* data, U32 stride) const; + bool setVariationAxis(const std::string& axis_tag, F32 value); bool hasGlyph(llwchar wch) const; // Has a glyph for this character LLFontGlyphInfo* addGlyph(llwchar wch, EFontGlyphType glyph_type) const; // Add a new character to the font if necessary - LLFontGlyphInfo* addGlyphFromFont(const LLFontFreetype *fontp, llwchar wch, U32 glyph_index, EFontGlyphType bitmap_type) const; // Add a glyph from this font to the other (returns the glyph_index, 0 if not found) + LLFontGlyphInfo* addGlyphFromFont( + const LLFontFreetype *fontp, + llwchar wch, + U32 glyph_index, + EFontGlyphType bitmap_type) const; // Add a glyph from this font to the other (returns the glyph_index, 0 if not found) void renderGlyph(EFontGlyphType bitmap_type, U32 glyph_index, llwchar wch) const; void insertGlyphInfo(llwchar wch, LLFontGlyphInfo* gi) const; @@ -179,12 +188,15 @@ private: LLFT_Face mFTFace; bool mIsFallback; + EFontHinting mHinting; + S32 mFontFlags; + S32 mWeight = -1; typedef std::pair<LLPointer<LLFontFreetype>, char_functor_t> fallback_font_t; typedef std::vector<fallback_font_t> fallback_font_vector_t; fallback_font_vector_t mFallbackFonts; // A list of fallback fonts to look for glyphs in (for Unicode chars) // *NOTE: the same glyph can be present with multiple representations (but the pointer is always unique) - typedef boost::unordered_multimap<llwchar, LLFontGlyphInfo*> char_glyph_info_map_t; + typedef std::unordered_multimap<llwchar, LLFontGlyphInfo*> char_glyph_info_map_t; mutable char_glyph_info_map_t mCharGlyphInfoMap; // Information about glyph location in bitmap mutable LLFontBitmapCache* mFontBitmapCachep; diff --git a/indra/llrender/llfontgl.cpp b/indra/llrender/llfontgl.cpp index 16eec1fdd2..5d99c35047 100644 --- a/indra/llrender/llfontgl.cpp +++ b/indra/llrender/llfontgl.cpp @@ -90,14 +90,14 @@ void LLFontGL::destroyGL() mFontFreetype->destroyGL(); } -bool LLFontGL::loadFace(const std::string& filename, F32 point_size, const F32 vert_dpi, const F32 horz_dpi, bool is_fallback, S32 face_n) +bool LLFontGL::loadFace(const std::string& filename, F32 point_size, const F32 vert_dpi, const F32 horz_dpi, S32 weight, bool is_fallback, S32 face_n, EFontHinting hinting, S32 flags) { if(mFontFreetype == reinterpret_cast<LLFontFreetype*>(NULL)) { mFontFreetype = new LLFontFreetype; } - return mFontFreetype->loadFace(filename, point_size, vert_dpi, horz_dpi, is_fallback, face_n); + return mFontFreetype->loadFace(filename, point_size, vert_dpi, horz_dpi, weight, is_fallback, face_n, hinting, flags); } S32 LLFontGL::getNumFaces(const std::string& filename) @@ -1115,7 +1115,14 @@ LLFontGL* LLFontGL::getFontSansSerifSmallItalic() //static LLFontGL* LLFontGL::getFontSansSerif() { - static LLFontGL* fontp = getFont(LLFontDescriptor("SansSerif","Medium",0)); + static LLFontGL* fontp = getFont(LLFontDescriptor("SansSerif","Small",0)); + return fontp; +} + +// static +LLFontGL* LLFontGL::getFontSansSerifMedium() +{ + static LLFontGL* fontp = getFont(LLFontDescriptor("SansSerif", "Medium", 0)); return fontp; } diff --git a/indra/llrender/llfontgl.h b/indra/llrender/llfontgl.h index 1c8e036f58..3b82d8e43d 100644 --- a/indra/llrender/llfontgl.h +++ b/indra/llrender/llfontgl.h @@ -87,7 +87,7 @@ public: void destroyGL(); - bool loadFace(const std::string& filename, F32 point_size, const F32 vert_dpi, const F32 horz_dpi, bool is_fallback, S32 face_n); + bool loadFace(const std::string& filename, F32 point_size, const F32 vert_dpi, const F32 horz_dpi, S32 weight, bool is_fallback, S32 face_n, EFontHinting hinting, S32 flags); S32 getNumFaces(const std::string& filename); S32 getCacheGeneration() const; @@ -204,6 +204,7 @@ public: static LLFontGL* getFontSansSerifSmallBold(); static LLFontGL* getFontSansSerifSmallItalic(); static LLFontGL* getFontSansSerif(); + static LLFontGL* getFontSansSerifMedium(); static LLFontGL* getFontSansSerifBig(); static LLFontGL* getFontSansSerifHuge(); static LLFontGL* getFontSansSerifBold(); diff --git a/indra/llrender/llfontregistry.cpp b/indra/llrender/llfontregistry.cpp index c48a389f6a..890308ab54 100644 --- a/indra/llrender/llfontregistry.cpp +++ b/indra/llrender/llfontregistry.cpp @@ -170,7 +170,7 @@ LLFontDescriptor LLFontDescriptor::normalize() const if (new_size != s_template_string && new_size.empty() && findSubString(new_name,"Monospace")) new_size = "Monospace"; if (new_size.empty()) - new_size = "Medium"; + new_size = "Small"; if (removeSubString(new_name,"Bold")) new_style |= LLFontGL::BOLD; @@ -181,16 +181,16 @@ LLFontDescriptor LLFontDescriptor::normalize() const return LLFontDescriptor(new_name,new_size,new_style, getFontFiles(), getFontCollectionFiles()); } -void LLFontDescriptor::addFontFile(const std::string& file_name, const std::string& char_functor) +void LLFontDescriptor::addFontFile(const std::string& file_name, EFontHinting hinting, S32 flags, F32 size_delta, S32 weight, const std::string& char_functor) { char_functor_map_t::const_iterator it = mCharFunctors.find(char_functor); - mFontFiles.push_back(LLFontFileInfo(file_name, (mCharFunctors.end() != it) ? it->second : nullptr)); + mFontFiles.push_back(LLFontFileInfo(file_name, hinting, flags, size_delta, weight, (mCharFunctors.end() != it) ? it->second : nullptr)); } -void LLFontDescriptor::addFontCollectionFile(const std::string& file_name, const std::string& char_functor) +void LLFontDescriptor::addFontCollectionFile(const std::string& file_name, EFontHinting hinting, S32 flags, F32 size_delta, S32 weight, const std::string& char_functor) { char_functor_map_t::const_iterator it = mCharFunctors.find(char_functor); - mFontCollectionFiles.push_back(LLFontFileInfo(file_name, (mCharFunctors.end() != it) ? it->second : nullptr)); + mFontCollectionFiles.push_back(LLFontFileInfo(file_name, hinting, flags, size_delta, weight, (mCharFunctors.end() != it) ? it->second : nullptr)); } LLFontRegistry::LLFontRegistry(bool create_gl_textures) @@ -289,23 +289,69 @@ bool font_desc_init_from_xml(LLXMLNodePtr node, LLFontDescriptor& desc) { std::string font_file_name = child->getTextContents(); std::string char_functor; + EFontHinting hinting = EFontHinting::FORCE_AUTOHINT; + S32 flags = 0; + S32 weight = -1; if (child->hasAttribute("functor")) { child->getAttributeString("functor", char_functor); } + if (child->hasAttribute("font_hinting")) + { + std::string attr_hinting; + child->getAttributeString("font_hinting", attr_hinting); + LLStringUtil::toLower(attr_hinting); + + if (attr_hinting == "default") + { + hinting = EFontHinting::DEFAULT; + } + else if (attr_hinting == "force_auto") + { + hinting = EFontHinting::FORCE_AUTOHINT; + } + else if (attr_hinting == "no_hinting") + { + hinting = EFontHinting::NO_HINTING; + } + } + + if (child->hasAttribute("flags")) + { + std::string attr_flags; + child->getAttributeString("flags", attr_flags); + LLStringUtil::toLower(attr_flags); + + if (attr_flags == "bold") + { + flags |= LLFontGL::BOLD; + } + } + + F32 size_delta = 0.f; + if (child->hasAttribute("size_delta")) + { + child->getAttributeF32("size_delta", size_delta); + } + + if (child->hasAttribute("font_weight")) + { + child->getAttributeS32("font_weight", weight); + } + if (child->hasAttribute("load_collection")) { bool col = false; child->getAttributeBOOL("load_collection", col); if (col) { - desc.addFontCollectionFile(font_file_name, char_functor); + desc.addFontCollectionFile(font_file_name, hinting, flags, size_delta, weight, char_functor); } } - desc.addFontFile(font_file_name, char_functor); + desc.addFontFile(font_file_name, hinting, flags, size_delta, weight, char_functor); } else if (child->hasName("os")) { @@ -462,7 +508,7 @@ LLFontGL *LLFontRegistry::createFont(const LLFontDescriptor& desc) // Add ultimate fallback list - generated dynamically on linux, // null elsewhere. std::transform(getUltimateFallbackList().begin(), getUltimateFallbackList().end(), std::back_inserter(font_files), - [](const std::string& file_name) { return LLFontFileInfo(file_name); }); + [](const std::string& file_name) { return LLFontFileInfo(file_name, EFontHinting::FORCE_AUTOHINT, 0, 0.f, -1); }); // Load fonts based on names. if (font_files.empty()) @@ -517,8 +563,8 @@ LLFontGL *LLFontRegistry::createFont(const LLFontDescriptor& desc) { fontp = new LLFontGL; } - if (fontp->loadFace(font_path, point_size_scale, - LLFontGL::sVertDPI, LLFontGL::sHorizDPI, is_fallback, i)) + if (fontp->loadFace(font_path, point_size_scale + font_file_it->mSizeDelta, + LLFontGL::sVertDPI, LLFontGL::sHorizDPI, font_file_it->mWeight, is_fallback, i, font_file_it->mHinting, font_file_it->mFlags)) { is_font_loaded = true; if (is_first_found) diff --git a/indra/llrender/llfontregistry.h b/indra/llrender/llfontregistry.h index 8bbf5aa30c..fcbb2667e4 100644 --- a/indra/llrender/llfontregistry.h +++ b/indra/llrender/llfontregistry.h @@ -34,22 +34,45 @@ class LLFontGL; typedef std::vector<std::string> string_vec_t; +enum class EFontHinting : S32 +{ + DEFAULT = 0, + NO_HINTING = 0x8000U, + FORCE_AUTOHINT = 0x20, +}; + struct LLFontFileInfo { - LLFontFileInfo(const std::string& file_name, const std::function<bool(llwchar)>& char_functor = nullptr) + LLFontFileInfo(const std::string& file_name, EFontHinting hinting, S32 flags, F32 size_delta, S32 weight, const std::function<bool(llwchar)>& char_functor = nullptr) : FileName(file_name) , CharFunctor(char_functor) + , mHinting(hinting) + , mFlags(flags) + , mSizeDelta(size_delta) + , mWeight(weight) { } - LLFontFileInfo(const LLFontFileInfo& ffi) + LLFontFileInfo(const LLFontFileInfo& ffi, EFontHinting hinting, S32 flags, F32 size_delta, S32 weight) : FileName(ffi.FileName) , CharFunctor(ffi.CharFunctor) + , mHinting(hinting) + , mFlags(flags) + , mSizeDelta(size_delta) + , mWeight(weight) { } std::string FileName; std::function<bool(llwchar)> CharFunctor; + EFontHinting mHinting; + S32 mFlags; + S32 mWeight; // -1 - default, whatever is in the file. + + // Not all fonts are the same size, Ex: dejavu is bigger than inter, + // so in some cases we want to adjust relative sizes to make characters + // from different files match. + F32 mSizeDelta; }; typedef std::vector<LLFontFileInfo> font_file_info_vec_t; @@ -71,10 +94,10 @@ public: const std::string& getSize() const { return mSize; } void setSize(const std::string& size) { mSize = size; } - void addFontFile(const std::string& file_name, const std::string& char_functor = LLStringUtil::null); + void addFontFile(const std::string& file_name, EFontHinting hinting, S32 flags, F32 size_delta, S32 weight, const std::string& char_functor = LLStringUtil::null); const font_file_info_vec_t & getFontFiles() const { return mFontFiles; } void setFontFiles(const font_file_info_vec_t& font_files) { mFontFiles = font_files; } - void addFontCollectionFile(const std::string& file_name, const std::string& char_functor = LLStringUtil::null); + void addFontCollectionFile(const std::string& file_name, EFontHinting hinting, S32 flags, F32 size_delta, S32 weight, const std::string& char_functor = LLStringUtil::null); const font_file_info_vec_t& getFontCollectionFiles() const { return mFontCollectionFiles; } void setFontCollectionFiles(const font_file_info_vec_t& font_collection_files) { mFontCollectionFiles = font_collection_files; } diff --git a/indra/llrender/llfontvertexbuffer.cpp b/indra/llrender/llfontvertexbuffer.cpp index a223509d30..2a0115265f 100644 --- a/indra/llrender/llfontvertexbuffer.cpp +++ b/indra/llrender/llfontvertexbuffer.cpp @@ -237,3 +237,80 @@ void LLFontVertexBuffer::renderBuffers() gGL.popUIMatrix(); } +// LLFontWidthBuffer +bool LLFontWidthBuffer::sEnableBufferCollection = true; + +LLFontWidthBuffer::LLFontWidthBuffer() +{ +} + +LLFontWidthBuffer::~LLFontWidthBuffer() +{ +} + +void LLFontWidthBuffer::reset() +{ + mLastFont = nullptr; + mLastOffset = 0; + mLastMaxChars = 0; + mLastNoPadding = false; + mWidth = -1.f; + mLastScaleX = 1.f; + mLastScaleY = 1.f; + mLastVertDPI = 0.f; + mLastHorizDPI = 0.f; + mLastResGeneration = 0; + mLastFontCacheGen = 0; +} + +F32 LLFontWidthBuffer::getWidth( + const LLFontGL* fontp, + const llwchar* wchars, + S32 begin_offset, + S32 max_chars, + bool no_padding) +{ + LL_PROFILE_ZONE_SCOPED_CATEGORY_UI; + if (!fontp || !wchars) + { + return 0.f; + } + + if (!sEnableBufferCollection) + { + return fontp->getWidthF32(wchars, begin_offset, max_chars, no_padding); + } + + // Check if we can use cached width + bool needs_recalc = (mWidth < 0.f) + || (mLastFont != fontp) + || (mLastOffset != begin_offset) + || (mLastMaxChars != max_chars) + || (mLastNoPadding != no_padding) + || (mLastScaleX != LLFontGL::sScaleX) + || (mLastScaleY != LLFontGL::sScaleY) + || (mLastVertDPI != LLFontGL::sVertDPI) + || (mLastHorizDPI != LLFontGL::sHorizDPI) + || (mLastResGeneration != LLFontGL::sResolutionGeneration) + || (mLastFontCacheGen != fontp->getCacheGeneration()); + + if (needs_recalc) + { + // Calculate width using the font + mWidth = fontp->getWidthF32(wchars, begin_offset, max_chars, no_padding); + + // Cache the parameters + mLastFont = fontp; + mLastOffset = begin_offset; + mLastMaxChars = max_chars; + mLastNoPadding = no_padding; + mLastScaleX = LLFontGL::sScaleX; + mLastScaleY = LLFontGL::sScaleY; + mLastVertDPI = LLFontGL::sVertDPI; + mLastHorizDPI = LLFontGL::sHorizDPI; + mLastResGeneration = LLFontGL::sResolutionGeneration; + mLastFontCacheGen = fontp->getCacheGeneration(); + } + + return mWidth; +} diff --git a/indra/llrender/llfontvertexbuffer.h b/indra/llrender/llfontvertexbuffer.h index a9e1e2337c..94b833d227 100644 --- a/indra/llrender/llfontvertexbuffer.h +++ b/indra/llrender/llfontvertexbuffer.h @@ -32,6 +32,11 @@ class LLVertexBufferData; +// Rendering fonts is expensive, this class is intended to store +// vertex buffers for rendered text, so that they can be reused. +// LLFontVertexBuffer tracks font and rendering parameters, but +// expects caller to track text changes and call reset() when +// text changes. class LLFontVertexBuffer { public: @@ -127,4 +132,45 @@ private: static bool sEnableBufferCollection; }; +// Extracting width from a font is expensive, and due to +// mechanics of font rendering, we need width separately +// and usually before rendering. +// LLFontWidthBuffer tracks font and rendering parameters, +// but expects caller to track text changes and call reset() +// when text changes. +class LLFontWidthBuffer +{ +public: + LLFontWidthBuffer(); + ~LLFontWidthBuffer(); + + void reset(); + + F32 getWidth(const LLFontGL* fontp, + const llwchar* wchars, + S32 begin_offset, + S32 max_chars, + bool no_padding); + + static void enableBufferCollection(bool enable) { sEnableBufferCollection = enable; } +private: + const LLFontGL* mLastFont = nullptr; + S32 mLastOffset = 0; + S32 mLastMaxChars = 0; + bool mLastNoPadding = false; + F32 mWidth = -1.f; + + // LLFontGL's values that affect width calculation + F32 mLastScaleX = 1.f; + F32 mLastScaleY = 1.f; + F32 mLastVertDPI = 0.f; + F32 mLastHorizDPI = 0.f; + S32 mLastResGeneration = 0; + + // Cache generation tracking + S32 mLastFontCacheGen = 0; + + static bool sEnableBufferCollection; +}; + #endif diff --git a/indra/llrender/llgl.cpp b/indra/llrender/llgl.cpp index b002d183ef..3a36e49029 100644 --- a/indra/llrender/llgl.cpp +++ b/indra/llrender/llgl.cpp @@ -2386,7 +2386,7 @@ void clear_glerror() // // Static members -boost::unordered_map<LLGLenum, LLGLboolean> LLGLState::sStateMap; +std::unordered_map<LLGLenum, LLGLboolean> LLGLState::sStateMap; GLboolean LLGLDepthTest::sDepthEnabled = GL_FALSE; // OpenGL default GLenum LLGLDepthTest::sDepthFunc = GL_LESS; // OpenGL default @@ -2433,7 +2433,7 @@ void LLGLState::resetTextureStates() void LLGLState::dumpStates() { LL_INFOS("RenderState") << "GL States:" << LL_ENDL; - for (boost::unordered_map<LLGLenum, LLGLboolean>::iterator iter = sStateMap.begin(); + for (std::unordered_map<LLGLenum, LLGLboolean>::iterator iter = sStateMap.begin(); iter != sStateMap.end(); ++iter) { LL_INFOS("RenderState") << llformat(" 0x%04x : %s",(S32)iter->first,iter->second?"true":"false") << LL_ENDL; @@ -2465,7 +2465,7 @@ void LLGLState::checkStates(GLboolean writeAlpha) //llassert_always(colorMask[2]); // llassert_always(colorMask[3] == writeAlpha); - for (boost::unordered_map<LLGLenum, LLGLboolean>::iterator iter = sStateMap.begin(); + for (std::unordered_map<LLGLenum, LLGLboolean>::iterator iter = sStateMap.begin(); iter != sStateMap.end(); ++iter) { LLGLenum state = iter->first; diff --git a/indra/llrender/llgl.h b/indra/llrender/llgl.h index 414c28481f..7e1f9eea24 100644 --- a/indra/llrender/llgl.h +++ b/indra/llrender/llgl.h @@ -31,7 +31,7 @@ #include <functional> #include <string> -#include <boost/unordered_map.hpp> +#include <unordered_map> #include <list> #include "llerror.h" @@ -162,7 +162,7 @@ void clear_glerror(); //#if !LL_RELEASE_FOR_DOWNLOAD -#if 1 +#if 0 # define LOG_GLERROR(COMMENT) log_glerror(COMMENT) # define stop_glerror() log_glerror("stop") # define llglassertok() log_glerror("ASSERTOK") @@ -248,7 +248,7 @@ public: static void checkStates(GLboolean writeAlpha = GL_TRUE); protected: - static boost::unordered_map<LLGLenum, LLGLboolean> sStateMap; + static std::unordered_map<LLGLenum, LLGLboolean> sStateMap; public: enum { CURRENT_STATE = -2, DISABLED_STATE = 0, ENABLED_STATE = 1 }; diff --git a/indra/llrender/llglslshader.cpp b/indra/llrender/llglslshader.cpp index 39bd9cbfed..7ec23f5da0 100644 --- a/indra/llrender/llglslshader.cpp +++ b/indra/llrender/llglslshader.cpp @@ -1098,7 +1098,14 @@ void LLGLSLShader::bind() { sCurBoundShaderPtr->readProfileQuery(); } - LLVertexBuffer::unbind(); + // Apple's OpenGL-on-Metal layer pays a high price for rebinding buffer + // objects on every shader transition. LLVertexBuffer tracks the + // attribute formats needed by the new shader, so the current buffer + // can remain bound until a draw actually selects a different one. + if (!gGLManager.mIsApple) + { + LLVertexBuffer::unbind(); + } glUseProgram(mProgramObject); sCurBoundShader = mProgramObject; sCurBoundShaderPtr = this; @@ -1194,22 +1201,22 @@ S32 LLGLSLShader::bindTexture(S32 uniform, LLRenderTarget* texture, bool depth, return -1; } - S32 channel = getTextureChannel(uniform); + uniform = getTextureChannel(uniform); - if (channel > -1) + if (uniform > -1) { if (depth) { - gGL.getTexUnit(channel)->bind(texture, true); + gGL.getTexUnit(uniform)->bind(texture, true); } else { bool has_mips = mode == LLTexUnit::TFO_TRILINEAR || mode == LLTexUnit::TFO_ANISOTROPIC; - gGL.getTexUnit(channel)->bindManual(texture->getUsage(), texture->getTexture(index), has_mips); + gGL.getTexUnit(uniform)->bindManual(texture->getUsage(), texture->getTexture(index), has_mips); } - gGL.getTexUnit(channel)->setTextureFilteringOption(mode); + gGL.getTexUnit(uniform)->setTextureFilteringOption(mode); } - return channel; + return uniform; } S32 LLGLSLShader::bindTexture(const std::string& uniform, LLRenderTarget* texture, bool depth, LLTexUnit::eTextureFilterOptions mode) diff --git a/indra/llrender/llimagegl.cpp b/indra/llrender/llimagegl.cpp index bc80bd9556..1942ddb648 100644 --- a/indra/llrender/llimagegl.cpp +++ b/indra/llrender/llimagegl.cpp @@ -1955,7 +1955,7 @@ bool LLImageGL::readBackRaw(S32 discard_level, LLImageRaw* imageraw, bool compre LLGLint is_compressed = 0; if (compressed_ok) { - glGetTexLevelParameteriv(mTarget, is_compressed, GL_TEXTURE_COMPRESSED, (GLint*)&is_compressed); + glGetTexLevelParameteriv(mTarget, gl_discard, GL_TEXTURE_COMPRESSED, (GLint*)&is_compressed); } //----------------------------------------------------------------------------------------------- @@ -2300,7 +2300,7 @@ void LLImageGL::calcAlphaChannelOffsetAndStride() void LLImageGL::analyzeAlpha(const void* data_in, U32 w, U32 h) { - if(sSkipAnalyzeAlpha || !mNeedsAlphaAndPickMask) + if(!data_in || sSkipAnalyzeAlpha || !mNeedsAlphaAndPickMask) { return ; } diff --git a/indra/llrender/llrender.cpp b/indra/llrender/llrender.cpp index 658947d531..bfe0586ff0 100644 --- a/indra/llrender/llrender.cpp +++ b/indra/llrender/llrender.cpp @@ -1788,7 +1788,16 @@ LLVertexBuffer* LLRender::genBuffer(U32 attribute_mask, S32 count) LLVertexBuffer * vb = new LLVertexBuffer(attribute_mask); vb->allocateBuffer(count, 0); - vb->setBuffer(); + // Non-Apple path uses glBufferSubData inside setXxxData, so the VBO + // must already be bound. On Apple, the VBO is lazily created in + // _unmapBuffer (LLAppleVBOPool); calling setBuffer() here would bind + // mGLBuffer == 0 and then setupVertexBuffer would issue + // glVertexAttribIPointer with a non-null offset against no bound + // GL_ARRAY_BUFFER -> GL_INVALID_OPERATION in core profile. + if (!gGLManager.mIsApple) + { + vb->setBuffer(); + } vb->setPositionData(mVerticesp.get()); @@ -1802,8 +1811,11 @@ LLVertexBuffer* LLRender::genBuffer(U32 attribute_mask, S32 count) vb->setColorData(mColorsp.get()); } - if(gGLManager.mIsApple && LLVertexBuffer::getVertexBufferMode() == 0) + if(gGLManager.mIsApple) { + // unmapBuffer creates the GL buffer, uploads, and leaves it bound, + // drawBuffer's later setBuffer() then runs setupVertexBuffer against + // a valid VBO. vb->unmapBuffer(); } diff --git a/indra/llrender/llshadermgr.cpp b/indra/llrender/llshadermgr.cpp index f50ce26793..fae2bd5034 100644 --- a/indra/llrender/llshadermgr.cpp +++ b/indra/llrender/llshadermgr.cpp @@ -1026,8 +1026,23 @@ void LLShaderMgr::initShaderCache(bool enabled, const LLUUID& old_cache_version, llifstream instream(meta_out_path, std::ifstream::in | std::ifstream::binary); LLSD in_data; - // todo: this is likely very expensive to parse, should use binary - LLSDSerialize::fromBinary(in_data, instream, LLSDSerialize::SIZE_UNLIMITED); + try + { + LLSDSerialize::fromBinary(in_data, instream, LLSDSerialize::SIZE_UNLIMITED); + } + catch( std::bad_alloc& ) + { + // Try to get a bit more memory back before we try to clear the cache. + in_data.clear(); + // Just in case it was somehow the cause, clear cache. + clearShaderCache(); + // If user run out of memory this early in init, + // we don't want to keep going just to crash again. + // Notify user and close. + LLError::LLUserWarningMsg::showOutOfMemory(); + LL_ERRS("ShaderMgr") << "Failed to parse shader cache metadata, potentially due to size. Purged cache." << LL_ENDL; + return; + } instream.close(); if (old_cache_version == current_cache_version diff --git a/indra/llrender/lltexturemanagerbridge.cpp b/indra/llrender/lltexturemanagerbridge.cpp index c243f0697a..67838418bf 100644 --- a/indra/llrender/lltexturemanagerbridge.cpp +++ b/indra/llrender/lltexturemanagerbridge.cpp @@ -24,6 +24,8 @@ * $/LicenseInfo$ */ +#include "linden_common.h" + #include "lltexturemanagerbridge.h" // Define a null texture manager bridge. Applications must provide their own bridge implementaton. diff --git a/indra/llrender/llvertexbuffer.cpp b/indra/llrender/llvertexbuffer.cpp index 4b62c3476f..62a102c166 100644 --- a/indra/llrender/llvertexbuffer.cpp +++ b/indra/llrender/llvertexbuffer.cpp @@ -262,7 +262,7 @@ static GLuint gen_buffer() LL_PROFILE_ZONE_SCOPED_CATEGORY_VERTEX; GLuint ret = 0; - constexpr U32 pool_size = 4096; + constexpr U32 pool_size = 32768; thread_local static GLuint sNamePool[pool_size]; thread_local static U32 sIndex = 0; @@ -271,19 +271,21 @@ static GLuint gen_buffer() { LL_PROFILE_ZONE_NAMED_CATEGORY_VERTEX("gen buffer"); sIndex = pool_size; -//#if !LL_DARWIN +#if !LL_DARWIN if (!gGLManager.mIsAMD) { glGenBuffers(pool_size, sNamePool); } else -//#endif { // work around for AMD driver bug for (U32 i = 0; i < pool_size; ++i) { glGenBuffers(1, sNamePool + i); } } +#else + glGenBuffers(pool_size, sNamePool); +#endif } ret = sNamePool[--sIndex]; @@ -344,7 +346,7 @@ public: void allocate(GLenum type, U32 size, GLuint& name, U8*& data) override { LL_PROFILE_ZONE_SCOPED_CATEGORY_VERTEX; - + STOP_GLERROR; llassert(type == GL_ARRAY_BUFFER || type == GL_ELEMENT_ARRAY_BUFFER); llassert(name == 0); // non zero name indicates a gl name that wasn't freed llassert(data == nullptr); // non null data indicates a buffer that wasn't freed @@ -358,12 +360,10 @@ public: if (type == GL_ARRAY_BUFFER) { - glGenBuffers(1, &name); LLVertexBuffer::sGLRenderBuffer = name; } else { - glGenBuffers(1, &name); LLVertexBuffer::sGLRenderIndices = name; } } @@ -385,7 +385,6 @@ public: { delete_buffers(1, &name); } - //LOG_GLERROR("LLAppleVBOPool::free()"); } }; @@ -458,7 +457,7 @@ public: mMisses++; name = gen_buffer(); glBindBuffer(type, name); - glBufferData(type, size, nullptr, GL_DYNAMIC_DRAW); + glBufferData(type, size, nullptr, GL_STREAM_DRAW); if (type == GL_ELEMENT_ARRAY_BUFFER) { LLVertexBuffer::sGLRenderIndices = name; @@ -612,8 +611,6 @@ public: static LLVBOPool* sVBOPool = nullptr; -static U32 sMPVertexBufferMode = 0; - void LLVertexBufferData::drawWithMatrix() { if (!mVB) @@ -690,6 +687,15 @@ U32 LLVertexBuffer::sGLRenderIndices = 0; U32 LLVertexBuffer::sLastMask = 0; U32 LLVertexBuffer::sVertexCount = 0; +// The viewer keeps a single VAO bound. Attribute pointer calls store state in +// that VAO and capture the current array buffer. Track the formats configured +// for sGLRenderBuffer to avoid resubmitting identical state to OpenGL-on-Metal. +// If VAO switching is introduced, invalidate this cache when the VAO changes. +static U32 sVertexAttribsConfigured = 0; +static U32 sColorPointerSource = 0; +static constexpr U32 COLOR_POINTER_COLOR = 1; +static constexpr U32 COLOR_POINTER_EMISSIVE = 2; + //NOTE: each component must be AT LEAST 4 bytes in size to avoid a performance penalty on AMD hardware const U32 LLVertexBuffer::sTypeSize[LLVertexBuffer::TYPE_MAX] = @@ -935,22 +941,21 @@ void LLVertexBuffer::drawArrays(U32 mode, U32 first, U32 count) const gGL.syncMatrices(); glDrawArrays(sGLMode[mode], first, count); - LOG_GLERROR("LLVertexBuffer::drawArrays()"); } //static -void LLVertexBuffer::initClass(LLWindow* window, U32 mode_) +void LLVertexBuffer::initClass(LLWindow* window) { llassert(sVBOPool == nullptr); - sMPVertexBufferMode = mode_; - - if (mode_ == 0 && gGLManager.mIsApple) +#if LL_DARWIN || LL_ARM64 + if (gGLManager.mIsApple) { LL_INFOS() << "VBO Pooling Disabled" << LL_ENDL; sVBOPool = new LLAppleVBOPool(); } else +#endif { LL_INFOS() << "VBO Pooling Enabled" << LL_ENDL; sVBOPool = new LLDefaultVBOPool(); @@ -968,18 +973,14 @@ void LLVertexBuffer::initClass(LLWindow* window, U32 mode_) } //static -U32 LLVertexBuffer::getVertexBufferMode() -{ - return sMPVertexBufferMode; -} - -//static void LLVertexBuffer::unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); sGLRenderBuffer = 0; sGLRenderIndices = 0; + sVertexAttribsConfigured = 0; + sColorPointerSource = 0; } //static @@ -1292,7 +1293,12 @@ U8* LLVertexBuffer::mapVertexBuffer(LLVertexBuffer::AttributeType type, U32 inde count = mNumVerts - index; } - if (!gGLManager.mIsApple || sMPVertexBufferMode == 1) +#if LL_DARWIN || LL_ARM64 + // Region tracking not needed on apple silicon - it recreates entire buffer + // While mIsApple can be encountered under windows, this is a + // macOS OpenGL behavior workaround. LL_ARM64 check might be not needed + if (!gGLManager.mIsApple) +#endif { U32 start = mOffsets[type] + sTypeSize[type] * index; U32 end = start + sTypeSize[type] * count-1; @@ -1329,7 +1335,9 @@ U8* LLVertexBuffer::mapIndexBuffer(U32 index, S32 count) count = mNumIndices-index; } - if (!gGLManager.mIsApple || sMPVertexBufferMode == 1) +#if LL_DARWIN || LL_ARM64 + if (!gGLManager.mIsApple) +#endif { U32 start = sizeof(U16) * index; U32 end = start + sizeof(U16) * count-1; @@ -1364,39 +1372,19 @@ U8* LLVertexBuffer::mapIndexBuffer(U32 index, S32 count) // dst -- mMappedData or mMappedIndexData void LLVertexBuffer::flush_vbo(GLenum target, U32 start, U32 end, void* data, U8* dst) { +#if LL_DARWIN || LL_ARM64 if (gGLManager.mIsApple) { - if(sMPVertexBufferMode == 1) - { - //LL_WARNS() << "flush_vbo mode 1" << LL_ENDL; - - U32 MapBits = GL_MAP_WRITE_BIT; - //U32 MapBits = GL_MAP_READ_BIT; - U32 buffer_size = end-start+1; - - U8 * mptr = NULL; - mptr = (U8*) glMapBufferRange( target, start, end-start+1, MapBits); - - if (mptr) - { - std::memcpy(mptr, (U8*) data, buffer_size); - if(!glUnmapBuffer(target)) LL_WARNS() << "glUnmapBuffer() failed" << LL_ENDL; - } - else LL_WARNS() << "glMapBufferRange() returned NULL" << LL_ENDL; - - } - else - { - //LL_WARNS() << "flush_vbo mode 0" << LL_ENDL; // on OS X, flush_vbo doesn't actually write to the GL buffer, so be sure to call // _mapBuffer to tag the buffer for flushing to GL _mapBuffer(); LL_PROFILE_ZONE_NAMED_CATEGORY_VERTEX("vb memcpy"); + STOP_GLERROR; // copy into mapped buffer memcpy(dst+start, data, end-start+1); - } } else +#endif { llassert(target == GL_ARRAY_BUFFER ? sGLRenderBuffer == mGLBuffer : sGLRenderIndices == mGLIndices); @@ -1451,48 +1439,54 @@ void LLVertexBuffer::_unmapBuffer() } }; - if (gGLManager.mIsApple && sMPVertexBufferMode == 0) +#if LL_DARWIN || LL_ARM64 + if (gGLManager.mIsApple) { - LOG_GLERROR("LLVertexBuffer::_unmapBuffer() - apple 1"); + STOP_GLERROR; if (mMappedData) { - if(mGLBuffer == 0) + if (mGLBuffer) { - LL_WARNS() << "mGLBuffer is ZERO in unmapbuffer" << LL_ENDL; - glGenBuffers(1, &mGLBuffer); + delete_buffers(1, &mGLBuffer); } + mGLBuffer = gen_buffer(); glBindBuffer(GL_ARRAY_BUFFER, mGLBuffer); sGLRenderBuffer = mGLBuffer; - glBufferData(GL_ARRAY_BUFFER, mSize, mMappedData, GL_STATIC_DRAW); + sVertexAttribsConfigured = 0; + sColorPointerSource = 0; + glBufferData(GL_ARRAY_BUFFER, mSize, mMappedData, GL_STREAM_DRAW ); } else if (mGLBuffer != sGLRenderBuffer) { glBindBuffer(GL_ARRAY_BUFFER, mGLBuffer); sGLRenderBuffer = mGLBuffer; + sVertexAttribsConfigured = 0; + sColorPointerSource = 0; } - LOG_GLERROR("LLVertexBuffer::_unmapBuffer() - apple 2"); + STOP_GLERROR; if (mMappedIndexData) { - if (mGLIndices == 0) + if (mGLIndices) { - LL_WARNS() << "mGLIndices is ZERO in unmapbuffer" << LL_ENDL; - glGenBuffers(1, &mGLIndices); + delete_buffers(1, &mGLIndices); } + mGLIndices = gen_buffer(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mGLIndices); sGLRenderIndices = mGLIndices; - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndicesSize, mMappedIndexData, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, mIndicesSize, mMappedIndexData, GL_STREAM_DRAW ); } else if (mGLIndices != sGLRenderIndices) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mGLIndices); sGLRenderIndices = mGLIndices; } - LOG_GLERROR("LLVertexBuffer::_unmapBuffer() - apple 3"); + STOP_GLERROR; } else +#endif // LL_DARWIN || LL_ARM64 { if (!mMappedVertexRegions.empty()) { @@ -1681,10 +1675,11 @@ bool LLVertexBuffer::getClothWeightStrider(LLStrider<LLVector4>& strider, U32 in // Set for rendering void LLVertexBuffer::setBuffer() { + STOP_GLERROR; if (mMapped) { - LL_WARNS() << "Missing call to unmapBuffer or flushBuffers" << LL_ENDL; + LL_WARNS_ONCE() << "Missing call to unmapBuffer or flushBuffers" << LL_ENDL; _unmapBuffer(); } @@ -1704,17 +1699,14 @@ void LLVertexBuffer::setBuffer() if (sGLRenderBuffer != mGLBuffer) { - if(mGLBuffer == 0) - { - LL_WARNS() << "mGLBuffer is ZERO: sGLRenderBuffer=" << sGLRenderBuffer << LL_ENDL; - } - glBindBuffer(GL_ARRAY_BUFFER, mGLBuffer); sGLRenderBuffer = mGLBuffer; + sVertexAttribsConfigured = 0; + sColorPointerSource = 0; setupVertexBuffer(); } - else if (sLastMask != data_mask) + else if (gGLManager.mIsApple || sLastMask != data_mask) { setupVertexBuffer(); sLastMask = data_mask; @@ -1726,141 +1718,120 @@ void LLVertexBuffer::setBuffer() sGLRenderIndices = mGLIndices; } - LOG_GLERROR("LLVertexBuffer::setBuffer()"); + STOP_GLERROR; } // virtual (default) void LLVertexBuffer::setupVertexBuffer() { - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer()"); + STOP_GLERROR; U8* base = nullptr; U32 data_mask = LLGLSLShader::sCurBoundShaderPtr->mAttributeMask; + U32 setup_mask = gGLManager.mIsApple ? + data_mask & ~sVertexAttribsConfigured : data_mask; - if (data_mask & MAP_NORMAL) + if (setup_mask & MAP_NORMAL) { AttributeType loc = TYPE_NORMAL; void* ptr = (void*)(base + mOffsets[TYPE_NORMAL]); glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, LLVertexBuffer::sTypeSize[TYPE_NORMAL], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_NORMAL"); } - if (data_mask & MAP_TEXCOORD3) + if (setup_mask & MAP_TEXCOORD3) { AttributeType loc = TYPE_TEXCOORD3; void* ptr = (void*)(base + mOffsets[TYPE_TEXCOORD3]); glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, LLVertexBuffer::sTypeSize[TYPE_TEXCOORD3], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_TEXCOORD3"); } - if (data_mask & MAP_TEXCOORD2) + if (setup_mask & MAP_TEXCOORD2) { AttributeType loc = TYPE_TEXCOORD2; void* ptr = (void*)(base + mOffsets[TYPE_TEXCOORD2]); glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, LLVertexBuffer::sTypeSize[TYPE_TEXCOORD2], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_TEXCOORD2"); } - if (data_mask & MAP_TEXCOORD1) + if (setup_mask & MAP_TEXCOORD1) { AttributeType loc = TYPE_TEXCOORD1; void* ptr = (void*)(base + mOffsets[TYPE_TEXCOORD1]); glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, LLVertexBuffer::sTypeSize[TYPE_TEXCOORD1], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_TEXCOORD1"); } - if (data_mask & MAP_TANGENT) + if (setup_mask & MAP_TANGENT) { AttributeType loc = TYPE_TANGENT; void* ptr = (void*)(base + mOffsets[TYPE_TANGENT]); glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, LLVertexBuffer::sTypeSize[TYPE_TANGENT], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_TANGENT"); } - if (data_mask & MAP_TEXCOORD0) + if (setup_mask & MAP_TEXCOORD0) { AttributeType loc = TYPE_TEXCOORD0; - //glEnableVertexAttribArray(loc); void* ptr = (void*)(base + mOffsets[TYPE_TEXCOORD0]); glVertexAttribPointer(loc, 2, GL_FLOAT, GL_FALSE, LLVertexBuffer::sTypeSize[TYPE_TEXCOORD0], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_TEXCOORD0"); } - if (data_mask & MAP_COLOR) + const U32 desired_color_source = (data_mask & MAP_EMISSIVE) ? + COLOR_POINTER_EMISSIVE : + ((data_mask & MAP_COLOR) ? COLOR_POINTER_COLOR : 0); + if (desired_color_source && + (!gGLManager.mIsApple || desired_color_source != sColorPointerSource)) { AttributeType loc = TYPE_COLOR; - //bind emissive instead of color pointer if emissive is present - void* ptr = (data_mask & MAP_EMISSIVE) ? (void*)(base + mOffsets[TYPE_EMISSIVE]) : (void*)(base + mOffsets[TYPE_COLOR]); + void* ptr = desired_color_source == COLOR_POINTER_EMISSIVE ? + (void*)(base + mOffsets[TYPE_EMISSIVE]) : + (void*)(base + mOffsets[TYPE_COLOR]); glVertexAttribPointer(loc, 4, GL_UNSIGNED_BYTE, GL_TRUE, LLVertexBuffer::sTypeSize[TYPE_COLOR], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_COLOR"); + sColorPointerSource = desired_color_source; } - if (data_mask & MAP_EMISSIVE) + if (setup_mask & MAP_EMISSIVE) { AttributeType loc = TYPE_EMISSIVE; void* ptr = (void*)(base + mOffsets[TYPE_EMISSIVE]); glVertexAttribPointer(loc, 4, GL_UNSIGNED_BYTE, GL_TRUE, LLVertexBuffer::sTypeSize[TYPE_EMISSIVE], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_EMISSIVE"); - - if (!(data_mask & MAP_COLOR)) - { //map emissive to color channel when color is not also being bound to avoid unnecessary shader swaps - loc = TYPE_COLOR; - glVertexAttribPointer(loc, 4, GL_UNSIGNED_BYTE, GL_TRUE, LLVertexBuffer::sTypeSize[TYPE_EMISSIVE], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_COLOR"); - } } - if (data_mask & MAP_WEIGHT) + if (setup_mask & MAP_WEIGHT) { AttributeType loc = TYPE_WEIGHT; void* ptr = (void*)(base + mOffsets[TYPE_WEIGHT]); glVertexAttribPointer(loc, 1, GL_FLOAT, GL_FALSE, LLVertexBuffer::sTypeSize[TYPE_WEIGHT], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_WEIGHT"); } - if (data_mask & MAP_WEIGHT4) + if (setup_mask & MAP_WEIGHT4) { AttributeType loc = TYPE_WEIGHT4; void* ptr = (void*)(base + mOffsets[TYPE_WEIGHT4]); glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, LLVertexBuffer::sTypeSize[TYPE_WEIGHT4], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_WEIGHT4"); } - if (data_mask & MAP_JOINT) + if (setup_mask & MAP_JOINT) { AttributeType loc = TYPE_JOINT; void* ptr = (void*)(base + mOffsets[TYPE_JOINT]); glVertexAttribIPointer(loc, 4, GL_UNSIGNED_SHORT, LLVertexBuffer::sTypeSize[TYPE_JOINT], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_JOINT"); } - if (data_mask & MAP_CLOTHWEIGHT) + if (setup_mask & MAP_CLOTHWEIGHT) { AttributeType loc = TYPE_CLOTHWEIGHT; void* ptr = (void*)(base + mOffsets[TYPE_CLOTHWEIGHT]); glVertexAttribPointer(loc, 4, GL_FLOAT, GL_TRUE, LLVertexBuffer::sTypeSize[TYPE_CLOTHWEIGHT], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_CLOTHWEIGHT"); } - - if (data_mask & MAP_TEXTURE_INDEX) + if (setup_mask & MAP_TEXTURE_INDEX) { AttributeType loc = TYPE_TEXTURE_INDEX; void* ptr = (void*)(base + mOffsets[TYPE_VERTEX] + 12); glVertexAttribIPointer(loc, 1, GL_UNSIGNED_INT, LLVertexBuffer::sTypeSize[TYPE_VERTEX], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_TEXTURE_INDEX"); } - if (data_mask & MAP_VERTEX) + if (setup_mask & MAP_VERTEX) { AttributeType loc = TYPE_VERTEX; void* ptr = (void*)(base + mOffsets[TYPE_VERTEX]); glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, LLVertexBuffer::sTypeSize[TYPE_VERTEX], ptr); - - LOG_GLERROR("LLVertexBuffer::setupVertexBuffer TYPE_VERTEX"); } + if (gGLManager.mIsApple) + { + sVertexAttribsConfigured |= data_mask; + if (desired_color_source) + { + sVertexAttribsConfigured |= MAP_COLOR; + } + } + STOP_GLERROR; } void LLVertexBuffer::setPositionData(const LLVector4a* data) diff --git a/indra/llrender/llvertexbuffer.h b/indra/llrender/llvertexbuffer.h index 0928f801ad..f24d75e41d 100644 --- a/indra/llrender/llvertexbuffer.h +++ b/indra/llrender/llvertexbuffer.h @@ -101,7 +101,7 @@ public: LLVertexBuffer(const LLVertexBuffer& rhs) = delete; const LLVertexBuffer& operator=(const LLVertexBuffer& rhs) = delete; - static void initClass(LLWindow* window, U32 mode_ = 0); + static void initClass(LLWindow* window); static void cleanupClass(); static void setupClientArrays(U32 data_mask); static void drawArrays(U32 mode, const std::vector<LLVector3>& pos); @@ -120,8 +120,6 @@ public: // flush any pending mapped buffers static void flushBuffers(); - static U32 getVertexBufferMode(); - //WARNING -- when updating these enums you MUST // 1 - update LLVertexBuffer::sTypeSize // 2 - update LLVertexBuffer::vb_type_name |
