glow rendering fixes to bring stars back

This commit is contained in:
asrael 2026-02-10 11:10:37 -06:00
parent 657b590b6f
commit c538641ec8
26 changed files with 773 additions and 1491 deletions

View file

@ -87,18 +87,15 @@ static u8 blend_indexed(
const pxl8_gfx_pipeline_desc* pipeline,
u8 src,
u8 dst,
const u32* palette,
const u8* colormap
const u32* palette
) {
(void)colormap;
if (!pipeline || !pipeline->blend.enabled) return src;
if (src == 0) return dst;
if (!palette) return src;
f32 src_a = src == 0 ? 0.0f : 1.0f;
f32 dst_a = dst == 0 ? 0.0f : 1.0f;
f32 sf = blend_factor_value(pipeline->blend.src, src_a, dst_a);
f32 df = blend_factor_value(pipeline->blend.dst, src_a, dst_a);
f32 sf = blend_factor_value(pipeline->blend.src, 1.0f, dst_a);
f32 df = blend_factor_value(pipeline->blend.dst, 1.0f, dst_a);
if (sf == 1.0f && df == 0.0f) return src;
if (sf == 0.0f && df == 1.0f) return dst;
@ -111,18 +108,11 @@ static u8 blend_indexed(
u8 dg = (palette[dst] >> 8) & 0xFF;
u8 db = (palette[dst] >> 16) & 0xFF;
i32 out_r = (i32)(sr * sf + dr * df);
i32 out_g = (i32)(sg * sf + dg * df);
i32 out_b = (i32)(sb * sf + db * df);
u8 out_r = (u8)pxl8_clamp_byte((i32)(sr * sf + dr * df));
u8 out_g = (u8)pxl8_clamp_byte((i32)(sg * sf + dg * df));
u8 out_b = (u8)pxl8_clamp_byte((i32)(sb * sf + db * df));
if (out_r < 0) out_r = 0;
if (out_g < 0) out_g = 0;
if (out_b < 0) out_b = 0;
if (out_r > 255) out_r = 255;
if (out_g > 255) out_g = 255;
if (out_b > 255) out_b = 255;
return palette_find_closest(palette, (u8)out_r, (u8)out_g, (u8)out_b);
return palette_find_closest(palette, out_r, out_g, out_b);
}
static inline pxl8_vec4 vec4_lerp(pxl8_vec4 a, pxl8_vec4 b, f32 t) {
@ -192,6 +182,15 @@ static i32 clip_triangle_near(
return 6;
}
static inline pxl8_vec3 clip_to_screen(pxl8_vec4 clip, f32 vp_x, f32 vp_y, f32 hw, f32 hh) {
f32 inv_w = 1.0f / clip.w;
return (pxl8_vec3){
vp_x + hw + clip.x * inv_w * hw,
vp_y + hh - clip.y * inv_w * hh,
clip.z * inv_w,
};
}
static bool setup_tri(
tri_setup* setup,
const raster_vertex* vo0, const raster_vertex* vo1, const raster_vertex* vo2,
@ -203,18 +202,12 @@ static bool setup_tri(
f32 hw = (f32)viewport_w * 0.5f;
f32 hh = (f32)viewport_h * 0.5f;
f32 vp_xf = (f32)viewport_x;
f32 vp_yf = (f32)viewport_y;
setup->p0.x = (f32)viewport_x + hw + vo0->clip_pos.x / vo0->clip_pos.w * hw;
setup->p0.y = (f32)viewport_y + hh - vo0->clip_pos.y / vo0->clip_pos.w * hh;
setup->p0.z = vo0->clip_pos.z / vo0->clip_pos.w;
setup->p1.x = (f32)viewport_x + hw + vo1->clip_pos.x / vo1->clip_pos.w * hw;
setup->p1.y = (f32)viewport_y + hh - vo1->clip_pos.y / vo1->clip_pos.w * hh;
setup->p1.z = vo1->clip_pos.z / vo1->clip_pos.w;
setup->p2.x = (f32)viewport_x + hw + vo2->clip_pos.x / vo2->clip_pos.w * hw;
setup->p2.y = (f32)viewport_y + hh - vo2->clip_pos.y / vo2->clip_pos.w * hh;
setup->p2.z = vo2->clip_pos.z / vo2->clip_pos.w;
setup->p0 = clip_to_screen(vo0->clip_pos, vp_xf, vp_yf, hw, hh);
setup->p1 = clip_to_screen(vo1->clip_pos, vp_xf, vp_yf, hw, hh);
setup->p2 = clip_to_screen(vo2->clip_pos, vp_xf, vp_yf, hw, hh);
f32 cross = (setup->p1.x - setup->p0.x) * (setup->p2.y - setup->p0.y) -
(setup->p1.y - setup->p0.y) * (setup->p2.x - setup->p0.x);
@ -303,7 +296,6 @@ static void rasterize_triangle(
u8 alpha_ref = pipeline ? pipeline->blend.alpha_ref : 0;
bool blend_enabled = pipeline && pipeline->blend.enabled;
const u32* palette = bindings ? bindings->palette : NULL;
const u8* colormap = bindings ? bindings->colormap : NULL;
for (i32 y = setup->y_start; y <= setup->y_end; y++) {
f32 yf = (f32)y + 0.5f;
@ -548,7 +540,7 @@ static void rasterize_triangle(
if (color != 0) {
u8 out_color = color;
if (blend_enabled) {
out_color = blend_indexed(pipeline, color, prow[px], palette, colormap);
out_color = blend_indexed(pipeline, color, prow[px], palette);
}
prow[px] = out_color;
@ -594,7 +586,7 @@ static void rasterize_triangle(
if (color != 0) {
u8 out_color = color;
if (blend_enabled) {
out_color = blend_indexed(pipeline, color, prow[px], palette, colormap);
out_color = blend_indexed(pipeline, color, prow[px], palette);
}
prow[px] = out_color;
@ -1142,6 +1134,12 @@ void pxl8_begin_pass(pxl8_gfx_cmdbuf* cb, pxl8_gfx_pass pass) {
cmd->begin_pass.pass = pass;
}
void pxl8_cmdbuf_clear_depth(pxl8_gfx_cmdbuf* cb, pxl8_gfx_texture texture) {
pxl8_gfx_cmd* cmd = cmd_alloc(cb);
cmd->type = PXL8_GFX_CMD_CLEAR_DEPTH;
cmd->clear_depth.texture = texture;
}
void pxl8_end_pass(pxl8_gfx_cmdbuf* cb) {
pxl8_gfx_cmd* cmd = cmd_alloc(cb);
cmd->type = PXL8_GFX_CMD_END_PASS;
@ -1370,17 +1368,16 @@ static void execute_draw(
if (is_wireframe) {
f32 hw = (f32)vp_w * 0.5f;
f32 hh = (f32)vp_h * 0.5f;
f32 vp_xf = (f32)vp_x;
f32 vp_yf = (f32)vp_y;
raster_vertex* wv0 = &clipped[t];
raster_vertex* wv1 = &clipped[t+1];
raster_vertex* wv2 = &clipped[t+2];
pxl8_vec3 s0 = clip_to_screen(clipped[t].clip_pos, vp_xf, vp_yf, hw, hh);
pxl8_vec3 s1 = clip_to_screen(clipped[t+1].clip_pos, vp_xf, vp_yf, hw, hh);
pxl8_vec3 s2 = clip_to_screen(clipped[t+2].clip_pos, vp_xf, vp_yf, hw, hh);
i32 sx0 = (i32)((f32)vp_x + hw + wv0->clip_pos.x / wv0->clip_pos.w * hw);
i32 sy0 = (i32)((f32)vp_y + hh - wv0->clip_pos.y / wv0->clip_pos.w * hh);
i32 sx1 = (i32)((f32)vp_x + hw + wv1->clip_pos.x / wv1->clip_pos.w * hw);
i32 sy1 = (i32)((f32)vp_y + hh - wv1->clip_pos.y / wv1->clip_pos.w * hh);
i32 sx2 = (i32)((f32)vp_x + hw + wv2->clip_pos.x / wv2->clip_pos.w * hw);
i32 sy2 = (i32)((f32)vp_y + hh - wv2->clip_pos.y / wv2->clip_pos.w * hh);
i32 sx0 = (i32)s0.x, sy0 = (i32)s0.y;
i32 sx1 = (i32)s1.x, sy1 = (i32)s1.y;
i32 sx2 = (i32)s2.x, sy2 = (i32)s2.y;
f32 cross = (f32)(sx1 - sx0) * (f32)(sy2 - sy0) - (f32)(sy1 - sy0) * (f32)(sx2 - sx0);
if (!double_sided) {
@ -1427,6 +1424,9 @@ void pxl8_gfx_submit(pxl8_renderer* r, pxl8_gfx_cmdbuf* cb) {
}
}
break;
case PXL8_GFX_CMD_CLEAR_DEPTH:
pxl8_clear_depth(r, cmd->clear_depth.texture);
break;
case PXL8_GFX_CMD_END_PASS:
r->current_pass = (pxl8_gfx_pass){ PXL8_GFX_INVALID_ID };
break;