speed up gfx, colored lighting is out for now
This commit is contained in:
parent
3c3e961995
commit
01e6059dd1
17 changed files with 1055 additions and 250 deletions
|
|
@ -34,50 +34,15 @@ void pxl8_lights_destroy(pxl8_lights* lights) {
|
|||
}
|
||||
|
||||
void pxl8_lights_add(pxl8_lights* lights, f32 x, f32 y, f32 z, u8 r, u8 g, u8 b, u8 intensity, f32 radius) {
|
||||
(void)r; (void)g; (void)b;
|
||||
if (!lights || lights->count >= lights->capacity) return;
|
||||
|
||||
f32 radius_sq = radius * radius;
|
||||
pxl8_light* l = &lights->data[lights->count++];
|
||||
l->position.x = x;
|
||||
l->position.y = y;
|
||||
l->position.z = z;
|
||||
l->r = r;
|
||||
l->g = g;
|
||||
l->b = b;
|
||||
l->intensity = intensity;
|
||||
l->radius = radius;
|
||||
l->position = (pxl8_vec3){{x, y, z}};
|
||||
l->radius_sq = radius_sq;
|
||||
l->inv_radius_sq = radius_sq > 0.0f ? 1.0f / radius_sq : 0.0f;
|
||||
|
||||
l->constant = 1.0f;
|
||||
if (radius <= 7.0f) {
|
||||
l->linear = 0.7f;
|
||||
l->quadratic = 1.8f;
|
||||
} else if (radius <= 13.0f) {
|
||||
l->linear = 0.35f;
|
||||
l->quadratic = 0.44f;
|
||||
} else if (radius <= 20.0f) {
|
||||
l->linear = 0.22f;
|
||||
l->quadratic = 0.20f;
|
||||
} else if (radius <= 32.0f) {
|
||||
l->linear = 0.14f;
|
||||
l->quadratic = 0.07f;
|
||||
} else if (radius <= 50.0f) {
|
||||
l->linear = 0.09f;
|
||||
l->quadratic = 0.032f;
|
||||
} else if (radius <= 65.0f) {
|
||||
l->linear = 0.07f;
|
||||
l->quadratic = 0.017f;
|
||||
} else if (radius <= 100.0f) {
|
||||
l->linear = 0.045f;
|
||||
l->quadratic = 0.0075f;
|
||||
} else if (radius <= 160.0f) {
|
||||
l->linear = 0.027f;
|
||||
l->quadratic = 0.0028f;
|
||||
} else {
|
||||
l->linear = 0.022f;
|
||||
l->quadratic = 0.0019f;
|
||||
}
|
||||
l->intensity = (f32)intensity / 255.0f;
|
||||
}
|
||||
|
||||
void pxl8_lights_clear(pxl8_lights* lights) {
|
||||
|
|
|
|||
|
|
@ -7,14 +7,9 @@
|
|||
|
||||
typedef struct pxl8_light {
|
||||
pxl8_vec3 position;
|
||||
f32 inv_radius_sq;
|
||||
u8 r, g, b;
|
||||
u8 intensity;
|
||||
f32 radius;
|
||||
f32 radius_sq;
|
||||
f32 constant;
|
||||
f32 linear;
|
||||
f32 quadratic;
|
||||
f32 inv_radius_sq;
|
||||
f32 intensity;
|
||||
} pxl8_light;
|
||||
|
||||
typedef struct pxl8_lights pxl8_lights;
|
||||
|
|
|
|||
|
|
@ -296,7 +296,6 @@ static void rasterize_triangle(
|
|||
const tri_setup* setup,
|
||||
u8* fb,
|
||||
u16* zb,
|
||||
u32* light_accum,
|
||||
u32 fb_width,
|
||||
pxl8_shader_fn shader,
|
||||
const pxl8_gfx_pipeline_desc* pipeline,
|
||||
|
|
@ -425,7 +424,6 @@ static void rasterize_triangle(
|
|||
u32 row_start = (u32)y * fb_width;
|
||||
u8* prow = fb + row_start;
|
||||
u16* zrow = zb + row_start;
|
||||
u32* lrow = light_accum ? light_accum + row_start : NULL;
|
||||
|
||||
i32 x = x_start;
|
||||
while (x <= x_end) {
|
||||
|
|
@ -433,8 +431,8 @@ static void rasterize_triangle(
|
|||
if (span_end > x_end) span_end = x_end;
|
||||
i32 span_len = span_end - x + 1;
|
||||
|
||||
f32 pw_start = pxl8_fast_rcp(wr);
|
||||
f32 pw_end = pxl8_fast_rcp(wr + dwr * (f32)span_len);
|
||||
f32 pw_start = 1.0f / wr;
|
||||
f32 pw_end = 1.0f / (wr + dwr * (f32)span_len);
|
||||
|
||||
f32 u_start = uw * pw_start;
|
||||
f32 v_start = vw * pw_start;
|
||||
|
|
@ -471,7 +469,80 @@ static void rasterize_triangle(
|
|||
f32 wy_a = wy_start;
|
||||
f32 wz_a = wz_start;
|
||||
|
||||
for (i32 px = x; px <= span_end; px++) {
|
||||
i32 px = x;
|
||||
|
||||
#if defined(PXL8_SIMD_SSE) || defined(PXL8_SIMD_NEON)
|
||||
if (depth_test && depth_compare == PXL8_GFX_COMPARE_LESS && !blend_enabled) {
|
||||
pxl8_f32_simd dz4_simd = pxl8_f32_simd_set(dz * 4.0f);
|
||||
pxl8_f32_simd half = pxl8_f32_simd_set(0.5f);
|
||||
pxl8_f32_simd one = pxl8_f32_simd_set(1.0f);
|
||||
pxl8_f32_simd zero = pxl8_f32_simd_zero();
|
||||
pxl8_f32_simd scale65535 = pxl8_f32_simd_set(65535.0f);
|
||||
pxl8_f32_simd z4 = pxl8_f32_simd_set4(z_a, z_a + dz, z_a + dz * 2.0f, z_a + dz * 3.0f);
|
||||
|
||||
pxl8_f32_simd offsets = pxl8_f32_simd_set4(0.0f, 1.0f, 2.0f, 3.0f);
|
||||
f32 du4 = du * 4.0f, dv4 = dv * 4.0f, dl4 = dl * 4.0f, dc4 = dc * 4.0f;
|
||||
f32 dz4 = dz * 4.0f, dwx4 = dwx * 4.0f, dwy4 = dwy * 4.0f, dwz4 = dwz * 4.0f;
|
||||
|
||||
for (; px + 3 <= span_end; px += 4) {
|
||||
pxl8_f32_simd depth_norm = pxl8_f32_simd_clamp(pxl8_f32_simd_mul(pxl8_f32_simd_add(z4, one), half), zero, one);
|
||||
pxl8_i32_simd z16_4 = pxl8_f32_simd_to_i32(pxl8_f32_simd_mul(depth_norm, scale65535));
|
||||
pxl8_i32_simd zbuf = pxl8_i32_simd_set4((i32)zrow[px], (i32)zrow[px+1], (i32)zrow[px+2], (i32)zrow[px+3]);
|
||||
i32 mask = pxl8_i32_simd_movemask(pxl8_i32_simd_cmpgt(zbuf, z16_4));
|
||||
|
||||
STATS_INC(stats, depth_tests, 4);
|
||||
|
||||
if (mask) {
|
||||
pxl8_shader_ctx frag_ctx = {
|
||||
.color_count = 4,
|
||||
.x = pxl8_i32_simd_set4(px, px + 1, px + 2, px + 3),
|
||||
.y = pxl8_i32_simd_set(y),
|
||||
.v_uv = {
|
||||
pxl8_f32_simd_add(pxl8_f32_simd_set(u_a), pxl8_f32_simd_mul(pxl8_f32_simd_set(du), offsets)),
|
||||
pxl8_f32_simd_add(pxl8_f32_simd_set(v_a), pxl8_f32_simd_mul(pxl8_f32_simd_set(dv), offsets))
|
||||
},
|
||||
.v_world = {
|
||||
pxl8_f32_simd_add(pxl8_f32_simd_set(wx_a), pxl8_f32_simd_mul(pxl8_f32_simd_set(dwx), offsets)),
|
||||
pxl8_f32_simd_add(pxl8_f32_simd_set(wy_a), pxl8_f32_simd_mul(pxl8_f32_simd_set(dwy), offsets)),
|
||||
pxl8_f32_simd_add(pxl8_f32_simd_set(wz_a), pxl8_f32_simd_mul(pxl8_f32_simd_set(dwz), offsets))
|
||||
},
|
||||
.v_normal = pxl8_vec3_simd_set(setup->normal),
|
||||
.v_light = pxl8_f32_simd_mul(
|
||||
pxl8_f32_simd_add(pxl8_f32_simd_set(l_a), pxl8_f32_simd_mul(pxl8_f32_simd_set(dl), offsets)),
|
||||
pxl8_f32_simd_set(1.0f / 255.0f)
|
||||
),
|
||||
.v_color = pxl8_f32_simd_add(pxl8_f32_simd_set(c_a), pxl8_f32_simd_mul(pxl8_f32_simd_set(dc), offsets)),
|
||||
.v_depth = z4,
|
||||
};
|
||||
|
||||
u8 colors[4];
|
||||
shader(&frag_ctx, bindings, uniforms, colors);
|
||||
STATS_INC(stats, shader_calls, 1);
|
||||
|
||||
i32 z16_arr[4];
|
||||
pxl8_i32_simd_store(z16_arr, z16_4);
|
||||
|
||||
for (i32 i = 0; i < 4; i++) {
|
||||
if (!(mask & (0x8 << (i * 4)))) continue;
|
||||
STATS_INC(stats, depth_passes, 1);
|
||||
|
||||
u8 color = colors[i];
|
||||
if (!(alpha_test && color <= alpha_ref) && color != 0) {
|
||||
prow[px + i] = color;
|
||||
if (depth_write) zrow[px + i] = (u16)z16_arr[i];
|
||||
STATS_INC(stats, pixels_written, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
u_a += du4; v_a += dv4; l_a += dl4; c_a += dc4;
|
||||
z_a += dz4; wx_a += dwx4; wy_a += dwy4; wz_a += dwz4;
|
||||
z4 = pxl8_f32_simd_add(z4, dz4_simd);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
for (; px <= span_end; px++) {
|
||||
f32 depth_norm = pxl8_clamp((z_a + 1.0f) * 0.5f, 0.0f, 1.0f);
|
||||
u16 z16 = (u16)(depth_norm * 65535.0f);
|
||||
|
||||
|
|
@ -480,18 +551,19 @@ static void rasterize_triangle(
|
|||
if (depth_pass) {
|
||||
STATS_INC(stats, depth_passes, 1);
|
||||
pxl8_shader_ctx frag_ctx = {
|
||||
.x = px,
|
||||
.y = y,
|
||||
.v_uv = { { u_a, v_a } },
|
||||
.v_world = { wx_a, wy_a, wz_a },
|
||||
.v_normal = setup->normal,
|
||||
.v_light = l_a / 255.0f,
|
||||
.v_color = c_a,
|
||||
.v_depth = z_a,
|
||||
.out_light_color = 0,
|
||||
.color_count = 1,
|
||||
.x = pxl8_i32_simd_set(px),
|
||||
.y = pxl8_i32_simd_set(y),
|
||||
.v_uv = { pxl8_f32_simd_set(u_a), pxl8_f32_simd_set(v_a) },
|
||||
.v_world = { pxl8_f32_simd_set(wx_a), pxl8_f32_simd_set(wy_a), pxl8_f32_simd_set(wz_a) },
|
||||
.v_normal = pxl8_vec3_simd_set(setup->normal),
|
||||
.v_light = pxl8_f32_simd_set(l_a / 255.0f),
|
||||
.v_color = pxl8_f32_simd_set(c_a),
|
||||
.v_depth = pxl8_f32_simd_set(z_a),
|
||||
};
|
||||
|
||||
u8 color = shader(&frag_ctx, bindings, uniforms);
|
||||
u8 color;
|
||||
shader(&frag_ctx, bindings, uniforms, &color);
|
||||
STATS_INC(stats, shader_calls, 1);
|
||||
|
||||
if (!(alpha_test && color <= alpha_ref)) {
|
||||
|
|
@ -506,10 +578,6 @@ static void rasterize_triangle(
|
|||
zrow[px] = z16;
|
||||
}
|
||||
STATS_INC(stats, pixels_written, 1);
|
||||
if (lrow && frag_ctx.out_light_color != 0) {
|
||||
lrow[px] = frag_ctx.out_light_color;
|
||||
STATS_INC(stats, light_writes, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1080,12 +1148,6 @@ static void execute_draw(
|
|||
return;
|
||||
}
|
||||
|
||||
u32* light_accum = NULL;
|
||||
if (VALID_TEX(r, pass->desc.light_accum.texture)) {
|
||||
texture_slot* light_tex = &r->textures[SLOT_INDEX(pass->desc.light_accum.texture.id)];
|
||||
light_accum = light_tex->data;
|
||||
}
|
||||
|
||||
const pxl8_vertex* vertices = vb->data;
|
||||
const u16* indices = use_indices ? ib->data : NULL;
|
||||
|
||||
|
|
@ -1239,7 +1301,7 @@ static void execute_draw(
|
|||
}
|
||||
|
||||
u64 raster_start = STATS_START();
|
||||
rasterize_triangle(&setup, fb, zb, light_accum, fb_w, shader, &pip->desc,
|
||||
rasterize_triangle(&setup, fb, zb, fb_w, shader, &pip->desc,
|
||||
&shader_bindings, &shader_uniforms, &r->stats);
|
||||
STATS_ADD(&r->stats, raster_ns, raster_start);
|
||||
}
|
||||
|
|
@ -1462,41 +1524,27 @@ void pxl8_resolve_to_rgba(pxl8_renderer* r, pxl8_gfx_texture color, pxl8_gfx_tex
|
|||
u8* fb = cs->data;
|
||||
u32 w = cs->width;
|
||||
u32 h = cs->height;
|
||||
u32 total = w * h;
|
||||
|
||||
u32* light_data = NULL;
|
||||
if (light_accum.id != PXL8_GFX_INVALID_ID && VALID_TEX(r, light_accum)) {
|
||||
light_data = r->textures[SLOT_INDEX(light_accum.id)].data;
|
||||
(void)light_accum;
|
||||
|
||||
#if defined(PXL8_SIMD_SSE) || defined(PXL8_SIMD_NEON)
|
||||
pxl8_i32_simd alpha_mask = pxl8_i32_simd_set((i32)0xFF000000);
|
||||
u32 i = 0;
|
||||
for (; i + 4 <= total; i += 4) {
|
||||
pxl8_i32_simd base = pxl8_i32_simd_set4(
|
||||
(i32)palette[fb[i + 0]], (i32)palette[fb[i + 1]],
|
||||
(i32)palette[fb[i + 2]], (i32)palette[fb[i + 3]]
|
||||
);
|
||||
base = pxl8_i32_simd_or(base, alpha_mask);
|
||||
pxl8_i32_simd_store((i32*)&output[i], base);
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < w * h; i++) {
|
||||
u8 idx = fb[i];
|
||||
u32 base = palette[idx];
|
||||
|
||||
if (light_data) {
|
||||
u32 lv = light_data[i];
|
||||
u32 la = lv >> 24;
|
||||
if (la > 0) {
|
||||
i32 br = base & 0xFF;
|
||||
i32 bg = (base >> 8) & 0xFF;
|
||||
i32 bb = (base >> 16) & 0xFF;
|
||||
|
||||
i32 lr = lv & 0xFF;
|
||||
i32 lg = (lv >> 8) & 0xFF;
|
||||
i32 lb = (lv >> 16) & 0xFF;
|
||||
|
||||
f32 t = (f32)la / 255.0f;
|
||||
br += (i32)((f32)(lr - 128) * t * 2.0f);
|
||||
bg += (i32)((f32)(lg - 128) * t * 2.0f);
|
||||
bb += (i32)((f32)(lb - 128) * t * 2.0f);
|
||||
|
||||
br = pxl8_clamp_byte(br);
|
||||
bg = pxl8_clamp_byte(bg);
|
||||
bb = pxl8_clamp_byte(bb);
|
||||
|
||||
base = (u32)br | ((u32)bg << 8) | ((u32)bb << 16) | 0xFF000000;
|
||||
}
|
||||
}
|
||||
|
||||
output[i] = base | 0xFF000000;
|
||||
for (; i < total; i++) {
|
||||
output[i] = palette[fb[i]] | 0xFF000000;
|
||||
}
|
||||
#else
|
||||
for (u32 i = 0; i < total; i++) {
|
||||
output[i] = palette[fb[i]] | 0xFF000000;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,7 +59,20 @@ typedef struct pxl8_shader_bindings {
|
|||
};
|
||||
} pxl8_shader_bindings;
|
||||
|
||||
#if defined(PXL8_SIMD_SSE) || defined(PXL8_SIMD_NEON)
|
||||
typedef struct pxl8_shader_ctx {
|
||||
u32 color_count;
|
||||
pxl8_i32_simd x, y;
|
||||
pxl8_vec2_simd v_uv;
|
||||
pxl8_vec3_simd v_world;
|
||||
pxl8_vec3_simd v_normal;
|
||||
pxl8_f32_simd v_color;
|
||||
pxl8_f32_simd v_light;
|
||||
pxl8_f32_simd v_depth;
|
||||
} pxl8_shader_ctx;
|
||||
#else
|
||||
typedef struct pxl8_shader_ctx {
|
||||
u32 color_count;
|
||||
i32 x, y;
|
||||
pxl8_vec2 v_uv;
|
||||
pxl8_vec3 v_world;
|
||||
|
|
@ -67,13 +80,14 @@ typedef struct pxl8_shader_ctx {
|
|||
f32 v_color;
|
||||
f32 v_light;
|
||||
f32 v_depth;
|
||||
u32 out_light_color;
|
||||
} pxl8_shader_ctx;
|
||||
#endif
|
||||
|
||||
typedef u8 (*pxl8_shader_fn)(
|
||||
pxl8_shader_ctx* ctx,
|
||||
typedef void (*pxl8_shader_fn)(
|
||||
const pxl8_shader_ctx* ctx,
|
||||
const pxl8_shader_bindings* bindings,
|
||||
const pxl8_shader_uniforms* uniforms
|
||||
const pxl8_shader_uniforms* uniforms,
|
||||
u8* colors_out
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
|||
|
|
@ -51,30 +51,6 @@ static inline u8 pxl8_colormap_lookup(const pxl8_shader_bindings* b, u8 color, u
|
|||
return cm[(row << 8) | (u32)color];
|
||||
}
|
||||
|
||||
static inline f32 pxl8_light_falloff(const pxl8_shader_ctx* ctx, const pxl8_shader_uniforms* u, u32 light_idx) {
|
||||
if (!u || light_idx >= u->lights_count) return 0.0f;
|
||||
const pxl8_light* light = &u->lights[light_idx];
|
||||
f32 dx = light->position.x - ctx->v_world.x;
|
||||
f32 dy = light->position.y - ctx->v_world.y;
|
||||
f32 dz = light->position.z - ctx->v_world.z;
|
||||
f32 dist_sq = dx * dx + dy * dy + dz * dz;
|
||||
if (dist_sq >= light->radius_sq) return 0.0f;
|
||||
return 1.0f - dist_sq * light->inv_radius_sq;
|
||||
}
|
||||
|
||||
static inline u32 pxl8_light_color(const pxl8_shader_uniforms* u, u32 light_idx) {
|
||||
if (!u || light_idx >= u->lights_count) return 0;
|
||||
const pxl8_light* light = &u->lights[light_idx];
|
||||
return (u32)light->r | ((u32)light->g << 8) | ((u32)light->b << 16);
|
||||
}
|
||||
|
||||
static inline void pxl8_set_light_tint(pxl8_shader_ctx* ctx, u32 color, f32 strength) {
|
||||
if (strength <= 0.0f) return;
|
||||
if (strength > 1.0f) strength = 1.0f;
|
||||
u32 alpha = (u32)(strength * 255.0f);
|
||||
ctx->out_light_color = (color & 0x00FFFFFF) | (alpha << 24);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
56
src/gfx/pxl8_shader_math.h
Normal file
56
src/gfx/pxl8_shader_math.h
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#pragma once
|
||||
|
||||
#include "pxl8_math.h"
|
||||
|
||||
#if defined(PXL8_SIMD_NEON) || defined(PXL8_SIMD_SSE)
|
||||
|
||||
#undef f32
|
||||
#define f32 pxl8_f32_simd
|
||||
|
||||
typedef pxl8_vec2_simd vec2;
|
||||
typedef pxl8_vec3_simd vec3;
|
||||
typedef pxl8_vec4_simd vec4;
|
||||
|
||||
#define set pxl8_f32_simd_set
|
||||
#define add pxl8_f32_simd_add
|
||||
#define sub pxl8_f32_simd_sub
|
||||
#define mul pxl8_f32_simd_mul
|
||||
#define div pxl8_f32_simd_div
|
||||
#define inversesqrt pxl8_f32_simd_rsqrt
|
||||
#define rcp pxl8_f32_simd_rcp
|
||||
#define max pxl8_f32_simd_max
|
||||
#define min pxl8_f32_simd_min
|
||||
#define clamp pxl8_f32_simd_clamp
|
||||
|
||||
#define vec3_set pxl8_vec3_simd_set
|
||||
#define vec3_add pxl8_vec3_simd_add
|
||||
#define vec3_sub pxl8_vec3_simd_sub
|
||||
#define vec3_scale pxl8_vec3_simd_scale
|
||||
#define dot pxl8_vec3_simd_dot
|
||||
#define normalize pxl8_vec3_simd_normalize
|
||||
|
||||
#else
|
||||
|
||||
typedef pxl8_vec2 vec2;
|
||||
typedef pxl8_vec3 vec3;
|
||||
typedef pxl8_vec4 vec4;
|
||||
|
||||
#define set(x) (x)
|
||||
#define add(a, b) ((a) + (b))
|
||||
#define sub(a, b) ((a) - (b))
|
||||
#define mul(a, b) ((a) * (b))
|
||||
#define div(a, b) ((a) / (b))
|
||||
#define inversesqrt pxl8_fast_inv_sqrt
|
||||
#define rcp(x) (1.0f / (x))
|
||||
#define max pxl8_max
|
||||
#define min pxl8_min
|
||||
#define clamp pxl8_clamp
|
||||
|
||||
#define vec3_set(v) (v)
|
||||
#define vec3_add pxl8_vec3_add
|
||||
#define vec3_sub pxl8_vec3_sub
|
||||
#define vec3_scale pxl8_vec3_scale
|
||||
#define dot pxl8_vec3_dot
|
||||
#define normalize pxl8_vec3_normalize
|
||||
|
||||
#endif
|
||||
|
|
@ -2,15 +2,105 @@
|
|||
#include "pxl8_shader.h"
|
||||
#include "pxl8_shader_builtins.h"
|
||||
|
||||
u8 pxl8_shader_lit(
|
||||
pxl8_shader_ctx* ctx,
|
||||
void pxl8_shader_lit(
|
||||
const pxl8_shader_ctx* ctx,
|
||||
const pxl8_shader_bindings* bindings,
|
||||
const pxl8_shader_uniforms* uniforms
|
||||
const pxl8_shader_uniforms* uniforms,
|
||||
u8* colors_out
|
||||
) {
|
||||
#if defined(PXL8_SIMD_SSE) || defined(PXL8_SIMD_NEON)
|
||||
f32 uv_x[4], uv_y[4], color_f[4];
|
||||
i32 px[4], py[4];
|
||||
pxl8_f32_simd_store(uv_x, ctx->v_uv.x);
|
||||
pxl8_f32_simd_store(uv_y, ctx->v_uv.y);
|
||||
pxl8_f32_simd_store(color_f, ctx->v_color);
|
||||
pxl8_i32_simd_store(px, ctx->x);
|
||||
pxl8_i32_simd_store(py, ctx->y);
|
||||
|
||||
u8 tex_idx[4];
|
||||
for (u32 i = 0; i < ctx->color_count; i++) {
|
||||
if (bindings && bindings->atlas) {
|
||||
tex_idx[i] = pxl8_sample_indexed(bindings, (pxl8_vec2){{ uv_x[i], uv_y[i] }});
|
||||
} else {
|
||||
if (uniforms && uniforms->dither) {
|
||||
tex_idx[i] = pxl8_gfx_dither(color_f[i], (u32)px[i], (u32)py[i]);
|
||||
} else {
|
||||
f32 clamped = pxl8_clamp(color_f[i], 0.0f, 255.0f);
|
||||
tex_idx[i] = (u8)(clamped);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pxl8_f32_simd light = ctx->v_light;
|
||||
|
||||
if (uniforms) {
|
||||
pxl8_f32_simd ambient = pxl8_f32_simd_set((f32)uniforms->ambient / 255.0f);
|
||||
light = pxl8_f32_simd_max(light, ambient);
|
||||
|
||||
if (uniforms->celestial_intensity > 0.0f) {
|
||||
pxl8_vec3_simd cel_dir = pxl8_vec3_simd_set(uniforms->celestial_dir);
|
||||
pxl8_f32_simd ndotl = pxl8_f32_simd_sub(
|
||||
pxl8_f32_simd_zero(),
|
||||
pxl8_vec3_simd_dot(ctx->v_normal, cel_dir)
|
||||
);
|
||||
pxl8_f32_simd cel_contrib = pxl8_f32_simd_mul(
|
||||
pxl8_f32_simd_max(ndotl, pxl8_f32_simd_zero()),
|
||||
pxl8_f32_simd_set(uniforms->celestial_intensity)
|
||||
);
|
||||
light = pxl8_f32_simd_add(light, cel_contrib);
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < uniforms->lights_count; i++) {
|
||||
const pxl8_light* l = &uniforms->lights[i];
|
||||
pxl8_vec3_simd light_pos = pxl8_vec3_simd_set(l->position);
|
||||
pxl8_vec3_simd to_light = pxl8_vec3_simd_sub(light_pos, ctx->v_world);
|
||||
pxl8_f32_simd dist_sq = pxl8_vec3_simd_dot(to_light, to_light);
|
||||
|
||||
pxl8_f32_simd in_range = pxl8_f32_simd_cmpgt(
|
||||
pxl8_f32_simd_set(l->radius_sq), dist_sq
|
||||
);
|
||||
if (!pxl8_f32_simd_movemask(in_range)) continue;
|
||||
|
||||
pxl8_f32_simd inv_dist = pxl8_f32_simd_rsqrt(dist_sq);
|
||||
pxl8_vec3_simd light_dir = pxl8_vec3_simd_scale(to_light, inv_dist);
|
||||
pxl8_f32_simd ndotl = pxl8_vec3_simd_dot(ctx->v_normal, light_dir);
|
||||
ndotl = pxl8_f32_simd_max(ndotl, pxl8_f32_simd_zero());
|
||||
|
||||
pxl8_f32_simd falloff = pxl8_f32_simd_sub(
|
||||
pxl8_f32_simd_set(1.0f),
|
||||
pxl8_f32_simd_mul(dist_sq, pxl8_f32_simd_set(l->inv_radius_sq))
|
||||
);
|
||||
falloff = pxl8_f32_simd_max(falloff, pxl8_f32_simd_zero());
|
||||
|
||||
pxl8_f32_simd strength = pxl8_f32_simd_mul(
|
||||
pxl8_f32_simd_mul(pxl8_f32_simd_set(l->intensity), falloff),
|
||||
ndotl
|
||||
);
|
||||
|
||||
light = pxl8_f32_simd_add(light, strength);
|
||||
}
|
||||
}
|
||||
|
||||
light = pxl8_f32_simd_clamp(light, pxl8_f32_simd_zero(), pxl8_f32_simd_set(1.0f));
|
||||
pxl8_f32_simd light_f = pxl8_f32_simd_mul(light, pxl8_f32_simd_set(255.0f));
|
||||
|
||||
f32 light_arr[4];
|
||||
pxl8_f32_simd_store(light_arr, light_f);
|
||||
|
||||
for (u32 i = 0; i < ctx->color_count; i++) {
|
||||
u8 light_u8;
|
||||
if (uniforms && uniforms->dither) {
|
||||
light_u8 = pxl8_gfx_dither(light_arr[i], (u32)px[i], (u32)py[i]);
|
||||
} else {
|
||||
light_u8 = (u8)light_arr[i];
|
||||
}
|
||||
colors_out[i] = pxl8_colormap_lookup(bindings, tex_idx[i], light_u8);
|
||||
}
|
||||
|
||||
#else
|
||||
u8 tex_idx = 0;
|
||||
if (bindings && bindings->atlas) {
|
||||
tex_idx = pxl8_sample_indexed(bindings, ctx->v_uv);
|
||||
if (pxl8_unlikely(tex_idx == 0)) return 0;
|
||||
} else {
|
||||
if (uniforms && uniforms->dither) {
|
||||
tex_idx = pxl8_gfx_dither(ctx->v_color, (u32)ctx->x, (u32)ctx->y);
|
||||
|
|
@ -35,11 +125,6 @@ u8 pxl8_shader_lit(
|
|||
}
|
||||
}
|
||||
|
||||
f32 dyn_strength = 0.0f;
|
||||
f32 dyn_r = 0.0f;
|
||||
f32 dyn_g = 0.0f;
|
||||
f32 dyn_b = 0.0f;
|
||||
|
||||
for (u32 i = 0; i < uniforms->lights_count; i++) {
|
||||
const pxl8_light* l = &uniforms->lights[i];
|
||||
f32 lx = l->position.x - ctx->v_world.x;
|
||||
|
|
@ -58,28 +143,9 @@ u8 pxl8_shader_lit(
|
|||
|
||||
f32 falloff = 1.0f - dist_sq * l->inv_radius_sq;
|
||||
if (falloff <= 0.0f) continue;
|
||||
if (uniforms->dither && falloff < 0.33f) {
|
||||
f32 threshold = (PXL8_BAYER_4X4[((u32)ctx->y & 3) * 4 + ((u32)ctx->x & 3)] + 0.5f) * (1.0f / 16.0f);
|
||||
if (falloff < threshold * 0.33f) continue;
|
||||
}
|
||||
|
||||
f32 strength = ((f32)l->intensity / 255.0f) * falloff * ndotl;
|
||||
if (strength <= 0.0f) continue;
|
||||
|
||||
dyn_strength += strength;
|
||||
dyn_r += strength * (f32)l->r;
|
||||
dyn_g += strength * (f32)l->g;
|
||||
dyn_b += strength * (f32)l->b;
|
||||
}
|
||||
|
||||
if (dyn_strength > 0.0f) {
|
||||
f32 inv = pxl8_fast_rcp(dyn_strength);
|
||||
u8 r = (u8)pxl8_clamp(dyn_r * inv, 0.0f, 255.0f);
|
||||
u8 g = (u8)pxl8_clamp(dyn_g * inv, 0.0f, 255.0f);
|
||||
u8 b = (u8)pxl8_clamp(dyn_b * inv, 0.0f, 255.0f);
|
||||
u8 a = (u8)pxl8_clamp(dyn_strength * 255.0f, 0.0f, 255.0f);
|
||||
ctx->out_light_color = (u32)r | ((u32)g << 8) | ((u32)b << 16) | ((u32)a << 24);
|
||||
light += dyn_strength;
|
||||
f32 strength = l->intensity * falloff * ndotl;
|
||||
light += strength;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -92,15 +158,6 @@ u8 pxl8_shader_lit(
|
|||
light_u8 = pxl8_gfx_dither(light_f, (u32)ctx->x, (u32)ctx->y);
|
||||
}
|
||||
|
||||
u8 shaded = pxl8_colormap_lookup(bindings, tex_idx, light_u8);
|
||||
|
||||
if (uniforms && uniforms->emissive) {
|
||||
u32 rgb = 0x00FFFFFF;
|
||||
if (bindings && bindings->palette) {
|
||||
rgb = bindings->palette[tex_idx] & 0x00FFFFFF;
|
||||
}
|
||||
pxl8_set_light_tint(ctx, rgb, 1.0f);
|
||||
}
|
||||
|
||||
return shaded;
|
||||
colors_out[0] = pxl8_colormap_lookup(bindings, tex_idx, light_u8);
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,37 @@
|
|||
#include "pxl8_shader.h"
|
||||
#include "pxl8_shader_builtins.h"
|
||||
|
||||
u8 pxl8_shader_unlit(
|
||||
pxl8_shader_ctx* ctx,
|
||||
void pxl8_shader_unlit(
|
||||
const pxl8_shader_ctx* ctx,
|
||||
const pxl8_shader_bindings* bindings,
|
||||
const pxl8_shader_uniforms* uniforms
|
||||
const pxl8_shader_uniforms* uniforms,
|
||||
u8* colors_out
|
||||
) {
|
||||
#if defined(PXL8_SIMD_SSE) || defined(PXL8_SIMD_NEON)
|
||||
f32 uv_x[4], uv_y[4], color_f[4];
|
||||
i32 px[4], py[4];
|
||||
pxl8_f32_simd_store(uv_x, ctx->v_uv.x);
|
||||
pxl8_f32_simd_store(uv_y, ctx->v_uv.y);
|
||||
pxl8_f32_simd_store(color_f, ctx->v_color);
|
||||
pxl8_i32_simd_store(px, ctx->x);
|
||||
pxl8_i32_simd_store(py, ctx->y);
|
||||
|
||||
for (u32 i = 0; i < ctx->color_count; i++) {
|
||||
if (bindings && bindings->atlas) {
|
||||
colors_out[i] = pxl8_sample_indexed(bindings, (pxl8_vec2){{ uv_x[i], uv_y[i] }});
|
||||
} else {
|
||||
if (uniforms && uniforms->dither) {
|
||||
colors_out[i] = pxl8_gfx_dither(color_f[i], (u32)px[i], (u32)py[i]);
|
||||
} else {
|
||||
f32 clamped = pxl8_clamp(color_f[i], 0.0f, 255.0f);
|
||||
colors_out[i] = (u8)(clamped);
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
u8 tex_idx = 0;
|
||||
if (bindings && bindings->atlas) {
|
||||
tex_idx = pxl8_sample_indexed(bindings, ctx->v_uv);
|
||||
if (tex_idx == 0) return 0;
|
||||
} else {
|
||||
if (uniforms && uniforms->dither) {
|
||||
tex_idx = pxl8_gfx_dither(ctx->v_color, (u32)ctx->x, (u32)ctx->y);
|
||||
|
|
@ -18,14 +40,6 @@ u8 pxl8_shader_unlit(
|
|||
tex_idx = (u8)(clamped);
|
||||
}
|
||||
}
|
||||
|
||||
if (uniforms && uniforms->emissive) {
|
||||
u32 rgb = 0x00FFFFFF;
|
||||
if (bindings && bindings->palette) {
|
||||
rgb = bindings->palette[tex_idx] & 0x00FFFFFF;
|
||||
}
|
||||
pxl8_set_light_tint(ctx, rgb, 1.0f);
|
||||
}
|
||||
|
||||
return tex_idx;
|
||||
colors_out[0] = tex_idx;
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue