fix: gles compatibility

This commit is contained in:
zhoubinjia committed 2026-07-28 19:24:46 +08:00
1 parent 4488ce8ed2
commit b424575b77
18 files changed
+572 -167

No files matched your search

+2 -2
View File
@@ -26,8 +26,8 @@ android {
applicationId = "com.hinnka.mycamera"
minSdk = 30
targetSdk = 36
versionCode = 136
versionName = "1.25.2"
versionCode = 137
versionName = "1.25.2.1test"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
@@ -10,9 +10,11 @@ import android.opengl.EGLDisplay
import android.opengl.EGLSurface
import android.opengl.GLES30
import android.opengl.GLES31
import com.hinnka.mycamera.model.ColorRecipeParams
import androidx.core.graphics.createBitmap
import com.hinnka.mycamera.model.ColorPaletteMapper
import com.hinnka.mycamera.model.ColorRecipeParams
import com.hinnka.mycamera.lut.ChromaDenoiseShaders
import com.hinnka.mycamera.processor.GlesComputeWorkGroup
import com.hinnka.mycamera.raw.DenoiseProfileNlmConfig
import com.hinnka.mycamera.raw.DenoiseProfileShaders
import com.hinnka.mycamera.raw.HncsFilmCurveMode
@@ -24,8 +26,8 @@ import com.hinnka.mycamera.raw.RawShaders
import com.hinnka.mycamera.raw.RawSrgbPassShaders
import com.hinnka.mycamera.raw.RawToneMappingGl
import com.hinnka.mycamera.raw.RawToneMappingParameters
import com.hinnka.mycamera.utils.PLog
import com.hinnka.mycamera.utils.LargeDirectBuffer
import com.hinnka.mycamera.utils.PLog
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
@@ -36,7 +38,6 @@ import java.nio.ByteOrder
import java.nio.FloatBuffer
import java.nio.ShortBuffer
import java.util.concurrent.Executors
import androidx.core.graphics.createBitmap
import kotlin.math.ln
import kotlin.math.max
import kotlin.math.pow
@@ -315,6 +316,9 @@ class LutImageProcessor(context: Context? = null) {
PLog.e(TAG, "Unable to make EGL current")
return false
}
if (!logAndValidateComputeCapabilities()) {
return false
}
// 初始化 shader 和缓冲区
initShaderProgram()
@@ -2585,7 +2589,11 @@ class LutImageProcessor(context: Context? = null) {
}
private fun dispatchBitmapDenoiseImage(width: Int, height: Int, tag: String) {
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(
GlesComputeWorkGroup.imageGroupCount(width),
GlesComputeWorkGroup.imageGroupCount(height),
1,
)
GLES31.glMemoryBarrier(
GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT or
GLES31.GL_TEXTURE_FETCH_BARRIER_BIT or
@@ -3163,6 +3171,7 @@ class LutImageProcessor(context: Context? = null) {
}
private fun compileComputeProgram(source: String, name: String): Int {
GlesComputeWorkGroup.requireBaselineCompatible(source, name)
val shader = GLES31.glCreateShader(GLES31.GL_COMPUTE_SHADER)
GLES31.glShaderSource(shader, source)
GLES31.glCompileShader(shader)
@@ -3188,6 +3197,57 @@ class LutImageProcessor(context: Context? = null) {
return program
}
private fun logAndValidateComputeCapabilities(): Boolean {
val vendor = GLES30.glGetString(GLES30.GL_VENDOR).orEmpty()
val renderer = GLES30.glGetString(GLES30.GL_RENDERER).orEmpty()
val version = GLES30.glGetString(GLES30.GL_VERSION).orEmpty()
val shadingLanguageVersion =
GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION).orEmpty()
PLog.i(
TAG,
"GL compute device: vendor=$vendor renderer=$renderer version=$version " +
"glsl=$shadingLanguageVersion",
)
if (!version.contains("OpenGL ES 3.1") && !version.contains("OpenGL ES 3.2")) {
PLog.e(TAG, "Bitmap denoise requires OpenGL ES 3.1+, got: $version")
return false
}
val value = IntArray(1)
GLES30.glGetIntegerv(GLES31.GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, value, 0)
val maxInvocations = value[0]
GLES30.glGetIntegerv(GLES31.GL_MAX_COMPUTE_SHARED_MEMORY_SIZE, value, 0)
val maxSharedMemory = value[0]
val maxSize = IntArray(3)
for (axis in maxSize.indices) {
GLES30.glGetIntegeri_v(
GLES31.GL_MAX_COMPUTE_WORK_GROUP_SIZE,
axis,
value,
0,
)
maxSize[axis] = value[0]
}
PLog.i(
TAG,
"GL compute limits: workGroupInvocations=$maxInvocations " +
"workGroupSize=${maxSize.contentToString()} sharedMemory=$maxSharedMemory",
)
val supported =
maxInvocations >= GlesComputeWorkGroup.BASELINE_MAX_INVOCATIONS &&
maxSize[0] >= GlesComputeWorkGroup.LINEAR_SIZE &&
maxSize[1] >= GlesComputeWorkGroup.IMAGE_TILE_SIZE &&
maxSize[2] >= 1
if (!supported) {
PLog.e(
TAG,
"Bitmap denoise requires the OpenGL ES 3.1 compute baseline; " +
"got invocations=$maxInvocations size=${maxSize.contentToString()}",
)
}
return supported
}
private fun initBuffers() {
// 顶点缓冲
vertexBuffer = ByteBuffer.allocateDirect(Shaders.FULL_QUAD_VERTICES.size * 4)
@@ -0,0 +1,77 @@
package com.hinnka.mycamera.processor
/**
* Compute work-group contract shared by every GLES 3.1 image-processing pipeline.
*
* OpenGL ES 3.1 only guarantees 128 invocations per work group. Image passes use an 8x8 tile so
* they stay below that baseline while retaining a square neighbourhood. One-dimensional passes
* and cooperative reductions use the complete 128-lane baseline.
*/
internal object GlesComputeWorkGroup {
const val BASELINE_MAX_INVOCATIONS = 128
const val BASELINE_MAX_SIZE_X = 128
const val BASELINE_MAX_SIZE_Y = 128
const val BASELINE_MAX_SIZE_Z = 64
const val IMAGE_TILE_SIZE = 8
const val LINEAR_SIZE = 128
private val layoutPattern =
Regex("""layout\s*\(([^)]*local_size_[xyz][^)]*)\)\s*in\s*;""")
private val axisPattern = Regex("""local_size_([xyz])\s*=\s*(\d+)""")
data class Size(
val x: Int,
val y: Int,
val z: Int,
) {
val invocations: Int
get() = x * y * z
}
fun imageGroupCount(value: Int): Int =
groupCount(value, IMAGE_TILE_SIZE)
fun linearGroupCount(value: Int): Int =
groupCount(value, LINEAR_SIZE)
fun groupCount(value: Int, localSize: Int): Int {
require(value >= 0)
require(localSize > 0)
return (value + localSize - 1) / localSize
}
fun declaredSize(source: String): Size? {
val layout = layoutPattern.find(source)?.groupValues?.get(1) ?: return null
val sizes = axisPattern.findAll(layout).associate { match ->
match.groupValues[1] to match.groupValues[2].toInt()
}
return Size(
x = sizes["x"] ?: 1,
y = sizes["y"] ?: 1,
z = sizes["z"] ?: 1,
)
}
/**
* Fails before entering a device driver if a new shader exceeds the GLES 3.1 baseline.
*/
fun requireBaselineCompatible(source: String, name: String) {
val size = requireNotNull(declaredSize(source)) {
"Compute shader $name does not declare a local work-group size"
}
require(size.x in 1..BASELINE_MAX_SIZE_X) {
"Compute shader $name local_size_x=${size.x} exceeds GLES 3.1 baseline"
}
require(size.y in 1..BASELINE_MAX_SIZE_Y) {
"Compute shader $name local_size_y=${size.y} exceeds GLES 3.1 baseline"
}
require(size.z in 1..BASELINE_MAX_SIZE_Z) {
"Compute shader $name local_size_z=${size.z} exceeds GLES 3.1 baseline"
}
require(size.invocations <= BASELINE_MAX_INVOCATIONS) {
"Compute shader $name has ${size.invocations} local invocations; " +
"GLES 3.1 only guarantees $BASELINE_MAX_INVOCATIONS"
}
}
}
@@ -138,7 +138,7 @@ internal object GlesRadianceReconstructionShaders {
precision highp int;
precision highp image2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(binding = 0) uniform highp usampler2D uRawRegion;
layout(binding = 1) uniform highp sampler2D uLensShadingMap;
layout(rgba16f, binding = 0) writeonly uniform image2D uPackedRaw;
@@ -210,7 +210,7 @@ internal object GlesRadianceReconstructionShaders {
precision highp uimage2D;
precision highp image2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D uYccd;
layout(rgba16f, binding = 1) writeonly uniform image2D uCameraRgb;
uniform ivec2 uOutputSize;
@@ -248,7 +248,7 @@ internal object GlesRadianceReconstructionShaders {
precision highp int;
precision highp image2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(binding = 0) uniform highp usampler2D uRawRegion;
layout(binding = 1) uniform highp sampler2D uLensShadingMap;
layout(rgba16f, binding = 0) writeonly uniform image2D uSemanticSeed;
@@ -325,7 +325,7 @@ internal object GlesRadianceReconstructionShaders {
precision highp int;
precision highp image2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16f, binding = 0) readonly uniform image2D uSemanticSeed;
layout(rgba16f, binding = 1) writeonly uniform image2D uSemanticOutput;
uniform ivec2 uRegionSize;
@@ -647,7 +647,8 @@ internal class GlesRadianceVgnChromaPostprocessor(
backend.checkGlError("Radiance VGN chroma readback framebuffer")
}
private fun groupCount(value: Int): Int = (value + 15) / 16
private fun groupCount(value: Int): Int =
GlesComputeWorkGroup.imageGroupCount(value)
}
internal data class RadianceVgnChromaIirCoefficients(
@@ -755,7 +756,7 @@ internal object GlesRadianceVgnChromaShaders {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uCameraRgb;
layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uYccd;
@@ -844,7 +845,7 @@ internal object GlesRadianceVgnChromaShaders {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uInput;
layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uOutput;
@@ -879,7 +880,7 @@ internal object GlesRadianceVgnChromaShaders {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uInput;
layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uOutput;
@@ -938,7 +939,7 @@ internal object GlesRadianceVgnChromaShaders {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uYccd;
layout(rgba16ui, binding = 1) readonly uniform highp uimage2D uSmooth;
@@ -1042,7 +1043,7 @@ internal object GlesRadianceVgnChromaShaders {
#version 310 es
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uSmooth;
layout(rgba16ui, binding = 1) readonly uniform highp uimage2D uDirectionalSource;
@@ -1243,7 +1244,7 @@ internal object GlesRadianceVgnChromaShaders {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uOriginal;
layout(rgba16ui, binding = 1) readonly uniform highp uimage2D uSmooth;
@@ -1306,7 +1307,7 @@ internal object GlesRadianceVgnChromaShaders {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uOriginal;
layout(rgba16ui, binding = 1) readonly uniform highp uimage2D uSmooth;
@@ -1345,7 +1346,7 @@ internal object GlesRadianceVgnChromaShaders {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uInput;
layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uOutput;
@@ -903,7 +903,7 @@ internal object GlesRawRadianceFusionShaders {
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16f, binding = 0) writeonly uniform highp image2D uNrAccumulator;
layout(rgba16f, binding = 1) writeonly uniform highp image2D uDetailAccumulator;
$rejectionDeclaration
@@ -1626,7 +1626,7 @@ internal object GlesRawRadianceFusionShaders {
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform highp sampler2D uNrAccumulator;
layout(rgba16f, binding = 0) writeonly uniform highp image2D uReferenceBase;
uniform ivec2 uImageSize;
@@ -987,6 +987,34 @@ internal class GlesRawRadianceStacker(
if (!version.contains("OpenGL ES 3.1") && !version.contains("OpenGL ES 3.2")) {
throw IllegalStateException("GLES RAW stack requires OpenGL ES 3.1+, got: $version")
}
val value = IntArray(1)
GLES30.glGetIntegerv(GLES31.GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, value, 0)
val maxInvocations = value[0]
val maxSize = IntArray(3)
for (axis in maxSize.indices) {
GLES30.glGetIntegeri_v(
GLES31.GL_MAX_COMPUTE_WORK_GROUP_SIZE,
axis,
value,
0,
)
maxSize[axis] = value[0]
}
PLog.i(
TAG,
"GL compute device: vendor=${GLES30.glGetString(GLES30.GL_VENDOR).orEmpty()} " +
"renderer=${GLES30.glGetString(GLES30.GL_RENDERER).orEmpty()} version=$version " +
"workGroupInvocations=$maxInvocations workGroupSize=${maxSize.contentToString()}",
)
check(
maxInvocations >= GlesComputeWorkGroup.BASELINE_MAX_INVOCATIONS &&
maxSize[0] >= GlesComputeWorkGroup.LINEAR_SIZE &&
maxSize[1] >= GlesComputeWorkGroup.IMAGE_TILE_SIZE &&
maxSize[2] >= 1
) {
"GLES RAW stack requires the OpenGL ES 3.1 compute baseline; " +
"got invocations=$maxInvocations size=${maxSize.contentToString()}"
}
}
private fun initPrograms() {
@@ -7883,6 +7911,7 @@ internal class GlesRawRadianceStacker(
}
private fun linkComputeProgram(source: String, name: String): Int {
GlesComputeWorkGroup.requireBaselineCompatible(source, name)
val shader = compileShader(GLES31.GL_COMPUTE_SHADER, source, "$name compute")
val program = GLES31.glCreateProgram()
GLES31.glAttachShader(program, shader)
@@ -7940,10 +7969,11 @@ internal class GlesRawRadianceStacker(
GLES30.glDisable(GLES30.GL_CULL_FACE)
}
private fun groupCount(value: Int): Int = (value + LOCAL_SIZE - 1) / LOCAL_SIZE
private fun groupCount(value: Int): Int =
GlesComputeWorkGroup.imageGroupCount(value)
private fun groupCount(value: Int, groupSize: Int): Int =
(value + groupSize - 1) / groupSize
GlesComputeWorkGroup.groupCount(value, groupSize)
private fun transposeMatrix3x3(matrix: FloatArray): FloatArray {
return floatArrayOf(
@@ -7998,7 +8028,7 @@ internal class GlesRawRadianceStacker(
private const val TAG = "GlesRawRadianceStacker"
private const val EGL_OPENGL_ES3_BIT_KHR = 0x00000040
private const val LOCAL_SIZE = 16
private const val LOCAL_SIZE = GlesComputeWorkGroup.IMAGE_TILE_SIZE
private const val RAW_BYTES_PER_PIXEL = 2
private const val PREWARM_TRACKING_SCALE = 4
private const val PREWARM_TRACKING_WIDTH = 64
@@ -8182,7 +8212,7 @@ internal class GlesRawRadianceStacker(
private val RAW_PROXY_COMPUTE_SHADER = """
#version 310 es
$RAW_COMMON
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform highp usampler2D uRaw;
layout(rgba16f, binding = 1) writeonly uniform highp image2D uProxy;
uniform ivec2 uProxySize;
@@ -8477,7 +8507,7 @@ internal class GlesRawRadianceStacker(
private val RAW_TRACKING_PROXY_COMPUTE_SHADER = """
#version 310 es
$RAW_COMMON
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform highp usampler2D uRaw;
layout(rgba16f, binding = 1) writeonly uniform highp image2D uTrackingProxy;
uniform ivec2 uPlaneSize;
@@ -8723,7 +8753,7 @@ internal class GlesRawRadianceStacker(
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform sampler2D uFlow;
uniform ivec2 uGridSize;
uniform int uSampleOffset;
@@ -8939,7 +8969,7 @@ internal class GlesRawRadianceStacker(
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform sampler2D uReference;
uniform sampler2D uCurrent;
uniform sampler2D uInputFlow;
@@ -9098,7 +9128,7 @@ internal class GlesRawRadianceStacker(
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform sampler2D uProxy;
layout(rgba16f, binding = 1) writeonly uniform highp image2D uKernel;
uniform ivec2 uProxySize;
@@ -9632,7 +9662,7 @@ internal class GlesRawRadianceStacker(
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform sampler2D uFlowGrid;
uniform sampler2D uRobustness;
uniform sampler2D uTileMask;
@@ -9738,7 +9768,7 @@ internal class GlesRawRadianceStacker(
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform sampler2D uFlowGrid;
uniform sampler2D uRobustness;
uniform sampler2D uTileMask;
@@ -13,7 +13,7 @@ internal object GlesRawRcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 1) readonly buffer RcdRgb0_Buf { float rcdRgb0[]; };
layout(std430, binding = 2) readonly buffer RcdRgb1_Buf { float rcdRgb1[]; };
layout(std430, binding = 3) readonly buffer RcdRgb2_Buf { float rcdRgb2[]; };
@@ -1,5 +1,7 @@
package com.hinnka.mycamera.raw
import com.hinnka.mycamera.processor.GlesComputeWorkGroup
/**
* OpenGL ES 3.1 compute port of darktable's denoiseprofile NLM path.
*
@@ -15,8 +17,8 @@ object DenoiseProfileShaders {
// darktable's neutral bias setting makes finish_v2 the exact inverse when NLM leaves a
// transformed sample unchanged. This is required to keep sensor black and channel ratios.
const val BLACK_PRESERVING_BIAS = 0.0f
const val IMAGE_LOCAL_X = 16
const val IMAGE_LOCAL_Y = 16
const val IMAGE_LOCAL_X = GlesComputeWorkGroup.IMAGE_TILE_SIZE
const val IMAGE_LOCAL_Y = GlesComputeWorkGroup.IMAGE_TILE_SIZE
private const val FUSED_TILE_X = IMAGE_LOCAL_X + SEARCH_RADIUS + 2 * PATCH_RADIUS
private const val FUSED_TILE_Y = IMAGE_LOCAL_Y + SEARCH_RADIUS + 2 * PATCH_RADIUS
@@ -5,7 +5,7 @@ internal object DngHdrProfileGainTableGpuShaders {
val CELL_STATS = """
#version 310 es
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(local_size_x = 16, local_size_y = 8, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -50,6 +50,8 @@ internal object DngHdrProfileGainTableGpuShaders {
float warpParameters[];
};
const uint CELL_SAMPLE_COUNT = 256u;
const uint CELL_LANE_COUNT = 128u;
shared float inputSamples[256];
${DcpHueSatMapGl.SHADER_FUNCTIONS}
@@ -236,20 +238,26 @@ internal object DngHdrProfileGainTableGpuShaders {
return clamp(sourcePixel, vec2(0.0), vec2(uImageSize - ivec2(1)));
}
void sortSamples(uint localIndex) {
void sortSamples(uint laneIndex) {
for (uint sequenceSize = 2u; sequenceSize <= 256u; sequenceSize <<= 1u) {
for (uint compareDistance = sequenceSize >> 1u;
compareDistance > 0u;
compareDistance >>= 1u
) {
uint partner = localIndex ^ compareDistance;
if (partner > localIndex) {
float first = inputSamples[localIndex];
float second = inputSamples[partner];
bool ascending = (localIndex & sequenceSize) == 0u;
if ((first > second) == ascending) {
inputSamples[localIndex] = second;
inputSamples[partner] = first;
for (
uint localIndex = laneIndex;
localIndex < CELL_SAMPLE_COUNT;
localIndex += CELL_LANE_COUNT
) {
uint partner = localIndex ^ compareDistance;
if (partner > localIndex) {
float first = inputSamples[localIndex];
float second = inputSamples[partner];
bool ascending = (localIndex & sequenceSize) == 0u;
if ((first > second) == ascending) {
inputSamples[localIndex] = second;
inputSamples[partner] = first;
}
}
}
barrier();
@@ -277,23 +285,29 @@ internal object DngHdrProfileGainTableGpuShaders {
endY = min(endY & ~1, uStatsBounds.w);
bool validCell = endX - startX >= 2 && endY - startY >= 2;
float inputValue = 0.0;
if (validCell) {
int localX = int(localIndex & 15u);
int localY = int(localIndex >> 4u);
int cellWidth = max(endX - startX, 2);
int cellHeight = max(endY - startY, 2);
int x = startX + ((localX * 2 + 1) * cellWidth) / 32;
int y = startY + ((localY * 2 + 1) * cellHeight) / 32;
vec2 sourcePixel = warpDestinationToSource(vec2(x, y));
ivec2 sourceCoord = ivec2(round(sourcePixel)) & ~ivec2(1);
sourceCoord = clamp(sourceCoord, ivec2(0), uImageSize - ivec2(2));
inputValue = sampleSceneInput(sourceCoord);
for (
uint sampleIndex = localIndex;
sampleIndex < CELL_SAMPLE_COUNT;
sampleIndex += CELL_LANE_COUNT
) {
float inputValue = 0.0;
if (validCell) {
int localX = int(sampleIndex & 15u);
int localY = int(sampleIndex >> 4u);
int cellWidth = max(endX - startX, 2);
int cellHeight = max(endY - startY, 2);
int x = startX + ((localX * 2 + 1) * cellWidth) / 32;
int y = startY + ((localY * 2 + 1) * cellHeight) / 32;
vec2 sourcePixel = warpDestinationToSource(vec2(x, y));
ivec2 sourceCoord = ivec2(round(sourcePixel)) & ~ivec2(1);
sourceCoord = clamp(sourceCoord, ivec2(0), uImageSize - ivec2(2));
inputValue = sampleSceneInput(sourceCoord);
}
if (uWriteCellSamples != 0) {
cellSamples[cellIndex * 256 + int(sampleIndex)] = inputValue;
}
inputSamples[sampleIndex] = inputValue;
}
if (uWriteCellSamples != 0) {
cellSamples[cellIndex * 256 + int(localIndex)] = inputValue;
}
inputSamples[localIndex] = inputValue;
barrier();
sortSamples(localIndex);
@@ -58,7 +58,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val prepareSource = """
#version 310 es
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -92,7 +92,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val normalizeLog = """
#version 310 es
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -130,7 +130,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val clearUint = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp int;
layout(std430, binding = 0) writeonly buffer Destination {
@@ -147,7 +147,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val downsample = """
#version 310 es
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -195,7 +195,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val remap = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -240,7 +240,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val accumulateLaplacian = """
#version 310 es
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -306,7 +306,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val copyRange = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -331,7 +331,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val reconstruct = """
#version 310 es
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -387,7 +387,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val logHistogram = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -443,7 +443,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val finalizeSdrTarget = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -547,7 +547,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val bguBlurZ = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -588,7 +588,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val bguBlurY = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -632,7 +632,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val bguBlurX = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -676,7 +676,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val bguSolve = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -707,7 +707,7 @@ internal object DngPhotonLocalToneMapGpuShaders {
private val gainCurves = """
#version 310 es
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in;
precision highp float;
precision highp int;
@@ -62,7 +62,7 @@ object QuadBayerShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout (binding = 0) uniform highp usampler2D uRawTexture;
layout (binding = 1) uniform highp sampler2D uLensShadingMap;
@@ -220,7 +220,7 @@ object QuadBayerShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 2) buffer RGB1_Buf { float rgb1[]; };
@@ -309,7 +309,7 @@ object QuadBayerShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; };
@@ -378,7 +378,7 @@ object QuadBayerShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; };
layout(std430, binding = 2) buffer RGB1_Buf { float rgb1[]; };
@@ -435,7 +435,7 @@ object QuadBayerShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 2) buffer RGB1_Buf { float rgb1[]; };
layout(std430, binding = 4) buffer TMP_R_Buf { float tmpR[]; };
@@ -24,6 +24,7 @@ import com.hinnka.mycamera.lut.ShadowsHighlightsShader
import com.hinnka.mycamera.ml.SharedDepthEstimator
import com.hinnka.mycamera.processor.GlesGpuCompletion
import com.hinnka.mycamera.processor.GlesGpuScheduler
import com.hinnka.mycamera.processor.GlesComputeWorkGroup
import com.hinnka.mycamera.processor.GlesRawRadianceStacker
import com.hinnka.mycamera.processor.GpuLinearRgbSource
import com.hinnka.mycamera.processor.GpuStackCompletionTimeline
@@ -3884,7 +3885,7 @@ class RawDemosaicProcessor {
uniformLocation(activeProgram, "uValue"),
value,
)
dispatch1d(count, 256)
dispatch1d(count, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
}
@@ -3910,7 +3911,7 @@ class RawDemosaicProcessor {
bindStorage(2, buffers[sourceRangeBuffer])
uniform2i(activeProgram, "uGridSize", gridWidth, gridHeight)
uniform2i(activeProgram, "uSampleSize", sampleWidth, sampleHeight)
dispatch2d(sampleWidth, sampleHeight, 16, 16)
dispatch2d(sampleWidth, sampleHeight, GlesComputeWorkGroup.IMAGE_TILE_SIZE, GlesComputeWorkGroup.IMAGE_TILE_SIZE)
storageBarrier()
activeProgram = program(DngPhotonLocalToneMapGpuShaders.Pass.NORMALIZE_LOG)
@@ -3921,7 +3922,7 @@ class RawDemosaicProcessor {
uniform2i(activeProgram, "uSampleSize", sampleWidth, sampleHeight)
uniform1i(activeProgram, "uOutputOffset", pyramid.levels.first().offset)
uniform1f(activeProgram, "uExposureGain", preToneMapExposureGain)
dispatch2d(sampleWidth, sampleHeight, 16, 16)
dispatch2d(sampleWidth, sampleHeight, GlesComputeWorkGroup.IMAGE_TILE_SIZE, GlesComputeWorkGroup.IMAGE_TILE_SIZE)
storageBarrier()
val sourceRange = readUintStorageBuffer(
@@ -3978,7 +3979,7 @@ class RawDemosaicProcessor {
uniform1i(activeProgram, "uSourceOffset", pyramid.levels.first().offset)
uniform1i(activeProgram, "uBinCount", histogramBinCount)
uniform1f(activeProgram, "uExposureGain", preToneMapExposureGain)
dispatch1d(sampleCount, 256)
dispatch1d(sampleCount, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
val sourceHistogram = readUintStorageBuffer(
@@ -4032,8 +4033,8 @@ class RawDemosaicProcessor {
dispatch2d(
destinationLevel.width,
destinationLevel.height,
16,
16,
GlesComputeWorkGroup.IMAGE_TILE_SIZE,
GlesComputeWorkGroup.IMAGE_TILE_SIZE,
)
storageBarrier()
}
@@ -4071,7 +4072,7 @@ class RawDemosaicProcessor {
)
uniform1f(activeProgram, "uEdgeSlope", edgeSlope)
uniform1f(activeProgram, "uExposureGain", preToneMapExposureGain)
dispatch1d(sampleCount, 256)
dispatch1d(sampleCount, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
activeProgram = program(DngPhotonLocalToneMapGpuShaders.Pass.DOWNSAMPLE)
@@ -4102,8 +4103,8 @@ class RawDemosaicProcessor {
dispatch2d(
destinationLevel.width,
destinationLevel.height,
16,
16,
GlesComputeWorkGroup.IMAGE_TILE_SIZE,
GlesComputeWorkGroup.IMAGE_TILE_SIZE,
)
storageBarrier()
}
@@ -4136,7 +4137,7 @@ class RawDemosaicProcessor {
uniform1i(activeProgram, "uOutputOffset", currentLevel.offset)
uniform1f(activeProgram, "uReference", reference)
uniform1f(activeProgram, "uIntensityStep", intensityStep)
dispatch2d(currentLevel.width, currentLevel.height, 16, 16)
dispatch2d(currentLevel.width, currentLevel.height, GlesComputeWorkGroup.IMAGE_TILE_SIZE, GlesComputeWorkGroup.IMAGE_TILE_SIZE)
}
storageBarrier()
}
@@ -4149,7 +4150,7 @@ class RawDemosaicProcessor {
uniform1i(activeProgram, "uCount", coarsestLevel.size)
uniform1i(activeProgram, "uSourceOffset", coarsestLevel.offset)
uniform1i(activeProgram, "uDestinationOffset", coarsestLevel.offset)
dispatch1d(coarsestLevel.size, 256)
dispatch1d(coarsestLevel.size, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
activeProgram = program(DngPhotonLocalToneMapGpuShaders.Pass.RECONSTRUCT)
@@ -4174,7 +4175,7 @@ class RawDemosaicProcessor {
uniform1i(activeProgram, "uLaplacianOffset", currentLevel.offset)
uniform1i(activeProgram, "uCurrentOffset", currentLevel.offset)
uniform1i(activeProgram, "uNextOffset", nextLevel.offset)
dispatch2d(currentLevel.width, currentLevel.height, 16, 16)
dispatch2d(currentLevel.width, currentLevel.height, GlesComputeWorkGroup.IMAGE_TILE_SIZE, GlesComputeWorkGroup.IMAGE_TILE_SIZE)
storageBarrier()
}
@@ -4204,7 +4205,7 @@ class RawDemosaicProcessor {
"uPreToneMapExposureGain",
preToneMapExposureGain,
)
dispatch1d(sampleCount, 256)
dispatch1d(sampleCount, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
allocate(buffers[bguBufferA], extendedBguFloatCount * Float.SIZE_BYTES)
@@ -4248,7 +4249,7 @@ class RawDemosaicProcessor {
)
uniform1i(activeProgram, "uRangePlaneCount", rangePlaneCount)
uniform1i(activeProgram, "uElementCount", extendedBguFloatCount)
dispatch1d(extendedBguFloatCount, 256)
dispatch1d(extendedBguFloatCount, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
activeProgram = program(DngPhotonLocalToneMapGpuShaders.Pass.BGU_BLUR_Y)
@@ -4264,7 +4265,7 @@ class RawDemosaicProcessor {
uniform1i(activeProgram, "uOutputHeight", gridHeight)
uniform1i(activeProgram, "uRangePlaneCount", rangePlaneCount)
uniform1i(activeProgram, "uElementCount", yBlurFloatCount)
dispatch1d(yBlurFloatCount, 256)
dispatch1d(yBlurFloatCount, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
activeProgram = program(DngPhotonLocalToneMapGpuShaders.Pass.BGU_BLUR_X)
@@ -4275,7 +4276,7 @@ class RawDemosaicProcessor {
uniform1i(activeProgram, "uInputWidth", extendedGridWidth)
uniform1i(activeProgram, "uRangePlaneCount", rangePlaneCount)
uniform1i(activeProgram, "uElementCount", fittedBguFloatCount)
dispatch1d(fittedBguFloatCount, 256)
dispatch1d(fittedBguFloatCount, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
activeProgram = program(DngPhotonLocalToneMapGpuShaders.Pass.BGU_SOLVE)
@@ -4293,7 +4294,7 @@ class RawDemosaicProcessor {
"uIdentitySlope",
1f,
)
dispatch1d(coefficientCount, 256)
dispatch1d(coefficientCount, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
allocate(
@@ -4337,7 +4338,7 @@ class RawDemosaicProcessor {
"uDiagnosticFeather",
plan.diagnosticBand?.feather ?: 0f,
)
dispatch1d(gainFloatCount, 256)
dispatch1d(gainFloatCount, GlesComputeWorkGroup.LINEAR_SIZE)
storageBarrier()
checkGlError("generatePhotonProfileGainCurvesOnGpu")
@@ -4847,6 +4848,10 @@ class RawDemosaicProcessor {
return false
}
if (!logGlResourceLimits()) {
return false
}
// 初始化着色器和缓冲区
initShaderProgram()
if (sharpenProgram == 0 || passthroughProgram == 0 ||
@@ -4869,13 +4874,6 @@ class RawDemosaicProcessor {
// 创建静默遮挡图
dummyShadingTextureId = createDummyShadingTexture()
// Query hardware texture size limit
val maxTexSizeArr = IntArray(1)
GLES30.glGetIntegerv(GLES30.GL_MAX_TEXTURE_SIZE, maxTexSizeArr, 0)
maxTextureSize = maxTexSizeArr[0]
PLog.d(TAG, "GL_MAX_TEXTURE_SIZE = $maxTextureSize")
logGlResourceLimits()
isInitialized = true
PLog.d(TAG, "RawDemosaicProcessor initialized, took=${System.currentTimeMillis() - initializeStart}ms")
return true
@@ -5243,7 +5241,7 @@ class RawDemosaicProcessor {
precision highp image2D;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uLinearRawInput;
layout(rgba16f, binding = 1) writeonly uniform highp image2D uLinearRawOutput;
uniform int uOutputY;
@@ -5273,7 +5271,7 @@ class RawDemosaicProcessor {
precision highp usampler2D;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform highp usampler2D uLinearRawRgbInput;
layout(rgba16ui, binding = 0) writeonly uniform highp uimage2D uLinearRawRgbaOutput;
uniform int uSourceY;
@@ -5614,6 +5612,7 @@ class RawDemosaicProcessor {
}
private fun compileComputeProgram(source: String, name: String): Int {
GlesComputeWorkGroup.requireBaselineCompatible(source, name)
val compileStart = System.currentTimeMillis()
val shader = GLES31.glCreateShader(GLES31.GL_COMPUTE_SHADER)
GLES31.glShaderSource(shader, source)
@@ -6379,7 +6378,7 @@ class RawDemosaicProcessor {
}
private fun dispatchDenoiseImage(width: Int, height: Int, tag: String) {
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(
GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT or
GLES31.GL_TEXTURE_FETCH_BARRIER_BIT or
@@ -6872,7 +6871,7 @@ class RawDemosaicProcessor {
GLES31.GL_WRITE_ONLY,
GLES31.GL_RGBA16UI,
)
GLES31.glDispatchCompute((width + 15) / 16, (rowCount + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(rowCount), 1)
}
private fun dispatchLinearRawUint16ToFloat(
@@ -6909,7 +6908,7 @@ class RawDemosaicProcessor {
GLES31.GL_WRITE_ONLY,
GLES31.GL_RGBA16F
)
GLES31.glDispatchCompute((width + 15) / 16, (rowCount + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(rowCount), 1)
}
private fun finishLinearRawUint16ToFloat() {
@@ -7364,12 +7363,12 @@ class RawDemosaicProcessor {
val workWidth = packedWidth * 4
val workHeight = ((roiBottom + VGN_WORK_HALO + 1) / 2) * 2
val halfHeight = workHeight / 2
val groupsPackedX = (packedWidth + 15) / 16
val groupsWorkX = (workWidth + 15) / 16
val groupsWorkY = (workHeight + 15) / 16
val groupsHalfHeight = (halfHeight + 15) / 16
val groupsOutputX = (width + 15) / 16
val groupsOutputY = (height + 15) / 16
val groupsPackedX = GlesComputeWorkGroup.imageGroupCount(packedWidth)
val groupsWorkX = GlesComputeWorkGroup.imageGroupCount(workWidth)
val groupsWorkY = GlesComputeWorkGroup.imageGroupCount(workHeight)
val groupsHalfHeight = GlesComputeWorkGroup.imageGroupCount(halfHeight)
val groupsOutputX = GlesComputeWorkGroup.imageGroupCount(width)
val groupsOutputY = GlesComputeWorkGroup.imageGroupCount(height)
val calculationGains = demosaicCalculationWbGains(metadata)
val blackLevel4 = FloatArray(4) { index ->
metadata.blackLevel.getOrElse(index) { metadata.blackLevel.firstOrNull() ?: 0f }
@@ -7921,33 +7920,33 @@ class RawDemosaicProcessor {
"white=${metadata.whiteLevel} metadataWb=${metadataWbGains.contentToString()} " +
"calculationWb=${calculationWbGains.contentToString()} lsc=$lscSize"
)
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Linear RCD Populate")
GLES31.glUseProgram(rcdStep1Program)
GLES31.glUniform2i(GLES31.glGetUniformLocation(rcdStep1Program, "uImageSize"), width, height)
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Linear RCD Step 1")
GLES31.glUseProgram(rcdStep2Program)
GLES31.glUniform2i(GLES31.glGetUniformLocation(rcdStep2Program, "uImageSize"), width, height)
GLES31.glUniform1i(GLES31.glGetUniformLocation(rcdStep2Program, "uCfaPattern"), metadata.cfaPattern)
GLES31.glDispatchCompute((width / 2 + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width / 2), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Linear RCD Step 2")
GLES31.glUseProgram(rcdStep3Program)
GLES31.glUniform2i(GLES31.glGetUniformLocation(rcdStep3Program, "uImageSize"), width, height)
GLES31.glUniform1i(GLES31.glGetUniformLocation(rcdStep3Program, "uCfaPattern"), metadata.cfaPattern)
GLES31.glDispatchCompute((width / 2 + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width / 2), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Linear RCD Step 3")
GLES31.glUseProgram(rcdStep40Program)
GLES31.glUniform2i(GLES31.glGetUniformLocation(rcdStep40Program, "uImageSize"), width, height)
GLES31.glDispatchCompute((width / 2 + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width / 2), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Linear RCD Step 4_0")
@@ -7955,7 +7954,7 @@ class RawDemosaicProcessor {
GLES31.glUseProgram(rcdStep41Program)
GLES31.glUniform2i(GLES31.glGetUniformLocation(rcdStep41Program, "uImageSize"), width, height)
GLES31.glUniform1i(GLES31.glGetUniformLocation(rcdStep41Program, "uCfaPattern"), metadata.cfaPattern)
GLES31.glDispatchCompute((width / 2 + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width / 2), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Linear RCD Step 4_1")
@@ -7963,7 +7962,7 @@ class RawDemosaicProcessor {
GLES31.glUseProgram(rcdStep42Program)
GLES31.glUniform2i(GLES31.glGetUniformLocation(rcdStep42Program, "uImageSize"), width, height)
GLES31.glUniform1i(GLES31.glGetUniformLocation(rcdStep42Program, "uCfaPattern"), metadata.cfaPattern)
GLES31.glDispatchCompute((width / 2 + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width / 2), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Linear RCD Step 4_2")
@@ -7971,7 +7970,7 @@ class RawDemosaicProcessor {
GLES31.glUseProgram(rcdStep43Program)
GLES31.glUniform2i(GLES31.glGetUniformLocation(rcdStep43Program, "uImageSize"), width, height)
GLES31.glUniform1i(GLES31.glGetUniformLocation(rcdStep43Program, "uCfaPattern"), metadata.cfaPattern)
GLES31.glDispatchCompute((width / 2 + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width / 2), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Linear RCD Step 4_3")
@@ -7993,7 +7992,7 @@ class RawDemosaicProcessor {
GLES31.GL_WRITE_ONLY,
GLES31.GL_RGBA16F
)
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_ALL_BARRIER_BITS)
checkGlError("Linear RCD Write Output")
GLES31.glBindImageTexture(
@@ -8108,7 +8107,7 @@ class RawDemosaicProcessor {
"highlightThreshold=${RcdShaders.HIGHLIGHT_RECONSTRUCTION_THRESHOLD} " +
"highlightCeiling=${RcdShaders.HIGHLIGHT_RECONSTRUCTION_CEILING}"
)
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Quad Bayer Populate")
@@ -8118,7 +8117,7 @@ class RawDemosaicProcessor {
GLES31.glGetUniformLocation(quadGreenProgram, "uCfaPattern"),
metadata.cfaPattern
)
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Quad Bayer Green")
@@ -8128,7 +8127,7 @@ class RawDemosaicProcessor {
GLES31.glGetUniformLocation(quadChromaProgram, "uCfaPattern"),
metadata.cfaPattern
)
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Quad Bayer Chroma")
@@ -8138,7 +8137,7 @@ class RawDemosaicProcessor {
GLES31.glGetUniformLocation(quadRefineProgram, "uCfaPattern"),
metadata.cfaPattern
)
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
checkGlError("Quad Bayer Refine")
@@ -8163,7 +8162,7 @@ class RawDemosaicProcessor {
GLES31.GL_WRITE_ONLY,
GLES31.GL_RGBA16F
)
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glDispatchCompute(GlesComputeWorkGroup.imageGroupCount(width), GlesComputeWorkGroup.imageGroupCount(height), 1)
GLES31.glMemoryBarrier(GLES31.GL_ALL_BARRIER_BITS)
checkGlError("Quad Bayer Write Output")
@@ -8259,7 +8258,7 @@ class RawDemosaicProcessor {
}
}
private fun logGlResourceLimits() {
private fun logGlResourceLimits(): Boolean {
val vendor = GLES30.glGetString(GLES30.GL_VENDOR).orEmpty()
val renderer = GLES30.glGetString(GLES30.GL_RENDERER).orEmpty()
val version = GLES30.glGetString(GLES30.GL_VERSION).orEmpty()
@@ -8272,6 +8271,8 @@ class RawDemosaicProcessor {
)
val value = IntArray(1)
GLES30.glGetIntegerv(GLES30.GL_MAX_TEXTURE_SIZE, value, 0)
maxTextureSize = value[0]
GLES30.glGetIntegerv(GLES30.GL_MAX_TEXTURE_IMAGE_UNITS, value, 0)
val textureImageUnits = value[0]
GLES30.glGetIntegerv(GLES31.GL_MAX_IMAGE_UNITS, value, 0)
@@ -8280,11 +8281,43 @@ class RawDemosaicProcessor {
val ssboBindings = value[0]
GLES30.glGetIntegerv(GLES31.GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS, value, 0)
val computeSsboBlocks = value[0]
GLES30.glGetIntegerv(GLES31.GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, value, 0)
val maxWorkGroupInvocations = value[0]
GLES30.glGetIntegerv(GLES31.GL_MAX_COMPUTE_SHARED_MEMORY_SIZE, value, 0)
val maxComputeSharedMemory = value[0]
val maxWorkGroupSize = IntArray(3)
for (axis in maxWorkGroupSize.indices) {
GLES30.glGetIntegeri_v(
GLES31.GL_MAX_COMPUTE_WORK_GROUP_SIZE,
axis,
value,
0,
)
maxWorkGroupSize[axis] = value[0]
}
PLog.d(
TAG,
"GL limits: textureImageUnits=$textureImageUnits imageUnits=$imageUnits " +
"ssboBindings=$ssboBindings computeSsboBlocks=$computeSsboBlocks"
"GL limits: maxTextureSize=$maxTextureSize textureImageUnits=$textureImageUnits " +
"imageUnits=$imageUnits ssboBindings=$ssboBindings " +
"computeSsboBlocks=$computeSsboBlocks " +
"computeWorkGroupInvocations=$maxWorkGroupInvocations " +
"computeWorkGroupSize=${maxWorkGroupSize.contentToString()} " +
"computeSharedMemory=$maxComputeSharedMemory",
)
val supportsRequiredWorkGroups =
maxWorkGroupInvocations >= GlesComputeWorkGroup.BASELINE_MAX_INVOCATIONS &&
maxWorkGroupSize[0] >= GlesComputeWorkGroup.LINEAR_SIZE &&
maxWorkGroupSize[1] >= GlesComputeWorkGroup.IMAGE_TILE_SIZE &&
maxWorkGroupSize[2] >= 1
if (!supportsRequiredWorkGroups) {
PLog.e(
TAG,
"GLES compute limits do not satisfy the OpenGL ES 3.1 baseline required by " +
"the RAW pipeline: invocations=$maxWorkGroupInvocations " +
"size=${maxWorkGroupSize.contentToString()}",
)
}
return supportsRequiredWorkGroups
}
private fun createDummyShadingTexture(): Int {
@@ -149,7 +149,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout (binding = 0) uniform highp usampler2D uRawTexture; // 单通道 R16UI 原始图像
layout (binding = 1) uniform highp sampler2D uLensShadingMap; // R, Gr, Gb, B 增益图
@@ -363,7 +363,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 4) buffer VH_Dir_Buf { float VH_dir[]; };
@@ -372,7 +372,8 @@ object RcdShaders {
#define epssq 1e-10f
shared float sh_buffer[576]; // 24 * 24
// 8x8 output tile plus a four-pixel halo on every edge.
shared float sh_buffer[256]; // 16 * 16
float fsquare(float x) {
return x * x;
@@ -391,16 +392,16 @@ object RcdShaders {
}
void main() {
int xlsz = 16;
int ylsz = 16;
int xlsz = 8;
int ylsz = 8;
int xlid = int(gl_LocalInvocationID.x);
int ylid = int(gl_LocalInvocationID.y);
int xgid = int(gl_WorkGroupID.x);
int ygid = int(gl_WorkGroupID.y);
int l = ylid * xlsz + xlid;
int lsz = xlsz * ylsz; // 256
int stride = 24;
int maxbuf = 576;
int lsz = xlsz * ylsz; // 64
int stride = 16;
int maxbuf = 256;
int xul = xgid * xlsz - 2;
int yul = ygid * ylsz - 2;
@@ -439,7 +440,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 5) buffer LPF_Buf { float lpf[]; };
@@ -486,7 +487,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 2) buffer RGB1_Buf { float rgb1[]; };
@@ -558,7 +559,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 6) buffer P_Diff_Buf { float p_diff[]; };
@@ -593,7 +594,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 6) buffer P_Diff_Buf { float p_diff[]; };
layout(std430, binding = 7) buffer Q_Diff_Buf { float q_diff[]; };
@@ -646,7 +647,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; }; // R
@@ -742,7 +743,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; }; // R
@@ -852,7 +853,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; };
@@ -945,7 +946,7 @@ object RcdShaders {
#version 310 es
precision highp float;
precision highp int;
layout (local_size_x = 16, local_size_y = 16) in;
layout (local_size_x = 8, local_size_y = 8) in;
layout(std430, binding = 0) readonly buffer CFA_Buf { float cfa[]; };
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; };
@@ -1051,7 +1052,7 @@ object RcdShaders {
fun regionPopulate(rawCommon: String): String = """
#version 310 es
$rawCommon
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
uniform highp usampler2D uRawRegion;
uniform sampler2D uLensShadingMap;
uniform ivec2 uRegionSize;
@@ -1161,7 +1162,7 @@ object VgnShaders {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D yccdTex;
layout(rgba16ui, binding = 1) readonly uniform uimage2D ycceSmoothTex;
@@ -1242,7 +1243,7 @@ void main() {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D yccdTex;
layout(rgba16ui, binding = 1) writeonly uniform uimage2D dstTex;
@@ -1297,7 +1298,7 @@ precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D inTex;
layout(rgba16ui, binding = 1) writeonly uniform uimage2D outTex;
@@ -1362,7 +1363,7 @@ precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D inTex;
layout(rgba16ui, binding = 1) writeonly uniform uimage2D outTex;
@@ -1483,7 +1484,7 @@ precision highp float;
precision highp int;
precision highp uimage2D;
precision highp image2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D yccdTex;
layout(rgba16ui, binding = 1) readonly uniform uimage2D smoothTex;
@@ -1663,7 +1664,7 @@ void main() {
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D yccdTex;
layout(rgba16ui, binding = 1) readonly uniform uimage2D smoothTex;
@@ -2120,7 +2121,7 @@ precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D inTex;
layout(rgba16ui, binding = 1) writeonly uniform uimage2D outTex;
@@ -2268,7 +2269,7 @@ precision highp int;
precision highp image2D;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D inTex;
layout(rgba16f, binding = 1) writeonly uniform image2D outTex;
@@ -2378,7 +2379,7 @@ precision highp int;
precision highp image2D;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16f, binding = 0) readonly uniform image2D inTex; // textureScale (width, height/2)
layout(rgba16f, binding = 1) writeonly uniform image2D outTex; // textureMedian (width/4, height/2)
@@ -2456,7 +2457,7 @@ precision highp int;
precision highp iimage2D;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D inTex;
layout(rgba16i, binding = 1) writeonly uniform iimage2D outTex;
@@ -2555,7 +2556,7 @@ precision highp image2D;
precision highp uimage2D;
precision highp iimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D uInputTexture0;
layout(rgba16i, binding = 1) readonly uniform iimage2D uInputTexture1;
@@ -2951,7 +2952,7 @@ precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D inTex;
layout(rgba16ui, binding = 1) writeonly uniform uimage2D outTex;
@@ -3070,7 +3071,7 @@ precision highp int;
precision highp uimage2D;
precision highp iimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D rgbdTex;
layout(rgba16ui, binding = 1) readonly uniform uimage2D bayerTex;
@@ -3411,7 +3412,7 @@ precision highp int;
precision highp image2D;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16f, binding = 0) readonly uniform image2D inTex;
layout(rgba16ui, binding = 1) writeonly uniform uimage2D outTex;
@@ -3475,7 +3476,7 @@ precision highp float;
precision highp int;
precision highp uimage2D;
precision highp image2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16ui, binding = 0) readonly uniform uimage2D yccdTex;
layout(rgba16f, binding = 1) writeonly uniform image2D outTex;
@@ -3536,7 +3537,7 @@ void main() {
precision highp int;
precision highp image2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(binding = 0) uniform highp usampler2D uRawTexture;
layout(binding = 1) uniform highp sampler2D uLensShadingMap;
@@ -3697,7 +3698,7 @@ void main() {
precision highp int;
precision highp image2D;
layout(local_size_x = 16, local_size_y = 16) in;
layout(local_size_x = 8, local_size_y = 8) in;
layout(rgba16f, binding = 0) readonly uniform image2D vgnTex;
layout(rgba16f, binding = 1) writeonly uniform image2D outTex;
@@ -0,0 +1,56 @@
package com.hinnka.mycamera.processor
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Test
class GlesComputeWorkGroupTest {
@Test
fun imageAndLinearWorkGroupsFitTheGles31Baseline() {
assertEquals(
64,
GlesComputeWorkGroup.IMAGE_TILE_SIZE * GlesComputeWorkGroup.IMAGE_TILE_SIZE,
)
assertEquals(128, GlesComputeWorkGroup.LINEAR_SIZE)
assertEquals(
GlesComputeWorkGroup.BASELINE_MAX_INVOCATIONS,
GlesComputeWorkGroup.LINEAR_SIZE,
)
}
@Test
fun dispatchGroupCountCoversPartialGroups() {
assertEquals(0, GlesComputeWorkGroup.imageGroupCount(0))
assertEquals(1, GlesComputeWorkGroup.imageGroupCount(1))
assertEquals(1, GlesComputeWorkGroup.imageGroupCount(8))
assertEquals(2, GlesComputeWorkGroup.imageGroupCount(9))
assertEquals(1, GlesComputeWorkGroup.linearGroupCount(128))
assertEquals(2, GlesComputeWorkGroup.linearGroupCount(129))
}
@Test
fun shaderDeclarationParserChecksTotalInvocations() {
val compatible = """
#version 310 es
layout(local_size_x = 16, local_size_y = 8, local_size_z = 1) in;
void main() {}
""".trimIndent()
val incompatible = """
#version 310 es
layout(local_size_x = 16, local_size_y = 16) in;
void main() {}
""".trimIndent()
assertEquals(
GlesComputeWorkGroup.Size(x = 16, y = 8, z = 1),
GlesComputeWorkGroup.declaredSize(compatible),
)
GlesComputeWorkGroup.requireBaselineCompatible(compatible, "compatible")
try {
GlesComputeWorkGroup.requireBaselineCompatible(incompatible, "incompatible")
fail("16x16 must exceed the GLES 3.1 invocation baseline")
} catch (_: IllegalArgumentException) {
// Expected.
}
}
}
@@ -0,0 +1,64 @@
package com.hinnka.mycamera.raw
import com.hinnka.mycamera.processor.GlesComputeWorkGroup
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.io.File
class DngHdrProfileGainTableGpuShadersTest {
@Test
fun cellStatsKeeps256SamplesWith128CooperativeLanes() {
val shader = DngHdrProfileGainTableGpuShaders.CELL_STATS
assertEquals(
GlesComputeWorkGroup.Size(x = 16, y = 8, z = 1),
GlesComputeWorkGroup.declaredSize(shader),
)
assertTrue(shader.contains("const uint CELL_SAMPLE_COUNT = 256u;"))
assertTrue(shader.contains("const uint CELL_LANE_COUNT = 128u;"))
assertTrue(shader.contains("sampleIndex += CELL_LANE_COUNT"))
assertTrue(shader.contains("localIndex += CELL_LANE_COUNT"))
assertTrue(shader.contains("sortSamples(localIndex);"))
GlesComputeWorkGroup.requireBaselineCompatible(shader, "DNG_PGTM_CELL_STATS")
}
@Test
fun cellStatsPassesAvailableNdkValidator() {
val sdkRoot = System.getenv("ANDROID_SDK_ROOT") ?: System.getenv("ANDROID_HOME")
val validator = sdkRoot?.let(::File)
?.resolve("ndk")
?.listFiles()
?.sortedByDescending { it.name }
?.asSequence()
?.mapNotNull { ndk ->
ndk.resolve("shader-tools")
.walkTopDown()
.firstOrNull { it.name == "glslc" && it.canExecute() }
}
?.firstOrNull()
assumeTrue("Android NDK glslc is unavailable", validator != null)
val sourceFile = File.createTempFile("dng-pgtm-cell-stats-", ".compute")
val outputFile = File.createTempFile("dng-pgtm-cell-stats-", ".spv")
try {
sourceFile.writeText(DngHdrProfileGainTableGpuShaders.CELL_STATS)
val process = ProcessBuilder(
checkNotNull(validator).absolutePath,
"--target-env=opengl",
"-fauto-map-locations",
"-fauto-bind-uniforms",
"-fshader-stage=compute",
sourceFile.absolutePath,
"-o",
outputFile.absolutePath,
).redirectErrorStream(true).start()
val output = process.inputStream.bufferedReader().use { it.readText() }
assertEquals(output, 0, process.waitFor())
} finally {
sourceFile.delete()
outputFile.delete()
}
}
}
@@ -1,10 +1,26 @@
package com.hinnka.mycamera.raw
import com.hinnka.mycamera.processor.GlesComputeWorkGroup
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class RcdShadersTest {
@Test
fun sharedGradientTileFitsTheBaselineAndKeepsItsFourPixelHalo() {
val shader = RcdShaders.STEP_1
assertEquals(
GlesComputeWorkGroup.Size(x = 8, y = 8, z = 1),
GlesComputeWorkGroup.declaredSize(shader),
)
assertTrue(shader.contains("shared float sh_buffer[256];"))
assertTrue(shader.contains("int stride = 16;"))
assertTrue(shader.contains("int buf_offset = (ylid + 4) * stride + (xlid + 4);"))
GlesComputeWorkGroup.requireBaselineCompatible(shader, "RCD_STEP_1")
}
@Test
fun ordinaryBayerBorderUsesPpgWithParityPreservingMirroring() {
val shader = RcdShaders.WRITE_OUTPUT
+51
View File
@@ -7,6 +7,57 @@
本文,并在代表设备上运行实际的 compile、bind、dispatch、readback 和后续采样测试。不能仅凭
GLES 版本号、扩展列表或桌面 GLSL 编译结果判断移动端可用性。
## Compute work-group 必须满足 GLES 3.1 最低保证
已确认设备:Xiaomi M2101K7BNY,Android 13。
### 问题
OpenGL ES 3.1 只保证:
- `GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS >= 128`;
- `GL_MAX_COMPUTE_WORK_GROUP_SIZE` 的 X/Y/Z 轴分别至少为 `128/128/64`。
不能假设所有 GLES 3.1 设备都允许 256 个 invocation。超限可能在 shader 编译或 program
链接阶段以不同形式失败:
```text
'local_size_x' qualifier value (256) exceeds the maximum supported size (128)
Max number of total work group invocations exceeded.
```
### 错误做法
- 图像 compute shader 固定使用 `16x16`。
- 一维 compute shader 固定使用 `256x1`。
- 只根据开发设备或 GLES 版本号判断工作组可用。
- 只修改 shader 的 `local_size`,但仍按旧尺寸计算 `glDispatchCompute`。
- 对依赖 `gl_LocalInvocationID`、shared memory 或 barrier 的算法机械缩小工作组。
- 在关键 shader 编译完成后才查询能力;失败设备将无法留下 renderer 和限制信息。
### 正确做法
- 通用二维图像 pass 使用 `8x8`,一维 pass 和归约使用 `128x1`。
- shader 编译入口统一验证每轴尺寸及三轴乘积不超过 GLES 3.1 最低保证。
- dispatch group count 与 shader 工作组尺寸共用
`GlesComputeWorkGroup` 中的常量和整除上取整逻辑。
- EGL context `makeCurrent` 后、编译任何 shader 前,记录 vendor、renderer、GL version、
`GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS` 和三轴
`GL_MAX_COMPUTE_WORK_GROUP_SIZE`。
- 修改包含 shared memory 或 barrier 的 shader 时,同步检查共享数组几何、lane 索引、
数据装载次数和每次 barrier 前后的完整参与关系。
`DNG_PGTM_CELL_STATS` 需要对每个 cell 的 256 个样本进行排序。兼容路径保留 256 项共享数组,
由 128 个 lane 各装载两个样本,并在 bitonic sort 的每个阶段各处理两个 comparator;不能通过
减少样本数量或只运行半个排序网络来规避工作组限制。
### 验证
- 静态确认所有 compute shader 的 invocation 乘积不超过 128。
- 覆盖不能整除工作组尺寸的宽高,确认 dispatch 不遗漏边缘。
- 真机执行 shader compile/link、dispatch、barrier 和 readback。
- 对共享算法比较修改前后的输出,包括 PGTM cell 分位数、NLM、RCD、Quad Bayer 和 Radiance。
## Graphics program 的 GLSL ES 版本必须一致
已确认设备会严格拒绝使用不同 GLSL ES 版本的 vertex/fragment shader 链接: