#include <maya/MFnPlugin.h>
#include <maya/MArgList.h>
#include <maya/MArgParser.h>
#include <maya/MSyntax.h>
#include <maya/MFileObject.h>
#include <maya/MGlobal.h>
#include <maya/MPxCommand.h>
#include <maya/MTextureManager.h>
#include <maya/MRenderTargetManager.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MFnDependencyNode.h>
#include "hwApiTextureTestStrings.h"
#include "hwRendererHelper.h"
#ifndef _WIN32
#include <unistd.h>
#endif
namespace
{
const char *loadArgName = "-l";
const char *loadArgLongName = "-load";
const char* loadNodeArgName = "-n";
const char* loadNodeArgLongName = "-nodeLoad";
const char *drawArgName = "-d";
const char *drawArgLongName = "-draw";
const char *editArgName = "-e";
const char *editArgLongName = "-edit";
const char *tileArgName = "-t";
const char *tileArgLongName = "-tile";
const char *saveArgName = "-s";
const char *saveArgLongName = "-save";
const char *formatArgName = "-f";
const char *formatArgLongName = "-format";
const char *listArgName = "-ls";
const char *listArgLongName = "-list";
{
{
{
extension = elements[elements.
length() - 1];
}
}
return extension;
}
{
if(index > 0)
{
newPath = filePath.
substring(0, index) + extension;
}
return newPath;
}
};
#ifdef _WIN32
#define mySleep(s) Sleep(s * 1000)
#else
#define mySleep(s) sleep(s)
#endif
{
public:
hwApiTextureTestCmd();
~hwApiTextureTestCmd() override;
static void* creator();
private:
private:
bool fLoadTextures;
bool fDrawTextures;
bool fEditTextures;
bool fTileTextures;
bool fLoadFromSelectionList;
bool fSaveAsTexture;
bool fSaveAllFormats;
bool fListFormats;
};
hwApiTextureTestCmd::hwApiTextureTestCmd()
: fLoadTextures(false)
, fLoadFromSelectionList(false)
, fLoadTexturesPath()
, fDrawTextures(false)
, fEditTextures(false)
, fTileTextures(false)
, fSaveAsTexture(false)
, fSaveAsTexturePath()
, fSaveAsTextureFormats()
, fSaveAllFormats(false)
, fListFormats(false)
{
fSupportedFormats.append( L"bmp" );
fSupportedFormats.append( L"dds" );
fSupportedFormats.append( L"exr" );
fSupportedFormats.append( L"gif" );
fSupportedFormats.append( L"iff" );
fSupportedFormats.append( L"jpg" );
fSupportedFormats.append( L"pct" );
fSupportedFormats.append( L"pic" );
fSupportedFormats.append( L"png" );
fSupportedFormats.append( L"psd" );
fSupportedFormats.append( L"rla" );
fSupportedFormats.append( L"sgi" );
fSupportedFormats.append( L"tga" );
fSupportedFormats.append( L"tif" );
}
hwApiTextureTestCmd::~hwApiTextureTestCmd()
{
}
void* hwApiTextureTestCmd::creator()
{
return (void *) (new hwApiTextureTestCmd);
}
MSyntax hwApiTextureTestCmd::newSyntax()
{
syntax.
addFlag(loadNodeArgName, loadNodeArgLongName );
syntax.
addFlag(drawArgName, drawArgLongName);
syntax.
addFlag(editArgName, editArgLongName);
syntax.
addFlag(tileArgName, tileArgLongName);
syntax.
addFlag(listArgName, listArgLongName);
return syntax;
}
{
{
if( argParser.isFlagSet(loadArgName) )
{
fLoadTextures = true;
fLoadTexturesPath.setRawPath(path);
}
if (argParser.isFlagSet(loadNodeArgName))
{
fLoadFromSelectionList = true;
fLoadTextures = false;
}
if( argParser.isFlagSet(drawArgName) )
{
fDrawTextures = true;
}
if( argParser.isFlagSet(editArgName) )
{
fEditTextures = true;
}
if( argParser.isFlagSet(tileArgName) )
{
fTileTextures = true;
fEditTextures = false;
}
if( argParser.isFlagSet(saveArgName) )
{
fSaveAsTexture = true;
fSaveAsTexturePath.setRawPath(path);
unsigned int numFormats = argParser.numberOfFlagUses(formatArgName);
for(unsigned int flag = 0; flag < numFormats; ++flag)
{
if( argParser.getFlagArgumentList(formatArgName, flag, formats) ==
MStatus::kSuccess )
{
for(
unsigned int arg = 0; arg < formats.
length(); ++arg)
{
{
if(format == "all")
fSaveAllFormats = true;
else
fSaveAsTextureFormats.append(format);
}
}
}
}
}
if( argParser.isFlagSet(listArgName) )
{
fListFormats = true;
}
}
return status;
}
{
if(status)
{
if(fListFormats)
{
status = listFormats();
}
else if(fLoadFromSelectionList)
{
status = loadSelectedTextures();
}
else if(fLoadTextures)
{
status = loadTextures();
}
else if(fSaveAsTexture)
{
status = saveAsTexture();
}
}
return status;
}
MStatus hwApiTextureTestCmd::loadSelectedTextures()
{
if(renderer == NULL)
{
}
if(textureManager == NULL)
{
}
if(fSaveAsTexture)
{
if(fSaveAsTexturePath.isSet() == false || fSaveAsTexturePath.exists() == false)
{
}
if(fSaveAsTextureFormats.length() > 0)
saveAsFormat = fSaveAsTextureFormats[0];
}
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kBeginLoadTest ) );
hwRendererHelper* renderHelper = NULL;
if(fDrawTextures)
{
renderHelper = hwRendererHelper::create(renderer);
}
for( ; !iter.isDone(); iter.next() )
{
iter.getDependNode( dependNode );
bool useNodeVersion = isFileTexture;
if (!useNodeVersion)
{
bool sampleFileName = true;
MPlug plugToSample = dependFn.findPlug(
"fileTextureName" ,
true);
{
sampleFileName = false;
plugToSample = dependFn.findPlug("outColor", true);
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorLoadTexture, dependFn.name().asChar() ) );
continue;
}
}
try
{
if (sampleFileName)
{
texture = textureManager->
acquireTexture(noName, plugToSample, 1, 1,
false);
}
else
{
texture = textureManager->
acquireTexture(name, plugToSample, 512, 512,
false);
}
}
catch(...)
{
texture = NULL;
}
}
else
{
try
{
}
catch(...)
{
texture = NULL;
}
}
if(texture == NULL)
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorLoadTexture, dependFn.name().asChar() ) );
}
else
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kSuccessLoadTexture, dependFn.name().asChar() ) );
if(fSaveAsTexture)
{
MString savePath = fSaveAsTexturePath.resolvedPath() + dependFn.name() +
".png";
{
savePath = changeExtension(savePath, saveAsFormat);
}
const MString extension = fileExtension(savePath);
try
{
status = textureManager->
saveTexture( texture, savePath );
}
catch(...)
{
}
if(status)
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kSuccessSaveTexture, savePath, extension ) );
}
else
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorSaveTexture, savePath, extension ) );
}
}
if(fDrawTextures)
{
if(renderHelper->renderTextureToScreen(texture))
{
mySleep(1);
}
}
}
}
delete renderHelper;
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kEndLoadTest ) );
}
MStatus hwApiTextureTestCmd::loadTextures()
{
if(renderer == NULL)
{
}
if(textureManager == NULL)
{
}
if(fLoadTexturesPath.isSet() == false || fLoadTexturesPath.exists() == false)
{
}
if(fSaveAsTexture)
{
if(fSaveAsTexturePath.isSet() == false || fSaveAsTexturePath.exists() == false)
{
}
if(fSaveAsTextureFormats.length() > 0)
saveAsFormat = fSaveAsTextureFormats[0];
}
{
fileListCmd.
format(
"getFileList -folder \"^1s\";", fLoadTexturesPath.resolvedPath());
for(
unsigned int fileIdx = 0; fileIdx < allFiles.
length(); ++fileIdx)
{
const MString& fileName = allFiles[fileIdx];
MString extension = fileExtension(fileName);
for(unsigned int formatIdx = 0; formatIdx < fSupportedFormats.length(); ++formatIdx)
{
const MString& format = fSupportedFormats[formatIdx];
if(format == extension)
{
texturesFiles.
append(fileName);
}
}
}
}
unsigned int textureFilesCount = texturesFiles.
length();
if(textureFilesCount == 0)
{
}
hwRendererHelper* renderHelper = NULL;
if(fDrawTextures)
{
renderHelper = hwRendererHelper::create(renderer);
}
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kBeginLoadTest ) );
if (fTileTextures && textureFilesCount > 1)
{
for(unsigned int textIdx = 0; textIdx < textureFilesCount; ++textIdx)
{
const MString& fileName = texturesFiles[textIdx];
const MString filePath = fLoadTexturesPath.resolvedPath() + fileName;
}
unsigned int tileU = (unsigned int) (sqrt( (float)textureFilesCount ));
unsigned int tileV = (tileU*tileU != textureFilesCount) ? tileU+1 : tileU;
printf("file count=%d, tileU=%d, tileV=%d\n",
textureFilesCount, tileU, tileV);
for (unsigned int i=0; i<tileU; i++)
{
for (unsigned int j=0; j<tileV; j++)
{
tilePositions.
append( (
float)i );
tilePositions.
append( (
float)j );
}
}
MString textureName(
"uvTiledTexture");
unsigned int maxWidth = 4096;
unsigned int maxHeight = 4096;
try
{
tilePaths,
tilePositions,
undefinedColor,
maxWidth, maxHeight,
failedTilePaths,
uvScaleOffset);
tilePaths,
tilePositions,
undefinedColor,
maxWidth, maxHeight,
failedTilePaths,
uvScaleOffset);
if (texture2)
}
catch(...)
{
texture = NULL;
}
if(texture == NULL)
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorLoadTexture,
"Tiled texture" ) );
}
else
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kSuccessLoadTexture,
"Tiled texture") );
for (
unsigned int i=0; i<failedTilePaths.
length(); i++)
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorTileTexture, failedTilePaths[i].asChar() ) );
}
uvScaleOffset[0],uvScaleOffset[1],uvScaleOffset[2],uvScaleOffset[3]) );
if(fSaveAsTexture)
{
MString savePath = fSaveAsTexturePath.resolvedPath() +
MString(
"uvTiledTexture.dds");
{
savePath = changeExtension(savePath, saveAsFormat);
}
try
{
status = textureManager->
saveTexture( texture, savePath );
}
catch(...)
{
}
if(status)
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kSuccessSaveTexture, savePath, extension ) );
}
else
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorSaveTexture, savePath, extension ) );
}
}
if(fDrawTextures)
{
if(renderHelper->renderTextureToScreen(texture))
{
mySleep(2);
}
}
}
}
else
{
for(unsigned int textIdx = 0; textIdx < textureFilesCount; ++textIdx)
{
const MString& fileName = texturesFiles[textIdx];
const MString filePath = fLoadTexturesPath.resolvedPath() + fileName;
try
{
}
catch(...)
{
texture = NULL;
}
if(texture == NULL)
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorLoadTexture, filePath ) );
}
else
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kSuccessLoadTexture, filePath ) );
if (fEditTextures)
{
if (bpp == 4 &&
(desc.
fFormat == MHWRender::kR8G8B8A8_UNORM ||
desc.
fFormat == MHWRender::kB8G8R8A8))
{
int rowPitch = 0;
size_t slicePitch = 0;
bool generateMipMaps = true;
unsigned char* pixelData = (
unsigned char *)texture->
rawData(rowPitch, slicePitch);
unsigned char* val = NULL;
if (pixelData && rowPitch > 0 && slicePitch > 0)
{
bool updateEntireImage = false;
if (updateEntireImage)
{
for (
unsigned int i=0; i<desc.
fHeight; i++)
{
val = pixelData + (i*rowPitch);
for (
unsigned int j=0; j<desc.
fWidth*4; j++)
{
*val = 255 - *val;
val++;
}
}
texture->
update(pixelData, generateMipMaps, rowPitch);
}
else
{
unsigned int minX = desc.
fWidth / 3;
unsigned int maxX = (desc.
fWidth * 2) / 3;
unsigned int newWidth = maxX - minX;
unsigned int minY = desc.
fHeight/ 3;
unsigned int maxY = (desc.
fHeight *2) / 3;
unsigned int newHeight = maxY - minY;
unsigned char* newData = new unsigned char[newWidth * newHeight * 4];
if (newData)
{
unsigned char* newVal = newData;
for (unsigned int i=minY; i<maxY; i++)
{
val = pixelData + (i*rowPitch) + minX*4;
for (unsigned int j=0; j<newWidth*4; j++)
{
*newVal = 255 - *val;
val++;
newVal++;
}
}
updateRegion.
fXRangeMin = minX;
updateRegion.
fXRangeMax = maxX;
updateRegion.
fYRangeMin = minY;
updateRegion.
fYRangeMax = maxY;
texture->
update(newData, generateMipMaps, newWidth*4, &updateRegion);
delete [] newData;
}
}
}
delete [] pixelData;
}
}
if(fSaveAsTexture)
{
MString savePath = fSaveAsTexturePath.resolvedPath() + fileName;
{
savePath = changeExtension(savePath, saveAsFormat);
}
const MString extension = fileExtension(savePath);
try
{
status = textureManager->
saveTexture( texture, savePath );
}
catch(...)
{
}
if(status)
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kSuccessSaveTexture, savePath, extension ) );
}
else
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorSaveTexture, savePath, extension ) );
}
}
if(fDrawTextures)
{
if(renderHelper->renderTextureToScreen(texture))
{
mySleep(1);
}
}
}
}
}
delete renderHelper;
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kEndLoadTest ) );
}
MStatus hwApiTextureTestCmd::saveAsTexture()
{
if(renderer == NULL)
{
}
if(textureManager == NULL)
{
}
if(fSaveAsTexturePath.isSet() == false || fSaveAsTexturePath.exists() == false)
{
}
const MStringArray& saveAsFormats = (fSaveAllFormats ? fSupportedFormats : fSaveAsTextureFormats);
if(saveAsFormats.
length() == 0)
{
}
hwRendererHelper* renderHelper = hwRendererHelper::create(renderer);
delete renderHelper;
if(texture == NULL)
{
MGlobal::displayWarning( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorSaveAcquireTexture ) );
}
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kBeginSaveTest ) );
for(
unsigned int formatIdx = 0; formatIdx < saveAsFormats.
length(); ++formatIdx)
{
const MString& format = saveAsFormats[formatIdx];
const MString filePath = fSaveAsTexturePath.resolvedPath() +
"hwApiTextureTest." + extension;
try
{
status = textureManager->
saveTexture( texture, filePath );
}
catch(...)
{
}
if(status)
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kSuccessSaveTexture, filePath, format ) );
}
else
{
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kErrorSaveTexture, filePath, format ) );
}
}
MGlobal::displayInfo( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kEndSaveTest ) );
}
MStatus hwApiTextureTestCmd::listFormats()
{
for(unsigned int formatIdx = 0; formatIdx < fSupportedFormats.length(); ++formatIdx)
{
const MString& format = fSupportedFormats[formatIdx];
allFormats += format;
}
}
{
MFnPlugin plugin( obj, PLUGIN_COMPANY,
"1.0",
"Any");
status = plugin.registerUIStrings( hwApiTextureTestStrings::registerMStringResources, "" );
if (!status)
{
status.
perror(
"registerUIStrings");
return status;
}
status = plugin.registerCommand("hwApiTextureTest",
hwApiTextureTestCmd::creator,
hwApiTextureTestCmd::newSyntax);
if (!status)
{
status.
perror(
"registerCommand");
}
return status;
}
{
status = plugin.deregisterCommand("hwApiTextureTest");
if (!status)
{
status.
perror(
"deregisterCommand");
}
return status;
}