From 0f98bd4a0ba9087dde047acf56dbaeaee18891d0 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Thu, 22 Jan 2026 19:40:18 +0100 Subject: [PATCH] base: use rgba64 format for envmaps --- base/sources/iron.h | 12 +++++++++--- base/tools/amake/aimage.c | 17 ++++++++++++++++- paint/sources/import_envmap.ts | 10 +++++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/base/sources/iron.h b/base/sources/iron.h index 311498d0..0477db54 100644 --- a/base/sources/iron.h +++ b/base/sources/iron.h @@ -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; diff --git a/base/tools/amake/aimage.c b/base/tools/amake/aimage.c index 015785a1..7dd010d6 100644 --- a/base/tools/amake/aimage.c +++ b/base/tools/amake/aimage.c @@ -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); diff --git a/paint/sources/import_envmap.ts b/paint/sources/import_envmap.ts index 66ffa0f1..955fb0a2 100644 --- a/paint/sources/import_envmap.ts +++ b/paint/sources/import_envmap.ts @@ -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;