123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557 |
- //-----------------------------------------------------------------------------
- // 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 "platform/platform.h"
- #if defined(TORQUE_OS_XENON)
- # include <xtl.h>
- #else
- # include <d3d9.h>
- #endif
- #include "gfx/D3D9/gfxD3D9Shader.h"
- #include "gfx/D3D9/gfxD3D9Device.h"
- #include "core/frameAllocator.h"
- #include "core/stream/fileStream.h"
- #include "core/util/safeDelete.h"
- #include "console/console.h"
- #include "math/mMathFn.h"
- using namespace Torque;
- extern bool gDisassembleAllShaders;
- /// D3DXInclude plugin
- class _gfxD3DXInclude : public ID3DXInclude, public StrongRefBase
- {
- private:
- Vector<String> mLastPath;
- public:
- void setPath( const String &path )
- {
- mLastPath.clear();
- mLastPath.push_back( path );
- }
- _gfxD3DXInclude() {}
- virtual ~_gfxD3DXInclude() {}
- STDMETHOD(Close)(THIS_ LPCVOID pData);
- // 360
- STDMETHOD(Open)(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes, /* OUT */ LPSTR pFullPath, DWORD cbFullPath);
- // PC
- STDMETHOD(Open)(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes)
- {
- return Open( IncludeType, pFileName, pParentData, ppData, pBytes, NULL, 0 );
- }
- };
- _gfxD3DXIncludeRef GFXD3D9Shader::smD3DXInclude = NULL;
- HRESULT _gfxD3DXInclude::Open(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName,
- LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes,
- LPSTR pFullPath, DWORD cbFullPath)
- {
- // First try making the path relative to the parent.
- Torque::Path path = Torque::Path::Join( mLastPath.last(), '/', pFileName );
- path = Torque::Path::CompressPath( path );
- if ( !Torque::FS::ReadFile( path, (void *&)*ppData, *pBytes, true ) )
- {
- // Ok... now try using the path as is.
- path = String( pFileName );
- path = Torque::Path::CompressPath( path );
- if ( !Torque::FS::ReadFile( path, (void *&)*ppData, *pBytes, true ) )
- {
- AssertISV(false, avar( "Failed to open include '%s'.", pFileName));
- return E_FAIL;
- }
- }
- // If the data was of zero size then we cannot recurse
- // into this file and DX won't call Close() below.
- //
- // So in this case don't push on the path.
- if ( *pBytes > 0 )
- mLastPath.push_back( path.getRootAndPath() );
- return S_OK;
- }
- HRESULT _gfxD3DXInclude::Close( THIS_ LPCVOID pData )
- {
- // Free the data file and pop its path off the stack.
- delete [] (U8*)pData;
- mLastPath.pop_back();
- return S_OK;
- }
- GFXD3D9ShaderConstHandle::GFXD3D9ShaderConstHandle()
- {
- clear();
- }
- const String& GFXD3D9ShaderConstHandle::getName() const
- {
- if ( mVertexConstant )
- return mVertexHandle.name;
- else
- return mPixelHandle.name;
- }
- GFXShaderConstType GFXD3D9ShaderConstHandle::getType() const
- {
- if ( mVertexConstant )
- return mVertexHandle.constType;
- else
- return mPixelHandle.constType;
- }
- U32 GFXD3D9ShaderConstHandle::getArraySize() const
- {
- if ( mVertexConstant )
- return mVertexHandle.arraySize;
- else
- return mPixelHandle.arraySize;
- }
- S32 GFXD3D9ShaderConstHandle::getSamplerRegister() const
- {
- if ( !mValid || !isSampler() )
- return -1;
- // We always store sampler type and register index in the pixelHandle,
- // sampler registers are shared between vertex and pixel shaders anyway.
- return mPixelHandle.offset;
- }
- //------------------------------------------------------------------------------
- //------------------------------------------------------------------------------
- bool GFXD3D9ShaderBufferLayout::setMatrix(const ParamDesc& pd, const GFXShaderConstType constType, const U32 size, const void* data, U8* basePointer)
- {
- PROFILE_SCOPE(GFXD3D9ShaderBufferLayout_setMatrix);
- if (pd.constType == GFXSCT_Float4x4)
- {
- // Special case, we can just blast this guy.
- AssertFatal(pd.size >= size, "Not enough room in the buffer for this data!");
- if (dMemcmp(basePointer+pd.offset, data, size) != 0)
- {
- dMemcpy(basePointer+pd.offset, data, size);
- return true;
- }
- return false;
- }
- else if (pd.constType == GFXSCT_Float4x3)
- {
- const U32 csize = 48;
- // Loop through and copy
- bool ret = false;
- U8* currDestPointer = basePointer + pd.offset;
- const U8* currSourcePointer = static_cast<const U8*>(data);
- const U8* endData = currSourcePointer + size;
- while (currSourcePointer < endData)
- {
- #ifdef TORQUE_DOUBLE_CHECK_43MATS
- Point4F col;
- ((MatrixF*)currSourcePointer)->getRow(3, &col);
- AssertFatal(col.x == 0.0f && col.y == 0.0f && col.z == 0.0f && col.w == 1.0f, "3rd row used");
- #endif
- if (dMemcmp(currDestPointer, currSourcePointer, csize) != 0)
- {
- dMemcpy(currDestPointer, currSourcePointer, csize);
- ret = true;
- }
- else if (pd.constType == GFXSCT_Float4x3)
- {
- ret = true;
- }
- currDestPointer += csize;
- currSourcePointer += sizeof(MatrixF);
- }
- return ret;
- }
- else
- {
- PROFILE_SCOPE(GFXD3D9ShaderBufferLayout_setMatrix_not4x4);
- // Figure out how big of a chunk we are copying. We're going to copy 4 columns by N rows of data
- U32 csize;
- switch (pd.constType)
- {
- case GFXSCT_Float2x2 :
- csize = 32;
- break;
- case GFXSCT_Float3x3 :
- csize = 48;
- break;
- case GFXSCT_Float3x4 :
- csize = 64;
- break;
- default:
- AssertFatal(false, "Unhandled case!");
- return false;
- break;
- }
- // Loop through and copy
- bool ret = false;
- U8* currDestPointer = basePointer+pd.offset;
- const U8* currSourcePointer = static_cast<const U8*>(data);
- const U8* endData = currSourcePointer + size;
- while (currSourcePointer < endData)
- {
- if (dMemcmp(currDestPointer, currSourcePointer, csize) != 0)
- {
- dMemcpy(currDestPointer, currSourcePointer, csize);
- ret = true;
- }
- else if (pd.constType == GFXSCT_Float4x3)
- {
- ret = true;
- }
- currDestPointer += csize;
- currSourcePointer += sizeof(MatrixF);
- }
- return ret;
- }
- }
- //------------------------------------------------------------------------------
- GFXD3D9ShaderConstBuffer::GFXD3D9ShaderConstBuffer( GFXD3D9Shader* shader,
- GFXD3D9ShaderBufferLayout* vertexLayoutF,
- GFXD3D9ShaderBufferLayout* vertexLayoutI,
- GFXD3D9ShaderBufferLayout* pixelLayoutF,
- GFXD3D9ShaderBufferLayout* pixelLayoutI )
- {
- AssertFatal( shader, "GFXD3D9ShaderConstBuffer() - Got a null shader!" );
- // We hold on to this so we don't have to call
- // this virtual method during activation.
- mDevice = static_cast<GFXD3D9Device*>( GFX )->getDevice();
-
- mShader = shader;
- // TODO: Remove buffers and layouts that don't exist for performance?
- mVertexConstBufferLayoutF = vertexLayoutF;
- mVertexConstBufferF = new GenericConstBuffer(vertexLayoutF);
- mVertexConstBufferLayoutI = vertexLayoutI;
- mVertexConstBufferI = new GenericConstBuffer(vertexLayoutI);
- mPixelConstBufferLayoutF = pixelLayoutF;
- mPixelConstBufferF = new GenericConstBuffer(pixelLayoutF);
- mPixelConstBufferLayoutI = pixelLayoutI;
- mPixelConstBufferI = new GenericConstBuffer(pixelLayoutI);
- }
- GFXD3D9ShaderConstBuffer::~GFXD3D9ShaderConstBuffer()
- {
- SAFE_DELETE(mVertexConstBufferF);
- SAFE_DELETE(mPixelConstBufferF);
- SAFE_DELETE(mVertexConstBufferI);
- SAFE_DELETE(mPixelConstBufferI);
- if ( mShader )
- mShader->_unlinkBuffer( this );
- }
- GFXShader* GFXD3D9ShaderConstBuffer::getShader()
- {
- return mShader;
- }
- // This is kind of cheesy, but I don't think templates would work well here because
- // these functions potentially need to be handled differently by other derived types
- template<class T>
- inline void GFXD3D9ShaderConstBuffer::SET_CONSTANT( GFXShaderConstHandle* handle, const T& fv, GenericConstBuffer *vBuffer, GenericConstBuffer *pBuffer )
- {
- AssertFatal(dynamic_cast<const GFXD3D9ShaderConstHandle*>(handle), "Incorrect const buffer type!");
- const GFXD3D9ShaderConstHandle* h = static_cast<const GFXD3D9ShaderConstHandle*>(handle);
- AssertFatal(h, "Handle is NULL!" );
- AssertFatal(h->isValid(), "Handle is not valid!" );
- AssertFatal(!h->isSampler(), "Handle is sampler constant!" );
- AssertFatal(!mShader.isNull(), "Buffer's shader is null!" );
- AssertFatal(!h->mShader.isNull(), "Handle's shader is null!" );
- AssertFatal(h->mShader.getPointer() == mShader.getPointer(), "Mismatched shaders!");
- if ( h->mInstancingConstant )
- {
- dMemcpy( mInstPtr+h->mPixelHandle.offset, &fv, sizeof( fv ) );
- return;
- }
- if (h->mVertexConstant)
- vBuffer->set(h->mVertexHandle, fv);
- if (h->mPixelConstant)
- pBuffer->set(h->mPixelHandle, fv);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const F32 fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2F& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3F& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4F& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const PlaneF& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const ColorF& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const S32 f)
- {
- // This is the only type that is allowed to be used
- // with a sampler shader constant type, but it is only
- // allowed to be set from GLSL.
- //
- // So we ignore it here... all other cases will assert.
- //
- if ( ((GFXD3D9ShaderConstHandle*)handle)->isSampler() )
- return;
- SET_CONSTANT(handle, f, mVertexConstBufferI, mPixelConstBufferI);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2I& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3I& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4I& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv)
- {
- SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
- }
- #undef SET_CONSTANT
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType)
- {
- AssertFatal(handle, "Handle is NULL!" );
- AssertFatal(handle->isValid(), "Handle is not valid!" );
- AssertFatal(dynamic_cast<const GFXD3D9ShaderConstHandle*>(handle), "Incorrect const buffer type!");
- const GFXD3D9ShaderConstHandle* h = static_cast<const GFXD3D9ShaderConstHandle*>(handle);
- AssertFatal(!h->isSampler(), "Handle is sampler constant!" );
- AssertFatal(h->mShader == mShader, "Mismatched shaders!");
- MatrixF transposed;
- if (matrixType == GFXSCT_Float4x3)
- {
- transposed = mat;
- }
- else
- {
- mat.transposeTo(transposed);
- }
- if (h->mInstancingConstant)
- {
- if ( matrixType == GFXSCT_Float4x4 )
- dMemcpy( mInstPtr+h->mPixelHandle.offset, mat, sizeof( mat ) );
-
- // TODO: Support 3x3 and 2x2 matricies?
- return;
- }
- if (h->mVertexConstant)
- mVertexConstBufferF->set(h->mVertexHandle, transposed, matrixType);
- if (h->mPixelConstant)
- mPixelConstBufferF->set(h->mPixelHandle, transposed, matrixType);
- }
- void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType)
- {
- AssertFatal(handle, "Handle is NULL!" );
- AssertFatal(handle->isValid(), "Handle is not valid!" );
- AssertFatal(dynamic_cast<const GFXD3D9ShaderConstHandle*>(handle), "Incorrect const buffer type!");
- const GFXD3D9ShaderConstHandle* h = static_cast<const GFXD3D9ShaderConstHandle*>(handle);
- AssertFatal(!h->isSampler(), "Handle is sampler constant!" );
- AssertFatal(h->mShader == mShader, "Mismatched shaders!");
- static Vector<MatrixF> transposed;
- if (arraySize > transposed.size())
- transposed.setSize(arraySize);
- if (matrixType == GFXSCT_Float4x3)
- {
- dMemcpy(transposed.address(), mat, arraySize * sizeof(MatrixF));
- }
- else
- {
- for (U32 i = 0; i < arraySize; i++)
- mat[i].transposeTo(transposed[i]);
- }
- // TODO: Maybe support this in the future?
- if (h->mInstancingConstant)
- return;
- if (h->mVertexConstant)
- mVertexConstBufferF->set(h->mVertexHandle, transposed.begin(), arraySize, matrixType);
- if (h->mPixelConstant)
- mPixelConstBufferF->set(h->mPixelHandle, transposed.begin(), arraySize, matrixType);
- }
- const String GFXD3D9ShaderConstBuffer::describeSelf() const
- {
- String ret;
- ret = String(" GFXD3D9ShaderConstBuffer\n");
- for (U32 i = 0; i < mVertexConstBufferLayoutF->getParameterCount(); i++)
- {
- GenericConstBufferLayout::ParamDesc pd;
- mVertexConstBufferLayoutF->getDesc(i, pd);
- ret += String::ToString(" Constant name: %s", pd.name.c_str());
- }
- return ret;
- }
- void GFXD3D9ShaderConstBuffer::zombify()
- {
- }
- void GFXD3D9ShaderConstBuffer::resurrect()
- {
- }
- bool GFXD3D9ShaderConstBuffer::isDirty()
- {
- bool ret = mVertexConstBufferF->isDirty();
- ret |= mVertexConstBufferI->isDirty();
- ret |= mPixelConstBufferF->isDirty();
- ret |= mPixelConstBufferI->isDirty();
- return ret;
- }
- void GFXD3D9ShaderConstBuffer::activate( GFXD3D9ShaderConstBuffer *prevShaderBuffer )
- {
- PROFILE_SCOPE(GFXD3D9ShaderConstBuffer_activate);
- // NOTE: This is a really critical function as it gets
- // called between every draw call to update the constants.
- //
- // Alot of the calls here are inlined... be careful
- // what you change.
- // If the buffer has changed we need to compare it
- // with the new buffer to see if we can skip copying
- // equal buffer content.
- //
- // If the buffer hasn't changed then we only will
- // be copying the changes that have occured since
- // the last activate call.
- //
- if ( prevShaderBuffer != this )
- {
- // If the previous buffer is dirty, than we can't compare
- // against it, because it hasn't sent its contents to the
- // card yet and must be copied.
- if ( prevShaderBuffer && !prevShaderBuffer->isDirty() )
- {
- PROFILE_SCOPE(GFXD3D9ShaderConstBuffer_activate_dirty_check_1);
- // If the buffer content is equal then we set the dirty
- // flag to false knowing the current state of the card matches
- // the new buffer.
- //
- // If the content is not equal we set the dirty flag to
- // true which causes the full content of the buffer to be
- // copied to the card.
- //
- mVertexConstBufferF->setDirty( !prevShaderBuffer->mVertexConstBufferF->isEqual( mVertexConstBufferF ) );
- mPixelConstBufferF->setDirty( !prevShaderBuffer->mPixelConstBufferF->isEqual( mPixelConstBufferF ) );
- mVertexConstBufferI->setDirty( !prevShaderBuffer->mVertexConstBufferF->isEqual( mVertexConstBufferI ) );
- mPixelConstBufferI->setDirty( !prevShaderBuffer->mPixelConstBufferF->isEqual( mPixelConstBufferI ) );
- }
- else
- {
- // This happens rarely... but it can happen.
- //
- // We copy the entire dirty state to the card.
- PROFILE_SCOPE(GFXD3D9ShaderConstBuffer_activate_dirty_check_2);
- mVertexConstBufferF->setDirty( true );
- mPixelConstBufferF->setDirty( true );
- mVertexConstBufferI->setDirty( true );
- mPixelConstBufferI->setDirty( true );
- }
- }
- const U32 bytesToFloat4 = 16;
- const U32 bytesToInt4 = 16;
- U32 start, bufferSize;
- const U8* buf;
- if ( mVertexConstBufferF->isDirty() )
- {
- buf = mVertexConstBufferF->getDirtyBuffer( &start, &bufferSize );
- mDevice->SetVertexShaderConstantF( start / bytesToFloat4, (float*)buf, bufferSize / bytesToFloat4 );
- }
- if ( mPixelConstBufferF->isDirty() )
- {
- buf = mPixelConstBufferF->getDirtyBuffer( &start, &bufferSize );
- mDevice->SetPixelShaderConstantF( start / bytesToFloat4, (float*)buf, bufferSize / bytesToFloat4 );
- }
- if ( mVertexConstBufferI->isDirty() )
- {
- buf = mVertexConstBufferI->getDirtyBuffer( &start, &bufferSize );
- mDevice->SetVertexShaderConstantI( start / bytesToInt4, (int*)buf, bufferSize / bytesToInt4 );
- }
- if ( mPixelConstBufferI->isDirty() )
- {
- buf = mPixelConstBufferI->getDirtyBuffer( &start, &bufferSize );
- mDevice->SetPixelShaderConstantI( start / bytesToInt4, (int*)buf, bufferSize / bytesToInt4 );
- }
- #ifdef TORQUE_DEBUG
- // Make sure all the constants for this buffer were assigned.
- if ( mWasLost )
- {
- mVertexConstBufferF->assertUnassignedConstants( mShader->getVertexShaderFile().c_str() );
- mVertexConstBufferI->assertUnassignedConstants( mShader->getVertexShaderFile().c_str() );
- mPixelConstBufferF->assertUnassignedConstants( mShader->getPixelShaderFile().c_str() );
- mPixelConstBufferI->assertUnassignedConstants( mShader->getPixelShaderFile().c_str() );
- }
- #endif
- // Clear the lost state.
- mWasLost = false;
- }
- void GFXD3D9ShaderConstBuffer::onShaderReload( GFXD3D9Shader *shader )
- {
- AssertFatal( shader == mShader, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
- SAFE_DELETE( mVertexConstBufferF );
- SAFE_DELETE( mPixelConstBufferF );
- SAFE_DELETE( mVertexConstBufferI );
- SAFE_DELETE( mPixelConstBufferI );
-
- AssertFatal( mVertexConstBufferLayoutF == shader->mVertexConstBufferLayoutF, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
- AssertFatal( mPixelConstBufferLayoutF == shader->mPixelConstBufferLayoutF, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
- AssertFatal( mVertexConstBufferLayoutI == shader->mVertexConstBufferLayoutI, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
- AssertFatal( mPixelConstBufferLayoutI == shader->mPixelConstBufferLayoutI, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
- mVertexConstBufferF = new GenericConstBuffer( mVertexConstBufferLayoutF );
- mVertexConstBufferI = new GenericConstBuffer( mVertexConstBufferLayoutI );
- mPixelConstBufferF = new GenericConstBuffer( mPixelConstBufferLayoutF );
- mPixelConstBufferI = new GenericConstBuffer( mPixelConstBufferLayoutI );
-
- // Set the lost state.
- mWasLost = true;
- }
- //------------------------------------------------------------------------------
- GFXD3D9Shader::GFXD3D9Shader()
- {
- VECTOR_SET_ASSOCIATION( mShaderConsts );
- mD3D9Device = dynamic_cast<GFXD3D9Device *>(GFX)->getDevice();
- AssertFatal(mD3D9Device, "Invalid device for shader.");
- mVertShader = NULL;
- mPixShader = NULL;
- mVertexConstBufferLayoutF = NULL;
- mPixelConstBufferLayoutF = NULL;
- mVertexConstBufferLayoutI = NULL;
- mPixelConstBufferLayoutI = NULL;
- if( smD3DXInclude == NULL )
- smD3DXInclude = new _gfxD3DXInclude;
- }
- //------------------------------------------------------------------------------
- GFXD3D9Shader::~GFXD3D9Shader()
- {
- for (HandleMap::Iterator i = mHandles.begin(); i != mHandles.end(); i++)
- delete i->value;
- SAFE_DELETE(mVertexConstBufferLayoutF);
- SAFE_DELETE(mPixelConstBufferLayoutF);
- SAFE_DELETE(mVertexConstBufferLayoutI);
- SAFE_DELETE(mPixelConstBufferLayoutI);
- SAFE_RELEASE(mVertShader);
- SAFE_RELEASE(mPixShader);
- }
- bool GFXD3D9Shader::_init()
- {
- PROFILE_SCOPE( GFXD3D9Shader_Init );
-
- if ( mPixVersion > GFX->getPixelShaderVersion() )
- {
- if ( smLogErrors )
- Con::errorf( "GFXD3D9Shader::init - Bad pixel shader version!" );
- return false;
- }
- if ( mPixVersion < 1.0f && mPixelFile.getFileName().isNotEmpty() )
- {
- if ( smLogErrors )
- Con::errorf( "GFXD3D9Shader::init - Pixel shaders not supported on SM %.1f!", mPixVersion );
- return false;
- }
- SAFE_RELEASE(mVertShader);
- SAFE_RELEASE(mPixShader);
- U32 mjVer = (U32)mFloor( mPixVersion );
- U32 mnVer = (U32)( ( mPixVersion - F32( mjVer ) ) * 10.01f ); // 10.01 instead of 10.0 because of floating point issues
- String vertTarget = String::ToString("vs_%d_%d", mjVer, mnVer);
- String pixTarget = String::ToString("ps_%d_%d", mjVer, mnVer);
- // Adjust version for vertex shaders
- if (mjVer == 2 && mnVer == 1)
- {
- pixTarget = "ps_2_a";
- vertTarget = "vs_2_0";
- }
- else if ( mjVer == 2 && mnVer == 2 )
- {
- pixTarget = "ps_2_b";
- vertTarget = "vs_2_0";
- }
- else if ( ( mPixVersion < 2.0f ) && ( mPixVersion > 1.101f ) )
- vertTarget = "vs_1_1";
- // Create the macro array including the system wide macros.
- const U32 macroCount = smGlobalMacros.size() + mMacros.size() + 2;
- FrameTemp<D3DXMACRO> d3dXMacros( macroCount );
- for ( U32 i=0; i < smGlobalMacros.size(); i++ )
- {
- d3dXMacros[i].Name = smGlobalMacros[i].name.c_str();
- d3dXMacros[i].Definition = smGlobalMacros[i].value.c_str();
- }
- for ( U32 i=0; i < mMacros.size(); i++ )
- {
- d3dXMacros[i+smGlobalMacros.size()].Name = mMacros[i].name.c_str();
- d3dXMacros[i+smGlobalMacros.size()].Definition = mMacros[i].value.c_str();
- }
- String smVersion = String::ToString( mjVer * 10 + mnVer );
- d3dXMacros[macroCount - 2].Name = "TORQUE_SM";
- d3dXMacros[macroCount - 2].Definition = smVersion.c_str();
- d3dXMacros[macroCount - 1].Name = NULL;
- d3dXMacros[macroCount - 1].Definition = NULL;
- if ( !mVertexConstBufferLayoutF )
- mVertexConstBufferLayoutF = new GFXD3D9ShaderBufferLayout();
- else
- mVertexConstBufferLayoutF->clear();
- if ( !mVertexConstBufferLayoutI )
- mVertexConstBufferLayoutI = new GFXD3D9ShaderBufferLayout();
- else
- mVertexConstBufferLayoutI->clear();
-
- if ( !mPixelConstBufferLayoutF )
- mPixelConstBufferLayoutF = new GFXD3D9ShaderBufferLayout();
- else
- mPixelConstBufferLayoutF->clear();
- if ( !mPixelConstBufferLayoutI )
- mPixelConstBufferLayoutI = new GFXD3D9ShaderBufferLayout();
- else
- mPixelConstBufferLayoutI->clear();
-
- mSamplerDescriptions.clear();
- mShaderConsts.clear();
- if ( GFXD3DX.isLoaded && !Con::getBoolVariable( "$shaders::forceLoadCSF", false ) )
- {
- if ( !mVertexFile.isEmpty() &&
- !_compileShader( mVertexFile, vertTarget, d3dXMacros, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mSamplerDescriptions ) )
- return false;
- if ( !mPixelFile.isEmpty() &&
- !_compileShader( mPixelFile, pixTarget, d3dXMacros, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI, mSamplerDescriptions ) )
- return false;
- }
- else
- {
- if ( !_loadCompiledOutput( mVertexFile, vertTarget, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mSamplerDescriptions ) )
- {
- if ( smLogErrors )
- Con::errorf( "GFXD3D9Shader::init - Unable to load precompiled vertex shader for '%s'.",
- mVertexFile.getFullPath().c_str() );
- return false;
- }
- if ( !_loadCompiledOutput( mPixelFile, pixTarget, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI, mSamplerDescriptions ) )
- {
- if ( smLogErrors )
- Con::errorf( "GFXD3D9Shader::init - Unable to load precompiled pixel shader for '%s'.",
- mPixelFile.getFullPath().c_str() );
- return false;
- }
- }
- // Existing handles are resored to an uninitialized state.
- // Those that are found when parsing the layout parameters
- // will then be re-initialized.
- HandleMap::Iterator iter = mHandles.begin();
- for ( ; iter != mHandles.end(); iter++ )
- (iter->value)->clear();
- _buildShaderConstantHandles(mVertexConstBufferLayoutF, true);
- _buildShaderConstantHandles(mVertexConstBufferLayoutI, true);
- _buildShaderConstantHandles(mPixelConstBufferLayoutF, false);
- _buildShaderConstantHandles(mPixelConstBufferLayoutI, false);
- _buildSamplerShaderConstantHandles( mSamplerDescriptions );
- _buildInstancingShaderConstantHandles();
- // Notify any existing buffers that the buffer
- // layouts have changed and they need to update.
- Vector<GFXShaderConstBuffer*>::iterator biter = mActiveBuffers.begin();
- for ( ; biter != mActiveBuffers.end(); biter++ )
- ((GFXD3D9ShaderConstBuffer*)(*biter))->onShaderReload( this );
- return true;
- }
- bool GFXD3D9Shader::_compileShader( const Torque::Path &filePath,
- const String& target,
- const D3DXMACRO *defines,
- GenericConstBufferLayout* bufferLayoutF,
- GenericConstBufferLayout* bufferLayoutI,
- Vector<GFXShaderConstDesc> &samplerDescriptions )
- {
- PROFILE_SCOPE( GFXD3D9Shader_CompileShader );
- HRESULT res = D3DERR_INVALIDCALL;
- LPD3DXBUFFER code = NULL;
- LPD3DXBUFFER errorBuff = NULL;
- #ifdef TORQUE_DEBUG
- U32 flags = D3DXSHADER_DEBUG;
- #else
- U32 flags = 0;
- #endif
- #ifdef TORQUE_OS_XENON
- flags |= D3DXSHADER_PREFER_FLOW_CONTROL;
- #endif
- #ifdef D3DXSHADER_USE_LEGACY_D3DX9_31_DLL
- if( D3DX_SDK_VERSION >= 32 )
- {
- // will need to use old compiler for 1_1 shaders - check for pixel
- // or vertex shader with appropriate version.
- if ((target.compare("vs1", 3) == 0) || (target.compare("vs_1", 4) == 0))
- flags |= D3DXSHADER_USE_LEGACY_D3DX9_31_DLL;
- if ((target.compare("ps1", 3) == 0) || (target.compare("ps_1", 4) == 0))
- flags |= D3DXSHADER_USE_LEGACY_D3DX9_31_DLL;
- }
- #endif
- #if !defined(TORQUE_OS_XENON) && (D3DX_SDK_VERSION <= 40)
- #error This version of the DirectX SDK is too old. Please install a newer version of the DirectX SDK: http://msdn.microsoft.com/en-us/directx/default.aspx
- #endif
- ID3DXConstantTable* table = NULL;
- static String sHLSLStr( "hlsl" );
- static String sOBJStr( "obj" );
- // Is it an HLSL shader?
- if ( filePath.getExtension().equal(sHLSLStr, String::NoCase) )
- {
- FrameAllocatorMarker fam;
- char *buffer = NULL;
- // Set this so that the D3DXInclude::Open will have this
- // information for relative paths.
- smD3DXInclude->setPath( filePath.getRootAndPath() );
- FileStream s;
- if ( !s.open( filePath, Torque::FS::File::Read ) )
- {
- AssertISV(false, avar("GFXD3D9Shader::initShader - failed to open shader '%s'.", filePath.getFullPath().c_str()));
- if ( smLogErrors )
- Con::errorf( "GFXD3D9Shader::_compileShader - Failed to open shader file '%s'.",
- filePath.getFullPath().c_str() );
- return false;
- }
- // Convert the path which might have virtualized
- // mount paths to a real file system path.
- Torque::Path realPath;
- if ( !FS::GetFSPath( filePath, realPath ) )
- realPath = filePath;
- // Add a #line pragma so that error and warning messages
- // returned by the HLSL compiler report the right file.
- String linePragma = String::ToString( "#line 1 \"%s\"\r\n", realPath.getFullPath().c_str() );
- U32 linePragmaLen = linePragma.length();
- U32 bufSize = s.getStreamSize();
- buffer = (char *)fam.alloc( bufSize + linePragmaLen + 1 );
- dStrncpy( buffer, linePragma.c_str(), linePragmaLen );
- s.read( bufSize, buffer + linePragmaLen );
- buffer[bufSize+linePragmaLen] = 0;
- res = GFXD3DX.D3DXCompileShader( buffer, bufSize + linePragmaLen, defines, smD3DXInclude, "main",
- target, flags, &code, &errorBuff, &table );
- }
- // Is it a precompiled obj shader?
- else if ( filePath.getExtension().equal( sOBJStr, String::NoCase ) )
- {
- FileStream s;
- if(!s.open(filePath, Torque::FS::File::Read))
- {
- AssertISV(false, avar("GFXD3D9Shader::initShader - failed to open shader '%s'.", filePath.getFullPath().c_str()));
- if ( smLogErrors )
- Con::errorf( "GFXD3D9Shader::_compileShader - Failed to open shader file '%s'.",
- filePath.getFullPath().c_str() );
- return false;
- }
- res = GFXD3DX.D3DXCreateBuffer(s.getStreamSize(), &code);
- AssertISV(res == D3D_OK, "Unable to create buffer!");
- s.read(s.getStreamSize(), code->GetBufferPointer());
-
- if (res == D3D_OK)
- {
- DWORD* data = (DWORD*) code->GetBufferPointer();
- res = GFXD3DX.D3DXGetShaderConstantTable(data, &table);
- }
- }
- else
- {
- if ( smLogErrors )
- Con::errorf( "GFXD3D9Shader::_compileShader - Unsupported shader file type '%s'.",
- filePath.getFullPath().c_str() );
- return false;
- }
- if ( res != D3D_OK && smLogErrors )
- Con::errorf( "GFXD3D9Shader::_compileShader - Error compiling shader: %s: %s (%x)",
- DXGetErrorStringA(res), DXGetErrorDescriptionA(res), res );
- if ( errorBuff )
- {
- // remove \n at end of buffer
- U8 *buffPtr = (U8*) errorBuff->GetBufferPointer();
- U32 len = dStrlen( (const char*) buffPtr );
- buffPtr[len-1] = '\0';
- if( res != D3D_OK )
- {
- if ( smLogErrors )
- Con::errorf( " %s", (const char*) errorBuff->GetBufferPointer() );
- }
- else
- {
- if ( smLogWarnings )
- Con::warnf( "%s", (const char*) errorBuff->GetBufferPointer() );
- }
- }
- else if ( code == NULL && smLogErrors )
- Con::errorf( "GFXD3D9Shader::_compileShader - no compiled code produced; possibly missing file '%s'.",
- filePath.getFullPath().c_str() );
- // Create the proper shader if we have code
- if( code != NULL )
- {
- #ifndef TORQUE_SHIPPING
- LPD3DXBUFFER disassem = NULL;
- D3DXDisassembleShader( (DWORD*)code->GetBufferPointer(), false, NULL, &disassem );
- mDissasembly = (const char*)disassem->GetBufferPointer();
- SAFE_RELEASE( disassem );
- if ( gDisassembleAllShaders )
- {
- String filename = filePath.getFullPath();
- filename.replace( ".hlsl", "_dis.txt" );
- FileStream *fstream = FileStream::createAndOpen( filename, Torque::FS::File::Write );
- if ( fstream )
- {
- fstream->write( mDissasembly );
- fstream->close();
- delete fstream;
- }
- }
- #endif
- if (target.compare("ps_", 3) == 0)
- res = mD3D9Device->CreatePixelShader( (DWORD*)code->GetBufferPointer(), &mPixShader );
- else
- res = mD3D9Device->CreateVertexShader( (DWORD*)code->GetBufferPointer(), &mVertShader );
- if (res == S_OK)
- _getShaderConstants(table, bufferLayoutF, bufferLayoutI, samplerDescriptions);
- #ifdef TORQUE_ENABLE_CSF_GENERATION
- // Ok, we've got a valid shader and constants, let's write them all out.
- if ( !_saveCompiledOutput(filePath, code, bufferLayoutF, bufferLayoutI) && smLogErrors )
- Con::errorf( "GFXD3D9Shader::_compileShader - Unable to save shader compile output for: %s",
- filePath.getFullPath().c_str() );
- #endif
- SAFE_RELEASE(table);
- if ( res != S_OK && smLogErrors )
- Con::errorf( "GFXD3D9Shader::_compileShader - Unable to create shader for '%s'.",
- filePath.getFullPath().c_str() );
- }
- bool result = code != NULL && res == S_OK;
- SAFE_RELEASE( code );
- SAFE_RELEASE( errorBuff );
- return result;
- }
- void GFXD3D9Shader::_getShaderConstants( ID3DXConstantTable *table,
- GenericConstBufferLayout *bufferLayoutF,
- GenericConstBufferLayout* bufferLayoutI,
- Vector<GFXShaderConstDesc> &samplerDescriptions )
- {
- PROFILE_SCOPE( GFXD3D9Shader_GetShaderConstants );
- AssertFatal(table, "NULL constant table not allowed, is this an assembly shader?");
- D3DXCONSTANTTABLE_DESC tableDesc;
- D3D9Assert(table->GetDesc(&tableDesc), "Unable to get constant table info.");
- for (U32 i = 0; i < tableDesc.Constants; i++)
- {
- D3DXHANDLE handle = table->GetConstant(0, i);
- const U32 descSize=16;
- D3DXCONSTANT_DESC constantDescArray[descSize];
- U32 size = descSize;
- if (table->GetConstantDesc(handle, constantDescArray, &size) == S_OK)
- {
- D3DXCONSTANT_DESC& constantDesc = constantDescArray[0];
- GFXShaderConstDesc desc;
-
- desc.name = String(constantDesc.Name);
- // Prepend a "$" if it doesn't exist. Just to make things consistent.
- if (desc.name.find("$") != 0)
- desc.name = String::ToString("$%s", desc.name.c_str());
- //Con::printf("name %s: , offset: %d, size: %d, constantDesc.Elements: %d", desc.name.c_str(), constantDesc.RegisterIndex, constantDesc.Bytes, constantDesc.Elements);
- desc.arraySize = constantDesc.Elements;
-
- GenericConstBufferLayout* bufferLayout = NULL;
- switch (constantDesc.RegisterSet)
- {
- case D3DXRS_INT4 :
- {
- bufferLayout = bufferLayoutI;
- switch (constantDesc.Class)
- {
- case D3DXPC_SCALAR :
- desc.constType = GFXSCT_Int;
- break;
- case D3DXPC_VECTOR :
- {
- switch (constantDesc.Columns)
- {
- case 1 :
- desc.constType = GFXSCT_Int;
- break;
- case 2 :
- desc.constType = GFXSCT_Int2;
- break;
- case 3 :
- desc.constType = GFXSCT_Int3;
- break;
- case 4 :
- desc.constType = GFXSCT_Int4;
- break;
- default:
- AssertFatal(false, "Unknown int vector type!");
- break;
- }
- }
- break;
- }
- desc.constType = GFXSCT_Int4;
- break;
- }
- case D3DXRS_FLOAT4 :
- {
- bufferLayout = bufferLayoutF;
- switch (constantDesc.Class)
- {
- case D3DXPC_SCALAR:
- desc.constType = GFXSCT_Float;
- break;
- case D3DXPC_VECTOR :
- {
- switch (constantDesc.Columns)
- {
- case 1 :
- desc.constType = GFXSCT_Float;
- break;
- case 2 :
- desc.constType = GFXSCT_Float2;
- break;
- case 3 :
- desc.constType = GFXSCT_Float3;
- break;
- case 4 :
- desc.constType = GFXSCT_Float4;
- break;
- default:
- AssertFatal(false, "Unknown float vector type!");
- break;
- }
- }
- break;
- case D3DXPC_MATRIX_ROWS :
- case D3DXPC_MATRIX_COLUMNS :
- {
- S32 fd, sd;
- fd = constantDesc.RegisterCount / constantDesc.Elements;
- sd = constantDesc.Class == D3DXPC_MATRIX_ROWS ? constantDesc.Columns : constantDesc.Rows;
- switch (fd)
- {
- case 2 :
- AssertFatal(sd == 2, "non-square 2x? mats not supported");
- desc.constType = GFXSCT_Float2x2;
- break;
- case 3 :
- switch (sd)
- {
- case 3 :
- desc.constType = GFXSCT_Float3x3;
- break;
- case 4 :
- desc.constType = GFXSCT_Float4x3;
- break;
- default:
- AssertFatal(false, "Unsupported matrix size");
- break;
- }
- break;
- case 4 :
- switch (sd)
- {
- case 3:
- desc.constType = GFXSCT_Float3x4;
- break;
- case 4:
- desc.constType = GFXSCT_Float4x4;
- break;
- default:
- AssertFatal(false, "Unsupported matrix size");
- break;
- }
- break;
- }
- }
- break;
- case D3DXPC_OBJECT :
- case D3DXPC_STRUCT :
- bufferLayout = NULL;
- break;
- }
- }
- break;
- case D3DXRS_SAMPLER :
- {
- AssertFatal( constantDesc.Elements == 1, "Sampler Arrays not yet supported!" );
- switch (constantDesc.Type)
- {
- case D3DXPT_SAMPLER :
- case D3DXPT_SAMPLER1D :
- case D3DXPT_SAMPLER2D :
- case D3DXPT_SAMPLER3D :
- // Hi-jack the desc's arraySize to store the registerIndex.
- desc.constType = GFXSCT_Sampler;
- desc.arraySize = constantDesc.RegisterIndex;
- samplerDescriptions.push_back( desc );
- mShaderConsts.push_back(desc);
- break;
- case D3DXPT_SAMPLERCUBE :
- desc.constType = GFXSCT_SamplerCube;
- desc.arraySize = constantDesc.RegisterIndex;
- samplerDescriptions.push_back( desc );
- mShaderConsts.push_back(desc);
- break;
- }
- }
- break;
- default:
- AssertFatal(false, "Unknown shader constant class enum");
- break;
- }
-
- if (bufferLayout)
- {
- mShaderConsts.push_back(desc);
- U32 alignBytes = getAlignmentValue(desc.constType);
- U32 paramSize = alignBytes * desc.arraySize;
- bufferLayout->addParameter( desc.name,
- desc.constType,
- constantDesc.RegisterIndex * sizeof(Point4F),
- paramSize,
- desc.arraySize,
- alignBytes );
- }
- }
- else
- AssertFatal(false, "Unable to get shader constant description! (may need more elements of constantDesc");
- }
- }
- const U32 GFXD3D9Shader::smCompiledShaderTag = MakeFourCC('t','c','s','f');
- bool GFXD3D9Shader::_saveCompiledOutput( const Torque::Path &filePath,
- LPD3DXBUFFER buffer,
- GenericConstBufferLayout *bufferLayoutF,
- GenericConstBufferLayout *bufferLayoutI,
- Vector<GFXShaderConstDesc> &samplerDescriptions )
- {
- Torque::Path outputPath(filePath);
- outputPath.setExtension("csf"); // "C"ompiled "S"hader "F"ile (fancy!)
- FileStream f;
- if (!f.open(outputPath, Torque::FS::File::Write))
- return false;
- if (!f.write(smCompiledShaderTag))
- return false;
- // We could reverse engineer the structure in the compiled output, but this
- // is a bit easier because we can just read it into the struct that we want.
- if (!bufferLayoutF->write(&f))
- return false;
- if (!bufferLayoutI->write(&f))
- return false;
- U32 bufferSize = buffer->GetBufferSize();
- if (!f.write(bufferSize))
- return false;
- if (!f.write(bufferSize, buffer->GetBufferPointer()))
- return false;
- // Write out sampler descriptions.
- f.write( samplerDescriptions.size() );
- for ( U32 i = 0; i < samplerDescriptions.size(); i++ )
- {
- f.write( samplerDescriptions[i].name );
- f.write( (U32)(samplerDescriptions[i].constType) );
- f.write( samplerDescriptions[i].arraySize );
- }
- f.close();
- return true;
- }
- bool GFXD3D9Shader::_loadCompiledOutput( const Torque::Path &filePath,
- const String &target,
- GenericConstBufferLayout *bufferLayoutF,
- GenericConstBufferLayout *bufferLayoutI,
- Vector<GFXShaderConstDesc> &samplerDescriptions )
- {
- Torque::Path outputPath(filePath);
- outputPath.setExtension("csf"); // "C"ompiled "S"hader "F"ile (fancy!)
- FileStream f;
- if (!f.open(outputPath, Torque::FS::File::Read))
- return false;
- U32 fileTag;
- if (!f.read(&fileTag))
- return false;
- if (fileTag != smCompiledShaderTag)
- return false;
- if (!bufferLayoutF->read(&f))
- return false;
- if (!bufferLayoutI->read(&f))
- return false;
- U32 bufferSize;
- if (!f.read(&bufferSize))
- return false;
- U32 waterMark = FrameAllocator::getWaterMark();
- DWORD* buffer = static_cast<DWORD*>(FrameAllocator::alloc(bufferSize));
- if (!f.read(bufferSize, buffer))
- return false;
- // Read sampler descriptions.
- U32 samplerCount;
- f.read( &samplerCount );
- for ( U32 i = 0; i < samplerCount; i++ )
- {
- GFXShaderConstDesc samplerDesc;
- f.read( &(samplerDesc.name) );
- f.read( (U32*)&(samplerDesc.constType) );
- f.read( &(samplerDesc.arraySize) );
- samplerDescriptions.push_back( samplerDesc );
- }
- f.close();
- HRESULT res;
- if (target.compare("ps_", 3) == 0)
- res = mD3D9Device->CreatePixelShader(buffer, &mPixShader );
- else
- res = mD3D9Device->CreateVertexShader(buffer, &mVertShader );
- AssertFatal(SUCCEEDED(res), "Unable to load shader!");
- FrameAllocator::setWaterMark(waterMark);
- return SUCCEEDED(res);
- }
- void GFXD3D9Shader::_buildShaderConstantHandles(GenericConstBufferLayout* layout, bool vertexConst)
- {
- for (U32 i = 0; i < layout->getParameterCount(); i++)
- {
- GenericConstBufferLayout::ParamDesc pd;
- layout->getDesc(i, pd);
- GFXD3D9ShaderConstHandle* handle;
- HandleMap::Iterator j = mHandles.find(pd.name);
- if (j != mHandles.end())
- {
- handle = j->value;
- handle->mShader = this;
- handle->setValid( true );
- }
- else
- {
- handle = new GFXD3D9ShaderConstHandle();
- handle->mShader = this;
- mHandles[pd.name] = handle;
- handle->setValid( true );
- }
- if ( vertexConst )
- {
- handle->mVertexConstant = true;
- handle->mVertexHandle = pd;
- }
- else
- {
- handle->mPixelConstant = true;
- handle->mPixelHandle = pd;
- }
- }
- }
- void GFXD3D9Shader::_buildSamplerShaderConstantHandles( Vector<GFXShaderConstDesc> &samplerDescriptions )
- {
- Vector<GFXShaderConstDesc>::iterator iter = samplerDescriptions.begin();
- for ( ; iter != samplerDescriptions.end(); iter++ )
- {
- const GFXShaderConstDesc &desc = *iter;
- AssertFatal( desc.constType == GFXSCT_Sampler ||
- desc.constType == GFXSCT_SamplerCube,
- "GFXD3D9Shader::_buildSamplerShaderConstantHandles - Invalid samplerDescription type!" );
- GFXD3D9ShaderConstHandle *handle;
- HandleMap::Iterator j = mHandles.find(desc.name);
- if ( j != mHandles.end() )
- handle = j->value;
- else
- {
- handle = new GFXD3D9ShaderConstHandle();
- mHandles[desc.name] = handle;
- }
- handle->mShader = this;
- handle->setValid( true );
- handle->mPixelConstant = true;
- handle->mPixelHandle.name = desc.name;
- handle->mPixelHandle.constType = desc.constType;
- handle->mPixelHandle.offset = desc.arraySize;
- }
- }
- void GFXD3D9Shader::_buildInstancingShaderConstantHandles()
- {
- // If we have no instancing than just return
- if (!mInstancingFormat)
- return;
- U32 offset = 0;
- for ( U32 i=0; i < mInstancingFormat->getElementCount(); i++ )
- {
- const GFXVertexElement &element = mInstancingFormat->getElement( i );
-
- String constName = String::ToString( "$%s", element.getSemantic().c_str() );
- GFXD3D9ShaderConstHandle *handle;
- HandleMap::Iterator j = mHandles.find( constName );
- if ( j != mHandles.end() )
- handle = j->value;
- else
- {
- handle = new GFXD3D9ShaderConstHandle();
- mHandles[ constName ] = handle;
- }
- handle->mShader = this;
- handle->setValid( true );
- handle->mInstancingConstant = true;
- // We shouldn't have an instancing constant that is also
- // a vertex or pixel constant! This means the shader features
- // are confused as to what is instanced.
- //
- AssertFatal( !handle->mVertexConstant &&
- !handle->mPixelConstant,
- "GFXD3D9Shader::_buildInstancingShaderConstantHandles - Bad instanced constant!" );
- // HACK: The GFXD3D9ShaderConstHandle will check mVertexConstant then
- // fall back to reading the mPixelHandle values. We depend on this here
- // and store the data we need in the mPixelHandle constant although its
- // not a pixel shader constant.
- //
- handle->mPixelHandle.name = constName;
- handle->mPixelHandle.offset = offset;
- // If this is a matrix we will have 2 or 3 more of these
- // semantics with the same name after it.
- for ( ; i < mInstancingFormat->getElementCount(); i++ )
- {
- const GFXVertexElement &nextElement = mInstancingFormat->getElement( i );
- if ( nextElement.getSemantic() != element.getSemantic() )
- {
- i--;
- break;
- }
- offset += nextElement.getSizeInBytes();
- }
- }
- }
- GFXShaderConstBufferRef GFXD3D9Shader::allocConstBuffer()
- {
- if (mVertexConstBufferLayoutF && mPixelConstBufferLayoutF)
- {
- GFXD3D9ShaderConstBuffer* buffer = new GFXD3D9ShaderConstBuffer(this, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI);
- mActiveBuffers.push_back( buffer );
- buffer->registerResourceWithDevice(getOwningDevice());
- return buffer;
- } else {
- return NULL;
- }
- }
- /// Returns a shader constant handle for name
- GFXShaderConstHandle* GFXD3D9Shader::getShaderConstHandle(const String& name)
- {
- HandleMap::Iterator i = mHandles.find(name);
- if ( i != mHandles.end() )
- {
- return i->value;
- }
- else
- {
- GFXD3D9ShaderConstHandle *handle = new GFXD3D9ShaderConstHandle();
- handle->setValid( false );
- handle->mShader = this;
- mHandles[name] = handle;
- return handle;
- }
- }
- /// Returns a shader constant handle for name, if the variable doesn't exist NULL is returned.
- GFXShaderConstHandle* GFXD3D9Shader::findShaderConstHandle(const String& name)
- {
- HandleMap::Iterator i = mHandles.find(name);
- if ( i != mHandles.end() )
- {
- return i->value;
- }
- else
- {
- return NULL;
- }
- }
- const Vector<GFXShaderConstDesc>& GFXD3D9Shader::getShaderConstDesc() const
- {
- return mShaderConsts;
- }
- U32 GFXD3D9Shader::getAlignmentValue(const GFXShaderConstType constType) const
- {
- const U32 mRowSizeF = 16;
- const U32 mRowSizeI = 16;
- switch (constType)
- {
- case GFXSCT_Float :
- case GFXSCT_Float2 :
- case GFXSCT_Float3 :
- case GFXSCT_Float4 :
- return mRowSizeF;
- break;
- // Matrices
- case GFXSCT_Float2x2 :
- return mRowSizeF * 2;
- break;
- case GFXSCT_Float3x3 :
- return mRowSizeF * 3;
- break;
- case GFXSCT_Float3x4 :
- return mRowSizeF * 4;
- break;
- case GFXSCT_Float4x4 :
- return mRowSizeF * 4;
- break;
- case GFXSCT_Float4x3 :
- return mRowSizeF * 3;
- break;
- //// Scalar
- case GFXSCT_Int :
- case GFXSCT_Int2 :
- case GFXSCT_Int3 :
- case GFXSCT_Int4 :
- return mRowSizeI;
- break;
- default:
- AssertFatal(false, "Unsupported type!");
- return 0;
- break;
- }
- }
- void GFXD3D9Shader::zombify()
- {
- // Shaders don't need zombification
- }
- void GFXD3D9Shader::resurrect()
- {
- // Shaders are never zombies, and therefore don't have to be brought back.
- }
|