Broke it down to work with a single probe for confirmation of general math and behavior.

This commit is contained in:
Areloch 2019-01-31 11:44:09 -06:00
parent 54a64c0d82
commit a0b9c8cfd6
2 changed files with 80 additions and 49 deletions

View file

@ -679,7 +679,7 @@ void RenderProbeMgr::render( SceneRenderState *state )
}*/ }*/
//Array rendering //Array rendering
static U32 MAXPROBECOUNT = 50; static U32 MAXPROBECOUNT = 1;// 50;
U32 probeCount = ProbeRenderInst::all.size(); U32 probeCount = ProbeRenderInst::all.size();
@ -688,7 +688,7 @@ void RenderProbeMgr::render( SceneRenderState *state )
MatrixF trans = MatrixF::Identity; MatrixF trans = MatrixF::Identity;
sgData.objTrans = &trans; sgData.objTrans = &trans;
AlignedArray<Point3F> probePositions(MAXPROBECOUNT, sizeof(Point3F)); /*AlignedArray<Point3F> probePositions(MAXPROBECOUNT, sizeof(Point3F));
Vector<MatrixF> probeWorldToObj; Vector<MatrixF> probeWorldToObj;
AlignedArray<Point3F> probeBBMin(MAXPROBECOUNT, sizeof(Point3F)); AlignedArray<Point3F> probeBBMin(MAXPROBECOUNT, sizeof(Point3F));
AlignedArray<Point3F> probeBBMax(MAXPROBECOUNT, sizeof(Point3F)); AlignedArray<Point3F> probeBBMax(MAXPROBECOUNT, sizeof(Point3F));
@ -705,7 +705,7 @@ void RenderProbeMgr::render( SceneRenderState *state )
dMemset(probeAttenuation.getBuffer(), 0, probeAttenuation.getBufferSize()); dMemset(probeAttenuation.getBuffer(), 0, probeAttenuation.getBufferSize());
Vector<GFXCubemapHandle> cubeMaps; Vector<GFXCubemapHandle> cubeMaps;
Vector<GFXCubemapHandle> irradMaps; Vector<GFXCubemapHandle> irradMaps;*/
if (reflProbeMat && reflProbeMat->matInstance) if (reflProbeMat && reflProbeMat->matInstance)
{ {
@ -721,7 +721,7 @@ void RenderProbeMgr::render( SceneRenderState *state )
MaterialParameterHandle *probeRadiusSC = reflProbeMat->matInstance->getMaterialParameterHandle("$radius"); MaterialParameterHandle *probeRadiusSC = reflProbeMat->matInstance->getMaterialParameterHandle("$radius");
MaterialParameterHandle *probeAttenuationSC = reflProbeMat->matInstance->getMaterialParameterHandle("$attenuation"); MaterialParameterHandle *probeAttenuationSC = reflProbeMat->matInstance->getMaterialParameterHandle("$attenuation");
U32 effectiveProbeCount = 0; /*U32 effectiveProbeCount = 0;
for (U32 i = 0; i < probeCount; i++) for (U32 i = 0; i < probeCount; i++)
{ {
@ -763,17 +763,17 @@ void RenderProbeMgr::render( SceneRenderState *state )
} }
if (effectiveProbeCount != 0) if (effectiveProbeCount != 0)
{ {*/
matParams->setSafe(numProbesSC, (float)effectiveProbeCount); //matParams->setSafe(numProbesSC, (float)effectiveProbeCount);
GFXCubemapArrayHandle mCubemapArray; /*GFXCubemapArrayHandle mCubemapArray;
mCubemapArray = GFXCubemapArrayHandle(GFX->createCubemapArray()); mCubemapArray = GFXCubemapArrayHandle(GFX->createCubemapArray());
GFXCubemapArrayHandle mIrradArray; GFXCubemapArrayHandle mIrradArray;
mIrradArray = GFXCubemapArrayHandle(GFX->createCubemapArray()); mIrradArray = GFXCubemapArrayHandle(GFX->createCubemapArray());
mCubemapArray->initStatic(cubeMaps.address(), cubeMaps.size()); mCubemapArray->initStatic(cubeMaps.address(), cubeMaps.size());
mIrradArray->initStatic(irradMaps.address(), irradMaps.size()); mIrradArray->initStatic(irradMaps.address(), irradMaps.size());*/
NamedTexTarget *deferredTarget = NamedTexTarget::find(RenderDeferredMgr::BufferName); NamedTexTarget *deferredTarget = NamedTexTarget::find(RenderDeferredMgr::BufferName);
if (deferredTarget) if (deferredTarget)
@ -801,11 +801,27 @@ void RenderProbeMgr::render( SceneRenderState *state )
GFX->setTexture(3, NULL); GFX->setTexture(3, NULL);
GFX->setCubeArrayTexture(4, mCubemapArray); //GFX->setCubeArrayTexture(4, mCubemapArray);
GFX->setCubeArrayTexture(5, mIrradArray); //GFX->setCubeArrayTexture(5, mIrradArray);
matParams->set(probePositionSC, probePositions); ProbeRenderInst* curEntry = ProbeRenderInst::all[0];
matParams->set(probeWorldToObjSC, probeWorldToObj.address(), probeWorldToObj.size());
Point3F probePosition = curEntry->getPosition();
MatrixF probeWorldToObj = curEntry->getTransform();
probeWorldToObj.inverse();
Point3F probeBBMin = curEntry->mBounds.minExtents;
Point3F probeBBMax = curEntry->mBounds.maxExtents;
float probeUseSphereMode = 0;
float probeRadius = curEntry->mRadius;
float probeAttenuation = 1;
GFX->setCubeTexture(4, curEntry->mCubemap);
GFX->setCubeTexture(5, curEntry->mIrradianceCubemap);
matParams->set(probePositionSC, probePosition);
matParams->set(probeWorldToObjSC, probeWorldToObj);
matParams->set(probeBBMinSC, probeBBMin); matParams->set(probeBBMinSC, probeBBMin);
matParams->set(probeBBMaxSC, probeBBMax); matParams->set(probeBBMaxSC, probeBBMax);
matParams->set(probeUseSphereModeSC, probeUseSphereMode); matParams->set(probeUseSphereModeSC, probeUseSphereMode);
@ -825,7 +841,7 @@ void RenderProbeMgr::render( SceneRenderState *state )
GFX->drawPrimitive(GFXTriangleStrip, 0, 2); GFX->drawPrimitive(GFXTriangleStrip, 0, 2);
} }
} //}
} }
} }
// //

View file

@ -16,18 +16,18 @@ uniform float3 eyePosWorld;
//cubemap arrays require all the same size. so shared mips# value //cubemap arrays require all the same size. so shared mips# value
uniform float cubeMips; uniform float cubeMips;
#define MAX_PROBES 50 #define MAX_PROBES 1 //50
uniform float numProbes; uniform float numProbes;
TORQUE_UNIFORM_SAMPLERCUBEARRAY(cubeMapAR, 4); TORQUE_UNIFORM_SAMPLERCUBE(cubeMapAR, 4);
TORQUE_UNIFORM_SAMPLERCUBEARRAY(irradianceCubemapAR, 5); TORQUE_UNIFORM_SAMPLERCUBE(irradianceCubemapAR, 5);
uniform float3 inProbePosArray[MAX_PROBES]; uniform float3 inProbePosArray;
uniform float4x4 worldToObjArray[MAX_PROBES]; uniform float4x4 worldToObjArray;
uniform float3 bbMinArray[MAX_PROBES]; uniform float3 bbMinArray;
uniform float3 bbMaxArray[MAX_PROBES]; uniform float3 bbMaxArray;
uniform float useSphereMode[MAX_PROBES]; uniform float useSphereMode;
uniform float radius[MAX_PROBES]; uniform float radius;
uniform float2 attenuation[MAX_PROBES]; uniform float2 attenuation;
// Box Projected IBL Lighting // Box Projected IBL Lighting
// Based on: http://www.gamedev.net/topic/568829-box-projected-cubemap-environment-mapping/ // Based on: http://www.gamedev.net/topic/568829-box-projected-cubemap-environment-mapping/
@ -46,14 +46,14 @@ float3 boxProject(float3 wsPosition, float3 reflectDir, float3 boxWSPos, float3
return posonbox - boxWSPos; return posonbox - boxWSPos;
} }
float3 iblBoxDiffuse( Surface surface, int id) float3 iblBoxDiffuse( Surface surface)
{ {
float3 cubeN = boxProject(surface.P, surface.N, inProbePosArray[id], bbMinArray[id], bbMaxArray[id]); float3 cubeN = boxProject(surface.P, surface.N, inProbePosArray, bbMinArray, bbMaxArray);
cubeN.z *=-1; cubeN.z *=-1;
return TORQUE_TEXCUBEARRAYLOD(irradianceCubemapAR,cubeN,id,0).xyz; return TORQUE_TEXCUBELOD(irradianceCubemapAR,float4(cubeN,0)).xyz;
} }
float3 iblBoxSpecular(Surface surface, float3 surfToEye, TORQUE_SAMPLER2D(brdfTexture), int id) float3 iblBoxSpecular(Surface surface, float3 surfToEye, TORQUE_SAMPLER2D(brdfTexture))
{ {
float ndotv = clamp(dot(surface.N, surfToEye), 0.0, 1.0); float ndotv = clamp(dot(surface.N, surfToEye), 0.0, 1.0);
@ -64,20 +64,20 @@ float3 iblBoxSpecular(Surface surface, float3 surfToEye, TORQUE_SAMPLER2D(brdfTe
float lod = surface.roughness*cubeMips; float lod = surface.roughness*cubeMips;
float3 r = reflect(surfToEye, surface.N); float3 r = reflect(surfToEye, surface.N);
float3 cubeR = normalize(r); float3 cubeR = normalize(r);
cubeR = boxProject(surface.P, surface.N, inProbePosArray[id], bbMinArray[id], bbMaxArray[id]); cubeR = boxProject(surface.P, surface.N, inProbePosArray, bbMinArray, bbMaxArray);
float3 radiance = TORQUE_TEXCUBEARRAYLOD(cubeMapAR,cubeR,id,lod).xyz * (brdf.x + brdf.y); float3 radiance = TORQUE_TEXCUBELOD(cubeMapAR,float4(cubeR,lod)).xyz * (brdf.x + brdf.y);
return radiance; return radiance;
} }
float defineBoxSpaceInfluence(Surface surface, int id) float defineBoxSpaceInfluence(Surface surface)
{ {
float tempAttenVal = 3.5; //replace with per probe atten float tempAttenVal = 3.5; //replace with per probe atten
float3 surfPosLS = mul( worldToObjArray[id], float4(surface.P,1.0)).xyz; float3 surfPosLS = mul( worldToObjArray, float4(surface.P,1.0)).xyz;
float3 boxMinLS = inProbePosArray[id]-(float3(1,1,1)*radius[id]); float3 boxMinLS = inProbePosArray-(float3(1,1,1)*radius);
float3 boxMaxLS = inProbePosArray[id]+(float3(1,1,1)*radius[id]); float3 boxMaxLS = inProbePosArray+(float3(1,1,1)*radius);
float boxOuterRange = length(boxMaxLS - boxMinLS); float boxOuterRange = length(boxMaxLS - boxMinLS);
float boxInnerRange = boxOuterRange / tempAttenVal; float boxInnerRange = boxOuterRange / tempAttenVal;
@ -93,23 +93,23 @@ float4 main( FarFrustumQuadConnectP IN ) : SV_TARGET
//unpack normal and linear depth //unpack normal and linear depth
float4 normDepth = TORQUE_DEFERRED_UNCONDITION(deferredBuffer, IN.uv0.xy); float4 normDepth = TORQUE_DEFERRED_UNCONDITION(deferredBuffer, IN.uv0.xy);
//create surface //create surface
Surface surface = createSurface( normDepth, TORQUE_SAMPLER2D_MAKEARG(colorBuffer),TORQUE_SAMPLER2D_MAKEARG(matInfoBuffer), Surface surface = createSurface( normDepth, TORQUE_SAMPLER2D_MAKEARG(colorBuffer),TORQUE_SAMPLER2D_MAKEARG(matInfoBuffer),
IN.uv0.xy, eyePosWorld, IN.wsEyeRay, cameraToWorld); IN.uv0.xy, eyePosWorld, IN.wsEyeRay, cameraToWorld);
//early out if emissive //early out if emissive
if (getFlag(surface.matFlag, 0)) if (getFlag(surface.matFlag, 0))
{ {
discard; discard;
} }
float blendVal[MAX_PROBES]; float blendVal;
float3 surfToEye = normalize(surface.P - eyePosWorld); float3 surfToEye = normalize(surface.P - eyePosWorld);
int i; int i;
float blendSum = 0; float blendSum = 0;
float invBlendSum = 0; float invBlendSum = 0;
for(i=0; i < numProbes; i++) /*for(i=0; i < numProbes; i++)
{ {
float3 probeWS = inProbePosArray[i]; float3 probeWS = inProbePosArray[i];
float3 L = probeWS - surface.P; float3 L = probeWS - surface.P;
@ -127,22 +127,37 @@ float4 main( FarFrustumQuadConnectP IN ) : SV_TARGET
} }
blendSum += blendVal[i]; blendSum += blendVal[i];
invBlendSum +=(1.0f - blendVal[i]); invBlendSum +=(1.0f - blendVal[i]);
} }*/
if(useSphereMode)
{
float3 L = inProbePosArray - surface.P;
blendVal = 1.0-length(L)/radius;
blendVal = max(0,blendVal);
}
else
{
blendVal = defineBoxSpaceInfluence(surface);
blendVal = max(0,blendVal);
}
// Weight0 = normalized NDF, inverted to have 1 at center, 0 at boundary. // Weight0 = normalized NDF, inverted to have 1 at center, 0 at boundary.
// And as we invert, we need to divide by Num-1 to stay normalized (else sum is > 1). // And as we invert, we need to divide by Num-1 to stay normalized (else sum is > 1).
// respect constraint B. // respect constraint B.
// Weight1 = normalized inverted NDF, so we have 1 at center, 0 at boundary // Weight1 = normalized inverted NDF, so we have 1 at center, 0 at boundary
// and respect constraint A. // and respect constraint A.
for(i=0; i < numProbes; i++) /*for(i=0; i < numProbes; i++)
{ {
blendVal[i] = (1.0f - ( blendVal[i] / blendSum)) / (numProbes - 1); blendVal[i] = (1.0f - ( blendVal[i] / blendSum)) / (numProbes - 1);
blendVal[i] *= ((1.0f - blendVal[i]) / invBlendSum); blendVal[i] *= ((1.0f - blendVal[i]) / invBlendSum);
blendSum += blendVal[i]; blendSum += blendVal[i];
} }*/
//float asdasg = defineBoxSpaceInfluence(surface, 0);
//return float4(asdasg,asdasg,asdasg,1);
// Normalize blendVal // Normalize blendVal
if (blendSum == 0.0f) // Possible with custom weight /*if (blendSum == 0.0f) // Possible with custom weight
{ {
blendSum = 1.0f; blendSum = 1.0f;
} }
@ -151,7 +166,7 @@ float4 main( FarFrustumQuadConnectP IN ) : SV_TARGET
for (i = 0; i < numProbes; ++i) for (i = 0; i < numProbes; ++i)
{ {
blendVal[i] *= invBlendSumWeighted; blendVal[i] *= invBlendSumWeighted;
} }*/
float3 irradiance = float3(0,0,0); float3 irradiance = float3(0,0,0);
float3 specular = float3(0,0,0); float3 specular = float3(0,0,0);
@ -160,15 +175,15 @@ float4 main( FarFrustumQuadConnectP IN ) : SV_TARGET
//energy conservation //energy conservation
float3 kD = 1.0.xxx - F; float3 kD = 1.0.xxx - F;
kD *= 1.0 - surface.metalness; kD *= 1.0 - surface.metalness;
for (i = 0; i < numProbes; ++i) //for (i = 0; i < numProbes; ++i)
{ //{
irradiance += blendVal[i]*iblBoxDiffuse(surface,i); irradiance = blendVal*iblBoxDiffuse(surface);
specular += blendVal[i]*F*iblBoxSpecular(surface, surfToEye, TORQUE_SAMPLER2D_MAKEARG(BRDFTexture),i); specular = blendVal*F*iblBoxSpecular(surface, surfToEye, TORQUE_SAMPLER2D_MAKEARG(BRDFTexture));
} //}
//final diffuse color //final diffuse color
float3 diffuse = kD * irradiance * surface.baseColor.rgb; float3 diffuse = kD * irradiance * surface.baseColor.rgb;
float4 finalColor = float4(diffuse + specular * surface.ao, blendSum); float4 finalColor = float4(diffuse + specular * surface.ao, 1);
return finalColor; return finalColor;
} }