Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icon atlas v2 #1738

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cont/base/bitmaps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(FILES
bitmaps/groundring.tga
bitmaps/guiicon_move.bmp
bitmaps/detailtex2.bmp
bitmaps/defaultradardot.png
bitmaps/graphPaper.bmp
bitmaps/ui/background/background.jpg
bitmaps/wake.tga
Expand Down
Binary file added cont/base/bitmaps/bitmaps/defaultradardot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions cont/base/springcontent/shaders/GLSL/Icons2DVS.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 150 compatibility

// VS input attributes
in vec2 pos;
in vec3 uvw;
in vec4 color;

//uniform mat4 transformMatrix = mat4(1.0);

// VS output attributes
out vec3 vuvw;
out vec4 vColor;

void main() {
vuvw = uvw;
vColor = color;
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos.x, pos.y, 0.0, 1.0);
}
18 changes: 18 additions & 0 deletions cont/base/springcontent/shaders/GLSL/Icons3DVS.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 150 compatibility

// VS input attributes
in vec3 pos;
in vec3 uvw;
in vec4 color;

//uniform mat4 transformMatrix = mat4(1.0);

// VS output attributes
out vec3 vuvw;
out vec4 vColor;

void main() {
vuvw = uvw;
vColor = color;
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
}
34 changes: 34 additions & 0 deletions cont/base/springcontent/shaders/GLSL/IconsFS.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#version 150

uniform sampler2D mainTex;
uniform sampler2D custTex;

uniform vec4 ucolor = vec4(1.0);

// FS input attributes
in vec3 vuvw;
in vec4 vColor;

// FS output attributes
out vec4 outColor;

uniform vec4 alphaCtrl = vec4(0.0, 0.0, 0.0, 1.0); //always pass

bool AlphaDiscard(float a) {
float alphaTestGT = float(a > alphaCtrl.x) * alphaCtrl.y;
float alphaTestLT = float(a < alphaCtrl.x) * alphaCtrl.z;

return ((alphaTestGT + alphaTestLT + alphaCtrl.w) == 0.0);
}

void main() {
if (vuvw.z == 0.0)
outColor = texture(mainTex, vuvw.xy);
else
outColor = texture(custTex, vuvw.xy);

outColor *= vColor * ucolor;

if (AlphaDiscard(outColor.a))
discard;
}
24 changes: 0 additions & 24 deletions rts/Game/UI/GuiHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2761,27 +2761,6 @@ static inline bool BindUnitTexByString(const std::string& str)
return true;
}


static inline bool BindIconTexByString(const std::string& str)
{
RECOIL_DETAILED_TRACY_ZONE;
char* endPtr;
const char* startPtr = str.c_str() + 1; // skip the '^'
const int unitDefID = (int)strtol(startPtr, &endPtr, 10);

if (endPtr == startPtr)
return false; // bad unitID spec

const UnitDef* ud = unitDefHandler->GetUnitDefByID(unitDefID);

if (ud == nullptr)
return false;

ud->iconType->BindTexture();
return true;
}


static inline bool BindLuaTexByString(const std::string& str)
{
RECOIL_DETAILED_TRACY_ZONE;
Expand Down Expand Up @@ -2820,9 +2799,6 @@ static bool BindTextureString(const std::string& str)
if (str[0] == '#')
return BindUnitTexByString(str);

if (str[0] == '^')
return BindIconTexByString(str);

if (str[0] == LuaTextures::prefix) // '!'
return BindLuaTexByString(str);

Expand Down
4 changes: 1 addition & 3 deletions rts/Game/UI/MiniMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class CUnit;
namespace icon {
class CIconData;
class IconData;
}

namespace Shader {
Expand Down Expand Up @@ -117,9 +117,7 @@ class CMiniMap : public CInputReceiver {
void DrawMinimizedButtonQuad() const;
void DrawMinimizedButtonLoop() const;

void DrawUnitHighlight(const CUnit* unit);
void DrawCircle(TypedRenderBuffer<VA_TYPE_C>& rb, const float3& pos, SColor color, float radius) const;
const icon::CIconData* GetUnitIcon(const CUnit* unit, float& scale) const;

void UpdateTextureCache();
void ResizeTextureCache();
Expand Down
31 changes: 0 additions & 31 deletions rts/Lua/LuaOpenGLUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,24 +409,6 @@ bool LuaOpenGLUtils::ParseTextureImage(lua_State* L, LuaMatTexture& texUnit, con
texUnit.data = reinterpret_cast<const void*>(ud);
} break;

case '^': {
// unit icon
char* endPtr;
const char* startPtr = image.c_str() + 1; // skip the '^'
const int unitDefID = (int)strtol(startPtr, &endPtr, 10);

if (endPtr == startPtr)
return false;

const UnitDef* ud = unitDefHandler->GetUnitDefByID(unitDefID);

if (ud == nullptr)
return false;

texUnit.type = LuaMatTexture::LUATEX_UNITRADARICON;
texUnit.data = reinterpret_cast<const void*>(ud);
} break;

case '$': {
switch ((texUnit.type = GetLuaMatTextureType(image))) {
case LuaMatTexture::LUATEX_NONE: {
Expand Down Expand Up @@ -538,10 +520,6 @@ GLuint LuaMatTexture::GetTextureID() const
if (unitDefHandler != nullptr)
texID = CUnitDrawer::GetUnitDefImage(reinterpret_cast<const UnitDef*>(data));
} break;
case LUATEX_UNITRADARICON: {
texID = (reinterpret_cast<const UnitDef*>(data))->iconType->GetTextureID();
} break;


// cubemap textures
case LUATEX_MAP_REFLECTION: {
Expand Down Expand Up @@ -694,8 +672,6 @@ GLuint LuaMatTexture::GetTextureTarget() const
case LUATEX_3DOTEXTURE:

case LUATEX_UNITBUILDPIC:
case LUATEX_UNITRADARICON:


case LUATEX_SHADOWMAP:
case LUATEX_SHADOWCOLOR:
Expand Down Expand Up @@ -884,12 +860,6 @@ std::tuple<int, int, int> LuaMatTexture::GetSize() const
return ReturnHelper(bp->imageSizeX, bp->imageSizeY);
}
} break;
case LUATEX_UNITRADARICON: {
const UnitDef* ud = reinterpret_cast<const UnitDef*>(data);
const icon::CIcon& it = ud->iconType;
return ReturnHelper(it->GetSizeX(), it->GetSizeY());
} break;


case LUATEX_MAP_REFLECTION: {
return ReturnHelper(cubeMapHandler.GetReflectionTextureSize());
Expand Down Expand Up @@ -1038,7 +1008,6 @@ void LuaMatTexture::Print(const string& indent) const
STRING_CASE(typeName, LUATEX_UNITTEXTURE2);
STRING_CASE(typeName, LUATEX_3DOTEXTURE);
STRING_CASE(typeName, LUATEX_UNITBUILDPIC);
STRING_CASE(typeName, LUATEX_UNITRADARICON);

STRING_CASE(typeName, LUATEX_MAP_REFLECTION);
STRING_CASE(typeName, LUATEX_SKY_REFLECTION);
Expand Down
1 change: 0 additions & 1 deletion rts/Lua/LuaOpenGLUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class LuaMatTexture {
LUATEX_UNITTEXTURE2,
LUATEX_3DOTEXTURE,
LUATEX_UNITBUILDPIC,
LUATEX_UNITRADARICON,

LUATEX_MAP_REFLECTION,
LUATEX_SKY_REFLECTION,
Expand Down
6 changes: 3 additions & 3 deletions rts/Lua/LuaUnitDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ static int SafeIconType(lua_State* L, const void* data)
{
// the iconType is unsynced because LuaUI has SetUnitDefIcon()
if (!CLuaHandle::GetHandleSynced(L)) {
const icon::CIcon& iconType = *((const icon::CIcon*)data);
lua_pushsstring(L, iconType->GetName());
const auto& iconName = *((const std::string*)data);
lua_pushsstring(L, iconName);
return 1;
}
return 0;
Expand Down Expand Up @@ -599,7 +599,7 @@ ADD_BOOL("canAttackWater", canAttackWater); // CUSTOM
ADD_FUNCTION("moveDef", ud.pathType, MoveDefTable);
ADD_FUNCTION("shieldWeaponDef", ud.shieldWeaponDef, WeaponDefToID);
ADD_FUNCTION("stockpileWeaponDef", ud.stockpileWeaponDef, WeaponDefToID);
ADD_FUNCTION("iconType", ud.iconType, SafeIconType);
ADD_FUNCTION("iconType", ud.iconName, SafeIconType);
ADD_FUNCTION("collisionVolume", ud.collisionVolume, ColVolTable);
ADD_FUNCTION("selectionVolume", ud.selectionVolume, ColVolTable);

Expand Down
28 changes: 23 additions & 5 deletions rts/Lua/LuaUnsyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,10 @@ int LuaUnsyncedCtrl::SetFeatureSelectionVolumeData(lua_State* L)
* @number[opt] size
* @number[opt] dist
* @number[opt] radAdjust
* @number[opt=0.0] u0
* @number[opt=0.0] v0
* @number[opt=1.0] u1
* @number[opt=1.0] v1
*
* @treturn ?nil|bool added
*/
Expand All @@ -2213,7 +2217,13 @@ int LuaUnsyncedCtrl::AddUnitIcon(lua_State* L)

const bool radAdjust = luaL_optboolean(L, 5, false);

lua_pushboolean(L, icon::iconHandler.AddIcon(iconName, texName, size, dist, radAdjust));
const float u0 = luaL_optnumber(L, 6, 0.0f);
const float v0 = luaL_optnumber(L, 7, 0.0f);

const float u1 = luaL_optnumber(L, 8, 1.0f);
const float v1 = luaL_optnumber(L, 9, 1.0f);

lua_pushboolean(L, icon::iconHandler.AddIcon(1, iconName, texName, size, dist, radAdjust, u0, v0, u1, v1));
return 1;
}

Expand Down Expand Up @@ -2271,11 +2281,19 @@ int LuaUnsyncedCtrl::SetUnitDefIcon(lua_State* L)
if (ud == nullptr)
return 0;

ud->iconType = icon::iconHandler.GetIcon(luaL_checksstring(L, 2));
const auto iconName = luaL_checksstring(L, 2);
const auto& [found, _] = icon::iconHandler.FindIconIdx(iconName);

if (!found) {
luaL_error(L, "Invalid icon name \"%s\"", iconName.c_str());
return 0;
}

ud->iconName = iconName;

// set decoys to the same icon
if (ud->decoyDef != nullptr)
ud->decoyDef->iconType = ud->iconType;
ud->decoyDef->iconName = ud->iconName;

// spring::unordered_map<int, std::vector<int> >
const auto& decoyMap = unitDefHandler->GetDecoyDefIDs();
Expand All @@ -2286,11 +2304,11 @@ int LuaUnsyncedCtrl::SetUnitDefIcon(lua_State* L)

for (const int decoyDefID: decoySet) {
const UnitDef* decoyDef = unitDefHandler->GetUnitDefByID(decoyDefID);
decoyDef->iconType = ud->iconType;
decoyDef->iconName = ud->iconName;
}
}

unitDrawer->UpdateUnitDefMiniMapIcons(ud);
unitDrawer->UpdateUnitIconsByUnitDef(ud);
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions rts/Rendering/Env/Decals/GroundDecalHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void CGroundDecalHandler::AddTexToAtlas(const std::string& name, const std::stri
try {
const auto& [bm, fn] = LoadTexture(filename, convertOldBMP);
const auto& decalAtlas = (mainTex ? atlasMain : atlasNorm);
decalAtlas->AddTexFromBitmap(name, bm);
decalAtlas->AddTexFromBitmap(name, filename, bm);
}
catch (const content_error& err) {
LOG_L(L_WARNING, "%s", err.what());
Expand Down Expand Up @@ -341,11 +341,11 @@ void CGroundDecalHandler::AddFallbackTextures()
RECOIL_DETAILED_TRACY_ZONE;
{
const auto minDim = std::max(atlasMain->GetMinDim(), 32);
atlasMain->AddTex("%FB_MAIN%", minDim, minDim, SColor(255, 0, 0, 255));
atlasMain->AddTex("%FB_MAIN%", "%FB_MAIN%", minDim, minDim, SColor(255, 0, 0, 255));
}
{
const auto minDim = std::max(atlasNorm->GetMinDim(), 32);
atlasNorm->AddTex("%FB_NORM%", minDim, minDim, SColor(128, 128, 255, 0));
atlasNorm->AddTex("%FB_NORM%", "%FB_NORM%", minDim, minDim, SColor(128, 128, 255, 0));
}
}

Expand Down
8 changes: 5 additions & 3 deletions rts/Rendering/Fonts/CFontTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ void CFontTexture::LoadWantedGlyphs(const std::vector<char32_t>& wanted)
thisGlyph.texCord = IGlyphRect(texpos [0], texpos [1], texpos [2] - texpos [0], texpos [3] - texpos [1]);
thisGlyph.shadowTexCord = IGlyphRect(texpos2[0], texpos2[1], texpos2[2] - texpos2[0], texpos2[3] - texpos2[1]);

const size_t glyphIdx = reinterpret_cast<size_t>(atlasAlloc.GetEntryData(glyphName));
const size_t glyphIdx = glyphNameToIdx[glyphName];

assert(glyphIdx < atlasGlyphs.size());

Expand All @@ -851,6 +851,7 @@ void CFontTexture::LoadWantedGlyphs(const std::vector<char32_t>& wanted)
}

atlasAlloc.clear();
glyphNameToIdx.clear();
atlasGlyphs.clear();
}

Expand Down Expand Up @@ -926,8 +927,9 @@ void CFontTexture::LoadGlyph(std::shared_ptr<FontFace>& f, char32_t ch, unsigned
// store glyph bitmap (index) in allocator until the next LoadWantedGlyphs call
atlasGlyphs.emplace_back(slot->bitmap.buffer, width, height, 1);

atlasAlloc.AddEntry(IntToString(ch) , int2(width , height ), reinterpret_cast<void*>(atlasGlyphs.size() - 1));
atlasAlloc.AddEntry(IntToString(ch) + "sh", int2(width + olSize, height + olSize) );
atlasAlloc.AddEntry(IntToString(ch) , int2(width , height ));
atlasAlloc.AddEntry(IntToString(ch) + "sh", int2(width + olSize, height + olSize));
glyphNameToIdx[IntToString(ch)] = atlasGlyphs.size() - 1;
#endif
}

Expand Down
1 change: 1 addition & 0 deletions rts/Rendering/Fonts/CFontTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class CFontTexture
std::vector<CBitmap> atlasGlyphs;

CRowAtlasAlloc atlasAlloc;
spring::unordered_map<std::string, size_t> glyphNameToIdx;

CBitmap atlasUpdate;
CBitmap atlasUpdateShadow;
Expand Down
30 changes: 16 additions & 14 deletions rts/Rendering/GL/RenderBuffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@

#include "System/Misc/TracyDefs.h"

std::array<std::unique_ptr<RenderBuffer>, 13> RenderBuffer::typedRenderBuffers;
std::array<std::unique_ptr<RenderBuffer>, 15> RenderBuffer::typedRenderBuffers;

void RenderBuffer::InitStatic()
{
RECOIL_DETAILED_TRACY_ZONE;
RenderBuffer::typedRenderBuffers = {
std::make_unique<TypedRenderBuffer<VA_TYPE_0 >>(1 << 16, 1 << 17),
std::make_unique<TypedRenderBuffer<VA_TYPE_C >>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_N >>(1 << 10, 1 << 11),
std::make_unique<TypedRenderBuffer<VA_TYPE_T >>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_T4 >>(1 << 16, 1 << 18),
std::make_unique<TypedRenderBuffer<VA_TYPE_TN >>(1 << 16, 1 << 17),
std::make_unique<TypedRenderBuffer<VA_TYPE_TC >>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_PROJ>>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_TNT >>(0 , 0 ),
std::make_unique<TypedRenderBuffer<VA_TYPE_2D0 >>(1 << 16, 1 << 17),
std::make_unique<TypedRenderBuffer<VA_TYPE_2DC >>(1 << 16, 1 << 17),
std::make_unique<TypedRenderBuffer<VA_TYPE_2DT >>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_2DTC>>(1 << 20, 1 << 21)
std::make_unique<TypedRenderBuffer<VA_TYPE_0 >>(1 << 16, 1 << 17),
std::make_unique<TypedRenderBuffer<VA_TYPE_C >>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_N >>(1 << 10, 1 << 11),
std::make_unique<TypedRenderBuffer<VA_TYPE_T >>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_T4 >>(1 << 16, 1 << 18),
std::make_unique<TypedRenderBuffer<VA_TYPE_TN >>(1 << 16, 1 << 17),
std::make_unique<TypedRenderBuffer<VA_TYPE_TC >>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_TC3 >>(1 << 10, 1 << 11),
std::make_unique<TypedRenderBuffer<VA_TYPE_PROJ >>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_TNT >>(0 , 0 ),
std::make_unique<TypedRenderBuffer<VA_TYPE_2D0 >>(1 << 16, 1 << 17),
std::make_unique<TypedRenderBuffer<VA_TYPE_2DC >>(1 << 16, 1 << 17),
std::make_unique<TypedRenderBuffer<VA_TYPE_2DT >>(1 << 20, 1 << 21),
std::make_unique<TypedRenderBuffer<VA_TYPE_2DTC >>(1 << 16, 1 << 17),
std::make_unique<TypedRenderBuffer<VA_TYPE_2DTC3>>(1 << 10, 1 << 11)
};
}

Expand Down
Loading