Probe Array WIP

This commit is contained in:
Areloch 2019-01-07 20:34:19 -06:00
parent 85d8c7a2de
commit edbbeaf008
11 changed files with 586 additions and 56 deletions

View file

@ -129,7 +129,8 @@ ProbeShaderConstants::ProbeShaderConstants()
mProbeBoxMaxSC(NULL),
mProbeIsSphereSC(NULL),
mProbeLocalPosSC(NULL),
mProbeCubemapSC(NULL)
mProbeCubemapSC(NULL),
mProbeCountSC(NULL)
{
}
@ -163,6 +164,7 @@ void ProbeShaderConstants::init(GFXShader* shader)
mProbeIsSphereSC = shader->getShaderConstHandle(ShaderGenVars::probeIsSphere);
mProbeLocalPosSC = shader->getShaderConstHandle(ShaderGenVars::probeLocalPos);
mProbeCubemapSC = shader->getShaderConstHandle(ShaderGenVars::probeCubemap);
mProbeCountSC = shader->getShaderConstHandle(ShaderGenVars::probeCount);
mInit = true;
}
@ -182,6 +184,7 @@ ProbeManager::ProbeManager()
mSkylightMaterial = nullptr;
mReflectProbeMaterial = nullptr;
mReflectProbeArrayMaterial = nullptr;
}
ProbeManager::~ProbeManager()
@ -624,6 +627,22 @@ ProbeManager::ReflectProbeMaterialInfo* ProbeManager::getReflectProbeMaterial()
return mReflectProbeMaterial;
}
ProbeManager::ReflectionProbeArrayMaterialInfo* ProbeManager::getReflectProbeArrayMaterial()
{
PROFILE_SCOPE(AdvancedLightBinManager_getReflectProbeArrayMaterial);
//ReflectProbeMaterialInfo *info = NULL;
if (!mReflectProbeArrayMaterial)
{
// Now create the material info object.
mReflectProbeArrayMaterial = new ReflectionProbeArrayMaterialInfo("ReflectionProbeArrayMaterial",
getGFXVertexFormat<GFXVertexPC>());
}
return mReflectProbeArrayMaterial;
}
void ProbeManager::setupSkylightProbe(ProbeRenderInst *probeInfo)
{
probeInfo->vertBuffer = getSphereMesh(probeInfo->numPrims, probeInfo->primBuffer);
@ -756,11 +775,19 @@ GFXVertexBufferHandle<GFXVertexPC> ProbeManager::getSphereMesh(U32 &outNumPrimit
//
bool ReflectProbeMatInstance::init(const FeatureSet &features, const GFXVertexFormat *vertexFormat)
{
mShaderMat = nullptr;
bool success = Parent::init(features, vertexFormat);
// If the initialization failed don't continue.
if (!success || !mProcessedMaterial || mProcessedMaterial->getNumPasses() == 0)
return false;
mShaderMat = static_cast<ProcessedShaderMaterial*>(getShaderMaterial());
mShaderMat->init(features, vertexFormat, mFeaturesDelegate);
//mShaderMat->setMaterialParameters(mDefaultParameters, 0);
return true;
}
@ -814,6 +841,41 @@ bool SkylightMatInstance::setupPass(SceneRenderState *state, const SceneData &sg
return true;
}
bool ReflectProbeArrayMatInstance::init(const FeatureSet &features, const GFXVertexFormat *vertexFormat)
{
bool success = Parent::init(features, vertexFormat);
// If the initialization failed don't continue.
if (!success || !mProcessedMaterial || mProcessedMaterial->getNumPasses() == 0)
return false;
return true;
}
bool ReflectProbeArrayMatInstance::setupPass(SceneRenderState *state, const SceneData &sgData)
{
if (!Parent::setupPass(state, sgData))
return false;
AssertFatal(mProcessedMaterial->getNumPasses() > 0, "No passes created! Ohnoes");
const RenderPassData *rpd = mProcessedMaterial->getPass(0);
AssertFatal(rpd, "No render pass data!");
AssertFatal(rpd->mRenderStates[0], "No render state 0!");
if (!mProjectionState)
{
GFXStateBlockDesc desc;
desc.setZReadWrite(false);
desc.zWriteEnable = false;
desc.setCullMode(GFXCullNone);
desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvDestAlpha, GFXBlendOpAdd);
mProjectionState = GFX->createStateBlock(desc);
}
// Now override stateblock with our own
GFX->setStateBlock(mProjectionState);
return true;
}
//
//
ProbeManager::ReflectProbeMaterialInfo::ReflectProbeMaterialInfo(const String &matName,
@ -861,6 +923,8 @@ ProbeManager::ReflectProbeMaterialInfo::ReflectProbeMaterialInfo(const String &m
useSphereMode = matInstance->getMaterialParameterHandle("$useSphereMode");
probeCount = matInstance->getMaterialParameterHandle("$numProbes");
for (U32 i = 0; i < 9; i++)
shTerms[i] = matInstance->getMaterialParameterHandle(String::ToString("$SHTerms%d", i));
@ -1010,6 +1074,41 @@ ProbeManager::SkylightMaterialInfo::~SkylightMaterialInfo()
SAFE_DELETE(matInstance);
}
//
//
ProbeManager::ReflectionProbeArrayMaterialInfo::ReflectionProbeArrayMaterialInfo(const String &matName,
const GFXVertexFormat *vertexFormat)
: ReflectProbeMaterialInfo(matName, vertexFormat)
{
Material *mat = MATMGR->getMaterialDefinitionByName(matName);
if (!mat)
return;
matInstance = new ReflectProbeArrayMatInstance(*mat);
const Vector<GFXShaderMacro> &macros = Vector<GFXShaderMacro>();
for (U32 i = 0; i < macros.size(); i++)
matInstance->addShaderMacro(macros[i].name, macros[i].value);
matInstance->init(MATMGR->getDefaultFeatures(), vertexFormat);
farPlane = matInstance->getMaterialParameterHandle("$farPlane");
vsFarPlane = matInstance->getMaterialParameterHandle("$vsFarPlane");
negFarPlaneDotEye = matInstance->getMaterialParameterHandle("$negFarPlaneDotEye");
zNearFarInvNearFar = matInstance->getMaterialParameterHandle("$zNearFarInvNearFar");
useCubemap = matInstance->getMaterialParameterHandle("$useCubemap");
cubemap = matInstance->getMaterialParameterHandle("$cubeMap");
eyePosWorld = matInstance->getMaterialParameterHandle("$eyePosWorld");
}
ProbeManager::ReflectionProbeArrayMaterialInfo::~ReflectionProbeArrayMaterialInfo()
{
SAFE_DELETE(matInstance);
}
/*bool ProbeManager::lightScene( const char* callback, const char* param )
{
BitSet32 flags = 0;

View file

@ -56,6 +56,8 @@
#include "core/util/SystemInterfaceList.h"
#include "materials/processedShaderMaterial.h"
class SimObject;
class ProbeManager;
class Material;
@ -177,6 +179,7 @@ struct ProbeShaderConstants
GFXShaderConstHandle *mProbeIsSphereSC;
GFXShaderConstHandle *mProbeLocalPosSC;
GFXShaderConstHandle *mProbeCubemapSC;
GFXShaderConstHandle *mProbeCountSC;
ProbeShaderConstants();
~ProbeShaderConstants();
@ -197,11 +200,15 @@ protected:
GFXStateBlockRef mProjectionState;
ProcessedShaderMaterial* mShaderMat;
public:
ReflectProbeMatInstance(Material &mat) : Parent(mat), mProbeParamsSC(NULL), mInternalPass(false), mProjectionState(NULL) {}
virtual bool init(const FeatureSet &features, const GFXVertexFormat *vertexFormat);
virtual bool setupPass(SceneRenderState *state, const SceneData &sgData);
ProcessedShaderMaterial* getProcessedShaderMaterial() { return mShaderMat; }
};
class SkylightMatInstance : public ReflectProbeMatInstance
@ -216,6 +223,22 @@ public:
virtual bool setupPass(SceneRenderState *state, const SceneData &sgData);
};
class ReflectProbeArrayMatInstance : public MatInstance
{
typedef MatInstance Parent;
protected:
MaterialParameterHandle * mProbeParamsSC;
bool mInternalPass;
GFXStateBlockRef mProjectionState;
public:
ReflectProbeArrayMatInstance(Material &mat) : Parent(mat), mProbeParamsSC(NULL), mInternalPass(false), mProjectionState(NULL) {}
virtual bool init(const FeatureSet &features, const GFXVertexFormat *vertexFormat);
virtual bool setupPass(SceneRenderState *state, const SceneData &sgData);
};
class ProbeManager
{
public:
@ -254,6 +277,8 @@ public:
MaterialParameterHandle *shTerms[9];
MaterialParameterHandle *shConsts[5];
MaterialParameterHandle *probeCount;
ReflectProbeMaterialInfo(const String &matName, const GFXVertexFormat *vertexFormat);
virtual ~ReflectProbeMaterialInfo();
@ -275,6 +300,15 @@ public:
virtual ~SkylightMaterialInfo();
};
struct ReflectionProbeArrayMaterialInfo : public ReflectProbeMaterialInfo
{
ReflectionProbeArrayMaterialInfo(const String &matName, const GFXVertexFormat *vertexFormat);
ReflectProbeArrayMatInstance *matInstance;
virtual ~ReflectionProbeArrayMaterialInfo();
};
enum SpecialProbeTypesEnum
{
SkylightProbeType,
@ -353,6 +387,7 @@ public:
ReflectProbeMaterialInfo* getReflectProbeMaterial();
SkylightMaterialInfo* getSkylightMaterial();
ReflectionProbeArrayMaterialInfo* getReflectProbeArrayMaterial();
protected:
@ -415,6 +450,8 @@ protected:
SkylightMaterialInfo* mSkylightMaterial;
ReflectionProbeArrayMaterialInfo* mReflectProbeArrayMaterial;
GFXVertexBufferHandle<GFXVertexPC> getSphereMesh(U32 &outNumPrimitives, GFXPrimitiveBufferHandle &outPrimitives);;
};

View file

@ -266,9 +266,11 @@ protected:
void _setPrimaryLightConst(const LightInfo* light, const MatrixF& objTrans, const U32 stageNum);
/// This is here to deal with the differences between ProcessedCustomMaterials and ProcessedShaderMaterials.
public:
virtual GFXShaderConstBuffer* _getShaderConstBuffer(const U32 pass);
virtual ShaderConstHandles* _getShaderConstHandles(const U32 pass);
protected:
///
virtual void _initMaterialParameters();

View file

@ -33,6 +33,8 @@
#include "gfx/gfxDebugEvent.h"
#include "materials/shaderData.h"
IMPLEMENT_CONOBJECT(RenderProbeMgr);
ConsoleDocClass( RenderProbeMgr,
@ -116,34 +118,43 @@ void RenderProbeMgr::_setupPerFrameParameters(const SceneRenderState *state)
// Now build the quad for drawing full-screen vector light
// passes.... this is a volatile VB and updates every frame.
FarFrustumQuadVert verts[4];
GFXVertexPC verts[4];
{
verts[0].point.set(wsFrustumPoints[Frustum::FarTopLeft] - cameraPos);
invCam.mulP(wsFrustumPoints[Frustum::FarTopLeft], &verts[0].normal);
verts[0].texCoord.set(-1.0, 1.0);
verts[0].tangent.set(wsFrustumPoints[Frustum::FarTopLeft] - cameraOffsetPos);
//invCam.mulP(wsFrustumPoints[Frustum::FarTopLeft], &verts[0].normal);
//verts[0].texCoord.set(-1.0, 1.0);
//verts[0].tangent.set(wsFrustumPoints[Frustum::FarTopLeft] - cameraOffsetPos);
verts[1].point.set(wsFrustumPoints[Frustum::FarTopRight] - cameraPos);
invCam.mulP(wsFrustumPoints[Frustum::FarTopRight], &verts[1].normal);
verts[1].texCoord.set(1.0, 1.0);
verts[1].tangent.set(wsFrustumPoints[Frustum::FarTopRight] - cameraOffsetPos);
// invCam.mulP(wsFrustumPoints[Frustum::FarTopRight], &verts[1].normal);
//verts[1].texCoord.set(1.0, 1.0);
//verts[1].tangent.set(wsFrustumPoints[Frustum::FarTopRight] - cameraOffsetPos);
verts[2].point.set(wsFrustumPoints[Frustum::FarBottomLeft] - cameraPos);
invCam.mulP(wsFrustumPoints[Frustum::FarBottomLeft], &verts[2].normal);
verts[2].texCoord.set(-1.0, -1.0);
verts[2].tangent.set(wsFrustumPoints[Frustum::FarBottomLeft] - cameraOffsetPos);
//invCam.mulP(wsFrustumPoints[Frustum::FarBottomLeft], &verts[2].normal);
// verts[2].texCoord.set(-1.0, -1.0);
// verts[2].tangent.set(wsFrustumPoints[Frustum::FarBottomLeft] - cameraOffsetPos);
verts[3].point.set(wsFrustumPoints[Frustum::FarBottomRight] - cameraPos);
invCam.mulP(wsFrustumPoints[Frustum::FarBottomRight], &verts[3].normal);
verts[3].texCoord.set(1.0, -1.0);
verts[3].tangent.set(wsFrustumPoints[Frustum::FarBottomRight] - cameraOffsetPos);
// invCam.mulP(wsFrustumPoints[Frustum::FarBottomRight], &verts[3].normal);
// verts[3].texCoord.set(1.0, -1.0);
// verts[3].tangent.set(wsFrustumPoints[Frustum::FarBottomRight] - cameraOffsetPos);
}
Point3F norms[4];
{
invCam.mulP(wsFrustumPoints[Frustum::FarTopLeft], &norms[0]);
invCam.mulP(wsFrustumPoints[Frustum::FarTopRight], &norms[1]);
invCam.mulP(wsFrustumPoints[Frustum::FarBottomLeft], &norms[2]);
invCam.mulP(wsFrustumPoints[Frustum::FarBottomRight], &norms[3]);
}
mFarFrustumQuadVerts.set(GFX, 4);
dMemcpy(mFarFrustumQuadVerts.lock(), verts, sizeof(verts));
mFarFrustumQuadVerts.unlock();
PlaneF farPlane(wsFrustumPoints[Frustum::FarBottomLeft], wsFrustumPoints[Frustum::FarTopLeft], wsFrustumPoints[Frustum::FarTopRight]);
PlaneF vsFarPlane(verts[0].normal, verts[1].normal, verts[2].normal);
PlaneF vsFarPlane(norms[0], norms[1], norms[2]);
// Parameters calculated, assign them to the materials
@ -168,6 +179,17 @@ void RenderProbeMgr::_setupPerFrameParameters(const SceneRenderState *state)
farPlane,
vsFarPlane);
}
ProbeManager::ReflectionProbeArrayMaterialInfo* reflProbeArrayMat = PROBEMGR->getReflectProbeArrayMaterial();
if (reflProbeArrayMat != nullptr && reflProbeArrayMat->matInstance != nullptr)
{
reflProbeArrayMat->setViewParameters(frustum.getNearDist(),
frustum.getFarDist(),
frustum.getPosition(),
farPlane,
vsFarPlane);
}
}
//-----------------------------------------------------------------------------
@ -230,7 +252,7 @@ void RenderProbeMgr::render( SceneRenderState *state )
ProbeManager::SkylightMaterialInfo* skylightMat = PROBEMGR->getSkylightMaterial();
ProbeManager::ReflectProbeMaterialInfo* reflProbeMat = PROBEMGR->getReflectProbeMaterial();
for (U32 i = 0; i < ProbeRenderInst::all.size(); i++)
/*for (U32 i = 0; i < ProbeRenderInst::all.size(); i++)
{
ProbeRenderInst* curEntry = ProbeRenderInst::all[i];
@ -268,54 +290,155 @@ void RenderProbeMgr::render( SceneRenderState *state )
GFX->drawPrimitive(GFXTriangleList, 0, curEntry->numPrims);
}
}
}*/
//Array rendering
static U32 MAXPROBECOUNT = 50;
U32 probeCount = PROBEMGR->mRegisteredProbes.size();
if (probeCount != 0)
{
AlignedArray<Point3F> probePositions(MAXPROBECOUNT, sizeof(Point3F));
dMemset(probePositions.getBuffer(), 0, probePositions.getBufferSize());
if (reflProbeMat && reflProbeMat->matInstance)
{
MaterialParameters *matParams = reflProbeMat->matInstance->getMaterialParameters();
MaterialParameterHandle *numProbesSC = reflProbeMat->matInstance->getMaterialParameterHandle("$numProbes");
matParams->setSafe(numProbesSC, (float)probeCount);
//ProcessedShaderMaterial* processedMat = reflProbeMat->matInstance->getProcessedShaderMaterial();
//GFXShaderConstBuffer* shaderConsts = processedMat->_getShaderConstBuffer(0);
//ProbeShaderConstants *psc = PROBEMGR->getProbeShaderConstants(shaderConsts);
MaterialParameterHandle *probePositionSC = reflProbeMat->matInstance->getMaterialParameterHandle("$inProbePosArray");
U32 effectiveProbeCount = 0;
for (U32 i = 0; i < probeCount; i++)
{
if (effectiveProbeCount >= MAXPROBECOUNT)
break;
ProbeRenderInst* curEntry = ProbeRenderInst::all[PROBEMGR->mRegisteredProbes[i]];
/*if (!curEntry->mIsEnabled)
continue;
if (curEntry->numPrims == 0)
continue;
if (curEntry->mIsSkylight && (!skylightMat || !skylightMat->matInstance))
continue;
if (!curEntry->mIsSkylight && (!reflProbeMat || !reflProbeMat->matInstance))
break;*/
//Setup
const Point3F &probePos = curEntry->getPosition();
probePositions[i].x = probePos.x;
probePositions[i].y = probePos.y;
probePositions[i].z = probePos.z;
Point3F test = probePositions[i];
MatrixF probeTrans = curEntry->getTransform();
if (!curEntry->mIsSkylight)
{
//if (curEntry->mProbeShapeType == ProbeRenderInst::Sphere)
// probeTrans.scale(curEntry->mRadius * 1.01f);
sgData.objTrans = &probeTrans;
reflProbeMat->setProbeParameters(curEntry, state, worldToCameraXfm);
}
effectiveProbeCount++;
}
if (effectiveProbeCount != 0)
{
Con::printf("Probe aligned position count: %i", probeCount);
for (U32 p = 0; p < probeCount; p++)
{
Point3F prb = probePositions[p];
Con::printf("Probe %i aligned position is: %g %g %g", p, prb.x, prb.y, prb.z);
bool tasadfh = true;
}
matParams->set(probePositionSC, probePositions);
// Set geometry
GFX->setVertexBuffer(mFarFrustumQuadVerts);
GFX->setPrimitiveBuffer(NULL);
while (reflProbeMat->matInstance->setupPass(state, sgData))
{
// Set transforms
matrixSet.setWorld(*sgData.objTrans);
reflProbeMat->matInstance->setTransforms(matrixSet, state);
reflProbeMat->matInstance->setSceneInfo(state, sgData);
GFX->drawPrimitive(GFXTriangleStrip, 0, 2);
}
}
}
}
//
//
/*ProbeManager::ReflectionProbeArrayMaterialInfo* reflProbeArrayMat = PROBEMGR->getReflectProbeArrayMaterial();
for (U32 i = 0; i < ProbeRenderInst::all.size(); i++)
{
ProbeRenderInst* curEntry = ProbeRenderInst::all[i];
if (i > 0)
return;
if (!curEntry->mIsEnabled)
continue;
ProbeRenderInst* curEntry = ProbeRenderInst::all[i];
if (curEntry->numPrims == 0)
continue;
if (!reflProbeArrayMat || !reflProbeArrayMat->matInstance)
break;
if (curEntry->mIsSkylight && (!skylightMat || !skylightMat->matInstance))
continue;
//Setup
//MatrixF probeTrans = curEntry->getTransform();
if (!curEntry->mIsSkylight && (!reflProbeMat || !reflProbeMat->matInstance))
break;
//if (!curEntry->mIsSkylight)
{
//if (curEntry->mProbeShapeType == ProbeRenderInst::Sphere)
// probeTrans.scale(curEntry->mRadius * 1.01f);
//Setup
MatrixF probeTrans = curEntry->getTransform();
if (!curEntry->mIsSkylight)
{
if (curEntry->mProbeShapeType == ProbeRenderInst::Sphere)
probeTrans.scale(curEntry->mRadius * 1.01f);
//sgData.objTrans = &state-;
sgData.objTrans = &probeTrans;
reflProbeArrayMat->setProbeParameters(curEntry, state, worldToCameraXfm);
reflProbeMat->setProbeParameters(curEntry, state, worldToCameraXfm);
// Set geometry
GFX->setVertexBuffer(mFarFrustumQuadVerts);
GFX->setPrimitiveBuffer(NULL);
while (reflProbeArrayMat->matInstance->setupPass(state, sgData))
{
// Set transforms
//matrixSet.setWorld(*sgData.objTrans);
reflProbeArrayMat->matInstance->setTransforms(matrixSet, state);
reflProbeArrayMat->matInstance->setSceneInfo(state, sgData);
// Set geometry
GFX->setVertexBuffer(curEntry->vertBuffer);
GFX->setPrimitiveBuffer(curEntry->primBuffer);
while (reflProbeMat->matInstance->setupPass(state, sgData))
{
// Set transforms
matrixSet.setWorld(*sgData.objTrans);
reflProbeMat->matInstance->setTransforms(matrixSet, state);
reflProbeMat->matInstance->setSceneInfo(state, sgData);
GFX->drawPrimitive(GFXTriangleList, 0, curEntry->numPrims);
}
}
}
GFX->drawPrimitive(GFXTriangleStrip, 0, 2);
}
}
}*/
//
//
GFX->popActiveRenderTarget();
//PROBEMGR->unregisterAllProbes();
PROBEMGR->mRegisteredProbes.clear();
PROFILE_END();
GFX->setVertexBuffer(NULL);

View file

@ -38,18 +38,18 @@
#include "gfx/gfxVertexBuffer.h"
#endif
#include "postFx/postEffectCommon.h"
//**************************************************************************
// RenderObjectMgr
//**************************************************************************
class RenderProbeMgr : public RenderBinManager
{
typedef RenderBinManager Parent;
public:
typedef GFXVertexPNTT FarFrustumQuadVert;
protected:
GFXVertexBufferHandle<FarFrustumQuadVert> mFarFrustumQuadVerts;
GFXVertexBufferHandle<GFXVertexPC> mFarFrustumQuadVerts;
public:
RenderProbeMgr();

View file

@ -77,6 +77,7 @@ const String ShaderGenVars::probeBoxMax("$inProbeBoxMax");
const String ShaderGenVars::probeLocalPos("$inProbeLocalPos");
const String ShaderGenVars::probeIsSphere("$inProbeIsSphere");
const String ShaderGenVars::probeCubemap("$inProbeCubemap");
const String ShaderGenVars::probeCount("$numProbes");
// These are ignored by the D3D layers.
const String ShaderGenVars::fogMap("$fogMap");

View file

@ -90,6 +90,7 @@ struct ShaderGenVars
const static String probeLocalPos;
const static String probeIsSphere;
const static String probeCubemap;
const static String probeCount;
// Textures
const static String fogMap;