Skip to content

Commit

Permalink
Merge pull request #209 from MBU-Team/material-network
Browse files Browse the repository at this point in the history
Material networking
  • Loading branch information
HumanGamer authored Mar 13, 2024
2 parents 3e14f77 + 1a1f798 commit 04247bf
Show file tree
Hide file tree
Showing 23 changed files with 274 additions and 42 deletions.
9 changes: 7 additions & 2 deletions engine/source/core/memStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,11 @@ bool ResizableMemStream::_write(const U32 in_numBytes, const void* in_pBuffer)
if (!expandToSize(m_currentPosition + in_numBytes))
return false;

U32 pastPos = m_currentPosition;
Parent::_write(in_numBytes, in_pBuffer);
m_filledSize = m_currentPosition + in_numBytes;
if (pastPos <= m_filledSize && pastPos + in_numBytes >= m_filledSize)
m_filledSize = pastPos + in_numBytes;
AssertISV(m_filledSize < m_bufferSize, "Invalid buffer size");
}


Expand Down Expand Up @@ -352,8 +355,10 @@ bool MemSubStream::setPosition(const U32 in_newPosition)
bool result = m_pStream->setPosition(in_newPosition);
m_pStream->setPosition(oldPos);

if (result)
if (result) {
m_currOffset = in_newPosition;
setStatus(Ok);
}

return result;
}
Expand Down
5 changes: 2 additions & 3 deletions engine/source/core/resManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ void ResManager::freeResource(ResourceObject* ro)

//------------------------------------------------------------------------------

bool ResManager::openFileForWrite(Stream*& stream, const char* fileName, U32 accessMode)
bool ResManager::openFileForWrite(Stream*& stream, const char* fileName, U32 accessMode, bool forceMemory)
{
if (!isValidWriteFileName(fileName))
return false;
Expand All @@ -1245,8 +1245,7 @@ bool ResManager::openFileForWrite(Stream*& stream, const char* fileName, U32 acc
return false; // don't allow storing files in root
*file++ = 0;

if (path && file && !dStrnicmp(path, "mem", 3)
&& path[3] == '/' || path[3] == 0)
if (path && file && (forceMemory || !dStrnicmp(path, "mem", 3) && path[3] == '/' || path[3] == 0))
{
// Opening memory file
ResourceObject* ret = createResource(StringTable->insert(path), StringTable->insert(file));
Expand Down
2 changes: 1 addition & 1 deletion engine/source/core/resManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class ResManager
bool isValidWriteFileName(const char* fn); ///< Checks to see if the given path is valid for writing.

/// Opens a file for writing!
bool openFileForWrite(Stream*& fs, const char* fileName, U32 accessMode = 1);
bool openFileForWrite(Stream*& fs, const char* fileName, U32 accessMode = 1, bool forceMemory = false);

void startResourceTraverse();
ResourceObject* getNextResource();
Expand Down
13 changes: 13 additions & 0 deletions engine/source/game/tsStatic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "sim/netConnection.h"
#include "gfx/gfxDevice.h"
#include "lightingSystem/sgLighting.h"
#include "materials/material.h"

IMPLEMENT_CO_NETOBJECT_V1(TSStatic);

Expand Down Expand Up @@ -115,6 +116,18 @@ bool TSStatic::onAdd()
if (!gSPMode && isClientObject() && !mShape->preloadMaterialList() && NetConnection::filesWereDownloaded())
return false;

bool foundAllMaterials = true;
for (int i = 0; i < !mShape->materialList->size(); i++) {
Material* mat = mShape->materialList->getMappedMaterial(i);
if (mat != NULL)
foundAllMaterials = foundAllMaterials && mat->preloadTextures();
}
if (!foundAllMaterials) {
Con::errorf(ConsoleLogEntry::General, "Unable to load TSStatic due to missing materials: %s", mShapeName);
NetConnection::setLastError("Unable to load TSStatic due to missing materials: %s", mShapeName);
return false;
}

mObjBox = mShape->bounds;
resetWorldBox();
setRenderTransform(mObjToWorld);
Expand Down
34 changes: 24 additions & 10 deletions engine/source/gfx/D3D9/gfxD3D9Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "shaderGen/shaderGenManager.h"
#include "core/fileStream.h"
#include "game/version.h"
#include "core/resManager.h"

//#define PRINT_SHADER_WARNINGS

Expand Down Expand Up @@ -72,19 +73,34 @@ HRESULT _gfxD3DXInclude::Open(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileNa
{
mIncludeFileName = ste;

FileStream fs;
AssertISV( fs.open( mIncludeFileName, FileStream::Read ), "Something went horribly wrong with the ID3DXInclude stuff" );
Stream* fs;
fs = ResourceManager->openStream(mIncludeFileName);
if (fs == NULL)
{
// Try the default shaders/ dir
char defPath[1024] = "shaders/";
dStrcat(defPath, tmpFileName);
StringTableEntry ste = StringTable->insert(defPath);
mIncludeFileName = ste;
fs = ResourceManager->openStream(mIncludeFileName);
}
if (fs == NULL)
{
AssertISV(false, "Something went horribly wrong with the ID3DXInclude stuff");
}
//FileStream fs;
// AssertISV( fs.open( mIncludeFileName, FileStream::Read ), "Something went horribly wrong with the ID3DXInclude stuff" );

//SAFE_DELETE_ARRAY( mIncludeData );

mIncludeDataSize = fs.getStreamSize();
mIncludeDataSize = fs->getStreamSize();
mIncludeData = new U8[mIncludeDataSize];
if(!fs.read( mIncludeDataSize, mIncludeData ))
if(!fs->read( mIncludeDataSize, mIncludeData ))
{
// Read failed, cut our losses.
mIncludeData[0] = 0;
}
fs.close();
ResourceManager->closeStream(fs);

gIncludeAllocs.push_back(mIncludeData);
}
Expand Down Expand Up @@ -217,8 +233,8 @@ void GFXD3D9Shader::initShader( const char *file, const char *target )
else
{
// Ok it's not in the shader gen manager, so ask Torque for it
s = new FileStream();
if(!static_cast<FileStream *>( s )->open( file, FileStream::Read ))
s = ResourceManager->openStream(file);
if(!s)
{
AssertISV(false, avar("GFXD3D9Shader::initShader - failed to open shader '%s'.", file));
}
Expand All @@ -231,16 +247,14 @@ void GFXD3D9Shader::initShader( const char *file, const char *target )
res = GFXD3DX.D3DXCompileShader( buffer, s->getStreamSize(), defines, smD3DXInclude, "main",
target, flags, &code, &errorBuff, NULL );

static_cast<FileStream *>( s )->close();
ResourceManager->closeStream(s);
}
else
{
res = GFXD3DX.D3DXCompileShaderFromFileA( Platform::createPlatformFriendlyFilename( file ),
defines, smD3DXInclude, "main", target,
flags, &code, &errorBuff, NULL );
}

SAFE_DELETE( s );
}
}
else
Expand Down
23 changes: 22 additions & 1 deletion engine/source/interior/interiorInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "math/mathUtils.h"
#include "renderInstance/renderInstMgr.h"
#include "sim/pathManager.h"
#include "materials/material.h"

//--------------------------------------------------------------------------
//-------------------------------------- Local classes, data, and functions
Expand Down Expand Up @@ -363,6 +364,26 @@ bool InteriorInstance::onAdd()
return false;
}

// Check for materials

bool foundAllMaterials = true;
for (i = 0; i < mInteriorRes->getNumDetailLevels(); i++) {
Interior* pInterior = mInteriorRes->getDetailLevel(i);
for (int j = 0; j < pInterior->mMaterialList->size(); j++)
{
Material* mat = pInterior->mMaterialList->getMappedMaterial(j);
if (mat != NULL)
foundAllMaterials = foundAllMaterials && mat->preloadTextures();
}
}
if (!foundAllMaterials) {
Con::errorf(ConsoleLogEntry::General, "Unable to load interior due to missing materials: %s", mInteriorFileName);
NetConnection::setLastError("Unable to load interior due to missing materials: %s", mInteriorFileName);
return false;
}



if (!isClientObject())
mCRC = mInteriorRes.getCRC();

Expand Down Expand Up @@ -432,7 +453,7 @@ bool InteriorInstance::onAdd()
mMaterialMaps.push_back(new MaterialList(pInterior->mMaterialList));
}

renewOverlays();
// renewOverlays();
//}
//else {
//
Expand Down
19 changes: 19 additions & 0 deletions engine/source/materials/customMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ void CustomMaterial::initPersistFields()

}

bool CustomMaterial::preloadTextures()
{
bool found = Parent::preloadTextures();
for (int i = 0; i < MAX_TEX_PER_PASS; i++)
{
found = found && (!texFilename[i] || didFindTexture(texFilename[i]));
}
for (int i = 0; i < MAX_PASSES; i++)
{
found = found && (!pass[i] || pass[i]->preloadTextures());
}
if (fallback != NULL)
found = found && fallback->preloadTextures();
found = found && (!mShaderData->DXVertexShaderName || ResourceManager->find(mShaderData->DXVertexShaderName)); // Transfer shaders too lmao (attempt)
found = found && (!mShaderData->DXVertexShaderName || ResourceManager->find(mShaderData->DXPixelShaderName));

return found;
}

//--------------------------------------------------------------------------
// On add - verify data settings
//--------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions engine/source/materials/customMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class CustomMaterial : public Material

static void initPersistFields();
static void updateTime();
virtual bool preloadTextures();
const char* mShaderDataName;
ShaderData* mShaderData;

Expand Down
54 changes: 49 additions & 5 deletions engine/source/materials/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,38 @@ void Material::updateTime()
}
}

bool Material::preloadTextures()
{
bool found = true;
for (int i = 0; i < MAX_STAGES; i++)
{
found = found && (!baseTexFilename[i] || didFindTexture(baseTexFilename[i]));
found = found && (!detailFilename[i] || didFindTexture(detailFilename[i]));
found = found && (!bumpFilename[i] || didFindTexture(bumpFilename[i]));
found = found && (!envFilename[i] || didFindTexture(envFilename[i]));
}
found = found && (!noiseTexFileName || didFindTexture(noiseTexFileName));
if (mCubemapData != NULL && !dynamicCubemap)
{
for (int i = 0; i < 6; i++)
found = found && (!mCubemapData->cubeFaceFile[i] || didFindTexture(mCubemapData->cubeFaceFile[i]));
}
return found;
}

bool Material::didFindTexture(const char* filename)
{
ResourceObject* ro = GBitmap::findBmpResource(filename);
if (ro)
{
return true;
}

// Find and load the texture.
GBitmap* bmp = GBitmap::load(filename);
return bmp != NULL;
}

void Material::updateTimeBasedParams()
{
if (mLastUpdateTime != mLastTime)
Expand Down Expand Up @@ -449,21 +481,24 @@ bool loadMaterialsFromJson(const char* path)
Stream* fs = ResourceManager->openStream(path);
if (fs == NULL) return false;

char* jsonBuf = new char[fs->getStreamSize() + 1];
U32 size = fs->getStreamSize();

char* jsonBuf = new char[size + 1];

if (!fs->read(fs->getStreamSize(), jsonBuf))
if (!fs->read(size, jsonBuf))
{
delete[] jsonBuf;
return false;
}
jsonBuf[fs->getStreamSize()] = '\0';
jsonBuf[size] = '\0';
ResourceManager->closeStream(fs);

Json::Value root;
Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader();

std::string errs;
if (!reader->parse(jsonBuf, jsonBuf + fs->getStreamSize(), &root, &errs))
if (!reader->parse(jsonBuf, jsonBuf + size, &root, &errs))
{
delete reader;
delete[] jsonBuf;
Expand All @@ -474,7 +509,7 @@ bool loadMaterialsFromJson(const char* path)
{
delete reader;
delete[] jsonBuf;

Json::Value materialDict = root["materials"];
Json::Value shaderDict = root["shaders"];
Json::Value cubemapDict = root["cubemaps"];
Expand All @@ -486,6 +521,9 @@ bool loadMaterialsFromJson(const char* path)
Json::Value key = it.key();
Json::Value val = *it;

if (Sim::findObject(key.asCString()) != NULL)
continue; // Don't add

ShaderData* shader = new ShaderData();
shader->assignName(key.asCString());
shader->pixVersion = val.get("pixVersion", 1.0).asFloat();
Expand All @@ -503,6 +541,9 @@ bool loadMaterialsFromJson(const char* path)
Json::Value key = it.key();
Json::Value val = *it;

if (Sim::findObject(key.asCString()) != NULL)
continue; // Don't add

CubemapData* cubeMap = new CubemapData();
cubeMap->assignName(key.asCString());
for (int i = 0; i < 6; i++)
Expand All @@ -520,6 +561,9 @@ bool loadMaterialsFromJson(const char* path)
Json::Value key = it.key();
Json::Value val = *it;

if (Sim::findObject(key.asCString()) != NULL)
continue; // Don't add

bool isCustom = val.get("custom", false).asBool();

Material* mat = isCustom ? new CustomMaterial() : new Material();
Expand Down
3 changes: 3 additions & 0 deletions engine/source/materials/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <renderInstance/renderInstMgr.h>

#include "sceneGraph/lightInfo.h"
#include "core/tDictionary.h"

class CubemapData;
struct SceneGraphData;
Expand Down Expand Up @@ -241,6 +242,8 @@ class Material : public SimObject
bool isIFL(){ return mIsIFL; }
bool isTranslucent() { return translucent || subPassTranslucent; }
char* getPath() { return mPath; }
virtual bool preloadTextures();
bool didFindTexture(const char* path);

void updateTimeBasedParams();

Expand Down
5 changes: 2 additions & 3 deletions engine/source/shaderGen/shaderGenManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "shaderGen/shaderGenManager.h"
#include "core/fileStream.h"
#include "core/memstream.h"
#include <core/resManager.h>

Vector<TrackedAutoGenShader> ShaderGenManager::_mTrackedShaders;
Stream* ShaderGenManager::_mOpenStream = NULL;
Expand Down Expand Up @@ -88,9 +89,7 @@ Stream* ShaderGenManager::readShaderStream(const char* fileName)
}
else
{
FileStream* fs = new FileStream();
fs->open(fileName, FileStream::Read);
_mOpenReadStream = fs;
_mOpenReadStream = ResourceManager->openStream(fileName);
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions engine/source/sim/netConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,8 @@ class NetConnection : public ConnectionProtocol, public SimGroup
void sendFastFileAcknowledgement(BitStream* stream);
void handleFastFileAcknowledgement(BitStream* stream);
void processFastFileAcknowledgement();
void addMissingFile(const char* path);
void popMissingFile();

#endif // TORQUE_NET_HOLEPUNCHING

Expand Down
Loading

0 comments on commit 04247bf

Please sign in to comment.