#ifndef HLSL_VERSION
#define HLSL_VERSION "1.0"
#endif
#define ERROR_LIMIT 20
#define MAX_SEGMENTED_BATCH_SIZE 40000
#include "hlslShader.h"
#include <maya/MGlobal.h>
#include <maya/MGeometry.h>
#include <maya/MGeometryData.h>
#include <maya/MGeometryPrimitive.h>
#include <maya/MGeometryList.h>
#include <maya/MGeometryManager.h>
#include <maya/MVaryingParameterList.h>
#include <maya/MUniformParameter.h>
#include <maya/MGlobal.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnStringData.h>
#include <maya/MFnStringArrayData.h>
#include <maya/MSceneMessage.h>
#include <maya/MFileIO.h>
#include <maya/MMatrix.h>
#include <maya/MFileObject.h>
#include <maya/MArgParser.h>
#include <maya/MSyntax.h>
#include <maya/MPointArray.h>
#include <maya/MD3D9Renderer.h>
#include <maya/MHwrCallback.h>
#include <stdio.h>
#include <new.h>
#include <maya/MHardwareRenderer.h>
#include <maya/MHWShaderSwatchGenerator.h>
#include <maya/MGeometryRequirements.h>
#include <maya/MHwTextureManager.h>
#include <maya/MImageFileInfo.h>
#include <maya/MViewport2Renderer.h>
#include <maya/MDrawContext.h>
#include <maya/MRenderUtilities.h>
#include <maya/MTextureManager.h>
#include <maya/MUIDrawManager.h>
#include <sys/stat.h>
#define DEBUG_TEXTURE_CACHE(X)
template <class T> class ObjArray
{
public:
inline ObjArray() : fLength( 0), fCapacity( 0), fData( NULL) {}
inline ~ObjArray() { if( fData) { destroy( 0, fLength); delete[] ((char*)fData); } }
inline int length() const { return fLength; }
inline void length( int l) { if( l > fLength) { if( l > fCapacity) resize( l); create( fLength, l); } else if( l < fLength) destroy( l, fLength); fLength = l; }
inline const T& operator[]( int i) const { return fData[ i]; }
inline T& operator[]( int i) { return fData[ i]; }
private:
inline void create( int s, int e) { for( ; s < e; s++) new ((void*)(fData + s)) T; }
inline void destroy( int s, int e) { for( ; s < e; s++) (fData + s)->T::~T(); }
enum
{
MIN_ARRAY_SIZE = 4
};
inline void resize( int l)
{
if( l < MIN_ARRAY_SIZE) l = MIN_ARRAY_SIZE;
else if( l < fCapacity * 2) l = fCapacity * 2;
T* newData = (T*)new char[ l * sizeof( T)];
if( fData)
{
memcpy( newData, fData, fCapacity * sizeof( T));
delete[] ((char*)fData);
}
fData = newData;
fCapacity = l;
}
int fLength;
int fCapacity;
T* fData;
};
void hlslDeviceManager::deviceNew()
{
fState = kValid;
fShader.setShader( fShader.fShader);
}
void hlslDeviceManager::deviceLost()
{
fState = kInvalid;
fShader.release();
}
void hlslDeviceManager::deviceDeleted()
{
fState = kInvalid;
fShader.release();
}
void hlslDeviceManager::deviceReset()
{
fState = kReset;
}
void hlslDeviceManager::resetShader()
{
fState = kValid;
fShader.setShader( fShader.fShader);
}
struct SemanticInfo
{
const char* Name;
int MinElements;
int MaxElements;
BYTE D3DType;
};
SemanticInfo gSemanticInfo[] =
{
};
class HLSLStateManager : public ID3DXEffectStateManager
{
public:
static HLSLStateManager sInstance;
inline HLSLStateManager() : fShapeCullMode(
MGeometryList::kCullCW), fShaderCullMode( D3DCULL_CCW) {}
{
fShapeCullMode = cullMode;
if( ignoreShaderCullMode) fShaderSetCullMode = false;
setupCulling();
}
STDMETHOD(SetRenderState)(THIS_ D3DRENDERSTATETYPE d3dRenderState, DWORD dwValue )
{
if( d3dRenderState == D3DRS_CULLMODE)
{
fShaderCullMode = dwValue;
fShaderSetCullMode = true;
return D3D_OK;
}
return fD3DDevice->SetRenderState( d3dRenderState, dwValue );
}
inline void BeginPass( LPD3DXEFFECT effect, int pass)
{
fShaderSetCullMode = false;
effect->BeginPass( pass);
setupCulling();
}
STDMETHOD(SetSamplerState)(THIS_ DWORD dwStage, D3DSAMPLERSTATETYPE d3dSamplerState, DWORD dwValue ) { return fD3DDevice->SetSamplerState( dwStage, d3dSamplerState, dwValue ); }
STDMETHOD(SetTextureStageState)(THIS_ DWORD dwStage, D3DTEXTURESTAGESTATETYPE d3dTextureStageState, DWORD dwValue ) { return fD3DDevice->SetTextureStageState( dwStage, d3dTextureStageState, dwValue ); }
STDMETHOD(SetTexture)(THIS_ DWORD dwStage, LPDIRECT3DBASETEXTURE9 pTexture ) { return fD3DDevice->SetTexture( dwStage, pTexture ); }
STDMETHOD(SetVertexShader)(THIS_ LPDIRECT3DVERTEXSHADER9 pShader ) { return fD3DDevice->SetVertexShader( pShader ); }
STDMETHOD(SetPixelShader)(THIS_ LPDIRECT3DPIXELSHADER9 pShader ) { return fD3DDevice->SetPixelShader( pShader ); }
STDMETHOD(SetFVF)(THIS_ DWORD dwFVF ) { return fD3DDevice->SetFVF( dwFVF ); }
STDMETHOD(SetTransform)(THIS_ D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX *pMatrix ) { return fD3DDevice->SetTransform( State, pMatrix ); }
STDMETHOD(SetMaterial)(THIS_ CONST D3DMATERIAL9 *pMaterial ) { return fD3DDevice->SetMaterial( pMaterial ); }
STDMETHOD(SetLight)(THIS_ DWORD Index, CONST D3DLIGHT9 *pLight ) { return fD3DDevice->SetLight( Index, pLight ); }
STDMETHOD(LightEnable)(THIS_ DWORD Index, BOOL Enable ) { return fD3DDevice->LightEnable( Index, Enable ); }
STDMETHOD(SetNPatchMode)(THIS_ FLOAT NumSegments ) { return fD3DDevice->SetNPatchMode( NumSegments ); }
STDMETHOD(SetVertexShaderConstantF)(THIS_ UINT RegisterIndex, CONST FLOAT *pConstantData, UINT RegisterCount ) { return fD3DDevice->SetVertexShaderConstantF( RegisterIndex, pConstantData, RegisterCount ); }
STDMETHOD(SetVertexShaderConstantI)(THIS_ UINT RegisterIndex, CONST INT *pConstantData, UINT RegisterCount ) { return fD3DDevice->SetVertexShaderConstantI( RegisterIndex, pConstantData, RegisterCount ); }
STDMETHOD(SetVertexShaderConstantB)(THIS_ UINT RegisterIndex, CONST BOOL *pConstantData, UINT RegisterCount ) { return fD3DDevice->SetVertexShaderConstantB( RegisterIndex, pConstantData, RegisterCount ); }
STDMETHOD(SetPixelShaderConstantF)(THIS_ UINT RegisterIndex, CONST FLOAT *pConstantData, UINT RegisterCount ) { return fD3DDevice->SetPixelShaderConstantF( RegisterIndex, pConstantData, RegisterCount ); }
STDMETHOD(SetPixelShaderConstantI)(THIS_ UINT RegisterIndex, CONST INT *pConstantData, UINT RegisterCount ) { return fD3DDevice->SetPixelShaderConstantI( RegisterIndex, pConstantData, RegisterCount ); }
STDMETHOD(SetPixelShaderConstantB)(THIS_ UINT RegisterIndex, CONST BOOL *pConstantData, UINT RegisterCount ) { return fD3DDevice->SetPixelShaderConstantB( RegisterIndex, pConstantData, RegisterCount ); }
STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) { if (iid == IID_IUnknown || iid == IID_ID3DXEffectStateManager) { *ppv = static_cast<ID3DXEffectStateManager*>(this); } else { *ppv = NULL; return E_NOINTERFACE; } reinterpret_cast<IUnknown*>(this)->AddRef(); return S_OK; }
STDMETHOD_(ULONG, AddRef)(THIS) { return 1L; }
STDMETHOD_(ULONG, Release)(THIS) { return 1L; }
LPDIRECT3DDEVICE9 fD3DDevice;
protected:
inline void setupCulling()
{
DWORD d3dCullMode = D3DCULL_NONE;
if( !fShaderSetCullMode)
{
d3dCullMode = D3DCULL_CW;
d3dCullMode = D3DCULL_CCW;
}
else if( fShaderCullMode == D3DCULL_CCW)
{
}
else if( fShaderCullMode == D3DCULL_CW)
{
}
fD3DDevice->SetRenderState( D3DRS_CULLMODE, d3dCullMode);
}
DWORD fShaderCullMode;
bool fShaderSetCullMode;
};
HLSLStateManager HLSLStateManager::sInstance;
{
public:
static HLSLTextureManager sInstance;
HLSLTextureManager()
{
}
{
User* user = NULL;
for( int i = users.length(); i--; )
{
if( users[ i].parameter == d3dParameter && users[ i].effect == d3dEffect)
{
user = &users[ i];
break;
}
}
if( !user && name.
length() > 0)
{
int idx = users.length();
users.length( idx + 1);
user = &users[ idx];
DEBUG_TEXTURE_CACHE( printf(
"Registering user %s at %d\n", uniform.
name().
asChar(), (int)((
long long)user)); )
user->parameter = d3dParameter;
user->effect = d3dEffect;
user->uniform = uniform;
}
if( user && user->texture)
{
Texture* texture = user->texture;
if( texture->name == name)
{
DEBUG_TEXTURE_CACHE( printf(
"Reloading %s\n", name.
asChar()); )
if( texture->texture)
{
texture->texture->Release();
texture->texture = NULL;
for( int i = users.length(); i--; )
{
if( &users[ i] != user && users[ i].texture == texture)
{
DEBUG_TEXTURE_CACHE( printf(
"Breaking texture reference for shared entry %s to avoid duplicate reloads\n", users[ i].uniform.
name().
asChar()); )
users[ i].texture = NULL;
texture->numUsers--;
}
}
}
}
else
{
decUsage( texture);
user->texture = NULL;
}
}
if( user && !user->texture && name.
length() > 0)
{
for( int i = textures.length(); i--; )
{
if( textures[ i].name == name && textures[ i].type == uniform.
type())
{
DEBUG_TEXTURE_CACHE( printf(
"Sharing %s\n", textures[ i].name.
asChar()); )
user->texture = &textures[ i];
user->texture->numUsers++;
break;
}
}
}
if( user && !user->texture && name.
length() > 0)
{
Texture* oldMem = &textures[ 0];
DEBUG_TEXTURE_CACHE( printf(
"Creating %s\n", name.
asChar()); )
int idx = textures.length();
textures.length( idx + 1);
Texture* newMem = &textures[ 0];
if( oldMem != newMem)
for( int i = users.length(); i--; )
if( users[ i].texture)
users[ i].texture = (Texture*)((char*)users[ i].texture + ((char*)newMem - (char*)oldMem));
Texture* texture = &textures[ idx];
texture->name = name;
texture->type = uniform.
type();
texture->numUsers = 1;
user->texture = texture;
}
if( user && user->texture && !user->texture->texture)
{
if (pRenderer != NULL) {
IDirect3DDevice9* d3dDevice = pRenderer->
getD3D9Device();
if(d3dDevice != NULL)
{
DEBUG_TEXTURE_CACHE( printf(
"Loading %s\n", name.
asChar()); )
{
LPDIRECT3DCUBETEXTURE9 d3dTexture;
if( D3DXCreateCubeTextureFromFile(d3dDevice, name.
asChar(), &d3dTexture) == D3D_OK)
user->texture->texture = d3dTexture;
}
{
LPDIRECT3DVOLUMETEXTURE9 d3dTexture;
if( D3DXCreateVolumeTextureFromFile( d3dDevice, name.
asChar(), &d3dTexture) == D3D_OK)
user->texture->texture = d3dTexture;
}
else
{
LPDIRECT3DTEXTURE9 d3dTexture;
if( D3DXCreateTextureFromFile(d3dDevice, name.
asChar(), &d3dTexture) == D3D_OK)
user->texture->texture = d3dTexture;
}
}
}
}
d3dEffect->SetTexture( d3dParameter, (user && user->texture) ? user->texture->texture : NULL);
}
void release( LPD3DXEFFECT effect)
{
for( int i = users.length(); i--; )
{
if( users[ i].effect == effect)
{
DEBUG_TEXTURE_CACHE( printf(
"Releasing effect usage %s\n", users[ i].uniform.
name().
asChar()); )
if( users[ i].texture) decUsage( users[ i].texture);
int newLength = users.length() - 1;
if( i != newLength)
users[ i] = users[ newLength];
users.length( newLength);
}
}
}
static void newSceneCallback( void* ptr) { ((HLSLTextureManager*)ptr)->release(); }
struct Texture
{
inline Texture() : numUsers( 0), texture( NULL) {}
LPDIRECT3DBASETEXTURE9 texture;
int numUsers;
};
struct User
{
inline User() : parameter( NULL), effect( NULL), texture( NULL) {}
D3DXHANDLE parameter;
LPD3DXEFFECT effect;
Texture* texture;
};
ObjArray<Texture> textures;
ObjArray<User> users;
MCallbackId sceneCallbackIds[ 2];
void release()
{
DEBUG_TEXTURE_CACHE( printf( "Releasing cache with %d textures and %d users\n", textures.length(), users.length()); )
for( int i = users.length(); i--; )
for( int i = textures.length(); i--; )
if( textures[ i].texture)
textures[ i].texture->Release();
users.length( 0);
textures.length( 0);
}
void decUsage( Texture* texture)
{
if( --texture->numUsers == 0)
{
DEBUG_TEXTURE_CACHE( printf( "Unloading %s\n", texture->name.asChar()); )
if( texture->texture)
texture->texture->Release();
Texture* lastTexture = &textures[ textures.length() - 1];
if( texture != lastTexture)
{
*texture = *lastTexture;
for( int i = users.length(); i--; )
if( users[ i].texture == lastTexture)
users[ i].texture = texture;
}
textures.length( textures.length() - 1);
}
DEBUG_TEXTURE_CACHE( else printf( "Unsharing %s\n", texture->name.asChar()); )
}
};
HLSLTextureManager HLSLTextureManager::sInstance;
bool hlslShader::GetAnnotation( D3DXHANDLE parameter, const char* name, LPCSTR& value)
{
D3DXHANDLE d3dSemantic = fD3DEffect->GetAnnotationByName( parameter, name);
return d3dSemantic && fD3DEffect->GetString( d3dSemantic, &value) == D3D_OK && value != NULL;
}
bool hlslShader::GetBOOLAnnotation( D3DXHANDLE parameter, const char* name, BOOL& value)
{
D3DXHANDLE d3dSemantic = fD3DEffect->GetAnnotationByName( parameter, name);
return d3dSemantic && fD3DEffect->GetBool( d3dSemantic, &value) == D3D_OK;
}
bool hlslShader::GetAnnotation( D3DXHANDLE parameter, const char* name, float& value)
{
D3DXHANDLE d3dSemantic = fD3DEffect->GetAnnotationByName( parameter, name);
return d3dSemantic && fD3DEffect->GetFloat( d3dSemantic, &value) == D3D_OK;
}
bool hlslShader::GetAnnotation( D3DXHANDLE parameter, const char* name, int& value)
{
D3DXHANDLE d3dSemantic = fD3DEffect->GetAnnotationByName( parameter, name);
return d3dSemantic && fD3DEffect->GetInt( d3dSemantic, &value) == D3D_OK;
}
{
switch( description.Type)
{
case D3DXPT_TEXTURE:
{
LPCSTR textureType;
if( ( GetAnnotation( parameter, "TextureType", textureType) || GetAnnotation( parameter, "ResourceType", textureType)) && textureType)
{
else
{
fDiagnostics += "Unrecognised texture type semantic ";
fDiagnostics += textureType;
fDiagnostics += " on parameter ";
fDiagnostics += description.Name;
fDiagnostics += "\n";
}
}
{
fDiagnostics += "No texture type provided for ";
fDiagnostics += description.Name;
fDiagnostics += ", assuming 2D\n";
}
break;
}
default: break;
}
return mtype;
}
{
LPCSTR ann;
if( GetAnnotation( parameter, "Space", ann) && ann)
{
else
{
fDiagnostics += "Unrecognised space ";
fDiagnostics += ann;
fDiagnostics += " on parameter ";
fDiagnostics += description.Name;
fDiagnostics += "\n";
}
}
return space;
}
{
if( description.Semantic && *description.Semantic)
{
else
{
fDiagnostics += "Unrecognised semantic ";
fDiagnostics += description.Semantic;
fDiagnostics += " on parameter ";
fDiagnostics += description.Name;
fDiagnostics += "\n";
}
}
{
LPCSTR sasSemantic;
if( GetAnnotation( parameter, "SasBindAddress", sasSemantic) && *sasSemantic)
{
else if( str.rindexW(
".Direction") >= 0 && str.rindexW(
".Direction") != str.rindexW(
".Directional")) msemantic = ConvertSpace( parameter, description,
MUniformParameter::kSemanticViewDir);
else
{
fDiagnostics += "Unrecognised semantic ";
fDiagnostics += sasSemantic;
fDiagnostics += " on parameter ";
fDiagnostics += description.Name;
fDiagnostics += "\n";
}
}
}
{
LPCSTR sasSemantic;
if( GetAnnotation( parameter, "SasUiControl", sasSemantic) && *sasSemantic)
{
}
}
{
else if( name.rindexW(
"Direction") >= 0 && name.rindexW(
"Direction") != name.rindexW(
"Directional")) msemantic = ConvertSpace( parameter, description,
MUniformParameter::kSemanticWorldDir);
else if( name.rindexW( "Color") >= 0 || name.rindexW( "Colour") >= 0 || name.rindexW( "Diffuse") >= 0 || name.rindexW( "Specular") >= 0 || name.rindexW( "Ambient") >= 0)
}
return msemantic;
}
MTypeId hlslShader::sId( 0xF3560C30 );
#define M_CHECK(assertion) if (assertion) ; else throw ((hlsl::InternalError*)__LINE__)
namespace hlsl
{
struct InternalError
{
char* message;
};
}
hlslShader::hlslShader()
: fErrorCount( 0), fD3DEffect( NULL), fD3DTechnique( NULL),
fD3DVertexDeclaration( NULL), fVertexStructure(
"VertexStructure",
MVaryingParameter::kStructure),
fDeviceManager( me()),
fTechniqueHasBlending( false ),
fPostDuplicateCallbackId( NULL )
{
}
void
hlslShader::postConstructor()
{
}
hlslShader::~hlslShader()
{
release();
}
void hlslShader::release()
{
releaseVertexDeclaration();
if( fD3DEffect)
{
HLSLTextureManager::sInstance.release( fD3DEffect);
fD3DEffect->Release();
fD3DEffect = NULL;
}
fD3DTechnique = NULL;
fTechniqueHasBlending = false;
}
void hlslShader::releaseVertexDeclaration()
{
if( fD3DVertexDeclaration)
{
for( unsigned int p = 0; p < fD3DTechniqueDesc.Passes; p++)
if( fD3DVertexDeclaration[ p])
fD3DVertexDeclaration[ p]->Release();
delete[] fD3DVertexDeclaration;
fD3DVertexDeclaration = NULL;
}
}
void* hlslShader::creator()
{
return new hlslShader();
}
hlslShader::initialize()
{
try
{
initializeNodeAttrs();
}
catch ( ... )
{
ms = MS::kFailure;
}
return ms;
}
void
hlslShader::initializeNodeAttrs()
{
M_CHECK( stat );
stat = addAttribute(sShader);
M_CHECK( stat );
M_CHECK( stat );
stat = addAttribute(sTechnique);
M_CHECK( stat );
M_CHECK( stat );
stat = addAttribute(sTechniques);
M_CHECK( stat );
M_CHECK( stat );
stat = addAttribute(sDescription);
M_CHECK( stat );
M_CHECK( stat );
stat = addAttribute(sDiagnostics);
M_CHECK( stat );
attributeAffects( sShader, sTechniques);
attributeAffects( sShader, sTechnique);
}
void
hlslShader::copyInternalData(
MPxNode* pSrc )
{
const hlslShader& src = *(hlslShader*)pSrc;
fTechnique = src.fTechnique;
fTechniqueHasBlending = src.fTechniqueHasBlending;
fShader = src.fShader;
}
bool hlslShader::setInternalValue(
const MPlug & plug,
const MDataHandle & handle)
{
bool retVal = true;
try
{
if (plug == sShader)
{
}
else if (plug == sTechnique)
{
}
else
{
}
}
catch ( ... )
{
reportInternalError( __FILE__, __LINE__ );
retVal = false;
}
return retVal;
}
{
bool retVal = true;
try
{
if (plug == sShader)
{
}
else if (plug == sTechnique)
{
}
else if (plug == sTechniques)
{
}
else
{
}
}
catch ( ... )
{
reportInternalError( __FILE__, __LINE__ );
retVal = false;
}
return retVal;
}
{
if (pRenderer == NULL)
{
}
IDirect3DDevice9* d3dDevice = pRenderer->getD3D9Device();
if ( NULL == d3dDevice )
{
}
if (fDeviceManager.deviceState() == hlslDeviceManager::kReset)
{
fDeviceManager.resetShader();
}
HLSLStateManager::sInstance.fD3DDevice = d3dDevice;
if( fD3DEffect && fD3DVertexDeclaration)
{
UINT numPasses = 0;
fD3DEffect->Begin( &numPasses, 0);
bool firstShape = true;
{
for( int u = fUniformParameters.length(); u--; )
{
{
D3DXHANDLE d3dParameter = (D3DXHANDLE)uniform.
userData();
if( d3dParameter)
{
{
{
if( data) fD3DEffect->SetValue( d3dParameter, data, uniform.
numElements() *
sizeof( float));
}
break;
fD3DEffect->SetInt( d3dParameter, uniform.
getAsInt( iter));
break;
fD3DEffect->SetBool( d3dParameter, uniform.
getAsBool( iter));
break;
break;
default:
HLSLTextureManager::sInstance.bind( uniform.
getAsString( iter).
asChar(), uniform, fD3DEffect, d3dParameter);
break;
}
}
}
}
const void* data;
unsigned int elements, count;
if( fVertexStructure.numElements() && fVertexStructure.getBuffer( geometry, data, elements, count) ==
MStatus::kSuccess)
{
HLSLStateManager::sInstance.shapeCullMode( iter.
cullMode());
for( UINT p = 0; p < numPasses; p++)
{
if( !fD3DVertexDeclaration[ p]) continue;
if( numPasses > 1 || firstShape)
HLSLStateManager::sInstance.BeginPass( fD3DEffect, p);
else
fD3DEffect->CommitChanges();
firstShape = false;
d3dDevice->SetVertexDeclaration( fD3DVertexDeclaration[ p]);
#ifdef MAX_SEGMENTED_BATCH_SIZE
unsigned int batchSize = numPrimitives / (numPrimitives / MAX_SEGMENTED_BATCH_SIZE + 1) + 1;
for( unsigned int start = 0; start < numPrimitives; )
{
unsigned int end = start + batchSize;
if( end > numPrimitives) end = numPrimitives;
d3dDevice->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST, 0, count, end - start, ((
const unsigned int*)primitives.
data()) + (start * 3), D3DFMT_INDEX32, data, elements);
start = end;
}
#else
d3dDevice->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST, 0, count, primitives.
elementCount() / 3, primitives.
data(), D3DFMT_INDEX32, data, elements);
#endif
if( numPasses > 1) fD3DEffect->EndPass();
}
}
}
if( numPasses == 1) fD3DEffect->EndPass();
fD3DEffect->End();
}
else
{
D3DMATERIAL9 Material;
Material.Emissive.r = 0.0f;
Material.Emissive.g = 0.6f;
Material.Emissive.b = 0.0f;
Material.Emissive.a = 1.0f;
d3dDevice->SetMaterial( &Material);
d3dDevice->LightEnable( 0, false);
d3dDevice->LightEnable( 1, false);
d3dDevice->SetFVF( D3DFVF_XYZ );
{
{
d3dDevice->SetTransform( D3DTS_WORLD, &tm);
D3DXMATRIXA16 projection( (
float)mproject.
matrix[0][0], (
float)mproject.
matrix[0][1], (
float)mproject.
matrix[0][2], (
float)mproject.
matrix[0][3],
(
float)mproject.
matrix[3][0], (
float)mproject.
matrix[3][1], (
float)mproject.
matrix[3][2], (
float)mproject.
matrix[3][3]);
d3dDevice->SetTransform( D3DTS_PROJECTION, &projection );
D3DXMATRIXA16 view( (
float)mview.
matrix[0][0], (
float)mview.
matrix[0][1], (
float)mview.
matrix[0][2], (
float)mview.
matrix[0][3],
d3dDevice->SetTransform( D3DTS_VIEW, &view );
HLSLStateManager::sInstance.shapeCullMode( iter.
cullMode(),
true);
d3dDevice->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST, 0, position.
elementCount(), primitives.
elementCount() / 3, primitives.
data(), D3DFMT_INDEX32, position.
data(), 3 *
sizeof( float));
}
else
{
}
}
}
}
unsigned int hlslShader::transparencyOptions()
{
if (fTechniqueHasBlending)
return ( kIsTransparent | kNoTransparencyFrontBackCull | kNoTransparencyPolygonSort );
return 0;
}
void hlslShader::setupUniform( D3DXHANDLE d3dParameter,
const MString& prefix)
{
if( d3dParameter)
{
D3DXPARAMETER_DESC parameterDesc;
fD3DEffect->GetParameterDesc( d3dParameter, ¶meterDesc);
if( parameterDesc.Class == D3DXPC_STRUCT)
{
for( unsigned int p = 0; p < parameterDesc.StructMembers; p++)
{
setupUniform( fD3DEffect->GetParameter( d3dParameter, p), prefix +
MString( parameterDesc.Name) +
".");
}
}
else
{
if( parameterDesc.Semantic && !_stricmp( parameterDesc.Semantic, "SasGlobal"))
{
LPCSTR sasDescription;
if( GetAnnotation( d3dParameter, "SasEffectDescription", sasDescription) && *sasDescription)
fDescription += sasDescription;
return;
}
else if( parameterDesc.Name && !_stricmp( parameterDesc.Name, "description") && parameterDesc.Type == D3DXPT_STRING)
{
LPCSTR Value;
if( fD3DEffect->GetString( d3dParameter, &Value) == D3D_OK)
fDescription += Value;
return;
}
{
int rows = parameterDesc.Rows;
int columns = parameterDesc.Columns;
MString uiName = prefix + parameterDesc.Name;
LPCSTR uiNameValue = NULL ;
bool hasUIName = GetAnnotation(d3dParameter, "UIName", uiNameValue) && uiNameValue;
MUniformParameter uniform( uiName, type, semantic, rows, columns, (
void*)d3dParameter);
if (hasUIName)
updateUIVisibilityFromAnnotation( d3dParameter, uniform );
updateRangeFromAnnotation( d3dParameter, uniform );
switch( type)
{
{
int length = rows * columns;
FLOAT* Value = new FLOAT[ length];
if( Value)
{
if( fD3DEffect->GetFloatArray( d3dParameter, Value, length) == D3D_OK)
delete [] Value;
}
break;
}
default:
{
LPCSTR resource;
if( GetAnnotation( d3dParameter, "ResourceName", resource) && *resource)
else if( GetAnnotation( d3dParameter, "SasResourceAddress", resource) && *resource)
}
}
fUniformParameters.append( uniform);
}
}
}
}
void hlslShader::updateUIVisibilityFromAnnotation( D3DXHANDLE d3dParameter,
MUniformParameter& uniform )
{
BOOL visible = true;
if( GetBOOLAnnotation( d3dParameter, "SasUiVisible", visible) )
{
}
else
{
LPCSTR uiTypeValue = NULL;
bool foundUIType = GetAnnotation( d3dParameter, "UIType", uiTypeValue);
if (foundUIType && !_stricmp(uiTypeValue, "None"))
{
}
LPCSTR uiWidgetValue = NULL;
foundUIType = GetAnnotation( d3dParameter, "UIWidget", uiWidgetValue);
if (foundUIType && !_stricmp(uiWidgetValue, "None"))
{
}
}
}
void hlslShader::updateRangeFromAnnotation( D3DXHANDLE d3dParameter,
MUniformParameter& uniform)
{
{
{
float uiMinFloat = NULL;
if(GetAnnotation(d3dParameter, "SasUiMin",uiMinFloat))
{
double uiMin = uiMinFloat;
}
else if(GetAnnotation(d3dParameter, "UIMin",uiMinFloat))
{
double uiMin = uiMinFloat;
}
else if(GetAnnotation(d3dParameter, "uimin",uiMinFloat))
{
double uiMin = uiMinFloat;
}
float uiMaxFloat = NULL;
if(GetAnnotation(d3dParameter, "SasUiMax",uiMaxFloat))
{
double uiMax = uiMaxFloat;
}
else if(GetAnnotation(d3dParameter, "UIMax",uiMaxFloat))
{
double uiMax = uiMaxFloat;
}
else if(GetAnnotation(d3dParameter, "uimax",uiMaxFloat))
{
double uiMax = uiMaxFloat;
}
}
break;
{
int uiMinInt = NULL;
if(GetAnnotation(d3dParameter, "SasUiMin",uiMinInt))
{
double uiMin = uiMinInt;
}
else if(GetAnnotation(d3dParameter, "UIMin",uiMinInt))
{
double uiMin = uiMinInt;
}
else if(GetAnnotation(d3dParameter, "uimin",uiMinInt))
{
double uiMin = uiMinInt;
}
int uiMaxInt = NULL;
if(GetAnnotation(d3dParameter, "SasUiMax",uiMaxInt))
{
double uiMax = uiMaxInt;
}
else if(GetAnnotation(d3dParameter, "UIMax",uiMaxInt))
{
double uiMax = uiMaxInt;
}
else if(GetAnnotation(d3dParameter, "uimax",uiMaxInt))
{
double uiMax = uiMaxInt;
}
}
break;
default:
break;
};
}
void hlslShader::postDuplicateCB(void* data)
{
hlslShader* shader = (hlslShader*)data;
shader->fPostDuplicateCallbackId = NULL;
shader->setShader(shader->fShader);
}
{
fDiagnostics = "";
fDescription = "";
fShader = shader;
if (pRenderer != NULL) {
IDirect3DDevice9* pDevice = pRenderer->getD3D9Device();
if( pDevice )
{
release();
fTechniques.setLength( 0);
LPD3DXBUFFER errors = NULL;
struct stat statBuf;
if (stat(shader.
asChar(), &statBuf) == -1)
{
if(shader.
index(
'/') == 0 || shader.
index(
'\\') == 0)
else
shaderSimpleName = shader;
}
else
{
shaderSimpleName = shader;
}
int idx = shaderSimpleName.
rindex(
'/');
if(idx == -1)
{
shaderName = shaderSimpleName;
}
else
{
shaderName = shaderSimpleName.
substring(idx + 1, shaderSimpleName.length() - 1);
}
TCHAR pwd[ MAX_PATH];
if( resolvedPath.
length() > 0)
{
::GetCurrentDirectory( MAX_PATH, pwd);
::SetCurrentDirectory( resolvedPath.
asChar());
}
#ifndef D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY
#define D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY 0
#endif
if( FAILED( D3DXCreateEffectFromFile( pDevice, shaderName.
asChar(), NULL, NULL, D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &fD3DEffect, &errors))
#ifdef D3DXSHADER_USE_LEGACY_D3DX9_31_DLL
&& FAILED( D3DXCreateEffectFromFile( pDevice, shaderName.
asChar(), NULL, NULL, D3DXSHADER_USE_LEGACY_D3DX9_31_DLL, NULL, &fD3DEffect, NULL))
#endif
)
{
fDiagnostics += "Error trying to create effect " + shader + ":\n\t";
if( errors)
fDiagnostics += (const char*)errors->GetBufferPointer();
fDiagnostics += "\n";
}
else
{
if( errors)
fDiagnostics += (const char*)errors->GetBufferPointer();
fD3DEffect->SetStateManager( &HLSLStateManager::sInstance);
fD3DEffect->GetDesc( &fD3DEffectDesc);
for( unsigned int t = 0; t < fD3DEffectDesc.Techniques; t++)
{
D3DXHANDLE technique = fD3DEffect->GetTechnique( t);
if( technique)
{
#ifdef VALIDATE_TECHNIQUE_LIST
if( fD3DEffect->ValidateTechnique( technique))
#endif
{
D3DXTECHNIQUE_DESC techniqueDesc;
fD3DEffect->GetTechniqueDesc( technique, &techniqueDesc);
fTechniques.append( techniqueDesc.Name);
}
}
}
}
if( resolvedPath.
length() > 0)
{
::SetCurrentDirectory( pwd);
}
if( fD3DEffect)
{
fUniformParameters.setLength( 0);
for( unsigned int p = 0; p < fD3DEffectDesc.Parameters; p++)
{
setupUniform( fD3DEffect->GetParameter( NULL, p), "");
}
setUniformParameters( fUniformParameters);
}
}
}
MPlug diagnosticsPlug( thisMObject(), sDiagnostics);
diagnosticsPlug.setValue( fDiagnostics);
MPlug descriptionPlug( thisMObject(), sDescription);
descriptionPlug.setValue( fDescription);
return MS::kSuccess;
}
bool hlslShader::passHasTranparency( D3DXHANDLE d3dPass )
{
bool hasTransparency = false;
BOOL boolParameter;
INT intParameter1, intParameter2;
boolParameter = false;
{
BOOL getAnnot = GetBOOLAnnotation( d3dPass, "Zenable", boolParameter);
getAnnot = GetBOOLAnnotation( d3dPass, "ZWriteEnable", boolParameter);
}
BOOL getAnnot = false;
{
D3DXHANDLE d3dSemantic = fD3DEffect->GetAnnotationByName( d3dPass, "AlphaBlendEnable");
if (d3dSemantic)
{
fD3DEffect->GetBool( d3dSemantic, &boolParameter);
getAnnot = true;
}
}
if (getAnnot && boolParameter)
{
hasTransparency = true;
{
D3DXHANDLE d3dSemantic = fD3DEffect->GetAnnotationByName( d3dPass, "SrcBlend");
if (d3dSemantic)
fD3DEffect->GetInt( d3dSemantic, &intParameter1);
}
{
D3DXHANDLE d3dSemantic = fD3DEffect->GetAnnotationByName( d3dPass, "DestBlend");
if (d3dSemantic)
fD3DEffect->GetInt( d3dSemantic, &intParameter2);
}
}
return hasTransparency;
}
{
releaseVertexDeclaration();
if( fD3DEffect && fTechniques.length())
{
fD3DTechnique = fD3DEffect->GetTechniqueByName( technique.
asChar());
#ifdef VALIDATE_TECHNIQUES
if( !fD3DTechnique || !fD3DEffect->ValidateTechnique( fD3DTechnique))
#else
if( !fD3DTechnique)
#endif
{
fD3DTechnique = fD3DEffect->GetTechniqueByName( fTechnique.asChar());
#ifdef VALIDATE_TECHNIQUES
if( !fD3DTechnique || !fD3DEffect->ValidateTechnique( fD3DTechnique))
#else
if( !fD3DTechnique)
#endif
{
#ifdef VALIDATE_TECHNIQUES
if( FAILED( fD3DEffect->FindNextValidTechnique( fD3DTechnique, &fD3DTechnique)))
{
fD3DTechnique = NULL;
}
#else
fD3DTechnique = fD3DEffect->GetTechnique( 0);
#endif
}
}
fD3DEffect->GetTechniqueDesc( fD3DTechnique, &fD3DTechniqueDesc);
fTechnique = fD3DTechniqueDesc.Name;
fD3DEffect->SetTechnique( fD3DTechnique);
fVertexStructure.removeElements();
fD3DVertexDeclaration = new IDirect3DVertexDeclaration9*[ fD3DTechniqueDesc.Passes];
int offset = 0;
for( unsigned int p = 0; p < fD3DTechniqueDesc.Passes; p++)
{
D3DXHANDLE d3dPass = fD3DEffect->GetPass( fD3DTechnique, p);
#if defined(_BLENDPARSING_READY_)
if (p == 0)
fTechniqueHasBlending = passHasTranparency( d3dPass );
#endif
D3DXPASS_DESC passDesc;
fD3DEffect->GetPassDesc( d3dPass, &passDesc);
D3DXSEMANTIC Semantics[ MAXD3DDECLLENGTH];
D3DVERTEXELEMENT9 VertexElements[ MAXD3DDECLLENGTH + 1];
UINT NumSemantics = 0;
D3DXGetShaderInputSemantics( passDesc.pVertexShaderFunction, Semantics, &NumSemantics);
for( unsigned int s = 0; s < NumSemantics; s++)
{
const SemanticInfo& Info = gSemanticInfo[ Semantics[ s].Usage];
Name += Semantics[ s].UsageIndex;
fVertexStructure.addElement( varying);
VertexElements[ s].Stream = 0;
VertexElements[ s].Offset = (WORD)offset;
VertexElements[ s].Type = Info.D3DType;
VertexElements[ s].Method = D3DDECLMETHOD_DEFAULT;
VertexElements[ s].Usage = (BYTE)Semantics[ s].Usage;
VertexElements[ s].UsageIndex = (BYTE)Semantics[ s].UsageIndex;
offset += varying.getMaximumStride();
}
static D3DVERTEXELEMENT9 VertexElementEnd = D3DDECL_END();
VertexElements[ NumSemantics] = VertexElementEnd;
fD3DVertexDeclaration[p] = NULL;
if (pRenderer != NULL) {
IDirect3DDevice9* d3dDevice = pRenderer->getD3D9Device();
if( d3dDevice) {
d3dDevice->CreateVertexDeclaration( VertexElements, &fD3DVertexDeclaration[ p]);
}
}
}
if( fVertexStructure.numElements() > 0)
{
list.
append( fVertexStructure);
}
setVaryingParameters( list);
}
return MS::kSuccess;
}
void hlslShader::reportInternalError( const char* function, size_t errcode )
{
try
{
if ( this )
{
if ( ++fErrorCount > ERROR_LIMIT )
return;
s += "\"";
s += name();
s += "\": ";
s += typeName();
es = s;
}
}
catch ( ... )
{}
es += " internal error ";
es += (int)errcode;
es += " in ";
es += function;
}
{
{
break;
break;
break;
break;
default:
break;
}
}
else {
for (
unsigned int i = 0; i < parameter.
numElements(); ++i) {
parameterRequirements(parameter.
getElement(i), requirements);
}
}
}
{
#if defined _USE_OPENGL_ //
if (pRenderer)
{
MGeometryRequirements requirements;
requirements.addNormal();
requirements.addTexCoord(
MString(
"map1"));
requirements.addComponentId();
if(geomIter == NULL) {
}
unsigned int width, height;
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
if( status2 != MS::kSuccess ) {
}
{
static float specular[] = { 0.7f, 0.7f, 0.7f, 1.0f};
static float shine[] = { 100.0f };
static float ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shine);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
float lightPos[4];
static float default_ambient_light[]={0.4f, 0.4f, 0.4f, 1.0f};
static float default_diffuse_light[]={0.8f, 0.8f, 0.8f, 1.0f};
static float default_specular_light[]={1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, default_ambient_light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, default_diffuse_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, default_specular_light);
glPushMatrix();
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_LIGHT0);
glPopMatrix();
}
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double l, r, b, t, n, f;
glFrustum( l, r, b, t, n, f );
}
float r, g, b, a;
glClearColor( r, g, b, a );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
{
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd(mtm[0]);
if (pos.
data() != NULL && primitives.
data() != NULL) {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, (
float*) pos.
data());
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, normal.
data());
}
bool boundTexture = false;
for( int u = fUniformParameters.length(); u--; ){
{
{
boundTexture = true;
break;
}
}
}
}
if (uvCoord.
data() && boundTexture) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, uvCoord.
data());
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
else {
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glColor4f(0.1f, 0.7f, 0.7f, 1.0f);
}
glDrawElements(GL_TRIANGLES, primitives.
elementCount(), GL_UNSIGNED_INT, primitives.
data());
glPopAttrib();
glPopClientAttrib();
}
}
}
#else
unsigned int width, height;
ShaderContext context;
populateRequirements(context, requirements);
if (Render != NULL) {
requirements);
if (geomIter == NULL) {
return status;
}
}
}
#endif
return status;
}
{
if( !fD3DEffect )
{
}
unsigned int nVarying = fVertexStructure.numElements();
for( unsigned int i = 0; i < nVarying; i++ )
{
{
}
}
{
attrName += "_DefaultTexture";
MPlug defaultTexPlug = depFn.findPlug( attrName );
if( !defaultTexPlug.
isNull() )
{
}
}
unsigned int nUniform = fUniformParameters.length();
unsigned int nUVParams = uvParams.
length();
for( unsigned int i = 0; i < nUVParams; i++ )
{
const MString& varName = uvParams[i];
for( unsigned int j = 0; j < nUniform; j++ )
{
{
continue;
}
LPCSTR uvLinkAnnot;
D3DXHANDLE d3dParameter = (D3DXHANDLE) elem.
userData();
if( !GetAnnotation(d3dParameter, "UVLink", uvLinkAnnot) )
{
continue;
}
if( uvLink == varName )
{
if( elem.
name() == defaultTex )
{
}
else
{
}
}
}
}
if( imageNames.
length() == 0 )
{
for( unsigned int i = 0; i < nUniform; i++ )
{
{
if( elem.
name() == defaultTex )
{
}
else
{
}
}
}
}
}
floatRegion region,
int &imageWidth,
int &imageHeight )
{
bool foundParam = false;
unsigned int nUniform = fUniformParameters.length();
for( unsigned int i = 0; i < nUniform; i++ )
{
if( elem.
name() == imageName )
{
imageParam = elem;
foundParam = true;
break;
}
}
if( !foundParam )
{
}
{
}
glPushAttrib( GL_ALL_ATTRIB_BITS );
glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT);
{
glPopAttrib();
glPopClientAttrib();
sMsg += fnNode.name();
sMsg += " : failed to load texture \"";
sMsg += imageName;
sMsg += "\".";
}
GLint width = 0;
GLint height = 0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBegin( GL_QUADS );
glTexCoord2f(region[0][0], region[0][1]);
glVertex2f(region[0][0], region[0][1]);
glTexCoord2f(region[0][0], region[1][1]);
glVertex2f(region[0][0], region[1][1]);
glTexCoord2f(region[1][0], region[1][1]);
glVertex2f(region[1][0], region[1][1]);
glTexCoord2f(region[1][0], region[0][1]);
glVertex2f(region[1][0], region[0][1]);
glEnd();
glPopAttrib();
glPopClientAttrib();
imageWidth = (int) width;
imageHeight = (int) height;
}
floatRegion region,
int& imageWidth,
int& imageHeight )
{
if (!theRenderer)
for( int i = 0; i < fUniformParameters.length(); i++ ) {
imageParam = elem;
break;
}
}
if(txtManager == NULL)
if(texture == NULL)
{
}
{
imageWidth = (int)desc.
fWidth;
}
if(region[0][0] == 0 && region[0][1] == 0 && region[1][0] == 0 && region[1][1] == 0)
{
}
positions.
append(region[0][0], region[0][1]);
positions.
append(region[1][0], region[0][1]);
positions.
append(region[1][0], region[1][1]);
positions.
append(region[0][0], region[0][1]);
positions.
append(region[1][0], region[1][1]);
positions.
append(region[0][0], region[1][1]);
uiDrawManager.
setColor( parameters.
baseColor );
}
{
if( !fD3DEffect )
{
return MS::kFailure;
}
unsigned int nUniform = fUniformParameters.length();
for( unsigned int i = 0; i < nUniform; i++ )
{
{
continue;
}
LPCSTR uvLinkAnnot;
D3DXHANDLE d3dParameter = (D3DXHANDLE) elem.
userData();
if( !GetAnnotation(d3dParameter, "UVLink", uvLinkAnnot) )
{
continue;
}
if( uvLink == varyingParamName )
{
}
}
return MS::kSuccess;
}
#define kUVLinkFlag "-uvl"
#define kUVLinkFlagLong "-uvLink"
hlslShaderCmd::hlslShaderCmd()
{}
hlslShaderCmd::~hlslShaderCmd()
{}
{
parser.getCommandArgument( 0, nodeName );
status = list.
add( nodeName );
if( status != MS::kSuccess )
{
displayError( "Invalid hlslShader node: " + nodeName );
return MS::kFailure;
}
if( depFn.typeId() != hlslShader::sId )
{
displayError( "Invalid hlslShader node: " + nodeName );
return MS::kFailure;
}
hlslShader* shader = (hlslShader*) depFn.userNode();
if( !shader )
{
displayError( "Invalid hlslShader node: " + nodeName );
return MS::kFailure;
}
if( parser.isFlagSet(kUVLinkFlag) )
{
parser.getFlagArgument( kUVLinkFlag, 0, paramName );
shader->uvLinks( paramName, textures );
setResult( textures );
}
return MS::kSuccess;
}
{
return syntax;
}
void* hlslShaderCmd::creator()
{
return new hlslShaderCmd;
}