base: use rgba64 format for envmaps

This commit is contained in:
luboslenco
2026-01-22 19:40:18 +01:00
parent ccca3bf10a
commit 0f98bd4a0b
3 changed files with 30 additions and 9 deletions
+9 -3
View File
@@ -1147,16 +1147,22 @@ gpu_texture_t *gpu_create_texture_from_encoded_bytes(buffer_t *data, string_t *f
texture_format = GPU_TEXTURE_FORMAT_RGBA32;
}
else { // "LZ4F"
int output_size = width * height * 16;
int output_size = width * height * 8;
texture_data = (unsigned char *)malloc(output_size);
LZ4_decompress_safe((char *)data->buffer + 12, (char *)texture_data, compressed_size, output_size);
texture_format = GPU_TEXTURE_FORMAT_RGBA128;
texture_format = GPU_TEXTURE_FORMAT_RGBA64;
}
}
else if (ends_with(format, "hdr")) {
int comp;
texture_data = (unsigned char *)stbi_loadf_from_memory(data->buffer, data->length, &width, &height, &comp, 4);
texture_format = GPU_TEXTURE_FORMAT_RGBA128;
texture_format = GPU_TEXTURE_FORMAT_RGBA64;
// F32 to F16
float *f32_data = (float *)texture_data;
uint16_t *f16_data = (uint16_t *)texture_data;
for (int i = 0; i < width * height * 4; ++i) {
f16_data[i] = float_to_half_fast(f32_data[i]);
}
}
else { // jpg, png, ..
int comp;
+16 -1
View File
@@ -161,16 +161,31 @@ static void write_k(int width, int height, const char *format, char *data, int s
fclose(file);
}
static uint16_t float_to_half_fast(float value) {
union { float f; uint32_t u; } v = {value};
uint32_t sign = (v.u >> 16) & 0x8000;
v.u &= 0x7FFFFFFF;
if (v.u >= 0x47800000) return sign | 0x7C00;
if (v.u < 0x38800000) return sign;
return sign | ((v.u - 0x38000000) >> 13);
}
void export_k(const char *from, const char *to) {
gpu_texture_t img;
if (ends_with(from, ".hdr")) {
img = read_hdr(from);
// F32 to F16
float *f32_data = (float *)img.data;
uint16_t *f16_data = (uint16_t *)img.data;
for (int i = 0; i < img.width * img.height * 4; ++i) {
f16_data[i] = float_to_half_fast(f32_data[i]);
}
}
else {
img = read_png_jpg(from);
}
int pixel_size = img.is_hdr ? 16 : 4;
int pixel_size = img.is_hdr ? 8 : 4;
int max = LZ4_compress_bound(img.width * img.height * pixel_size);
char *compressed = malloc(max);
int compressed_size = LZ4_compress_default((char *)img.data, compressed, img.width * img.height * pixel_size, max);
+5 -5
View File
@@ -18,26 +18,26 @@ function import_envmap_run(path: string, image: gpu_texture_t) {
gpu_vertex_struct_add(vs, "pos", gpu_vertex_data_t.F32_2X);
import_envmap_pipeline.input_layout = vs;
import_envmap_pipeline.color_attachment_count = 1;
ARRAY_ACCESS(import_envmap_pipeline.color_attachment, 0) = gpu_texture_format_t.RGBA128;
ARRAY_ACCESS(import_envmap_pipeline.color_attachment, 0) = gpu_texture_format_t.RGBA64;
gpu_pipeline_compile(import_envmap_pipeline);
import_envmap_params_loc = 0;
import_envmap_radiance_loc = 0;
import_envmap_noise_loc = 1;
import_envmap_radiance = gpu_create_render_target(1024, 512, gpu_texture_format_t.RGBA128);
import_envmap_radiance = gpu_create_render_target(1024, 512, gpu_texture_format_t.RGBA64);
import_envmap_mips = [];
let w: i32 = 512;
for (let i: i32 = 0; i < 5; ++i) {
array_push(import_envmap_mips, gpu_create_render_target(w, w > 1 ? math_floor(w / 2) : 1, gpu_texture_format_t.RGBA128));
array_push(import_envmap_mips, gpu_create_render_target(w, w > 1 ? math_floor(w / 2) : 1, gpu_texture_format_t.RGBA64));
w = math_floor(w / 2);
}
}
// Down-scale to 1024x512
draw_begin(import_envmap_radiance);
draw_set_pipeline(pipes_copy128);
draw_set_pipeline(pipes_copy64);
draw_scaled_image(image, 0, 0, 1024, 512);
draw_set_pipeline(null);
draw_end();
@@ -111,7 +111,7 @@ function import_envmap_get_spherical_harmonics(source: buffer_t, source_width: i
import_envmap_n = import_envmap_reverse_equirect(x / source_width, y / source_height);
for (let i: i32 = 0; i < 3; ++i) {
let value: f32 = buffer_get_f32(source, ((x + y * source_width) * 16 + i * 4));
let value: f32 = buffer_get_f16(source, ((x + y * source_width) * 8 + i * 2));
value = math_pow(value, 1.0 / 2.2);
sh[0 + i] += value * weight1;