gfxD3D9Shader.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #if defined(TORQUE_OS_XENON)
  24. # include <xtl.h>
  25. #else
  26. # include <d3d9.h>
  27. #endif
  28. #include "gfx/D3D9/gfxD3D9Shader.h"
  29. #include "gfx/D3D9/gfxD3D9Device.h"
  30. #include "core/frameAllocator.h"
  31. #include "core/stream/fileStream.h"
  32. #include "core/util/safeDelete.h"
  33. #include "console/console.h"
  34. using namespace Torque;
  35. extern bool gDisassembleAllShaders;
  36. /// D3DXInclude plugin
  37. class _gfxD3DXInclude : public ID3DXInclude, public StrongRefBase
  38. {
  39. private:
  40. Vector<String> mLastPath;
  41. public:
  42. void setPath( const String &path )
  43. {
  44. mLastPath.clear();
  45. mLastPath.push_back( path );
  46. }
  47. _gfxD3DXInclude() {}
  48. virtual ~_gfxD3DXInclude() {}
  49. STDMETHOD(Close)(THIS_ LPCVOID pData);
  50. // 360
  51. STDMETHOD(Open)(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes, /* OUT */ LPSTR pFullPath, DWORD cbFullPath);
  52. // PC
  53. STDMETHOD(Open)(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes)
  54. {
  55. return Open( IncludeType, pFileName, pParentData, ppData, pBytes, NULL, 0 );
  56. }
  57. };
  58. _gfxD3DXIncludeRef GFXD3D9Shader::smD3DXInclude = NULL;
  59. HRESULT _gfxD3DXInclude::Open(THIS_ D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName,
  60. LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes,
  61. LPSTR pFullPath, DWORD cbFullPath)
  62. {
  63. // First try making the path relative to the parent.
  64. Torque::Path path = Torque::Path::Join( mLastPath.last(), '/', pFileName );
  65. path = Torque::Path::CompressPath( path );
  66. if ( !Torque::FS::ReadFile( path, (void *&)*ppData, *pBytes, true ) )
  67. {
  68. // Ok... now try using the path as is.
  69. path = String( pFileName );
  70. path = Torque::Path::CompressPath( path );
  71. if ( !Torque::FS::ReadFile( path, (void *&)*ppData, *pBytes, true ) )
  72. {
  73. AssertISV(false, avar( "Failed to open include '%s'.", pFileName));
  74. return E_FAIL;
  75. }
  76. }
  77. // If the data was of zero size then we cannot recurse
  78. // into this file and DX won't call Close() below.
  79. //
  80. // So in this case don't push on the path.
  81. if ( *pBytes > 0 )
  82. mLastPath.push_back( path.getRootAndPath() );
  83. return S_OK;
  84. }
  85. HRESULT _gfxD3DXInclude::Close( THIS_ LPCVOID pData )
  86. {
  87. // Free the data file and pop its path off the stack.
  88. delete [] (U8*)pData;
  89. mLastPath.pop_back();
  90. return S_OK;
  91. }
  92. GFXD3D9ShaderConstHandle::GFXD3D9ShaderConstHandle()
  93. {
  94. clear();
  95. }
  96. const String& GFXD3D9ShaderConstHandle::getName() const
  97. {
  98. if ( mVertexConstant )
  99. return mVertexHandle.name;
  100. else
  101. return mPixelHandle.name;
  102. }
  103. GFXShaderConstType GFXD3D9ShaderConstHandle::getType() const
  104. {
  105. if ( mVertexConstant )
  106. return mVertexHandle.constType;
  107. else
  108. return mPixelHandle.constType;
  109. }
  110. U32 GFXD3D9ShaderConstHandle::getArraySize() const
  111. {
  112. if ( mVertexConstant )
  113. return mVertexHandle.arraySize;
  114. else
  115. return mPixelHandle.arraySize;
  116. }
  117. S32 GFXD3D9ShaderConstHandle::getSamplerRegister() const
  118. {
  119. if ( !mValid || !isSampler() )
  120. return -1;
  121. // We always store sampler type and register index in the pixelHandle,
  122. // sampler registers are shared between vertex and pixel shaders anyway.
  123. return mPixelHandle.offset;
  124. }
  125. //------------------------------------------------------------------------------
  126. //------------------------------------------------------------------------------
  127. bool GFXD3D9ShaderBufferLayout::setMatrix(const ParamDesc& pd, const GFXShaderConstType constType, const U32 size, const void* data, U8* basePointer)
  128. {
  129. PROFILE_SCOPE(GFXD3D9ShaderBufferLayout_setMatrix);
  130. if (pd.constType == GFXSCT_Float4x4)
  131. {
  132. // Special case, we can just blast this guy.
  133. AssertFatal(pd.size >= size, "Not enough room in the buffer for this data!");
  134. if (dMemcmp(basePointer+pd.offset, data, size) != 0)
  135. {
  136. dMemcpy(basePointer+pd.offset, data, size);
  137. return true;
  138. }
  139. return false;
  140. }
  141. else
  142. {
  143. PROFILE_SCOPE(GFXD3D9ShaderBufferLayout_setMatrix_not4x4);
  144. // Figure out how big of a chunk we are copying. We're going to copy 4 columns by N rows of data
  145. U32 csize;
  146. switch (pd.constType)
  147. {
  148. case GFXSCT_Float2x2 :
  149. csize = 32;
  150. break;
  151. case GFXSCT_Float3x3 :
  152. csize = 48;
  153. break;
  154. default:
  155. AssertFatal(false, "Unhandled case!");
  156. return false;
  157. break;
  158. }
  159. // Loop through and copy
  160. bool ret = false;
  161. U8* currDestPointer = basePointer+pd.offset;
  162. const U8* currSourcePointer = static_cast<const U8*>(data);
  163. const U8* endData = currSourcePointer + size;
  164. while (currSourcePointer < endData)
  165. {
  166. if (dMemcmp(currDestPointer, currSourcePointer, csize) != 0)
  167. {
  168. dMemcpy(currDestPointer, currSourcePointer, csize);
  169. ret = true;
  170. }
  171. currDestPointer += csize;
  172. currSourcePointer += sizeof(MatrixF);
  173. }
  174. return ret;
  175. }
  176. }
  177. //------------------------------------------------------------------------------
  178. GFXD3D9ShaderConstBuffer::GFXD3D9ShaderConstBuffer( GFXD3D9Shader* shader,
  179. GFXD3D9ShaderBufferLayout* vertexLayoutF,
  180. GFXD3D9ShaderBufferLayout* vertexLayoutI,
  181. GFXD3D9ShaderBufferLayout* pixelLayoutF,
  182. GFXD3D9ShaderBufferLayout* pixelLayoutI )
  183. {
  184. AssertFatal( shader, "GFXD3D9ShaderConstBuffer() - Got a null shader!" );
  185. // We hold on to this so we don't have to call
  186. // this virtual method during activation.
  187. mDevice = static_cast<GFXD3D9Device*>( GFX )->getDevice();
  188. mShader = shader;
  189. // TODO: Remove buffers and layouts that don't exist for performance?
  190. mVertexConstBufferLayoutF = vertexLayoutF;
  191. mVertexConstBufferF = new GenericConstBuffer(vertexLayoutF);
  192. mVertexConstBufferLayoutI = vertexLayoutI;
  193. mVertexConstBufferI = new GenericConstBuffer(vertexLayoutI);
  194. mPixelConstBufferLayoutF = pixelLayoutF;
  195. mPixelConstBufferF = new GenericConstBuffer(pixelLayoutF);
  196. mPixelConstBufferLayoutI = pixelLayoutI;
  197. mPixelConstBufferI = new GenericConstBuffer(pixelLayoutI);
  198. }
  199. GFXD3D9ShaderConstBuffer::~GFXD3D9ShaderConstBuffer()
  200. {
  201. SAFE_DELETE(mVertexConstBufferF);
  202. SAFE_DELETE(mPixelConstBufferF);
  203. SAFE_DELETE(mVertexConstBufferI);
  204. SAFE_DELETE(mPixelConstBufferI);
  205. if ( mShader )
  206. mShader->_unlinkBuffer( this );
  207. }
  208. GFXShader* GFXD3D9ShaderConstBuffer::getShader()
  209. {
  210. return mShader;
  211. }
  212. // This is kind of cheesy, but I don't think templates would work well here because
  213. // these functions potentially need to be handled differently by other derived types
  214. template<class T>
  215. inline void GFXD3D9ShaderConstBuffer::SET_CONSTANT( GFXShaderConstHandle* handle, const T& fv, GenericConstBuffer *vBuffer, GenericConstBuffer *pBuffer )
  216. {
  217. AssertFatal(dynamic_cast<const GFXD3D9ShaderConstHandle*>(handle), "Incorrect const buffer type!");
  218. const GFXD3D9ShaderConstHandle* h = static_cast<const GFXD3D9ShaderConstHandle*>(handle);
  219. AssertFatal(h, "Handle is NULL!" );
  220. AssertFatal(h->isValid(), "Handle is not valid!" );
  221. AssertFatal(!h->isSampler(), "Handle is sampler constant!" );
  222. AssertFatal(!mShader.isNull(), "Buffer's shader is null!" );
  223. AssertFatal(!h->mShader.isNull(), "Handle's shader is null!" );
  224. AssertFatal(h->mShader.getPointer() == mShader.getPointer(), "Mismatched shaders!");
  225. if ( h->mInstancingConstant )
  226. {
  227. dMemcpy( mInstPtr+h->mPixelHandle.offset, &fv, sizeof( fv ) );
  228. return;
  229. }
  230. if (h->mVertexConstant)
  231. vBuffer->set(h->mVertexHandle, fv);
  232. if (h->mPixelConstant)
  233. pBuffer->set(h->mPixelHandle, fv);
  234. }
  235. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const F32 fv)
  236. {
  237. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  238. }
  239. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2F& fv)
  240. {
  241. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  242. }
  243. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3F& fv)
  244. {
  245. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  246. }
  247. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4F& fv)
  248. {
  249. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  250. }
  251. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const PlaneF& fv)
  252. {
  253. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  254. }
  255. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const ColorF& fv)
  256. {
  257. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  258. }
  259. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const S32 f)
  260. {
  261. // This is the only type that is allowed to be used
  262. // with a sampler shader constant type, but it is only
  263. // allowed to be set from GLSL.
  264. //
  265. // So we ignore it here... all other cases will assert.
  266. //
  267. if ( ((GFXD3D9ShaderConstHandle*)handle)->isSampler() )
  268. return;
  269. SET_CONSTANT(handle, f, mVertexConstBufferI, mPixelConstBufferI);
  270. }
  271. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point2I& fv)
  272. {
  273. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  274. }
  275. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point3I& fv)
  276. {
  277. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  278. }
  279. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const Point4I& fv)
  280. {
  281. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  282. }
  283. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<F32>& fv)
  284. {
  285. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  286. }
  287. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2F>& fv)
  288. {
  289. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  290. }
  291. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3F>& fv)
  292. {
  293. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  294. }
  295. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4F>& fv)
  296. {
  297. SET_CONSTANT(handle, fv, mVertexConstBufferF, mPixelConstBufferF);
  298. }
  299. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<S32>& fv)
  300. {
  301. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  302. }
  303. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point2I>& fv)
  304. {
  305. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  306. }
  307. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point3I>& fv)
  308. {
  309. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  310. }
  311. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const AlignedArray<Point4I>& fv)
  312. {
  313. SET_CONSTANT(handle, fv, mVertexConstBufferI, mPixelConstBufferI);
  314. }
  315. #undef SET_CONSTANT
  316. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF& mat, const GFXShaderConstType matrixType)
  317. {
  318. AssertFatal(handle, "Handle is NULL!" );
  319. AssertFatal(handle->isValid(), "Handle is not valid!" );
  320. AssertFatal(dynamic_cast<const GFXD3D9ShaderConstHandle*>(handle), "Incorrect const buffer type!");
  321. const GFXD3D9ShaderConstHandle* h = static_cast<const GFXD3D9ShaderConstHandle*>(handle);
  322. AssertFatal(!h->isSampler(), "Handle is sampler constant!" );
  323. AssertFatal(h->mShader == mShader, "Mismatched shaders!");
  324. MatrixF transposed;
  325. mat.transposeTo(transposed);
  326. if (h->mInstancingConstant)
  327. {
  328. if ( matrixType == GFXSCT_Float4x4 )
  329. dMemcpy( mInstPtr+h->mPixelHandle.offset, mat, sizeof( mat ) );
  330. // TODO: Support 3x3 and 2x2 matricies?
  331. return;
  332. }
  333. if (h->mVertexConstant)
  334. mVertexConstBufferF->set(h->mVertexHandle, transposed, matrixType);
  335. if (h->mPixelConstant)
  336. mPixelConstBufferF->set(h->mPixelHandle, transposed, matrixType);
  337. }
  338. void GFXD3D9ShaderConstBuffer::set(GFXShaderConstHandle* handle, const MatrixF* mat, const U32 arraySize, const GFXShaderConstType matrixType)
  339. {
  340. AssertFatal(handle, "Handle is NULL!" );
  341. AssertFatal(handle->isValid(), "Handle is not valid!" );
  342. AssertFatal(dynamic_cast<const GFXD3D9ShaderConstHandle*>(handle), "Incorrect const buffer type!");
  343. const GFXD3D9ShaderConstHandle* h = static_cast<const GFXD3D9ShaderConstHandle*>(handle);
  344. AssertFatal(!h->isSampler(), "Handle is sampler constant!" );
  345. AssertFatal(h->mShader == mShader, "Mismatched shaders!");
  346. static Vector<MatrixF> transposed;
  347. if (arraySize > transposed.size())
  348. transposed.setSize(arraySize);
  349. for (U32 i = 0; i < arraySize; i++)
  350. mat[i].transposeTo(transposed[i]);
  351. // TODO: Maybe support this in the future?
  352. if (h->mInstancingConstant)
  353. return;
  354. if (h->mVertexConstant)
  355. mVertexConstBufferF->set(h->mVertexHandle, transposed.begin(), arraySize, matrixType);
  356. if (h->mPixelConstant)
  357. mPixelConstBufferF->set(h->mPixelHandle, transposed.begin(), arraySize, matrixType);
  358. }
  359. const String GFXD3D9ShaderConstBuffer::describeSelf() const
  360. {
  361. String ret;
  362. ret = String(" GFXD3D9ShaderConstBuffer\n");
  363. for (U32 i = 0; i < mVertexConstBufferLayoutF->getParameterCount(); i++)
  364. {
  365. GenericConstBufferLayout::ParamDesc pd;
  366. mVertexConstBufferLayoutF->getDesc(i, pd);
  367. ret += String::ToString(" Constant name: %s", pd.name.c_str());
  368. }
  369. return ret;
  370. }
  371. void GFXD3D9ShaderConstBuffer::zombify()
  372. {
  373. }
  374. void GFXD3D9ShaderConstBuffer::resurrect()
  375. {
  376. }
  377. bool GFXD3D9ShaderConstBuffer::isDirty()
  378. {
  379. bool ret = mVertexConstBufferF->isDirty();
  380. ret |= mVertexConstBufferI->isDirty();
  381. ret |= mPixelConstBufferF->isDirty();
  382. ret |= mPixelConstBufferI->isDirty();
  383. return ret;
  384. }
  385. void GFXD3D9ShaderConstBuffer::activate( GFXD3D9ShaderConstBuffer *prevShaderBuffer )
  386. {
  387. PROFILE_SCOPE(GFXD3D9ShaderConstBuffer_activate);
  388. // NOTE: This is a really critical function as it gets
  389. // called between every draw call to update the constants.
  390. //
  391. // Alot of the calls here are inlined... be careful
  392. // what you change.
  393. // If the buffer has changed we need to compare it
  394. // with the new buffer to see if we can skip copying
  395. // equal buffer content.
  396. //
  397. // If the buffer hasn't changed then we only will
  398. // be copying the changes that have occured since
  399. // the last activate call.
  400. //
  401. if ( prevShaderBuffer != this )
  402. {
  403. // If the previous buffer is dirty, than we can't compare
  404. // against it, because it hasn't sent its contents to the
  405. // card yet and must be copied.
  406. if ( prevShaderBuffer && !prevShaderBuffer->isDirty() )
  407. {
  408. PROFILE_SCOPE(GFXD3D9ShaderConstBuffer_activate_dirty_check_1);
  409. // If the buffer content is equal then we set the dirty
  410. // flag to false knowing the current state of the card matches
  411. // the new buffer.
  412. //
  413. // If the content is not equal we set the dirty flag to
  414. // true which causes the full content of the buffer to be
  415. // copied to the card.
  416. //
  417. mVertexConstBufferF->setDirty( !prevShaderBuffer->mVertexConstBufferF->isEqual( mVertexConstBufferF ) );
  418. mPixelConstBufferF->setDirty( !prevShaderBuffer->mPixelConstBufferF->isEqual( mPixelConstBufferF ) );
  419. mVertexConstBufferI->setDirty( !prevShaderBuffer->mVertexConstBufferF->isEqual( mVertexConstBufferI ) );
  420. mPixelConstBufferI->setDirty( !prevShaderBuffer->mPixelConstBufferF->isEqual( mPixelConstBufferI ) );
  421. }
  422. else
  423. {
  424. // This happens rarely... but it can happen.
  425. //
  426. // We copy the entire dirty state to the card.
  427. PROFILE_SCOPE(GFXD3D9ShaderConstBuffer_activate_dirty_check_2);
  428. mVertexConstBufferF->setDirty( true );
  429. mPixelConstBufferF->setDirty( true );
  430. mVertexConstBufferI->setDirty( true );
  431. mPixelConstBufferI->setDirty( true );
  432. }
  433. }
  434. const U32 bytesToFloat4 = 16;
  435. const U32 bytesToInt4 = 16;
  436. U32 start, bufferSize;
  437. const U8* buf;
  438. if ( mVertexConstBufferF->isDirty() )
  439. {
  440. buf = mVertexConstBufferF->getDirtyBuffer( &start, &bufferSize );
  441. mDevice->SetVertexShaderConstantF( start / bytesToFloat4, (float*)buf, bufferSize / bytesToFloat4 );
  442. }
  443. if ( mPixelConstBufferF->isDirty() )
  444. {
  445. buf = mPixelConstBufferF->getDirtyBuffer( &start, &bufferSize );
  446. mDevice->SetPixelShaderConstantF( start / bytesToFloat4, (float*)buf, bufferSize / bytesToFloat4 );
  447. }
  448. if ( mVertexConstBufferI->isDirty() )
  449. {
  450. buf = mVertexConstBufferI->getDirtyBuffer( &start, &bufferSize );
  451. mDevice->SetVertexShaderConstantI( start / bytesToInt4, (int*)buf, bufferSize / bytesToInt4 );
  452. }
  453. if ( mPixelConstBufferI->isDirty() )
  454. {
  455. buf = mPixelConstBufferI->getDirtyBuffer( &start, &bufferSize );
  456. mDevice->SetPixelShaderConstantI( start / bytesToInt4, (int*)buf, bufferSize / bytesToInt4 );
  457. }
  458. #ifdef TORQUE_DEBUG
  459. // Make sure all the constants for this buffer were assigned.
  460. if ( mWasLost )
  461. {
  462. mVertexConstBufferF->assertUnassignedConstants( mShader->getVertexShaderFile().c_str() );
  463. mVertexConstBufferI->assertUnassignedConstants( mShader->getVertexShaderFile().c_str() );
  464. mPixelConstBufferF->assertUnassignedConstants( mShader->getPixelShaderFile().c_str() );
  465. mPixelConstBufferI->assertUnassignedConstants( mShader->getPixelShaderFile().c_str() );
  466. }
  467. #endif
  468. // Clear the lost state.
  469. mWasLost = false;
  470. }
  471. void GFXD3D9ShaderConstBuffer::onShaderReload( GFXD3D9Shader *shader )
  472. {
  473. AssertFatal( shader == mShader, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  474. SAFE_DELETE( mVertexConstBufferF );
  475. SAFE_DELETE( mPixelConstBufferF );
  476. SAFE_DELETE( mVertexConstBufferI );
  477. SAFE_DELETE( mPixelConstBufferI );
  478. AssertFatal( mVertexConstBufferLayoutF == shader->mVertexConstBufferLayoutF, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  479. AssertFatal( mPixelConstBufferLayoutF == shader->mPixelConstBufferLayoutF, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  480. AssertFatal( mVertexConstBufferLayoutI == shader->mVertexConstBufferLayoutI, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  481. AssertFatal( mPixelConstBufferLayoutI == shader->mPixelConstBufferLayoutI, "GFXD3D9ShaderConstBuffer::onShaderReload is hosed!" );
  482. mVertexConstBufferF = new GenericConstBuffer( mVertexConstBufferLayoutF );
  483. mVertexConstBufferI = new GenericConstBuffer( mVertexConstBufferLayoutI );
  484. mPixelConstBufferF = new GenericConstBuffer( mPixelConstBufferLayoutF );
  485. mPixelConstBufferI = new GenericConstBuffer( mPixelConstBufferLayoutI );
  486. // Set the lost state.
  487. mWasLost = true;
  488. }
  489. //------------------------------------------------------------------------------
  490. GFXD3D9Shader::GFXD3D9Shader()
  491. {
  492. VECTOR_SET_ASSOCIATION( mShaderConsts );
  493. mD3D9Device = dynamic_cast<GFXD3D9Device *>(GFX)->getDevice();
  494. AssertFatal(mD3D9Device, "Invalid device for shader.");
  495. mVertShader = NULL;
  496. mPixShader = NULL;
  497. mVertexConstBufferLayoutF = NULL;
  498. mPixelConstBufferLayoutF = NULL;
  499. mVertexConstBufferLayoutI = NULL;
  500. mPixelConstBufferLayoutI = NULL;
  501. if( smD3DXInclude == NULL )
  502. smD3DXInclude = new _gfxD3DXInclude;
  503. }
  504. //------------------------------------------------------------------------------
  505. GFXD3D9Shader::~GFXD3D9Shader()
  506. {
  507. for (HandleMap::Iterator i = mHandles.begin(); i != mHandles.end(); i++)
  508. delete i->value;
  509. SAFE_DELETE(mVertexConstBufferLayoutF);
  510. SAFE_DELETE(mPixelConstBufferLayoutF);
  511. SAFE_DELETE(mVertexConstBufferLayoutI);
  512. SAFE_DELETE(mPixelConstBufferLayoutI);
  513. SAFE_RELEASE(mVertShader);
  514. SAFE_RELEASE(mPixShader);
  515. }
  516. bool GFXD3D9Shader::_init()
  517. {
  518. PROFILE_SCOPE( GFXD3D9Shader_Init );
  519. if ( mPixVersion > GFX->getPixelShaderVersion() )
  520. {
  521. if ( smLogErrors )
  522. Con::errorf( "GFXD3D9Shader::init - Bad pixel shader version!" );
  523. return false;
  524. }
  525. if ( mPixVersion < 1.0f && mPixelFile.getFileName().isNotEmpty() )
  526. {
  527. if ( smLogErrors )
  528. Con::errorf( "GFXD3D9Shader::init - Pixel shaders not supported on SM %.1f!", mPixVersion );
  529. return false;
  530. }
  531. SAFE_RELEASE(mVertShader);
  532. SAFE_RELEASE(mPixShader);
  533. U32 mjVer = (U32)mFloor( mPixVersion );
  534. U32 mnVer = (U32)( ( mPixVersion - F32( mjVer ) ) * 10.01f ); // 10.01 instead of 10.0 because of floating point issues
  535. String vertTarget = String::ToString("vs_%d_%d", mjVer, mnVer);
  536. String pixTarget = String::ToString("ps_%d_%d", mjVer, mnVer);
  537. // Adjust version for vertex shaders
  538. if (mjVer == 2 && mnVer == 1)
  539. {
  540. pixTarget = "ps_2_a";
  541. vertTarget = "vs_2_0";
  542. }
  543. else if ( mjVer == 2 && mnVer == 2 )
  544. {
  545. pixTarget = "ps_2_b";
  546. vertTarget = "vs_2_0";
  547. }
  548. else if ( ( mPixVersion < 2.0f ) && ( mPixVersion > 1.101f ) )
  549. vertTarget = "vs_1_1";
  550. // Create the macro array including the system wide macros.
  551. const U32 macroCount = smGlobalMacros.size() + mMacros.size() + 2;
  552. FrameTemp<D3DXMACRO> d3dXMacros( macroCount );
  553. for ( U32 i=0; i < smGlobalMacros.size(); i++ )
  554. {
  555. d3dXMacros[i].Name = smGlobalMacros[i].name.c_str();
  556. d3dXMacros[i].Definition = smGlobalMacros[i].value.c_str();
  557. }
  558. for ( U32 i=0; i < mMacros.size(); i++ )
  559. {
  560. d3dXMacros[i+smGlobalMacros.size()].Name = mMacros[i].name.c_str();
  561. d3dXMacros[i+smGlobalMacros.size()].Definition = mMacros[i].value.c_str();
  562. }
  563. String smVersion = String::ToString( mjVer * 10 + mnVer );
  564. d3dXMacros[macroCount - 2].Name = "TORQUE_SM";
  565. d3dXMacros[macroCount - 2].Definition = smVersion.c_str();
  566. d3dXMacros[macroCount - 1].Name = NULL;
  567. d3dXMacros[macroCount - 1].Definition = NULL;
  568. if ( !mVertexConstBufferLayoutF )
  569. mVertexConstBufferLayoutF = new GFXD3D9ShaderBufferLayout();
  570. else
  571. mVertexConstBufferLayoutF->clear();
  572. if ( !mVertexConstBufferLayoutI )
  573. mVertexConstBufferLayoutI = new GFXD3D9ShaderBufferLayout();
  574. else
  575. mVertexConstBufferLayoutI->clear();
  576. if ( !mPixelConstBufferLayoutF )
  577. mPixelConstBufferLayoutF = new GFXD3D9ShaderBufferLayout();
  578. else
  579. mPixelConstBufferLayoutF->clear();
  580. if ( !mPixelConstBufferLayoutI )
  581. mPixelConstBufferLayoutI = new GFXD3D9ShaderBufferLayout();
  582. else
  583. mPixelConstBufferLayoutI->clear();
  584. mSamplerDescriptions.clear();
  585. mShaderConsts.clear();
  586. if ( GFXD3DX.isLoaded && !Con::getBoolVariable( "$shaders::forceLoadCSF", false ) )
  587. {
  588. if ( !mVertexFile.isEmpty() &&
  589. !_compileShader( mVertexFile, vertTarget, d3dXMacros, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mSamplerDescriptions ) )
  590. return false;
  591. if ( !mPixelFile.isEmpty() &&
  592. !_compileShader( mPixelFile, pixTarget, d3dXMacros, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI, mSamplerDescriptions ) )
  593. return false;
  594. }
  595. else
  596. {
  597. if ( !_loadCompiledOutput( mVertexFile, vertTarget, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mSamplerDescriptions ) )
  598. {
  599. if ( smLogErrors )
  600. Con::errorf( "GFXD3D9Shader::init - Unable to load precompiled vertex shader for '%s'.",
  601. mVertexFile.getFullPath().c_str() );
  602. return false;
  603. }
  604. if ( !_loadCompiledOutput( mPixelFile, pixTarget, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI, mSamplerDescriptions ) )
  605. {
  606. if ( smLogErrors )
  607. Con::errorf( "GFXD3D9Shader::init - Unable to load precompiled pixel shader for '%s'.",
  608. mPixelFile.getFullPath().c_str() );
  609. return false;
  610. }
  611. }
  612. // Existing handles are resored to an uninitialized state.
  613. // Those that are found when parsing the layout parameters
  614. // will then be re-initialized.
  615. HandleMap::Iterator iter = mHandles.begin();
  616. for ( ; iter != mHandles.end(); iter++ )
  617. (iter->value)->clear();
  618. _buildShaderConstantHandles(mVertexConstBufferLayoutF, true);
  619. _buildShaderConstantHandles(mVertexConstBufferLayoutI, true);
  620. _buildShaderConstantHandles(mPixelConstBufferLayoutF, false);
  621. _buildShaderConstantHandles(mPixelConstBufferLayoutI, false);
  622. _buildSamplerShaderConstantHandles( mSamplerDescriptions );
  623. _buildInstancingShaderConstantHandles();
  624. // Notify any existing buffers that the buffer
  625. // layouts have changed and they need to update.
  626. Vector<GFXShaderConstBuffer*>::iterator biter = mActiveBuffers.begin();
  627. for ( ; biter != mActiveBuffers.end(); biter++ )
  628. ((GFXD3D9ShaderConstBuffer*)(*biter))->onShaderReload( this );
  629. return true;
  630. }
  631. bool GFXD3D9Shader::_compileShader( const Torque::Path &filePath,
  632. const String& target,
  633. const D3DXMACRO *defines,
  634. GenericConstBufferLayout* bufferLayoutF,
  635. GenericConstBufferLayout* bufferLayoutI,
  636. Vector<GFXShaderConstDesc> &samplerDescriptions )
  637. {
  638. PROFILE_SCOPE( GFXD3D9Shader_CompileShader );
  639. HRESULT res = D3DERR_INVALIDCALL;
  640. LPD3DXBUFFER code = NULL;
  641. LPD3DXBUFFER errorBuff = NULL;
  642. #ifdef TORQUE_DEBUG
  643. U32 flags = D3DXSHADER_DEBUG;
  644. #else
  645. U32 flags = 0;
  646. #endif
  647. #ifdef TORQUE_OS_XENON
  648. flags |= D3DXSHADER_PREFER_FLOW_CONTROL;
  649. #endif
  650. #ifdef D3DXSHADER_USE_LEGACY_D3DX9_31_DLL
  651. if( D3DX_SDK_VERSION >= 32 )
  652. {
  653. // will need to use old compiler for 1_1 shaders - check for pixel
  654. // or vertex shader with appropriate version.
  655. if ((target.compare("vs1", 3) == 0) || (target.compare("vs_1", 4) == 0))
  656. flags |= D3DXSHADER_USE_LEGACY_D3DX9_31_DLL;
  657. if ((target.compare("ps1", 3) == 0) || (target.compare("ps_1", 4) == 0))
  658. flags |= D3DXSHADER_USE_LEGACY_D3DX9_31_DLL;
  659. }
  660. #endif
  661. #if !defined(TORQUE_OS_XENON) && (D3DX_SDK_VERSION <= 40)
  662. #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
  663. #endif
  664. ID3DXConstantTable* table = NULL;
  665. static String sHLSLStr( "hlsl" );
  666. static String sOBJStr( "obj" );
  667. // Is it an HLSL shader?
  668. if ( filePath.getExtension().equal(sHLSLStr, String::NoCase) )
  669. {
  670. FrameAllocatorMarker fam;
  671. char *buffer = NULL;
  672. // Set this so that the D3DXInclude::Open will have this
  673. // information for relative paths.
  674. smD3DXInclude->setPath( filePath.getRootAndPath() );
  675. FileStream s;
  676. if ( !s.open( filePath, Torque::FS::File::Read ) )
  677. {
  678. AssertISV(false, avar("GFXD3D9Shader::initShader - failed to open shader '%s'.", filePath.getFullPath().c_str()));
  679. if ( smLogErrors )
  680. Con::errorf( "GFXD3D9Shader::_compileShader - Failed to open shader file '%s'.",
  681. filePath.getFullPath().c_str() );
  682. return false;
  683. }
  684. // Convert the path which might have virtualized
  685. // mount paths to a real file system path.
  686. Torque::Path realPath;
  687. if ( !FS::GetFSPath( filePath, realPath ) )
  688. realPath = filePath;
  689. // Add a #line pragma so that error and warning messages
  690. // returned by the HLSL compiler report the right file.
  691. String linePragma = String::ToString( "#line 1 \"%s\"\r\n", realPath.getFullPath().c_str() );
  692. U32 linePragmaLen = linePragma.length();
  693. U32 bufSize = s.getStreamSize();
  694. buffer = (char *)fam.alloc( bufSize + linePragmaLen + 1 );
  695. dStrncpy( buffer, linePragma.c_str(), linePragmaLen );
  696. s.read( bufSize, buffer + linePragmaLen );
  697. buffer[bufSize+linePragmaLen] = 0;
  698. res = GFXD3DX.D3DXCompileShader( buffer, bufSize + linePragmaLen, defines, smD3DXInclude, "main",
  699. target, flags, &code, &errorBuff, &table );
  700. }
  701. // Is it a precompiled obj shader?
  702. else if ( filePath.getExtension().equal( sOBJStr, String::NoCase ) )
  703. {
  704. FileStream s;
  705. if(!s.open(filePath, Torque::FS::File::Read))
  706. {
  707. AssertISV(false, avar("GFXD3D9Shader::initShader - failed to open shader '%s'.", filePath.getFullPath().c_str()));
  708. if ( smLogErrors )
  709. Con::errorf( "GFXD3D9Shader::_compileShader - Failed to open shader file '%s'.",
  710. filePath.getFullPath().c_str() );
  711. return false;
  712. }
  713. res = GFXD3DX.D3DXCreateBuffer(s.getStreamSize(), &code);
  714. AssertISV(res == D3D_OK, "Unable to create buffer!");
  715. s.read(s.getStreamSize(), code->GetBufferPointer());
  716. if (res == D3D_OK)
  717. {
  718. DWORD* data = (DWORD*) code->GetBufferPointer();
  719. res = GFXD3DX.D3DXGetShaderConstantTable(data, &table);
  720. }
  721. }
  722. else
  723. {
  724. if ( smLogErrors )
  725. Con::errorf( "GFXD3D9Shader::_compileShader - Unsupported shader file type '%s'.",
  726. filePath.getFullPath().c_str() );
  727. return false;
  728. }
  729. if ( res != D3D_OK && smLogErrors )
  730. Con::errorf( "GFXD3D9Shader::_compileShader - Error compiling shader: %s: %s (%x)",
  731. DXGetErrorStringA(res), DXGetErrorDescriptionA(res), res );
  732. if ( errorBuff )
  733. {
  734. // remove \n at end of buffer
  735. U8 *buffPtr = (U8*) errorBuff->GetBufferPointer();
  736. U32 len = dStrlen( (const char*) buffPtr );
  737. buffPtr[len-1] = '\0';
  738. if( res != D3D_OK )
  739. {
  740. if ( smLogErrors )
  741. Con::errorf( " %s", (const char*) errorBuff->GetBufferPointer() );
  742. }
  743. else
  744. {
  745. if ( smLogWarnings )
  746. Con::warnf( "%s", (const char*) errorBuff->GetBufferPointer() );
  747. }
  748. }
  749. else if ( code == NULL && smLogErrors )
  750. Con::errorf( "GFXD3D9Shader::_compileShader - no compiled code produced; possibly missing file '%s'.",
  751. filePath.getFullPath().c_str() );
  752. // Create the proper shader if we have code
  753. if( code != NULL )
  754. {
  755. #ifndef TORQUE_SHIPPING
  756. LPD3DXBUFFER disassem = NULL;
  757. D3DXDisassembleShader( (DWORD*)code->GetBufferPointer(), false, NULL, &disassem );
  758. mDissasembly = (const char*)disassem->GetBufferPointer();
  759. SAFE_RELEASE( disassem );
  760. if ( gDisassembleAllShaders )
  761. {
  762. String filename = filePath.getFullPath();
  763. filename.replace( ".hlsl", "_dis.txt" );
  764. FileStream *fstream = FileStream::createAndOpen( filename, Torque::FS::File::Write );
  765. if ( fstream )
  766. {
  767. fstream->write( mDissasembly );
  768. fstream->close();
  769. delete fstream;
  770. }
  771. }
  772. #endif
  773. if (target.compare("ps_", 3) == 0)
  774. res = mD3D9Device->CreatePixelShader( (DWORD*)code->GetBufferPointer(), &mPixShader );
  775. else
  776. res = mD3D9Device->CreateVertexShader( (DWORD*)code->GetBufferPointer(), &mVertShader );
  777. if (res == S_OK)
  778. _getShaderConstants(table, bufferLayoutF, bufferLayoutI, samplerDescriptions);
  779. #ifdef TORQUE_ENABLE_CSF_GENERATION
  780. // Ok, we've got a valid shader and constants, let's write them all out.
  781. if ( !_saveCompiledOutput(filePath, code, bufferLayoutF, bufferLayoutI) && smLogErrors )
  782. Con::errorf( "GFXD3D9Shader::_compileShader - Unable to save shader compile output for: %s",
  783. filePath.getFullPath().c_str() );
  784. #endif
  785. SAFE_RELEASE(table);
  786. if ( res != S_OK && smLogErrors )
  787. Con::errorf( "GFXD3D9Shader::_compileShader - Unable to create shader for '%s'.",
  788. filePath.getFullPath().c_str() );
  789. }
  790. bool result = code != NULL && res == S_OK;
  791. SAFE_RELEASE( code );
  792. SAFE_RELEASE( errorBuff );
  793. return result;
  794. }
  795. void GFXD3D9Shader::_getShaderConstants( ID3DXConstantTable *table,
  796. GenericConstBufferLayout *bufferLayoutF,
  797. GenericConstBufferLayout* bufferLayoutI,
  798. Vector<GFXShaderConstDesc> &samplerDescriptions )
  799. {
  800. PROFILE_SCOPE( GFXD3D9Shader_GetShaderConstants );
  801. AssertFatal(table, "NULL constant table not allowed, is this an assembly shader?");
  802. D3DXCONSTANTTABLE_DESC tableDesc;
  803. D3D9Assert(table->GetDesc(&tableDesc), "Unable to get constant table info.");
  804. for (U32 i = 0; i < tableDesc.Constants; i++)
  805. {
  806. D3DXHANDLE handle = table->GetConstant(0, i);
  807. const U32 descSize=16;
  808. D3DXCONSTANT_DESC constantDescArray[descSize];
  809. U32 size = descSize;
  810. if (table->GetConstantDesc(handle, constantDescArray, &size) == S_OK)
  811. {
  812. D3DXCONSTANT_DESC& constantDesc = constantDescArray[0];
  813. GFXShaderConstDesc desc;
  814. desc.name = String(constantDesc.Name);
  815. // Prepend a "$" if it doesn't exist. Just to make things consistent.
  816. if (desc.name.find("$") != 0)
  817. desc.name = String::ToString("$%s", desc.name.c_str());
  818. //Con::printf("name %s: , offset: %d, size: %d, constantDesc.Elements: %d", desc.name.c_str(), constantDesc.RegisterIndex, constantDesc.Bytes, constantDesc.Elements);
  819. desc.arraySize = constantDesc.Elements;
  820. GenericConstBufferLayout* bufferLayout = NULL;
  821. switch (constantDesc.RegisterSet)
  822. {
  823. case D3DXRS_INT4 :
  824. {
  825. bufferLayout = bufferLayoutI;
  826. switch (constantDesc.Class)
  827. {
  828. case D3DXPC_SCALAR :
  829. desc.constType = GFXSCT_Int;
  830. break;
  831. case D3DXPC_VECTOR :
  832. {
  833. switch (constantDesc.Columns)
  834. {
  835. case 1 :
  836. desc.constType = GFXSCT_Int;
  837. break;
  838. case 2 :
  839. desc.constType = GFXSCT_Int2;
  840. break;
  841. case 3 :
  842. desc.constType = GFXSCT_Int3;
  843. break;
  844. case 4 :
  845. desc.constType = GFXSCT_Int4;
  846. break;
  847. default:
  848. AssertFatal(false, "Unknown int vector type!");
  849. break;
  850. }
  851. }
  852. break;
  853. }
  854. desc.constType = GFXSCT_Int4;
  855. break;
  856. }
  857. case D3DXRS_FLOAT4 :
  858. {
  859. bufferLayout = bufferLayoutF;
  860. switch (constantDesc.Class)
  861. {
  862. case D3DXPC_SCALAR:
  863. desc.constType = GFXSCT_Float;
  864. break;
  865. case D3DXPC_VECTOR :
  866. {
  867. switch (constantDesc.Columns)
  868. {
  869. case 1 :
  870. desc.constType = GFXSCT_Float;
  871. break;
  872. case 2 :
  873. desc.constType = GFXSCT_Float2;
  874. break;
  875. case 3 :
  876. desc.constType = GFXSCT_Float3;
  877. break;
  878. case 4 :
  879. desc.constType = GFXSCT_Float4;
  880. break;
  881. default:
  882. AssertFatal(false, "Unknown float vector type!");
  883. break;
  884. }
  885. }
  886. break;
  887. case D3DXPC_MATRIX_ROWS :
  888. case D3DXPC_MATRIX_COLUMNS :
  889. {
  890. switch (constantDesc.RegisterCount)
  891. {
  892. case 3 :
  893. desc.constType = GFXSCT_Float3x3;
  894. break;
  895. case 4 :
  896. desc.constType = GFXSCT_Float4x4;
  897. break;
  898. }
  899. }
  900. break;
  901. case D3DXPC_OBJECT :
  902. case D3DXPC_STRUCT :
  903. bufferLayout = NULL;
  904. break;
  905. }
  906. }
  907. break;
  908. case D3DXRS_SAMPLER :
  909. {
  910. AssertFatal( constantDesc.Elements == 1, "Sampler Arrays not yet supported!" );
  911. switch (constantDesc.Type)
  912. {
  913. case D3DXPT_SAMPLER :
  914. case D3DXPT_SAMPLER1D :
  915. case D3DXPT_SAMPLER2D :
  916. case D3DXPT_SAMPLER3D :
  917. // Hi-jack the desc's arraySize to store the registerIndex.
  918. desc.constType = GFXSCT_Sampler;
  919. desc.arraySize = constantDesc.RegisterIndex;
  920. samplerDescriptions.push_back( desc );
  921. break;
  922. case D3DXPT_SAMPLERCUBE :
  923. desc.constType = GFXSCT_SamplerCube;
  924. desc.arraySize = constantDesc.RegisterIndex;
  925. samplerDescriptions.push_back( desc );
  926. break;
  927. }
  928. }
  929. break;
  930. default:
  931. AssertFatal(false, "Unknown shader constant class enum");
  932. break;
  933. }
  934. if (bufferLayout)
  935. {
  936. mShaderConsts.push_back(desc);
  937. U32 alignBytes = getAlignmentValue(desc.constType);
  938. U32 paramSize = alignBytes * desc.arraySize;
  939. bufferLayout->addParameter( desc.name,
  940. desc.constType,
  941. constantDesc.RegisterIndex * sizeof(Point4F),
  942. paramSize,
  943. desc.arraySize,
  944. alignBytes );
  945. }
  946. }
  947. else
  948. AssertFatal(false, "Unable to get shader constant description! (may need more elements of constantDesc");
  949. }
  950. }
  951. const U32 GFXD3D9Shader::smCompiledShaderTag = MakeFourCC('t','c','s','f');
  952. bool GFXD3D9Shader::_saveCompiledOutput( const Torque::Path &filePath,
  953. LPD3DXBUFFER buffer,
  954. GenericConstBufferLayout *bufferLayoutF,
  955. GenericConstBufferLayout *bufferLayoutI,
  956. Vector<GFXShaderConstDesc> &samplerDescriptions )
  957. {
  958. Torque::Path outputPath(filePath);
  959. outputPath.setExtension("csf"); // "C"ompiled "S"hader "F"ile (fancy!)
  960. FileStream f;
  961. if (!f.open(outputPath, Torque::FS::File::Write))
  962. return false;
  963. if (!f.write(smCompiledShaderTag))
  964. return false;
  965. // We could reverse engineer the structure in the compiled output, but this
  966. // is a bit easier because we can just read it into the struct that we want.
  967. if (!bufferLayoutF->write(&f))
  968. return false;
  969. if (!bufferLayoutI->write(&f))
  970. return false;
  971. U32 bufferSize = buffer->GetBufferSize();
  972. if (!f.write(bufferSize))
  973. return false;
  974. if (!f.write(bufferSize, buffer->GetBufferPointer()))
  975. return false;
  976. // Write out sampler descriptions.
  977. f.write( samplerDescriptions.size() );
  978. for ( U32 i = 0; i < samplerDescriptions.size(); i++ )
  979. {
  980. f.write( samplerDescriptions[i].name );
  981. f.write( (U32)(samplerDescriptions[i].constType) );
  982. f.write( samplerDescriptions[i].arraySize );
  983. }
  984. f.close();
  985. return true;
  986. }
  987. bool GFXD3D9Shader::_loadCompiledOutput( const Torque::Path &filePath,
  988. const String &target,
  989. GenericConstBufferLayout *bufferLayoutF,
  990. GenericConstBufferLayout *bufferLayoutI,
  991. Vector<GFXShaderConstDesc> &samplerDescriptions )
  992. {
  993. Torque::Path outputPath(filePath);
  994. outputPath.setExtension("csf"); // "C"ompiled "S"hader "F"ile (fancy!)
  995. FileStream f;
  996. if (!f.open(outputPath, Torque::FS::File::Read))
  997. return false;
  998. U32 fileTag;
  999. if (!f.read(&fileTag))
  1000. return false;
  1001. if (fileTag != smCompiledShaderTag)
  1002. return false;
  1003. if (!bufferLayoutF->read(&f))
  1004. return false;
  1005. if (!bufferLayoutI->read(&f))
  1006. return false;
  1007. U32 bufferSize;
  1008. if (!f.read(&bufferSize))
  1009. return false;
  1010. U32 waterMark = FrameAllocator::getWaterMark();
  1011. DWORD* buffer = static_cast<DWORD*>(FrameAllocator::alloc(bufferSize));
  1012. if (!f.read(bufferSize, buffer))
  1013. return false;
  1014. // Read sampler descriptions.
  1015. U32 samplerCount;
  1016. f.read( &samplerCount );
  1017. for ( U32 i = 0; i < samplerCount; i++ )
  1018. {
  1019. GFXShaderConstDesc samplerDesc;
  1020. f.read( &(samplerDesc.name) );
  1021. f.read( (U32*)&(samplerDesc.constType) );
  1022. f.read( &(samplerDesc.arraySize) );
  1023. samplerDescriptions.push_back( samplerDesc );
  1024. }
  1025. f.close();
  1026. HRESULT res;
  1027. if (target.compare("ps_", 3) == 0)
  1028. res = mD3D9Device->CreatePixelShader(buffer, &mPixShader );
  1029. else
  1030. res = mD3D9Device->CreateVertexShader(buffer, &mVertShader );
  1031. AssertFatal(SUCCEEDED(res), "Unable to load shader!");
  1032. FrameAllocator::setWaterMark(waterMark);
  1033. return SUCCEEDED(res);
  1034. }
  1035. void GFXD3D9Shader::_buildShaderConstantHandles(GenericConstBufferLayout* layout, bool vertexConst)
  1036. {
  1037. for (U32 i = 0; i < layout->getParameterCount(); i++)
  1038. {
  1039. GenericConstBufferLayout::ParamDesc pd;
  1040. layout->getDesc(i, pd);
  1041. GFXD3D9ShaderConstHandle* handle;
  1042. HandleMap::Iterator j = mHandles.find(pd.name);
  1043. if (j != mHandles.end())
  1044. {
  1045. handle = j->value;
  1046. handle->mShader = this;
  1047. handle->setValid( true );
  1048. }
  1049. else
  1050. {
  1051. handle = new GFXD3D9ShaderConstHandle();
  1052. handle->mShader = this;
  1053. mHandles[pd.name] = handle;
  1054. handle->setValid( true );
  1055. }
  1056. if ( vertexConst )
  1057. {
  1058. handle->mVertexConstant = true;
  1059. handle->mVertexHandle = pd;
  1060. }
  1061. else
  1062. {
  1063. handle->mPixelConstant = true;
  1064. handle->mPixelHandle = pd;
  1065. }
  1066. }
  1067. }
  1068. void GFXD3D9Shader::_buildSamplerShaderConstantHandles( Vector<GFXShaderConstDesc> &samplerDescriptions )
  1069. {
  1070. Vector<GFXShaderConstDesc>::iterator iter = samplerDescriptions.begin();
  1071. for ( ; iter != samplerDescriptions.end(); iter++ )
  1072. {
  1073. const GFXShaderConstDesc &desc = *iter;
  1074. AssertFatal( desc.constType == GFXSCT_Sampler ||
  1075. desc.constType == GFXSCT_SamplerCube,
  1076. "GFXD3D9Shader::_buildSamplerShaderConstantHandles - Invalid samplerDescription type!" );
  1077. GFXD3D9ShaderConstHandle *handle;
  1078. HandleMap::Iterator j = mHandles.find(desc.name);
  1079. if ( j != mHandles.end() )
  1080. handle = j->value;
  1081. else
  1082. {
  1083. handle = new GFXD3D9ShaderConstHandle();
  1084. mHandles[desc.name] = handle;
  1085. }
  1086. handle->mShader = this;
  1087. handle->setValid( true );
  1088. handle->mPixelConstant = true;
  1089. handle->mPixelHandle.name = desc.name;
  1090. handle->mPixelHandle.constType = desc.constType;
  1091. handle->mPixelHandle.offset = desc.arraySize;
  1092. }
  1093. }
  1094. void GFXD3D9Shader::_buildInstancingShaderConstantHandles()
  1095. {
  1096. U32 offset = 0;
  1097. for ( U32 i=0; i < mInstancingFormat.getElementCount(); i++ )
  1098. {
  1099. const GFXVertexElement &element = mInstancingFormat.getElement( i );
  1100. String constName = String::ToString( "$%s", element.getSemantic().c_str() );
  1101. GFXD3D9ShaderConstHandle *handle;
  1102. HandleMap::Iterator j = mHandles.find( constName );
  1103. if ( j != mHandles.end() )
  1104. handle = j->value;
  1105. else
  1106. {
  1107. handle = new GFXD3D9ShaderConstHandle();
  1108. mHandles[ constName ] = handle;
  1109. }
  1110. handle->mShader = this;
  1111. handle->setValid( true );
  1112. handle->mInstancingConstant = true;
  1113. // We shouldn't have an instancing constant that is also
  1114. // a vertex or pixel constant! This means the shader features
  1115. // are confused as to what is instanced.
  1116. //
  1117. AssertFatal( !handle->mVertexConstant &&
  1118. !handle->mPixelConstant,
  1119. "GFXD3D9Shader::_buildInstancingShaderConstantHandles - Bad instanced constant!" );
  1120. // HACK: The GFXD3D9ShaderConstHandle will check mVertexConstant then
  1121. // fall back to reading the mPixelHandle values. We depend on this here
  1122. // and store the data we need in the mPixelHandle constant although its
  1123. // not a pixel shader constant.
  1124. //
  1125. handle->mPixelHandle.name = constName;
  1126. handle->mPixelHandle.offset = offset;
  1127. // If this is a matrix we will have 2 or 3 more of these
  1128. // semantics with the same name after it.
  1129. for ( ; i < mInstancingFormat.getElementCount(); i++ )
  1130. {
  1131. const GFXVertexElement &nextElement = mInstancingFormat.getElement( i );
  1132. if ( nextElement.getSemantic() != element.getSemantic() )
  1133. {
  1134. i--;
  1135. break;
  1136. }
  1137. offset += nextElement.getSizeInBytes();
  1138. }
  1139. }
  1140. }
  1141. GFXShaderConstBufferRef GFXD3D9Shader::allocConstBuffer()
  1142. {
  1143. if (mVertexConstBufferLayoutF && mPixelConstBufferLayoutF)
  1144. {
  1145. GFXD3D9ShaderConstBuffer* buffer = new GFXD3D9ShaderConstBuffer(this, mVertexConstBufferLayoutF, mVertexConstBufferLayoutI, mPixelConstBufferLayoutF, mPixelConstBufferLayoutI);
  1146. mActiveBuffers.push_back( buffer );
  1147. buffer->registerResourceWithDevice(getOwningDevice());
  1148. return buffer;
  1149. } else {
  1150. return NULL;
  1151. }
  1152. }
  1153. /// Returns a shader constant handle for name, if the variable doesn't exist NULL is returned.
  1154. GFXShaderConstHandle* GFXD3D9Shader::getShaderConstHandle(const String& name)
  1155. {
  1156. HandleMap::Iterator i = mHandles.find(name);
  1157. if ( i != mHandles.end() )
  1158. {
  1159. return i->value;
  1160. }
  1161. else
  1162. {
  1163. GFXD3D9ShaderConstHandle *handle = new GFXD3D9ShaderConstHandle();
  1164. handle->setValid( false );
  1165. handle->mShader = this;
  1166. mHandles[name] = handle;
  1167. return handle;
  1168. }
  1169. }
  1170. const Vector<GFXShaderConstDesc>& GFXD3D9Shader::getShaderConstDesc() const
  1171. {
  1172. return mShaderConsts;
  1173. }
  1174. U32 GFXD3D9Shader::getAlignmentValue(const GFXShaderConstType constType) const
  1175. {
  1176. const U32 mRowSizeF = 16;
  1177. const U32 mRowSizeI = 16;
  1178. switch (constType)
  1179. {
  1180. case GFXSCT_Float :
  1181. case GFXSCT_Float2 :
  1182. case GFXSCT_Float3 :
  1183. case GFXSCT_Float4 :
  1184. return mRowSizeF;
  1185. break;
  1186. // Matrices
  1187. case GFXSCT_Float2x2 :
  1188. return mRowSizeF * 2;
  1189. break;
  1190. case GFXSCT_Float3x3 :
  1191. return mRowSizeF * 3;
  1192. break;
  1193. case GFXSCT_Float4x4 :
  1194. return mRowSizeF * 4;
  1195. break;
  1196. //// Scalar
  1197. case GFXSCT_Int :
  1198. case GFXSCT_Int2 :
  1199. case GFXSCT_Int3 :
  1200. case GFXSCT_Int4 :
  1201. return mRowSizeI;
  1202. break;
  1203. default:
  1204. AssertFatal(false, "Unsupported type!");
  1205. return 0;
  1206. break;
  1207. }
  1208. }
  1209. void GFXD3D9Shader::zombify()
  1210. {
  1211. // Shaders don't need zombification
  1212. }
  1213. void GFXD3D9Shader::resurrect()
  1214. {
  1215. // Shaders are never zombies, and therefore don't have to be brought back.
  1216. }