compress ta_surface parameters into a single bitstring for faster merge comparisons

This commit is contained in:
Anthony Pesch 2017-10-21 11:59:28 -04:00
parent c662ab2022
commit be3cdea37c
6 changed files with 88 additions and 85 deletions

View File

@ -232,14 +232,7 @@ static struct ta_vertex *tr_reserve_vert(struct tr *tr, struct tr_context *rc) {
static inline int tr_can_merge_surfs(struct ta_surface *a,
struct ta_surface *b) {
return a->texture == b->texture && a->depth_write == b->depth_write &&
a->depth_func == b->depth_func && a->cull == b->cull &&
a->src_blend == b->src_blend && a->dst_blend == b->dst_blend &&
a->shade == b->shade && a->ignore_alpha == b->ignore_alpha &&
a->ignore_texture_alpha == b->ignore_texture_alpha &&
a->offset_color == b->offset_color &&
a->pt_alpha_test == b->pt_alpha_test &&
a->pt_alpha_ref == b->pt_alpha_ref;
return a->params.full == b->params.full;
}
static void tr_commit_surf(struct tr *tr, struct tr_context *rc) {
@ -379,12 +372,13 @@ static void tr_parse_bg(struct tr *tr, const struct ta_context *ctx,
/* translate the surface */
struct ta_surface *surf = tr_reserve_surf(tr, rc, 0);
surf->texture = 0;
surf->depth_write = !ctx->bg_isp.z_write_disable;
surf->depth_func = translate_depth_func(ctx->bg_isp.depth_compare_mode);
surf->cull = translate_cull(ctx->bg_isp.culling_mode);
surf->src_blend = BLEND_NONE;
surf->dst_blend = BLEND_NONE;
surf->params.texture = 0;
surf->params.depth_write = !ctx->bg_isp.z_write_disable;
surf->params.depth_func =
translate_depth_func(ctx->bg_isp.depth_compare_mode);
surf->params.cull = translate_cull(ctx->bg_isp.culling_mode);
surf->params.src_blend = BLEND_NONE;
surf->params.dst_blend = BLEND_NONE;
/* translate the first 3 vertices */
struct ta_vertex *v0 = tr_reserve_vert(tr, rc);
@ -474,36 +468,40 @@ static void tr_parse_poly_param(struct tr *tr, const struct ta_context *ctx,
isp/tsp instruction word, so use the pcw for the uv_16bit, gouraud, offset,
and texture settings */
struct ta_surface *surf = tr_reserve_surf(tr, rc, 0);
surf->depth_write = !param->type0.isp.z_write_disable;
surf->depth_func = translate_depth_func(param->type0.isp.depth_compare_mode);
surf->cull = translate_cull(param->type0.isp.culling_mode);
surf->src_blend = translate_src_blend_func(param->type0.tsp.src_alpha_instr);
surf->dst_blend = translate_dst_blend_func(param->type0.tsp.dst_alpha_instr);
surf->shade = translate_shade_mode(param->type0.tsp.texture_shading_instr);
surf->ignore_alpha = !param->type0.tsp.use_alpha;
surf->ignore_texture_alpha = param->type0.tsp.ignore_tex_alpha;
surf->offset_color = param->type0.pcw.offset;
surf->pt_alpha_test = tr->list_type == TA_LIST_PUNCH_THROUGH;
surf->pt_alpha_ref = (float)ctx->pt_alpha_ref / 0xff;
surf->params.depth_write = !param->type0.isp.z_write_disable;
surf->params.depth_func =
translate_depth_func(param->type0.isp.depth_compare_mode);
surf->params.cull = translate_cull(param->type0.isp.culling_mode);
surf->params.src_blend =
translate_src_blend_func(param->type0.tsp.src_alpha_instr);
surf->params.dst_blend =
translate_dst_blend_func(param->type0.tsp.dst_alpha_instr);
surf->params.shade =
translate_shade_mode(param->type0.tsp.texture_shading_instr);
surf->params.ignore_alpha = !param->type0.tsp.use_alpha;
surf->params.ignore_texture_alpha = param->type0.tsp.ignore_tex_alpha;
surf->params.offset_color = param->type0.pcw.offset;
surf->params.pt_alpha_test = tr->list_type == TA_LIST_PUNCH_THROUGH;
surf->params.pt_alpha_ref = ctx->pt_alpha_ref;
/* override a few surface parameters based on the list type */
if (tr->list_type != TA_LIST_TRANSLUCENT &&
tr->list_type != TA_LIST_TRANSLUCENT_MODVOL) {
surf->src_blend = BLEND_NONE;
surf->dst_blend = BLEND_NONE;
surf->params.src_blend = BLEND_NONE;
surf->params.dst_blend = BLEND_NONE;
} else if ((tr->list_type == TA_LIST_TRANSLUCENT ||
tr->list_type == TA_LIST_TRANSLUCENT_MODVOL) &&
ctx->autosort) {
surf->depth_func = DEPTH_LEQUAL;
surf->params.depth_func = DEPTH_LEQUAL;
} else if (tr->list_type == TA_LIST_PUNCH_THROUGH) {
surf->depth_func = DEPTH_GEQUAL;
surf->params.depth_func = DEPTH_GEQUAL;
}
if (param->type0.pcw.texture) {
surf->texture =
surf->params.texture =
tr_convert_texture(tr, ctx, param->type0.tsp, param->type0.tcw);
} else {
surf->texture = 0;
surf->params.texture = 0;
}
}

View File

@ -36,9 +36,11 @@ struct tr_texture {
struct tr_param {
/* offset of parameter in ta_context param stream */
int offset;
/* global list and vertex types at time of parsing */
int list_type;
int vert_type;
/* last surf / vert generated for the param */
int last_surf;
int last_vert;

View File

@ -88,7 +88,7 @@ void imgui_end_frame(struct imgui *imgui) {
surf.texture = (texture_handle_t)(intptr_t)cmd.TextureId;
surf.src_blend = BLEND_SRC_ALPHA;
surf.dst_blend = BLEND_ONE_MINUS_SRC_ALPHA;
surf.scissor = true;
surf.scissor = 1;
surf.scissor_rect[0] = cmd.ClipRect.x;
surf.scissor_rect[1] = io.DisplaySize.y - cmd.ClipRect.w;
surf.scissor_rect[2] = cmd.ClipRect.z - cmd.ClipRect.x;

View File

@ -3,8 +3,6 @@
#include "host/host.h"
#include "render/render_backend.h"
#define MAX_TEXTURES 8192
enum texture_map {
MAP_DIFFUSE,
};
@ -142,7 +140,6 @@ static GLenum prim_types[] = {
};
static GLuint internal_formats[] = {
GL_NONE, /* PXL_INVALID */
GL_RGB, /* PXL_RGB */
GL_RGBA, /* PXL_RGBA */
GL_RGBA, /* PXL_RGBA5551 */
@ -151,7 +148,6 @@ static GLuint internal_formats[] = {
};
static GLuint pixel_formats[] = {
GL_NONE, /* PXL_INVALID */
GL_UNSIGNED_BYTE, /* PXL_RGB */
GL_UNSIGNED_BYTE, /* PXL_RGBA */
GL_UNSIGNED_SHORT_5_5_5_1, /* PXL_RGBA5551 */
@ -443,23 +439,23 @@ static void r_set_initial_state(struct render_backend *r) {
static struct shader_program *r_get_ta_program(struct render_backend *r,
const struct ta_surface *surf) {
int idx = surf->shade;
if (surf->texture) {
int idx = surf->params.shade;
if (surf->params.texture) {
idx |= ATTR_TEXTURE;
}
if (surf->ignore_alpha) {
if (surf->params.ignore_alpha) {
idx |= ATTR_IGNORE_ALPHA;
}
if (surf->ignore_texture_alpha) {
if (surf->params.ignore_texture_alpha) {
idx |= ATTR_IGNORE_TEXTURE_ALPHA;
}
if (surf->offset_color) {
if (surf->params.offset_color) {
idx |= ATTR_OFFSET_COLOR;
}
if (surf->pt_alpha_test) {
if (surf->params.pt_alpha_test) {
idx |= ATTR_PT_ALPHA_TEST;
}
if (surf->debug_depth) {
if (surf->params.debug_depth) {
idx |= ATTR_DEBUG_DEPTH_BUFFER;
}
@ -599,27 +595,29 @@ void r_end_ta_surfaces(struct render_backend *r) {}
void r_draw_ta_surface(struct render_backend *r,
const struct ta_surface *surf) {
glDepthMask(!!surf->depth_write);
glDepthMask(!!surf->params.depth_write);
if (surf->depth_func == DEPTH_NONE) {
if (surf->params.depth_func == DEPTH_NONE) {
glDisable(GL_DEPTH_TEST);
} else {
glEnable(GL_DEPTH_TEST);
glDepthFunc(depth_funcs[surf->depth_func]);
glDepthFunc(depth_funcs[surf->params.depth_func]);
}
if (surf->cull == CULL_NONE) {
if (surf->params.cull == CULL_NONE) {
glDisable(GL_CULL_FACE);
} else {
glEnable(GL_CULL_FACE);
glCullFace(cull_face[surf->cull]);
glCullFace(cull_face[surf->params.cull]);
}
if (surf->src_blend == BLEND_NONE || surf->dst_blend == BLEND_NONE) {
if (surf->params.src_blend == BLEND_NONE ||
surf->params.dst_blend == BLEND_NONE) {
glDisable(GL_BLEND);
} else {
glEnable(GL_BLEND);
glBlendFunc(blend_funcs[surf->src_blend], blend_funcs[surf->dst_blend]);
glBlendFunc(blend_funcs[surf->params.src_blend],
blend_funcs[surf->params.dst_blend]);
}
struct shader_program *program = r_get_ta_program(r, surf);
@ -633,10 +631,11 @@ void r_draw_ta_surface(struct render_backend *r,
}
/* bind non-global uniforms every time */
glUniform1f(program->loc[UNIFORM_PT_ALPHA_REF], surf->pt_alpha_ref);
float alpha_ref = surf->params.pt_alpha_ref / 255.0f;
glUniform1f(program->loc[UNIFORM_PT_ALPHA_REF], alpha_ref);
if (surf->texture) {
struct texture *tex = &r->textures[surf->texture];
if (surf->params.texture) {
struct texture *tex = &r->textures[surf->params.texture];
r_bind_texture(r, MAP_DIFFUSE, tex->texture);
}

View File

@ -5,10 +5,12 @@
struct host;
typedef unsigned texture_handle_t;
/* note, this can't be larger than the width of ta_surface's texture param */
#define MAX_TEXTURES (1 << 13)
typedef int texture_handle_t;
enum pxl_format {
PXL_INVALID,
PXL_RGB,
PXL_RGBA,
PXL_RGBA5551,
@ -67,11 +69,6 @@ enum shade_mode {
SHADE_MODULATE_ALPHA,
};
enum box_type {
BOX_BAR,
BOX_FLAT,
};
enum prim_type {
PRIM_TRIANGLES,
PRIM_LINES,
@ -85,19 +82,26 @@ struct ta_vertex {
};
struct ta_surface {
texture_handle_t texture;
int depth_write;
enum depth_func depth_func;
enum cull_face cull;
enum blend_func src_blend;
enum blend_func dst_blend;
enum shade_mode shade;
int ignore_alpha;
int ignore_texture_alpha;
int offset_color;
int pt_alpha_test;
float pt_alpha_ref;
int debug_depth;
union {
uint64_t full;
struct {
texture_handle_t texture : 13;
int depth_write : 1;
enum depth_func depth_func : 4;
enum cull_face cull : 2;
enum blend_func src_blend : 4;
enum blend_func dst_blend : 4;
enum shade_mode shade : 3;
int ignore_alpha : 1;
int ignore_texture_alpha : 1;
int offset_color : 1;
int pt_alpha_test : 1;
int pt_alpha_ref : 8;
int debug_depth : 1;
int : 20;
};
} params;
int first_vert;
int num_verts;

View File

@ -489,24 +489,24 @@ static void tracer_param_tooltip(struct tracer *tracer, struct tr_param *rp) {
igSeparator();
if (surf->texture) {
if (surf->params.texture) {
struct ImVec2 tex_size = {128.0f, 128.0f};
struct ImVec2 tex_uv0 = {0.0f, 1.0f};
struct ImVec2 tex_uv1 = {1.0f, 0.0f};
ImTextureID handle_id = (ImTextureID)(intptr_t)surf->texture;
ImTextureID handle_id = (ImTextureID)(intptr_t)surf->params.texture;
igImage(handle_id, tex_size, tex_uv0, tex_uv1, one_vec4, zero_vec4);
}
igText("depth_write: %d", surf->depth_write);
igText("depth_func: %s", depthfunc_names[surf->depth_func]);
igText("cull: %s", cullface_names[surf->cull]);
igText("src_blend: %s", blendfunc_names[surf->src_blend]);
igText("dst_blend: %s", blendfunc_names[surf->dst_blend]);
igText("shade: %s", shademode_names[surf->shade]);
igText("ignore_alpha: %d", surf->ignore_alpha);
igText("ignore_texture_alpha: %d", surf->ignore_texture_alpha);
igText("offset_color: %d", surf->offset_color);
igText("depth_write: %d", surf->params.depth_write);
igText("depth_func: %s", depthfunc_names[surf->params.depth_func]);
igText("cull: %s", cullface_names[surf->params.cull]);
igText("src_blend: %s", blendfunc_names[surf->params.src_blend]);
igText("dst_blend: %s", blendfunc_names[surf->params.dst_blend]);
igText("shade: %s", shademode_names[surf->params.shade]);
igText("ignore_alpha: %d", surf->params.ignore_alpha);
igText("ignore_texture_alpha: %d", surf->params.ignore_texture_alpha);
igText("offset_color: %d", surf->params.offset_color);
igText("first_vert: %d", surf->first_vert);
igText("num_verts: %d", surf->num_verts);
}
@ -661,7 +661,7 @@ void tracer_render_frame(struct tracer *tracer) {
for (int i = 0; i < rc->num_surfs; i++) {
struct ta_surface *surf = &rc->surfs[i];
surf->debug_depth = tracer->debug_depth;
surf->params.debug_depth = tracer->debug_depth;
}
tr_convert_context(tracer->r, tracer, &tracer_find_texture, &tracer->ctx,