From 677ce3960cd13cbf994311c76d75b343b22fd480 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sat, 29 Sep 2012 10:40:11 +0200 Subject: Some fixes and optimizations - fixed 2nd texture setting - added padding to some structs for faster access - changed rendering primitives to glDrawArrays() - optimized texture modesetting calls - fixed some valgrind errors --- src/graphics/engine/engine.cpp | 56 +++++++++++++++++++++++++----------------- src/graphics/engine/engine.h | 2 ++ src/graphics/engine/text.cpp | 42 ++++++++++--------------------- src/graphics/engine/text.h | 5 +++- 4 files changed, 52 insertions(+), 53 deletions(-) (limited to 'src/graphics/engine') diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index d470b06..e34034d 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -217,6 +217,11 @@ CEngine::CEngine(CInstanceManager *iMan, CApplication *app) m_defaultTexParams.minFilter = TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; m_defaultTexParams.magFilter = TEX_MAG_FILTER_LINEAR; + m_terrainTexParams.format = TEX_IMG_AUTO; + m_terrainTexParams.mipmap = false; + m_terrainTexParams.minFilter = TEX_MIN_FILTER_LINEAR; + m_terrainTexParams.magFilter = TEX_MAG_FILTER_LINEAR; + m_objectTree.reserve(LEVEL1_PREALLOCATE_COUNT); m_objects.reserve(OBJECT_PREALLOCATE_COUNT); m_shadows.reserve(SHADOW_PREALLOCATE_COUNT); @@ -1854,8 +1859,7 @@ void CEngine::SetState(int state, const Color& color) params.colorOperation = TEX_MIX_OPER_MODULATE; params.colorArg1 = TEX_MIX_ARG_TEXTURE; params.colorArg2 = TEX_MIX_ARG_FACTOR; - params.alphaOperation = TEX_MIX_OPER_DEFAULT; - params.alphaOperation = TEX_MIX_OPER_REPLACE; // TODO: replace with src color ? + params.alphaOperation = TEX_MIX_OPER_DEFAULT; // TODO: replace with src color ? m_device->SetTextureEnabled(0, true); m_device->SetTextureStageParams(0, params); @@ -2012,7 +2016,7 @@ void CEngine::SetState(int state, const Color& color) params.colorArg1 = TEX_MIX_ARG_TEXTURE; params.colorArg2 = TEX_MIX_ARG_COMPUTED_COLOR; params.alphaOperation = TEX_MIX_OPER_DEFAULT; // TODO: ??? - m_device->SetTextureEnabled(0, true); + m_device->SetTextureEnabled(1, true); m_device->SetTextureStageParams(1, params); } else if ((state & ENG_RSTATE_DUAL_WHITE) && second) @@ -2022,7 +2026,7 @@ void CEngine::SetState(int state, const Color& color) params.colorArg1 = TEX_MIX_ARG_TEXTURE; params.colorArg2 = TEX_MIX_ARG_COMPUTED_COLOR; params.alphaOperation = TEX_MIX_OPER_DEFAULT; // TODO: ??? - m_device->SetTextureEnabled(0, true); + m_device->SetTextureEnabled(1, true); m_device->SetTextureStageParams(1, params); } else @@ -2032,25 +2036,13 @@ void CEngine::SetState(int state, const Color& color) if (state & ENG_RSTATE_WRAP) { - // TODO: separate function for setting wrap mode? - - TextureStageParams p1 = m_device->GetTextureStageParams(0); - p1.wrapS = p1.wrapT = TEX_WRAP_REPEAT; - m_device->SetTextureStageParams(0, p1); - - TextureStageParams p2 = m_device->GetTextureStageParams(1); - p2.wrapS = p2.wrapT = TEX_WRAP_REPEAT; - m_device->SetTextureStageParams(1, p2); + m_device->SetTextureStageWrap(0, TEX_WRAP_REPEAT, TEX_WRAP_REPEAT); + m_device->SetTextureStageWrap(1, TEX_WRAP_REPEAT, TEX_WRAP_REPEAT); } else // if (state & ENG_RSTATE_CLAMP) or otherwise { - TextureStageParams p1 = m_device->GetTextureStageParams(0); - p1.wrapS = p1.wrapT = TEX_WRAP_CLAMP; - m_device->SetTextureStageParams(0, p1); - - TextureStageParams p2 = m_device->GetTextureStageParams(1); - p2.wrapS = p2.wrapT = TEX_WRAP_CLAMP; - m_device->SetTextureStageParams(1, p2); + m_device->SetTextureStageWrap(0, TEX_WRAP_CLAMP, TEX_WRAP_CLAMP); + m_device->SetTextureStageWrap(1, TEX_WRAP_CLAMP, TEX_WRAP_CLAMP); } if (state & ENG_RSTATE_2FACE) @@ -2182,16 +2174,35 @@ bool CEngine::LoadAllTextures() EngineObjLevel1& p1 = m_objectTree[l1]; if (! p1.used) continue; + bool terrain = false; + + for (int l2 = 0; l2 < static_cast( p1.next.size() ); l2++) + { + EngineObjLevel2& p2 = p1.next[l2]; + if (! p2.used) continue; + + if (m_objects[p2.objRank].type == ENG_OBJTYPE_TERRAIN) + terrain = true; + } + if (! p1.tex1Name.empty()) { - p1.tex1 = LoadTexture(p1.tex1Name); + if (terrain) + p1.tex1 = LoadTexture(p1.tex1Name, m_terrainTexParams); + else + p1.tex1 = LoadTexture(p1.tex1Name); + if (! p1.tex1.Valid()) ok = false; } if (! p1.tex2Name.empty()) { - p1.tex2 = LoadTexture(p1.tex2Name); + if (terrain) + p1.tex2 = LoadTexture(p1.tex2Name, m_terrainTexParams); + else + p1.tex2 = LoadTexture(p1.tex2Name); + if (! p1.tex2.Valid()) ok = false; } @@ -2815,7 +2826,6 @@ void CEngine::Draw3DScene() if (m_shadowVisible) { - m_device->DebugHook(); m_lightMan->UpdateDeviceLights(ENG_OBJTYPE_TERRAIN); // Draw the terrain diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h index e188dea..6363fd3 100644 --- a/src/graphics/engine/engine.h +++ b/src/graphics/engine/engine.h @@ -1348,6 +1348,8 @@ protected: //! Default texture create params TextureCreateParams m_defaultTexParams; + //! Create params for terrain textures + TextureCreateParams m_terrainTexParams; //! Map of loaded textures (by name) std::map m_texNameMap; diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index a77a6fd..4deeb53 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -279,18 +279,14 @@ float CText::GetHeight(FontType font, float size) float CText::GetStringWidth(const std::string &text, std::map &format, float size) { - // TODO assert was commented as new code uses map not vector and if's size doesn't have to match text length - // this has to be tested if it's correct - //assert(StrUtils::Utf8StringLength(text) == format.size()); - float width = 0.0f; unsigned int index = 0; unsigned int fmtIndex = 0; while (index < text.length()) { FontType font = FONT_COLOBOT; - if (format.count(fmtIndex)) - font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + if (format.count(fmtIndex)) + font = static_cast(format[fmtIndex] & FONT_MASK_FONT); UTF8Char ch; @@ -350,10 +346,6 @@ float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset) int CText::Justify(const std::string &text, std::map &format, float size, float width) { - // TODO assert was commented as new code uses map not vector and if's size doesn't have to match text length - // this has to be tested if it's correct - //assert(StrUtils::Utf8StringLength(text) == format.size()); - float pos = 0.0f; int cut = 0; unsigned int index = 0; @@ -361,8 +353,8 @@ int CText::Justify(const std::string &text, std::map while (index < text.length()) { FontType font = FONT_COLOBOT; - if (format.count(fmtIndex)) - font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + if (format.count(fmtIndex)) + font = static_cast(format[fmtIndex] & FONT_MASK_FONT); UTF8Char ch; @@ -437,18 +429,14 @@ int CText::Justify(const std::string &text, FontType font, float size, float wid int CText::Detect(const std::string &text, std::map &format, float size, float offset) { - // TODO assert was commented as new code uses map not vector and if's size doesn't have to match text length - // this has to be tested if it's correct - //assert(StrUtils::Utf8StringLength(text) == format.size()); - float pos = 0.0f; unsigned int index = 0; unsigned int fmtIndex = 0; while (index < text.length()) { FontType font = FONT_COLOBOT; - if (format.count(fmtIndex)) - font = static_cast(format[fmtIndex] & FONT_MASK_FONT); + if (format.count(fmtIndex)) + font = static_cast(format[fmtIndex] & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) if (font == FONT_BUTTON) continue; @@ -514,13 +502,8 @@ int CText::Detect(const std::string &text, FontType font, float size, float offs void CText::DrawString(const std::string &text, std::map &format, float size, Math::Point pos, float width, int eol) { - // TODO assert was commented as new code uses map not vector and if's size doesn't have to match text length - // this has to be tested if it's correct - //assert(StrUtils::Utf8StringLength(text) == format.size()); - m_engine->SetState(ENG_RSTATE_TEXT); - FontType font = FONT_COLOBOT; float start = pos.x; unsigned int fmtIndex = 0; @@ -530,8 +513,8 @@ void CText::DrawString(const std::string &text, std::map(format[fmtIndex] & FONT_MASK_FONT); + if (format.count(fmtIndex)) + font = static_cast(format[fmtIndex] & FONT_MASK_FONT); // TODO: if (font == FONT_BUTTON) if (font == FONT_BUTTON) continue; @@ -566,17 +549,18 @@ void CText::DrawString(const std::string &text, std::map &chars) { unsigned int index = 0; - while (index < text.length()) + unsigned int totalLength = text.length(); + while (index < totalLength) { UTF8Char ch; int len = StrUtils::Utf8CharSizeAt(text, index); if (len >= 1) - ch.c1 = text[index]; + ch.c1 = text[index]; if (len >= 2) - ch.c2 = text[index+1]; + ch.c2 = text[index+1]; if (len >= 3) - ch.c3 = text[index+2]; + ch.c3 = text[index+2]; index += len; diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h index cc18f55..7fa8768 100644 --- a/src/graphics/engine/text.h +++ b/src/graphics/engine/text.h @@ -149,9 +149,12 @@ enum FontMask struct UTF8Char { char c1, c2, c3; + // Padding for 4-byte alignment + // It also seems to fix some problems reported by valgrind + char pad; explicit UTF8Char(char ch1 = '\0', char ch2 = '\0', char ch3 = '\0') - : c1(ch1), c2(ch2), c3(ch3) {} + : c1(ch1), c2(ch2), c3(ch3), pad('\0') {} inline bool operator<(const UTF8Char &other) const { -- cgit v1.2.3-1-g7c22