Merge pull request #1334 from marauder2k9-torque/CubemapBakingBug

Cubemap Baking DX11
This commit is contained in:
Brian Roberts 2024-12-04 16:53:27 -06:00 committed by GitHub
commit 925b73309e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 94 additions and 72 deletions

View file

@ -231,7 +231,7 @@ void GFXD3D11Cubemap::initDynamic(U32 texSize, GFXFormat faceFormat, U32 mipLeve
miscFlags |= D3D11_RESOURCE_MISC_GENERATE_MIPS;
}
D3D11_TEXTURE2D_DESC desc;
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = mTexSize;
desc.Height = mTexSize;
@ -248,7 +248,7 @@ void GFXD3D11Cubemap::initDynamic(U32 texSize, GFXFormat faceFormat, U32 mipLeve
HRESULT hr = D3D11DEVICE->CreateTexture2D(&desc, NULL, &mTexture);
D3D11_SHADER_RESOURCE_VIEW_DESC SMViewDesc;
D3D11_SHADER_RESOURCE_VIEW_DESC SMViewDesc = {};
SMViewDesc.Format = GFXD3D11TextureFormat[mFaceFormat];
SMViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
SMViewDesc.TextureCube.MipLevels = -1;
@ -272,13 +272,13 @@ void GFXD3D11Cubemap::initDynamic(U32 texSize, GFXFormat faceFormat, U32 mipLeve
AssertFatal(false, "GFXD3D11Cubemap::initDynamic - CreateTexture2D call failure");
}
D3D11_RENDER_TARGET_VIEW_DESC viewDesc;
viewDesc.Format = desc.Format;
viewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
viewDesc.Texture2DArray.ArraySize = 1;
viewDesc.Texture2DArray.MipSlice = 0;
for (U32 i = 0; i < CubeFaces; i++)
{
D3D11_RENDER_TARGET_VIEW_DESC viewDesc = {};
viewDesc.Format = desc.Format;
viewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
viewDesc.Texture2DArray.ArraySize = 1;
viewDesc.Texture2DArray.MipSlice = 0;
viewDesc.Texture2DArray.FirstArraySlice = i;
hr = D3D11DEVICE->CreateRenderTargetView(mTexture, &viewDesc, &mRTView[i]);

View file

@ -190,12 +190,12 @@ void GFXD3D11TextureTarget::attachTexture( RenderSlot slot, GFXCubemap *tex, U32
// Mark state as dirty so device can know to update.
invalidateState();
// Release what we had, it's definitely going to change.
SAFE_RELEASE(mTargetViews[slot]);
SAFE_RELEASE(mTargets[slot]);
SAFE_RELEASE(mTargetSRViews[slot]);
mResolveTargets[slot] = NULL;
mGenMips = false;
// Cast the texture object to D3D...
AssertFatal(!tex || static_cast<GFXD3D11Cubemap*>(tex), "GFXD3DTextureTarget::attachTexture - invalid cubemap object.");
@ -214,14 +214,14 @@ void GFXD3D11TextureTarget::attachTexture( RenderSlot slot, GFXCubemap *tex, U32
}
GFXD3D11Cubemap *cube = static_cast<GFXD3D11Cubemap*>(tex);
// Grab the surface level.
mTargets[slot] = cube->get2DTex();
mTargets[slot]->AddRef();
mTargetViews[slot] = cube->getRTView(face);
mTargetViews[slot]->AddRef();
mTargetSRViews[slot] = cube->getSRView();
mTargetSRViews[slot]->AddRef();
/*mTargetSRViews[slot] = cube->getSRView();
mTargetSRViews[slot]->AddRef();*/
// Update surface size
if(slot == Color0)
{

View file

@ -45,19 +45,7 @@ GFXCubemap::~GFXCubemap()
U32 GFXCubemap::zUpFaceIndex(const U32 index)
{
switch (index)
{
case 2:
return 4;
case 3:
return 5;
case 4:
return 2;
case 5:
return 3;
default:
return index;
};
return index;
}
void GFXCubemap::initNormalize( U32 size )

View file

@ -235,6 +235,14 @@ public:
MatrixF& add( const MatrixF& m );
/// <summary>
/// Turns this matrix into a view matrix that looks at target.
/// </summary>
/// <param name="eye">The eye position.</param>
/// <param name="target">The target position/direction.</param>
/// <param name="up">The up direction.</param>
void LookAt(const VectorF& eye, const VectorF& target, const VectorF& up);
/// Convenience function to allow people to treat this like an array.
F32& operator ()(S32 row, S32 col) { return m[idx(col,row)]; }
F32 operator ()(S32 row, S32 col) const { return m[idx(col,row)]; }
@ -498,6 +506,35 @@ inline MatrixF& MatrixF::add( const MatrixF& a )
return *this;
}
inline void MatrixF::LookAt(const VectorF& eye, const VectorF& target, const VectorF& up)
{
VectorF yAxis = target - eye; // Forward
yAxis.normalize();
VectorF xAxis = mCross(up, yAxis); // Right
xAxis.normalize();
VectorF zAxis = mCross(yAxis, xAxis); // Up
// Right vector.
setColumn(0, xAxis);
m[12] = -mDot(xAxis, eye);
// Forward vector.
setColumn(1, yAxis);
m[13] = -mDot(yAxis, eye);
// Up vector.
setColumn(2, zAxis);
m[14] = -mDot(zAxis, eye);
m[3] = 0.0f;
m[7] = 0.0f;
m[11] = 0.0f;
m[15] = 1.0f;
}
inline void MatrixF::getColumn(S32 col, Point4F *cptr) const
{
cptr->x = m[col];

View file

@ -622,6 +622,7 @@ void RenderProbeMgr::bakeProbe(ReflectionProbe* probe)
IBLUtilities::GenerateIrradianceMap(renderTarget, cubeRefl.getCubemap(), clientProbe->mIrridianceMap->mCubemap);
//IBLUtilities::GeneratePrefilterMap(renderTarget, cubeRefl.getCubemap(), prefilterMipLevels, clientProbe->mPrefilterMap->mCubemap);
clientProbe->mIrridianceMap->mCubemap->generateMipMaps();
U32 endMSTime = Platform::getRealMilliseconds();
F32 diffTime = F32(endMSTime - startMSTime);

View file

@ -320,24 +320,38 @@ void CubeReflector::updateReflection( const ReflectParams &params, Point3F expli
mCubemap->getFormat() != reflectFormat )
{
mCubemap = GFX->createCubemap();
mCubemap->initDynamic( texDim, reflectFormat );
mCubemap->initDynamic( texDim, reflectFormat);
}
mDepthBuff = LightShadowMap::_getDepthTarget( texDim, texDim );
if ( mRenderTarget.isNull() )
mRenderTarget = GFX->allocRenderToTextureTarget();
GFX->pushActiveRenderTarget();
mRenderTarget->attachTexture( GFXTextureTarget::DepthStencil, mDepthBuff );
mDepthBuff = LightShadowMap::_getDepthTarget(texDim, texDim);
mRenderTarget->attachTexture(GFXTextureTarget::DepthStencil, mDepthBuff);
F32 oldVisibleDist = gClientSceneGraph->getVisibleDistance();
gClientSceneGraph->setVisibleDistance( mDesc->farDist );
// store current matrices
GFXTransformSaver saver;
for ( U32 i = 0; i < 6; i++ )
updateFace( params, i, explicitPostion);
F32 detailAdjustBackup = TSShapeInstance::smDetailAdjust;
TSShapeInstance::smDetailAdjust *= mDesc->detailAdjust;
// set projection to 90 degrees vertical and horizontal
F32 left, right, top, bottom;
MathUtils::makeFrustum(&left, &right, &top, &bottom, M_HALFPI_F, 1.0f, mDesc->nearDist);
GFX->setFrustum(left, right, bottom, top, mDesc->nearDist, mDesc->farDist);
// We don't use a special clipping projection, but still need to initialize
// this for objects like SkyBox which will use it during a reflect pass.
gClientSceneGraph->setNonClipProjection(GFX->getProjectionMatrix());
for (S32 i = 5; i >= 0; i--) {
updateFace(params, i, explicitPostion);
}
TSShapeInstance::smDetailAdjust = detailAdjustBackup;
mCubemap->generateMipMaps();
@ -353,76 +367,59 @@ void CubeReflector::updateFace( const ReflectParams &params, U32 faceidx, Point3
{
GFXDEBUGEVENT_SCOPE( CubeReflector_UpdateFace, ColorI::WHITE );
// store current matrices
GFXTransformSaver saver;
F32 detailAdjustBackup = TSShapeInstance::smDetailAdjust;
TSShapeInstance::smDetailAdjust *= mDesc->detailAdjust;
// set projection to 90 degrees vertical and horizontal
F32 left, right, top, bottom;
MathUtils::makeFrustum( &left, &right, &top, &bottom, M_HALFPI_F, 1.0f, mDesc->nearDist );
GFX->setFrustum( left, right, bottom, top, mDesc->nearDist, mDesc->farDist );
// We don't use a special clipping projection, but still need to initialize
// this for objects like SkyBox which will use it during a reflect pass.
gClientSceneGraph->setNonClipProjection( GFX->getProjectionMatrix() );
// Standard view that will be overridden below.
VectorF vLookatPt(0.0f, 0.0f, 0.0f), vUpVec(0.0f, 0.0f, 0.0f), vRight(0.0f, 0.0f, 0.0f);
VectorF target = VectorF::Zero;
VectorF eye = VectorF::Zero;
if (explicitPostion == Point3F::Max)
{
eye = mObject->getPosition();
}
else
{
eye = explicitPostion;
}
VectorF vUpVec(0.0f, 0.0f, 0.0f);
switch( faceidx )
{
case 0 : // D3DCUBEMAP_FACE_POSITIVE_X:
vLookatPt = VectorF( 1.0f, 0.0f, 0.0f );
target = eye + VectorF( 1.0f, 0.0f, 0.0f );
vUpVec = VectorF( 0.0f, 1.0f, 0.0f );
break;
case 1 : // D3DCUBEMAP_FACE_NEGATIVE_X:
vLookatPt = VectorF( -1.0f, 0.0f, 0.0f );
target = eye + VectorF( -1.0f, 0.0f, 0.0f );
vUpVec = VectorF( 0.0f, 1.0f, 0.0f );
break;
case 2 : // D3DCUBEMAP_FACE_POSITIVE_Y:
vLookatPt = VectorF( 0.0f, 1.0f, 0.0f );
target = eye + VectorF( 0.0f, 1.0f, 0.0f );
vUpVec = VectorF( 0.0f, 0.0f,-1.0f );
break;
case 3 : // D3DCUBEMAP_FACE_NEGATIVE_Y:
vLookatPt = VectorF( 0.0f, -1.0f, 0.0f );
target = eye + VectorF( 0.0f, -1.0f, 0.0f );
vUpVec = VectorF( 0.0f, 0.0f, 1.0f );
break;
case 4 : // D3DCUBEMAP_FACE_POSITIVE_Z:
vLookatPt = VectorF( 0.0f, 0.0f, 1.0f );
target = eye + VectorF( 0.0f, 0.0f, 1.0f );
vUpVec = VectorF( 0.0f, 1.0f, 0.0f );
break;
case 5: // D3DCUBEMAP_FACE_NEGATIVE_Z:
vLookatPt = VectorF( 0.0f, 0.0f, -1.0f );
target = eye + VectorF( 0.0f, 0.0f, -1.0f );
vUpVec = VectorF( 0.0f, 1.0f, 0.0f );
break;
}
// create camera matrix
VectorF cross = mCross( vUpVec, vLookatPt );
cross.normalizeSafe();
MatrixF matView(true);
matView.setColumn( 0, cross );
matView.setColumn( 1, vLookatPt );
matView.setColumn( 2, vUpVec );
if (explicitPostion == Point3F::Max)
{
matView.setPosition(mObject->getPosition());
}
else
{
matView.setPosition(explicitPostion);
}
matView.LookAt(eye, target, vUpVec);
matView.inverse();
GFX->setWorldMatrix(matView);
GFX->clearTextureStateImmediate(0);
mRenderTarget->attachTexture( GFXTextureTarget::Color0, mCubemap, faceidx );
GFX->setActiveRenderTarget(mRenderTarget);
GFX->clear( GFXClearStencil | GFXClearTarget | GFXClearZBuffer, gCanvasClearColor, 0.0f, 0 );
GFX->clear( GFXClearStencil | GFXClearTarget | GFXClearZBuffer, gCanvasClearColor, 0.0f, 0);
SceneRenderState reflectRenderState
(
@ -441,7 +438,6 @@ void CubeReflector::updateFace( const ReflectParams &params, U32 faceidx, Point3
// Clean up.
mRenderTarget->resolve();
TSShapeInstance::smDetailAdjust = detailAdjustBackup;
}
F32 CubeReflector::calcFaceScore( const ReflectParams &params, U32 faceidx )