timmy merge work

This commit is contained in:
Tim Barnes 2018-11-28 17:51:52 +10:00
parent f1e584ca69
commit ab10cc0c87
29 changed files with 295 additions and 925 deletions

View file

@ -1,3 +1,26 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "T3D/lighting/IBLUtilities.h"
#include "console/engineAPI.h"
#include "materials/shaderData.h"
#include "gfx/gfxTextureManager.h"
@ -167,62 +190,7 @@ namespace IBLUtilities
{
Con::errorf("IBLUtilities::GenerateAndSavePrefilterMap - Failed to properly save out the baked irradiance!");
}
}
void GenerateBRDFTexture(GFXTexHandle &textureOut)
{
GFXTransformSaver saver;
ShaderData *brdfShaderData;
GFXShaderRef brdfShader = Sim::findObject("BRDFLookupShader", brdfShaderData) ? brdfShaderData->getShader() : NULL;
if (!brdfShader)
{
Con::errorf("IBLUtilities::GenerateBRDFTexture() - could not find BRDFLookupShader");
return;
}
U32 textureSize = textureOut->getWidth();
GFXTextureTargetRef renderTarget = GFX->allocRenderToTextureTarget();
GFX->pushActiveRenderTarget();
GFX->setShader(brdfShader);
renderTarget->attachTexture(GFXTextureTarget::Color0, textureOut);
GFX->setActiveRenderTarget(renderTarget);//potential bug here with the viewport not updating with the new size
GFX->setViewport(RectI(0, 0, textureSize, textureSize));//see above comment
GFX->clear(GFXClearTarget, LinearColorF::BLUE, 1.0f, 0);
GFX->drawPrimitive(GFXTriangleList, 0, 3);
renderTarget->resolve();
GFX->popActiveRenderTarget();
}
GFXTexHandle GenerateAndSaveBRDFTexture(String outputPath, S32 resolution)
{
GFXTexHandle brdfTexture = TEXMGR->createTexture(resolution, resolution, GFXFormatR8G8B8A8, &GFXRenderTargetProfile, 1, 0);
GenerateBRDFTexture(brdfTexture);
FileStream fs;
if (fs.open(outputPath, Torque::FS::File::Write))
{
// Read back the render target, dxt compress it, and write it to disk.
GBitmap brdfBmp(brdfTexture.getHeight(), brdfTexture.getWidth(), false, GFXFormatR8G8B8A8);
brdfTexture.copyToBmp(&brdfBmp);
brdfBmp.extrudeMipLevels();
DDSFile *brdfDDS = DDSFile::createDDSFileFromGBitmap(&brdfBmp);
ImageUtil::ddsCompress(brdfDDS, GFXFormatBC1);
// Write result to file stream
brdfDDS->write(fs);
delete brdfDDS;
}
fs.close();
return brdfTexture;
}
}
void bakeReflection(String outputPath, S32 resolution)
{
@ -662,9 +630,3 @@ namespace IBLUtilities
return angle;
}
};
DefineEngineFunction(GenerateBRDFTexture, bool, (String outputPath, S32 resolution), ("", 256),
"@brief returns true if control object is inside the fog\n\n.")
{
return IBLUtilities::GenerateAndSaveBRDFTexture(outputPath, resolution);
}

View file

@ -1,4 +1,40 @@
#pragma once
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef IBL_UTILS_H_
#define IBL_UTILS_H_
#ifndef _GFXTARGET_H_
#include "gfx/gfxTarget.h"
#endif
#ifndef _GFXCUBEMAP_H_
#include "gfx/gfxCubemap.h"
#endif
#ifndef _COLOR_H_
#include "core/color.h"
#endif
namespace IBLUtilities
{
@ -10,9 +46,6 @@ namespace IBLUtilities
void SaveCubeMap(String outputPath, GFXCubemapHandle &cubemap);
GFXTexHandle GenerateAndSaveBRDFTexture(String outputPath, S32 resolution);
void GenerateBRDFTexture(GFXTexHandle &textureOut);
void bakeReflection(String outputPath, S32 resolution);
LinearColorF decodeSH(Point3F normal, const LinearColorF SHTerms[9], const F32 SHConstants[5]);
@ -32,4 +65,6 @@ namespace IBLUtilities
F32 texelSolidAngle(F32 aU, F32 aV, U32 width, U32 height);
F32 areaElement(F32 x, F32 y);
};
};
#endif

View file

@ -36,7 +36,6 @@
#include "core/fileObject.h"
#include "core/resourceManager.h"
#include "console/simPersistId.h"
#include <string>
#include "T3D/gameFunctions.h"
#include "postFx/postEffect.h"
#include "renderInstance/renderProbeMgr.h"
@ -620,16 +619,13 @@ bool ReflectionProbe::createClientResources()
Con::errorf("ReflectionProbe::createClientResources() - Unable to load baked prefilter map at %s", getPrefilterMapPath().c_str());
}
//brdf lookup resources
//make the brdf lookup texture the same size as the prefilter texture
//brdf lookup texture
String brdfPath = Con::getVariable("$Core::BRDFTexture", "core/art/pbr/brdfTexture.dds");
mBrdfTexture = TEXMGR->createTexture(brdfPath, &GFXTexturePersistentProfile);
if (!mBrdfTexture)
{
mBrdfTexture = IBLUtilities::GenerateAndSaveBRDFTexture(brdfPath, 512);
return false;
}
mResourcesCreated = true;
@ -652,8 +648,8 @@ void ReflectionProbe::prepRenderImage(SceneRenderState *state)
//Culling distance. Can be adjusted for performance options considerations via the scalar
if (dist > mMaxDrawDistance * Con::getFloatVariable("$pref::GI::ProbeDrawDistScale", 1.0))
{
mProbeInfo->mScore = mMaxDrawDistance;
return;
mProbeInfo->mScore = mMaxDrawDistance;
return;
}
if (mReflectionModeType == DynamicCubemap && mRefreshRateMS < (Platform::getRealMilliseconds() - mDynamicLastBakeMS))
@ -685,7 +681,7 @@ void ReflectionProbe::prepRenderImage(SceneRenderState *state)
Point3F cameraOffset;
getRenderTransform().getColumn(3, &cameraOffset);
cameraOffset -= state->getDiffuseCameraPosition();
F32 dist = cameraOffset.len();
dist = cameraOffset.len();
if (dist < 0.01f)
dist = 0.01f;
@ -832,7 +828,7 @@ String ReflectionProbe::getPrefilterMapPath()
}
char fileName[256];
dSprintf(fileName, 256, "%s%s_Prefilter.DDS", mReflectionPath.c_str(), mProbeUniqueID.c_str());
dSprintf(fileName, 256, "%s%s_Prefilter.dds", mReflectionPath.c_str(), mProbeUniqueID.c_str());
return fileName;
}
@ -846,7 +842,7 @@ String ReflectionProbe::getIrradianceMapPath()
}
char fileName[256];
dSprintf(fileName, 256, "%s%s_Irradiance.DDS", mReflectionPath.c_str(), mProbeUniqueID.c_str());
dSprintf(fileName, 256, "%s%s_Irradiance.dds", mReflectionPath.c_str(), mProbeUniqueID.c_str());
return fileName;
}

View file

@ -36,7 +36,6 @@
#include "core/fileObject.h"
#include "core/resourceManager.h"
#include "console/simPersistId.h"
#include <string>
#include "T3D/gameFunctions.h"
#include "postFx/postEffect.h"
#include "renderInstance/renderProbeMgr.h"
@ -183,9 +182,6 @@ void Skylight::prepRenderImage(SceneRenderState *state)
if (!mEnabled || !Skylight::smRenderSkylights)
return;
Point3F distVec = getPosition() - state->getCameraPosition();
F32 dist = distVec.len();
//special hook-in for skylights
Point3F camPos = state->getCameraPosition();
mProbeInfo->mBounds.setCenter(camPos);

View file

@ -177,7 +177,7 @@ void GFXD3D11Cubemap::initStatic(DDSFile *dds)
continue;
// convert to Z up
const U32 faceIndex = _zUpFaceIndex(currentFace);
const U32 faceIndex = zUpFaceIndex(currentFace);
for(U32 currentMip = 0; currentMip < mMipMapLevels; currentMip++)
{

View file

@ -909,6 +909,25 @@ void GFXD3D11Device::setShaderConstBufferInternal(GFXShaderConstBuffer* buffer)
//-----------------------------------------------------------------------------
void GFXD3D11Device::copyResource(GFXTextureObject *pDst, GFXCubemap *pSrc, const U32 face)
{
AssertFatal(pDst, "GFXD3D11Device::copyResource: Destination texture is null");
AssertFatal(pSrc, "GFXD3D11Device::copyResource: Source cubemap is null");
GFXD3D11TextureObject *pD3DDst = static_cast<GFXD3D11TextureObject*>(pDst);
GFXD3D11Cubemap *pD3DSrc = static_cast<GFXD3D11Cubemap*>(pSrc);
const U32 mipLevels = pD3DSrc->getMipMapLevels();
for (U32 mip = 0; mip < mipLevels; mip++)
{
const U32 srcSubResource = D3D11CalcSubresource(mip, face, mipLevels);
const U32 dstSubResource = D3D11CalcSubresource(mip, 0, mipLevels);
mD3DDeviceContext->CopySubresourceRegion(pD3DDst->get2DTex(), dstSubResource, 0, 0, 0, pD3DSrc->get2DTex(), srcSubResource, NULL);
}
}
//-----------------------------------------------------------------------------
void GFXD3D11Device::clear(U32 flags, const LinearColorF& color, F32 z, U32 stencil)
{
// Make sure we have flushed our render target state.

View file

@ -239,6 +239,11 @@ public:
virtual U32 getNumRenderTargets() const { return 8; }
// }
// Copy methods
// {
virtual void copyResource(GFXTextureObject *pDst, GFXCubemap *pSrc, const U32 face);
// }
// Misc rendering control
// {
virtual void clear( U32 flags, const LinearColorF& color, F32 z, U32 stencil );

View file

@ -185,8 +185,7 @@ bool GFXD3D11TextureObject::copyToBmp(GBitmap* bmp)
AssertFatal(bmp->getWidth() == getWidth(), "GFXD3D11TextureObject::copyToBmp - source/dest width does not match");
AssertFatal(bmp->getHeight() == getHeight(), "GFXD3D11TextureObject::copyToBmp - source/dest height does not match");
const U32 width = getWidth();
const U32 height = getHeight();
const U32 mipLevels = getMipLevels();
bmp->setHasTransparency(mHasTransparency);
@ -221,52 +220,58 @@ bool GFXD3D11TextureObject::copyToBmp(GBitmap* bmp)
//copy the classes texture to the staging texture
D3D11DEVICECONTEXT->CopyResource(pStagingTexture, mD3DTexture);
//map the staging resource
D3D11_MAPPED_SUBRESOURCE mappedRes;
hr = D3D11DEVICECONTEXT->Map(pStagingTexture, 0, D3D11_MAP_READ, 0, &mappedRes);
if (FAILED(hr))
for (U32 mip = 0; mip < mipLevels; mip++)
{
//cleanup
SAFE_RELEASE(pStagingTexture);
Con::errorf("GFXD3D11TextureObject::copyToBmp - Failed to map staging texture");
return false;
}
// set pointers
const U8* srcPtr = (U8*)mappedRes.pData;
U8* destPtr = bmp->getWritableBits();
// we will want to skip over any D3D cache data in the source texture
const S32 sourceCacheSize = mappedRes.RowPitch - width * sourceBytesPerPixel;
AssertFatal(sourceCacheSize >= 0, "GFXD3D11TextureObject::copyToBmp - cache size is less than zero?");
// copy data into bitmap
for (U32 row = 0; row < height; ++row)
{
for (U32 col = 0; col < width; ++col)
const U32 width = bmp->getWidth(mip);
const U32 height = bmp->getHeight(mip);
//map the staging resource
D3D11_MAPPED_SUBRESOURCE mappedRes;
const U32 subResource = D3D11CalcSubresource(mip, 0, mipLevels);
hr = D3D11DEVICECONTEXT->Map(pStagingTexture, subResource, D3D11_MAP_READ, 0, &mappedRes);
if (FAILED(hr))
{
destPtr[0] = srcPtr[2]; // red
destPtr[1] = srcPtr[1]; // green
destPtr[2] = srcPtr[0]; // blue
if (destBytesPerPixel == 4)
destPtr[3] = srcPtr[3]; // alpha
// go to next pixel in src
srcPtr += sourceBytesPerPixel;
// go to next pixel in dest
destPtr += destBytesPerPixel;
//cleanup
SAFE_RELEASE(pStagingTexture);
Con::errorf("GFXD3D11TextureObject::copyToBmp - Failed to map staging texture");
return false;
}
// skip past the cache data for this row (if any)
srcPtr += sourceCacheSize;
// set pointers
const U8* srcPtr = (U8*)mappedRes.pData;
U8* destPtr = bmp->getWritableBits(mip);
// we will want to skip over any D3D cache data in the source texture
const S32 sourceCacheSize = mappedRes.RowPitch - width * sourceBytesPerPixel;
AssertFatal(sourceCacheSize >= 0, "GFXD3D11TextureObject::copyToBmp - cache size is less than zero?");
// copy data into bitmap
for (U32 row = 0; row < height; ++row)
{
for (U32 col = 0; col < width; ++col)
{
destPtr[0] = srcPtr[2]; // red
destPtr[1] = srcPtr[1]; // green
destPtr[2] = srcPtr[0]; // blue
if (destBytesPerPixel == 4)
destPtr[3] = srcPtr[3]; // alpha
// go to next pixel in src
srcPtr += sourceBytesPerPixel;
// go to next pixel in dest
destPtr += destBytesPerPixel;
}
// skip past the cache data for this row (if any)
srcPtr += sourceCacheSize;
}
// assert if we stomped or underran memory
AssertFatal(U32(destPtr - bmp->getWritableBits(mip)) == width * height * destBytesPerPixel, "GFXD3D11TextureObject::copyToBmp - memory error");
AssertFatal(U32(srcPtr - (U8*)mappedRes.pData) == height * mappedRes.RowPitch, "GFXD3D11TextureObject::copyToBmp - memory error");
D3D11DEVICECONTEXT->Unmap(pStagingTexture, subResource);
}
// assert if we stomped or underran memory
AssertFatal(U32(destPtr - bmp->getWritableBits()) == width * height * destBytesPerPixel, "GFXD3D11TextureObject::copyToBmp - memory error");
AssertFatal(U32(srcPtr - (U8*)mappedRes.pData) == height * mappedRes.RowPitch, "GFXD3D11TextureObject::copyToBmp - memory error");
D3D11DEVICECONTEXT->Unmap(pStagingTexture, 0);
SAFE_RELEASE(pStagingTexture);
PROFILE_END();

View file

@ -150,7 +150,7 @@ public:
virtual GFXShader* createShader() { return NULL; };
virtual void copyResource(GFXTextureObject *pDst, GFXCubemap *pSrc, const U32 face) { };
virtual void clear( U32 flags, const LinearColorF& color, F32 z, U32 stencil ) { };
virtual void clearColorAttachment(const U32 attachment, const LinearColorF& color) { };
virtual bool beginSceneInternal() { return true; };

View file

@ -32,29 +32,10 @@
#include "math/mathUtils.h"
#include "math/mTransform.h"
namespace CubemapSaver
{
const U32 CubeFaces = 6;
void _setConstBuffer(GFXShaderConstHandle* handle, GFXShaderConstBuffer *cbuf, const VectorF &vLookatPt, const VectorF &vUpVec)
{
VectorF cross = mCross(vUpVec, vLookatPt);
cross.normalizeSafe();
MatrixF matView(true);
matView.setColumn(0, cross);
matView.setColumn(1, vLookatPt);
matView.setColumn(2, vUpVec);
matView.setPosition(VectorF(0.0f, 0.0f, 1.0f));
matView.inverse();
if (handle->isValid())
cbuf->set(handle, matView);
else
Con::errorf("CubemapSaver: Failed to set a shader constant handle.");
}
bool save(GFXCubemapHandle cubemap, const Torque::Path &path, GFXFormat compressionFormat)
{
if (!cubemap.isValid())
@ -63,93 +44,40 @@ namespace CubemapSaver
return false;
}
// This can sometimes occur outside a begin/end scene.
const bool sceneBegun = GFX->canCurrentlyRender();
if (!sceneBegun)
GFX->beginScene();
GFXCubemap *pCubemap = cubemap.getPointer();
U32 faceSize = pCubemap->getSize();
const U32 faceSize = pCubemap->getSize();
const U32 mipLevels = pCubemap->getMipMapLevels();
ShaderData *shaderData = nullptr;
GFXShaderRef shader = Sim::findObject("CubemapSaveShader", shaderData) ? shaderData->getShader() : nullptr;
if (!shader)
{
Con::errorf("CubemapSaver::save - could not find CubemapSaveShader");
return false;
}
GFXShaderConstHandle *matHandles[CubeFaces];
matHandles[0] = shader->getShaderConstHandle("$matrix0");
matHandles[1] = shader->getShaderConstHandle("$matrix1");
matHandles[2] = shader->getShaderConstHandle("$matrix2");
matHandles[3] = shader->getShaderConstHandle("$matrix3");
matHandles[4] = shader->getShaderConstHandle("$matrix4");
matHandles[5] = shader->getShaderConstHandle("$matrix5");
GFXShaderConstBufferRef cbuffer = shader->allocConstBuffer();
GFXTextureTarget *pTarget = GFX->allocRenderToTextureTarget();
GFX->pushActiveRenderTarget();
GFXFormat renderTargetFmt = GFXFormatR8G8B8A8;
GFXFormat targetFmt = pCubemap->getFormat();
//setup render targets
GFXTexHandle pTextures[CubeFaces];
for (U32 i = 0; i < CubeFaces; i++)
for (U32 face = 0; face < CubeFaces; face++)
{
pTextures[i].set(faceSize, faceSize, renderTargetFmt,
&GFXRenderTargetProfile, avar("%s() - (line %d)", __FUNCTION__, __LINE__),
1, GFXTextureManager::AA_MATCH_BACKBUFFER);
pTextures[face].set(faceSize, faceSize, targetFmt,
&GFXStaticTextureProfile, avar("%s() - (line %d)", __FUNCTION__, __LINE__),
mipLevels, GFXTextureManager::AA_MATCH_BACKBUFFER);
pTarget->attachTexture(GFXTextureTarget::RenderSlot(GFXTextureTarget::Color0 + i), pTextures[i]);
// yep t3d has funky z up, need to change the face order
GFX->copyResource(pTextures[face], pCubemap, GFXCubemap::zUpFaceIndex(face) );
}
//create stateblock
GFXStateBlockDesc desc;
desc.setZReadWrite(false, false);
desc.samplersDefined = true;
desc.samplers[0].addressModeU = GFXAddressClamp;
desc.samplers[0].addressModeV = GFXAddressClamp;
desc.samplers[0].addressModeW = GFXAddressClamp;
desc.samplers[0].magFilter = GFXTextureFilterLinear;
desc.samplers[0].minFilter = GFXTextureFilterLinear;
desc.samplers[0].mipFilter = GFXTextureFilterLinear;
//yep funky order and rotations with t3d z up
_setConstBuffer(matHandles[0], cbuffer, VectorF(0.0f, 1.0f, 0.0f), VectorF(-1.0f, 0.0f, 0.0f));
_setConstBuffer(matHandles[1], cbuffer, VectorF(0.0f, 1.0f, 0.0f), VectorF(1.0f, 0.0f, 0.0f));
_setConstBuffer(matHandles[2], cbuffer, VectorF(0.0f, 1.0f, 0.0f), VectorF(0.0f, 0.0f, -1.0f));
_setConstBuffer(matHandles[3], cbuffer, VectorF(0.0f, 1.0f, 0.0f), VectorF(0.0f, 0.0f, 1.0f));
_setConstBuffer(matHandles[4], cbuffer, VectorF(0.0f, 0.0f, -1.0f), VectorF(0.0f, -1.0f, 0.0f));
_setConstBuffer(matHandles[5], cbuffer, VectorF(0.0f, 0.0f, 1.0f), VectorF(0.0f, 1.0f, 0.0f));
GFXTransformSaver saver;
GFX->setActiveRenderTarget(pTarget);
GFX->clear(GFXClearTarget, ColorI(0, 0, 0, 0), 1.0f, 0);
GFX->setStateBlockByDesc(desc);
GFX->setWorldMatrix(MatrixF::Identity);
GFX->setProjectionMatrix(MatrixF::Identity);
GFX->setCubeTexture(0, pCubemap);
GFX->setShaderConstBuffer(cbuffer);
GFX->setShader(shader);
GFX->drawPrimitive(GFXTriangleList, 0, 3);
pTarget->resolve();
GBitmap *pBitmaps[CubeFaces];
bool error = false;
const bool compressedFormat = ImageUtil::isCompressedFormat(compressionFormat);
const bool hasMips = mipLevels > 1 ? true : false;
for (U32 i = 0; i < CubeFaces; i++)
{
pBitmaps[i] = new GBitmap(faceSize, faceSize, false, renderTargetFmt);
pBitmaps[i] = new GBitmap(faceSize, faceSize, hasMips, targetFmt);
bool result = pTextures[i].copyToBmp(pBitmaps[i]);
if (!result)
{
Con::errorf("CubemapSaver: cubemap number %u failed to copy", i);
error = true;
}
//gen mip maps
pBitmaps[i]->extrudeMipLevels();
//gen mip maps if there are none
if(!hasMips)
pBitmaps[i]->extrudeMipLevels();
}
if (!error)
@ -176,20 +104,10 @@ namespace CubemapSaver
}
}
//cleanup
for (U32 i = 0; i < CubeFaces; i++)
SAFE_DELETE(pBitmaps[i]);
//cleaup
GFX->popActiveRenderTarget();
GFX->setTexture(0, NULL);
GFX->setShader(NULL);
GFX->setShaderConstBuffer(NULL);
GFX->setVertexBuffer(NULL);
// End it if we begun it.
if (!sceneBegun)
GFX->endScene();
return true;
}
@ -202,135 +120,6 @@ namespace CubemapSaver
return false;
}
// This can sometimes occur outside a begin/end scene.
const bool sceneBegun = GFX->canCurrentlyRender();
if (!sceneBegun)
GFX->beginScene();
GFXCubemap *pCubemap = cubemap.getPointer();
U32 faceSize = pCubemap->getSize();
ShaderData *shaderData = nullptr;
GFXShaderRef shader = Sim::findObject("CubemapSaveShader", shaderData) ? shaderData->getShader() : nullptr;
if (!shader)
{
Con::errorf("CubemapSaver::save - could not find CubemapSaveShader");
return false;
}
GFXShaderConstHandle *matHandles[CubeFaces];
matHandles[0] = shader->getShaderConstHandle("$matrix0");
matHandles[1] = shader->getShaderConstHandle("$matrix1");
matHandles[2] = shader->getShaderConstHandle("$matrix2");
matHandles[3] = shader->getShaderConstHandle("$matrix3");
matHandles[4] = shader->getShaderConstHandle("$matrix4");
matHandles[5] = shader->getShaderConstHandle("$matrix5");
GFXShaderConstBufferRef cbuffer = shader->allocConstBuffer();
GFXTextureTarget *pTarget = GFX->allocRenderToTextureTarget();
GFX->pushActiveRenderTarget();
GFXFormat renderTargetFmt = GFXFormatR8G8B8A8;
//setup render targets
GFXTexHandle pTextures[CubeFaces];
for (U32 i = 0; i < CubeFaces; i++)
{
pTextures[i].set(faceSize, faceSize, renderTargetFmt,
&GFXRenderTargetProfile, avar("%s() - (line %d)", __FUNCTION__, __LINE__),
1, GFXTextureManager::AA_MATCH_BACKBUFFER);
pTarget->attachTexture(GFXTextureTarget::RenderSlot(GFXTextureTarget::Color0 + i), pTextures[i]);
}
//create stateblock
GFXStateBlockDesc desc;
desc.setZReadWrite(false, false);
desc.samplersDefined = true;
desc.samplers[0].addressModeU = GFXAddressClamp;
desc.samplers[0].addressModeV = GFXAddressClamp;
desc.samplers[0].addressModeW = GFXAddressClamp;
desc.samplers[0].magFilter = GFXTextureFilterLinear;
desc.samplers[0].minFilter = GFXTextureFilterLinear;
desc.samplers[0].mipFilter = GFXTextureFilterLinear;
//yep funky order and rotations with t3d z up
_setConstBuffer(matHandles[0], cbuffer, VectorF(0.0f, 1.0f, 0.0f), VectorF(-1.0f, 0.0f, 0.0f));
_setConstBuffer(matHandles[1], cbuffer, VectorF(0.0f, 1.0f, 0.0f), VectorF(1.0f, 0.0f, 0.0f));
_setConstBuffer(matHandles[2], cbuffer, VectorF(0.0f, 1.0f, 0.0f), VectorF(0.0f, 0.0f, -1.0f));
_setConstBuffer(matHandles[3], cbuffer, VectorF(0.0f, 1.0f, 0.0f), VectorF(0.0f, 0.0f, 1.0f));
_setConstBuffer(matHandles[4], cbuffer, VectorF(0.0f, 0.0f, -1.0f), VectorF(0.0f, -1.0f, 0.0f));
_setConstBuffer(matHandles[5], cbuffer, VectorF(0.0f, 0.0f, 1.0f), VectorF(0.0f, 1.0f, 0.0f));
GFXTransformSaver saver;
GFX->setActiveRenderTarget(pTarget);
GFX->clear(GFXClearTarget, ColorI(0, 0, 0, 0), 1.0f, 0);
GFX->setStateBlockByDesc(desc);
GFX->setWorldMatrix(MatrixF::Identity);
GFX->setProjectionMatrix(MatrixF::Identity);
GFX->setCubeTexture(0, pCubemap);
GFX->setShaderConstBuffer(cbuffer);
GFX->setShader(shader);
GFX->drawPrimitive(GFXTriangleList, 0, 3);
pTarget->resolve();
bool error = false;
const bool compressedFormat = ImageUtil::isCompressedFormat(compressionFormat);
for (U32 i = 0; i < CubeFaces; i++)
{
//faceBitmaps[i] = new GBitmap(faceSize, faceSize, false, renderTargetFmt);
bool result = pTextures[i].copyToBmp(faceBitmaps[i]);
if (!result)
{
Con::errorf("CubemapSaver: cubemap number %u failed to copy", i);
error = true;
}
//gen mip maps
faceBitmaps[i]->extrudeMipLevels();
}
/*if (!error)
{
DDSFile *pDds = DDSFile::createDDSCubemapFileFromGBitmaps(pBitmaps);
if (pDds)
{
// non compressed format needs swizzling
if (!compressedFormat)
ImageUtil::swizzleDDS(pDds, Swizzles::bgra);
if (compressedFormat)
ImageUtil::ddsCompress(pDds, compressionFormat);
FileStream stream;
stream.open(path, Torque::FS::File::Write);
if (stream.getStatus() == Stream::Ok)
pDds->write(stream);
else
Con::errorf("CubemapSaver: failed to open file stream for file %s", path.getFullPath().c_str());
SAFE_DELETE(pDds);
}
}
for (U32 i = 0; i < CubeFaces; i++)
SAFE_DELETE(pBitmaps[i]);*/
//cleaup
GFX->popActiveRenderTarget();
GFX->setTexture(0, NULL);
GFX->setShader(NULL);
GFX->setShaderConstBuffer(NULL);
GFX->setVertexBuffer(NULL);
// End it if we begun it.
if (!sceneBegun)
GFX->endScene();
if (error)
return false;
return true;
return false;
}
}

View file

@ -35,7 +35,7 @@ GFXCubemap::~GFXCubemap()
TEXMGR->releaseCubemap( this );
}
U32 GFXCubemap::_zUpFaceIndex(const U32 index)
U32 GFXCubemap::zUpFaceIndex(const U32 index)
{
switch (index)
{

View file

@ -47,8 +47,6 @@ protected:
/// Sets the cubemap file path.
void _setPath( const String &path ) { mPath = path; }
/// Get Z up face index of the cubemap. DDS files will be stored Y up
U32 _zUpFaceIndex(const U32 index);
U32 mMipMapLevels;
public:
@ -81,6 +79,9 @@ public:
/// Get the number of mip maps
const U32 getMipMapLevels() const { return mMipMapLevels; }
/// Get Z up face index of the cubemap. DDS files will be stored Y up
static U32 zUpFaceIndex(const U32 index);
};

View file

@ -826,6 +826,13 @@ public:
//-----------------------------------------------------------------------------
/// @name Copying methods
/// @{
virtual void copyResource(GFXTextureObject *pDst, GFXCubemap *pSrc, const U32 face) = 0;
/// @}
/// @name Rendering methods
/// @{

View file

@ -165,7 +165,7 @@ void GFXGLCubemap::initStatic( DDSFile *dds )
}
// convert to Z up
const U32 faceIndex = _zUpFaceIndex(i);
const U32 faceIndex = zUpFaceIndex(i);
// Now loop thru the mip levels!
for (U32 mip = 0; mip < mMipMapLevels; ++mip)

View file

@ -116,7 +116,8 @@ public:
virtual U32 getNumRenderTargets() const;
virtual GFXShader* createShader();
//TODO: implement me!
virtual void copyResource(GFXTextureObject *pDst, GFXCubemap *pSrc, const U32 face) {};
virtual void clear( U32 flags, const LinearColorF& color, F32 z, U32 stencil );
virtual void clearColorAttachment(const U32 attachment, const LinearColorF& color);
virtual bool beginSceneInternal();

View file

@ -286,8 +286,6 @@ void AdvancedLightBinManager::render( SceneRenderState *state )
MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
matrixSet.restoreSceneViewProjection();
const MatrixF &worldToCameraXfm = matrixSet.getWorldToCamera();
// Set up the SG Data
SceneData sgData;
sgData.init( state );
@ -528,17 +526,6 @@ void AdvancedLightBinManager::_setupPerFrameParameters( const SceneRenderState *
farPlane,
vsFarPlane);
}
MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
//matrixSet.restoreSceneViewProjection();
const MatrixF &worldToCameraXfm = matrixSet.getWorldToCamera();
MatrixF inverseViewMatrix = worldToCameraXfm;
//inverseViewMatrix.fullInverse();
//inverseViewMatrix.transpose();
//MatrixF inverseViewMatrix = MatrixF::Identity;
}
void AdvancedLightBinManager::setupSGData( SceneData &data, const SceneRenderState* state, LightInfo *light )
@ -623,10 +610,9 @@ AdvancedLightBinManager::LightMaterialInfo::LightMaterialInfo( const String &mat
lightPosition(NULL),
lightDirection(NULL),
lightColor(NULL),
lightAttenuation(NULL),
lightRange(NULL),
lightAmbient(NULL),
lightTrilight(NULL),
lightRange(NULL),
lightInvSqrRange(NULL),
lightAmbient(NULL),
lightSpotParams(NULL)
{
Material *mat = MATMGR->getMaterialDefinitionByName( matName );
@ -642,10 +628,9 @@ AdvancedLightBinManager::LightMaterialInfo::LightMaterialInfo( const String &mat
lightDirection = matInstance->getMaterialParameterHandle("$lightDirection");
lightAmbient = matInstance->getMaterialParameterHandle("$lightAmbient");
lightTrilight = matInstance->getMaterialParameterHandle("$lightTrilight");
lightSpotParams = matInstance->getMaterialParameterHandle("$lightSpotParams");
lightAttenuation = matInstance->getMaterialParameterHandle("$lightAttenuation");
lightRange = matInstance->getMaterialParameterHandle("$lightRange");
lightInvSqrRange = matInstance->getMaterialParameterHandle("$lightInvSqrRange");
lightPosition = matInstance->getMaterialParameterHandle("$lightPosition");
farPlane = matInstance->getMaterialParameterHandle("$farPlane");
vsFarPlane = matInstance->getMaterialParameterHandle("$vsFarPlane");
@ -686,87 +671,47 @@ void AdvancedLightBinManager::LightMaterialInfo::setLightParameters( const Light
{
MaterialParameters *matParams = matInstance->getMaterialParameters();
// Set color in the right format, set alpha to the luminance value for the color.
LinearColorF col = lightInfo->getColor();
// TODO: The specularity control of the light
// is being scaled by the overall lumiance.
//
// Not sure if this may be the source of our
// bad specularity results maybe?
//
const Point3F colorToLumiance( 0.3576f, 0.7152f, 0.1192f );
F32 lumiance = mDot(*((const Point3F *)&lightInfo->getColor()), colorToLumiance );
col.alpha *= lumiance;
matParams->setSafe( lightColor, col );
matParams->setSafe( lightColor, lightInfo->getColor() );
matParams->setSafe( lightBrightness, lightInfo->getBrightness() );
switch( lightInfo->getType() )
{
case LightInfo::Vector:
{
const VectorF lightDir = lightInfo->getDirection();
matParams->setSafe( lightDirection, lightDir );
// Set small number for alpha since it represents existing specular in
// the vector light. This prevents a divide by zero.
LinearColorF ambientColor = renderState->getAmbientLightColor();
ambientColor.alpha = 0.00001f;
matParams->setSafe( lightAmbient, ambientColor );
// If no alt color is specified, set it to the average of
// the ambient and main color to avoid artifacts.
//
// TODO: Trilight disabled until we properly implement it
// in the light info!
//
//LinearColorF lightAlt = lightInfo->getAltColor();
LinearColorF lightAlt( LinearColorF::BLACK ); // = lightInfo->getAltColor();
if ( lightAlt.red == 0.0f && lightAlt.green == 0.0f && lightAlt.blue == 0.0f )
lightAlt = (lightInfo->getColor() + renderState->getAmbientLightColor()) / 2.0f;
LinearColorF trilightColor = lightAlt;
matParams->setSafe(lightTrilight, trilightColor);
matParams->setSafe( lightDirection, lightInfo->getDirection());
matParams->setSafe( lightAmbient, renderState->getAmbientLightColor());
}
break;
case LightInfo::Spot:
{
const F32 outerCone = lightInfo->getOuterConeAngle();
const F32 innerCone = getMin( lightInfo->getInnerConeAngle(), outerCone );
const F32 outerCos = mCos( mDegToRad( outerCone / 2.0f ) );
const F32 innerCos = mCos( mDegToRad( innerCone / 2.0f ) );
Point4F spotParams( outerCos,
innerCos - outerCos,
mCos( mDegToRad( outerCone ) ),
0.0f );
const F32 innerCone = getMin(lightInfo->getInnerConeAngle(), outerCone);
const F32 outerCos = mCos(mDegToRad(outerCone / 2.0f));
const F32 innerCos = mCos(mDegToRad(innerCone / 2.0f));
Point2F spotParams(outerCos,innerCos - outerCos);
matParams->setSafe( lightSpotParams, spotParams );
matParams->setSafe( lightDirection, lightInfo->getDirection());
matParams->setSafe( lightPosition, lightInfo->getPosition());
const F32 radius = lightInfo->getRange().x;
const F32 invSqrRadius = 1.0f / mSquared(radius);
matParams->setSafe(lightRange, radius);
matParams->setSafe(lightInvSqrRange, invSqrRadius);
}
// Fall through
break;
case LightInfo::Point:
{
const F32 radius = lightInfo->getRange().x;
matParams->setSafe( lightRange, radius );
matParams->setSafe( lightPosition, lightInfo->getPosition());
{
matParams->setSafe(lightPosition, lightInfo->getPosition());
// Get the attenuation falloff ratio and normalize it.
Point3F attenRatio = lightInfo->getExtended<ShadowMapParams>()->attenuationRatio;
F32 total = attenRatio.x + attenRatio.y + attenRatio.z;
if ( total > 0.0f )
attenRatio /= total;
Point2F attenParams( ( 1.0f / radius ) * attenRatio.y,
( 1.0f / ( radius * radius ) ) * attenRatio.z );
matParams->setSafe( lightAttenuation, attenParams );
const F32 radius = lightInfo->getRange().x;
const F32 invSqrRadius = 1.0f / (radius * radius);
matParams->setSafe( lightRange, radius);
matParams->setSafe( lightInvSqrRange, invSqrRadius);
}
break;
}
default:
AssertFatal( false, "Bad light type!" );

View file

@ -165,10 +165,9 @@ protected:
MaterialParameterHandle *lightDirection;
MaterialParameterHandle *lightColor;
MaterialParameterHandle *lightBrightness;
MaterialParameterHandle *lightAttenuation;
MaterialParameterHandle *lightRange;
MaterialParameterHandle *lightInvSqrRange;
MaterialParameterHandle *lightAmbient;
MaterialParameterHandle *lightTrilight;
MaterialParameterHandle *lightSpotParams;
LightMaterialInfo( const String &matName,