trying to a add automatic exposure

This commit is contained in:
Dynamitos
2025-03-11 22:08:23 +01:00
parent a957b05615
commit 80f86ca95c
10 changed files with 254 additions and 112 deletions
+1 -1
View File
@@ -105,7 +105,7 @@ void taskMain(
viewFrustum.sides[3] = computePlane(origin, corners[3], corners[2]);
meshVisible = true;
#ifdef DEPTH_CULLING
meshVisible = mesh.bounding.insideFrustum(viewFrustum) && isBoxVisible(mesh.bounding);
//meshVisible = mesh.bounding.insideFrustum(viewFrustum) && isBoxVisible(mesh.bounding);
#endif
}
GroupMemoryBarrierWithGroupSync();
+78
View File
@@ -0,0 +1,78 @@
struct HistogramParameters
{
float minLogLum;
float inverseLogLumRange;
float timeCoeff;
uint numPixels;
Texture2D hdrImage;
globallycoherent RWStructuredBuffer<uint> histogram;
RWStructuredBuffer<float> averageLuminance;
};
ParameterBlock<HistogramParameters> pHistogramParams;
static const float3 RGB_TO_LUMINANCE = float3(0.2125, 0.7154, 0.0721);
static const float eps = 0.005;
uint colorToBin(float3 hdrColor) {
float lum = dot(hdrColor, RGB_TO_LUMINANCE);
if(lum < eps) {
return 0;
}
float logLum = clamp((log2(lum) - pHistogramParams.minLogLum) * pHistogramParams.inverseLogLumRange, 0, 1);
return uint(logLum * 254.0 + 1.0);
}
groupshared uint histogramShared[256];
[shader("compute")]
[numthreads(16, 16, 1)]
void histogram(uint threadID : SV_GroupIndex, uint2 globalThreadID : SV_DispatchThreadID)
{
histogramShared[threadID] = 0;
GroupMemoryBarrierWithGroupSync();
uint2 dim;
pHistogramParams.hdrImage.GetDimensions(dim.x, dim.y);
if(globalThreadID.x < dim.x && globalThreadID.y < dim.y) {
float3 hdrColor = pHistogramParams.hdrImage.Load(int3(globalThreadID, 0)).xyz;
uint binIndex = colorToBin(hdrColor);
InterlockedAdd(histogramShared[binIndex], 1);
}
GroupMemoryBarrierWithGroupSync();
InterlockedAdd(pHistogramParams.histogram[threadID], histogramShared[threadID]);
}
[shader("compute")]
[numthreads(256, 1, 1)]
void exposure(uint threadID : SV_GroupThreadID)
{
uint countForThisBin = pHistogramParams.histogram[threadID];
histogramShared[threadID] = countForThisBin * threadID;
GroupMemoryBarrierWithGroupSync();
pHistogramParams.histogram[threadID] = 0;
for(uint cutoff = (256 >> 1); cutoff > 0; cutoff >>= 1) {
if(uint(threadID) < cutoff) {
histogramShared[threadID] += histogramShared[threadID + cutoff];
}
GroupMemoryBarrierWithGroupSync();
}
if(threadID == 0) {
float weightedLogAverage = (histogramShared[0] / max(pHistogramParams.numPixels - float(countForThisBin), 1.0f)) - 1.0f;
float weightedAvgLum = exp2(((weightedLogAverage / 254.0f) * pHistogramParams.inverseLogLumRange) + pHistogramParams.minLogLum);
float lumLastFrame = pHistogramParams.averageLuminance[0];
float adaptedLum = lumLastFrame + (weightedAvgLum - lumLastFrame) * pHistogramParams.timeCoeff;
pHistogramParams.averageLuminance[0] = adaptedLum;
}
}
-15
View File
@@ -1,15 +0,0 @@
{
"name": "Placeholder",
"params": {
},
"code": [
{
"exp": "BRDF",
"profile": "BlinnPhong",
"values": {
"baseColor": "float3(0, 1, 0)",
"normal": "float3(0, 0, 1)"
}
}
]
}
+15 -14
View File
@@ -31,8 +31,9 @@ struct Parameters
float sat = 1.0;
Texture2D hdrInputTexture;
SamplerState hdrSampler;
StructuredBuffer<float> averageLuminance;
};
ParameterBlock<Parameters> pParams;
ParameterBlock<Parameters> pToneMappingParams;
// AgX
// ->
@@ -88,15 +89,15 @@ float3 agxEotf(float3 val) {
return val;
}
float3 agxLook(float3 val) {
float3 agxLook(float3 val, float avgLum) {
const float3 lw = float3(0.2126, 0.7152, 0.0722);
float luma = dot(val, lw);
float luma = dot(val, lw) / (9.6 * avgLum + 0.00001);
// Default
float3 offset = pParams.offset.xyz;
float3 slope = pParams.slope.xyz;
float3 power = pParams.power.xyz;
float sat = pParams.sat;
float3 offset = pToneMappingParams.offset.xyz;
float3 slope = pToneMappingParams.slope.xyz;
float3 power = pToneMappingParams.power.xyz;
float sat = pToneMappingParams.sat;
// ASC CDL
val = pow(val * slope + offset, power);
@@ -106,12 +107,12 @@ float3 agxLook(float3 val) {
[shader("pixel")]
float4 toneMapping(float2 uv : UV) : SV_Target
{
float3 hdrValue = pParams.hdrInputTexture.Sample(pParams.hdrSampler, uv).rgb;
//float3 value = agx(hdrValue);
//value = agxLook(value);
//value = agxEotf(value);
float3 value = hdrValue / (hdrValue + float3(10));
float3 hdrValue = pToneMappingParams.hdrInputTexture.Sample(pToneMappingParams.hdrSampler, uv).rgb;
float avgLum = pToneMappingParams.averageLuminance[0];
float3 value = agx(hdrValue);
value = agxLook(value, avgLum);
value = agxEotf(value);
return float4(value, 1);
}
}
-2
View File
@@ -7,8 +7,6 @@ import StaticMeshVertexData;
[shader("anyhit")]
void anyhit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
{
if(RayTCurrent() > hitValue.t)
return;
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
InstanceData inst = pScene.instances[InstanceID()];
-44
View File
@@ -1,44 +0,0 @@
const static float2 positions[3] = {
float2(0.0, -0.5),
float2(0.5, 0.5),
float2(-0.5, 0.5)
};
const static float3 colors[3] = {
float3(1.0, 1.0, 0.0),
float3(0.0, 1.0, 1.0),
float3(1.0, 0.0, 1.0)
};
struct Vertex
{
float4 pos : SV_Position;
float3 color : Color;
int index : Index;
};
const static uint MAX_VERTS = 12;
const static uint MAX_PRIMS = 4;
[outputtopology("triangle")]
[shader("mesh")]
[numthreads(3, 1, 1)]
void meshMain(
in uint tig : SV_GroupIndex,
out Vertices<Vertex, MAX_VERTS> verts,
out Indices<uint3, MAX_PRIMS> triangles)
{
const uint numVertices = 12;
const uint numPrimitives = 4;
SetMeshOutputCounts(numVertices, numPrimitives);
for(uint i = tig; i < numVertices; ++i)
{
const int tri = i / 3;
verts[i] = {float4(positions[i % 3], 0, 1), colors[i % 3], tri};
}
for(uint i = tig; i < numPrimitives; ++i)
{
triangles[i] = i * 3 + uint3(0,1,2);
}
}