Optimise tex ids overlay rendering (part 2)

This commit is contained in:
UnknownShadow200 2020-05-02 12:56:18 +10:00
parent feeaf9c7b5
commit 1d7bc6953f

View File

@ -2980,17 +2980,16 @@ static void Overlay_MakeMainButtons(struct ButtonWidget* btns) {
/*########################################################################################################################* /*########################################################################################################################*
*------------------------------------------------------TexIdsOverlay------------------------------------------------------* *------------------------------------------------------TexIdsOverlay------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
#define TEXID_OVERLAY_MAX_PER_PAGE (ATLAS2D_TILES_PER_ROW * ATLAS2D_TILES_PER_ROW)
#define TEXID_OVERLAY_VERTICES_COUNT (TEXID_OVERLAY_MAX_PER_PAGE * 4)
static struct TexIdsOverlay { static struct TexIdsOverlay {
Screen_Body Screen_Body
GfxResourceID dynamicVb; int xOffset, yOffset, tileSize, textVertices;
int xOffset, yOffset, tileSize, baseTexLoc;
struct TextAtlas idAtlas; struct TextAtlas idAtlas;
struct TextWidget title; struct TextWidget title;
} TexIdsOverlay_Instance; } TexIdsOverlay_Instance;
#define TEXIDS_MAX_VERTICES (TEXTWIDGET_MAX + 4 * ATLAS1D_MAX_ATLASES)
#define TEXIDS_MAX_PER_PAGE (ATLAS2D_TILES_PER_ROW * ATLAS2D_TILES_PER_ROW)
#define TEXIDS_TEXT_VERTICES (10 * 4 + 90 * 8 + 412 * 12) /* '0'-'9' + '10'-'99' + '100'-'511' */
#define TEXIDS_MAX_VERTICES (TEXTWIDGET_MAX + 4 * ATLAS1D_MAX_ATLASES + TEXIDS_TEXT_VERTICES)
static void TexIdsOverlay_Layout(void* screen) { static void TexIdsOverlay_Layout(void* screen) {
struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen; struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen;
@ -3011,7 +3010,6 @@ static void TexIdsOverlay_Layout(void* screen) {
static void TexIdsOverlay_ContextLost(void* screen) { static void TexIdsOverlay_ContextLost(void* screen) {
struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen; struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen;
Screen_ContextLost(s); Screen_ContextLost(s);
Gfx_DeleteDynamicVb(&s->dynamicVb);
TextAtlas_Free(&s->idAtlas); TextAtlas_Free(&s->idAtlas);
} }
@ -3022,7 +3020,6 @@ static void TexIdsOverlay_ContextRecreated(void* screen) {
struct FontDesc textFont, titleFont; struct FontDesc textFont, titleFont;
Screen_CreateVb(screen); Screen_CreateVb(screen);
s->dynamicVb = Gfx_CreateDynamicVb(VERTEX_FORMAT_P3FT2FC4B, TEXID_OVERLAY_VERTICES_COUNT);
Drawer2D_MakeFont(&textFont, 8, FONT_STYLE_NORMAL); Drawer2D_MakeFont(&textFont, 8, FONT_STYLE_NORMAL);
Font_ReducePadding(&textFont, 4); Font_ReducePadding(&textFont, 4);
TextAtlas_Make(&s->idAtlas, &chars, &textFont, &prefix); TextAtlas_Make(&s->idAtlas, &chars, &textFont, &prefix);
@ -3047,7 +3044,7 @@ static void TexIdsOverlay_BuildTerrain(struct TexIdsOverlay* s, VertexP3fT2fC4b*
tex.Width = size; tex.Height = size; tex.Width = size; tex.Height = size;
for (row = 0; row < Atlas2D.RowsCount; row += ATLAS2D_TILES_PER_ROW) { for (row = 0; row < Atlas2D.RowsCount; row += ATLAS2D_TILES_PER_ROW) {
for (i = 0; i < TEXID_OVERLAY_MAX_PER_PAGE; i++) { for (i = 0; i < TEXIDS_MAX_PER_PAGE; i++) {
tex.X = xOffset + Atlas2D_TileX(i) * size; tex.X = xOffset + Atlas2D_TileX(i) * size;
tex.Y = s->yOffset + Atlas2D_TileY(i) * size; tex.Y = s->yOffset + Atlas2D_TileY(i) * size;
@ -3058,11 +3055,37 @@ static void TexIdsOverlay_BuildTerrain(struct TexIdsOverlay* s, VertexP3fT2fC4b*
Gfx_Make2DQuad(&tex, PACKEDCOL_WHITE, ptr); Gfx_Make2DQuad(&tex, PACKEDCOL_WHITE, ptr);
} }
baseLoc += TEXID_OVERLAY_MAX_PER_PAGE; baseLoc += TEXIDS_MAX_PER_PAGE;
xOffset += size * ATLAS2D_TILES_PER_ROW; xOffset += size * ATLAS2D_TILES_PER_ROW;
} }
} }
static void TexIdsOverlay_BuildText(struct TexIdsOverlay* s, VertexP3fT2fC4b** ptr) {
struct TextAtlas* idAtlas;
VertexP3fT2fC4b* beg;
int xOffset, size, row;
int x, y, id = 0;
size = s->tileSize;
xOffset = s->xOffset;
idAtlas = &s->idAtlas;
beg = *ptr;
for (row = 0; row < Atlas2D.RowsCount; row += ATLAS2D_TILES_PER_ROW) {
idAtlas->tex.Y = s->yOffset + (size - idAtlas->tex.Height);
for (y = 0; y < ATLAS2D_TILES_PER_ROW; y++) {
for (x = 0; x < ATLAS2D_TILES_PER_ROW; x++) {
idAtlas->curX = xOffset + size * x + 3; /* offset text by 3 pixels */
TextAtlas_AddInt(idAtlas, id++, ptr);
}
idAtlas->tex.Y += size;
}
xOffset += size * ATLAS2D_TILES_PER_ROW;
}
s->textVertices = (int)(*ptr - beg);
}
static void TexIdsOverlay_BuildMesh(void* screen) { static void TexIdsOverlay_BuildMesh(void* screen) {
struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen; struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen;
VertexP3fT2fC4b* data; VertexP3fT2fC4b* data;
@ -3074,6 +3097,7 @@ static void TexIdsOverlay_BuildMesh(void* screen) {
Widget_BuildMesh(&s->title, ptr); Widget_BuildMesh(&s->title, ptr);
TexIdsOverlay_BuildTerrain(s, ptr); TexIdsOverlay_BuildTerrain(s, ptr);
TexIdsOverlay_BuildText(s, ptr);
Gfx_UnlockDynamicVb(s->vb); Gfx_UnlockDynamicVb(s->vb);
} }
@ -3088,38 +3112,10 @@ static int TexIdsOverlay_RenderTerrain(struct TexIdsOverlay* s, int offset) {
return offset; return offset;
} }
static void TexIdsOverlay_RenderTextOverlay(struct TexIdsOverlay* s) {
VertexP3fT2fC4b vertices[TEXID_OVERLAY_VERTICES_COUNT];
VertexP3fT2fC4b* ptr = vertices;
struct TextAtlas* idAtlas;
int size, count;
int x, y, id;
size = s->tileSize;
idAtlas = &s->idAtlas;
idAtlas->tex.Y = s->yOffset + (size - idAtlas->tex.Height);
for (y = 0; y < ATLAS2D_TILES_PER_ROW; y++) {
for (x = 0; x < ATLAS2D_TILES_PER_ROW; x++) {
idAtlas->curX = s->xOffset + size * x + 3; /* offset text by 3 pixels */
id = x + y * ATLAS2D_TILES_PER_ROW;
TextAtlas_AddInt(idAtlas, id + s->baseTexLoc, &ptr);
}
idAtlas->tex.Y += size;
if ((y % 4) != 3) continue;
Gfx_BindTexture(idAtlas->tex.ID);
count = (int)(ptr - vertices);
Gfx_UpdateDynamicVb_IndexedTris(s->dynamicVb, vertices, count);
ptr = vertices;
}
}
static void TexIdsOverlay_OnAtlasChanged(void* screen) { static void TexIdsOverlay_OnAtlasChanged(void* screen) {
struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen; struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen;
s->dirty = true; s->dirty = true;
/* Atlas may have 256 or 512 textures, which changes xOffset */ /* Atlas may have 256 or 512 textures, which changes s->xOffset */
/* This can resize the position of the 'pages', so just re-layout */ /* This can resize the position of the 'pages', so just re-layout */
TexIdsOverlay_Layout(screen); TexIdsOverlay_Layout(screen);
} }
@ -3142,7 +3138,7 @@ static void TexIdsOverlay_Free(void* screen) {
static void TexIdsOverlay_Render(void* screen, double delta) { static void TexIdsOverlay_Render(void* screen, double delta) {
struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen; struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen;
int rows, offset = 0, origXOffset; int offset = 0;
Menu_RenderBounds(); Menu_RenderBounds();
Gfx_SetTexturing(true); Gfx_SetTexturing(true);
@ -3153,16 +3149,8 @@ static void TexIdsOverlay_Render(void* screen, double delta) {
offset = Widget_Render2(&s->title, offset); offset = Widget_Render2(&s->title, offset);
offset = TexIdsOverlay_RenderTerrain(s, offset); offset = TexIdsOverlay_RenderTerrain(s, offset);
origXOffset = s->xOffset; Gfx_BindTexture(s->idAtlas.tex.ID);
s->baseTexLoc = 0; Gfx_DrawVb_IndexedTris_Range(s->textVertices, offset);
for (rows = Atlas2D.RowsCount; rows > 0; rows -= ATLAS2D_TILES_PER_ROW) {
TexIdsOverlay_RenderTextOverlay(s);
s->xOffset += s->tileSize * ATLAS2D_TILES_PER_ROW;
s->baseTexLoc += ATLAS2D_TILES_PER_ROW * ATLAS2D_TILES_PER_ROW;
}
s->xOffset = origXOffset;
Gfx_SetTexturing(false); Gfx_SetTexturing(false);
} }