feat: port MGC Sabre resolve to GLES

This commit is contained in:
zhoubinjia committed 2026-11-03 12:52:29 +08:00
1 parent 74b91e815d
commit f3921777e5
9 files changed
+525 -947

No files matched your search

-37
View File
@@ -11,40 +11,6 @@ set(
)
option(PHOTON_ENABLE_DNG_JXL "Enable Adobe DNG SDK JPEG XL support" OFF)
set(
MGC_SABRE_CAPSULE
"${CMAKE_CURRENT_SOURCE_DIR}/mgc_denoise_static/mgc_sabre_resolve_capsule.bin"
)
set(
MGC_SABRE_CAPSULE_SHA256
"725c5ac2c2de63e7f17c8fb3516fe176d2387c6eed579da25e35df12588e0e69"
)
if(NOT EXISTS "${MGC_SABRE_CAPSULE}")
message(
FATAL_ERROR
"MGC Sabre Resolve capsule is missing; regenerate it with "
"scripts/research/generate_mgc_sabre_resolve_static_asm.py"
)
endif()
file(SHA256 "${MGC_SABRE_CAPSULE}" MGC_SABRE_CAPSULE_ACTUAL_SHA256)
if(NOT MGC_SABRE_CAPSULE_ACTUAL_SHA256 STREQUAL MGC_SABRE_CAPSULE_SHA256)
message(FATAL_ERROR "MGC Sabre Resolve capsule hash mismatch")
endif()
set(
MGC_SABRE_ASM
"${CMAKE_CURRENT_BINARY_DIR}/mgc_sabre_resolve_static.S"
)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/mgc_denoise_static/mgc_sabre_resolve_static.S.in"
"${MGC_SABRE_ASM}"
@ONLY
)
set_source_files_properties(
"${MGC_SABRE_ASM}"
PROPERTIES
OBJECT_DEPENDS "${MGC_SABRE_CAPSULE}"
)
set(
MGC_SHARPEN_CAPSULE
"${CMAKE_CURRENT_SOURCE_DIR}/mgc_denoise_static/mgc_sharpen_capsule.bin"
@@ -305,7 +271,6 @@ set_source_files_properties(
# as configure dependencies so a missing output also causes CMake to regenerate it.
set_property(
DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
"${MGC_SABRE_ASM}"
"${MGC_SPATIAL_RGB_ASM}"
"${MGC_RAISR_ASM}"
"${MGC_RAISR_TABLES_ASM}"
@@ -422,7 +387,6 @@ add_library(my-native-lib SHARED
dcp_parser.cpp
mgc_denoise_static/mgc_halide_runtime.cpp
mgc_denoise_static/mgc_denoise_static.cpp
mgc_denoise_static/mgc_sabre_resolve_jni.cpp
mgc_denoise_static/mgc_spatial_rgb_merge_jni.cpp
mgc_denoise_static/mgc_guided_upsample_jni.cpp
mgc_denoise_static/mgc_sharpen_jni.cpp
@@ -434,7 +398,6 @@ add_library(my-native-lib SHARED
"${MGC_RAISR_ASM}"
"${MGC_RAISR_TABLES_ASM}"
"${MGC_POLYSHARP_ASM}"
"${MGC_SABRE_ASM}"
"${MGC_SPATIAL_RGB_ASM}"
${LIBRAW_SOURCES}
${ADOBE_DNG_SOURCES}
@@ -1,211 +0,0 @@
#include <android/log.h>
#include <jni.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
namespace {
constexpr char kTag[] = "PLog_MgcSabreResolve";
struct HalideType {
uint8_t code;
uint8_t bits;
uint16_t lanes;
};
struct HalideDimension {
int32_t min;
int32_t extent;
int32_t stride;
uint32_t flags;
};
struct HalideBuffer {
uint64_t device;
const void* device_interface;
uint8_t* host;
uint64_t flags;
HalideType type;
int32_t dimensions;
const HalideDimension* dim;
void* padding;
};
static_assert(sizeof(HalideBuffer) == 56);
static_assert(sizeof(HalideDimension) == 16);
static_assert(offsetof(HalideBuffer, type) == 32);
static_assert(offsetof(HalideBuffer, dimensions) == 36);
static_assert(offsetof(HalideBuffer, dim) == 40);
constexpr HalideType kUInt16 = {1, 16, 1};
constexpr HalideType kFloat32 = {2, 32, 1};
constexpr int32_t Camera2CfaToMgcBayerPattern(int cfa_pattern) {
switch (cfa_pattern) {
case 0:
return 1; // RGGB
case 1:
return 3; // GRBG
case 2:
return 4; // GBRG
case 3:
return 2; // BGGR
default:
return 0;
}
}
bool CopyThree(JNIEnv* env, jfloatArray source, std::array<float, 3>* output) {
if (source == nullptr || env->GetArrayLength(source) != 3) return false;
env->GetFloatArrayRegion(source, 0, 3, output->data());
if (env->ExceptionCheck()) return false;
return std::all_of(output->begin(), output->end(), [](float value) {
return std::isfinite(value);
});
}
HalideBuffer MakeBuffer(
void* host,
HalideType type,
const HalideDimension* dimensions,
int dimension_count) {
HalideBuffer buffer = {};
buffer.host = static_cast<uint8_t*>(host);
buffer.type = type;
buffer.dimensions = dimension_count;
buffer.dim = dimensions;
return buffer;
}
} // namespace
// Exact generated ABI at libgcastartup.so+0x34a55d4. In particular, the two
// F32 vectors are ordered final_gains first and final_black_level second.
extern "C" int photon_mgc_resolve_sabre_halide(
void* user_context,
int32_t bayer_pattern,
HalideBuffer* sabre_accumulated_color,
HalideBuffer* final_gains,
HalideBuffer* final_black_level,
int16_t demosaic_white_level,
HalideBuffer* output,
float output_white_level,
float demosaic_blend_scale,
float demosaic_blend_bias,
float demosaic_sharpness_scale);
extern "C" JNIEXPORT jint JNICALL
Java_com_hinnka_mycamera_processor_MgcSabreResolver_nativeResolve(
JNIEnv* env,
jobject,
jobject accumulated_color_buffer,
jobject output_rgb_buffer,
jint width,
jint height,
jint cfa_pattern,
jfloatArray final_black_level_array,
jfloatArray final_gains_array,
jint demosaic_white_level,
jfloat output_white_level,
jfloat demosaic_blend_scale,
jfloat demosaic_blend_bias,
jfloat demosaic_sharpness_scale) {
const int32_t mgc_pattern = Camera2CfaToMgcBayerPattern(cfa_pattern);
if (accumulated_color_buffer == nullptr || output_rgb_buffer == nullptr ||
width <= 0 || height <= 0 || mgc_pattern == 0 ||
demosaic_white_level <= 0 ||
demosaic_white_level > std::numeric_limits<int16_t>::max() ||
!std::isfinite(output_white_level) || output_white_level < 0.0f ||
!std::isfinite(demosaic_blend_scale) ||
!std::isfinite(demosaic_blend_bias) ||
!std::isfinite(demosaic_sharpness_scale)) {
return -1;
}
auto* accumulated_color = static_cast<uint16_t*>(
env->GetDirectBufferAddress(accumulated_color_buffer));
auto* output_rgb = static_cast<uint16_t*>(
env->GetDirectBufferAddress(output_rgb_buffer));
std::array<float, 3> final_black_level = {};
std::array<float, 3> final_gains = {};
if (accumulated_color == nullptr || output_rgb == nullptr ||
!CopyThree(env, final_black_level_array, &final_black_level) ||
!CopyThree(env, final_gains_array, &final_gains) ||
std::any_of(final_gains.begin(), final_gains.end(), [](float gain) {
return gain <= 0.0f;
})) {
return -1;
}
const int64_t pixel_count = static_cast<int64_t>(width) * height;
const int64_t accumulated_bytes =
pixel_count * 4 * static_cast<int64_t>(sizeof(uint16_t));
const int64_t output_bytes =
pixel_count * 3 * static_cast<int64_t>(sizeof(uint16_t));
if (pixel_count <= 0 ||
pixel_count > std::numeric_limits<int32_t>::max() ||
width > std::numeric_limits<int32_t>::max() / 4 ||
env->GetDirectBufferCapacity(accumulated_color_buffer) < accumulated_bytes ||
env->GetDirectBufferCapacity(output_rgb_buffer) < output_bytes) {
return -1;
}
const HalideDimension accumulated_dimensions[3] = {
{0, width, 4, 0},
{0, height, 4 * width, 0},
{0, 4, 1, 0},
};
const HalideDimension output_dimensions[3] = {
{0, width, 1, 0},
{0, height, width, 0},
{0, 3, width * height, 0},
};
const HalideDimension vector_dimension[1] = {{0, 3, 1, 0}};
HalideBuffer accumulated = MakeBuffer(
accumulated_color,
kUInt16,
accumulated_dimensions,
3);
HalideBuffer output = MakeBuffer(output_rgb, kUInt16, output_dimensions, 3);
HalideBuffer black = MakeBuffer(
final_black_level.data(),
kFloat32,
vector_dimension,
1);
HalideBuffer gains = MakeBuffer(
final_gains.data(),
kFloat32,
vector_dimension,
1);
const int result = photon_mgc_resolve_sabre_halide(
nullptr,
mgc_pattern,
&accumulated,
&gains,
&black,
static_cast<int16_t>(demosaic_white_level),
&output,
output_white_level,
demosaic_blend_scale,
demosaic_blend_bias,
demosaic_sharpness_scale);
if (result != 0) {
__android_log_print(
ANDROID_LOG_ERROR,
kTag,
"ResolveSabreHalide failed result=%d size=%dx%d cfa=%d mgcPattern=%d",
result,
width,
height,
cfa_pattern,
mgc_pattern);
}
return result;
}
@@ -1,492 +0,0 @@
/* Generated mechanically from pinned MGC 9.6.080 V24 ResolveSabreHalide.
* Do not edit; regenerate with
* scripts/research/generate_mgc_sabre_resolve_static_asm.py.
*/
#if !defined(__aarch64__)
#error "MGC Sabre Resolve static lift is arm64-only"
#endif
.hidden photon_mgc_halide_error_5ef61ac
.hidden photon_mgc_halide_error_5ef6294
.hidden photon_mgc_halide_error_5ef636c
.hidden photon_mgc_halide_error_5ef63f8
.hidden photon_mgc_halide_error_5ef64dc
.hidden photon_mgc_halide_error_5ef6568
.hidden photon_mgc_halide_error_5ef6600
.hidden photon_mgc_halide_error_5ef6794
.hidden photon_mgc_halide_error_5ef6a74
.hidden photon_mgc_halide_error_5ef6a94
.hidden photon_mgc_halide_error_5ef6b6c
.hidden photon_mgc_halide_error_5ef6bd8
.hidden photon_mgc_halide_error_5ef6c38
.hidden photon_mgc_halide_do_par_for
.hidden photon_mgc_halide_free
.hidden photon_mgc_halide_malloc
.hidden photon_mgc_halide_trace
.section .text.photon_mgc.ResolveSabreHalide,"ax",@progbits
.p2align 12
.global photon_mgc_resolve_sabre_halide
.hidden photon_mgc_resolve_sabre_halide
.type photon_mgc_resolve_sabre_halide, %function
photon_mgc_sabre_page_349b000:
.incbin "@MGC_SABRE_CAPSULE@", 0x0, 0x1b8
photon_mgc_sabre_address_349b1b8:
.incbin "@MGC_SABRE_CAPSULE@", 0x1b8, 0x1fc
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x3b4, 0x4c
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x400, 0x1c
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x41c, 0x40
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x45c, 0x18
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x474, 0x1c4
adrp x4, photon_mgc_sabre_page_6b1000
.incbin "@MGC_SABRE_CAPSULE@", 0x638, 0xe4
adrp x2, photon_mgc_sabre_page_6ba000
.incbin "@MGC_SABRE_CAPSULE@", 0x71c, 0x8c8
photon_mgc_sabre_page_349c000:
.incbin "@MGC_SABRE_CAPSULE@", 0xfe4, 0x1000
photon_mgc_sabre_page_349d000:
.incbin "@MGC_SABRE_CAPSULE@", 0x1fe4, 0x1000
photon_mgc_sabre_page_349e000:
.incbin "@MGC_SABRE_CAPSULE@", 0x2fe4, 0x48c
adrp x9, photon_mgc_sabre_page_6b2000
.incbin "@MGC_SABRE_CAPSULE@", 0x3470, 0x8
adrp x9, photon_mgc_sabre_page_6b0000
.incbin "@MGC_SABRE_CAPSULE@", 0x3478, 0x538
adrp x9, photon_mgc_sabre_page_6b2000
.incbin "@MGC_SABRE_CAPSULE@", 0x39b0, 0xec
adrp x9, photon_mgc_sabre_page_6b0000
.incbin "@MGC_SABRE_CAPSULE@", 0x3a9c, 0x1c0
adrp x9, photon_mgc_sabre_page_6b9000
.incbin "@MGC_SABRE_CAPSULE@", 0x3c5c, 0x200
adrp x9, photon_mgc_sabre_page_6ba000
.incbin "@MGC_SABRE_CAPSULE@", 0x3e5c, 0x170
photon_mgc_sabre_page_349f000:
.incbin "@MGC_SABRE_CAPSULE@", 0x3fcc, 0x834
adrp x18, photon_mgc_sabre_page_6b5000
adrp x0, photon_mgc_sabre_page_6af000
adrp x1, photon_mgc_sabre_page_6b6000
.incbin "@MGC_SABRE_CAPSULE@", 0x4800, 0x8
adrp x2, photon_mgc_sabre_page_6b1000
.incbin "@MGC_SABRE_CAPSULE@", 0x4808, 0x8
adrp x3, photon_mgc_sabre_page_6b6000
.incbin "@MGC_SABRE_CAPSULE@", 0x4810, 0x4
adrp x4, photon_mgc_sabre_page_6b6000
adrp x5, photon_mgc_sabre_page_6b9000
adrp x6, photon_mgc_sabre_page_6b0000
.incbin "@MGC_SABRE_CAPSULE@", 0x4814, 0x5ac
adrp x9, photon_mgc_sabre_page_6b2000
.incbin "@MGC_SABRE_CAPSULE@", 0x4dc0, 0x8
adrp x9, photon_mgc_sabre_page_6b0000
.incbin "@MGC_SABRE_CAPSULE@", 0x4dc8, 0x1dc
photon_mgc_sabre_page_34a0000:
.incbin "@MGC_SABRE_CAPSULE@", 0x4fa4, 0x364
adrp x9, photon_mgc_sabre_page_6b2000
.incbin "@MGC_SABRE_CAPSULE@", 0x5308, 0xec
adrp x9, photon_mgc_sabre_page_6b0000
.incbin "@MGC_SABRE_CAPSULE@", 0x53f4, 0x1d0
adrp x9, photon_mgc_sabre_page_6b9000
.incbin "@MGC_SABRE_CAPSULE@", 0x55c4, 0x200
adrp x9, photon_mgc_sabre_page_6ba000
.incbin "@MGC_SABRE_CAPSULE@", 0x57c4, 0x7d0
photon_mgc_sabre_page_34a1000:
.incbin "@MGC_SABRE_CAPSULE@", 0x5f94, 0x1fc
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x6190, 0x54
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x61e4, 0x94
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x6278, 0x48
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x62c0, 0x1c
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x62dc, 0x38
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x6314, 0x20
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x6334, 0x298
adrp x9, photon_mgc_sabre_page_6ba000
.incbin "@MGC_SABRE_CAPSULE@", 0x65cc, 0x18
adrp x8, photon_mgc_sabre_page_6b1000
.incbin "@MGC_SABRE_CAPSULE@", 0x65e4, 0x98c
photon_mgc_sabre_page_34a2000:
.incbin "@MGC_SABRE_CAPSULE@", 0x6f70, 0x1000
photon_mgc_sabre_page_34a3000:
.incbin "@MGC_SABRE_CAPSULE@", 0x7f70, 0x80c
adrp x8, photon_mgc_sabre_page_6b2000
.incbin "@MGC_SABRE_CAPSULE@", 0x877c, 0xc
adrp x8, photon_mgc_sabre_page_6b0000
.incbin "@MGC_SABRE_CAPSULE@", 0x8788, 0x544
adrp x8, photon_mgc_sabre_page_6b2000
.incbin "@MGC_SABRE_CAPSULE@", 0x8ccc, 0xec
adrp x8, photon_mgc_sabre_page_6b0000
.incbin "@MGC_SABRE_CAPSULE@", 0x8db8, 0x1a8
photon_mgc_sabre_page_34a4000:
.incbin "@MGC_SABRE_CAPSULE@", 0x8f60, 0x8
adrp x15, photon_mgc_sabre_page_6b9000
.incbin "@MGC_SABRE_CAPSULE@", 0x8f68, 0x208
adrp x8, photon_mgc_sabre_page_6ba000
.incbin "@MGC_SABRE_CAPSULE@", 0x9170, 0x9d8
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9b48, 0x60
bl photon_mgc_halide_malloc
.incbin "@MGC_SABRE_CAPSULE@", 0x9ba8, 0xa8
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9c50, 0x10
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9c60, 0x10
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9c70, 0xc
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9c7c, 0x10
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9c8c, 0x4c
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9cd8, 0x8
adrp x2, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9ce0, 0x4
adrp x3, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9ce4, 0x4
bl photon_mgc_halide_error_5ef6c38
.incbin "@MGC_SABRE_CAPSULE@", 0x9ce8, 0xc
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9cf4, 0x8
adrp x2, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9cfc, 0x4
adrp x3, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9d00, 0x8
bl photon_mgc_halide_error_5ef6c38
.incbin "@MGC_SABRE_CAPSULE@", 0x9d08, 0x24
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9d2c, 0x8
adrp x2, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9d34, 0x4
adrp x3, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9d38, 0x8
bl photon_mgc_halide_error_5ef6c38
.incbin "@MGC_SABRE_CAPSULE@", 0x9d40, 0x40
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9d80, 0x1c
bl photon_mgc_halide_error_5ef64dc
.incbin "@MGC_SABRE_CAPSULE@", 0x9d9c, 0x3c
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9dd8, 0x18
bl photon_mgc_halide_error_5ef64dc
.incbin "@MGC_SABRE_CAPSULE@", 0x9df0, 0x3c
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9e2c, 0x7c
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9ea8, 0x10
bl photon_mgc_halide_error_5ef64dc
.incbin "@MGC_SABRE_CAPSULE@", 0x9eb8, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9ec0, 0x10
bl photon_mgc_halide_error_5ef64dc
.incbin "@MGC_SABRE_CAPSULE@", 0x9ed0, 0x14
adrp x1, photon_mgc_sabre_page_b9b000
photon_mgc_sabre_page_34a5000:
.incbin "@MGC_SABRE_CAPSULE@", 0x9ee4, 0x1c
bl photon_mgc_halide_error_5ef64dc
.incbin "@MGC_SABRE_CAPSULE@", 0x9f00, 0x10
bl photon_mgc_halide_error_5ef6a74
.incbin "@MGC_SABRE_CAPSULE@", 0x9f10, 0x3c
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9f4c, 0x14
bl photon_mgc_halide_error_5ef64dc
.incbin "@MGC_SABRE_CAPSULE@", 0x9f60, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0x9f68, 0x14
bl photon_mgc_halide_error_5ef64dc
.incbin "@MGC_SABRE_CAPSULE@", 0x9f7c, 0x24
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9fa0, 0x10
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9fb0, 0x20
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9fd0, 0xc
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9fdc, 0x10
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0x9fec, 0x54
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa040, 0x28
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa068, 0x2c
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa094, 0x24
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa0b8, 0x30
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa0e8, 0x24
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa10c, 0x48
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa154, 0x30
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa184, 0x3c
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa1c0, 0x28
bl photon_mgc_halide_free
.incbin "@MGC_SABRE_CAPSULE@", 0xa1e8, 0x20
bl photon_mgc_halide_error_5ef6a74
.incbin "@MGC_SABRE_CAPSULE@", 0xa208, 0x48
bl photon_mgc_halide_error_5ef6a74
.incbin "@MGC_SABRE_CAPSULE@", 0xa250, 0x4c
bl photon_mgc_halide_error_5ef6a74
.incbin "@MGC_SABRE_CAPSULE@", 0xa29c, 0x34
bl photon_mgc_halide_error_5ef6a74
.incbin "@MGC_SABRE_CAPSULE@", 0xa2d0, 0x48
bl photon_mgc_halide_error_5ef6a74
.incbin "@MGC_SABRE_CAPSULE@", 0xa318, 0x48
bl photon_mgc_halide_error_5ef6a74
.incbin "@MGC_SABRE_CAPSULE@", 0xa360, 0x4c
bl photon_mgc_halide_error_5ef6a74
.incbin "@MGC_SABRE_CAPSULE@", 0xa3ac, 0x50
bl photon_mgc_halide_error_5ef6a74
.incbin "@MGC_SABRE_CAPSULE@", 0xa3fc, 0x48
photon_mgc_sabre_address_34a55d4:
photon_mgc_resolve_sabre_halide:
.incbin "@MGC_SABRE_CAPSULE@", 0xa444, 0xcc
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xa510, 0x2c0
adrp x15, photon_mgc_sabre_page_6b4000
.incbin "@MGC_SABRE_CAPSULE@", 0xa7d0, 0x30
adrp x15, photon_mgc_sabre_page_6b4000
.incbin "@MGC_SABRE_CAPSULE@", 0xa800, 0x54
adrp x17, photon_mgc_sabre_page_6ae000
.incbin "@MGC_SABRE_CAPSULE@", 0xa854, 0x2c
adrp x2, photon_mgc_sabre_page_6b9000
.incbin "@MGC_SABRE_CAPSULE@", 0xa880, 0xc4
adrp x17, photon_mgc_sabre_page_6aa000
.incbin "@MGC_SABRE_CAPSULE@", 0xa944, 0x440
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xad84, 0x40
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xadc4, 0x4c
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xae10, 0x3c
photon_mgc_sabre_page_34a6000:
.incbin "@MGC_SABRE_CAPSULE@", 0xae4c, 0x4
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xae50, 0x30
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xae80, 0x2c
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xaeac, 0x48
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xaef4, 0x34
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xaf28, 0x4
adrp x3, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xaf2c, 0x28
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xaf54, 0x38
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xaf8c, 0x2c
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xafb8, 0x28
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xafe0, 0x28
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb008, 0x3c
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb044, 0x34
b photon_mgc_halide_error_5ef63f8
.incbin "@MGC_SABRE_CAPSULE@", 0xb078, 0x240
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb2b8, 0x34
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb2ec, 0x28
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb314, 0x28
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb33c, 0x20
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb35c, 0x24
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb380, 0x20
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb3a0, 0x3c
b photon_mgc_halide_error_5ef6600
.incbin "@MGC_SABRE_CAPSULE@", 0xb3dc, 0x28
adrp x22, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb404, 0x4
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb408, 0x20
bl photon_mgc_halide_trace
.incbin "@MGC_SABRE_CAPSULE@", 0xb428, 0x8c
adrp x1, photon_mgc_sabre_page_6145000
.incbin "@MGC_SABRE_CAPSULE@", 0xb4b4, 0x1d4
bl photon_mgc_halide_do_par_for
.incbin "@MGC_SABRE_CAPSULE@", 0xb688, 0x14
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb69c, 0x20
bl photon_mgc_halide_trace
.incbin "@MGC_SABRE_CAPSULE@", 0xb6bc, 0x38
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb6f4, 0x4
adrp x2, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb6f8, 0x40
b photon_mgc_halide_error_5ef61ac
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb738, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb740, 0xc
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb74c, 0x30
b photon_mgc_halide_error_5ef6a94
.incbin "@MGC_SABRE_CAPSULE@", 0xb77c, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb784, 0x38
b photon_mgc_halide_error_5ef6294
.incbin "@MGC_SABRE_CAPSULE@", 0xb7bc, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb7c4, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb7cc, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb7dc, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb7ec, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb7fc, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb804, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb814, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb824, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb82c, 0x34
b photon_mgc_halide_error_5ef6b6c
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb860, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb870, 0x3c
b photon_mgc_halide_error_5ef64dc
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb8ac, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb8b4, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb8bc, 0x34
b photon_mgc_halide_error_5ef6bd8
.incbin "@MGC_SABRE_CAPSULE@", 0xb8f0, 0x4
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb8f4, 0x18
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb90c, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb91c, 0x14
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb930, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb940, 0x18
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb958, 0x14
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb96c, 0x4
adrp x3, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb970, 0x14
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb984, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb994, 0x14
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb9a8, 0x4
adrp x3, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb9ac, 0x14
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb9c0, 0x34
b photon_mgc_halide_error_5ef636c
.incbin "@MGC_SABRE_CAPSULE@", 0xb9f4, 0x4
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xb9f8, 0x10
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xba08, 0x38
b photon_mgc_halide_error_5ef6568
.incbin "@MGC_SABRE_CAPSULE@", 0xba40, 0x8
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xba48, 0x4
adrp x3, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xba4c, 0x14
adrp x1, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xba60, 0x4
adrp x3, photon_mgc_sabre_page_b9b000
.incbin "@MGC_SABRE_CAPSULE@", 0xba64, 0x34
b photon_mgc_halide_error_5ef6794
.incbin "@MGC_SABRE_CAPSULE@", 0xba98, 0x284
.section .rodata.photon_mgc.sabre_page_6aa000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6aa000:
.incbin "@MGC_SABRE_CAPSULE@", 0xbd1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6ae000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6ae000:
.incbin "@MGC_SABRE_CAPSULE@", 0xcd1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6af000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6af000:
.incbin "@MGC_SABRE_CAPSULE@", 0xdd1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6b0000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6b0000:
.incbin "@MGC_SABRE_CAPSULE@", 0xed1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6b1000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6b1000:
.incbin "@MGC_SABRE_CAPSULE@", 0xfd1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6b2000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6b2000:
.incbin "@MGC_SABRE_CAPSULE@", 0x10d1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6b4000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6b4000:
.incbin "@MGC_SABRE_CAPSULE@", 0x11d1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6b5000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6b5000:
.incbin "@MGC_SABRE_CAPSULE@", 0x12d1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6b6000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6b6000:
.incbin "@MGC_SABRE_CAPSULE@", 0x13d1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6b9000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6b9000:
.incbin "@MGC_SABRE_CAPSULE@", 0x14d1c, 0x1000
.section .rodata.photon_mgc.sabre_page_6ba000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_6ba000:
.incbin "@MGC_SABRE_CAPSULE@", 0x15d1c, 0x1000
.section .rodata.photon_mgc.sabre_page_b9b000,"a",@progbits
.p2align 12
photon_mgc_sabre_page_b9b000:
.incbin "@MGC_SABRE_CAPSULE@", 0x16d1c, 0x1000
.section .data.rel.ro.photon_mgc.sabre_page_6145000,"aw",@progbits
.p2align 12
photon_mgc_sabre_page_6145000:
.incbin "@MGC_SABRE_CAPSULE@", 0x17d1c, 0x810
.xword photon_mgc_sabre_address_349b1b8
.incbin "@MGC_SABRE_CAPSULE@", 0x1852c, 0x7e8
.section .note.GNU-stack,"",@progbits
@@ -14,7 +14,6 @@ import com.hinnka.mycamera.camera.MultiFrameConfig
import com.hinnka.mycamera.model.SafeImage
import com.hinnka.mycamera.raw.RawDenoisePipeline
import com.hinnka.mycamera.raw.denoise.PhotonMergeNoise
import com.hinnka.mycamera.raw.RawFloatTextureTransfer
import com.hinnka.mycamera.raw.RawSceneAERawStats
import com.hinnka.mycamera.utils.DngCaptureDiagnostics
import com.hinnka.mycamera.utils.DirectBufferPixelPacker
@@ -518,6 +517,10 @@ internal class GlesMgcRawSpatialStacker(
private var sabreCopyMaskProgram = 0
private var sabreCopyAlphaProgram = 0
private var sabreDehomogenizeProgram = 0
private var sabreResolveConvertProgram = 0
private var sabreResolveGreenBalanceProgram = 0
private var sabreResolveGreenProgram = 0
private var sabreResolveOutputProgram = 0
/** §2.2 merge variance accumulator of the merge in flight; released with the merge. */
private var mergeNoise: PhotonMergeNoise? = null
@@ -2056,8 +2059,6 @@ internal class GlesMgcRawSpatialStacker(
return null
}
var cpuOutput: ByteBuffer? = null
val sabreTransfer = RawFloatTextureTransfer(RawFloatTextureTransfer.Layout.HALF)
var sabreResolvedRgb: ByteBuffer? = null
var exportedTexture = 0
var postprocessedUi = 0
var exportedCompletionTimeline: GpuStackCompletionTimeline? = null
@@ -2069,7 +2070,6 @@ internal class GlesMgcRawSpatialStacker(
if (useCurrentGlContext) attachCurrentEgl() else initEgl()
ensureGles3()
initSabrePrograms()
sabreTransfer.prepare(width, height)
renderFbo = createFramebuffer()
applyRawRenderState()
@@ -2348,15 +2348,9 @@ internal class GlesMgcRawSpatialStacker(
alphaScale = accumulatedWeightScale / frames.size.toFloat(),
alphaBias = 1f / frames.size.toFloat(),
)
// The original merge shaders write normalized, white-balanced half floats. The
// Resolve AOT accepts those RGBA16F bit patterns directly; rawScale applies only to
// the black/white scalar parameters below.
sabreResolvedRgb = checkNotNull(
LargeDirectBuffer.allocate(
width.toLong() * height * 3L * Short.SIZE_BYTES,
"MGC Sabre Resolve RGB16",
),
) { "Unable to allocate MGC Sabre Resolve output" }
// The original merge shaders write normalized, white-balanced half floats, which
// ResolveSabre consumes directly; rawScale applies only to the black/white scalar
// parameters below.
val demosaicWhiteLevel = minOf(
(sensorWhiteLevel * sabreResolveRawScale).toInt(),
Short.MAX_VALUE.toInt(),
@@ -2370,44 +2364,26 @@ internal class GlesMgcRawSpatialStacker(
"black=${sabreResolveFinalBlackLevel.contentToString()} " +
"gains=${sabreResolveFinalGains.contentToString()} " +
"demosaicWhite=$demosaicWhiteLevel " +
"outputWhite=${sabreResolveParameters.outputWhiteLevel} " +
"greenImbalanceRetention=" +
"${sabreResolveParameters.greenImbalanceRetention} " +
"demosaicSharpness=${sabreResolveParameters.demosaicSharpness}",
)
sabreTransfer.read(
accumulatedColor, width, height, label = "MGC Sabre Resolve",
writable = false,
) { accumulated ->
MgcSabreResolver.resolve(
accumulatedColorRgba16f = accumulated,
outputRgb16Planar = checkNotNull(sabreResolvedRgb),
width = width,
height = height,
cfaPattern = cfaPattern,
finalBlackLevel = sabreResolveFinalBlackLevel,
finalGains = sabreResolveFinalGains,
demosaicWhiteLevel = demosaicWhiteLevel,
outputWhiteLevel = sabreResolveParameters.outputWhiteLevel,
demosaicBlendScale = -demosaicBlendRange * frames.size,
demosaicBlendBias = SABRE_DEMOSAIC_BLEND_END / demosaicBlendRange,
demosaicSharpnessScale =
demosaicWhiteLevel * sabreResolveParameters.demosaicSharpness,
)
}
logSabreResolvedRgbStats(sabreResolvedRgb)
sabreTransfer.releaseBuffers()
val nativeResolvedPlanes = IntArray(3) {
createTexture(
width,
height,
GLES30.GL_R16UI,
GLES30.GL_NEAREST,
)
}
uploadSabreResolvedRgb16Planar(sabreResolvedRgb, nativeResolvedPlanes)
LargeDirectBuffer.free(sabreResolvedRgb)
sabreResolvedRgb = null
releaseSabreMergePhaseTextures(nativeResolvedPlanes)
val resolveStartNs = System.nanoTime()
val resolvedPlanes = renderSabreResolve(
accumulatedColor = accumulatedColor,
finalBlackLevel = sabreResolveFinalBlackLevel,
demosaicWhiteLevel = demosaicWhiteLevel,
greenImbalanceRetention = sabreResolveParameters.greenImbalanceRetention,
demosaicBlendScale = -demosaicBlendRange * frames.size,
demosaicBlendBias = SABRE_DEMOSAIC_BLEND_END / demosaicBlendRange,
demosaicQuantizationScale =
demosaicWhiteLevel * sabreResolveParameters.demosaicSharpness,
)
PLog.i(
SABRE_TAG,
"MGC Sabre Resolve GPU submit=${elapsedMs(resolveStartNs)}ms",
)
releaseSabreMergePhaseTextures(resolvedPlanes)
val lensShadingTexture = createLensShadingTexture()
val fullOutput = MgcSpatialRgbRect(0, 0, width, height)
val fullOutputTile = MgcSpatialRgbTile(index = 0, outputCore = fullOutput)
@@ -2416,7 +2392,7 @@ internal class GlesMgcRawSpatialStacker(
}
chromaPostprocessor.initStorage(listOf(fullOutputTile))
renderSabreOutputTransform(
resolvedRgbPlanes = nativeResolvedPlanes,
resolvedRgbPlanes = resolvedPlanes,
lensShadingTexture = lensShadingTexture,
finalBlackLevel = sabreResolveFinalBlackLevel,
demosaicWhiteLevel = demosaicWhiteLevel.toFloat(),
@@ -2525,10 +2501,8 @@ internal class GlesMgcRawSpatialStacker(
null
} finally {
images.forEach { it.close() }
sabreTransfer.release()
release()
GlesGpuScheduler.restoreCurrentThreadPriority(originalThreadPriority, SABRE_TAG)
LargeDirectBuffer.free(sabreResolvedRgb)
if (!returned) {
exportedCompletionTimeline?.releasePending()
LargeDirectBuffer.free(cpuOutput)
@@ -2836,6 +2810,97 @@ internal class GlesMgcRawSpatialStacker(
}
}
/**
* ResolveSabre on the GPU (see [GlesMgcSabreResolveShaders]): demosaics the dehomogenized
* accumulation, blends it with the accumulated colour by coverage and returns the R/G/B
* RAW14 planes as R16UI textures. The int16 intermediates are padded so every pass reads the
* virtual-CFA neighbourhood the original AOT reads.
*/
private fun renderSabreResolve(
accumulatedColor: Int,
finalBlackLevel: FloatArray,
demosaicWhiteLevel: Int,
greenImbalanceRetention: Float,
demosaicBlendScale: Float,
demosaicBlendBias: Float,
demosaicQuantizationScale: Float,
): IntArray {
require(finalBlackLevel.size == 3)
require(demosaicWhiteLevel in 1..Short.MAX_VALUE.toInt())
require(demosaicQuantizationScale.isFinite() && demosaicQuantizationScale > 0f)
require(sabreResolveFinalGains.all { it.isFinite() && it > 0f })
// Offset of the red site; the AOT resolves in a virtual RGGB frame anchored there.
val (redX, redY) = when (cfaPattern) {
0 -> 0 to 0 // RGGB
1 -> 1 to 0 // GRBG
2 -> 0 to 1 // GBRG
3 -> 1 to 1 // BGGR
else -> throw IllegalArgumentException("MGC Sabre Resolve CFA pattern $cfaPattern")
}
val padding = GlesMgcSabreResolveShaders.PLANE_PADDING
val planeWidth = width + 2 * padding
val planeHeight = height + 2 * padding
val converted = createTexture(planeWidth, planeHeight, GLES30.GL_R16I, GLES30.GL_NEAREST)
val balanced = createTexture(planeWidth, planeHeight, GLES30.GL_R16I, GLES30.GL_NEAREST)
val green = createTexture(planeWidth, planeHeight, GLES30.GL_R16I, GLES30.GL_NEAREST)
val resolvedPlanes = IntArray(3) {
createTexture(width, height, GLES30.GL_R16UI, GLES30.GL_NEAREST)
}
GLES30.glUseProgram(sabreResolveConvertProgram)
bindTexture(sabreResolveConvertProgram, "uAccumulated", 0, accumulatedColor)
uniform2i(sabreResolveConvertProgram, "uImageSize", width, height)
uniform2i(sabreResolveConvertProgram, "uRedSite", redX, redY)
uniform1f(
sabreResolveConvertProgram,
"uQuantizationScale",
demosaicQuantizationScale,
)
draw(sabreResolveConvertProgram, planeWidth, planeHeight, intArrayOf(converted))
// Entry 0x34A67F8: k = fcvtzs(clamp(retention * 65536, 0, 65535)).
val retentionFixed16 = (greenImbalanceRetention * 65536f).coerceIn(0f, 65535f).toInt()
GLES30.glUseProgram(sabreResolveGreenBalanceProgram)
bindTexture(sabreResolveGreenBalanceProgram, "uConverted", 0, converted)
uniform2i(sabreResolveGreenBalanceProgram, "uPlaneSize", planeWidth, planeHeight)
uniform1i(sabreResolveGreenBalanceProgram, "uGreenImbalanceRetention", retentionFixed16)
draw(sabreResolveGreenBalanceProgram, planeWidth, planeHeight, intArrayOf(balanced))
GLES30.glUseProgram(sabreResolveGreenProgram)
bindTexture(sabreResolveGreenProgram, "uBalanced", 0, balanced)
uniform2i(sabreResolveGreenProgram, "uPlaneSize", planeWidth, planeHeight)
draw(sabreResolveGreenProgram, planeWidth, planeHeight, intArrayOf(green))
val program = sabreResolveOutputProgram
val white = demosaicWhiteLevel.toFloat()
val black = finalBlackLevel
val gains = sabreResolveFinalGains
GLES30.glUseProgram(program)
bindTexture(program, "uAccumulated", 0, accumulatedColor)
bindTexture(program, "uBalanced", 1, balanced)
bindTexture(program, "uGreen", 2, green)
uniform2i(program, "uImageSize", width, height)
uniform2i(program, "uPlaneSize", planeWidth, planeHeight)
uniform2i(program, "uRedSite", redX, redY)
uniform1f(program, "uBlendBias", demosaicBlendBias)
uniform1f(program, "uBlendScale", demosaicBlendScale)
// The AOT multiplies by the binary32 reciprocal rather than dividing per pixel.
uniform1f(program, "uInverseQuantizationScale", 1f / demosaicQuantizationScale)
uniform3f(program, "uFinalGains", gains[0], gains[1], gains[2])
uniform3f(program, "uFinalBlackLevel", black[0], black[1], black[2])
uniform3f(
program,
"uOutputRange",
white - black[0],
white - black[1],
white - black[2],
)
GLES30.glUniform1ui(uniformLocation(program, "uFloatFence"), 0)
draw(program, width, height, resolvedPlanes)
checkGlError("MGC Sabre Resolve")
return resolvedPlanes
}
private fun renderSabreOutputTransform(
resolvedRgbPlanes: IntArray,
lensShadingTexture: Int,
@@ -2865,72 +2930,6 @@ internal class GlesMgcRawSpatialStacker(
draw(program, width, height, intArrayOf(output))
}
private fun uploadSabreResolvedRgb16Planar(buffer: ByteBuffer, textures: IntArray) {
require(textures.size == 3)
val planeBytes = Math.multiplyExact(
Math.multiplyExact(width, height),
Short.SIZE_BYTES,
)
require(buffer.capacity() >= Math.multiplyExact(planeBytes, textures.size))
GLES30.glBindBuffer(GLES30.GL_PIXEL_UNPACK_BUFFER, 0)
GLES30.glPixelStorei(GLES30.GL_UNPACK_ALIGNMENT, 1)
textures.forEachIndexed { channel, texture ->
val plane = buffer.duplicate().order(ByteOrder.nativeOrder())
plane.position(channel * planeBytes)
plane.limit((channel + 1) * planeBytes)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texture)
GLES30.glTexSubImage2D(
GLES30.GL_TEXTURE_2D,
0,
0,
0,
width,
height,
GLES30.GL_RED_INTEGER,
GLES30.GL_UNSIGNED_SHORT,
plane,
)
}
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0)
checkGlError("upload MGC Sabre Resolve planar RGB16")
}
private fun logSabreResolvedRgbStats(buffer: ByteBuffer) {
val pixelCount = Math.multiplyExact(width, height)
val samplesPerPlane = 4096
val sampleStep = max(1, pixelCount / samplesPerPlane)
val values = buffer.duplicate().order(ByteOrder.nativeOrder()).asShortBuffer()
val minimum = IntArray(3) { 0xffff }
val maximum = IntArray(3)
val sum = LongArray(3)
val count = IntArray(3)
for (channel in 0 until 3) {
val planeOffset = channel * pixelCount
var pixel = 0
while (pixel < pixelCount) {
val value = values.get(planeOffset + pixel).toInt() and 0xffff
minimum[channel] = minOf(minimum[channel], value)
maximum[channel] = maxOf(maximum[channel], value)
sum[channel] += value.toLong()
count[channel]++
pixel += sampleStep
}
}
val mean = FloatArray(3) { channel ->
if (count[channel] > 0) {
sum[channel].toFloat() / count[channel].toFloat()
} else {
0f
}
}
PLog.i(
SABRE_TAG,
"MGC Sabre Resolve output samples min=${minimum.contentToString()} " +
"max=${maximum.contentToString()} mean=${mean.contentToString()} " +
"step=$sampleStep",
)
}
private fun initSabrePrograms() {
check(supportsComputePrograms) {
"MGC Sabre VGN color noise/IIR requires the GLES 3.1 compute baseline"
@@ -2995,6 +2994,22 @@ internal class GlesMgcRawSpatialStacker(
GlesMgcRawSabreShaders.dehomogenize,
"mgc_sabre_dehomogenize",
)
sabreResolveConvertProgram = linkProgram(
GlesMgcSabreResolveShaders.convertToInt,
"mgc_sabre_resolve_convert_to_int",
)
sabreResolveGreenBalanceProgram = linkProgram(
GlesMgcSabreResolveShaders.greenBalance,
"mgc_sabre_resolve_green_balance",
)
sabreResolveGreenProgram = linkProgram(
GlesMgcSabreResolveShaders.greenAtColor,
"mgc_sabre_resolve_green_at_color",
)
sabreResolveOutputProgram = linkProgram(
GlesMgcSabreResolveShaders.resolveOutput,
"mgc_sabre_resolve_output",
)
sabreOutputTransformProgram = linkProgram(
GlesMgcRawSabreShaders.outputTransformUint16,
"mgc_sabre_output_transform_rgba16ui",
@@ -0,0 +1,357 @@
package com.hinnka.mycamera.processor
/**
* Bit-exact GLES translation of MGC 9.6.080 V24 `gcam::ResolveSabreHalide`
* (libgcastartup.so entry 0x34A55D4, worker 0x349B1B8). The independent reference and the
* oracle that checks it against the original AOT live in tools/mgc_sabre_resolve.
*
* The AOT works in a virtual RGGB frame V(u, v) = input(clamp(u + bx), clamp(v + by)), where
* (bx, by) is the red site of the sensor CFA; output(x, y) is the result at V(x, y). Its Funcs are
* split into four passes so no pixel recomputes a neighbour's intermediate:
*
* 1. `converted_to_int`: remosaic the dehomogenized RGBA16F colour into an int16 CFA.
* 2. `g_gb`: Gr/Gb balance at G sites; R/B sites keep their converted value.
* 3. `g_b$1`: adaptive green at R/B sites; G sites keep their balanced value.
* 4. `demosaic` + `output`: chroma interpolation, coverage blend and RAW14 output mapping.
*
* Passes 1-3 write int16 planes padded by [PLANE_PADDING] on each side, covering the virtual
* coordinates their consumers touch (radius 4 for pass 1 down to radius 1 for pass 3). Every
* integer operation reproduces the NEON lane semantics of the AOT: 16-bit wraparound,
* SHADD/SRHADD rounding, SABD magnitudes, SQADD/SQSUB saturation and SSHL by a signed byte.
*/
internal object GlesMgcSabreResolveShaders {
const val PLANE_PADDING = 4
private val header = """
#version 310 es
precision highp float;
precision highp int;
precision highp sampler2D;
precision highp isampler2D;
""".trimIndent()
private val integerOps = """
const int PLANE_PADDING = $PLANE_PADDING;
int s16(int v) { return (v << 16) >> 16; }
int u16(int v) { return v & 0xffff; }
int clz16(int x) { return x == 0 ? 16 : 15 - findMSB(x); }
int clz32(uint x) { return x == 0u ? 32 : 31 - findMSB(x); }
// SSHL .8h: the shift is the signed low byte of the lane; negative shifts are arithmetic.
int sshl16(int x, int lane) {
int s = ((lane & 0xff) << 24) >> 24;
if (s >= 0) return s >= 16 ? 0 : s16(x << s);
if (s <= -16) return x < 0 ? -1 : 0;
return x >> (-s);
}
int shadd(int a, int b) { return (a + b) >> 1; }
int srhadd(int a, int b) { return (a + b + 1) >> 1; }
int sabd(int a, int b) { return abs(a - b); }
int urhadd(int a, int b) { return (a + b + 1) >> 1; }
int sqadd(int a, int b) { return clamp(a + b, -32768, 32767); }
int sqsub(int a, int b) { return clamp(a - b, -32768, 32767); }
// Inverse-gradient pair blend. Candidate A has error dA, candidate B has error dB.
struct Ig1 {
bool aIsNear;
int shift;
};
Ig1 makeIg1(int dA, int dB) {
int hA = dA >> 1;
int hB = dB >> 1;
int total = max(u16(hA + hB), 1);
return Ig1(hB > hA, clz16(total) - clz16(min(hA, hB)));
}
int blend1(Ig1 g, int xA, int xB) {
int nearValue = g.aIsNear ? xA : xB;
int farValue = g.aIsNear ? xB : xA;
return s16(nearValue + sshl16(s16(farValue - nearValue), g.shift));
}
// Inverse-squared-cost blend; X has cost cX and Y has cost cY.
int ig2(int cX, int cY, int x, int y) {
uint sX = (uint(cX) * uint(cX)) >> 1;
uint sY = (uint(cY) * uint(cY)) >> 1;
bool xIsNear = sY > sX;
int nearValue = xIsNear ? x : y;
int farValue = xIsNear ? y : x;
uint total = max(sX + sY, 1u);
int shift = clamp(s16(clz32(total) - clz32(min(sX, sY))), -15, 15);
return s16(nearValue + sshl16(s16(farValue - nearValue), shift));
}
// Bit below the leading one: the half-step fraction of log2(x).
int log2Fraction(int x) { return sshl16(x, clz16(u16(x)) - 14) & 1; }
// Log-domain ratio correction: xWeighted * g / gWeighted in half-step powers of two.
int ratioCorrect(int xWeighted, int g, int gWeighted) {
int xp = max(xWeighted, 0);
int m = max(xp, max(gWeighted, g));
int l = s16(
log2Fraction(xp) - 2 * clz16(u16(xp)) + 2 * clz16(u16(m)) - log2Fraction(m)
);
int t = sshl16(sqsub(g, gWeighted), max(s16(l >> 1), -15));
int odd = (l & 1) != 0 ? t >> 1 : 0;
return sqadd(xWeighted, s16(odd + t));
}
int planeAt(isampler2D plane, ivec2 planeSize, int u, int v) {
ivec2 p = clamp(
ivec2(u + PLANE_PADDING, v + PLANE_PADDING),
ivec2(0),
planeSize - ivec2(1)
);
return texelFetch(plane, p, 0).r;
}
""".trimIndent()
private val halfToFloat = """
// The AOT widens F16 with an exponent rebias only: bits = sign | magnitude << 13, plus
// 0x38000000. Zero becomes 2^-15 and subnormals keep the normal-number formula.
float aotHalfToFloat(float value) {
uint h = packHalf2x16(vec2(value, 0.0)) & 0xffffu;
uint bits = ((h & 0x8000u) << 16) | ((h & 0x7fffu) << 13);
return uintBitsToFloat(bits + 0x38000000u);
}
""".trimIndent()
/** Pass 1, `converted_to_int`: int16(clamp(roundEven(h2f * S), -32768, 32767)). */
val convertToInt = """
${header.prependIndent(" ").trimStart()}
uniform highp sampler2D uAccumulated;
uniform ivec2 uImageSize;
uniform ivec2 uRedSite;
uniform float uQuantizationScale;
layout(location = 0) out highp ivec4 oConverted;
${halfToFloat.prependIndent(" ").trimStart()}
void main() {
ivec2 virtualSite = ivec2(gl_FragCoord.xy) - ivec2($PLANE_PADDING);
ivec2 source = clamp(virtualSite + uRedSite, ivec2(0), uImageSize - ivec2(1));
vec4 color = texelFetch(uAccumulated, source, 0);
int cfaColor = (virtualSite.x & 1) + (virtualSite.y & 1);
float sampleValue = cfaColor == 0 ? color.r : (cfaColor == 1 ? color.g : color.b);
float scaled = aotHalfToFloat(sampleValue) * uQuantizationScale;
oConverted = ivec4(int(clamp(roundEven(scaled), -32768.0, 32767.0)), 0, 0, 0);
}
""".trimIndent()
/**
* Pass 2, `g_gb`: blur = (4G + diagonal G + 4) >> 3 and
* G' = blur + (((G - blur) * k + 0x8000) >> 16), where k = 0 equalizes Gr/Gb fully.
*/
val greenBalance = """
${header.prependIndent(" ").trimStart()}
uniform highp isampler2D uConverted;
uniform ivec2 uPlaneSize;
uniform int uGreenImbalanceRetention;
layout(location = 0) out highp ivec4 oBalanced;
${integerOps.prependIndent(" ").trimStart()}
int converted(int u, int v) { return planeAt(uConverted, uPlaneSize, u, v); }
void main() {
ivec2 virtualSite = ivec2(gl_FragCoord.xy) - ivec2(PLANE_PADDING);
int u = virtualSite.x;
int v = virtualSite.y;
int g = converted(u, v);
if (((u + v) & 1) == 0) {
oBalanced = ivec4(g, 0, 0, 0);
return;
}
int total = 4 * g + converted(u - 1, v - 1) + converted(u + 1, v - 1) +
converted(u - 1, v + 1) + converted(u + 1, v + 1);
int blur = s16((total + 4) >> 3);
int difference = s16(g - blur);
int correction = s16(
int(uint(difference * uGreenImbalanceRetention) + 0x8000u) >> 16
);
oBalanced = ivec4(s16(correction + blur), 0, 0, 0);
}
""".trimIndent()
/**
* Pass 3, `g_b$1`: green at R/B sites from directional (G + C) / 2 estimates, inverse-gradient
* candidate blends, log-domain ratio correction and inverse-squared direction costs.
*/
val greenAtColor = """
${header.prependIndent(" ").trimStart()}
uniform highp isampler2D uBalanced;
uniform ivec2 uPlaneSize;
layout(location = 0) out highp ivec4 oGreen;
${integerOps.prependIndent(" ").trimStart()}
// R/B sites hold the converted value, G sites the balanced green.
int at(int u, int v) { return planeAt(uBalanced, uPlaneSize, u, v); }
void main() {
ivec2 virtualSite = ivec2(gl_FragCoord.xy) - ivec2(PLANE_PADDING);
int u = virtualSite.x;
int v = virtualSite.y;
int c = at(u, v);
if (((u + v) & 1) != 0) {
oGreen = ivec4(c, 0, 0, 0);
return;
}
int cL = at(u - 2, v);
int cR = at(u + 2, v);
int cU = at(u, v - 2);
int cD = at(u, v + 2);
int gl = at(u - 1, v);
int gr = at(u + 1, v);
int gu = at(u, v - 1);
int gd = at(u, v + 1);
int ul = at(u - 1, v - 1);
int ur = at(u + 1, v - 1);
int dl = at(u - 1, v + 1);
int dr = at(u + 1, v + 1);
int eH = srhadd(shadd(gl, gr), c);
int eR = srhadd(shadd(c, cR), gr);
int eL = srhadd(shadd(cL, c), gl);
int eV = srhadd(shadd(gu, gd), c);
int eD = srhadd(shadd(c, cD), gd);
int eU = srhadd(shadd(cU, c), gu);
Ig1 horizontal = makeIg1(sabd(eH, eR), sabd(eH, eL));
int eHWeighted = blend1(horizontal, eR, eL);
int gH = blend1(horizontal, gr, gl);
Ig1 vertical = makeIg1(sabd(eV, eD), sabd(eV, eU));
int eVWeighted = blend1(vertical, eD, eU);
int gV = blend1(vertical, gd, gu);
int cH = shadd(srhadd(gl, gr), c);
int cV = shadd(srhadd(gu, gd), c);
int eHUpDown = srhadd(
shadd(srhadd(at(u - 1, v - 2), at(u + 1, v - 2)), cU),
shadd(srhadd(at(u - 1, v + 2), at(u + 1, v + 2)), cD)
);
int eVLeftRight = srhadd(
shadd(srhadd(at(u - 2, v - 1), at(u - 2, v + 1)), cL),
shadd(srhadd(at(u + 2, v - 1), at(u + 2, v + 1)), cR)
);
int pl = shadd(srhadd(ul, dl), gl);
int pr = shadd(srhadd(ur, dr), gr);
int pu = shadd(srhadd(ul, ur), gu);
int pd = shadd(srhadd(dl, dr), gd);
// (|.| >> 2) * 9 is a wrapping 16-bit MUL then USHR #3; the sums are USRA mod 2^16.
int costA = u16(
(u16((sabd(cV, eVLeftRight) >> 2) * 9) >> 3) +
(sabd(cV, srhadd(pl, pr)) >> 3) + (sabd(pl, pr) >> 1)
);
int costB = u16(
(u16((sabd(cH, eHUpDown) >> 2) * 9) >> 3) +
(sabd(cH, srhadd(pu, pd)) >> 3) + (sabd(pu, pd) >> 1)
);
int greenH = ratioCorrect(gH, eH, eHWeighted);
int greenV = ratioCorrect(gV, eV, eVWeighted);
oGreen = ivec4(ig2(costB, costA, greenV, greenH), 0, 0, 0);
}
""".trimIndent()
/**
* Pass 4, `demosaic` + `output`. Float arithmetic follows the AOT's FMUL/FADD order without
* fused multiply-add: every product that feeds an addition passes through [uFloatFence],
* a uniform zero the compiler cannot prove to be zero, so it is rounded to binary32 first.
*/
val resolveOutput = """
${header.prependIndent(" ").trimStart()}
uniform highp sampler2D uAccumulated;
uniform highp isampler2D uBalanced;
uniform highp isampler2D uGreen;
uniform ivec2 uImageSize;
uniform ivec2 uPlaneSize;
uniform ivec2 uRedSite;
uniform float uBlendBias;
uniform float uBlendScale;
uniform float uInverseQuantizationScale;
uniform vec3 uFinalGains;
uniform vec3 uFinalBlackLevel;
uniform vec3 uOutputRange;
uniform uint uFloatFence;
layout(location = 0) out highp uvec4 oRed;
layout(location = 1) out highp uvec4 oGreen;
layout(location = 2) out highp uvec4 oBlue;
${integerOps.prependIndent(" ").trimStart()}
${halfToFloat.prependIndent(" ").trimStart()}
float rounded(float value) { return uintBitsToFloat(floatBitsToUint(value) ^ uFloatFence); }
// Converted value; only read at R/B sites, where pass 2 kept it.
int converted(int u, int v) { return planeAt(uBalanced, uPlaneSize, u, v); }
int green(int u, int v) { return planeAt(uGreen, uPlaneSize, u, v); }
// Chroma of colour `wantBlue ? B : R` at a site whose native colour differs.
int chroma(int u, int v, bool wantBlue) {
int nativeOffset = wantBlue ? 1 : 0;
bool sameRow = ((v - nativeOffset) & 1) == 0;
bool sameColumn = ((u - nativeOffset) & 1) == 0;
int gc = green(u, v);
if (sameRow && !sameColumn) {
Ig1 g = makeIg1(sabd(gc, green(u + 1, v)), sabd(gc, green(u - 1, v)));
int xWeighted = blend1(g, converted(u + 1, v), converted(u - 1, v));
int gWeighted = blend1(g, green(u + 1, v), green(u - 1, v));
return ratioCorrect(xWeighted, gc, gWeighted);
}
if (!sameRow && sameColumn) {
Ig1 g = makeIg1(sabd(gc, green(u, v + 1)), sabd(gc, green(u, v - 1)));
int xWeighted = blend1(g, converted(u, v + 1), converted(u, v - 1));
int gWeighted = blend1(g, green(u, v + 1), green(u, v - 1));
return ratioCorrect(xWeighted, gc, gWeighted);
}
int center = converted(u, v);
int topLeft = converted(u - 1, v - 1);
int topRight = converted(u + 1, v - 1);
int bottomLeft = converted(u - 1, v + 1);
int bottomRight = converted(u + 1, v + 1);
Ig1 g1 = makeIg1(sabd(gc, green(u + 1, v - 1)), sabd(gc, green(u - 1, v + 1)));
int x1 = ratioCorrect(
blend1(g1, topRight, bottomLeft),
gc,
blend1(g1, green(u + 1, v - 1), green(u - 1, v + 1))
);
Ig1 g2 = makeIg1(sabd(gc, green(u + 1, v + 1)), sabd(gc, green(u - 1, v - 1)));
int x2 = ratioCorrect(
blend1(g2, bottomRight, topLeft),
gc,
blend1(g2, green(u + 1, v + 1), green(u - 1, v - 1))
);
int cost2 = urhadd(sabd(center, topLeft), sabd(center, bottomRight));
int cost1 = urhadd(sabd(center, bottomLeft), sabd(center, topRight));
return ig2(cost2, cost1, x2, x1);
}
uint resolveChannel(float accumulated, int demosaiced, float weight, int channel) {
float demosaicedValue = float(demosaiced) * uInverseQuantizationScale;
float mixed = rounded((1.0 - weight) * accumulated) + rounded(weight * demosaicedValue);
float scaled = rounded((mixed * uFinalGains[channel]) * uOutputRange[channel]);
float value = roundEven(uFinalBlackLevel[channel] + scaled);
return uint(clamp(value, 0.0, 65535.0));
}
void main() {
ivec2 site = ivec2(gl_FragCoord.xy);
int u = site.x;
int v = site.y;
int cfaColor = (u & 1) + (v & 1);
int demosaicedRed = cfaColor == 0 ? converted(u, v) : chroma(u, v, false);
int demosaicedGreen = green(u, v);
int demosaicedBlue = cfaColor == 2 ? converted(u, v) : chroma(u, v, true);
ivec2 source = clamp(site + uRedSite, ivec2(0), uImageSize - ivec2(1));
vec4 color = texelFetch(uAccumulated, source, 0);
float weight = uBlendBias + rounded(aotHalfToFloat(color.a) * uBlendScale);
weight = max(min(weight, 1.0), 0.0);
oRed = uvec4(resolveChannel(aotHalfToFloat(color.r), demosaicedRed, weight, 0));
oGreen = uvec4(resolveChannel(aotHalfToFloat(color.g), demosaicedGreen, weight, 1));
oBlue = uvec4(resolveChannel(aotHalfToFloat(color.b), demosaicedBlue, weight, 2));
}
""".trimIndent()
}
@@ -5,12 +5,16 @@ package com.hinnka.mycamera.processor
*
* Recovered from libgcastartup.so:
* - default SNR maps: 0x3882c20
* - output-white lookup and interpolation: 0x348c96c
* - Gr/Gb imbalance retention lookup and interpolation: 0x348c96c
* - demosaic sharpness TET ratio: 0x348a234
*/
internal object MgcSabreResolveTuning {
data class Parameters(
val outputWhiteLevel: Float,
/**
* Fraction of the Gr/Gb difference kept by ResolveSabre's `g_gb` stage: 0 replaces each
* green with its diagonal-green blur, 1 keeps it unchanged.
*/
val greenImbalanceRetention: Float,
val demosaicSharpness: Float,
)
@@ -19,7 +23,7 @@ internal object MgcSabreResolveTuning {
desiredExposureProduct: Double?,
actualExposureProduct: Double,
): Parameters = Parameters(
outputWhiteLevel = outputWhiteLevel(referenceSnr),
greenImbalanceRetention = greenImbalanceRetention(referenceSnr),
demosaicSharpness = demosaicSharpness(
desiredExposureProduct = desiredExposureProduct,
actualExposureProduct = actualExposureProduct,
@@ -27,25 +31,25 @@ internal object MgcSabreResolveTuning {
)
/** std::map<float, float>{{5, 0}, {20, 0.5}, {40, 1}} with endpoint clamping. */
internal fun outputWhiteLevel(referenceSnr: Float): Float {
val snr = referenceSnr.takeIf { it.isFinite() } ?: OUTPUT_WHITE_SNR_LOW
internal fun greenImbalanceRetention(referenceSnr: Float): Float {
val snr = referenceSnr.takeIf { it.isFinite() } ?: GREEN_IMBALANCE_SNR_LOW
return when {
snr <= OUTPUT_WHITE_SNR_LOW -> OUTPUT_WHITE_LOW
snr < OUTPUT_WHITE_SNR_MID -> interpolate(
snr <= GREEN_IMBALANCE_SNR_LOW -> GREEN_IMBALANCE_LOW
snr < GREEN_IMBALANCE_SNR_MID -> interpolate(
snr,
OUTPUT_WHITE_SNR_LOW,
OUTPUT_WHITE_LOW,
OUTPUT_WHITE_SNR_MID,
OUTPUT_WHITE_MID,
GREEN_IMBALANCE_SNR_LOW,
GREEN_IMBALANCE_LOW,
GREEN_IMBALANCE_SNR_MID,
GREEN_IMBALANCE_MID,
)
snr < OUTPUT_WHITE_SNR_HIGH -> interpolate(
snr < GREEN_IMBALANCE_SNR_HIGH -> interpolate(
snr,
OUTPUT_WHITE_SNR_MID,
OUTPUT_WHITE_MID,
OUTPUT_WHITE_SNR_HIGH,
OUTPUT_WHITE_HIGH,
GREEN_IMBALANCE_SNR_MID,
GREEN_IMBALANCE_MID,
GREEN_IMBALANCE_SNR_HIGH,
GREEN_IMBALANCE_HIGH,
)
else -> OUTPUT_WHITE_HIGH
else -> GREEN_IMBALANCE_HIGH
}
}
@@ -78,11 +82,11 @@ internal object MgcSabreResolveTuning {
return upperY * amount + lowerY * (1f - amount)
}
private const val OUTPUT_WHITE_SNR_LOW = 5f
private const val OUTPUT_WHITE_SNR_MID = 20f
private const val OUTPUT_WHITE_SNR_HIGH = 40f
private const val OUTPUT_WHITE_LOW = 0f
private const val OUTPUT_WHITE_MID = 0.5f
private const val OUTPUT_WHITE_HIGH = 1f
private const val GREEN_IMBALANCE_SNR_LOW = 5f
private const val GREEN_IMBALANCE_SNR_MID = 20f
private const val GREEN_IMBALANCE_SNR_HIGH = 40f
private const val GREEN_IMBALANCE_LOW = 0f
private const val GREEN_IMBALANCE_MID = 0.5f
private const val GREEN_IMBALANCE_HIGH = 1f
private const val DEFAULT_DEMOSAIC_SHARPNESS = 1f
}
@@ -1,58 +0,0 @@
package com.hinnka.mycamera.processor
import java.nio.ByteBuffer
/** JNI boundary for MGC 9.6.080 V24's statically lifted ResolveSabreHalide AOT kernel. */
internal object MgcSabreResolver {
init {
System.loadLibrary("my-native-lib")
}
fun resolve(
accumulatedColorRgba16f: ByteBuffer,
outputRgb16Planar: ByteBuffer,
width: Int,
height: Int,
cfaPattern: Int,
finalBlackLevel: FloatArray,
finalGains: FloatArray,
demosaicWhiteLevel: Int,
outputWhiteLevel: Float,
demosaicBlendScale: Float,
demosaicBlendBias: Float,
demosaicSharpnessScale: Float,
) {
val result = nativeResolve(
accumulatedColorRgba16f,
outputRgb16Planar,
width,
height,
cfaPattern,
finalBlackLevel,
finalGains,
demosaicWhiteLevel,
outputWhiteLevel,
demosaicBlendScale,
demosaicBlendBias,
demosaicSharpnessScale,
)
check(result == 0) { "MGC ResolveSabreHalide failed with status=$result" }
outputRgb16Planar.position(0)
outputRgb16Planar.limit(outputRgb16Planar.capacity())
}
private external fun nativeResolve(
accumulatedColorRgba16f: ByteBuffer,
outputRgb16Planar: ByteBuffer,
width: Int,
height: Int,
cfaPattern: Int,
finalBlackLevel: FloatArray,
finalGains: FloatArray,
demosaicWhiteLevel: Int,
outputWhiteLevel: Float,
demosaicBlendScale: Float,
demosaicBlendBias: Float,
demosaicSharpnessScale: Float,
): Int
}
@@ -5,14 +5,14 @@ import org.junit.Test
class MgcSabreResolveTuningTest {
@Test
fun outputWhiteUsesOriginalSnrCurveAndEndpointClamping() {
assertEquals(0f, MgcSabreResolveTuning.outputWhiteLevel(0f), 0f)
assertEquals(0f, MgcSabreResolveTuning.outputWhiteLevel(5f), 0f)
assertEquals(0.25f, MgcSabreResolveTuning.outputWhiteLevel(12.5f), 1e-7f)
assertEquals(0.5f, MgcSabreResolveTuning.outputWhiteLevel(20f), 0f)
assertEquals(0.75f, MgcSabreResolveTuning.outputWhiteLevel(30f), 1e-7f)
assertEquals(1f, MgcSabreResolveTuning.outputWhiteLevel(40f), 0f)
assertEquals(1f, MgcSabreResolveTuning.outputWhiteLevel(80f), 0f)
fun greenImbalanceRetentionUsesOriginalSnrCurveAndEndpointClamping() {
assertEquals(0f, MgcSabreResolveTuning.greenImbalanceRetention(0f), 0f)
assertEquals(0f, MgcSabreResolveTuning.greenImbalanceRetention(5f), 0f)
assertEquals(0.25f, MgcSabreResolveTuning.greenImbalanceRetention(12.5f), 1e-7f)
assertEquals(0.5f, MgcSabreResolveTuning.greenImbalanceRetention(20f), 0f)
assertEquals(0.75f, MgcSabreResolveTuning.greenImbalanceRetention(30f), 1e-7f)
assertEquals(1f, MgcSabreResolveTuning.greenImbalanceRetention(40f), 0f)
assertEquals(1f, MgcSabreResolveTuning.greenImbalanceRetention(80f), 0f)
}
@Test