Optimise tex ids overlay rendering (part 1)

This commit is contained in:
UnknownShadow200 2020-05-01 22:14:09 +10:00
parent 3a725a390e
commit ab1cdd9dde

View File

@ -2990,6 +2990,7 @@ static struct TexIdsOverlay {
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)
static void TexIdsOverlay_ContextLost(void* screen) { static void TexIdsOverlay_ContextLost(void* screen) {
struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen; struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen;
@ -3009,6 +3010,7 @@ static void TexIdsOverlay_ContextRecreated(void* screen) {
size = (size / 8) * 8; size = (size / 8) * 8;
Math_Clamp(size, 8, 40); Math_Clamp(size, 8, 40);
Screen_CreateVb(screen);
s->dynamicVb = Gfx_CreateDynamicVb(VERTEX_FORMAT_P3FT2FC4B, TEXID_OVERLAY_VERTICES_COUNT); 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);
@ -3025,41 +3027,60 @@ static void TexIdsOverlay_ContextRecreated(void* screen) {
Font_Free(&titleFont); Font_Free(&titleFont);
} }
static void TexIdsOverlay_BuildMesh(void* screen) { } static void TexIdsOverlay_BuildTerrain(struct TexIdsOverlay* s, VertexP3fT2fC4b** ptr) {
static void TexIdsOverlay_RenderTerrain(struct TexIdsOverlay* s) {
PackedCol col = PACKEDCOL_WHITE;
VertexP3fT2fC4b vertices[TEXID_OVERLAY_VERTICES_COUNT];
VertexP3fT2fC4b* ptr;
struct Texture tex; struct Texture tex;
int size, count; int baseLoc, xOffset;
int i, idx, end; int i, row, size;
size = s->tileSize; size = s->tileSize;
baseLoc = 0;
xOffset = s->xOffset;
tex.uv.U1 = 0.0f; tex.uv.U2 = UV2_Scale; tex.uv.U1 = 0.0f; tex.uv.U2 = UV2_Scale;
tex.Width = size; tex.Height = size; tex.Width = size; tex.Height = size;
for (i = 0; i < TEXID_OVERLAY_MAX_PER_PAGE;) { for (row = 0; row < Atlas2D.RowsCount; row += ATLAS2D_TILES_PER_ROW) {
ptr = vertices; for (i = 0; i < TEXID_OVERLAY_MAX_PER_PAGE; i++) {
idx = Atlas1D_Index(i + s->baseTexLoc);
end = min(i + Atlas1D.TilesPerAtlas, TEXID_OVERLAY_MAX_PER_PAGE);
for (; i < end; i++) { tex.X = xOffset + Atlas2D_TileX(i) * size;
tex.X = s->xOffset + Atlas2D_TileX(i) * size;
tex.Y = s->yOffset + Atlas2D_TileY(i) * size; tex.Y = s->yOffset + Atlas2D_TileY(i) * size;
tex.uv.V1 = Atlas1D_RowId(i + s->baseTexLoc) * Atlas1D.InvTileSize; tex.uv.V1 = Atlas1D_RowId(i + baseLoc) * Atlas1D.InvTileSize;
tex.uv.V2 = tex.uv.V1 + UV2_Scale * Atlas1D.InvTileSize; tex.uv.V2 = tex.uv.V1 + UV2_Scale * Atlas1D.InvTileSize;
Gfx_Make2DQuad(&tex, col, &ptr); Gfx_Make2DQuad(&tex, PACKEDCOL_WHITE, ptr);
} }
Gfx_BindTexture(Atlas1D.TexIds[idx]); baseLoc += TEXID_OVERLAY_MAX_PER_PAGE;
count = (int)(ptr - vertices); xOffset += size * ATLAS2D_TILES_PER_ROW;
Gfx_UpdateDynamicVb_IndexedTris(s->dynamicVb, vertices, count);
} }
} }
static void TexIdsOverlay_BuildMesh(void* screen) {
struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen;
VertexP3fT2fC4b* data;
VertexP3fT2fC4b** ptr;
int offset = 0;
data = (VertexP3fT2fC4b*)Gfx_LockDynamicVb(s->vb, VERTEX_FORMAT_P3FT2FC4B, s->maxVertices);
ptr = &data;
Widget_BuildMesh(&s->title, ptr);
TexIdsOverlay_BuildTerrain(s, ptr);
Gfx_UnlockDynamicVb(s->vb);
}
static int TexIdsOverlay_RenderTerrain(struct TexIdsOverlay* s, int offset) {
int i, count = Atlas1D.TilesPerAtlas * 4;
for (i = 0; i < Atlas1D.Count; i++) {
Gfx_BindTexture(Atlas1D.TexIds[i]);
Gfx_DrawVb_IndexedTris_Range(count, offset);
offset += count;
}
return offset;
}
static void TexIdsOverlay_RenderTextOverlay(struct TexIdsOverlay* s) { static void TexIdsOverlay_RenderTextOverlay(struct TexIdsOverlay* s) {
VertexP3fT2fC4b vertices[TEXID_OVERLAY_VERTICES_COUNT]; VertexP3fT2fC4b vertices[TEXID_OVERLAY_VERTICES_COUNT];
VertexP3fT2fC4b* ptr = vertices; VertexP3fT2fC4b* ptr = vertices;
@ -3093,25 +3114,28 @@ static void TexIdsOverlay_Init(void* screen) {
struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen; struct TexIdsOverlay* s = (struct TexIdsOverlay*)screen;
s->widgets = widgets; s->widgets = widgets;
s->numWidgets = Array_Elems(widgets); s->numWidgets = Array_Elems(widgets);
s->maxVertices = TEXIDS_MAX_VERTICES;
Menu_Label(s, 0, &s->title, ANCHOR_CENTRE, ANCHOR_MIN, 0, 0); Menu_Label(s, 0, &s->title, ANCHOR_CENTRE, ANCHOR_MIN, 0, 0);
} }
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, origXOffset; int rows, offset = 0, origXOffset;
Menu_RenderBounds(); Menu_RenderBounds();
Gfx_SetTexturing(true); Gfx_SetTexturing(true);
Gfx_SetVertexFormat(VERTEX_FORMAT_P3FT2FC4B); Gfx_SetVertexFormat(VERTEX_FORMAT_P3FT2FC4B);
Screen_RenderWidgets(s, delta); Gfx_BindDynamicVb(s->vb);
offset = Widget_Render2(&s->title, offset);
offset = TexIdsOverlay_RenderTerrain(s, offset);
origXOffset = s->xOffset; origXOffset = s->xOffset;
s->baseTexLoc = 0; s->baseTexLoc = 0;
for (rows = Atlas2D.RowsCount; rows > 0; rows -= ATLAS2D_TILES_PER_ROW) { for (rows = Atlas2D.RowsCount; rows > 0; rows -= ATLAS2D_TILES_PER_ROW) {
TexIdsOverlay_RenderTerrain(s);
TexIdsOverlay_RenderTextOverlay(s); TexIdsOverlay_RenderTextOverlay(s);
s->xOffset += s->tileSize * ATLAS2D_TILES_PER_ROW; s->xOffset += s->tileSize * ATLAS2D_TILES_PER_ROW;
s->baseTexLoc += ATLAS2D_TILES_PER_ROW * ATLAS2D_TILES_PER_ROW; s->baseTexLoc += ATLAS2D_TILES_PER_ROW * ATLAS2D_TILES_PER_ROW;
} }