gfxD3D9Shader.cpp 49 KB

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